4 * Copyright (c) 2013 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
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.
17 uint32_t tree_mark_size = 256;
19 int f2fs_set_main_bitmap(struct f2fs_sb_info *sbi, u32 blk, int type)
21 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
25 se = get_seg_entry(sbi, GET_SEGNO(sbi, blk));
26 if (se->type >= NO_CHECK_TYPE)
28 else if (IS_DATASEG(se->type) != IS_DATASEG(type))
31 /* just check data and node types */
33 DBG(1, "Wrong segment type [0x%x] %x -> %x",
34 GET_SEGNO(sbi, blk), se->type, type);
37 return f2fs_set_bit(BLKOFF_FROM_MAIN(sbi, blk), fsck->main_area_bitmap);
40 static inline int f2fs_test_main_bitmap(struct f2fs_sb_info *sbi, u32 blk)
42 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
44 return f2fs_test_bit(BLKOFF_FROM_MAIN(sbi, blk),
45 fsck->main_area_bitmap);
48 static inline int f2fs_clear_main_bitmap(struct f2fs_sb_info *sbi, u32 blk)
50 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
52 return f2fs_clear_bit(BLKOFF_FROM_MAIN(sbi, blk),
53 fsck->main_area_bitmap);
56 static inline int f2fs_test_sit_bitmap(struct f2fs_sb_info *sbi, u32 blk)
58 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
60 return f2fs_test_bit(BLKOFF_FROM_MAIN(sbi, blk), fsck->sit_area_bitmap);
63 int f2fs_set_sit_bitmap(struct f2fs_sb_info *sbi, u32 blk)
65 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
67 return f2fs_set_bit(BLKOFF_FROM_MAIN(sbi, blk), fsck->sit_area_bitmap);
70 static int add_into_hard_link_list(struct f2fs_sb_info *sbi,
71 u32 nid, u32 link_cnt)
73 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
74 struct hard_link_node *node = NULL, *tmp = NULL, *prev = NULL;
76 node = calloc(sizeof(struct hard_link_node), 1);
80 node->links = link_cnt;
81 node->actual_links = 1;
84 if (fsck->hard_link_list_head == NULL) {
85 fsck->hard_link_list_head = node;
89 tmp = fsck->hard_link_list_head;
91 /* Find insertion position */
92 while (tmp && (nid < tmp->nid)) {
93 ASSERT(tmp->nid != nid);
98 if (tmp == fsck->hard_link_list_head) {
100 fsck->hard_link_list_head = node;
107 DBG(2, "ino[0x%x] has hard links [0x%x]\n", nid, link_cnt);
111 static int find_and_dec_hard_link_list(struct f2fs_sb_info *sbi, u32 nid)
113 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
114 struct hard_link_node *node = NULL, *prev = NULL;
116 if (fsck->hard_link_list_head == NULL)
119 node = fsck->hard_link_list_head;
121 while (node && (nid < node->nid)) {
126 if (node == NULL || (nid != node->nid))
129 /* Decrease link count */
130 node->links = node->links - 1;
131 node->actual_links++;
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;
138 prev->next = node->next;
144 static int is_valid_ssa_node_blk(struct f2fs_sb_info *sbi, u32 nid,
147 struct f2fs_summary_block *sum_blk;
148 struct f2fs_summary *sum_entry;
149 struct seg_entry * se;
151 int need_fix = 0, ret = 0;
154 segno = GET_SEGNO(sbi, blk_addr);
155 offset = OFFSET_IN_SEG(sbi, blk_addr);
157 sum_blk = get_sum_block(sbi, segno, &type);
159 if (type != SEG_TYPE_NODE && type != SEG_TYPE_CUR_NODE) {
160 /* can't fix current summary, then drop the block */
161 if (!c.fix_on || type < 0) {
162 ASSERT_MSG("Summary footer is not for node segment");
168 se = get_seg_entry(sbi, segno);
169 if(IS_NODESEG(se->type)) {
170 FIX_MSG("Summary footer indicates a node segment: 0x%x", segno);
171 sum_blk->footer.entry_type = SUM_TYPE_NODE;
178 sum_entry = &(sum_blk->entries[offset]);
180 if (le32_to_cpu(sum_entry->nid) != nid) {
181 if (!c.fix_on || type < 0) {
182 DBG(0, "nid [0x%x]\n", nid);
183 DBG(0, "target blk_addr [0x%x]\n", blk_addr);
184 DBG(0, "summary blk_addr [0x%x]\n",
186 GET_SEGNO(sbi, blk_addr)));
187 DBG(0, "seg no / offset [0x%x / 0x%x]\n",
188 GET_SEGNO(sbi, blk_addr),
189 OFFSET_IN_SEG(sbi, blk_addr));
190 DBG(0, "summary_entry.nid [0x%x]\n",
191 le32_to_cpu(sum_entry->nid));
192 DBG(0, "--> node block's nid [0x%x]\n", nid);
193 ASSERT_MSG("Invalid node seg summary\n");
196 FIX_MSG("Set node summary 0x%x -> [0x%x] [0x%x]",
197 segno, nid, blk_addr);
198 sum_entry->nid = cpu_to_le32(nid);
202 if (need_fix && f2fs_dev_is_writable()) {
206 ssa_blk = GET_SUM_BLKADDR(sbi, segno);
207 ret2 = dev_write_block(sum_blk, ssa_blk);
211 if (type == SEG_TYPE_NODE || type == SEG_TYPE_DATA ||
212 type == SEG_TYPE_MAX)
217 static int is_valid_summary(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
220 u16 ofs_in_node = le16_to_cpu(sum->ofs_in_node);
221 u32 nid = le32_to_cpu(sum->nid);
222 struct f2fs_node *node_blk = NULL;
223 __le32 target_blk_addr;
227 node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
228 ASSERT(node_blk != NULL);
230 if (!IS_VALID_NID(sbi, nid))
233 get_node_info(sbi, nid, &ni);
235 if (!IS_VALID_BLK_ADDR(sbi, ni.blk_addr))
238 /* read node_block */
239 ret = dev_read_block(node_blk, ni.blk_addr);
242 if (le32_to_cpu(node_blk->footer.nid) != nid)
245 /* check its block address */
246 if (node_blk->footer.nid == node_blk->footer.ino) {
247 int ofs = get_extra_isize(node_blk);
249 target_blk_addr = node_blk->i.i_addr[ofs + ofs_in_node];
251 target_blk_addr = node_blk->dn.addr[ofs_in_node];
254 if (blk_addr == le32_to_cpu(target_blk_addr))
261 static int is_valid_ssa_data_blk(struct f2fs_sb_info *sbi, u32 blk_addr,
262 u32 parent_nid, u16 idx_in_node, u8 version)
264 struct f2fs_summary_block *sum_blk;
265 struct f2fs_summary *sum_entry;
266 struct seg_entry * se;
268 int need_fix = 0, ret = 0;
271 segno = GET_SEGNO(sbi, blk_addr);
272 offset = OFFSET_IN_SEG(sbi, blk_addr);
274 sum_blk = get_sum_block(sbi, segno, &type);
276 if (type != SEG_TYPE_DATA && type != SEG_TYPE_CUR_DATA) {
277 /* can't fix current summary, then drop the block */
278 if (!c.fix_on || type < 0) {
279 ASSERT_MSG("Summary footer is not for data segment");
285 se = get_seg_entry(sbi, segno);
286 if (IS_DATASEG(se->type)) {
287 FIX_MSG("Summary footer indicates a data segment: 0x%x", segno);
288 sum_blk->footer.entry_type = SUM_TYPE_DATA;
295 sum_entry = &(sum_blk->entries[offset]);
297 if (le32_to_cpu(sum_entry->nid) != parent_nid ||
298 sum_entry->version != version ||
299 le16_to_cpu(sum_entry->ofs_in_node) != idx_in_node) {
300 if (!c.fix_on || type < 0) {
301 DBG(0, "summary_entry.nid [0x%x]\n",
302 le32_to_cpu(sum_entry->nid));
303 DBG(0, "summary_entry.version [0x%x]\n",
305 DBG(0, "summary_entry.ofs_in_node [0x%x]\n",
306 le16_to_cpu(sum_entry->ofs_in_node));
307 DBG(0, "parent nid [0x%x]\n",
309 DBG(0, "version from nat [0x%x]\n", version);
310 DBG(0, "idx in parent node [0x%x]\n",
313 DBG(0, "Target data block addr [0x%x]\n", blk_addr);
314 ASSERT_MSG("Invalid data seg summary\n");
316 } else if (is_valid_summary(sbi, sum_entry, blk_addr)) {
317 /* delete wrong index */
320 FIX_MSG("Set data summary 0x%x -> [0x%x] [0x%x] [0x%x]",
321 segno, parent_nid, version, idx_in_node);
322 sum_entry->nid = cpu_to_le32(parent_nid);
323 sum_entry->version = version;
324 sum_entry->ofs_in_node = cpu_to_le16(idx_in_node);
328 if (need_fix && f2fs_dev_is_writable()) {
332 ssa_blk = GET_SUM_BLKADDR(sbi, segno);
333 ret2 = dev_write_block(sum_blk, ssa_blk);
337 if (type == SEG_TYPE_NODE || type == SEG_TYPE_DATA ||
338 type == SEG_TYPE_MAX)
343 static int __check_inode_mode(u32 nid, enum FILE_TYPE ftype, u16 mode)
345 if (ftype >= F2FS_FT_MAX)
347 /* f2fs_iget will return -EIO if mode is not valid file type */
348 if (!S_ISLNK(mode) && !S_ISREG(mode) && !S_ISDIR(mode) &&
349 !S_ISCHR(mode) && !S_ISBLK(mode) && !S_ISFIFO(mode) &&
351 ASSERT_MSG("inode [0x%x] unknown file type i_mode [0x%x]",
356 if (S_ISLNK(mode) && ftype != F2FS_FT_SYMLINK)
358 if (S_ISREG(mode) && ftype != F2FS_FT_REG_FILE)
360 if (S_ISDIR(mode) && ftype != F2FS_FT_DIR)
362 if (S_ISCHR(mode) && ftype != F2FS_FT_CHRDEV)
364 if (S_ISBLK(mode) && ftype != F2FS_FT_BLKDEV)
366 if (S_ISFIFO(mode) && ftype != F2FS_FT_FIFO)
368 if (S_ISSOCK(mode) && ftype != F2FS_FT_SOCK)
372 ASSERT_MSG("inode [0x%x] mismatch i_mode [0x%x vs. 0x%x]",
377 static int sanity_check_nid(struct f2fs_sb_info *sbi, u32 nid,
378 struct f2fs_node *node_blk,
379 enum FILE_TYPE ftype, enum NODE_TYPE ntype,
380 struct node_info *ni)
382 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
385 if (!IS_VALID_NID(sbi, nid)) {
386 ASSERT_MSG("nid is not valid. [0x%x]", nid);
390 get_node_info(sbi, nid, ni);
392 ASSERT_MSG("nid[0x%x] ino is 0", nid);
396 if (ni->blk_addr == NEW_ADDR) {
397 ASSERT_MSG("nid is NEW_ADDR. [0x%x]", nid);
401 if (!IS_VALID_BLK_ADDR(sbi, ni->blk_addr)) {
402 ASSERT_MSG("blkaddress is not valid. [0x%x]", ni->blk_addr);
406 ret = dev_read_block(node_blk, ni->blk_addr);
409 if (ntype == TYPE_INODE &&
410 node_blk->footer.nid != node_blk->footer.ino) {
411 ASSERT_MSG("nid[0x%x] footer.nid[0x%x] footer.ino[0x%x]",
412 nid, le32_to_cpu(node_blk->footer.nid),
413 le32_to_cpu(node_blk->footer.ino));
416 if (ni->ino != le32_to_cpu(node_blk->footer.ino)) {
417 ASSERT_MSG("nid[0x%x] nat_entry->ino[0x%x] footer.ino[0x%x]",
418 nid, ni->ino, le32_to_cpu(node_blk->footer.ino));
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));
429 if (le32_to_cpu(node_blk->footer.nid) != nid) {
430 ASSERT_MSG("nid[0x%x] blk_addr[0x%x] footer.nid[0x%x]",
432 le32_to_cpu(node_blk->footer.nid));
436 if (ntype == TYPE_XATTR) {
437 u32 flag = le32_to_cpu(node_blk->footer.flag);
439 if ((flag >> OFFSET_BIT_SHIFT) != XATTR_NODE_OFFSET) {
440 ASSERT_MSG("xnid[0x%x] has wrong ofs:[0x%x]",
446 if ((ntype == TYPE_INODE && ftype == F2FS_FT_DIR) ||
447 (ntype == TYPE_XATTR && ftype == F2FS_FT_XATTR)) {
448 /* not included '.' & '..' */
449 if (f2fs_test_main_bitmap(sbi, ni->blk_addr) != 0) {
450 ASSERT_MSG("Duplicated node blk. nid[0x%x][0x%x]\n",
456 /* this if only from fix_hard_links */
457 if (ftype == F2FS_FT_MAX)
460 if (ntype == TYPE_INODE &&
461 __check_inode_mode(nid, ftype, le16_to_cpu(node_blk->i.i_mode)))
464 /* workaround to fix later */
465 if (ftype != F2FS_FT_ORPHAN ||
466 f2fs_test_bit(nid, fsck->nat_area_bitmap) != 0) {
467 f2fs_clear_bit(nid, fsck->nat_area_bitmap);
468 /* avoid reusing nid when reconnecting files */
469 f2fs_set_bit(nid, NM_I(sbi)->nid_bitmap);
471 ASSERT_MSG("orphan or xattr nid is duplicated [0x%x]\n",
474 if (is_valid_ssa_node_blk(sbi, nid, ni->blk_addr)) {
475 ASSERT_MSG("summary node block is not valid. [0x%x]", nid);
479 if (f2fs_test_sit_bitmap(sbi, ni->blk_addr) == 0)
480 ASSERT_MSG("SIT bitmap is 0x0. blk_addr[0x%x]",
483 if (f2fs_test_main_bitmap(sbi, ni->blk_addr) == 0) {
484 fsck->chk.valid_blk_cnt++;
485 fsck->chk.valid_node_cnt++;
490 int fsck_sanity_check_nid(struct f2fs_sb_info *sbi, u32 nid,
491 struct f2fs_node *node_blk,
492 enum FILE_TYPE ftype, enum NODE_TYPE ntype,
493 struct node_info *ni)
495 return sanity_check_nid(sbi, nid, node_blk, ftype, ntype, ni);
498 static int fsck_chk_xattr_blk(struct f2fs_sb_info *sbi, u32 ino,
499 u32 x_nid, u32 *blk_cnt)
501 struct f2fs_node *node_blk = NULL;
508 node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
509 ASSERT(node_blk != NULL);
512 if (sanity_check_nid(sbi, x_nid, node_blk,
513 F2FS_FT_XATTR, TYPE_XATTR, &ni)) {
518 *blk_cnt = *blk_cnt + 1;
519 f2fs_set_main_bitmap(sbi, ni.blk_addr, CURSEG_COLD_NODE);
520 DBG(2, "ino[0x%x] x_nid[0x%x]\n", ino, x_nid);
526 int fsck_chk_node_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
527 u32 nid, enum FILE_TYPE ftype, enum NODE_TYPE ntype,
528 u32 *blk_cnt, struct child_info *child)
531 struct f2fs_node *node_blk = NULL;
533 node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
534 ASSERT(node_blk != NULL);
536 if (sanity_check_nid(sbi, nid, node_blk, ftype, ntype, &ni))
539 if (ntype == TYPE_INODE) {
540 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
542 fsck_chk_inode_blk(sbi, nid, ftype, node_blk, blk_cnt, &ni, child);
543 quota_add_inode_usage(fsck->qctx, nid, &node_blk->i);
546 case TYPE_DIRECT_NODE:
547 f2fs_set_main_bitmap(sbi, ni.blk_addr,
549 fsck_chk_dnode_blk(sbi, inode, nid, ftype, node_blk,
550 blk_cnt, child, &ni);
552 case TYPE_INDIRECT_NODE:
553 f2fs_set_main_bitmap(sbi, ni.blk_addr,
555 fsck_chk_idnode_blk(sbi, inode, ftype, node_blk,
558 case TYPE_DOUBLE_INDIRECT_NODE:
559 f2fs_set_main_bitmap(sbi, ni.blk_addr,
561 fsck_chk_didnode_blk(sbi, inode, ftype, node_blk,
575 static inline void get_extent_info(struct extent_info *ext,
576 struct f2fs_extent *i_ext)
578 ext->fofs = le32_to_cpu(i_ext->fofs);
579 ext->blk = le32_to_cpu(i_ext->blk_addr);
580 ext->len = le32_to_cpu(i_ext->len);
583 static void check_extent_info(struct child_info *child,
584 block_t blkaddr, int last)
586 struct extent_info *ei = &child->ei;
587 u32 pgofs = child->pgofs;
593 if (child->state & FSCK_UNMATCHED_EXTENT)
596 if ((child->state & FSCK_INLINE_INODE) && ei->len)
600 /* hole exist in the back of extent */
601 if (child->last_blk != ei->blk + ei->len - 1)
602 child->state |= FSCK_UNMATCHED_EXTENT;
606 if (blkaddr == NULL_ADDR || blkaddr == NEW_ADDR)
609 if (pgofs >= ei->fofs && pgofs < ei->fofs + ei->len) {
610 /* unmatched blkaddr */
611 if (is_hole || (blkaddr != pgofs - ei->fofs + ei->blk))
614 if (!child->last_blk) {
615 /* hole exists in the front of extent */
616 if (pgofs != ei->fofs)
618 } else if (child->last_blk + 1 != blkaddr) {
619 /* hole exists in the middle of extent */
622 child->last_blk = blkaddr;
629 if (blkaddr < ei->blk || blkaddr >= ei->blk + ei->len)
631 /* unmatched file offset */
633 child->state |= FSCK_UNMATCHED_EXTENT;
636 void fsck_reada_node_block(struct f2fs_sb_info *sbi, u32 nid)
640 if (nid != 0 && IS_VALID_NID(sbi, nid)) {
641 get_node_info(sbi, nid, &ni);
642 if (IS_VALID_BLK_ADDR(sbi, ni.blk_addr))
643 dev_reada_block(ni.blk_addr);
647 void fsck_reada_all_direct_node_blocks(struct f2fs_sb_info *sbi,
648 struct f2fs_node *node_blk)
652 for (i = 0; i < NIDS_PER_BLOCK; i++) {
653 u32 nid = le32_to_cpu(node_blk->in.nid[i]);
655 fsck_reada_node_block(sbi, nid);
659 /* start with valid nid and blkaddr */
660 void fsck_chk_inode_blk(struct f2fs_sb_info *sbi, u32 nid,
661 enum FILE_TYPE ftype, struct f2fs_node *node_blk,
662 u32 *blk_cnt, struct node_info *ni, struct child_info *child_d)
664 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
665 struct child_info child;
666 enum NODE_TYPE ntype;
667 u32 i_links = le32_to_cpu(node_blk->i.i_links);
668 u64 i_size = le64_to_cpu(node_blk->i.i_size);
669 u64 i_blocks = le64_to_cpu(node_blk->i.i_blocks);
670 nid_t i_xattr_nid = le32_to_cpu(node_blk->i.i_xattr_nid);
674 unsigned int idx = 0;
675 unsigned short i_gc_failures;
679 memset(&child, 0, sizeof(child));
682 child.pp_ino = le32_to_cpu(node_blk->i.i_pino);
683 child.dir_level = node_blk->i.i_dir_level;
685 if (f2fs_test_main_bitmap(sbi, ni->blk_addr) == 0)
686 fsck->chk.valid_inode_cnt++;
688 if (ftype == F2FS_FT_DIR) {
689 f2fs_set_main_bitmap(sbi, ni->blk_addr, CURSEG_HOT_NODE);
691 if (f2fs_test_main_bitmap(sbi, ni->blk_addr) == 0) {
692 f2fs_set_main_bitmap(sbi, ni->blk_addr,
694 if (i_links > 1 && ftype != F2FS_FT_ORPHAN &&
695 !is_qf_ino(F2FS_RAW_SUPER(sbi), nid)) {
696 /* First time. Create new hard link node */
697 add_into_hard_link_list(sbi, nid, i_links);
698 fsck->chk.multi_hard_link_files++;
701 DBG(3, "[0x%x] has hard links [0x%x]\n", nid, i_links);
702 if (find_and_dec_hard_link_list(sbi, nid)) {
703 ASSERT_MSG("[0x%x] needs more i_links=0x%x",
706 node_blk->i.i_links =
707 cpu_to_le32(i_links + 1);
709 FIX_MSG("File: 0x%x "
710 "i_links= 0x%x -> 0x%x",
711 nid, i_links, i_links + 1);
713 goto skip_blkcnt_fix;
715 /* No need to go deep into the node */
720 /* readahead xattr node block */
721 fsck_reada_node_block(sbi, i_xattr_nid);
723 if (fsck_chk_xattr_blk(sbi, nid, i_xattr_nid, blk_cnt)) {
725 node_blk->i.i_xattr_nid = 0;
727 FIX_MSG("Remove xattr block: 0x%x, x_nid = 0x%x",
732 if (ftype == F2FS_FT_CHRDEV || ftype == F2FS_FT_BLKDEV ||
733 ftype == F2FS_FT_FIFO || ftype == F2FS_FT_SOCK)
736 /* init extent info */
737 get_extent_info(&child.ei, &node_blk->i.i_ext);
740 if (f2fs_has_extra_isize(&node_blk->i)) {
741 if (c.feature & cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR)) {
743 le16_to_cpu(node_blk->i.i_extra_isize);
744 if (isize > 4 * DEF_ADDRS_PER_INODE) {
745 ASSERT_MSG("[0x%x] wrong i_extra_isize=0x%x",
748 FIX_MSG("ino[0x%x] recover i_extra_isize "
752 node_blk->i.i_extra_isize =
753 cpu_to_le16(calc_extra_isize());
758 ASSERT_MSG("[0x%x] wrong extra_attr flag", nid);
760 FIX_MSG("ino[0x%x] remove F2FS_EXTRA_ATTR "
761 "flag in i_inline:%u",
762 nid, node_blk->i.i_inline);
763 /* we don't support tuning F2FS_FEATURE_EXTRA_ATTR now */
764 node_blk->i.i_inline &= ~F2FS_EXTRA_ATTR;
770 cpu_to_le32(F2FS_FEATURE_FLEXIBLE_INLINE_XATTR)) &&
771 (node_blk->i.i_inline & F2FS_INLINE_XATTR)) {
772 unsigned int inline_size =
773 le16_to_cpu(node_blk->i.i_inline_xattr_size);
776 inline_size > MAX_INLINE_XATTR_SIZE) {
777 ASSERT_MSG("[0x%x] wrong inline_xattr_size:%u",
780 FIX_MSG("ino[0x%x] recover inline xattr size "
783 DEFAULT_INLINE_XATTR_ADDRS);
784 node_blk->i.i_inline_xattr_size =
785 cpu_to_le16(DEFAULT_INLINE_XATTR_ADDRS);
791 ofs = get_extra_isize(node_blk);
793 if ((node_blk->i.i_inline & F2FS_INLINE_DATA)) {
794 unsigned int inline_size = MAX_INLINE_DATA(node_blk);
795 block_t blkaddr = le32_to_cpu(node_blk->i.i_addr[ofs]);
798 ASSERT_MSG("[0x%x] wrong inline reserve blkaddr:%u",
801 FIX_MSG("inline_data has wrong 0'th block = %x",
803 node_blk->i.i_addr[ofs] = 0;
804 node_blk->i.i_blocks = cpu_to_le64(*blk_cnt);
808 if (i_size > inline_size) {
809 ASSERT_MSG("[0x%x] wrong inline size:%lu",
810 nid, (unsigned long)i_size);
812 node_blk->i.i_size = cpu_to_le64(inline_size);
813 FIX_MSG("inline_data has wrong i_size %lu",
814 (unsigned long)i_size);
818 if (!(node_blk->i.i_inline & F2FS_DATA_EXIST)) {
819 char buf[MAX_INLINE_DATA(node_blk)];
820 memset(buf, 0, MAX_INLINE_DATA(node_blk));
822 if (memcmp(buf, inline_data_addr(node_blk),
823 MAX_INLINE_DATA(node_blk))) {
824 ASSERT_MSG("[0x%x] junk inline data", nid);
826 FIX_MSG("inline_data has DATA_EXIST");
827 node_blk->i.i_inline |= F2FS_DATA_EXIST;
832 DBG(3, "ino[0x%x] has inline data!\n", nid);
833 child.state |= FSCK_INLINE_INODE;
837 if ((node_blk->i.i_inline & F2FS_INLINE_DENTRY)) {
838 block_t blkaddr = le32_to_cpu(node_blk->i.i_addr[ofs]);
840 DBG(3, "ino[0x%x] has inline dentry!\n", nid);
842 ASSERT_MSG("[0x%x] wrong inline reserve blkaddr:%u",
845 FIX_MSG("inline_dentry has wrong 0'th block = %x",
847 node_blk->i.i_addr[ofs] = 0;
848 node_blk->i.i_blocks = cpu_to_le64(*blk_cnt);
853 ret = fsck_chk_inline_dentries(sbi, node_blk, &child);
858 child.state |= FSCK_INLINE_INODE;
862 /* check data blocks in inode */
863 for (idx = 0; idx < ADDRS_PER_INODE(&node_blk->i);
864 idx++, child.pgofs++) {
865 block_t blkaddr = le32_to_cpu(node_blk->i.i_addr[ofs + idx]);
867 /* check extent info */
868 check_extent_info(&child, blkaddr, 0);
870 if (blkaddr == COMPRESS_ADDR) {
871 if (node_blk->i.i_compr_blocks) {
872 fsck->chk.valid_blk_cnt++;
873 *blk_cnt = *blk_cnt + 1;
879 ret = fsck_chk_data_blk(sbi,
880 IS_CASEFOLDED(&node_blk->i),
882 &child, (i_blocks == *blk_cnt),
883 ftype, nid, idx, ni->version,
884 file_is_encrypt(&node_blk->i));
886 *blk_cnt = *blk_cnt + 1;
887 } else if (c.fix_on) {
888 node_blk->i.i_addr[ofs + idx] = 0;
890 FIX_MSG("[0x%x] i_addr[%d] = 0",
896 /* readahead node blocks */
897 for (idx = 0; idx < 5; idx++) {
898 u32 nid = le32_to_cpu(node_blk->i.i_nid[idx]);
899 fsck_reada_node_block(sbi, nid);
902 /* check node blocks in inode */
903 for (idx = 0; idx < 5; idx++) {
904 nid_t i_nid = le32_to_cpu(node_blk->i.i_nid[idx]);
906 if (idx == 0 || idx == 1)
907 ntype = TYPE_DIRECT_NODE;
908 else if (idx == 2 || idx == 3)
909 ntype = TYPE_INDIRECT_NODE;
911 ntype = TYPE_DOUBLE_INDIRECT_NODE;
918 ret = fsck_chk_node_blk(sbi, &node_blk->i, i_nid,
919 ftype, ntype, blk_cnt, &child);
921 *blk_cnt = *blk_cnt + 1;
922 } else if (ret == -EINVAL) {
924 node_blk->i.i_nid[idx] = 0;
926 FIX_MSG("[0x%x] i_nid[%d] = 0", nid, idx);
929 if (ntype == TYPE_DIRECT_NODE)
930 child.pgofs += ADDRS_PER_BLOCK(&node_blk->i);
931 else if (ntype == TYPE_INDIRECT_NODE)
932 child.pgofs += ADDRS_PER_BLOCK(&node_blk->i) *
935 child.pgofs += ADDRS_PER_BLOCK(&node_blk->i) *
936 NIDS_PER_BLOCK * NIDS_PER_BLOCK;
942 /* check uncovered range in the back of extent */
943 check_extent_info(&child, 0, 1);
945 if (child.state & FSCK_UNMATCHED_EXTENT) {
946 ASSERT_MSG("ino: 0x%x has wrong ext: [pgofs:%u, blk:%u, len:%u]",
947 nid, child.ei.fofs, child.ei.blk, child.ei.len);
952 if (i_blocks != *blk_cnt) {
953 ASSERT_MSG("ino: 0x%x has i_blocks: %08"PRIx64", "
955 nid, i_blocks, *blk_cnt);
957 node_blk->i.i_blocks = cpu_to_le64(*blk_cnt);
959 FIX_MSG("[0x%x] i_blocks=0x%08"PRIx64" -> 0x%x",
960 nid, i_blocks, *blk_cnt);
964 en = malloc(F2FS_PRINT_NAMELEN);
967 namelen = le32_to_cpu(node_blk->i.i_namelen);
968 if (namelen > F2FS_NAME_LEN) {
969 if (child_d && child_d->i_namelen <= F2FS_NAME_LEN) {
970 ASSERT_MSG("ino: 0x%x has i_namelen: 0x%x, "
971 "but has %d characters for name",
972 nid, namelen, child_d->i_namelen);
974 FIX_MSG("[0x%x] i_namelen=0x%x -> 0x%x", nid, namelen,
976 node_blk->i.i_namelen = cpu_to_le32(child_d->i_namelen);
979 namelen = child_d->i_namelen;
981 namelen = F2FS_NAME_LEN;
983 pretty_print_filename(node_blk->i.i_name, namelen, en,
984 file_enc_name(&node_blk->i));
985 if (ftype == F2FS_FT_ORPHAN)
986 DBG(1, "Orphan Inode: 0x%x [%s] i_blocks: %u\n\n",
987 le32_to_cpu(node_blk->footer.ino),
990 if (is_qf_ino(F2FS_RAW_SUPER(sbi), nid))
991 DBG(1, "Quota Inode: 0x%x [%s] i_blocks: %u\n\n",
992 le32_to_cpu(node_blk->footer.ino),
995 if (ftype == F2FS_FT_DIR) {
996 DBG(1, "Directory Inode: 0x%x [%s] depth: %d has %d files\n\n",
997 le32_to_cpu(node_blk->footer.ino), en,
998 le32_to_cpu(node_blk->i.i_current_depth),
1001 if (i_links != child.links) {
1002 ASSERT_MSG("ino: 0x%x i_links: %u, real links: %u",
1003 nid, i_links, child.links);
1005 node_blk->i.i_links = cpu_to_le32(child.links);
1007 FIX_MSG("Dir: 0x%x i_links= 0x%x -> 0x%x",
1008 nid, i_links, child.links);
1011 if (child.dots < 2 &&
1012 !(node_blk->i.i_inline & F2FS_INLINE_DOTS)) {
1013 ASSERT_MSG("ino: 0x%x dots: %u",
1016 node_blk->i.i_inline |= F2FS_INLINE_DOTS;
1018 FIX_MSG("Dir: 0x%x set inline_dots", nid);
1023 i_gc_failures = le16_to_cpu(node_blk->i.i_gc_failures);
1026 * old kernel initialized i_gc_failures as 0x01, in preen mode 2,
1027 * let's skip repairing.
1029 if (ftype == F2FS_FT_REG_FILE && i_gc_failures &&
1030 (c.preen_mode != PREEN_MODE_2 || i_gc_failures != 0x01)) {
1032 DBG(1, "Regular Inode: 0x%x [%s] depth: %d\n\n",
1033 le32_to_cpu(node_blk->footer.ino), en,
1037 node_blk->i.i_gc_failures = cpu_to_le16(0);
1039 FIX_MSG("Regular: 0x%x reset i_gc_failures from 0x%x to 0x00",
1040 nid, i_gc_failures);
1046 if (ftype == F2FS_FT_SYMLINK && i_size == 0 &&
1047 i_blocks == (i_xattr_nid ? 3 : 2)) {
1048 ASSERT_MSG("ino: 0x%x i_blocks: %lu with zero i_size\n",
1049 nid, (unsigned long)i_blocks);
1051 node_blk->i.i_size = cpu_to_le64(F2FS_BLKSIZE);
1053 FIX_MSG("Symlink: recover 0x%x with i_size=%lu",
1054 nid, (unsigned long)F2FS_BLKSIZE);
1058 if (ftype == F2FS_FT_ORPHAN && i_links) {
1059 ASSERT_MSG("ino: 0x%x is orphan inode, but has i_links: %u",
1062 node_blk->i.i_links = 0;
1064 FIX_MSG("ino: 0x%x orphan_inode, i_links= 0x%x -> 0",
1069 /* drop extent information to avoid potential wrong access */
1070 if (need_fix && f2fs_dev_is_writable())
1071 node_blk->i.i_ext.len = 0;
1073 if ((c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM)) &&
1074 f2fs_has_extra_isize(&node_blk->i)) {
1075 __u32 provided, calculated;
1077 provided = le32_to_cpu(node_blk->i.i_inode_checksum);
1078 calculated = f2fs_inode_chksum(node_blk);
1080 if (provided != calculated) {
1081 ASSERT_MSG("ino: 0x%x chksum:0x%x, but calculated one is: 0x%x",
1082 nid, provided, calculated);
1084 node_blk->i.i_inode_checksum =
1085 cpu_to_le32(calculated);
1087 FIX_MSG("ino: 0x%x recover, i_inode_checksum= 0x%x -> 0x%x",
1088 nid, provided, calculated);
1093 if (need_fix && f2fs_dev_is_writable()) {
1094 ret = dev_write_block(node_blk, ni->blk_addr);
1099 int fsck_chk_dnode_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
1100 u32 nid, enum FILE_TYPE ftype, struct f2fs_node *node_blk,
1101 u32 *blk_cnt, struct child_info *child, struct node_info *ni)
1106 child->pp_ino = le32_to_cpu(inode->i_pino);
1108 for (idx = 0; idx < ADDRS_PER_BLOCK(inode); idx++, child->pgofs++) {
1109 block_t blkaddr = le32_to_cpu(node_blk->dn.addr[idx]);
1111 check_extent_info(child, blkaddr, 0);
1115 if (blkaddr == COMPRESS_ADDR) {
1116 if (inode->i_compr_blocks) {
1117 F2FS_FSCK(sbi)->chk.valid_blk_cnt++;
1118 *blk_cnt = *blk_cnt + 1;
1122 ret = fsck_chk_data_blk(sbi, IS_CASEFOLDED(inode),
1124 le64_to_cpu(inode->i_blocks) == *blk_cnt, ftype,
1125 nid, idx, ni->version,
1126 file_is_encrypt(inode));
1128 *blk_cnt = *blk_cnt + 1;
1129 } else if (c.fix_on) {
1130 node_blk->dn.addr[idx] = 0;
1132 FIX_MSG("[0x%x] dn.addr[%d] = 0", nid, idx);
1135 if (need_fix && f2fs_dev_is_writable()) {
1136 ret = dev_write_block(node_blk, ni->blk_addr);
1142 int fsck_chk_idnode_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
1143 enum FILE_TYPE ftype, struct f2fs_node *node_blk, u32 *blk_cnt,
1144 struct child_info *child)
1146 int need_fix = 0, ret;
1149 fsck_reada_all_direct_node_blocks(sbi, node_blk);
1151 for (i = 0; i < NIDS_PER_BLOCK; i++) {
1152 if (le32_to_cpu(node_blk->in.nid[i]) == 0x0)
1154 ret = fsck_chk_node_blk(sbi, inode,
1155 le32_to_cpu(node_blk->in.nid[i]),
1156 ftype, TYPE_DIRECT_NODE, blk_cnt, child);
1158 *blk_cnt = *blk_cnt + 1;
1159 else if (ret == -EINVAL) {
1161 printf("should delete in.nid[i] = 0;\n");
1163 node_blk->in.nid[i] = 0;
1165 FIX_MSG("Set indirect node 0x%x -> 0", i);
1168 child->pgofs += ADDRS_PER_BLOCK(&node_blk->i);
1172 if (need_fix && f2fs_dev_is_writable()) {
1173 struct node_info ni;
1174 nid_t nid = le32_to_cpu(node_blk->footer.nid);
1176 get_node_info(sbi, nid, &ni);
1177 ret = dev_write_block(node_blk, ni.blk_addr);
1184 int fsck_chk_didnode_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
1185 enum FILE_TYPE ftype, struct f2fs_node *node_blk, u32 *blk_cnt,
1186 struct child_info *child)
1189 int need_fix = 0, ret = 0;
1191 fsck_reada_all_direct_node_blocks(sbi, node_blk);
1193 for (i = 0; i < NIDS_PER_BLOCK; i++) {
1194 if (le32_to_cpu(node_blk->in.nid[i]) == 0x0)
1196 ret = fsck_chk_node_blk(sbi, inode,
1197 le32_to_cpu(node_blk->in.nid[i]),
1198 ftype, TYPE_INDIRECT_NODE, blk_cnt, child);
1200 *blk_cnt = *blk_cnt + 1;
1201 else if (ret == -EINVAL) {
1203 printf("should delete in.nid[i] = 0;\n");
1205 node_blk->in.nid[i] = 0;
1207 FIX_MSG("Set double indirect node 0x%x -> 0", i);
1210 child->pgofs += ADDRS_PER_BLOCK(&node_blk->i) *
1215 if (need_fix && f2fs_dev_is_writable()) {
1216 struct node_info ni;
1217 nid_t nid = le32_to_cpu(node_blk->footer.nid);
1219 get_node_info(sbi, nid, &ni);
1220 ret = dev_write_block(node_blk, ni.blk_addr);
1227 static const char *lookup_table =
1228 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
1233 * Encodes the input string using characters from the set [A-Za-z0-9+,].
1234 * The encoded string is roughly 4/3 times the size of the input string.
1236 static int base64_encode(const u8 *src, int len, char *dst)
1238 int i, bits = 0, ac = 0;
1241 for (i = 0; i < len; i++) {
1242 ac += src[i] << bits;
1245 *cp++ = lookup_table[ac & 0x3f];
1248 } while (bits >= 6);
1251 *cp++ = lookup_table[ac & 0x3f];
1255 void pretty_print_filename(const u8 *raw_name, u32 len,
1256 char out[F2FS_PRINT_NAMELEN], int enc_name)
1258 len = min(len, (u32)F2FS_NAME_LEN);
1261 len = base64_encode(raw_name, len, out);
1263 memcpy(out, raw_name, len);
1267 static void print_dentry(__u32 depth, __u8 *name,
1268 u8 *bitmap, struct f2fs_dir_entry *dentry,
1269 int max, int idx, int last_blk, int enc_name)
1276 char new[F2FS_PRINT_NAMELEN];
1281 name_len = le16_to_cpu(dentry[idx].name_len);
1282 next_idx = idx + (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN;
1284 bit_offset = find_next_bit_le(bitmap, max, next_idx);
1285 if (bit_offset >= max && last_blk)
1288 if (tree_mark_size <= depth) {
1289 tree_mark_size *= 2;
1290 ASSERT(tree_mark_size != 0);
1291 tree_mark = realloc(tree_mark, tree_mark_size);
1292 ASSERT(tree_mark != NULL);
1296 tree_mark[depth] = '`';
1298 tree_mark[depth] = '|';
1300 if (tree_mark[depth - 1] == '`')
1301 tree_mark[depth - 1] = ' ';
1303 for (i = 1; i < depth; i++)
1304 printf("%c ", tree_mark[i]);
1306 pretty_print_filename(name, name_len, new, enc_name);
1308 printf("%c-- %s <ino = 0x%x>, <encrypted (%d)>\n",
1309 last_de ? '`' : '|',
1310 new, le32_to_cpu(dentry[idx].ino),
1314 static int f2fs_check_hash_code(int encoding, int casefolded,
1315 struct f2fs_dir_entry *dentry,
1316 const unsigned char *name, u32 len, int enc_name)
1318 /* Casefolded Encrypted names require a key to compute siphash */
1319 if (enc_name && casefolded)
1322 f2fs_hash_t hash_code = f2fs_dentry_hash(encoding, casefolded, name, len);
1323 /* fix hash_code made by old buggy code */
1324 if (dentry->hash_code != hash_code) {
1325 char new[F2FS_PRINT_NAMELEN];
1327 pretty_print_filename(name, len, new, enc_name);
1328 FIX_MSG("Mismatch hash_code for \"%s\" [%x:%x]",
1329 new, le32_to_cpu(dentry->hash_code),
1331 dentry->hash_code = cpu_to_le32(hash_code);
1338 static int __get_current_level(int dir_level, u32 pgofs)
1340 unsigned int bidx = 0;
1343 for (i = 0; i < MAX_DIR_HASH_DEPTH; i++) {
1344 bidx += dir_buckets(i, dir_level) * bucket_blocks(i);
1351 static int f2fs_check_dirent_position(int encoding, int casefolded,
1352 u8 *name, u16 name_len, u32 pgofs,
1353 u8 dir_level, u32 pino)
1355 f2fs_hash_t namehash = f2fs_dentry_hash(encoding, casefolded, name, name_len);
1356 unsigned int nbucket, nblock;
1357 unsigned int bidx, end_block;
1360 level = __get_current_level(dir_level, pgofs);
1362 nbucket = dir_buckets(level, dir_level);
1363 nblock = bucket_blocks(level);
1365 bidx = dir_block_index(level, dir_level,
1366 le32_to_cpu(namehash) % nbucket);
1367 end_block = bidx + nblock;
1369 if (pgofs >= bidx && pgofs < end_block)
1372 ASSERT_MSG("Wrong position of dirent pino:%u, name:%s, level:%d, "
1373 "dir_level:%d, pgofs:%u, correct range:[%u, %u]\n",
1374 pino, name, level, dir_level, pgofs, bidx, end_block - 1);
1378 static int __chk_dots_dentries(struct f2fs_sb_info *sbi,
1380 struct f2fs_dir_entry *dentry,
1381 struct child_info *child,
1383 __u8 (*filename)[F2FS_SLOT_LEN],
1388 if ((name[0] == '.' && len == 1)) {
1389 if (le32_to_cpu(dentry->ino) != child->p_ino) {
1390 ASSERT_MSG("Bad inode number[0x%x] for '.', parent_ino is [0x%x]\n",
1391 le32_to_cpu(dentry->ino), child->p_ino);
1392 dentry->ino = cpu_to_le32(child->p_ino);
1397 if (name[0] == '.' && name[1] == '.' && len == 2) {
1398 if (child->p_ino == F2FS_ROOT_INO(sbi)) {
1399 if (le32_to_cpu(dentry->ino) != F2FS_ROOT_INO(sbi)) {
1400 ASSERT_MSG("Bad inode number[0x%x] for '..'\n",
1401 le32_to_cpu(dentry->ino));
1402 dentry->ino = cpu_to_le32(F2FS_ROOT_INO(sbi));
1405 } else if (le32_to_cpu(dentry->ino) != child->pp_ino) {
1406 ASSERT_MSG("Bad inode number[0x%x] for '..', parent parent ino is [0x%x]\n",
1407 le32_to_cpu(dentry->ino), child->pp_ino);
1408 dentry->ino = cpu_to_le32(child->pp_ino);
1413 if (f2fs_check_hash_code(get_encoding(sbi), casefolded, dentry, name, len, enc_name))
1416 if (name[len] != '\0') {
1417 ASSERT_MSG("'.' is not NULL terminated\n");
1419 memcpy(*filename, name, len);
1425 static void nullify_dentry(struct f2fs_dir_entry *dentry, int offs,
1426 __u8 (*filename)[F2FS_SLOT_LEN], u8 **bitmap)
1428 memset(dentry, 0, sizeof(struct f2fs_dir_entry));
1429 test_and_clear_bit_le(offs, *bitmap);
1430 memset(*filename, 0, F2FS_SLOT_LEN);
1433 static int __chk_dentries(struct f2fs_sb_info *sbi, int casefolded,
1434 struct child_info *child,
1435 u8 *bitmap, struct f2fs_dir_entry *dentry,
1436 __u8 (*filenames)[F2FS_SLOT_LEN],
1437 int max, int last_blk, int enc_name)
1439 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1440 enum FILE_TYPE ftype;
1444 char en[F2FS_PRINT_NAMELEN];
1450 /* readahead inode blocks */
1451 for (i = 0; i < max; i++) {
1454 if (test_bit_le(i, bitmap) == 0)
1457 ino = le32_to_cpu(dentry[i].ino);
1459 if (IS_VALID_NID(sbi, ino)) {
1460 struct node_info ni;
1462 get_node_info(sbi, ino, &ni);
1463 if (IS_VALID_BLK_ADDR(sbi, ni.blk_addr)) {
1464 dev_reada_block(ni.blk_addr);
1465 name_len = le16_to_cpu(dentry[i].name_len);
1467 i += (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN - 1;
1472 for (i = 0; i < max;) {
1473 if (test_bit_le(i, bitmap) == 0) {
1477 if (!IS_VALID_NID(sbi, le32_to_cpu(dentry[i].ino))) {
1478 ASSERT_MSG("Bad dentry 0x%x with invalid NID/ino 0x%x",
1479 i, le32_to_cpu(dentry[i].ino));
1481 FIX_MSG("Clear bad dentry 0x%x with bad ino 0x%x",
1482 i, le32_to_cpu(dentry[i].ino));
1483 test_and_clear_bit_le(i, bitmap);
1490 ftype = dentry[i].file_type;
1491 if ((ftype <= F2FS_FT_UNKNOWN || ftype > F2FS_FT_LAST_FILE_TYPE)) {
1492 ASSERT_MSG("Bad dentry 0x%x with unexpected ftype 0x%x",
1493 le32_to_cpu(dentry[i].ino), ftype);
1495 FIX_MSG("Clear bad dentry 0x%x with bad ftype 0x%x",
1497 test_and_clear_bit_le(i, bitmap);
1504 name_len = le16_to_cpu(dentry[i].name_len);
1506 if (name_len == 0 || name_len > F2FS_NAME_LEN) {
1507 ASSERT_MSG("Bad dentry 0x%x with invalid name_len", i);
1509 FIX_MSG("Clear bad dentry 0x%x", i);
1510 test_and_clear_bit_le(i, bitmap);
1516 name = calloc(name_len + 1, 1);
1519 memcpy(name, filenames[i], name_len);
1520 slots = (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN;
1522 /* Becareful. 'dentry.file_type' is not imode. */
1523 if (ftype == F2FS_FT_DIR) {
1524 if ((name[0] == '.' && name_len == 1) ||
1525 (name[0] == '.' && name[1] == '.' &&
1527 ret = __chk_dots_dentries(sbi, casefolded, &dentry[i],
1528 child, name, name_len, &filenames[i],
1538 if (child->dots > 2) {
1539 ASSERT_MSG("More than one '.' or '..', should delete the extra one\n");
1540 nullify_dentry(&dentry[i], i,
1541 &filenames[i], &bitmap);
1552 if (f2fs_check_hash_code(get_encoding(sbi), casefolded, dentry + i, name, name_len, enc_name))
1555 if (max == NR_DENTRY_IN_BLOCK) {
1556 ret = f2fs_check_dirent_position(get_encoding(sbi), casefolded,
1557 name, name_len, child->pgofs,
1558 child->dir_level, child->p_ino);
1561 FIX_MSG("Clear bad dentry 0x%x", i);
1562 test_and_clear_bit_le(i, bitmap);
1571 pretty_print_filename(name, name_len, en, enc_name);
1572 DBG(1, "[%3u]-[0x%x] name[%s] len[0x%x] ino[0x%x] type[0x%x]\n",
1573 fsck->dentry_depth, i, en, name_len,
1574 le32_to_cpu(dentry[i].ino),
1575 dentry[i].file_type);
1577 print_dentry(fsck->dentry_depth, name, bitmap,
1578 dentry, max, i, last_blk, enc_name);
1581 child->i_namelen = name_len;
1582 ret = fsck_chk_node_blk(sbi,
1583 NULL, le32_to_cpu(dentry[i].ino),
1584 ftype, TYPE_INODE, &blk_cnt, child);
1586 if (ret && c.fix_on) {
1589 for (j = 0; j < slots; j++)
1590 test_and_clear_bit_le(i + j, bitmap);
1591 FIX_MSG("Unlink [0x%x] - %s len[0x%x], type[0x%x]",
1592 le32_to_cpu(dentry[i].ino),
1594 dentry[i].file_type);
1596 } else if (ret == 0) {
1597 if (ftype == F2FS_FT_DIR)
1606 return fixed ? -1 : dentries;
1609 int fsck_chk_inline_dentries(struct f2fs_sb_info *sbi,
1610 struct f2fs_node *node_blk, struct child_info *child)
1612 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1613 struct f2fs_dentry_ptr d;
1614 void *inline_dentry;
1617 inline_dentry = inline_data_addr(node_blk);
1618 ASSERT(inline_dentry != NULL);
1620 make_dentry_ptr(&d, node_blk, inline_dentry, 2);
1622 fsck->dentry_depth++;
1623 dentries = __chk_dentries(sbi, IS_CASEFOLDED(&node_blk->i), child,
1624 d.bitmap, d.dentry, d.filename, d.max, 1,
1625 file_is_encrypt(&node_blk->i));// pass through
1627 DBG(1, "[%3d] Inline Dentry Block Fixed hash_codes\n\n",
1628 fsck->dentry_depth);
1630 DBG(1, "[%3d] Inline Dentry Block Done : "
1631 "dentries:%d in %d slots (len:%d)\n\n",
1632 fsck->dentry_depth, dentries,
1633 d.max, F2FS_NAME_LEN);
1635 fsck->dentry_depth--;
1639 int fsck_chk_dentry_blk(struct f2fs_sb_info *sbi, int casefolded, u32 blk_addr,
1640 struct child_info *child, int last_blk, int enc_name)
1642 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1643 struct f2fs_dentry_block *de_blk;
1646 de_blk = (struct f2fs_dentry_block *)calloc(BLOCK_SZ, 1);
1647 ASSERT(de_blk != NULL);
1649 ret = dev_read_block(de_blk, blk_addr);
1652 fsck->dentry_depth++;
1653 dentries = __chk_dentries(sbi, casefolded, child,
1654 de_blk->dentry_bitmap,
1655 de_blk->dentry, de_blk->filename,
1656 NR_DENTRY_IN_BLOCK, last_blk, enc_name);
1658 if (dentries < 0 && f2fs_dev_is_writable()) {
1659 ret = dev_write_block(de_blk, blk_addr);
1661 DBG(1, "[%3d] Dentry Block [0x%x] Fixed hash_codes\n\n",
1662 fsck->dentry_depth, blk_addr);
1664 DBG(1, "[%3d] Dentry Block [0x%x] Done : "
1665 "dentries:%d in %d slots (len:%d)\n\n",
1666 fsck->dentry_depth, blk_addr, dentries,
1667 NR_DENTRY_IN_BLOCK, F2FS_NAME_LEN);
1669 fsck->dentry_depth--;
1674 int fsck_chk_data_blk(struct f2fs_sb_info *sbi, int casefolded,
1675 u32 blk_addr, struct child_info *child, int last_blk,
1676 enum FILE_TYPE ftype, u32 parent_nid, u16 idx_in_node, u8 ver,
1679 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1681 /* Is it reserved block? */
1682 if (blk_addr == NEW_ADDR) {
1683 fsck->chk.valid_blk_cnt++;
1687 if (!IS_VALID_BLK_ADDR(sbi, blk_addr)) {
1688 ASSERT_MSG("blkaddress is not valid. [0x%x]", blk_addr);
1692 if (is_valid_ssa_data_blk(sbi, blk_addr, parent_nid,
1693 idx_in_node, ver)) {
1694 ASSERT_MSG("summary data block is not valid. [0x%x]",
1699 if (f2fs_test_sit_bitmap(sbi, blk_addr) == 0)
1700 ASSERT_MSG("SIT bitmap is 0x0. blk_addr[0x%x]", blk_addr);
1702 if (f2fs_test_main_bitmap(sbi, blk_addr) != 0)
1703 ASSERT_MSG("Duplicated data [0x%x]. pnid[0x%x] idx[0x%x]",
1704 blk_addr, parent_nid, idx_in_node);
1706 fsck->chk.valid_blk_cnt++;
1708 if (ftype == F2FS_FT_DIR) {
1709 f2fs_set_main_bitmap(sbi, blk_addr, CURSEG_HOT_DATA);
1710 return fsck_chk_dentry_blk(sbi, casefolded, blk_addr, child,
1711 last_blk, enc_name);
1713 f2fs_set_main_bitmap(sbi, blk_addr, CURSEG_WARM_DATA);
1718 int fsck_chk_orphan_node(struct f2fs_sb_info *sbi)
1721 block_t start_blk, orphan_blkaddr, i, j;
1722 struct f2fs_orphan_block *orphan_blk, *new_blk;
1723 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
1726 if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG))
1729 start_blk = __start_cp_addr(sbi) + 1 + get_sb(cp_payload);
1730 orphan_blkaddr = __start_sum_addr(sbi) - 1 - get_sb(cp_payload);
1732 f2fs_ra_meta_pages(sbi, start_blk, orphan_blkaddr, META_CP);
1734 orphan_blk = calloc(BLOCK_SZ, 1);
1737 new_blk = calloc(BLOCK_SZ, 1);
1740 for (i = 0; i < orphan_blkaddr; i++) {
1741 int ret = dev_read_block(orphan_blk, start_blk + i);
1742 u32 new_entry_count = 0;
1745 entry_count = le32_to_cpu(orphan_blk->entry_count);
1747 for (j = 0; j < entry_count; j++) {
1748 nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
1749 DBG(1, "[%3d] ino [0x%x]\n", i, ino);
1750 struct node_info ni;
1753 if (c.preen_mode == PREEN_MODE_1 && !c.fix_on) {
1754 get_node_info(sbi, ino, &ni);
1755 if (!IS_VALID_NID(sbi, ino) ||
1756 !IS_VALID_BLK_ADDR(sbi, ni.blk_addr))
1762 ret = fsck_chk_node_blk(sbi, NULL, ino,
1763 F2FS_FT_ORPHAN, TYPE_INODE, &blk_cnt,
1766 new_blk->ino[new_entry_count++] =
1768 else if (ret && c.fix_on)
1769 FIX_MSG("[0x%x] remove from orphan list", ino);
1771 ASSERT_MSG("[0x%x] wrong orphan inode", ino);
1773 if (f2fs_dev_is_writable() && c.fix_on &&
1774 entry_count != new_entry_count) {
1775 new_blk->entry_count = cpu_to_le32(new_entry_count);
1776 ret = dev_write_block(new_blk, start_blk + i);
1779 memset(orphan_blk, 0, BLOCK_SZ);
1780 memset(new_blk, 0, BLOCK_SZ);
1788 int fsck_chk_quota_node(struct f2fs_sb_info *sbi)
1790 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
1791 enum quota_type qtype;
1795 for (qtype = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
1796 if (sb->qf_ino[qtype] == 0)
1798 nid_t ino = QUOTA_INO(sb, qtype);
1799 struct node_info ni;
1801 DBG(1, "qtype [%d] ino [0x%x]\n", qtype, ino);
1804 if (c.preen_mode == PREEN_MODE_1 && !c.fix_on) {
1805 get_node_info(sbi, ino, &ni);
1806 if (!IS_VALID_NID(sbi, ino) ||
1807 !IS_VALID_BLK_ADDR(sbi, ni.blk_addr))
1811 ret = fsck_chk_node_blk(sbi, NULL, ino,
1812 F2FS_FT_REG_FILE, TYPE_INODE, &blk_cnt, NULL);
1814 ASSERT_MSG("wrong quota inode, qtype [%d] ino [0x%x]",
1820 int fsck_chk_quota_files(struct f2fs_sb_info *sbi)
1822 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1823 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
1824 enum quota_type qtype;
1829 /* Return if quota feature is disabled */
1833 for (qtype = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
1834 ino = sb->qf_ino[qtype];
1838 DBG(1, "Checking Quota file ([%3d] ino [0x%x])\n", qtype, ino);
1840 ret = quota_compare_and_update(sbi, qtype, &needs_writeout,
1842 if (ret == 0 && needs_writeout == 0) {
1847 /* Something is wrong */
1849 DBG(0, "Fixing Quota file ([%3d] ino [0x%x])\n",
1851 f2fs_filesize_update(sbi, ino, 0);
1852 ret = quota_write_inode(sbi, qtype);
1857 ASSERT_MSG("Unable to write quota file");
1860 ASSERT_MSG("Quota file is missing or invalid"
1861 " quota file content found.");
1867 int fsck_chk_meta(struct f2fs_sb_info *sbi)
1869 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1870 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
1871 struct seg_entry *se;
1872 unsigned int sit_valid_segs = 0, sit_node_blks = 0;
1875 /* 1. check sit usage with CP: curseg is lost? */
1876 for (i = 0; i < TOTAL_SEGS(sbi); i++) {
1877 se = get_seg_entry(sbi, i);
1878 if (se->valid_blocks != 0)
1880 else if (IS_CUR_SEGNO(sbi, i)) {
1881 /* curseg has not been written back to device */
1882 MSG(1, "\tInfo: curseg %u is counted in valid segs\n", i);
1885 if (IS_NODESEG(se->type))
1886 sit_node_blks += se->valid_blocks;
1888 if (fsck->chk.sit_free_segs + sit_valid_segs != TOTAL_SEGS(sbi)) {
1889 ASSERT_MSG("SIT usage does not match: sit_free_segs %u, "
1890 "sit_valid_segs %u, total_segs %u",
1891 fsck->chk.sit_free_segs, sit_valid_segs,
1896 /* 2. check node count */
1897 if (fsck->chk.valid_nat_entry_cnt != sit_node_blks) {
1898 ASSERT_MSG("node count does not match: valid_nat_entry_cnt %u,"
1899 " sit_node_blks %u",
1900 fsck->chk.valid_nat_entry_cnt, sit_node_blks);
1904 /* 3. check SIT with CP */
1905 if (fsck->chk.sit_free_segs != le32_to_cpu(cp->free_segment_count)) {
1906 ASSERT_MSG("free segs does not match: sit_free_segs %u, "
1907 "free_segment_count %u",
1908 fsck->chk.sit_free_segs,
1909 le32_to_cpu(cp->free_segment_count));
1913 /* 4. check NAT with CP */
1914 if (fsck->chk.valid_nat_entry_cnt !=
1915 le32_to_cpu(cp->valid_node_count)) {
1916 ASSERT_MSG("valid node does not match: valid_nat_entry_cnt %u,"
1917 " valid_node_count %u",
1918 fsck->chk.valid_nat_entry_cnt,
1919 le32_to_cpu(cp->valid_node_count));
1923 /* 4. check orphan inode simply */
1924 if (fsck_chk_orphan_node(sbi))
1927 /* 5. check nat entry -- must be done before quota check */
1928 for (i = 0; i < fsck->nr_nat_entries; i++) {
1929 u32 blk = le32_to_cpu(fsck->entries[i].block_addr);
1930 nid_t ino = le32_to_cpu(fsck->entries[i].ino);
1934 * skip entry whose ino is 0, otherwise, we will
1935 * get a negative number by BLKOFF_FROM_MAIN(sbi, blk)
1939 if (!IS_VALID_BLK_ADDR(sbi, blk)) {
1940 MSG(0, "\tError: nat entry[ino %u block_addr 0x%x]"
1946 if (!f2fs_test_sit_bitmap(sbi, blk)) {
1947 MSG(0, "\tError: nat entry[ino %u block_addr 0x%x]"
1948 " not find it in sit_area_bitmap\n",
1953 if (!IS_VALID_NID(sbi, ino)) {
1954 MSG(0, "\tError: nat_entry->ino %u exceeds the range"
1955 " of nat entries %u\n",
1956 ino, fsck->nr_nat_entries);
1960 if (!f2fs_test_bit(ino, fsck->nat_area_bitmap)) {
1961 MSG(0, "\tError: nat_entry->ino %u is not set in"
1962 " nat_area_bitmap\n", ino);
1967 /* 6. check quota inode simply */
1968 if (fsck_chk_quota_node(sbi))
1971 if (fsck->nat_valid_inode_cnt != le32_to_cpu(cp->valid_inode_count)) {
1972 ASSERT_MSG("valid inode does not match: nat_valid_inode_cnt %u,"
1973 " valid_inode_count %u",
1974 fsck->nat_valid_inode_cnt,
1975 le32_to_cpu(cp->valid_inode_count));
1982 void fsck_chk_checkpoint(struct f2fs_sb_info *sbi)
1984 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
1986 if (get_cp(ckpt_flags) & CP_LARGE_NAT_BITMAP_FLAG) {
1987 if (get_cp(checksum_offset) != CP_MIN_CHKSUM_OFFSET) {
1988 ASSERT_MSG("Deprecated layout of large_nat_bitmap, "
1989 "chksum_offset:%u", get_cp(checksum_offset));
1995 void fsck_init(struct f2fs_sb_info *sbi)
1997 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1998 struct f2fs_sm_info *sm_i = SM_I(sbi);
2001 * We build three bitmap for main/sit/nat so that may check consistency
2003 * 1. main_area_bitmap will be used to check whether all blocks of main
2004 * area is used or not.
2005 * 2. nat_area_bitmap has bitmap information of used nid in NAT.
2006 * 3. sit_area_bitmap has bitmap information of used main block.
2007 * At Last sequence, we compare main_area_bitmap with sit_area_bitmap.
2009 fsck->nr_main_blks = sm_i->main_segments << sbi->log_blocks_per_seg;
2010 fsck->main_area_bitmap_sz = (fsck->nr_main_blks + 7) / 8;
2011 fsck->main_area_bitmap = calloc(fsck->main_area_bitmap_sz, 1);
2012 ASSERT(fsck->main_area_bitmap != NULL);
2014 build_nat_area_bitmap(sbi);
2016 build_sit_area_bitmap(sbi);
2018 ASSERT(tree_mark_size != 0);
2019 tree_mark = calloc(tree_mark_size, 1);
2020 ASSERT(tree_mark != NULL);
2023 static void fix_hard_links(struct f2fs_sb_info *sbi)
2025 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2026 struct hard_link_node *tmp, *node;
2027 struct f2fs_node *node_blk = NULL;
2028 struct node_info ni;
2031 if (fsck->hard_link_list_head == NULL)
2034 node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
2035 ASSERT(node_blk != NULL);
2037 node = fsck->hard_link_list_head;
2040 if (sanity_check_nid(sbi, node->nid, node_blk,
2041 F2FS_FT_MAX, TYPE_INODE, &ni))
2042 FIX_MSG("Failed to fix, rerun fsck.f2fs");
2044 node_blk->i.i_links = cpu_to_le32(node->actual_links);
2046 FIX_MSG("File: 0x%x i_links= 0x%x -> 0x%x",
2047 node->nid, node->links, node->actual_links);
2049 ret = dev_write_block(node_blk, ni.blk_addr);
2058 static void fix_nat_entries(struct f2fs_sb_info *sbi)
2060 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2063 for (i = 0; i < fsck->nr_nat_entries; i++)
2064 if (f2fs_test_bit(i, fsck->nat_area_bitmap) != 0)
2065 nullify_nat_entry(sbi, i);
2068 static void flush_curseg_sit_entries(struct f2fs_sb_info *sbi)
2070 struct sit_info *sit_i = SIT_I(sbi);
2071 struct f2fs_sit_block *sit_blk;
2074 sit_blk = calloc(BLOCK_SZ, 1);
2076 /* update curseg sit entries, since we may change
2077 * a segment type in move_curseg_info
2079 for (i = 0; i < NO_CHECK_TYPE; i++) {
2080 struct curseg_info *curseg = CURSEG_I(sbi, i);
2081 struct f2fs_sit_entry *sit;
2082 struct seg_entry *se;
2084 se = get_seg_entry(sbi, curseg->segno);
2085 get_current_sit_page(sbi, curseg->segno, sit_blk);
2086 sit = &sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, curseg->segno)];
2087 sit->vblocks = cpu_to_le16((se->type << SIT_VBLOCKS_SHIFT) |
2089 rewrite_current_sit_page(sbi, curseg->segno, sit_blk);
2095 static void fix_checksum(struct f2fs_sb_info *sbi)
2097 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
2098 struct f2fs_nm_info *nm_i = NM_I(sbi);
2099 struct sit_info *sit_i = SIT_I(sbi);
2100 void *bitmap_offset;
2105 bitmap_offset = cp->sit_nat_version_bitmap + sizeof(__le32);
2107 memcpy(bitmap_offset, nm_i->nat_bitmap, nm_i->bitmap_size);
2108 memcpy(bitmap_offset + nm_i->bitmap_size,
2109 sit_i->sit_bitmap, sit_i->bitmap_size);
2112 static void fix_checkpoint(struct f2fs_sb_info *sbi)
2114 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2115 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
2116 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
2117 unsigned long long cp_blk_no;
2118 u32 flags = c.alloc_failed ? CP_FSCK_FLAG: CP_UMOUNT_FLAG;
2119 block_t orphan_blks = 0;
2125 /* should call from fsck */
2126 ASSERT(c.func == FSCK);
2128 if (is_set_ckpt_flags(cp, CP_ORPHAN_PRESENT_FLAG)) {
2129 orphan_blks = __start_sum_addr(sbi) - 1;
2130 flags |= CP_ORPHAN_PRESENT_FLAG;
2132 if (is_set_ckpt_flags(cp, CP_TRIMMED_FLAG))
2133 flags |= CP_TRIMMED_FLAG;
2134 if (is_set_ckpt_flags(cp, CP_DISABLED_FLAG))
2135 flags |= CP_DISABLED_FLAG;
2136 if (is_set_ckpt_flags(cp, CP_LARGE_NAT_BITMAP_FLAG)) {
2137 flags |= CP_LARGE_NAT_BITMAP_FLAG;
2138 set_cp(checksum_offset, CP_MIN_CHKSUM_OFFSET);
2140 set_cp(checksum_offset, CP_CHKSUM_OFFSET);
2143 if (flags & CP_UMOUNT_FLAG)
2148 set_cp(cp_pack_total_block_count, cp_blocks +
2149 orphan_blks + get_sb(cp_payload));
2151 flags = update_nat_bits_flags(sb, cp, flags);
2152 flags |= CP_NOCRC_RECOVERY_FLAG;
2153 set_cp(ckpt_flags, flags);
2155 set_cp(free_segment_count, get_free_segments(sbi));
2156 set_cp(valid_block_count, fsck->chk.valid_blk_cnt);
2157 set_cp(valid_node_count, fsck->chk.valid_node_cnt);
2158 set_cp(valid_inode_count, fsck->chk.valid_inode_cnt);
2160 crc = f2fs_checkpoint_chksum(cp);
2161 *((__le32 *)((unsigned char *)cp + get_cp(checksum_offset))) =
2164 cp_blk_no = get_sb(cp_blkaddr);
2165 if (sbi->cur_cp == 2)
2166 cp_blk_no += 1 << get_sb(log_blocks_per_seg);
2168 ret = dev_write_block(cp, cp_blk_no++);
2171 for (i = 0; i < get_sb(cp_payload); i++) {
2172 ret = dev_write_block(((unsigned char *)cp) +
2173 (i + 1) * F2FS_BLKSIZE, cp_blk_no++);
2177 cp_blk_no += orphan_blks;
2179 for (i = 0; i < NO_CHECK_TYPE; i++) {
2180 struct curseg_info *curseg = CURSEG_I(sbi, i);
2182 if (!(flags & CP_UMOUNT_FLAG) && IS_NODESEG(i))
2185 ret = dev_write_block(curseg->sum_blk, cp_blk_no++);
2189 /* Write nat bits */
2190 if (flags & CP_NAT_BITS_FLAG)
2191 write_nat_bits(sbi, sb, cp, sbi->cur_cp);
2193 ret = f2fs_fsync_device();
2196 ret = dev_write_block(cp, cp_blk_no++);
2199 ret = f2fs_fsync_device();
2203 static void fix_checkpoints(struct f2fs_sb_info *sbi)
2205 /* copy valid checkpoint to its mirror position */
2206 duplicate_checkpoint(sbi);
2208 /* repair checkpoint at CP #0 position */
2210 fix_checkpoint(sbi);
2213 #ifdef HAVE_LINUX_BLKZONED_H
2216 * Refer valid block map and return offset of the last valid block in the zone.
2217 * Obtain valid block map from SIT and fsync data.
2218 * If there is no valid block in the zone, return -1.
2220 static int last_vblk_off_in_zone(struct f2fs_sb_info *sbi,
2221 unsigned int zone_segno)
2224 unsigned int segs_per_zone = sbi->segs_per_sec * sbi->secs_per_zone;
2225 struct seg_entry *se;
2227 for (s = segs_per_zone - 1; s >= 0; s--) {
2228 se = get_seg_entry(sbi, zone_segno + s);
2231 * Refer not cur_valid_map but ckpt_valid_map which reflects
2234 ASSERT(se->ckpt_valid_map);
2235 for (b = sbi->blocks_per_seg - 1; b >= 0; b--)
2236 if (f2fs_test_bit(b, (const char*)se->ckpt_valid_map))
2237 return b + (s << sbi->log_blocks_per_seg);
2243 static int check_curseg_write_pointer(struct f2fs_sb_info *sbi, int type)
2245 struct curseg_info *curseg = CURSEG_I(sbi, type);
2246 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2247 struct blk_zone blkz;
2248 block_t cs_block, wp_block, zone_last_vblock;
2249 u_int64_t cs_sector, wp_sector;
2251 unsigned int zone_segno;
2252 int log_sectors_per_block = sbi->log_blocksize - SECTOR_SHIFT;
2254 /* get the device the curseg points to */
2255 cs_block = START_BLOCK(sbi, curseg->segno) + curseg->next_blkoff;
2256 for (i = 0; i < MAX_DEVICES; i++) {
2257 if (!c.devices[i].path)
2259 if (c.devices[i].start_blkaddr <= cs_block &&
2260 cs_block <= c.devices[i].end_blkaddr)
2264 if (i >= MAX_DEVICES)
2267 /* get write pointer position of the zone the curseg points to */
2268 cs_sector = (cs_block - c.devices[i].start_blkaddr)
2269 << log_sectors_per_block;
2270 ret = f2fs_report_zone(i, cs_sector, &blkz);
2274 if (blk_zone_type(&blkz) != BLK_ZONE_TYPE_SEQWRITE_REQ)
2277 /* check consistency between the curseg and the write pointer */
2278 wp_block = c.devices[i].start_blkaddr +
2279 (blk_zone_wp_sector(&blkz) >> log_sectors_per_block);
2280 wp_sector = blk_zone_wp_sector(&blkz);
2282 if (cs_sector == wp_sector)
2285 if (cs_sector > wp_sector) {
2286 MSG(0, "Inconsistent write pointer with curseg %d: "
2287 "curseg %d[0x%x,0x%x] > wp[0x%x,0x%x]\n",
2288 type, type, curseg->segno, curseg->next_blkoff,
2289 GET_SEGNO(sbi, wp_block), OFFSET_IN_SEG(sbi, wp_block));
2290 fsck->chk.wp_inconsistent_zones++;
2294 MSG(0, "Write pointer goes advance from curseg %d: "
2295 "curseg %d[0x%x,0x%x] wp[0x%x,0x%x]\n",
2296 type, type, curseg->segno, curseg->next_blkoff,
2297 GET_SEGNO(sbi, wp_block), OFFSET_IN_SEG(sbi, wp_block));
2299 zone_segno = GET_SEG_FROM_SEC(sbi,
2300 GET_SEC_FROM_SEG(sbi, curseg->segno));
2301 zone_last_vblock = START_BLOCK(sbi, zone_segno) +
2302 last_vblk_off_in_zone(sbi, zone_segno);
2305 * If valid blocks exist between the curseg position and the write
2306 * pointer, they are fsync data. This is not an error to fix. Leave it
2307 * for kernel to recover later.
2308 * If valid blocks exist between the curseg's zone start and the curseg
2309 * position, or if there is no valid block in the curseg's zone, fix
2310 * the inconsistency between the curseg and the writ pointer.
2311 * Of Note is that if there is no valid block in the curseg's zone,
2312 * last_vblk_off_in_zone() returns -1 and zone_last_vblock is always
2313 * smaller than cs_block.
2315 if (cs_block <= zone_last_vblock && zone_last_vblock < wp_block) {
2316 MSG(0, "Curseg has fsync data: curseg %d[0x%x,0x%x] "
2317 "last valid block in zone[0x%x,0x%x]\n",
2318 type, curseg->segno, curseg->next_blkoff,
2319 GET_SEGNO(sbi, zone_last_vblock),
2320 OFFSET_IN_SEG(sbi, zone_last_vblock));
2324 fsck->chk.wp_inconsistent_zones++;
2330 static int check_curseg_write_pointer(struct f2fs_sb_info *UNUSED(sbi),
2338 int check_curseg_offset(struct f2fs_sb_info *sbi, int type)
2340 struct curseg_info *curseg = CURSEG_I(sbi, type);
2341 struct seg_entry *se;
2344 if ((curseg->next_blkoff >> 3) >= SIT_VBLOCK_MAP_SIZE) {
2345 ASSERT_MSG("Next block offset:%u is invalid, type:%d",
2346 curseg->next_blkoff, type);
2349 se = get_seg_entry(sbi, curseg->segno);
2350 if (f2fs_test_bit(curseg->next_blkoff,
2351 (const char *)se->cur_valid_map)) {
2352 ASSERT_MSG("Next block offset is not free, type:%d", type);
2355 if (curseg->alloc_type == SSR)
2358 nblocks = sbi->blocks_per_seg;
2359 for (j = curseg->next_blkoff + 1; j < nblocks; j++) {
2360 if (f2fs_test_bit(j, (const char *)se->cur_valid_map)) {
2361 ASSERT_MSG("For LFS curseg, space after .next_blkoff "
2362 "should be unused, type:%d", type);
2367 if (c.zoned_model == F2FS_ZONED_HM)
2368 return check_curseg_write_pointer(sbi, type);
2373 int check_curseg_offsets(struct f2fs_sb_info *sbi)
2377 for (i = 0; i < NO_CHECK_TYPE; i++) {
2378 ret = check_curseg_offset(sbi, i);
2385 static void fix_curseg_info(struct f2fs_sb_info *sbi)
2387 int i, need_update = 0;
2389 for (i = 0; i < NO_CHECK_TYPE; i++) {
2390 if (check_curseg_offset(sbi, i)) {
2391 update_curseg_info(sbi, i);
2397 write_curseg_info(sbi);
2398 flush_curseg_sit_entries(sbi);
2402 int check_sit_types(struct f2fs_sb_info *sbi)
2407 for (i = 0; i < TOTAL_SEGS(sbi); i++) {
2408 struct seg_entry *se;
2410 se = get_seg_entry(sbi, i);
2411 if (se->orig_type != se->type) {
2412 if (se->orig_type == CURSEG_COLD_DATA &&
2413 se->type <= CURSEG_COLD_DATA) {
2414 se->type = se->orig_type;
2416 FIX_MSG("Wrong segment type [0x%x] %x -> %x",
2417 i, se->orig_type, se->type);
2425 static struct f2fs_node *fsck_get_lpf(struct f2fs_sb_info *sbi)
2427 struct f2fs_node *node;
2428 struct node_info ni;
2432 /* read root inode first */
2433 node = calloc(F2FS_BLKSIZE, 1);
2435 get_node_info(sbi, F2FS_ROOT_INO(sbi), &ni);
2436 err = dev_read_block(node, ni.blk_addr);
2439 /* lookup lost+found in root directory */
2440 lpf_ino = f2fs_lookup(sbi, node, (u8 *)LPF, strlen(LPF));
2441 if (lpf_ino) { /* found */
2442 get_node_info(sbi, lpf_ino, &ni);
2443 err = dev_read_block(node, ni.blk_addr);
2445 DBG(1, "Found lost+found 0x%x at blkaddr [0x%x]\n",
2446 lpf_ino, ni.blk_addr);
2447 if (!S_ISDIR(le16_to_cpu(node->i.i_mode))) {
2448 ASSERT_MSG("lost+found is not directory [0%o]\n",
2449 le16_to_cpu(node->i.i_mode));
2450 /* FIXME: give up? */
2453 } else { /* not found, create it */
2456 memset(&de, 0, sizeof(de));
2457 de.name = (u8 *) LPF;
2458 de.len = strlen(LPF);
2460 de.pino = F2FS_ROOT_INO(sbi),
2461 de.file_type = F2FS_FT_DIR,
2464 de.mtime = time(NULL);
2466 err = f2fs_mkdir(sbi, &de);
2468 ASSERT_MSG("Failed create lost+found");
2472 get_node_info(sbi, de.ino, &ni);
2473 err = dev_read_block(node, ni.blk_addr);
2475 DBG(1, "Create lost+found 0x%x at blkaddr [0x%x]\n",
2476 de.ino, ni.blk_addr);
2479 c.lpf_ino = le32_to_cpu(node->footer.ino);
2486 static int fsck_do_reconnect_file(struct f2fs_sb_info *sbi,
2487 struct f2fs_node *lpf,
2488 struct f2fs_node *fnode)
2492 nid_t ino = le32_to_cpu(fnode->footer.ino);
2493 struct node_info ni;
2496 namelen = snprintf(name, 80, "%u", ino);
2498 /* ignore terminating '\0', should never happen */
2501 if (f2fs_lookup(sbi, lpf, (u8 *)name, namelen)) {
2502 ASSERT_MSG("Name %s already exist in lost+found", name);
2506 get_node_info(sbi, le32_to_cpu(lpf->footer.ino), &ni);
2507 ftype = map_de_type(le16_to_cpu(fnode->i.i_mode));
2508 ret = f2fs_add_link(sbi, lpf, (unsigned char *)name, namelen,
2509 ino, ftype, ni.blk_addr, 0);
2511 ASSERT_MSG("Failed to add inode [0x%x] to lost+found", ino);
2516 memcpy(fnode->i.i_name, name, namelen);
2517 fnode->i.i_namelen = cpu_to_le32(namelen);
2518 fnode->i.i_pino = c.lpf_ino;
2519 get_node_info(sbi, le32_to_cpu(fnode->footer.ino), &ni);
2520 ret = dev_write_block(fnode, ni.blk_addr);
2523 DBG(1, "Reconnect inode [0x%x] to lost+found\n", ino);
2527 static void fsck_failed_reconnect_file_dnode(struct f2fs_sb_info *sbi,
2530 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2531 struct f2fs_node *node;
2532 struct node_info ni;
2536 node = calloc(F2FS_BLKSIZE, 1);
2539 get_node_info(sbi, nid, &ni);
2540 err = dev_read_block(node, ni.blk_addr);
2543 fsck->chk.valid_node_cnt--;
2544 fsck->chk.valid_blk_cnt--;
2545 f2fs_clear_main_bitmap(sbi, ni.blk_addr);
2547 for (i = 0; i < ADDRS_PER_BLOCK(&node->i); i++) {
2548 addr = le32_to_cpu(node->dn.addr[i]);
2551 fsck->chk.valid_blk_cnt--;
2552 if (addr == NEW_ADDR)
2554 f2fs_clear_main_bitmap(sbi, addr);
2560 static void fsck_failed_reconnect_file_idnode(struct f2fs_sb_info *sbi,
2563 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2564 struct f2fs_node *node;
2565 struct node_info ni;
2569 node = calloc(F2FS_BLKSIZE, 1);
2572 get_node_info(sbi, nid, &ni);
2573 err = dev_read_block(node, ni.blk_addr);
2576 fsck->chk.valid_node_cnt--;
2577 fsck->chk.valid_blk_cnt--;
2578 f2fs_clear_main_bitmap(sbi, ni.blk_addr);
2580 for (i = 0; i < NIDS_PER_BLOCK; i++) {
2581 tmp = le32_to_cpu(node->in.nid[i]);
2584 fsck_failed_reconnect_file_dnode(sbi, tmp);
2590 static void fsck_failed_reconnect_file_didnode(struct f2fs_sb_info *sbi,
2593 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2594 struct f2fs_node *node;
2595 struct node_info ni;
2599 node = calloc(F2FS_BLKSIZE, 1);
2602 get_node_info(sbi, nid, &ni);
2603 err = dev_read_block(node, ni.blk_addr);
2606 fsck->chk.valid_node_cnt--;
2607 fsck->chk.valid_blk_cnt--;
2608 f2fs_clear_main_bitmap(sbi, ni.blk_addr);
2610 for (i = 0; i < NIDS_PER_BLOCK; i++) {
2611 tmp = le32_to_cpu(node->in.nid[i]);
2614 fsck_failed_reconnect_file_idnode(sbi, tmp);
2621 * Counters and main_area_bitmap are already changed during checking
2622 * inode block, so clear them. There is no need to clear new blocks
2623 * allocted to lost+found.
2625 static void fsck_failed_reconnect_file(struct f2fs_sb_info *sbi, nid_t ino)
2627 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2628 struct f2fs_node *node;
2629 struct node_info ni;
2633 node = calloc(F2FS_BLKSIZE, 1);
2636 get_node_info(sbi, ino, &ni);
2637 err = dev_read_block(node, ni.blk_addr);
2640 /* clear inode counters */
2641 fsck->chk.valid_inode_cnt--;
2642 fsck->chk.valid_node_cnt--;
2643 fsck->chk.valid_blk_cnt--;
2644 f2fs_clear_main_bitmap(sbi, ni.blk_addr);
2646 /* clear xnid counters */
2647 if (node->i.i_xattr_nid) {
2648 nid = le32_to_cpu(node->i.i_xattr_nid);
2649 fsck->chk.valid_node_cnt--;
2650 fsck->chk.valid_blk_cnt--;
2651 get_node_info(sbi, nid, &ni);
2652 f2fs_clear_main_bitmap(sbi, ni.blk_addr);
2655 /* clear data counters */
2656 if(!(node->i.i_inline & F2FS_INLINE_DATA)) {
2657 ofs = get_extra_isize(node);
2658 for (i = 0; i < ADDRS_PER_INODE(&node->i); i++) {
2659 block_t addr = le32_to_cpu(node->i.i_addr[ofs + i]);
2662 fsck->chk.valid_blk_cnt--;
2663 if (addr == NEW_ADDR)
2665 f2fs_clear_main_bitmap(sbi, addr);
2669 for (i = 0; i < 5; i++) {
2670 nid = le32_to_cpu(node->i.i_nid[i]);
2675 case 0: /* direct node */
2677 fsck_failed_reconnect_file_dnode(sbi, nid);
2679 case 2: /* indirect node */
2681 fsck_failed_reconnect_file_idnode(sbi, nid);
2683 case 4: /* double indirect node */
2684 fsck_failed_reconnect_file_didnode(sbi, nid);
2693 * Scan unreachable nids and find only regular file inodes. If these files
2694 * are not corrupted, reconnect them to lost+found.
2696 * Since all unreachable nodes are already checked, we can allocate new
2699 * This function returns the number of files been reconnected.
2701 static int fsck_reconnect_file(struct f2fs_sb_info *sbi)
2703 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2704 struct f2fs_node *lpf_node, *node;
2705 struct node_info ni;
2706 char *reconnect_bitmap;
2709 int err, cnt = 0, ftype;
2711 node = calloc(F2FS_BLKSIZE, 1);
2714 reconnect_bitmap = calloc(fsck->nat_area_bitmap_sz, 1);
2715 ASSERT(reconnect_bitmap);
2717 for (nid = 0; nid < fsck->nr_nat_entries; nid++) {
2718 if (f2fs_test_bit(nid, fsck->nat_area_bitmap)) {
2719 if (is_qf_ino(F2FS_RAW_SUPER(sbi), nid)) {
2720 DBG(1, "Not support quota inode [0x%x]\n",
2725 get_node_info(sbi, nid, &ni);
2726 err = dev_read_block(node, ni.blk_addr);
2729 /* reconnection will restore these nodes if needed */
2730 if (node->footer.ino != node->footer.nid) {
2731 DBG(1, "Not support non-inode node [0x%x]\n",
2736 if (S_ISDIR(le16_to_cpu(node->i.i_mode))) {
2737 DBG(1, "Not support directory inode [0x%x]\n",
2742 ftype = map_de_type(le16_to_cpu(node->i.i_mode));
2743 if (sanity_check_nid(sbi, nid, node, ftype,
2745 ASSERT_MSG("Invalid nid [0x%x]\n", nid);
2749 DBG(1, "Check inode 0x%x\n", nid);
2751 fsck_chk_inode_blk(sbi, nid, ftype, node,
2752 &blk_cnt, &ni, NULL);
2754 f2fs_set_bit(nid, reconnect_bitmap);
2758 lpf_node = fsck_get_lpf(sbi);
2762 for (nid = 0; nid < fsck->nr_nat_entries; nid++) {
2763 if (f2fs_test_bit(nid, reconnect_bitmap)) {
2764 get_node_info(sbi, nid, &ni);
2765 err = dev_read_block(node, ni.blk_addr);
2768 if (fsck_do_reconnect_file(sbi, lpf_node, node)) {
2769 DBG(1, "Failed to reconnect inode [0x%x]\n",
2771 fsck_failed_reconnect_file(sbi, nid);
2775 quota_add_inode_usage(fsck->qctx, nid, &node->i);
2777 DBG(1, "Reconnected inode [0x%x] to lost+found\n", nid);
2785 free(reconnect_bitmap);
2789 #ifdef HAVE_LINUX_BLKZONED_H
2791 struct write_pointer_check_data {
2792 struct f2fs_sb_info *sbi;
2796 static int chk_and_fix_wp_with_sit(int i, void *blkzone, void *opaque)
2798 struct blk_zone *blkz = (struct blk_zone *)blkzone;
2799 struct write_pointer_check_data *wpd = opaque;
2800 struct f2fs_sb_info *sbi = wpd->sbi;
2801 struct device_info *dev = c.devices + wpd->dev_index;
2802 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2803 block_t zone_block, wp_block, wp_blkoff;
2804 unsigned int zone_segno, wp_segno;
2805 struct curseg_info *cs;
2806 int cs_index, ret, last_valid_blkoff;
2807 int log_sectors_per_block = sbi->log_blocksize - SECTOR_SHIFT;
2808 unsigned int segs_per_zone = sbi->segs_per_sec * sbi->secs_per_zone;
2810 if (blk_zone_conv(blkz))
2813 zone_block = dev->start_blkaddr
2814 + (blk_zone_sector(blkz) >> log_sectors_per_block);
2815 zone_segno = GET_SEGNO(sbi, zone_block);
2816 if (zone_segno >= MAIN_SEGS(sbi))
2819 wp_block = dev->start_blkaddr
2820 + (blk_zone_wp_sector(blkz) >> log_sectors_per_block);
2821 wp_segno = GET_SEGNO(sbi, wp_block);
2822 wp_blkoff = wp_block - START_BLOCK(sbi, wp_segno);
2824 /* if a curseg points to the zone, skip the check */
2825 for (cs_index = 0; cs_index < NO_CHECK_TYPE; cs_index++) {
2826 cs = &SM_I(sbi)->curseg_array[cs_index];
2827 if (zone_segno <= cs->segno &&
2828 cs->segno < zone_segno + segs_per_zone)
2832 last_valid_blkoff = last_vblk_off_in_zone(sbi, zone_segno);
2835 * When there is no valid block in the zone, check write pointer is
2836 * at zone start. If not, reset the write pointer.
2838 if (last_valid_blkoff < 0 &&
2839 blk_zone_wp_sector(blkz) != blk_zone_sector(blkz)) {
2841 MSG(0, "Inconsistent write pointer: wp[0x%x,0x%x]\n",
2842 wp_segno, wp_blkoff);
2843 fsck->chk.wp_inconsistent_zones++;
2847 FIX_MSG("Reset write pointer of zone at segment 0x%x",
2849 ret = f2fs_reset_zone(wpd->dev_index, blkz);
2851 printf("[FSCK] Write pointer reset failed: %s\n",
2855 fsck->chk.wp_fixed = 1;
2860 * If valid blocks exist in the zone beyond the write pointer, it
2861 * is a bug. No need to fix because the zone is not selected for the
2862 * write. Just report it.
2864 if (last_valid_blkoff + zone_block > wp_block) {
2865 MSG(0, "Unexpected invalid write pointer: wp[0x%x,0x%x]\n",
2866 wp_segno, wp_blkoff);
2873 static void fix_wp_sit_alignment(struct f2fs_sb_info *sbi)
2876 struct write_pointer_check_data wpd = { sbi, 0 };
2878 if (c.zoned_model != F2FS_ZONED_HM)
2881 for (i = 0; i < MAX_DEVICES; i++) {
2882 if (!c.devices[i].path)
2884 if (c.devices[i].zoned_model != F2FS_ZONED_HM)
2888 if (f2fs_report_zones(i, chk_and_fix_wp_with_sit, &wpd)) {
2889 printf("[FSCK] Write pointer check failed: %s\n",
2898 static void fix_wp_sit_alignment(struct f2fs_sb_info *UNUSED(sbi))
2906 * Check and fix consistency with write pointers at the beginning of
2907 * fsck so that following writes by fsck do not fail.
2909 void fsck_chk_and_fix_write_pointers(struct f2fs_sb_info *sbi)
2911 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2913 if (c.zoned_model != F2FS_ZONED_HM)
2916 if (check_curseg_offsets(sbi) && c.fix_on) {
2917 fix_curseg_info(sbi);
2918 fsck->chk.wp_fixed = 1;
2921 fix_wp_sit_alignment(sbi);
2924 int fsck_chk_curseg_info(struct f2fs_sb_info *sbi)
2926 struct curseg_info *curseg;
2927 struct seg_entry *se;
2928 struct f2fs_summary_block *sum_blk;
2931 for (i = 0; i < NO_CHECK_TYPE; i++) {
2932 curseg = CURSEG_I(sbi, i);
2933 se = get_seg_entry(sbi, curseg->segno);
2934 sum_blk = curseg->sum_blk;
2936 if (se->type != i) {
2937 ASSERT_MSG("Incorrect curseg [%d]: segno [0x%x] "
2938 "type(SIT) [%d]", i, curseg->segno,
2940 if (c.fix_on || c.preen_mode)
2944 if (i <= CURSEG_COLD_DATA && IS_SUM_DATA_SEG(sum_blk->footer)) {
2946 } else if (i > CURSEG_COLD_DATA && IS_SUM_NODE_SEG(sum_blk->footer)) {
2949 ASSERT_MSG("Incorrect curseg [%d]: segno [0x%x] "
2950 "type(SSA) [%d]", i, curseg->segno,
2951 sum_blk->footer.entry_type);
2952 if (c.fix_on || c.preen_mode)
2953 sum_blk->footer.entry_type =
2954 i <= CURSEG_COLD_DATA ?
2955 SUM_TYPE_DATA : SUM_TYPE_NODE;
2963 int fsck_verify(struct f2fs_sb_info *sbi)
2968 u32 nr_unref_nid = 0;
2969 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2970 struct hard_link_node *node = NULL;
2974 if (c.zoned_model == F2FS_ZONED_HM) {
2975 printf("[FSCK] Write pointers consistency ");
2976 if (fsck->chk.wp_inconsistent_zones == 0x0) {
2977 printf(" [Ok..]\n");
2979 printf(" [Fail] [0x%x]\n",
2980 fsck->chk.wp_inconsistent_zones);
2984 if (fsck->chk.wp_fixed && c.fix_on)
2988 if (c.feature & cpu_to_le32(F2FS_FEATURE_LOST_FOUND)) {
2989 for (i = 0; i < fsck->nr_nat_entries; i++)
2990 if (f2fs_test_bit(i, fsck->nat_area_bitmap) != 0)
2992 if (i < fsck->nr_nat_entries) {
2993 i = fsck_reconnect_file(sbi);
2994 printf("[FSCK] Reconnect %u files to lost+found\n", i);
2998 for (i = 0; i < fsck->nr_nat_entries; i++) {
2999 if (f2fs_test_bit(i, fsck->nat_area_bitmap) != 0) {
3000 struct node_info ni;
3002 get_node_info(sbi, i, &ni);
3003 printf("NID[0x%x] is unreachable, blkaddr:0x%x\n",
3009 if (fsck->hard_link_list_head != NULL) {
3010 node = fsck->hard_link_list_head;
3012 printf("NID[0x%x] has [0x%x] more unreachable links\n",
3013 node->nid, node->links);
3019 printf("[FSCK] Unreachable nat entries ");
3020 if (nr_unref_nid == 0x0) {
3021 printf(" [Ok..] [0x%x]\n", nr_unref_nid);
3023 printf(" [Fail] [0x%x]\n", nr_unref_nid);
3024 ret = EXIT_ERR_CODE;
3028 printf("[FSCK] SIT valid block bitmap checking ");
3029 if (memcmp(fsck->sit_area_bitmap, fsck->main_area_bitmap,
3030 fsck->sit_area_bitmap_sz) == 0x0) {
3034 ret = EXIT_ERR_CODE;
3038 printf("[FSCK] Hard link checking for regular file ");
3039 if (fsck->hard_link_list_head == NULL) {
3040 printf(" [Ok..] [0x%x]\n", fsck->chk.multi_hard_link_files);
3042 printf(" [Fail] [0x%x]\n", fsck->chk.multi_hard_link_files);
3043 ret = EXIT_ERR_CODE;
3047 printf("[FSCK] valid_block_count matching with CP ");
3048 if (sbi->total_valid_block_count == fsck->chk.valid_blk_cnt) {
3049 printf(" [Ok..] [0x%x]\n", (u32)fsck->chk.valid_blk_cnt);
3051 printf(" [Fail] [0x%x]\n", (u32)fsck->chk.valid_blk_cnt);
3052 ret = EXIT_ERR_CODE;
3056 printf("[FSCK] valid_node_count matching with CP (de lookup) ");
3057 if (sbi->total_valid_node_count == fsck->chk.valid_node_cnt) {
3058 printf(" [Ok..] [0x%x]\n", fsck->chk.valid_node_cnt);
3060 printf(" [Fail] [0x%x]\n", fsck->chk.valid_node_cnt);
3061 ret = EXIT_ERR_CODE;
3065 printf("[FSCK] valid_node_count matching with CP (nat lookup)");
3066 if (sbi->total_valid_node_count == fsck->chk.valid_nat_entry_cnt) {
3067 printf(" [Ok..] [0x%x]\n", fsck->chk.valid_nat_entry_cnt);
3069 printf(" [Fail] [0x%x]\n", fsck->chk.valid_nat_entry_cnt);
3070 ret = EXIT_ERR_CODE;
3074 printf("[FSCK] valid_inode_count matched with CP ");
3075 if (sbi->total_valid_inode_count == fsck->chk.valid_inode_cnt) {
3076 printf(" [Ok..] [0x%x]\n", fsck->chk.valid_inode_cnt);
3078 printf(" [Fail] [0x%x]\n", fsck->chk.valid_inode_cnt);
3079 ret = EXIT_ERR_CODE;
3083 printf("[FSCK] free segment_count matched with CP ");
3084 if (le32_to_cpu(F2FS_CKPT(sbi)->free_segment_count) ==
3085 fsck->chk.sit_free_segs) {
3086 printf(" [Ok..] [0x%x]\n", fsck->chk.sit_free_segs);
3088 printf(" [Fail] [0x%x]\n", fsck->chk.sit_free_segs);
3089 ret = EXIT_ERR_CODE;
3093 printf("[FSCK] next block offset is free ");
3094 if (check_curseg_offsets(sbi) == 0) {
3095 printf(" [Ok..]\n");
3097 printf(" [Fail]\n");
3098 ret = EXIT_ERR_CODE;
3102 printf("[FSCK] fixing SIT types\n");
3103 if (check_sit_types(sbi) != 0)
3106 printf("[FSCK] other corrupted bugs ");
3107 if (c.bug_on == 0) {
3108 printf(" [Ok..]\n");
3110 printf(" [Fail]\n");
3111 ret = EXIT_ERR_CODE;
3114 #ifndef WITH_ANDROID
3115 if (nr_unref_nid && !c.ro) {
3116 char ans[255] = {0};
3118 printf("\nDo you want to restore lost files into ./lost_found/? [Y/N] ");
3119 ret = scanf("%s", ans);
3121 if (!strcasecmp(ans, "y")) {
3122 for (i = 0; i < fsck->nr_nat_entries; i++) {
3123 if (f2fs_test_bit(i, fsck->nat_area_bitmap))
3124 dump_node(sbi, i, 1);
3130 /* fix global metadata */
3131 if (force || (c.fix_on && f2fs_dev_is_writable())) {
3132 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
3134 if (force || c.bug_on || c.bug_nat_bits) {
3135 /* flush nats to write_nit_bits below */
3136 flush_journal_entries(sbi);
3137 fix_hard_links(sbi);
3138 fix_nat_entries(sbi);
3139 rewrite_sit_area_bitmap(sbi);
3140 fix_wp_sit_alignment(sbi);
3141 fix_curseg_info(sbi);
3143 fix_checkpoints(sbi);
3144 } else if (is_set_ckpt_flags(cp, CP_FSCK_FLAG) ||
3145 is_set_ckpt_flags(cp, CP_QUOTA_NEED_FSCK_FLAG)) {
3146 write_checkpoints(sbi);
3152 void fsck_free(struct f2fs_sb_info *sbi)
3154 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
3157 quota_release_context(&fsck->qctx);
3159 if (fsck->main_area_bitmap)
3160 free(fsck->main_area_bitmap);
3162 if (fsck->nat_area_bitmap)
3163 free(fsck->nat_area_bitmap);
3165 if (fsck->sit_area_bitmap)
3166 free(fsck->sit_area_bitmap);
3169 free(fsck->entries);