f2fs_fs.h revision 3a8c9a69525f75a6c59bee0eb3d882ddc341b3f1
1/** 2 * f2fs_fs.h 3 * 4 * Copyright (c) 2012 Samsung Electronics Co., Ltd. 5 * http://www.samsung.com/ 6 * 7 * Dual licensed under the GPL or LGPL version 2 licenses. 8 * 9 * The byteswap codes are copied from: 10 * samba_3_master/lib/ccan/endian/endian.h under LGPL 2.1 11 */ 12#ifndef __F2FS_FS_H__ 13#define __F2FS_FS_H__ 14 15#include <inttypes.h> 16#include <linux/types.h> 17#include <sys/types.h> 18 19#ifdef HAVE_CONFIG_H 20#include <config.h> 21#endif 22 23typedef u_int64_t u64; 24typedef u_int32_t u32; 25typedef u_int16_t u16; 26typedef u_int8_t u8; 27typedef u32 block_t; 28typedef u32 nid_t; 29typedef u8 bool; 30typedef unsigned long pgoff_t; 31 32#if HAVE_BYTESWAP_H 33#include <byteswap.h> 34#else 35/** 36 * bswap_16 - reverse bytes in a uint16_t value. 37 * @val: value whose bytes to swap. 38 * 39 * Example: 40 * // Output contains "1024 is 4 as two bytes reversed" 41 * printf("1024 is %u as two bytes reversed\n", bswap_16(1024)); 42 */ 43static inline uint16_t bswap_16(uint16_t val) 44{ 45 return ((val & (uint16_t)0x00ffU) << 8) 46 | ((val & (uint16_t)0xff00U) >> 8); 47} 48 49/** 50 * bswap_32 - reverse bytes in a uint32_t value. 51 * @val: value whose bytes to swap. 52 * 53 * Example: 54 * // Output contains "1024 is 262144 as four bytes reversed" 55 * printf("1024 is %u as four bytes reversed\n", bswap_32(1024)); 56 */ 57static inline uint32_t bswap_32(uint32_t val) 58{ 59 return ((val & (uint32_t)0x000000ffUL) << 24) 60 | ((val & (uint32_t)0x0000ff00UL) << 8) 61 | ((val & (uint32_t)0x00ff0000UL) >> 8) 62 | ((val & (uint32_t)0xff000000UL) >> 24); 63} 64#endif /* !HAVE_BYTESWAP_H */ 65 66#if !HAVE_BSWAP_64 67/** 68 * bswap_64 - reverse bytes in a uint64_t value. 69 * @val: value whose bytes to swap. 70 * 71 * Example: 72 * // Output contains "1024 is 1125899906842624 as eight bytes reversed" 73 * printf("1024 is %llu as eight bytes reversed\n", 74 * (unsigned long long)bswap_64(1024)); 75 */ 76static inline uint64_t bswap_64(uint64_t val) 77{ 78 return ((val & (uint64_t)0x00000000000000ffULL) << 56) 79 | ((val & (uint64_t)0x000000000000ff00ULL) << 40) 80 | ((val & (uint64_t)0x0000000000ff0000ULL) << 24) 81 | ((val & (uint64_t)0x00000000ff000000ULL) << 8) 82 | ((val & (uint64_t)0x000000ff00000000ULL) >> 8) 83 | ((val & (uint64_t)0x0000ff0000000000ULL) >> 24) 84 | ((val & (uint64_t)0x00ff000000000000ULL) >> 40) 85 | ((val & (uint64_t)0xff00000000000000ULL) >> 56); 86} 87#endif 88 89#if __BYTE_ORDER == __LITTLE_ENDIAN 90#define le16_to_cpu(x) ((__u16)(x)) 91#define le32_to_cpu(x) ((__u32)(x)) 92#define le64_to_cpu(x) ((__u64)(x)) 93#define cpu_to_le16(x) ((__u16)(x)) 94#define cpu_to_le32(x) ((__u32)(x)) 95#define cpu_to_le64(x) ((__u64)(x)) 96#elif __BYTE_ORDER == __BIG_ENDIAN 97#define le16_to_cpu(x) bswap_16(x) 98#define le32_to_cpu(x) bswap_32(x) 99#define le64_to_cpu(x) bswap_64(x) 100#define cpu_to_le16(x) bswap_16(x) 101#define cpu_to_le32(x) bswap_32(x) 102#define cpu_to_le64(x) bswap_64(x) 103#endif 104 105#define typecheck(type,x) \ 106 ({ type __dummy; \ 107 typeof(x) __dummy2; \ 108 (void)(&__dummy == &__dummy2); \ 109 1; \ 110 }) 111 112#define NULL_SEGNO ((unsigned int)~0) 113 114/* 115 * Debugging interfaces 116 */ 117#define FIX_MSG(fmt, ...) \ 118 do { \ 119 printf("[FIX] (%s:%4d) ", __func__, __LINE__); \ 120 printf(" --> "fmt"\n", ##__VA_ARGS__); \ 121 } while (0) 122 123#define ASSERT_MSG(fmt, ...) \ 124 do { \ 125 printf("[ASSERT] (%s:%4d) ", __func__, __LINE__); \ 126 printf(" --> "fmt"\n", ##__VA_ARGS__); \ 127 config.bug_on = 1; \ 128 } while (0) 129 130#define ASSERT(exp) \ 131 do { \ 132 if (!(exp)) { \ 133 printf("[ASSERT] (%s:%4d) " #exp"\n", \ 134 __func__, __LINE__); \ 135 exit(-1); \ 136 } \ 137 } while (0) 138 139#define ERR_MSG(fmt, ...) \ 140 do { \ 141 printf("[%s:%d] " fmt, __func__, __LINE__, ##__VA_ARGS__); \ 142 } while (0) 143 144#define MSG(n, fmt, ...) \ 145 do { \ 146 if (config.dbg_lv >= n) { \ 147 printf(fmt, ##__VA_ARGS__); \ 148 } \ 149 } while (0) 150 151#define DBG(n, fmt, ...) \ 152 do { \ 153 if (config.dbg_lv >= n) { \ 154 printf("[%s:%4d] " fmt, \ 155 __func__, __LINE__, ##__VA_ARGS__); \ 156 } \ 157 } while (0) 158 159/* Display on console */ 160#define DISP(fmt, ptr, member) \ 161 do { \ 162 printf("%-30s" fmt, #member, ((ptr)->member)); \ 163 } while (0) 164 165#define DISP_u32(ptr, member) \ 166 do { \ 167 assert(sizeof((ptr)->member) <= 4); \ 168 printf("%-30s" "\t\t[0x%8x : %u]\n", \ 169 #member, ((ptr)->member), ((ptr)->member)); \ 170 } while (0) 171 172#define DISP_u64(ptr, member) \ 173 do { \ 174 assert(sizeof((ptr)->member) == 8); \ 175 printf("%-30s" "\t\t[0x%8llx : %llu]\n", \ 176 #member, ((ptr)->member), ((ptr)->member)); \ 177 } while (0) 178 179#define DISP_utf(ptr, member) \ 180 do { \ 181 printf("%-30s" "\t\t[%s]\n", #member, ((ptr)->member)); \ 182 } while (0) 183 184/* Display to buffer */ 185#define BUF_DISP_u32(buf, data, len, ptr, member) \ 186 do { \ 187 assert(sizeof((ptr)->member) <= 4); \ 188 snprintf(buf, len, #member); \ 189 snprintf(data, len, "0x%x : %u", ((ptr)->member), \ 190 ((ptr)->member)); \ 191 } while (0) 192 193#define BUF_DISP_u64(buf, data, len, ptr, member) \ 194 do { \ 195 assert(sizeof((ptr)->member) == 8); \ 196 snprintf(buf, len, #member); \ 197 snprintf(data, len, "0x%llx : %llu", ((ptr)->member), \ 198 ((ptr)->member)); \ 199 } while (0) 200 201#define BUF_DISP_utf(buf, data, len, ptr, member) \ 202 snprintf(buf, len, #member) 203 204/* these are defined in kernel */ 205#define PAGE_SIZE 4096 206#define PAGE_CACHE_SIZE 4096 207#define BITS_PER_BYTE 8 208#define F2FS_SUPER_MAGIC 0xF2F52010 /* F2FS Magic Number */ 209#define CHECKSUM_OFFSET 4092 210 211/* for mkfs */ 212#define F2FS_MIN_VOLUME_SIZE 104857600 213#define F2FS_NUMBER_OF_CHECKPOINT_PACK 2 214#define DEFAULT_SECTOR_SIZE 512 215#define DEFAULT_SECTORS_PER_BLOCK 8 216#define DEFAULT_BLOCKS_PER_SEGMENT 512 217#define DEFAULT_SEGMENTS_PER_SECTION 1 218 219enum f2fs_config_func { 220 FSCK, 221 DUMP, 222}; 223 224struct f2fs_configuration { 225 u_int32_t sector_size; 226 u_int32_t reserved_segments; 227 u_int32_t overprovision; 228 u_int32_t cur_seg[6]; 229 u_int32_t segs_per_sec; 230 u_int32_t secs_per_zone; 231 u_int32_t start_sector; 232 u_int64_t total_sectors; 233 u_int32_t sectors_per_blk; 234 u_int32_t blks_per_seg; 235 char *vol_label; 236 int heap; 237 int32_t fd; 238 int32_t dump_fd; 239 char *device_name; 240 char *extension_list; 241 int dbg_lv; 242 int trim; 243 int func; 244 void *private; 245 int fix_on; 246 int bug_on; 247 int auto_fix; 248} __attribute__((packed)); 249 250#ifdef CONFIG_64BIT 251#define BITS_PER_LONG 64 252#else 253#define BITS_PER_LONG 32 254#endif 255 256#define BIT_MASK(nr) (1 << (nr % BITS_PER_LONG)) 257#define BIT_WORD(nr) (nr / BITS_PER_LONG) 258 259/* 260 * Copied from fs/f2fs/f2fs.h 261 */ 262#define NR_CURSEG_DATA_TYPE (3) 263#define NR_CURSEG_NODE_TYPE (3) 264#define NR_CURSEG_TYPE (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE) 265 266enum { 267 CURSEG_HOT_DATA = 0, /* directory entry blocks */ 268 CURSEG_WARM_DATA, /* data blocks */ 269 CURSEG_COLD_DATA, /* multimedia or GCed data blocks */ 270 CURSEG_HOT_NODE, /* direct node blocks of directory files */ 271 CURSEG_WARM_NODE, /* direct node blocks of normal files */ 272 CURSEG_COLD_NODE, /* indirect node blocks */ 273 NO_CHECK_TYPE 274}; 275 276/* 277 * Copied from fs/f2fs/segment.h 278 */ 279#define GET_SUM_TYPE(footer) ((footer)->entry_type) 280#define SET_SUM_TYPE(footer, type) ((footer)->entry_type = type) 281 282/* 283 * Copied from include/linux/f2fs_sb.h 284 */ 285#define F2FS_SUPER_OFFSET 1024 /* byte-size offset */ 286#define F2FS_LOG_SECTOR_SIZE 9 /* 9 bits for 512 byte */ 287#define F2FS_LOG_SECTORS_PER_BLOCK 3 /* 4KB: F2FS_BLKSIZE */ 288#define F2FS_BLKSIZE 4096 /* support only 4KB block */ 289#define F2FS_MAX_EXTENSION 64 /* # of extension entries */ 290#define F2FS_BLK_ALIGN(x) (((x) + F2FS_BLKSIZE - 1) / F2FS_BLKSIZE) 291 292#define NULL_ADDR 0x0U 293#define NEW_ADDR -1U 294 295#define F2FS_ROOT_INO(sbi) (sbi->root_ino_num) 296#define F2FS_NODE_INO(sbi) (sbi->node_ino_num) 297#define F2FS_META_INO(sbi) (sbi->meta_ino_num) 298 299/* This flag is used by node and meta inodes, and by recovery */ 300#define GFP_F2FS_ZERO (GFP_NOFS | __GFP_ZERO) 301 302/* 303 * For further optimization on multi-head logs, on-disk layout supports maximum 304 * 16 logs by default. The number, 16, is expected to cover all the cases 305 * enoughly. The implementaion currently uses no more than 6 logs. 306 * Half the logs are used for nodes, and the other half are used for data. 307 */ 308#define MAX_ACTIVE_LOGS 16 309#define MAX_ACTIVE_NODE_LOGS 8 310#define MAX_ACTIVE_DATA_LOGS 8 311 312/* 313 * For superblock 314 */ 315struct f2fs_super_block { 316 __le32 magic; /* Magic Number */ 317 __le16 major_ver; /* Major Version */ 318 __le16 minor_ver; /* Minor Version */ 319 __le32 log_sectorsize; /* log2 sector size in bytes */ 320 __le32 log_sectors_per_block; /* log2 # of sectors per block */ 321 __le32 log_blocksize; /* log2 block size in bytes */ 322 __le32 log_blocks_per_seg; /* log2 # of blocks per segment */ 323 __le32 segs_per_sec; /* # of segments per section */ 324 __le32 secs_per_zone; /* # of sections per zone */ 325 __le32 checksum_offset; /* checksum offset inside super block */ 326 __le64 block_count; /* total # of user blocks */ 327 __le32 section_count; /* total # of sections */ 328 __le32 segment_count; /* total # of segments */ 329 __le32 segment_count_ckpt; /* # of segments for checkpoint */ 330 __le32 segment_count_sit; /* # of segments for SIT */ 331 __le32 segment_count_nat; /* # of segments for NAT */ 332 __le32 segment_count_ssa; /* # of segments for SSA */ 333 __le32 segment_count_main; /* # of segments for main area */ 334 __le32 segment0_blkaddr; /* start block address of segment 0 */ 335 __le32 cp_blkaddr; /* start block address of checkpoint */ 336 __le32 sit_blkaddr; /* start block address of SIT */ 337 __le32 nat_blkaddr; /* start block address of NAT */ 338 __le32 ssa_blkaddr; /* start block address of SSA */ 339 __le32 main_blkaddr; /* start block address of main area */ 340 __le32 root_ino; /* root inode number */ 341 __le32 node_ino; /* node inode number */ 342 __le32 meta_ino; /* meta inode number */ 343 __u8 uuid[16]; /* 128-bit uuid for volume */ 344 __le16 volume_name[512]; /* volume name */ 345 __le32 extension_count; /* # of extensions below */ 346 __u8 extension_list[F2FS_MAX_EXTENSION][8]; /* extension array */ 347 __le32 cp_payload; 348} __attribute__((packed)); 349 350/* 351 * For checkpoint 352 */ 353#define CP_FSCK_FLAG 0x00000010 354#define CP_ERROR_FLAG 0x00000008 355#define CP_COMPACT_SUM_FLAG 0x00000004 356#define CP_ORPHAN_PRESENT_FLAG 0x00000002 357#define CP_UMOUNT_FLAG 0x00000001 358 359struct f2fs_checkpoint { 360 __le64 checkpoint_ver; /* checkpoint block version number */ 361 __le64 user_block_count; /* # of user blocks */ 362 __le64 valid_block_count; /* # of valid blocks in main area */ 363 __le32 rsvd_segment_count; /* # of reserved segments for gc */ 364 __le32 overprov_segment_count; /* # of overprovision segments */ 365 __le32 free_segment_count; /* # of free segments in main area */ 366 367 /* information of current node segments */ 368 __le32 cur_node_segno[MAX_ACTIVE_NODE_LOGS]; 369 __le16 cur_node_blkoff[MAX_ACTIVE_NODE_LOGS]; 370 /* information of current data segments */ 371 __le32 cur_data_segno[MAX_ACTIVE_DATA_LOGS]; 372 __le16 cur_data_blkoff[MAX_ACTIVE_DATA_LOGS]; 373 __le32 ckpt_flags; /* Flags : umount and journal_present */ 374 __le32 cp_pack_total_block_count; /* total # of one cp pack */ 375 __le32 cp_pack_start_sum; /* start block number of data summary */ 376 __le32 valid_node_count; /* Total number of valid nodes */ 377 __le32 valid_inode_count; /* Total number of valid inodes */ 378 __le32 next_free_nid; /* Next free node number */ 379 __le32 sit_ver_bitmap_bytesize; /* Default value 64 */ 380 __le32 nat_ver_bitmap_bytesize; /* Default value 256 */ 381 __le32 checksum_offset; /* checksum offset inside cp block */ 382 __le64 elapsed_time; /* mounted time */ 383 /* allocation type of current segment */ 384 unsigned char alloc_type[MAX_ACTIVE_LOGS]; 385 386 /* SIT and NAT version bitmap */ 387 unsigned char sit_nat_version_bitmap[1]; 388} __attribute__((packed)); 389 390/* 391 * For orphan inode management 392 */ 393#define F2FS_ORPHANS_PER_BLOCK 1020 394 395struct f2fs_orphan_block { 396 __le32 ino[F2FS_ORPHANS_PER_BLOCK]; /* inode numbers */ 397 __le32 reserved; /* reserved */ 398 __le16 blk_addr; /* block index in current CP */ 399 __le16 blk_count; /* Number of orphan inode blocks in CP */ 400 __le32 entry_count; /* Total number of orphan nodes in current CP */ 401 __le32 check_sum; /* CRC32 for orphan inode block */ 402} __attribute__((packed)); 403 404/* 405 * For NODE structure 406 */ 407struct f2fs_extent { 408 __le32 fofs; /* start file offset of the extent */ 409 __le32 blk_addr; /* start block address of the extent */ 410 __le32 len; /* lengh of the extent */ 411} __attribute__((packed)); 412 413#define F2FS_NAME_LEN 255 414#define F2FS_INLINE_XATTR_ADDRS 50 /* 200 bytes for inline xattrs */ 415#define DEF_ADDRS_PER_INODE 923 /* Address Pointers in an Inode */ 416#define ADDRS_PER_INODE(fi) addrs_per_inode(fi) 417#define ADDRS_PER_BLOCK 1018 /* Address Pointers in a Direct Block */ 418#define NIDS_PER_BLOCK 1018 /* Node IDs in an Indirect Block */ 419 420#define NODE_DIR1_BLOCK (DEF_ADDRS_PER_INODE + 1) 421#define NODE_DIR2_BLOCK (DEF_ADDRS_PER_INODE + 2) 422#define NODE_IND1_BLOCK (DEF_ADDRS_PER_INODE + 3) 423#define NODE_IND2_BLOCK (DEF_ADDRS_PER_INODE + 4) 424#define NODE_DIND_BLOCK (DEF_ADDRS_PER_INODE + 5) 425 426#define F2FS_INLINE_XATTR 0x01 /* file inline xattr flag */ 427#define F2FS_INLINE_DATA 0x02 /* file inline data flag */ 428#define F2FS_INLINE_DENTRY 0x04 /* file inline dentry flag */ 429 430#define MAX_INLINE_DATA (sizeof(__le32) * (DEF_ADDRS_PER_INODE - \ 431 F2FS_INLINE_XATTR_ADDRS - 1)) 432 433#define INLINE_DATA_OFFSET (PAGE_CACHE_SIZE - sizeof(struct node_footer) \ 434 - sizeof(__le32)*(DEF_ADDRS_PER_INODE + 5 - 1)) 435 436#define DEF_DIR_LEVEL 0 437 438struct f2fs_inode { 439 __le16 i_mode; /* file mode */ 440 __u8 i_advise; /* file hints */ 441 __u8 i_inline; /* file inline flags */ 442 __le32 i_uid; /* user ID */ 443 __le32 i_gid; /* group ID */ 444 __le32 i_links; /* links count */ 445 __le64 i_size; /* file size in bytes */ 446 __le64 i_blocks; /* file size in blocks */ 447 __le64 i_atime; /* access time */ 448 __le64 i_ctime; /* change time */ 449 __le64 i_mtime; /* modification time */ 450 __le32 i_atime_nsec; /* access time in nano scale */ 451 __le32 i_ctime_nsec; /* change time in nano scale */ 452 __le32 i_mtime_nsec; /* modification time in nano scale */ 453 __le32 i_generation; /* file version (for NFS) */ 454 __le32 i_current_depth; /* only for directory depth */ 455 __le32 i_xattr_nid; /* nid to save xattr */ 456 __le32 i_flags; /* file attributes */ 457 __le32 i_pino; /* parent inode number */ 458 __le32 i_namelen; /* file name length */ 459 __u8 i_name[F2FS_NAME_LEN]; /* file name for SPOR */ 460 __u8 i_dir_level; /* dentry_level for large dir */ 461 462 struct f2fs_extent i_ext; /* caching a largest extent */ 463 464 __le32 i_addr[DEF_ADDRS_PER_INODE]; /* Pointers to data blocks */ 465 466 __le32 i_nid[5]; /* direct(2), indirect(2), 467 double_indirect(1) node id */ 468} __attribute__((packed)); 469 470struct direct_node { 471 __le32 addr[ADDRS_PER_BLOCK]; /* array of data block address */ 472} __attribute__((packed)); 473 474struct indirect_node { 475 __le32 nid[NIDS_PER_BLOCK]; /* array of data block address */ 476} __attribute__((packed)); 477 478enum { 479 COLD_BIT_SHIFT = 0, 480 FSYNC_BIT_SHIFT, 481 DENT_BIT_SHIFT, 482 OFFSET_BIT_SHIFT 483}; 484 485#define XATTR_NODE_OFFSET ((((unsigned int)-1) << OFFSET_BIT_SHIFT) \ 486 >> OFFSET_BIT_SHIFT) 487 488struct node_footer { 489 __le32 nid; /* node id */ 490 __le32 ino; /* inode nunmber */ 491 __le32 flag; /* include cold/fsync/dentry marks and offset */ 492 __le64 cp_ver; /* checkpoint version */ 493 __le32 next_blkaddr; /* next node page block address */ 494} __attribute__((packed)); 495 496struct f2fs_node { 497 /* can be one of three types: inode, direct, and indirect types */ 498 union { 499 struct f2fs_inode i; 500 struct direct_node dn; 501 struct indirect_node in; 502 }; 503 struct node_footer footer; 504} __attribute__((packed)); 505 506/* 507 * For NAT entries 508 */ 509#define NAT_ENTRY_PER_BLOCK (PAGE_CACHE_SIZE / sizeof(struct f2fs_nat_entry)) 510 511struct f2fs_nat_entry { 512 __u8 version; /* latest version of cached nat entry */ 513 __le32 ino; /* inode number */ 514 __le32 block_addr; /* block address */ 515} __attribute__((packed)); 516 517struct f2fs_nat_block { 518 struct f2fs_nat_entry entries[NAT_ENTRY_PER_BLOCK]; 519} __attribute__((packed)); 520 521/* 522 * For SIT entries 523 * 524 * Each segment is 2MB in size by default so that a bitmap for validity of 525 * there-in blocks should occupy 64 bytes, 512 bits. 526 * Not allow to change this. 527 */ 528#define SIT_VBLOCK_MAP_SIZE 64 529#define SIT_ENTRY_PER_BLOCK (PAGE_CACHE_SIZE / sizeof(struct f2fs_sit_entry)) 530 531/* 532 * F2FS uses 4 bytes to represent block address. As a result, supported size of 533 * disk is 16 TB and it equals to 16 * 1024 * 1024 / 2 segments. 534 */ 535#define F2FS_MAX_SEGMENT ((16 * 1024 * 1024) / 2) 536#define MAX_SIT_BITMAP_SIZE ((F2FS_MAX_SEGMENT / SIT_ENTRY_PER_BLOCK) / 8) 537 538/* 539 * Note that f2fs_sit_entry->vblocks has the following bit-field information. 540 * [15:10] : allocation type such as CURSEG_XXXX_TYPE 541 * [9:0] : valid block count 542 */ 543#define SIT_VBLOCKS_SHIFT 10 544#define SIT_VBLOCKS_MASK ((1 << SIT_VBLOCKS_SHIFT) - 1) 545#define GET_SIT_VBLOCKS(raw_sit) \ 546 (le16_to_cpu((raw_sit)->vblocks) & SIT_VBLOCKS_MASK) 547#define GET_SIT_TYPE(raw_sit) \ 548 ((le16_to_cpu((raw_sit)->vblocks) & ~SIT_VBLOCKS_MASK) \ 549 >> SIT_VBLOCKS_SHIFT) 550 551struct f2fs_sit_entry { 552 __le16 vblocks; /* reference above */ 553 __u8 valid_map[SIT_VBLOCK_MAP_SIZE]; /* bitmap for valid blocks */ 554 __le64 mtime; /* segment age for cleaning */ 555} __attribute__((packed)); 556 557struct f2fs_sit_block { 558 struct f2fs_sit_entry entries[SIT_ENTRY_PER_BLOCK]; 559} __attribute__((packed)); 560 561/* 562 * For segment summary 563 * 564 * One summary block contains exactly 512 summary entries, which represents 565 * exactly 2MB segment by default. Not allow to change the basic units. 566 * 567 * NOTE: For initializing fields, you must use set_summary 568 * 569 * - If data page, nid represents dnode's nid 570 * - If node page, nid represents the node page's nid. 571 * 572 * The ofs_in_node is used by only data page. It represents offset 573 * from node's page's beginning to get a data block address. 574 * ex) data_blkaddr = (block_t)(nodepage_start_address + ofs_in_node) 575 */ 576#define ENTRIES_IN_SUM 512 577#define SUMMARY_SIZE (7) /* sizeof(struct summary) */ 578#define SUM_FOOTER_SIZE (5) /* sizeof(struct summary_footer) */ 579#define SUM_ENTRIES_SIZE (SUMMARY_SIZE * ENTRIES_IN_SUM) 580 581/* a summary entry for a 4KB-sized block in a segment */ 582struct f2fs_summary { 583 __le32 nid; /* parent node id */ 584 union { 585 __u8 reserved[3]; 586 struct { 587 __u8 version; /* node version number */ 588 __le16 ofs_in_node; /* block index in parent node */ 589 } __attribute__((packed)); 590 }; 591} __attribute__((packed)); 592 593/* summary block type, node or data, is stored to the summary_footer */ 594#define SUM_TYPE_NODE (1) 595#define SUM_TYPE_DATA (0) 596 597struct summary_footer { 598 unsigned char entry_type; /* SUM_TYPE_XXX */ 599 __u32 check_sum; /* summary checksum */ 600} __attribute__((packed)); 601 602#define SUM_JOURNAL_SIZE (F2FS_BLKSIZE - SUM_FOOTER_SIZE -\ 603 SUM_ENTRIES_SIZE) 604#define NAT_JOURNAL_ENTRIES ((SUM_JOURNAL_SIZE - 2) /\ 605 sizeof(struct nat_journal_entry)) 606#define NAT_JOURNAL_RESERVED ((SUM_JOURNAL_SIZE - 2) %\ 607 sizeof(struct nat_journal_entry)) 608#define SIT_JOURNAL_ENTRIES ((SUM_JOURNAL_SIZE - 2) /\ 609 sizeof(struct sit_journal_entry)) 610#define SIT_JOURNAL_RESERVED ((SUM_JOURNAL_SIZE - 2) %\ 611 sizeof(struct sit_journal_entry)) 612/* 613 * frequently updated NAT/SIT entries can be stored in the spare area in 614 * summary blocks 615 */ 616enum { 617 NAT_JOURNAL = 0, 618 SIT_JOURNAL 619}; 620 621struct nat_journal_entry { 622 __le32 nid; 623 struct f2fs_nat_entry ne; 624} __attribute__((packed)); 625 626struct nat_journal { 627 struct nat_journal_entry entries[NAT_JOURNAL_ENTRIES]; 628 __u8 reserved[NAT_JOURNAL_RESERVED]; 629} __attribute__((packed)); 630 631struct sit_journal_entry { 632 __le32 segno; 633 struct f2fs_sit_entry se; 634} __attribute__((packed)); 635 636struct sit_journal { 637 struct sit_journal_entry entries[SIT_JOURNAL_ENTRIES]; 638 __u8 reserved[SIT_JOURNAL_RESERVED]; 639} __attribute__((packed)); 640 641/* 4KB-sized summary block structure */ 642struct f2fs_summary_block { 643 struct f2fs_summary entries[ENTRIES_IN_SUM]; 644 union { 645 __le16 n_nats; 646 __le16 n_sits; 647 }; 648 /* spare area is used by NAT or SIT journals */ 649 union { 650 struct nat_journal nat_j; 651 struct sit_journal sit_j; 652 }; 653 struct summary_footer footer; 654} __attribute__((packed)); 655 656/* 657 * For directory operations 658 */ 659#define F2FS_DOT_HASH 0 660#define F2FS_DDOT_HASH F2FS_DOT_HASH 661#define F2FS_MAX_HASH (~((0x3ULL) << 62)) 662#define F2FS_HASH_COL_BIT ((0x1ULL) << 63) 663 664typedef __le32 f2fs_hash_t; 665 666/* One directory entry slot covers 8bytes-long file name */ 667#define F2FS_SLOT_LEN 8 668#define F2FS_SLOT_LEN_BITS 3 669 670#define GET_DENTRY_SLOTS(x) ((x + F2FS_SLOT_LEN - 1) >> F2FS_SLOT_LEN_BITS) 671 672/* the number of dentry in a block */ 673#define NR_DENTRY_IN_BLOCK 214 674 675/* MAX level for dir lookup */ 676#define MAX_DIR_HASH_DEPTH 63 677 678#define SIZE_OF_DIR_ENTRY 11 /* by byte */ 679#define SIZE_OF_DENTRY_BITMAP ((NR_DENTRY_IN_BLOCK + BITS_PER_BYTE - 1) / \ 680 BITS_PER_BYTE) 681#define SIZE_OF_RESERVED (PAGE_SIZE - ((SIZE_OF_DIR_ENTRY + \ 682 F2FS_SLOT_LEN) * \ 683 NR_DENTRY_IN_BLOCK + SIZE_OF_DENTRY_BITMAP)) 684 685/* One directory entry slot representing F2FS_SLOT_LEN-sized file name */ 686struct f2fs_dir_entry { 687 __le32 hash_code; /* hash code of file name */ 688 __le32 ino; /* inode number */ 689 __le16 name_len; /* lengh of file name */ 690 __u8 file_type; /* file type */ 691} __attribute__((packed)); 692 693/* 4KB-sized directory entry block */ 694struct f2fs_dentry_block { 695 /* validity bitmap for directory entries in each block */ 696 __u8 dentry_bitmap[SIZE_OF_DENTRY_BITMAP]; 697 __u8 reserved[SIZE_OF_RESERVED]; 698 struct f2fs_dir_entry dentry[NR_DENTRY_IN_BLOCK]; 699 __u8 filename[NR_DENTRY_IN_BLOCK][F2FS_SLOT_LEN]; 700} __attribute__((packed)); 701 702/* for inline dir */ 703#define NR_INLINE_DENTRY (MAX_INLINE_DATA * BITS_PER_BYTE / \ 704 ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \ 705 BITS_PER_BYTE + 1)) 706#define INLINE_DENTRY_BITMAP_SIZE ((NR_INLINE_DENTRY + \ 707 BITS_PER_BYTE - 1) / BITS_PER_BYTE) 708#define INLINE_RESERVED_SIZE (MAX_INLINE_DATA - \ 709 ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \ 710 NR_INLINE_DENTRY + INLINE_DENTRY_BITMAP_SIZE)) 711 712/* inline directory entry structure */ 713struct f2fs_inline_dentry { 714 __u8 dentry_bitmap[INLINE_DENTRY_BITMAP_SIZE]; 715 __u8 reserved[INLINE_RESERVED_SIZE]; 716 struct f2fs_dir_entry dentry[NR_INLINE_DENTRY]; 717 __u8 filename[NR_INLINE_DENTRY][F2FS_SLOT_LEN]; 718} __packed; 719 720/* file types used in inode_info->flags */ 721enum FILE_TYPE { 722 F2FS_FT_UNKNOWN, 723 F2FS_FT_REG_FILE, 724 F2FS_FT_DIR, 725 F2FS_FT_CHRDEV, 726 F2FS_FT_BLKDEV, 727 F2FS_FT_FIFO, 728 F2FS_FT_SOCK, 729 F2FS_FT_SYMLINK, 730 F2FS_FT_MAX, 731 /* added for fsck */ 732 F2FS_FT_ORPHAN, 733 F2FS_FT_XATTR, 734}; 735 736/* from f2fs/segment.h */ 737enum { 738 LFS = 0, 739 SSR 740}; 741 742extern void ASCIIToUNICODE(u_int16_t *, u_int8_t *); 743extern int log_base_2(u_int32_t); 744extern unsigned int addrs_per_inode(struct f2fs_inode *); 745 746extern int get_bits_in_byte(unsigned char n); 747extern int set_bit(unsigned int nr,void * addr); 748extern int clear_bit(unsigned int nr, void * addr); 749extern int test_bit(unsigned int nr, const void * addr); 750extern int f2fs_test_bit(unsigned int, const char *); 751extern int f2fs_set_bit(unsigned int, char *); 752extern int f2fs_clear_bit(unsigned int, char *); 753extern unsigned long find_next_bit(const unsigned long *, 754 unsigned long, unsigned long); 755 756extern u_int32_t f2fs_cal_crc32(u_int32_t, void *, int); 757extern int f2fs_crc_valid(u_int32_t blk_crc, void *buf, int len); 758 759extern void f2fs_init_configuration(struct f2fs_configuration *); 760extern int f2fs_dev_is_umounted(struct f2fs_configuration *); 761extern int f2fs_get_device_info(struct f2fs_configuration *); 762extern void f2fs_finalize_device(struct f2fs_configuration *); 763 764extern int dev_read(void *, __u64, size_t); 765extern int dev_write(void *, __u64, size_t); 766extern int dev_write_block(void *, __u64); 767extern int dev_write_dump(void *, __u64, size_t); 768/* All bytes in the buffer must be 0 use dev_fill(). */ 769extern int dev_fill(void *, __u64, size_t); 770 771extern int dev_read_block(void *, __u64); 772extern int dev_read_blocks(void *, __u64, __u32 ); 773 774f2fs_hash_t f2fs_dentry_hash(const unsigned char *, int); 775 776extern struct f2fs_configuration config; 777 778#endif /*__F2FS_FS_H */ 779