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 static int fsck_chk_xattr_blk(struct f2fs_sb_info *sbi, u32 ino,
491 u32 x_nid, u32 *blk_cnt)
493 struct f2fs_node *node_blk = NULL;
500 node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
501 ASSERT(node_blk != NULL);
504 if (sanity_check_nid(sbi, x_nid, node_blk,
505 F2FS_FT_XATTR, TYPE_XATTR, &ni)) {
510 *blk_cnt = *blk_cnt + 1;
511 f2fs_set_main_bitmap(sbi, ni.blk_addr, CURSEG_COLD_NODE);
512 DBG(2, "ino[0x%x] x_nid[0x%x]\n", ino, x_nid);
518 int fsck_chk_node_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
519 u32 nid, enum FILE_TYPE ftype, enum NODE_TYPE ntype,
520 u32 *blk_cnt, struct child_info *child)
523 struct f2fs_node *node_blk = NULL;
525 node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
526 ASSERT(node_blk != NULL);
528 if (sanity_check_nid(sbi, nid, node_blk, ftype, ntype, &ni))
531 if (ntype == TYPE_INODE) {
532 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
534 fsck_chk_inode_blk(sbi, nid, ftype, node_blk, blk_cnt, &ni, child);
535 quota_add_inode_usage(fsck->qctx, nid, &node_blk->i);
538 case TYPE_DIRECT_NODE:
539 f2fs_set_main_bitmap(sbi, ni.blk_addr,
541 fsck_chk_dnode_blk(sbi, inode, nid, ftype, node_blk,
542 blk_cnt, child, &ni);
544 case TYPE_INDIRECT_NODE:
545 f2fs_set_main_bitmap(sbi, ni.blk_addr,
547 fsck_chk_idnode_blk(sbi, inode, ftype, node_blk,
550 case TYPE_DOUBLE_INDIRECT_NODE:
551 f2fs_set_main_bitmap(sbi, ni.blk_addr,
553 fsck_chk_didnode_blk(sbi, inode, ftype, node_blk,
567 static inline void get_extent_info(struct extent_info *ext,
568 struct f2fs_extent *i_ext)
570 ext->fofs = le32_to_cpu(i_ext->fofs);
571 ext->blk = le32_to_cpu(i_ext->blk_addr);
572 ext->len = le32_to_cpu(i_ext->len);
575 static void check_extent_info(struct child_info *child,
576 block_t blkaddr, int last)
578 struct extent_info *ei = &child->ei;
579 u32 pgofs = child->pgofs;
585 if (child->state & FSCK_UNMATCHED_EXTENT)
588 if ((child->state & FSCK_INLINE_INODE) && ei->len)
592 /* hole exist in the back of extent */
593 if (child->last_blk != ei->blk + ei->len - 1)
594 child->state |= FSCK_UNMATCHED_EXTENT;
598 if (blkaddr == NULL_ADDR || blkaddr == NEW_ADDR)
601 if (pgofs >= ei->fofs && pgofs < ei->fofs + ei->len) {
602 /* unmatched blkaddr */
603 if (is_hole || (blkaddr != pgofs - ei->fofs + ei->blk))
606 if (!child->last_blk) {
607 /* hole exists in the front of extent */
608 if (pgofs != ei->fofs)
610 } else if (child->last_blk + 1 != blkaddr) {
611 /* hole exists in the middle of extent */
614 child->last_blk = blkaddr;
621 if (blkaddr < ei->blk || blkaddr >= ei->blk + ei->len)
623 /* unmatched file offset */
625 child->state |= FSCK_UNMATCHED_EXTENT;
628 void fsck_reada_node_block(struct f2fs_sb_info *sbi, u32 nid)
632 if (nid != 0 && IS_VALID_NID(sbi, nid)) {
633 get_node_info(sbi, nid, &ni);
634 if (IS_VALID_BLK_ADDR(sbi, ni.blk_addr))
635 dev_reada_block(ni.blk_addr);
639 void fsck_reada_all_direct_node_blocks(struct f2fs_sb_info *sbi,
640 struct f2fs_node *node_blk)
644 for (i = 0; i < NIDS_PER_BLOCK; i++) {
645 u32 nid = le32_to_cpu(node_blk->in.nid[i]);
647 fsck_reada_node_block(sbi, nid);
651 /* start with valid nid and blkaddr */
652 void fsck_chk_inode_blk(struct f2fs_sb_info *sbi, u32 nid,
653 enum FILE_TYPE ftype, struct f2fs_node *node_blk,
654 u32 *blk_cnt, struct node_info *ni, struct child_info *child_d)
656 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
657 struct child_info child;
658 enum NODE_TYPE ntype;
659 u32 i_links = le32_to_cpu(node_blk->i.i_links);
660 u64 i_size = le64_to_cpu(node_blk->i.i_size);
661 u64 i_blocks = le64_to_cpu(node_blk->i.i_blocks);
662 nid_t i_xattr_nid = le32_to_cpu(node_blk->i.i_xattr_nid);
666 unsigned int idx = 0;
667 unsigned short i_gc_failures;
671 memset(&child, 0, sizeof(child));
674 child.pp_ino = le32_to_cpu(node_blk->i.i_pino);
675 child.dir_level = node_blk->i.i_dir_level;
677 if (f2fs_test_main_bitmap(sbi, ni->blk_addr) == 0)
678 fsck->chk.valid_inode_cnt++;
680 if (ftype == F2FS_FT_DIR) {
681 f2fs_set_main_bitmap(sbi, ni->blk_addr, CURSEG_HOT_NODE);
683 if (f2fs_test_main_bitmap(sbi, ni->blk_addr) == 0) {
684 f2fs_set_main_bitmap(sbi, ni->blk_addr,
686 if (i_links > 1 && ftype != F2FS_FT_ORPHAN &&
687 !is_qf_ino(F2FS_RAW_SUPER(sbi), nid)) {
688 /* First time. Create new hard link node */
689 add_into_hard_link_list(sbi, nid, i_links);
690 fsck->chk.multi_hard_link_files++;
693 DBG(3, "[0x%x] has hard links [0x%x]\n", nid, i_links);
694 if (find_and_dec_hard_link_list(sbi, nid)) {
695 ASSERT_MSG("[0x%x] needs more i_links=0x%x",
698 node_blk->i.i_links =
699 cpu_to_le32(i_links + 1);
701 FIX_MSG("File: 0x%x "
702 "i_links= 0x%x -> 0x%x",
703 nid, i_links, i_links + 1);
705 goto skip_blkcnt_fix;
707 /* No need to go deep into the node */
712 /* readahead xattr node block */
713 fsck_reada_node_block(sbi, i_xattr_nid);
715 if (fsck_chk_xattr_blk(sbi, nid, i_xattr_nid, blk_cnt)) {
717 node_blk->i.i_xattr_nid = 0;
719 FIX_MSG("Remove xattr block: 0x%x, x_nid = 0x%x",
724 if (ftype == F2FS_FT_CHRDEV || ftype == F2FS_FT_BLKDEV ||
725 ftype == F2FS_FT_FIFO || ftype == F2FS_FT_SOCK)
728 /* init extent info */
729 get_extent_info(&child.ei, &node_blk->i.i_ext);
732 if (f2fs_has_extra_isize(&node_blk->i)) {
733 if (c.feature & cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR)) {
735 le16_to_cpu(node_blk->i.i_extra_isize);
736 if (isize > 4 * DEF_ADDRS_PER_INODE) {
737 ASSERT_MSG("[0x%x] wrong i_extra_isize=0x%x",
740 FIX_MSG("ino[0x%x] recover i_extra_isize "
744 node_blk->i.i_extra_isize =
745 cpu_to_le16(calc_extra_isize());
750 ASSERT_MSG("[0x%x] wrong extra_attr flag", nid);
752 FIX_MSG("ino[0x%x] remove F2FS_EXTRA_ATTR "
753 "flag in i_inline:%u",
754 nid, node_blk->i.i_inline);
755 /* we don't support tuning F2FS_FEATURE_EXTRA_ATTR now */
756 node_blk->i.i_inline &= ~F2FS_EXTRA_ATTR;
762 cpu_to_le32(F2FS_FEATURE_FLEXIBLE_INLINE_XATTR)) &&
763 (node_blk->i.i_inline & F2FS_INLINE_XATTR)) {
764 unsigned int inline_size =
765 le16_to_cpu(node_blk->i.i_inline_xattr_size);
768 inline_size > MAX_INLINE_XATTR_SIZE) {
769 ASSERT_MSG("[0x%x] wrong inline_xattr_size:%u",
772 FIX_MSG("ino[0x%x] recover inline xattr size "
775 DEFAULT_INLINE_XATTR_ADDRS);
776 node_blk->i.i_inline_xattr_size =
777 cpu_to_le16(DEFAULT_INLINE_XATTR_ADDRS);
783 ofs = get_extra_isize(node_blk);
785 if ((node_blk->i.i_inline & F2FS_INLINE_DATA)) {
786 unsigned int inline_size = MAX_INLINE_DATA(node_blk);
787 block_t blkaddr = le32_to_cpu(node_blk->i.i_addr[ofs]);
790 ASSERT_MSG("[0x%x] wrong inline reserve blkaddr:%u",
793 FIX_MSG("inline_data has wrong 0'th block = %x",
795 node_blk->i.i_addr[ofs] = 0;
796 node_blk->i.i_blocks = cpu_to_le64(*blk_cnt);
800 if (i_size > inline_size) {
801 ASSERT_MSG("[0x%x] wrong inline size:%lu",
802 nid, (unsigned long)i_size);
804 node_blk->i.i_size = cpu_to_le64(inline_size);
805 FIX_MSG("inline_data has wrong i_size %lu",
806 (unsigned long)i_size);
810 if (!(node_blk->i.i_inline & F2FS_DATA_EXIST)) {
811 char buf[MAX_INLINE_DATA(node_blk)];
812 memset(buf, 0, MAX_INLINE_DATA(node_blk));
814 if (memcmp(buf, inline_data_addr(node_blk),
815 MAX_INLINE_DATA(node_blk))) {
816 ASSERT_MSG("[0x%x] junk inline data", nid);
818 FIX_MSG("inline_data has DATA_EXIST");
819 node_blk->i.i_inline |= F2FS_DATA_EXIST;
824 DBG(3, "ino[0x%x] has inline data!\n", nid);
825 child.state |= FSCK_INLINE_INODE;
829 if ((node_blk->i.i_inline & F2FS_INLINE_DENTRY)) {
830 block_t blkaddr = le32_to_cpu(node_blk->i.i_addr[ofs]);
832 DBG(3, "ino[0x%x] has inline dentry!\n", nid);
834 ASSERT_MSG("[0x%x] wrong inline reserve blkaddr:%u",
837 FIX_MSG("inline_dentry has wrong 0'th block = %x",
839 node_blk->i.i_addr[ofs] = 0;
840 node_blk->i.i_blocks = cpu_to_le64(*blk_cnt);
845 ret = fsck_chk_inline_dentries(sbi, node_blk, &child);
850 child.state |= FSCK_INLINE_INODE;
854 /* check data blocks in inode */
855 for (idx = 0; idx < ADDRS_PER_INODE(&node_blk->i);
856 idx++, child.pgofs++) {
857 block_t blkaddr = le32_to_cpu(node_blk->i.i_addr[ofs + idx]);
859 /* check extent info */
860 check_extent_info(&child, blkaddr, 0);
862 if (blkaddr == COMPRESS_ADDR) {
863 if (node_blk->i.i_compr_blocks) {
864 fsck->chk.valid_blk_cnt++;
865 *blk_cnt = *blk_cnt + 1;
871 ret = fsck_chk_data_blk(sbi,
872 IS_CASEFOLDED(&node_blk->i),
874 &child, (i_blocks == *blk_cnt),
875 ftype, nid, idx, ni->version,
876 file_is_encrypt(&node_blk->i));
878 *blk_cnt = *blk_cnt + 1;
879 } else if (c.fix_on) {
880 node_blk->i.i_addr[ofs + idx] = 0;
882 FIX_MSG("[0x%x] i_addr[%d] = 0",
888 /* readahead node blocks */
889 for (idx = 0; idx < 5; idx++) {
890 u32 nid = le32_to_cpu(node_blk->i.i_nid[idx]);
891 fsck_reada_node_block(sbi, nid);
894 /* check node blocks in inode */
895 for (idx = 0; idx < 5; idx++) {
896 nid_t i_nid = le32_to_cpu(node_blk->i.i_nid[idx]);
898 if (idx == 0 || idx == 1)
899 ntype = TYPE_DIRECT_NODE;
900 else if (idx == 2 || idx == 3)
901 ntype = TYPE_INDIRECT_NODE;
903 ntype = TYPE_DOUBLE_INDIRECT_NODE;
910 ret = fsck_chk_node_blk(sbi, &node_blk->i, i_nid,
911 ftype, ntype, blk_cnt, &child);
913 *blk_cnt = *blk_cnt + 1;
914 } else if (ret == -EINVAL) {
916 node_blk->i.i_nid[idx] = 0;
918 FIX_MSG("[0x%x] i_nid[%d] = 0", nid, idx);
921 if (ntype == TYPE_DIRECT_NODE)
922 child.pgofs += ADDRS_PER_BLOCK(&node_blk->i);
923 else if (ntype == TYPE_INDIRECT_NODE)
924 child.pgofs += ADDRS_PER_BLOCK(&node_blk->i) *
927 child.pgofs += ADDRS_PER_BLOCK(&node_blk->i) *
928 NIDS_PER_BLOCK * NIDS_PER_BLOCK;
934 /* check uncovered range in the back of extent */
935 check_extent_info(&child, 0, 1);
937 if (child.state & FSCK_UNMATCHED_EXTENT) {
938 ASSERT_MSG("ino: 0x%x has wrong ext: [pgofs:%u, blk:%u, len:%u]",
939 nid, child.ei.fofs, child.ei.blk, child.ei.len);
944 if (i_blocks != *blk_cnt) {
945 ASSERT_MSG("ino: 0x%x has i_blocks: %08"PRIx64", "
947 nid, i_blocks, *blk_cnt);
949 node_blk->i.i_blocks = cpu_to_le64(*blk_cnt);
951 FIX_MSG("[0x%x] i_blocks=0x%08"PRIx64" -> 0x%x",
952 nid, i_blocks, *blk_cnt);
956 en = malloc(F2FS_PRINT_NAMELEN);
959 namelen = le32_to_cpu(node_blk->i.i_namelen);
960 if (namelen > F2FS_NAME_LEN) {
961 if (child_d && child_d->i_namelen <= F2FS_NAME_LEN) {
962 ASSERT_MSG("ino: 0x%x has i_namelen: 0x%x, "
963 "but has %d characters for name",
964 nid, namelen, child_d->i_namelen);
966 FIX_MSG("[0x%x] i_namelen=0x%x -> 0x%x", nid, namelen,
968 node_blk->i.i_namelen = cpu_to_le32(child_d->i_namelen);
971 namelen = child_d->i_namelen;
973 namelen = F2FS_NAME_LEN;
975 pretty_print_filename(node_blk->i.i_name, namelen, en,
976 file_enc_name(&node_blk->i));
977 if (ftype == F2FS_FT_ORPHAN)
978 DBG(1, "Orphan Inode: 0x%x [%s] i_blocks: %u\n\n",
979 le32_to_cpu(node_blk->footer.ino),
982 if (is_qf_ino(F2FS_RAW_SUPER(sbi), nid))
983 DBG(1, "Quota Inode: 0x%x [%s] i_blocks: %u\n\n",
984 le32_to_cpu(node_blk->footer.ino),
987 if (ftype == F2FS_FT_DIR) {
988 DBG(1, "Directory Inode: 0x%x [%s] depth: %d has %d files\n\n",
989 le32_to_cpu(node_blk->footer.ino), en,
990 le32_to_cpu(node_blk->i.i_current_depth),
993 if (i_links != child.links) {
994 ASSERT_MSG("ino: 0x%x i_links: %u, real links: %u",
995 nid, i_links, child.links);
997 node_blk->i.i_links = cpu_to_le32(child.links);
999 FIX_MSG("Dir: 0x%x i_links= 0x%x -> 0x%x",
1000 nid, i_links, child.links);
1003 if (child.dots < 2 &&
1004 !(node_blk->i.i_inline & F2FS_INLINE_DOTS)) {
1005 ASSERT_MSG("ino: 0x%x dots: %u",
1008 node_blk->i.i_inline |= F2FS_INLINE_DOTS;
1010 FIX_MSG("Dir: 0x%x set inline_dots", nid);
1015 i_gc_failures = le16_to_cpu(node_blk->i.i_gc_failures);
1018 * old kernel initialized i_gc_failures as 0x01, in preen mode 2,
1019 * let's skip repairing.
1021 if (ftype == F2FS_FT_REG_FILE && i_gc_failures &&
1022 (c.preen_mode != PREEN_MODE_2 || i_gc_failures != 0x01)) {
1024 DBG(1, "Regular Inode: 0x%x [%s] depth: %d\n\n",
1025 le32_to_cpu(node_blk->footer.ino), en,
1029 node_blk->i.i_gc_failures = cpu_to_le16(0);
1031 FIX_MSG("Regular: 0x%x reset i_gc_failures from 0x%x to 0x00",
1032 nid, i_gc_failures);
1038 if (ftype == F2FS_FT_SYMLINK && i_size == 0 &&
1039 i_blocks == (i_xattr_nid ? 3 : 2)) {
1040 ASSERT_MSG("ino: 0x%x i_blocks: %lu with zero i_size\n",
1041 nid, (unsigned long)i_blocks);
1043 node_blk->i.i_size = cpu_to_le64(F2FS_BLKSIZE);
1045 FIX_MSG("Symlink: recover 0x%x with i_size=%lu",
1046 nid, (unsigned long)F2FS_BLKSIZE);
1050 if (ftype == F2FS_FT_ORPHAN && i_links) {
1051 ASSERT_MSG("ino: 0x%x is orphan inode, but has i_links: %u",
1054 node_blk->i.i_links = 0;
1056 FIX_MSG("ino: 0x%x orphan_inode, i_links= 0x%x -> 0",
1061 /* drop extent information to avoid potential wrong access */
1062 if (need_fix && f2fs_dev_is_writable())
1063 node_blk->i.i_ext.len = 0;
1065 if ((c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM)) &&
1066 f2fs_has_extra_isize(&node_blk->i)) {
1067 __u32 provided, calculated;
1069 provided = le32_to_cpu(node_blk->i.i_inode_checksum);
1070 calculated = f2fs_inode_chksum(node_blk);
1072 if (provided != calculated) {
1073 ASSERT_MSG("ino: 0x%x chksum:0x%x, but calculated one is: 0x%x",
1074 nid, provided, calculated);
1076 node_blk->i.i_inode_checksum =
1077 cpu_to_le32(calculated);
1079 FIX_MSG("ino: 0x%x recover, i_inode_checksum= 0x%x -> 0x%x",
1080 nid, provided, calculated);
1085 if (need_fix && f2fs_dev_is_writable()) {
1086 ret = dev_write_block(node_blk, ni->blk_addr);
1091 int fsck_chk_dnode_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
1092 u32 nid, enum FILE_TYPE ftype, struct f2fs_node *node_blk,
1093 u32 *blk_cnt, struct child_info *child, struct node_info *ni)
1098 child->pp_ino = le32_to_cpu(inode->i_pino);
1100 for (idx = 0; idx < ADDRS_PER_BLOCK(inode); idx++, child->pgofs++) {
1101 block_t blkaddr = le32_to_cpu(node_blk->dn.addr[idx]);
1103 check_extent_info(child, blkaddr, 0);
1107 if (blkaddr == COMPRESS_ADDR) {
1108 if (inode->i_compr_blocks) {
1109 F2FS_FSCK(sbi)->chk.valid_blk_cnt++;
1110 *blk_cnt = *blk_cnt + 1;
1114 ret = fsck_chk_data_blk(sbi, IS_CASEFOLDED(inode),
1116 le64_to_cpu(inode->i_blocks) == *blk_cnt, ftype,
1117 nid, idx, ni->version,
1118 file_is_encrypt(inode));
1120 *blk_cnt = *blk_cnt + 1;
1121 } else if (c.fix_on) {
1122 node_blk->dn.addr[idx] = 0;
1124 FIX_MSG("[0x%x] dn.addr[%d] = 0", nid, idx);
1127 if (need_fix && f2fs_dev_is_writable()) {
1128 ret = dev_write_block(node_blk, ni->blk_addr);
1134 int fsck_chk_idnode_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
1135 enum FILE_TYPE ftype, struct f2fs_node *node_blk, u32 *blk_cnt,
1136 struct child_info *child)
1138 int need_fix = 0, ret;
1141 fsck_reada_all_direct_node_blocks(sbi, node_blk);
1143 for (i = 0; i < NIDS_PER_BLOCK; i++) {
1144 if (le32_to_cpu(node_blk->in.nid[i]) == 0x0)
1146 ret = fsck_chk_node_blk(sbi, inode,
1147 le32_to_cpu(node_blk->in.nid[i]),
1148 ftype, TYPE_DIRECT_NODE, blk_cnt, child);
1150 *blk_cnt = *blk_cnt + 1;
1151 else if (ret == -EINVAL) {
1153 printf("should delete in.nid[i] = 0;\n");
1155 node_blk->in.nid[i] = 0;
1157 FIX_MSG("Set indirect node 0x%x -> 0", i);
1160 child->pgofs += ADDRS_PER_BLOCK(&node_blk->i);
1164 if (need_fix && f2fs_dev_is_writable()) {
1165 struct node_info ni;
1166 nid_t nid = le32_to_cpu(node_blk->footer.nid);
1168 get_node_info(sbi, nid, &ni);
1169 ret = dev_write_block(node_blk, ni.blk_addr);
1176 int fsck_chk_didnode_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
1177 enum FILE_TYPE ftype, struct f2fs_node *node_blk, u32 *blk_cnt,
1178 struct child_info *child)
1181 int need_fix = 0, ret = 0;
1183 fsck_reada_all_direct_node_blocks(sbi, node_blk);
1185 for (i = 0; i < NIDS_PER_BLOCK; i++) {
1186 if (le32_to_cpu(node_blk->in.nid[i]) == 0x0)
1188 ret = fsck_chk_node_blk(sbi, inode,
1189 le32_to_cpu(node_blk->in.nid[i]),
1190 ftype, TYPE_INDIRECT_NODE, blk_cnt, child);
1192 *blk_cnt = *blk_cnt + 1;
1193 else if (ret == -EINVAL) {
1195 printf("should delete in.nid[i] = 0;\n");
1197 node_blk->in.nid[i] = 0;
1199 FIX_MSG("Set double indirect node 0x%x -> 0", i);
1202 child->pgofs += ADDRS_PER_BLOCK(&node_blk->i) *
1207 if (need_fix && f2fs_dev_is_writable()) {
1208 struct node_info ni;
1209 nid_t nid = le32_to_cpu(node_blk->footer.nid);
1211 get_node_info(sbi, nid, &ni);
1212 ret = dev_write_block(node_blk, ni.blk_addr);
1219 static const char *lookup_table =
1220 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
1225 * Encodes the input string using characters from the set [A-Za-z0-9+,].
1226 * The encoded string is roughly 4/3 times the size of the input string.
1228 static int base64_encode(const u8 *src, int len, char *dst)
1230 int i, bits = 0, ac = 0;
1233 for (i = 0; i < len; i++) {
1234 ac += src[i] << bits;
1237 *cp++ = lookup_table[ac & 0x3f];
1240 } while (bits >= 6);
1243 *cp++ = lookup_table[ac & 0x3f];
1247 void pretty_print_filename(const u8 *raw_name, u32 len,
1248 char out[F2FS_PRINT_NAMELEN], int enc_name)
1250 len = min(len, (u32)F2FS_NAME_LEN);
1253 len = base64_encode(raw_name, len, out);
1255 memcpy(out, raw_name, len);
1259 static void print_dentry(__u32 depth, __u8 *name,
1260 u8 *bitmap, struct f2fs_dir_entry *dentry,
1261 int max, int idx, int last_blk, int enc_name)
1268 char new[F2FS_PRINT_NAMELEN];
1273 name_len = le16_to_cpu(dentry[idx].name_len);
1274 next_idx = idx + (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN;
1276 bit_offset = find_next_bit_le(bitmap, max, next_idx);
1277 if (bit_offset >= max && last_blk)
1280 if (tree_mark_size <= depth) {
1281 tree_mark_size *= 2;
1282 ASSERT(tree_mark_size != 0);
1283 tree_mark = realloc(tree_mark, tree_mark_size);
1284 ASSERT(tree_mark != NULL);
1288 tree_mark[depth] = '`';
1290 tree_mark[depth] = '|';
1292 if (tree_mark[depth - 1] == '`')
1293 tree_mark[depth - 1] = ' ';
1295 for (i = 1; i < depth; i++)
1296 printf("%c ", tree_mark[i]);
1298 pretty_print_filename(name, name_len, new, enc_name);
1300 printf("%c-- %s <ino = 0x%x>, <encrypted (%d)>\n",
1301 last_de ? '`' : '|',
1302 new, le32_to_cpu(dentry[idx].ino),
1306 static int f2fs_check_hash_code(int encoding, int casefolded,
1307 struct f2fs_dir_entry *dentry,
1308 const unsigned char *name, u32 len, int enc_name)
1310 /* Casefolded Encrypted names require a key to compute siphash */
1311 if (enc_name && casefolded)
1314 f2fs_hash_t hash_code = f2fs_dentry_hash(encoding, casefolded, name, len);
1315 /* fix hash_code made by old buggy code */
1316 if (dentry->hash_code != hash_code) {
1317 char new[F2FS_PRINT_NAMELEN];
1319 pretty_print_filename(name, len, new, enc_name);
1320 FIX_MSG("Mismatch hash_code for \"%s\" [%x:%x]",
1321 new, le32_to_cpu(dentry->hash_code),
1323 dentry->hash_code = cpu_to_le32(hash_code);
1330 static int __get_current_level(int dir_level, u32 pgofs)
1332 unsigned int bidx = 0;
1335 for (i = 0; i < MAX_DIR_HASH_DEPTH; i++) {
1336 bidx += dir_buckets(i, dir_level) * bucket_blocks(i);
1343 static int f2fs_check_dirent_position(int encoding, int casefolded,
1344 u8 *name, u16 name_len, u32 pgofs,
1345 u8 dir_level, u32 pino)
1347 f2fs_hash_t namehash = f2fs_dentry_hash(encoding, casefolded, name, name_len);
1348 unsigned int nbucket, nblock;
1349 unsigned int bidx, end_block;
1352 level = __get_current_level(dir_level, pgofs);
1354 nbucket = dir_buckets(level, dir_level);
1355 nblock = bucket_blocks(level);
1357 bidx = dir_block_index(level, dir_level,
1358 le32_to_cpu(namehash) % nbucket);
1359 end_block = bidx + nblock;
1361 if (pgofs >= bidx && pgofs < end_block)
1364 ASSERT_MSG("Wrong position of dirent pino:%u, name:%s, level:%d, "
1365 "dir_level:%d, pgofs:%u, correct range:[%u, %u]\n",
1366 pino, name, level, dir_level, pgofs, bidx, end_block - 1);
1370 static int __chk_dots_dentries(struct f2fs_sb_info *sbi,
1372 struct f2fs_dir_entry *dentry,
1373 struct child_info *child,
1375 __u8 (*filename)[F2FS_SLOT_LEN],
1380 if ((name[0] == '.' && len == 1)) {
1381 if (le32_to_cpu(dentry->ino) != child->p_ino) {
1382 ASSERT_MSG("Bad inode number[0x%x] for '.', parent_ino is [0x%x]\n",
1383 le32_to_cpu(dentry->ino), child->p_ino);
1384 dentry->ino = cpu_to_le32(child->p_ino);
1389 if (name[0] == '.' && name[1] == '.' && len == 2) {
1390 if (child->p_ino == F2FS_ROOT_INO(sbi)) {
1391 if (le32_to_cpu(dentry->ino) != F2FS_ROOT_INO(sbi)) {
1392 ASSERT_MSG("Bad inode number[0x%x] for '..'\n",
1393 le32_to_cpu(dentry->ino));
1394 dentry->ino = cpu_to_le32(F2FS_ROOT_INO(sbi));
1397 } else if (le32_to_cpu(dentry->ino) != child->pp_ino) {
1398 ASSERT_MSG("Bad inode number[0x%x] for '..', parent parent ino is [0x%x]\n",
1399 le32_to_cpu(dentry->ino), child->pp_ino);
1400 dentry->ino = cpu_to_le32(child->pp_ino);
1405 if (f2fs_check_hash_code(get_encoding(sbi), casefolded, dentry, name, len, enc_name))
1408 if (name[len] != '\0') {
1409 ASSERT_MSG("'.' is not NULL terminated\n");
1411 memcpy(*filename, name, len);
1417 static void nullify_dentry(struct f2fs_dir_entry *dentry, int offs,
1418 __u8 (*filename)[F2FS_SLOT_LEN], u8 **bitmap)
1420 memset(dentry, 0, sizeof(struct f2fs_dir_entry));
1421 test_and_clear_bit_le(offs, *bitmap);
1422 memset(*filename, 0, F2FS_SLOT_LEN);
1425 static int __chk_dentries(struct f2fs_sb_info *sbi, int casefolded,
1426 struct child_info *child,
1427 u8 *bitmap, struct f2fs_dir_entry *dentry,
1428 __u8 (*filenames)[F2FS_SLOT_LEN],
1429 int max, int last_blk, int enc_name)
1431 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1432 enum FILE_TYPE ftype;
1436 char en[F2FS_PRINT_NAMELEN];
1442 /* readahead inode blocks */
1443 for (i = 0; i < max; i++) {
1446 if (test_bit_le(i, bitmap) == 0)
1449 ino = le32_to_cpu(dentry[i].ino);
1451 if (IS_VALID_NID(sbi, ino)) {
1452 struct node_info ni;
1454 get_node_info(sbi, ino, &ni);
1455 if (IS_VALID_BLK_ADDR(sbi, ni.blk_addr)) {
1456 dev_reada_block(ni.blk_addr);
1457 name_len = le16_to_cpu(dentry[i].name_len);
1459 i += (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN - 1;
1464 for (i = 0; i < max;) {
1465 if (test_bit_le(i, bitmap) == 0) {
1469 if (!IS_VALID_NID(sbi, le32_to_cpu(dentry[i].ino))) {
1470 ASSERT_MSG("Bad dentry 0x%x with invalid NID/ino 0x%x",
1471 i, le32_to_cpu(dentry[i].ino));
1473 FIX_MSG("Clear bad dentry 0x%x with bad ino 0x%x",
1474 i, le32_to_cpu(dentry[i].ino));
1475 test_and_clear_bit_le(i, bitmap);
1482 ftype = dentry[i].file_type;
1483 if ((ftype <= F2FS_FT_UNKNOWN || ftype > F2FS_FT_LAST_FILE_TYPE)) {
1484 ASSERT_MSG("Bad dentry 0x%x with unexpected ftype 0x%x",
1485 le32_to_cpu(dentry[i].ino), ftype);
1487 FIX_MSG("Clear bad dentry 0x%x with bad ftype 0x%x",
1489 test_and_clear_bit_le(i, bitmap);
1496 name_len = le16_to_cpu(dentry[i].name_len);
1498 if (name_len == 0 || name_len > F2FS_NAME_LEN) {
1499 ASSERT_MSG("Bad dentry 0x%x with invalid name_len", i);
1501 FIX_MSG("Clear bad dentry 0x%x", i);
1502 test_and_clear_bit_le(i, bitmap);
1508 name = calloc(name_len + 1, 1);
1511 memcpy(name, filenames[i], name_len);
1512 slots = (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN;
1514 /* Becareful. 'dentry.file_type' is not imode. */
1515 if (ftype == F2FS_FT_DIR) {
1516 if ((name[0] == '.' && name_len == 1) ||
1517 (name[0] == '.' && name[1] == '.' &&
1519 ret = __chk_dots_dentries(sbi, casefolded, &dentry[i],
1520 child, name, name_len, &filenames[i],
1530 if (child->dots > 2) {
1531 ASSERT_MSG("More than one '.' or '..', should delete the extra one\n");
1532 nullify_dentry(&dentry[i], i,
1533 &filenames[i], &bitmap);
1544 if (f2fs_check_hash_code(get_encoding(sbi), casefolded, dentry + i, name, name_len, enc_name))
1547 if (max == NR_DENTRY_IN_BLOCK) {
1548 ret = f2fs_check_dirent_position(get_encoding(sbi), casefolded,
1549 name, name_len, child->pgofs,
1550 child->dir_level, child->p_ino);
1553 FIX_MSG("Clear bad dentry 0x%x", i);
1554 test_and_clear_bit_le(i, bitmap);
1563 pretty_print_filename(name, name_len, en, enc_name);
1564 DBG(1, "[%3u]-[0x%x] name[%s] len[0x%x] ino[0x%x] type[0x%x]\n",
1565 fsck->dentry_depth, i, en, name_len,
1566 le32_to_cpu(dentry[i].ino),
1567 dentry[i].file_type);
1569 print_dentry(fsck->dentry_depth, name, bitmap,
1570 dentry, max, i, last_blk, enc_name);
1573 child->i_namelen = name_len;
1574 ret = fsck_chk_node_blk(sbi,
1575 NULL, le32_to_cpu(dentry[i].ino),
1576 ftype, TYPE_INODE, &blk_cnt, child);
1578 if (ret && c.fix_on) {
1581 for (j = 0; j < slots; j++)
1582 test_and_clear_bit_le(i + j, bitmap);
1583 FIX_MSG("Unlink [0x%x] - %s len[0x%x], type[0x%x]",
1584 le32_to_cpu(dentry[i].ino),
1586 dentry[i].file_type);
1588 } else if (ret == 0) {
1589 if (ftype == F2FS_FT_DIR)
1598 return fixed ? -1 : dentries;
1601 int fsck_chk_inline_dentries(struct f2fs_sb_info *sbi,
1602 struct f2fs_node *node_blk, struct child_info *child)
1604 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1605 struct f2fs_dentry_ptr d;
1606 void *inline_dentry;
1609 inline_dentry = inline_data_addr(node_blk);
1610 ASSERT(inline_dentry != NULL);
1612 make_dentry_ptr(&d, node_blk, inline_dentry, 2);
1614 fsck->dentry_depth++;
1615 dentries = __chk_dentries(sbi, IS_CASEFOLDED(&node_blk->i), child,
1616 d.bitmap, d.dentry, d.filename, d.max, 1,
1617 file_is_encrypt(&node_blk->i));// pass through
1619 DBG(1, "[%3d] Inline Dentry Block Fixed hash_codes\n\n",
1620 fsck->dentry_depth);
1622 DBG(1, "[%3d] Inline Dentry Block Done : "
1623 "dentries:%d in %d slots (len:%d)\n\n",
1624 fsck->dentry_depth, dentries,
1625 d.max, F2FS_NAME_LEN);
1627 fsck->dentry_depth--;
1631 int fsck_chk_dentry_blk(struct f2fs_sb_info *sbi, int casefolded, u32 blk_addr,
1632 struct child_info *child, int last_blk, int enc_name)
1634 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1635 struct f2fs_dentry_block *de_blk;
1638 de_blk = (struct f2fs_dentry_block *)calloc(BLOCK_SZ, 1);
1639 ASSERT(de_blk != NULL);
1641 ret = dev_read_block(de_blk, blk_addr);
1644 fsck->dentry_depth++;
1645 dentries = __chk_dentries(sbi, casefolded, child,
1646 de_blk->dentry_bitmap,
1647 de_blk->dentry, de_blk->filename,
1648 NR_DENTRY_IN_BLOCK, last_blk, enc_name);
1650 if (dentries < 0 && f2fs_dev_is_writable()) {
1651 ret = dev_write_block(de_blk, blk_addr);
1653 DBG(1, "[%3d] Dentry Block [0x%x] Fixed hash_codes\n\n",
1654 fsck->dentry_depth, blk_addr);
1656 DBG(1, "[%3d] Dentry Block [0x%x] Done : "
1657 "dentries:%d in %d slots (len:%d)\n\n",
1658 fsck->dentry_depth, blk_addr, dentries,
1659 NR_DENTRY_IN_BLOCK, F2FS_NAME_LEN);
1661 fsck->dentry_depth--;
1666 int fsck_chk_data_blk(struct f2fs_sb_info *sbi, int casefolded,
1667 u32 blk_addr, struct child_info *child, int last_blk,
1668 enum FILE_TYPE ftype, u32 parent_nid, u16 idx_in_node, u8 ver,
1671 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1673 /* Is it reserved block? */
1674 if (blk_addr == NEW_ADDR) {
1675 fsck->chk.valid_blk_cnt++;
1679 if (!IS_VALID_BLK_ADDR(sbi, blk_addr)) {
1680 ASSERT_MSG("blkaddress is not valid. [0x%x]", blk_addr);
1684 if (is_valid_ssa_data_blk(sbi, blk_addr, parent_nid,
1685 idx_in_node, ver)) {
1686 ASSERT_MSG("summary data block is not valid. [0x%x]",
1691 if (f2fs_test_sit_bitmap(sbi, blk_addr) == 0)
1692 ASSERT_MSG("SIT bitmap is 0x0. blk_addr[0x%x]", blk_addr);
1694 if (f2fs_test_main_bitmap(sbi, blk_addr) != 0)
1695 ASSERT_MSG("Duplicated data [0x%x]. pnid[0x%x] idx[0x%x]",
1696 blk_addr, parent_nid, idx_in_node);
1698 fsck->chk.valid_blk_cnt++;
1700 if (ftype == F2FS_FT_DIR) {
1701 f2fs_set_main_bitmap(sbi, blk_addr, CURSEG_HOT_DATA);
1702 return fsck_chk_dentry_blk(sbi, casefolded, blk_addr, child,
1703 last_blk, enc_name);
1705 f2fs_set_main_bitmap(sbi, blk_addr, CURSEG_WARM_DATA);
1710 int fsck_chk_orphan_node(struct f2fs_sb_info *sbi)
1713 block_t start_blk, orphan_blkaddr, i, j;
1714 struct f2fs_orphan_block *orphan_blk, *new_blk;
1715 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
1718 if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG))
1721 start_blk = __start_cp_addr(sbi) + 1 + get_sb(cp_payload);
1722 orphan_blkaddr = __start_sum_addr(sbi) - 1 - get_sb(cp_payload);
1724 f2fs_ra_meta_pages(sbi, start_blk, orphan_blkaddr, META_CP);
1726 orphan_blk = calloc(BLOCK_SZ, 1);
1729 new_blk = calloc(BLOCK_SZ, 1);
1732 for (i = 0; i < orphan_blkaddr; i++) {
1733 int ret = dev_read_block(orphan_blk, start_blk + i);
1734 u32 new_entry_count = 0;
1737 entry_count = le32_to_cpu(orphan_blk->entry_count);
1739 for (j = 0; j < entry_count; j++) {
1740 nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
1741 DBG(1, "[%3d] ino [0x%x]\n", i, ino);
1742 struct node_info ni;
1745 if (c.preen_mode == PREEN_MODE_1 && !c.fix_on) {
1746 get_node_info(sbi, ino, &ni);
1747 if (!IS_VALID_NID(sbi, ino) ||
1748 !IS_VALID_BLK_ADDR(sbi, ni.blk_addr))
1754 ret = fsck_chk_node_blk(sbi, NULL, ino,
1755 F2FS_FT_ORPHAN, TYPE_INODE, &blk_cnt,
1758 new_blk->ino[new_entry_count++] =
1760 else if (ret && c.fix_on)
1761 FIX_MSG("[0x%x] remove from orphan list", ino);
1763 ASSERT_MSG("[0x%x] wrong orphan inode", ino);
1765 if (f2fs_dev_is_writable() && c.fix_on &&
1766 entry_count != new_entry_count) {
1767 new_blk->entry_count = cpu_to_le32(new_entry_count);
1768 ret = dev_write_block(new_blk, start_blk + i);
1771 memset(orphan_blk, 0, BLOCK_SZ);
1772 memset(new_blk, 0, BLOCK_SZ);
1780 int fsck_chk_quota_node(struct f2fs_sb_info *sbi)
1782 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
1783 enum quota_type qtype;
1787 for (qtype = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
1788 if (sb->qf_ino[qtype] == 0)
1790 nid_t ino = QUOTA_INO(sb, qtype);
1791 struct node_info ni;
1793 DBG(1, "qtype [%d] ino [0x%x]\n", qtype, ino);
1796 if (c.preen_mode == PREEN_MODE_1 && !c.fix_on) {
1797 get_node_info(sbi, ino, &ni);
1798 if (!IS_VALID_NID(sbi, ino) ||
1799 !IS_VALID_BLK_ADDR(sbi, ni.blk_addr))
1803 ret = fsck_chk_node_blk(sbi, NULL, ino,
1804 F2FS_FT_REG_FILE, TYPE_INODE, &blk_cnt, NULL);
1806 ASSERT_MSG("wrong quota inode, qtype [%d] ino [0x%x]",
1812 int fsck_chk_quota_files(struct f2fs_sb_info *sbi)
1814 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1815 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
1816 enum quota_type qtype;
1821 /* Return if quota feature is disabled */
1825 for (qtype = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
1826 ino = sb->qf_ino[qtype];
1830 DBG(1, "Checking Quota file ([%3d] ino [0x%x])\n", qtype, ino);
1832 ret = quota_compare_and_update(sbi, qtype, &needs_writeout,
1834 if (ret == 0 && needs_writeout == 0) {
1839 /* Something is wrong */
1841 DBG(0, "Fixing Quota file ([%3d] ino [0x%x])\n",
1843 f2fs_filesize_update(sbi, ino, 0);
1844 ret = quota_write_inode(sbi, qtype);
1849 ASSERT_MSG("Unable to write quota file");
1852 ASSERT_MSG("Quota file is missing or invalid"
1853 " quota file content found.");
1859 int fsck_chk_meta(struct f2fs_sb_info *sbi)
1861 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1862 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
1863 struct seg_entry *se;
1864 unsigned int sit_valid_segs = 0, sit_node_blks = 0;
1867 /* 1. check sit usage with CP: curseg is lost? */
1868 for (i = 0; i < TOTAL_SEGS(sbi); i++) {
1869 se = get_seg_entry(sbi, i);
1870 if (se->valid_blocks != 0)
1872 else if (IS_CUR_SEGNO(sbi, i)) {
1873 /* curseg has not been written back to device */
1874 MSG(1, "\tInfo: curseg %u is counted in valid segs\n", i);
1877 if (IS_NODESEG(se->type))
1878 sit_node_blks += se->valid_blocks;
1880 if (fsck->chk.sit_free_segs + sit_valid_segs != TOTAL_SEGS(sbi)) {
1881 ASSERT_MSG("SIT usage does not match: sit_free_segs %u, "
1882 "sit_valid_segs %u, total_segs %u",
1883 fsck->chk.sit_free_segs, sit_valid_segs,
1888 /* 2. check node count */
1889 if (fsck->chk.valid_nat_entry_cnt != sit_node_blks) {
1890 ASSERT_MSG("node count does not match: valid_nat_entry_cnt %u,"
1891 " sit_node_blks %u",
1892 fsck->chk.valid_nat_entry_cnt, sit_node_blks);
1896 /* 3. check SIT with CP */
1897 if (fsck->chk.sit_free_segs != le32_to_cpu(cp->free_segment_count)) {
1898 ASSERT_MSG("free segs does not match: sit_free_segs %u, "
1899 "free_segment_count %u",
1900 fsck->chk.sit_free_segs,
1901 le32_to_cpu(cp->free_segment_count));
1905 /* 4. check NAT with CP */
1906 if (fsck->chk.valid_nat_entry_cnt !=
1907 le32_to_cpu(cp->valid_node_count)) {
1908 ASSERT_MSG("valid node does not match: valid_nat_entry_cnt %u,"
1909 " valid_node_count %u",
1910 fsck->chk.valid_nat_entry_cnt,
1911 le32_to_cpu(cp->valid_node_count));
1915 /* 4. check orphan inode simply */
1916 if (fsck_chk_orphan_node(sbi))
1919 /* 5. check nat entry -- must be done before quota check */
1920 for (i = 0; i < fsck->nr_nat_entries; i++) {
1921 u32 blk = le32_to_cpu(fsck->entries[i].block_addr);
1922 nid_t ino = le32_to_cpu(fsck->entries[i].ino);
1926 * skip entry whose ino is 0, otherwise, we will
1927 * get a negative number by BLKOFF_FROM_MAIN(sbi, blk)
1931 if (!IS_VALID_BLK_ADDR(sbi, blk)) {
1932 MSG(0, "\tError: nat entry[ino %u block_addr 0x%x]"
1938 if (!f2fs_test_sit_bitmap(sbi, blk)) {
1939 MSG(0, "\tError: nat entry[ino %u block_addr 0x%x]"
1940 " not find it in sit_area_bitmap\n",
1945 if (!IS_VALID_NID(sbi, ino)) {
1946 MSG(0, "\tError: nat_entry->ino %u exceeds the range"
1947 " of nat entries %u\n",
1948 ino, fsck->nr_nat_entries);
1952 if (!f2fs_test_bit(ino, fsck->nat_area_bitmap)) {
1953 MSG(0, "\tError: nat_entry->ino %u is not set in"
1954 " nat_area_bitmap\n", ino);
1959 /* 6. check quota inode simply */
1960 if (fsck_chk_quota_node(sbi))
1963 if (fsck->nat_valid_inode_cnt != le32_to_cpu(cp->valid_inode_count)) {
1964 ASSERT_MSG("valid inode does not match: nat_valid_inode_cnt %u,"
1965 " valid_inode_count %u",
1966 fsck->nat_valid_inode_cnt,
1967 le32_to_cpu(cp->valid_inode_count));
1974 void fsck_chk_checkpoint(struct f2fs_sb_info *sbi)
1976 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
1978 if (get_cp(ckpt_flags) & CP_LARGE_NAT_BITMAP_FLAG) {
1979 if (get_cp(checksum_offset) != CP_MIN_CHKSUM_OFFSET) {
1980 ASSERT_MSG("Deprecated layout of large_nat_bitmap, "
1981 "chksum_offset:%u", get_cp(checksum_offset));
1987 void fsck_init(struct f2fs_sb_info *sbi)
1989 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1990 struct f2fs_sm_info *sm_i = SM_I(sbi);
1993 * We build three bitmap for main/sit/nat so that may check consistency
1995 * 1. main_area_bitmap will be used to check whether all blocks of main
1996 * area is used or not.
1997 * 2. nat_area_bitmap has bitmap information of used nid in NAT.
1998 * 3. sit_area_bitmap has bitmap information of used main block.
1999 * At Last sequence, we compare main_area_bitmap with sit_area_bitmap.
2001 fsck->nr_main_blks = sm_i->main_segments << sbi->log_blocks_per_seg;
2002 fsck->main_area_bitmap_sz = (fsck->nr_main_blks + 7) / 8;
2003 fsck->main_area_bitmap = calloc(fsck->main_area_bitmap_sz, 1);
2004 ASSERT(fsck->main_area_bitmap != NULL);
2006 build_nat_area_bitmap(sbi);
2008 build_sit_area_bitmap(sbi);
2010 ASSERT(tree_mark_size != 0);
2011 tree_mark = calloc(tree_mark_size, 1);
2012 ASSERT(tree_mark != NULL);
2015 static void fix_hard_links(struct f2fs_sb_info *sbi)
2017 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2018 struct hard_link_node *tmp, *node;
2019 struct f2fs_node *node_blk = NULL;
2020 struct node_info ni;
2023 if (fsck->hard_link_list_head == NULL)
2026 node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
2027 ASSERT(node_blk != NULL);
2029 node = fsck->hard_link_list_head;
2032 if (sanity_check_nid(sbi, node->nid, node_blk,
2033 F2FS_FT_MAX, TYPE_INODE, &ni))
2034 FIX_MSG("Failed to fix, rerun fsck.f2fs");
2036 node_blk->i.i_links = cpu_to_le32(node->actual_links);
2038 FIX_MSG("File: 0x%x i_links= 0x%x -> 0x%x",
2039 node->nid, node->links, node->actual_links);
2041 ret = dev_write_block(node_blk, ni.blk_addr);
2050 static void fix_nat_entries(struct f2fs_sb_info *sbi)
2052 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2055 for (i = 0; i < fsck->nr_nat_entries; i++)
2056 if (f2fs_test_bit(i, fsck->nat_area_bitmap) != 0)
2057 nullify_nat_entry(sbi, i);
2060 static void flush_curseg_sit_entries(struct f2fs_sb_info *sbi)
2062 struct sit_info *sit_i = SIT_I(sbi);
2063 struct f2fs_sit_block *sit_blk;
2066 sit_blk = calloc(BLOCK_SZ, 1);
2068 /* update curseg sit entries, since we may change
2069 * a segment type in move_curseg_info
2071 for (i = 0; i < NO_CHECK_TYPE; i++) {
2072 struct curseg_info *curseg = CURSEG_I(sbi, i);
2073 struct f2fs_sit_entry *sit;
2074 struct seg_entry *se;
2076 se = get_seg_entry(sbi, curseg->segno);
2077 get_current_sit_page(sbi, curseg->segno, sit_blk);
2078 sit = &sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, curseg->segno)];
2079 sit->vblocks = cpu_to_le16((se->type << SIT_VBLOCKS_SHIFT) |
2081 rewrite_current_sit_page(sbi, curseg->segno, sit_blk);
2087 static void fix_checksum(struct f2fs_sb_info *sbi)
2089 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
2090 struct f2fs_nm_info *nm_i = NM_I(sbi);
2091 struct sit_info *sit_i = SIT_I(sbi);
2092 void *bitmap_offset;
2097 bitmap_offset = cp->sit_nat_version_bitmap + sizeof(__le32);
2099 memcpy(bitmap_offset, nm_i->nat_bitmap, nm_i->bitmap_size);
2100 memcpy(bitmap_offset + nm_i->bitmap_size,
2101 sit_i->sit_bitmap, sit_i->bitmap_size);
2104 static void fix_checkpoint(struct f2fs_sb_info *sbi)
2106 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2107 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
2108 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
2109 unsigned long long cp_blk_no;
2110 u32 flags = c.alloc_failed ? CP_FSCK_FLAG: CP_UMOUNT_FLAG;
2111 block_t orphan_blks = 0;
2117 /* should call from fsck */
2118 ASSERT(c.func == FSCK);
2120 if (is_set_ckpt_flags(cp, CP_ORPHAN_PRESENT_FLAG)) {
2121 orphan_blks = __start_sum_addr(sbi) - 1;
2122 flags |= CP_ORPHAN_PRESENT_FLAG;
2124 if (is_set_ckpt_flags(cp, CP_TRIMMED_FLAG))
2125 flags |= CP_TRIMMED_FLAG;
2126 if (is_set_ckpt_flags(cp, CP_DISABLED_FLAG))
2127 flags |= CP_DISABLED_FLAG;
2128 if (is_set_ckpt_flags(cp, CP_LARGE_NAT_BITMAP_FLAG)) {
2129 flags |= CP_LARGE_NAT_BITMAP_FLAG;
2130 set_cp(checksum_offset, CP_MIN_CHKSUM_OFFSET);
2132 set_cp(checksum_offset, CP_CHKSUM_OFFSET);
2135 if (flags & CP_UMOUNT_FLAG)
2140 set_cp(cp_pack_total_block_count, cp_blocks +
2141 orphan_blks + get_sb(cp_payload));
2143 flags = update_nat_bits_flags(sb, cp, flags);
2144 flags |= CP_NOCRC_RECOVERY_FLAG;
2145 set_cp(ckpt_flags, flags);
2147 set_cp(free_segment_count, get_free_segments(sbi));
2148 set_cp(valid_block_count, fsck->chk.valid_blk_cnt);
2149 set_cp(valid_node_count, fsck->chk.valid_node_cnt);
2150 set_cp(valid_inode_count, fsck->chk.valid_inode_cnt);
2152 crc = f2fs_checkpoint_chksum(cp);
2153 *((__le32 *)((unsigned char *)cp + get_cp(checksum_offset))) =
2156 cp_blk_no = get_sb(cp_blkaddr);
2157 if (sbi->cur_cp == 2)
2158 cp_blk_no += 1 << get_sb(log_blocks_per_seg);
2160 ret = dev_write_block(cp, cp_blk_no++);
2163 for (i = 0; i < get_sb(cp_payload); i++) {
2164 ret = dev_write_block(((unsigned char *)cp) +
2165 (i + 1) * F2FS_BLKSIZE, cp_blk_no++);
2169 cp_blk_no += orphan_blks;
2171 for (i = 0; i < NO_CHECK_TYPE; i++) {
2172 struct curseg_info *curseg = CURSEG_I(sbi, i);
2174 if (!(flags & CP_UMOUNT_FLAG) && IS_NODESEG(i))
2177 ret = dev_write_block(curseg->sum_blk, cp_blk_no++);
2181 /* Write nat bits */
2182 if (flags & CP_NAT_BITS_FLAG)
2183 write_nat_bits(sbi, sb, cp, sbi->cur_cp);
2185 ret = f2fs_fsync_device();
2188 ret = dev_write_block(cp, cp_blk_no++);
2191 ret = f2fs_fsync_device();
2195 static void fix_checkpoints(struct f2fs_sb_info *sbi)
2197 /* copy valid checkpoint to its mirror position */
2198 duplicate_checkpoint(sbi);
2200 /* repair checkpoint at CP #0 position */
2202 fix_checkpoint(sbi);
2205 #ifdef HAVE_LINUX_BLKZONED_H
2208 * Refer valid block map and return offset of the last valid block in the zone.
2209 * Obtain valid block map from SIT and fsync data.
2210 * If there is no valid block in the zone, return -1.
2212 static int last_vblk_off_in_zone(struct f2fs_sb_info *sbi,
2213 unsigned int zone_segno)
2216 unsigned int segs_per_zone = sbi->segs_per_sec * sbi->secs_per_zone;
2217 struct seg_entry *se;
2219 for (s = segs_per_zone - 1; s >= 0; s--) {
2220 se = get_seg_entry(sbi, zone_segno + s);
2223 * Refer not cur_valid_map but ckpt_valid_map which reflects
2226 ASSERT(se->ckpt_valid_map);
2227 for (b = sbi->blocks_per_seg - 1; b >= 0; b--)
2228 if (f2fs_test_bit(b, (const char*)se->ckpt_valid_map))
2229 return b + (s << sbi->log_blocks_per_seg);
2235 static int check_curseg_write_pointer(struct f2fs_sb_info *sbi, int type)
2237 struct curseg_info *curseg = CURSEG_I(sbi, type);
2238 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2239 struct blk_zone blkz;
2240 block_t cs_block, wp_block, zone_last_vblock;
2241 u_int64_t cs_sector, wp_sector;
2243 unsigned int zone_segno;
2244 int log_sectors_per_block = sbi->log_blocksize - SECTOR_SHIFT;
2246 /* get the device the curseg points to */
2247 cs_block = START_BLOCK(sbi, curseg->segno) + curseg->next_blkoff;
2248 for (i = 0; i < MAX_DEVICES; i++) {
2249 if (!c.devices[i].path)
2251 if (c.devices[i].start_blkaddr <= cs_block &&
2252 cs_block <= c.devices[i].end_blkaddr)
2256 if (i >= MAX_DEVICES)
2259 /* get write pointer position of the zone the curseg points to */
2260 cs_sector = (cs_block - c.devices[i].start_blkaddr)
2261 << log_sectors_per_block;
2262 ret = f2fs_report_zone(i, cs_sector, &blkz);
2266 if (blk_zone_type(&blkz) != BLK_ZONE_TYPE_SEQWRITE_REQ)
2269 /* check consistency between the curseg and the write pointer */
2270 wp_block = c.devices[i].start_blkaddr +
2271 (blk_zone_wp_sector(&blkz) >> log_sectors_per_block);
2272 wp_sector = blk_zone_wp_sector(&blkz);
2274 if (cs_sector == wp_sector)
2277 if (cs_sector > wp_sector) {
2278 MSG(0, "Inconsistent write pointer with curseg %d: "
2279 "curseg %d[0x%x,0x%x] > wp[0x%x,0x%x]\n",
2280 type, type, curseg->segno, curseg->next_blkoff,
2281 GET_SEGNO(sbi, wp_block), OFFSET_IN_SEG(sbi, wp_block));
2282 fsck->chk.wp_inconsistent_zones++;
2286 MSG(0, "Write pointer goes advance from 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));
2291 zone_segno = GET_SEG_FROM_SEC(sbi,
2292 GET_SEC_FROM_SEG(sbi, curseg->segno));
2293 zone_last_vblock = START_BLOCK(sbi, zone_segno) +
2294 last_vblk_off_in_zone(sbi, zone_segno);
2297 * If valid blocks exist between the curseg position and the write
2298 * pointer, they are fsync data. This is not an error to fix. Leave it
2299 * for kernel to recover later.
2300 * If valid blocks exist between the curseg's zone start and the curseg
2301 * position, or if there is no valid block in the curseg's zone, fix
2302 * the inconsistency between the curseg and the writ pointer.
2303 * Of Note is that if there is no valid block in the curseg's zone,
2304 * last_vblk_off_in_zone() returns -1 and zone_last_vblock is always
2305 * smaller than cs_block.
2307 if (cs_block <= zone_last_vblock && zone_last_vblock < wp_block) {
2308 MSG(0, "Curseg has fsync data: curseg %d[0x%x,0x%x] "
2309 "last valid block in zone[0x%x,0x%x]\n",
2310 type, curseg->segno, curseg->next_blkoff,
2311 GET_SEGNO(sbi, zone_last_vblock),
2312 OFFSET_IN_SEG(sbi, zone_last_vblock));
2316 fsck->chk.wp_inconsistent_zones++;
2322 static int check_curseg_write_pointer(struct f2fs_sb_info *UNUSED(sbi),
2330 int check_curseg_offset(struct f2fs_sb_info *sbi, int type)
2332 struct curseg_info *curseg = CURSEG_I(sbi, type);
2333 struct seg_entry *se;
2336 if ((curseg->next_blkoff >> 3) >= SIT_VBLOCK_MAP_SIZE) {
2337 ASSERT_MSG("Next block offset:%u is invalid, type:%d",
2338 curseg->next_blkoff, type);
2341 se = get_seg_entry(sbi, curseg->segno);
2342 if (f2fs_test_bit(curseg->next_blkoff,
2343 (const char *)se->cur_valid_map)) {
2344 ASSERT_MSG("Next block offset is not free, type:%d", type);
2347 if (curseg->alloc_type == SSR)
2350 nblocks = sbi->blocks_per_seg;
2351 for (j = curseg->next_blkoff + 1; j < nblocks; j++) {
2352 if (f2fs_test_bit(j, (const char *)se->cur_valid_map)) {
2353 ASSERT_MSG("For LFS curseg, space after .next_blkoff "
2354 "should be unused, type:%d", type);
2359 if (c.zoned_model == F2FS_ZONED_HM)
2360 return check_curseg_write_pointer(sbi, type);
2365 int check_curseg_offsets(struct f2fs_sb_info *sbi)
2369 for (i = 0; i < NO_CHECK_TYPE; i++) {
2370 ret = check_curseg_offset(sbi, i);
2377 static void fix_curseg_info(struct f2fs_sb_info *sbi)
2379 int i, need_update = 0;
2381 for (i = 0; i < NO_CHECK_TYPE; i++) {
2382 if (check_curseg_offset(sbi, i)) {
2383 update_curseg_info(sbi, i);
2389 write_curseg_info(sbi);
2390 flush_curseg_sit_entries(sbi);
2394 int check_sit_types(struct f2fs_sb_info *sbi)
2399 for (i = 0; i < TOTAL_SEGS(sbi); i++) {
2400 struct seg_entry *se;
2402 se = get_seg_entry(sbi, i);
2403 if (se->orig_type != se->type) {
2404 if (se->orig_type == CURSEG_COLD_DATA &&
2405 se->type <= CURSEG_COLD_DATA) {
2406 se->type = se->orig_type;
2408 FIX_MSG("Wrong segment type [0x%x] %x -> %x",
2409 i, se->orig_type, se->type);
2417 static struct f2fs_node *fsck_get_lpf(struct f2fs_sb_info *sbi)
2419 struct f2fs_node *node;
2420 struct node_info ni;
2424 /* read root inode first */
2425 node = calloc(F2FS_BLKSIZE, 1);
2427 get_node_info(sbi, F2FS_ROOT_INO(sbi), &ni);
2428 err = dev_read_block(node, ni.blk_addr);
2431 /* lookup lost+found in root directory */
2432 lpf_ino = f2fs_lookup(sbi, node, (u8 *)LPF, strlen(LPF));
2433 if (lpf_ino) { /* found */
2434 get_node_info(sbi, lpf_ino, &ni);
2435 err = dev_read_block(node, ni.blk_addr);
2437 DBG(1, "Found lost+found 0x%x at blkaddr [0x%x]\n",
2438 lpf_ino, ni.blk_addr);
2439 if (!S_ISDIR(le16_to_cpu(node->i.i_mode))) {
2440 ASSERT_MSG("lost+found is not directory [0%o]\n",
2441 le16_to_cpu(node->i.i_mode));
2442 /* FIXME: give up? */
2445 } else { /* not found, create it */
2448 memset(&de, 0, sizeof(de));
2449 de.name = (u8 *) LPF;
2450 de.len = strlen(LPF);
2452 de.pino = F2FS_ROOT_INO(sbi),
2453 de.file_type = F2FS_FT_DIR,
2456 de.mtime = time(NULL);
2458 err = f2fs_mkdir(sbi, &de);
2460 ASSERT_MSG("Failed create lost+found");
2464 get_node_info(sbi, de.ino, &ni);
2465 err = dev_read_block(node, ni.blk_addr);
2467 DBG(1, "Create lost+found 0x%x at blkaddr [0x%x]\n",
2468 de.ino, ni.blk_addr);
2471 c.lpf_ino = le32_to_cpu(node->footer.ino);
2478 static int fsck_do_reconnect_file(struct f2fs_sb_info *sbi,
2479 struct f2fs_node *lpf,
2480 struct f2fs_node *fnode)
2484 nid_t ino = le32_to_cpu(fnode->footer.ino);
2485 struct node_info ni;
2488 namelen = snprintf(name, 80, "%u", ino);
2490 /* ignore terminating '\0', should never happen */
2493 if (f2fs_lookup(sbi, lpf, (u8 *)name, namelen)) {
2494 ASSERT_MSG("Name %s already exist in lost+found", name);
2498 get_node_info(sbi, le32_to_cpu(lpf->footer.ino), &ni);
2499 ftype = map_de_type(le16_to_cpu(fnode->i.i_mode));
2500 ret = f2fs_add_link(sbi, lpf, (unsigned char *)name, namelen,
2501 ino, ftype, ni.blk_addr, 0);
2503 ASSERT_MSG("Failed to add inode [0x%x] to lost+found", ino);
2508 memcpy(fnode->i.i_name, name, namelen);
2509 fnode->i.i_namelen = cpu_to_le32(namelen);
2510 fnode->i.i_pino = c.lpf_ino;
2511 get_node_info(sbi, le32_to_cpu(fnode->footer.ino), &ni);
2512 ret = dev_write_block(fnode, ni.blk_addr);
2515 DBG(1, "Reconnect inode [0x%x] to lost+found\n", ino);
2519 static void fsck_failed_reconnect_file_dnode(struct f2fs_sb_info *sbi,
2522 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2523 struct f2fs_node *node;
2524 struct node_info ni;
2528 node = calloc(F2FS_BLKSIZE, 1);
2531 get_node_info(sbi, nid, &ni);
2532 err = dev_read_block(node, ni.blk_addr);
2535 fsck->chk.valid_node_cnt--;
2536 fsck->chk.valid_blk_cnt--;
2537 f2fs_clear_main_bitmap(sbi, ni.blk_addr);
2539 for (i = 0; i < ADDRS_PER_BLOCK(&node->i); i++) {
2540 addr = le32_to_cpu(node->dn.addr[i]);
2543 fsck->chk.valid_blk_cnt--;
2544 if (addr == NEW_ADDR)
2546 f2fs_clear_main_bitmap(sbi, addr);
2552 static void fsck_failed_reconnect_file_idnode(struct f2fs_sb_info *sbi,
2555 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2556 struct f2fs_node *node;
2557 struct node_info ni;
2561 node = calloc(F2FS_BLKSIZE, 1);
2564 get_node_info(sbi, nid, &ni);
2565 err = dev_read_block(node, ni.blk_addr);
2568 fsck->chk.valid_node_cnt--;
2569 fsck->chk.valid_blk_cnt--;
2570 f2fs_clear_main_bitmap(sbi, ni.blk_addr);
2572 for (i = 0; i < NIDS_PER_BLOCK; i++) {
2573 tmp = le32_to_cpu(node->in.nid[i]);
2576 fsck_failed_reconnect_file_dnode(sbi, tmp);
2582 static void fsck_failed_reconnect_file_didnode(struct f2fs_sb_info *sbi,
2585 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2586 struct f2fs_node *node;
2587 struct node_info ni;
2591 node = calloc(F2FS_BLKSIZE, 1);
2594 get_node_info(sbi, nid, &ni);
2595 err = dev_read_block(node, ni.blk_addr);
2598 fsck->chk.valid_node_cnt--;
2599 fsck->chk.valid_blk_cnt--;
2600 f2fs_clear_main_bitmap(sbi, ni.blk_addr);
2602 for (i = 0; i < NIDS_PER_BLOCK; i++) {
2603 tmp = le32_to_cpu(node->in.nid[i]);
2606 fsck_failed_reconnect_file_idnode(sbi, tmp);
2613 * Counters and main_area_bitmap are already changed during checking
2614 * inode block, so clear them. There is no need to clear new blocks
2615 * allocted to lost+found.
2617 static void fsck_failed_reconnect_file(struct f2fs_sb_info *sbi, nid_t ino)
2619 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2620 struct f2fs_node *node;
2621 struct node_info ni;
2625 node = calloc(F2FS_BLKSIZE, 1);
2628 get_node_info(sbi, ino, &ni);
2629 err = dev_read_block(node, ni.blk_addr);
2632 /* clear inode counters */
2633 fsck->chk.valid_inode_cnt--;
2634 fsck->chk.valid_node_cnt--;
2635 fsck->chk.valid_blk_cnt--;
2636 f2fs_clear_main_bitmap(sbi, ni.blk_addr);
2638 /* clear xnid counters */
2639 if (node->i.i_xattr_nid) {
2640 nid = le32_to_cpu(node->i.i_xattr_nid);
2641 fsck->chk.valid_node_cnt--;
2642 fsck->chk.valid_blk_cnt--;
2643 get_node_info(sbi, nid, &ni);
2644 f2fs_clear_main_bitmap(sbi, ni.blk_addr);
2647 /* clear data counters */
2648 if(!(node->i.i_inline & F2FS_INLINE_DATA)) {
2649 ofs = get_extra_isize(node);
2650 for (i = 0; i < ADDRS_PER_INODE(&node->i); i++) {
2651 block_t addr = le32_to_cpu(node->i.i_addr[ofs + i]);
2654 fsck->chk.valid_blk_cnt--;
2655 if (addr == NEW_ADDR)
2657 f2fs_clear_main_bitmap(sbi, addr);
2661 for (i = 0; i < 5; i++) {
2662 nid = le32_to_cpu(node->i.i_nid[i]);
2667 case 0: /* direct node */
2669 fsck_failed_reconnect_file_dnode(sbi, nid);
2671 case 2: /* indirect node */
2673 fsck_failed_reconnect_file_idnode(sbi, nid);
2675 case 4: /* double indirect node */
2676 fsck_failed_reconnect_file_didnode(sbi, nid);
2685 * Scan unreachable nids and find only regular file inodes. If these files
2686 * are not corrupted, reconnect them to lost+found.
2688 * Since all unreachable nodes are already checked, we can allocate new
2691 * This function returns the number of files been reconnected.
2693 static int fsck_reconnect_file(struct f2fs_sb_info *sbi)
2695 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2696 struct f2fs_node *lpf_node, *node;
2697 struct node_info ni;
2698 char *reconnect_bitmap;
2701 int err, cnt = 0, ftype;
2703 node = calloc(F2FS_BLKSIZE, 1);
2706 reconnect_bitmap = calloc(fsck->nat_area_bitmap_sz, 1);
2707 ASSERT(reconnect_bitmap);
2709 for (nid = 0; nid < fsck->nr_nat_entries; nid++) {
2710 if (f2fs_test_bit(nid, fsck->nat_area_bitmap)) {
2711 if (is_qf_ino(F2FS_RAW_SUPER(sbi), nid)) {
2712 DBG(1, "Not support quota inode [0x%x]\n",
2717 get_node_info(sbi, nid, &ni);
2718 err = dev_read_block(node, ni.blk_addr);
2721 /* reconnection will restore these nodes if needed */
2722 if (node->footer.ino != node->footer.nid) {
2723 DBG(1, "Not support non-inode node [0x%x]\n",
2728 if (S_ISDIR(le16_to_cpu(node->i.i_mode))) {
2729 DBG(1, "Not support directory inode [0x%x]\n",
2734 ftype = map_de_type(le16_to_cpu(node->i.i_mode));
2735 if (sanity_check_nid(sbi, nid, node, ftype,
2737 ASSERT_MSG("Invalid nid [0x%x]\n", nid);
2741 DBG(1, "Check inode 0x%x\n", nid);
2743 fsck_chk_inode_blk(sbi, nid, ftype, node,
2744 &blk_cnt, &ni, NULL);
2746 f2fs_set_bit(nid, reconnect_bitmap);
2750 lpf_node = fsck_get_lpf(sbi);
2754 for (nid = 0; nid < fsck->nr_nat_entries; nid++) {
2755 if (f2fs_test_bit(nid, reconnect_bitmap)) {
2756 get_node_info(sbi, nid, &ni);
2757 err = dev_read_block(node, ni.blk_addr);
2760 if (fsck_do_reconnect_file(sbi, lpf_node, node)) {
2761 DBG(1, "Failed to reconnect inode [0x%x]\n",
2763 fsck_failed_reconnect_file(sbi, nid);
2767 quota_add_inode_usage(fsck->qctx, nid, &node->i);
2769 DBG(1, "Reconnected inode [0x%x] to lost+found\n", nid);
2777 free(reconnect_bitmap);
2781 #ifdef HAVE_LINUX_BLKZONED_H
2783 struct write_pointer_check_data {
2784 struct f2fs_sb_info *sbi;
2788 static int chk_and_fix_wp_with_sit(int i, void *blkzone, void *opaque)
2790 struct blk_zone *blkz = (struct blk_zone *)blkzone;
2791 struct write_pointer_check_data *wpd = opaque;
2792 struct f2fs_sb_info *sbi = wpd->sbi;
2793 struct device_info *dev = c.devices + wpd->dev_index;
2794 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2795 block_t zone_block, wp_block, wp_blkoff;
2796 unsigned int zone_segno, wp_segno;
2797 struct curseg_info *cs;
2798 int cs_index, ret, last_valid_blkoff;
2799 int log_sectors_per_block = sbi->log_blocksize - SECTOR_SHIFT;
2800 unsigned int segs_per_zone = sbi->segs_per_sec * sbi->secs_per_zone;
2802 if (blk_zone_conv(blkz))
2805 zone_block = dev->start_blkaddr
2806 + (blk_zone_sector(blkz) >> log_sectors_per_block);
2807 zone_segno = GET_SEGNO(sbi, zone_block);
2808 if (zone_segno >= MAIN_SEGS(sbi))
2811 wp_block = dev->start_blkaddr
2812 + (blk_zone_wp_sector(blkz) >> log_sectors_per_block);
2813 wp_segno = GET_SEGNO(sbi, wp_block);
2814 wp_blkoff = wp_block - START_BLOCK(sbi, wp_segno);
2816 /* if a curseg points to the zone, skip the check */
2817 for (cs_index = 0; cs_index < NO_CHECK_TYPE; cs_index++) {
2818 cs = &SM_I(sbi)->curseg_array[cs_index];
2819 if (zone_segno <= cs->segno &&
2820 cs->segno < zone_segno + segs_per_zone)
2824 last_valid_blkoff = last_vblk_off_in_zone(sbi, zone_segno);
2827 * When there is no valid block in the zone, check write pointer is
2828 * at zone start. If not, reset the write pointer.
2830 if (last_valid_blkoff < 0 &&
2831 blk_zone_wp_sector(blkz) != blk_zone_sector(blkz)) {
2833 MSG(0, "Inconsistent write pointer: wp[0x%x,0x%x]\n",
2834 wp_segno, wp_blkoff);
2835 fsck->chk.wp_inconsistent_zones++;
2839 FIX_MSG("Reset write pointer of zone at segment 0x%x",
2841 ret = f2fs_reset_zone(wpd->dev_index, blkz);
2843 printf("[FSCK] Write pointer reset failed: %s\n",
2847 fsck->chk.wp_fixed = 1;
2852 * If valid blocks exist in the zone beyond the write pointer, it
2853 * is a bug. No need to fix because the zone is not selected for the
2854 * write. Just report it.
2856 if (last_valid_blkoff + zone_block > wp_block) {
2857 MSG(0, "Unexpected invalid write pointer: wp[0x%x,0x%x]\n",
2858 wp_segno, wp_blkoff);
2865 static void fix_wp_sit_alignment(struct f2fs_sb_info *sbi)
2868 struct write_pointer_check_data wpd = { sbi, 0 };
2870 if (c.zoned_model != F2FS_ZONED_HM)
2873 for (i = 0; i < MAX_DEVICES; i++) {
2874 if (!c.devices[i].path)
2876 if (c.devices[i].zoned_model != F2FS_ZONED_HM)
2880 if (f2fs_report_zones(i, chk_and_fix_wp_with_sit, &wpd)) {
2881 printf("[FSCK] Write pointer check failed: %s\n",
2890 static void fix_wp_sit_alignment(struct f2fs_sb_info *UNUSED(sbi))
2898 * Check and fix consistency with write pointers at the beginning of
2899 * fsck so that following writes by fsck do not fail.
2901 void fsck_chk_and_fix_write_pointers(struct f2fs_sb_info *sbi)
2903 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2905 if (c.zoned_model != F2FS_ZONED_HM)
2908 if (check_curseg_offsets(sbi) && c.fix_on) {
2909 fix_curseg_info(sbi);
2910 fsck->chk.wp_fixed = 1;
2913 fix_wp_sit_alignment(sbi);
2916 int fsck_chk_curseg_info(struct f2fs_sb_info *sbi)
2918 struct curseg_info *curseg;
2919 struct seg_entry *se;
2920 struct f2fs_summary_block *sum_blk;
2923 for (i = 0; i < NO_CHECK_TYPE; i++) {
2924 curseg = CURSEG_I(sbi, i);
2925 se = get_seg_entry(sbi, curseg->segno);
2926 sum_blk = curseg->sum_blk;
2928 if (se->type != i) {
2929 ASSERT_MSG("Incorrect curseg [%d]: segno [0x%x] "
2930 "type(SIT) [%d]", i, curseg->segno,
2932 if (c.fix_on || c.preen_mode)
2936 if (i <= CURSEG_COLD_DATA && IS_SUM_DATA_SEG(sum_blk->footer)) {
2938 } else if (i > CURSEG_COLD_DATA && IS_SUM_NODE_SEG(sum_blk->footer)) {
2941 ASSERT_MSG("Incorrect curseg [%d]: segno [0x%x] "
2942 "type(SSA) [%d]", i, curseg->segno,
2943 sum_blk->footer.entry_type);
2944 if (c.fix_on || c.preen_mode)
2945 sum_blk->footer.entry_type =
2946 i <= CURSEG_COLD_DATA ?
2947 SUM_TYPE_DATA : SUM_TYPE_NODE;
2955 int fsck_verify(struct f2fs_sb_info *sbi)
2960 u32 nr_unref_nid = 0;
2961 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2962 struct hard_link_node *node = NULL;
2966 if (c.zoned_model == F2FS_ZONED_HM) {
2967 printf("[FSCK] Write pointers consistency ");
2968 if (fsck->chk.wp_inconsistent_zones == 0x0) {
2969 printf(" [Ok..]\n");
2971 printf(" [Fail] [0x%x]\n",
2972 fsck->chk.wp_inconsistent_zones);
2976 if (fsck->chk.wp_fixed && c.fix_on)
2980 if (c.feature & cpu_to_le32(F2FS_FEATURE_LOST_FOUND)) {
2981 for (i = 0; i < fsck->nr_nat_entries; i++)
2982 if (f2fs_test_bit(i, fsck->nat_area_bitmap) != 0)
2984 if (i < fsck->nr_nat_entries) {
2985 i = fsck_reconnect_file(sbi);
2986 printf("[FSCK] Reconnect %u files to lost+found\n", i);
2990 for (i = 0; i < fsck->nr_nat_entries; i++) {
2991 if (f2fs_test_bit(i, fsck->nat_area_bitmap) != 0) {
2992 struct node_info ni;
2994 get_node_info(sbi, i, &ni);
2995 printf("NID[0x%x] is unreachable, blkaddr:0x%x\n",
3001 if (fsck->hard_link_list_head != NULL) {
3002 node = fsck->hard_link_list_head;
3004 printf("NID[0x%x] has [0x%x] more unreachable links\n",
3005 node->nid, node->links);
3011 printf("[FSCK] Unreachable nat entries ");
3012 if (nr_unref_nid == 0x0) {
3013 printf(" [Ok..] [0x%x]\n", nr_unref_nid);
3015 printf(" [Fail] [0x%x]\n", nr_unref_nid);
3016 ret = EXIT_ERR_CODE;
3020 printf("[FSCK] SIT valid block bitmap checking ");
3021 if (memcmp(fsck->sit_area_bitmap, fsck->main_area_bitmap,
3022 fsck->sit_area_bitmap_sz) == 0x0) {
3026 ret = EXIT_ERR_CODE;
3030 printf("[FSCK] Hard link checking for regular file ");
3031 if (fsck->hard_link_list_head == NULL) {
3032 printf(" [Ok..] [0x%x]\n", fsck->chk.multi_hard_link_files);
3034 printf(" [Fail] [0x%x]\n", fsck->chk.multi_hard_link_files);
3035 ret = EXIT_ERR_CODE;
3039 printf("[FSCK] valid_block_count matching with CP ");
3040 if (sbi->total_valid_block_count == fsck->chk.valid_blk_cnt) {
3041 printf(" [Ok..] [0x%x]\n", (u32)fsck->chk.valid_blk_cnt);
3043 printf(" [Fail] [0x%x]\n", (u32)fsck->chk.valid_blk_cnt);
3044 ret = EXIT_ERR_CODE;
3048 printf("[FSCK] valid_node_count matching with CP (de lookup) ");
3049 if (sbi->total_valid_node_count == fsck->chk.valid_node_cnt) {
3050 printf(" [Ok..] [0x%x]\n", fsck->chk.valid_node_cnt);
3052 printf(" [Fail] [0x%x]\n", fsck->chk.valid_node_cnt);
3053 ret = EXIT_ERR_CODE;
3057 printf("[FSCK] valid_node_count matching with CP (nat lookup)");
3058 if (sbi->total_valid_node_count == fsck->chk.valid_nat_entry_cnt) {
3059 printf(" [Ok..] [0x%x]\n", fsck->chk.valid_nat_entry_cnt);
3061 printf(" [Fail] [0x%x]\n", fsck->chk.valid_nat_entry_cnt);
3062 ret = EXIT_ERR_CODE;
3066 printf("[FSCK] valid_inode_count matched with CP ");
3067 if (sbi->total_valid_inode_count == fsck->chk.valid_inode_cnt) {
3068 printf(" [Ok..] [0x%x]\n", fsck->chk.valid_inode_cnt);
3070 printf(" [Fail] [0x%x]\n", fsck->chk.valid_inode_cnt);
3071 ret = EXIT_ERR_CODE;
3075 printf("[FSCK] free segment_count matched with CP ");
3076 if (le32_to_cpu(F2FS_CKPT(sbi)->free_segment_count) ==
3077 fsck->chk.sit_free_segs) {
3078 printf(" [Ok..] [0x%x]\n", fsck->chk.sit_free_segs);
3080 printf(" [Fail] [0x%x]\n", fsck->chk.sit_free_segs);
3081 ret = EXIT_ERR_CODE;
3085 printf("[FSCK] next block offset is free ");
3086 if (check_curseg_offsets(sbi) == 0) {
3087 printf(" [Ok..]\n");
3089 printf(" [Fail]\n");
3090 ret = EXIT_ERR_CODE;
3094 printf("[FSCK] fixing SIT types\n");
3095 if (check_sit_types(sbi) != 0)
3098 printf("[FSCK] other corrupted bugs ");
3099 if (c.bug_on == 0) {
3100 printf(" [Ok..]\n");
3102 printf(" [Fail]\n");
3103 ret = EXIT_ERR_CODE;
3106 #ifndef WITH_ANDROID
3107 if (nr_unref_nid && !c.ro) {
3108 char ans[255] = {0};
3110 printf("\nDo you want to restore lost files into ./lost_found/? [Y/N] ");
3111 ret = scanf("%s", ans);
3113 if (!strcasecmp(ans, "y")) {
3114 for (i = 0; i < fsck->nr_nat_entries; i++) {
3115 if (f2fs_test_bit(i, fsck->nat_area_bitmap))
3116 dump_node(sbi, i, 1);
3122 /* fix global metadata */
3123 if (force || (c.fix_on && f2fs_dev_is_writable())) {
3124 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
3126 if (force || c.bug_on || c.bug_nat_bits) {
3127 /* flush nats to write_nit_bits below */
3128 flush_journal_entries(sbi);
3129 fix_hard_links(sbi);
3130 fix_nat_entries(sbi);
3131 rewrite_sit_area_bitmap(sbi);
3132 fix_wp_sit_alignment(sbi);
3133 fix_curseg_info(sbi);
3135 fix_checkpoints(sbi);
3136 } else if (is_set_ckpt_flags(cp, CP_FSCK_FLAG) ||
3137 is_set_ckpt_flags(cp, CP_QUOTA_NEED_FSCK_FLAG)) {
3138 write_checkpoints(sbi);
3144 void fsck_free(struct f2fs_sb_info *sbi)
3146 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
3149 quota_release_context(&fsck->qctx);
3151 if (fsck->main_area_bitmap)
3152 free(fsck->main_area_bitmap);
3154 if (fsck->nat_area_bitmap)
3155 free(fsck->nat_area_bitmap);
3157 if (fsck->sit_area_bitmap)
3158 free(fsck->sit_area_bitmap);
3161 free(fsck->entries);