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 "xattr.h"
13 #include "quotaio.h"
14 #include <time.h>
15 
16 char *tree_mark;
17 uint32_t tree_mark_size = 256;
18 
19 int f2fs_set_main_bitmap(struct f2fs_sb_info *sbi, u32 blk, int type)
20 {
21 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
22 	struct seg_entry *se;
23 	int fix = 0;
24 
25 	se = get_seg_entry(sbi, GET_SEGNO(sbi, blk));
26 	if (se->type >= NO_CHECK_TYPE)
27 		fix = 1;
28 	else if (IS_DATASEG(se->type) != IS_DATASEG(type))
29 		fix = 1;
30 
31 	/* just check data and node types */
32 	if (fix) {
33 		DBG(1, "Wrong segment type [0x%x] %x -> %x",
34 				GET_SEGNO(sbi, blk), se->type, type);
35 		se->type = type;
36 	}
37 	return f2fs_set_bit(BLKOFF_FROM_MAIN(sbi, blk), fsck->main_area_bitmap);
38 }
39 
40 static inline int f2fs_test_main_bitmap(struct f2fs_sb_info *sbi, u32 blk)
41 {
42 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
43 
44 	return f2fs_test_bit(BLKOFF_FROM_MAIN(sbi, blk),
45 						fsck->main_area_bitmap);
46 }
47 
48 static inline int f2fs_clear_main_bitmap(struct f2fs_sb_info *sbi, u32 blk)
49 {
50 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
51 
52 	return f2fs_clear_bit(BLKOFF_FROM_MAIN(sbi, blk),
53 						fsck->main_area_bitmap);
54 }
55 
56 static inline int f2fs_test_sit_bitmap(struct f2fs_sb_info *sbi, u32 blk)
57 {
58 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
59 
60 	return f2fs_test_bit(BLKOFF_FROM_MAIN(sbi, blk), fsck->sit_area_bitmap);
61 }
62 
63 int f2fs_set_sit_bitmap(struct f2fs_sb_info *sbi, u32 blk)
64 {
65 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
66 
67 	return f2fs_set_bit(BLKOFF_FROM_MAIN(sbi, blk), fsck->sit_area_bitmap);
68 }
69 
70 static int add_into_hard_link_list(struct f2fs_sb_info *sbi,
71 						u32 nid, u32 link_cnt)
72 {
73 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
74 	struct hard_link_node *node = NULL, *tmp = NULL, *prev = NULL;
75 
76 	node = calloc(sizeof(struct hard_link_node), 1);
77 	ASSERT(node != NULL);
78 
79 	node->nid = nid;
80 	node->links = link_cnt;
81 	node->actual_links = 1;
82 	node->next = NULL;
83 
84 	if (fsck->hard_link_list_head == NULL) {
85 		fsck->hard_link_list_head = node;
86 		goto out;
87 	}
88 
89 	tmp = fsck->hard_link_list_head;
90 
91 	/* Find insertion position */
92 	while (tmp && (nid < tmp->nid)) {
93 		ASSERT(tmp->nid != nid);
94 		prev = tmp;
95 		tmp = tmp->next;
96 	}
97 
98 	if (tmp == fsck->hard_link_list_head) {
99 		node->next = tmp;
100 		fsck->hard_link_list_head = node;
101 	} else {
102 		prev->next = node;
103 		node->next = tmp;
104 	}
105 
106 out:
107 	DBG(2, "ino[0x%x] has hard links [0x%x]\n", nid, link_cnt);
108 	return 0;
109 }
110 
111 static int find_and_dec_hard_link_list(struct f2fs_sb_info *sbi, u32 nid)
112 {
113 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
114 	struct hard_link_node *node = NULL, *prev = NULL;
115 
116 	if (fsck->hard_link_list_head == NULL)
117 		return -EINVAL;
118 
119 	node = fsck->hard_link_list_head;
120 
121 	while (node && (nid < node->nid)) {
122 		prev = node;
123 		node = node->next;
124 	}
125 
126 	if (node == NULL || (nid != node->nid))
127 		return -EINVAL;
128 
129 	/* Decrease link count */
130 	node->links = node->links - 1;
131 	node->actual_links++;
132 
133 	/* if link count becomes one, remove the node */
134 	if (node->links == 1) {
135 		if (fsck->hard_link_list_head == node)
136 			fsck->hard_link_list_head = node->next;
137 		else
138 			prev->next = node->next;
139 		free(node);
140 	}
141 	return 0;
142 }
143 
144 static int is_valid_ssa_node_blk(struct f2fs_sb_info *sbi, u32 nid,
145 							u32 blk_addr)
146 {
147 	struct f2fs_summary_block *sum_blk;
148 	struct f2fs_summary *sum_entry;
149 	struct seg_entry * se;
150 	u32 segno, offset;
151 	int need_fix = 0, ret = 0;
152 	int type;
153 
154 	segno = GET_SEGNO(sbi, blk_addr);
155 	offset = OFFSET_IN_SEG(sbi, blk_addr);
156 
157 	sum_blk = get_sum_block(sbi, segno, &type);
158 
159 	if (type != SEG_TYPE_NODE && type != SEG_TYPE_CUR_NODE) {
160 		/* can't fix current summary, then drop the block */
161 		if (!c.fix_on || type < 0) {
162 			ASSERT_MSG("Summary footer is not for node segment");
163 			ret = -EINVAL;
164 			goto out;
165 		}
166 
167 		need_fix = 1;
168 		se = get_seg_entry(sbi, segno);
169 		if(IS_NODESEG(se->type)) {
170 			FIX_MSG("Summary footer indicates a node segment: 0x%x", segno);
171 			sum_blk->footer.entry_type = SUM_TYPE_NODE;
172 		} else {
173 			ret = -EINVAL;
174 			goto out;
175 		}
176 	}
177 
178 	sum_entry = &(sum_blk->entries[offset]);
179 
180 	if (le32_to_cpu(sum_entry->nid) != nid) {
181 		if (!c.fix_on || type < 0) {
182 			DBG(0, "nid                       [0x%x]\n", nid);
183 			DBG(0, "target blk_addr           [0x%x]\n", blk_addr);
184 			DBG(0, "summary blk_addr          [0x%x]\n",
185 						GET_SUM_BLKADDR(sbi,
186 						GET_SEGNO(sbi, blk_addr)));
187 			DBG(0, "seg no / offset           [0x%x / 0x%x]\n",
188 						GET_SEGNO(sbi, blk_addr),
189 						OFFSET_IN_SEG(sbi, blk_addr));
190 			DBG(0, "summary_entry.nid         [0x%x]\n",
191 						le32_to_cpu(sum_entry->nid));
192 			DBG(0, "--> node block's nid      [0x%x]\n", nid);
193 			ASSERT_MSG("Invalid node seg summary\n");
194 			ret = -EINVAL;
195 		} else {
196 			FIX_MSG("Set node summary 0x%x -> [0x%x] [0x%x]",
197 						segno, nid, blk_addr);
198 			sum_entry->nid = cpu_to_le32(nid);
199 			need_fix = 1;
200 		}
201 	}
202 	if (need_fix && f2fs_dev_is_writable()) {
203 		u64 ssa_blk;
204 		int ret2;
205 
206 		ssa_blk = GET_SUM_BLKADDR(sbi, segno);
207 		ret2 = dev_write_block(sum_blk, ssa_blk);
208 		ASSERT(ret2 >= 0);
209 	}
210 out:
211 	if (type == SEG_TYPE_NODE || type == SEG_TYPE_DATA ||
212 					type == SEG_TYPE_MAX)
213 		free(sum_blk);
214 	return ret;
215 }
216 
217 static int is_valid_summary(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
218 							u32 blk_addr)
219 {
220 	u16 ofs_in_node = le16_to_cpu(sum->ofs_in_node);
221 	u32 nid = le32_to_cpu(sum->nid);
222 	struct f2fs_node *node_blk = NULL;
223 	__le32 target_blk_addr;
224 	struct node_info ni;
225 	int ret = 0;
226 
227 	node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
228 	ASSERT(node_blk != NULL);
229 
230 	if (!IS_VALID_NID(sbi, nid))
231 		goto out;
232 
233 	get_node_info(sbi, nid, &ni);
234 
235 	if (!IS_VALID_BLK_ADDR(sbi, ni.blk_addr))
236 		goto out;
237 
238 	/* read node_block */
239 	ret = dev_read_block(node_blk, ni.blk_addr);
240 	ASSERT(ret >= 0);
241 
242 	if (le32_to_cpu(node_blk->footer.nid) != nid)
243 		goto out;
244 
245 	/* check its block address */
246 	if (node_blk->footer.nid == node_blk->footer.ino) {
247 		int ofs = get_extra_isize(node_blk);
248 
249 		target_blk_addr = node_blk->i.i_addr[ofs + ofs_in_node];
250 	} else {
251 		target_blk_addr = node_blk->dn.addr[ofs_in_node];
252 	}
253 
254 	if (blk_addr == le32_to_cpu(target_blk_addr))
255 		ret = 1;
256 out:
257 	free(node_blk);
258 	return ret;
259 }
260 
261 static int is_valid_ssa_data_blk(struct f2fs_sb_info *sbi, u32 blk_addr,
262 		u32 parent_nid, u16 idx_in_node, u8 version)
263 {
264 	struct f2fs_summary_block *sum_blk;
265 	struct f2fs_summary *sum_entry;
266 	struct seg_entry * se;
267 	u32 segno, offset;
268 	int need_fix = 0, ret = 0;
269 	int type;
270 
271 	segno = GET_SEGNO(sbi, blk_addr);
272 	offset = OFFSET_IN_SEG(sbi, blk_addr);
273 
274 	sum_blk = get_sum_block(sbi, segno, &type);
275 
276 	if (type != SEG_TYPE_DATA && type != SEG_TYPE_CUR_DATA) {
277 		/* can't fix current summary, then drop the block */
278 		if (!c.fix_on || type < 0) {
279 			ASSERT_MSG("Summary footer is not for data segment");
280 			ret = -EINVAL;
281 			goto out;
282 		}
283 
284 		need_fix = 1;
285 		se = get_seg_entry(sbi, segno);
286 		if (IS_DATASEG(se->type)) {
287 			FIX_MSG("Summary footer indicates a data segment: 0x%x", segno);
288 			sum_blk->footer.entry_type = SUM_TYPE_DATA;
289 		} else {
290 			ret = -EINVAL;
291 			goto out;
292 		}
293 	}
294 
295 	sum_entry = &(sum_blk->entries[offset]);
296 
297 	if (le32_to_cpu(sum_entry->nid) != parent_nid ||
298 			sum_entry->version != version ||
299 			le16_to_cpu(sum_entry->ofs_in_node) != idx_in_node) {
300 		if (!c.fix_on || type < 0) {
301 			DBG(0, "summary_entry.nid         [0x%x]\n",
302 					le32_to_cpu(sum_entry->nid));
303 			DBG(0, "summary_entry.version     [0x%x]\n",
304 					sum_entry->version);
305 			DBG(0, "summary_entry.ofs_in_node [0x%x]\n",
306 					le16_to_cpu(sum_entry->ofs_in_node));
307 			DBG(0, "parent nid                [0x%x]\n",
308 					parent_nid);
309 			DBG(0, "version from nat          [0x%x]\n", version);
310 			DBG(0, "idx in parent node        [0x%x]\n",
311 					idx_in_node);
312 
313 			DBG(0, "Target data block addr    [0x%x]\n", blk_addr);
314 			ASSERT_MSG("Invalid data seg summary\n");
315 			ret = -EINVAL;
316 		} else if (is_valid_summary(sbi, sum_entry, blk_addr)) {
317 			/* delete wrong index */
318 			ret = -EINVAL;
319 		} else {
320 			FIX_MSG("Set data summary 0x%x -> [0x%x] [0x%x] [0x%x]",
321 					segno, parent_nid, version, idx_in_node);
322 			sum_entry->nid = cpu_to_le32(parent_nid);
323 			sum_entry->version = version;
324 			sum_entry->ofs_in_node = cpu_to_le16(idx_in_node);
325 			need_fix = 1;
326 		}
327 	}
328 	if (need_fix && f2fs_dev_is_writable()) {
329 		u64 ssa_blk;
330 		int ret2;
331 
332 		ssa_blk = GET_SUM_BLKADDR(sbi, segno);
333 		ret2 = dev_write_block(sum_blk, ssa_blk);
334 		ASSERT(ret2 >= 0);
335 	}
336 out:
337 	if (type == SEG_TYPE_NODE || type == SEG_TYPE_DATA ||
338 					type == SEG_TYPE_MAX)
339 		free(sum_blk);
340 	return ret;
341 }
342 
343 static int __check_inode_mode(u32 nid, enum FILE_TYPE ftype, u16 mode)
344 {
345 	if (ftype >= F2FS_FT_MAX)
346 		return 0;
347 	/* f2fs_iget will return -EIO if mode is not valid file type */
348 	if (!S_ISLNK(mode) && !S_ISREG(mode) && !S_ISDIR(mode) &&
349 	    !S_ISCHR(mode) && !S_ISBLK(mode) && !S_ISFIFO(mode) &&
350 	    !S_ISSOCK(mode)) {
351 		ASSERT_MSG("inode [0x%x] unknown file type i_mode [0x%x]",
352 			   nid, mode);
353 		return -1;
354 	}
355 
356 	if (S_ISLNK(mode) && ftype != F2FS_FT_SYMLINK)
357 		goto err;
358 	if (S_ISREG(mode) && ftype != F2FS_FT_REG_FILE)
359 		goto err;
360 	if (S_ISDIR(mode) && ftype != F2FS_FT_DIR)
361 		goto err;
362 	if (S_ISCHR(mode) && ftype != F2FS_FT_CHRDEV)
363 		goto err;
364 	if (S_ISBLK(mode) && ftype != F2FS_FT_BLKDEV)
365 		goto err;
366 	if (S_ISFIFO(mode) && ftype != F2FS_FT_FIFO)
367 		goto err;
368 	if (S_ISSOCK(mode) && ftype != F2FS_FT_SOCK)
369 		goto err;
370 	return 0;
371 err:
372 	ASSERT_MSG("inode [0x%x] mismatch i_mode [0x%x vs. 0x%x]",
373 		   nid, ftype, mode);
374 	return -1;
375 }
376 
377 static int sanity_check_nid(struct f2fs_sb_info *sbi, u32 nid,
378 			struct f2fs_node *node_blk,
379 			enum FILE_TYPE ftype, enum NODE_TYPE ntype,
380 			struct node_info *ni)
381 {
382 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
383 	int ret;
384 
385 	if (!IS_VALID_NID(sbi, nid)) {
386 		ASSERT_MSG("nid is not valid. [0x%x]", nid);
387 		return -EINVAL;
388 	}
389 
390 	get_node_info(sbi, nid, ni);
391 	if (ni->ino == 0) {
392 		ASSERT_MSG("nid[0x%x] ino is 0", nid);
393 		return -EINVAL;
394 	}
395 
396 	if (ni->blk_addr == NEW_ADDR) {
397 		ASSERT_MSG("nid is NEW_ADDR. [0x%x]", nid);
398 		return -EINVAL;
399 	}
400 
401 	if (!IS_VALID_BLK_ADDR(sbi, ni->blk_addr)) {
402 		ASSERT_MSG("blkaddress is not valid. [0x%x]", ni->blk_addr);
403 		return -EINVAL;
404 	}
405 
406 	ret = dev_read_block(node_blk, ni->blk_addr);
407 	ASSERT(ret >= 0);
408 
409 	if (ntype == TYPE_INODE &&
410 			node_blk->footer.nid != node_blk->footer.ino) {
411 		ASSERT_MSG("nid[0x%x] footer.nid[0x%x] footer.ino[0x%x]",
412 				nid, le32_to_cpu(node_blk->footer.nid),
413 				le32_to_cpu(node_blk->footer.ino));
414 		return -EINVAL;
415 	}
416 	if (ni->ino != le32_to_cpu(node_blk->footer.ino)) {
417 		ASSERT_MSG("nid[0x%x] nat_entry->ino[0x%x] footer.ino[0x%x]",
418 				nid, ni->ino, le32_to_cpu(node_blk->footer.ino));
419 		return -EINVAL;
420 	}
421 	if (ntype != TYPE_INODE &&
422 			node_blk->footer.nid == node_blk->footer.ino) {
423 		ASSERT_MSG("nid[0x%x] footer.nid[0x%x] footer.ino[0x%x]",
424 				nid, le32_to_cpu(node_blk->footer.nid),
425 				le32_to_cpu(node_blk->footer.ino));
426 		return -EINVAL;
427 	}
428 
429 	if (le32_to_cpu(node_blk->footer.nid) != nid) {
430 		ASSERT_MSG("nid[0x%x] blk_addr[0x%x] footer.nid[0x%x]",
431 				nid, ni->blk_addr,
432 				le32_to_cpu(node_blk->footer.nid));
433 		return -EINVAL;
434 	}
435 
436 	if (ntype == TYPE_XATTR) {
437 		u32 flag = le32_to_cpu(node_blk->footer.flag);
438 
439 		if ((flag >> OFFSET_BIT_SHIFT) != XATTR_NODE_OFFSET) {
440 			ASSERT_MSG("xnid[0x%x] has wrong ofs:[0x%x]",
441 					nid, flag);
442 			return -EINVAL;
443 		}
444 	}
445 
446 	if ((ntype == TYPE_INODE && ftype == F2FS_FT_DIR) ||
447 			(ntype == TYPE_XATTR && ftype == F2FS_FT_XATTR)) {
448 		/* not included '.' & '..' */
449 		if (f2fs_test_main_bitmap(sbi, ni->blk_addr) != 0) {
450 			ASSERT_MSG("Duplicated node blk. nid[0x%x][0x%x]\n",
451 					nid, ni->blk_addr);
452 			return -EINVAL;
453 		}
454 	}
455 
456 	/* this if only from fix_hard_links */
457 	if (ftype == F2FS_FT_MAX)
458 		return 0;
459 
460 	if (ntype == TYPE_INODE &&
461 		__check_inode_mode(nid, ftype, le16_to_cpu(node_blk->i.i_mode)))
462 		return -EINVAL;
463 
464 	/* workaround to fix later */
465 	if (ftype != F2FS_FT_ORPHAN ||
466 			f2fs_test_bit(nid, fsck->nat_area_bitmap) != 0) {
467 		f2fs_clear_bit(nid, fsck->nat_area_bitmap);
468 		/* avoid reusing nid when reconnecting files */
469 		f2fs_set_bit(nid, NM_I(sbi)->nid_bitmap);
470 	} else
471 		ASSERT_MSG("orphan or xattr nid is duplicated [0x%x]\n",
472 				nid);
473 
474 	if (is_valid_ssa_node_blk(sbi, nid, ni->blk_addr)) {
475 		ASSERT_MSG("summary node block is not valid. [0x%x]", nid);
476 		return -EINVAL;
477 	}
478 
479 	if (f2fs_test_sit_bitmap(sbi, ni->blk_addr) == 0)
480 		ASSERT_MSG("SIT bitmap is 0x0. blk_addr[0x%x]",
481 				ni->blk_addr);
482 
483 	if (f2fs_test_main_bitmap(sbi, ni->blk_addr) == 0) {
484 		fsck->chk.valid_blk_cnt++;
485 		fsck->chk.valid_node_cnt++;
486 	}
487 	return 0;
488 }
489 
490 int fsck_sanity_check_nid(struct f2fs_sb_info *sbi, u32 nid,
491 			struct f2fs_node *node_blk,
492 			enum FILE_TYPE ftype, enum NODE_TYPE ntype,
493 			struct node_info *ni)
494 {
495 	return sanity_check_nid(sbi, nid, node_blk, ftype, ntype, ni);
496 }
497 
498 static int fsck_chk_xattr_blk(struct f2fs_sb_info *sbi, u32 ino,
499 					u32 x_nid, u32 *blk_cnt)
500 {
501 	struct f2fs_node *node_blk = NULL;
502 	struct node_info ni;
503 	int ret = 0;
504 
505 	if (x_nid == 0x0)
506 		return 0;
507 
508 	node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
509 	ASSERT(node_blk != NULL);
510 
511 	/* Sanity check */
512 	if (sanity_check_nid(sbi, x_nid, node_blk,
513 				F2FS_FT_XATTR, TYPE_XATTR, &ni)) {
514 		ret = -EINVAL;
515 		goto out;
516 	}
517 
518 	*blk_cnt = *blk_cnt + 1;
519 	f2fs_set_main_bitmap(sbi, ni.blk_addr, CURSEG_COLD_NODE);
520 	DBG(2, "ino[0x%x] x_nid[0x%x]\n", ino, x_nid);
521 out:
522 	free(node_blk);
523 	return ret;
524 }
525 
526 int fsck_chk_node_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
527 		u32 nid, enum FILE_TYPE ftype, enum NODE_TYPE ntype,
528 		u32 *blk_cnt, struct child_info *child)
529 {
530 	struct node_info ni;
531 	struct f2fs_node *node_blk = NULL;
532 
533 	node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
534 	ASSERT(node_blk != NULL);
535 
536 	if (sanity_check_nid(sbi, nid, node_blk, ftype, ntype, &ni))
537 		goto err;
538 
539 	if (ntype == TYPE_INODE) {
540 		struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
541 
542 		fsck_chk_inode_blk(sbi, nid, ftype, node_blk, blk_cnt, &ni, child);
543 		quota_add_inode_usage(fsck->qctx, nid, &node_blk->i);
544 	} else {
545 		switch (ntype) {
546 		case TYPE_DIRECT_NODE:
547 			f2fs_set_main_bitmap(sbi, ni.blk_addr,
548 							CURSEG_WARM_NODE);
549 			fsck_chk_dnode_blk(sbi, inode, nid, ftype, node_blk,
550 					blk_cnt, child, &ni);
551 			break;
552 		case TYPE_INDIRECT_NODE:
553 			f2fs_set_main_bitmap(sbi, ni.blk_addr,
554 							CURSEG_COLD_NODE);
555 			fsck_chk_idnode_blk(sbi, inode, ftype, node_blk,
556 					blk_cnt, child);
557 			break;
558 		case TYPE_DOUBLE_INDIRECT_NODE:
559 			f2fs_set_main_bitmap(sbi, ni.blk_addr,
560 							CURSEG_COLD_NODE);
561 			fsck_chk_didnode_blk(sbi, inode, ftype, node_blk,
562 					blk_cnt, child);
563 			break;
564 		default:
565 			ASSERT(0);
566 		}
567 	}
568 	free(node_blk);
569 	return 0;
570 err:
571 	free(node_blk);
572 	return -EINVAL;
573 }
574 
575 static inline void get_extent_info(struct extent_info *ext,
576 					struct f2fs_extent *i_ext)
577 {
578 	ext->fofs = le32_to_cpu(i_ext->fofs);
579 	ext->blk = le32_to_cpu(i_ext->blk_addr);
580 	ext->len = le32_to_cpu(i_ext->len);
581 }
582 
583 static void check_extent_info(struct child_info *child,
584 						block_t blkaddr, int last)
585 {
586 	struct extent_info *ei = &child->ei;
587 	u32 pgofs = child->pgofs;
588 	int is_hole = 0;
589 
590 	if (!ei->len)
591 		return;
592 
593 	if (child->state & FSCK_UNMATCHED_EXTENT)
594 		return;
595 
596 	if ((child->state & FSCK_INLINE_INODE) && ei->len)
597 		goto unmatched;
598 
599 	if (last) {
600 		/* hole exist in the back of extent */
601 		if (child->last_blk != ei->blk + ei->len - 1)
602 			child->state |= FSCK_UNMATCHED_EXTENT;
603 		return;
604 	}
605 
606 	if (blkaddr == NULL_ADDR || blkaddr == NEW_ADDR)
607 		is_hole = 1;
608 
609 	if (pgofs >= ei->fofs && pgofs < ei->fofs + ei->len) {
610 		/* unmatched blkaddr */
611 		if (is_hole || (blkaddr != pgofs - ei->fofs + ei->blk))
612 			goto unmatched;
613 
614 		if (!child->last_blk) {
615 			/* hole exists in the front of extent */
616 			if (pgofs != ei->fofs)
617 				goto unmatched;
618 		} else if (child->last_blk + 1 != blkaddr) {
619 			/* hole exists in the middle of extent */
620 			goto unmatched;
621 		}
622 		child->last_blk = blkaddr;
623 		return;
624 	}
625 
626 	if (is_hole)
627 		return;
628 
629 	if (blkaddr < ei->blk || blkaddr >= ei->blk + ei->len)
630 		return;
631 	/* unmatched file offset */
632 unmatched:
633 	child->state |= FSCK_UNMATCHED_EXTENT;
634 }
635 
636 void fsck_reada_node_block(struct f2fs_sb_info *sbi, u32 nid)
637 {
638 	struct node_info ni;
639 
640 	if (nid != 0 && IS_VALID_NID(sbi, nid)) {
641 		get_node_info(sbi, nid, &ni);
642 		if (IS_VALID_BLK_ADDR(sbi, ni.blk_addr))
643 			dev_reada_block(ni.blk_addr);
644 	}
645 }
646 
647 void fsck_reada_all_direct_node_blocks(struct f2fs_sb_info *sbi,
648 						struct f2fs_node *node_blk)
649 {
650 	int i;
651 
652 	for (i = 0; i < NIDS_PER_BLOCK; i++) {
653 		u32 nid = le32_to_cpu(node_blk->in.nid[i]);
654 
655 		fsck_reada_node_block(sbi, nid);
656 	}
657 }
658 
659 /* start with valid nid and blkaddr */
660 void fsck_chk_inode_blk(struct f2fs_sb_info *sbi, u32 nid,
661 		enum FILE_TYPE ftype, struct f2fs_node *node_blk,
662 		u32 *blk_cnt, struct node_info *ni, struct child_info *child_d)
663 {
664 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
665 	struct child_info child;
666 	enum NODE_TYPE ntype;
667 	u32 i_links = le32_to_cpu(node_blk->i.i_links);
668 	u64 i_size = le64_to_cpu(node_blk->i.i_size);
669 	u64 i_blocks = le64_to_cpu(node_blk->i.i_blocks);
670 	nid_t i_xattr_nid = le32_to_cpu(node_blk->i.i_xattr_nid);
671 	int ofs;
672 	char *en;
673 	u32 namelen;
674 	unsigned int idx = 0;
675 	unsigned short i_gc_failures;
676 	int need_fix = 0;
677 	int ret;
678 
679 	memset(&child, 0, sizeof(child));
680 	child.links = 2;
681 	child.p_ino = nid;
682 	child.pp_ino = le32_to_cpu(node_blk->i.i_pino);
683 	child.dir_level = node_blk->i.i_dir_level;
684 
685 	if (f2fs_test_main_bitmap(sbi, ni->blk_addr) == 0)
686 		fsck->chk.valid_inode_cnt++;
687 
688 	if (ftype == F2FS_FT_DIR) {
689 		f2fs_set_main_bitmap(sbi, ni->blk_addr, CURSEG_HOT_NODE);
690 	} else {
691 		if (f2fs_test_main_bitmap(sbi, ni->blk_addr) == 0) {
692 			f2fs_set_main_bitmap(sbi, ni->blk_addr,
693 							CURSEG_WARM_NODE);
694 			if (i_links > 1 && ftype != F2FS_FT_ORPHAN &&
695 					!is_qf_ino(F2FS_RAW_SUPER(sbi), nid)) {
696 				/* First time. Create new hard link node */
697 				add_into_hard_link_list(sbi, nid, i_links);
698 				fsck->chk.multi_hard_link_files++;
699 			}
700 		} else {
701 			DBG(3, "[0x%x] has hard links [0x%x]\n", nid, i_links);
702 			if (find_and_dec_hard_link_list(sbi, nid)) {
703 				ASSERT_MSG("[0x%x] needs more i_links=0x%x",
704 						nid, i_links);
705 				if (c.fix_on) {
706 					node_blk->i.i_links =
707 						cpu_to_le32(i_links + 1);
708 					need_fix = 1;
709 					FIX_MSG("File: 0x%x "
710 						"i_links= 0x%x -> 0x%x",
711 						nid, i_links, i_links + 1);
712 				}
713 				goto skip_blkcnt_fix;
714 			}
715 			/* No need to go deep into the node */
716 			return;
717 		}
718 	}
719 
720 	/* readahead xattr node block */
721 	fsck_reada_node_block(sbi, i_xattr_nid);
722 
723 	if (fsck_chk_xattr_blk(sbi, nid, i_xattr_nid, blk_cnt)) {
724 		if (c.fix_on) {
725 			node_blk->i.i_xattr_nid = 0;
726 			need_fix = 1;
727 			FIX_MSG("Remove xattr block: 0x%x, x_nid = 0x%x",
728 							nid, i_xattr_nid);
729 		}
730 	}
731 
732 	if (ftype == F2FS_FT_CHRDEV || ftype == F2FS_FT_BLKDEV ||
733 			ftype == F2FS_FT_FIFO || ftype == F2FS_FT_SOCK)
734 		goto check;
735 
736 	/* init extent info */
737 	get_extent_info(&child.ei, &node_blk->i.i_ext);
738 	child.last_blk = 0;
739 
740 	if (f2fs_has_extra_isize(&node_blk->i)) {
741 		if (c.feature & cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR)) {
742 			unsigned int isize =
743 				le16_to_cpu(node_blk->i.i_extra_isize);
744 			if (isize > 4 * DEF_ADDRS_PER_INODE) {
745 				ASSERT_MSG("[0x%x] wrong i_extra_isize=0x%x",
746 						nid, isize);
747 				if (c.fix_on) {
748 					FIX_MSG("ino[0x%x] recover i_extra_isize "
749 						"from %u to %u",
750 						nid, isize,
751 						calc_extra_isize());
752 					node_blk->i.i_extra_isize =
753 						cpu_to_le16(calc_extra_isize());
754 					need_fix = 1;
755 				}
756 			}
757 		} else {
758 			ASSERT_MSG("[0x%x] wrong extra_attr flag", nid);
759 			if (c.fix_on) {
760 				FIX_MSG("ino[0x%x] remove F2FS_EXTRA_ATTR "
761 					"flag in i_inline:%u",
762 					nid, node_blk->i.i_inline);
763 				/* we don't support tuning F2FS_FEATURE_EXTRA_ATTR now */
764 				node_blk->i.i_inline &= ~F2FS_EXTRA_ATTR;
765 				need_fix = 1;
766 			}
767 		}
768 
769 		if ((c.feature &
770 			cpu_to_le32(F2FS_FEATURE_FLEXIBLE_INLINE_XATTR)) &&
771 			(node_blk->i.i_inline & F2FS_INLINE_XATTR)) {
772 			unsigned int inline_size =
773 				le16_to_cpu(node_blk->i.i_inline_xattr_size);
774 
775 			if (!inline_size ||
776 					inline_size > MAX_INLINE_XATTR_SIZE) {
777 				ASSERT_MSG("[0x%x] wrong inline_xattr_size:%u",
778 						nid, inline_size);
779 				if (c.fix_on) {
780 					FIX_MSG("ino[0x%x] recover inline xattr size "
781 						"from %u to %u",
782 						nid, inline_size,
783 						DEFAULT_INLINE_XATTR_ADDRS);
784 					node_blk->i.i_inline_xattr_size =
785 						cpu_to_le16(DEFAULT_INLINE_XATTR_ADDRS);
786 					need_fix = 1;
787 				}
788 			}
789 		}
790 	}
791 	ofs = get_extra_isize(node_blk);
792 
793 	if ((node_blk->i.i_inline & F2FS_INLINE_DATA)) {
794 		unsigned int inline_size = MAX_INLINE_DATA(node_blk);
795 		block_t blkaddr = le32_to_cpu(node_blk->i.i_addr[ofs]);
796 
797 		if (blkaddr != 0) {
798 			ASSERT_MSG("[0x%x] wrong inline reserve blkaddr:%u",
799 					nid, blkaddr);
800 			if (c.fix_on) {
801 				FIX_MSG("inline_data has wrong 0'th block = %x",
802 								blkaddr);
803 				node_blk->i.i_addr[ofs] = 0;
804 				node_blk->i.i_blocks = cpu_to_le64(*blk_cnt);
805 				need_fix = 1;
806 			}
807 		}
808 		if (i_size > inline_size) {
809 			ASSERT_MSG("[0x%x] wrong inline size:%lu",
810 					nid, (unsigned long)i_size);
811 			if (c.fix_on) {
812 				node_blk->i.i_size = cpu_to_le64(inline_size);
813 				FIX_MSG("inline_data has wrong i_size %lu",
814 							(unsigned long)i_size);
815 				need_fix = 1;
816 			}
817 		}
818 		if (!(node_blk->i.i_inline & F2FS_DATA_EXIST)) {
819 			char buf[MAX_INLINE_DATA(node_blk)];
820 			memset(buf, 0, MAX_INLINE_DATA(node_blk));
821 
822 			if (memcmp(buf, inline_data_addr(node_blk),
823 						MAX_INLINE_DATA(node_blk))) {
824 				ASSERT_MSG("[0x%x] junk inline data", nid);
825 				if (c.fix_on) {
826 					FIX_MSG("inline_data has DATA_EXIST");
827 					node_blk->i.i_inline |= F2FS_DATA_EXIST;
828 					need_fix = 1;
829 				}
830 			}
831 		}
832 		DBG(3, "ino[0x%x] has inline data!\n", nid);
833 		child.state |= FSCK_INLINE_INODE;
834 		goto check;
835 	}
836 
837 	if ((node_blk->i.i_inline & F2FS_INLINE_DENTRY)) {
838 		block_t blkaddr = le32_to_cpu(node_blk->i.i_addr[ofs]);
839 
840 		DBG(3, "ino[0x%x] has inline dentry!\n", nid);
841 		if (blkaddr != 0) {
842 			ASSERT_MSG("[0x%x] wrong inline reserve blkaddr:%u",
843 								nid, blkaddr);
844 			if (c.fix_on) {
845 				FIX_MSG("inline_dentry has wrong 0'th block = %x",
846 								blkaddr);
847 				node_blk->i.i_addr[ofs] = 0;
848 				node_blk->i.i_blocks = cpu_to_le64(*blk_cnt);
849 				need_fix = 1;
850 			}
851 		}
852 
853 		ret = fsck_chk_inline_dentries(sbi, node_blk, &child);
854 		if (ret < 0) {
855 			if (c.fix_on)
856 				need_fix = 1;
857 		}
858 		child.state |= FSCK_INLINE_INODE;
859 		goto check;
860 	}
861 
862 	/* check data blocks in inode */
863 	for (idx = 0; idx < ADDRS_PER_INODE(&node_blk->i);
864 						idx++, child.pgofs++) {
865 		block_t blkaddr = le32_to_cpu(node_blk->i.i_addr[ofs + idx]);
866 
867 		/* check extent info */
868 		check_extent_info(&child, blkaddr, 0);
869 
870 		if (blkaddr == COMPRESS_ADDR) {
871 			if (node_blk->i.i_compr_blocks) {
872 				fsck->chk.valid_blk_cnt++;
873 				*blk_cnt = *blk_cnt + 1;
874 			}
875 			continue;
876 		}
877 
878 		if (blkaddr != 0) {
879 			ret = fsck_chk_data_blk(sbi,
880 					IS_CASEFOLDED(&node_blk->i),
881 					blkaddr,
882 					&child, (i_blocks == *blk_cnt),
883 					ftype, nid, idx, ni->version,
884 					file_is_encrypt(&node_blk->i));
885 			if (!ret) {
886 				*blk_cnt = *blk_cnt + 1;
887 			} else if (c.fix_on) {
888 				node_blk->i.i_addr[ofs + idx] = 0;
889 				need_fix = 1;
890 				FIX_MSG("[0x%x] i_addr[%d] = 0",
891 							nid, ofs + idx);
892 			}
893 		}
894 	}
895 
896 	/* readahead node blocks */
897 	for (idx = 0; idx < 5; idx++) {
898 		u32 nid = le32_to_cpu(node_blk->i.i_nid[idx]);
899 		fsck_reada_node_block(sbi, nid);
900 	}
901 
902 	/* check node blocks in inode */
903 	for (idx = 0; idx < 5; idx++) {
904 		nid_t i_nid = le32_to_cpu(node_blk->i.i_nid[idx]);
905 
906 		if (idx == 0 || idx == 1)
907 			ntype = TYPE_DIRECT_NODE;
908 		else if (idx == 2 || idx == 3)
909 			ntype = TYPE_INDIRECT_NODE;
910 		else if (idx == 4)
911 			ntype = TYPE_DOUBLE_INDIRECT_NODE;
912 		else
913 			ASSERT(0);
914 
915 		if (i_nid == 0x0)
916 			goto skip;
917 
918 		ret = fsck_chk_node_blk(sbi, &node_blk->i, i_nid,
919 					ftype, ntype, blk_cnt, &child);
920 		if (!ret) {
921 			*blk_cnt = *blk_cnt + 1;
922 		} else if (ret == -EINVAL) {
923 			if (c.fix_on) {
924 				node_blk->i.i_nid[idx] = 0;
925 				need_fix = 1;
926 				FIX_MSG("[0x%x] i_nid[%d] = 0", nid, idx);
927 			}
928 skip:
929 			if (ntype == TYPE_DIRECT_NODE)
930 				child.pgofs += ADDRS_PER_BLOCK(&node_blk->i);
931 			else if (ntype == TYPE_INDIRECT_NODE)
932 				child.pgofs += ADDRS_PER_BLOCK(&node_blk->i) *
933 								NIDS_PER_BLOCK;
934 			else
935 				child.pgofs += ADDRS_PER_BLOCK(&node_blk->i) *
936 						NIDS_PER_BLOCK * NIDS_PER_BLOCK;
937 		}
938 
939 	}
940 
941 check:
942 	/* check uncovered range in the back of extent */
943 	check_extent_info(&child, 0, 1);
944 
945 	if (child.state & FSCK_UNMATCHED_EXTENT) {
946 		ASSERT_MSG("ino: 0x%x has wrong ext: [pgofs:%u, blk:%u, len:%u]",
947 				nid, child.ei.fofs, child.ei.blk, child.ei.len);
948 		if (c.fix_on)
949 			need_fix = 1;
950 	}
951 
952 	if (i_blocks != *blk_cnt) {
953 		ASSERT_MSG("ino: 0x%x has i_blocks: %08"PRIx64", "
954 				"but has %u blocks",
955 				nid, i_blocks, *blk_cnt);
956 		if (c.fix_on) {
957 			node_blk->i.i_blocks = cpu_to_le64(*blk_cnt);
958 			need_fix = 1;
959 			FIX_MSG("[0x%x] i_blocks=0x%08"PRIx64" -> 0x%x",
960 					nid, i_blocks, *blk_cnt);
961 		}
962 	}
963 skip_blkcnt_fix:
964 	en = malloc(F2FS_PRINT_NAMELEN);
965 	ASSERT(en);
966 
967 	namelen = le32_to_cpu(node_blk->i.i_namelen);
968 	if (namelen > F2FS_NAME_LEN) {
969 		if (child_d && child_d->i_namelen <= F2FS_NAME_LEN) {
970 			ASSERT_MSG("ino: 0x%x has i_namelen: 0x%x, "
971 					"but has %d characters for name",
972 					nid, namelen, child_d->i_namelen);
973 			if (c.fix_on) {
974 				FIX_MSG("[0x%x] i_namelen=0x%x -> 0x%x", nid, namelen,
975 					child_d->i_namelen);
976 				node_blk->i.i_namelen = cpu_to_le32(child_d->i_namelen);
977 				need_fix = 1;
978 			}
979 			namelen = child_d->i_namelen;
980 		} else
981 			namelen = F2FS_NAME_LEN;
982 	}
983 	pretty_print_filename(node_blk->i.i_name, namelen, en,
984 			      file_enc_name(&node_blk->i));
985 	if (ftype == F2FS_FT_ORPHAN)
986 		DBG(1, "Orphan Inode: 0x%x [%s] i_blocks: %u\n\n",
987 				le32_to_cpu(node_blk->footer.ino),
988 				en, (u32)i_blocks);
989 
990 	if (is_qf_ino(F2FS_RAW_SUPER(sbi), nid))
991 		DBG(1, "Quota Inode: 0x%x [%s] i_blocks: %u\n\n",
992 				le32_to_cpu(node_blk->footer.ino),
993 				en, (u32)i_blocks);
994 
995 	if (ftype == F2FS_FT_DIR) {
996 		DBG(1, "Directory Inode: 0x%x [%s] depth: %d has %d files\n\n",
997 				le32_to_cpu(node_blk->footer.ino), en,
998 				le32_to_cpu(node_blk->i.i_current_depth),
999 				child.files);
1000 
1001 		if (i_links != child.links) {
1002 			ASSERT_MSG("ino: 0x%x i_links: %u, real links: %u",
1003 					nid, i_links, child.links);
1004 			if (c.fix_on) {
1005 				node_blk->i.i_links = cpu_to_le32(child.links);
1006 				need_fix = 1;
1007 				FIX_MSG("Dir: 0x%x i_links= 0x%x -> 0x%x",
1008 						nid, i_links, child.links);
1009 			}
1010 		}
1011 		if (child.dots < 2 &&
1012 				!(node_blk->i.i_inline & F2FS_INLINE_DOTS)) {
1013 			ASSERT_MSG("ino: 0x%x dots: %u",
1014 					nid, child.dots);
1015 			if (c.fix_on) {
1016 				node_blk->i.i_inline |= F2FS_INLINE_DOTS;
1017 				need_fix = 1;
1018 				FIX_MSG("Dir: 0x%x set inline_dots", nid);
1019 			}
1020 		}
1021 	}
1022 
1023 	i_gc_failures = le16_to_cpu(node_blk->i.i_gc_failures);
1024 
1025 	/*
1026 	 * old kernel initialized i_gc_failures as 0x01, in preen mode 2,
1027 	 * let's skip repairing.
1028 	 */
1029 	if (ftype == F2FS_FT_REG_FILE && i_gc_failures &&
1030 		(c.preen_mode != PREEN_MODE_2 || i_gc_failures != 0x01)) {
1031 
1032 		DBG(1, "Regular Inode: 0x%x [%s] depth: %d\n\n",
1033 				le32_to_cpu(node_blk->footer.ino), en,
1034 				i_gc_failures);
1035 
1036 		if (c.fix_on) {
1037 			node_blk->i.i_gc_failures = cpu_to_le16(0);
1038 			need_fix = 1;
1039 			FIX_MSG("Regular: 0x%x reset i_gc_failures from 0x%x to 0x00",
1040 					nid, i_gc_failures);
1041 		}
1042 	}
1043 
1044 	free(en);
1045 
1046 	if (ftype == F2FS_FT_SYMLINK && i_size == 0 &&
1047 			i_blocks == (i_xattr_nid ? 3 : 2)) {
1048 		ASSERT_MSG("ino: 0x%x i_blocks: %lu with zero i_size\n",
1049 						nid, (unsigned long)i_blocks);
1050 		if (c.fix_on) {
1051 			node_blk->i.i_size = cpu_to_le64(F2FS_BLKSIZE);
1052 			need_fix = 1;
1053 			FIX_MSG("Symlink: recover 0x%x with i_size=%lu",
1054 					nid, (unsigned long)F2FS_BLKSIZE);
1055 		}
1056 	}
1057 
1058 	if (ftype == F2FS_FT_ORPHAN && i_links) {
1059 		ASSERT_MSG("ino: 0x%x is orphan inode, but has i_links: %u",
1060 				nid, i_links);
1061 		if (c.fix_on) {
1062 			node_blk->i.i_links = 0;
1063 			need_fix = 1;
1064 			FIX_MSG("ino: 0x%x orphan_inode, i_links= 0x%x -> 0",
1065 					nid, i_links);
1066 		}
1067 	}
1068 
1069 	/* drop extent information to avoid potential wrong access */
1070 	if (need_fix && f2fs_dev_is_writable())
1071 		node_blk->i.i_ext.len = 0;
1072 
1073 	if ((c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM)) &&
1074 				f2fs_has_extra_isize(&node_blk->i)) {
1075 		__u32 provided, calculated;
1076 
1077 		provided = le32_to_cpu(node_blk->i.i_inode_checksum);
1078 		calculated = f2fs_inode_chksum(node_blk);
1079 
1080 		if (provided != calculated) {
1081 			ASSERT_MSG("ino: 0x%x chksum:0x%x, but calculated one is: 0x%x",
1082 				nid, provided, calculated);
1083 			if (c.fix_on) {
1084 				node_blk->i.i_inode_checksum =
1085 							cpu_to_le32(calculated);
1086 				need_fix = 1;
1087 				FIX_MSG("ino: 0x%x recover, i_inode_checksum= 0x%x -> 0x%x",
1088 						nid, provided, calculated);
1089 			}
1090 		}
1091 	}
1092 
1093 	if (need_fix && f2fs_dev_is_writable()) {
1094 		ret = dev_write_block(node_blk, ni->blk_addr);
1095 		ASSERT(ret >= 0);
1096 	}
1097 }
1098 
1099 int fsck_chk_dnode_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
1100 		u32 nid, enum FILE_TYPE ftype, struct f2fs_node *node_blk,
1101 		u32 *blk_cnt, struct child_info *child, struct node_info *ni)
1102 {
1103 	int idx, ret;
1104 	int need_fix = 0;
1105 	child->p_ino = nid;
1106 	child->pp_ino = le32_to_cpu(inode->i_pino);
1107 
1108 	for (idx = 0; idx < ADDRS_PER_BLOCK(inode); idx++, child->pgofs++) {
1109 		block_t blkaddr = le32_to_cpu(node_blk->dn.addr[idx]);
1110 
1111 		check_extent_info(child, blkaddr, 0);
1112 
1113 		if (blkaddr == 0x0)
1114 			continue;
1115 		if (blkaddr == COMPRESS_ADDR) {
1116 			if (inode->i_compr_blocks) {
1117 				F2FS_FSCK(sbi)->chk.valid_blk_cnt++;
1118 				*blk_cnt = *blk_cnt + 1;
1119 			}
1120 			continue;
1121 		}
1122 		ret = fsck_chk_data_blk(sbi, IS_CASEFOLDED(inode),
1123 			blkaddr, child,
1124 			le64_to_cpu(inode->i_blocks) == *blk_cnt, ftype,
1125 			nid, idx, ni->version,
1126 			file_is_encrypt(inode));
1127 		if (!ret) {
1128 			*blk_cnt = *blk_cnt + 1;
1129 		} else if (c.fix_on) {
1130 			node_blk->dn.addr[idx] = 0;
1131 			need_fix = 1;
1132 			FIX_MSG("[0x%x] dn.addr[%d] = 0", nid, idx);
1133 		}
1134 	}
1135 	if (need_fix && f2fs_dev_is_writable()) {
1136 		ret = dev_write_block(node_blk, ni->blk_addr);
1137 		ASSERT(ret >= 0);
1138 	}
1139 	return 0;
1140 }
1141 
1142 int fsck_chk_idnode_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
1143 		enum FILE_TYPE ftype, struct f2fs_node *node_blk, u32 *blk_cnt,
1144 		struct child_info *child)
1145 {
1146 	int need_fix = 0, ret;
1147 	int i = 0;
1148 
1149 	fsck_reada_all_direct_node_blocks(sbi, node_blk);
1150 
1151 	for (i = 0; i < NIDS_PER_BLOCK; i++) {
1152 		if (le32_to_cpu(node_blk->in.nid[i]) == 0x0)
1153 			goto skip;
1154 		ret = fsck_chk_node_blk(sbi, inode,
1155 				le32_to_cpu(node_blk->in.nid[i]),
1156 				ftype, TYPE_DIRECT_NODE, blk_cnt, child);
1157 		if (!ret)
1158 			*blk_cnt = *blk_cnt + 1;
1159 		else if (ret == -EINVAL) {
1160 			if (!c.fix_on)
1161 				printf("should delete in.nid[i] = 0;\n");
1162 			else {
1163 				node_blk->in.nid[i] = 0;
1164 				need_fix = 1;
1165 				FIX_MSG("Set indirect node 0x%x -> 0", i);
1166 			}
1167 skip:
1168 			child->pgofs += ADDRS_PER_BLOCK(&node_blk->i);
1169 		}
1170 	}
1171 
1172 	if (need_fix && f2fs_dev_is_writable()) {
1173 		struct node_info ni;
1174 		nid_t nid = le32_to_cpu(node_blk->footer.nid);
1175 
1176 		get_node_info(sbi, nid, &ni);
1177 		ret = dev_write_block(node_blk, ni.blk_addr);
1178 		ASSERT(ret >= 0);
1179 	}
1180 
1181 	return 0;
1182 }
1183 
1184 int fsck_chk_didnode_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
1185 		enum FILE_TYPE ftype, struct f2fs_node *node_blk, u32 *blk_cnt,
1186 		struct child_info *child)
1187 {
1188 	int i = 0;
1189 	int need_fix = 0, ret = 0;
1190 
1191 	fsck_reada_all_direct_node_blocks(sbi, node_blk);
1192 
1193 	for (i = 0; i < NIDS_PER_BLOCK; i++) {
1194 		if (le32_to_cpu(node_blk->in.nid[i]) == 0x0)
1195 			goto skip;
1196 		ret = fsck_chk_node_blk(sbi, inode,
1197 				le32_to_cpu(node_blk->in.nid[i]),
1198 				ftype, TYPE_INDIRECT_NODE, blk_cnt, child);
1199 		if (!ret)
1200 			*blk_cnt = *blk_cnt + 1;
1201 		else if (ret == -EINVAL) {
1202 			if (!c.fix_on)
1203 				printf("should delete in.nid[i] = 0;\n");
1204 			else {
1205 				node_blk->in.nid[i] = 0;
1206 				need_fix = 1;
1207 				FIX_MSG("Set double indirect node 0x%x -> 0", i);
1208 			}
1209 skip:
1210 			child->pgofs += ADDRS_PER_BLOCK(&node_blk->i) *
1211 							NIDS_PER_BLOCK;
1212 		}
1213 	}
1214 
1215 	if (need_fix && f2fs_dev_is_writable()) {
1216 		struct node_info ni;
1217 		nid_t nid = le32_to_cpu(node_blk->footer.nid);
1218 
1219 		get_node_info(sbi, nid, &ni);
1220 		ret = dev_write_block(node_blk, ni.blk_addr);
1221 		ASSERT(ret >= 0);
1222 	}
1223 
1224 	return 0;
1225 }
1226 
1227 static const char *lookup_table =
1228         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
1229 
1230 /**
1231  * base64_encode() -
1232  *
1233  * Encodes the input string using characters from the set [A-Za-z0-9+,].
1234  * The encoded string is roughly 4/3 times the size of the input string.
1235  */
1236 static int base64_encode(const u8 *src, int len, char *dst)
1237 {
1238 	int i, bits = 0, ac = 0;
1239 	char *cp = dst;
1240 
1241 	for (i = 0; i < len; i++) {
1242 		ac += src[i] << bits;
1243 		bits += 8;
1244 		do {
1245 			*cp++ = lookup_table[ac & 0x3f];
1246 			ac >>= 6;
1247 			bits -= 6;
1248 		} while (bits >= 6);
1249 	}
1250 	if (bits)
1251 		*cp++ = lookup_table[ac & 0x3f];
1252 	return cp - dst;
1253 }
1254 
1255 void pretty_print_filename(const u8 *raw_name, u32 len,
1256 			   char out[F2FS_PRINT_NAMELEN], int enc_name)
1257 {
1258 	len = min(len, (u32)F2FS_NAME_LEN);
1259 
1260 	if (enc_name)
1261 		len = base64_encode(raw_name, len, out);
1262 	else
1263 		memcpy(out, raw_name, len);
1264 	out[len] = 0;
1265 }
1266 
1267 static void print_dentry(__u32 depth, __u8 *name,
1268 		u8 *bitmap, struct f2fs_dir_entry *dentry,
1269 		int max, int idx, int last_blk, int enc_name)
1270 {
1271 	int last_de = 0;
1272 	int next_idx = 0;
1273 	u32 name_len;
1274 	unsigned int i;
1275 	int bit_offset;
1276 	char new[F2FS_PRINT_NAMELEN];
1277 
1278 	if (!c.show_dentry)
1279 		return;
1280 
1281 	name_len = le16_to_cpu(dentry[idx].name_len);
1282 	next_idx = idx + (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN;
1283 
1284 	bit_offset = find_next_bit_le(bitmap, max, next_idx);
1285 	if (bit_offset >= max && last_blk)
1286 		last_de = 1;
1287 
1288 	if (tree_mark_size <= depth) {
1289 		tree_mark_size *= 2;
1290 		ASSERT(tree_mark_size != 0);
1291 		tree_mark = realloc(tree_mark, tree_mark_size);
1292 		ASSERT(tree_mark != NULL);
1293 	}
1294 
1295 	if (last_de)
1296 		tree_mark[depth] = '`';
1297 	else
1298 		tree_mark[depth] = '|';
1299 
1300 	if (tree_mark[depth - 1] == '`')
1301 		tree_mark[depth - 1] = ' ';
1302 
1303 	for (i = 1; i < depth; i++)
1304 		printf("%c   ", tree_mark[i]);
1305 
1306 	pretty_print_filename(name, name_len, new, enc_name);
1307 
1308 	printf("%c-- %s <ino = 0x%x>, <encrypted (%d)>\n",
1309 			last_de ? '`' : '|',
1310 			new, le32_to_cpu(dentry[idx].ino),
1311 			enc_name);
1312 }
1313 
1314 static int f2fs_check_hash_code(int encoding, int casefolded,
1315 			struct f2fs_dir_entry *dentry,
1316 			const unsigned char *name, u32 len, int enc_name)
1317 {
1318 	/* Casefolded Encrypted names require a key to compute siphash */
1319 	if (enc_name && casefolded)
1320 		return 0;
1321 
1322 	f2fs_hash_t hash_code = f2fs_dentry_hash(encoding, casefolded, name, len);
1323 	/* fix hash_code made by old buggy code */
1324 	if (dentry->hash_code != hash_code) {
1325 		char new[F2FS_PRINT_NAMELEN];
1326 
1327 		pretty_print_filename(name, len, new, enc_name);
1328 		FIX_MSG("Mismatch hash_code for \"%s\" [%x:%x]",
1329 				new, le32_to_cpu(dentry->hash_code),
1330 				hash_code);
1331 		dentry->hash_code = cpu_to_le32(hash_code);
1332 		return 1;
1333 	}
1334 	return 0;
1335 }
1336 
1337 
1338 static int __get_current_level(int dir_level, u32 pgofs)
1339 {
1340 	unsigned int bidx = 0;
1341 	int i;
1342 
1343 	for (i = 0; i < MAX_DIR_HASH_DEPTH; i++) {
1344 		bidx += dir_buckets(i, dir_level) * bucket_blocks(i);
1345 		if (bidx > pgofs)
1346 			break;
1347 	}
1348 	return i;
1349 }
1350 
1351 static int f2fs_check_dirent_position(const struct f2fs_dir_entry *dentry,
1352 				      const char *printable_name,
1353 				      u32 pgofs, u8 dir_level, u32 pino)
1354 {
1355 	unsigned int nbucket, nblock;
1356 	unsigned int bidx, end_block;
1357 	int level;
1358 
1359 	level = __get_current_level(dir_level, pgofs);
1360 
1361 	nbucket = dir_buckets(level, dir_level);
1362 	nblock = bucket_blocks(level);
1363 
1364 	bidx = dir_block_index(level, dir_level,
1365 			       le32_to_cpu(dentry->hash_code) % nbucket);
1366 	end_block = bidx + nblock;
1367 
1368 	if (pgofs >= bidx && pgofs < end_block)
1369 		return 0;
1370 
1371 	ASSERT_MSG("Wrong position of dirent pino:%u, name:%s, level:%d, "
1372 		"dir_level:%d, pgofs:%u, correct range:[%u, %u]\n",
1373 		pino, printable_name, level, dir_level, pgofs, bidx,
1374 		end_block - 1);
1375 	return 1;
1376 }
1377 
1378 static int __chk_dots_dentries(struct f2fs_sb_info *sbi,
1379 			       int casefolded,
1380 			       struct f2fs_dir_entry *dentry,
1381 			       struct child_info *child,
1382 			       u8 *name, int len,
1383 			       __u8 (*filename)[F2FS_SLOT_LEN],
1384 			       int enc_name)
1385 {
1386 	int fixed = 0;
1387 
1388 	if ((name[0] == '.' && len == 1)) {
1389 		if (le32_to_cpu(dentry->ino) != child->p_ino) {
1390 			ASSERT_MSG("Bad inode number[0x%x] for '.', parent_ino is [0x%x]\n",
1391 				le32_to_cpu(dentry->ino), child->p_ino);
1392 			dentry->ino = cpu_to_le32(child->p_ino);
1393 			fixed = 1;
1394 		}
1395 	}
1396 
1397 	if (name[0] == '.' && name[1] == '.' && len == 2) {
1398 		if (child->p_ino == F2FS_ROOT_INO(sbi)) {
1399 			if (le32_to_cpu(dentry->ino) != F2FS_ROOT_INO(sbi)) {
1400 				ASSERT_MSG("Bad inode number[0x%x] for '..'\n",
1401 					le32_to_cpu(dentry->ino));
1402 				dentry->ino = cpu_to_le32(F2FS_ROOT_INO(sbi));
1403 				fixed = 1;
1404 			}
1405 		} else if (le32_to_cpu(dentry->ino) != child->pp_ino) {
1406 			ASSERT_MSG("Bad inode number[0x%x] for '..', parent parent ino is [0x%x]\n",
1407 				le32_to_cpu(dentry->ino), child->pp_ino);
1408 			dentry->ino = cpu_to_le32(child->pp_ino);
1409 			fixed = 1;
1410 		}
1411 	}
1412 
1413 	if (f2fs_check_hash_code(get_encoding(sbi), casefolded, dentry, name, len, enc_name))
1414 		fixed = 1;
1415 
1416 	if (name[len] != '\0') {
1417 		ASSERT_MSG("'.' is not NULL terminated\n");
1418 		name[len] = '\0';
1419 		memcpy(*filename, name, len);
1420 		fixed = 1;
1421 	}
1422 	return fixed;
1423 }
1424 
1425 static void nullify_dentry(struct f2fs_dir_entry *dentry, int offs,
1426 			   __u8 (*filename)[F2FS_SLOT_LEN], u8 **bitmap)
1427 {
1428 	memset(dentry, 0, sizeof(struct f2fs_dir_entry));
1429 	test_and_clear_bit_le(offs, *bitmap);
1430 	memset(*filename, 0, F2FS_SLOT_LEN);
1431 }
1432 
1433 static int __chk_dentries(struct f2fs_sb_info *sbi, int casefolded,
1434 			struct child_info *child,
1435 			u8 *bitmap, struct f2fs_dir_entry *dentry,
1436 			__u8 (*filenames)[F2FS_SLOT_LEN],
1437 			int max, int last_blk, int enc_name)
1438 {
1439 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1440 	enum FILE_TYPE ftype;
1441 	int dentries = 0;
1442 	u32 blk_cnt;
1443 	u8 *name;
1444 	char en[F2FS_PRINT_NAMELEN];
1445 	u16 name_len;
1446 	int ret = 0;
1447 	int fixed = 0;
1448 	int i, slots;
1449 
1450 	/* readahead inode blocks */
1451 	for (i = 0; i < max; i++) {
1452 		u32 ino;
1453 
1454 		if (test_bit_le(i, bitmap) == 0)
1455 			continue;
1456 
1457 		ino = le32_to_cpu(dentry[i].ino);
1458 
1459 		if (IS_VALID_NID(sbi, ino)) {
1460 			struct node_info ni;
1461 
1462 			get_node_info(sbi, ino, &ni);
1463 			if (IS_VALID_BLK_ADDR(sbi, ni.blk_addr)) {
1464 				dev_reada_block(ni.blk_addr);
1465 				name_len = le16_to_cpu(dentry[i].name_len);
1466 				if (name_len > 0)
1467 					i += (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN - 1;
1468 			}
1469 		}
1470 	}
1471 
1472 	for (i = 0; i < max;) {
1473 		if (test_bit_le(i, bitmap) == 0) {
1474 			i++;
1475 			continue;
1476 		}
1477 		if (!IS_VALID_NID(sbi, le32_to_cpu(dentry[i].ino))) {
1478 			ASSERT_MSG("Bad dentry 0x%x with invalid NID/ino 0x%x",
1479 				    i, le32_to_cpu(dentry[i].ino));
1480 			if (c.fix_on) {
1481 				FIX_MSG("Clear bad dentry 0x%x with bad ino 0x%x",
1482 					i, le32_to_cpu(dentry[i].ino));
1483 				test_and_clear_bit_le(i, bitmap);
1484 				fixed = 1;
1485 			}
1486 			i++;
1487 			continue;
1488 		}
1489 
1490 		ftype = dentry[i].file_type;
1491 		if ((ftype <= F2FS_FT_UNKNOWN || ftype > F2FS_FT_LAST_FILE_TYPE)) {
1492 			ASSERT_MSG("Bad dentry 0x%x with unexpected ftype 0x%x",
1493 						le32_to_cpu(dentry[i].ino), ftype);
1494 			if (c.fix_on) {
1495 				FIX_MSG("Clear bad dentry 0x%x with bad ftype 0x%x",
1496 					i, ftype);
1497 				test_and_clear_bit_le(i, bitmap);
1498 				fixed = 1;
1499 			}
1500 			i++;
1501 			continue;
1502 		}
1503 
1504 		name_len = le16_to_cpu(dentry[i].name_len);
1505 
1506 		if (name_len == 0 || name_len > F2FS_NAME_LEN) {
1507 			ASSERT_MSG("Bad dentry 0x%x with invalid name_len", i);
1508 			if (c.fix_on) {
1509 				FIX_MSG("Clear bad dentry 0x%x", i);
1510 				test_and_clear_bit_le(i, bitmap);
1511 				fixed = 1;
1512 			}
1513 			i++;
1514 			continue;
1515 		}
1516 		name = calloc(name_len + 1, 1);
1517 		ASSERT(name);
1518 
1519 		memcpy(name, filenames[i], name_len);
1520 		slots = (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN;
1521 
1522 		/* Becareful. 'dentry.file_type' is not imode. */
1523 		if (ftype == F2FS_FT_DIR) {
1524 			if ((name[0] == '.' && name_len == 1) ||
1525 				(name[0] == '.' && name[1] == '.' &&
1526 							name_len == 2)) {
1527 				ret = __chk_dots_dentries(sbi, casefolded, &dentry[i],
1528 					child, name, name_len, &filenames[i],
1529 					enc_name);
1530 				switch (ret) {
1531 				case 1:
1532 					fixed = 1;
1533 				case 0:
1534 					child->dots++;
1535 					break;
1536 				}
1537 
1538 				if (child->dots > 2) {
1539 					ASSERT_MSG("More than one '.' or '..', should delete the extra one\n");
1540 					nullify_dentry(&dentry[i], i,
1541 						       &filenames[i], &bitmap);
1542 					child->dots--;
1543 					fixed = 1;
1544 				}
1545 
1546 				i++;
1547 				free(name);
1548 				continue;
1549 			}
1550 		}
1551 
1552 		if (f2fs_check_hash_code(get_encoding(sbi), casefolded, dentry + i, name, name_len, enc_name))
1553 			fixed = 1;
1554 
1555 		pretty_print_filename(name, name_len, en, enc_name);
1556 
1557 		if (max == NR_DENTRY_IN_BLOCK) {
1558 			ret = f2fs_check_dirent_position(dentry + i, en,
1559 					child->pgofs, child->dir_level,
1560 					child->p_ino);
1561 			if (ret) {
1562 				if (c.fix_on) {
1563 					FIX_MSG("Clear bad dentry 0x%x", i);
1564 					test_and_clear_bit_le(i, bitmap);
1565 					fixed = 1;
1566 				}
1567 				i++;
1568 				free(name);
1569 				continue;
1570 			}
1571 		}
1572 
1573 		DBG(1, "[%3u]-[0x%x] name[%s] len[0x%x] ino[0x%x] type[0x%x]\n",
1574 				fsck->dentry_depth, i, en, name_len,
1575 				le32_to_cpu(dentry[i].ino),
1576 				dentry[i].file_type);
1577 
1578 		print_dentry(fsck->dentry_depth, name, bitmap,
1579 				dentry, max, i, last_blk, enc_name);
1580 
1581 		blk_cnt = 1;
1582 		child->i_namelen = name_len;
1583 		ret = fsck_chk_node_blk(sbi,
1584 				NULL, le32_to_cpu(dentry[i].ino),
1585 				ftype, TYPE_INODE, &blk_cnt, child);
1586 
1587 		if (ret && c.fix_on) {
1588 			int j;
1589 
1590 			for (j = 0; j < slots; j++)
1591 				test_and_clear_bit_le(i + j, bitmap);
1592 			FIX_MSG("Unlink [0x%x] - %s len[0x%x], type[0x%x]",
1593 					le32_to_cpu(dentry[i].ino),
1594 					en, name_len,
1595 					dentry[i].file_type);
1596 			fixed = 1;
1597 		} else if (ret == 0) {
1598 			if (ftype == F2FS_FT_DIR)
1599 				child->links++;
1600 			dentries++;
1601 			child->files++;
1602 		}
1603 
1604 		i += slots;
1605 		free(name);
1606 	}
1607 	return fixed ? -1 : dentries;
1608 }
1609 
1610 int fsck_chk_inline_dentries(struct f2fs_sb_info *sbi,
1611 		struct f2fs_node *node_blk, struct child_info *child)
1612 {
1613 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1614 	struct f2fs_dentry_ptr d;
1615 	void *inline_dentry;
1616 	int dentries;
1617 
1618 	inline_dentry = inline_data_addr(node_blk);
1619 	ASSERT(inline_dentry != NULL);
1620 
1621 	make_dentry_ptr(&d, node_blk, inline_dentry, 2);
1622 
1623 	fsck->dentry_depth++;
1624 	dentries = __chk_dentries(sbi, IS_CASEFOLDED(&node_blk->i), child,
1625 			d.bitmap, d.dentry, d.filename, d.max, 1,
1626 			file_is_encrypt(&node_blk->i));// pass through
1627 	if (dentries < 0) {
1628 		DBG(1, "[%3d] Inline Dentry Block Fixed hash_codes\n\n",
1629 			fsck->dentry_depth);
1630 	} else {
1631 		DBG(1, "[%3d] Inline Dentry Block Done : "
1632 				"dentries:%d in %d slots (len:%d)\n\n",
1633 			fsck->dentry_depth, dentries,
1634 			d.max, F2FS_NAME_LEN);
1635 	}
1636 	fsck->dentry_depth--;
1637 	return dentries;
1638 }
1639 
1640 int fsck_chk_dentry_blk(struct f2fs_sb_info *sbi, int casefolded, u32 blk_addr,
1641 		struct child_info *child, int last_blk, int enc_name)
1642 {
1643 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1644 	struct f2fs_dentry_block *de_blk;
1645 	int dentries, ret;
1646 
1647 	de_blk = (struct f2fs_dentry_block *)calloc(BLOCK_SZ, 1);
1648 	ASSERT(de_blk != NULL);
1649 
1650 	ret = dev_read_block(de_blk, blk_addr);
1651 	ASSERT(ret >= 0);
1652 
1653 	fsck->dentry_depth++;
1654 	dentries = __chk_dentries(sbi, casefolded, child,
1655 			de_blk->dentry_bitmap,
1656 			de_blk->dentry, de_blk->filename,
1657 			NR_DENTRY_IN_BLOCK, last_blk, enc_name);
1658 
1659 	if (dentries < 0 && f2fs_dev_is_writable()) {
1660 		ret = dev_write_block(de_blk, blk_addr);
1661 		ASSERT(ret >= 0);
1662 		DBG(1, "[%3d] Dentry Block [0x%x] Fixed hash_codes\n\n",
1663 			fsck->dentry_depth, blk_addr);
1664 	} else {
1665 		DBG(1, "[%3d] Dentry Block [0x%x] Done : "
1666 				"dentries:%d in %d slots (len:%d)\n\n",
1667 			fsck->dentry_depth, blk_addr, dentries,
1668 			NR_DENTRY_IN_BLOCK, F2FS_NAME_LEN);
1669 	}
1670 	fsck->dentry_depth--;
1671 	free(de_blk);
1672 	return 0;
1673 }
1674 
1675 int fsck_chk_data_blk(struct f2fs_sb_info *sbi, int casefolded,
1676 		u32 blk_addr, struct child_info *child, int last_blk,
1677 		enum FILE_TYPE ftype, u32 parent_nid, u16 idx_in_node, u8 ver,
1678 		int enc_name)
1679 {
1680 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1681 
1682 	/* Is it reserved block? */
1683 	if (blk_addr == NEW_ADDR) {
1684 		fsck->chk.valid_blk_cnt++;
1685 		return 0;
1686 	}
1687 
1688 	if (!IS_VALID_BLK_ADDR(sbi, blk_addr)) {
1689 		ASSERT_MSG("blkaddress is not valid. [0x%x]", blk_addr);
1690 		return -EINVAL;
1691 	}
1692 
1693 	if (is_valid_ssa_data_blk(sbi, blk_addr, parent_nid,
1694 						idx_in_node, ver)) {
1695 		ASSERT_MSG("summary data block is not valid. [0x%x]",
1696 						parent_nid);
1697 		return -EINVAL;
1698 	}
1699 
1700 	if (f2fs_test_sit_bitmap(sbi, blk_addr) == 0)
1701 		ASSERT_MSG("SIT bitmap is 0x0. blk_addr[0x%x]", blk_addr);
1702 
1703 	if (f2fs_test_main_bitmap(sbi, blk_addr) != 0)
1704 		ASSERT_MSG("Duplicated data [0x%x]. pnid[0x%x] idx[0x%x]",
1705 				blk_addr, parent_nid, idx_in_node);
1706 
1707 	fsck->chk.valid_blk_cnt++;
1708 
1709 	if (ftype == F2FS_FT_DIR) {
1710 		f2fs_set_main_bitmap(sbi, blk_addr, CURSEG_HOT_DATA);
1711 		return fsck_chk_dentry_blk(sbi, casefolded, blk_addr, child,
1712 						last_blk, enc_name);
1713 	} else {
1714 		f2fs_set_main_bitmap(sbi, blk_addr, CURSEG_WARM_DATA);
1715 	}
1716 	return 0;
1717 }
1718 
1719 int fsck_chk_orphan_node(struct f2fs_sb_info *sbi)
1720 {
1721 	u32 blk_cnt = 0;
1722 	block_t start_blk, orphan_blkaddr, i, j;
1723 	struct f2fs_orphan_block *orphan_blk, *new_blk;
1724 	struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
1725 	u32 entry_count;
1726 
1727 	if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG))
1728 		return 0;
1729 
1730 	start_blk = __start_cp_addr(sbi) + 1 + get_sb(cp_payload);
1731 	orphan_blkaddr = __start_sum_addr(sbi) - 1 - get_sb(cp_payload);
1732 
1733 	f2fs_ra_meta_pages(sbi, start_blk, orphan_blkaddr, META_CP);
1734 
1735 	orphan_blk = calloc(BLOCK_SZ, 1);
1736 	ASSERT(orphan_blk);
1737 
1738 	new_blk = calloc(BLOCK_SZ, 1);
1739 	ASSERT(new_blk);
1740 
1741 	for (i = 0; i < orphan_blkaddr; i++) {
1742 		int ret = dev_read_block(orphan_blk, start_blk + i);
1743 		u32 new_entry_count = 0;
1744 
1745 		ASSERT(ret >= 0);
1746 		entry_count = le32_to_cpu(orphan_blk->entry_count);
1747 
1748 		for (j = 0; j < entry_count; j++) {
1749 			nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
1750 			DBG(1, "[%3d] ino [0x%x]\n", i, ino);
1751 			struct node_info ni;
1752 			blk_cnt = 1;
1753 
1754 			if (c.preen_mode == PREEN_MODE_1 && !c.fix_on) {
1755 				get_node_info(sbi, ino, &ni);
1756 				if (!IS_VALID_NID(sbi, ino) ||
1757 						!IS_VALID_BLK_ADDR(sbi, ni.blk_addr))
1758 					return -EINVAL;
1759 
1760 				continue;
1761 			}
1762 
1763 			ret = fsck_chk_node_blk(sbi, NULL, ino,
1764 					F2FS_FT_ORPHAN, TYPE_INODE, &blk_cnt,
1765 					NULL);
1766 			if (!ret)
1767 				new_blk->ino[new_entry_count++] =
1768 							orphan_blk->ino[j];
1769 			else if (ret && c.fix_on)
1770 				FIX_MSG("[0x%x] remove from orphan list", ino);
1771 			else if (ret)
1772 				ASSERT_MSG("[0x%x] wrong orphan inode", ino);
1773 		}
1774 		if (f2fs_dev_is_writable() && c.fix_on &&
1775 				entry_count != new_entry_count) {
1776 			new_blk->entry_count = cpu_to_le32(new_entry_count);
1777 			ret = dev_write_block(new_blk, start_blk + i);
1778 			ASSERT(ret >= 0);
1779 		}
1780 		memset(orphan_blk, 0, BLOCK_SZ);
1781 		memset(new_blk, 0, BLOCK_SZ);
1782 	}
1783 	free(orphan_blk);
1784 	free(new_blk);
1785 
1786 	return 0;
1787 }
1788 
1789 int fsck_chk_quota_node(struct f2fs_sb_info *sbi)
1790 {
1791 	struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
1792 	enum quota_type qtype;
1793 	int ret = 0;
1794 	u32 blk_cnt = 0;
1795 
1796 	for (qtype = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
1797 		if (sb->qf_ino[qtype] == 0)
1798 			continue;
1799 		nid_t ino = QUOTA_INO(sb, qtype);
1800 		struct node_info ni;
1801 
1802 		DBG(1, "qtype [%d] ino [0x%x]\n", qtype, ino);
1803 		blk_cnt = 1;
1804 
1805 		if (c.preen_mode == PREEN_MODE_1 && !c.fix_on) {
1806 			get_node_info(sbi, ino, &ni);
1807 			if (!IS_VALID_NID(sbi, ino) ||
1808 					!IS_VALID_BLK_ADDR(sbi, ni.blk_addr))
1809 				return -EINVAL;
1810 			continue;
1811 		}
1812 		ret = fsck_chk_node_blk(sbi, NULL, ino,
1813 				F2FS_FT_REG_FILE, TYPE_INODE, &blk_cnt, NULL);
1814 		if (ret)
1815 			ASSERT_MSG("wrong quota inode, qtype [%d] ino [0x%x]",
1816 								qtype, ino);
1817 	}
1818 	return ret;
1819 }
1820 
1821 int fsck_chk_quota_files(struct f2fs_sb_info *sbi)
1822 {
1823 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1824 	struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
1825 	enum quota_type qtype;
1826 	f2fs_ino_t ino;
1827 	int ret = 0;
1828 	int needs_writeout;
1829 
1830 	/* Return if quota feature is disabled */
1831 	if (!fsck->qctx)
1832 		return 0;
1833 
1834 	for (qtype = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
1835 		ino = sb->qf_ino[qtype];
1836 		if (!ino)
1837 			continue;
1838 
1839 	        DBG(1, "Checking Quota file ([%3d] ino [0x%x])\n", qtype, ino);
1840 		needs_writeout = 0;
1841 		ret = quota_compare_and_update(sbi, qtype, &needs_writeout,
1842 						c.preserve_limits);
1843 		if (ret == 0 && needs_writeout == 0) {
1844 			DBG(1, "OK\n");
1845 			continue;
1846 		}
1847 
1848 		/* Something is wrong */
1849 		if (c.fix_on) {
1850 			DBG(0, "Fixing Quota file ([%3d] ino [0x%x])\n",
1851 							qtype, ino);
1852 			f2fs_filesize_update(sbi, ino, 0);
1853 			ret = quota_write_inode(sbi, qtype);
1854 			if (!ret) {
1855 				c.bug_on = 1;
1856 				DBG(1, "OK\n");
1857 			} else {
1858 				ASSERT_MSG("Unable to write quota file");
1859 			}
1860 		} else {
1861 			ASSERT_MSG("Quota file is missing or invalid"
1862 					" quota file content found.");
1863 		}
1864 	}
1865 	return ret;
1866 }
1867 
1868 int fsck_chk_meta(struct f2fs_sb_info *sbi)
1869 {
1870 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1871 	struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
1872 	struct seg_entry *se;
1873 	unsigned int sit_valid_segs = 0, sit_node_blks = 0;
1874 	unsigned int i;
1875 
1876 	/* 1. check sit usage with CP: curseg is lost? */
1877 	for (i = 0; i < TOTAL_SEGS(sbi); i++) {
1878 		se = get_seg_entry(sbi, i);
1879 		if (se->valid_blocks != 0)
1880 			sit_valid_segs++;
1881 		else if (IS_CUR_SEGNO(sbi, i)) {
1882 			/* curseg has not been written back to device */
1883 			MSG(1, "\tInfo: curseg %u is counted in valid segs\n", i);
1884 			sit_valid_segs++;
1885 		}
1886 		if (IS_NODESEG(se->type))
1887 			sit_node_blks += se->valid_blocks;
1888 	}
1889 	if (fsck->chk.sit_free_segs + sit_valid_segs != TOTAL_SEGS(sbi)) {
1890 		ASSERT_MSG("SIT usage does not match: sit_free_segs %u, "
1891 				"sit_valid_segs %u, total_segs %u",
1892 			fsck->chk.sit_free_segs, sit_valid_segs,
1893 			TOTAL_SEGS(sbi));
1894 		return -EINVAL;
1895 	}
1896 
1897 	/* 2. check node count */
1898 	if (fsck->chk.valid_nat_entry_cnt != sit_node_blks) {
1899 		ASSERT_MSG("node count does not match: valid_nat_entry_cnt %u,"
1900 			" sit_node_blks %u",
1901 			fsck->chk.valid_nat_entry_cnt, sit_node_blks);
1902 		return -EINVAL;
1903 	}
1904 
1905 	/* 3. check SIT with CP */
1906 	if (fsck->chk.sit_free_segs != le32_to_cpu(cp->free_segment_count)) {
1907 		ASSERT_MSG("free segs does not match: sit_free_segs %u, "
1908 				"free_segment_count %u",
1909 				fsck->chk.sit_free_segs,
1910 				le32_to_cpu(cp->free_segment_count));
1911 		return -EINVAL;
1912 	}
1913 
1914 	/* 4. check NAT with CP */
1915 	if (fsck->chk.valid_nat_entry_cnt !=
1916 					le32_to_cpu(cp->valid_node_count)) {
1917 		ASSERT_MSG("valid node does not match: valid_nat_entry_cnt %u,"
1918 				" valid_node_count %u",
1919 				fsck->chk.valid_nat_entry_cnt,
1920 				le32_to_cpu(cp->valid_node_count));
1921 		return -EINVAL;
1922 	}
1923 
1924 	/* 4. check orphan inode simply */
1925 	if (fsck_chk_orphan_node(sbi))
1926 		return -EINVAL;
1927 
1928 	/* 5. check nat entry -- must be done before quota check */
1929 	for (i = 0; i < fsck->nr_nat_entries; i++) {
1930 		u32 blk = le32_to_cpu(fsck->entries[i].block_addr);
1931 		nid_t ino = le32_to_cpu(fsck->entries[i].ino);
1932 
1933 		if (!blk)
1934 			/*
1935 			 * skip entry whose ino is 0, otherwise, we will
1936 			 * get a negative number by BLKOFF_FROM_MAIN(sbi, blk)
1937 			 */
1938 			continue;
1939 
1940 		if (!IS_VALID_BLK_ADDR(sbi, blk)) {
1941 			MSG(0, "\tError: nat entry[ino %u block_addr 0x%x]"
1942 				" is in valid\n",
1943 				ino, blk);
1944 			return -EINVAL;
1945 		}
1946 
1947 		if (!f2fs_test_sit_bitmap(sbi, blk)) {
1948 			MSG(0, "\tError: nat entry[ino %u block_addr 0x%x]"
1949 				" not find it in sit_area_bitmap\n",
1950 				ino, blk);
1951 			return -EINVAL;
1952 		}
1953 
1954 		if (!IS_VALID_NID(sbi, ino)) {
1955 			MSG(0, "\tError: nat_entry->ino %u exceeds the range"
1956 				" of nat entries %u\n",
1957 				ino, fsck->nr_nat_entries);
1958 			return -EINVAL;
1959 		}
1960 
1961 		if (!f2fs_test_bit(ino, fsck->nat_area_bitmap)) {
1962 			MSG(0, "\tError: nat_entry->ino %u is not set in"
1963 				" nat_area_bitmap\n", ino);
1964 			return -EINVAL;
1965 		}
1966 	}
1967 
1968 	/* 6. check quota inode simply */
1969 	if (fsck_chk_quota_node(sbi))
1970 		return -EINVAL;
1971 
1972 	if (fsck->nat_valid_inode_cnt != le32_to_cpu(cp->valid_inode_count)) {
1973 		ASSERT_MSG("valid inode does not match: nat_valid_inode_cnt %u,"
1974 				" valid_inode_count %u",
1975 				fsck->nat_valid_inode_cnt,
1976 				le32_to_cpu(cp->valid_inode_count));
1977 		return -EINVAL;
1978 	}
1979 
1980 	return 0;
1981 }
1982 
1983 void fsck_chk_checkpoint(struct f2fs_sb_info *sbi)
1984 {
1985 	struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
1986 
1987 	if (get_cp(ckpt_flags) & CP_LARGE_NAT_BITMAP_FLAG) {
1988 		if (get_cp(checksum_offset) != CP_MIN_CHKSUM_OFFSET) {
1989 			ASSERT_MSG("Deprecated layout of large_nat_bitmap, "
1990 				"chksum_offset:%u", get_cp(checksum_offset));
1991 			c.fix_chksum = 1;
1992 		}
1993 	}
1994 }
1995 
1996 void fsck_init(struct f2fs_sb_info *sbi)
1997 {
1998 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1999 	struct f2fs_sm_info *sm_i = SM_I(sbi);
2000 
2001 	/*
2002 	 * We build three bitmap for main/sit/nat so that may check consistency
2003 	 * of filesystem.
2004 	 * 1. main_area_bitmap will be used to check whether all blocks of main
2005 	 *    area is used or not.
2006 	 * 2. nat_area_bitmap has bitmap information of used nid in NAT.
2007 	 * 3. sit_area_bitmap has bitmap information of used main block.
2008 	 * At Last sequence, we compare main_area_bitmap with sit_area_bitmap.
2009 	 */
2010 	fsck->nr_main_blks = sm_i->main_segments << sbi->log_blocks_per_seg;
2011 	fsck->main_area_bitmap_sz = (fsck->nr_main_blks + 7) / 8;
2012 	fsck->main_area_bitmap = calloc(fsck->main_area_bitmap_sz, 1);
2013 	ASSERT(fsck->main_area_bitmap != NULL);
2014 
2015 	build_nat_area_bitmap(sbi);
2016 
2017 	build_sit_area_bitmap(sbi);
2018 
2019 	ASSERT(tree_mark_size != 0);
2020 	tree_mark = calloc(tree_mark_size, 1);
2021 	ASSERT(tree_mark != NULL);
2022 }
2023 
2024 static void fix_hard_links(struct f2fs_sb_info *sbi)
2025 {
2026 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2027 	struct hard_link_node *tmp, *node;
2028 	struct f2fs_node *node_blk = NULL;
2029 	struct node_info ni;
2030 	int ret;
2031 
2032 	if (fsck->hard_link_list_head == NULL)
2033 		return;
2034 
2035 	node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
2036 	ASSERT(node_blk != NULL);
2037 
2038 	node = fsck->hard_link_list_head;
2039 	while (node) {
2040 		/* Sanity check */
2041 		if (sanity_check_nid(sbi, node->nid, node_blk,
2042 					F2FS_FT_MAX, TYPE_INODE, &ni))
2043 			FIX_MSG("Failed to fix, rerun fsck.f2fs");
2044 
2045 		node_blk->i.i_links = cpu_to_le32(node->actual_links);
2046 
2047 		FIX_MSG("File: 0x%x i_links= 0x%x -> 0x%x",
2048 				node->nid, node->links, node->actual_links);
2049 
2050 		ret = dev_write_block(node_blk, ni.blk_addr);
2051 		ASSERT(ret >= 0);
2052 		tmp = node;
2053 		node = node->next;
2054 		free(tmp);
2055 	}
2056 	free(node_blk);
2057 }
2058 
2059 static void fix_nat_entries(struct f2fs_sb_info *sbi)
2060 {
2061 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2062 	u32 i;
2063 
2064 	for (i = 0; i < fsck->nr_nat_entries; i++)
2065 		if (f2fs_test_bit(i, fsck->nat_area_bitmap) != 0)
2066 			nullify_nat_entry(sbi, i);
2067 }
2068 
2069 static void flush_curseg_sit_entries(struct f2fs_sb_info *sbi)
2070 {
2071 	struct sit_info *sit_i = SIT_I(sbi);
2072 	struct f2fs_sit_block *sit_blk;
2073 	int i;
2074 
2075 	sit_blk = calloc(BLOCK_SZ, 1);
2076 	ASSERT(sit_blk);
2077 	/* update curseg sit entries, since we may change
2078 	 * a segment type in move_curseg_info
2079 	 */
2080 	for (i = 0; i < NO_CHECK_TYPE; i++) {
2081 		struct curseg_info *curseg = CURSEG_I(sbi, i);
2082 		struct f2fs_sit_entry *sit;
2083 		struct seg_entry *se;
2084 
2085 		se = get_seg_entry(sbi, curseg->segno);
2086 		get_current_sit_page(sbi, curseg->segno, sit_blk);
2087 		sit = &sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, curseg->segno)];
2088 		sit->vblocks = cpu_to_le16((se->type << SIT_VBLOCKS_SHIFT) |
2089 							se->valid_blocks);
2090 		rewrite_current_sit_page(sbi, curseg->segno, sit_blk);
2091 	}
2092 
2093 	free(sit_blk);
2094 }
2095 
2096 static void fix_checksum(struct f2fs_sb_info *sbi)
2097 {
2098 	struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
2099 	struct f2fs_nm_info *nm_i = NM_I(sbi);
2100 	struct sit_info *sit_i = SIT_I(sbi);
2101 	void *bitmap_offset;
2102 
2103 	if (!c.fix_chksum)
2104 		return;
2105 
2106 	bitmap_offset = cp->sit_nat_version_bitmap + sizeof(__le32);
2107 
2108 	memcpy(bitmap_offset, nm_i->nat_bitmap, nm_i->bitmap_size);
2109 	memcpy(bitmap_offset + nm_i->bitmap_size,
2110 			sit_i->sit_bitmap, sit_i->bitmap_size);
2111 }
2112 
2113 static void fix_checkpoint(struct f2fs_sb_info *sbi)
2114 {
2115 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2116 	struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
2117 	struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
2118 	unsigned long long cp_blk_no;
2119 	u32 flags = c.alloc_failed ? CP_FSCK_FLAG: CP_UMOUNT_FLAG;
2120 	block_t orphan_blks = 0;
2121 	block_t cp_blocks;
2122 	u32 i;
2123 	int ret;
2124 	u_int32_t crc = 0;
2125 
2126 	/* should call from fsck */
2127 	ASSERT(c.func == FSCK);
2128 
2129 	if (is_set_ckpt_flags(cp, CP_ORPHAN_PRESENT_FLAG)) {
2130 		orphan_blks = __start_sum_addr(sbi) - 1;
2131 		flags |= CP_ORPHAN_PRESENT_FLAG;
2132 	}
2133 	if (is_set_ckpt_flags(cp, CP_TRIMMED_FLAG))
2134 		flags |= CP_TRIMMED_FLAG;
2135 	if (is_set_ckpt_flags(cp, CP_DISABLED_FLAG))
2136 		flags |= CP_DISABLED_FLAG;
2137 	if (is_set_ckpt_flags(cp, CP_LARGE_NAT_BITMAP_FLAG)) {
2138 		flags |= CP_LARGE_NAT_BITMAP_FLAG;
2139 		set_cp(checksum_offset, CP_MIN_CHKSUM_OFFSET);
2140 	} else {
2141 		set_cp(checksum_offset, CP_CHKSUM_OFFSET);
2142 	}
2143 
2144 	if (flags & CP_UMOUNT_FLAG)
2145 		cp_blocks = 8;
2146 	else
2147 		cp_blocks = 5;
2148 
2149 	set_cp(cp_pack_total_block_count, cp_blocks +
2150 				orphan_blks + get_sb(cp_payload));
2151 
2152 	flags = update_nat_bits_flags(sb, cp, flags);
2153 	flags |= CP_NOCRC_RECOVERY_FLAG;
2154 	set_cp(ckpt_flags, flags);
2155 
2156 	set_cp(free_segment_count, get_free_segments(sbi));
2157 	set_cp(valid_block_count, fsck->chk.valid_blk_cnt);
2158 	set_cp(valid_node_count, fsck->chk.valid_node_cnt);
2159 	set_cp(valid_inode_count, fsck->chk.valid_inode_cnt);
2160 
2161 	crc = f2fs_checkpoint_chksum(cp);
2162 	*((__le32 *)((unsigned char *)cp + get_cp(checksum_offset))) =
2163 							cpu_to_le32(crc);
2164 
2165 	cp_blk_no = get_sb(cp_blkaddr);
2166 	if (sbi->cur_cp == 2)
2167 		cp_blk_no += 1 << get_sb(log_blocks_per_seg);
2168 
2169 	ret = dev_write_block(cp, cp_blk_no++);
2170 	ASSERT(ret >= 0);
2171 
2172 	for (i = 0; i < get_sb(cp_payload); i++) {
2173 		ret = dev_write_block(((unsigned char *)cp) +
2174 					(i + 1) * F2FS_BLKSIZE, cp_blk_no++);
2175 		ASSERT(ret >= 0);
2176 	}
2177 
2178 	cp_blk_no += orphan_blks;
2179 
2180 	for (i = 0; i < NO_CHECK_TYPE; i++) {
2181 		struct curseg_info *curseg = CURSEG_I(sbi, i);
2182 
2183 		if (!(flags & CP_UMOUNT_FLAG) && IS_NODESEG(i))
2184 			continue;
2185 
2186 		ret = dev_write_block(curseg->sum_blk, cp_blk_no++);
2187 		ASSERT(ret >= 0);
2188 	}
2189 
2190 	/* Write nat bits */
2191 	if (flags & CP_NAT_BITS_FLAG)
2192 		write_nat_bits(sbi, sb, cp, sbi->cur_cp);
2193 
2194 	ret = f2fs_fsync_device();
2195 	ASSERT(ret >= 0);
2196 
2197 	ret = dev_write_block(cp, cp_blk_no++);
2198 	ASSERT(ret >= 0);
2199 
2200 	ret = f2fs_fsync_device();
2201 	ASSERT(ret >= 0);
2202 }
2203 
2204 static void fix_checkpoints(struct f2fs_sb_info *sbi)
2205 {
2206 	/* copy valid checkpoint to its mirror position */
2207 	duplicate_checkpoint(sbi);
2208 
2209 	/* repair checkpoint at CP #0 position */
2210 	sbi->cur_cp = 1;
2211 	fix_checkpoint(sbi);
2212 }
2213 
2214 #ifdef HAVE_LINUX_BLKZONED_H
2215 
2216 /*
2217  * Refer valid block map and return offset of the last valid block in the zone.
2218  * Obtain valid block map from SIT and fsync data.
2219  * If there is no valid block in the zone, return -1.
2220  */
2221 static int last_vblk_off_in_zone(struct f2fs_sb_info *sbi,
2222 				 unsigned int zone_segno)
2223 {
2224 	int s, b;
2225 	unsigned int segs_per_zone = sbi->segs_per_sec * sbi->secs_per_zone;
2226 	struct seg_entry *se;
2227 
2228 	for (s = segs_per_zone - 1; s >= 0; s--) {
2229 		se = get_seg_entry(sbi, zone_segno + s);
2230 
2231 		/*
2232 		 * Refer not cur_valid_map but ckpt_valid_map which reflects
2233 		 * fsync data.
2234 		 */
2235 		ASSERT(se->ckpt_valid_map);
2236 		for (b = sbi->blocks_per_seg - 1; b >= 0; b--)
2237 			if (f2fs_test_bit(b, (const char*)se->ckpt_valid_map))
2238 				return b + (s << sbi->log_blocks_per_seg);
2239 	}
2240 
2241 	return -1;
2242 }
2243 
2244 static int check_curseg_write_pointer(struct f2fs_sb_info *sbi, int type)
2245 {
2246 	struct curseg_info *curseg = CURSEG_I(sbi, type);
2247 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2248 	struct blk_zone blkz;
2249 	block_t cs_block, wp_block, zone_last_vblock;
2250 	u_int64_t cs_sector, wp_sector;
2251 	int i, ret;
2252 	unsigned int zone_segno;
2253 	int log_sectors_per_block = sbi->log_blocksize - SECTOR_SHIFT;
2254 
2255 	/* get the device the curseg points to */
2256 	cs_block = START_BLOCK(sbi, curseg->segno) + curseg->next_blkoff;
2257 	for (i = 0; i < MAX_DEVICES; i++) {
2258 		if (!c.devices[i].path)
2259 			break;
2260 		if (c.devices[i].start_blkaddr <= cs_block &&
2261 		    cs_block <= c.devices[i].end_blkaddr)
2262 			break;
2263 	}
2264 
2265 	if (i >= MAX_DEVICES)
2266 		return -EINVAL;
2267 
2268 	/* get write pointer position of the zone the curseg points to */
2269 	cs_sector = (cs_block - c.devices[i].start_blkaddr)
2270 		<< log_sectors_per_block;
2271 	ret = f2fs_report_zone(i, cs_sector, &blkz);
2272 	if (ret)
2273 		return ret;
2274 
2275 	if (blk_zone_type(&blkz) != BLK_ZONE_TYPE_SEQWRITE_REQ)
2276 		return 0;
2277 
2278 	/* check consistency between the curseg and the write pointer */
2279 	wp_block = c.devices[i].start_blkaddr +
2280 		(blk_zone_wp_sector(&blkz) >> log_sectors_per_block);
2281 	wp_sector = blk_zone_wp_sector(&blkz);
2282 
2283 	if (cs_sector == wp_sector)
2284 		return 0;
2285 
2286 	if (cs_sector > wp_sector) {
2287 		MSG(0, "Inconsistent write pointer with curseg %d: "
2288 		    "curseg %d[0x%x,0x%x] > wp[0x%x,0x%x]\n",
2289 		    type, type, curseg->segno, curseg->next_blkoff,
2290 		    GET_SEGNO(sbi, wp_block), OFFSET_IN_SEG(sbi, wp_block));
2291 		fsck->chk.wp_inconsistent_zones++;
2292 		return -EINVAL;
2293 	}
2294 
2295 	MSG(0, "Write pointer goes advance from curseg %d: "
2296 	    "curseg %d[0x%x,0x%x] wp[0x%x,0x%x]\n",
2297 	    type, type, curseg->segno, curseg->next_blkoff,
2298 	    GET_SEGNO(sbi, wp_block), OFFSET_IN_SEG(sbi, wp_block));
2299 
2300 	zone_segno = GET_SEG_FROM_SEC(sbi,
2301 				      GET_SEC_FROM_SEG(sbi, curseg->segno));
2302 	zone_last_vblock = START_BLOCK(sbi, zone_segno) +
2303 		last_vblk_off_in_zone(sbi, zone_segno);
2304 
2305 	/*
2306 	 * If valid blocks exist between the curseg position and the write
2307 	 * pointer, they are fsync data. This is not an error to fix. Leave it
2308 	 * for kernel to recover later.
2309 	 * If valid blocks exist between the curseg's zone start and the curseg
2310 	 * position, or if there is no valid block in the curseg's zone, fix
2311 	 * the inconsistency between the curseg and the writ pointer.
2312 	 * Of Note is that if there is no valid block in the curseg's zone,
2313 	 * last_vblk_off_in_zone() returns -1 and zone_last_vblock is always
2314 	 * smaller than cs_block.
2315 	 */
2316 	if (cs_block <= zone_last_vblock && zone_last_vblock < wp_block) {
2317 		MSG(0, "Curseg has fsync data: curseg %d[0x%x,0x%x] "
2318 		    "last valid block in zone[0x%x,0x%x]\n",
2319 		    type, curseg->segno, curseg->next_blkoff,
2320 		    GET_SEGNO(sbi, zone_last_vblock),
2321 		    OFFSET_IN_SEG(sbi, zone_last_vblock));
2322 		return 0;
2323 	}
2324 
2325 	fsck->chk.wp_inconsistent_zones++;
2326 	return -EINVAL;
2327 }
2328 
2329 #else
2330 
2331 static int check_curseg_write_pointer(struct f2fs_sb_info *UNUSED(sbi),
2332 					int UNUSED(type))
2333 {
2334 	return 0;
2335 }
2336 
2337 #endif
2338 
2339 int check_curseg_offset(struct f2fs_sb_info *sbi, int type)
2340 {
2341 	struct curseg_info *curseg = CURSEG_I(sbi, type);
2342 	struct seg_entry *se;
2343 	int j, nblocks;
2344 
2345 	if ((curseg->next_blkoff >> 3) >= SIT_VBLOCK_MAP_SIZE) {
2346 		ASSERT_MSG("Next block offset:%u is invalid, type:%d",
2347 			curseg->next_blkoff, type);
2348 		return -EINVAL;
2349 	}
2350 	se = get_seg_entry(sbi, curseg->segno);
2351 	if (f2fs_test_bit(curseg->next_blkoff,
2352 				(const char *)se->cur_valid_map)) {
2353 		ASSERT_MSG("Next block offset is not free, type:%d", type);
2354 		return -EINVAL;
2355 	}
2356 	if (curseg->alloc_type == SSR)
2357 		return 0;
2358 
2359 	nblocks = sbi->blocks_per_seg;
2360 	for (j = curseg->next_blkoff + 1; j < nblocks; j++) {
2361 		if (f2fs_test_bit(j, (const char *)se->cur_valid_map)) {
2362 			ASSERT_MSG("For LFS curseg, space after .next_blkoff "
2363 				"should be unused, type:%d", type);
2364 			return -EINVAL;
2365 		}
2366 	}
2367 
2368 	if (c.zoned_model == F2FS_ZONED_HM)
2369 		return check_curseg_write_pointer(sbi, type);
2370 
2371 	return 0;
2372 }
2373 
2374 int check_curseg_offsets(struct f2fs_sb_info *sbi)
2375 {
2376 	int i, ret;
2377 
2378 	for (i = 0; i < NO_CHECK_TYPE; i++) {
2379 		ret = check_curseg_offset(sbi, i);
2380 		if (ret)
2381 			return ret;
2382 	}
2383 	return 0;
2384 }
2385 
2386 static void fix_curseg_info(struct f2fs_sb_info *sbi)
2387 {
2388 	int i, need_update = 0;
2389 
2390 	for (i = 0; i < NO_CHECK_TYPE; i++) {
2391 		if (check_curseg_offset(sbi, i)) {
2392 			update_curseg_info(sbi, i);
2393 			need_update = 1;
2394 		}
2395 	}
2396 
2397 	if (need_update) {
2398 		write_curseg_info(sbi);
2399 		flush_curseg_sit_entries(sbi);
2400 	}
2401 }
2402 
2403 int check_sit_types(struct f2fs_sb_info *sbi)
2404 {
2405 	unsigned int i;
2406 	int err = 0;
2407 
2408 	for (i = 0; i < TOTAL_SEGS(sbi); i++) {
2409 		struct seg_entry *se;
2410 
2411 		se = get_seg_entry(sbi, i);
2412 		if (se->orig_type != se->type) {
2413 			if (se->orig_type == CURSEG_COLD_DATA &&
2414 					se->type <= CURSEG_COLD_DATA) {
2415 				se->type = se->orig_type;
2416 			} else {
2417 				FIX_MSG("Wrong segment type [0x%x] %x -> %x",
2418 						i, se->orig_type, se->type);
2419 				err = -EINVAL;
2420 			}
2421 		}
2422 	}
2423 	return err;
2424 }
2425 
2426 static struct f2fs_node *fsck_get_lpf(struct f2fs_sb_info *sbi)
2427 {
2428 	struct f2fs_node *node;
2429 	struct node_info ni;
2430 	nid_t lpf_ino;
2431 	int err;
2432 
2433 	/* read root inode first */
2434 	node = calloc(F2FS_BLKSIZE, 1);
2435 	ASSERT(node);
2436 	get_node_info(sbi, F2FS_ROOT_INO(sbi), &ni);
2437 	err = dev_read_block(node, ni.blk_addr);
2438 	ASSERT(err >= 0);
2439 
2440 	/* lookup lost+found in root directory */
2441 	lpf_ino = f2fs_lookup(sbi, node, (u8 *)LPF, strlen(LPF));
2442 	if (lpf_ino) { /* found */
2443 		get_node_info(sbi, lpf_ino, &ni);
2444 		err = dev_read_block(node, ni.blk_addr);
2445 		ASSERT(err >= 0);
2446 		DBG(1, "Found lost+found 0x%x at blkaddr [0x%x]\n",
2447 		    lpf_ino, ni.blk_addr);
2448 		if (!S_ISDIR(le16_to_cpu(node->i.i_mode))) {
2449 			ASSERT_MSG("lost+found is not directory [0%o]\n",
2450 				   le16_to_cpu(node->i.i_mode));
2451 			/* FIXME: give up? */
2452 			goto out;
2453 		}
2454 	} else { /* not found, create it */
2455 		struct dentry de;
2456 
2457 		memset(&de, 0, sizeof(de));
2458 		de.name = (u8 *) LPF;
2459 		de.len = strlen(LPF);
2460 		de.mode = 0x41c0;
2461 		de.pino = F2FS_ROOT_INO(sbi),
2462 		de.file_type = F2FS_FT_DIR,
2463 		de.uid = getuid();
2464 		de.gid = getgid();
2465 		de.mtime = time(NULL);
2466 
2467 		err = f2fs_mkdir(sbi, &de);
2468 		if (err) {
2469 			ASSERT_MSG("Failed create lost+found");
2470 			goto out;
2471 		}
2472 
2473 		get_node_info(sbi, de.ino, &ni);
2474 		err = dev_read_block(node, ni.blk_addr);
2475 		ASSERT(err >= 0);
2476 		DBG(1, "Create lost+found 0x%x at blkaddr [0x%x]\n",
2477 		    de.ino, ni.blk_addr);
2478 	}
2479 
2480 	c.lpf_ino = le32_to_cpu(node->footer.ino);
2481 	return node;
2482 out:
2483 	free(node);
2484 	return NULL;
2485 }
2486 
2487 static int fsck_do_reconnect_file(struct f2fs_sb_info *sbi,
2488 				  struct f2fs_node *lpf,
2489 				  struct f2fs_node *fnode)
2490 {
2491 	char name[80];
2492 	size_t namelen;
2493 	nid_t ino = le32_to_cpu(fnode->footer.ino);
2494 	struct node_info ni;
2495 	int ftype, ret;
2496 
2497 	namelen = snprintf(name, 80, "%u", ino);
2498 	if (namelen >= 80)
2499 		/* ignore terminating '\0', should never happen */
2500 		namelen = 79;
2501 
2502 	if (f2fs_lookup(sbi, lpf, (u8 *)name, namelen)) {
2503 		ASSERT_MSG("Name %s already exist in lost+found", name);
2504 		return -EEXIST;
2505 	}
2506 
2507 	get_node_info(sbi, le32_to_cpu(lpf->footer.ino), &ni);
2508 	ftype = map_de_type(le16_to_cpu(fnode->i.i_mode));
2509 	ret = f2fs_add_link(sbi, lpf, (unsigned char *)name, namelen,
2510 			    ino, ftype, ni.blk_addr, 0);
2511 	if (ret) {
2512 		ASSERT_MSG("Failed to add inode [0x%x] to lost+found", ino);
2513 		return -EINVAL;
2514 	}
2515 
2516 	/* update fnode */
2517 	memcpy(fnode->i.i_name, name, namelen);
2518 	fnode->i.i_namelen = cpu_to_le32(namelen);
2519 	fnode->i.i_pino = c.lpf_ino;
2520 	get_node_info(sbi, le32_to_cpu(fnode->footer.ino), &ni);
2521 	ret = dev_write_block(fnode, ni.blk_addr);
2522 	ASSERT(ret >= 0);
2523 
2524 	DBG(1, "Reconnect inode [0x%x] to lost+found\n", ino);
2525 	return 0;
2526 }
2527 
2528 static void fsck_failed_reconnect_file_dnode(struct f2fs_sb_info *sbi,
2529 					     nid_t nid)
2530 {
2531 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2532 	struct f2fs_node *node;
2533 	struct node_info ni;
2534 	u32 addr;
2535 	int i, err;
2536 
2537 	node = calloc(F2FS_BLKSIZE, 1);
2538 	ASSERT(node);
2539 
2540 	get_node_info(sbi, nid, &ni);
2541 	err = dev_read_block(node, ni.blk_addr);
2542 	ASSERT(err >= 0);
2543 
2544 	fsck->chk.valid_node_cnt--;
2545 	fsck->chk.valid_blk_cnt--;
2546 	f2fs_clear_main_bitmap(sbi, ni.blk_addr);
2547 
2548 	for (i = 0; i < ADDRS_PER_BLOCK(&node->i); i++) {
2549 		addr = le32_to_cpu(node->dn.addr[i]);
2550 		if (!addr)
2551 			continue;
2552 		fsck->chk.valid_blk_cnt--;
2553 		if (addr == NEW_ADDR)
2554 			continue;
2555 		f2fs_clear_main_bitmap(sbi, addr);
2556 	}
2557 
2558 	free(node);
2559 }
2560 
2561 static void fsck_failed_reconnect_file_idnode(struct f2fs_sb_info *sbi,
2562 					      nid_t nid)
2563 {
2564 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2565 	struct f2fs_node *node;
2566 	struct node_info ni;
2567 	nid_t tmp;
2568 	int i, err;
2569 
2570 	node = calloc(F2FS_BLKSIZE, 1);
2571 	ASSERT(node);
2572 
2573 	get_node_info(sbi, nid, &ni);
2574 	err = dev_read_block(node, ni.blk_addr);
2575 	ASSERT(err >= 0);
2576 
2577 	fsck->chk.valid_node_cnt--;
2578 	fsck->chk.valid_blk_cnt--;
2579 	f2fs_clear_main_bitmap(sbi, ni.blk_addr);
2580 
2581 	for (i = 0; i < NIDS_PER_BLOCK; i++) {
2582 		tmp = le32_to_cpu(node->in.nid[i]);
2583 		if (!tmp)
2584 			continue;
2585 		fsck_failed_reconnect_file_dnode(sbi, tmp);
2586 	}
2587 
2588 	free(node);
2589 }
2590 
2591 static void fsck_failed_reconnect_file_didnode(struct f2fs_sb_info *sbi,
2592 					       nid_t nid)
2593 {
2594 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2595 	struct f2fs_node *node;
2596 	struct node_info ni;
2597 	nid_t tmp;
2598 	int i, err;
2599 
2600 	node = calloc(F2FS_BLKSIZE, 1);
2601 	ASSERT(node);
2602 
2603 	get_node_info(sbi, nid, &ni);
2604 	err = dev_read_block(node, ni.blk_addr);
2605 	ASSERT(err >= 0);
2606 
2607 	fsck->chk.valid_node_cnt--;
2608 	fsck->chk.valid_blk_cnt--;
2609 	f2fs_clear_main_bitmap(sbi, ni.blk_addr);
2610 
2611 	for (i = 0; i < NIDS_PER_BLOCK; i++) {
2612 		tmp = le32_to_cpu(node->in.nid[i]);
2613 		if (!tmp)
2614 			continue;
2615 		fsck_failed_reconnect_file_idnode(sbi, tmp);
2616 	}
2617 
2618 	free(node);
2619 }
2620 
2621 /*
2622  * Counters and main_area_bitmap are already changed during checking
2623  * inode block, so clear them. There is no need to clear new blocks
2624  * allocted to lost+found.
2625  */
2626 static void fsck_failed_reconnect_file(struct f2fs_sb_info *sbi, nid_t ino)
2627 {
2628 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2629 	struct f2fs_node *node;
2630 	struct node_info ni;
2631 	nid_t nid;
2632 	int ofs, i, err;
2633 
2634 	node = calloc(F2FS_BLKSIZE, 1);
2635 	ASSERT(node);
2636 
2637 	get_node_info(sbi, ino, &ni);
2638 	err = dev_read_block(node, ni.blk_addr);
2639 	ASSERT(err >= 0);
2640 
2641 	/* clear inode counters */
2642 	fsck->chk.valid_inode_cnt--;
2643 	fsck->chk.valid_node_cnt--;
2644 	fsck->chk.valid_blk_cnt--;
2645 	f2fs_clear_main_bitmap(sbi, ni.blk_addr);
2646 
2647 	/* clear xnid counters */
2648 	if (node->i.i_xattr_nid) {
2649 		nid = le32_to_cpu(node->i.i_xattr_nid);
2650 		fsck->chk.valid_node_cnt--;
2651 		fsck->chk.valid_blk_cnt--;
2652 		get_node_info(sbi, nid, &ni);
2653 		f2fs_clear_main_bitmap(sbi, ni.blk_addr);
2654 	}
2655 
2656 	/* clear data counters */
2657 	if(!(node->i.i_inline & F2FS_INLINE_DATA)) {
2658 		ofs = get_extra_isize(node);
2659 		for (i = 0; i < ADDRS_PER_INODE(&node->i); i++) {
2660 			block_t addr = le32_to_cpu(node->i.i_addr[ofs + i]);
2661 			if (!addr)
2662 				continue;
2663 			fsck->chk.valid_blk_cnt--;
2664 			if (addr == NEW_ADDR)
2665 				continue;
2666 			f2fs_clear_main_bitmap(sbi, addr);
2667 		}
2668 	}
2669 
2670 	for (i = 0; i < 5; i++) {
2671 		nid = le32_to_cpu(node->i.i_nid[i]);
2672 		if (!nid)
2673 			continue;
2674 
2675 		switch (i) {
2676 		case 0: /* direct node */
2677 		case 1:
2678 			fsck_failed_reconnect_file_dnode(sbi, nid);
2679 			break;
2680 		case 2: /* indirect node */
2681 		case 3:
2682 			fsck_failed_reconnect_file_idnode(sbi, nid);
2683 			break;
2684 		case 4: /* double indirect node */
2685 			fsck_failed_reconnect_file_didnode(sbi, nid);
2686 			break;
2687 		}
2688 	}
2689 
2690 	free(node);
2691 }
2692 
2693 /*
2694  * Scan unreachable nids and find only regular file inodes. If these files
2695  * are not corrupted, reconnect them to lost+found.
2696  *
2697  * Since all unreachable nodes are already checked, we can allocate new
2698  * blocks safely.
2699  *
2700  * This function returns the number of files been reconnected.
2701  */
2702 static int fsck_reconnect_file(struct f2fs_sb_info *sbi)
2703 {
2704 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2705 	struct f2fs_node *lpf_node, *node;
2706 	struct node_info ni;
2707 	char *reconnect_bitmap;
2708 	u32 blk_cnt;
2709 	nid_t nid;
2710 	int err, cnt = 0, ftype;
2711 
2712 	node = calloc(F2FS_BLKSIZE, 1);
2713 	ASSERT(node);
2714 
2715 	reconnect_bitmap = calloc(fsck->nat_area_bitmap_sz, 1);
2716 	ASSERT(reconnect_bitmap);
2717 
2718 	for (nid = 0; nid < fsck->nr_nat_entries; nid++) {
2719 		if (f2fs_test_bit(nid, fsck->nat_area_bitmap)) {
2720 			if (is_qf_ino(F2FS_RAW_SUPER(sbi), nid)) {
2721 				DBG(1, "Not support quota inode [0x%x]\n",
2722 				    nid);
2723 				continue;
2724 			}
2725 
2726 			get_node_info(sbi, nid, &ni);
2727 			err = dev_read_block(node, ni.blk_addr);
2728 			ASSERT(err >= 0);
2729 
2730 			/* reconnection will restore these nodes if needed */
2731 			if (node->footer.ino != node->footer.nid) {
2732 				DBG(1, "Not support non-inode node [0x%x]\n",
2733 				    nid);
2734 				continue;
2735 			}
2736 
2737 			if (S_ISDIR(le16_to_cpu(node->i.i_mode))) {
2738 				DBG(1, "Not support directory inode [0x%x]\n",
2739 				    nid);
2740 				continue;
2741 			}
2742 
2743 			ftype = map_de_type(le16_to_cpu(node->i.i_mode));
2744 			if (sanity_check_nid(sbi, nid, node, ftype,
2745 					     TYPE_INODE, &ni)) {
2746 				ASSERT_MSG("Invalid nid [0x%x]\n", nid);
2747 				continue;
2748 			}
2749 
2750 			DBG(1, "Check inode 0x%x\n", nid);
2751 			blk_cnt = 1;
2752 			fsck_chk_inode_blk(sbi, nid, ftype, node,
2753 					   &blk_cnt, &ni, NULL);
2754 
2755 			f2fs_set_bit(nid, reconnect_bitmap);
2756 		}
2757 	}
2758 
2759 	lpf_node = fsck_get_lpf(sbi);
2760 	if (!lpf_node)
2761 		goto out;
2762 
2763 	for (nid = 0; nid < fsck->nr_nat_entries; nid++) {
2764 		if (f2fs_test_bit(nid, reconnect_bitmap)) {
2765 			get_node_info(sbi, nid, &ni);
2766 			err = dev_read_block(node, ni.blk_addr);
2767 			ASSERT(err >= 0);
2768 
2769 			if (fsck_do_reconnect_file(sbi, lpf_node, node)) {
2770 				DBG(1, "Failed to reconnect inode [0x%x]\n",
2771 				    nid);
2772 				fsck_failed_reconnect_file(sbi, nid);
2773 				continue;
2774 			}
2775 
2776 			quota_add_inode_usage(fsck->qctx, nid, &node->i);
2777 
2778 			DBG(1, "Reconnected inode [0x%x] to lost+found\n", nid);
2779 			cnt++;
2780 		}
2781 	}
2782 
2783 out:
2784 	free(node);
2785 	free(lpf_node);
2786 	free(reconnect_bitmap);
2787 	return cnt;
2788 }
2789 
2790 #ifdef HAVE_LINUX_BLKZONED_H
2791 
2792 struct write_pointer_check_data {
2793 	struct f2fs_sb_info *sbi;
2794 	int dev_index;
2795 };
2796 
2797 static int chk_and_fix_wp_with_sit(int i, void *blkzone, void *opaque)
2798 {
2799 	struct blk_zone *blkz = (struct blk_zone *)blkzone;
2800 	struct write_pointer_check_data *wpd = opaque;
2801 	struct f2fs_sb_info *sbi = wpd->sbi;
2802 	struct device_info *dev = c.devices + wpd->dev_index;
2803 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2804 	block_t zone_block, wp_block, wp_blkoff;
2805 	unsigned int zone_segno, wp_segno;
2806 	struct curseg_info *cs;
2807 	int cs_index, ret, last_valid_blkoff;
2808 	int log_sectors_per_block = sbi->log_blocksize - SECTOR_SHIFT;
2809 	unsigned int segs_per_zone = sbi->segs_per_sec * sbi->secs_per_zone;
2810 
2811 	if (blk_zone_conv(blkz))
2812 		return 0;
2813 
2814 	zone_block = dev->start_blkaddr
2815 		+ (blk_zone_sector(blkz) >> log_sectors_per_block);
2816 	zone_segno = GET_SEGNO(sbi, zone_block);
2817 	if (zone_segno >= MAIN_SEGS(sbi))
2818 		return 0;
2819 
2820 	wp_block = dev->start_blkaddr
2821 		+ (blk_zone_wp_sector(blkz) >> log_sectors_per_block);
2822 	wp_segno = GET_SEGNO(sbi, wp_block);
2823 	wp_blkoff = wp_block - START_BLOCK(sbi, wp_segno);
2824 
2825 	/* if a curseg points to the zone, skip the check */
2826 	for (cs_index = 0; cs_index < NO_CHECK_TYPE; cs_index++) {
2827 		cs = &SM_I(sbi)->curseg_array[cs_index];
2828 		if (zone_segno <= cs->segno &&
2829 		    cs->segno < zone_segno + segs_per_zone)
2830 			return 0;
2831 	}
2832 
2833 	last_valid_blkoff = last_vblk_off_in_zone(sbi, zone_segno);
2834 
2835 	/*
2836 	 * When there is no valid block in the zone, check write pointer is
2837 	 * at zone start. If not, reset the write pointer.
2838 	 */
2839 	if (last_valid_blkoff < 0 &&
2840 	    blk_zone_wp_sector(blkz) != blk_zone_sector(blkz)) {
2841 		if (!c.fix_on) {
2842 			MSG(0, "Inconsistent write pointer: wp[0x%x,0x%x]\n",
2843 			    wp_segno, wp_blkoff);
2844 			fsck->chk.wp_inconsistent_zones++;
2845 			return 0;
2846 		}
2847 
2848 		FIX_MSG("Reset write pointer of zone at segment 0x%x",
2849 			zone_segno);
2850 		ret = f2fs_reset_zone(wpd->dev_index, blkz);
2851 		if (ret) {
2852 			printf("[FSCK] Write pointer reset failed: %s\n",
2853 			       dev->path);
2854 			return ret;
2855 		}
2856 		fsck->chk.wp_fixed = 1;
2857 		return 0;
2858 	}
2859 
2860 	/*
2861 	 * If valid blocks exist in the zone beyond the write pointer, it
2862 	 * is a bug. No need to fix because the zone is not selected for the
2863 	 * write. Just report it.
2864 	 */
2865 	if (last_valid_blkoff + zone_block > wp_block) {
2866 		MSG(0, "Unexpected invalid write pointer: wp[0x%x,0x%x]\n",
2867 		    wp_segno, wp_blkoff);
2868 		return 0;
2869 	}
2870 
2871 	return 0;
2872 }
2873 
2874 static void fix_wp_sit_alignment(struct f2fs_sb_info *sbi)
2875 {
2876 	unsigned int i;
2877 	struct write_pointer_check_data wpd = {	sbi, 0 };
2878 
2879 	if (c.zoned_model != F2FS_ZONED_HM)
2880 		return;
2881 
2882 	for (i = 0; i < MAX_DEVICES; i++) {
2883 		if (!c.devices[i].path)
2884 			break;
2885 		if (c.devices[i].zoned_model != F2FS_ZONED_HM)
2886 			break;
2887 
2888 		wpd.dev_index = i;
2889 		if (f2fs_report_zones(i, chk_and_fix_wp_with_sit, &wpd)) {
2890 			printf("[FSCK] Write pointer check failed: %s\n",
2891 			       c.devices[i].path);
2892 			return;
2893 		}
2894 	}
2895 }
2896 
2897 #else
2898 
2899 static void fix_wp_sit_alignment(struct f2fs_sb_info *UNUSED(sbi))
2900 {
2901 	return;
2902 }
2903 
2904 #endif
2905 
2906 /*
2907  * Check and fix consistency with write pointers at the beginning of
2908  * fsck so that following writes by fsck do not fail.
2909  */
2910 void fsck_chk_and_fix_write_pointers(struct f2fs_sb_info *sbi)
2911 {
2912 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2913 
2914 	if (c.zoned_model != F2FS_ZONED_HM)
2915 		return;
2916 
2917 	if (check_curseg_offsets(sbi) && c.fix_on) {
2918 		fix_curseg_info(sbi);
2919 		fsck->chk.wp_fixed = 1;
2920 	}
2921 
2922 	fix_wp_sit_alignment(sbi);
2923 }
2924 
2925 int fsck_chk_curseg_info(struct f2fs_sb_info *sbi)
2926 {
2927 	struct curseg_info *curseg;
2928 	struct seg_entry *se;
2929 	struct f2fs_summary_block *sum_blk;
2930 	int i, ret = 0;
2931 
2932 	for (i = 0; i < NO_CHECK_TYPE; i++) {
2933 		curseg = CURSEG_I(sbi, i);
2934 		se = get_seg_entry(sbi, curseg->segno);
2935 		sum_blk = curseg->sum_blk;
2936 
2937 		if (se->type != i) {
2938 			ASSERT_MSG("Incorrect curseg [%d]: segno [0x%x] "
2939 				   "type(SIT) [%d]", i, curseg->segno,
2940 				   se->type);
2941 			if (c.fix_on || c.preen_mode)
2942 				se->type = i;
2943 			ret = -1;
2944 		}
2945 		if (i <= CURSEG_COLD_DATA && IS_SUM_DATA_SEG(sum_blk->footer)) {
2946 			continue;
2947 		} else if (i > CURSEG_COLD_DATA && IS_SUM_NODE_SEG(sum_blk->footer)) {
2948 			continue;
2949 		} else {
2950 			ASSERT_MSG("Incorrect curseg [%d]: segno [0x%x] "
2951 				   "type(SSA) [%d]", i, curseg->segno,
2952 				   sum_blk->footer.entry_type);
2953 			if (c.fix_on || c.preen_mode)
2954 				sum_blk->footer.entry_type =
2955 					i <= CURSEG_COLD_DATA ?
2956 					SUM_TYPE_DATA : SUM_TYPE_NODE;
2957 			ret = -1;
2958 		}
2959 	}
2960 
2961 	return ret;
2962 }
2963 
2964 int fsck_verify(struct f2fs_sb_info *sbi)
2965 {
2966 	unsigned int i = 0;
2967 	int ret = 0;
2968 	int force = 0;
2969 	u32 nr_unref_nid = 0;
2970 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2971 	struct hard_link_node *node = NULL;
2972 
2973 	printf("\n");
2974 
2975 	if (c.zoned_model == F2FS_ZONED_HM) {
2976 		printf("[FSCK] Write pointers consistency                    ");
2977 		if (fsck->chk.wp_inconsistent_zones == 0x0) {
2978 			printf(" [Ok..]\n");
2979 		} else {
2980 			printf(" [Fail] [0x%x]\n",
2981 			       fsck->chk.wp_inconsistent_zones);
2982 			c.bug_on = 1;
2983 		}
2984 
2985 		if (fsck->chk.wp_fixed && c.fix_on)
2986 			force = 1;
2987 	}
2988 
2989 	if (c.feature & cpu_to_le32(F2FS_FEATURE_LOST_FOUND)) {
2990 		for (i = 0; i < fsck->nr_nat_entries; i++)
2991 			if (f2fs_test_bit(i, fsck->nat_area_bitmap) != 0)
2992 				break;
2993 		if (i < fsck->nr_nat_entries) {
2994 			i = fsck_reconnect_file(sbi);
2995 			printf("[FSCK] Reconnect %u files to lost+found\n", i);
2996 		}
2997 	}
2998 
2999 	for (i = 0; i < fsck->nr_nat_entries; i++) {
3000 		if (f2fs_test_bit(i, fsck->nat_area_bitmap) != 0) {
3001 			struct node_info ni;
3002 
3003 			get_node_info(sbi, i, &ni);
3004 			printf("NID[0x%x] is unreachable, blkaddr:0x%x\n",
3005 							i, ni.blk_addr);
3006 			nr_unref_nid++;
3007 		}
3008 	}
3009 
3010 	if (fsck->hard_link_list_head != NULL) {
3011 		node = fsck->hard_link_list_head;
3012 		while (node) {
3013 			printf("NID[0x%x] has [0x%x] more unreachable links\n",
3014 					node->nid, node->links);
3015 			node = node->next;
3016 		}
3017 		c.bug_on = 1;
3018 	}
3019 
3020 	printf("[FSCK] Unreachable nat entries                       ");
3021 	if (nr_unref_nid == 0x0) {
3022 		printf(" [Ok..] [0x%x]\n", nr_unref_nid);
3023 	} else {
3024 		printf(" [Fail] [0x%x]\n", nr_unref_nid);
3025 		ret = EXIT_ERR_CODE;
3026 		c.bug_on = 1;
3027 	}
3028 
3029 	printf("[FSCK] SIT valid block bitmap checking                ");
3030 	if (memcmp(fsck->sit_area_bitmap, fsck->main_area_bitmap,
3031 					fsck->sit_area_bitmap_sz) == 0x0) {
3032 		printf("[Ok..]\n");
3033 	} else {
3034 		printf("[Fail]\n");
3035 		ret = EXIT_ERR_CODE;
3036 		c.bug_on = 1;
3037 	}
3038 
3039 	printf("[FSCK] Hard link checking for regular file           ");
3040 	if (fsck->hard_link_list_head == NULL) {
3041 		printf(" [Ok..] [0x%x]\n", fsck->chk.multi_hard_link_files);
3042 	} else {
3043 		printf(" [Fail] [0x%x]\n", fsck->chk.multi_hard_link_files);
3044 		ret = EXIT_ERR_CODE;
3045 		c.bug_on = 1;
3046 	}
3047 
3048 	printf("[FSCK] valid_block_count matching with CP            ");
3049 	if (sbi->total_valid_block_count == fsck->chk.valid_blk_cnt) {
3050 		printf(" [Ok..] [0x%x]\n", (u32)fsck->chk.valid_blk_cnt);
3051 	} else {
3052 		printf(" [Fail] [0x%x]\n", (u32)fsck->chk.valid_blk_cnt);
3053 		ret = EXIT_ERR_CODE;
3054 		c.bug_on = 1;
3055 	}
3056 
3057 	printf("[FSCK] valid_node_count matching with CP (de lookup) ");
3058 	if (sbi->total_valid_node_count == fsck->chk.valid_node_cnt) {
3059 		printf(" [Ok..] [0x%x]\n", fsck->chk.valid_node_cnt);
3060 	} else {
3061 		printf(" [Fail] [0x%x]\n", fsck->chk.valid_node_cnt);
3062 		ret = EXIT_ERR_CODE;
3063 		c.bug_on = 1;
3064 	}
3065 
3066 	printf("[FSCK] valid_node_count matching with CP (nat lookup)");
3067 	if (sbi->total_valid_node_count == fsck->chk.valid_nat_entry_cnt) {
3068 		printf(" [Ok..] [0x%x]\n", fsck->chk.valid_nat_entry_cnt);
3069 	} else {
3070 		printf(" [Fail] [0x%x]\n", fsck->chk.valid_nat_entry_cnt);
3071 		ret = EXIT_ERR_CODE;
3072 		c.bug_on = 1;
3073 	}
3074 
3075 	printf("[FSCK] valid_inode_count matched with CP             ");
3076 	if (sbi->total_valid_inode_count == fsck->chk.valid_inode_cnt) {
3077 		printf(" [Ok..] [0x%x]\n", fsck->chk.valid_inode_cnt);
3078 	} else {
3079 		printf(" [Fail] [0x%x]\n", fsck->chk.valid_inode_cnt);
3080 		ret = EXIT_ERR_CODE;
3081 		c.bug_on = 1;
3082 	}
3083 
3084 	printf("[FSCK] free segment_count matched with CP            ");
3085 	if (le32_to_cpu(F2FS_CKPT(sbi)->free_segment_count) ==
3086 						fsck->chk.sit_free_segs) {
3087 		printf(" [Ok..] [0x%x]\n", fsck->chk.sit_free_segs);
3088 	} else {
3089 		printf(" [Fail] [0x%x]\n", fsck->chk.sit_free_segs);
3090 		ret = EXIT_ERR_CODE;
3091 		c.bug_on = 1;
3092 	}
3093 
3094 	printf("[FSCK] next block offset is free                     ");
3095 	if (check_curseg_offsets(sbi) == 0) {
3096 		printf(" [Ok..]\n");
3097 	} else {
3098 		printf(" [Fail]\n");
3099 		ret = EXIT_ERR_CODE;
3100 		c.bug_on = 1;
3101 	}
3102 
3103 	printf("[FSCK] fixing SIT types\n");
3104 	if (check_sit_types(sbi) != 0)
3105 		force = 1;
3106 
3107 	printf("[FSCK] other corrupted bugs                          ");
3108 	if (c.bug_on == 0) {
3109 		printf(" [Ok..]\n");
3110 	} else {
3111 		printf(" [Fail]\n");
3112 		ret = EXIT_ERR_CODE;
3113 	}
3114 
3115 #ifndef WITH_ANDROID
3116 	if (nr_unref_nid && !c.ro) {
3117 		char ans[255] = {0};
3118 
3119 		printf("\nDo you want to restore lost files into ./lost_found/? [Y/N] ");
3120 		ret = scanf("%s", ans);
3121 		ASSERT(ret >= 0);
3122 		if (!strcasecmp(ans, "y")) {
3123 			for (i = 0; i < fsck->nr_nat_entries; i++) {
3124 				if (f2fs_test_bit(i, fsck->nat_area_bitmap))
3125 					dump_node(sbi, i, 1);
3126 			}
3127 		}
3128 	}
3129 #endif
3130 
3131 	/* fix global metadata */
3132 	if (force || (c.fix_on && f2fs_dev_is_writable())) {
3133 		struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
3134 
3135 		if (force || c.bug_on || c.bug_nat_bits) {
3136 			/* flush nats to write_nit_bits below */
3137 			flush_journal_entries(sbi);
3138 			fix_hard_links(sbi);
3139 			fix_nat_entries(sbi);
3140 			rewrite_sit_area_bitmap(sbi);
3141 			fix_wp_sit_alignment(sbi);
3142 			fix_curseg_info(sbi);
3143 			fix_checksum(sbi);
3144 			fix_checkpoints(sbi);
3145 		} else if (is_set_ckpt_flags(cp, CP_FSCK_FLAG) ||
3146 			is_set_ckpt_flags(cp, CP_QUOTA_NEED_FSCK_FLAG)) {
3147 			write_checkpoints(sbi);
3148 		}
3149 	}
3150 	return ret;
3151 }
3152 
3153 void fsck_free(struct f2fs_sb_info *sbi)
3154 {
3155 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
3156 
3157 	if (fsck->qctx)
3158 		quota_release_context(&fsck->qctx);
3159 
3160 	if (fsck->main_area_bitmap)
3161 		free(fsck->main_area_bitmap);
3162 
3163 	if (fsck->nat_area_bitmap)
3164 		free(fsck->nat_area_bitmap);
3165 
3166 	if (fsck->sit_area_bitmap)
3167 		free(fsck->sit_area_bitmap);
3168 
3169 	if (fsck->entries)
3170 		free(fsck->entries);
3171 
3172 	if (tree_mark)
3173 		free(tree_mark);
3174 }
3175