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 #include "quotaio.h"
     13 
     14 char *tree_mark;
     15 uint32_t tree_mark_size = 256;
     16 
     17 int f2fs_set_main_bitmap(struct f2fs_sb_info *sbi, u32 blk, int type)
     18 {
     19 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
     20 	struct seg_entry *se;
     21 	int fix = 0;
     22 
     23 	se = get_seg_entry(sbi, GET_SEGNO(sbi, blk));
     24 	if (se->type >= NO_CHECK_TYPE)
     25 		fix = 1;
     26 	else if (IS_DATASEG(se->type) != IS_DATASEG(type))
     27 		fix = 1;
     28 
     29 	/* just check data and node types */
     30 	if (fix) {
     31 		DBG(1, "Wrong segment type [0x%x] %x -> %x",
     32 				GET_SEGNO(sbi, blk), se->type, type);
     33 		se->type = type;
     34 	}
     35 	return f2fs_set_bit(BLKOFF_FROM_MAIN(sbi, blk), fsck->main_area_bitmap);
     36 }
     37 
     38 static inline int f2fs_test_main_bitmap(struct f2fs_sb_info *sbi, u32 blk)
     39 {
     40 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
     41 
     42 	return f2fs_test_bit(BLKOFF_FROM_MAIN(sbi, blk),
     43 						fsck->main_area_bitmap);
     44 }
     45 
     46 static inline int f2fs_test_sit_bitmap(struct f2fs_sb_info *sbi, u32 blk)
     47 {
     48 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
     49 
     50 	return f2fs_test_bit(BLKOFF_FROM_MAIN(sbi, blk), fsck->sit_area_bitmap);
     51 }
     52 
     53 int f2fs_set_sit_bitmap(struct f2fs_sb_info *sbi, u32 blk)
     54 {
     55 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
     56 
     57 	return f2fs_set_bit(BLKOFF_FROM_MAIN(sbi, blk), fsck->sit_area_bitmap);
     58 }
     59 
     60 static int add_into_hard_link_list(struct f2fs_sb_info *sbi,
     61 						u32 nid, u32 link_cnt)
     62 {
     63 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
     64 	struct hard_link_node *node = NULL, *tmp = NULL, *prev = NULL;
     65 
     66 	node = calloc(sizeof(struct hard_link_node), 1);
     67 	ASSERT(node != NULL);
     68 
     69 	node->nid = nid;
     70 	node->links = link_cnt;
     71 	node->actual_links = 1;
     72 	node->next = NULL;
     73 
     74 	if (fsck->hard_link_list_head == NULL) {
     75 		fsck->hard_link_list_head = node;
     76 		goto out;
     77 	}
     78 
     79 	tmp = fsck->hard_link_list_head;
     80 
     81 	/* Find insertion position */
     82 	while (tmp && (nid < tmp->nid)) {
     83 		ASSERT(tmp->nid != nid);
     84 		prev = tmp;
     85 		tmp = tmp->next;
     86 	}
     87 
     88 	if (tmp == fsck->hard_link_list_head) {
     89 		node->next = tmp;
     90 		fsck->hard_link_list_head = node;
     91 	} else {
     92 		prev->next = node;
     93 		node->next = tmp;
     94 	}
     95 
     96 out:
     97 	DBG(2, "ino[0x%x] has hard links [0x%x]\n", nid, link_cnt);
     98 	return 0;
     99 }
    100 
    101 static int find_and_dec_hard_link_list(struct f2fs_sb_info *sbi, u32 nid)
    102 {
    103 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
    104 	struct hard_link_node *node = NULL, *prev = NULL;
    105 
    106 	if (fsck->hard_link_list_head == NULL)
    107 		return -EINVAL;
    108 
    109 	node = fsck->hard_link_list_head;
    110 
    111 	while (node && (nid < node->nid)) {
    112 		prev = node;
    113 		node = node->next;
    114 	}
    115 
    116 	if (node == NULL || (nid != node->nid))
    117 		return -EINVAL;
    118 
    119 	/* Decrease link count */
    120 	node->links = node->links - 1;
    121 	node->actual_links++;
    122 
    123 	/* if link count becomes one, remove the node */
    124 	if (node->links == 1) {
    125 		if (fsck->hard_link_list_head == node)
    126 			fsck->hard_link_list_head = node->next;
    127 		else
    128 			prev->next = node->next;
    129 		free(node);
    130 	}
    131 	return 0;
    132 }
    133 
    134 static int is_valid_ssa_node_blk(struct f2fs_sb_info *sbi, u32 nid,
    135 							u32 blk_addr)
    136 {
    137 	struct f2fs_summary_block *sum_blk;
    138 	struct f2fs_summary *sum_entry;
    139 	struct seg_entry * se;
    140 	u32 segno, offset;
    141 	int need_fix = 0, ret = 0;
    142 	int type;
    143 
    144 	segno = GET_SEGNO(sbi, blk_addr);
    145 	offset = OFFSET_IN_SEG(sbi, blk_addr);
    146 
    147 	sum_blk = get_sum_block(sbi, segno, &type);
    148 
    149 	if (type != SEG_TYPE_NODE && type != SEG_TYPE_CUR_NODE) {
    150 		/* can't fix current summary, then drop the block */
    151 		if (!c.fix_on || type < 0) {
    152 			ASSERT_MSG("Summary footer is not for node segment");
    153 			ret = -EINVAL;
    154 			goto out;
    155 		}
    156 
    157 		need_fix = 1;
    158 		se = get_seg_entry(sbi, segno);
    159 		if(IS_NODESEG(se->type)) {
    160 			FIX_MSG("Summary footer indicates a node segment: 0x%x", segno);
    161 			sum_blk->footer.entry_type = SUM_TYPE_NODE;
    162 		} else {
    163 			ret = -EINVAL;
    164 			goto out;
    165 		}
    166 	}
    167 
    168 	sum_entry = &(sum_blk->entries[offset]);
    169 
    170 	if (le32_to_cpu(sum_entry->nid) != nid) {
    171 		if (!c.fix_on || type < 0) {
    172 			DBG(0, "nid                       [0x%x]\n", nid);
    173 			DBG(0, "target blk_addr           [0x%x]\n", blk_addr);
    174 			DBG(0, "summary blk_addr          [0x%x]\n",
    175 						GET_SUM_BLKADDR(sbi,
    176 						GET_SEGNO(sbi, blk_addr)));
    177 			DBG(0, "seg no / offset           [0x%x / 0x%x]\n",
    178 						GET_SEGNO(sbi, blk_addr),
    179 						OFFSET_IN_SEG(sbi, blk_addr));
    180 			DBG(0, "summary_entry.nid         [0x%x]\n",
    181 						le32_to_cpu(sum_entry->nid));
    182 			DBG(0, "--> node block's nid      [0x%x]\n", nid);
    183 			ASSERT_MSG("Invalid node seg summary\n");
    184 			ret = -EINVAL;
    185 		} else {
    186 			FIX_MSG("Set node summary 0x%x -> [0x%x] [0x%x]",
    187 						segno, nid, blk_addr);
    188 			sum_entry->nid = cpu_to_le32(nid);
    189 			need_fix = 1;
    190 		}
    191 	}
    192 	if (need_fix && !c.ro) {
    193 		u64 ssa_blk;
    194 		int ret2;
    195 
    196 		ssa_blk = GET_SUM_BLKADDR(sbi, segno);
    197 		ret2 = dev_write_block(sum_blk, ssa_blk);
    198 		ASSERT(ret2 >= 0);
    199 	}
    200 out:
    201 	if (type == SEG_TYPE_NODE || type == SEG_TYPE_DATA ||
    202 					type == SEG_TYPE_MAX)
    203 		free(sum_blk);
    204 	return ret;
    205 }
    206 
    207 static int is_valid_summary(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
    208 							u32 blk_addr)
    209 {
    210 	u16 ofs_in_node = le16_to_cpu(sum->ofs_in_node);
    211 	u32 nid = le32_to_cpu(sum->nid);
    212 	struct f2fs_node *node_blk = NULL;
    213 	__le32 target_blk_addr;
    214 	struct node_info ni;
    215 	int ret = 0;
    216 
    217 	node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
    218 	ASSERT(node_blk != NULL);
    219 
    220 	if (!IS_VALID_NID(sbi, nid))
    221 		goto out;
    222 
    223 	get_node_info(sbi, nid, &ni);
    224 
    225 	if (!IS_VALID_BLK_ADDR(sbi, ni.blk_addr))
    226 		goto out;
    227 
    228 	/* read node_block */
    229 	ret = dev_read_block(node_blk, ni.blk_addr);
    230 	ASSERT(ret >= 0);
    231 
    232 	if (le32_to_cpu(node_blk->footer.nid) != nid)
    233 		goto out;
    234 
    235 	/* check its block address */
    236 	if (node_blk->footer.nid == node_blk->footer.ino) {
    237 		int ofs = get_extra_isize(node_blk);
    238 
    239 		target_blk_addr = node_blk->i.i_addr[ofs + ofs_in_node];
    240 	} else {
    241 		target_blk_addr = node_blk->dn.addr[ofs_in_node];
    242 	}
    243 
    244 	if (blk_addr == le32_to_cpu(target_blk_addr))
    245 		ret = 1;
    246 out:
    247 	free(node_blk);
    248 	return ret;
    249 }
    250 
    251 static int is_valid_ssa_data_blk(struct f2fs_sb_info *sbi, u32 blk_addr,
    252 		u32 parent_nid, u16 idx_in_node, u8 version)
    253 {
    254 	struct f2fs_summary_block *sum_blk;
    255 	struct f2fs_summary *sum_entry;
    256 	struct seg_entry * se;
    257 	u32 segno, offset;
    258 	int need_fix = 0, ret = 0;
    259 	int type;
    260 
    261 	segno = GET_SEGNO(sbi, blk_addr);
    262 	offset = OFFSET_IN_SEG(sbi, blk_addr);
    263 
    264 	sum_blk = get_sum_block(sbi, segno, &type);
    265 
    266 	if (type != SEG_TYPE_DATA && type != SEG_TYPE_CUR_DATA) {
    267 		/* can't fix current summary, then drop the block */
    268 		if (!c.fix_on || type < 0) {
    269 			ASSERT_MSG("Summary footer is not for data segment");
    270 			ret = -EINVAL;
    271 			goto out;
    272 		}
    273 
    274 		need_fix = 1;
    275 		se = get_seg_entry(sbi, segno);
    276 		if (IS_DATASEG(se->type)) {
    277 			FIX_MSG("Summary footer indicates a data segment: 0x%x", segno);
    278 			sum_blk->footer.entry_type = SUM_TYPE_DATA;
    279 		} else {
    280 			ret = -EINVAL;
    281 			goto out;
    282 		}
    283 	}
    284 
    285 	sum_entry = &(sum_blk->entries[offset]);
    286 
    287 	if (le32_to_cpu(sum_entry->nid) != parent_nid ||
    288 			sum_entry->version != version ||
    289 			le16_to_cpu(sum_entry->ofs_in_node) != idx_in_node) {
    290 		if (!c.fix_on || type < 0) {
    291 			DBG(0, "summary_entry.nid         [0x%x]\n",
    292 					le32_to_cpu(sum_entry->nid));
    293 			DBG(0, "summary_entry.version     [0x%x]\n",
    294 					sum_entry->version);
    295 			DBG(0, "summary_entry.ofs_in_node [0x%x]\n",
    296 					le16_to_cpu(sum_entry->ofs_in_node));
    297 			DBG(0, "parent nid                [0x%x]\n",
    298 					parent_nid);
    299 			DBG(0, "version from nat          [0x%x]\n", version);
    300 			DBG(0, "idx in parent node        [0x%x]\n",
    301 					idx_in_node);
    302 
    303 			DBG(0, "Target data block addr    [0x%x]\n", blk_addr);
    304 			ASSERT_MSG("Invalid data seg summary\n");
    305 			ret = -EINVAL;
    306 		} else if (is_valid_summary(sbi, sum_entry, blk_addr)) {
    307 			/* delete wrong index */
    308 			ret = -EINVAL;
    309 		} else {
    310 			FIX_MSG("Set data summary 0x%x -> [0x%x] [0x%x] [0x%x]",
    311 					segno, parent_nid, version, idx_in_node);
    312 			sum_entry->nid = cpu_to_le32(parent_nid);
    313 			sum_entry->version = version;
    314 			sum_entry->ofs_in_node = cpu_to_le16(idx_in_node);
    315 			need_fix = 1;
    316 		}
    317 	}
    318 	if (need_fix && !c.ro) {
    319 		u64 ssa_blk;
    320 		int ret2;
    321 
    322 		ssa_blk = GET_SUM_BLKADDR(sbi, segno);
    323 		ret2 = dev_write_block(sum_blk, ssa_blk);
    324 		ASSERT(ret2 >= 0);
    325 	}
    326 out:
    327 	if (type == SEG_TYPE_NODE || type == SEG_TYPE_DATA ||
    328 					type == SEG_TYPE_MAX)
    329 		free(sum_blk);
    330 	return ret;
    331 }
    332 
    333 static int __check_inode_mode(u32 nid, enum FILE_TYPE ftype, u32 mode)
    334 {
    335 	if (ftype >= F2FS_FT_MAX)
    336 		return 0;
    337 	if (S_ISLNK(mode) && ftype != F2FS_FT_SYMLINK)
    338 		goto err;
    339 	if (S_ISREG(mode) && ftype != F2FS_FT_REG_FILE)
    340 		goto err;
    341 	if (S_ISDIR(mode) && ftype != F2FS_FT_DIR)
    342 		goto err;
    343 	if (S_ISCHR(mode) && ftype != F2FS_FT_CHRDEV)
    344 		goto err;
    345 	if (S_ISBLK(mode) && ftype != F2FS_FT_BLKDEV)
    346 		goto err;
    347 	if (S_ISFIFO(mode) && ftype != F2FS_FT_FIFO)
    348 		goto err;
    349 	if (S_ISSOCK(mode) && ftype != F2FS_FT_SOCK)
    350 		goto err;
    351 	return 0;
    352 err:
    353 	ASSERT_MSG("mismatch i_mode [0x%x] [0x%x vs. 0x%x]", nid, ftype, mode);
    354 	return -1;
    355 }
    356 
    357 static int sanity_check_nid(struct f2fs_sb_info *sbi, u32 nid,
    358 			struct f2fs_node *node_blk,
    359 			enum FILE_TYPE ftype, enum NODE_TYPE ntype,
    360 			struct node_info *ni)
    361 {
    362 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
    363 	int ret;
    364 
    365 	if (!IS_VALID_NID(sbi, nid)) {
    366 		ASSERT_MSG("nid is not valid. [0x%x]", nid);
    367 		return -EINVAL;
    368 	}
    369 
    370 	get_node_info(sbi, nid, ni);
    371 	if (ni->ino == 0) {
    372 		ASSERT_MSG("nid[0x%x] ino is 0", nid);
    373 		return -EINVAL;
    374 	}
    375 
    376 	if (ni->blk_addr == NEW_ADDR) {
    377 		ASSERT_MSG("nid is NEW_ADDR. [0x%x]", nid);
    378 		return -EINVAL;
    379 	}
    380 
    381 	if (!IS_VALID_BLK_ADDR(sbi, ni->blk_addr)) {
    382 		ASSERT_MSG("blkaddress is not valid. [0x%x]", ni->blk_addr);
    383 		return -EINVAL;
    384 	}
    385 
    386 	ret = dev_read_block(node_blk, ni->blk_addr);
    387 	ASSERT(ret >= 0);
    388 
    389 	if (ntype == TYPE_INODE &&
    390 			node_blk->footer.nid != node_blk->footer.ino) {
    391 		ASSERT_MSG("nid[0x%x] footer.nid[0x%x] footer.ino[0x%x]",
    392 				nid, le32_to_cpu(node_blk->footer.nid),
    393 				le32_to_cpu(node_blk->footer.ino));
    394 		return -EINVAL;
    395 	}
    396 	if (ni->ino != le32_to_cpu(node_blk->footer.ino)) {
    397 		ASSERT_MSG("nid[0x%x] nat_entry->ino[0x%x] footer.ino[0x%x]",
    398 				nid, ni->ino, le32_to_cpu(node_blk->footer.ino));
    399 		return -EINVAL;
    400 	}
    401 	if (ntype != TYPE_INODE &&
    402 			node_blk->footer.nid == node_blk->footer.ino) {
    403 		ASSERT_MSG("nid[0x%x] footer.nid[0x%x] footer.ino[0x%x]",
    404 				nid, le32_to_cpu(node_blk->footer.nid),
    405 				le32_to_cpu(node_blk->footer.ino));
    406 		return -EINVAL;
    407 	}
    408 
    409 	if (le32_to_cpu(node_blk->footer.nid) != nid) {
    410 		ASSERT_MSG("nid[0x%x] blk_addr[0x%x] footer.nid[0x%x]",
    411 				nid, ni->blk_addr,
    412 				le32_to_cpu(node_blk->footer.nid));
    413 		return -EINVAL;
    414 	}
    415 
    416 	if (ntype == TYPE_XATTR) {
    417 		u32 flag = le32_to_cpu(node_blk->footer.flag);
    418 
    419 		if ((flag >> OFFSET_BIT_SHIFT) != XATTR_NODE_OFFSET) {
    420 			ASSERT_MSG("xnid[0x%x] has wrong ofs:[0x%x]",
    421 					nid, flag);
    422 			return -EINVAL;
    423 		}
    424 	}
    425 
    426 	if ((ntype == TYPE_INODE && ftype == F2FS_FT_DIR) ||
    427 			(ntype == TYPE_XATTR && ftype == F2FS_FT_XATTR)) {
    428 		/* not included '.' & '..' */
    429 		if (f2fs_test_main_bitmap(sbi, ni->blk_addr) != 0) {
    430 			ASSERT_MSG("Duplicated node blk. nid[0x%x][0x%x]\n",
    431 					nid, ni->blk_addr);
    432 			return -EINVAL;
    433 		}
    434 	}
    435 
    436 	/* this if only from fix_hard_links */
    437 	if (ftype == F2FS_FT_MAX)
    438 		return 0;
    439 
    440 	if (ntype == TYPE_INODE &&
    441 		__check_inode_mode(nid, ftype, le32_to_cpu(node_blk->i.i_mode)))
    442 		return -EINVAL;
    443 
    444 	/* workaround to fix later */
    445 	if (ftype != F2FS_FT_ORPHAN ||
    446 			f2fs_test_bit(nid, fsck->nat_area_bitmap) != 0)
    447 		f2fs_clear_bit(nid, fsck->nat_area_bitmap);
    448 	else
    449 		ASSERT_MSG("orphan or xattr nid is duplicated [0x%x]\n",
    450 				nid);
    451 
    452 	if (is_valid_ssa_node_blk(sbi, nid, ni->blk_addr)) {
    453 		ASSERT_MSG("summary node block is not valid. [0x%x]", nid);
    454 		return -EINVAL;
    455 	}
    456 
    457 	if (f2fs_test_sit_bitmap(sbi, ni->blk_addr) == 0)
    458 		ASSERT_MSG("SIT bitmap is 0x0. blk_addr[0x%x]",
    459 				ni->blk_addr);
    460 
    461 	if (f2fs_test_main_bitmap(sbi, ni->blk_addr) == 0) {
    462 		fsck->chk.valid_blk_cnt++;
    463 		fsck->chk.valid_node_cnt++;
    464 	}
    465 	return 0;
    466 }
    467 
    468 static int sanity_check_inode(struct f2fs_sb_info *sbi, struct f2fs_node *node)
    469 {
    470 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
    471 	struct f2fs_inode *fi = &node->i;
    472 
    473 	if (!(le16_to_cpu(fi->i_mode) & S_IFMT)) {
    474 		ASSERT_MSG("i_mode is not valid. [0x%x]", le16_to_cpu(fi->i_mode));
    475 		goto remove_node;
    476 	}
    477 
    478 	return 0;
    479 
    480 remove_node:
    481 	f2fs_set_bit(le32_to_cpu(node->footer.ino), fsck->nat_area_bitmap);
    482 	fsck->chk.valid_blk_cnt--;
    483 	fsck->chk.valid_node_cnt--;
    484 	return -EINVAL;
    485 }
    486 
    487 static int fsck_chk_xattr_blk(struct f2fs_sb_info *sbi, u32 ino,
    488 					u32 x_nid, u32 *blk_cnt)
    489 {
    490 	struct f2fs_node *node_blk = NULL;
    491 	struct node_info ni;
    492 	int ret = 0;
    493 
    494 	if (x_nid == 0x0)
    495 		return 0;
    496 
    497 	node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
    498 	ASSERT(node_blk != NULL);
    499 
    500 	/* Sanity check */
    501 	if (sanity_check_nid(sbi, x_nid, node_blk,
    502 				F2FS_FT_XATTR, TYPE_XATTR, &ni)) {
    503 		ret = -EINVAL;
    504 		goto out;
    505 	}
    506 
    507 	*blk_cnt = *blk_cnt + 1;
    508 	f2fs_set_main_bitmap(sbi, ni.blk_addr, CURSEG_COLD_NODE);
    509 	DBG(2, "ino[0x%x] x_nid[0x%x]\n", ino, x_nid);
    510 out:
    511 	free(node_blk);
    512 	return ret;
    513 }
    514 
    515 int fsck_chk_node_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
    516 		u32 nid, enum FILE_TYPE ftype, enum NODE_TYPE ntype,
    517 		u32 *blk_cnt, struct child_info *child)
    518 {
    519 	struct node_info ni;
    520 	struct f2fs_node *node_blk = NULL;
    521 
    522 	node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
    523 	ASSERT(node_blk != NULL);
    524 
    525 	if (sanity_check_nid(sbi, nid, node_blk, ftype, ntype, &ni))
    526 		goto err;
    527 
    528 	if (ntype == TYPE_INODE) {
    529 		struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
    530 
    531 		if (sanity_check_inode(sbi, node_blk))
    532 			goto err;
    533 		fsck_chk_inode_blk(sbi, nid, ftype, node_blk, blk_cnt, &ni, child);
    534 		quota_add_inode_usage(fsck->qctx, nid, &node_blk->i);
    535 	} else {
    536 		switch (ntype) {
    537 		case TYPE_DIRECT_NODE:
    538 			f2fs_set_main_bitmap(sbi, ni.blk_addr,
    539 							CURSEG_WARM_NODE);
    540 			fsck_chk_dnode_blk(sbi, inode, nid, ftype, node_blk,
    541 					blk_cnt, child, &ni);
    542 			break;
    543 		case TYPE_INDIRECT_NODE:
    544 			f2fs_set_main_bitmap(sbi, ni.blk_addr,
    545 							CURSEG_COLD_NODE);
    546 			fsck_chk_idnode_blk(sbi, inode, ftype, node_blk,
    547 					blk_cnt, child);
    548 			break;
    549 		case TYPE_DOUBLE_INDIRECT_NODE:
    550 			f2fs_set_main_bitmap(sbi, ni.blk_addr,
    551 							CURSEG_COLD_NODE);
    552 			fsck_chk_didnode_blk(sbi, inode, ftype, node_blk,
    553 					blk_cnt, child);
    554 			break;
    555 		default:
    556 			ASSERT(0);
    557 		}
    558 	}
    559 	free(node_blk);
    560 	return 0;
    561 err:
    562 	free(node_blk);
    563 	return -EINVAL;
    564 }
    565 
    566 static inline void get_extent_info(struct extent_info *ext,
    567 					struct f2fs_extent *i_ext)
    568 {
    569 	ext->fofs = le32_to_cpu(i_ext->fofs);
    570 	ext->blk = le32_to_cpu(i_ext->blk_addr);
    571 	ext->len = le32_to_cpu(i_ext->len);
    572 }
    573 
    574 static void check_extent_info(struct child_info *child,
    575 						block_t blkaddr, int last)
    576 {
    577 	struct extent_info *ei = &child->ei;
    578 	u32 pgofs = child->pgofs;
    579 	int is_hole = 0;
    580 
    581 	if (!ei->len)
    582 		return;
    583 
    584 	if (child->state & FSCK_UNMATCHED_EXTENT)
    585 		return;
    586 
    587 	if (last) {
    588 		/* hole exist in the back of extent */
    589 		if (child->last_blk != ei->blk + ei->len - 1)
    590 			child->state |= FSCK_UNMATCHED_EXTENT;
    591 		return;
    592 	}
    593 
    594 	if (blkaddr == NULL_ADDR || blkaddr == NEW_ADDR)
    595 		is_hole = 1;
    596 
    597 	if (pgofs >= ei->fofs && pgofs < ei->fofs + ei->len) {
    598 		/* unmatched blkaddr */
    599 		if (is_hole || (blkaddr != pgofs - ei->fofs + ei->blk))
    600 			goto unmatched;
    601 
    602 		if (!child->last_blk) {
    603 			/* hole exists in the front of extent */
    604 			if (pgofs != ei->fofs)
    605 				goto unmatched;
    606 		} else if (child->last_blk + 1 != blkaddr) {
    607 			/* hole exists in the middle of extent */
    608 			goto unmatched;
    609 		}
    610 		child->last_blk = blkaddr;
    611 		return;
    612 	}
    613 
    614 	if (is_hole)
    615 		return;
    616 
    617 	if (blkaddr < ei->blk || blkaddr >= ei->blk + ei->len)
    618 		return;
    619 	/* unmatched file offset */
    620 unmatched:
    621 	child->state |= FSCK_UNMATCHED_EXTENT;
    622 }
    623 
    624 /* start with valid nid and blkaddr */
    625 void fsck_chk_inode_blk(struct f2fs_sb_info *sbi, u32 nid,
    626 		enum FILE_TYPE ftype, struct f2fs_node *node_blk,
    627 		u32 *blk_cnt, struct node_info *ni, struct child_info *child_d)
    628 {
    629 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
    630 	struct child_info child;
    631 	enum NODE_TYPE ntype;
    632 	u32 i_links = le32_to_cpu(node_blk->i.i_links);
    633 	u64 i_size = le64_to_cpu(node_blk->i.i_size);
    634 	u64 i_blocks = le64_to_cpu(node_blk->i.i_blocks);
    635 	int ofs = get_extra_isize(node_blk);
    636 	unsigned char *en;
    637 	int namelen;
    638 	unsigned int idx = 0;
    639 	int need_fix = 0;
    640 	int ret;
    641 
    642 	memset(&child, 0, sizeof(child));
    643 	child.links = 2;
    644 	child.p_ino = nid;
    645 	child.pp_ino = le32_to_cpu(node_blk->i.i_pino);
    646 	child.dir_level = node_blk->i.i_dir_level;
    647 
    648 	if (f2fs_test_main_bitmap(sbi, ni->blk_addr) == 0)
    649 		fsck->chk.valid_inode_cnt++;
    650 
    651 	if (ftype == F2FS_FT_DIR) {
    652 		f2fs_set_main_bitmap(sbi, ni->blk_addr, CURSEG_HOT_NODE);
    653 	} else {
    654 		if (f2fs_test_main_bitmap(sbi, ni->blk_addr) == 0) {
    655 			f2fs_set_main_bitmap(sbi, ni->blk_addr,
    656 							CURSEG_WARM_NODE);
    657 			if (i_links > 1 && ftype != F2FS_FT_ORPHAN &&
    658 					!is_qf_ino(F2FS_RAW_SUPER(sbi), nid)) {
    659 				/* First time. Create new hard link node */
    660 				add_into_hard_link_list(sbi, nid, i_links);
    661 				fsck->chk.multi_hard_link_files++;
    662 			}
    663 		} else {
    664 			DBG(3, "[0x%x] has hard links [0x%x]\n", nid, i_links);
    665 			if (find_and_dec_hard_link_list(sbi, nid)) {
    666 				ASSERT_MSG("[0x%x] needs more i_links=0x%x",
    667 						nid, i_links);
    668 				if (c.fix_on) {
    669 					node_blk->i.i_links =
    670 						cpu_to_le32(i_links + 1);
    671 					need_fix = 1;
    672 					FIX_MSG("File: 0x%x "
    673 						"i_links= 0x%x -> 0x%x",
    674 						nid, i_links, i_links + 1);
    675 				}
    676 				goto skip_blkcnt_fix;
    677 			}
    678 			/* No need to go deep into the node */
    679 			return;
    680 		}
    681 	}
    682 
    683 	if (fsck_chk_xattr_blk(sbi, nid,
    684 			le32_to_cpu(node_blk->i.i_xattr_nid), blk_cnt) &&
    685 			c.fix_on) {
    686 		node_blk->i.i_xattr_nid = 0;
    687 		need_fix = 1;
    688 		FIX_MSG("Remove xattr block: 0x%x, x_nid = 0x%x",
    689 				nid, le32_to_cpu(node_blk->i.i_xattr_nid));
    690 	}
    691 
    692 	if (ftype == F2FS_FT_CHRDEV || ftype == F2FS_FT_BLKDEV ||
    693 			ftype == F2FS_FT_FIFO || ftype == F2FS_FT_SOCK)
    694 		goto check;
    695 
    696 	if ((node_blk->i.i_inline & F2FS_INLINE_DATA)) {
    697 		if (le32_to_cpu(node_blk->i.i_addr[ofs]) != 0) {
    698 			/* should fix this bug all the time */
    699 			FIX_MSG("inline_data has wrong 0'th block = %x",
    700 					le32_to_cpu(node_blk->i.i_addr[ofs]));
    701 			node_blk->i.i_addr[ofs] = 0;
    702 			node_blk->i.i_blocks = cpu_to_le64(*blk_cnt);
    703 			need_fix = 1;
    704 		}
    705 		if (!(node_blk->i.i_inline & F2FS_DATA_EXIST)) {
    706 			char buf[MAX_INLINE_DATA(node_blk)];
    707 			memset(buf, 0, MAX_INLINE_DATA(node_blk));
    708 
    709 			if (memcmp(buf, inline_data_addr(node_blk),
    710 						MAX_INLINE_DATA(node_blk))) {
    711 				FIX_MSG("inline_data has DATA_EXIST");
    712 				node_blk->i.i_inline |= F2FS_DATA_EXIST;
    713 				need_fix = 1;
    714 			}
    715 		}
    716 		DBG(3, "ino[0x%x] has inline data!\n", nid);
    717 		goto check;
    718 	}
    719 
    720 	if ((node_blk->i.i_inline & F2FS_INLINE_DENTRY)) {
    721 		DBG(3, "ino[0x%x] has inline dentry!\n", nid);
    722 		if (le32_to_cpu(node_blk->i.i_addr[ofs]) != 0) {
    723 			/* should fix this bug all the time */
    724 			FIX_MSG("inline_dentry has wrong 0'th block = %x",
    725 					le32_to_cpu(node_blk->i.i_addr[ofs]));
    726 			node_blk->i.i_addr[ofs] = 0;
    727 			node_blk->i.i_blocks = cpu_to_le64(*blk_cnt);
    728 			need_fix = 1;
    729 		}
    730 
    731 		ret = fsck_chk_inline_dentries(sbi, node_blk, &child);
    732 		if (ret < 0) {
    733 			/* should fix this bug all the time */
    734 			need_fix = 1;
    735 		}
    736 		goto check;
    737 	}
    738 
    739 	/* readahead node blocks */
    740 	for (idx = 0; idx < 5; idx++) {
    741 		u32 nid = le32_to_cpu(node_blk->i.i_nid[idx]);
    742 
    743 		if (nid != 0 && IS_VALID_NID(sbi, nid)) {
    744 			struct node_info ni;
    745 
    746 			get_node_info(sbi, nid, &ni);
    747 			if (IS_VALID_BLK_ADDR(sbi, ni.blk_addr))
    748 				dev_reada_block(ni.blk_addr);
    749 		}
    750 	}
    751 
    752 	/* init extent info */
    753 	get_extent_info(&child.ei, &node_blk->i.i_ext);
    754 	child.last_blk = 0;
    755 
    756 	/* check data blocks in inode */
    757 	for (idx = 0; idx < ADDRS_PER_INODE(&node_blk->i);
    758 						idx++, child.pgofs++) {
    759 		block_t blkaddr = le32_to_cpu(node_blk->i.i_addr[ofs + idx]);
    760 
    761 		/* check extent info */
    762 		check_extent_info(&child, blkaddr, 0);
    763 
    764 		if (blkaddr != 0) {
    765 			ret = fsck_chk_data_blk(sbi,
    766 					blkaddr,
    767 					&child, (i_blocks == *blk_cnt),
    768 					ftype, nid, idx, ni->version,
    769 					file_is_encrypt(&node_blk->i));
    770 			if (!ret) {
    771 				*blk_cnt = *blk_cnt + 1;
    772 			} else if (c.fix_on) {
    773 				node_blk->i.i_addr[ofs + idx] = 0;
    774 				need_fix = 1;
    775 				FIX_MSG("[0x%x] i_addr[%d] = 0",
    776 							nid, ofs + idx);
    777 			}
    778 		}
    779 	}
    780 
    781 	/* check node blocks in inode */
    782 	for (idx = 0; idx < 5; idx++) {
    783 		nid_t i_nid = le32_to_cpu(node_blk->i.i_nid[idx]);
    784 
    785 		if (idx == 0 || idx == 1)
    786 			ntype = TYPE_DIRECT_NODE;
    787 		else if (idx == 2 || idx == 3)
    788 			ntype = TYPE_INDIRECT_NODE;
    789 		else if (idx == 4)
    790 			ntype = TYPE_DOUBLE_INDIRECT_NODE;
    791 		else
    792 			ASSERT(0);
    793 
    794 		if (i_nid == 0x0)
    795 			goto skip;
    796 
    797 		ret = fsck_chk_node_blk(sbi, &node_blk->i, i_nid,
    798 					ftype, ntype, blk_cnt, &child);
    799 		if (!ret) {
    800 			*blk_cnt = *blk_cnt + 1;
    801 		} else if (ret == -EINVAL) {
    802 			if (c.fix_on) {
    803 				node_blk->i.i_nid[idx] = 0;
    804 				need_fix = 1;
    805 				FIX_MSG("[0x%x] i_nid[%d] = 0", nid, idx);
    806 			}
    807 skip:
    808 			if (ntype == TYPE_DIRECT_NODE)
    809 				child.pgofs += ADDRS_PER_BLOCK;
    810 			else if (ntype == TYPE_INDIRECT_NODE)
    811 				child.pgofs += ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
    812 			else
    813 				child.pgofs += ADDRS_PER_BLOCK *
    814 						NIDS_PER_BLOCK * NIDS_PER_BLOCK;
    815 		}
    816 
    817 	}
    818 
    819 	/* check uncovered range in the back of extent */
    820 	check_extent_info(&child, 0, 1);
    821 
    822 	if (child.state & FSCK_UNMATCHED_EXTENT) {
    823 		ASSERT_MSG("ino: 0x%x has wrong ext: [pgofs:%u, blk:%u, len:%u]",
    824 				nid, child.ei.fofs, child.ei.blk, child.ei.len);
    825 		if (c.fix_on)
    826 			need_fix = 1;
    827 	}
    828 check:
    829 	if (i_blocks != *blk_cnt) {
    830 		ASSERT_MSG("ino: 0x%x has i_blocks: %08"PRIx64", "
    831 				"but has %u blocks",
    832 				nid, i_blocks, *blk_cnt);
    833 		if (c.fix_on) {
    834 			node_blk->i.i_blocks = cpu_to_le64(*blk_cnt);
    835 			need_fix = 1;
    836 			FIX_MSG("[0x%x] i_blocks=0x%08"PRIx64" -> 0x%x",
    837 					nid, i_blocks, *blk_cnt);
    838 		}
    839 	}
    840 skip_blkcnt_fix:
    841 	en = malloc(F2FS_NAME_LEN + 1);
    842 	ASSERT(en);
    843 
    844 	namelen = le32_to_cpu(node_blk->i.i_namelen);
    845 	if (namelen > F2FS_NAME_LEN) {
    846 		if (child_d && child_d->i_namelen <= F2FS_NAME_LEN) {
    847 			ASSERT_MSG("ino: 0x%x has i_namelen: 0x%x, "
    848 					"but has %d characters for name",
    849 					nid, namelen, child_d->i_namelen);
    850 			if (c.fix_on) {
    851 				FIX_MSG("[0x%x] i_namelen=0x%x -> 0x%x", nid, namelen,
    852 					child_d->i_namelen);
    853 				node_blk->i.i_namelen = cpu_to_le32(child_d->i_namelen);
    854 				need_fix = 1;
    855 			}
    856 			namelen = child_d->i_namelen;
    857 		} else
    858 			namelen = F2FS_NAME_LEN;
    859 	}
    860 	namelen = convert_encrypted_name(node_blk->i.i_name, namelen,
    861 					en, file_enc_name(&node_blk->i));
    862 	en[namelen] = '\0';
    863 	if (ftype == F2FS_FT_ORPHAN)
    864 		DBG(1, "Orphan Inode: 0x%x [%s] i_blocks: %u\n\n",
    865 				le32_to_cpu(node_blk->footer.ino),
    866 				en, (u32)i_blocks);
    867 
    868 	if (is_qf_ino(F2FS_RAW_SUPER(sbi), nid))
    869 		DBG(1, "Quota Inode: 0x%x [%s] i_blocks: %u\n\n",
    870 				le32_to_cpu(node_blk->footer.ino),
    871 				en, (u32)i_blocks);
    872 
    873 	if (ftype == F2FS_FT_DIR) {
    874 		DBG(1, "Directory Inode: 0x%x [%s] depth: %d has %d files\n\n",
    875 				le32_to_cpu(node_blk->footer.ino), en,
    876 				le32_to_cpu(node_blk->i.i_current_depth),
    877 				child.files);
    878 
    879 		if (i_links != child.links) {
    880 			ASSERT_MSG("ino: 0x%x i_links: %u, real links: %u",
    881 					nid, i_links, child.links);
    882 			if (c.fix_on) {
    883 				node_blk->i.i_links = cpu_to_le32(child.links);
    884 				need_fix = 1;
    885 				FIX_MSG("Dir: 0x%x i_links= 0x%x -> 0x%x",
    886 						nid, i_links, child.links);
    887 			}
    888 		}
    889 		if (child.dots < 2 &&
    890 				!(node_blk->i.i_inline & F2FS_INLINE_DOTS)) {
    891 			ASSERT_MSG("ino: 0x%x dots: %u",
    892 					nid, child.dots);
    893 			if (c.fix_on) {
    894 				node_blk->i.i_inline |= F2FS_INLINE_DOTS;
    895 				need_fix = 1;
    896 				FIX_MSG("Dir: 0x%x set inline_dots", nid);
    897 			}
    898 		}
    899 	}
    900 
    901 	free(en);
    902 
    903 	if (ftype == F2FS_FT_SYMLINK && i_blocks && i_size == 0) {
    904 		DBG(1, "ino: 0x%x i_blocks: %lu with zero i_size",
    905 						nid, (unsigned long)i_blocks);
    906 		if (c.fix_on) {
    907 			u64 i_size = i_blocks * F2FS_BLKSIZE;
    908 
    909 			node_blk->i.i_size = cpu_to_le64(i_size);
    910 			need_fix = 1;
    911 			FIX_MSG("Symlink: recover 0x%x with i_size=%lu",
    912 						nid, (unsigned long)i_size);
    913 		}
    914 	}
    915 
    916 	if (ftype == F2FS_FT_ORPHAN && i_links) {
    917 		MSG(0, "ino: 0x%x is orphan inode, but has i_links: %u",
    918 				nid, i_links);
    919 		if (c.fix_on) {
    920 			node_blk->i.i_links = 0;
    921 			need_fix = 1;
    922 			FIX_MSG("ino: 0x%x orphan_inode, i_links= 0x%x -> 0",
    923 					nid, i_links);
    924 		}
    925 	}
    926 
    927 	/* drop extent information to avoid potential wrong access */
    928 	if (need_fix && !c.ro)
    929 		node_blk->i.i_ext.len = 0;
    930 
    931 	if ((c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM)) &&
    932 				f2fs_has_extra_isize(&node_blk->i)) {
    933 		__u32 provided, calculated;
    934 
    935 		provided = le32_to_cpu(node_blk->i.i_inode_checksum);
    936 		calculated = f2fs_inode_chksum(node_blk);
    937 
    938 		if (provided != calculated) {
    939 			ASSERT_MSG("ino: 0x%x chksum:0x%x, but calculated one is: 0x%x",
    940 				nid, provided, calculated);
    941 			if (c.fix_on) {
    942 				node_blk->i.i_inode_checksum =
    943 							cpu_to_le32(calculated);
    944 				need_fix = 1;
    945 				FIX_MSG("ino: 0x%x recover, i_inode_checksum= 0x%x -> 0x%x",
    946 						nid, provided, calculated);
    947 			}
    948 		}
    949 	}
    950 
    951 	if (need_fix && !c.ro) {
    952 		ret = dev_write_block(node_blk, ni->blk_addr);
    953 		ASSERT(ret >= 0);
    954 	}
    955 }
    956 
    957 int fsck_chk_dnode_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
    958 		u32 nid, enum FILE_TYPE ftype, struct f2fs_node *node_blk,
    959 		u32 *blk_cnt, struct child_info *child, struct node_info *ni)
    960 {
    961 	int idx, ret;
    962 	int need_fix = 0;
    963 	child->p_ino = nid;
    964 	child->pp_ino = le32_to_cpu(inode->i_pino);
    965 
    966 	for (idx = 0; idx < ADDRS_PER_BLOCK; idx++, child->pgofs++) {
    967 		block_t blkaddr = le32_to_cpu(node_blk->dn.addr[idx]);
    968 
    969 		check_extent_info(child, blkaddr, 0);
    970 
    971 		if (blkaddr == 0x0)
    972 			continue;
    973 		ret = fsck_chk_data_blk(sbi,
    974 			blkaddr, child,
    975 			le64_to_cpu(inode->i_blocks) == *blk_cnt, ftype,
    976 			nid, idx, ni->version,
    977 			file_is_encrypt(inode));
    978 		if (!ret) {
    979 			*blk_cnt = *blk_cnt + 1;
    980 		} else if (c.fix_on) {
    981 			node_blk->dn.addr[idx] = 0;
    982 			need_fix = 1;
    983 			FIX_MSG("[0x%x] dn.addr[%d] = 0", nid, idx);
    984 		}
    985 	}
    986 	if (need_fix && !c.ro) {
    987 		ret = dev_write_block(node_blk, ni->blk_addr);
    988 		ASSERT(ret >= 0);
    989 	}
    990 	return 0;
    991 }
    992 
    993 int fsck_chk_idnode_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
    994 		enum FILE_TYPE ftype, struct f2fs_node *node_blk, u32 *blk_cnt,
    995 		struct child_info *child)
    996 {
    997 	int need_fix = 0, ret;
    998 	int i = 0;
    999 
   1000 	for (i = 0; i < NIDS_PER_BLOCK; i++) {
   1001 		if (le32_to_cpu(node_blk->in.nid[i]) == 0x0)
   1002 			goto skip;
   1003 		ret = fsck_chk_node_blk(sbi, inode,
   1004 				le32_to_cpu(node_blk->in.nid[i]),
   1005 				ftype, TYPE_DIRECT_NODE, blk_cnt, child);
   1006 		if (!ret)
   1007 			*blk_cnt = *blk_cnt + 1;
   1008 		else if (ret == -EINVAL) {
   1009 			if (!c.fix_on)
   1010 				printf("should delete in.nid[i] = 0;\n");
   1011 			else {
   1012 				node_blk->in.nid[i] = 0;
   1013 				need_fix = 1;
   1014 				FIX_MSG("Set indirect node 0x%x -> 0", i);
   1015 			}
   1016 skip:
   1017 			child->pgofs += ADDRS_PER_BLOCK;
   1018 		}
   1019 	}
   1020 
   1021 	if (need_fix && !c.ro) {
   1022 		struct node_info ni;
   1023 		nid_t nid = le32_to_cpu(node_blk->footer.nid);
   1024 
   1025 		get_node_info(sbi, nid, &ni);
   1026 		ret = dev_write_block(node_blk, ni.blk_addr);
   1027 		ASSERT(ret >= 0);
   1028 	}
   1029 
   1030 	return 0;
   1031 }
   1032 
   1033 int fsck_chk_didnode_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
   1034 		enum FILE_TYPE ftype, struct f2fs_node *node_blk, u32 *blk_cnt,
   1035 		struct child_info *child)
   1036 {
   1037 	int i = 0;
   1038 	int need_fix = 0, ret = 0;
   1039 
   1040 	for (i = 0; i < NIDS_PER_BLOCK; i++) {
   1041 		if (le32_to_cpu(node_blk->in.nid[i]) == 0x0)
   1042 			goto skip;
   1043 		ret = fsck_chk_node_blk(sbi, inode,
   1044 				le32_to_cpu(node_blk->in.nid[i]),
   1045 				ftype, TYPE_INDIRECT_NODE, blk_cnt, child);
   1046 		if (!ret)
   1047 			*blk_cnt = *blk_cnt + 1;
   1048 		else if (ret == -EINVAL) {
   1049 			if (!c.fix_on)
   1050 				printf("should delete in.nid[i] = 0;\n");
   1051 			else {
   1052 				node_blk->in.nid[i] = 0;
   1053 				need_fix = 1;
   1054 				FIX_MSG("Set double indirect node 0x%x -> 0", i);
   1055 			}
   1056 skip:
   1057 			child->pgofs += ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
   1058 		}
   1059 	}
   1060 
   1061 	if (need_fix && !c.ro) {
   1062 		struct node_info ni;
   1063 		nid_t nid = le32_to_cpu(node_blk->footer.nid);
   1064 
   1065 		get_node_info(sbi, nid, &ni);
   1066 		ret = dev_write_block(node_blk, ni.blk_addr);
   1067 		ASSERT(ret >= 0);
   1068 	}
   1069 
   1070 	return 0;
   1071 }
   1072 
   1073 static const char *lookup_table =
   1074         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
   1075 
   1076 /**
   1077  * digest_encode() -
   1078  *
   1079  * Encodes the input digest using characters from the set [a-zA-Z0-9_+].
   1080  * The encoded string is roughly 4/3 times the size of the input string.
   1081  */
   1082 static int digest_encode(const char *src, int len, char *dst)
   1083 {
   1084 	int i = 0, bits = 0, ac = 0;
   1085 	char *cp = dst;
   1086 
   1087 	while (i < len) {
   1088 		ac += (((unsigned char) src[i]) << bits);
   1089 		bits += 8;
   1090 		do {
   1091 			*cp++ = lookup_table[ac & 0x3f];
   1092 			ac >>= 6;
   1093 			bits -= 6;
   1094 		} while (bits >= 6);
   1095 		i++;
   1096 	}
   1097 	if (bits)
   1098 		*cp++ = lookup_table[ac & 0x3f];
   1099 	*cp = 0;
   1100 	return cp - dst;
   1101 }
   1102 
   1103 int convert_encrypted_name(unsigned char *name, int len,
   1104 				unsigned char *new, int enc_name)
   1105 {
   1106 	if (!enc_name) {
   1107 		if (len > F2FS_NAME_LEN)
   1108 			len = F2FS_NAME_LEN;
   1109 		memcpy(new, name, len);
   1110 		new[len] = 0;
   1111 		return len;
   1112 	}
   1113 
   1114 	*new = '_';
   1115 	return digest_encode((const char *)name, 24, (char *)new + 1);
   1116 }
   1117 
   1118 static void print_dentry(__u32 depth, __u8 *name,
   1119 		u8 *bitmap, struct f2fs_dir_entry *dentry,
   1120 		int max, int idx, int last_blk, int enc_name)
   1121 {
   1122 	int last_de = 0;
   1123 	int next_idx = 0;
   1124 	int name_len;
   1125 	unsigned int i;
   1126 	int bit_offset;
   1127 	unsigned char new[F2FS_NAME_LEN + 1];
   1128 
   1129 	if (!c.show_dentry)
   1130 		return;
   1131 
   1132 	name_len = le16_to_cpu(dentry[idx].name_len);
   1133 	next_idx = idx + (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN;
   1134 
   1135 	bit_offset = find_next_bit_le(bitmap, max, next_idx);
   1136 	if (bit_offset >= max && last_blk)
   1137 		last_de = 1;
   1138 
   1139 	if (tree_mark_size <= depth) {
   1140 		tree_mark_size *= 2;
   1141 		ASSERT(tree_mark_size != 0);
   1142 		tree_mark = realloc(tree_mark, tree_mark_size);
   1143 		ASSERT(tree_mark != NULL);
   1144 	}
   1145 
   1146 	if (last_de)
   1147 		tree_mark[depth] = '`';
   1148 	else
   1149 		tree_mark[depth] = '|';
   1150 
   1151 	if (tree_mark[depth - 1] == '`')
   1152 		tree_mark[depth - 1] = ' ';
   1153 
   1154 	for (i = 1; i < depth; i++)
   1155 		printf("%c   ", tree_mark[i]);
   1156 
   1157 	convert_encrypted_name(name, name_len, new, enc_name);
   1158 
   1159 	printf("%c-- %s <ino = 0x%x>, <encrypted (%d)>\n",
   1160 			last_de ? '`' : '|',
   1161 			new, le32_to_cpu(dentry[idx].ino),
   1162 			enc_name);
   1163 }
   1164 
   1165 static int f2fs_check_hash_code(struct f2fs_dir_entry *dentry,
   1166 			const unsigned char *name, u32 len, int enc_name)
   1167 {
   1168 	f2fs_hash_t hash_code = f2fs_dentry_hash(name, len);
   1169 
   1170 	/* fix hash_code made by old buggy code */
   1171 	if (dentry->hash_code != hash_code) {
   1172 		unsigned char new[F2FS_NAME_LEN + 1];
   1173 
   1174 		convert_encrypted_name((unsigned char *)name, len,
   1175 							new, enc_name);
   1176 		FIX_MSG("Mismatch hash_code for \"%s\" [%x:%x]",
   1177 				new, le32_to_cpu(dentry->hash_code),
   1178 				hash_code);
   1179 		dentry->hash_code = cpu_to_le32(hash_code);
   1180 		return 1;
   1181 	}
   1182 	return 0;
   1183 }
   1184 
   1185 
   1186 static int __get_current_level(int dir_level, u32 pgofs)
   1187 {
   1188 	unsigned int bidx = 0;
   1189 	int i;
   1190 
   1191 	for (i = 0; i < MAX_DIR_HASH_DEPTH; i++) {
   1192 		bidx += dir_buckets(i, dir_level) * bucket_blocks(i);
   1193 		if (bidx > pgofs)
   1194 			break;
   1195 	}
   1196 	return i;
   1197 }
   1198 
   1199 static int f2fs_check_dirent_position(u8 *name, u16 name_len, u32 pgofs,
   1200 						u8 dir_level, u32 pino)
   1201 {
   1202 	f2fs_hash_t namehash = f2fs_dentry_hash(name, name_len);
   1203 	unsigned int nbucket, nblock;
   1204 	unsigned int bidx, end_block;
   1205 	int level;
   1206 
   1207 	level = __get_current_level(dir_level, pgofs);
   1208 
   1209 	nbucket = dir_buckets(level, dir_level);
   1210 	nblock = bucket_blocks(level);
   1211 
   1212 	bidx = dir_block_index(level, dir_level,
   1213 					le32_to_cpu(namehash) % nbucket);
   1214 	end_block = bidx + nblock;
   1215 
   1216 	if (pgofs >= bidx && pgofs < end_block)
   1217 		return 0;
   1218 
   1219 	ASSERT_MSG("Wrong position of dirent pino:%u, name:%s, level:%d, "
   1220 		"dir_level:%d, pgofs:%u, correct range:[%u, %u]\n",
   1221 		pino, name, level, dir_level, pgofs, bidx, end_block - 1);
   1222 	return 1;
   1223 }
   1224 
   1225 static int __chk_dots_dentries(struct f2fs_sb_info *sbi,
   1226 			       struct f2fs_dir_entry *dentry,
   1227 			       struct child_info *child,
   1228 			       u8 *name, int len,
   1229 			       __u8 (*filename)[F2FS_SLOT_LEN],
   1230 			       int enc_name)
   1231 {
   1232 	int fixed = 0;
   1233 
   1234 	if ((name[0] == '.' && len == 1)) {
   1235 		if (le32_to_cpu(dentry->ino) != child->p_ino) {
   1236 			ASSERT_MSG("Bad inode number[0x%x] for '.', parent_ino is [0x%x]\n",
   1237 				le32_to_cpu(dentry->ino), child->p_ino);
   1238 			dentry->ino = cpu_to_le32(child->p_ino);
   1239 			fixed = 1;
   1240 		}
   1241 	}
   1242 
   1243 	if (name[0] == '.' && name[1] == '.' && len == 2) {
   1244 		if (child->p_ino == F2FS_ROOT_INO(sbi)) {
   1245 			if (le32_to_cpu(dentry->ino) != F2FS_ROOT_INO(sbi)) {
   1246 				ASSERT_MSG("Bad inode number[0x%x] for '..'\n",
   1247 					le32_to_cpu(dentry->ino));
   1248 				dentry->ino = cpu_to_le32(F2FS_ROOT_INO(sbi));
   1249 				fixed = 1;
   1250 			}
   1251 		} else if (le32_to_cpu(dentry->ino) != child->pp_ino) {
   1252 			ASSERT_MSG("Bad inode number[0x%x] for '..', parent parent ino is [0x%x]\n",
   1253 				le32_to_cpu(dentry->ino), child->pp_ino);
   1254 			dentry->ino = cpu_to_le32(child->pp_ino);
   1255 			fixed = 1;
   1256 		}
   1257 	}
   1258 
   1259 	if (f2fs_check_hash_code(dentry, name, len, enc_name))
   1260 		fixed = 1;
   1261 
   1262 	if (name[len] != '\0') {
   1263 		ASSERT_MSG("'.' is not NULL terminated\n");
   1264 		name[len] = '\0';
   1265 		memcpy(*filename, name, len);
   1266 		fixed = 1;
   1267 	}
   1268 	return fixed;
   1269 }
   1270 
   1271 static void nullify_dentry(struct f2fs_dir_entry *dentry, int offs,
   1272 			   __u8 (*filename)[F2FS_SLOT_LEN], u8 **bitmap)
   1273 {
   1274 	memset(dentry, 0, sizeof(struct f2fs_dir_entry));
   1275 	test_and_clear_bit_le(offs, *bitmap);
   1276 	memset(*filename, 0, F2FS_SLOT_LEN);
   1277 }
   1278 
   1279 static int __chk_dentries(struct f2fs_sb_info *sbi, struct child_info *child,
   1280 			u8 *bitmap, struct f2fs_dir_entry *dentry,
   1281 			__u8 (*filenames)[F2FS_SLOT_LEN],
   1282 			int max, int last_blk, int enc_name)
   1283 {
   1284 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
   1285 	enum FILE_TYPE ftype;
   1286 	int dentries = 0;
   1287 	u32 blk_cnt;
   1288 	u8 *name;
   1289 	unsigned char en[F2FS_NAME_LEN + 1];
   1290 	u16 name_len, en_len;
   1291 	int ret = 0;
   1292 	int fixed = 0;
   1293 	int i, slots;
   1294 
   1295 	/* readahead inode blocks */
   1296 	for (i = 0; i < max; i++) {
   1297 		u32 ino;
   1298 
   1299 		if (test_bit_le(i, bitmap) == 0)
   1300 			continue;
   1301 
   1302 		ino = le32_to_cpu(dentry[i].ino);
   1303 
   1304 		if (IS_VALID_NID(sbi, ino)) {
   1305 			struct node_info ni;
   1306 
   1307 			get_node_info(sbi, ino, &ni);
   1308 			if (IS_VALID_BLK_ADDR(sbi, ni.blk_addr)) {
   1309 				dev_reada_block(ni.blk_addr);
   1310 				name_len = le16_to_cpu(dentry[i].name_len);
   1311 				if (name_len > 0)
   1312 					i += (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN - 1;
   1313 			}
   1314 		}
   1315 	}
   1316 
   1317 	for (i = 0; i < max;) {
   1318 		if (test_bit_le(i, bitmap) == 0) {
   1319 			i++;
   1320 			continue;
   1321 		}
   1322 		if (!IS_VALID_NID(sbi, le32_to_cpu(dentry[i].ino))) {
   1323 			ASSERT_MSG("Bad dentry 0x%x with invalid NID/ino 0x%x",
   1324 				    i, le32_to_cpu(dentry[i].ino));
   1325 			if (c.fix_on) {
   1326 				FIX_MSG("Clear bad dentry 0x%x with bad ino 0x%x",
   1327 					i, le32_to_cpu(dentry[i].ino));
   1328 				test_and_clear_bit_le(i, bitmap);
   1329 				fixed = 1;
   1330 			}
   1331 			i++;
   1332 			continue;
   1333 		}
   1334 
   1335 		ftype = dentry[i].file_type;
   1336 		if ((ftype <= F2FS_FT_UNKNOWN || ftype > F2FS_FT_LAST_FILE_TYPE)) {
   1337 			ASSERT_MSG("Bad dentry 0x%x with unexpected ftype 0x%x",
   1338 						le32_to_cpu(dentry[i].ino), ftype);
   1339 			if (c.fix_on) {
   1340 				FIX_MSG("Clear bad dentry 0x%x with bad ftype 0x%x",
   1341 					i, ftype);
   1342 				test_and_clear_bit_le(i, bitmap);
   1343 				fixed = 1;
   1344 			}
   1345 			i++;
   1346 			continue;
   1347 		}
   1348 
   1349 		name_len = le16_to_cpu(dentry[i].name_len);
   1350 
   1351 		if (name_len == 0 || name_len > F2FS_NAME_LEN) {
   1352 			ASSERT_MSG("Bad dentry 0x%x with invalid name_len", i);
   1353 			if (c.fix_on) {
   1354 				FIX_MSG("Clear bad dentry 0x%x", i);
   1355 				test_and_clear_bit_le(i, bitmap);
   1356 				fixed = 1;
   1357 			}
   1358 			i++;
   1359 			continue;
   1360 		}
   1361 		name = calloc(name_len + 1, 1);
   1362 		memcpy(name, filenames[i], name_len);
   1363 		slots = (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN;
   1364 
   1365 		/* Becareful. 'dentry.file_type' is not imode. */
   1366 		if (ftype == F2FS_FT_DIR) {
   1367 			if ((name[0] == '.' && name_len == 1) ||
   1368 				(name[0] == '.' && name[1] == '.' &&
   1369 							name_len == 2)) {
   1370 				ret = __chk_dots_dentries(sbi, &dentry[i],
   1371 					child, name, name_len, &filenames[i],
   1372 					enc_name);
   1373 				switch (ret) {
   1374 				case 1:
   1375 					fixed = 1;
   1376 				case 0:
   1377 					child->dots++;
   1378 					break;
   1379 				}
   1380 
   1381 				if (child->dots > 2) {
   1382 					ASSERT_MSG("More than one '.' or '..', should delete the extra one\n");
   1383 					nullify_dentry(&dentry[i], i,
   1384 						       &filenames[i], &bitmap);
   1385 					child->dots--;
   1386 					fixed = 1;
   1387 				}
   1388 
   1389 				i++;
   1390 				free(name);
   1391 				continue;
   1392 			}
   1393 		}
   1394 
   1395 		if (f2fs_check_hash_code(dentry + i, name, name_len, enc_name))
   1396 			fixed = 1;
   1397 
   1398 		if (max == NR_DENTRY_IN_BLOCK) {
   1399 			ret = f2fs_check_dirent_position(name, name_len,
   1400 					child->pgofs,
   1401 					child->dir_level, child->p_ino);
   1402 			if (ret) {
   1403 				if (c.fix_on) {
   1404 					FIX_MSG("Clear bad dentry 0x%x", i);
   1405 					test_and_clear_bit_le(i, bitmap);
   1406 					fixed = 1;
   1407 				}
   1408 				i++;
   1409 				free(name);
   1410 				continue;
   1411 			}
   1412 		}
   1413 
   1414 		en_len = convert_encrypted_name(name, name_len, en, enc_name);
   1415 		en[en_len] = '\0';
   1416 		DBG(1, "[%3u]-[0x%x] name[%s] len[0x%x] ino[0x%x] type[0x%x]\n",
   1417 				fsck->dentry_depth, i, en, name_len,
   1418 				le32_to_cpu(dentry[i].ino),
   1419 				dentry[i].file_type);
   1420 
   1421 		print_dentry(fsck->dentry_depth, name, bitmap,
   1422 				dentry, max, i, last_blk, enc_name);
   1423 
   1424 		blk_cnt = 1;
   1425 		child->i_namelen = name_len;
   1426 		ret = fsck_chk_node_blk(sbi,
   1427 				NULL, le32_to_cpu(dentry[i].ino),
   1428 				ftype, TYPE_INODE, &blk_cnt, child);
   1429 
   1430 		if (ret && c.fix_on) {
   1431 			int j;
   1432 
   1433 			for (j = 0; j < slots; j++)
   1434 				test_and_clear_bit_le(i + j, bitmap);
   1435 			FIX_MSG("Unlink [0x%x] - %s len[0x%x], type[0x%x]",
   1436 					le32_to_cpu(dentry[i].ino),
   1437 					en, name_len,
   1438 					dentry[i].file_type);
   1439 			fixed = 1;
   1440 		} else if (ret == 0) {
   1441 			if (ftype == F2FS_FT_DIR)
   1442 				child->links++;
   1443 			dentries++;
   1444 			child->files++;
   1445 		}
   1446 
   1447 		i += slots;
   1448 		free(name);
   1449 	}
   1450 	return fixed ? -1 : dentries;
   1451 }
   1452 
   1453 int fsck_chk_inline_dentries(struct f2fs_sb_info *sbi,
   1454 		struct f2fs_node *node_blk, struct child_info *child)
   1455 {
   1456 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
   1457 	struct f2fs_dentry_ptr d;
   1458 	void *inline_dentry;
   1459 	int dentries;
   1460 
   1461 	inline_dentry = inline_data_addr(node_blk);
   1462 	ASSERT(inline_dentry != NULL);
   1463 
   1464 	make_dentry_ptr(&d, node_blk, inline_dentry, 2);
   1465 
   1466 	fsck->dentry_depth++;
   1467 	dentries = __chk_dentries(sbi, child,
   1468 			d.bitmap, d.dentry, d.filename, d.max, 1,
   1469 			file_is_encrypt(&node_blk->i));
   1470 	if (dentries < 0) {
   1471 		DBG(1, "[%3d] Inline Dentry Block Fixed hash_codes\n\n",
   1472 			fsck->dentry_depth);
   1473 	} else {
   1474 		DBG(1, "[%3d] Inline Dentry Block Done : "
   1475 				"dentries:%d in %d slots (len:%d)\n\n",
   1476 			fsck->dentry_depth, dentries,
   1477 			d.max, F2FS_NAME_LEN);
   1478 	}
   1479 	fsck->dentry_depth--;
   1480 	return dentries;
   1481 }
   1482 
   1483 int fsck_chk_dentry_blk(struct f2fs_sb_info *sbi, u32 blk_addr,
   1484 		struct child_info *child, int last_blk, int enc_name)
   1485 {
   1486 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
   1487 	struct f2fs_dentry_block *de_blk;
   1488 	int dentries, ret;
   1489 
   1490 	de_blk = (struct f2fs_dentry_block *)calloc(BLOCK_SZ, 1);
   1491 	ASSERT(de_blk != NULL);
   1492 
   1493 	ret = dev_read_block(de_blk, blk_addr);
   1494 	ASSERT(ret >= 0);
   1495 
   1496 	fsck->dentry_depth++;
   1497 	dentries = __chk_dentries(sbi, child,
   1498 			de_blk->dentry_bitmap,
   1499 			de_blk->dentry, de_blk->filename,
   1500 			NR_DENTRY_IN_BLOCK, last_blk, enc_name);
   1501 
   1502 	if (dentries < 0 && !c.ro) {
   1503 		ret = dev_write_block(de_blk, blk_addr);
   1504 		ASSERT(ret >= 0);
   1505 		DBG(1, "[%3d] Dentry Block [0x%x] Fixed hash_codes\n\n",
   1506 			fsck->dentry_depth, blk_addr);
   1507 	} else {
   1508 		DBG(1, "[%3d] Dentry Block [0x%x] Done : "
   1509 				"dentries:%d in %d slots (len:%d)\n\n",
   1510 			fsck->dentry_depth, blk_addr, dentries,
   1511 			NR_DENTRY_IN_BLOCK, F2FS_NAME_LEN);
   1512 	}
   1513 	fsck->dentry_depth--;
   1514 	free(de_blk);
   1515 	return 0;
   1516 }
   1517 
   1518 int fsck_chk_data_blk(struct f2fs_sb_info *sbi, u32 blk_addr,
   1519 		struct child_info *child, int last_blk,
   1520 		enum FILE_TYPE ftype, u32 parent_nid, u16 idx_in_node, u8 ver,
   1521 		int enc_name)
   1522 {
   1523 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
   1524 
   1525 	/* Is it reserved block? */
   1526 	if (blk_addr == NEW_ADDR) {
   1527 		fsck->chk.valid_blk_cnt++;
   1528 		return 0;
   1529 	}
   1530 
   1531 	if (!IS_VALID_BLK_ADDR(sbi, blk_addr)) {
   1532 		ASSERT_MSG("blkaddress is not valid. [0x%x]", blk_addr);
   1533 		return -EINVAL;
   1534 	}
   1535 
   1536 	if (is_valid_ssa_data_blk(sbi, blk_addr, parent_nid,
   1537 						idx_in_node, ver)) {
   1538 		ASSERT_MSG("summary data block is not valid. [0x%x]",
   1539 						parent_nid);
   1540 		return -EINVAL;
   1541 	}
   1542 
   1543 	if (f2fs_test_sit_bitmap(sbi, blk_addr) == 0)
   1544 		ASSERT_MSG("SIT bitmap is 0x0. blk_addr[0x%x]", blk_addr);
   1545 
   1546 	if (f2fs_test_main_bitmap(sbi, blk_addr) != 0)
   1547 		ASSERT_MSG("Duplicated data [0x%x]. pnid[0x%x] idx[0x%x]",
   1548 				blk_addr, parent_nid, idx_in_node);
   1549 
   1550 	fsck->chk.valid_blk_cnt++;
   1551 
   1552 	if (ftype == F2FS_FT_DIR) {
   1553 		f2fs_set_main_bitmap(sbi, blk_addr, CURSEG_HOT_DATA);
   1554 		return fsck_chk_dentry_blk(sbi, blk_addr, child,
   1555 						last_blk, enc_name);
   1556 	} else {
   1557 		f2fs_set_main_bitmap(sbi, blk_addr, CURSEG_WARM_DATA);
   1558 	}
   1559 	return 0;
   1560 }
   1561 
   1562 int fsck_chk_orphan_node(struct f2fs_sb_info *sbi)
   1563 {
   1564 	u32 blk_cnt = 0;
   1565 	block_t start_blk, orphan_blkaddr, i, j;
   1566 	struct f2fs_orphan_block *orphan_blk, *new_blk;
   1567 	struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
   1568 	u32 entry_count;
   1569 
   1570 	if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG))
   1571 		return 0;
   1572 
   1573 	start_blk = __start_cp_addr(sbi) + 1 + get_sb(cp_payload);
   1574 	orphan_blkaddr = __start_sum_addr(sbi) - 1 - get_sb(cp_payload);
   1575 
   1576 	orphan_blk = calloc(BLOCK_SZ, 1);
   1577 	ASSERT(orphan_blk);
   1578 
   1579 	new_blk = calloc(BLOCK_SZ, 1);
   1580 	ASSERT(new_blk);
   1581 
   1582 	for (i = 0; i < orphan_blkaddr; i++) {
   1583 		int ret = dev_read_block(orphan_blk, start_blk + i);
   1584 		u32 new_entry_count = 0;
   1585 
   1586 		ASSERT(ret >= 0);
   1587 		entry_count = le32_to_cpu(orphan_blk->entry_count);
   1588 
   1589 		for (j = 0; j < entry_count; j++) {
   1590 			nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
   1591 			DBG(1, "[%3d] ino [0x%x]\n", i, ino);
   1592 			struct node_info ni;
   1593 			blk_cnt = 1;
   1594 
   1595 			if (c.preen_mode == PREEN_MODE_1 && !c.fix_on) {
   1596 				get_node_info(sbi, ino, &ni);
   1597 				if (!IS_VALID_NID(sbi, ino) ||
   1598 						!IS_VALID_BLK_ADDR(sbi, ni.blk_addr))
   1599 					return -EINVAL;
   1600 
   1601 				continue;
   1602 			}
   1603 
   1604 			ret = fsck_chk_node_blk(sbi, NULL, ino,
   1605 					F2FS_FT_ORPHAN, TYPE_INODE, &blk_cnt,
   1606 					NULL);
   1607 			if (!ret)
   1608 				new_blk->ino[new_entry_count++] =
   1609 							orphan_blk->ino[j];
   1610 			else if (ret && c.fix_on)
   1611 				FIX_MSG("[0x%x] remove from orphan list", ino);
   1612 			else if (ret)
   1613 				ASSERT_MSG("[0x%x] wrong orphan inode", ino);
   1614 		}
   1615 		if (!c.ro && c.fix_on &&
   1616 				entry_count != new_entry_count) {
   1617 			new_blk->entry_count = cpu_to_le32(new_entry_count);
   1618 			ret = dev_write_block(new_blk, start_blk + i);
   1619 			ASSERT(ret >= 0);
   1620 		}
   1621 		memset(orphan_blk, 0, BLOCK_SZ);
   1622 		memset(new_blk, 0, BLOCK_SZ);
   1623 	}
   1624 	free(orphan_blk);
   1625 	free(new_blk);
   1626 
   1627 	return 0;
   1628 }
   1629 
   1630 int fsck_chk_quota_node(struct f2fs_sb_info *sbi)
   1631 {
   1632 	struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
   1633 	enum quota_type qtype;
   1634 	int ret = 0;
   1635 	u32 blk_cnt = 0;
   1636 
   1637 	for (qtype = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
   1638 		if (sb->qf_ino[qtype] == 0)
   1639 			continue;
   1640 		nid_t ino = QUOTA_INO(sb, qtype);
   1641 		struct node_info ni;
   1642 
   1643 		DBG(1, "[%3d] ino [0x%x]\n", qtype, ino);
   1644 		blk_cnt = 1;
   1645 
   1646 		if (c.preen_mode == PREEN_MODE_1 && !c.fix_on) {
   1647 			get_node_info(sbi, ino, &ni);
   1648 			if (!IS_VALID_NID(sbi, ino) ||
   1649 					!IS_VALID_BLK_ADDR(sbi, ni.blk_addr))
   1650 				return -EINVAL;
   1651 		}
   1652 		ret = fsck_chk_node_blk(sbi, NULL, ino,
   1653 				F2FS_FT_REG_FILE, TYPE_INODE, &blk_cnt, NULL);
   1654 		if (ret)
   1655 			ASSERT_MSG("[0x%x] wrong orphan inode", ino);
   1656 	}
   1657 	return ret;
   1658 }
   1659 
   1660 int fsck_chk_quota_files(struct f2fs_sb_info *sbi)
   1661 {
   1662 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
   1663 	struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
   1664 	enum quota_type qtype;
   1665 	f2fs_ino_t ino;
   1666 	int ret = 0;
   1667 	int needs_writeout;
   1668 
   1669 	/* Return if quota feature is disabled */
   1670 	if (!fsck->qctx)
   1671 		return 0;
   1672 
   1673 	for (qtype = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
   1674 		ino = sb->qf_ino[qtype];
   1675 		if (!ino)
   1676 			continue;
   1677 
   1678 	        DBG(1, "Checking Quota file ([%3d] ino [0x%x])\n", qtype, ino);
   1679 		needs_writeout = 0;
   1680 		ret = quota_compare_and_update(sbi, qtype, &needs_writeout,
   1681 						c.preserve_limits);
   1682 		if (ret == 0 && needs_writeout == 0) {
   1683 			DBG(1, "OK\n");
   1684 			continue;
   1685 		}
   1686 
   1687 		/* Something is wrong */
   1688 		if (c.fix_on) {
   1689 			DBG(0, "Fixing Quota file ([%3d] ino [0x%x])\n",
   1690 							qtype, ino);
   1691 			f2fs_filesize_update(sbi, ino, 0);
   1692 			ret = quota_write_inode(sbi, qtype);
   1693 			if (!ret) {
   1694 				c.bug_on = 1;
   1695 				DBG(1, "OK\n");
   1696 			} else {
   1697 				ASSERT_MSG("Unable to write quota file");
   1698 			}
   1699 		} else {
   1700 			ASSERT_MSG("Quota file is missing or invalid"
   1701 					" quota file content found.");
   1702 		}
   1703 	}
   1704 	return ret;
   1705 }
   1706 
   1707 int fsck_chk_meta(struct f2fs_sb_info *sbi)
   1708 {
   1709 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
   1710 	struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
   1711 	struct seg_entry *se;
   1712 	unsigned int sit_valid_segs = 0, sit_node_blks = 0;
   1713 	unsigned int i;
   1714 
   1715 	/* 1. check sit usage with CP: curseg is lost? */
   1716 	for (i = 0; i < TOTAL_SEGS(sbi); i++) {
   1717 		se = get_seg_entry(sbi, i);
   1718 		if (se->valid_blocks != 0)
   1719 			sit_valid_segs++;
   1720 		else if (IS_CUR_SEGNO(sbi, i, NO_CHECK_TYPE)) {
   1721 			/* curseg has not been written back to device */
   1722 			MSG(1, "\tInfo: curseg %u is counted in valid segs\n", i);
   1723 			sit_valid_segs++;
   1724 		}
   1725 		if (IS_NODESEG(se->type))
   1726 			sit_node_blks += se->valid_blocks;
   1727 	}
   1728 	if (fsck->chk.sit_free_segs + sit_valid_segs != TOTAL_SEGS(sbi)) {
   1729 		ASSERT_MSG("SIT usage does not match: sit_free_segs %u, "
   1730 				"sit_valid_segs %u, total_segs %u",
   1731 			fsck->chk.sit_free_segs, sit_valid_segs,
   1732 			TOTAL_SEGS(sbi));
   1733 		return -EINVAL;
   1734 	}
   1735 
   1736 	/* 2. check node count */
   1737 	if (fsck->chk.valid_nat_entry_cnt != sit_node_blks) {
   1738 		ASSERT_MSG("node count does not match: valid_nat_entry_cnt %u,"
   1739 			" sit_node_blks %u",
   1740 			fsck->chk.valid_nat_entry_cnt, sit_node_blks);
   1741 		return -EINVAL;
   1742 	}
   1743 
   1744 	/* 3. check SIT with CP */
   1745 	if (fsck->chk.sit_free_segs != le32_to_cpu(cp->free_segment_count)) {
   1746 		ASSERT_MSG("free segs does not match: sit_free_segs %u, "
   1747 				"free_segment_count %u",
   1748 				fsck->chk.sit_free_segs,
   1749 				le32_to_cpu(cp->free_segment_count));
   1750 		return -EINVAL;
   1751 	}
   1752 
   1753 	/* 4. check NAT with CP */
   1754 	if (fsck->chk.valid_nat_entry_cnt !=
   1755 					le32_to_cpu(cp->valid_node_count)) {
   1756 		ASSERT_MSG("valid node does not match: valid_nat_entry_cnt %u,"
   1757 				" valid_node_count %u",
   1758 				fsck->chk.valid_nat_entry_cnt,
   1759 				le32_to_cpu(cp->valid_node_count));
   1760 		return -EINVAL;
   1761 	}
   1762 
   1763 	/* 4. check orphan inode simply */
   1764 	if (fsck_chk_orphan_node(sbi))
   1765 		return -EINVAL;
   1766 
   1767 	/* 5. check nat entry -- must be done before quota check */
   1768 	for (i = 0; i < fsck->nr_nat_entries; i++) {
   1769 		u32 blk = le32_to_cpu(fsck->entries[i].block_addr);
   1770 		nid_t ino = le32_to_cpu(fsck->entries[i].ino);
   1771 
   1772 		if (!blk)
   1773 			/*
   1774 			 * skip entry whose ino is 0, otherwise, we will
   1775 			 * get a negative number by BLKOFF_FROM_MAIN(sbi, blk)
   1776 			 */
   1777 			continue;
   1778 
   1779 		if (!IS_VALID_BLK_ADDR(sbi, blk)) {
   1780 			MSG(0, "\tError: nat entry[ino %u block_addr 0x%x]"
   1781 				" is in valid\n",
   1782 				ino, blk);
   1783 			return -EINVAL;
   1784 		}
   1785 
   1786 		if (!f2fs_test_sit_bitmap(sbi, blk)) {
   1787 			MSG(0, "\tError: nat entry[ino %u block_addr 0x%x]"
   1788 				" not find it in sit_area_bitmap\n",
   1789 				ino, blk);
   1790 			return -EINVAL;
   1791 		}
   1792 
   1793 		if (!IS_VALID_NID(sbi, ino)) {
   1794 			MSG(0, "\tError: nat_entry->ino %u exceeds the range"
   1795 				" of nat entries %u\n",
   1796 				ino, fsck->nr_nat_entries);
   1797 			return -EINVAL;
   1798 		}
   1799 
   1800 		if (!f2fs_test_bit(ino, fsck->nat_area_bitmap)) {
   1801 			MSG(0, "\tError: nat_entry->ino %u is not set in"
   1802 				" nat_area_bitmap\n", ino);
   1803 			return -EINVAL;
   1804 		}
   1805 	}
   1806 
   1807 	/* 6. check quota inode simply */
   1808 	if (fsck_chk_quota_node(sbi))
   1809 		return -EINVAL;
   1810 
   1811 	if (fsck->nat_valid_inode_cnt != le32_to_cpu(cp->valid_inode_count)) {
   1812 		ASSERT_MSG("valid inode does not match: nat_valid_inode_cnt %u,"
   1813 				" valid_inode_count %u",
   1814 				fsck->nat_valid_inode_cnt,
   1815 				le32_to_cpu(cp->valid_inode_count));
   1816 		return -EINVAL;
   1817 	}
   1818 
   1819 	return 0;
   1820 }
   1821 
   1822 void fsck_init(struct f2fs_sb_info *sbi)
   1823 {
   1824 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
   1825 	struct f2fs_sm_info *sm_i = SM_I(sbi);
   1826 
   1827 	/*
   1828 	 * We build three bitmap for main/sit/nat so that may check consistency
   1829 	 * of filesystem.
   1830 	 * 1. main_area_bitmap will be used to check whether all blocks of main
   1831 	 *    area is used or not.
   1832 	 * 2. nat_area_bitmap has bitmap information of used nid in NAT.
   1833 	 * 3. sit_area_bitmap has bitmap information of used main block.
   1834 	 * At Last sequence, we compare main_area_bitmap with sit_area_bitmap.
   1835 	 */
   1836 	fsck->nr_main_blks = sm_i->main_segments << sbi->log_blocks_per_seg;
   1837 	fsck->main_area_bitmap_sz = (fsck->nr_main_blks + 7) / 8;
   1838 	fsck->main_area_bitmap = calloc(fsck->main_area_bitmap_sz, 1);
   1839 	ASSERT(fsck->main_area_bitmap != NULL);
   1840 
   1841 	build_nat_area_bitmap(sbi);
   1842 
   1843 	build_sit_area_bitmap(sbi);
   1844 
   1845 	ASSERT(tree_mark_size != 0);
   1846 	tree_mark = calloc(tree_mark_size, 1);
   1847 	ASSERT(tree_mark != NULL);
   1848 }
   1849 
   1850 static void fix_hard_links(struct f2fs_sb_info *sbi)
   1851 {
   1852 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
   1853 	struct hard_link_node *tmp, *node;
   1854 	struct f2fs_node *node_blk = NULL;
   1855 	struct node_info ni;
   1856 	int ret;
   1857 
   1858 	if (fsck->hard_link_list_head == NULL)
   1859 		return;
   1860 
   1861 	node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
   1862 	ASSERT(node_blk != NULL);
   1863 
   1864 	node = fsck->hard_link_list_head;
   1865 	while (node) {
   1866 		/* Sanity check */
   1867 		if (sanity_check_nid(sbi, node->nid, node_blk,
   1868 					F2FS_FT_MAX, TYPE_INODE, &ni))
   1869 			FIX_MSG("Failed to fix, rerun fsck.f2fs");
   1870 
   1871 		node_blk->i.i_links = cpu_to_le32(node->actual_links);
   1872 
   1873 		FIX_MSG("File: 0x%x i_links= 0x%x -> 0x%x",
   1874 				node->nid, node->links, node->actual_links);
   1875 
   1876 		ret = dev_write_block(node_blk, ni.blk_addr);
   1877 		ASSERT(ret >= 0);
   1878 		tmp = node;
   1879 		node = node->next;
   1880 		free(tmp);
   1881 	}
   1882 	free(node_blk);
   1883 }
   1884 
   1885 static void fix_nat_entries(struct f2fs_sb_info *sbi)
   1886 {
   1887 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
   1888 	u32 i;
   1889 
   1890 	for (i = 0; i < fsck->nr_nat_entries; i++)
   1891 		if (f2fs_test_bit(i, fsck->nat_area_bitmap) != 0)
   1892 			nullify_nat_entry(sbi, i);
   1893 }
   1894 
   1895 static void flush_curseg_sit_entries(struct f2fs_sb_info *sbi)
   1896 {
   1897 	struct sit_info *sit_i = SIT_I(sbi);
   1898 	int i;
   1899 
   1900 	/* update curseg sit entries, since we may change
   1901 	 * a segment type in move_curseg_info
   1902 	 */
   1903 	for (i = 0; i < NO_CHECK_TYPE; i++) {
   1904 		struct curseg_info *curseg = CURSEG_I(sbi, i);
   1905 		struct f2fs_sit_block *sit_blk;
   1906 		struct f2fs_sit_entry *sit;
   1907 		struct seg_entry *se;
   1908 
   1909 		se = get_seg_entry(sbi, curseg->segno);
   1910 		sit_blk = get_current_sit_page(sbi, curseg->segno);
   1911 		sit = &sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, curseg->segno)];
   1912 		sit->vblocks = cpu_to_le16((se->type << SIT_VBLOCKS_SHIFT) |
   1913 							se->valid_blocks);
   1914 		rewrite_current_sit_page(sbi, curseg->segno, sit_blk);
   1915 		free(sit_blk);
   1916 	}
   1917 }
   1918 
   1919 static void fix_checkpoint(struct f2fs_sb_info *sbi)
   1920 {
   1921 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
   1922 	struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
   1923 	struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
   1924 	unsigned long long cp_blk_no;
   1925 	u32 flags = CP_UMOUNT_FLAG;
   1926 	block_t orphan_blks = 0;
   1927 	u32 i;
   1928 	int ret;
   1929 	u_int32_t crc = 0;
   1930 
   1931 	if (is_set_ckpt_flags(cp, CP_ORPHAN_PRESENT_FLAG)) {
   1932 		orphan_blks = __start_sum_addr(sbi) - 1;
   1933 		flags |= CP_ORPHAN_PRESENT_FLAG;
   1934 	}
   1935 
   1936 	set_cp(cp_pack_total_block_count, 8 + orphan_blks + get_sb(cp_payload));
   1937 
   1938 	flags = update_nat_bits_flags(sb, cp, flags);
   1939 	flags |= CP_NOCRC_RECOVERY_FLAG;
   1940 	set_cp(ckpt_flags, flags);
   1941 
   1942 	set_cp(free_segment_count, get_free_segments(sbi));
   1943 	set_cp(valid_block_count, fsck->chk.valid_blk_cnt);
   1944 	set_cp(valid_node_count, fsck->chk.valid_node_cnt);
   1945 	set_cp(valid_inode_count, fsck->chk.valid_inode_cnt);
   1946 
   1947 	crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, cp, CHECKSUM_OFFSET);
   1948 	*((__le32 *)((unsigned char *)cp + CHECKSUM_OFFSET)) = cpu_to_le32(crc);
   1949 
   1950 	cp_blk_no = get_sb(cp_blkaddr);
   1951 	if (sbi->cur_cp == 2)
   1952 		cp_blk_no += 1 << get_sb(log_blocks_per_seg);
   1953 
   1954 	ret = dev_write_block(cp, cp_blk_no++);
   1955 	ASSERT(ret >= 0);
   1956 
   1957 	for (i = 0; i < get_sb(cp_payload); i++) {
   1958 		ret = dev_write_block(((unsigned char *)cp) + i * F2FS_BLKSIZE,
   1959 								cp_blk_no++);
   1960 		ASSERT(ret >= 0);
   1961 	}
   1962 
   1963 	cp_blk_no += orphan_blks;
   1964 
   1965 	for (i = 0; i < NO_CHECK_TYPE; i++) {
   1966 		struct curseg_info *curseg = CURSEG_I(sbi, i);
   1967 
   1968 		ret = dev_write_block(curseg->sum_blk, cp_blk_no++);
   1969 		ASSERT(ret >= 0);
   1970 	}
   1971 
   1972 	ret = dev_write_block(cp, cp_blk_no++);
   1973 	ASSERT(ret >= 0);
   1974 
   1975 	/* Write nat bits */
   1976 	if (flags & CP_NAT_BITS_FLAG)
   1977 		write_nat_bits(sbi, sb, cp, sbi->cur_cp);
   1978 }
   1979 
   1980 int check_curseg_offset(struct f2fs_sb_info *sbi)
   1981 {
   1982 	int i;
   1983 
   1984 	for (i = 0; i < NO_CHECK_TYPE; i++) {
   1985 		struct curseg_info *curseg = CURSEG_I(sbi, i);
   1986 		struct seg_entry *se;
   1987 		int j, nblocks;
   1988 
   1989 		if ((curseg->next_blkoff >> 3) >= SIT_VBLOCK_MAP_SIZE)
   1990 			return -EINVAL;
   1991 		se = get_seg_entry(sbi, curseg->segno);
   1992 		if (f2fs_test_bit(curseg->next_blkoff,
   1993 					(const char *)se->cur_valid_map)) {
   1994 			ASSERT_MSG("Next block offset is not free, type:%d", i);
   1995 			return -EINVAL;
   1996 		}
   1997 		if (curseg->alloc_type == SSR)
   1998 			return 0;
   1999 
   2000 		nblocks = sbi->blocks_per_seg;
   2001 		for (j = curseg->next_blkoff + 1; j < nblocks; j++) {
   2002 			if (f2fs_test_bit(j, (const char *)se->cur_valid_map)) {
   2003 				ASSERT_MSG("LFS must have free section:%d", i);
   2004 				return -EINVAL;
   2005 			}
   2006 		}
   2007 	}
   2008 	return 0;
   2009 }
   2010 
   2011 int check_sit_types(struct f2fs_sb_info *sbi)
   2012 {
   2013 	unsigned int i;
   2014 	int err = 0;
   2015 
   2016 	for (i = 0; i < TOTAL_SEGS(sbi); i++) {
   2017 		struct seg_entry *se;
   2018 
   2019 		se = get_seg_entry(sbi, i);
   2020 		if (se->orig_type != se->type) {
   2021 			if (se->orig_type == CURSEG_COLD_DATA &&
   2022 					se->type <= CURSEG_COLD_DATA) {
   2023 				se->type = se->orig_type;
   2024 			} else {
   2025 				FIX_MSG("Wrong segment type [0x%x] %x -> %x",
   2026 						i, se->orig_type, se->type);
   2027 				err = -EINVAL;
   2028 			}
   2029 		}
   2030 	}
   2031 	return err;
   2032 }
   2033 
   2034 int fsck_verify(struct f2fs_sb_info *sbi)
   2035 {
   2036 	unsigned int i = 0;
   2037 	int ret = 0;
   2038 	int force = 0;
   2039 	u32 nr_unref_nid = 0;
   2040 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
   2041 	struct hard_link_node *node = NULL;
   2042 
   2043 	printf("\n");
   2044 
   2045 	for (i = 0; i < fsck->nr_nat_entries; i++) {
   2046 		if (f2fs_test_bit(i, fsck->nat_area_bitmap) != 0) {
   2047 			printf("NID[0x%x] is unreachable\n", i);
   2048 			nr_unref_nid++;
   2049 		}
   2050 	}
   2051 
   2052 	if (fsck->hard_link_list_head != NULL) {
   2053 		node = fsck->hard_link_list_head;
   2054 		while (node) {
   2055 			printf("NID[0x%x] has [0x%x] more unreachable links\n",
   2056 					node->nid, node->links);
   2057 			node = node->next;
   2058 		}
   2059 		c.bug_on = 1;
   2060 	}
   2061 
   2062 	printf("[FSCK] Unreachable nat entries                       ");
   2063 	if (nr_unref_nid == 0x0) {
   2064 		printf(" [Ok..] [0x%x]\n", nr_unref_nid);
   2065 	} else {
   2066 		printf(" [Fail] [0x%x]\n", nr_unref_nid);
   2067 		ret = EXIT_ERR_CODE;
   2068 		c.bug_on = 1;
   2069 	}
   2070 
   2071 	printf("[FSCK] SIT valid block bitmap checking                ");
   2072 	if (memcmp(fsck->sit_area_bitmap, fsck->main_area_bitmap,
   2073 					fsck->sit_area_bitmap_sz) == 0x0) {
   2074 		printf("[Ok..]\n");
   2075 	} else {
   2076 		printf("[Fail]\n");
   2077 		ret = EXIT_ERR_CODE;
   2078 		c.bug_on = 1;
   2079 	}
   2080 
   2081 	printf("[FSCK] Hard link checking for regular file           ");
   2082 	if (fsck->hard_link_list_head == NULL) {
   2083 		printf(" [Ok..] [0x%x]\n", fsck->chk.multi_hard_link_files);
   2084 	} else {
   2085 		printf(" [Fail] [0x%x]\n", fsck->chk.multi_hard_link_files);
   2086 		ret = EXIT_ERR_CODE;
   2087 		c.bug_on = 1;
   2088 	}
   2089 
   2090 	printf("[FSCK] valid_block_count matching with CP            ");
   2091 	if (sbi->total_valid_block_count == fsck->chk.valid_blk_cnt) {
   2092 		printf(" [Ok..] [0x%x]\n", (u32)fsck->chk.valid_blk_cnt);
   2093 	} else {
   2094 		printf(" [Fail] [0x%x]\n", (u32)fsck->chk.valid_blk_cnt);
   2095 		ret = EXIT_ERR_CODE;
   2096 		c.bug_on = 1;
   2097 	}
   2098 
   2099 	printf("[FSCK] valid_node_count matcing with CP (de lookup)  ");
   2100 	if (sbi->total_valid_node_count == fsck->chk.valid_node_cnt) {
   2101 		printf(" [Ok..] [0x%x]\n", fsck->chk.valid_node_cnt);
   2102 	} else {
   2103 		printf(" [Fail] [0x%x]\n", fsck->chk.valid_node_cnt);
   2104 		ret = EXIT_ERR_CODE;
   2105 		c.bug_on = 1;
   2106 	}
   2107 
   2108 	printf("[FSCK] valid_node_count matcing with CP (nat lookup) ");
   2109 	if (sbi->total_valid_node_count == fsck->chk.valid_nat_entry_cnt) {
   2110 		printf(" [Ok..] [0x%x]\n", fsck->chk.valid_nat_entry_cnt);
   2111 	} else {
   2112 		printf(" [Fail] [0x%x]\n", fsck->chk.valid_nat_entry_cnt);
   2113 		ret = EXIT_ERR_CODE;
   2114 		c.bug_on = 1;
   2115 	}
   2116 
   2117 	printf("[FSCK] valid_inode_count matched with CP             ");
   2118 	if (sbi->total_valid_inode_count == fsck->chk.valid_inode_cnt) {
   2119 		printf(" [Ok..] [0x%x]\n", fsck->chk.valid_inode_cnt);
   2120 	} else {
   2121 		printf(" [Fail] [0x%x]\n", fsck->chk.valid_inode_cnt);
   2122 		ret = EXIT_ERR_CODE;
   2123 		c.bug_on = 1;
   2124 	}
   2125 
   2126 	printf("[FSCK] free segment_count matched with CP            ");
   2127 	if (le32_to_cpu(F2FS_CKPT(sbi)->free_segment_count) ==
   2128 						fsck->chk.sit_free_segs) {
   2129 		printf(" [Ok..] [0x%x]\n", fsck->chk.sit_free_segs);
   2130 	} else {
   2131 		printf(" [Fail] [0x%x]\n", fsck->chk.sit_free_segs);
   2132 		ret = EXIT_ERR_CODE;
   2133 		c.bug_on = 1;
   2134 	}
   2135 
   2136 	printf("[FSCK] next block offset is free                     ");
   2137 	if (check_curseg_offset(sbi) == 0) {
   2138 		printf(" [Ok..]\n");
   2139 	} else {
   2140 		printf(" [Fail]\n");
   2141 		ret = EXIT_ERR_CODE;
   2142 		c.bug_on = 1;
   2143 	}
   2144 
   2145 	printf("[FSCK] fixing SIT types\n");
   2146 	if (check_sit_types(sbi) != 0)
   2147 		force = 1;
   2148 
   2149 	printf("[FSCK] other corrupted bugs                          ");
   2150 	if (c.bug_on == 0) {
   2151 		printf(" [Ok..]\n");
   2152 	} else {
   2153 		printf(" [Fail]\n");
   2154 		ret = EXIT_ERR_CODE;
   2155 	}
   2156 
   2157 #ifndef WITH_ANDROID
   2158 	if (nr_unref_nid && !c.ro) {
   2159 		char ans[255] = {0};
   2160 
   2161 		printf("\nDo you want to restore lost files into ./lost_found/? [Y/N] ");
   2162 		ret = scanf("%s", ans);
   2163 		ASSERT(ret >= 0);
   2164 		if (!strcasecmp(ans, "y")) {
   2165 			for (i = 0; i < fsck->nr_nat_entries; i++) {
   2166 				if (f2fs_test_bit(i, fsck->nat_area_bitmap))
   2167 					dump_node(sbi, i, 1);
   2168 			}
   2169 		}
   2170 	}
   2171 #endif
   2172 	/* fix global metadata */
   2173 	if (force || (c.fix_on && !c.ro)) {
   2174 		struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
   2175 
   2176 		if (force || c.bug_on) {
   2177 			fix_hard_links(sbi);
   2178 			fix_nat_entries(sbi);
   2179 			rewrite_sit_area_bitmap(sbi);
   2180 			if (check_curseg_offset(sbi)) {
   2181 				move_curseg_info(sbi, SM_I(sbi)->main_blkaddr);
   2182 				write_curseg_info(sbi);
   2183 				flush_curseg_sit_entries(sbi);
   2184 			}
   2185 			fix_checkpoint(sbi);
   2186 		} else if (is_set_ckpt_flags(cp, CP_FSCK_FLAG)) {
   2187 			write_checkpoint(sbi);
   2188 		}
   2189 	}
   2190 	return ret;
   2191 }
   2192 
   2193 void fsck_free(struct f2fs_sb_info *sbi)
   2194 {
   2195 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
   2196 
   2197 	if (fsck->qctx)
   2198 		quota_release_context(&fsck->qctx);
   2199 
   2200 	if (fsck->main_area_bitmap)
   2201 		free(fsck->main_area_bitmap);
   2202 
   2203 	if (fsck->nat_area_bitmap)
   2204 		free(fsck->nat_area_bitmap);
   2205 
   2206 	if (fsck->sit_area_bitmap)
   2207 		free(fsck->sit_area_bitmap);
   2208 
   2209 	if (fsck->entries)
   2210 		free(fsck->entries);
   2211 
   2212 	if (tree_mark)
   2213 		free(tree_mark);
   2214 }
   2215