Home | History | Annotate | Download | only in fsck
      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 
     13 char *tree_mark;
     14 uint32_t tree_mark_size = 256;
     15 
     16 static int add_into_hard_link_list(struct f2fs_sb_info *sbi, u32 nid, u32 link_cnt)
     17 {
     18 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
     19 	struct hard_link_node *node = NULL, *tmp = NULL, *prev = NULL;
     20 
     21 	node = calloc(sizeof(struct hard_link_node), 1);
     22 	ASSERT(node != NULL);
     23 
     24 	node->nid = nid;
     25 	node->links = link_cnt;
     26 	node->next = NULL;
     27 
     28 	if (fsck->hard_link_list_head == NULL) {
     29 		fsck->hard_link_list_head = node;
     30 		goto out;
     31 	}
     32 
     33 	tmp = fsck->hard_link_list_head;
     34 
     35 	/* Find insertion position */
     36 	while (tmp && (nid < tmp->nid)) {
     37 		ASSERT(tmp->nid != nid);
     38 		prev = tmp;
     39 		tmp = tmp->next;
     40 	}
     41 
     42 	if (tmp == fsck->hard_link_list_head) {
     43 		node->next = tmp;
     44 		fsck->hard_link_list_head = node;
     45 	} else {
     46 		prev->next = node;
     47 		node->next = tmp;
     48 	}
     49 
     50 out:
     51 	DBG(2, "ino[0x%x] has hard links [0x%x]\n", nid, link_cnt);
     52 	return 0;
     53 }
     54 
     55 static int find_and_dec_hard_link_list(struct f2fs_sb_info *sbi, u32 nid)
     56 {
     57 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
     58 	struct hard_link_node *node = NULL, *prev = NULL;
     59 
     60 	if (fsck->hard_link_list_head == NULL) {
     61 		ASSERT(0);
     62 		return -1;
     63 	}
     64 
     65 	node = fsck->hard_link_list_head;
     66 
     67 	while (node && (nid < node->nid)) {
     68 		prev = node;
     69 		node = node->next;
     70 	}
     71 
     72 	if (node == NULL || (nid != node->nid)) {
     73 		ASSERT(0);
     74 		return -1;
     75 	}
     76 
     77 	/* Decrease link count */
     78 	node->links = node->links - 1;
     79 
     80 	/* if link count becomes one, remove the node */
     81 	if (node->links == 1) {
     82 		if (fsck->hard_link_list_head == node)
     83 			fsck->hard_link_list_head = node->next;
     84 		else
     85 			prev->next = node->next;
     86 		free(node);
     87 	}
     88 
     89 	return 0;
     90 
     91 }
     92 
     93 static int is_valid_ssa_node_blk(struct f2fs_sb_info *sbi, u32 nid, u32 blk_addr)
     94 {
     95 	int ret = 0;
     96 	struct f2fs_summary sum_entry;
     97 
     98 	ret = get_sum_entry(sbi, blk_addr, &sum_entry);
     99 	ASSERT(ret >= 0);
    100 
    101 	if (ret == SEG_TYPE_DATA || ret == SEG_TYPE_CUR_DATA) {
    102 		ASSERT_MSG(0, "Summary footer is not a node segment summary\n");;
    103 	} else if (ret == SEG_TYPE_NODE) {
    104 		if (le32_to_cpu(sum_entry.nid) != nid) {
    105 			DBG(0, "nid                       [0x%x]\n", nid);
    106 			DBG(0, "target blk_addr           [0x%x]\n", blk_addr);
    107 			DBG(0, "summary blk_addr          [0x%x]\n",
    108 					GET_SUM_BLKADDR(sbi, GET_SEGNO(sbi, blk_addr)));
    109 			DBG(0, "seg no / offset           [0x%x / 0x%x]\n",
    110 					GET_SEGNO(sbi, blk_addr), OFFSET_IN_SEG(sbi, blk_addr));
    111 			DBG(0, "summary_entry.nid         [0x%x]\n", le32_to_cpu(sum_entry.nid));
    112 			DBG(0, "--> node block's nid      [0x%x]\n", nid);
    113 			ASSERT_MSG(0, "Invalid node seg summary\n");
    114 		}
    115 	} else if (ret == SEG_TYPE_CUR_NODE) {
    116 		/* current node segment has no ssa */
    117 	} else {
    118 		ASSERT_MSG(0, "Invalid return value of 'get_sum_entry'");
    119 	}
    120 
    121 	return 1;
    122 }
    123 
    124 static int is_valid_ssa_data_blk(struct f2fs_sb_info *sbi, u32 blk_addr,
    125 		u32 parent_nid, u16 idx_in_node, u8 version)
    126 {
    127 	int ret = 0;
    128 	struct f2fs_summary sum_entry;
    129 
    130 	ret = get_sum_entry(sbi, blk_addr, &sum_entry);
    131 	ASSERT(ret == SEG_TYPE_DATA || ret == SEG_TYPE_CUR_DATA);
    132 
    133 	if (le32_to_cpu(sum_entry.nid) != parent_nid ||
    134 			sum_entry.version != version ||
    135 			le16_to_cpu(sum_entry.ofs_in_node) != idx_in_node) {
    136 
    137 		DBG(0, "summary_entry.nid         [0x%x]\n", le32_to_cpu(sum_entry.nid));
    138 		DBG(0, "summary_entry.version     [0x%x]\n", sum_entry.version);
    139 		DBG(0, "summary_entry.ofs_in_node [0x%x]\n", le16_to_cpu(sum_entry.ofs_in_node));
    140 
    141 		DBG(0, "parent nid                [0x%x]\n", parent_nid);
    142 		DBG(0, "version from nat          [0x%x]\n", version);
    143 		DBG(0, "idx in parent node        [0x%x]\n", idx_in_node);
    144 
    145 		DBG(0, "Target data block addr    [0x%x]\n", blk_addr);
    146 		ASSERT_MSG(0, "Invalid data seg summary\n");
    147 	}
    148 
    149 	return 1;
    150 }
    151 
    152 int fsck_chk_node_blk(struct f2fs_sb_info *sbi,
    153 		struct f2fs_inode *inode,
    154 		u32 nid,
    155 		enum FILE_TYPE ftype,
    156 		enum NODE_TYPE ntype,
    157 		u32 *blk_cnt)
    158 {
    159 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
    160 	struct node_info ni;
    161 	struct f2fs_node *node_blk = NULL;
    162 	int ret = 0;
    163 
    164 	IS_VALID_NID(sbi, nid);
    165 
    166 	if (ftype != F2FS_FT_ORPHAN ||
    167 			f2fs_test_bit(nid, fsck->nat_area_bitmap) != 0x0)
    168 		f2fs_clear_bit(nid, fsck->nat_area_bitmap);
    169 	else
    170 		ASSERT_MSG(0, "nid duplicated [0x%x]\n", nid);
    171 
    172 	ret = get_node_info(sbi, nid, &ni);
    173 	ASSERT(ret >= 0);
    174 
    175 	/* Is it reserved block?
    176 	 * if block addresss was 0xffff,ffff,ffff,ffff
    177 	 * it means that block was already allocated, but not stored in disk
    178 	 */
    179 	if (ni.blk_addr == NEW_ADDR) {
    180 		fsck->chk.valid_blk_cnt++;
    181 		fsck->chk.valid_node_cnt++;
    182 		if (ntype == TYPE_INODE)
    183 			fsck->chk.valid_inode_cnt++;
    184 		return 0;
    185 	}
    186 
    187 	IS_VALID_BLK_ADDR(sbi, ni.blk_addr);
    188 
    189 	is_valid_ssa_node_blk(sbi, nid, ni.blk_addr);
    190 
    191 	if (f2fs_test_bit(BLKOFF_FROM_MAIN(sbi, ni.blk_addr), fsck->sit_area_bitmap) == 0x0) {
    192 		DBG(0, "SIT bitmap is 0x0. blk_addr[0x%x]\n", ni.blk_addr);
    193 		ASSERT(0);
    194 	}
    195 
    196 	if (f2fs_test_bit(BLKOFF_FROM_MAIN(sbi, ni.blk_addr), fsck->main_area_bitmap) == 0x0) {
    197 		fsck->chk.valid_blk_cnt++;
    198 		fsck->chk.valid_node_cnt++;
    199 	}
    200 
    201 	node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
    202 	ASSERT(node_blk != NULL);
    203 
    204 	ret = dev_read_block(node_blk, ni.blk_addr);
    205 	ASSERT(ret >= 0);
    206 
    207 	ASSERT_MSG(nid == le32_to_cpu(node_blk->footer.nid),
    208 			"nid[0x%x] blk_addr[0x%x] footer.nid[0x%x]\n",
    209 			nid, ni.blk_addr, le32_to_cpu(node_blk->footer.nid));
    210 
    211 	if (ntype == TYPE_INODE) {
    212 		ret = fsck_chk_inode_blk(sbi,
    213 				nid,
    214 				ftype,
    215 				node_blk,
    216 				blk_cnt,
    217 				&ni);
    218 	} else {
    219 		/* it's not inode */
    220 		ASSERT(node_blk->footer.nid != node_blk->footer.ino);
    221 
    222 		if (f2fs_test_bit(BLKOFF_FROM_MAIN(sbi, ni.blk_addr), fsck->main_area_bitmap) != 0) {
    223 			DBG(0, "Duplicated node block. ino[0x%x][0x%x]\n", nid, ni.blk_addr);
    224 			ASSERT(0);
    225 		}
    226 		f2fs_set_bit(BLKOFF_FROM_MAIN(sbi, ni.blk_addr), fsck->main_area_bitmap);
    227 
    228 		switch (ntype) {
    229 		case TYPE_DIRECT_NODE:
    230 			ret = fsck_chk_dnode_blk(sbi,
    231 					inode,
    232 					nid,
    233 					ftype,
    234 					node_blk,
    235 					blk_cnt,
    236 					&ni);
    237 			break;
    238 		case TYPE_INDIRECT_NODE:
    239 			ret = fsck_chk_idnode_blk(sbi,
    240 					inode,
    241 					ftype,
    242 					node_blk,
    243 					blk_cnt);
    244 			break;
    245 		case TYPE_DOUBLE_INDIRECT_NODE:
    246 			ret = fsck_chk_didnode_blk(sbi,
    247 					inode,
    248 					ftype,
    249 					node_blk,
    250 					blk_cnt);
    251 			break;
    252 		default:
    253 			ASSERT(0);
    254 		}
    255 	}
    256 	ASSERT(ret >= 0);
    257 
    258 	free(node_blk);
    259 	return 0;
    260 }
    261 
    262 int fsck_chk_inode_blk(struct f2fs_sb_info *sbi,
    263 		u32 nid,
    264 		enum FILE_TYPE ftype,
    265 		struct f2fs_node *node_blk,
    266 		u32 *blk_cnt,
    267 		struct node_info *ni)
    268 {
    269 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
    270 	u32 child_cnt = 0, child_files = 0;
    271 	enum NODE_TYPE ntype;
    272 	u32 i_links = le32_to_cpu(node_blk->i.i_links);
    273 	u64 i_blocks = le64_to_cpu(node_blk->i.i_blocks);
    274 	unsigned int idx = 0;
    275 	int ret = 0;
    276 
    277 	ASSERT(node_blk->footer.nid == node_blk->footer.ino);
    278 	ASSERT(le32_to_cpu(node_blk->footer.nid) == nid);
    279 
    280 	if (f2fs_test_bit(BLKOFF_FROM_MAIN(sbi, ni->blk_addr), fsck->main_area_bitmap) == 0x0)
    281 		fsck->chk.valid_inode_cnt++;
    282 
    283 	/* Orphan node. i_links should be 0 */
    284 	if (ftype == F2FS_FT_ORPHAN) {
    285 		ASSERT(i_links == 0);
    286 	} else {
    287 		ASSERT(i_links > 0);
    288 	}
    289 
    290 	if (ftype == F2FS_FT_DIR) {
    291 
    292 		/* not included '.' & '..' */
    293 		if (f2fs_test_bit(BLKOFF_FROM_MAIN(sbi, ni->blk_addr), fsck->main_area_bitmap) != 0) {
    294 			DBG(0, "Duplicated inode blk. ino[0x%x][0x%x]\n", nid, ni->blk_addr);
    295 			ASSERT(0);
    296 		}
    297 		f2fs_set_bit(BLKOFF_FROM_MAIN(sbi, ni->blk_addr), fsck->main_area_bitmap);
    298 
    299 	} else {
    300 
    301 		if (f2fs_test_bit(BLKOFF_FROM_MAIN(sbi, ni->blk_addr), fsck->main_area_bitmap) == 0x0) {
    302 			f2fs_set_bit(BLKOFF_FROM_MAIN(sbi, ni->blk_addr), fsck->main_area_bitmap);
    303 			if (i_links > 1) {
    304 				/* First time. Create new hard link node */
    305 				add_into_hard_link_list(sbi, nid, i_links);
    306 				fsck->chk.multi_hard_link_files++;
    307 			}
    308 		} else {
    309 			if (i_links <= 1) {
    310 				DBG(0, "Error. Node ID [0x%x]."
    311 						" There are one more hard links."
    312 						" But i_links is [0x%x]\n",
    313 						nid, i_links);
    314 				ASSERT(0);
    315 			}
    316 
    317 			DBG(3, "ino[0x%x] has hard links [0x%x]\n", nid, i_links);
    318 			ret = find_and_dec_hard_link_list(sbi, nid);
    319 			ASSERT(ret >= 0);
    320 
    321 			/* No need to go deep into the node */
    322 			goto out;
    323 		}
    324 	}
    325 
    326 	fsck_chk_xattr_blk(sbi, nid, le32_to_cpu(node_blk->i.i_xattr_nid), blk_cnt);
    327 
    328 	if (ftype == F2FS_FT_CHRDEV || ftype == F2FS_FT_BLKDEV ||
    329 			ftype == F2FS_FT_FIFO || ftype == F2FS_FT_SOCK)
    330 		goto check;
    331 	if((node_blk->i.i_inline & F2FS_INLINE_DATA)){
    332 		DBG(3, "ino[0x%x] has inline data!\n", nid);
    333 		goto check;
    334 	}
    335 
    336 	/* check data blocks in inode */
    337 	for (idx = 0; idx < ADDRS_PER_INODE(&node_blk->i); idx++) {
    338 		if (le32_to_cpu(node_blk->i.i_addr[idx]) != 0) {
    339 			*blk_cnt = *blk_cnt + 1;
    340 			ret = fsck_chk_data_blk(sbi,
    341 					le32_to_cpu(node_blk->i.i_addr[idx]),
    342 					&child_cnt,
    343 					&child_files,
    344 					(i_blocks == *blk_cnt),
    345 					ftype,
    346 					nid,
    347 					idx,
    348 					ni->version);
    349 			ASSERT(ret >= 0);
    350 		}
    351 	}
    352 
    353 	/* check node blocks in inode */
    354 	for (idx = 0; idx < 5; idx++) {
    355 		if (idx == 0 || idx == 1)
    356 			ntype = TYPE_DIRECT_NODE;
    357 		else if (idx == 2 || idx == 3)
    358 			ntype = TYPE_INDIRECT_NODE;
    359 		else if (idx == 4)
    360 			ntype = TYPE_DOUBLE_INDIRECT_NODE;
    361 		else
    362 			ASSERT(0);
    363 
    364 		if (le32_to_cpu(node_blk->i.i_nid[idx]) != 0) {
    365 			*blk_cnt = *blk_cnt + 1;
    366 			ret = fsck_chk_node_blk(sbi,
    367 					&node_blk->i,
    368 					le32_to_cpu(node_blk->i.i_nid[idx]),
    369 					ftype,
    370 					ntype,
    371 					blk_cnt);
    372 			ASSERT(ret >= 0);
    373 		}
    374 	}
    375 check:
    376 	if (ftype == F2FS_FT_DIR)
    377 		DBG(1, "Directory Inode: ino: %x name: %s depth: %d child files: %d\n\n",
    378 				le32_to_cpu(node_blk->footer.ino), node_blk->i.i_name,
    379 				le32_to_cpu(node_blk->i.i_current_depth), child_files);
    380 	if (ftype == F2FS_FT_ORPHAN)
    381 		DBG(1, "Orphan Inode: ino: %x name: %s i_blocks: %u\n\n",
    382 				le32_to_cpu(node_blk->footer.ino), node_blk->i.i_name,
    383 				(u32)i_blocks);
    384 	if ((ftype == F2FS_FT_DIR && i_links != child_cnt) ||
    385 			(i_blocks != *blk_cnt)) {
    386 		print_node_info(node_blk);
    387 		DBG(1, "blk   cnt [0x%x]\n", *blk_cnt);
    388 		DBG(1, "child cnt [0x%x]\n", child_cnt);
    389 	}
    390 
    391 	ASSERT(i_blocks == *blk_cnt);
    392 	if (ftype == F2FS_FT_DIR)
    393 		ASSERT(i_links == child_cnt);
    394 out:
    395 	return 0;
    396 }
    397 
    398 int fsck_chk_dnode_blk(struct f2fs_sb_info *sbi,
    399 		struct f2fs_inode *inode,
    400 		u32 nid,
    401 		enum FILE_TYPE ftype,
    402 		struct f2fs_node *node_blk,
    403 		u32 *blk_cnt,
    404 		struct node_info *ni)
    405 {
    406 	int idx;
    407 	u32 child_cnt = 0, child_files = 0;
    408 
    409 	for (idx = 0; idx < ADDRS_PER_BLOCK; idx++) {
    410 		if (le32_to_cpu(node_blk->dn.addr[idx]) == 0x0)
    411 			continue;
    412 		*blk_cnt = *blk_cnt + 1;
    413 		fsck_chk_data_blk(sbi,
    414 				le32_to_cpu(node_blk->dn.addr[idx]),
    415 				&child_cnt,
    416 				&child_files,
    417 				le64_to_cpu(inode->i_blocks) == *blk_cnt,
    418 				ftype,
    419 				nid,
    420 				idx,
    421 				ni->version);
    422 	}
    423 
    424 	return 0;
    425 }
    426 
    427 int fsck_chk_idnode_blk(struct f2fs_sb_info *sbi,
    428 		struct f2fs_inode *inode,
    429 		enum FILE_TYPE ftype,
    430 		struct f2fs_node *node_blk,
    431 		u32 *blk_cnt)
    432 {
    433 	int i = 0;
    434 
    435 	for (i = 0 ; i < NIDS_PER_BLOCK; i++) {
    436 		if (le32_to_cpu(node_blk->in.nid[i]) == 0x0)
    437 			continue;
    438 		*blk_cnt = *blk_cnt + 1;
    439 		fsck_chk_node_blk(sbi,
    440 				inode,
    441 				le32_to_cpu(node_blk->in.nid[i]),
    442 				ftype,
    443 				TYPE_DIRECT_NODE,
    444 				blk_cnt);
    445 	}
    446 
    447 	return 0;
    448 }
    449 
    450 int fsck_chk_didnode_blk(struct f2fs_sb_info *sbi,
    451 		struct f2fs_inode *inode,
    452 		enum FILE_TYPE ftype,
    453 		struct f2fs_node *node_blk,
    454 		u32 *blk_cnt)
    455 {
    456 	int i = 0;
    457 
    458 	for (i = 0; i < NIDS_PER_BLOCK; i++) {
    459 		if (le32_to_cpu(node_blk->in.nid[i]) == 0x0)
    460 			continue;
    461 		*blk_cnt = *blk_cnt + 1;
    462 		fsck_chk_node_blk(sbi,
    463 				inode,
    464 				le32_to_cpu(node_blk->in.nid[i]),
    465 				ftype,
    466 				TYPE_INDIRECT_NODE,
    467 				blk_cnt);
    468 	}
    469 
    470 	return 0;
    471 }
    472 
    473 static void print_dentry(__u32 depth, __u8 *name,
    474 		struct f2fs_dentry_block *de_blk, int idx, int last_blk)
    475 {
    476 	int last_de = 0;
    477 	int next_idx = 0;
    478 	int name_len;
    479 	unsigned int i;
    480 	int bit_offset;
    481 
    482 	if (config.dbg_lv != -1)
    483 		return;
    484 
    485 	name_len = le16_to_cpu(de_blk->dentry[idx].name_len);
    486 	next_idx = idx + (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN;
    487 
    488 	bit_offset = find_next_bit((unsigned long *)de_blk->dentry_bitmap,
    489 			NR_DENTRY_IN_BLOCK, next_idx);
    490 	if (bit_offset >= NR_DENTRY_IN_BLOCK && last_blk)
    491 		last_de = 1;
    492 
    493 	if (tree_mark_size <= depth) {
    494 		tree_mark_size *= 2;
    495 		tree_mark = realloc(tree_mark, tree_mark_size);
    496 	}
    497 
    498 	if (last_de)
    499 		tree_mark[depth] = '`';
    500 	else
    501 		tree_mark[depth] = '|';
    502 
    503 	if (tree_mark[depth - 1] == '`')
    504 		tree_mark[depth - 1] = ' ';
    505 
    506 
    507 	for (i = 1; i < depth; i++)
    508 		printf("%c   ", tree_mark[i]);
    509 	printf("%c-- %s\n", last_de ? '`' : '|', name);
    510 }
    511 
    512 int fsck_chk_dentry_blk(struct f2fs_sb_info *sbi,
    513 		u32 blk_addr,
    514 		u32 *child_cnt,
    515 		u32 *child_files,
    516 		int last_blk)
    517 {
    518 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
    519 	int i;
    520 	int ret = 0;
    521 	int dentries = 0;
    522 	u8 *name;
    523 	u32 hash_code;
    524 	u32 blk_cnt;
    525 	u16 name_len;;
    526 
    527 	enum FILE_TYPE ftype;
    528 	struct f2fs_dentry_block *de_blk;
    529 
    530 	de_blk = (struct f2fs_dentry_block *)calloc(BLOCK_SZ, 1);
    531 	ASSERT(de_blk != NULL);
    532 
    533 	ret = dev_read_block(de_blk, blk_addr);
    534 	ASSERT(ret >= 0);
    535 
    536 	fsck->dentry_depth++;
    537 
    538 	for (i = 0; i < NR_DENTRY_IN_BLOCK;) {
    539 		if (test_bit(i, (unsigned long *)de_blk->dentry_bitmap) == 0x0) {
    540 			i++;
    541 			continue;
    542 		}
    543 
    544 		name_len = le32_to_cpu(de_blk->dentry[i].name_len);
    545 		name = calloc(name_len + 1, 1);
    546 		memcpy(name, de_blk->filename[i], name_len);
    547 
    548 		hash_code = f2fs_dentry_hash((const char *)name, name_len);
    549 		ASSERT(le32_to_cpu(de_blk->dentry[i].hash_code) == hash_code);
    550 
    551 		ftype = de_blk->dentry[i].file_type;
    552 
    553 		/* Becareful. 'dentry.file_type' is not imode. */
    554 		if (ftype == F2FS_FT_DIR) {
    555 			*child_cnt = *child_cnt + 1;
    556 			if ((name[0] == '.' && name[1] == '.' && name_len == 2) ||
    557 					(name[0] == '.' && name_len == 1)) {
    558 				i++;
    559 				free(name);
    560 				continue;
    561 			}
    562 		}
    563 
    564 		DBG(2, "[%3u] - no[0x%x] name[%s] len[0x%x] ino[0x%x] type[0x%x]\n",
    565 				fsck->dentry_depth, i, name, name_len,
    566 				le32_to_cpu(de_blk->dentry[i].ino),
    567 				de_blk->dentry[i].file_type);
    568 
    569 		print_dentry(fsck->dentry_depth, name, de_blk, i, last_blk);
    570 
    571 		blk_cnt = 1;
    572 		ret = fsck_chk_node_blk(sbi,
    573 				NULL,
    574 				le32_to_cpu(de_blk->dentry[i].ino),
    575 				ftype,
    576 				TYPE_INODE,
    577 				&blk_cnt);
    578 
    579 		ASSERT(ret >= 0);
    580 
    581 		i += (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN;
    582 		dentries++;
    583 		*child_files = *child_files + 1;
    584 		free(name);
    585 	}
    586 
    587 	DBG(1, "[%3d] Dentry Block [0x%x] Done : dentries:%d in %d slots (len:%d)\n\n",
    588 			fsck->dentry_depth, blk_addr, dentries, NR_DENTRY_IN_BLOCK, F2FS_NAME_LEN);
    589 	fsck->dentry_depth--;
    590 
    591 	free(de_blk);
    592 	return 0;
    593 }
    594 
    595 int fsck_chk_data_blk(struct f2fs_sb_info *sbi,
    596 		u32 blk_addr,
    597 		u32 *child_cnt,
    598 		u32 *child_files,
    599 		int last_blk,
    600 		enum FILE_TYPE ftype,
    601 		u32 parent_nid,
    602 		u16 idx_in_node,
    603 		u8 ver)
    604 {
    605 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
    606 
    607 	/* Is it reserved block? */
    608 	if (blk_addr == NEW_ADDR) {
    609 		fsck->chk.valid_blk_cnt++;
    610 		return 0;
    611 	}
    612 
    613 	IS_VALID_BLK_ADDR(sbi, blk_addr);
    614 
    615 	is_valid_ssa_data_blk(sbi, blk_addr, parent_nid, idx_in_node, ver);
    616 
    617 	if (f2fs_test_bit(BLKOFF_FROM_MAIN(sbi, blk_addr), fsck->sit_area_bitmap) == 0x0) {
    618 		ASSERT_MSG(0, "SIT bitmap is 0x0. blk_addr[0x%x]\n", blk_addr);
    619 	}
    620 
    621 	if (f2fs_test_bit(BLKOFF_FROM_MAIN(sbi, blk_addr), fsck->main_area_bitmap) != 0) {
    622 		ASSERT_MSG(0, "Duplicated data block. pnid[0x%x] idx[0x%x] blk_addr[0x%x]\n",
    623 				parent_nid, idx_in_node, blk_addr);
    624 	}
    625 	f2fs_set_bit(BLKOFF_FROM_MAIN(sbi, blk_addr), fsck->main_area_bitmap);
    626 
    627 	fsck->chk.valid_blk_cnt++;
    628 
    629 	if (ftype == F2FS_FT_DIR) {
    630 		fsck_chk_dentry_blk(sbi,
    631 				blk_addr,
    632 				child_cnt,
    633 				child_files,
    634 				last_blk);
    635 	}
    636 
    637 	return 0;
    638 }
    639 
    640 int fsck_chk_orphan_node(struct f2fs_sb_info *sbi)
    641 {
    642 	int ret = 0;
    643 	u32 blk_cnt = 0;
    644 
    645 	block_t start_blk, orphan_blkaddr, i, j;
    646 	struct f2fs_orphan_block *orphan_blk;
    647 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
    648 
    649 	if (!is_set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG))
    650 		return 0;
    651 
    652 	start_blk = __start_cp_addr(sbi) + 1 +
    653 		le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
    654 
    655 	orphan_blkaddr = __start_sum_addr(sbi) - 1;
    656 
    657 	orphan_blk = calloc(BLOCK_SZ, 1);
    658 
    659 	for (i = 0; i < orphan_blkaddr; i++) {
    660 		dev_read_block(orphan_blk, start_blk + i);
    661 
    662 		for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
    663 			nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
    664 			DBG(1, "[%3d] ino [0x%x]\n", i, ino);
    665 			blk_cnt = 1;
    666 			ret = fsck_chk_node_blk(sbi,
    667 					NULL,
    668 					ino,
    669 					F2FS_FT_ORPHAN,
    670 					TYPE_INODE,
    671 					&blk_cnt);
    672 			ASSERT(ret >= 0);
    673 		}
    674 		memset(orphan_blk, 0, BLOCK_SZ);
    675 	}
    676 	free(orphan_blk);
    677 
    678 
    679 	return 0;
    680 }
    681 
    682 int fsck_chk_xattr_blk(struct f2fs_sb_info *sbi, u32 ino, u32 x_nid, u32 *blk_cnt)
    683 {
    684 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
    685 	struct node_info ni;
    686 
    687 	if (x_nid == 0x0)
    688 		return 0;
    689 
    690 	if (f2fs_test_bit(x_nid, fsck->nat_area_bitmap) != 0x0) {
    691 		f2fs_clear_bit(x_nid, fsck->nat_area_bitmap);
    692 	} else {
    693 		ASSERT_MSG(0, "xattr_nid duplicated [0x%x]\n", x_nid);
    694 	}
    695 
    696 	*blk_cnt = *blk_cnt + 1;
    697 	fsck->chk.valid_blk_cnt++;
    698 	fsck->chk.valid_node_cnt++;
    699 
    700 	ASSERT(get_node_info(sbi, x_nid, &ni) >= 0);
    701 
    702 	if (f2fs_test_bit(BLKOFF_FROM_MAIN(sbi, ni.blk_addr), fsck->main_area_bitmap) != 0) {
    703 		ASSERT_MSG(0, "Duplicated node block for x_attr. "
    704 				"x_nid[0x%x] block addr[0x%x]\n",
    705 				x_nid, ni.blk_addr);
    706 	}
    707 	f2fs_set_bit(BLKOFF_FROM_MAIN(sbi, ni.blk_addr), fsck->main_area_bitmap);
    708 
    709 	DBG(2, "ino[0x%x] x_nid[0x%x]\n", ino, x_nid);
    710 	return 0;
    711 }
    712 
    713 int fsck_init(struct f2fs_sb_info *sbi)
    714 {
    715 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
    716 	struct f2fs_sm_info *sm_i = SM_I(sbi);
    717 
    718 	/*
    719 	 * We build three bitmap for main/sit/nat so that may check consistency of filesystem.
    720 	 * 1. main_area_bitmap will be used to check whether all blocks of main area is used or not.
    721 	 * 2. nat_area_bitmap has bitmap information of used nid in NAT.
    722 	 * 3. sit_area_bitmap has bitmap information of used main block.
    723 	 * At Last sequence, we compare main_area_bitmap with sit_area_bitmap.
    724 	 */
    725 	fsck->nr_main_blks = sm_i->main_segments << sbi->log_blocks_per_seg;
    726 	fsck->main_area_bitmap_sz = (fsck->nr_main_blks + 7) / 8;
    727 	fsck->main_area_bitmap = calloc(fsck->main_area_bitmap_sz, 1);
    728 	ASSERT(fsck->main_area_bitmap != NULL);
    729 
    730 	build_nat_area_bitmap(sbi);
    731 
    732 	build_sit_area_bitmap(sbi);
    733 
    734 	tree_mark = calloc(tree_mark_size, 1);
    735 	return 0;
    736 }
    737 
    738 int fsck_verify(struct f2fs_sb_info *sbi)
    739 {
    740 	unsigned int i = 0;
    741 	int ret = 0;
    742 	u32 nr_unref_nid = 0;
    743 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
    744 	struct hard_link_node *node = NULL;
    745 
    746 	printf("\n");
    747 
    748 	for (i = 0; i < fsck->nr_nat_entries; i++) {
    749 		if (f2fs_test_bit(i, fsck->nat_area_bitmap) != 0) {
    750 			printf("NID[0x%x] is unreachable\n", i);
    751 			nr_unref_nid++;
    752 		}
    753 	}
    754 
    755 	if (fsck->hard_link_list_head != NULL) {
    756 		node = fsck->hard_link_list_head;
    757 		while (node) {
    758 			printf("NID[0x%x] has [0x%x] more unreachable links\n",
    759 					node->nid, node->links);
    760 			node = node->next;
    761 		}
    762 	}
    763 
    764 	printf("[FSCK] Unreachable nat entries                       ");
    765 	if (nr_unref_nid == 0x0) {
    766 		printf(" [Ok..] [0x%x]\n", nr_unref_nid);
    767 	} else {
    768 		printf(" [Fail] [0x%x]\n", nr_unref_nid);
    769 		ret = EXIT_ERR_CODE;
    770 	}
    771 
    772 	printf("[FSCK] SIT valid block bitmap checking                ");
    773 	if (memcmp(fsck->sit_area_bitmap, fsck->main_area_bitmap, fsck->sit_area_bitmap_sz) == 0x0) {
    774 		printf("[Ok..]\n");
    775 	} else {
    776 		printf("[Fail]\n");
    777 		ret = EXIT_ERR_CODE;
    778 	}
    779 
    780 	printf("[FSCK] Hard link checking for regular file           ");
    781 	if (fsck->hard_link_list_head == NULL) {
    782 		printf(" [Ok..] [0x%x]\n", fsck->chk.multi_hard_link_files);
    783 	} else {
    784 		printf(" [Fail] [0x%x]\n", fsck->chk.multi_hard_link_files);
    785 		ret = EXIT_ERR_CODE;
    786 	}
    787 
    788 	printf("[FSCK] valid_block_count matching with CP            ");
    789 	if (sbi->total_valid_block_count == fsck->chk.valid_blk_cnt) {
    790 		printf(" [Ok..] [0x%x]\n", (u32)fsck->chk.valid_blk_cnt);
    791 	} else {
    792 		printf(" [Fail] [0x%x]\n", (u32)fsck->chk.valid_blk_cnt);
    793 		ret = EXIT_ERR_CODE;
    794 	}
    795 
    796 	printf("[FSCK] valid_node_count matcing with CP (de lookup)  ");
    797 	if (sbi->total_valid_node_count == fsck->chk.valid_node_cnt) {
    798 		printf(" [Ok..] [0x%x]\n", fsck->chk.valid_node_cnt);
    799 	} else {
    800 		printf(" [Fail] [0x%x]\n", fsck->chk.valid_node_cnt);
    801 		ret = EXIT_ERR_CODE;
    802 	}
    803 
    804 	printf("[FSCK] valid_node_count matcing with CP (nat lookup) ");
    805 	if (sbi->total_valid_node_count == fsck->chk.valid_nat_entry_cnt) {
    806 		printf(" [Ok..] [0x%x]\n", fsck->chk.valid_nat_entry_cnt);
    807 	} else {
    808 		printf(" [Fail] [0x%x]\n", fsck->chk.valid_nat_entry_cnt);
    809 		ret = EXIT_ERR_CODE;
    810 	}
    811 
    812 	printf("[FSCK] valid_inode_count matched with CP             ");
    813 	if (sbi->total_valid_inode_count == fsck->chk.valid_inode_cnt) {
    814 		printf(" [Ok..] [0x%x]\n", fsck->chk.valid_inode_cnt);
    815 	} else {
    816 		printf(" [Fail] [0x%x]\n", fsck->chk.valid_inode_cnt);
    817 		ret = EXIT_ERR_CODE;
    818 	}
    819 
    820 	return ret;
    821 }
    822 
    823 void fsck_free(struct f2fs_sb_info *sbi)
    824 {
    825 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
    826 	if (fsck->main_area_bitmap)
    827 		free(fsck->main_area_bitmap);
    828 
    829 	if (fsck->nat_area_bitmap)
    830 		free(fsck->nat_area_bitmap);
    831 
    832 	if (fsck->sit_area_bitmap)
    833 		free(fsck->sit_area_bitmap);
    834 
    835 	if (tree_mark)
    836 		free(tree_mark);
    837 }
    838