1 /* SPDX-License-Identifier: GPL-2.0-only OR Apache-2.0 */
3 * EROFS (Enhanced ROM File System) on-disk format definition
5 * Copyright (C) 2017-2018 HUAWEI, Inc.
6 * https://www.huawei.com/
7 * Copyright (C) 2021, Alibaba Cloud
12 #define EROFS_SUPER_OFFSET 1024
14 #define EROFS_FEATURE_COMPAT_SB_CHKSUM 0x00000001
15 #define EROFS_FEATURE_COMPAT_MTIME 0x00000002
18 * Any bits that aren't in EROFS_ALL_FEATURE_INCOMPAT should
19 * be incompatible with this kernel version.
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_FEATURE_INCOMPAT_XATTR_PREFIXES 0x00000040
31 #define EROFS_ALL_FEATURE_INCOMPAT \
32 (EROFS_FEATURE_INCOMPAT_ZERO_PADDING | \
33 EROFS_FEATURE_INCOMPAT_COMPR_CFGS | \
34 EROFS_FEATURE_INCOMPAT_BIG_PCLUSTER | \
35 EROFS_FEATURE_INCOMPAT_CHUNKED_FILE | \
36 EROFS_FEATURE_INCOMPAT_DEVICE_TABLE | \
37 EROFS_FEATURE_INCOMPAT_COMPR_HEAD2 | \
38 EROFS_FEATURE_INCOMPAT_ZTAILPACKING | \
39 EROFS_FEATURE_INCOMPAT_FRAGMENTS | \
40 EROFS_FEATURE_INCOMPAT_DEDUPE | \
41 EROFS_FEATURE_INCOMPAT_XATTR_PREFIXES)
43 #define EROFS_SB_EXTSLOT_SIZE 16
45 struct erofs_deviceslot {
46 u8 tag[64]; /* digest(sha256), etc. */
47 __le32 blocks; /* total fs blocks of this device */
48 __le32 mapped_blkaddr; /* map starting at mapped_blkaddr */
51 #define EROFS_DEVT_SLOT_SIZE sizeof(struct erofs_deviceslot)
53 /* erofs on-disk super block (currently 128 bytes) */
54 struct erofs_super_block {
55 __le32 magic; /* file system magic number */
56 __le32 checksum; /* crc32c(super_block) */
57 __le32 feature_compat;
58 __u8 blkszbits; /* filesystem block size in bit shift */
59 __u8 sb_extslots; /* superblock size = 128 + sb_extslots * 16 */
61 __le16 root_nid; /* nid of root directory */
62 __le64 inos; /* total valid ino # (== f_files - f_favail) */
64 __le64 build_time; /* compact inode time derivation */
65 __le32 build_time_nsec; /* compact inode time derivation in ns scale */
66 __le32 blocks; /* used for statfs */
67 __le32 meta_blkaddr; /* start block address of metadata area */
68 __le32 xattr_blkaddr; /* start block address of shared xattr area */
69 __u8 uuid[16]; /* 128-bit uuid for volume */
70 __u8 volume_name[16]; /* volume name */
71 __le32 feature_incompat;
73 /* bitmap for available compression algorithms */
74 __le16 available_compr_algs;
75 /* customized sliding window size instead of 64k by default */
76 __le16 lz4_max_distance;
78 __le16 extra_devices; /* # of devices besides the primary device */
79 __le16 devt_slotoff; /* startoff = devt_slotoff * devt_slotsize */
80 __u8 dirblkbits; /* directory block size in bit shift */
81 __u8 xattr_prefix_count; /* # of long xattr name prefixes */
82 __le32 xattr_prefix_start; /* start of long xattr prefixes */
83 __le64 packed_nid; /* nid of the special packed inode */
88 * EROFS inode datalayout (i_format in on-disk inode):
89 * 0 - uncompressed flat inode without tail-packing inline data:
90 * 1 - compressed inode with non-compact indexes:
91 * 2 - uncompressed flat inode with tail-packing inline data:
92 * 3 - compressed inode with compact indexes:
93 * 4 - chunk-based inode with (optional) multi-device support:
97 EROFS_INODE_FLAT_PLAIN = 0,
98 EROFS_INODE_COMPRESSED_FULL = 1,
99 EROFS_INODE_FLAT_INLINE = 2,
100 EROFS_INODE_COMPRESSED_COMPACT = 3,
101 EROFS_INODE_CHUNK_BASED = 4,
102 EROFS_INODE_DATALAYOUT_MAX
105 static inline bool erofs_inode_is_data_compressed(unsigned int datamode)
107 return datamode == EROFS_INODE_COMPRESSED_COMPACT ||
108 datamode == EROFS_INODE_COMPRESSED_FULL;
111 /* bit definitions of inode i_format */
112 #define EROFS_I_VERSION_MASK 0x01
113 #define EROFS_I_DATALAYOUT_MASK 0x07
115 #define EROFS_I_VERSION_BIT 0
116 #define EROFS_I_DATALAYOUT_BIT 1
117 #define EROFS_I_ALL_BIT 4
119 #define EROFS_I_ALL ((1 << EROFS_I_ALL_BIT) - 1)
121 /* indicate chunk blkbits, thus 'chunksize = blocksize << chunk blkbits' */
122 #define EROFS_CHUNK_FORMAT_BLKBITS_MASK 0x001F
123 /* with chunk indexes or just a 4-byte blkaddr array */
124 #define EROFS_CHUNK_FORMAT_INDEXES 0x0020
126 #define EROFS_CHUNK_FORMAT_ALL \
127 (EROFS_CHUNK_FORMAT_BLKBITS_MASK | EROFS_CHUNK_FORMAT_INDEXES)
129 /* 32-byte on-disk inode */
130 #define EROFS_INODE_LAYOUT_COMPACT 0
131 /* 64-byte on-disk inode */
132 #define EROFS_INODE_LAYOUT_EXTENDED 1
134 struct erofs_inode_chunk_info {
135 __le16 format; /* chunk blkbits, etc. */
139 union erofs_inode_i_u {
140 /* total compressed blocks for compressed inodes */
141 __le32 compressed_blocks;
143 /* block address for uncompressed flat inodes */
146 /* for device files, used to indicate old/new device # */
149 /* for chunk-based files, it contains the summary info */
150 struct erofs_inode_chunk_info c;
153 /* 32-byte reduced form of an ondisk inode */
154 struct erofs_inode_compact {
155 __le16 i_format; /* inode format hints */
157 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
158 __le16 i_xattr_icount;
163 union erofs_inode_i_u i_u;
165 __le32 i_ino; /* only used for 32-bit stat compatibility */
171 /* 64-byte complete form of an ondisk inode */
172 struct erofs_inode_extended {
173 __le16 i_format; /* inode format hints */
175 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
176 __le16 i_xattr_icount;
180 union erofs_inode_i_u i_u;
182 __le32 i_ino; /* only used for 32-bit stat compatibility */
188 __u8 i_reserved2[16];
192 * inline xattrs (n == i_xattr_icount):
193 * erofs_xattr_ibody_header(1) + (n - 1) * 4 bytes
196 * /-----------------------\
197 * | erofs_xattr_entries+ |
198 * +-----------------------+
199 * inline xattrs must starts in erofs_xattr_ibody_header,
200 * for read-only fs, no need to introduce h_refcount
202 struct erofs_xattr_ibody_header {
206 __le32 h_shared_xattrs[]; /* shared xattr id array */
210 #define EROFS_XATTR_INDEX_USER 1
211 #define EROFS_XATTR_INDEX_POSIX_ACL_ACCESS 2
212 #define EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT 3
213 #define EROFS_XATTR_INDEX_TRUSTED 4
214 #define EROFS_XATTR_INDEX_LUSTRE 5
215 #define EROFS_XATTR_INDEX_SECURITY 6
218 * bit 7 of e_name_index is set when it refers to a long xattr name prefix,
219 * while the remained lower bits represent the index of the prefix.
221 #define EROFS_XATTR_LONG_PREFIX 0x80
222 #define EROFS_XATTR_LONG_PREFIX_MASK 0x7f
224 /* xattr entry (for both inline & shared xattrs) */
225 struct erofs_xattr_entry {
226 __u8 e_name_len; /* length of name */
227 __u8 e_name_index; /* attribute name index */
228 __le16 e_value_size; /* size of attribute value */
229 /* followed by e_name and e_value */
230 char e_name[]; /* attribute name */
233 /* long xattr name prefix */
234 struct erofs_xattr_long_prefix {
235 __u8 base_index; /* short xattr name prefix index */
236 char infix[]; /* infix apart from short prefix */
239 static inline unsigned int erofs_xattr_ibody_size(__le16 i_xattr_icount)
244 return sizeof(struct erofs_xattr_ibody_header) +
245 sizeof(__u32) * (le16_to_cpu(i_xattr_icount) - 1);
248 #define EROFS_XATTR_ALIGN(size) round_up(size, sizeof(struct erofs_xattr_entry))
250 static inline unsigned int erofs_xattr_entry_size(struct erofs_xattr_entry *e)
252 return EROFS_XATTR_ALIGN(sizeof(struct erofs_xattr_entry) +
253 e->e_name_len + le16_to_cpu(e->e_value_size));
256 /* represent a zeroed chunk (hole) */
257 #define EROFS_NULL_ADDR -1
259 /* 4-byte block address array */
260 #define EROFS_BLOCK_MAP_ENTRY_SIZE sizeof(__le32)
262 /* 8-byte inode chunk indexes */
263 struct erofs_inode_chunk_index {
264 __le16 advise; /* always 0, don't care for now */
265 __le16 device_id; /* back-end storage id (with bits masked) */
266 __le32 blkaddr; /* start block address of this inode chunk */
269 /* dirent sorts in alphabet order, thus we can do binary search */
270 struct erofs_dirent {
271 __le64 nid; /* node number */
272 __le16 nameoff; /* start offset of file name */
273 __u8 file_type; /* file type */
274 __u8 reserved; /* reserved */
278 * EROFS file types should match generic FT_* types and
279 * it seems no need to add BUILD_BUG_ONs since potential
280 * unmatchness will break other fses as well...
283 #define EROFS_NAME_LEN 255
285 /* maximum supported size of a physical compression cluster */
286 #define Z_EROFS_PCLUSTER_MAX_SIZE (1024 * 1024)
288 /* available compression algorithm types (for h_algorithmtype) */
290 Z_EROFS_COMPRESSION_LZ4 = 0,
291 Z_EROFS_COMPRESSION_LZMA = 1,
292 Z_EROFS_COMPRESSION_MAX
294 #define Z_EROFS_ALL_COMPR_ALGS ((1 << Z_EROFS_COMPRESSION_MAX) - 1)
296 /* 14 bytes (+ length field = 16 bytes) */
297 struct z_erofs_lz4_cfgs {
299 __le16 max_pclusterblks;
303 /* 14 bytes (+ length field = 16 bytes) */
304 struct z_erofs_lzma_cfgs {
310 #define Z_EROFS_LZMA_MAX_DICT_SIZE (8 * Z_EROFS_PCLUSTER_MAX_SIZE)
313 * bit 0 : COMPACTED_2B indexes (0 - off; 1 - on)
314 * e.g. for 4k logical cluster size, 4B if compacted 2B is off;
315 * (4B) + 2B + (4B) if compacted 2B is on.
316 * bit 1 : HEAD1 big pcluster (0 - off; 1 - on)
317 * bit 2 : HEAD2 big pcluster (0 - off; 1 - on)
318 * bit 3 : tailpacking inline pcluster (0 - off; 1 - on)
319 * bit 4 : interlaced plain pcluster (0 - off; 1 - on)
320 * bit 5 : fragment pcluster (0 - off; 1 - on)
322 #define Z_EROFS_ADVISE_COMPACTED_2B 0x0001
323 #define Z_EROFS_ADVISE_BIG_PCLUSTER_1 0x0002
324 #define Z_EROFS_ADVISE_BIG_PCLUSTER_2 0x0004
325 #define Z_EROFS_ADVISE_INLINE_PCLUSTER 0x0008
326 #define Z_EROFS_ADVISE_INTERLACED_PCLUSTER 0x0010
327 #define Z_EROFS_ADVISE_FRAGMENT_PCLUSTER 0x0020
329 #define Z_EROFS_FRAGMENT_INODE_BIT 7
330 struct z_erofs_map_header {
332 /* fragment data offset in the packed inode */
333 __le32 h_fragmentoff;
336 /* indicates the encoded size of tailpacking data */
342 * bit 0-3 : algorithm type of head 1 (logical cluster type 01);
343 * bit 4-7 : algorithm type of head 2 (logical cluster type 11).
345 __u8 h_algorithmtype;
347 * bit 0-2 : logical cluster bits - 12, e.g. 0 for 4096;
348 * bit 3-6 : reserved;
349 * bit 7 : move the whole file into packed inode or not.
355 * On-disk logical cluster type:
356 * 0 - literal (uncompressed) lcluster
357 * 1,3 - compressed lcluster (for HEAD lclusters)
358 * 2 - compressed lcluster (for NONHEAD lclusters)
361 * 0 - literal (uncompressed) lcluster,
363 * di_clusterofs = the literal data offset of the lcluster
364 * di_blkaddr = the blkaddr of the literal pcluster
366 * 1,3 - compressed lcluster (for HEAD lclusters)
368 * di_clusterofs = the decompressed data offset of the lcluster
369 * di_blkaddr = the blkaddr of the compressed pcluster
371 * 2 - compressed lcluster (for NONHEAD lclusters)
374 * the decompressed data offset in its own HEAD lcluster
375 * di_u.delta[0] = distance to this HEAD lcluster
376 * di_u.delta[1] = distance to the next HEAD lcluster
379 Z_EROFS_LCLUSTER_TYPE_PLAIN = 0,
380 Z_EROFS_LCLUSTER_TYPE_HEAD1 = 1,
381 Z_EROFS_LCLUSTER_TYPE_NONHEAD = 2,
382 Z_EROFS_LCLUSTER_TYPE_HEAD2 = 3,
383 Z_EROFS_LCLUSTER_TYPE_MAX
386 #define Z_EROFS_LI_LCLUSTER_TYPE_BITS 2
387 #define Z_EROFS_LI_LCLUSTER_TYPE_BIT 0
389 /* (noncompact only, HEAD) This pcluster refers to partial decompressed data */
390 #define Z_EROFS_LI_PARTIAL_REF (1 << 15)
393 * D0_CBLKCNT will be marked _only_ at the 1st non-head lcluster to store the
394 * compressed block count of a compressed extent (in logical clusters, aka.
395 * block count of a pcluster).
397 #define Z_EROFS_LI_D0_CBLKCNT (1 << 11)
399 struct z_erofs_lcluster_index {
401 /* where to decompress in the head lcluster */
402 __le16 di_clusterofs;
405 /* for the HEAD lclusters */
408 * for the NONHEAD lclusters
409 * [0] - distance to its HEAD lcluster
410 * [1] - distance to the next HEAD lcluster
416 #define Z_EROFS_FULL_INDEX_ALIGN(end) \
417 (ALIGN(end, 8) + sizeof(struct z_erofs_map_header) + 8)
419 /* check the EROFS on-disk layout strictly at compile time */
420 static inline void erofs_check_ondisk_layout_definitions(void)
422 const __le64 fmh = *(__le64 *)&(struct z_erofs_map_header) {
423 .h_clusterbits = 1 << Z_EROFS_FRAGMENT_INODE_BIT
426 BUILD_BUG_ON(sizeof(struct erofs_super_block) != 128);
427 BUILD_BUG_ON(sizeof(struct erofs_inode_compact) != 32);
428 BUILD_BUG_ON(sizeof(struct erofs_inode_extended) != 64);
429 BUILD_BUG_ON(sizeof(struct erofs_xattr_ibody_header) != 12);
430 BUILD_BUG_ON(sizeof(struct erofs_xattr_entry) != 4);
431 BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_info) != 4);
432 BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_index) != 8);
433 BUILD_BUG_ON(sizeof(struct z_erofs_map_header) != 8);
434 BUILD_BUG_ON(sizeof(struct z_erofs_lcluster_index) != 8);
435 BUILD_BUG_ON(sizeof(struct erofs_dirent) != 12);
436 /* keep in sync between 2 index structures for better extendibility */
437 BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_index) !=
438 sizeof(struct z_erofs_lcluster_index));
439 BUILD_BUG_ON(sizeof(struct erofs_deviceslot) != 128);
441 BUILD_BUG_ON(BIT(Z_EROFS_LI_LCLUSTER_TYPE_BITS) <
442 Z_EROFS_LCLUSTER_TYPE_MAX - 1);
443 /* exclude old compiler versions like gcc 7.5.0 */
444 BUILD_BUG_ON(__builtin_constant_p(fmh) ?
445 fmh != cpu_to_le64(1ULL << 63) : 0);