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 "xattr.h" 13 #include <locale.h> 14 #ifdef HAVE_LINUX_POSIX_ACL_H 15 #include <linux/posix_acl.h> 16 #endif 17 #ifdef HAVE_SYS_ACL_H 18 #include <sys/acl.h> 19 #endif 20 21 #ifndef ACL_UNDEFINED_TAG 22 #define ACL_UNDEFINED_TAG (0x00) 23 #define ACL_USER_OBJ (0x01) 24 #define ACL_USER (0x02) 25 #define ACL_GROUP_OBJ (0x04) 26 #define ACL_GROUP (0x08) 27 #define ACL_MASK (0x10) 28 #define ACL_OTHER (0x20) 29 #endif 30 31 u32 get_free_segments(struct f2fs_sb_info *sbi) 32 { 33 u32 i, free_segs = 0; 34 35 for (i = 0; i < TOTAL_SEGS(sbi); i++) { 36 struct seg_entry *se = get_seg_entry(sbi, i); 37 38 if (se->valid_blocks == 0x0 && !IS_CUR_SEGNO(sbi, i)) 39 free_segs++; 40 } 41 return free_segs; 42 } 43 44 void update_free_segments(struct f2fs_sb_info *sbi) 45 { 46 char *progress = "-*|*-"; 47 static int i = 0; 48 49 if (c.dbg_lv) 50 return; 51 52 MSG(0, "\r [ %c ] Free segments: 0x%x", progress[i % 5], get_free_segments(sbi)); 53 fflush(stdout); 54 i++; 55 } 56 57 #if defined(HAVE_LINUX_POSIX_ACL_H) || defined(HAVE_SYS_ACL_H) 58 void print_acl(char *value, int size) 59 { 60 struct f2fs_acl_header *hdr = (struct f2fs_acl_header *)value; 61 struct f2fs_acl_entry *entry = (struct f2fs_acl_entry *)(hdr + 1); 62 const char *end = value + size; 63 int i, count; 64 65 if (hdr->a_version != cpu_to_le32(F2FS_ACL_VERSION)) { 66 MSG(0, "Invalid ACL version [0x%x : 0x%x]\n", 67 le32_to_cpu(hdr->a_version), F2FS_ACL_VERSION); 68 return; 69 } 70 71 count = f2fs_acl_count(size); 72 if (count <= 0) { 73 MSG(0, "Invalid ACL value size %d\n", size); 74 return; 75 } 76 77 for (i = 0; i < count; i++) { 78 if ((char *)entry > end) { 79 MSG(0, "Invalid ACL entries count %d\n", count); 80 return; 81 } 82 83 switch (le16_to_cpu(entry->e_tag)) { 84 case ACL_USER_OBJ: 85 case ACL_GROUP_OBJ: 86 case ACL_MASK: 87 case ACL_OTHER: 88 MSG(0, "tag:0x%x perm:0x%x\n", 89 le16_to_cpu(entry->e_tag), 90 le16_to_cpu(entry->e_perm)); 91 entry = (struct f2fs_acl_entry *)((char *)entry + 92 sizeof(struct f2fs_acl_entry_short)); 93 break; 94 case ACL_USER: 95 MSG(0, "tag:0x%x perm:0x%x uid:%u\n", 96 le16_to_cpu(entry->e_tag), 97 le16_to_cpu(entry->e_perm), 98 le32_to_cpu(entry->e_id)); 99 entry = (struct f2fs_acl_entry *)((char *)entry + 100 sizeof(struct f2fs_acl_entry)); 101 break; 102 case ACL_GROUP: 103 MSG(0, "tag:0x%x perm:0x%x gid:%u\n", 104 le16_to_cpu(entry->e_tag), 105 le16_to_cpu(entry->e_perm), 106 le32_to_cpu(entry->e_id)); 107 entry = (struct f2fs_acl_entry *)((char *)entry + 108 sizeof(struct f2fs_acl_entry)); 109 break; 110 default: 111 MSG(0, "Unknown ACL tag 0x%x\n", 112 le16_to_cpu(entry->e_tag)); 113 return; 114 } 115 } 116 } 117 #else 118 #define print_acl(value, size) do { \ 119 int i; \ 120 for (i = 0; i < size; i++) \ 121 MSG(0, "%02X", value[i]); \ 122 MSG(0, "\n"); \ 123 } while (0) 124 #endif 125 126 void print_xattr_entry(struct f2fs_xattr_entry *ent) 127 { 128 char *value = (char *)(ent->e_name + le16_to_cpu(ent->e_name_len)); 129 struct fscrypt_context *ctx; 130 int i; 131 132 MSG(0, "\nxattr: e_name_index:%d e_name:", ent->e_name_index); 133 for (i = 0; i < le16_to_cpu(ent->e_name_len); i++) 134 MSG(0, "%c", ent->e_name[i]); 135 MSG(0, " e_name_len:%d e_value_size:%d e_value:\n", 136 ent->e_name_len, le16_to_cpu(ent->e_value_size)); 137 138 switch (ent->e_name_index) { 139 case F2FS_XATTR_INDEX_POSIX_ACL_ACCESS: 140 case F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT: 141 print_acl(value, le16_to_cpu(ent->e_value_size)); 142 break; 143 case F2FS_XATTR_INDEX_USER: 144 case F2FS_XATTR_INDEX_SECURITY: 145 case F2FS_XATTR_INDEX_TRUSTED: 146 case F2FS_XATTR_INDEX_LUSTRE: 147 for (i = 0; i < le16_to_cpu(ent->e_value_size); i++) 148 MSG(0, "%02X", value[i]); 149 MSG(0, "\n"); 150 break; 151 case F2FS_XATTR_INDEX_ENCRYPTION: 152 ctx = (struct fscrypt_context *)value; 153 MSG(0, "format: %d\n", ctx->format); 154 MSG(0, "contents_encryption_mode: 0x%x\n", ctx->contents_encryption_mode); 155 MSG(0, "filenames_encryption_mode: 0x%x\n", ctx->filenames_encryption_mode); 156 MSG(0, "flags: 0x%x\n", ctx->flags); 157 MSG(0, "master_key_descriptor: "); 158 for (i = 0; i < FS_KEY_DESCRIPTOR_SIZE; i++) 159 MSG(0, "%02X", ctx->master_key_descriptor[i]); 160 MSG(0, "\nnonce: "); 161 for (i = 0; i < FS_KEY_DERIVATION_NONCE_SIZE; i++) 162 MSG(0, "%02X", ctx->nonce[i]); 163 MSG(0, "\n"); 164 break; 165 default: 166 break; 167 } 168 } 169 170 void print_inode_info(struct f2fs_sb_info *sbi, 171 struct f2fs_node *node, int name) 172 { 173 struct f2fs_inode *inode = &node->i; 174 void *xattr_addr; 175 struct f2fs_xattr_entry *ent; 176 unsigned char en[F2FS_NAME_LEN + 1]; 177 unsigned int i = 0; 178 u32 namelen = le32_to_cpu(inode->i_namelen); 179 int enc_name = file_enc_name(inode); 180 int ofs = __get_extra_isize(inode); 181 182 namelen = convert_encrypted_name(inode->i_name, namelen, en, enc_name); 183 en[namelen] = '\0'; 184 if (name && namelen) { 185 inode->i_name[namelen] = '\0'; 186 MSG(0, " - File name : %s%s\n", en, 187 enc_name ? " <encrypted>" : ""); 188 setlocale(LC_ALL, ""); 189 MSG(0, " - File size : %'llu (bytes)\n", 190 le64_to_cpu(inode->i_size)); 191 return; 192 } 193 194 DISP_u32(inode, i_mode); 195 DISP_u32(inode, i_advise); 196 DISP_u32(inode, i_uid); 197 DISP_u32(inode, i_gid); 198 DISP_u32(inode, i_links); 199 DISP_u64(inode, i_size); 200 DISP_u64(inode, i_blocks); 201 202 DISP_u64(inode, i_atime); 203 DISP_u32(inode, i_atime_nsec); 204 DISP_u64(inode, i_ctime); 205 DISP_u32(inode, i_ctime_nsec); 206 DISP_u64(inode, i_mtime); 207 DISP_u32(inode, i_mtime_nsec); 208 209 DISP_u32(inode, i_generation); 210 DISP_u32(inode, i_current_depth); 211 DISP_u32(inode, i_xattr_nid); 212 DISP_u32(inode, i_flags); 213 DISP_u32(inode, i_inline); 214 DISP_u32(inode, i_pino); 215 DISP_u32(inode, i_dir_level); 216 217 if (namelen) { 218 DISP_u32(inode, i_namelen); 219 printf("%-30s\t\t[%s]\n", "i_name", en); 220 } 221 222 printf("i_ext: fofs:%x blkaddr:%x len:%x\n", 223 le32_to_cpu(inode->i_ext.fofs), 224 le32_to_cpu(inode->i_ext.blk_addr), 225 le32_to_cpu(inode->i_ext.len)); 226 227 if (c.feature & cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR)) { 228 DISP_u16(inode, i_extra_isize); 229 if (c.feature & cpu_to_le32(F2FS_FEATURE_FLEXIBLE_INLINE_XATTR)) 230 DISP_u16(inode, i_inline_xattr_size); 231 if (c.feature & cpu_to_le32(F2FS_FEATURE_PRJQUOTA)) 232 DISP_u32(inode, i_projid); 233 if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM)) 234 DISP_u32(inode, i_inode_checksum); 235 if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CRTIME)) { 236 DISP_u64(inode, i_crtime); 237 DISP_u32(inode, i_crtime_nsec); 238 } 239 } 240 241 DISP_u32(inode, i_addr[ofs]); /* Pointers to data blocks */ 242 DISP_u32(inode, i_addr[ofs + 1]); /* Pointers to data blocks */ 243 DISP_u32(inode, i_addr[ofs + 2]); /* Pointers to data blocks */ 244 DISP_u32(inode, i_addr[ofs + 3]); /* Pointers to data blocks */ 245 246 for (i = ofs + 3; i < ADDRS_PER_INODE(inode); i++) { 247 if (inode->i_addr[i] == 0x0) 248 break; 249 printf("i_addr[0x%x] points data block\t\t[0x%4x]\n", 250 i, le32_to_cpu(inode->i_addr[i])); 251 } 252 253 DISP_u32(inode, i_nid[0]); /* direct */ 254 DISP_u32(inode, i_nid[1]); /* direct */ 255 DISP_u32(inode, i_nid[2]); /* indirect */ 256 DISP_u32(inode, i_nid[3]); /* indirect */ 257 DISP_u32(inode, i_nid[4]); /* double indirect */ 258 259 xattr_addr = read_all_xattrs(sbi, node); 260 list_for_each_xattr(ent, xattr_addr) { 261 print_xattr_entry(ent); 262 } 263 free(xattr_addr); 264 265 printf("\n"); 266 } 267 268 void print_node_info(struct f2fs_sb_info *sbi, 269 struct f2fs_node *node_block, int verbose) 270 { 271 nid_t ino = le32_to_cpu(node_block->footer.ino); 272 nid_t nid = le32_to_cpu(node_block->footer.nid); 273 /* Is this inode? */ 274 if (ino == nid) { 275 DBG(verbose, "Node ID [0x%x:%u] is inode\n", nid, nid); 276 print_inode_info(sbi, node_block, verbose); 277 } else { 278 int i; 279 u32 *dump_blk = (u32 *)node_block; 280 DBG(verbose, 281 "Node ID [0x%x:%u] is direct node or indirect node.\n", 282 nid, nid); 283 for (i = 0; i <= 10; i++) 284 MSG(verbose, "[%d]\t\t\t[0x%8x : %d]\n", 285 i, dump_blk[i], dump_blk[i]); 286 } 287 } 288 289 static void DISP_label(u_int16_t *name) 290 { 291 char buffer[MAX_VOLUME_NAME]; 292 293 utf16_to_utf8(buffer, name, MAX_VOLUME_NAME, MAX_VOLUME_NAME); 294 printf("%-30s" "\t\t[%s]\n", "volum_name", buffer); 295 } 296 297 void print_raw_sb_info(struct f2fs_super_block *sb) 298 { 299 if (!c.dbg_lv) 300 return; 301 302 printf("\n"); 303 printf("+--------------------------------------------------------+\n"); 304 printf("| Super block |\n"); 305 printf("+--------------------------------------------------------+\n"); 306 307 DISP_u32(sb, magic); 308 DISP_u32(sb, major_ver); 309 310 DISP_label(sb->volume_name); 311 312 DISP_u32(sb, minor_ver); 313 DISP_u32(sb, log_sectorsize); 314 DISP_u32(sb, log_sectors_per_block); 315 316 DISP_u32(sb, log_blocksize); 317 DISP_u32(sb, log_blocks_per_seg); 318 DISP_u32(sb, segs_per_sec); 319 DISP_u32(sb, secs_per_zone); 320 DISP_u32(sb, checksum_offset); 321 DISP_u64(sb, block_count); 322 323 DISP_u32(sb, section_count); 324 DISP_u32(sb, segment_count); 325 DISP_u32(sb, segment_count_ckpt); 326 DISP_u32(sb, segment_count_sit); 327 DISP_u32(sb, segment_count_nat); 328 329 DISP_u32(sb, segment_count_ssa); 330 DISP_u32(sb, segment_count_main); 331 DISP_u32(sb, segment0_blkaddr); 332 333 DISP_u32(sb, cp_blkaddr); 334 DISP_u32(sb, sit_blkaddr); 335 DISP_u32(sb, nat_blkaddr); 336 DISP_u32(sb, ssa_blkaddr); 337 DISP_u32(sb, main_blkaddr); 338 339 DISP_u32(sb, root_ino); 340 DISP_u32(sb, node_ino); 341 DISP_u32(sb, meta_ino); 342 DISP_u32(sb, cp_payload); 343 DISP_u32(sb, crc); 344 DISP("%-.256s", sb, version); 345 printf("\n"); 346 } 347 348 void print_ckpt_info(struct f2fs_sb_info *sbi) 349 { 350 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi); 351 352 if (!c.dbg_lv) 353 return; 354 355 printf("\n"); 356 printf("+--------------------------------------------------------+\n"); 357 printf("| Checkpoint |\n"); 358 printf("+--------------------------------------------------------+\n"); 359 360 DISP_u64(cp, checkpoint_ver); 361 DISP_u64(cp, user_block_count); 362 DISP_u64(cp, valid_block_count); 363 DISP_u32(cp, rsvd_segment_count); 364 DISP_u32(cp, overprov_segment_count); 365 DISP_u32(cp, free_segment_count); 366 367 DISP_u32(cp, alloc_type[CURSEG_HOT_NODE]); 368 DISP_u32(cp, alloc_type[CURSEG_WARM_NODE]); 369 DISP_u32(cp, alloc_type[CURSEG_COLD_NODE]); 370 DISP_u32(cp, cur_node_segno[0]); 371 DISP_u32(cp, cur_node_segno[1]); 372 DISP_u32(cp, cur_node_segno[2]); 373 374 DISP_u32(cp, cur_node_blkoff[0]); 375 DISP_u32(cp, cur_node_blkoff[1]); 376 DISP_u32(cp, cur_node_blkoff[2]); 377 378 379 DISP_u32(cp, alloc_type[CURSEG_HOT_DATA]); 380 DISP_u32(cp, alloc_type[CURSEG_WARM_DATA]); 381 DISP_u32(cp, alloc_type[CURSEG_COLD_DATA]); 382 DISP_u32(cp, cur_data_segno[0]); 383 DISP_u32(cp, cur_data_segno[1]); 384 DISP_u32(cp, cur_data_segno[2]); 385 386 DISP_u32(cp, cur_data_blkoff[0]); 387 DISP_u32(cp, cur_data_blkoff[1]); 388 DISP_u32(cp, cur_data_blkoff[2]); 389 390 DISP_u32(cp, ckpt_flags); 391 DISP_u32(cp, cp_pack_total_block_count); 392 DISP_u32(cp, cp_pack_start_sum); 393 DISP_u32(cp, valid_node_count); 394 DISP_u32(cp, valid_inode_count); 395 DISP_u32(cp, next_free_nid); 396 DISP_u32(cp, sit_ver_bitmap_bytesize); 397 DISP_u32(cp, nat_ver_bitmap_bytesize); 398 DISP_u32(cp, checksum_offset); 399 DISP_u64(cp, elapsed_time); 400 401 DISP_u32(cp, sit_nat_version_bitmap[0]); 402 printf("\n\n"); 403 } 404 405 void print_cp_state(u32 flag) 406 { 407 MSG(0, "Info: checkpoint state = %x : ", flag); 408 if (flag & CP_QUOTA_NEED_FSCK_FLAG) 409 MSG(0, "%s", " quota_need_fsck"); 410 if (flag & CP_LARGE_NAT_BITMAP_FLAG) 411 MSG(0, "%s", " large_nat_bitmap"); 412 if (flag & CP_NOCRC_RECOVERY_FLAG) 413 MSG(0, "%s", " allow_nocrc"); 414 if (flag & CP_TRIMMED_FLAG) 415 MSG(0, "%s", " trimmed"); 416 if (flag & CP_NAT_BITS_FLAG) 417 MSG(0, "%s", " nat_bits"); 418 if (flag & CP_CRC_RECOVERY_FLAG) 419 MSG(0, "%s", " crc"); 420 if (flag & CP_FASTBOOT_FLAG) 421 MSG(0, "%s", " fastboot"); 422 if (flag & CP_FSCK_FLAG) 423 MSG(0, "%s", " fsck"); 424 if (flag & CP_ERROR_FLAG) 425 MSG(0, "%s", " error"); 426 if (flag & CP_COMPACT_SUM_FLAG) 427 MSG(0, "%s", " compacted_summary"); 428 if (flag & CP_ORPHAN_PRESENT_FLAG) 429 MSG(0, "%s", " orphan_inodes"); 430 if (flag & CP_DISABLED_FLAG) 431 MSG(0, "%s", " disabled"); 432 if (flag & CP_UMOUNT_FLAG) 433 MSG(0, "%s", " unmount"); 434 else 435 MSG(0, "%s", " sudden-power-off"); 436 MSG(0, "\n"); 437 } 438 439 void print_sb_state(struct f2fs_super_block *sb) 440 { 441 __le32 f = sb->feature; 442 int i; 443 444 MSG(0, "Info: superblock features = %x : ", f); 445 if (f & cpu_to_le32(F2FS_FEATURE_ENCRYPT)) { 446 MSG(0, "%s", " encrypt"); 447 } 448 if (f & cpu_to_le32(F2FS_FEATURE_VERITY)) { 449 MSG(0, "%s", " verity"); 450 } 451 if (f & cpu_to_le32(F2FS_FEATURE_BLKZONED)) { 452 MSG(0, "%s", " blkzoned"); 453 } 454 if (f & cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR)) { 455 MSG(0, "%s", " extra_attr"); 456 } 457 if (f & cpu_to_le32(F2FS_FEATURE_PRJQUOTA)) { 458 MSG(0, "%s", " project_quota"); 459 } 460 if (f & cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM)) { 461 MSG(0, "%s", " inode_checksum"); 462 } 463 if (f & cpu_to_le32(F2FS_FEATURE_FLEXIBLE_INLINE_XATTR)) { 464 MSG(0, "%s", " flexible_inline_xattr"); 465 } 466 if (f & cpu_to_le32(F2FS_FEATURE_QUOTA_INO)) { 467 MSG(0, "%s", " quota_ino"); 468 } 469 if (f & cpu_to_le32(F2FS_FEATURE_INODE_CRTIME)) { 470 MSG(0, "%s", " inode_crtime"); 471 } 472 if (f & cpu_to_le32(F2FS_FEATURE_LOST_FOUND)) { 473 MSG(0, "%s", " lost_found"); 474 } 475 if (f & cpu_to_le32(F2FS_FEATURE_SB_CHKSUM)) { 476 MSG(0, "%s", " sb_checksum"); 477 } 478 MSG(0, "\n"); 479 MSG(0, "Info: superblock encrypt level = %d, salt = ", 480 sb->encryption_level); 481 for (i = 0; i < 16; i++) 482 MSG(0, "%02x", sb->encrypt_pw_salt[i]); 483 MSG(0, "\n"); 484 } 485 486 void update_superblock(struct f2fs_super_block *sb, int sb_mask) 487 { 488 int addr, ret; 489 u_int8_t *buf; 490 u32 old_crc, new_crc; 491 492 buf = calloc(BLOCK_SZ, 1); 493 ASSERT(buf); 494 495 if (get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) { 496 old_crc = get_sb(crc); 497 new_crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, sb, 498 SB_CHKSUM_OFFSET); 499 set_sb(crc, new_crc); 500 MSG(1, "Info: SB CRC is updated (0x%x -> 0x%x)\n", 501 old_crc, new_crc); 502 } 503 504 memcpy(buf + F2FS_SUPER_OFFSET, sb, sizeof(*sb)); 505 for (addr = SB0_ADDR; addr < SB_MAX_ADDR; addr++) { 506 if (SB_MASK(addr) & sb_mask) { 507 ret = dev_write_block(buf, addr); 508 ASSERT(ret >= 0); 509 } 510 } 511 512 free(buf); 513 DBG(0, "Info: Done to update superblock\n"); 514 } 515 516 static inline int sanity_check_area_boundary(struct f2fs_super_block *sb, 517 enum SB_ADDR sb_addr) 518 { 519 u32 segment0_blkaddr = get_sb(segment0_blkaddr); 520 u32 cp_blkaddr = get_sb(cp_blkaddr); 521 u32 sit_blkaddr = get_sb(sit_blkaddr); 522 u32 nat_blkaddr = get_sb(nat_blkaddr); 523 u32 ssa_blkaddr = get_sb(ssa_blkaddr); 524 u32 main_blkaddr = get_sb(main_blkaddr); 525 u32 segment_count_ckpt = get_sb(segment_count_ckpt); 526 u32 segment_count_sit = get_sb(segment_count_sit); 527 u32 segment_count_nat = get_sb(segment_count_nat); 528 u32 segment_count_ssa = get_sb(segment_count_ssa); 529 u32 segment_count_main = get_sb(segment_count_main); 530 u32 segment_count = get_sb(segment_count); 531 u32 log_blocks_per_seg = get_sb(log_blocks_per_seg); 532 u64 main_end_blkaddr = main_blkaddr + 533 (segment_count_main << log_blocks_per_seg); 534 u64 seg_end_blkaddr = segment0_blkaddr + 535 (segment_count << log_blocks_per_seg); 536 537 if (segment0_blkaddr != cp_blkaddr) { 538 MSG(0, "\tMismatch segment0(%u) cp_blkaddr(%u)\n", 539 segment0_blkaddr, cp_blkaddr); 540 return -1; 541 } 542 543 if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) != 544 sit_blkaddr) { 545 MSG(0, "\tWrong CP boundary, start(%u) end(%u) blocks(%u)\n", 546 cp_blkaddr, sit_blkaddr, 547 segment_count_ckpt << log_blocks_per_seg); 548 return -1; 549 } 550 551 if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) != 552 nat_blkaddr) { 553 MSG(0, "\tWrong SIT boundary, start(%u) end(%u) blocks(%u)\n", 554 sit_blkaddr, nat_blkaddr, 555 segment_count_sit << log_blocks_per_seg); 556 return -1; 557 } 558 559 if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) != 560 ssa_blkaddr) { 561 MSG(0, "\tWrong NAT boundary, start(%u) end(%u) blocks(%u)\n", 562 nat_blkaddr, ssa_blkaddr, 563 segment_count_nat << log_blocks_per_seg); 564 return -1; 565 } 566 567 if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) != 568 main_blkaddr) { 569 MSG(0, "\tWrong SSA boundary, start(%u) end(%u) blocks(%u)\n", 570 ssa_blkaddr, main_blkaddr, 571 segment_count_ssa << log_blocks_per_seg); 572 return -1; 573 } 574 575 if (main_end_blkaddr > seg_end_blkaddr) { 576 MSG(0, "\tWrong MAIN_AREA, start(%u) end(%u) block(%u)\n", 577 main_blkaddr, 578 segment0_blkaddr + 579 (segment_count << log_blocks_per_seg), 580 segment_count_main << log_blocks_per_seg); 581 return -1; 582 } else if (main_end_blkaddr < seg_end_blkaddr) { 583 set_sb(segment_count, (main_end_blkaddr - 584 segment0_blkaddr) >> log_blocks_per_seg); 585 586 update_superblock(sb, SB_MASK(sb_addr)); 587 MSG(0, "Info: Fix alignment: start(%u) end(%u) block(%u)\n", 588 main_blkaddr, 589 segment0_blkaddr + 590 (segment_count << log_blocks_per_seg), 591 segment_count_main << log_blocks_per_seg); 592 } 593 return 0; 594 } 595 596 static int verify_sb_chksum(struct f2fs_super_block *sb) 597 { 598 if (SB_CHKSUM_OFFSET != get_sb(checksum_offset)) { 599 MSG(0, "\tInvalid SB CRC offset: %u\n", 600 get_sb(checksum_offset)); 601 return -1; 602 } 603 if (f2fs_crc_valid(get_sb(crc), sb, 604 get_sb(checksum_offset))) { 605 MSG(0, "\tInvalid SB CRC: 0x%x\n", get_sb(crc)); 606 return -1; 607 } 608 return 0; 609 } 610 611 int sanity_check_raw_super(struct f2fs_super_block *sb, enum SB_ADDR sb_addr) 612 { 613 unsigned int blocksize; 614 615 if ((get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) && 616 verify_sb_chksum(sb)) 617 return -1; 618 619 if (F2FS_SUPER_MAGIC != get_sb(magic)) 620 return -1; 621 622 if (F2FS_BLKSIZE != PAGE_CACHE_SIZE) 623 return -1; 624 625 blocksize = 1 << get_sb(log_blocksize); 626 if (F2FS_BLKSIZE != blocksize) 627 return -1; 628 629 /* check log blocks per segment */ 630 if (get_sb(log_blocks_per_seg) != 9) 631 return -1; 632 633 /* Currently, support 512/1024/2048/4096 bytes sector size */ 634 if (get_sb(log_sectorsize) > F2FS_MAX_LOG_SECTOR_SIZE || 635 get_sb(log_sectorsize) < F2FS_MIN_LOG_SECTOR_SIZE) 636 return -1; 637 638 if (get_sb(log_sectors_per_block) + get_sb(log_sectorsize) != 639 F2FS_MAX_LOG_SECTOR_SIZE) 640 return -1; 641 642 /* check reserved ino info */ 643 if (get_sb(node_ino) != 1 || get_sb(meta_ino) != 2 || 644 get_sb(root_ino) != 3) 645 return -1; 646 647 /* Check zoned block device feature */ 648 if (c.devices[0].zoned_model == F2FS_ZONED_HM && 649 !(sb->feature & cpu_to_le32(F2FS_FEATURE_BLKZONED))) { 650 MSG(0, "\tMissing zoned block device feature\n"); 651 return -1; 652 } 653 654 if (get_sb(segment_count) > F2FS_MAX_SEGMENT) 655 return -1; 656 657 if (sanity_check_area_boundary(sb, sb_addr)) 658 return -1; 659 return 0; 660 } 661 662 int validate_super_block(struct f2fs_sb_info *sbi, enum SB_ADDR sb_addr) 663 { 664 char buf[F2FS_BLKSIZE]; 665 666 sbi->raw_super = malloc(sizeof(struct f2fs_super_block)); 667 if (!sbi->raw_super) 668 return -ENOMEM; 669 670 if (dev_read_block(buf, sb_addr)) 671 return -1; 672 673 memcpy(sbi->raw_super, buf + F2FS_SUPER_OFFSET, 674 sizeof(struct f2fs_super_block)); 675 676 if (!sanity_check_raw_super(sbi->raw_super, sb_addr)) { 677 /* get kernel version */ 678 if (c.kd >= 0) { 679 dev_read_version(c.version, 0, VERSION_LEN); 680 get_kernel_version(c.version); 681 } else { 682 get_kernel_uname_version(c.version); 683 } 684 685 /* build sb version */ 686 memcpy(c.sb_version, sbi->raw_super->version, VERSION_LEN); 687 get_kernel_version(c.sb_version); 688 memcpy(c.init_version, sbi->raw_super->init_version, VERSION_LEN); 689 get_kernel_version(c.init_version); 690 691 MSG(0, "Info: MKFS version\n \"%s\"\n", c.init_version); 692 MSG(0, "Info: FSCK version\n from \"%s\"\n to \"%s\"\n", 693 c.sb_version, c.version); 694 if (memcmp(c.sb_version, c.version, VERSION_LEN)) { 695 memcpy(sbi->raw_super->version, 696 c.version, VERSION_LEN); 697 update_superblock(sbi->raw_super, SB_MASK(sb_addr)); 698 699 c.auto_fix = 0; 700 c.fix_on = 1; 701 } 702 print_sb_state(sbi->raw_super); 703 return 0; 704 } 705 706 free(sbi->raw_super); 707 sbi->raw_super = NULL; 708 MSG(0, "\tCan't find a valid F2FS superblock at 0x%x\n", sb_addr); 709 710 return -EINVAL; 711 } 712 713 int init_sb_info(struct f2fs_sb_info *sbi) 714 { 715 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi); 716 u64 total_sectors; 717 int i; 718 719 sbi->log_sectors_per_block = get_sb(log_sectors_per_block); 720 sbi->log_blocksize = get_sb(log_blocksize); 721 sbi->blocksize = 1 << sbi->log_blocksize; 722 sbi->log_blocks_per_seg = get_sb(log_blocks_per_seg); 723 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg; 724 sbi->segs_per_sec = get_sb(segs_per_sec); 725 sbi->secs_per_zone = get_sb(secs_per_zone); 726 sbi->total_sections = get_sb(section_count); 727 sbi->total_node_count = (get_sb(segment_count_nat) / 2) * 728 sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK; 729 sbi->root_ino_num = get_sb(root_ino); 730 sbi->node_ino_num = get_sb(node_ino); 731 sbi->meta_ino_num = get_sb(meta_ino); 732 sbi->cur_victim_sec = NULL_SEGNO; 733 734 for (i = 0; i < MAX_DEVICES; i++) { 735 if (!sb->devs[i].path[0]) 736 break; 737 738 if (i) { 739 c.devices[i].path = strdup((char *)sb->devs[i].path); 740 if (get_device_info(i)) 741 ASSERT(0); 742 } else { 743 ASSERT(!strcmp((char *)sb->devs[i].path, 744 (char *)c.devices[i].path)); 745 } 746 747 c.devices[i].total_segments = 748 le32_to_cpu(sb->devs[i].total_segments); 749 if (i) 750 c.devices[i].start_blkaddr = 751 c.devices[i - 1].end_blkaddr + 1; 752 c.devices[i].end_blkaddr = c.devices[i].start_blkaddr + 753 c.devices[i].total_segments * 754 c.blks_per_seg - 1; 755 if (i == 0) 756 c.devices[i].end_blkaddr += get_sb(segment0_blkaddr); 757 758 c.ndevs = i + 1; 759 MSG(0, "Info: Device[%d] : %s blkaddr = %"PRIx64"--%"PRIx64"\n", 760 i, c.devices[i].path, 761 c.devices[i].start_blkaddr, 762 c.devices[i].end_blkaddr); 763 } 764 765 total_sectors = get_sb(block_count) << sbi->log_sectors_per_block; 766 MSG(0, "Info: total FS sectors = %"PRIu64" (%"PRIu64" MB)\n", 767 total_sectors, total_sectors >> 768 (20 - get_sb(log_sectorsize))); 769 return 0; 770 } 771 772 void *validate_checkpoint(struct f2fs_sb_info *sbi, block_t cp_addr, 773 unsigned long long *version) 774 { 775 void *cp_page_1, *cp_page_2; 776 struct f2fs_checkpoint *cp; 777 unsigned long blk_size = sbi->blocksize; 778 unsigned long long cur_version = 0, pre_version = 0; 779 unsigned int crc = 0; 780 size_t crc_offset; 781 782 /* Read the 1st cp block in this CP pack */ 783 cp_page_1 = malloc(PAGE_SIZE); 784 ASSERT(cp_page_1); 785 786 if (dev_read_block(cp_page_1, cp_addr) < 0) 787 goto invalid_cp1; 788 789 cp = (struct f2fs_checkpoint *)cp_page_1; 790 crc_offset = get_cp(checksum_offset); 791 if (crc_offset > (blk_size - sizeof(__le32))) 792 goto invalid_cp1; 793 794 crc = le32_to_cpu(*(__le32 *)((unsigned char *)cp + crc_offset)); 795 if (f2fs_crc_valid(crc, cp, crc_offset)) 796 goto invalid_cp1; 797 798 if (get_cp(cp_pack_total_block_count) > sbi->blocks_per_seg) 799 goto invalid_cp1; 800 801 pre_version = get_cp(checkpoint_ver); 802 803 /* Read the 2nd cp block in this CP pack */ 804 cp_page_2 = malloc(PAGE_SIZE); 805 ASSERT(cp_page_2); 806 807 cp_addr += get_cp(cp_pack_total_block_count) - 1; 808 809 if (dev_read_block(cp_page_2, cp_addr) < 0) 810 goto invalid_cp2; 811 812 cp = (struct f2fs_checkpoint *)cp_page_2; 813 crc_offset = get_cp(checksum_offset); 814 if (crc_offset > (blk_size - sizeof(__le32))) 815 goto invalid_cp2; 816 817 crc = le32_to_cpu(*(__le32 *)((unsigned char *)cp + crc_offset)); 818 if (f2fs_crc_valid(crc, cp, crc_offset)) 819 goto invalid_cp2; 820 821 cur_version = get_cp(checkpoint_ver); 822 823 if (cur_version == pre_version) { 824 *version = cur_version; 825 free(cp_page_2); 826 return cp_page_1; 827 } 828 829 invalid_cp2: 830 free(cp_page_2); 831 invalid_cp1: 832 free(cp_page_1); 833 return NULL; 834 } 835 836 int get_valid_checkpoint(struct f2fs_sb_info *sbi) 837 { 838 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi); 839 void *cp1, *cp2, *cur_page; 840 unsigned long blk_size = sbi->blocksize; 841 unsigned long long cp1_version = 0, cp2_version = 0, version; 842 unsigned long long cp_start_blk_no; 843 unsigned int cp_payload, cp_blks; 844 int ret; 845 846 cp_payload = get_sb(cp_payload); 847 if (cp_payload > F2FS_BLK_ALIGN(MAX_SIT_BITMAP_SIZE)) 848 return -EINVAL; 849 850 cp_blks = 1 + cp_payload; 851 sbi->ckpt = malloc(cp_blks * blk_size); 852 if (!sbi->ckpt) 853 return -ENOMEM; 854 /* 855 * Finding out valid cp block involves read both 856 * sets( cp pack1 and cp pack 2) 857 */ 858 cp_start_blk_no = get_sb(cp_blkaddr); 859 cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version); 860 861 /* The second checkpoint pack should start at the next segment */ 862 cp_start_blk_no += 1 << get_sb(log_blocks_per_seg); 863 cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version); 864 865 if (cp1 && cp2) { 866 if (ver_after(cp2_version, cp1_version)) { 867 cur_page = cp2; 868 sbi->cur_cp = 2; 869 version = cp2_version; 870 } else { 871 cur_page = cp1; 872 sbi->cur_cp = 1; 873 version = cp1_version; 874 } 875 } else if (cp1) { 876 cur_page = cp1; 877 sbi->cur_cp = 1; 878 version = cp1_version; 879 } else if (cp2) { 880 cur_page = cp2; 881 sbi->cur_cp = 2; 882 version = cp2_version; 883 } else 884 goto fail_no_cp; 885 886 MSG(0, "Info: CKPT version = %llx\n", version); 887 888 memcpy(sbi->ckpt, cur_page, blk_size); 889 890 if (cp_blks > 1) { 891 unsigned int i; 892 unsigned long long cp_blk_no; 893 894 cp_blk_no = get_sb(cp_blkaddr); 895 if (cur_page == cp2) 896 cp_blk_no += 1 << get_sb(log_blocks_per_seg); 897 898 /* copy sit bitmap */ 899 for (i = 1; i < cp_blks; i++) { 900 unsigned char *ckpt = (unsigned char *)sbi->ckpt; 901 ret = dev_read_block(cur_page, cp_blk_no + i); 902 ASSERT(ret >= 0); 903 memcpy(ckpt + i * blk_size, cur_page, blk_size); 904 } 905 } 906 if (cp1) 907 free(cp1); 908 if (cp2) 909 free(cp2); 910 return 0; 911 912 fail_no_cp: 913 free(sbi->ckpt); 914 sbi->ckpt = NULL; 915 return -EINVAL; 916 } 917 918 int sanity_check_ckpt(struct f2fs_sb_info *sbi) 919 { 920 unsigned int total, fsmeta; 921 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi); 922 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi); 923 unsigned int ovp_segments, reserved_segments; 924 unsigned int main_segs, blocks_per_seg; 925 unsigned int sit_segs, nat_segs; 926 unsigned int sit_bitmap_size, nat_bitmap_size; 927 unsigned int log_blocks_per_seg; 928 unsigned int segment_count_main; 929 unsigned int cp_pack_start_sum, cp_payload; 930 block_t user_block_count; 931 int i; 932 933 total = get_sb(segment_count); 934 fsmeta = get_sb(segment_count_ckpt); 935 sit_segs = get_sb(segment_count_sit); 936 fsmeta += sit_segs; 937 nat_segs = get_sb(segment_count_nat); 938 fsmeta += nat_segs; 939 fsmeta += get_cp(rsvd_segment_count); 940 fsmeta += get_sb(segment_count_ssa); 941 942 if (fsmeta >= total) 943 return 1; 944 945 ovp_segments = get_cp(overprov_segment_count); 946 reserved_segments = get_cp(rsvd_segment_count); 947 948 if (fsmeta < F2FS_MIN_SEGMENT || ovp_segments == 0 || 949 reserved_segments == 0) { 950 MSG(0, "\tWrong layout: check mkfs.f2fs version\n"); 951 return 1; 952 } 953 954 user_block_count = get_cp(user_block_count); 955 segment_count_main = get_sb(segment_count_main); 956 log_blocks_per_seg = get_sb(log_blocks_per_seg); 957 if (!user_block_count || user_block_count >= 958 segment_count_main << log_blocks_per_seg) { 959 MSG(0, "\tWrong user_block_count(%u)\n", user_block_count); 960 return 1; 961 } 962 963 main_segs = get_sb(segment_count_main); 964 blocks_per_seg = sbi->blocks_per_seg; 965 966 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) { 967 if (get_cp(cur_node_segno[i]) >= main_segs || 968 get_cp(cur_node_blkoff[i]) >= blocks_per_seg) 969 return 1; 970 } 971 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) { 972 if (get_cp(cur_data_segno[i]) >= main_segs || 973 get_cp(cur_data_blkoff[i]) >= blocks_per_seg) 974 return 1; 975 } 976 977 sit_bitmap_size = get_cp(sit_ver_bitmap_bytesize); 978 nat_bitmap_size = get_cp(nat_ver_bitmap_bytesize); 979 980 if (sit_bitmap_size != ((sit_segs / 2) << log_blocks_per_seg) / 8 || 981 nat_bitmap_size != ((nat_segs / 2) << log_blocks_per_seg) / 8) { 982 MSG(0, "\tWrong bitmap size: sit(%u), nat(%u)\n", 983 sit_bitmap_size, nat_bitmap_size); 984 return 1; 985 } 986 987 cp_pack_start_sum = __start_sum_addr(sbi); 988 cp_payload = __cp_payload(sbi); 989 if (cp_pack_start_sum < cp_payload + 1 || 990 cp_pack_start_sum > blocks_per_seg - 1 - 991 NR_CURSEG_TYPE) { 992 MSG(0, "\tWrong cp_pack_start_sum(%u) or cp_payload(%u)\n", 993 cp_pack_start_sum, cp_payload); 994 if ((get_sb(feature) & F2FS_FEATURE_SB_CHKSUM)) 995 return 1; 996 set_sb(cp_payload, cp_pack_start_sum - 1); 997 update_superblock(sb, SB_MASK_ALL); 998 } 999 1000 return 0; 1001 } 1002 1003 pgoff_t current_nat_addr(struct f2fs_sb_info *sbi, nid_t start, int *pack) 1004 { 1005 struct f2fs_nm_info *nm_i = NM_I(sbi); 1006 pgoff_t block_off; 1007 pgoff_t block_addr; 1008 int seg_off; 1009 1010 block_off = NAT_BLOCK_OFFSET(start); 1011 seg_off = block_off >> sbi->log_blocks_per_seg; 1012 1013 block_addr = (pgoff_t)(nm_i->nat_blkaddr + 1014 (seg_off << sbi->log_blocks_per_seg << 1) + 1015 (block_off & ((1 << sbi->log_blocks_per_seg) -1))); 1016 if (pack) 1017 *pack = 1; 1018 1019 if (f2fs_test_bit(block_off, nm_i->nat_bitmap)) { 1020 block_addr += sbi->blocks_per_seg; 1021 if (pack) 1022 *pack = 2; 1023 } 1024 1025 return block_addr; 1026 } 1027 1028 static int f2fs_init_nid_bitmap(struct f2fs_sb_info *sbi) 1029 { 1030 struct f2fs_nm_info *nm_i = NM_I(sbi); 1031 int nid_bitmap_size = (nm_i->max_nid + BITS_PER_BYTE - 1) / BITS_PER_BYTE; 1032 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); 1033 struct f2fs_summary_block *sum = curseg->sum_blk; 1034 struct f2fs_journal *journal = &sum->journal; 1035 struct f2fs_nat_block *nat_block; 1036 block_t start_blk; 1037 nid_t nid; 1038 int i; 1039 1040 if (!(c.func == SLOAD || c.func == FSCK)) 1041 return 0; 1042 1043 nm_i->nid_bitmap = (char *)calloc(nid_bitmap_size, 1); 1044 if (!nm_i->nid_bitmap) 1045 return -ENOMEM; 1046 1047 /* arbitrarily set 0 bit */ 1048 f2fs_set_bit(0, nm_i->nid_bitmap); 1049 1050 nat_block = malloc(F2FS_BLKSIZE); 1051 if (!nat_block) { 1052 free(nm_i->nid_bitmap); 1053 return -ENOMEM; 1054 } 1055 1056 for (nid = 0; nid < nm_i->max_nid; nid++) { 1057 if (!(nid % NAT_ENTRY_PER_BLOCK)) { 1058 int ret; 1059 1060 start_blk = current_nat_addr(sbi, nid, NULL); 1061 ret = dev_read_block(nat_block, start_blk); 1062 ASSERT(ret >= 0); 1063 } 1064 1065 if (nat_block->entries[nid % NAT_ENTRY_PER_BLOCK].block_addr) 1066 f2fs_set_bit(nid, nm_i->nid_bitmap); 1067 } 1068 1069 if (nats_in_cursum(journal) > NAT_JOURNAL_ENTRIES) { 1070 MSG(0, "\tError: f2fs_init_nid_bitmap truncate n_nats(%u) to " 1071 "NAT_JOURNAL_ENTRIES(%lu)\n", 1072 nats_in_cursum(journal), NAT_JOURNAL_ENTRIES); 1073 journal->n_nats = cpu_to_le16(NAT_JOURNAL_ENTRIES); 1074 } 1075 1076 for (i = 0; i < nats_in_cursum(journal); i++) { 1077 block_t addr; 1078 1079 addr = le32_to_cpu(nat_in_journal(journal, i).block_addr); 1080 if (!IS_VALID_BLK_ADDR(sbi, addr)) { 1081 MSG(0, "\tError: f2fs_init_nid_bitmap: addr(%u) is invalid!!!\n", addr); 1082 journal->n_nats = cpu_to_le16(i); 1083 continue; 1084 } 1085 1086 nid = le32_to_cpu(nid_in_journal(journal, i)); 1087 if (!IS_VALID_NID(sbi, nid)) { 1088 MSG(0, "\tError: f2fs_init_nid_bitmap: nid(%u) is invalid!!!\n", nid); 1089 journal->n_nats = cpu_to_le16(i); 1090 continue; 1091 } 1092 if (addr != NULL_ADDR) 1093 f2fs_set_bit(nid, nm_i->nid_bitmap); 1094 } 1095 free(nat_block); 1096 return 0; 1097 } 1098 1099 u32 update_nat_bits_flags(struct f2fs_super_block *sb, 1100 struct f2fs_checkpoint *cp, u32 flags) 1101 { 1102 u_int32_t nat_bits_bytes, nat_bits_blocks; 1103 1104 nat_bits_bytes = get_sb(segment_count_nat) << 5; 1105 nat_bits_blocks = F2FS_BYTES_TO_BLK((nat_bits_bytes << 1) + 8 + 1106 F2FS_BLKSIZE - 1); 1107 if (get_cp(cp_pack_total_block_count) <= 1108 (1 << get_sb(log_blocks_per_seg)) - nat_bits_blocks) 1109 flags |= CP_NAT_BITS_FLAG; 1110 else 1111 flags &= (~CP_NAT_BITS_FLAG); 1112 1113 return flags; 1114 } 1115 1116 /* should call flush_journal_entries() bfore this */ 1117 void write_nat_bits(struct f2fs_sb_info *sbi, 1118 struct f2fs_super_block *sb, struct f2fs_checkpoint *cp, int set) 1119 { 1120 struct f2fs_nm_info *nm_i = NM_I(sbi); 1121 u_int32_t nat_blocks = get_sb(segment_count_nat) << 1122 (get_sb(log_blocks_per_seg) - 1); 1123 u_int32_t nat_bits_bytes = nat_blocks >> 3; 1124 u_int32_t nat_bits_blocks = F2FS_BYTES_TO_BLK((nat_bits_bytes << 1) + 1125 8 + F2FS_BLKSIZE - 1); 1126 unsigned char *nat_bits, *full_nat_bits, *empty_nat_bits; 1127 struct f2fs_nat_block *nat_block; 1128 u_int32_t i, j; 1129 block_t blkaddr; 1130 int ret; 1131 1132 nat_bits = calloc(F2FS_BLKSIZE, nat_bits_blocks); 1133 ASSERT(nat_bits); 1134 1135 nat_block = malloc(F2FS_BLKSIZE); 1136 ASSERT(nat_block); 1137 1138 full_nat_bits = nat_bits + 8; 1139 empty_nat_bits = full_nat_bits + nat_bits_bytes; 1140 1141 memset(full_nat_bits, 0, nat_bits_bytes); 1142 memset(empty_nat_bits, 0, nat_bits_bytes); 1143 1144 for (i = 0; i < nat_blocks; i++) { 1145 int seg_off = i >> get_sb(log_blocks_per_seg); 1146 int valid = 0; 1147 1148 blkaddr = (pgoff_t)(get_sb(nat_blkaddr) + 1149 (seg_off << get_sb(log_blocks_per_seg) << 1) + 1150 (i & ((1 << get_sb(log_blocks_per_seg)) - 1))); 1151 1152 /* 1153 * Should consider new nat_blocks is larger than old 1154 * nm_i->nat_blocks, since nm_i->nat_bitmap is based on 1155 * old one. 1156 */ 1157 if (i < nm_i->nat_blocks && f2fs_test_bit(i, nm_i->nat_bitmap)) 1158 blkaddr += (1 << get_sb(log_blocks_per_seg)); 1159 1160 ret = dev_read_block(nat_block, blkaddr); 1161 ASSERT(ret >= 0); 1162 1163 for (j = 0; j < NAT_ENTRY_PER_BLOCK; j++) { 1164 if ((i == 0 && j == 0) || 1165 nat_block->entries[j].block_addr != NULL_ADDR) 1166 valid++; 1167 } 1168 if (valid == 0) 1169 test_and_set_bit_le(i, empty_nat_bits); 1170 else if (valid == NAT_ENTRY_PER_BLOCK) 1171 test_and_set_bit_le(i, full_nat_bits); 1172 } 1173 *(__le64 *)nat_bits = get_cp_crc(cp); 1174 free(nat_block); 1175 1176 blkaddr = get_sb(segment0_blkaddr) + (set << 1177 get_sb(log_blocks_per_seg)) - nat_bits_blocks; 1178 1179 DBG(1, "\tWriting NAT bits pages, at offset 0x%08x\n", blkaddr); 1180 1181 for (i = 0; i < nat_bits_blocks; i++) { 1182 if (dev_write_block(nat_bits + i * F2FS_BLKSIZE, blkaddr + i)) 1183 ASSERT_MSG("\tError: write NAT bits to disk!!!\n"); 1184 } 1185 MSG(0, "Info: Write valid nat_bits in checkpoint\n"); 1186 1187 free(nat_bits); 1188 } 1189 1190 static int check_nat_bits(struct f2fs_sb_info *sbi, 1191 struct f2fs_super_block *sb, struct f2fs_checkpoint *cp) 1192 { 1193 struct f2fs_nm_info *nm_i = NM_I(sbi); 1194 u_int32_t nat_blocks = get_sb(segment_count_nat) << 1195 (get_sb(log_blocks_per_seg) - 1); 1196 u_int32_t nat_bits_bytes = nat_blocks >> 3; 1197 u_int32_t nat_bits_blocks = F2FS_BYTES_TO_BLK((nat_bits_bytes << 1) + 1198 8 + F2FS_BLKSIZE - 1); 1199 unsigned char *nat_bits, *full_nat_bits, *empty_nat_bits; 1200 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); 1201 struct f2fs_journal *journal = &curseg->sum_blk->journal; 1202 u_int32_t i, j; 1203 block_t blkaddr; 1204 int err = 0; 1205 1206 nat_bits = calloc(F2FS_BLKSIZE, nat_bits_blocks); 1207 ASSERT(nat_bits); 1208 1209 full_nat_bits = nat_bits + 8; 1210 empty_nat_bits = full_nat_bits + nat_bits_bytes; 1211 1212 blkaddr = get_sb(segment0_blkaddr) + (sbi->cur_cp << 1213 get_sb(log_blocks_per_seg)) - nat_bits_blocks; 1214 1215 for (i = 0; i < nat_bits_blocks; i++) { 1216 if (dev_read_block(nat_bits + i * F2FS_BLKSIZE, blkaddr + i)) 1217 ASSERT_MSG("\tError: read NAT bits to disk!!!\n"); 1218 } 1219 1220 if (*(__le64 *)nat_bits != get_cp_crc(cp) || nats_in_cursum(journal)) { 1221 /* 1222 * if there is a journal, f2fs was not shutdown cleanly. Let's 1223 * flush them with nat_bits. 1224 */ 1225 if (c.fix_on) 1226 err = -1; 1227 /* Otherwise, kernel will disable nat_bits */ 1228 goto out; 1229 } 1230 1231 for (i = 0; i < nat_blocks; i++) { 1232 u_int32_t start_nid = i * NAT_ENTRY_PER_BLOCK; 1233 u_int32_t valid = 0; 1234 int empty = test_bit_le(i, empty_nat_bits); 1235 int full = test_bit_le(i, full_nat_bits); 1236 1237 for (j = 0; j < NAT_ENTRY_PER_BLOCK; j++) { 1238 if (f2fs_test_bit(start_nid + j, nm_i->nid_bitmap)) 1239 valid++; 1240 } 1241 if (valid == 0) { 1242 if (!empty || full) { 1243 err = -1; 1244 goto out; 1245 } 1246 } else if (valid == NAT_ENTRY_PER_BLOCK) { 1247 if (empty || !full) { 1248 err = -1; 1249 goto out; 1250 } 1251 } else { 1252 if (empty || full) { 1253 err = -1; 1254 goto out; 1255 } 1256 } 1257 } 1258 out: 1259 free(nat_bits); 1260 if (!err) { 1261 MSG(0, "Info: Checked valid nat_bits in checkpoint\n"); 1262 } else { 1263 c.bug_nat_bits = 1; 1264 MSG(0, "Info: Corrupted valid nat_bits in checkpoint\n"); 1265 } 1266 return err; 1267 } 1268 1269 int init_node_manager(struct f2fs_sb_info *sbi) 1270 { 1271 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi); 1272 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi); 1273 struct f2fs_nm_info *nm_i = NM_I(sbi); 1274 unsigned char *version_bitmap; 1275 unsigned int nat_segs; 1276 1277 nm_i->nat_blkaddr = get_sb(nat_blkaddr); 1278 1279 /* segment_count_nat includes pair segment so divide to 2. */ 1280 nat_segs = get_sb(segment_count_nat) >> 1; 1281 nm_i->nat_blocks = nat_segs << get_sb(log_blocks_per_seg); 1282 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nm_i->nat_blocks; 1283 nm_i->fcnt = 0; 1284 nm_i->nat_cnt = 0; 1285 nm_i->init_scan_nid = get_cp(next_free_nid); 1286 nm_i->next_scan_nid = get_cp(next_free_nid); 1287 1288 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP); 1289 1290 nm_i->nat_bitmap = malloc(nm_i->bitmap_size); 1291 if (!nm_i->nat_bitmap) 1292 return -ENOMEM; 1293 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP); 1294 if (!version_bitmap) 1295 return -EFAULT; 1296 1297 /* copy version bitmap */ 1298 memcpy(nm_i->nat_bitmap, version_bitmap, nm_i->bitmap_size); 1299 return f2fs_init_nid_bitmap(sbi); 1300 } 1301 1302 int build_node_manager(struct f2fs_sb_info *sbi) 1303 { 1304 int err; 1305 sbi->nm_info = malloc(sizeof(struct f2fs_nm_info)); 1306 if (!sbi->nm_info) 1307 return -ENOMEM; 1308 1309 err = init_node_manager(sbi); 1310 if (err) 1311 return err; 1312 1313 return 0; 1314 } 1315 1316 int build_sit_info(struct f2fs_sb_info *sbi) 1317 { 1318 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi); 1319 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi); 1320 struct sit_info *sit_i; 1321 unsigned int sit_segs, start; 1322 char *src_bitmap, *dst_bitmap; 1323 unsigned int bitmap_size; 1324 1325 sit_i = malloc(sizeof(struct sit_info)); 1326 if (!sit_i) { 1327 MSG(1, "\tError: Malloc failed for build_sit_info!\n"); 1328 return -ENOMEM; 1329 } 1330 1331 SM_I(sbi)->sit_info = sit_i; 1332 1333 sit_i->sentries = calloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry), 1); 1334 if (!sit_i->sentries) { 1335 MSG(1, "\tError: Calloc failed for build_sit_info!\n"); 1336 goto free_sit_info; 1337 } 1338 1339 for (start = 0; start < TOTAL_SEGS(sbi); start++) { 1340 sit_i->sentries[start].cur_valid_map 1341 = calloc(SIT_VBLOCK_MAP_SIZE, 1); 1342 if (!sit_i->sentries[start].cur_valid_map) { 1343 MSG(1, "\tError: Calloc failed for build_sit_info!!\n"); 1344 goto free_validity_maps; 1345 } 1346 } 1347 1348 sit_segs = get_sb(segment_count_sit) >> 1; 1349 bitmap_size = __bitmap_size(sbi, SIT_BITMAP); 1350 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP); 1351 1352 dst_bitmap = malloc(bitmap_size); 1353 if (!dst_bitmap) { 1354 MSG(1, "\tError: Malloc failed for build_sit_info!!\n"); 1355 goto free_validity_maps; 1356 } 1357 1358 memcpy(dst_bitmap, src_bitmap, bitmap_size); 1359 1360 sit_i->sit_base_addr = get_sb(sit_blkaddr); 1361 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg; 1362 sit_i->written_valid_blocks = get_cp(valid_block_count); 1363 sit_i->sit_bitmap = dst_bitmap; 1364 sit_i->bitmap_size = bitmap_size; 1365 sit_i->dirty_sentries = 0; 1366 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK; 1367 sit_i->elapsed_time = get_cp(elapsed_time); 1368 return 0; 1369 1370 free_validity_maps: 1371 for (--start ; start >= 0; --start) 1372 free(sit_i->sentries[start].cur_valid_map); 1373 free(sit_i->sentries); 1374 1375 free_sit_info: 1376 free(sit_i); 1377 1378 return -ENOMEM; 1379 } 1380 1381 void reset_curseg(struct f2fs_sb_info *sbi, int type) 1382 { 1383 struct curseg_info *curseg = CURSEG_I(sbi, type); 1384 struct summary_footer *sum_footer; 1385 struct seg_entry *se; 1386 1387 sum_footer = &(curseg->sum_blk->footer); 1388 memset(sum_footer, 0, sizeof(struct summary_footer)); 1389 if (IS_DATASEG(type)) 1390 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA); 1391 if (IS_NODESEG(type)) 1392 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE); 1393 se = get_seg_entry(sbi, curseg->segno); 1394 se->type = type; 1395 se->dirty = 1; 1396 } 1397 1398 static void read_compacted_summaries(struct f2fs_sb_info *sbi) 1399 { 1400 struct curseg_info *curseg; 1401 unsigned int i, j, offset; 1402 block_t start; 1403 char *kaddr; 1404 int ret; 1405 1406 start = start_sum_block(sbi); 1407 1408 kaddr = (char *)malloc(PAGE_SIZE); 1409 ASSERT(kaddr); 1410 1411 ret = dev_read_block(kaddr, start++); 1412 ASSERT(ret >= 0); 1413 1414 curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); 1415 memcpy(&curseg->sum_blk->journal.n_nats, kaddr, SUM_JOURNAL_SIZE); 1416 1417 curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); 1418 memcpy(&curseg->sum_blk->journal.n_sits, kaddr + SUM_JOURNAL_SIZE, 1419 SUM_JOURNAL_SIZE); 1420 1421 offset = 2 * SUM_JOURNAL_SIZE; 1422 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) { 1423 unsigned short blk_off; 1424 struct curseg_info *curseg = CURSEG_I(sbi, i); 1425 1426 reset_curseg(sbi, i); 1427 1428 if (curseg->alloc_type == SSR) 1429 blk_off = sbi->blocks_per_seg; 1430 else 1431 blk_off = curseg->next_blkoff; 1432 1433 ASSERT(blk_off <= ENTRIES_IN_SUM); 1434 1435 for (j = 0; j < blk_off; j++) { 1436 struct f2fs_summary *s; 1437 s = (struct f2fs_summary *)(kaddr + offset); 1438 curseg->sum_blk->entries[j] = *s; 1439 offset += SUMMARY_SIZE; 1440 if (offset + SUMMARY_SIZE <= 1441 PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) 1442 continue; 1443 memset(kaddr, 0, PAGE_SIZE); 1444 ret = dev_read_block(kaddr, start++); 1445 ASSERT(ret >= 0); 1446 offset = 0; 1447 } 1448 } 1449 free(kaddr); 1450 } 1451 1452 static void restore_node_summary(struct f2fs_sb_info *sbi, 1453 unsigned int segno, struct f2fs_summary_block *sum_blk) 1454 { 1455 struct f2fs_node *node_blk; 1456 struct f2fs_summary *sum_entry; 1457 block_t addr; 1458 unsigned int i; 1459 int ret; 1460 1461 node_blk = malloc(F2FS_BLKSIZE); 1462 ASSERT(node_blk); 1463 1464 /* scan the node segment */ 1465 addr = START_BLOCK(sbi, segno); 1466 sum_entry = &sum_blk->entries[0]; 1467 1468 for (i = 0; i < sbi->blocks_per_seg; i++, sum_entry++) { 1469 ret = dev_read_block(node_blk, addr); 1470 ASSERT(ret >= 0); 1471 sum_entry->nid = node_blk->footer.nid; 1472 addr++; 1473 } 1474 free(node_blk); 1475 } 1476 1477 static void read_normal_summaries(struct f2fs_sb_info *sbi, int type) 1478 { 1479 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi); 1480 struct f2fs_summary_block *sum_blk; 1481 struct curseg_info *curseg; 1482 unsigned int segno = 0; 1483 block_t blk_addr = 0; 1484 int ret; 1485 1486 if (IS_DATASEG(type)) { 1487 segno = get_cp(cur_data_segno[type]); 1488 if (is_set_ckpt_flags(cp, CP_UMOUNT_FLAG)) 1489 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type); 1490 else 1491 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type); 1492 } else { 1493 segno = get_cp(cur_node_segno[type - CURSEG_HOT_NODE]); 1494 if (is_set_ckpt_flags(cp, CP_UMOUNT_FLAG)) 1495 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE, 1496 type - CURSEG_HOT_NODE); 1497 else 1498 blk_addr = GET_SUM_BLKADDR(sbi, segno); 1499 } 1500 1501 sum_blk = (struct f2fs_summary_block *)malloc(PAGE_SIZE); 1502 ASSERT(sum_blk); 1503 1504 ret = dev_read_block(sum_blk, blk_addr); 1505 ASSERT(ret >= 0); 1506 1507 if (IS_NODESEG(type) && !is_set_ckpt_flags(cp, CP_UMOUNT_FLAG)) 1508 restore_node_summary(sbi, segno, sum_blk); 1509 1510 curseg = CURSEG_I(sbi, type); 1511 memcpy(curseg->sum_blk, sum_blk, PAGE_CACHE_SIZE); 1512 reset_curseg(sbi, type); 1513 free(sum_blk); 1514 } 1515 1516 void update_sum_entry(struct f2fs_sb_info *sbi, block_t blk_addr, 1517 struct f2fs_summary *sum) 1518 { 1519 struct f2fs_summary_block *sum_blk; 1520 u32 segno, offset; 1521 int type, ret; 1522 struct seg_entry *se; 1523 1524 segno = GET_SEGNO(sbi, blk_addr); 1525 offset = OFFSET_IN_SEG(sbi, blk_addr); 1526 1527 se = get_seg_entry(sbi, segno); 1528 1529 sum_blk = get_sum_block(sbi, segno, &type); 1530 memcpy(&sum_blk->entries[offset], sum, sizeof(*sum)); 1531 sum_blk->footer.entry_type = IS_NODESEG(se->type) ? SUM_TYPE_NODE : 1532 SUM_TYPE_DATA; 1533 1534 /* write SSA all the time */ 1535 ret = dev_write_block(sum_blk, GET_SUM_BLKADDR(sbi, segno)); 1536 ASSERT(ret >= 0); 1537 1538 if (type == SEG_TYPE_NODE || type == SEG_TYPE_DATA || 1539 type == SEG_TYPE_MAX) 1540 free(sum_blk); 1541 } 1542 1543 static void restore_curseg_summaries(struct f2fs_sb_info *sbi) 1544 { 1545 int type = CURSEG_HOT_DATA; 1546 1547 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) { 1548 read_compacted_summaries(sbi); 1549 type = CURSEG_HOT_NODE; 1550 } 1551 1552 for (; type <= CURSEG_COLD_NODE; type++) 1553 read_normal_summaries(sbi, type); 1554 } 1555 1556 static int build_curseg(struct f2fs_sb_info *sbi) 1557 { 1558 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi); 1559 struct curseg_info *array; 1560 unsigned short blk_off; 1561 unsigned int segno; 1562 int i; 1563 1564 array = malloc(sizeof(*array) * NR_CURSEG_TYPE); 1565 if (!array) { 1566 MSG(1, "\tError: Malloc failed for build_curseg!\n"); 1567 return -ENOMEM; 1568 } 1569 1570 SM_I(sbi)->curseg_array = array; 1571 1572 for (i = 0; i < NR_CURSEG_TYPE; i++) { 1573 array[i].sum_blk = malloc(PAGE_CACHE_SIZE); 1574 if (!array[i].sum_blk) { 1575 MSG(1, "\tError: Malloc failed for build_curseg!!\n"); 1576 goto seg_cleanup; 1577 } 1578 1579 if (i <= CURSEG_COLD_DATA) { 1580 blk_off = get_cp(cur_data_blkoff[i]); 1581 segno = get_cp(cur_data_segno[i]); 1582 } 1583 if (i > CURSEG_COLD_DATA) { 1584 blk_off = get_cp(cur_node_blkoff[i - CURSEG_HOT_NODE]); 1585 segno = get_cp(cur_node_segno[i - CURSEG_HOT_NODE]); 1586 } 1587 ASSERT(segno < TOTAL_SEGS(sbi)); 1588 ASSERT(blk_off < DEFAULT_BLOCKS_PER_SEGMENT); 1589 1590 array[i].segno = segno; 1591 array[i].zone = GET_ZONENO_FROM_SEGNO(sbi, segno); 1592 array[i].next_segno = NULL_SEGNO; 1593 array[i].next_blkoff = blk_off; 1594 array[i].alloc_type = cp->alloc_type[i]; 1595 } 1596 restore_curseg_summaries(sbi); 1597 return 0; 1598 1599 seg_cleanup: 1600 for(--i ; i >=0; --i) 1601 free(array[i].sum_blk); 1602 free(array); 1603 1604 return -ENOMEM; 1605 } 1606 1607 static inline void check_seg_range(struct f2fs_sb_info *sbi, unsigned int segno) 1608 { 1609 unsigned int end_segno = SM_I(sbi)->segment_count - 1; 1610 ASSERT(segno <= end_segno); 1611 } 1612 1613 void get_current_sit_page(struct f2fs_sb_info *sbi, 1614 unsigned int segno, struct f2fs_sit_block *sit_blk) 1615 { 1616 struct sit_info *sit_i = SIT_I(sbi); 1617 unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno); 1618 block_t blk_addr = sit_i->sit_base_addr + offset; 1619 int ret; 1620 1621 check_seg_range(sbi, segno); 1622 1623 /* calculate sit block address */ 1624 if (f2fs_test_bit(offset, sit_i->sit_bitmap)) 1625 blk_addr += sit_i->sit_blocks; 1626 1627 ret = dev_read_block(sit_blk, blk_addr); 1628 ASSERT(ret >= 0); 1629 } 1630 1631 void rewrite_current_sit_page(struct f2fs_sb_info *sbi, 1632 unsigned int segno, struct f2fs_sit_block *sit_blk) 1633 { 1634 struct sit_info *sit_i = SIT_I(sbi); 1635 unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno); 1636 block_t blk_addr = sit_i->sit_base_addr + offset; 1637 int ret; 1638 1639 /* calculate sit block address */ 1640 if (f2fs_test_bit(offset, sit_i->sit_bitmap)) 1641 blk_addr += sit_i->sit_blocks; 1642 1643 ret = dev_write_block(sit_blk, blk_addr); 1644 ASSERT(ret >= 0); 1645 } 1646 1647 void check_block_count(struct f2fs_sb_info *sbi, 1648 unsigned int segno, struct f2fs_sit_entry *raw_sit) 1649 { 1650 struct f2fs_sm_info *sm_info = SM_I(sbi); 1651 unsigned int end_segno = sm_info->segment_count - 1; 1652 int valid_blocks = 0; 1653 unsigned int i; 1654 1655 /* check segment usage */ 1656 if (GET_SIT_VBLOCKS(raw_sit) > sbi->blocks_per_seg) 1657 ASSERT_MSG("Invalid SIT vblocks: segno=0x%x, %u", 1658 segno, GET_SIT_VBLOCKS(raw_sit)); 1659 1660 /* check boundary of a given segment number */ 1661 if (segno > end_segno) 1662 ASSERT_MSG("Invalid SEGNO: 0x%x", segno); 1663 1664 /* check bitmap with valid block count */ 1665 for (i = 0; i < SIT_VBLOCK_MAP_SIZE; i++) 1666 valid_blocks += get_bits_in_byte(raw_sit->valid_map[i]); 1667 1668 if (GET_SIT_VBLOCKS(raw_sit) != valid_blocks) 1669 ASSERT_MSG("Wrong SIT valid blocks: segno=0x%x, %u vs. %u", 1670 segno, GET_SIT_VBLOCKS(raw_sit), valid_blocks); 1671 1672 if (GET_SIT_TYPE(raw_sit) >= NO_CHECK_TYPE) 1673 ASSERT_MSG("Wrong SIT type: segno=0x%x, %u", 1674 segno, GET_SIT_TYPE(raw_sit)); 1675 } 1676 1677 void seg_info_from_raw_sit(struct seg_entry *se, 1678 struct f2fs_sit_entry *raw_sit) 1679 { 1680 se->valid_blocks = GET_SIT_VBLOCKS(raw_sit); 1681 memcpy(se->cur_valid_map, raw_sit->valid_map, SIT_VBLOCK_MAP_SIZE); 1682 se->type = GET_SIT_TYPE(raw_sit); 1683 se->orig_type = GET_SIT_TYPE(raw_sit); 1684 se->mtime = le64_to_cpu(raw_sit->mtime); 1685 } 1686 1687 struct seg_entry *get_seg_entry(struct f2fs_sb_info *sbi, 1688 unsigned int segno) 1689 { 1690 struct sit_info *sit_i = SIT_I(sbi); 1691 return &sit_i->sentries[segno]; 1692 } 1693 1694 struct f2fs_summary_block *get_sum_block(struct f2fs_sb_info *sbi, 1695 unsigned int segno, int *ret_type) 1696 { 1697 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi); 1698 struct f2fs_summary_block *sum_blk; 1699 struct curseg_info *curseg; 1700 int type, ret; 1701 u64 ssa_blk; 1702 1703 *ret_type= SEG_TYPE_MAX; 1704 1705 ssa_blk = GET_SUM_BLKADDR(sbi, segno); 1706 for (type = 0; type < NR_CURSEG_NODE_TYPE; type++) { 1707 if (segno == get_cp(cur_node_segno[type])) { 1708 curseg = CURSEG_I(sbi, CURSEG_HOT_NODE + type); 1709 if (!IS_SUM_NODE_SEG(curseg->sum_blk->footer)) { 1710 ASSERT_MSG("segno [0x%x] indicates a data " 1711 "segment, but should be node", 1712 segno); 1713 *ret_type = -SEG_TYPE_CUR_NODE; 1714 } else { 1715 *ret_type = SEG_TYPE_CUR_NODE; 1716 } 1717 return curseg->sum_blk; 1718 } 1719 } 1720 1721 for (type = 0; type < NR_CURSEG_DATA_TYPE; type++) { 1722 if (segno == get_cp(cur_data_segno[type])) { 1723 curseg = CURSEG_I(sbi, type); 1724 if (IS_SUM_NODE_SEG(curseg->sum_blk->footer)) { 1725 ASSERT_MSG("segno [0x%x] indicates a node " 1726 "segment, but should be data", 1727 segno); 1728 *ret_type = -SEG_TYPE_CUR_DATA; 1729 } else { 1730 *ret_type = SEG_TYPE_CUR_DATA; 1731 } 1732 return curseg->sum_blk; 1733 } 1734 } 1735 1736 sum_blk = calloc(BLOCK_SZ, 1); 1737 ASSERT(sum_blk); 1738 1739 ret = dev_read_block(sum_blk, ssa_blk); 1740 ASSERT(ret >= 0); 1741 1742 if (IS_SUM_NODE_SEG(sum_blk->footer)) 1743 *ret_type = SEG_TYPE_NODE; 1744 else if (IS_SUM_DATA_SEG(sum_blk->footer)) 1745 *ret_type = SEG_TYPE_DATA; 1746 1747 return sum_blk; 1748 } 1749 1750 int get_sum_entry(struct f2fs_sb_info *sbi, u32 blk_addr, 1751 struct f2fs_summary *sum_entry) 1752 { 1753 struct f2fs_summary_block *sum_blk; 1754 u32 segno, offset; 1755 int type; 1756 1757 segno = GET_SEGNO(sbi, blk_addr); 1758 offset = OFFSET_IN_SEG(sbi, blk_addr); 1759 1760 sum_blk = get_sum_block(sbi, segno, &type); 1761 memcpy(sum_entry, &(sum_blk->entries[offset]), 1762 sizeof(struct f2fs_summary)); 1763 if (type == SEG_TYPE_NODE || type == SEG_TYPE_DATA || 1764 type == SEG_TYPE_MAX) 1765 free(sum_blk); 1766 return type; 1767 } 1768 1769 static void get_nat_entry(struct f2fs_sb_info *sbi, nid_t nid, 1770 struct f2fs_nat_entry *raw_nat) 1771 { 1772 struct f2fs_nat_block *nat_block; 1773 pgoff_t block_addr; 1774 int entry_off; 1775 int ret; 1776 1777 if (lookup_nat_in_journal(sbi, nid, raw_nat) >= 0) 1778 return; 1779 1780 nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1); 1781 ASSERT(nat_block); 1782 1783 entry_off = nid % NAT_ENTRY_PER_BLOCK; 1784 block_addr = current_nat_addr(sbi, nid, NULL); 1785 1786 ret = dev_read_block(nat_block, block_addr); 1787 ASSERT(ret >= 0); 1788 1789 memcpy(raw_nat, &nat_block->entries[entry_off], 1790 sizeof(struct f2fs_nat_entry)); 1791 free(nat_block); 1792 } 1793 1794 void update_data_blkaddr(struct f2fs_sb_info *sbi, nid_t nid, 1795 u16 ofs_in_node, block_t newaddr) 1796 { 1797 struct f2fs_node *node_blk = NULL; 1798 struct node_info ni; 1799 block_t oldaddr, startaddr, endaddr; 1800 int ret; 1801 1802 node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1); 1803 ASSERT(node_blk); 1804 1805 get_node_info(sbi, nid, &ni); 1806 1807 /* read node_block */ 1808 ret = dev_read_block(node_blk, ni.blk_addr); 1809 ASSERT(ret >= 0); 1810 1811 /* check its block address */ 1812 if (node_blk->footer.nid == node_blk->footer.ino) { 1813 int ofs = get_extra_isize(node_blk); 1814 1815 oldaddr = le32_to_cpu(node_blk->i.i_addr[ofs + ofs_in_node]); 1816 node_blk->i.i_addr[ofs + ofs_in_node] = cpu_to_le32(newaddr); 1817 } else { 1818 oldaddr = le32_to_cpu(node_blk->dn.addr[ofs_in_node]); 1819 node_blk->dn.addr[ofs_in_node] = cpu_to_le32(newaddr); 1820 } 1821 1822 ret = dev_write_block(node_blk, ni.blk_addr); 1823 ASSERT(ret >= 0); 1824 1825 /* check extent cache entry */ 1826 if (node_blk->footer.nid != node_blk->footer.ino) { 1827 get_node_info(sbi, le32_to_cpu(node_blk->footer.ino), &ni); 1828 1829 /* read inode block */ 1830 ret = dev_read_block(node_blk, ni.blk_addr); 1831 ASSERT(ret >= 0); 1832 } 1833 1834 startaddr = le32_to_cpu(node_blk->i.i_ext.blk_addr); 1835 endaddr = startaddr + le32_to_cpu(node_blk->i.i_ext.len); 1836 if (oldaddr >= startaddr && oldaddr < endaddr) { 1837 node_blk->i.i_ext.len = 0; 1838 1839 /* update inode block */ 1840 ret = dev_write_block(node_blk, ni.blk_addr); 1841 ASSERT(ret >= 0); 1842 } 1843 free(node_blk); 1844 } 1845 1846 void update_nat_blkaddr(struct f2fs_sb_info *sbi, nid_t ino, 1847 nid_t nid, block_t newaddr) 1848 { 1849 struct f2fs_nat_block *nat_block; 1850 pgoff_t block_addr; 1851 int entry_off; 1852 int ret; 1853 1854 nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1); 1855 ASSERT(nat_block); 1856 1857 entry_off = nid % NAT_ENTRY_PER_BLOCK; 1858 block_addr = current_nat_addr(sbi, nid, NULL); 1859 1860 ret = dev_read_block(nat_block, block_addr); 1861 ASSERT(ret >= 0); 1862 1863 if (ino) 1864 nat_block->entries[entry_off].ino = cpu_to_le32(ino); 1865 nat_block->entries[entry_off].block_addr = cpu_to_le32(newaddr); 1866 if (c.func == FSCK) 1867 F2FS_FSCK(sbi)->entries[nid] = nat_block->entries[entry_off]; 1868 1869 ret = dev_write_block(nat_block, block_addr); 1870 ASSERT(ret >= 0); 1871 free(nat_block); 1872 } 1873 1874 void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni) 1875 { 1876 struct f2fs_nat_entry raw_nat; 1877 1878 ni->nid = nid; 1879 if (c.func == FSCK) { 1880 node_info_from_raw_nat(ni, &(F2FS_FSCK(sbi)->entries[nid])); 1881 if (ni->blk_addr) 1882 return; 1883 /* nat entry is not cached, read it */ 1884 } 1885 1886 get_nat_entry(sbi, nid, &raw_nat); 1887 node_info_from_raw_nat(ni, &raw_nat); 1888 } 1889 1890 static int build_sit_entries(struct f2fs_sb_info *sbi) 1891 { 1892 struct sit_info *sit_i = SIT_I(sbi); 1893 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); 1894 struct f2fs_journal *journal = &curseg->sum_blk->journal; 1895 struct f2fs_sit_block *sit_blk; 1896 struct seg_entry *se; 1897 struct f2fs_sit_entry sit; 1898 unsigned int i, segno; 1899 1900 sit_blk = calloc(BLOCK_SZ, 1); 1901 if (!sit_blk) { 1902 MSG(1, "\tError: Calloc failed for build_sit_entries!\n"); 1903 return -ENOMEM; 1904 } 1905 1906 for (segno = 0; segno < TOTAL_SEGS(sbi); segno++) { 1907 se = &sit_i->sentries[segno]; 1908 1909 get_current_sit_page(sbi, segno, sit_blk); 1910 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, segno)]; 1911 1912 check_block_count(sbi, segno, &sit); 1913 seg_info_from_raw_sit(se, &sit); 1914 } 1915 1916 free(sit_blk); 1917 for (i = 0; i < sits_in_cursum(journal); i++) { 1918 segno = le32_to_cpu(segno_in_journal(journal, i)); 1919 se = &sit_i->sentries[segno]; 1920 sit = sit_in_journal(journal, i); 1921 1922 check_block_count(sbi, segno, &sit); 1923 seg_info_from_raw_sit(se, &sit); 1924 } 1925 return 0; 1926 } 1927 1928 static int build_segment_manager(struct f2fs_sb_info *sbi) 1929 { 1930 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi); 1931 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi); 1932 struct f2fs_sm_info *sm_info; 1933 1934 sm_info = malloc(sizeof(struct f2fs_sm_info)); 1935 if (!sm_info) { 1936 MSG(1, "\tError: Malloc failed for build_segment_manager!\n"); 1937 return -ENOMEM; 1938 } 1939 1940 /* init sm info */ 1941 sbi->sm_info = sm_info; 1942 sm_info->seg0_blkaddr = get_sb(segment0_blkaddr); 1943 sm_info->main_blkaddr = get_sb(main_blkaddr); 1944 sm_info->segment_count = get_sb(segment_count); 1945 sm_info->reserved_segments = get_cp(rsvd_segment_count); 1946 sm_info->ovp_segments = get_cp(overprov_segment_count); 1947 sm_info->main_segments = get_sb(segment_count_main); 1948 sm_info->ssa_blkaddr = get_sb(ssa_blkaddr); 1949 1950 if (build_sit_info(sbi) || build_curseg(sbi) || build_sit_entries(sbi)) { 1951 free(sm_info); 1952 return -ENOMEM; 1953 } 1954 1955 return 0; 1956 } 1957 1958 void build_sit_area_bitmap(struct f2fs_sb_info *sbi) 1959 { 1960 struct f2fs_fsck *fsck = F2FS_FSCK(sbi); 1961 struct f2fs_sm_info *sm_i = SM_I(sbi); 1962 unsigned int segno = 0; 1963 char *ptr = NULL; 1964 u32 sum_vblocks = 0; 1965 u32 free_segs = 0; 1966 struct seg_entry *se; 1967 1968 fsck->sit_area_bitmap_sz = sm_i->main_segments * SIT_VBLOCK_MAP_SIZE; 1969 fsck->sit_area_bitmap = calloc(1, fsck->sit_area_bitmap_sz); 1970 ASSERT(fsck->sit_area_bitmap); 1971 ptr = fsck->sit_area_bitmap; 1972 1973 ASSERT(fsck->sit_area_bitmap_sz == fsck->main_area_bitmap_sz); 1974 1975 for (segno = 0; segno < TOTAL_SEGS(sbi); segno++) { 1976 se = get_seg_entry(sbi, segno); 1977 1978 memcpy(ptr, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE); 1979 ptr += SIT_VBLOCK_MAP_SIZE; 1980 1981 if (se->valid_blocks == 0x0) { 1982 if (le32_to_cpu(sbi->ckpt->cur_node_segno[0]) == segno || 1983 le32_to_cpu(sbi->ckpt->cur_data_segno[0]) == segno || 1984 le32_to_cpu(sbi->ckpt->cur_node_segno[1]) == segno || 1985 le32_to_cpu(sbi->ckpt->cur_data_segno[1]) == segno || 1986 le32_to_cpu(sbi->ckpt->cur_node_segno[2]) == segno || 1987 le32_to_cpu(sbi->ckpt->cur_data_segno[2]) == segno) { 1988 continue; 1989 } else { 1990 free_segs++; 1991 } 1992 } else { 1993 sum_vblocks += se->valid_blocks; 1994 } 1995 } 1996 fsck->chk.sit_valid_blocks = sum_vblocks; 1997 fsck->chk.sit_free_segs = free_segs; 1998 1999 DBG(1, "Blocks [0x%x : %d] Free Segs [0x%x : %d]\n\n", 2000 sum_vblocks, sum_vblocks, 2001 free_segs, free_segs); 2002 } 2003 2004 void rewrite_sit_area_bitmap(struct f2fs_sb_info *sbi) 2005 { 2006 struct f2fs_fsck *fsck = F2FS_FSCK(sbi); 2007 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); 2008 struct sit_info *sit_i = SIT_I(sbi); 2009 struct f2fs_sit_block *sit_blk; 2010 unsigned int segno = 0; 2011 struct f2fs_summary_block *sum = curseg->sum_blk; 2012 char *ptr = NULL; 2013 2014 sit_blk = calloc(BLOCK_SZ, 1); 2015 ASSERT(sit_blk); 2016 /* remove sit journal */ 2017 sum->journal.n_sits = 0; 2018 2019 ptr = fsck->main_area_bitmap; 2020 2021 for (segno = 0; segno < TOTAL_SEGS(sbi); segno++) { 2022 struct f2fs_sit_entry *sit; 2023 struct seg_entry *se; 2024 u16 valid_blocks = 0; 2025 u16 type; 2026 int i; 2027 2028 get_current_sit_page(sbi, segno, sit_blk); 2029 sit = &sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, segno)]; 2030 memcpy(sit->valid_map, ptr, SIT_VBLOCK_MAP_SIZE); 2031 2032 /* update valid block count */ 2033 for (i = 0; i < SIT_VBLOCK_MAP_SIZE; i++) 2034 valid_blocks += get_bits_in_byte(sit->valid_map[i]); 2035 2036 se = get_seg_entry(sbi, segno); 2037 memcpy(se->cur_valid_map, ptr, SIT_VBLOCK_MAP_SIZE); 2038 se->valid_blocks = valid_blocks; 2039 type = se->type; 2040 if (type >= NO_CHECK_TYPE) { 2041 ASSERT_MSG("Invalide type and valid blocks=%x,%x", 2042 segno, valid_blocks); 2043 type = 0; 2044 } 2045 sit->vblocks = cpu_to_le16((type << SIT_VBLOCKS_SHIFT) | 2046 valid_blocks); 2047 rewrite_current_sit_page(sbi, segno, sit_blk); 2048 2049 ptr += SIT_VBLOCK_MAP_SIZE; 2050 } 2051 2052 free(sit_blk); 2053 } 2054 2055 static int flush_sit_journal_entries(struct f2fs_sb_info *sbi) 2056 { 2057 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); 2058 struct f2fs_journal *journal = &curseg->sum_blk->journal; 2059 struct sit_info *sit_i = SIT_I(sbi); 2060 struct f2fs_sit_block *sit_blk; 2061 unsigned int segno; 2062 int i; 2063 2064 sit_blk = calloc(BLOCK_SZ, 1); 2065 ASSERT(sit_blk); 2066 for (i = 0; i < sits_in_cursum(journal); i++) { 2067 struct f2fs_sit_entry *sit; 2068 struct seg_entry *se; 2069 2070 segno = segno_in_journal(journal, i); 2071 se = get_seg_entry(sbi, segno); 2072 2073 get_current_sit_page(sbi, segno, sit_blk); 2074 sit = &sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, segno)]; 2075 2076 memcpy(sit->valid_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE); 2077 sit->vblocks = cpu_to_le16((se->type << SIT_VBLOCKS_SHIFT) | 2078 se->valid_blocks); 2079 sit->mtime = cpu_to_le64(se->mtime); 2080 2081 rewrite_current_sit_page(sbi, segno, sit_blk); 2082 } 2083 2084 free(sit_blk); 2085 journal->n_sits = 0; 2086 return i; 2087 } 2088 2089 static int flush_nat_journal_entries(struct f2fs_sb_info *sbi) 2090 { 2091 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); 2092 struct f2fs_journal *journal = &curseg->sum_blk->journal; 2093 struct f2fs_nat_block *nat_block; 2094 pgoff_t block_addr; 2095 int entry_off; 2096 nid_t nid; 2097 int ret; 2098 int i = 0; 2099 2100 nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1); 2101 ASSERT(nat_block); 2102 next: 2103 if (i >= nats_in_cursum(journal)) { 2104 free(nat_block); 2105 journal->n_nats = 0; 2106 return i; 2107 } 2108 2109 nid = le32_to_cpu(nid_in_journal(journal, i)); 2110 2111 entry_off = nid % NAT_ENTRY_PER_BLOCK; 2112 block_addr = current_nat_addr(sbi, nid, NULL); 2113 2114 ret = dev_read_block(nat_block, block_addr); 2115 ASSERT(ret >= 0); 2116 2117 memcpy(&nat_block->entries[entry_off], &nat_in_journal(journal, i), 2118 sizeof(struct f2fs_nat_entry)); 2119 2120 ret = dev_write_block(nat_block, block_addr); 2121 ASSERT(ret >= 0); 2122 i++; 2123 goto next; 2124 } 2125 2126 void flush_journal_entries(struct f2fs_sb_info *sbi) 2127 { 2128 int n_nats = flush_nat_journal_entries(sbi); 2129 int n_sits = flush_sit_journal_entries(sbi); 2130 2131 if (n_nats || n_sits) 2132 write_checkpoint(sbi); 2133 } 2134 2135 void flush_sit_entries(struct f2fs_sb_info *sbi) 2136 { 2137 struct sit_info *sit_i = SIT_I(sbi); 2138 struct f2fs_sit_block *sit_blk; 2139 unsigned int segno = 0; 2140 2141 sit_blk = calloc(BLOCK_SZ, 1); 2142 ASSERT(sit_blk); 2143 /* update free segments */ 2144 for (segno = 0; segno < TOTAL_SEGS(sbi); segno++) { 2145 struct f2fs_sit_entry *sit; 2146 struct seg_entry *se; 2147 2148 se = get_seg_entry(sbi, segno); 2149 2150 if (!se->dirty) 2151 continue; 2152 2153 get_current_sit_page(sbi, segno, sit_blk); 2154 sit = &sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, segno)]; 2155 memcpy(sit->valid_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE); 2156 sit->vblocks = cpu_to_le16((se->type << SIT_VBLOCKS_SHIFT) | 2157 se->valid_blocks); 2158 rewrite_current_sit_page(sbi, segno, sit_blk); 2159 } 2160 2161 free(sit_blk); 2162 } 2163 2164 int find_next_free_block(struct f2fs_sb_info *sbi, u64 *to, int left, int type) 2165 { 2166 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi); 2167 struct seg_entry *se; 2168 u32 segno; 2169 u32 offset; 2170 int not_enough = 0; 2171 u64 end_blkaddr = (get_sb(segment_count_main) << 2172 get_sb(log_blocks_per_seg)) + get_sb(main_blkaddr); 2173 2174 if (*to > 0) 2175 *to -= left; 2176 if (get_free_segments(sbi) <= SM_I(sbi)->reserved_segments + 1) 2177 not_enough = 1; 2178 2179 while (*to >= SM_I(sbi)->main_blkaddr && *to < end_blkaddr) { 2180 segno = GET_SEGNO(sbi, *to); 2181 offset = OFFSET_IN_SEG(sbi, *to); 2182 2183 se = get_seg_entry(sbi, segno); 2184 2185 if (se->valid_blocks == sbi->blocks_per_seg || 2186 IS_CUR_SEGNO(sbi, segno)) { 2187 *to = left ? START_BLOCK(sbi, segno) - 1: 2188 START_BLOCK(sbi, segno + 1); 2189 continue; 2190 } 2191 2192 if (se->valid_blocks == 0 && not_enough) { 2193 *to = left ? START_BLOCK(sbi, segno) - 1: 2194 START_BLOCK(sbi, segno + 1); 2195 continue; 2196 } 2197 2198 if (se->valid_blocks == 0 && !(segno % sbi->segs_per_sec)) { 2199 struct seg_entry *se2; 2200 unsigned int i; 2201 2202 for (i = 1; i < sbi->segs_per_sec; i++) { 2203 se2 = get_seg_entry(sbi, segno + i); 2204 if (se2->valid_blocks) 2205 break; 2206 } 2207 if (i == sbi->segs_per_sec) 2208 return 0; 2209 } 2210 2211 if (se->type == type && 2212 !f2fs_test_bit(offset, (const char *)se->cur_valid_map)) 2213 return 0; 2214 2215 *to = left ? *to - 1: *to + 1; 2216 } 2217 return -1; 2218 } 2219 2220 void move_curseg_info(struct f2fs_sb_info *sbi, u64 from, int left) 2221 { 2222 int i, ret; 2223 2224 /* update summary blocks having nullified journal entries */ 2225 for (i = 0; i < NO_CHECK_TYPE; i++) { 2226 struct curseg_info *curseg = CURSEG_I(sbi, i); 2227 struct f2fs_summary_block buf; 2228 u32 old_segno; 2229 u64 ssa_blk, to; 2230 2231 /* update original SSA too */ 2232 ssa_blk = GET_SUM_BLKADDR(sbi, curseg->segno); 2233 ret = dev_write_block(curseg->sum_blk, ssa_blk); 2234 ASSERT(ret >= 0); 2235 2236 to = from; 2237 ret = find_next_free_block(sbi, &to, left, i); 2238 ASSERT(ret == 0); 2239 2240 old_segno = curseg->segno; 2241 curseg->segno = GET_SEGNO(sbi, to); 2242 curseg->next_blkoff = OFFSET_IN_SEG(sbi, to); 2243 curseg->alloc_type = SSR; 2244 2245 /* update new segno */ 2246 ssa_blk = GET_SUM_BLKADDR(sbi, curseg->segno); 2247 ret = dev_read_block(&buf, ssa_blk); 2248 ASSERT(ret >= 0); 2249 2250 memcpy(curseg->sum_blk, &buf, SUM_ENTRIES_SIZE); 2251 2252 /* update se->types */ 2253 reset_curseg(sbi, i); 2254 2255 DBG(1, "Move curseg[%d] %x -> %x after %"PRIx64"\n", 2256 i, old_segno, curseg->segno, from); 2257 } 2258 } 2259 2260 void zero_journal_entries(struct f2fs_sb_info *sbi) 2261 { 2262 int i; 2263 2264 for (i = 0; i < NO_CHECK_TYPE; i++) 2265 CURSEG_I(sbi, i)->sum_blk->journal.n_nats = 0; 2266 } 2267 2268 void write_curseg_info(struct f2fs_sb_info *sbi) 2269 { 2270 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi); 2271 int i; 2272 2273 for (i = 0; i < NO_CHECK_TYPE; i++) { 2274 cp->alloc_type[i] = CURSEG_I(sbi, i)->alloc_type; 2275 if (i < CURSEG_HOT_NODE) { 2276 set_cp(cur_data_segno[i], CURSEG_I(sbi, i)->segno); 2277 set_cp(cur_data_blkoff[i], 2278 CURSEG_I(sbi, i)->next_blkoff); 2279 } else { 2280 int n = i - CURSEG_HOT_NODE; 2281 2282 set_cp(cur_node_segno[n], CURSEG_I(sbi, i)->segno); 2283 set_cp(cur_node_blkoff[n], 2284 CURSEG_I(sbi, i)->next_blkoff); 2285 } 2286 } 2287 } 2288 2289 int lookup_nat_in_journal(struct f2fs_sb_info *sbi, u32 nid, 2290 struct f2fs_nat_entry *raw_nat) 2291 { 2292 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); 2293 struct f2fs_journal *journal = &curseg->sum_blk->journal; 2294 int i = 0; 2295 2296 for (i = 0; i < nats_in_cursum(journal); i++) { 2297 if (le32_to_cpu(nid_in_journal(journal, i)) == nid) { 2298 memcpy(raw_nat, &nat_in_journal(journal, i), 2299 sizeof(struct f2fs_nat_entry)); 2300 DBG(3, "==> Found nid [0x%x] in nat cache\n", nid); 2301 return i; 2302 } 2303 } 2304 return -1; 2305 } 2306 2307 void nullify_nat_entry(struct f2fs_sb_info *sbi, u32 nid) 2308 { 2309 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); 2310 struct f2fs_journal *journal = &curseg->sum_blk->journal; 2311 struct f2fs_nat_block *nat_block; 2312 pgoff_t block_addr; 2313 int entry_off; 2314 int ret; 2315 int i = 0; 2316 2317 /* check in journal */ 2318 for (i = 0; i < nats_in_cursum(journal); i++) { 2319 if (le32_to_cpu(nid_in_journal(journal, i)) == nid) { 2320 memset(&nat_in_journal(journal, i), 0, 2321 sizeof(struct f2fs_nat_entry)); 2322 FIX_MSG("Remove nid [0x%x] in nat journal", nid); 2323 return; 2324 } 2325 } 2326 nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1); 2327 ASSERT(nat_block); 2328 2329 entry_off = nid % NAT_ENTRY_PER_BLOCK; 2330 block_addr = current_nat_addr(sbi, nid, NULL); 2331 2332 ret = dev_read_block(nat_block, block_addr); 2333 ASSERT(ret >= 0); 2334 2335 if (nid == F2FS_NODE_INO(sbi) || nid == F2FS_META_INO(sbi)) { 2336 FIX_MSG("nid [0x%x] block_addr= 0x%x -> 0x1", nid, 2337 le32_to_cpu(nat_block->entries[entry_off].block_addr)); 2338 nat_block->entries[entry_off].block_addr = cpu_to_le32(0x1); 2339 } else { 2340 memset(&nat_block->entries[entry_off], 0, 2341 sizeof(struct f2fs_nat_entry)); 2342 FIX_MSG("Remove nid [0x%x] in NAT", nid); 2343 } 2344 2345 ret = dev_write_block(nat_block, block_addr); 2346 ASSERT(ret >= 0); 2347 free(nat_block); 2348 } 2349 2350 void write_checkpoint(struct f2fs_sb_info *sbi) 2351 { 2352 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi); 2353 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi); 2354 block_t orphan_blks = 0; 2355 unsigned long long cp_blk_no; 2356 u32 flags = CP_UMOUNT_FLAG; 2357 int i, ret; 2358 u_int32_t crc = 0; 2359 2360 if (is_set_ckpt_flags(cp, CP_ORPHAN_PRESENT_FLAG)) { 2361 orphan_blks = __start_sum_addr(sbi) - 1; 2362 flags |= CP_ORPHAN_PRESENT_FLAG; 2363 } 2364 if (is_set_ckpt_flags(cp, CP_TRIMMED_FLAG)) 2365 flags |= CP_TRIMMED_FLAG; 2366 if (is_set_ckpt_flags(cp, CP_DISABLED_FLAG)) 2367 flags |= CP_DISABLED_FLAG; 2368 2369 set_cp(free_segment_count, get_free_segments(sbi)); 2370 set_cp(valid_block_count, sbi->total_valid_block_count); 2371 set_cp(cp_pack_total_block_count, 8 + orphan_blks + get_sb(cp_payload)); 2372 2373 flags = update_nat_bits_flags(sb, cp, flags); 2374 set_cp(ckpt_flags, flags); 2375 2376 crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, cp, CP_CHKSUM_OFFSET); 2377 *((__le32 *)((unsigned char *)cp + CP_CHKSUM_OFFSET)) = cpu_to_le32(crc); 2378 2379 cp_blk_no = get_sb(cp_blkaddr); 2380 if (sbi->cur_cp == 2) 2381 cp_blk_no += 1 << get_sb(log_blocks_per_seg); 2382 2383 /* write the first cp */ 2384 ret = dev_write_block(cp, cp_blk_no++); 2385 ASSERT(ret >= 0); 2386 2387 /* skip payload */ 2388 cp_blk_no += get_sb(cp_payload); 2389 /* skip orphan blocks */ 2390 cp_blk_no += orphan_blks; 2391 2392 /* update summary blocks having nullified journal entries */ 2393 for (i = 0; i < NO_CHECK_TYPE; i++) { 2394 struct curseg_info *curseg = CURSEG_I(sbi, i); 2395 u64 ssa_blk; 2396 2397 ret = dev_write_block(curseg->sum_blk, cp_blk_no++); 2398 ASSERT(ret >= 0); 2399 2400 /* update original SSA too */ 2401 ssa_blk = GET_SUM_BLKADDR(sbi, curseg->segno); 2402 ret = dev_write_block(curseg->sum_blk, ssa_blk); 2403 ASSERT(ret >= 0); 2404 } 2405 2406 /* Write nat bits */ 2407 if (flags & CP_NAT_BITS_FLAG) 2408 write_nat_bits(sbi, sb, cp, sbi->cur_cp); 2409 2410 /* in case of sudden power off */ 2411 ret = f2fs_fsync_device(); 2412 ASSERT(ret >= 0); 2413 2414 /* write the last cp */ 2415 ret = dev_write_block(cp, cp_blk_no++); 2416 ASSERT(ret >= 0); 2417 } 2418 2419 void build_nat_area_bitmap(struct f2fs_sb_info *sbi) 2420 { 2421 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); 2422 struct f2fs_journal *journal = &curseg->sum_blk->journal; 2423 struct f2fs_fsck *fsck = F2FS_FSCK(sbi); 2424 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi); 2425 struct f2fs_nm_info *nm_i = NM_I(sbi); 2426 struct f2fs_nat_block *nat_block; 2427 struct node_info ni; 2428 u32 nid, nr_nat_blks; 2429 pgoff_t block_off; 2430 pgoff_t block_addr; 2431 int seg_off; 2432 int ret; 2433 unsigned int i; 2434 2435 nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1); 2436 ASSERT(nat_block); 2437 2438 /* Alloc & build nat entry bitmap */ 2439 nr_nat_blks = (get_sb(segment_count_nat) / 2) << 2440 sbi->log_blocks_per_seg; 2441 2442 fsck->nr_nat_entries = nr_nat_blks * NAT_ENTRY_PER_BLOCK; 2443 fsck->nat_area_bitmap_sz = (fsck->nr_nat_entries + 7) / 8; 2444 fsck->nat_area_bitmap = calloc(fsck->nat_area_bitmap_sz, 1); 2445 ASSERT(fsck->nat_area_bitmap); 2446 2447 fsck->entries = calloc(sizeof(struct f2fs_nat_entry), 2448 fsck->nr_nat_entries); 2449 ASSERT(fsck->entries); 2450 2451 for (block_off = 0; block_off < nr_nat_blks; block_off++) { 2452 2453 seg_off = block_off >> sbi->log_blocks_per_seg; 2454 block_addr = (pgoff_t)(nm_i->nat_blkaddr + 2455 (seg_off << sbi->log_blocks_per_seg << 1) + 2456 (block_off & ((1 << sbi->log_blocks_per_seg) - 1))); 2457 2458 if (f2fs_test_bit(block_off, nm_i->nat_bitmap)) 2459 block_addr += sbi->blocks_per_seg; 2460 2461 ret = dev_read_block(nat_block, block_addr); 2462 ASSERT(ret >= 0); 2463 2464 nid = block_off * NAT_ENTRY_PER_BLOCK; 2465 for (i = 0; i < NAT_ENTRY_PER_BLOCK; i++) { 2466 ni.nid = nid + i; 2467 2468 if ((nid + i) == F2FS_NODE_INO(sbi) || 2469 (nid + i) == F2FS_META_INO(sbi)) { 2470 /* 2471 * block_addr of node/meta inode should be 0x1. 2472 * Set this bit, and fsck_verify will fix it. 2473 */ 2474 if (le32_to_cpu(nat_block->entries[i].block_addr) != 0x1) { 2475 ASSERT_MSG("\tError: ino[0x%x] block_addr[0x%x] is invalid\n", 2476 nid + i, le32_to_cpu(nat_block->entries[i].block_addr)); 2477 f2fs_set_bit(nid + i, fsck->nat_area_bitmap); 2478 } 2479 continue; 2480 } 2481 2482 node_info_from_raw_nat(&ni, &nat_block->entries[i]); 2483 if (ni.blk_addr == 0x0) 2484 continue; 2485 if (ni.ino == 0x0) { 2486 ASSERT_MSG("\tError: ino[0x%8x] or blk_addr[0x%16x]" 2487 " is invalid\n", ni.ino, ni.blk_addr); 2488 } 2489 if (ni.ino == (nid + i)) { 2490 fsck->nat_valid_inode_cnt++; 2491 DBG(3, "ino[0x%8x] maybe is inode\n", ni.ino); 2492 } 2493 if (nid + i == 0) { 2494 /* 2495 * nat entry [0] must be null. If 2496 * it is corrupted, set its bit in 2497 * nat_area_bitmap, fsck_verify will 2498 * nullify it 2499 */ 2500 ASSERT_MSG("Invalid nat entry[0]: " 2501 "blk_addr[0x%x]\n", ni.blk_addr); 2502 fsck->chk.valid_nat_entry_cnt--; 2503 } 2504 2505 DBG(3, "nid[0x%8x] addr[0x%16x] ino[0x%8x]\n", 2506 nid + i, ni.blk_addr, ni.ino); 2507 f2fs_set_bit(nid + i, fsck->nat_area_bitmap); 2508 fsck->chk.valid_nat_entry_cnt++; 2509 2510 fsck->entries[nid + i] = nat_block->entries[i]; 2511 } 2512 } 2513 2514 /* Traverse nat journal, update the corresponding entries */ 2515 for (i = 0; i < nats_in_cursum(journal); i++) { 2516 struct f2fs_nat_entry raw_nat; 2517 nid = le32_to_cpu(nid_in_journal(journal, i)); 2518 ni.nid = nid; 2519 2520 DBG(3, "==> Found nid [0x%x] in nat cache, update it\n", nid); 2521 2522 /* Clear the original bit and count */ 2523 if (fsck->entries[nid].block_addr != 0x0) { 2524 fsck->chk.valid_nat_entry_cnt--; 2525 f2fs_clear_bit(nid, fsck->nat_area_bitmap); 2526 if (fsck->entries[nid].ino == nid) 2527 fsck->nat_valid_inode_cnt--; 2528 } 2529 2530 /* Use nat entries in journal */ 2531 memcpy(&raw_nat, &nat_in_journal(journal, i), 2532 sizeof(struct f2fs_nat_entry)); 2533 node_info_from_raw_nat(&ni, &raw_nat); 2534 if (ni.blk_addr != 0x0) { 2535 if (ni.ino == 0x0) 2536 ASSERT_MSG("\tError: ino[0x%8x] or blk_addr[0x%16x]" 2537 " is invalid\n", ni.ino, ni.blk_addr); 2538 if (ni.ino == nid) { 2539 fsck->nat_valid_inode_cnt++; 2540 DBG(3, "ino[0x%8x] maybe is inode\n", ni.ino); 2541 } 2542 f2fs_set_bit(nid, fsck->nat_area_bitmap); 2543 fsck->chk.valid_nat_entry_cnt++; 2544 DBG(3, "nid[0x%x] in nat cache\n", nid); 2545 } 2546 fsck->entries[nid] = raw_nat; 2547 } 2548 free(nat_block); 2549 2550 DBG(1, "valid nat entries (block_addr != 0x0) [0x%8x : %u]\n", 2551 fsck->chk.valid_nat_entry_cnt, 2552 fsck->chk.valid_nat_entry_cnt); 2553 } 2554 2555 static int check_sector_size(struct f2fs_super_block *sb) 2556 { 2557 u_int32_t log_sectorsize, log_sectors_per_block; 2558 2559 log_sectorsize = log_base_2(c.sector_size); 2560 log_sectors_per_block = log_base_2(c.sectors_per_blk); 2561 2562 if (log_sectorsize == get_sb(log_sectorsize) && 2563 log_sectors_per_block == get_sb(log_sectors_per_block)) 2564 return 0; 2565 2566 set_sb(log_sectorsize, log_sectorsize); 2567 set_sb(log_sectors_per_block, log_sectors_per_block); 2568 2569 update_superblock(sb, SB_MASK_ALL); 2570 return 0; 2571 } 2572 2573 static void tune_sb_features(struct f2fs_sb_info *sbi) 2574 { 2575 int sb_changed = 0; 2576 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi); 2577 2578 if (!(sb->feature & cpu_to_le32(F2FS_FEATURE_ENCRYPT)) && 2579 c.feature & cpu_to_le32(F2FS_FEATURE_ENCRYPT)) { 2580 sb->feature |= cpu_to_le32(F2FS_FEATURE_ENCRYPT); 2581 MSG(0, "Info: Set Encryption feature\n"); 2582 sb_changed = 1; 2583 } 2584 /* TODO: quota needs to allocate inode numbers */ 2585 2586 c.feature = sb->feature; 2587 if (!sb_changed) 2588 return; 2589 2590 update_superblock(sb, SB_MASK_ALL); 2591 } 2592 2593 int f2fs_do_mount(struct f2fs_sb_info *sbi) 2594 { 2595 struct f2fs_checkpoint *cp = NULL; 2596 struct f2fs_super_block *sb = NULL; 2597 int ret; 2598 2599 sbi->active_logs = NR_CURSEG_TYPE; 2600 ret = validate_super_block(sbi, SB0_ADDR); 2601 if (ret) { 2602 ret = validate_super_block(sbi, SB1_ADDR); 2603 if (ret) 2604 return -1; 2605 } 2606 sb = F2FS_RAW_SUPER(sbi); 2607 2608 ret = check_sector_size(sb); 2609 if (ret) 2610 return -1; 2611 2612 print_raw_sb_info(sb); 2613 2614 init_sb_info(sbi); 2615 2616 ret = get_valid_checkpoint(sbi); 2617 if (ret) { 2618 ERR_MSG("Can't find valid checkpoint\n"); 2619 return -1; 2620 } 2621 2622 if (sanity_check_ckpt(sbi)) { 2623 ERR_MSG("Checkpoint is polluted\n"); 2624 return -1; 2625 } 2626 cp = F2FS_CKPT(sbi); 2627 2628 print_ckpt_info(sbi); 2629 2630 if (c.quota_fix) { 2631 if (get_cp(ckpt_flags) & CP_QUOTA_NEED_FSCK_FLAG) 2632 c.fix_on = 1; 2633 } 2634 2635 if (c.auto_fix || c.preen_mode) { 2636 u32 flag = get_cp(ckpt_flags); 2637 2638 if (flag & CP_FSCK_FLAG || 2639 flag & CP_QUOTA_NEED_FSCK_FLAG || 2640 (exist_qf_ino(sb) && (flag & CP_ERROR_FLAG))) { 2641 c.fix_on = 1; 2642 } else if (!c.preen_mode) { 2643 print_cp_state(flag); 2644 return 1; 2645 } 2646 } 2647 2648 c.bug_on = 0; 2649 2650 tune_sb_features(sbi); 2651 2652 /* precompute checksum seed for metadata */ 2653 if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM)) 2654 c.chksum_seed = f2fs_cal_crc32(~0, sb->uuid, sizeof(sb->uuid)); 2655 2656 sbi->total_valid_node_count = get_cp(valid_node_count); 2657 sbi->total_valid_inode_count = get_cp(valid_inode_count); 2658 sbi->user_block_count = get_cp(user_block_count); 2659 sbi->total_valid_block_count = get_cp(valid_block_count); 2660 sbi->last_valid_block_count = sbi->total_valid_block_count; 2661 sbi->alloc_valid_block_count = 0; 2662 2663 if (build_segment_manager(sbi)) { 2664 ERR_MSG("build_segment_manager failed\n"); 2665 return -1; 2666 } 2667 2668 if (build_node_manager(sbi)) { 2669 ERR_MSG("build_node_manager failed\n"); 2670 return -1; 2671 } 2672 2673 /* Check nat_bits */ 2674 if (c.func == FSCK && is_set_ckpt_flags(cp, CP_NAT_BITS_FLAG)) { 2675 if (check_nat_bits(sbi, sb, cp) && c.fix_on) 2676 write_nat_bits(sbi, sb, cp, sbi->cur_cp); 2677 } 2678 return 0; 2679 } 2680 2681 void f2fs_do_umount(struct f2fs_sb_info *sbi) 2682 { 2683 struct sit_info *sit_i = SIT_I(sbi); 2684 struct f2fs_sm_info *sm_i = SM_I(sbi); 2685 struct f2fs_nm_info *nm_i = NM_I(sbi); 2686 unsigned int i; 2687 2688 /* free nm_info */ 2689 if (c.func == SLOAD || c.func == FSCK) 2690 free(nm_i->nid_bitmap); 2691 free(nm_i->nat_bitmap); 2692 free(sbi->nm_info); 2693 2694 /* free sit_info */ 2695 for (i = 0; i < TOTAL_SEGS(sbi); i++) 2696 free(sit_i->sentries[i].cur_valid_map); 2697 2698 free(sit_i->sit_bitmap); 2699 free(sm_i->sit_info); 2700 2701 /* free sm_info */ 2702 for (i = 0; i < NR_CURSEG_TYPE; i++) 2703 free(sm_i->curseg_array[i].sum_blk); 2704 2705 free(sm_i->curseg_array); 2706 free(sbi->sm_info); 2707 2708 free(sbi->ckpt); 2709 free(sbi->raw_super); 2710 } 2711