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