Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only OR Apache-2.0 */ |
| 2 | /* |
| 3 | * EROFS (Enhanced ROM File System) on-disk format definition |
| 4 | * |
| 5 | * Copyright (C) 2017-2018 HUAWEI, Inc. |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 6 | * https://www.huawei.com/ |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 7 | * Copyright (C) 2021, Alibaba Cloud |
| 8 | */ |
| 9 | #ifndef __EROFS_FS_H |
| 10 | #define __EROFS_FS_H |
| 11 | |
| 12 | #include <asm/unaligned.h> |
| 13 | #include <fs.h> |
| 14 | #include <part.h> |
| 15 | #include <stdint.h> |
| 16 | #include <compiler.h> |
| 17 | |
| 18 | #define EROFS_SUPER_MAGIC_V1 0xE0F5E1E2 |
| 19 | #define EROFS_SUPER_OFFSET 1024 |
| 20 | |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 21 | #define EROFS_FEATURE_COMPAT_SB_CHKSUM 0x00000001 |
| 22 | #define EROFS_FEATURE_COMPAT_MTIME 0x00000002 |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 23 | |
| 24 | /* |
| 25 | * Any bits that aren't in EROFS_ALL_FEATURE_INCOMPAT should |
| 26 | * be incompatible with this kernel version. |
| 27 | */ |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 28 | #define EROFS_FEATURE_INCOMPAT_ZERO_PADDING 0x00000001 |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 29 | #define EROFS_FEATURE_INCOMPAT_COMPR_CFGS 0x00000002 |
| 30 | #define EROFS_FEATURE_INCOMPAT_BIG_PCLUSTER 0x00000002 |
| 31 | #define EROFS_FEATURE_INCOMPAT_CHUNKED_FILE 0x00000004 |
| 32 | #define EROFS_FEATURE_INCOMPAT_DEVICE_TABLE 0x00000008 |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 33 | #define EROFS_FEATURE_INCOMPAT_COMPR_HEAD2 0x00000008 |
| 34 | #define EROFS_FEATURE_INCOMPAT_ZTAILPACKING 0x00000010 |
| 35 | #define EROFS_FEATURE_INCOMPAT_FRAGMENTS 0x00000020 |
| 36 | #define EROFS_FEATURE_INCOMPAT_DEDUPE 0x00000020 |
| 37 | #define EROFS_FEATURE_INCOMPAT_XATTR_PREFIXES 0x00000040 |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 38 | #define EROFS_ALL_FEATURE_INCOMPAT \ |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 39 | (EROFS_FEATURE_INCOMPAT_ZERO_PADDING | \ |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 40 | EROFS_FEATURE_INCOMPAT_COMPR_CFGS | \ |
| 41 | EROFS_FEATURE_INCOMPAT_BIG_PCLUSTER | \ |
| 42 | EROFS_FEATURE_INCOMPAT_CHUNKED_FILE | \ |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 43 | EROFS_FEATURE_INCOMPAT_DEVICE_TABLE | \ |
| 44 | EROFS_FEATURE_INCOMPAT_COMPR_HEAD2 | \ |
| 45 | EROFS_FEATURE_INCOMPAT_ZTAILPACKING | \ |
| 46 | EROFS_FEATURE_INCOMPAT_FRAGMENTS | \ |
| 47 | EROFS_FEATURE_INCOMPAT_DEDUPE | \ |
| 48 | EROFS_FEATURE_INCOMPAT_XATTR_PREFIXES) |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 49 | |
| 50 | #define EROFS_SB_EXTSLOT_SIZE 16 |
| 51 | |
| 52 | struct erofs_deviceslot { |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 53 | u8 tag[64]; /* digest(sha256), etc. */ |
| 54 | __le32 blocks; /* total fs blocks of this device */ |
| 55 | __le32 mapped_blkaddr; /* map starting at mapped_blkaddr */ |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 56 | u8 reserved[56]; |
| 57 | }; |
| 58 | |
| 59 | #define EROFS_DEVT_SLOT_SIZE sizeof(struct erofs_deviceslot) |
| 60 | |
| 61 | /* erofs on-disk super block (currently 128 bytes) */ |
| 62 | struct erofs_super_block { |
| 63 | __le32 magic; /* file system magic number */ |
| 64 | __le32 checksum; /* crc32c(super_block) */ |
| 65 | __le32 feature_compat; |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 66 | __u8 blkszbits; /* filesystem block size in bit shift */ |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 67 | __u8 sb_extslots; /* superblock size = 128 + sb_extslots * 16 */ |
| 68 | |
| 69 | __le16 root_nid; /* nid of root directory */ |
| 70 | __le64 inos; /* total valid ino # (== f_files - f_favail) */ |
| 71 | |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 72 | __le64 build_time; /* compact inode time derivation */ |
| 73 | __le32 build_time_nsec; /* compact inode time derivation in ns scale */ |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 74 | __le32 blocks; /* used for statfs */ |
| 75 | __le32 meta_blkaddr; /* start block address of metadata area */ |
| 76 | __le32 xattr_blkaddr; /* start block address of shared xattr area */ |
| 77 | __u8 uuid[16]; /* 128-bit uuid for volume */ |
| 78 | __u8 volume_name[16]; /* volume name */ |
| 79 | __le32 feature_incompat; |
| 80 | union { |
| 81 | /* bitmap for available compression algorithms */ |
| 82 | __le16 available_compr_algs; |
| 83 | /* customized sliding window size instead of 64k by default */ |
| 84 | __le16 lz4_max_distance; |
| 85 | } __packed u1; |
| 86 | __le16 extra_devices; /* # of devices besides the primary device */ |
| 87 | __le16 devt_slotoff; /* startoff = devt_slotoff * devt_slotsize */ |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 88 | __u8 dirblkbits; /* directory block size in bit shift */ |
| 89 | __u8 xattr_prefix_count; /* # of long xattr name prefixes */ |
| 90 | __le32 xattr_prefix_start; /* start of long xattr prefixes */ |
| 91 | __le64 packed_nid; /* nid of the special packed inode */ |
| 92 | __u8 reserved2[24]; |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | /* |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 96 | * EROFS inode datalayout (i_format in on-disk inode): |
| 97 | * 0 - uncompressed flat inode without tail-packing inline data: |
| 98 | * 1 - compressed inode with non-compact indexes: |
| 99 | * 2 - uncompressed flat inode with tail-packing inline data: |
| 100 | * 3 - compressed inode with compact indexes: |
| 101 | * 4 - chunk-based inode with (optional) multi-device support: |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 102 | * 5~7 - reserved |
| 103 | */ |
| 104 | enum { |
| 105 | EROFS_INODE_FLAT_PLAIN = 0, |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 106 | EROFS_INODE_COMPRESSED_FULL = 1, |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 107 | EROFS_INODE_FLAT_INLINE = 2, |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 108 | EROFS_INODE_COMPRESSED_COMPACT = 3, |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 109 | EROFS_INODE_CHUNK_BASED = 4, |
| 110 | EROFS_INODE_DATALAYOUT_MAX |
| 111 | }; |
| 112 | |
| 113 | static inline bool erofs_inode_is_data_compressed(unsigned int datamode) |
| 114 | { |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 115 | return datamode == EROFS_INODE_COMPRESSED_COMPACT || |
| 116 | datamode == EROFS_INODE_COMPRESSED_FULL; |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 117 | } |
| 118 | |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 119 | /* bit definitions of inode i_format */ |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 120 | #define EROFS_I_VERSION_BITS 1 |
| 121 | #define EROFS_I_DATALAYOUT_BITS 3 |
| 122 | |
| 123 | #define EROFS_I_VERSION_BIT 0 |
| 124 | #define EROFS_I_DATALAYOUT_BIT 1 |
| 125 | |
| 126 | #define EROFS_I_ALL \ |
| 127 | ((1 << (EROFS_I_DATALAYOUT_BIT + EROFS_I_DATALAYOUT_BITS)) - 1) |
| 128 | |
| 129 | /* indicate chunk blkbits, thus 'chunksize = blocksize << chunk blkbits' */ |
| 130 | #define EROFS_CHUNK_FORMAT_BLKBITS_MASK 0x001F |
| 131 | /* with chunk indexes or just a 4-byte blkaddr array */ |
| 132 | #define EROFS_CHUNK_FORMAT_INDEXES 0x0020 |
| 133 | |
| 134 | #define EROFS_CHUNK_FORMAT_ALL \ |
| 135 | (EROFS_CHUNK_FORMAT_BLKBITS_MASK | EROFS_CHUNK_FORMAT_INDEXES) |
| 136 | |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 137 | /* 32-byte on-disk inode */ |
| 138 | #define EROFS_INODE_LAYOUT_COMPACT 0 |
| 139 | /* 64-byte on-disk inode */ |
| 140 | #define EROFS_INODE_LAYOUT_EXTENDED 1 |
| 141 | |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 142 | struct erofs_inode_chunk_info { |
| 143 | __le16 format; /* chunk blkbits, etc. */ |
| 144 | __le16 reserved; |
| 145 | }; |
| 146 | |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 147 | union erofs_inode_i_u { |
| 148 | /* total compressed blocks for compressed inodes */ |
| 149 | __le32 compressed_blocks; |
| 150 | |
| 151 | /* block address for uncompressed flat inodes */ |
| 152 | __le32 raw_blkaddr; |
| 153 | |
| 154 | /* for device files, used to indicate old/new device # */ |
| 155 | __le32 rdev; |
| 156 | |
| 157 | /* for chunk-based files, it contains the summary info */ |
| 158 | struct erofs_inode_chunk_info c; |
| 159 | }; |
| 160 | |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 161 | /* 32-byte reduced form of an ondisk inode */ |
| 162 | struct erofs_inode_compact { |
| 163 | __le16 i_format; /* inode format hints */ |
| 164 | |
| 165 | /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */ |
| 166 | __le16 i_xattr_icount; |
| 167 | __le16 i_mode; |
| 168 | __le16 i_nlink; |
| 169 | __le32 i_size; |
| 170 | __le32 i_reserved; |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 171 | union erofs_inode_i_u i_u; |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 172 | |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 173 | __le32 i_ino; /* only used for 32-bit stat compatibility */ |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 174 | __le16 i_uid; |
| 175 | __le16 i_gid; |
| 176 | __le32 i_reserved2; |
| 177 | }; |
| 178 | |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 179 | /* 64-byte complete form of an ondisk inode */ |
| 180 | struct erofs_inode_extended { |
| 181 | __le16 i_format; /* inode format hints */ |
| 182 | |
| 183 | /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */ |
| 184 | __le16 i_xattr_icount; |
| 185 | __le16 i_mode; |
| 186 | __le16 i_reserved; |
| 187 | __le64 i_size; |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 188 | union erofs_inode_i_u i_u; |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 189 | |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 190 | __le32 i_ino; /* only used for 32-bit stat compatibility */ |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 191 | __le32 i_uid; |
| 192 | __le32 i_gid; |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 193 | __le64 i_mtime; |
| 194 | __le32 i_mtime_nsec; |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 195 | __le32 i_nlink; |
| 196 | __u8 i_reserved2[16]; |
| 197 | }; |
| 198 | |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 199 | /* |
| 200 | * inline xattrs (n == i_xattr_icount): |
| 201 | * erofs_xattr_ibody_header(1) + (n - 1) * 4 bytes |
| 202 | * 12 bytes / \ |
| 203 | * / \ |
| 204 | * /-----------------------\ |
| 205 | * | erofs_xattr_entries+ | |
| 206 | * +-----------------------+ |
| 207 | * inline xattrs must starts in erofs_xattr_ibody_header, |
| 208 | * for read-only fs, no need to introduce h_refcount |
| 209 | */ |
| 210 | struct erofs_xattr_ibody_header { |
| 211 | __le32 h_reserved; |
| 212 | __u8 h_shared_count; |
| 213 | __u8 h_reserved2[7]; |
| 214 | __le32 h_shared_xattrs[0]; /* shared xattr id array */ |
| 215 | }; |
| 216 | |
| 217 | /* Name indexes */ |
| 218 | #define EROFS_XATTR_INDEX_USER 1 |
| 219 | #define EROFS_XATTR_INDEX_POSIX_ACL_ACCESS 2 |
| 220 | #define EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT 3 |
| 221 | #define EROFS_XATTR_INDEX_TRUSTED 4 |
| 222 | #define EROFS_XATTR_INDEX_LUSTRE 5 |
| 223 | #define EROFS_XATTR_INDEX_SECURITY 6 |
| 224 | |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 225 | /* |
| 226 | * bit 7 of e_name_index is set when it refers to a long xattr name prefix, |
| 227 | * while the remained lower bits represent the index of the prefix. |
| 228 | */ |
| 229 | #define EROFS_XATTR_LONG_PREFIX 0x80 |
| 230 | #define EROFS_XATTR_LONG_PREFIX_MASK 0x7f |
| 231 | |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 232 | /* xattr entry (for both inline & shared xattrs) */ |
| 233 | struct erofs_xattr_entry { |
| 234 | __u8 e_name_len; /* length of name */ |
| 235 | __u8 e_name_index; /* attribute name index */ |
| 236 | __le16 e_value_size; /* size of attribute value */ |
| 237 | /* followed by e_name and e_value */ |
| 238 | char e_name[0]; /* attribute name */ |
| 239 | }; |
| 240 | |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 241 | /* long xattr name prefix */ |
| 242 | struct erofs_xattr_long_prefix { |
| 243 | __u8 base_index; /* short xattr name prefix index */ |
| 244 | char infix[0]; /* infix apart from short prefix */ |
| 245 | }; |
| 246 | |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 247 | static inline unsigned int erofs_xattr_ibody_size(__le16 i_xattr_icount) |
| 248 | { |
| 249 | if (!i_xattr_icount) |
| 250 | return 0; |
| 251 | |
| 252 | return sizeof(struct erofs_xattr_ibody_header) + |
| 253 | sizeof(__u32) * (le16_to_cpu(i_xattr_icount) - 1); |
| 254 | } |
| 255 | |
| 256 | #define EROFS_XATTR_ALIGN(size) round_up(size, sizeof(struct erofs_xattr_entry)) |
| 257 | |
| 258 | static inline unsigned int erofs_xattr_entry_size(struct erofs_xattr_entry *e) |
| 259 | { |
| 260 | return EROFS_XATTR_ALIGN(sizeof(struct erofs_xattr_entry) + |
| 261 | e->e_name_len + le16_to_cpu(e->e_value_size)); |
| 262 | } |
| 263 | |
| 264 | /* represent a zeroed chunk (hole) */ |
| 265 | #define EROFS_NULL_ADDR -1 |
| 266 | |
| 267 | /* 4-byte block address array */ |
| 268 | #define EROFS_BLOCK_MAP_ENTRY_SIZE sizeof(__le32) |
| 269 | |
| 270 | /* 8-byte inode chunk indexes */ |
| 271 | struct erofs_inode_chunk_index { |
| 272 | __le16 advise; /* always 0, don't care for now */ |
| 273 | __le16 device_id; /* back-end storage id (with bits masked) */ |
| 274 | __le32 blkaddr; /* start block address of this inode chunk */ |
| 275 | }; |
| 276 | |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 277 | /* dirent sorts in alphabet order, thus we can do binary search */ |
| 278 | struct erofs_dirent { |
| 279 | __le64 nid; /* node number */ |
| 280 | __le16 nameoff; /* start offset of file name */ |
| 281 | __u8 file_type; /* file type */ |
| 282 | __u8 reserved; /* reserved */ |
| 283 | } __packed; |
| 284 | |
| 285 | /* file types used in inode_info->flags */ |
| 286 | enum { |
| 287 | EROFS_FT_UNKNOWN, |
| 288 | EROFS_FT_REG_FILE, |
| 289 | EROFS_FT_DIR, |
| 290 | EROFS_FT_CHRDEV, |
| 291 | EROFS_FT_BLKDEV, |
| 292 | EROFS_FT_FIFO, |
| 293 | EROFS_FT_SOCK, |
| 294 | EROFS_FT_SYMLINK, |
| 295 | EROFS_FT_MAX |
| 296 | }; |
| 297 | |
| 298 | #define EROFS_NAME_LEN 255 |
| 299 | |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 300 | /* maximum supported size of a physical compression cluster */ |
| 301 | #define Z_EROFS_PCLUSTER_MAX_SIZE (1024 * 1024) |
| 302 | |
| 303 | /* available compression algorithm types (for h_algorithmtype) */ |
| 304 | enum { |
| 305 | Z_EROFS_COMPRESSION_LZ4 = 0, |
| 306 | Z_EROFS_COMPRESSION_LZMA = 1, |
Jianan Huang | 4f831f2 | 2024-04-14 23:04:14 +0800 | [diff] [blame] | 307 | Z_EROFS_COMPRESSION_DEFLATE = 2, |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 308 | Z_EROFS_COMPRESSION_MAX |
| 309 | }; |
| 310 | |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 311 | #define Z_EROFS_ALL_COMPR_ALGS ((1 << Z_EROFS_COMPRESSION_MAX) - 1) |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 312 | |
| 313 | /* 14 bytes (+ length field = 16 bytes) */ |
| 314 | struct z_erofs_lz4_cfgs { |
| 315 | __le16 max_distance; |
| 316 | __le16 max_pclusterblks; |
| 317 | u8 reserved[10]; |
| 318 | } __packed; |
| 319 | |
| 320 | /* 14 bytes (+ length field = 16 bytes) */ |
| 321 | struct z_erofs_lzma_cfgs { |
| 322 | __le32 dict_size; |
| 323 | __le16 format; |
| 324 | u8 reserved[8]; |
| 325 | } __packed; |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 326 | |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 327 | #define Z_EROFS_LZMA_MAX_DICT_SIZE (8 * Z_EROFS_PCLUSTER_MAX_SIZE) |
| 328 | |
| 329 | /* |
| 330 | * bit 0 : COMPACTED_2B indexes (0 - off; 1 - on) |
| 331 | * e.g. for 4k logical cluster size, 4B if compacted 2B is off; |
| 332 | * (4B) + 2B + (4B) if compacted 2B is on. |
| 333 | * bit 1 : HEAD1 big pcluster (0 - off; 1 - on) |
| 334 | * bit 2 : HEAD2 big pcluster (0 - off; 1 - on) |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 335 | * bit 3 : tailpacking inline pcluster (0 - off; 1 - on) |
| 336 | * bit 4 : interlaced plain pcluster (0 - off; 1 - on) |
| 337 | * bit 5 : fragment pcluster (0 - off; 1 - on) |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 338 | */ |
| 339 | #define Z_EROFS_ADVISE_COMPACTED_2B 0x0001 |
| 340 | #define Z_EROFS_ADVISE_BIG_PCLUSTER_1 0x0002 |
| 341 | #define Z_EROFS_ADVISE_BIG_PCLUSTER_2 0x0004 |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 342 | #define Z_EROFS_ADVISE_INLINE_PCLUSTER 0x0008 |
| 343 | #define Z_EROFS_ADVISE_INTERLACED_PCLUSTER 0x0010 |
| 344 | #define Z_EROFS_ADVISE_FRAGMENT_PCLUSTER 0x0020 |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 345 | |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 346 | #define Z_EROFS_FRAGMENT_INODE_BIT 7 |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 347 | struct z_erofs_map_header { |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 348 | union { |
| 349 | /* fragment data offset in the packed inode */ |
| 350 | __le32 h_fragmentoff; |
| 351 | struct { |
| 352 | __le16 h_reserved1; |
| 353 | /* indicates the encoded size of tailpacking data */ |
| 354 | __le16 h_idata_size; |
| 355 | }; |
| 356 | }; |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 357 | __le16 h_advise; |
| 358 | /* |
| 359 | * bit 0-3 : algorithm type of head 1 (logical cluster type 01); |
| 360 | * bit 4-7 : algorithm type of head 2 (logical cluster type 11). |
| 361 | */ |
| 362 | __u8 h_algorithmtype; |
| 363 | /* |
| 364 | * bit 0-2 : logical cluster bits - 12, e.g. 0 for 4096; |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 365 | * bit 3-6 : reserved; |
| 366 | * bit 7 : move the whole file into packed inode or not. |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 367 | */ |
| 368 | __u8 h_clusterbits; |
| 369 | }; |
| 370 | |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 371 | /* |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 372 | * On-disk logical cluster type: |
| 373 | * 0 - literal (uncompressed) lcluster |
| 374 | * 1,3 - compressed lcluster (for HEAD lclusters) |
| 375 | * 2 - compressed lcluster (for NONHEAD lclusters) |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 376 | * |
| 377 | * In detail, |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 378 | * 0 - literal (uncompressed) lcluster, |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 379 | * di_advise = 0 |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 380 | * di_clusterofs = the literal data offset of the lcluster |
| 381 | * di_blkaddr = the blkaddr of the literal pcluster |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 382 | * |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 383 | * 1,3 - compressed lcluster (for HEAD lclusters) |
| 384 | * di_advise = 1 or 3 |
| 385 | * di_clusterofs = the decompressed data offset of the lcluster |
| 386 | * di_blkaddr = the blkaddr of the compressed pcluster |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 387 | * |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 388 | * 2 - compressed lcluster (for NONHEAD lclusters) |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 389 | * di_advise = 2 |
| 390 | * di_clusterofs = |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 391 | * the decompressed data offset in its own HEAD lcluster |
| 392 | * di_u.delta[0] = distance to this HEAD lcluster |
| 393 | * di_u.delta[1] = distance to the next HEAD lcluster |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 394 | */ |
| 395 | enum { |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 396 | Z_EROFS_LCLUSTER_TYPE_PLAIN = 0, |
| 397 | Z_EROFS_LCLUSTER_TYPE_HEAD1 = 1, |
| 398 | Z_EROFS_LCLUSTER_TYPE_NONHEAD = 2, |
| 399 | Z_EROFS_LCLUSTER_TYPE_HEAD2 = 3, |
| 400 | Z_EROFS_LCLUSTER_TYPE_MAX |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 401 | }; |
| 402 | |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 403 | #define Z_EROFS_LI_LCLUSTER_TYPE_BITS 2 |
| 404 | #define Z_EROFS_LI_LCLUSTER_TYPE_BIT 0 |
| 405 | |
| 406 | /* (noncompact only, HEAD) This pcluster refers to partial decompressed data */ |
| 407 | #define Z_EROFS_LI_PARTIAL_REF (1 << 15) |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 408 | |
| 409 | /* |
| 410 | * D0_CBLKCNT will be marked _only_ at the 1st non-head lcluster to store the |
| 411 | * compressed block count of a compressed extent (in logical clusters, aka. |
| 412 | * block count of a pcluster). |
| 413 | */ |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 414 | #define Z_EROFS_LI_D0_CBLKCNT (1 << 11) |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 415 | |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 416 | struct z_erofs_lcluster_index { |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 417 | __le16 di_advise; |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 418 | /* where to decompress in the head lcluster */ |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 419 | __le16 di_clusterofs; |
| 420 | |
| 421 | union { |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 422 | /* for the HEAD lclusters */ |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 423 | __le32 blkaddr; |
| 424 | /* |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 425 | * for the NONHEAD lclusters |
| 426 | * [0] - distance to its HEAD lcluster |
| 427 | * [1] - distance to the next HEAD lcluster |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 428 | */ |
| 429 | __le16 delta[2]; |
| 430 | } di_u; |
| 431 | }; |
| 432 | |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 433 | #define Z_EROFS_FULL_INDEX_ALIGN(end) \ |
| 434 | (round_up(end, 8) + sizeof(struct z_erofs_map_header) + 8) |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 435 | |
| 436 | /* check the EROFS on-disk layout strictly at compile time */ |
| 437 | static inline void erofs_check_ondisk_layout_definitions(void) |
| 438 | { |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 439 | const __le64 fmh __maybe_unused = |
| 440 | *(__le64 *)&(struct z_erofs_map_header) { |
| 441 | .h_clusterbits = 1 << Z_EROFS_FRAGMENT_INODE_BIT |
| 442 | }; |
| 443 | |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 444 | BUILD_BUG_ON(sizeof(struct erofs_super_block) != 128); |
| 445 | BUILD_BUG_ON(sizeof(struct erofs_inode_compact) != 32); |
| 446 | BUILD_BUG_ON(sizeof(struct erofs_inode_extended) != 64); |
| 447 | BUILD_BUG_ON(sizeof(struct erofs_xattr_ibody_header) != 12); |
| 448 | BUILD_BUG_ON(sizeof(struct erofs_xattr_entry) != 4); |
| 449 | BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_info) != 4); |
| 450 | BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_index) != 8); |
| 451 | BUILD_BUG_ON(sizeof(struct z_erofs_map_header) != 8); |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 452 | BUILD_BUG_ON(sizeof(struct z_erofs_lcluster_index) != 8); |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 453 | BUILD_BUG_ON(sizeof(struct erofs_dirent) != 12); |
| 454 | /* keep in sync between 2 index structures for better extendibility */ |
| 455 | BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_index) != |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 456 | sizeof(struct z_erofs_lcluster_index)); |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 457 | BUILD_BUG_ON(sizeof(struct erofs_deviceslot) != 128); |
| 458 | |
Yifan Zhao | 39ace5c | 2023-07-07 23:52:12 +0800 | [diff] [blame] | 459 | BUILD_BUG_ON(BIT(Z_EROFS_LI_LCLUSTER_TYPE_BITS) < |
| 460 | Z_EROFS_LCLUSTER_TYPE_MAX - 1); |
| 461 | /* exclude old compiler versions like gcc 7.5.0 */ |
| 462 | BUILD_BUG_ON(__builtin_constant_p(fmh) ? |
| 463 | fmh != cpu_to_le64(1ULL << 63) : 0); |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | #endif |