mount.c revision 767a93ea8bf1f66cf119830d2a42775395675cd6
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#include <locale.h> 13 14static u32 get_free_segments(struct f2fs_sb_info *sbi) 15{ 16 u32 i, free_segs = 0; 17 18 for (i = 0; i < TOTAL_SEGS(sbi); i++) { 19 struct seg_entry *se = get_seg_entry(sbi, i); 20 21 if (se->valid_blocks == 0x0 && 22 !IS_CUR_SEGNO(sbi, i, NO_CHECK_TYPE)) 23 free_segs++; 24 } 25 return free_segs; 26} 27 28void print_inode_info(struct f2fs_inode *inode, int name) 29{ 30 unsigned int i = 0; 31 int namelen = le32_to_cpu(inode->i_namelen); 32 33 if (name && namelen) { 34 inode->i_name[namelen] = '\0'; 35 MSG(0, " - File name : %s\n", inode->i_name); 36 setlocale(LC_ALL, ""); 37 MSG(0, " - File size : %'llu (bytes)\n", 38 le64_to_cpu(inode->i_size)); 39 return; 40 } 41 42 DISP_u32(inode, i_mode); 43 DISP_u32(inode, i_advise); 44 DISP_u32(inode, i_uid); 45 DISP_u32(inode, i_gid); 46 DISP_u32(inode, i_links); 47 DISP_u64(inode, i_size); 48 DISP_u64(inode, i_blocks); 49 50 DISP_u64(inode, i_atime); 51 DISP_u32(inode, i_atime_nsec); 52 DISP_u64(inode, i_ctime); 53 DISP_u32(inode, i_ctime_nsec); 54 DISP_u64(inode, i_mtime); 55 DISP_u32(inode, i_mtime_nsec); 56 57 DISP_u32(inode, i_generation); 58 DISP_u32(inode, i_current_depth); 59 DISP_u32(inode, i_xattr_nid); 60 DISP_u32(inode, i_flags); 61 DISP_u32(inode, i_inline); 62 DISP_u32(inode, i_pino); 63 64 if (namelen) { 65 DISP_u32(inode, i_namelen); 66 inode->i_name[namelen] = '\0'; 67 DISP_utf(inode, i_name); 68 } 69 70 printf("i_ext: fofs:%x blkaddr:%x len:%x\n", 71 inode->i_ext.fofs, 72 inode->i_ext.blk_addr, 73 inode->i_ext.len); 74 75 DISP_u32(inode, i_addr[0]); /* Pointers to data blocks */ 76 DISP_u32(inode, i_addr[1]); /* Pointers to data blocks */ 77 DISP_u32(inode, i_addr[2]); /* Pointers to data blocks */ 78 DISP_u32(inode, i_addr[3]); /* Pointers to data blocks */ 79 80 for (i = 4; i < ADDRS_PER_INODE(inode); i++) { 81 if (inode->i_addr[i] != 0x0) { 82 printf("i_addr[0x%x] points data block\r\t\t[0x%4x]\n", 83 i, inode->i_addr[i]); 84 break; 85 } 86 } 87 88 DISP_u32(inode, i_nid[0]); /* direct */ 89 DISP_u32(inode, i_nid[1]); /* direct */ 90 DISP_u32(inode, i_nid[2]); /* indirect */ 91 DISP_u32(inode, i_nid[3]); /* indirect */ 92 DISP_u32(inode, i_nid[4]); /* double indirect */ 93 94 printf("\n"); 95} 96 97void print_node_info(struct f2fs_node *node_block) 98{ 99 nid_t ino = le32_to_cpu(node_block->footer.ino); 100 nid_t nid = le32_to_cpu(node_block->footer.nid); 101 /* Is this inode? */ 102 if (ino == nid) { 103 DBG(0, "Node ID [0x%x:%u] is inode\n", nid, nid); 104 print_inode_info(&node_block->i, 0); 105 } else { 106 int i; 107 u32 *dump_blk = (u32 *)node_block; 108 DBG(0, "Node ID [0x%x:%u] is direct node or indirect node.\n", 109 nid, nid); 110 for (i = 0; i <= 10; i++) 111 MSG(0, "[%d]\t\t\t[0x%8x : %d]\n", 112 i, dump_blk[i], dump_blk[i]); 113 } 114} 115 116static void DISP_label(u_int16_t *name) 117{ 118 char buffer[MAX_VOLUME_NAME]; 119 120 utf16_to_utf8(buffer, name, MAX_VOLUME_NAME, MAX_VOLUME_NAME); 121 printf("%-30s" "\t\t[%s]\n", "volum_name", buffer); 122} 123 124void print_raw_sb_info(struct f2fs_super_block *sb) 125{ 126 if (!config.dbg_lv) 127 return; 128 129 printf("\n"); 130 printf("+--------------------------------------------------------+\n"); 131 printf("| Super block |\n"); 132 printf("+--------------------------------------------------------+\n"); 133 134 DISP_u32(sb, magic); 135 DISP_u32(sb, major_ver); 136 137 DISP_label(sb->volume_name); 138 139 DISP_u32(sb, minor_ver); 140 DISP_u32(sb, log_sectorsize); 141 DISP_u32(sb, log_sectors_per_block); 142 143 DISP_u32(sb, log_blocksize); 144 DISP_u32(sb, log_blocks_per_seg); 145 DISP_u32(sb, segs_per_sec); 146 DISP_u32(sb, secs_per_zone); 147 DISP_u32(sb, checksum_offset); 148 DISP_u64(sb, block_count); 149 150 DISP_u32(sb, section_count); 151 DISP_u32(sb, segment_count); 152 DISP_u32(sb, segment_count_ckpt); 153 DISP_u32(sb, segment_count_sit); 154 DISP_u32(sb, segment_count_nat); 155 156 DISP_u32(sb, segment_count_ssa); 157 DISP_u32(sb, segment_count_main); 158 DISP_u32(sb, segment0_blkaddr); 159 160 DISP_u32(sb, cp_blkaddr); 161 DISP_u32(sb, sit_blkaddr); 162 DISP_u32(sb, nat_blkaddr); 163 DISP_u32(sb, ssa_blkaddr); 164 DISP_u32(sb, main_blkaddr); 165 166 DISP_u32(sb, root_ino); 167 DISP_u32(sb, node_ino); 168 DISP_u32(sb, meta_ino); 169 DISP_u32(sb, cp_payload); 170 DISP("%s", sb, version); 171 printf("\n"); 172} 173 174void print_ckpt_info(struct f2fs_sb_info *sbi) 175{ 176 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi); 177 178 if (!config.dbg_lv) 179 return; 180 181 printf("\n"); 182 printf("+--------------------------------------------------------+\n"); 183 printf("| Checkpoint |\n"); 184 printf("+--------------------------------------------------------+\n"); 185 186 DISP_u64(cp, checkpoint_ver); 187 DISP_u64(cp, user_block_count); 188 DISP_u64(cp, valid_block_count); 189 DISP_u32(cp, rsvd_segment_count); 190 DISP_u32(cp, overprov_segment_count); 191 DISP_u32(cp, free_segment_count); 192 193 DISP_u32(cp, alloc_type[CURSEG_HOT_NODE]); 194 DISP_u32(cp, alloc_type[CURSEG_WARM_NODE]); 195 DISP_u32(cp, alloc_type[CURSEG_COLD_NODE]); 196 DISP_u32(cp, cur_node_segno[0]); 197 DISP_u32(cp, cur_node_segno[1]); 198 DISP_u32(cp, cur_node_segno[2]); 199 200 DISP_u32(cp, cur_node_blkoff[0]); 201 DISP_u32(cp, cur_node_blkoff[1]); 202 DISP_u32(cp, cur_node_blkoff[2]); 203 204 205 DISP_u32(cp, alloc_type[CURSEG_HOT_DATA]); 206 DISP_u32(cp, alloc_type[CURSEG_WARM_DATA]); 207 DISP_u32(cp, alloc_type[CURSEG_COLD_DATA]); 208 DISP_u32(cp, cur_data_segno[0]); 209 DISP_u32(cp, cur_data_segno[1]); 210 DISP_u32(cp, cur_data_segno[2]); 211 212 DISP_u32(cp, cur_data_blkoff[0]); 213 DISP_u32(cp, cur_data_blkoff[1]); 214 DISP_u32(cp, cur_data_blkoff[2]); 215 216 DISP_u32(cp, ckpt_flags); 217 DISP_u32(cp, cp_pack_total_block_count); 218 DISP_u32(cp, cp_pack_start_sum); 219 DISP_u32(cp, valid_node_count); 220 DISP_u32(cp, valid_inode_count); 221 DISP_u32(cp, next_free_nid); 222 DISP_u32(cp, sit_ver_bitmap_bytesize); 223 DISP_u32(cp, nat_ver_bitmap_bytesize); 224 DISP_u32(cp, checksum_offset); 225 DISP_u64(cp, elapsed_time); 226 227 DISP_u32(cp, sit_nat_version_bitmap[0]); 228 printf("\n\n"); 229} 230 231void print_cp_state(u32 flag) 232{ 233 MSG(0, "Info: checkpoint state = %x : ", flag); 234 if (flag & CP_FSCK_FLAG) 235 MSG(0, "%s", " fsck"); 236 if (flag & CP_ERROR_FLAG) 237 MSG(0, "%s", " error"); 238 if (flag & CP_COMPACT_SUM_FLAG) 239 MSG(0, "%s", " compacted_summary"); 240 if (flag & CP_ORPHAN_PRESENT_FLAG) 241 MSG(0, "%s", " orphan_inodes"); 242 if (flag & CP_FASTBOOT_FLAG) 243 MSG(0, "%s", " fastboot"); 244 if (flag & CP_UMOUNT_FLAG) 245 MSG(0, "%s", " unmount"); 246 else 247 MSG(0, "%s", " sudden-power-off"); 248 MSG(0, "\n"); 249} 250 251void print_sb_state(struct f2fs_super_block *sb) 252{ 253 __le32 f = sb->feature; 254 int i; 255 256 MSG(0, "Info: superblock features = %x : ", f); 257 if (f & cpu_to_le32(F2FS_FEATURE_ENCRYPT)) { 258 MSG(0, "%s", " encrypt"); 259 } 260 MSG(0, "\n"); 261 MSG(0, "Info: superblock encrypt level = %d, salt = ", 262 sb->encryption_level); 263 for (i = 0; i < 16; i++) 264 MSG(0, "%02x", sb->encrypt_pw_salt[i]); 265 MSG(0, "\n"); 266} 267 268int sanity_check_raw_super(struct f2fs_super_block *sb) 269{ 270 unsigned int blocksize; 271 272 if (F2FS_SUPER_MAGIC != get_sb(magic)) 273 return -1; 274 275 if (F2FS_BLKSIZE != PAGE_CACHE_SIZE) 276 return -1; 277 278 blocksize = 1 << get_sb(log_blocksize); 279 if (F2FS_BLKSIZE != blocksize) 280 return -1; 281 282 if (get_sb(log_sectorsize) > F2FS_MAX_LOG_SECTOR_SIZE || 283 get_sb(log_sectorsize) < F2FS_MIN_LOG_SECTOR_SIZE) 284 return -1; 285 286 if (get_sb(log_sectors_per_block) + get_sb(log_sectorsize) != 287 F2FS_MAX_LOG_SECTOR_SIZE) 288 return -1; 289 290 return 0; 291} 292 293int validate_super_block(struct f2fs_sb_info *sbi, int block) 294{ 295 u64 offset; 296 297 sbi->raw_super = malloc(sizeof(struct f2fs_super_block)); 298 299 if (block == 0) 300 offset = F2FS_SUPER_OFFSET; 301 else 302 offset = F2FS_BLKSIZE + F2FS_SUPER_OFFSET; 303 304 if (dev_read(sbi->raw_super, offset, sizeof(struct f2fs_super_block))) 305 return -1; 306 307 if (!sanity_check_raw_super(sbi->raw_super)) { 308 /* get kernel version */ 309 if (config.kd >= 0) { 310 dev_read_version(config.version, 0, VERSION_LEN); 311 get_kernel_version(config.version); 312 } else { 313 memset(config.version, 0, VERSION_LEN); 314 } 315 316 /* build sb version */ 317 memcpy(config.sb_version, sbi->raw_super->version, VERSION_LEN); 318 get_kernel_version(config.sb_version); 319 memcpy(config.init_version, sbi->raw_super->init_version, VERSION_LEN); 320 get_kernel_version(config.init_version); 321 322 MSG(0, "Info: MKFS version\n \"%s\"\n", config.init_version); 323 MSG(0, "Info: FSCK version\n from \"%s\"\n to \"%s\"\n", 324 config.sb_version, config.version); 325 if (memcmp(config.sb_version, config.version, VERSION_LEN)) { 326 int ret; 327 328 memcpy(sbi->raw_super->version, 329 config.version, VERSION_LEN); 330 ret = dev_write(sbi->raw_super, offset, 331 sizeof(struct f2fs_super_block)); 332 ASSERT(ret >= 0); 333 334 config.auto_fix = 0; 335 config.fix_on = 1; 336 } 337 print_sb_state(sbi->raw_super); 338 return 0; 339 } 340 341 free(sbi->raw_super); 342 MSG(0, "\tCan't find a valid F2FS superblock at 0x%x\n", block); 343 344 return -EINVAL; 345} 346 347int init_sb_info(struct f2fs_sb_info *sbi) 348{ 349 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi); 350 u64 total_sectors; 351 352 sbi->log_sectors_per_block = get_sb(log_sectors_per_block); 353 sbi->log_blocksize = get_sb(log_blocksize); 354 sbi->blocksize = 1 << sbi->log_blocksize; 355 sbi->log_blocks_per_seg = get_sb(log_blocks_per_seg); 356 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg; 357 sbi->segs_per_sec = get_sb(segs_per_sec); 358 sbi->secs_per_zone = get_sb(secs_per_zone); 359 sbi->total_sections = get_sb(section_count); 360 sbi->total_node_count = (get_sb(segment_count_nat) / 2) * 361 sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK; 362 sbi->root_ino_num = get_sb(root_ino); 363 sbi->node_ino_num = get_sb(node_ino); 364 sbi->meta_ino_num = get_sb(meta_ino); 365 sbi->cur_victim_sec = NULL_SEGNO; 366 367 total_sectors = get_sb(block_count) << sbi->log_sectors_per_block; 368 MSG(0, "Info: total FS sectors = %"PRIu64" (%"PRIu64" MB)\n", 369 total_sectors, total_sectors >> 11); 370 return 0; 371} 372 373void *validate_checkpoint(struct f2fs_sb_info *sbi, block_t cp_addr, 374 unsigned long long *version) 375{ 376 void *cp_page_1, *cp_page_2; 377 struct f2fs_checkpoint *cp; 378 unsigned long blk_size = sbi->blocksize; 379 unsigned long long cur_version = 0, pre_version = 0; 380 unsigned int crc = 0; 381 size_t crc_offset; 382 383 /* Read the 1st cp block in this CP pack */ 384 cp_page_1 = malloc(PAGE_SIZE); 385 if (dev_read_block(cp_page_1, cp_addr) < 0) 386 return NULL; 387 388 cp = (struct f2fs_checkpoint *)cp_page_1; 389 crc_offset = get_cp(checksum_offset); 390 if (crc_offset >= blk_size) 391 goto invalid_cp1; 392 393 crc = *(unsigned int *)((unsigned char *)cp + crc_offset); 394 if (f2fs_crc_valid(crc, cp, crc_offset)) 395 goto invalid_cp1; 396 397 pre_version = get_cp(checkpoint_ver); 398 399 /* Read the 2nd cp block in this CP pack */ 400 cp_page_2 = malloc(PAGE_SIZE); 401 cp_addr += get_cp(cp_pack_total_block_count) - 1; 402 403 if (dev_read_block(cp_page_2, cp_addr) < 0) 404 goto invalid_cp2; 405 406 cp = (struct f2fs_checkpoint *)cp_page_2; 407 crc_offset = get_cp(checksum_offset); 408 if (crc_offset >= blk_size) 409 goto invalid_cp2; 410 411 crc = *(unsigned int *)((unsigned char *)cp + crc_offset); 412 if (f2fs_crc_valid(crc, cp, crc_offset)) 413 goto invalid_cp2; 414 415 cur_version = get_cp(checkpoint_ver); 416 417 if (cur_version == pre_version) { 418 *version = cur_version; 419 free(cp_page_2); 420 return cp_page_1; 421 } 422 423invalid_cp2: 424 free(cp_page_2); 425invalid_cp1: 426 free(cp_page_1); 427 return NULL; 428} 429 430int get_valid_checkpoint(struct f2fs_sb_info *sbi) 431{ 432 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi); 433 void *cp1, *cp2, *cur_page; 434 unsigned long blk_size = sbi->blocksize; 435 unsigned long long cp1_version = 0, cp2_version = 0, version; 436 unsigned long long cp_start_blk_no; 437 unsigned int cp_blks = 1 + get_sb(cp_payload); 438 int ret; 439 440 sbi->ckpt = malloc(cp_blks * blk_size); 441 if (!sbi->ckpt) 442 return -ENOMEM; 443 /* 444 * Finding out valid cp block involves read both 445 * sets( cp pack1 and cp pack 2) 446 */ 447 cp_start_blk_no = get_sb(cp_blkaddr); 448 cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version); 449 450 /* The second checkpoint pack should start at the next segment */ 451 cp_start_blk_no += 1 << get_sb(log_blocks_per_seg); 452 cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version); 453 454 if (cp1 && cp2) { 455 if (ver_after(cp2_version, cp1_version)) { 456 cur_page = cp2; 457 sbi->cur_cp = 2; 458 version = cp2_version; 459 } else { 460 cur_page = cp1; 461 sbi->cur_cp = 1; 462 version = cp1_version; 463 } 464 } else if (cp1) { 465 cur_page = cp1; 466 sbi->cur_cp = 1; 467 version = cp1_version; 468 } else if (cp2) { 469 cur_page = cp2; 470 sbi->cur_cp = 2; 471 version = cp2_version; 472 } else { 473 free(cp1); 474 free(cp2); 475 goto fail_no_cp; 476 } 477 478 MSG(0, "Info: CKPT version = %llx\n", version); 479 480 memcpy(sbi->ckpt, cur_page, blk_size); 481 482 if (cp_blks > 1) { 483 unsigned int i; 484 unsigned long long cp_blk_no; 485 486 cp_blk_no = get_sb(cp_blkaddr); 487 if (cur_page == cp2) 488 cp_blk_no += 1 << get_sb(log_blocks_per_seg); 489 490 /* copy sit bitmap */ 491 for (i = 1; i < cp_blks; i++) { 492 unsigned char *ckpt = (unsigned char *)sbi->ckpt; 493 ret = dev_read_block(cur_page, cp_blk_no + i); 494 ASSERT(ret >= 0); 495 memcpy(ckpt + i * blk_size, cur_page, blk_size); 496 } 497 } 498 free(cp1); 499 free(cp2); 500 return 0; 501 502fail_no_cp: 503 free(sbi->ckpt); 504 return -EINVAL; 505} 506 507int sanity_check_ckpt(struct f2fs_sb_info *sbi) 508{ 509 unsigned int total, fsmeta; 510 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi); 511 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi); 512 513 total = get_sb(segment_count); 514 fsmeta = get_sb(segment_count_ckpt); 515 fsmeta += get_sb(segment_count_sit); 516 fsmeta += get_sb(segment_count_nat); 517 fsmeta += get_cp(rsvd_segment_count); 518 fsmeta += get_sb(segment_count_ssa); 519 520 if (fsmeta >= total) 521 return 1; 522 523 return 0; 524} 525 526int init_node_manager(struct f2fs_sb_info *sbi) 527{ 528 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi); 529 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi); 530 struct f2fs_nm_info *nm_i = NM_I(sbi); 531 unsigned char *version_bitmap; 532 unsigned int nat_segs, nat_blocks; 533 534 nm_i->nat_blkaddr = get_sb(nat_blkaddr); 535 536 /* segment_count_nat includes pair segment so divide to 2. */ 537 nat_segs = get_sb(segment_count_nat) >> 1; 538 nat_blocks = nat_segs << get_sb(log_blocks_per_seg); 539 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks; 540 nm_i->fcnt = 0; 541 nm_i->nat_cnt = 0; 542 nm_i->init_scan_nid = get_cp(next_free_nid); 543 nm_i->next_scan_nid = get_cp(next_free_nid); 544 545 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP); 546 547 nm_i->nat_bitmap = malloc(nm_i->bitmap_size); 548 if (!nm_i->nat_bitmap) 549 return -ENOMEM; 550 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP); 551 if (!version_bitmap) 552 return -EFAULT; 553 554 /* copy version bitmap */ 555 memcpy(nm_i->nat_bitmap, version_bitmap, nm_i->bitmap_size); 556 return 0; 557} 558 559int build_node_manager(struct f2fs_sb_info *sbi) 560{ 561 int err; 562 sbi->nm_info = malloc(sizeof(struct f2fs_nm_info)); 563 if (!sbi->nm_info) 564 return -ENOMEM; 565 566 err = init_node_manager(sbi); 567 if (err) 568 return err; 569 570 return 0; 571} 572 573int build_sit_info(struct f2fs_sb_info *sbi) 574{ 575 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi); 576 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi); 577 struct sit_info *sit_i; 578 unsigned int sit_segs, start; 579 char *src_bitmap, *dst_bitmap; 580 unsigned int bitmap_size; 581 582 sit_i = malloc(sizeof(struct sit_info)); 583 if (!sit_i) 584 return -ENOMEM; 585 586 SM_I(sbi)->sit_info = sit_i; 587 588 sit_i->sentries = calloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry), 1); 589 590 for (start = 0; start < TOTAL_SEGS(sbi); start++) { 591 sit_i->sentries[start].cur_valid_map 592 = calloc(SIT_VBLOCK_MAP_SIZE, 1); 593 sit_i->sentries[start].ckpt_valid_map 594 = calloc(SIT_VBLOCK_MAP_SIZE, 1); 595 if (!sit_i->sentries[start].cur_valid_map 596 || !sit_i->sentries[start].ckpt_valid_map) 597 return -ENOMEM; 598 } 599 600 sit_segs = get_sb(segment_count_sit) >> 1; 601 bitmap_size = __bitmap_size(sbi, SIT_BITMAP); 602 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP); 603 604 dst_bitmap = malloc(bitmap_size); 605 memcpy(dst_bitmap, src_bitmap, bitmap_size); 606 607 sit_i->sit_base_addr = get_sb(sit_blkaddr); 608 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg; 609 sit_i->written_valid_blocks = get_cp(valid_block_count); 610 sit_i->sit_bitmap = dst_bitmap; 611 sit_i->bitmap_size = bitmap_size; 612 sit_i->dirty_sentries = 0; 613 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK; 614 sit_i->elapsed_time = get_cp(elapsed_time); 615 return 0; 616} 617 618void reset_curseg(struct f2fs_sb_info *sbi, int type) 619{ 620 struct curseg_info *curseg = CURSEG_I(sbi, type); 621 struct summary_footer *sum_footer; 622 struct seg_entry *se; 623 624 sum_footer = &(curseg->sum_blk->footer); 625 memset(sum_footer, 0, sizeof(struct summary_footer)); 626 if (IS_DATASEG(type)) 627 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA); 628 if (IS_NODESEG(type)) 629 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE); 630 se = get_seg_entry(sbi, curseg->segno); 631 se->type = type; 632} 633 634static void read_compacted_summaries(struct f2fs_sb_info *sbi) 635{ 636 struct curseg_info *curseg; 637 unsigned int i, j, offset; 638 block_t start; 639 char *kaddr; 640 int ret; 641 642 start = start_sum_block(sbi); 643 644 kaddr = (char *)malloc(PAGE_SIZE); 645 ret = dev_read_block(kaddr, start++); 646 ASSERT(ret >= 0); 647 648 curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); 649 memcpy(&curseg->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE); 650 651 curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); 652 memcpy(&curseg->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE, 653 SUM_JOURNAL_SIZE); 654 655 offset = 2 * SUM_JOURNAL_SIZE; 656 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) { 657 unsigned short blk_off; 658 struct curseg_info *curseg = CURSEG_I(sbi, i); 659 660 reset_curseg(sbi, i); 661 662 if (curseg->alloc_type == SSR) 663 blk_off = sbi->blocks_per_seg; 664 else 665 blk_off = curseg->next_blkoff; 666 667 for (j = 0; j < blk_off; j++) { 668 struct f2fs_summary *s; 669 s = (struct f2fs_summary *)(kaddr + offset); 670 curseg->sum_blk->entries[j] = *s; 671 offset += SUMMARY_SIZE; 672 if (offset + SUMMARY_SIZE <= 673 PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) 674 continue; 675 memset(kaddr, 0, PAGE_SIZE); 676 ret = dev_read_block(kaddr, start++); 677 ASSERT(ret >= 0); 678 offset = 0; 679 } 680 } 681 free(kaddr); 682} 683 684static void restore_node_summary(struct f2fs_sb_info *sbi, 685 unsigned int segno, struct f2fs_summary_block *sum_blk) 686{ 687 struct f2fs_node *node_blk; 688 struct f2fs_summary *sum_entry; 689 block_t addr; 690 unsigned int i; 691 int ret; 692 693 node_blk = malloc(F2FS_BLKSIZE); 694 ASSERT(node_blk); 695 696 /* scan the node segment */ 697 addr = START_BLOCK(sbi, segno); 698 sum_entry = &sum_blk->entries[0]; 699 700 for (i = 0; i < sbi->blocks_per_seg; i++, sum_entry++) { 701 ret = dev_read_block(node_blk, addr); 702 ASSERT(ret >= 0); 703 sum_entry->nid = node_blk->footer.nid; 704 addr++; 705 } 706 free(node_blk); 707} 708 709static void read_normal_summaries(struct f2fs_sb_info *sbi, int type) 710{ 711 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi); 712 struct f2fs_summary_block *sum_blk; 713 struct curseg_info *curseg; 714 unsigned int segno = 0; 715 block_t blk_addr = 0; 716 int ret; 717 718 if (IS_DATASEG(type)) { 719 segno = get_cp(cur_data_segno[type]); 720 if (is_set_ckpt_flags(cp, CP_UMOUNT_FLAG)) 721 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type); 722 else 723 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type); 724 } else { 725 segno = get_cp(cur_node_segno[type - CURSEG_HOT_NODE]); 726 if (is_set_ckpt_flags(cp, CP_UMOUNT_FLAG)) 727 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE, 728 type - CURSEG_HOT_NODE); 729 else 730 blk_addr = GET_SUM_BLKADDR(sbi, segno); 731 } 732 733 sum_blk = (struct f2fs_summary_block *)malloc(PAGE_SIZE); 734 ret = dev_read_block(sum_blk, blk_addr); 735 ASSERT(ret >= 0); 736 737 if (IS_NODESEG(type) && !is_set_ckpt_flags(cp, CP_UMOUNT_FLAG)) 738 restore_node_summary(sbi, segno, sum_blk); 739 740 curseg = CURSEG_I(sbi, type); 741 memcpy(curseg->sum_blk, sum_blk, PAGE_CACHE_SIZE); 742 reset_curseg(sbi, type); 743 free(sum_blk); 744} 745 746void update_sum_entry(struct f2fs_sb_info *sbi, block_t blk_addr, 747 struct f2fs_summary *sum) 748{ 749 struct f2fs_summary_block *sum_blk; 750 u32 segno, offset; 751 int type, ret; 752 struct seg_entry *se; 753 754 segno = GET_SEGNO(sbi, blk_addr); 755 offset = OFFSET_IN_SEG(sbi, blk_addr); 756 757 se = get_seg_entry(sbi, segno); 758 759 sum_blk = get_sum_block(sbi, segno, &type); 760 memcpy(&sum_blk->entries[offset], sum, sizeof(*sum)); 761 sum_blk->footer.entry_type = IS_NODESEG(se->type) ? SUM_TYPE_NODE : 762 SUM_TYPE_DATA; 763 764 /* write SSA all the time */ 765 if (type < SEG_TYPE_MAX) { 766 u64 ssa_blk = GET_SUM_BLKADDR(sbi, segno); 767 ret = dev_write_block(sum_blk, ssa_blk); 768 ASSERT(ret >= 0); 769 } 770 771 if (type == SEG_TYPE_NODE || type == SEG_TYPE_DATA || 772 type == SEG_TYPE_MAX) 773 free(sum_blk); 774} 775 776static void restore_curseg_summaries(struct f2fs_sb_info *sbi) 777{ 778 int type = CURSEG_HOT_DATA; 779 780 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) { 781 read_compacted_summaries(sbi); 782 type = CURSEG_HOT_NODE; 783 } 784 785 for (; type <= CURSEG_COLD_NODE; type++) 786 read_normal_summaries(sbi, type); 787} 788 789static void build_curseg(struct f2fs_sb_info *sbi) 790{ 791 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi); 792 struct curseg_info *array; 793 unsigned short blk_off; 794 unsigned int segno; 795 int i; 796 797 array = malloc(sizeof(*array) * NR_CURSEG_TYPE); 798 ASSERT(array); 799 800 SM_I(sbi)->curseg_array = array; 801 802 for (i = 0; i < NR_CURSEG_TYPE; i++) { 803 array[i].sum_blk = malloc(PAGE_CACHE_SIZE); 804 ASSERT(array[i].sum_blk); 805 if (i <= CURSEG_COLD_DATA) { 806 blk_off = get_cp(cur_data_blkoff[i]); 807 segno = get_cp(cur_data_segno[i]); 808 } 809 if (i > CURSEG_COLD_DATA) { 810 blk_off = get_cp(cur_node_blkoff[i - CURSEG_HOT_NODE]); 811 segno = get_cp(cur_node_segno[i - CURSEG_HOT_NODE]); 812 } 813 array[i].segno = segno; 814 array[i].zone = GET_ZONENO_FROM_SEGNO(sbi, segno); 815 array[i].next_segno = NULL_SEGNO; 816 array[i].next_blkoff = blk_off; 817 array[i].alloc_type = cp->alloc_type[i]; 818 } 819 restore_curseg_summaries(sbi); 820} 821 822static inline void check_seg_range(struct f2fs_sb_info *sbi, unsigned int segno) 823{ 824 unsigned int end_segno = SM_I(sbi)->segment_count - 1; 825 ASSERT(segno <= end_segno); 826} 827 828static struct f2fs_sit_block *get_current_sit_page(struct f2fs_sb_info *sbi, 829 unsigned int segno) 830{ 831 struct sit_info *sit_i = SIT_I(sbi); 832 unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno); 833 block_t blk_addr = sit_i->sit_base_addr + offset; 834 struct f2fs_sit_block *sit_blk = calloc(BLOCK_SZ, 1); 835 int ret; 836 837 check_seg_range(sbi, segno); 838 839 /* calculate sit block address */ 840 if (f2fs_test_bit(offset, sit_i->sit_bitmap)) 841 blk_addr += sit_i->sit_blocks; 842 843 ret = dev_read_block(sit_blk, blk_addr); 844 ASSERT(ret >= 0); 845 846 return sit_blk; 847} 848 849void rewrite_current_sit_page(struct f2fs_sb_info *sbi, 850 unsigned int segno, struct f2fs_sit_block *sit_blk) 851{ 852 struct sit_info *sit_i = SIT_I(sbi); 853 unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno); 854 block_t blk_addr = sit_i->sit_base_addr + offset; 855 int ret; 856 857 /* calculate sit block address */ 858 if (f2fs_test_bit(offset, sit_i->sit_bitmap)) 859 blk_addr += sit_i->sit_blocks; 860 861 ret = dev_write_block(sit_blk, blk_addr); 862 ASSERT(ret >= 0); 863} 864 865void check_block_count(struct f2fs_sb_info *sbi, 866 unsigned int segno, struct f2fs_sit_entry *raw_sit) 867{ 868 struct f2fs_sm_info *sm_info = SM_I(sbi); 869 unsigned int end_segno = sm_info->segment_count - 1; 870 int valid_blocks = 0; 871 unsigned int i; 872 873 /* check segment usage */ 874 if (GET_SIT_VBLOCKS(raw_sit) > sbi->blocks_per_seg) 875 ASSERT_MSG("Invalid SIT vblocks: segno=0x%x, %u", 876 segno, GET_SIT_VBLOCKS(raw_sit)); 877 878 /* check boundary of a given segment number */ 879 if (segno > end_segno) 880 ASSERT_MSG("Invalid SEGNO: 0x%x", segno); 881 882 /* check bitmap with valid block count */ 883 for (i = 0; i < SIT_VBLOCK_MAP_SIZE; i++) 884 valid_blocks += get_bits_in_byte(raw_sit->valid_map[i]); 885 886 if (GET_SIT_VBLOCKS(raw_sit) != valid_blocks) 887 ASSERT_MSG("Wrong SIT valid blocks: segno=0x%x, %u vs. %u", 888 segno, GET_SIT_VBLOCKS(raw_sit), valid_blocks); 889 890 if (GET_SIT_TYPE(raw_sit) >= NO_CHECK_TYPE) 891 ASSERT_MSG("Wrong SIT type: segno=0x%x, %u", 892 segno, GET_SIT_TYPE(raw_sit)); 893} 894 895void seg_info_from_raw_sit(struct seg_entry *se, 896 struct f2fs_sit_entry *raw_sit) 897{ 898 se->valid_blocks = GET_SIT_VBLOCKS(raw_sit); 899 se->ckpt_valid_blocks = GET_SIT_VBLOCKS(raw_sit); 900 memcpy(se->cur_valid_map, raw_sit->valid_map, SIT_VBLOCK_MAP_SIZE); 901 memcpy(se->ckpt_valid_map, raw_sit->valid_map, SIT_VBLOCK_MAP_SIZE); 902 se->type = GET_SIT_TYPE(raw_sit); 903 se->orig_type = GET_SIT_TYPE(raw_sit); 904 se->mtime = le64_to_cpu(raw_sit->mtime); 905} 906 907struct seg_entry *get_seg_entry(struct f2fs_sb_info *sbi, 908 unsigned int segno) 909{ 910 struct sit_info *sit_i = SIT_I(sbi); 911 return &sit_i->sentries[segno]; 912} 913 914struct f2fs_summary_block *get_sum_block(struct f2fs_sb_info *sbi, 915 unsigned int segno, int *ret_type) 916{ 917 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi); 918 struct f2fs_summary_block *sum_blk; 919 struct curseg_info *curseg; 920 int type, ret; 921 u64 ssa_blk; 922 923 *ret_type= SEG_TYPE_MAX; 924 925 ssa_blk = GET_SUM_BLKADDR(sbi, segno); 926 for (type = 0; type < NR_CURSEG_NODE_TYPE; type++) { 927 if (segno == get_cp(cur_node_segno[type])) { 928 curseg = CURSEG_I(sbi, CURSEG_HOT_NODE + type); 929 if (!IS_SUM_NODE_SEG(curseg->sum_blk->footer)) { 930 ASSERT_MSG("segno [0x%x] indicates a data " 931 "segment, but should be node", 932 segno); 933 *ret_type = -SEG_TYPE_CUR_NODE; 934 } else { 935 *ret_type = SEG_TYPE_CUR_NODE; 936 } 937 return curseg->sum_blk; 938 } 939 } 940 941 for (type = 0; type < NR_CURSEG_DATA_TYPE; type++) { 942 if (segno == get_cp(cur_data_segno[type])) { 943 curseg = CURSEG_I(sbi, type); 944 if (IS_SUM_NODE_SEG(curseg->sum_blk->footer)) { 945 ASSERT_MSG("segno [0x%x] indicates a node " 946 "segment, but should be data", 947 segno); 948 *ret_type = -SEG_TYPE_CUR_DATA; 949 } else { 950 *ret_type = SEG_TYPE_CUR_DATA; 951 } 952 return curseg->sum_blk; 953 } 954 } 955 956 sum_blk = calloc(BLOCK_SZ, 1); 957 ASSERT(sum_blk); 958 959 ret = dev_read_block(sum_blk, ssa_blk); 960 ASSERT(ret >= 0); 961 962 if (IS_SUM_NODE_SEG(sum_blk->footer)) 963 *ret_type = SEG_TYPE_NODE; 964 else if (IS_SUM_DATA_SEG(sum_blk->footer)) 965 *ret_type = SEG_TYPE_DATA; 966 967 return sum_blk; 968} 969 970int get_sum_entry(struct f2fs_sb_info *sbi, u32 blk_addr, 971 struct f2fs_summary *sum_entry) 972{ 973 struct f2fs_summary_block *sum_blk; 974 u32 segno, offset; 975 int type; 976 977 segno = GET_SEGNO(sbi, blk_addr); 978 offset = OFFSET_IN_SEG(sbi, blk_addr); 979 980 sum_blk = get_sum_block(sbi, segno, &type); 981 memcpy(sum_entry, &(sum_blk->entries[offset]), 982 sizeof(struct f2fs_summary)); 983 if (type == SEG_TYPE_NODE || type == SEG_TYPE_DATA || 984 type == SEG_TYPE_MAX) 985 free(sum_blk); 986 return type; 987} 988 989static void get_nat_entry(struct f2fs_sb_info *sbi, nid_t nid, 990 struct f2fs_nat_entry *raw_nat) 991{ 992 struct f2fs_nm_info *nm_i = NM_I(sbi); 993 struct f2fs_nat_block *nat_block; 994 pgoff_t block_off; 995 pgoff_t block_addr; 996 int seg_off, entry_off; 997 int ret; 998 999 if (lookup_nat_in_journal(sbi, nid, raw_nat) >= 0) 1000 return; 1001 1002 nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1); 1003 1004 block_off = nid / NAT_ENTRY_PER_BLOCK; 1005 entry_off = nid % NAT_ENTRY_PER_BLOCK; 1006 1007 seg_off = block_off >> sbi->log_blocks_per_seg; 1008 block_addr = (pgoff_t)(nm_i->nat_blkaddr + 1009 (seg_off << sbi->log_blocks_per_seg << 1) + 1010 (block_off & ((1 << sbi->log_blocks_per_seg) - 1))); 1011 1012 if (f2fs_test_bit(block_off, nm_i->nat_bitmap)) 1013 block_addr += sbi->blocks_per_seg; 1014 1015 ret = dev_read_block(nat_block, block_addr); 1016 ASSERT(ret >= 0); 1017 1018 memcpy(raw_nat, &nat_block->entries[entry_off], 1019 sizeof(struct f2fs_nat_entry)); 1020 free(nat_block); 1021} 1022 1023void update_data_blkaddr(struct f2fs_sb_info *sbi, nid_t nid, 1024 u16 ofs_in_node, block_t newaddr) 1025{ 1026 struct f2fs_node *node_blk = NULL; 1027 struct node_info ni; 1028 block_t oldaddr, startaddr, endaddr; 1029 int ret; 1030 1031 node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1); 1032 ASSERT(node_blk != NULL); 1033 1034 get_node_info(sbi, nid, &ni); 1035 1036 /* read node_block */ 1037 ret = dev_read_block(node_blk, ni.blk_addr); 1038 ASSERT(ret >= 0); 1039 1040 /* check its block address */ 1041 if (node_blk->footer.nid == node_blk->footer.ino) { 1042 oldaddr = le32_to_cpu(node_blk->i.i_addr[ofs_in_node]); 1043 node_blk->i.i_addr[ofs_in_node] = cpu_to_le32(newaddr); 1044 } else { 1045 oldaddr = le32_to_cpu(node_blk->dn.addr[ofs_in_node]); 1046 node_blk->dn.addr[ofs_in_node] = cpu_to_le32(newaddr); 1047 } 1048 1049 ret = dev_write_block(node_blk, ni.blk_addr); 1050 ASSERT(ret >= 0); 1051 1052 /* check extent cache entry */ 1053 if (node_blk->footer.nid != node_blk->footer.ino) { 1054 get_node_info(sbi, le32_to_cpu(node_blk->footer.ino), &ni); 1055 1056 /* read inode block */ 1057 ret = dev_read_block(node_blk, ni.blk_addr); 1058 ASSERT(ret >= 0); 1059 } 1060 1061 startaddr = le32_to_cpu(node_blk->i.i_ext.blk_addr); 1062 endaddr = startaddr + le32_to_cpu(node_blk->i.i_ext.len); 1063 if (oldaddr >= startaddr && oldaddr < endaddr) { 1064 node_blk->i.i_ext.len = 0; 1065 1066 /* update inode block */ 1067 ret = dev_write_block(node_blk, ni.blk_addr); 1068 ASSERT(ret >= 0); 1069 } 1070 free(node_blk); 1071} 1072 1073void update_nat_blkaddr(struct f2fs_sb_info *sbi, nid_t nid, block_t newaddr) 1074{ 1075 struct f2fs_nm_info *nm_i = NM_I(sbi); 1076 struct f2fs_nat_block *nat_block; 1077 pgoff_t block_off; 1078 pgoff_t block_addr; 1079 int seg_off, entry_off; 1080 int ret; 1081 1082 nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1); 1083 1084 block_off = nid / NAT_ENTRY_PER_BLOCK; 1085 entry_off = nid % NAT_ENTRY_PER_BLOCK; 1086 1087 seg_off = block_off >> sbi->log_blocks_per_seg; 1088 block_addr = (pgoff_t)(nm_i->nat_blkaddr + 1089 (seg_off << sbi->log_blocks_per_seg << 1) + 1090 (block_off & ((1 << sbi->log_blocks_per_seg) - 1))); 1091 1092 if (f2fs_test_bit(block_off, nm_i->nat_bitmap)) 1093 block_addr += sbi->blocks_per_seg; 1094 1095 ret = dev_read_block(nat_block, block_addr); 1096 ASSERT(ret >= 0); 1097 1098 nat_block->entries[entry_off].block_addr = cpu_to_le32(newaddr); 1099 1100 ret = dev_write_block(nat_block, block_addr); 1101 ASSERT(ret >= 0); 1102 free(nat_block); 1103} 1104 1105void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni) 1106{ 1107 struct f2fs_nat_entry raw_nat; 1108 get_nat_entry(sbi, nid, &raw_nat); 1109 ni->nid = nid; 1110 node_info_from_raw_nat(ni, &raw_nat); 1111} 1112 1113void build_sit_entries(struct f2fs_sb_info *sbi) 1114{ 1115 struct sit_info *sit_i = SIT_I(sbi); 1116 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); 1117 struct f2fs_summary_block *sum = curseg->sum_blk; 1118 unsigned int segno; 1119 1120 for (segno = 0; segno < TOTAL_SEGS(sbi); segno++) { 1121 struct seg_entry *se = &sit_i->sentries[segno]; 1122 struct f2fs_sit_block *sit_blk; 1123 struct f2fs_sit_entry sit; 1124 int i; 1125 1126 for (i = 0; i < sits_in_cursum(sum); i++) { 1127 if (le32_to_cpu(segno_in_journal(sum, i)) == segno) { 1128 sit = sit_in_journal(sum, i); 1129 goto got_it; 1130 } 1131 } 1132 sit_blk = get_current_sit_page(sbi, segno); 1133 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, segno)]; 1134 free(sit_blk); 1135got_it: 1136 check_block_count(sbi, segno, &sit); 1137 seg_info_from_raw_sit(se, &sit); 1138 } 1139 1140} 1141 1142int build_segment_manager(struct f2fs_sb_info *sbi) 1143{ 1144 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi); 1145 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi); 1146 struct f2fs_sm_info *sm_info; 1147 1148 sm_info = malloc(sizeof(struct f2fs_sm_info)); 1149 if (!sm_info) 1150 return -ENOMEM; 1151 1152 /* init sm info */ 1153 sbi->sm_info = sm_info; 1154 sm_info->seg0_blkaddr = get_sb(segment0_blkaddr); 1155 sm_info->main_blkaddr = get_sb(main_blkaddr); 1156 sm_info->segment_count = get_sb(segment_count); 1157 sm_info->reserved_segments = get_cp(rsvd_segment_count); 1158 sm_info->ovp_segments = get_cp(overprov_segment_count); 1159 sm_info->main_segments = get_sb(segment_count_main); 1160 sm_info->ssa_blkaddr = get_sb(ssa_blkaddr); 1161 1162 build_sit_info(sbi); 1163 1164 build_curseg(sbi); 1165 1166 build_sit_entries(sbi); 1167 1168 return 0; 1169} 1170 1171void build_sit_area_bitmap(struct f2fs_sb_info *sbi) 1172{ 1173 struct f2fs_fsck *fsck = F2FS_FSCK(sbi); 1174 struct f2fs_sm_info *sm_i = SM_I(sbi); 1175 unsigned int segno = 0; 1176 char *ptr = NULL; 1177 u32 sum_vblocks = 0; 1178 u32 free_segs = 0; 1179 struct seg_entry *se; 1180 1181 fsck->sit_area_bitmap_sz = sm_i->main_segments * SIT_VBLOCK_MAP_SIZE; 1182 fsck->sit_area_bitmap = calloc(1, fsck->sit_area_bitmap_sz); 1183 ptr = fsck->sit_area_bitmap; 1184 1185 ASSERT(fsck->sit_area_bitmap_sz == fsck->main_area_bitmap_sz); 1186 1187 for (segno = 0; segno < TOTAL_SEGS(sbi); segno++) { 1188 se = get_seg_entry(sbi, segno); 1189 1190 memcpy(ptr, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE); 1191 ptr += SIT_VBLOCK_MAP_SIZE; 1192 1193 if (se->valid_blocks == 0x0) { 1194 if (sbi->ckpt->cur_node_segno[0] == segno || 1195 sbi->ckpt->cur_data_segno[0] == segno || 1196 sbi->ckpt->cur_node_segno[1] == segno || 1197 sbi->ckpt->cur_data_segno[1] == segno || 1198 sbi->ckpt->cur_node_segno[2] == segno || 1199 sbi->ckpt->cur_data_segno[2] == segno) { 1200 continue; 1201 } else { 1202 free_segs++; 1203 } 1204 } else { 1205 sum_vblocks += se->valid_blocks; 1206 } 1207 } 1208 fsck->chk.sit_valid_blocks = sum_vblocks; 1209 fsck->chk.sit_free_segs = free_segs; 1210 1211 DBG(1, "Blocks [0x%x : %d] Free Segs [0x%x : %d]\n\n", 1212 sum_vblocks, sum_vblocks, 1213 free_segs, free_segs); 1214} 1215 1216void rewrite_sit_area_bitmap(struct f2fs_sb_info *sbi) 1217{ 1218 struct f2fs_fsck *fsck = F2FS_FSCK(sbi); 1219 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); 1220 struct sit_info *sit_i = SIT_I(sbi); 1221 unsigned int segno = 0; 1222 struct f2fs_summary_block *sum = curseg->sum_blk; 1223 char *ptr = NULL; 1224 1225 /* remove sit journal */ 1226 sum->n_sits = 0; 1227 1228 fsck->chk.free_segs = 0; 1229 1230 ptr = fsck->main_area_bitmap; 1231 1232 for (segno = 0; segno < TOTAL_SEGS(sbi); segno++) { 1233 struct f2fs_sit_block *sit_blk; 1234 struct f2fs_sit_entry *sit; 1235 struct seg_entry *se; 1236 u16 valid_blocks = 0; 1237 u16 type; 1238 int i; 1239 1240 sit_blk = get_current_sit_page(sbi, segno); 1241 sit = &sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, segno)]; 1242 memcpy(sit->valid_map, ptr, SIT_VBLOCK_MAP_SIZE); 1243 1244 /* update valid block count */ 1245 for (i = 0; i < SIT_VBLOCK_MAP_SIZE; i++) 1246 valid_blocks += get_bits_in_byte(sit->valid_map[i]); 1247 1248 se = get_seg_entry(sbi, segno); 1249 type = se->type; 1250 if (type >= NO_CHECK_TYPE) { 1251 ASSERT_MSG("Invalide type and valid blocks=%x,%x", 1252 segno, valid_blocks); 1253 type = 0; 1254 } 1255 sit->vblocks = cpu_to_le16((type << SIT_VBLOCKS_SHIFT) | 1256 valid_blocks); 1257 rewrite_current_sit_page(sbi, segno, sit_blk); 1258 free(sit_blk); 1259 1260 if (valid_blocks == 0 && 1261 sbi->ckpt->cur_node_segno[0] != segno && 1262 sbi->ckpt->cur_data_segno[0] != segno && 1263 sbi->ckpt->cur_node_segno[1] != segno && 1264 sbi->ckpt->cur_data_segno[1] != segno && 1265 sbi->ckpt->cur_node_segno[2] != segno && 1266 sbi->ckpt->cur_data_segno[2] != segno) 1267 fsck->chk.free_segs++; 1268 1269 ptr += SIT_VBLOCK_MAP_SIZE; 1270 } 1271} 1272 1273static void flush_sit_journal_entries(struct f2fs_sb_info *sbi) 1274{ 1275 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); 1276 struct f2fs_summary_block *sum = curseg->sum_blk; 1277 struct sit_info *sit_i = SIT_I(sbi); 1278 unsigned int segno; 1279 int i; 1280 1281 for (i = 0; i < sits_in_cursum(sum); i++) { 1282 struct f2fs_sit_block *sit_blk; 1283 struct f2fs_sit_entry *sit; 1284 struct seg_entry *se; 1285 1286 segno = segno_in_journal(sum, i); 1287 se = get_seg_entry(sbi, segno); 1288 1289 sit_blk = get_current_sit_page(sbi, segno); 1290 sit = &sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, segno)]; 1291 1292 memcpy(sit->valid_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE); 1293 sit->vblocks = cpu_to_le16((se->type << SIT_VBLOCKS_SHIFT) | 1294 se->valid_blocks); 1295 sit->mtime = cpu_to_le64(se->mtime); 1296 1297 rewrite_current_sit_page(sbi, segno, sit_blk); 1298 free(sit_blk); 1299 } 1300 sum->n_sits = 0; 1301} 1302 1303static void flush_nat_journal_entries(struct f2fs_sb_info *sbi) 1304{ 1305 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); 1306 struct f2fs_summary_block *sum = curseg->sum_blk; 1307 struct f2fs_nm_info *nm_i = NM_I(sbi); 1308 struct f2fs_nat_block *nat_block; 1309 pgoff_t block_off; 1310 pgoff_t block_addr; 1311 int seg_off, entry_off; 1312 nid_t nid; 1313 int ret; 1314 int i = 0; 1315 1316next: 1317 if (i >= nats_in_cursum(sum)) { 1318 sum->n_nats = 0; 1319 return; 1320 } 1321 1322 nid = le32_to_cpu(nid_in_journal(sum, i)); 1323 nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1); 1324 1325 block_off = nid / NAT_ENTRY_PER_BLOCK; 1326 entry_off = nid % NAT_ENTRY_PER_BLOCK; 1327 1328 seg_off = block_off >> sbi->log_blocks_per_seg; 1329 block_addr = (pgoff_t)(nm_i->nat_blkaddr + 1330 (seg_off << sbi->log_blocks_per_seg << 1) + 1331 (block_off & ((1 << sbi->log_blocks_per_seg) - 1))); 1332 1333 if (f2fs_test_bit(block_off, nm_i->nat_bitmap)) 1334 block_addr += sbi->blocks_per_seg; 1335 1336 ret = dev_read_block(nat_block, block_addr); 1337 ASSERT(ret >= 0); 1338 1339 memcpy(&nat_block->entries[entry_off], &nat_in_journal(sum, i), 1340 sizeof(struct f2fs_nat_entry)); 1341 1342 ret = dev_write_block(nat_block, block_addr); 1343 ASSERT(ret >= 0); 1344 free(nat_block); 1345 i++; 1346 goto next; 1347} 1348 1349void flush_journal_entries(struct f2fs_sb_info *sbi) 1350{ 1351 flush_nat_journal_entries(sbi); 1352 flush_sit_journal_entries(sbi); 1353 write_checkpoint(sbi); 1354} 1355 1356void flush_sit_entries(struct f2fs_sb_info *sbi) 1357{ 1358 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi); 1359 struct sit_info *sit_i = SIT_I(sbi); 1360 unsigned int segno = 0; 1361 u32 free_segs = 0; 1362 1363 /* update free segments */ 1364 for (segno = 0; segno < TOTAL_SEGS(sbi); segno++) { 1365 struct f2fs_sit_block *sit_blk; 1366 struct f2fs_sit_entry *sit; 1367 struct seg_entry *se; 1368 1369 se = get_seg_entry(sbi, segno); 1370 1371 if (!se->dirty) 1372 continue; 1373 1374 sit_blk = get_current_sit_page(sbi, segno); 1375 sit = &sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, segno)]; 1376 memcpy(sit->valid_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE); 1377 sit->vblocks = cpu_to_le16((se->type << SIT_VBLOCKS_SHIFT) | 1378 se->valid_blocks); 1379 rewrite_current_sit_page(sbi, segno, sit_blk); 1380 free(sit_blk); 1381 1382 if (se->valid_blocks == 0x0 && 1383 !IS_CUR_SEGNO(sbi, segno, NO_CHECK_TYPE)) 1384 free_segs++; 1385 } 1386 1387 set_cp(free_segment_count, free_segs); 1388} 1389 1390int find_next_free_block(struct f2fs_sb_info *sbi, u64 *to, int left, int type) 1391{ 1392 struct seg_entry *se; 1393 u32 segno; 1394 u64 offset; 1395 1396 if (get_free_segments(sbi) <= SM_I(sbi)->reserved_segments + 1) 1397 return -1; 1398 1399 while (*to >= SM_I(sbi)->main_blkaddr && 1400 *to < F2FS_RAW_SUPER(sbi)->block_count) { 1401 segno = GET_SEGNO(sbi, *to); 1402 offset = OFFSET_IN_SEG(sbi, *to); 1403 1404 se = get_seg_entry(sbi, segno); 1405 1406 if (se->valid_blocks == sbi->blocks_per_seg || 1407 IS_CUR_SEGNO(sbi, segno, type)) { 1408 *to = left ? START_BLOCK(sbi, segno) - 1: 1409 START_BLOCK(sbi, segno + 1); 1410 continue; 1411 } 1412 if (se->valid_blocks == 0 && !(segno % sbi->segs_per_sec)) { 1413 struct seg_entry *se2; 1414 int i; 1415 1416 for (i = 0; i < sbi->segs_per_sec; i++) { 1417 se2 = get_seg_entry(sbi, segno + i); 1418 if (se2->valid_blocks) 1419 break; 1420 } 1421 if (i == sbi->segs_per_sec) 1422 return 0; 1423 } 1424 1425 if (se->type == type && 1426 !f2fs_test_bit(offset, (const char *)se->cur_valid_map)) 1427 return 0; 1428 1429 *to = left ? *to - 1: *to + 1; 1430 } 1431 return -1; 1432} 1433 1434void move_curseg_info(struct f2fs_sb_info *sbi, u64 from) 1435{ 1436 int i, ret; 1437 1438 /* update summary blocks having nullified journal entries */ 1439 for (i = 0; i < NO_CHECK_TYPE; i++) { 1440 struct curseg_info *curseg = CURSEG_I(sbi, i); 1441 u32 old_segno; 1442 u64 ssa_blk, to; 1443 1444 /* update original SSA too */ 1445 ssa_blk = GET_SUM_BLKADDR(sbi, curseg->segno); 1446 ret = dev_write_block(curseg->sum_blk, ssa_blk); 1447 ASSERT(ret >= 0); 1448 1449 to = from; 1450 ret = find_next_free_block(sbi, &to, 0, i); 1451 ASSERT(ret == 0); 1452 1453 old_segno = curseg->segno; 1454 curseg->segno = GET_SEGNO(sbi, to); 1455 curseg->next_blkoff = OFFSET_IN_SEG(sbi, to); 1456 curseg->alloc_type = SSR; 1457 1458 /* update new segno */ 1459 ssa_blk = GET_SUM_BLKADDR(sbi, curseg->segno); 1460 ret = dev_read_block(curseg->sum_blk, ssa_blk); 1461 ASSERT(ret >= 0); 1462 1463 /* update se->types */ 1464 reset_curseg(sbi, i); 1465 1466 DBG(0, "Move curseg[%d] %x -> %x after %"PRIx64"\n", 1467 i, old_segno, curseg->segno, from); 1468 } 1469} 1470 1471void zero_journal_entries(struct f2fs_sb_info *sbi) 1472{ 1473 int i; 1474 1475 for (i = 0; i < NO_CHECK_TYPE; i++) 1476 CURSEG_I(sbi, i)->sum_blk->n_nats = 0; 1477} 1478 1479void write_curseg_info(struct f2fs_sb_info *sbi) 1480{ 1481 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi); 1482 int i; 1483 1484 for (i = 0; i < NO_CHECK_TYPE; i++) { 1485 cp->alloc_type[i] = CURSEG_I(sbi, i)->alloc_type; 1486 if (i < CURSEG_HOT_NODE) { 1487 set_cp(cur_data_segno[i], CURSEG_I(sbi, i)->segno); 1488 set_cp(cur_data_blkoff[i], 1489 CURSEG_I(sbi, i)->next_blkoff); 1490 } else { 1491 int n = i - CURSEG_HOT_NODE; 1492 1493 set_cp(cur_node_segno[n], CURSEG_I(sbi, i)->segno); 1494 set_cp(cur_node_blkoff[n], 1495 CURSEG_I(sbi, i)->next_blkoff); 1496 } 1497 } 1498} 1499 1500int lookup_nat_in_journal(struct f2fs_sb_info *sbi, u32 nid, 1501 struct f2fs_nat_entry *raw_nat) 1502{ 1503 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); 1504 struct f2fs_summary_block *sum = curseg->sum_blk; 1505 int i = 0; 1506 1507 for (i = 0; i < nats_in_cursum(sum); i++) { 1508 if (le32_to_cpu(nid_in_journal(sum, i)) == nid) { 1509 memcpy(raw_nat, &nat_in_journal(sum, i), 1510 sizeof(struct f2fs_nat_entry)); 1511 DBG(3, "==> Found nid [0x%x] in nat cache\n", nid); 1512 return i; 1513 } 1514 } 1515 return -1; 1516} 1517 1518void nullify_nat_entry(struct f2fs_sb_info *sbi, u32 nid) 1519{ 1520 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); 1521 struct f2fs_summary_block *sum = curseg->sum_blk; 1522 struct f2fs_nm_info *nm_i = NM_I(sbi); 1523 struct f2fs_nat_block *nat_block; 1524 pgoff_t block_off; 1525 pgoff_t block_addr; 1526 int seg_off, entry_off; 1527 int ret; 1528 int i = 0; 1529 1530 /* check in journal */ 1531 for (i = 0; i < nats_in_cursum(sum); i++) { 1532 if (le32_to_cpu(nid_in_journal(sum, i)) == nid) { 1533 memset(&nat_in_journal(sum, i), 0, 1534 sizeof(struct f2fs_nat_entry)); 1535 FIX_MSG("Remove nid [0x%x] in nat journal\n", nid); 1536 return; 1537 } 1538 } 1539 nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1); 1540 1541 block_off = nid / NAT_ENTRY_PER_BLOCK; 1542 entry_off = nid % NAT_ENTRY_PER_BLOCK; 1543 1544 seg_off = block_off >> sbi->log_blocks_per_seg; 1545 block_addr = (pgoff_t)(nm_i->nat_blkaddr + 1546 (seg_off << sbi->log_blocks_per_seg << 1) + 1547 (block_off & ((1 << sbi->log_blocks_per_seg) - 1))); 1548 1549 if (f2fs_test_bit(block_off, nm_i->nat_bitmap)) 1550 block_addr += sbi->blocks_per_seg; 1551 1552 ret = dev_read_block(nat_block, block_addr); 1553 ASSERT(ret >= 0); 1554 1555 memset(&nat_block->entries[entry_off], 0, 1556 sizeof(struct f2fs_nat_entry)); 1557 1558 ret = dev_write_block(nat_block, block_addr); 1559 ASSERT(ret >= 0); 1560 free(nat_block); 1561} 1562 1563void write_checkpoint(struct f2fs_sb_info *sbi) 1564{ 1565 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi); 1566 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi); 1567 block_t orphan_blks = 0; 1568 unsigned long long cp_blk_no; 1569 u32 flags = CP_UMOUNT_FLAG; 1570 int i, ret; 1571 u_int32_t crc = 0; 1572 1573 if (is_set_ckpt_flags(cp, CP_ORPHAN_PRESENT_FLAG)) { 1574 orphan_blks = __start_sum_addr(sbi) - 1; 1575 flags |= CP_ORPHAN_PRESENT_FLAG; 1576 } 1577 1578 set_cp(ckpt_flags, flags); 1579 1580 set_cp(free_segment_count, get_free_segments(sbi)); 1581 set_cp(cp_pack_total_block_count, 8 + orphan_blks + get_sb(cp_payload)); 1582 1583 crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, cp, CHECKSUM_OFFSET); 1584 *((__le32 *)((unsigned char *)cp + CHECKSUM_OFFSET)) = cpu_to_le32(crc); 1585 1586 cp_blk_no = get_sb(cp_blkaddr); 1587 if (sbi->cur_cp == 2) 1588 cp_blk_no += 1 << get_sb(log_blocks_per_seg); 1589 1590 /* write the first cp */ 1591 ret = dev_write_block(cp, cp_blk_no++); 1592 ASSERT(ret >= 0); 1593 1594 /* skip payload */ 1595 cp_blk_no += get_sb(cp_payload); 1596 /* skip orphan blocks */ 1597 cp_blk_no += orphan_blks; 1598 1599 /* update summary blocks having nullified journal entries */ 1600 for (i = 0; i < NO_CHECK_TYPE; i++) { 1601 struct curseg_info *curseg = CURSEG_I(sbi, i); 1602 u64 ssa_blk; 1603 1604 ret = dev_write_block(curseg->sum_blk, cp_blk_no++); 1605 ASSERT(ret >= 0); 1606 1607 /* update original SSA too */ 1608 ssa_blk = GET_SUM_BLKADDR(sbi, curseg->segno); 1609 ret = dev_write_block(curseg->sum_blk, ssa_blk); 1610 ASSERT(ret >= 0); 1611 } 1612 1613 /* write the last cp */ 1614 ret = dev_write_block(cp, cp_blk_no++); 1615 ASSERT(ret >= 0); 1616} 1617 1618void build_nat_area_bitmap(struct f2fs_sb_info *sbi) 1619{ 1620 struct f2fs_fsck *fsck = F2FS_FSCK(sbi); 1621 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi); 1622 struct f2fs_nm_info *nm_i = NM_I(sbi); 1623 struct f2fs_nat_block *nat_block; 1624 u32 nid, nr_nat_blks; 1625 pgoff_t block_off; 1626 pgoff_t block_addr; 1627 int seg_off; 1628 int ret; 1629 unsigned int i; 1630 1631 nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1); 1632 ASSERT(nat_block); 1633 1634 /* Alloc & build nat entry bitmap */ 1635 nr_nat_blks = (get_sb(segment_count_nat) / 2) << 1636 sbi->log_blocks_per_seg; 1637 1638 fsck->nr_nat_entries = nr_nat_blks * NAT_ENTRY_PER_BLOCK; 1639 fsck->nat_area_bitmap_sz = (fsck->nr_nat_entries + 7) / 8; 1640 fsck->nat_area_bitmap = calloc(fsck->nat_area_bitmap_sz, 1); 1641 ASSERT(fsck->nat_area_bitmap != NULL); 1642 1643 for (block_off = 0; block_off < nr_nat_blks; block_off++) { 1644 1645 seg_off = block_off >> sbi->log_blocks_per_seg; 1646 block_addr = (pgoff_t)(nm_i->nat_blkaddr + 1647 (seg_off << sbi->log_blocks_per_seg << 1) + 1648 (block_off & ((1 << sbi->log_blocks_per_seg) - 1))); 1649 1650 if (f2fs_test_bit(block_off, nm_i->nat_bitmap)) 1651 block_addr += sbi->blocks_per_seg; 1652 1653 ret = dev_read_block(nat_block, block_addr); 1654 ASSERT(ret >= 0); 1655 1656 nid = block_off * NAT_ENTRY_PER_BLOCK; 1657 for (i = 0; i < NAT_ENTRY_PER_BLOCK; i++) { 1658 struct f2fs_nat_entry raw_nat; 1659 struct node_info ni; 1660 ni.nid = nid + i; 1661 1662 if ((nid + i) == F2FS_NODE_INO(sbi) || 1663 (nid + i) == F2FS_META_INO(sbi)) { 1664 ASSERT(nat_block->entries[i].block_addr != 0x0); 1665 continue; 1666 } 1667 1668 if (lookup_nat_in_journal(sbi, nid + i, 1669 &raw_nat) >= 0) { 1670 node_info_from_raw_nat(&ni, &raw_nat); 1671 if (ni.blk_addr != 0x0) { 1672 f2fs_set_bit(nid + i, 1673 fsck->nat_area_bitmap); 1674 fsck->chk.valid_nat_entry_cnt++; 1675 DBG(3, "nid[0x%x] in nat cache\n", 1676 nid + i); 1677 } 1678 } else { 1679 node_info_from_raw_nat(&ni, 1680 &nat_block->entries[i]); 1681 if (ni.blk_addr == 0) 1682 continue; 1683 ASSERT(nid + i != 0x0); 1684 1685 DBG(3, "nid[0x%8x] addr[0x%16x] ino[0x%8x]\n", 1686 nid + i, ni.blk_addr, ni.ino); 1687 f2fs_set_bit(nid + i, fsck->nat_area_bitmap); 1688 fsck->chk.valid_nat_entry_cnt++; 1689 } 1690 } 1691 } 1692 free(nat_block); 1693 1694 DBG(1, "valid nat entries (block_addr != 0x0) [0x%8x : %u]\n", 1695 fsck->chk.valid_nat_entry_cnt, 1696 fsck->chk.valid_nat_entry_cnt); 1697} 1698 1699int f2fs_do_mount(struct f2fs_sb_info *sbi) 1700{ 1701 struct f2fs_checkpoint *cp = NULL; 1702 int ret; 1703 1704 sbi->active_logs = NR_CURSEG_TYPE; 1705 ret = validate_super_block(sbi, 0); 1706 if (ret) { 1707 ret = validate_super_block(sbi, 1); 1708 if (ret) 1709 return -1; 1710 } 1711 1712 print_raw_sb_info(F2FS_RAW_SUPER(sbi)); 1713 1714 init_sb_info(sbi); 1715 1716 ret = get_valid_checkpoint(sbi); 1717 if (ret) { 1718 ERR_MSG("Can't find valid checkpoint\n"); 1719 return -1; 1720 } 1721 1722 if (sanity_check_ckpt(sbi)) { 1723 ERR_MSG("Checkpoint is polluted\n"); 1724 return -1; 1725 } 1726 1727 print_ckpt_info(sbi); 1728 1729 if (config.auto_fix) { 1730 u32 flag = get_cp(ckpt_flags); 1731 1732 if (flag & CP_FSCK_FLAG) 1733 config.fix_on = 1; 1734 else 1735 return 1; 1736 } 1737 1738 config.bug_on = 0; 1739 1740 cp = F2FS_CKPT(sbi); 1741 sbi->total_valid_node_count = get_cp(valid_node_count); 1742 sbi->total_valid_inode_count = get_cp(valid_inode_count); 1743 sbi->user_block_count = get_cp(user_block_count); 1744 sbi->total_valid_block_count = get_cp(valid_block_count); 1745 sbi->last_valid_block_count = sbi->total_valid_block_count; 1746 sbi->alloc_valid_block_count = 0; 1747 1748 if (build_segment_manager(sbi)) { 1749 ERR_MSG("build_segment_manager failed\n"); 1750 return -1; 1751 } 1752 1753 if (build_node_manager(sbi)) { 1754 ERR_MSG("build_segment_manager failed\n"); 1755 return -1; 1756 } 1757 1758 return 0; 1759} 1760 1761void f2fs_do_umount(struct f2fs_sb_info *sbi) 1762{ 1763 struct sit_info *sit_i = SIT_I(sbi); 1764 struct f2fs_sm_info *sm_i = SM_I(sbi); 1765 struct f2fs_nm_info *nm_i = NM_I(sbi); 1766 unsigned int i; 1767 1768 /* free nm_info */ 1769 free(nm_i->nat_bitmap); 1770 free(sbi->nm_info); 1771 1772 /* free sit_info */ 1773 for (i = 0; i < TOTAL_SEGS(sbi); i++) { 1774 free(sit_i->sentries[i].cur_valid_map); 1775 free(sit_i->sentries[i].ckpt_valid_map); 1776 } 1777 free(sit_i->sit_bitmap); 1778 free(sm_i->sit_info); 1779 1780 /* free sm_info */ 1781 for (i = 0; i < NR_CURSEG_TYPE; i++) 1782 free(sm_i->curseg_array[i].sum_blk); 1783 1784 free(sm_i->curseg_array); 1785 free(sbi->sm_info); 1786 1787 free(sbi->ckpt); 1788 free(sbi->raw_super); 1789} 1790