| // Copyright 2021 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #ifndef F2FS_LAYOUT_H_ |
| #define F2FS_LAYOUT_H_ |
| |
| #include "f2fs_types.h" |
| |
| namespace f2fs { |
| |
| #define F2FS_SUPER_OFFSET 1024 /* byte-size offset */ |
| #define F2FS_LOG_SECTOR_SIZE 9 /* 9 bits for 512 byte */ |
| #define F2FS_LOG_SECTORS_PER_BLOCK 3 /* 4KB: F2FS_BLKSIZE */ |
| #define F2FS_BLKSIZE 4096 /* support only 4KB block */ |
| #define F2FS_MAX_EXTENSION 64 /* # of extension entries */ |
| |
| #define NULL_ADDR 0x0U |
| #define NEW_ADDR (-1U) |
| |
| // sukka |
| #define F2FS_ROOT_INO(sbi) ((sbi)->root_ino_num) |
| #define F2FS_NODE_INO(sbi) ((sbi)->node_ino_num) |
| #define F2FS_META_INO(sbi) ((sbi)->meta_ino_num) |
| #define NULL_ADDR 0x0U // from f2fs_fs.h |
| |
| constexpr uint32_t kF2fsBlockSize = 4096; |
| |
| // Superblock location. |
| constexpr size_t kSuperblockStart = 0; |
| |
| /* for mkfs */ |
| #define F2FS_MIN_VOLUME_SIZE 104857600 |
| |
| #define F2FS_MAJOR_VERSION 1 |
| #define F2FS_MINOR_VERSION 0 |
| |
| #define F2FS_O_DIRECTORY 0x00004000 |
| #define F2FS_O_EONLY 0x00000040 |
| #define F2FS_O_WRONLY 0x00000080 |
| #define F2FS_O_RDONLY 0x00000100 |
| |
| #define F2FS_NUMBER_OF_CHECKPOINT_PACK 2 |
| |
| #define DEFAULT_SECTOR_SIZE 512 |
| #define DEFAULT_SECTORS_PER_BLOCK 8 |
| #define DEFAULT_BLOCKS_PER_SEGMENT 512 |
| #define DEFAULT_SEGMENTS_PER_SECTION 1 |
| #define F2FS_CP_BLOCK_SIZE (DEFAULT_SECTOR_SIZE * DEFAULT_SECTORS_PER_BLOCK) |
| |
| /* |
| * For further optimization on multi-head logs, on-disk layout supports maximum |
| * 16 logs by default. The number, 16, is expected to cover all the cases |
| * enoughly. The implementaion currently uses no more than 6 logs. |
| * Half the logs are used for nodes, and the other half are used for data. |
| */ |
| #define MAX_ACTIVE_LOGS 16 |
| #define MAX_ACTIVE_NODE_LOGS 8 |
| #define MAX_ACTIVE_DATA_LOGS 8 |
| |
| struct f2fs_global_parameters { |
| uint32_t sector_size; |
| uint32_t reserved_segments; |
| uint32_t overprovision; |
| uint32_t cur_seg[6]; |
| uint32_t segs_per_sec; |
| uint32_t secs_per_zone; |
| uint32_t start_sector; |
| uint64_t total_sectors; |
| uint32_t sectors_per_blk; |
| uint32_t blks_per_seg; |
| uint8_t vol_label[16]; |
| int heap; |
| int32_t fd; |
| char *device_name; |
| char *extension_list; |
| } __attribute__((packed)); |
| |
| #define BITS_PER_LONG 64 |
| |
| #define BIT_MASK(nr) (1 << ((nr) % BITS_PER_LONG)) |
| #define BIT_WORD(nr) ((nr) / BITS_PER_LONG) |
| |
| struct f2fs_super_block { |
| uint32_t magic; /* Magic Number */ |
| uint16_t major_ver; /* Major Version */ |
| uint16_t minor_ver; /* Minor Version */ |
| uint32_t log_sectorsize; /* log2 sector size in bytes */ |
| uint32_t log_sectors_per_block; /* log2 # of sectors per block */ |
| uint32_t log_blocksize; /* log2 block size in bytes */ |
| uint32_t log_blocks_per_seg; /* log2 # of blocks per segment */ |
| uint32_t segs_per_sec; /* # of segments per section */ |
| uint32_t secs_per_zone; /* # of sections per zone */ |
| uint32_t checksum_offset; /* checksum offset inside super block */ |
| uint64_t block_count; /* total # of user blocks */ |
| uint32_t section_count; /* total # of sections */ |
| uint32_t segment_count; /* total # of segments */ |
| uint32_t segment_count_ckpt; /* # of segments for checkpoint */ |
| uint32_t segment_count_sit; /* # of segments for SIT */ |
| uint32_t segment_count_nat; /* # of segments for NAT */ |
| uint32_t segment_count_ssa; /* # of segments for SSA */ |
| uint32_t segment_count_main; /* # of segments for main area */ |
| uint32_t segment0_blkaddr; /* start block address of segment 0 */ |
| uint32_t cp_blkaddr; /* start block address of checkpoint */ |
| uint32_t sit_blkaddr; /* start block address of SIT */ |
| uint32_t nat_blkaddr; /* start block address of NAT */ |
| uint32_t ssa_blkaddr; /* start block address of SSA */ |
| uint32_t main_blkaddr; /* start block address of main area */ |
| uint32_t root_ino; /* root inode number */ |
| uint32_t node_ino; /* node inode number */ |
| uint32_t meta_ino; /* meta inode number */ |
| uint8_t uuid[16]; /* 128-bit uuid for volume */ |
| uint16_t volume_name[512]; /* volume name */ |
| uint32_t extension_count; /* # of extensions below */ |
| uint8_t extension_list[F2FS_MAX_EXTENSION][8]; /* extension array */ |
| } __attribute__((packed)); |
| |
| /* |
| * For checkpoint |
| */ |
| #define CP_ERROR_FLAG 0x00000008 |
| #define CP_COMPACT_SUM_FLAG 0x00000004 |
| #define CP_ORPHAN_PRESENT_FLAG 0x00000002 |
| #define CP_UMOUNT_FLAG 0x00000001 |
| |
| struct f2fs_checkpoint { |
| uint64_t checkpoint_ver; /* checkpoint block version number */ |
| uint64_t user_block_count; /* # of user blocks */ |
| uint64_t valid_block_count; /* # of valid blocks in main area */ |
| uint32_t rsvd_segment_count; /* # of reserved segments for gc */ |
| uint32_t overprov_segment_count; /* # of overprovision segments */ |
| uint32_t free_segment_count; /* # of free segments in main area */ |
| |
| /* information of current node segments */ |
| uint32_t cur_node_segno[MAX_ACTIVE_NODE_LOGS]; |
| uint16_t cur_node_blkoff[MAX_ACTIVE_NODE_LOGS]; |
| /* information of current data segments */ |
| uint32_t cur_data_segno[MAX_ACTIVE_DATA_LOGS]; |
| uint16_t cur_data_blkoff[MAX_ACTIVE_DATA_LOGS]; |
| uint32_t ckpt_flags; /* Flags : umount and journal_present */ |
| uint32_t cp_pack_total_block_count; /* total # of one cp pack */ |
| uint32_t cp_pack_start_sum; /* start block number of data summary */ |
| uint32_t valid_node_count; /* Total number of valid nodes */ |
| uint32_t valid_inode_count; /* Total number of valid inodes */ |
| uint32_t next_free_nid; /* Next free node number */ |
| uint32_t sit_ver_bitmap_bytesize; /* Default value 64 */ |
| uint32_t nat_ver_bitmap_bytesize; /* Default value 256 */ |
| uint32_t checksum_offset; /* checksum offset inside cp block */ |
| uint64_t elapsed_time; /* mounted time */ |
| /* allocation type of current segment */ |
| unsigned char alloc_type[MAX_ACTIVE_LOGS]; |
| |
| /* SIT and NAT version bitmap */ |
| unsigned char sit_nat_version_bitmap[1]; |
| } __attribute__((packed)); |
| |
| /* |
| * For orphan inode management |
| */ |
| #define F2FS_ORPHANS_PER_BLOCK 1020 |
| |
| struct f2fs_orphan_block { |
| uint32_t ino[F2FS_ORPHANS_PER_BLOCK]; /* inode numbers */ |
| uint32_t reserved; /* reserved */ |
| uint16_t blk_addr; /* block index in current CP */ |
| uint16_t blk_count; /* Number of orphan inode blocks in CP */ |
| uint32_t entry_count; /* Total number of orphan nodes in current CP */ |
| uint32_t check_sum; /* CRC32 for orphan inode block */ |
| } __attribute__((packed)); |
| |
| /* |
| * For NODE structure |
| */ |
| struct f2fs_extent { |
| uint32_t fofs; /* start file offset of the extent */ |
| uint32_t blk_addr; /* start block address of the extent */ |
| uint32_t len; /* lengh of the extent */ |
| } __attribute__((packed)); |
| |
| #define F2FS_MAX_NAME_LEN 256 |
| #define ADDRS_PER_INODE 923 /* Address Pointers in an Inode */ |
| #define ADDRS_PER_BLOCK 1018 /* Address Pointers in a Direct Block */ |
| #define NIDS_PER_BLOCK 1018 /* Node IDs in an Indirect Block */ |
| |
| struct f2fs_inode { |
| uint16_t i_mode; /* file mode */ |
| uint8_t i_advise; /* file hints */ |
| uint8_t i_reserved; /* reserved */ |
| uint32_t i_uid; /* user ID */ |
| uint32_t i_gid; /* group ID */ |
| uint32_t i_links; /* links count */ |
| uint64_t i_size; /* file size in bytes */ |
| uint64_t i_blocks; /* file size in blocks */ |
| uint64_t i_atime; /* access time */ |
| uint64_t i_ctime; /* change time */ |
| uint64_t i_mtime; /* modification time */ |
| uint32_t i_atime_nsec; /* access time in nano scale */ |
| uint32_t i_ctime_nsec; /* change time in nano scale */ |
| uint32_t i_mtime_nsec; /* modification time in nano scale */ |
| uint32_t i_generation; /* file version (for NFS) */ |
| uint32_t i_current_depth; /* only for directory depth */ |
| uint32_t i_xattr_nid; /* nid to save xattr */ |
| uint32_t i_flags; /* file attributes */ |
| uint32_t i_pino; /* parent inode number */ |
| uint32_t i_namelen; /* file name length */ |
| uint8_t i_name[F2FS_MAX_NAME_LEN]; /* file name for SPOR */ |
| |
| struct f2fs_extent i_ext; /* caching a largest extent */ |
| |
| uint32_t i_addr[ADDRS_PER_INODE]; /* Pointers to data blocks */ |
| |
| uint32_t i_nid[5]; /* direct(2), indirect(2), |
| double_indirect(1) node id */ |
| } __attribute__((packed)); |
| |
| struct direct_node { |
| uint32_t addr[ADDRS_PER_BLOCK]; /* aray of data block address */ |
| } __attribute__((packed)); |
| |
| struct indirect_node { |
| uint32_t nid[NIDS_PER_BLOCK]; /* aray of data block address */ |
| } __attribute__((packed)); |
| |
| enum { COLD_BIT_SHIFT = 0, FSYNC_BIT_SHIFT, DENT_BIT_SHIFT, OFFSET_BIT_SHIFT }; |
| |
| struct node_footer { |
| uint32_t nid; /* node id */ |
| uint32_t ino; /* inode nunmber */ |
| uint32_t flag; /* include cold/fsync/dentry marks and offset */ |
| uint64_t cp_ver; /* checkpoint version */ |
| uint32_t next_blkaddr; /* next node page block address */ |
| } __attribute__((packed)); |
| |
| struct f2fs_node { |
| /* can be one of three types: inode, direct, and indirect types */ |
| union { |
| struct f2fs_inode i; |
| struct direct_node dn; |
| struct indirect_node in; |
| }; |
| struct node_footer footer; |
| } __attribute__((packed)); |
| |
| /* |
| * For NAT entries |
| */ |
| #define NAT_ENTRY_PER_BLOCK (kPageCacheSize / sizeof(struct f2fs_nat_entry)) |
| |
| struct f2fs_nat_entry { |
| uint8_t version; /* latest version of cached nat entry */ |
| uint32_t ino; /* inode number */ |
| uint32_t block_addr; /* block address */ |
| } __attribute__((packed)); |
| |
| struct f2fs_nat_block { |
| struct f2fs_nat_entry entries[NAT_ENTRY_PER_BLOCK]; |
| } __attribute__((packed)); |
| |
| /* |
| * For SIT entries |
| * |
| * Each segment is 2MB in size by default so that a bitmap for validity of |
| * there-in blocks should occupy 64 bytes, 512 bits. |
| * Not allow to change this. |
| */ |
| #define SIT_VBLOCK_MAP_SIZE 64 |
| #define SIT_ENTRY_PER_BLOCK (kPageCacheSize / sizeof(struct f2fs_sit_entry)) |
| |
| /* |
| * Note that f2fs_sit_entry->vblocks has the following bit-field information. |
| * [15:10] : allocation type such as CURSEG_XXXX_TYPE |
| * [9:0] : valid block count |
| */ |
| #define SIT_VBLOCKS_SHIFT 10 |
| #define SIT_VBLOCKS_MASK ((1 << SIT_VBLOCKS_SHIFT) - 1) |
| #define GET_SIT_VBLOCKS(raw_sit) (LeToCpu((raw_sit)->vblocks) & SIT_VBLOCKS_MASK) |
| #define GET_SIT_TYPE(raw_sit) \ |
| ((LeToCpu((raw_sit)->vblocks) & ~SIT_VBLOCKS_MASK) >> SIT_VBLOCKS_SHIFT) |
| |
| /* |
| * Note that f2fs_sit_entry->vblocks has the following bit-field information. |
| * [15:10] : allocation type such as CURSEG_XXXX_TYPE |
| * [9:0] : valid block count |
| */ |
| #define CURSEG_NULL ((-1 << 10) >> 10) /* use 6bit - 0x3f */ |
| |
| struct f2fs_sit_entry { |
| uint16_t vblocks; /* reference above */ |
| uint8_t valid_map[SIT_VBLOCK_MAP_SIZE]; /* bitmap for valid blocks */ |
| uint64_t mtime; /* segment age for cleaning */ |
| } __attribute__((packed)); |
| |
| struct f2fs_sit_block { |
| struct f2fs_sit_entry entries[SIT_ENTRY_PER_BLOCK]; |
| } __attribute__((packed)); |
| |
| /** |
| * For segment summary |
| * |
| * One summary block contains exactly 512 summary entries, which represents |
| * exactly 2MB segment by default. Not allow to change the basic units. |
| * |
| * NOTE : For initializing fields, you must use set_summary |
| * |
| * - If data page, nid represents dnode's nid |
| * - If node page, nid represents the node page's nid. |
| * |
| * The ofs_in_node is used by only data page. It represents offset |
| * from node's page's beginning to get a data block address. |
| * ex) data_blkaddr = (block_t)(nodepage_start_address + ofs_in_node) |
| */ |
| #define ENTRIES_IN_SUM 512 |
| #define SUMMARY_SIZE (sizeof(struct f2fs_summary)) |
| #define SUM_FOOTER_SIZE (sizeof(struct summary_footer)) |
| #define SUM_ENTRY_SIZE (SUMMARY_SIZE * ENTRIES_IN_SUM) |
| |
| /* a summary entry for a 4KB-sized block in a segment */ |
| struct f2fs_summary { |
| uint32_t nid; /* parent node id */ |
| union { |
| uint8_t reserved[3]; |
| struct { |
| uint8_t version; /* node version number */ |
| uint16_t ofs_in_node; /* block index in parent node */ |
| } __attribute__((packed)); |
| }; |
| } __attribute__((packed)); |
| |
| /* summary block type, node or data, is stored to the summary_footer */ |
| #define SUM_TYPE_NODE (1) |
| #define SUM_TYPE_DATA (0) |
| #define GET_SUM_TYPE(footer) (footer->entry_type) |
| #define SET_SUM_TYPE(footer, type) (footer->entry_type = type) |
| |
| struct summary_footer { |
| unsigned char entry_type; /* SUM_TYPE_XXX */ |
| uint32_t check_sum; /* summary checksum */ |
| } __attribute__((packed)); |
| |
| #define SUM_JOURNAL_SIZE (kPageCacheSize - SUM_FOOTER_SIZE - SUM_ENTRY_SIZE) |
| #define NAT_JOURNAL_ENTRIES ((SUM_JOURNAL_SIZE - 2) / sizeof(struct nat_journal_entry)) |
| #define NAT_JOURNAL_RESERVED ((SUM_JOURNAL_SIZE - 2) % sizeof(struct nat_journal_entry)) |
| #define SIT_JOURNAL_ENTRIES ((SUM_JOURNAL_SIZE - 2) / sizeof(struct sit_journal_entry)) |
| #define SIT_JOURNAL_RESERVED ((SUM_JOURNAL_SIZE - 2) % sizeof(struct sit_journal_entry)) |
| /* |
| * frequently updated NAT/SIT entries can be stored in the spare area in |
| * summary blocks |
| */ |
| enum { NAT_JOURNAL = 0, SIT_JOURNAL }; |
| |
| struct nat_journal_entry { |
| uint32_t nid; |
| struct f2fs_nat_entry ne; |
| } __attribute__((packed)); |
| |
| struct nat_journal { |
| struct nat_journal_entry entries[NAT_JOURNAL_ENTRIES]; |
| uint8_t reserved[NAT_JOURNAL_RESERVED]; |
| } __attribute__((packed)); |
| |
| struct sit_journal_entry { |
| uint32_t segno; |
| struct f2fs_sit_entry se; |
| } __attribute__((packed)); |
| |
| struct sit_journal { |
| struct sit_journal_entry entries[SIT_JOURNAL_ENTRIES]; |
| uint8_t reserved[SIT_JOURNAL_RESERVED]; |
| } __attribute__((packed)); |
| |
| /* 4KB-sized summary block structure */ |
| struct f2fs_summary_block { |
| struct f2fs_summary entries[ENTRIES_IN_SUM]; |
| union { |
| uint16_t n_nats; |
| uint16_t n_sits; |
| }; |
| /* spare area is used by NAT or SIT journals */ |
| union { |
| struct nat_journal nat_j; |
| struct sit_journal sit_j; |
| }; |
| struct summary_footer footer; |
| } __attribute__((packed)); |
| |
| /* |
| * For directory operations |
| */ |
| #define F2FS_DOT_HASH 0 |
| #define F2FS_DDOT_HASH F2FS_DOT_HASH |
| #define F2FS_MAX_HASH (~((0x3ULL) << 62)) |
| #define F2FS_HASH_COL_BIT ((0x1ULL) << 63) |
| |
| using f2fs_hash_t = uint32_t; |
| |
| /* One directory entry slot covers 8bytes-long file name */ |
| #define F2FS_NAME_LEN 8 |
| |
| /* the number of dentry in a block */ |
| #define NR_DENTRY_IN_BLOCK 214 |
| |
| /* MAX level for dir lookup */ |
| #define MAX_DIR_HASH_DEPTH 63 |
| |
| #define SIZE_OF_DIR_ENTRY 11 /* by byte */ |
| #define SIZE_OF_DENTRY_BITMAP ((NR_DENTRY_IN_BLOCK + kBitsPerByte - 1) / kBitsPerByte) |
| #define SIZE_OF_RESERVED \ |
| (kPageSize - ((SIZE_OF_DIR_ENTRY + F2FS_NAME_LEN) * NR_DENTRY_IN_BLOCK + SIZE_OF_DENTRY_BITMAP)) |
| |
| /* One directory entry slot representing F2FS_NAME_LEN-sized file name */ |
| struct f2fs_dir_entry { |
| uint32_t hash_code; /* hash code of file name */ |
| uint32_t ino; /* inode number */ |
| uint16_t name_len; /* lengh of file name */ |
| uint8_t file_type; /* file type */ |
| } __attribute__((packed)); |
| |
| /* 4KB-sized directory entry block */ |
| struct f2fs_dentry_block { |
| /* validity bitmap for directory entries in each block */ |
| uint8_t dentry_bitmap[SIZE_OF_DENTRY_BITMAP]; |
| uint8_t reserved[SIZE_OF_RESERVED]; |
| struct f2fs_dir_entry dentry[NR_DENTRY_IN_BLOCK]; |
| uint8_t filename[NR_DENTRY_IN_BLOCK][F2FS_NAME_LEN]; |
| } __attribute__((packed)); |
| |
| /* file types used in inode_info->flags */ |
| enum { |
| F2FS_FT_UNKNOWN, |
| F2FS_FT_REG_FILE, |
| F2FS_FT_DIR, |
| F2FS_FT_CHRDEV, |
| F2FS_FT_BLKDEV, |
| F2FS_FT_FIFO, |
| F2FS_FT_SOCK, |
| F2FS_FT_SYMLINK, |
| F2FS_FT_MAX |
| }; |
| |
| constexpr uint32_t kF2fsHashBits = (8); |
| |
| } // namespace f2fs |
| |
| #endif // F2FS_LAYOUT_H_ |