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_ALL_FEATURE_INCOMPAT \
29 (EROFS_FEATURE_INCOMPAT_ZERO_PADDING | \
30 EROFS_FEATURE_INCOMPAT_COMPR_CFGS | \
31 EROFS_FEATURE_INCOMPAT_BIG_PCLUSTER | \
32 EROFS_FEATURE_INCOMPAT_CHUNKED_FILE | \
33 EROFS_FEATURE_INCOMPAT_DEVICE_TABLE | \
34 EROFS_FEATURE_INCOMPAT_COMPR_HEAD2 | \
35 EROFS_FEATURE_INCOMPAT_ZTAILPACKING)
37 #define EROFS_SB_EXTSLOT_SIZE 16
39 struct erofs_deviceslot {
41 u8 uuid[16]; /* used for device manager later */
42 u8 userdata[64]; /* digest(sha256), etc. */
44 __le32 blocks; /* total fs blocks of this device */
45 __le32 mapped_blkaddr; /* map starting at mapped_blkaddr */
48 #define EROFS_DEVT_SLOT_SIZE sizeof(struct erofs_deviceslot)
50 /* erofs on-disk super block (currently 128 bytes) */
51 struct erofs_super_block {
52 __le32 magic; /* file system magic number */
53 __le32 checksum; /* crc32c(super_block) */
54 __le32 feature_compat;
55 __u8 blkszbits; /* support block_size == PAGE_SIZE only */
56 __u8 sb_extslots; /* superblock size = 128 + sb_extslots * 16 */
58 __le16 root_nid; /* nid of root directory */
59 __le64 inos; /* total valid ino # (== f_files - f_favail) */
61 __le64 build_time; /* inode v1 time derivation */
62 __le32 build_time_nsec; /* inode v1 time derivation in nano scale */
63 __le32 blocks; /* used for statfs */
64 __le32 meta_blkaddr; /* start block address of metadata area */
65 __le32 xattr_blkaddr; /* start block address of shared xattr area */
66 __u8 uuid[16]; /* 128-bit uuid for volume */
67 __u8 volume_name[16]; /* volume name */
68 __le32 feature_incompat;
70 /* bitmap for available compression algorithms */
71 __le16 available_compr_algs;
72 /* customized sliding window size instead of 64k by default */
73 __le16 lz4_max_distance;
75 __le16 extra_devices; /* # of devices besides the primary device */
76 __le16 devt_slotoff; /* startoff = devt_slotoff * devt_slotsize */
81 * erofs inode datalayout (i_format in on-disk inode):
82 * 0 - inode plain without inline data A:
83 * inode, [xattrs], ... | ... | no-holed data
84 * 1 - inode VLE compression B (legacy):
85 * inode, [xattrs], extents ... | ...
86 * 2 - inode plain with inline data C:
87 * inode, [xattrs], last_inline_data, ... | ... | no-holed data
88 * 3 - inode compression D:
89 * inode, [xattrs], map_header, extents ... | ...
90 * 4 - inode chunk-based E:
91 * inode, [xattrs], chunk indexes ... | ...
95 EROFS_INODE_FLAT_PLAIN = 0,
96 EROFS_INODE_FLAT_COMPRESSION_LEGACY = 1,
97 EROFS_INODE_FLAT_INLINE = 2,
98 EROFS_INODE_FLAT_COMPRESSION = 3,
99 EROFS_INODE_CHUNK_BASED = 4,
100 EROFS_INODE_DATALAYOUT_MAX
103 static inline bool erofs_inode_is_data_compressed(unsigned int datamode)
105 return datamode == EROFS_INODE_FLAT_COMPRESSION ||
106 datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY;
109 /* bit definitions of inode i_advise */
110 #define EROFS_I_VERSION_BITS 1
111 #define EROFS_I_DATALAYOUT_BITS 3
113 #define EROFS_I_VERSION_BIT 0
114 #define EROFS_I_DATALAYOUT_BIT 1
116 #define EROFS_I_ALL \
117 ((1 << (EROFS_I_DATALAYOUT_BIT + EROFS_I_DATALAYOUT_BITS)) - 1)
119 /* indicate chunk blkbits, thus 'chunksize = blocksize << chunk blkbits' */
120 #define EROFS_CHUNK_FORMAT_BLKBITS_MASK 0x001F
121 /* with chunk indexes or just a 4-byte blkaddr array */
122 #define EROFS_CHUNK_FORMAT_INDEXES 0x0020
124 #define EROFS_CHUNK_FORMAT_ALL \
125 (EROFS_CHUNK_FORMAT_BLKBITS_MASK | EROFS_CHUNK_FORMAT_INDEXES)
127 struct erofs_inode_chunk_info {
128 __le16 format; /* chunk blkbits, etc. */
132 /* 32-byte reduced form of an ondisk inode */
133 struct erofs_inode_compact {
134 __le16 i_format; /* inode format hints */
136 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
137 __le16 i_xattr_icount;
143 /* file total compressed blocks for data mapping 1 */
144 __le32 compressed_blocks;
147 /* for device files, used to indicate old/new device # */
150 /* for chunk-based files, it contains the summary info */
151 struct erofs_inode_chunk_info c;
153 __le32 i_ino; /* only used for 32-bit stat compatibility */
159 /* 32 bytes on-disk inode */
160 #define EROFS_INODE_LAYOUT_COMPACT 0
161 /* 64 bytes on-disk inode */
162 #define EROFS_INODE_LAYOUT_EXTENDED 1
164 /* 64-byte complete form of an ondisk inode */
165 struct erofs_inode_extended {
166 __le16 i_format; /* inode format hints */
168 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
169 __le16 i_xattr_icount;
174 /* file total compressed blocks for data mapping 1 */
175 __le32 compressed_blocks;
178 /* for device files, used to indicate old/new device # */
181 /* for chunk-based files, it contains the summary info */
182 struct erofs_inode_chunk_info c;
185 /* only used for 32-bit stat compatibility */
193 __u8 i_reserved2[16];
196 #define EROFS_MAX_SHARED_XATTRS (128)
197 /* h_shared_count between 129 ... 255 are special # */
198 #define EROFS_SHARED_XATTR_EXTENT (255)
201 * inline xattrs (n == i_xattr_icount):
202 * erofs_xattr_ibody_header(1) + (n - 1) * 4 bytes
205 * /-----------------------\
206 * | erofs_xattr_entries+ |
207 * +-----------------------+
208 * inline xattrs must starts in erofs_xattr_ibody_header,
209 * for read-only fs, no need to introduce h_refcount
211 struct erofs_xattr_ibody_header {
215 __le32 h_shared_xattrs[]; /* shared xattr id array */
219 #define EROFS_XATTR_INDEX_USER 1
220 #define EROFS_XATTR_INDEX_POSIX_ACL_ACCESS 2
221 #define EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT 3
222 #define EROFS_XATTR_INDEX_TRUSTED 4
223 #define EROFS_XATTR_INDEX_LUSTRE 5
224 #define EROFS_XATTR_INDEX_SECURITY 6
226 /* xattr entry (for both inline & shared xattrs) */
227 struct erofs_xattr_entry {
228 __u8 e_name_len; /* length of name */
229 __u8 e_name_index; /* attribute name index */
230 __le16 e_value_size; /* size of attribute value */
231 /* followed by e_name and e_value */
232 char e_name[]; /* attribute name */
235 static inline unsigned int erofs_xattr_ibody_size(__le16 i_xattr_icount)
240 return sizeof(struct erofs_xattr_ibody_header) +
241 sizeof(__u32) * (le16_to_cpu(i_xattr_icount) - 1);
244 #define EROFS_XATTR_ALIGN(size) round_up(size, sizeof(struct erofs_xattr_entry))
246 static inline unsigned int erofs_xattr_entry_size(struct erofs_xattr_entry *e)
248 return EROFS_XATTR_ALIGN(sizeof(struct erofs_xattr_entry) +
249 e->e_name_len + le16_to_cpu(e->e_value_size));
252 /* represent a zeroed chunk (hole) */
253 #define EROFS_NULL_ADDR -1
255 /* 4-byte block address array */
256 #define EROFS_BLOCK_MAP_ENTRY_SIZE sizeof(__le32)
258 /* 8-byte inode chunk indexes */
259 struct erofs_inode_chunk_index {
260 __le16 advise; /* always 0, don't care for now */
261 __le16 device_id; /* back-end storage id (with bits masked) */
262 __le32 blkaddr; /* start block address of this inode chunk */
265 /* maximum supported size of a physical compression cluster */
266 #define Z_EROFS_PCLUSTER_MAX_SIZE (1024 * 1024)
268 /* available compression algorithm types (for h_algorithmtype) */
270 Z_EROFS_COMPRESSION_LZ4 = 0,
271 Z_EROFS_COMPRESSION_LZMA = 1,
272 Z_EROFS_COMPRESSION_MAX
274 #define Z_EROFS_ALL_COMPR_ALGS ((1 << Z_EROFS_COMPRESSION_MAX) - 1)
276 /* 14 bytes (+ length field = 16 bytes) */
277 struct z_erofs_lz4_cfgs {
279 __le16 max_pclusterblks;
283 /* 14 bytes (+ length field = 16 bytes) */
284 struct z_erofs_lzma_cfgs {
290 #define Z_EROFS_LZMA_MAX_DICT_SIZE (8 * Z_EROFS_PCLUSTER_MAX_SIZE)
293 * bit 0 : COMPACTED_2B indexes (0 - off; 1 - on)
294 * e.g. for 4k logical cluster size, 4B if compacted 2B is off;
295 * (4B) + 2B + (4B) if compacted 2B is on.
296 * bit 1 : HEAD1 big pcluster (0 - off; 1 - on)
297 * bit 2 : HEAD2 big pcluster (0 - off; 1 - on)
298 * bit 3 : tailpacking inline pcluster (0 - off; 1 - on)
300 #define Z_EROFS_ADVISE_COMPACTED_2B 0x0001
301 #define Z_EROFS_ADVISE_BIG_PCLUSTER_1 0x0002
302 #define Z_EROFS_ADVISE_BIG_PCLUSTER_2 0x0004
303 #define Z_EROFS_ADVISE_INLINE_PCLUSTER 0x0008
305 struct z_erofs_map_header {
307 /* indicates the encoded size of tailpacking data */
311 * bit 0-3 : algorithm type of head 1 (logical cluster type 01);
312 * bit 4-7 : algorithm type of head 2 (logical cluster type 11).
314 __u8 h_algorithmtype;
316 * bit 0-2 : logical cluster bits - 12, e.g. 0 for 4096;
317 * bit 3-7 : reserved.
322 #define Z_EROFS_VLE_LEGACY_HEADER_PADDING 8
325 * Fixed-sized output compression on-disk logical cluster type:
326 * 0 - literal (uncompressed) lcluster
327 * 1,3 - compressed lcluster (for HEAD lclusters)
328 * 2 - compressed lcluster (for NONHEAD lclusters)
331 * 0 - literal (uncompressed) lcluster,
333 * di_clusterofs = the literal data offset of the lcluster
334 * di_blkaddr = the blkaddr of the literal pcluster
336 * 1,3 - compressed lcluster (for HEAD lclusters)
338 * di_clusterofs = the decompressed data offset of the lcluster
339 * di_blkaddr = the blkaddr of the compressed pcluster
341 * 2 - compressed lcluster (for NONHEAD lclusters)
344 * the decompressed data offset in its own HEAD lcluster
345 * di_u.delta[0] = distance to this HEAD lcluster
346 * di_u.delta[1] = distance to the next HEAD lcluster
349 Z_EROFS_VLE_CLUSTER_TYPE_PLAIN = 0,
350 Z_EROFS_VLE_CLUSTER_TYPE_HEAD1 = 1,
351 Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD = 2,
352 Z_EROFS_VLE_CLUSTER_TYPE_HEAD2 = 3,
353 Z_EROFS_VLE_CLUSTER_TYPE_MAX
356 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS 2
357 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT 0
360 * D0_CBLKCNT will be marked _only_ at the 1st non-head lcluster to store the
361 * compressed block count of a compressed extent (in logical clusters, aka.
362 * block count of a pcluster).
364 #define Z_EROFS_VLE_DI_D0_CBLKCNT (1 << 11)
366 struct z_erofs_vle_decompressed_index {
368 /* where to decompress in the head cluster */
369 __le16 di_clusterofs;
372 /* for the head cluster */
375 * for the rest clusters
376 * eg. for 4k page-sized cluster, maximum 4K*64k = 256M)
377 * [0] - pointing to the head cluster
378 * [1] - pointing to the tail cluster
384 #define Z_EROFS_VLE_LEGACY_INDEX_ALIGN(size) \
385 (round_up(size, sizeof(struct z_erofs_vle_decompressed_index)) + \
386 sizeof(struct z_erofs_map_header) + Z_EROFS_VLE_LEGACY_HEADER_PADDING)
388 /* dirent sorts in alphabet order, thus we can do binary search */
389 struct erofs_dirent {
390 __le64 nid; /* node number */
391 __le16 nameoff; /* start offset of file name */
392 __u8 file_type; /* file type */
393 __u8 reserved; /* reserved */
397 * EROFS file types should match generic FT_* types and
398 * it seems no need to add BUILD_BUG_ONs since potential
399 * unmatchness will break other fses as well...
402 #define EROFS_NAME_LEN 255
404 /* check the EROFS on-disk layout strictly at compile time */
405 static inline void erofs_check_ondisk_layout_definitions(void)
407 BUILD_BUG_ON(sizeof(struct erofs_super_block) != 128);
408 BUILD_BUG_ON(sizeof(struct erofs_inode_compact) != 32);
409 BUILD_BUG_ON(sizeof(struct erofs_inode_extended) != 64);
410 BUILD_BUG_ON(sizeof(struct erofs_xattr_ibody_header) != 12);
411 BUILD_BUG_ON(sizeof(struct erofs_xattr_entry) != 4);
412 BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_info) != 4);
413 BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_index) != 8);
414 BUILD_BUG_ON(sizeof(struct z_erofs_map_header) != 8);
415 BUILD_BUG_ON(sizeof(struct z_erofs_vle_decompressed_index) != 8);
416 BUILD_BUG_ON(sizeof(struct erofs_dirent) != 12);
417 /* keep in sync between 2 index structures for better extendibility */
418 BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_index) !=
419 sizeof(struct z_erofs_vle_decompressed_index));
420 BUILD_BUG_ON(sizeof(struct erofs_deviceslot) != 128);
422 BUILD_BUG_ON(BIT(Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) <
423 Z_EROFS_VLE_CLUSTER_TYPE_MAX - 1);