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