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