fsck.c revision 893312ced238b9ee93c5427aa6e6c1f29fe39899
1/** 2 * fsck.c 3 * 4 * Copyright (c) 2013 Samsung Electronics Co., Ltd. 5 * http://www.samsung.com/ 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License version 2 as 9 * published by the Free Software Foundation. 10 */ 11#include "fsck.h" 12 13char *tree_mark; 14uint32_t tree_mark_size = 256; 15 16static inline int f2fs_set_main_bitmap(struct f2fs_sb_info *sbi, u32 blk) 17{ 18 struct f2fs_fsck *fsck = F2FS_FSCK(sbi); 19 20 return f2fs_set_bit(BLKOFF_FROM_MAIN(sbi, blk), fsck->main_area_bitmap); 21} 22 23static inline int f2fs_test_main_bitmap(struct f2fs_sb_info *sbi, u32 blk) 24{ 25 struct f2fs_fsck *fsck = F2FS_FSCK(sbi); 26 27 return f2fs_test_bit(BLKOFF_FROM_MAIN(sbi, blk), 28 fsck->main_area_bitmap); 29} 30 31static inline int f2fs_test_sit_bitmap(struct f2fs_sb_info *sbi, u32 blk) 32{ 33 struct f2fs_fsck *fsck = F2FS_FSCK(sbi); 34 35 return f2fs_test_bit(BLKOFF_FROM_MAIN(sbi, blk), fsck->sit_area_bitmap); 36} 37 38static int add_into_hard_link_list(struct f2fs_sb_info *sbi, 39 u32 nid, u32 link_cnt) 40{ 41 struct f2fs_fsck *fsck = F2FS_FSCK(sbi); 42 struct hard_link_node *node = NULL, *tmp = NULL, *prev = NULL; 43 44 node = calloc(sizeof(struct hard_link_node), 1); 45 ASSERT(node != NULL); 46 47 node->nid = nid; 48 node->links = link_cnt; 49 node->next = NULL; 50 51 if (fsck->hard_link_list_head == NULL) { 52 fsck->hard_link_list_head = node; 53 goto out; 54 } 55 56 tmp = fsck->hard_link_list_head; 57 58 /* Find insertion position */ 59 while (tmp && (nid < tmp->nid)) { 60 ASSERT(tmp->nid != nid); 61 prev = tmp; 62 tmp = tmp->next; 63 } 64 65 if (tmp == fsck->hard_link_list_head) { 66 node->next = tmp; 67 fsck->hard_link_list_head = node; 68 } else { 69 prev->next = node; 70 node->next = tmp; 71 } 72 73out: 74 DBG(2, "ino[0x%x] has hard links [0x%x]\n", nid, link_cnt); 75 return 0; 76} 77 78static int find_and_dec_hard_link_list(struct f2fs_sb_info *sbi, u32 nid) 79{ 80 struct f2fs_fsck *fsck = F2FS_FSCK(sbi); 81 struct hard_link_node *node = NULL, *prev = NULL; 82 83 if (fsck->hard_link_list_head == NULL) 84 return -EINVAL; 85 86 node = fsck->hard_link_list_head; 87 88 while (node && (nid < node->nid)) { 89 prev = node; 90 node = node->next; 91 } 92 93 if (node == NULL || (nid != node->nid)) 94 return -EINVAL; 95 96 /* Decrease link count */ 97 node->links = node->links - 1; 98 99 /* if link count becomes one, remove the node */ 100 if (node->links == 1) { 101 if (fsck->hard_link_list_head == node) 102 fsck->hard_link_list_head = node->next; 103 else 104 prev->next = node->next; 105 free(node); 106 } 107 return 0; 108} 109 110static int is_valid_ssa_node_blk(struct f2fs_sb_info *sbi, u32 nid, 111 u32 blk_addr) 112{ 113 int ret = 0; 114 struct f2fs_summary sum_entry; 115 116 ret = get_sum_entry(sbi, blk_addr, &sum_entry); 117 118 if (ret != SEG_TYPE_NODE && ret != SEG_TYPE_CUR_NODE) { 119 ASSERT_MSG("Summary footer is not for node segment"); 120 return -EINVAL; 121 } 122 123 if (le32_to_cpu(sum_entry.nid) != nid) { 124 DBG(0, "nid [0x%x]\n", nid); 125 DBG(0, "target blk_addr [0x%x]\n", blk_addr); 126 DBG(0, "summary blk_addr [0x%x]\n", 127 GET_SUM_BLKADDR(sbi, 128 GET_SEGNO(sbi, blk_addr))); 129 DBG(0, "seg no / offset [0x%x / 0x%x]\n", 130 GET_SEGNO(sbi, blk_addr), 131 OFFSET_IN_SEG(sbi, blk_addr)); 132 DBG(0, "summary_entry.nid [0x%x]\n", 133 le32_to_cpu(sum_entry.nid)); 134 DBG(0, "--> node block's nid [0x%x]\n", nid); 135 ASSERT_MSG("Invalid node seg summary\n"); 136 return -EINVAL; 137 } 138 return 0; 139} 140 141static int is_valid_ssa_data_blk(struct f2fs_sb_info *sbi, u32 blk_addr, 142 u32 parent_nid, u16 idx_in_node, u8 version) 143{ 144 int ret = 0; 145 struct f2fs_summary sum_entry; 146 147 ret = get_sum_entry(sbi, blk_addr, &sum_entry); 148 149 if (ret != SEG_TYPE_DATA && ret != SEG_TYPE_CUR_DATA) { 150 ASSERT_MSG("Summary footer is not for data segment"); 151 return -EINVAL; 152 } 153 154 if (le32_to_cpu(sum_entry.nid) != parent_nid || 155 sum_entry.version != version || 156 le16_to_cpu(sum_entry.ofs_in_node) != idx_in_node) { 157 158 DBG(0, "summary_entry.nid [0x%x]\n", 159 le32_to_cpu(sum_entry.nid)); 160 DBG(0, "summary_entry.version [0x%x]\n", 161 sum_entry.version); 162 DBG(0, "summary_entry.ofs_in_node [0x%x]\n", 163 le16_to_cpu(sum_entry.ofs_in_node)); 164 DBG(0, "parent nid [0x%x]\n", parent_nid); 165 DBG(0, "version from nat [0x%x]\n", version); 166 DBG(0, "idx in parent node [0x%x]\n", idx_in_node); 167 168 DBG(0, "Target data block addr [0x%x]\n", blk_addr); 169 ASSERT_MSG("Invalid data seg summary\n"); 170 return -EINVAL; 171 } 172 return 0; 173} 174 175static int sanity_check_nid(struct f2fs_sb_info *sbi, u32 nid, 176 struct f2fs_node *node_blk, 177 enum FILE_TYPE ftype, enum NODE_TYPE ntype, 178 struct node_info *ni) 179{ 180 struct f2fs_fsck *fsck = F2FS_FSCK(sbi); 181 int ret; 182 183 if (!IS_VALID_NID(sbi, nid)) { 184 ASSERT_MSG("nid is not valid. [0x%x]", nid); 185 return -EINVAL; 186 } 187 188 get_node_info(sbi, nid, ni); 189 if (ni->blk_addr == NEW_ADDR) { 190 ASSERT_MSG("nid is NEW_ADDR. [0x%x]", nid); 191 return -EINVAL; 192 } 193 194 if (!IS_VALID_BLK_ADDR(sbi, ni->blk_addr)) { 195 ASSERT_MSG("blkaddres is not valid. [0x%x]", ni->blk_addr); 196 return -EINVAL; 197 } 198 199 if (is_valid_ssa_node_blk(sbi, nid, ni->blk_addr)) { 200 ASSERT_MSG("summary node block is not valid. [0x%x]", nid); 201 return -EINVAL; 202 } 203 204 ret = dev_read_block(node_blk, ni->blk_addr); 205 ASSERT(ret >= 0); 206 207 if (ntype == TYPE_INODE && 208 node_blk->footer.nid != node_blk->footer.ino) { 209 ASSERT_MSG("nid[0x%x] footer.nid[0x%x] footer.ino[0x%x]", 210 nid, le32_to_cpu(node_blk->footer.nid), 211 le32_to_cpu(node_blk->footer.ino)); 212 return -EINVAL; 213 } 214 if (ntype != TYPE_INODE && 215 node_blk->footer.nid == node_blk->footer.ino) { 216 ASSERT_MSG("nid[0x%x] footer.nid[0x%x] footer.ino[0x%x]", 217 nid, le32_to_cpu(node_blk->footer.nid), 218 le32_to_cpu(node_blk->footer.ino)); 219 return -EINVAL; 220 } 221 222 if (le32_to_cpu(node_blk->footer.nid) != nid) { 223 ASSERT_MSG("nid[0x%x] blk_addr[0x%x] footer.nid[0x%x]", 224 nid, ni->blk_addr, 225 le32_to_cpu(node_blk->footer.nid)); 226 return -EINVAL; 227 } 228 229 if (ntype == TYPE_XATTR) { 230 u32 flag = le32_to_cpu(node_blk->footer.flag); 231 232 if ((flag >> OFFSET_BIT_SHIFT) != XATTR_NODE_OFFSET) { 233 ASSERT_MSG("xnid[0x%x] has wrong ofs:[0x%x]", 234 nid, flag); 235 return -EINVAL; 236 } 237 } 238 239 if ((ntype == TYPE_INODE && ftype == F2FS_FT_DIR) || 240 (ntype == TYPE_XATTR && ftype == F2FS_FT_XATTR)) { 241 /* not included '.' & '..' */ 242 if (f2fs_test_main_bitmap(sbi, ni->blk_addr) != 0) { 243 ASSERT_MSG("Duplicated node blk. nid[0x%x][0x%x]\n", 244 nid, ni->blk_addr); 245 return -EINVAL; 246 } 247 } 248 249 /* workaround to fix later */ 250 if (ftype != F2FS_FT_ORPHAN || 251 f2fs_test_bit(nid, fsck->nat_area_bitmap) != 0) 252 f2fs_clear_bit(nid, fsck->nat_area_bitmap); 253 else 254 ASSERT_MSG("orphan or xattr nid is duplicated [0x%x]\n", 255 nid); 256 257 if (f2fs_test_sit_bitmap(sbi, ni->blk_addr) == 0) 258 ASSERT_MSG("SIT bitmap is 0x0. blk_addr[0x%x]", 259 ni->blk_addr); 260 261 if (f2fs_test_main_bitmap(sbi, ni->blk_addr) == 0) { 262 fsck->chk.valid_blk_cnt++; 263 fsck->chk.valid_node_cnt++; 264 } 265 return 0; 266} 267 268static int fsck_chk_xattr_blk(struct f2fs_sb_info *sbi, u32 ino, 269 u32 x_nid, u32 *blk_cnt) 270{ 271 struct f2fs_node *node_blk = NULL; 272 struct node_info ni; 273 int ret = 0; 274 275 if (x_nid == 0x0) 276 return 0; 277 278 node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1); 279 ASSERT(node_blk != NULL); 280 281 /* Sanity check */ 282 if (sanity_check_nid(sbi, x_nid, node_blk, 283 F2FS_FT_XATTR, TYPE_XATTR, &ni)) { 284 ret = -EINVAL; 285 goto out; 286 } 287 288 *blk_cnt = *blk_cnt + 1; 289 f2fs_set_main_bitmap(sbi, ni.blk_addr); 290 DBG(2, "ino[0x%x] x_nid[0x%x]\n", ino, x_nid); 291out: 292 free(node_blk); 293 return ret; 294} 295 296int fsck_chk_node_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode, 297 u32 nid, enum FILE_TYPE ftype, enum NODE_TYPE ntype, 298 u32 *blk_cnt) 299{ 300 struct node_info ni; 301 struct f2fs_node *node_blk = NULL; 302 303 node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1); 304 ASSERT(node_blk != NULL); 305 306 if (sanity_check_nid(sbi, nid, node_blk, ftype, ntype, &ni)) 307 goto err; 308 309 if (ntype == TYPE_INODE) { 310 fsck_chk_inode_blk(sbi, nid, ftype, node_blk, blk_cnt, &ni); 311 } else { 312 f2fs_set_main_bitmap(sbi, ni.blk_addr); 313 314 switch (ntype) { 315 case TYPE_DIRECT_NODE: 316 fsck_chk_dnode_blk(sbi, inode, nid, ftype, node_blk, 317 blk_cnt, &ni); 318 break; 319 case TYPE_INDIRECT_NODE: 320 fsck_chk_idnode_blk(sbi, inode, ftype, node_blk, 321 blk_cnt); 322 break; 323 case TYPE_DOUBLE_INDIRECT_NODE: 324 fsck_chk_didnode_blk(sbi, inode, ftype, node_blk, 325 blk_cnt); 326 break; 327 default: 328 ASSERT(0); 329 } 330 } 331 free(node_blk); 332 return 0; 333err: 334 free(node_blk); 335 return -EINVAL; 336} 337 338/* start with valid nid and blkaddr */ 339void fsck_chk_inode_blk(struct f2fs_sb_info *sbi, u32 nid, 340 enum FILE_TYPE ftype, struct f2fs_node *node_blk, 341 u32 *blk_cnt, struct node_info *ni) 342{ 343 struct f2fs_fsck *fsck = F2FS_FSCK(sbi); 344 u32 child_cnt = 0, child_files = 0; 345 enum NODE_TYPE ntype; 346 u32 i_links = le32_to_cpu(node_blk->i.i_links); 347 u64 i_blocks = le64_to_cpu(node_blk->i.i_blocks); 348 unsigned int idx = 0; 349 int need_fix = 0; 350 int ret; 351 352 if (f2fs_test_main_bitmap(sbi, ni->blk_addr) == 0) 353 fsck->chk.valid_inode_cnt++; 354 355 if (ftype == F2FS_FT_DIR) { 356 f2fs_set_main_bitmap(sbi, ni->blk_addr); 357 } else { 358 if (f2fs_test_main_bitmap(sbi, ni->blk_addr) == 0) { 359 f2fs_set_main_bitmap(sbi, ni->blk_addr); 360 if (i_links > 1) { 361 /* First time. Create new hard link node */ 362 add_into_hard_link_list(sbi, nid, i_links); 363 fsck->chk.multi_hard_link_files++; 364 } 365 } else { 366 DBG(3, "[0x%x] has hard links [0x%x]\n", nid, i_links); 367 if (find_and_dec_hard_link_list(sbi, nid)) { 368 ASSERT_MSG("[0x%x] needs more i_links=0x%x", 369 nid, i_links); 370 if (config.fix_cnt) { 371 node_blk->i.i_links = 372 cpu_to_le32(i_links + 1); 373 need_fix = 1; 374 FIX_MSG("File: 0x%x " 375 "i_links= 0x%x -> 0x%x", 376 nid, i_links, i_links + 1); 377 } 378 } 379 /* No need to go deep into the node */ 380 return; 381 } 382 } 383 384 if (fsck_chk_xattr_blk(sbi, nid, 385 le32_to_cpu(node_blk->i.i_xattr_nid), blk_cnt) && 386 config.fix_cnt) { 387 node_blk->i.i_xattr_nid = 0; 388 need_fix = 1; 389 FIX_MSG("Remove xattr block: 0x%x, x_nid = 0x%x", 390 nid, le32_to_cpu(node_blk->i.i_xattr_nid)); 391 } 392 393 if (ftype == F2FS_FT_CHRDEV || ftype == F2FS_FT_BLKDEV || 394 ftype == F2FS_FT_FIFO || ftype == F2FS_FT_SOCK) 395 goto check; 396 if((node_blk->i.i_inline & F2FS_INLINE_DATA)){ 397 DBG(3, "ino[0x%x] has inline data!\n", nid); 398 goto check; 399 } 400 401 /* check data blocks in inode */ 402 for (idx = 0; idx < ADDRS_PER_INODE(&node_blk->i); idx++) { 403 if (le32_to_cpu(node_blk->i.i_addr[idx]) != 0) { 404 ret = fsck_chk_data_blk(sbi, 405 le32_to_cpu(node_blk->i.i_addr[idx]), 406 &child_cnt, &child_files, 407 (i_blocks == *blk_cnt), 408 ftype, nid, idx, ni->version); 409 if (!ret) { 410 *blk_cnt = *blk_cnt + 1; 411 } else if (config.fix_cnt) { 412 node_blk->i.i_addr[idx] = 0; 413 need_fix = 1; 414 FIX_MSG("[0x%x] i_addr[%d] = 0", nid, idx); 415 } 416 } 417 } 418 419 /* check node blocks in inode */ 420 for (idx = 0; idx < 5; idx++) { 421 if (idx == 0 || idx == 1) 422 ntype = TYPE_DIRECT_NODE; 423 else if (idx == 2 || idx == 3) 424 ntype = TYPE_INDIRECT_NODE; 425 else if (idx == 4) 426 ntype = TYPE_DOUBLE_INDIRECT_NODE; 427 else 428 ASSERT(0); 429 430 if (le32_to_cpu(node_blk->i.i_nid[idx]) != 0) { 431 ret = fsck_chk_node_blk(sbi, &node_blk->i, 432 le32_to_cpu(node_blk->i.i_nid[idx]), 433 ftype, ntype, blk_cnt); 434 if (!ret) { 435 *blk_cnt = *blk_cnt + 1; 436 } else if (config.fix_cnt) { 437 node_blk->i.i_nid[idx] = 0; 438 need_fix = 1; 439 FIX_MSG("[0x%x] i_nid[%d] = 0", nid, idx); 440 } 441 } 442 } 443check: 444 if (ftype == F2FS_FT_DIR) 445 DBG(1, "Directory Inode: 0x%x [%s] depth: %d has %d files\n\n", 446 le32_to_cpu(node_blk->footer.ino), 447 node_blk->i.i_name, 448 le32_to_cpu(node_blk->i.i_current_depth), 449 child_files); 450 if (ftype == F2FS_FT_ORPHAN) 451 DBG(1, "Orphan Inode: 0x%x [%s] i_blocks: %u\n\n", 452 le32_to_cpu(node_blk->footer.ino), 453 node_blk->i.i_name, 454 (u32)i_blocks); 455 456 if (i_blocks != *blk_cnt) { 457 ASSERT_MSG("ino: 0x%x has i_blocks: %lu, but has %u blocks", 458 nid, i_blocks, *blk_cnt); 459 if (config.fix_cnt) { 460 node_blk->i.i_blocks = cpu_to_le64(*blk_cnt); 461 need_fix = 1; 462 FIX_MSG("[0x%x] i_blocks=0x%lx -> 0x%x", 463 nid, i_blocks, *blk_cnt); 464 } 465 } 466 if (ftype == F2FS_FT_DIR && i_links != child_cnt) { 467 ASSERT_MSG("ino: 0x%x has i_links: %u but real links: %u", 468 nid, i_links, child_cnt); 469 if (config.fix_cnt) { 470 node_blk->i.i_links = cpu_to_le32(child_cnt); 471 need_fix = 1; 472 FIX_MSG("Dir: 0x%x i_links= 0x%x -> 0x%x", 473 nid, i_links, child_cnt); 474 } 475 } 476 477 if (ftype == F2FS_FT_ORPHAN && i_links) 478 ASSERT_MSG("ino: 0x%x is orphan inode, but has i_links: %u", 479 nid, i_links); 480 if (need_fix) { 481 ret = dev_write_block(node_blk, ni->blk_addr); 482 ASSERT(ret >= 0); 483 } 484} 485 486int fsck_chk_dnode_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode, 487 u32 nid, enum FILE_TYPE ftype, struct f2fs_node *node_blk, 488 u32 *blk_cnt, struct node_info *ni) 489{ 490 int idx, ret; 491 u32 child_cnt = 0, child_files = 0; 492 493 for (idx = 0; idx < ADDRS_PER_BLOCK; idx++) { 494 if (le32_to_cpu(node_blk->dn.addr[idx]) == 0x0) 495 continue; 496 ret = fsck_chk_data_blk(sbi, 497 le32_to_cpu(node_blk->dn.addr[idx]), 498 &child_cnt, &child_files, 499 le64_to_cpu(inode->i_blocks) == *blk_cnt, ftype, 500 nid, idx, ni->version); 501 if (!ret) 502 *blk_cnt = *blk_cnt + 1; 503 } 504 return 0; 505} 506 507int fsck_chk_idnode_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode, 508 enum FILE_TYPE ftype, struct f2fs_node *node_blk, u32 *blk_cnt) 509{ 510 int ret; 511 int i = 0; 512 513 for (i = 0 ; i < NIDS_PER_BLOCK; i++) { 514 if (le32_to_cpu(node_blk->in.nid[i]) == 0x0) 515 continue; 516 ret = fsck_chk_node_blk(sbi, inode, 517 le32_to_cpu(node_blk->in.nid[i]), 518 ftype, TYPE_DIRECT_NODE, blk_cnt); 519 if (!ret) 520 *blk_cnt = *blk_cnt + 1; 521 else if (ret == -EINVAL) 522 printf("delete in.nid[i] = 0;\n"); 523 } 524 return 0; 525} 526 527int fsck_chk_didnode_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode, 528 enum FILE_TYPE ftype, struct f2fs_node *node_blk, u32 *blk_cnt) 529{ 530 int i = 0; 531 int ret = 0; 532 533 for (i = 0; i < NIDS_PER_BLOCK; i++) { 534 if (le32_to_cpu(node_blk->in.nid[i]) == 0x0) 535 continue; 536 ret = fsck_chk_node_blk(sbi, inode, 537 le32_to_cpu(node_blk->in.nid[i]), 538 ftype, TYPE_INDIRECT_NODE, blk_cnt); 539 if (!ret) 540 *blk_cnt = *blk_cnt + 1; 541 else if (ret == -EINVAL) 542 printf("delete in.nid[i] = 0;\n"); 543 } 544 return 0; 545} 546 547static void print_dentry(__u32 depth, __u8 *name, 548 struct f2fs_dentry_block *de_blk, int idx, int last_blk) 549{ 550 int last_de = 0; 551 int next_idx = 0; 552 int name_len; 553 unsigned int i; 554 int bit_offset; 555 556 if (config.dbg_lv != -1) 557 return; 558 559 name_len = le16_to_cpu(de_blk->dentry[idx].name_len); 560 next_idx = idx + (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN; 561 562 bit_offset = find_next_bit((unsigned long *)de_blk->dentry_bitmap, 563 NR_DENTRY_IN_BLOCK, next_idx); 564 if (bit_offset >= NR_DENTRY_IN_BLOCK && last_blk) 565 last_de = 1; 566 567 if (tree_mark_size <= depth) { 568 tree_mark_size *= 2; 569 tree_mark = realloc(tree_mark, tree_mark_size); 570 } 571 572 if (last_de) 573 tree_mark[depth] = '`'; 574 else 575 tree_mark[depth] = '|'; 576 577 if (tree_mark[depth - 1] == '`') 578 tree_mark[depth - 1] = ' '; 579 580 581 for (i = 1; i < depth; i++) 582 printf("%c ", tree_mark[i]); 583 printf("%c-- %s 0x%x\n", last_de ? '`' : '|', 584 name, le32_to_cpu(de_blk->dentry[idx].ino)); 585} 586 587int fsck_chk_dentry_blk(struct f2fs_sb_info *sbi, u32 blk_addr, 588 u32 *child_cnt, u32 *child_files, int last_blk) 589{ 590 struct f2fs_fsck *fsck = F2FS_FSCK(sbi); 591 int i; 592 int ret = 0; 593 int dentries = 0; 594 u8 *name; 595 u32 hash_code; 596 u32 blk_cnt; 597 u16 name_len;; 598 599 enum FILE_TYPE ftype; 600 struct f2fs_dentry_block *de_blk; 601 602 de_blk = (struct f2fs_dentry_block *)calloc(BLOCK_SZ, 1); 603 ASSERT(de_blk != NULL); 604 605 ret = dev_read_block(de_blk, blk_addr); 606 ASSERT(ret >= 0); 607 608 fsck->dentry_depth++; 609 610 for (i = 0; i < NR_DENTRY_IN_BLOCK;) { 611 if (test_bit(i, (unsigned long *)de_blk->dentry_bitmap) == 0) { 612 i++; 613 continue; 614 } 615 616 name_len = le16_to_cpu(de_blk->dentry[i].name_len); 617 name = calloc(name_len + 1, 1); 618 memcpy(name, de_blk->filename[i], name_len); 619 hash_code = f2fs_dentry_hash((const unsigned char *)name, 620 name_len); 621 622 ASSERT(le32_to_cpu(de_blk->dentry[i].hash_code) == hash_code); 623 624 ftype = de_blk->dentry[i].file_type; 625 626 /* Becareful. 'dentry.file_type' is not imode. */ 627 if (ftype == F2FS_FT_DIR) { 628 *child_cnt = *child_cnt + 1; 629 if ((name[0] == '.' && name_len == 1) || 630 (name[0] == '.' && name[1] == '.' && 631 name_len == 2)) { 632 i++; 633 free(name); 634 continue; 635 } 636 } 637 638 DBG(1, "[%3u]-[0x%x] name[%s] len[0x%x] ino[0x%x] type[0x%x]\n", 639 fsck->dentry_depth, i, name, name_len, 640 le32_to_cpu(de_blk->dentry[i].ino), 641 de_blk->dentry[i].file_type); 642 643 print_dentry(fsck->dentry_depth, name, de_blk, i, last_blk); 644 645 blk_cnt = 1; 646 ret = fsck_chk_node_blk(sbi, 647 NULL, 648 le32_to_cpu(de_blk->dentry[i].ino), 649 ftype, 650 TYPE_INODE, 651 &blk_cnt); 652 653 if (ret && config.fix_cnt) { 654 int j; 655 int slots = (name_len + F2FS_SLOT_LEN - 1) / 656 F2FS_SLOT_LEN; 657 for (j = 0; j < slots; j++) 658 clear_bit(i + j, 659 (unsigned long *)de_blk->dentry_bitmap); 660 FIX_MSG("Unlink [0x%x] - %s len[0x%x], type[0x%x]", 661 le32_to_cpu(de_blk->dentry[i].ino), 662 name, name_len, 663 de_blk->dentry[i].file_type); 664 i += slots; 665 free(name); 666 continue; 667 } 668 669 i += (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN; 670 dentries++; 671 *child_files = *child_files + 1; 672 free(name); 673 } 674 675 DBG(1, "[%3d] Dentry Block [0x%x] Done : " 676 "dentries:%d in %d slots (len:%d)\n\n", 677 fsck->dentry_depth, blk_addr, dentries, 678 NR_DENTRY_IN_BLOCK, F2FS_NAME_LEN); 679 fsck->dentry_depth--; 680 681 free(de_blk); 682 return 0; 683} 684 685int fsck_chk_data_blk(struct f2fs_sb_info *sbi, u32 blk_addr, 686 u32 *child_cnt, u32 *child_files, int last_blk, 687 enum FILE_TYPE ftype, u32 parent_nid, u16 idx_in_node, u8 ver) 688{ 689 struct f2fs_fsck *fsck = F2FS_FSCK(sbi); 690 691 /* Is it reserved block? */ 692 if (blk_addr == NEW_ADDR) { 693 fsck->chk.valid_blk_cnt++; 694 return 0; 695 } 696 697 if (!IS_VALID_BLK_ADDR(sbi, blk_addr)) { 698 ASSERT_MSG("blkaddres is not valid. [0x%x]", blk_addr); 699 return -EINVAL; 700 } 701 702 if (is_valid_ssa_data_blk(sbi, blk_addr, parent_nid, 703 idx_in_node, ver)) { 704 ASSERT_MSG("summary data block is not valid. [0x%x]", 705 parent_nid); 706 return -EINVAL; 707 } 708 709 if (f2fs_test_sit_bitmap(sbi, blk_addr) == 0) 710 ASSERT_MSG("SIT bitmap is 0x0. blk_addr[0x%x]", blk_addr); 711 712 if (f2fs_test_main_bitmap(sbi, blk_addr) != 0) 713 ASSERT_MSG("Duplicated data [0x%x]. pnid[0x%x] idx[0x%x]", 714 blk_addr, parent_nid, idx_in_node); 715 716 f2fs_set_main_bitmap(sbi, blk_addr); 717 718 fsck->chk.valid_blk_cnt++; 719 720 if (ftype == F2FS_FT_DIR) 721 return fsck_chk_dentry_blk(sbi, blk_addr, child_cnt, 722 child_files, last_blk); 723 return 0; 724} 725 726void fsck_chk_orphan_node(struct f2fs_sb_info *sbi) 727{ 728 u32 blk_cnt = 0; 729 block_t start_blk, orphan_blkaddr, i, j; 730 struct f2fs_orphan_block *orphan_blk; 731 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 732 733 if (!is_set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG)) 734 return; 735 736 if (config.fix_cnt) 737 return; 738 739 start_blk = __start_cp_addr(sbi) + 1 + 740 le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload); 741 orphan_blkaddr = __start_sum_addr(sbi) - 1; 742 orphan_blk = calloc(BLOCK_SZ, 1); 743 744 for (i = 0; i < orphan_blkaddr; i++) { 745 int ret = dev_read_block(orphan_blk, start_blk + i); 746 747 ASSERT(ret >= 0); 748 749 for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) { 750 nid_t ino = le32_to_cpu(orphan_blk->ino[j]); 751 DBG(1, "[%3d] ino [0x%x]\n", i, ino); 752 blk_cnt = 1; 753 fsck_chk_node_blk(sbi, NULL, ino, 754 F2FS_FT_ORPHAN, TYPE_INODE, &blk_cnt); 755 } 756 memset(orphan_blk, 0, BLOCK_SZ); 757 } 758 free(orphan_blk); 759} 760 761void fsck_init(struct f2fs_sb_info *sbi) 762{ 763 struct f2fs_fsck *fsck = F2FS_FSCK(sbi); 764 struct f2fs_sm_info *sm_i = SM_I(sbi); 765 766 /* 767 * We build three bitmap for main/sit/nat so that may check consistency 768 * of filesystem. 769 * 1. main_area_bitmap will be used to check whether all blocks of main 770 * area is used or not. 771 * 2. nat_area_bitmap has bitmap information of used nid in NAT. 772 * 3. sit_area_bitmap has bitmap information of used main block. 773 * At Last sequence, we compare main_area_bitmap with sit_area_bitmap. 774 */ 775 fsck->nr_main_blks = sm_i->main_segments << sbi->log_blocks_per_seg; 776 fsck->main_area_bitmap_sz = (fsck->nr_main_blks + 7) / 8; 777 fsck->main_area_bitmap = calloc(fsck->main_area_bitmap_sz, 1); 778 ASSERT(fsck->main_area_bitmap != NULL); 779 780 build_nat_area_bitmap(sbi); 781 782 build_sit_area_bitmap(sbi); 783 784 tree_mark = calloc(tree_mark_size, 1); 785 ASSERT(tree_mark != NULL); 786} 787 788static void fix_nat_entries(struct f2fs_sb_info *sbi) 789{ 790 struct f2fs_fsck *fsck = F2FS_FSCK(sbi); 791 int i; 792 793 for (i = 0; i < fsck->nr_nat_entries; i++) 794 if (f2fs_test_bit(i, fsck->nat_area_bitmap) != 0) 795 nullify_nat_entry(sbi, i); 796} 797 798static void fix_checkpoint(struct f2fs_sb_info *sbi) 799{ 800 struct f2fs_fsck *fsck = F2FS_FSCK(sbi); 801 struct f2fs_super_block *raw_sb = sbi->raw_super; 802 struct f2fs_checkpoint *ckp = F2FS_CKPT(sbi); 803 unsigned long long cp_blk_no; 804 int i, ret; 805 u_int32_t crc = 0; 806 807 ckp->ckpt_flags = cpu_to_le32(CP_UMOUNT_FLAG); 808 ckp->cp_pack_total_block_count = 809 cpu_to_le32(8 + le32_to_cpu(raw_sb->cp_payload)); 810 ckp->cp_pack_start_sum = cpu_to_le32(1 + 811 le32_to_cpu(raw_sb->cp_payload)); 812 813 ckp->free_segment_count = cpu_to_le32(fsck->chk.free_segs); 814 ckp->valid_block_count = cpu_to_le32(fsck->chk.valid_blk_cnt); 815 ckp->valid_node_count = cpu_to_le32(fsck->chk.valid_node_cnt); 816 ckp->valid_inode_count = cpu_to_le32(fsck->chk.valid_inode_cnt); 817 818 crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, ckp, CHECKSUM_OFFSET); 819 *((__le32 *)((unsigned char *)ckp + CHECKSUM_OFFSET)) = 820 cpu_to_le32(crc); 821 822 cp_blk_no = le32_to_cpu(raw_sb->cp_blkaddr); 823 if (sbi->cur_cp == 2) 824 cp_blk_no += 1 << le32_to_cpu(raw_sb->log_blocks_per_seg); 825 826 ret = dev_write_block(ckp, cp_blk_no++); 827 ASSERT(ret >= 0); 828 829 for (i = 0; i < le32_to_cpu(raw_sb->cp_payload); i++) { 830 ret = dev_write_block(((unsigned char *)ckp) + i * F2FS_BLKSIZE, 831 cp_blk_no++); 832 ASSERT(ret >= 0); 833 } 834 835 for (i = 0; i < NO_CHECK_TYPE; i++) { 836 struct curseg_info *curseg = CURSEG_I(sbi, i); 837 838 ret = dev_write_block(curseg->sum_blk, cp_blk_no++); 839 ASSERT(ret >= 0); 840 } 841 842 ret = dev_write_block(ckp, cp_blk_no++); 843 ASSERT(ret >= 0); 844} 845 846int fsck_verify(struct f2fs_sb_info *sbi) 847{ 848 unsigned int i = 0; 849 int ret = 0; 850 u32 nr_unref_nid = 0; 851 struct f2fs_fsck *fsck = F2FS_FSCK(sbi); 852 struct hard_link_node *node = NULL; 853 854 printf("\n"); 855 856 for (i = 0; i < fsck->nr_nat_entries; i++) { 857 if (f2fs_test_bit(i, fsck->nat_area_bitmap) != 0) { 858 printf("NID[0x%x] is unreachable\n", i); 859 nr_unref_nid++; 860 } 861 } 862 863 if (fsck->hard_link_list_head != NULL) { 864 node = fsck->hard_link_list_head; 865 while (node) { 866 printf("NID[0x%x] has [0x%x] more unreachable links\n", 867 node->nid, node->links); 868 node = node->next; 869 } 870 config.bug_on = 1; 871 } 872 873 printf("[FSCK] Unreachable nat entries "); 874 if (nr_unref_nid == 0x0) { 875 printf(" [Ok..] [0x%x]\n", nr_unref_nid); 876 } else { 877 printf(" [Fail] [0x%x]\n", nr_unref_nid); 878 ret = EXIT_ERR_CODE; 879 config.bug_on = 1; 880 } 881 882 printf("[FSCK] SIT valid block bitmap checking "); 883 if (memcmp(fsck->sit_area_bitmap, fsck->main_area_bitmap, 884 fsck->sit_area_bitmap_sz) == 0x0) { 885 printf("[Ok..]\n"); 886 } else { 887 printf("[Fail]\n"); 888 ret = EXIT_ERR_CODE; 889 config.bug_on = 1; 890 } 891 892 printf("[FSCK] Hard link checking for regular file "); 893 if (fsck->hard_link_list_head == NULL) { 894 printf(" [Ok..] [0x%x]\n", fsck->chk.multi_hard_link_files); 895 } else { 896 printf(" [Fail] [0x%x]\n", fsck->chk.multi_hard_link_files); 897 ret = EXIT_ERR_CODE; 898 config.bug_on = 1; 899 } 900 901 printf("[FSCK] valid_block_count matching with CP "); 902 if (sbi->total_valid_block_count == fsck->chk.valid_blk_cnt) { 903 printf(" [Ok..] [0x%x]\n", (u32)fsck->chk.valid_blk_cnt); 904 } else { 905 printf(" [Fail] [0x%x]\n", (u32)fsck->chk.valid_blk_cnt); 906 ret = EXIT_ERR_CODE; 907 config.bug_on = 1; 908 } 909 910 printf("[FSCK] valid_node_count matcing with CP (de lookup) "); 911 if (sbi->total_valid_node_count == fsck->chk.valid_node_cnt) { 912 printf(" [Ok..] [0x%x]\n", fsck->chk.valid_node_cnt); 913 } else { 914 printf(" [Fail] [0x%x]\n", fsck->chk.valid_node_cnt); 915 ret = EXIT_ERR_CODE; 916 config.bug_on = 1; 917 } 918 919 printf("[FSCK] valid_node_count matcing with CP (nat lookup) "); 920 if (sbi->total_valid_node_count == fsck->chk.valid_nat_entry_cnt) { 921 printf(" [Ok..] [0x%x]\n", fsck->chk.valid_nat_entry_cnt); 922 } else { 923 printf(" [Fail] [0x%x]\n", fsck->chk.valid_nat_entry_cnt); 924 ret = EXIT_ERR_CODE; 925 config.bug_on = 1; 926 } 927 928 printf("[FSCK] valid_inode_count matched with CP "); 929 if (sbi->total_valid_inode_count == fsck->chk.valid_inode_cnt) { 930 printf(" [Ok..] [0x%x]\n", fsck->chk.valid_inode_cnt); 931 } else { 932 printf(" [Fail] [0x%x]\n", fsck->chk.valid_inode_cnt); 933 ret = EXIT_ERR_CODE; 934 config.bug_on = 1; 935 } 936 937 printf("[FSCK] free segment_count matched with CP "); 938 if (le32_to_cpu(F2FS_CKPT(sbi)->free_segment_count) == 939 fsck->chk.sit_free_segs) { 940 printf(" [Ok..] [0x%x]\n", fsck->chk.sit_free_segs); 941 } else { 942 printf(" [Fail] [0x%x]\n", fsck->chk.sit_free_segs); 943 ret = EXIT_ERR_CODE; 944 config.bug_on = 1; 945 } 946 947 printf("[FSCK] other corrupted bugs "); 948 if (config.bug_on == 0) { 949 printf(" [Ok..]\n"); 950 } else { 951 printf(" [Fail]\n"); 952 ret = EXIT_ERR_CODE; 953 config.bug_on = 1; 954 } 955 956 /* fix global metadata */ 957 if (config.bug_on && config.fix_cnt) { 958 fix_nat_entries(sbi); 959 rewrite_sit_area_bitmap(sbi); 960 fix_checkpoint(sbi); 961 } 962 return ret; 963} 964 965void fsck_free(struct f2fs_sb_info *sbi) 966{ 967 struct f2fs_fsck *fsck = F2FS_FSCK(sbi); 968 if (fsck->main_area_bitmap) 969 free(fsck->main_area_bitmap); 970 971 if (fsck->nat_area_bitmap) 972 free(fsck->nat_area_bitmap); 973 974 if (fsck->sit_area_bitmap) 975 free(fsck->sit_area_bitmap); 976 977 if (tree_mark) 978 free(tree_mark); 979} 980