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