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