fsck.c revision 2c82eafaa38922a370545dc56d6210bf94b929e9
1/**
2 * fsck.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
13char *tree_mark;
14uint32_t tree_mark_size = 256;
15
16static inline int f2fs_set_main_bitmap(struct f2fs_sb_info *sbi, u32 blk)
17{
18	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
19
20	return f2fs_set_bit(BLKOFF_FROM_MAIN(sbi, blk), fsck->main_area_bitmap);
21}
22
23static inline int f2fs_test_main_bitmap(struct f2fs_sb_info *sbi, u32 blk)
24{
25	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
26
27	return f2fs_test_bit(BLKOFF_FROM_MAIN(sbi, blk),
28						fsck->main_area_bitmap);
29}
30
31static inline int f2fs_test_sit_bitmap(struct f2fs_sb_info *sbi, u32 blk)
32{
33	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
34
35	return f2fs_test_bit(BLKOFF_FROM_MAIN(sbi, blk), fsck->sit_area_bitmap);
36}
37
38static int add_into_hard_link_list(struct f2fs_sb_info *sbi,
39						u32 nid, u32 link_cnt)
40{
41	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
42	struct hard_link_node *node = NULL, *tmp = NULL, *prev = NULL;
43
44	node = calloc(sizeof(struct hard_link_node), 1);
45	ASSERT(node != NULL);
46
47	node->nid = nid;
48	node->links = link_cnt;
49	node->next = NULL;
50
51	if (fsck->hard_link_list_head == NULL) {
52		fsck->hard_link_list_head = node;
53		goto out;
54	}
55
56	tmp = fsck->hard_link_list_head;
57
58	/* Find insertion position */
59	while (tmp && (nid < tmp->nid)) {
60		ASSERT(tmp->nid != nid);
61		prev = tmp;
62		tmp = tmp->next;
63	}
64
65	if (tmp == fsck->hard_link_list_head) {
66		node->next = tmp;
67		fsck->hard_link_list_head = node;
68	} else {
69		prev->next = node;
70		node->next = tmp;
71	}
72
73out:
74	DBG(2, "ino[0x%x] has hard links [0x%x]\n", nid, link_cnt);
75	return 0;
76}
77
78static int find_and_dec_hard_link_list(struct f2fs_sb_info *sbi, u32 nid)
79{
80	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
81	struct hard_link_node *node = NULL, *prev = NULL;
82
83	if (fsck->hard_link_list_head == NULL)
84		return -EINVAL;
85
86	node = fsck->hard_link_list_head;
87
88	while (node && (nid < node->nid)) {
89		prev = node;
90		node = node->next;
91	}
92
93	if (node == NULL || (nid != node->nid))
94		return -EINVAL;
95
96	/* Decrease link count */
97	node->links = node->links - 1;
98
99	/* if link count becomes one, remove the node */
100	if (node->links == 1) {
101		if (fsck->hard_link_list_head == node)
102			fsck->hard_link_list_head = node->next;
103		else
104			prev->next = node->next;
105		free(node);
106	}
107	return 0;
108}
109
110static int is_valid_ssa_node_blk(struct f2fs_sb_info *sbi, u32 nid,
111							u32 blk_addr)
112{
113	int ret = 0;
114	struct f2fs_summary sum_entry;
115
116	ret = get_sum_entry(sbi, blk_addr, &sum_entry);
117
118	if (ret != SEG_TYPE_NODE && ret != SEG_TYPE_CUR_NODE) {
119		ASSERT_MSG("Summary footer is not for node segment");
120		return -EINVAL;
121	}
122
123	if (le32_to_cpu(sum_entry.nid) != nid) {
124		DBG(0, "nid                       [0x%x]\n", nid);
125		DBG(0, "target blk_addr           [0x%x]\n", blk_addr);
126		DBG(0, "summary blk_addr          [0x%x]\n",
127					GET_SUM_BLKADDR(sbi,
128					GET_SEGNO(sbi, blk_addr)));
129		DBG(0, "seg no / offset           [0x%x / 0x%x]\n",
130					GET_SEGNO(sbi, blk_addr),
131					OFFSET_IN_SEG(sbi, blk_addr));
132		DBG(0, "summary_entry.nid         [0x%x]\n",
133					le32_to_cpu(sum_entry.nid));
134		DBG(0, "--> node block's nid      [0x%x]\n", nid);
135		ASSERT_MSG("Invalid node seg summary\n");
136		return -EINVAL;
137	}
138	return 0;
139}
140
141static int is_valid_ssa_data_blk(struct f2fs_sb_info *sbi, u32 blk_addr,
142		u32 parent_nid, u16 idx_in_node, u8 version)
143{
144	int ret = 0;
145	struct f2fs_summary sum_entry;
146
147	ret = get_sum_entry(sbi, blk_addr, &sum_entry);
148
149	if (ret != SEG_TYPE_DATA && ret != SEG_TYPE_CUR_DATA) {
150		ASSERT_MSG("Summary footer is not for data segment");
151		return -EINVAL;
152	}
153
154	if (le32_to_cpu(sum_entry.nid) != parent_nid ||
155			sum_entry.version != version ||
156			le16_to_cpu(sum_entry.ofs_in_node) != idx_in_node) {
157
158		DBG(0, "summary_entry.nid         [0x%x]\n",
159					le32_to_cpu(sum_entry.nid));
160		DBG(0, "summary_entry.version     [0x%x]\n",
161					sum_entry.version);
162		DBG(0, "summary_entry.ofs_in_node [0x%x]\n",
163					le16_to_cpu(sum_entry.ofs_in_node));
164		DBG(0, "parent nid                [0x%x]\n", parent_nid);
165		DBG(0, "version from nat          [0x%x]\n", version);
166		DBG(0, "idx in parent node        [0x%x]\n", idx_in_node);
167
168		DBG(0, "Target data block addr    [0x%x]\n", blk_addr);
169		ASSERT_MSG("Invalid data seg summary\n");
170		return -EINVAL;
171	}
172	return 0;
173}
174
175static int sanity_check_nid(struct f2fs_sb_info *sbi, u32 nid,
176			struct f2fs_node *node_blk,
177			enum FILE_TYPE ftype, enum NODE_TYPE ntype,
178			struct node_info *ni)
179{
180	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
181	int ret;
182
183	if (!IS_VALID_NID(sbi, nid)) {
184		ASSERT_MSG("nid is not valid. [0x%x]", nid);
185		return -EINVAL;
186	}
187
188	get_node_info(sbi, nid, ni);
189	if (ni->blk_addr == NEW_ADDR) {
190		ASSERT_MSG("nid is NEW_ADDR. [0x%x]", nid);
191		return -EINVAL;
192	}
193
194	if (!IS_VALID_BLK_ADDR(sbi, ni->blk_addr)) {
195		ASSERT_MSG("blkaddres is not valid. [0x%x]", ni->blk_addr);
196		return -EINVAL;
197	}
198
199	if (is_valid_ssa_node_blk(sbi, nid, ni->blk_addr)) {
200		ASSERT_MSG("summary node block is not valid. [0x%x]", nid);
201		return -EINVAL;
202	}
203
204	ret = dev_read_block(node_blk, ni->blk_addr);
205	ASSERT(ret >= 0);
206
207	if (ntype == TYPE_INODE &&
208			node_blk->footer.nid != node_blk->footer.ino) {
209		ASSERT_MSG("nid[0x%x] footer.nid[0x%x] footer.ino[0x%x]",
210				nid, le32_to_cpu(node_blk->footer.nid),
211				le32_to_cpu(node_blk->footer.ino));
212		return -EINVAL;
213	}
214	if (ntype != TYPE_INODE &&
215			node_blk->footer.nid == node_blk->footer.ino) {
216		ASSERT_MSG("nid[0x%x] footer.nid[0x%x] footer.ino[0x%x]",
217				nid, le32_to_cpu(node_blk->footer.nid),
218				le32_to_cpu(node_blk->footer.ino));
219		return -EINVAL;
220	}
221
222	if (le32_to_cpu(node_blk->footer.nid) != nid) {
223		ASSERT_MSG("nid[0x%x] blk_addr[0x%x] footer.nid[0x%x]",
224				nid, ni->blk_addr,
225				le32_to_cpu(node_blk->footer.nid));
226		return -EINVAL;
227	}
228
229	if (ntype == TYPE_XATTR) {
230		u32 flag = le32_to_cpu(node_blk->footer.flag);
231
232		if ((flag >> OFFSET_BIT_SHIFT) != XATTR_NODE_OFFSET) {
233			ASSERT_MSG("xnid[0x%x] has wrong ofs:[0x%x]",
234					nid, flag);
235			return -EINVAL;
236		}
237	}
238
239	if ((ntype == TYPE_INODE && ftype == F2FS_FT_DIR) ||
240			(ntype == TYPE_XATTR && ftype == F2FS_FT_XATTR)) {
241		/* not included '.' & '..' */
242		if (f2fs_test_main_bitmap(sbi, ni->blk_addr) != 0) {
243			ASSERT_MSG("Duplicated node blk. nid[0x%x][0x%x]\n",
244					nid, ni->blk_addr);
245			return -EINVAL;
246		}
247	}
248
249	/* workaround to fix later */
250	if (ftype != F2FS_FT_ORPHAN ||
251			f2fs_test_bit(nid, fsck->nat_area_bitmap) != 0)
252		f2fs_clear_bit(nid, fsck->nat_area_bitmap);
253	else
254		ASSERT_MSG("orphan or xattr nid is duplicated [0x%x]\n",
255				nid);
256
257	if (f2fs_test_sit_bitmap(sbi, ni->blk_addr) == 0)
258		ASSERT_MSG("SIT bitmap is 0x0. blk_addr[0x%x]",
259				ni->blk_addr);
260
261	if (f2fs_test_main_bitmap(sbi, ni->blk_addr) == 0) {
262		fsck->chk.valid_blk_cnt++;
263		fsck->chk.valid_node_cnt++;
264	}
265	return 0;
266}
267
268static int fsck_chk_xattr_blk(struct f2fs_sb_info *sbi, u32 ino,
269					u32 x_nid, u32 *blk_cnt)
270{
271	struct f2fs_node *node_blk = NULL;
272	struct node_info ni;
273	int ret = 0;
274
275	if (x_nid == 0x0)
276		return 0;
277
278	node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
279	ASSERT(node_blk != NULL);
280
281	/* Sanity check */
282	if (sanity_check_nid(sbi, x_nid, node_blk,
283				F2FS_FT_XATTR, TYPE_XATTR, &ni)) {
284		ret = -EINVAL;
285		goto out;
286	}
287
288	*blk_cnt = *blk_cnt + 1;
289	f2fs_set_main_bitmap(sbi, ni.blk_addr);
290	DBG(2, "ino[0x%x] x_nid[0x%x]\n", ino, x_nid);
291out:
292	free(node_blk);
293	return ret;
294}
295
296int fsck_chk_node_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
297		u32 nid, enum FILE_TYPE ftype, enum NODE_TYPE ntype,
298		u32 *blk_cnt)
299{
300	struct node_info ni;
301	struct f2fs_node *node_blk = NULL;
302
303	node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
304	ASSERT(node_blk != NULL);
305
306	if (sanity_check_nid(sbi, nid, node_blk, ftype, ntype, &ni))
307		goto err;
308
309	if (ntype == TYPE_INODE) {
310		fsck_chk_inode_blk(sbi, nid, ftype, node_blk, blk_cnt, &ni);
311	} else {
312		f2fs_set_main_bitmap(sbi, ni.blk_addr);
313
314		switch (ntype) {
315		case TYPE_DIRECT_NODE:
316			fsck_chk_dnode_blk(sbi, inode, nid, ftype, node_blk,
317					blk_cnt, &ni);
318			break;
319		case TYPE_INDIRECT_NODE:
320			fsck_chk_idnode_blk(sbi, inode, ftype, node_blk,
321					blk_cnt);
322			break;
323		case TYPE_DOUBLE_INDIRECT_NODE:
324			fsck_chk_didnode_blk(sbi, inode, ftype, node_blk,
325					blk_cnt);
326			break;
327		default:
328			ASSERT(0);
329		}
330	}
331	free(node_blk);
332	return 0;
333err:
334	free(node_blk);
335	return -EINVAL;
336}
337
338/* start with valid nid and blkaddr */
339void fsck_chk_inode_blk(struct f2fs_sb_info *sbi, u32 nid,
340		enum FILE_TYPE ftype, struct f2fs_node *node_blk,
341		u32 *blk_cnt, struct node_info *ni)
342{
343	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
344	u32 child_cnt = 0, child_files = 0;
345	enum NODE_TYPE ntype;
346	u32 i_links = le32_to_cpu(node_blk->i.i_links);
347	u64 i_blocks = le64_to_cpu(node_blk->i.i_blocks);
348	unsigned int idx = 0;
349	int need_fix = 0;
350	int ret;
351
352	if (f2fs_test_main_bitmap(sbi, ni->blk_addr) == 0)
353		fsck->chk.valid_inode_cnt++;
354
355	if (ftype == F2FS_FT_DIR) {
356		f2fs_set_main_bitmap(sbi, ni->blk_addr);
357	} else {
358		if (f2fs_test_main_bitmap(sbi, ni->blk_addr) == 0) {
359			f2fs_set_main_bitmap(sbi, ni->blk_addr);
360			if (i_links > 1) {
361				/* First time. Create new hard link node */
362				add_into_hard_link_list(sbi, nid, i_links);
363				fsck->chk.multi_hard_link_files++;
364			}
365		} else {
366			DBG(3, "[0x%x] has hard links [0x%x]\n", nid, i_links);
367			if (find_and_dec_hard_link_list(sbi, nid)) {
368				ASSERT_MSG("[0x%x] needs more i_links=0x%x",
369						nid, i_links);
370				if (config.fix_on) {
371					node_blk->i.i_links =
372						cpu_to_le32(i_links + 1);
373					need_fix = 1;
374					FIX_MSG("File: 0x%x "
375						"i_links= 0x%x -> 0x%x",
376						nid, i_links, i_links + 1);
377				}
378				goto check;
379			}
380			/* No need to go deep into the node */
381			return;
382		}
383	}
384
385	if (fsck_chk_xattr_blk(sbi, nid,
386			le32_to_cpu(node_blk->i.i_xattr_nid), blk_cnt) &&
387			config.fix_on) {
388		node_blk->i.i_xattr_nid = 0;
389		need_fix = 1;
390		FIX_MSG("Remove xattr block: 0x%x, x_nid = 0x%x",
391				nid, le32_to_cpu(node_blk->i.i_xattr_nid));
392	}
393
394	if (ftype == F2FS_FT_CHRDEV || ftype == F2FS_FT_BLKDEV ||
395			ftype == F2FS_FT_FIFO || ftype == F2FS_FT_SOCK)
396		goto check;
397
398	if((node_blk->i.i_inline & F2FS_INLINE_DATA)) {
399		if (le32_to_cpu(node_blk->i.i_addr[0]) != 0) {
400			/* should fix this bug all the time */
401			FIX_MSG("inline_data has wrong 0'th block = %x",
402					le32_to_cpu(node_blk->i.i_addr[0]));
403			node_blk->i.i_addr[0] = 0;
404			node_blk->i.i_blocks = cpu_to_le64(*blk_cnt);
405			need_fix = 1;
406		}
407		DBG(3, "ino[0x%x] has inline data!\n", nid);
408		goto check;
409	}
410	if((node_blk->i.i_inline & F2FS_INLINE_DENTRY)) {
411		DBG(3, "ino[0x%x] has inline dentry!\n", nid);
412		ret = fsck_chk_inline_dentries(sbi, node_blk,
413					&child_cnt, &child_files);
414		if (ret < 0) {
415			/* should fix this bug all the time */
416			need_fix = 1;
417		}
418		goto check;
419	}
420
421	/* check data blocks in inode */
422	for (idx = 0; idx < ADDRS_PER_INODE(&node_blk->i); idx++) {
423		if (le32_to_cpu(node_blk->i.i_addr[idx]) != 0) {
424			ret = fsck_chk_data_blk(sbi,
425					le32_to_cpu(node_blk->i.i_addr[idx]),
426					&child_cnt, &child_files,
427					(i_blocks == *blk_cnt),
428					ftype, nid, idx, ni->version);
429			if (!ret) {
430				*blk_cnt = *blk_cnt + 1;
431			} else if (config.fix_on) {
432				node_blk->i.i_addr[idx] = 0;
433				need_fix = 1;
434				FIX_MSG("[0x%x] i_addr[%d] = 0", nid, idx);
435			}
436		}
437	}
438
439	/* check node blocks in inode */
440	for (idx = 0; idx < 5; idx++) {
441		if (idx == 0 || idx == 1)
442			ntype = TYPE_DIRECT_NODE;
443		else if (idx == 2 || idx == 3)
444			ntype = TYPE_INDIRECT_NODE;
445		else if (idx == 4)
446			ntype = TYPE_DOUBLE_INDIRECT_NODE;
447		else
448			ASSERT(0);
449
450		if (le32_to_cpu(node_blk->i.i_nid[idx]) != 0) {
451			ret = fsck_chk_node_blk(sbi, &node_blk->i,
452					le32_to_cpu(node_blk->i.i_nid[idx]),
453					ftype, ntype, blk_cnt);
454			if (!ret) {
455				*blk_cnt = *blk_cnt + 1;
456			} else if (config.fix_on) {
457				node_blk->i.i_nid[idx] = 0;
458				need_fix = 1;
459				FIX_MSG("[0x%x] i_nid[%d] = 0", nid, idx);
460			}
461		}
462	}
463check:
464	if (ftype == F2FS_FT_DIR)
465		DBG(1, "Directory Inode: 0x%x [%s] depth: %d has %d files\n\n",
466				le32_to_cpu(node_blk->footer.ino),
467				node_blk->i.i_name,
468				le32_to_cpu(node_blk->i.i_current_depth),
469				child_files);
470	if (ftype == F2FS_FT_ORPHAN)
471		DBG(1, "Orphan Inode: 0x%x [%s] i_blocks: %u\n\n",
472				le32_to_cpu(node_blk->footer.ino),
473				node_blk->i.i_name,
474				(u32)i_blocks);
475
476	if (i_blocks != *blk_cnt) {
477		ASSERT_MSG("ino: 0x%x has i_blocks: %08"PRIx64", "
478				"but has %u blocks",
479				nid, i_blocks, *blk_cnt);
480		if (config.fix_on) {
481			node_blk->i.i_blocks = cpu_to_le64(*blk_cnt);
482			need_fix = 1;
483			FIX_MSG("[0x%x] i_blocks=0x%08"PRIx64" -> 0x%x",
484					nid, i_blocks, *blk_cnt);
485		}
486	}
487	if (ftype == F2FS_FT_DIR && i_links != child_cnt) {
488		ASSERT_MSG("ino: 0x%x has i_links: %u but real links: %u",
489				nid, i_links, child_cnt);
490		if (config.fix_on) {
491			node_blk->i.i_links = cpu_to_le32(child_cnt);
492			need_fix = 1;
493			FIX_MSG("Dir: 0x%x i_links= 0x%x -> 0x%x",
494						nid, i_links, child_cnt);
495		}
496	}
497
498	if (ftype == F2FS_FT_ORPHAN && i_links)
499		ASSERT_MSG("ino: 0x%x is orphan inode, but has i_links: %u",
500				nid, i_links);
501	if (need_fix) {
502		ret = dev_write_block(node_blk, ni->blk_addr);
503		ASSERT(ret >= 0);
504	}
505}
506
507int fsck_chk_dnode_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
508		u32 nid, enum FILE_TYPE ftype, struct f2fs_node *node_blk,
509		u32 *blk_cnt, struct node_info *ni)
510{
511	int idx, ret;
512	u32 child_cnt = 0, child_files = 0;
513
514	for (idx = 0; idx < ADDRS_PER_BLOCK; idx++) {
515		if (le32_to_cpu(node_blk->dn.addr[idx]) == 0x0)
516			continue;
517		ret = fsck_chk_data_blk(sbi,
518			le32_to_cpu(node_blk->dn.addr[idx]),
519			&child_cnt, &child_files,
520			le64_to_cpu(inode->i_blocks) == *blk_cnt, ftype,
521			nid, idx, ni->version);
522		if (!ret)
523			*blk_cnt = *blk_cnt + 1;
524	}
525	return 0;
526}
527
528int fsck_chk_idnode_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
529		enum FILE_TYPE ftype, struct f2fs_node *node_blk, u32 *blk_cnt)
530{
531	int ret;
532	int i = 0;
533
534	for (i = 0 ; i < NIDS_PER_BLOCK; i++) {
535		if (le32_to_cpu(node_blk->in.nid[i]) == 0x0)
536			continue;
537		ret = fsck_chk_node_blk(sbi, inode,
538				le32_to_cpu(node_blk->in.nid[i]),
539				ftype, TYPE_DIRECT_NODE, blk_cnt);
540		if (!ret)
541			*blk_cnt = *blk_cnt + 1;
542		else if (ret == -EINVAL)
543			printf("delete in.nid[i] = 0;\n");
544	}
545	return 0;
546}
547
548int fsck_chk_didnode_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
549		enum FILE_TYPE ftype, struct f2fs_node *node_blk, u32 *blk_cnt)
550{
551	int i = 0;
552	int ret = 0;
553
554	for (i = 0; i < NIDS_PER_BLOCK; i++) {
555		if (le32_to_cpu(node_blk->in.nid[i]) == 0x0)
556			continue;
557		ret = fsck_chk_node_blk(sbi, inode,
558				le32_to_cpu(node_blk->in.nid[i]),
559				ftype, TYPE_INDIRECT_NODE, blk_cnt);
560		if (!ret)
561			*blk_cnt = *blk_cnt + 1;
562		else if (ret == -EINVAL)
563			printf("delete in.nid[i] = 0;\n");
564	}
565	return 0;
566}
567
568static void print_dentry(__u32 depth, __u8 *name,
569		unsigned long *bitmap,
570		struct f2fs_dir_entry *dentry,
571		int max, int idx, int last_blk)
572{
573	int last_de = 0;
574	int next_idx = 0;
575	int name_len;
576	unsigned int i;
577	int bit_offset;
578
579	if (config.dbg_lv != -1)
580		return;
581
582	name_len = le16_to_cpu(dentry[idx].name_len);
583	next_idx = idx + (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN;
584
585	bit_offset = find_next_bit(bitmap, max, next_idx);
586	if (bit_offset >= max && last_blk)
587		last_de = 1;
588
589	if (tree_mark_size <= depth) {
590		tree_mark_size *= 2;
591		tree_mark = realloc(tree_mark, tree_mark_size);
592	}
593
594	if (last_de)
595		tree_mark[depth] = '`';
596	else
597		tree_mark[depth] = '|';
598
599	if (tree_mark[depth - 1] == '`')
600		tree_mark[depth - 1] = ' ';
601
602
603	for (i = 1; i < depth; i++)
604		printf("%c   ", tree_mark[i]);
605	printf("%c-- %s 0x%x\n", last_de ? '`' : '|',
606				name, le32_to_cpu(dentry[idx].ino));
607}
608
609static int __chk_dentries(struct f2fs_sb_info *sbi, u32 *child_cnt,
610			u32* child_files,
611			unsigned long *bitmap,
612			struct f2fs_dir_entry *dentry,
613			__u8 (*filenames)[F2FS_SLOT_LEN],
614			int max, int last_blk)
615{
616	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
617	enum FILE_TYPE ftype;
618	int dentries = 0;
619	u32 blk_cnt;
620	u8 *name;
621	u32 hash_code;
622	u16 name_len;;
623	int ret = 0;
624	int fixed = 0;
625	int i;
626
627	for (i = 0; i < max;) {
628		if (test_bit(i, bitmap) == 0) {
629			i++;
630			continue;
631		}
632		if (!IS_VALID_NID(sbi, le32_to_cpu(dentry[i].ino))) {
633			DBG(1, "Bad dentry 0x%x with invalid NID/ino 0x%x",
634			    i, le32_to_cpu(dentry[i].ino));
635			if (config.fix_on) {
636				FIX_MSG("Clear bad dentry 0x%x with bad ino 0x%x",
637					i, le32_to_cpu(dentry[i].ino));
638				clear_bit(i, bitmap);
639				i++;
640				continue;
641			}
642		}
643		ftype = dentry[i].file_type;
644		if ((ftype <= F2FS_FT_UNKNOWN || ftype > F2FS_FT_LAST_FILE_TYPE) && config.fix_on) {
645			DBG(1, "Bad dentry 0x%x with unexpected ftype 0x%x",
646			    i, ftype);
647			if (config.fix_on) {
648				FIX_MSG("Clear bad dentry 0x%x with bad ftype 0x%x",
649					i, ftype);
650				clear_bit(i, bitmap);
651				i++;
652				continue;
653			}
654		}
655		name_len = le16_to_cpu(dentry[i].name_len);
656		name = calloc(name_len + 1, 1);
657		memcpy(name, filenames[i], name_len);
658		hash_code = f2fs_dentry_hash((const unsigned char *)name,
659								name_len);
660
661		/* fix hash_code made by old buggy code */
662		if (le32_to_cpu(dentry[i].hash_code) != hash_code) {
663			dentry[i].hash_code = hash_code;
664			fixed = 1;
665			FIX_MSG("hash_code[%d] of %s", i, name);
666		}
667
668		/* Becareful. 'dentry.file_type' is not imode. */
669		if (ftype == F2FS_FT_DIR) {
670			*child_cnt = *child_cnt + 1;
671			if ((name[0] == '.' && name_len == 1) ||
672				(name[0] == '.' && name[1] == '.' &&
673							name_len == 2)) {
674				i++;
675				free(name);
676				continue;
677			}
678		}
679
680		DBG(1, "[%3u]-[0x%x] name[%s] len[0x%x] ino[0x%x] type[0x%x]\n",
681				fsck->dentry_depth, i, name, name_len,
682				le32_to_cpu(dentry[i].ino),
683				dentry[i].file_type);
684
685		print_dentry(fsck->dentry_depth, name, bitmap,
686						dentry, max, i, 1);
687
688		blk_cnt = 1;
689		ret = fsck_chk_node_blk(sbi,
690				NULL, le32_to_cpu(dentry[i].ino),
691				ftype, TYPE_INODE, &blk_cnt);
692
693		if (ret && config.fix_on) {
694			int j;
695			int slots = (name_len + F2FS_SLOT_LEN - 1) /
696				F2FS_SLOT_LEN;
697			for (j = 0; j < slots; j++)
698				clear_bit(i + j, bitmap);
699			FIX_MSG("Unlink [0x%x] - %s len[0x%x], type[0x%x]",
700					le32_to_cpu(dentry[i].ino),
701					name, name_len,
702					dentry[i].file_type);
703			i += slots;
704			free(name);
705			continue;
706		}
707
708		i += (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN;
709		dentries++;
710		*child_files = *child_files + 1;
711		free(name);
712	}
713	return fixed ? -1 : dentries;
714}
715
716int fsck_chk_inline_dentries(struct f2fs_sb_info *sbi,
717		struct f2fs_node *node_blk, u32 *child_cnt, u32 *child_files)
718{
719	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
720	struct f2fs_inline_dentry *de_blk;
721	int dentries;
722
723	de_blk = inline_data_addr(node_blk);
724	ASSERT(de_blk != NULL);
725
726	fsck->dentry_depth++;
727	dentries = __chk_dentries(sbi, child_cnt, child_files,
728			(unsigned long *)de_blk->dentry_bitmap,
729			de_blk->dentry, de_blk->filename,
730			NR_INLINE_DENTRY, 1);
731	if (dentries < 0) {
732		DBG(1, "[%3d] Inline Dentry Block Fixed hash_codes\n\n",
733			fsck->dentry_depth);
734	} else {
735		DBG(1, "[%3d] Inline Dentry Block Done : "
736				"dentries:%d in %d slots (len:%d)\n\n",
737			fsck->dentry_depth, dentries,
738			(int)NR_INLINE_DENTRY, F2FS_NAME_LEN);
739	}
740	fsck->dentry_depth--;
741	return dentries;
742}
743
744int fsck_chk_dentry_blk(struct f2fs_sb_info *sbi, u32 blk_addr,
745		u32 *child_cnt, u32 *child_files, int last_blk)
746{
747	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
748	struct f2fs_dentry_block *de_blk;
749	int dentries, ret;
750
751	de_blk = (struct f2fs_dentry_block *)calloc(BLOCK_SZ, 1);
752	ASSERT(de_blk != NULL);
753
754	ret = dev_read_block(de_blk, blk_addr);
755	ASSERT(ret >= 0);
756
757	fsck->dentry_depth++;
758	dentries = __chk_dentries(sbi, child_cnt, child_files,
759			(unsigned long *)de_blk->dentry_bitmap,
760			de_blk->dentry, de_blk->filename,
761			NR_DENTRY_IN_BLOCK, last_blk);
762
763	if (dentries < 0) {
764		ret = dev_write_block(de_blk, blk_addr);
765		ASSERT(ret >= 0);
766		DBG(1, "[%3d] Dentry Block [0x%x] Fixed hash_codes\n\n",
767			fsck->dentry_depth, blk_addr);
768	} else {
769		DBG(1, "[%3d] Dentry Block [0x%x] Done : "
770				"dentries:%d in %d slots (len:%d)\n\n",
771			fsck->dentry_depth, blk_addr, dentries,
772			NR_DENTRY_IN_BLOCK, F2FS_NAME_LEN);
773	}
774	fsck->dentry_depth--;
775	free(de_blk);
776	return 0;
777}
778
779int fsck_chk_data_blk(struct f2fs_sb_info *sbi, u32 blk_addr,
780		u32 *child_cnt, u32 *child_files, int last_blk,
781		enum FILE_TYPE ftype, u32 parent_nid, u16 idx_in_node, u8 ver)
782{
783	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
784
785	/* Is it reserved block? */
786	if (blk_addr == NEW_ADDR) {
787		fsck->chk.valid_blk_cnt++;
788		return 0;
789	}
790
791	if (!IS_VALID_BLK_ADDR(sbi, blk_addr)) {
792		ASSERT_MSG("blkaddres is not valid. [0x%x]", blk_addr);
793		return -EINVAL;
794	}
795
796	if (is_valid_ssa_data_blk(sbi, blk_addr, parent_nid,
797						idx_in_node, ver)) {
798		ASSERT_MSG("summary data block is not valid. [0x%x]",
799						parent_nid);
800		return -EINVAL;
801	}
802
803	if (f2fs_test_sit_bitmap(sbi, blk_addr) == 0)
804		ASSERT_MSG("SIT bitmap is 0x0. blk_addr[0x%x]", blk_addr);
805
806	if (f2fs_test_main_bitmap(sbi, blk_addr) != 0)
807		ASSERT_MSG("Duplicated data [0x%x]. pnid[0x%x] idx[0x%x]",
808				blk_addr, parent_nid, idx_in_node);
809
810	f2fs_set_main_bitmap(sbi, blk_addr);
811
812	fsck->chk.valid_blk_cnt++;
813
814	if (ftype == F2FS_FT_DIR)
815		return fsck_chk_dentry_blk(sbi, blk_addr, child_cnt,
816				child_files, last_blk);
817	return 0;
818}
819
820void fsck_chk_orphan_node(struct f2fs_sb_info *sbi)
821{
822	u32 blk_cnt = 0;
823	block_t start_blk, orphan_blkaddr, i, j;
824	struct f2fs_orphan_block *orphan_blk;
825	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
826
827	if (!is_set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG))
828		return;
829
830	if (config.fix_on)
831		return;
832
833	start_blk = __start_cp_addr(sbi) + 1 +
834		le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
835	orphan_blkaddr = __start_sum_addr(sbi) - 1;
836	orphan_blk = calloc(BLOCK_SZ, 1);
837
838	for (i = 0; i < orphan_blkaddr; i++) {
839		int ret = dev_read_block(orphan_blk, start_blk + i);
840
841		ASSERT(ret >= 0);
842
843		for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
844			nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
845			DBG(1, "[%3d] ino [0x%x]\n", i, ino);
846			blk_cnt = 1;
847			fsck_chk_node_blk(sbi, NULL, ino,
848					F2FS_FT_ORPHAN, TYPE_INODE, &blk_cnt);
849		}
850		memset(orphan_blk, 0, BLOCK_SZ);
851	}
852	free(orphan_blk);
853}
854
855void fsck_init(struct f2fs_sb_info *sbi)
856{
857	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
858	struct f2fs_sm_info *sm_i = SM_I(sbi);
859
860	/*
861	 * We build three bitmap for main/sit/nat so that may check consistency
862	 * of filesystem.
863	 * 1. main_area_bitmap will be used to check whether all blocks of main
864	 *    area is used or not.
865	 * 2. nat_area_bitmap has bitmap information of used nid in NAT.
866	 * 3. sit_area_bitmap has bitmap information of used main block.
867	 * At Last sequence, we compare main_area_bitmap with sit_area_bitmap.
868	 */
869	fsck->nr_main_blks = sm_i->main_segments << sbi->log_blocks_per_seg;
870	fsck->main_area_bitmap_sz = (fsck->nr_main_blks + 7) / 8;
871	fsck->main_area_bitmap = calloc(fsck->main_area_bitmap_sz, 1);
872	ASSERT(fsck->main_area_bitmap != NULL);
873
874	build_nat_area_bitmap(sbi);
875
876	build_sit_area_bitmap(sbi);
877
878	tree_mark = calloc(tree_mark_size, 1);
879	ASSERT(tree_mark != NULL);
880}
881
882static void fix_nat_entries(struct f2fs_sb_info *sbi)
883{
884	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
885	u32 i;
886
887	for (i = 0; i < fsck->nr_nat_entries; i++)
888		if (f2fs_test_bit(i, fsck->nat_area_bitmap) != 0)
889			nullify_nat_entry(sbi, i);
890}
891
892static void fix_checkpoint(struct f2fs_sb_info *sbi)
893{
894	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
895	struct f2fs_super_block *raw_sb = sbi->raw_super;
896	struct f2fs_checkpoint *ckp = F2FS_CKPT(sbi);
897	unsigned long long cp_blk_no;
898	u32 i;
899	int ret;
900	u_int32_t crc = 0;
901
902	ckp->ckpt_flags = cpu_to_le32(CP_UMOUNT_FLAG);
903	ckp->cp_pack_total_block_count =
904		cpu_to_le32(8 + le32_to_cpu(raw_sb->cp_payload));
905	ckp->cp_pack_start_sum = cpu_to_le32(1 +
906				le32_to_cpu(raw_sb->cp_payload));
907
908	ckp->free_segment_count = cpu_to_le32(fsck->chk.free_segs);
909	ckp->valid_block_count = cpu_to_le32(fsck->chk.valid_blk_cnt);
910	ckp->valid_node_count = cpu_to_le32(fsck->chk.valid_node_cnt);
911	ckp->valid_inode_count = cpu_to_le32(fsck->chk.valid_inode_cnt);
912
913	crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, ckp, CHECKSUM_OFFSET);
914	*((__le32 *)((unsigned char *)ckp + CHECKSUM_OFFSET)) =
915							cpu_to_le32(crc);
916
917	cp_blk_no = le32_to_cpu(raw_sb->cp_blkaddr);
918	if (sbi->cur_cp == 2)
919		cp_blk_no += 1 << le32_to_cpu(raw_sb->log_blocks_per_seg);
920
921	ret = dev_write_block(ckp, cp_blk_no++);
922	ASSERT(ret >= 0);
923
924	for (i = 0; i < le32_to_cpu(raw_sb->cp_payload); i++) {
925		ret = dev_write_block(((unsigned char *)ckp) + i * F2FS_BLKSIZE,
926								cp_blk_no++);
927		ASSERT(ret >= 0);
928	}
929
930	for (i = 0; i < NO_CHECK_TYPE; i++) {
931		struct curseg_info *curseg = CURSEG_I(sbi, i);
932
933		ret = dev_write_block(curseg->sum_blk, cp_blk_no++);
934		ASSERT(ret >= 0);
935	}
936
937	ret = dev_write_block(ckp, cp_blk_no++);
938	ASSERT(ret >= 0);
939}
940
941int check_curseg_offset(struct f2fs_sb_info *sbi)
942{
943	int i;
944
945	for (i = 0; i < NO_CHECK_TYPE; i++) {
946		struct curseg_info *curseg = CURSEG_I(sbi, i);
947		struct seg_entry *se;
948
949		se = get_seg_entry(sbi, curseg->segno);
950		if (f2fs_test_bit(curseg->next_blkoff,
951				(const char *)se->cur_valid_map) == 1) {
952			ASSERT_MSG("Next block offset is not free, type:%d", i);
953			return -EINVAL;
954		}
955	}
956	return 0;
957}
958
959int fsck_verify(struct f2fs_sb_info *sbi)
960{
961	unsigned int i = 0;
962	int ret = 0;
963	u32 nr_unref_nid = 0;
964	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
965	struct hard_link_node *node = NULL;
966
967	printf("\n");
968
969	for (i = 0; i < fsck->nr_nat_entries; i++) {
970		if (f2fs_test_bit(i, fsck->nat_area_bitmap) != 0) {
971			printf("NID[0x%x] is unreachable\n", i);
972			nr_unref_nid++;
973		}
974	}
975
976	if (fsck->hard_link_list_head != NULL) {
977		node = fsck->hard_link_list_head;
978		while (node) {
979			printf("NID[0x%x] has [0x%x] more unreachable links\n",
980					node->nid, node->links);
981			node = node->next;
982		}
983		config.bug_on = 1;
984	}
985
986	printf("[FSCK] Unreachable nat entries                       ");
987	if (nr_unref_nid == 0x0) {
988		printf(" [Ok..] [0x%x]\n", nr_unref_nid);
989	} else {
990		printf(" [Fail] [0x%x]\n", nr_unref_nid);
991		ret = EXIT_ERR_CODE;
992		config.bug_on = 1;
993	}
994
995	printf("[FSCK] SIT valid block bitmap checking                ");
996	if (memcmp(fsck->sit_area_bitmap, fsck->main_area_bitmap,
997					fsck->sit_area_bitmap_sz) == 0x0) {
998		printf("[Ok..]\n");
999	} else {
1000		printf("[Fail]\n");
1001		ret = EXIT_ERR_CODE;
1002		config.bug_on = 1;
1003	}
1004
1005	printf("[FSCK] Hard link checking for regular file           ");
1006	if (fsck->hard_link_list_head == NULL) {
1007		printf(" [Ok..] [0x%x]\n", fsck->chk.multi_hard_link_files);
1008	} else {
1009		printf(" [Fail] [0x%x]\n", fsck->chk.multi_hard_link_files);
1010		ret = EXIT_ERR_CODE;
1011		config.bug_on = 1;
1012	}
1013
1014	printf("[FSCK] valid_block_count matching with CP            ");
1015	if (sbi->total_valid_block_count == fsck->chk.valid_blk_cnt) {
1016		printf(" [Ok..] [0x%x]\n", (u32)fsck->chk.valid_blk_cnt);
1017	} else {
1018		printf(" [Fail] [0x%x]\n", (u32)fsck->chk.valid_blk_cnt);
1019		ret = EXIT_ERR_CODE;
1020		config.bug_on = 1;
1021	}
1022
1023	printf("[FSCK] valid_node_count matcing with CP (de lookup)  ");
1024	if (sbi->total_valid_node_count == fsck->chk.valid_node_cnt) {
1025		printf(" [Ok..] [0x%x]\n", fsck->chk.valid_node_cnt);
1026	} else {
1027		printf(" [Fail] [0x%x]\n", fsck->chk.valid_node_cnt);
1028		ret = EXIT_ERR_CODE;
1029		config.bug_on = 1;
1030	}
1031
1032	printf("[FSCK] valid_node_count matcing with CP (nat lookup) ");
1033	if (sbi->total_valid_node_count == fsck->chk.valid_nat_entry_cnt) {
1034		printf(" [Ok..] [0x%x]\n", fsck->chk.valid_nat_entry_cnt);
1035	} else {
1036		printf(" [Fail] [0x%x]\n", fsck->chk.valid_nat_entry_cnt);
1037		ret = EXIT_ERR_CODE;
1038		config.bug_on = 1;
1039	}
1040
1041	printf("[FSCK] valid_inode_count matched with CP             ");
1042	if (sbi->total_valid_inode_count == fsck->chk.valid_inode_cnt) {
1043		printf(" [Ok..] [0x%x]\n", fsck->chk.valid_inode_cnt);
1044	} else {
1045		printf(" [Fail] [0x%x]\n", fsck->chk.valid_inode_cnt);
1046		ret = EXIT_ERR_CODE;
1047		config.bug_on = 1;
1048	}
1049
1050	printf("[FSCK] free segment_count matched with CP            ");
1051	if (le32_to_cpu(F2FS_CKPT(sbi)->free_segment_count) ==
1052						fsck->chk.sit_free_segs) {
1053		printf(" [Ok..] [0x%x]\n", fsck->chk.sit_free_segs);
1054	} else {
1055		printf(" [Fail] [0x%x]\n", fsck->chk.sit_free_segs);
1056		ret = EXIT_ERR_CODE;
1057		config.bug_on = 1;
1058	}
1059
1060	printf("[FSCK] next block offset is free                     ");
1061	if (check_curseg_offset(sbi) == 0) {
1062		printf(" [Ok..]\n");
1063	} else {
1064		printf(" [Fail]\n");
1065		ret = EXIT_ERR_CODE;
1066		config.bug_on = 1;
1067	}
1068
1069	printf("[FSCK] other corrupted bugs                          ");
1070	if (config.bug_on == 0) {
1071		printf(" [Ok..]\n");
1072	} else {
1073		printf(" [Fail]\n");
1074		ret = EXIT_ERR_CODE;
1075		config.bug_on = 1;
1076	}
1077
1078	/* fix global metadata */
1079	if (config.bug_on && config.fix_on) {
1080		fix_nat_entries(sbi);
1081		rewrite_sit_area_bitmap(sbi);
1082		fix_checkpoint(sbi);
1083	}
1084	return ret;
1085}
1086
1087void fsck_free(struct f2fs_sb_info *sbi)
1088{
1089	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1090	if (fsck->main_area_bitmap)
1091		free(fsck->main_area_bitmap);
1092
1093	if (fsck->nat_area_bitmap)
1094		free(fsck->nat_area_bitmap);
1095
1096	if (fsck->sit_area_bitmap)
1097		free(fsck->sit_area_bitmap);
1098
1099	if (tree_mark)
1100		free(tree_mark);
1101}
1102