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/
11 #define EROFS_SUPER_OFFSET 1024
13 #define EROFS_FEATURE_COMPAT_SB_CHKSUM 0x00000001
16 * Any bits that aren't in EROFS_ALL_FEATURE_INCOMPAT should
17 * be incompatible with this kernel version.
19 #define EROFS_FEATURE_INCOMPAT_LZ4_0PADDING 0x00000001
20 #define EROFS_FEATURE_INCOMPAT_COMPR_CFGS 0x00000002
21 #define EROFS_FEATURE_INCOMPAT_BIG_PCLUSTER 0x00000002
22 #define EROFS_ALL_FEATURE_INCOMPAT \
23 (EROFS_FEATURE_INCOMPAT_LZ4_0PADDING | \
24 EROFS_FEATURE_INCOMPAT_COMPR_CFGS | \
25 EROFS_FEATURE_INCOMPAT_BIG_PCLUSTER)
27 #define EROFS_SB_EXTSLOT_SIZE 16
29 /* erofs on-disk super block (currently 128 bytes) */
30 struct erofs_super_block {
31 __le32 magic; /* file system magic number */
32 __le32 checksum; /* crc32c(super_block) */
33 __le32 feature_compat;
34 __u8 blkszbits; /* support block_size == PAGE_SIZE only */
35 __u8 sb_extslots; /* superblock size = 128 + sb_extslots * 16 */
37 __le16 root_nid; /* nid of root directory */
38 __le64 inos; /* total valid ino # (== f_files - f_favail) */
40 __le64 build_time; /* inode v1 time derivation */
41 __le32 build_time_nsec; /* inode v1 time derivation in nano scale */
42 __le32 blocks; /* used for statfs */
43 __le32 meta_blkaddr; /* start block address of metadata area */
44 __le32 xattr_blkaddr; /* start block address of shared xattr area */
45 __u8 uuid[16]; /* 128-bit uuid for volume */
46 __u8 volume_name[16]; /* volume name */
47 __le32 feature_incompat;
49 /* bitmap for available compression algorithms */
50 __le16 available_compr_algs;
51 /* customized sliding window size instead of 64k by default */
52 __le16 lz4_max_distance;
58 * erofs inode datalayout (i_format in on-disk inode):
59 * 0 - inode plain without inline data A:
60 * inode, [xattrs], ... | ... | no-holed data
61 * 1 - inode VLE compression B (legacy):
62 * inode, [xattrs], extents ... | ...
63 * 2 - inode plain with inline data C:
64 * inode, [xattrs], last_inline_data, ... | ... | no-holed data
65 * 3 - inode compression D:
66 * inode, [xattrs], map_header, extents ... | ...
70 EROFS_INODE_FLAT_PLAIN = 0,
71 EROFS_INODE_FLAT_COMPRESSION_LEGACY = 1,
72 EROFS_INODE_FLAT_INLINE = 2,
73 EROFS_INODE_FLAT_COMPRESSION = 3,
74 EROFS_INODE_DATALAYOUT_MAX
77 static inline bool erofs_inode_is_data_compressed(unsigned int datamode)
79 return datamode == EROFS_INODE_FLAT_COMPRESSION ||
80 datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY;
83 /* bit definitions of inode i_advise */
84 #define EROFS_I_VERSION_BITS 1
85 #define EROFS_I_DATALAYOUT_BITS 3
87 #define EROFS_I_VERSION_BIT 0
88 #define EROFS_I_DATALAYOUT_BIT 1
91 ((1 << (EROFS_I_DATALAYOUT_BIT + EROFS_I_DATALAYOUT_BITS)) - 1)
93 /* 32-byte reduced form of an ondisk inode */
94 struct erofs_inode_compact {
95 __le16 i_format; /* inode format hints */
97 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
98 __le16 i_xattr_icount;
104 /* file total compressed blocks for data mapping 1 */
105 __le32 compressed_blocks;
108 /* for device files, used to indicate old/new device # */
111 __le32 i_ino; /* only used for 32-bit stat compatibility */
117 /* 32 bytes on-disk inode */
118 #define EROFS_INODE_LAYOUT_COMPACT 0
119 /* 64 bytes on-disk inode */
120 #define EROFS_INODE_LAYOUT_EXTENDED 1
122 /* 64-byte complete form of an ondisk inode */
123 struct erofs_inode_extended {
124 __le16 i_format; /* inode format hints */
126 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
127 __le16 i_xattr_icount;
132 /* file total compressed blocks for data mapping 1 */
133 __le32 compressed_blocks;
136 /* for device files, used to indicate old/new device # */
140 /* only used for 32-bit stat compatibility */
148 __u8 i_reserved2[16];
151 #define EROFS_MAX_SHARED_XATTRS (128)
152 /* h_shared_count between 129 ... 255 are special # */
153 #define EROFS_SHARED_XATTR_EXTENT (255)
156 * inline xattrs (n == i_xattr_icount):
157 * erofs_xattr_ibody_header(1) + (n - 1) * 4 bytes
160 * /-----------------------\
161 * | erofs_xattr_entries+ |
162 * +-----------------------+
163 * inline xattrs must starts in erofs_xattr_ibody_header,
164 * for read-only fs, no need to introduce h_refcount
166 struct erofs_xattr_ibody_header {
170 __le32 h_shared_xattrs[0]; /* shared xattr id array */
174 #define EROFS_XATTR_INDEX_USER 1
175 #define EROFS_XATTR_INDEX_POSIX_ACL_ACCESS 2
176 #define EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT 3
177 #define EROFS_XATTR_INDEX_TRUSTED 4
178 #define EROFS_XATTR_INDEX_LUSTRE 5
179 #define EROFS_XATTR_INDEX_SECURITY 6
181 /* xattr entry (for both inline & shared xattrs) */
182 struct erofs_xattr_entry {
183 __u8 e_name_len; /* length of name */
184 __u8 e_name_index; /* attribute name index */
185 __le16 e_value_size; /* size of attribute value */
186 /* followed by e_name and e_value */
187 char e_name[0]; /* attribute name */
190 static inline unsigned int erofs_xattr_ibody_size(__le16 i_xattr_icount)
195 return sizeof(struct erofs_xattr_ibody_header) +
196 sizeof(__u32) * (le16_to_cpu(i_xattr_icount) - 1);
199 #define EROFS_XATTR_ALIGN(size) round_up(size, sizeof(struct erofs_xattr_entry))
201 static inline unsigned int erofs_xattr_entry_size(struct erofs_xattr_entry *e)
203 return EROFS_XATTR_ALIGN(sizeof(struct erofs_xattr_entry) +
204 e->e_name_len + le16_to_cpu(e->e_value_size));
207 /* maximum supported size of a physical compression cluster */
208 #define Z_EROFS_PCLUSTER_MAX_SIZE (1024 * 1024)
210 /* available compression algorithm types (for h_algorithmtype) */
212 Z_EROFS_COMPRESSION_LZ4 = 0,
213 Z_EROFS_COMPRESSION_MAX
215 #define Z_EROFS_ALL_COMPR_ALGS (1 << (Z_EROFS_COMPRESSION_MAX - 1))
217 /* 14 bytes (+ length field = 16 bytes) */
218 struct z_erofs_lz4_cfgs {
220 __le16 max_pclusterblks;
225 * bit 0 : COMPACTED_2B indexes (0 - off; 1 - on)
226 * e.g. for 4k logical cluster size, 4B if compacted 2B is off;
227 * (4B) + 2B + (4B) if compacted 2B is on.
228 * bit 1 : HEAD1 big pcluster (0 - off; 1 - on)
229 * bit 2 : HEAD2 big pcluster (0 - off; 1 - on)
231 #define Z_EROFS_ADVISE_COMPACTED_2B 0x0001
232 #define Z_EROFS_ADVISE_BIG_PCLUSTER_1 0x0002
233 #define Z_EROFS_ADVISE_BIG_PCLUSTER_2 0x0004
235 struct z_erofs_map_header {
239 * bit 0-3 : algorithm type of head 1 (logical cluster type 01);
240 * bit 4-7 : algorithm type of head 2 (logical cluster type 11).
242 __u8 h_algorithmtype;
244 * bit 0-2 : logical cluster bits - 12, e.g. 0 for 4096;
245 * bit 3-7 : reserved.
250 #define Z_EROFS_VLE_LEGACY_HEADER_PADDING 8
253 * Fixed-sized output compression ondisk Logical Extent cluster type:
254 * 0 - literal (uncompressed) cluster
255 * 1 - compressed cluster (for the head logical cluster)
256 * 2 - compressed cluster (for the other logical clusters)
259 * 0 - literal (uncompressed) cluster,
261 * di_clusterofs = the literal data offset of the cluster
262 * di_blkaddr = the blkaddr of the literal cluster
264 * 1 - compressed cluster (for the head logical cluster)
266 * di_clusterofs = the decompressed data offset of the cluster
267 * di_blkaddr = the blkaddr of the compressed cluster
269 * 2 - compressed cluster (for the other logical clusters)
272 * the decompressed data offset in its own head cluster
273 * di_u.delta[0] = distance to its corresponding head cluster
274 * di_u.delta[1] = distance to its corresponding tail cluster
275 * (di_advise could be 0, 1 or 2)
278 Z_EROFS_VLE_CLUSTER_TYPE_PLAIN = 0,
279 Z_EROFS_VLE_CLUSTER_TYPE_HEAD = 1,
280 Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD = 2,
281 Z_EROFS_VLE_CLUSTER_TYPE_RESERVED = 3,
282 Z_EROFS_VLE_CLUSTER_TYPE_MAX
285 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS 2
286 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT 0
289 * D0_CBLKCNT will be marked _only_ at the 1st non-head lcluster to store the
290 * compressed block count of a compressed extent (in logical clusters, aka.
291 * block count of a pcluster).
293 #define Z_EROFS_VLE_DI_D0_CBLKCNT (1 << 11)
295 struct z_erofs_vle_decompressed_index {
297 /* where to decompress in the head cluster */
298 __le16 di_clusterofs;
301 /* for the head cluster */
304 * for the rest clusters
305 * eg. for 4k page-sized cluster, maximum 4K*64k = 256M)
306 * [0] - pointing to the head cluster
307 * [1] - pointing to the tail cluster
313 #define Z_EROFS_VLE_LEGACY_INDEX_ALIGN(size) \
314 (round_up(size, sizeof(struct z_erofs_vle_decompressed_index)) + \
315 sizeof(struct z_erofs_map_header) + Z_EROFS_VLE_LEGACY_HEADER_PADDING)
317 /* dirent sorts in alphabet order, thus we can do binary search */
318 struct erofs_dirent {
319 __le64 nid; /* node number */
320 __le16 nameoff; /* start offset of file name */
321 __u8 file_type; /* file type */
322 __u8 reserved; /* reserved */
326 * EROFS file types should match generic FT_* types and
327 * it seems no need to add BUILD_BUG_ONs since potential
328 * unmatchness will break other fses as well...
331 #define EROFS_NAME_LEN 255
333 /* check the EROFS on-disk layout strictly at compile time */
334 static inline void erofs_check_ondisk_layout_definitions(void)
336 BUILD_BUG_ON(sizeof(struct erofs_super_block) != 128);
337 BUILD_BUG_ON(sizeof(struct erofs_inode_compact) != 32);
338 BUILD_BUG_ON(sizeof(struct erofs_inode_extended) != 64);
339 BUILD_BUG_ON(sizeof(struct erofs_xattr_ibody_header) != 12);
340 BUILD_BUG_ON(sizeof(struct erofs_xattr_entry) != 4);
341 BUILD_BUG_ON(sizeof(struct z_erofs_map_header) != 8);
342 BUILD_BUG_ON(sizeof(struct z_erofs_vle_decompressed_index) != 8);
343 BUILD_BUG_ON(sizeof(struct erofs_dirent) != 12);
345 BUILD_BUG_ON(BIT(Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) <
346 Z_EROFS_VLE_CLUSTER_TYPE_MAX - 1);