mount.c revision 223fdf3e00a56cb32fb8c69dc4523ee53ff7731a
1/** 2 * mount.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 13void print_inode_info(struct f2fs_inode *inode) 14{ 15 int i = 0; 16 int namelen = le32_to_cpu(inode->i_namelen); 17 18 DISP_u32(inode, i_mode); 19 DISP_u32(inode, i_uid); 20 DISP_u32(inode, i_gid); 21 DISP_u32(inode, i_links); 22 DISP_u64(inode, i_size); 23 DISP_u64(inode, i_blocks); 24 25 DISP_u64(inode, i_atime); 26 DISP_u32(inode, i_atime_nsec); 27 DISP_u64(inode, i_ctime); 28 DISP_u32(inode, i_ctime_nsec); 29 DISP_u64(inode, i_mtime); 30 DISP_u32(inode, i_mtime_nsec); 31 32 DISP_u32(inode, i_generation); 33 DISP_u32(inode, i_current_depth); 34 DISP_u32(inode, i_xattr_nid); 35 DISP_u32(inode, i_flags); 36 DISP_u32(inode, i_pino); 37 38 if (namelen) { 39 DISP_u32(inode, i_namelen); 40 inode->i_name[namelen] = '\0'; 41 DISP_utf(inode, i_name); 42 } 43 44 printf("i_ext: fofs:%x blkaddr:%x len:%x\n", 45 inode->i_ext.fofs, 46 inode->i_ext.blk_addr, 47 inode->i_ext.len); 48 49 DISP_u32(inode, i_addr[0]); /* Pointers to data blocks */ 50 DISP_u32(inode, i_addr[1]); /* Pointers to data blocks */ 51 DISP_u32(inode, i_addr[2]); /* Pointers to data blocks */ 52 DISP_u32(inode, i_addr[3]); /* Pointers to data blocks */ 53 54 for (i = 4; i < ADDRS_PER_INODE; i++) { 55 if (inode->i_addr[i] != 0x0) { 56 printf("i_addr[0x%x] points data block\r\t\t\t\t[0x%4x]\n", 57 i, inode->i_addr[i]); 58 break; 59 } 60 } 61 62 DISP_u32(inode, i_nid[0]); /* direct */ 63 DISP_u32(inode, i_nid[1]); /* direct */ 64 DISP_u32(inode, i_nid[2]); /* indirect */ 65 DISP_u32(inode, i_nid[3]); /* indirect */ 66 DISP_u32(inode, i_nid[4]); /* double indirect */ 67 68 printf("\n"); 69} 70 71void print_node_info(struct f2fs_node *node_block) 72{ 73 nid_t ino = le32_to_cpu(node_block->footer.ino); 74 nid_t nid = le32_to_cpu(node_block->footer.nid); 75 /* Is this inode? */ 76 if (ino == nid) { 77 DBG(0, "Node ID [0x%x:%u] is inode\n", nid, nid); 78 print_inode_info(&node_block->i); 79 } else { 80 int i; 81 u32 *dump_blk = (u32 *)node_block; 82 DBG(0, "Node ID [0x%x:%u] is direct node or indirect node.\n", nid, nid); 83 for (i = 0; i <= 10; i++) 84 MSG(0, "[%d]\t\t\t[0x%8x : %d]\n", i, dump_blk[i], dump_blk[i]); 85 } 86} 87 88void print_raw_sb_info(struct f2fs_sb_info *sbi) 89{ 90 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi); 91 92 if (!config.dbg_lv) 93 return; 94 95 printf("\n"); 96 printf("+--------------------------------------------------------+\n"); 97 printf("| Super block |\n"); 98 printf("+--------------------------------------------------------+\n"); 99 100 DISP_u32(sb, magic); 101 DISP_u32(sb, major_ver); 102 DISP_u32(sb, minor_ver); 103 DISP_u32(sb, log_sectorsize); 104 DISP_u32(sb, log_sectors_per_block); 105 106 DISP_u32(sb, log_blocksize); 107 DISP_u32(sb, log_blocks_per_seg); 108 DISP_u32(sb, segs_per_sec); 109 DISP_u32(sb, secs_per_zone); 110 DISP_u32(sb, checksum_offset); 111 DISP_u64(sb, block_count); 112 113 DISP_u32(sb, section_count); 114 DISP_u32(sb, segment_count); 115 DISP_u32(sb, segment_count_ckpt); 116 DISP_u32(sb, segment_count_sit); 117 DISP_u32(sb, segment_count_nat); 118 119 DISP_u32(sb, segment_count_ssa); 120 DISP_u32(sb, segment_count_main); 121 DISP_u32(sb, segment0_blkaddr); 122 123 DISP_u32(sb, cp_blkaddr); 124 DISP_u32(sb, sit_blkaddr); 125 DISP_u32(sb, nat_blkaddr); 126 DISP_u32(sb, ssa_blkaddr); 127 DISP_u32(sb, main_blkaddr); 128 129 DISP_u32(sb, root_ino); 130 DISP_u32(sb, node_ino); 131 DISP_u32(sb, meta_ino); 132 printf("\n"); 133} 134 135void print_ckpt_info(struct f2fs_sb_info *sbi) 136{ 137 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi); 138 139 if (!config.dbg_lv) 140 return; 141 142 printf("\n"); 143 printf("+--------------------------------------------------------+\n"); 144 printf("| Checkpoint |\n"); 145 printf("+--------------------------------------------------------+\n"); 146 147 DISP_u64(cp, checkpoint_ver); 148 DISP_u64(cp, user_block_count); 149 DISP_u64(cp, valid_block_count); 150 DISP_u32(cp, rsvd_segment_count); 151 DISP_u32(cp, overprov_segment_count); 152 DISP_u32(cp, free_segment_count); 153 154 DISP_u32(cp, alloc_type[CURSEG_HOT_NODE]); 155 DISP_u32(cp, alloc_type[CURSEG_WARM_NODE]); 156 DISP_u32(cp, alloc_type[CURSEG_COLD_NODE]); 157 DISP_u32(cp, cur_node_segno[0]); 158 DISP_u32(cp, cur_node_segno[1]); 159 DISP_u32(cp, cur_node_segno[2]); 160 161 DISP_u32(cp, cur_node_blkoff[0]); 162 DISP_u32(cp, cur_node_blkoff[1]); 163 DISP_u32(cp, cur_node_blkoff[2]); 164 165 166 DISP_u32(cp, alloc_type[CURSEG_HOT_DATA]); 167 DISP_u32(cp, alloc_type[CURSEG_WARM_DATA]); 168 DISP_u32(cp, alloc_type[CURSEG_COLD_DATA]); 169 DISP_u32(cp, cur_data_segno[0]); 170 DISP_u32(cp, cur_data_segno[1]); 171 DISP_u32(cp, cur_data_segno[2]); 172 173 DISP_u32(cp, cur_data_blkoff[0]); 174 DISP_u32(cp, cur_data_blkoff[1]); 175 DISP_u32(cp, cur_data_blkoff[2]); 176 177 DISP_u32(cp, ckpt_flags); 178 DISP_u32(cp, cp_pack_total_block_count); 179 DISP_u32(cp, cp_pack_start_sum); 180 DISP_u32(cp, valid_node_count); 181 DISP_u32(cp, valid_inode_count); 182 DISP_u32(cp, next_free_nid); 183 DISP_u32(cp, sit_ver_bitmap_bytesize); 184 DISP_u32(cp, nat_ver_bitmap_bytesize); 185 DISP_u32(cp, checksum_offset); 186 DISP_u64(cp, elapsed_time); 187 188 DISP_u32(cp, sit_nat_version_bitmap[0]); 189 printf("\n\n"); 190} 191 192int sanity_check_raw_super(struct f2fs_super_block *raw_super) 193{ 194 unsigned int blocksize; 195 196 if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) { 197 return -1; 198 } 199 200 if (F2FS_BLKSIZE != PAGE_CACHE_SIZE) { 201 return -1; 202 } 203 204 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize); 205 if (F2FS_BLKSIZE != blocksize) { 206 return -1; 207 } 208 209 if (F2FS_LOG_SECTOR_SIZE != le32_to_cpu(raw_super->log_sectorsize)) { 210 return -1; 211 } 212 213 if (F2FS_LOG_SECTORS_PER_BLOCK != le32_to_cpu(raw_super->log_sectors_per_block)) { 214 return -1; 215 } 216 217 return 0; 218} 219 220int validate_super_block(struct f2fs_sb_info *sbi, int block) 221{ 222 u64 offset = (block + 1) * F2FS_SUPER_OFFSET; 223 sbi->raw_super = malloc(sizeof(struct f2fs_super_block)); 224 225 if (dev_read(sbi->raw_super, offset, sizeof(struct f2fs_super_block))) 226 return -1; 227 228 if (!sanity_check_raw_super(sbi->raw_super)) 229 return 0; 230 231 free(sbi->raw_super); 232 MSG(0, "\tCan't find a valid F2FS filesystem in %d superblock\n", block); 233 234 return -EINVAL; 235} 236 237int init_sb_info(struct f2fs_sb_info *sbi) 238{ 239 struct f2fs_super_block *raw_super = sbi->raw_super; 240 241 sbi->log_sectors_per_block = 242 le32_to_cpu(raw_super->log_sectors_per_block); 243 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize); 244 sbi->blocksize = 1 << sbi->log_blocksize; 245 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg); 246 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg; 247 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec); 248 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone); 249 sbi->total_sections = le32_to_cpu(raw_super->section_count); 250 sbi->total_node_count = 251 (le32_to_cpu(raw_super->segment_count_nat) / 2) 252 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK; 253 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino); 254 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino); 255 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino); 256 sbi->cur_victim_sec = NULL_SEGNO; 257 return 0; 258} 259 260void *validate_checkpoint(struct f2fs_sb_info *sbi, block_t cp_addr, unsigned long long *version) 261{ 262 void *cp_page_1, *cp_page_2; 263 struct f2fs_checkpoint *cp_block; 264 unsigned long blk_size = sbi->blocksize; 265 unsigned long long cur_version = 0, pre_version = 0; 266 unsigned int crc = 0; 267 size_t crc_offset; 268 269 /* Read the 1st cp block in this CP pack */ 270 cp_page_1 = malloc(PAGE_SIZE); 271 if (dev_read_block(cp_page_1, cp_addr) < 0) 272 return NULL; 273 274 cp_block = (struct f2fs_checkpoint *)cp_page_1; 275 crc_offset = le32_to_cpu(cp_block->checksum_offset); 276 if (crc_offset >= blk_size) 277 goto invalid_cp1; 278 279 crc = *(unsigned int *)((unsigned char *)cp_block + crc_offset); 280 if (f2fs_crc_valid(crc, cp_block, crc_offset)) 281 goto invalid_cp1; 282 283 pre_version = le64_to_cpu(cp_block->checkpoint_ver); 284 285 /* Read the 2nd cp block in this CP pack */ 286 cp_page_2 = malloc(PAGE_SIZE); 287 cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1; 288 if (dev_read_block(cp_page_2, cp_addr) < 0) 289 goto invalid_cp2; 290 291 cp_block = (struct f2fs_checkpoint *)cp_page_2; 292 crc_offset = le32_to_cpu(cp_block->checksum_offset); 293 if (crc_offset >= blk_size) 294 goto invalid_cp2; 295 296 crc = *(unsigned int *)((unsigned char *)cp_block + crc_offset); 297 if (f2fs_crc_valid(crc, cp_block, crc_offset)) 298 goto invalid_cp1; 299 300 cur_version = le64_to_cpu(cp_block->checkpoint_ver); 301 302 if (cur_version == pre_version) { 303 *version = cur_version; 304 free(cp_page_2); 305 return cp_page_1; 306 } 307 308invalid_cp2: 309 free(cp_page_2); 310invalid_cp1: 311 free(cp_page_1); 312 return NULL; 313} 314 315int get_valid_checkpoint(struct f2fs_sb_info *sbi) 316{ 317 struct f2fs_super_block *raw_sb = sbi->raw_super; 318 void *cp1, *cp2, *cur_page; 319 unsigned long blk_size = sbi->blocksize; 320 unsigned long long cp1_version = 0, cp2_version = 0; 321 unsigned long long cp_start_blk_no; 322 323 sbi->ckpt = malloc(blk_size); 324 if (!sbi->ckpt) 325 return -ENOMEM; 326 /* 327 * Finding out valid cp block involves read both 328 * sets( cp pack1 and cp pack 2) 329 */ 330 cp_start_blk_no = le32_to_cpu(raw_sb->cp_blkaddr); 331 cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version); 332 333 /* The second checkpoint pack should start at the next segment */ 334 cp_start_blk_no += 1 << le32_to_cpu(raw_sb->log_blocks_per_seg); 335 cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version); 336 337 if (cp1 && cp2) { 338 if (ver_after(cp2_version, cp1_version)) 339 cur_page = cp2; 340 else 341 cur_page = cp1; 342 } else if (cp1) { 343 cur_page = cp1; 344 } else if (cp2) { 345 cur_page = cp2; 346 } else { 347 free(cp1); 348 free(cp2); 349 goto fail_no_cp; 350 } 351 352 memcpy(sbi->ckpt, cur_page, blk_size); 353 354 free(cp1); 355 free(cp2); 356 return 0; 357 358fail_no_cp: 359 free(sbi->ckpt); 360 return -EINVAL; 361} 362 363int sanity_check_ckpt(struct f2fs_sb_info *sbi) 364{ 365 unsigned int total, fsmeta; 366 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi); 367 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 368 369 total = le32_to_cpu(raw_super->segment_count); 370 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt); 371 fsmeta += le32_to_cpu(raw_super->segment_count_sit); 372 fsmeta += le32_to_cpu(raw_super->segment_count_nat); 373 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count); 374 fsmeta += le32_to_cpu(raw_super->segment_count_ssa); 375 376 if (fsmeta >= total) 377 return 1; 378 379 return 0; 380} 381 382int init_node_manager(struct f2fs_sb_info *sbi) 383{ 384 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi); 385 struct f2fs_nm_info *nm_i = NM_I(sbi); 386 unsigned char *version_bitmap; 387 unsigned int nat_segs, nat_blocks; 388 389 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr); 390 391 /* segment_count_nat includes pair segment so divide to 2. */ 392 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1; 393 nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg); 394 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks; 395 nm_i->fcnt = 0; 396 nm_i->nat_cnt = 0; 397 nm_i->init_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid); 398 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid); 399 400 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP); 401 402 nm_i->nat_bitmap = malloc(nm_i->bitmap_size); 403 if (!nm_i->nat_bitmap) 404 return -ENOMEM; 405 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP); 406 if (!version_bitmap) 407 return -EFAULT; 408 409 /* copy version bitmap */ 410 memcpy(nm_i->nat_bitmap, version_bitmap, nm_i->bitmap_size); 411 return 0; 412} 413 414int build_node_manager(struct f2fs_sb_info *sbi) 415{ 416 int err; 417 sbi->nm_info = malloc(sizeof(struct f2fs_nm_info)); 418 if (!sbi->nm_info) 419 return -ENOMEM; 420 421 err = init_node_manager(sbi); 422 if (err) 423 return err; 424 425 return 0; 426} 427 428int build_sit_info(struct f2fs_sb_info *sbi) 429{ 430 struct f2fs_super_block *raw_sb = F2FS_RAW_SUPER(sbi); 431 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 432 struct sit_info *sit_i; 433 unsigned int sit_segs, start; 434 char *src_bitmap, *dst_bitmap; 435 unsigned int bitmap_size; 436 437 sit_i = malloc(sizeof(struct sit_info)); 438 if (!sit_i) 439 return -ENOMEM; 440 441 SM_I(sbi)->sit_info = sit_i; 442 443 sit_i->sentries = calloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry), 1); 444 445 for (start = 0; start < TOTAL_SEGS(sbi); start++) { 446 sit_i->sentries[start].cur_valid_map 447 = calloc(SIT_VBLOCK_MAP_SIZE, 1); 448 sit_i->sentries[start].ckpt_valid_map 449 = calloc(SIT_VBLOCK_MAP_SIZE, 1); 450 if (!sit_i->sentries[start].cur_valid_map 451 || !sit_i->sentries[start].ckpt_valid_map) 452 return -ENOMEM; 453 } 454 455 sit_segs = le32_to_cpu(raw_sb->segment_count_sit) >> 1; 456 bitmap_size = __bitmap_size(sbi, SIT_BITMAP); 457 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP); 458 459 dst_bitmap = malloc(bitmap_size); 460 memcpy(dst_bitmap, src_bitmap, bitmap_size); 461 462 sit_i->sit_base_addr = le32_to_cpu(raw_sb->sit_blkaddr); 463 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg; 464 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count); 465 sit_i->sit_bitmap = dst_bitmap; 466 sit_i->bitmap_size = bitmap_size; 467 sit_i->dirty_sentries = 0; 468 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK; 469 sit_i->elapsed_time = le64_to_cpu(ckpt->elapsed_time); 470 return 0; 471} 472 473void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified) 474{ 475 struct curseg_info *curseg = CURSEG_I(sbi, type); 476 477 curseg->segno = curseg->next_segno; 478 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno); 479 curseg->next_blkoff = 0; 480 curseg->next_segno = NULL_SEGNO; 481 482} 483 484int read_compacted_summaries(struct f2fs_sb_info *sbi) 485{ 486 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 487 struct curseg_info *curseg; 488 block_t start; 489 char *kaddr; 490 unsigned int i, j, offset; 491 492 start = start_sum_block(sbi); 493 494 kaddr = (char *)malloc(PAGE_SIZE); 495 dev_read_block(kaddr, start++); 496 497 curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); 498 memcpy(&curseg->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE); 499 500 curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); 501 memcpy(&curseg->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE, SUM_JOURNAL_SIZE); 502 503 offset = 2 * SUM_JOURNAL_SIZE; 504 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) { 505 unsigned short blk_off; 506 unsigned int segno; 507 508 curseg = CURSEG_I(sbi, i); 509 segno = le32_to_cpu(ckpt->cur_data_segno[i]); 510 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]); 511 curseg->next_segno = segno; 512 reset_curseg(sbi, i, 0); 513 curseg->alloc_type = ckpt->alloc_type[i]; 514 curseg->next_blkoff = blk_off; 515 516 if (curseg->alloc_type == SSR) 517 blk_off = sbi->blocks_per_seg; 518 519 for (j = 0; j < blk_off; j++) { 520 struct f2fs_summary *s; 521 s = (struct f2fs_summary *)(kaddr + offset); 522 curseg->sum_blk->entries[j] = *s; 523 offset += SUMMARY_SIZE; 524 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) 525 continue; 526 memset(kaddr, 0, PAGE_SIZE); 527 dev_read_block(kaddr, start++); 528 offset = 0; 529 } 530 } 531 532 free(kaddr); 533 return 0; 534} 535 536int restore_node_summary(struct f2fs_sb_info *sbi, 537 unsigned int segno, struct f2fs_summary_block *sum_blk) 538{ 539 struct f2fs_node *node_blk; 540 struct f2fs_summary *sum_entry; 541 void *page; 542 block_t addr; 543 int i; 544 545 page = malloc(PAGE_SIZE); 546 if (!page) 547 return -ENOMEM; 548 549 /* scan the node segment */ 550 addr = START_BLOCK(sbi, segno); 551 sum_entry = &sum_blk->entries[0]; 552 553 for (i = 0; i < sbi->blocks_per_seg; i++, sum_entry++) { 554 if (dev_read_block(page, addr)) 555 goto out; 556 557 node_blk = (struct f2fs_node *)page; 558 sum_entry->nid = node_blk->footer.nid; 559 /* do not change original value */ 560#if 0 561 sum_entry->version = 0; 562 sum_entry->ofs_in_node = 0; 563#endif 564 addr++; 565 566 } 567out: 568 free(page); 569 return 0; 570} 571 572int read_normal_summaries(struct f2fs_sb_info *sbi, int type) 573{ 574 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 575 struct f2fs_summary_block *sum_blk; 576 struct curseg_info *curseg; 577 unsigned short blk_off; 578 unsigned int segno = 0; 579 block_t blk_addr = 0; 580 581 if (IS_DATASEG(type)) { 582 segno = le32_to_cpu(ckpt->cur_data_segno[type]); 583 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type - CURSEG_HOT_DATA]); 584 585 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) 586 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type); 587 else 588 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type); 589 } else { 590 segno = le32_to_cpu(ckpt->cur_node_segno[type - CURSEG_HOT_NODE]); 591 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type - CURSEG_HOT_NODE]); 592 593 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) 594 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE, type - CURSEG_HOT_NODE); 595 else 596 blk_addr = GET_SUM_BLKADDR(sbi, segno); 597 } 598 599 sum_blk = (struct f2fs_summary_block *)malloc(PAGE_SIZE); 600 dev_read_block(sum_blk, blk_addr); 601 602 if (IS_NODESEG(type)) { 603 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) { 604 struct f2fs_summary *sum_entry = &sum_blk->entries[0]; 605 int i; 606 for (i = 0; i < sbi->blocks_per_seg; i++, sum_entry++) { 607 /* do not change original value */ 608#if 0 609 sum_entry->version = 0; 610 sum_entry->ofs_in_node = 0; 611#endif 612 } 613 } else { 614 if (restore_node_summary(sbi, segno, sum_blk)) { 615 free(sum_blk); 616 return -EINVAL; 617 } 618 } 619 } 620 621 curseg = CURSEG_I(sbi, type); 622 memcpy(curseg->sum_blk, sum_blk, PAGE_CACHE_SIZE); 623 curseg->next_segno = segno; 624 reset_curseg(sbi, type, 0); 625 curseg->alloc_type = ckpt->alloc_type[type]; 626 curseg->next_blkoff = blk_off; 627 free(sum_blk); 628 629 return 0; 630} 631 632int restore_curseg_summaries(struct f2fs_sb_info *sbi) 633{ 634 int type = CURSEG_HOT_DATA; 635 636 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) { 637 if (read_compacted_summaries(sbi)) 638 return -EINVAL; 639 type = CURSEG_HOT_NODE; 640 } 641 642 for (; type <= CURSEG_COLD_NODE; type++) { 643 if (read_normal_summaries(sbi, type)) 644 return -EINVAL; 645 } 646 return 0; 647} 648 649int build_curseg(struct f2fs_sb_info *sbi) 650{ 651 struct curseg_info *array; 652 int i; 653 654 array = malloc(sizeof(*array) * NR_CURSEG_TYPE); 655 656 SM_I(sbi)->curseg_array = array; 657 658 for (i = 0; i < NR_CURSEG_TYPE; i++) { 659 array[i].sum_blk = malloc(PAGE_CACHE_SIZE); 660 if (!array[i].sum_blk) 661 return -ENOMEM; 662 array[i].segno = NULL_SEGNO; 663 array[i].next_blkoff = 0; 664 } 665 return restore_curseg_summaries(sbi); 666} 667 668inline void check_seg_range(struct f2fs_sb_info *sbi, unsigned int segno) 669{ 670 unsigned int end_segno = SM_I(sbi)->segment_count - 1; 671 ASSERT(segno <= end_segno); 672} 673 674struct f2fs_sit_block *get_current_sit_page(struct f2fs_sb_info *sbi, unsigned int segno) 675{ 676 struct sit_info *sit_i = SIT_I(sbi); 677 unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno); 678 block_t blk_addr = sit_i->sit_base_addr + offset; 679 struct f2fs_sit_block *sit_blk = calloc(BLOCK_SZ, 1); 680 681 check_seg_range(sbi, segno); 682 683 /* calculate sit block address */ 684 if (f2fs_test_bit(offset, sit_i->sit_bitmap)) 685 blk_addr += sit_i->sit_blocks; 686 687 dev_read_block(sit_blk, blk_addr); 688 689 return sit_blk; 690} 691 692void check_block_count(struct f2fs_sb_info *sbi, 693 int segno, struct f2fs_sit_entry *raw_sit) 694{ 695 struct f2fs_sm_info *sm_info = SM_I(sbi); 696 unsigned int end_segno = sm_info->segment_count - 1; 697 int valid_blocks = 0; 698 int i; 699 700 /* check segment usage */ 701 ASSERT(GET_SIT_VBLOCKS(raw_sit) <= sbi->blocks_per_seg); 702 703 /* check boundary of a given segment number */ 704 ASSERT(segno <= end_segno); 705 706 /* check bitmap with valid block count */ 707 for (i = 0; i < sbi->blocks_per_seg; i++) 708 if (f2fs_test_bit(i, (char *)raw_sit->valid_map)) 709 valid_blocks++; 710 ASSERT(GET_SIT_VBLOCKS(raw_sit) == valid_blocks); 711} 712 713void seg_info_from_raw_sit(struct seg_entry *se, 714 struct f2fs_sit_entry *raw_sit) 715{ 716 se->valid_blocks = GET_SIT_VBLOCKS(raw_sit); 717 se->ckpt_valid_blocks = GET_SIT_VBLOCKS(raw_sit); 718 memcpy(se->cur_valid_map, raw_sit->valid_map, SIT_VBLOCK_MAP_SIZE); 719 memcpy(se->ckpt_valid_map, raw_sit->valid_map, SIT_VBLOCK_MAP_SIZE); 720 se->type = GET_SIT_TYPE(raw_sit); 721 se->mtime = le64_to_cpu(raw_sit->mtime); 722} 723 724struct seg_entry *get_seg_entry(struct f2fs_sb_info *sbi, 725 unsigned int segno) 726{ 727 struct sit_info *sit_i = SIT_I(sbi); 728 return &sit_i->sentries[segno]; 729} 730 731int get_sum_block(struct f2fs_sb_info *sbi, unsigned int segno, struct f2fs_summary_block *sum_blk) 732{ 733 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 734 struct curseg_info *curseg; 735 int type, ret; 736 u64 ssa_blk; 737 738 ssa_blk = GET_SUM_BLKADDR(sbi, segno); 739 for (type = 0; type < NR_CURSEG_NODE_TYPE; type++) { 740 if (segno == ckpt->cur_node_segno[type]) { 741 curseg = CURSEG_I(sbi, type); 742 memcpy(sum_blk, curseg->sum_blk, BLOCK_SZ); 743 return SEG_TYPE_CUR_NODE; /* current node seg was not stored */ 744 } 745 } 746 747 for (type = 0; type < NR_CURSEG_DATA_TYPE; type++) { 748 if (segno == ckpt->cur_data_segno[type]) { 749 curseg = CURSEG_I(sbi, type); 750 memcpy(sum_blk, curseg->sum_blk, BLOCK_SZ); 751 ASSERT(!IS_SUM_NODE_SEG(sum_blk->footer)); 752 DBG(2, "segno [0x%x] is current data seg[0x%x]\n", segno, type); 753 return SEG_TYPE_CUR_DATA; /* current data seg was not stored */ 754 } 755 } 756 757 ret = dev_read_block(sum_blk, ssa_blk); 758 ASSERT(ret >= 0); 759 760 if (IS_SUM_NODE_SEG(sum_blk->footer)) 761 return SEG_TYPE_NODE; 762 else 763 return SEG_TYPE_DATA; 764 765} 766 767int get_sum_entry(struct f2fs_sb_info *sbi, u32 blk_addr, struct f2fs_summary *sum_entry) 768{ 769 struct f2fs_summary_block *sum_blk; 770 u32 segno, offset; 771 int ret; 772 773 segno = GET_SEGNO(sbi, blk_addr); 774 offset = OFFSET_IN_SEG(sbi, blk_addr); 775 776 sum_blk = calloc(BLOCK_SZ, 1); 777 778 ret = get_sum_block(sbi, segno, sum_blk); 779 780 memcpy(sum_entry, &(sum_blk->entries[offset]), sizeof(struct f2fs_summary)); 781 782 free(sum_blk); 783 return ret; 784} 785 786int get_nat_entry(struct f2fs_sb_info *sbi, nid_t nid, struct f2fs_nat_entry *raw_nat) 787{ 788 struct f2fs_fsck *fsck = F2FS_FSCK(sbi); 789 struct f2fs_nm_info *nm_i = NM_I(sbi); 790 struct f2fs_nat_block *nat_block; 791 pgoff_t block_off; 792 pgoff_t block_addr; 793 int seg_off, entry_off; 794 int ret; 795 796 if ((nid / NAT_ENTRY_PER_BLOCK) > fsck->nr_nat_entries) { 797 DBG(0, "nid is over max nid\n"); 798 return -EINVAL; 799 } 800 801 if (lookup_nat_in_journal(sbi, nid, raw_nat) >= 0) 802 return 0; 803 804 nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1); 805 806 block_off = nid / NAT_ENTRY_PER_BLOCK; 807 entry_off = nid % NAT_ENTRY_PER_BLOCK; 808 809 seg_off = block_off >> sbi->log_blocks_per_seg; 810 block_addr = (pgoff_t)(nm_i->nat_blkaddr + 811 (seg_off << sbi->log_blocks_per_seg << 1) + 812 (block_off & ((1 << sbi->log_blocks_per_seg) - 1))); 813 814 if (f2fs_test_bit(block_off, nm_i->nat_bitmap)) 815 block_addr += sbi->blocks_per_seg; 816 817 ret = dev_read_block(nat_block, block_addr); 818 ASSERT(ret >= 0); 819 820 memcpy(raw_nat, &nat_block->entries[entry_off], sizeof(struct f2fs_nat_entry)); 821 free(nat_block); 822 823 return 0; 824} 825 826int get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni) 827{ 828 struct f2fs_nat_entry raw_nat; 829 int ret; 830 831 ret = get_nat_entry(sbi, nid, &raw_nat); 832 ni->nid = nid; 833 node_info_from_raw_nat(ni, &raw_nat); 834 return ret; 835} 836 837void build_sit_entries(struct f2fs_sb_info *sbi) 838{ 839 struct sit_info *sit_i = SIT_I(sbi); 840 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); 841 struct f2fs_summary_block *sum = curseg->sum_blk; 842 unsigned int segno; 843 844 for (segno = 0; segno < TOTAL_SEGS(sbi); segno++) { 845 struct seg_entry *se = &sit_i->sentries[segno]; 846 struct f2fs_sit_block *sit_blk; 847 struct f2fs_sit_entry sit; 848 int i; 849 850 for (i = 0; i < sits_in_cursum(sum); i++) { 851 if (le32_to_cpu(segno_in_journal(sum, i)) == segno) { 852 sit = sit_in_journal(sum, i); 853 goto got_it; 854 } 855 } 856 sit_blk = get_current_sit_page(sbi, segno); 857 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, segno)]; 858 free(sit_blk); 859got_it: 860 check_block_count(sbi, segno, &sit); 861 seg_info_from_raw_sit(se, &sit); 862 } 863 864} 865 866int build_segment_manager(struct f2fs_sb_info *sbi) 867{ 868 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi); 869 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 870 struct f2fs_sm_info *sm_info; 871 872 sm_info = malloc(sizeof(struct f2fs_sm_info)); 873 if (!sm_info) 874 return -ENOMEM; 875 876 /* init sm info */ 877 sbi->sm_info = sm_info; 878 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr); 879 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr); 880 sm_info->segment_count = le32_to_cpu(raw_super->segment_count); 881 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count); 882 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count); 883 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main); 884 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr); 885 886 build_sit_info(sbi); 887 888 build_curseg(sbi); 889 890 build_sit_entries(sbi); 891 892 return 0; 893} 894 895int build_sit_area_bitmap(struct f2fs_sb_info *sbi) 896{ 897 struct f2fs_fsck *fsck = F2FS_FSCK(sbi); 898 struct f2fs_sm_info *sm_i = SM_I(sbi); 899 int segno = 0, j = 0; 900 char *ptr = NULL; 901 902 u32 sum_vblocks = 0; 903 u32 free_segs = 0; 904 u32 vblocks = 0; 905 906 struct seg_entry *se; 907 908 fsck->sit_area_bitmap_sz = sm_i->main_segments * SIT_VBLOCK_MAP_SIZE; 909 fsck->sit_area_bitmap = calloc(1, fsck->sit_area_bitmap_sz); 910 ptr = fsck->sit_area_bitmap; 911 912 ASSERT(fsck->sit_area_bitmap_sz == fsck->main_area_bitmap_sz); 913 914 for (segno = 0; segno < sm_i->main_segments; segno++) { 915 se = get_seg_entry(sbi, segno); 916 917 memcpy(ptr, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE); 918 ptr += SIT_VBLOCK_MAP_SIZE; 919 920 vblocks = 0; 921 for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++) { 922 vblocks += get_bits_in_byte(se->cur_valid_map[j]); 923 } 924 ASSERT(vblocks == se->valid_blocks); 925 926 if (se->valid_blocks == 0x0) { 927 928 if (sbi->ckpt->cur_node_segno[0] == segno || 929 sbi->ckpt->cur_data_segno[0] == segno || 930 sbi->ckpt->cur_node_segno[1] == segno || 931 sbi->ckpt->cur_data_segno[1] == segno || 932 sbi->ckpt->cur_node_segno[2] == segno || 933 sbi->ckpt->cur_data_segno[2] == segno) { 934 continue; 935 } else { 936 free_segs++; 937 } 938 939 } else { 940 ASSERT(se->valid_blocks <= 512); 941 sum_vblocks += se->valid_blocks; 942 } 943 } 944 945 fsck->chk.sit_valid_blocks = sum_vblocks; 946 fsck->chk.sit_free_segs = free_segs; 947 948 DBG(1, "Blocks [0x%x : %d] Free Segs [0x%x : %d]\n\n", sum_vblocks, sum_vblocks, 949 free_segs, free_segs); 950 return 0; 951} 952 953int lookup_nat_in_journal(struct f2fs_sb_info *sbi, u32 nid, struct f2fs_nat_entry *raw_nat) 954{ 955 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); 956 struct f2fs_summary_block *sum = curseg->sum_blk; 957 int i = 0; 958 959 for (i = 0; i < nats_in_cursum(sum); i++) { 960 if (le32_to_cpu(nid_in_journal(sum, i)) == nid) { 961 memcpy(raw_nat, &nat_in_journal(sum, i), sizeof(struct f2fs_nat_entry)); 962 DBG(3, "==> Found nid [0x%x] in nat cache\n", nid); 963 return i; 964 } 965 } 966 return -1; 967} 968 969void build_nat_area_bitmap(struct f2fs_sb_info *sbi) 970{ 971 struct f2fs_fsck *fsck = F2FS_FSCK(sbi); 972 struct f2fs_super_block *raw_sb = F2FS_RAW_SUPER(sbi); 973 struct f2fs_nm_info *nm_i = NM_I(sbi); 974 struct f2fs_nat_block *nat_block; 975 u32 nid, nr_nat_blks; 976 977 pgoff_t block_off; 978 pgoff_t block_addr; 979 int seg_off; 980 int ret, i; 981 982 983 nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1); 984 985 /* Alloc & build nat entry bitmap */ 986 nr_nat_blks = (le32_to_cpu(raw_sb->segment_count_nat) / 2) << sbi->log_blocks_per_seg; 987 988 fsck->nr_nat_entries = nr_nat_blks * NAT_ENTRY_PER_BLOCK; 989 fsck->nat_area_bitmap_sz = (fsck->nr_nat_entries + 7) / 8; 990 fsck->nat_area_bitmap = calloc(fsck->nat_area_bitmap_sz, 1); 991 ASSERT(fsck->nat_area_bitmap != NULL); 992 993 for (block_off = 0; block_off < nr_nat_blks; block_off++) { 994 995 seg_off = block_off >> sbi->log_blocks_per_seg; 996 block_addr = (pgoff_t)(nm_i->nat_blkaddr + 997 (seg_off << sbi->log_blocks_per_seg << 1) + 998 (block_off & ((1 << sbi->log_blocks_per_seg) - 1))); 999 1000 if (f2fs_test_bit(block_off, nm_i->nat_bitmap)) 1001 block_addr += sbi->blocks_per_seg; 1002 1003 ret = dev_read_block(nat_block, block_addr); 1004 ASSERT(ret >= 0); 1005 1006 nid = block_off * NAT_ENTRY_PER_BLOCK; 1007 for (i = 0; i < NAT_ENTRY_PER_BLOCK; i++) { 1008 struct f2fs_nat_entry raw_nat; 1009 struct node_info ni; 1010 ni.nid = nid + i; 1011 1012 if ((nid + i) == F2FS_NODE_INO(sbi) || (nid + i) == F2FS_META_INO(sbi)) { 1013 ASSERT(nat_block->entries[i].block_addr != 0x0); 1014 continue; 1015 } 1016 1017 if (lookup_nat_in_journal(sbi, nid + i, &raw_nat) >= 0) { 1018 node_info_from_raw_nat(&ni, &raw_nat); 1019 if (ni.blk_addr != 0x0) { 1020 f2fs_set_bit(nid + i, fsck->nat_area_bitmap); 1021 fsck->chk.valid_nat_entry_cnt++; 1022 DBG(3, "nid[0x%x] in nat cache\n", nid + i); 1023 } 1024 } else { 1025 node_info_from_raw_nat(&ni, &nat_block->entries[i]); 1026 if (ni.blk_addr != 0) { 1027 ASSERT(nid + i != 0x0); 1028 1029 DBG(3, "nid[0x%8x] in nat entry [0x%16x] [0x%8x]\n", 1030 nid + i, 1031 ni.blk_addr, 1032 ni.ino); 1033 1034 f2fs_set_bit(nid + i, fsck->nat_area_bitmap); 1035 fsck->chk.valid_nat_entry_cnt++; 1036 } 1037 } 1038 } 1039 } 1040 free(nat_block); 1041 1042 DBG(1, "valid nat entries (block_addr != 0x0) [0x%8x : %u]\n", 1043 fsck->chk.valid_nat_entry_cnt, fsck->chk.valid_nat_entry_cnt); 1044 1045} 1046 1047int f2fs_do_mount(struct f2fs_sb_info *sbi) 1048{ 1049 int ret; 1050 sbi->active_logs = NR_CURSEG_TYPE; 1051 ret = validate_super_block(sbi, 0); 1052 if (ret) { 1053 ret = validate_super_block(sbi, 1); 1054 if (ret) 1055 return -1; 1056 } 1057 1058 print_raw_sb_info(sbi); 1059 1060 init_sb_info(sbi); 1061 1062 ret = get_valid_checkpoint(sbi); 1063 if (ret) { 1064 ERR_MSG("Can't find valid checkpoint\n"); 1065 return -1; 1066 } 1067 1068 if (sanity_check_ckpt(sbi)) { 1069 ERR_MSG("Checkpoint is polluted\n"); 1070 return -1; 1071 } 1072 1073 print_ckpt_info(sbi); 1074 1075 sbi->total_valid_node_count = le32_to_cpu(sbi->ckpt->valid_node_count); 1076 sbi->total_valid_inode_count = le32_to_cpu(sbi->ckpt->valid_inode_count); 1077 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count); 1078 sbi->total_valid_block_count = le64_to_cpu(sbi->ckpt->valid_block_count); 1079 sbi->last_valid_block_count = sbi->total_valid_block_count; 1080 sbi->alloc_valid_block_count = 0; 1081 1082 if (build_segment_manager(sbi)) { 1083 ERR_MSG("build_segment_manager failed\n"); 1084 return -1; 1085 } 1086 1087 if (build_node_manager(sbi)) { 1088 ERR_MSG("build_segment_manager failed\n"); 1089 return -1; 1090 } 1091 1092 return ret; 1093} 1094 1095void f2fs_do_umount(struct f2fs_sb_info *sbi) 1096{ 1097 struct sit_info *sit_i = SIT_I(sbi); 1098 struct f2fs_sm_info *sm_i = SM_I(sbi); 1099 struct f2fs_nm_info *nm_i = NM_I(sbi); 1100 int i; 1101 1102 /* free nm_info */ 1103 free(nm_i->nat_bitmap); 1104 free(sbi->nm_info); 1105 1106 /* free sit_info */ 1107 for (i = 0; i < TOTAL_SEGS(sbi); i++) { 1108 free(sit_i->sentries[i].cur_valid_map); 1109 free(sit_i->sentries[i].ckpt_valid_map); 1110 } 1111 free(sit_i->sit_bitmap); 1112 free(sm_i->sit_info); 1113 1114 /* free sm_info */ 1115 for (i = 0; i < NR_CURSEG_TYPE; i++) 1116 free(sm_i->curseg_array[i].sum_blk); 1117 1118 free(sm_i->curseg_array); 1119 free(sbi->sm_info); 1120 1121 free(sbi->ckpt); 1122 free(sbi->raw_super); 1123} 1124