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