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 {
40 u8 tag[64]; /* digest(sha256), etc. */
41 __le32 blocks; /* total fs blocks of this device */
42 __le32 mapped_blkaddr; /* map starting at mapped_blkaddr */
45 #define EROFS_DEVT_SLOT_SIZE sizeof(struct erofs_deviceslot)
47 /* erofs on-disk super block (currently 128 bytes) */
48 struct erofs_super_block {
49 __le32 magic; /* file system magic number */
50 __le32 checksum; /* crc32c(super_block) */
51 __le32 feature_compat;
52 __u8 blkszbits; /* support block_size == PAGE_SIZE only */
53 __u8 sb_extslots; /* superblock size = 128 + sb_extslots * 16 */
55 __le16 root_nid; /* nid of root directory */
56 __le64 inos; /* total valid ino # (== f_files - f_favail) */
58 __le64 build_time; /* compact inode time derivation */
59 __le32 build_time_nsec; /* compact inode time derivation in ns scale */
60 __le32 blocks; /* used for statfs */
61 __le32 meta_blkaddr; /* start block address of metadata area */
62 __le32 xattr_blkaddr; /* start block address of shared xattr area */
63 __u8 uuid[16]; /* 128-bit uuid for volume */
64 __u8 volume_name[16]; /* volume name */
65 __le32 feature_incompat;
67 /* bitmap for available compression algorithms */
68 __le16 available_compr_algs;
69 /* customized sliding window size instead of 64k by default */
70 __le16 lz4_max_distance;
72 __le16 extra_devices; /* # of devices besides the primary device */
73 __le16 devt_slotoff; /* startoff = devt_slotoff * devt_slotsize */
78 * erofs inode datalayout (i_format in on-disk inode):
79 * 0 - uncompressed flat inode without tail-packing inline data:
80 * inode, [xattrs], ... | ... | no-holed data
81 * 1 - compressed inode with non-compact indexes:
82 * inode, [xattrs], [map_header], extents ... | ...
83 * 2 - uncompressed flat inode with tail-packing inline data:
84 * inode, [xattrs], tailpacking data, ... | ... | no-holed data
85 * 3 - compressed inode with compact indexes:
86 * inode, [xattrs], map_header, extents ... | ...
87 * 4 - chunk-based inode with (optional) multi-device support:
88 * inode, [xattrs], chunk indexes ... | ...
92 EROFS_INODE_FLAT_PLAIN = 0,
93 EROFS_INODE_FLAT_COMPRESSION_LEGACY = 1,
94 EROFS_INODE_FLAT_INLINE = 2,
95 EROFS_INODE_FLAT_COMPRESSION = 3,
96 EROFS_INODE_CHUNK_BASED = 4,
97 EROFS_INODE_DATALAYOUT_MAX
100 static inline bool erofs_inode_is_data_compressed(unsigned int datamode)
102 return datamode == EROFS_INODE_FLAT_COMPRESSION ||
103 datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY;
106 /* bit definitions of inode i_format */
107 #define EROFS_I_VERSION_BITS 1
108 #define EROFS_I_DATALAYOUT_BITS 3
110 #define EROFS_I_VERSION_BIT 0
111 #define EROFS_I_DATALAYOUT_BIT 1
113 #define EROFS_I_ALL \
114 ((1 << (EROFS_I_DATALAYOUT_BIT + EROFS_I_DATALAYOUT_BITS)) - 1)
116 /* indicate chunk blkbits, thus 'chunksize = blocksize << chunk blkbits' */
117 #define EROFS_CHUNK_FORMAT_BLKBITS_MASK 0x001F
118 /* with chunk indexes or just a 4-byte blkaddr array */
119 #define EROFS_CHUNK_FORMAT_INDEXES 0x0020
121 #define EROFS_CHUNK_FORMAT_ALL \
122 (EROFS_CHUNK_FORMAT_BLKBITS_MASK | EROFS_CHUNK_FORMAT_INDEXES)
124 struct erofs_inode_chunk_info {
125 __le16 format; /* chunk blkbits, etc. */
129 /* 32-byte reduced form of an ondisk inode */
130 struct erofs_inode_compact {
131 __le16 i_format; /* inode format hints */
133 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
134 __le16 i_xattr_icount;
140 /* total compressed blocks for compressed inodes */
141 __le32 compressed_blocks;
142 /* block address for uncompressed flat inodes */
145 /* for device files, used to indicate old/new device # */
148 /* for chunk-based files, it contains the summary info */
149 struct erofs_inode_chunk_info c;
151 __le32 i_ino; /* only used for 32-bit stat compatibility */
157 /* 32-byte on-disk inode */
158 #define EROFS_INODE_LAYOUT_COMPACT 0
159 /* 64-byte on-disk inode */
160 #define EROFS_INODE_LAYOUT_EXTENDED 1
162 /* 64-byte complete form of an ondisk inode */
163 struct erofs_inode_extended {
164 __le16 i_format; /* inode format hints */
166 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
167 __le16 i_xattr_icount;
172 /* total compressed blocks for compressed inodes */
173 __le32 compressed_blocks;
174 /* block address for uncompressed flat inodes */
177 /* for device files, used to indicate old/new device # */
180 /* for chunk-based files, it contains the summary info */
181 struct erofs_inode_chunk_info c;
184 /* only used for 32-bit stat compatibility */
192 __u8 i_reserved2[16];
195 #define EROFS_MAX_SHARED_XATTRS (128)
196 /* h_shared_count between 129 ... 255 are special # */
197 #define EROFS_SHARED_XATTR_EXTENT (255)
200 * inline xattrs (n == i_xattr_icount):
201 * erofs_xattr_ibody_header(1) + (n - 1) * 4 bytes
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
210 struct erofs_xattr_ibody_header {
214 __le32 h_shared_xattrs[]; /* shared xattr id array */
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
225 /* xattr entry (for both inline & shared xattrs) */
226 struct erofs_xattr_entry {
227 __u8 e_name_len; /* length of name */
228 __u8 e_name_index; /* attribute name index */
229 __le16 e_value_size; /* size of attribute value */
230 /* followed by e_name and e_value */
231 char e_name[]; /* attribute name */
234 static inline unsigned int erofs_xattr_ibody_size(__le16 i_xattr_icount)
239 return sizeof(struct erofs_xattr_ibody_header) +
240 sizeof(__u32) * (le16_to_cpu(i_xattr_icount) - 1);
243 #define EROFS_XATTR_ALIGN(size) round_up(size, sizeof(struct erofs_xattr_entry))
245 static inline unsigned int erofs_xattr_entry_size(struct erofs_xattr_entry *e)
247 return EROFS_XATTR_ALIGN(sizeof(struct erofs_xattr_entry) +
248 e->e_name_len + le16_to_cpu(e->e_value_size));
251 /* represent a zeroed chunk (hole) */
252 #define EROFS_NULL_ADDR -1
254 /* 4-byte block address array */
255 #define EROFS_BLOCK_MAP_ENTRY_SIZE sizeof(__le32)
257 /* 8-byte inode chunk indexes */
258 struct erofs_inode_chunk_index {
259 __le16 advise; /* always 0, don't care for now */
260 __le16 device_id; /* back-end storage id (with bits masked) */
261 __le32 blkaddr; /* start block address of this inode chunk */
264 /* maximum supported size of a physical compression cluster */
265 #define Z_EROFS_PCLUSTER_MAX_SIZE (1024 * 1024)
267 /* available compression algorithm types (for h_algorithmtype) */
269 Z_EROFS_COMPRESSION_LZ4 = 0,
270 Z_EROFS_COMPRESSION_LZMA = 1,
271 Z_EROFS_COMPRESSION_MAX
273 #define Z_EROFS_ALL_COMPR_ALGS ((1 << Z_EROFS_COMPRESSION_MAX) - 1)
275 /* 14 bytes (+ length field = 16 bytes) */
276 struct z_erofs_lz4_cfgs {
278 __le16 max_pclusterblks;
282 /* 14 bytes (+ length field = 16 bytes) */
283 struct z_erofs_lzma_cfgs {
289 #define Z_EROFS_LZMA_MAX_DICT_SIZE (8 * Z_EROFS_PCLUSTER_MAX_SIZE)
292 * bit 0 : COMPACTED_2B indexes (0 - off; 1 - on)
293 * e.g. for 4k logical cluster size, 4B if compacted 2B is off;
294 * (4B) + 2B + (4B) if compacted 2B is on.
295 * bit 1 : HEAD1 big pcluster (0 - off; 1 - on)
296 * bit 2 : HEAD2 big pcluster (0 - off; 1 - on)
297 * bit 3 : tailpacking inline pcluster (0 - off; 1 - on)
299 #define Z_EROFS_ADVISE_COMPACTED_2B 0x0001
300 #define Z_EROFS_ADVISE_BIG_PCLUSTER_1 0x0002
301 #define Z_EROFS_ADVISE_BIG_PCLUSTER_2 0x0004
302 #define Z_EROFS_ADVISE_INLINE_PCLUSTER 0x0008
304 struct z_erofs_map_header {
306 /* indicates the encoded size of tailpacking data */
310 * bit 0-3 : algorithm type of head 1 (logical cluster type 01);
311 * bit 4-7 : algorithm type of head 2 (logical cluster type 11).
313 __u8 h_algorithmtype;
315 * bit 0-2 : logical cluster bits - 12, e.g. 0 for 4096;
316 * bit 3-7 : reserved.
321 #define Z_EROFS_VLE_LEGACY_HEADER_PADDING 8
324 * Fixed-sized output compression on-disk logical cluster type:
325 * 0 - literal (uncompressed) lcluster
326 * 1,3 - compressed lcluster (for HEAD lclusters)
327 * 2 - compressed lcluster (for NONHEAD lclusters)
330 * 0 - literal (uncompressed) lcluster,
332 * di_clusterofs = the literal data offset of the lcluster
333 * di_blkaddr = the blkaddr of the literal pcluster
335 * 1,3 - compressed lcluster (for HEAD lclusters)
337 * di_clusterofs = the decompressed data offset of the lcluster
338 * di_blkaddr = the blkaddr of the compressed pcluster
340 * 2 - compressed lcluster (for NONHEAD lclusters)
343 * the decompressed data offset in its own HEAD lcluster
344 * di_u.delta[0] = distance to this HEAD lcluster
345 * di_u.delta[1] = distance to the next HEAD lcluster
348 Z_EROFS_VLE_CLUSTER_TYPE_PLAIN = 0,
349 Z_EROFS_VLE_CLUSTER_TYPE_HEAD1 = 1,
350 Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD = 2,
351 Z_EROFS_VLE_CLUSTER_TYPE_HEAD2 = 3,
352 Z_EROFS_VLE_CLUSTER_TYPE_MAX
355 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS 2
356 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT 0
359 * D0_CBLKCNT will be marked _only_ at the 1st non-head lcluster to store the
360 * compressed block count of a compressed extent (in logical clusters, aka.
361 * block count of a pcluster).
363 #define Z_EROFS_VLE_DI_D0_CBLKCNT (1 << 11)
365 struct z_erofs_vle_decompressed_index {
367 /* where to decompress in the head lcluster */
368 __le16 di_clusterofs;
371 /* for the HEAD lclusters */
374 * for the NONHEAD lclusters
375 * [0] - distance to its HEAD lcluster
376 * [1] - distance to the next HEAD lcluster
382 #define Z_EROFS_VLE_LEGACY_INDEX_ALIGN(size) \
383 (round_up(size, sizeof(struct z_erofs_vle_decompressed_index)) + \
384 sizeof(struct z_erofs_map_header) + Z_EROFS_VLE_LEGACY_HEADER_PADDING)
386 /* dirent sorts in alphabet order, thus we can do binary search */
387 struct erofs_dirent {
388 __le64 nid; /* node number */
389 __le16 nameoff; /* start offset of file name */
390 __u8 file_type; /* file type */
391 __u8 reserved; /* reserved */
395 * EROFS file types should match generic FT_* types and
396 * it seems no need to add BUILD_BUG_ONs since potential
397 * unmatchness will break other fses as well...
400 #define EROFS_NAME_LEN 255
402 /* check the EROFS on-disk layout strictly at compile time */
403 static inline void erofs_check_ondisk_layout_definitions(void)
405 BUILD_BUG_ON(sizeof(struct erofs_super_block) != 128);
406 BUILD_BUG_ON(sizeof(struct erofs_inode_compact) != 32);
407 BUILD_BUG_ON(sizeof(struct erofs_inode_extended) != 64);
408 BUILD_BUG_ON(sizeof(struct erofs_xattr_ibody_header) != 12);
409 BUILD_BUG_ON(sizeof(struct erofs_xattr_entry) != 4);
410 BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_info) != 4);
411 BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_index) != 8);
412 BUILD_BUG_ON(sizeof(struct z_erofs_map_header) != 8);
413 BUILD_BUG_ON(sizeof(struct z_erofs_vle_decompressed_index) != 8);
414 BUILD_BUG_ON(sizeof(struct erofs_dirent) != 12);
415 /* keep in sync between 2 index structures for better extendibility */
416 BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_index) !=
417 sizeof(struct z_erofs_vle_decompressed_index));
418 BUILD_BUG_ON(sizeof(struct erofs_deviceslot) != 128);
420 BUILD_BUG_ON(BIT(Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) <
421 Z_EROFS_VLE_CLUSTER_TYPE_MAX - 1);