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
17 * Any bits that aren't in EROFS_ALL_FEATURE_INCOMPAT should
18 * be incompatible with this kernel version.
20 #define EROFS_FEATURE_INCOMPAT_LZ4_0PADDING 0x00000001
21 #define EROFS_FEATURE_INCOMPAT_COMPR_CFGS 0x00000002
22 #define EROFS_FEATURE_INCOMPAT_BIG_PCLUSTER 0x00000002
23 #define EROFS_FEATURE_INCOMPAT_CHUNKED_FILE 0x00000004
24 #define EROFS_ALL_FEATURE_INCOMPAT \
25 (EROFS_FEATURE_INCOMPAT_LZ4_0PADDING | \
26 EROFS_FEATURE_INCOMPAT_COMPR_CFGS | \
27 EROFS_FEATURE_INCOMPAT_BIG_PCLUSTER | \
28 EROFS_FEATURE_INCOMPAT_CHUNKED_FILE)
30 #define EROFS_SB_EXTSLOT_SIZE 16
32 /* erofs on-disk super block (currently 128 bytes) */
33 struct erofs_super_block {
34 __le32 magic; /* file system magic number */
35 __le32 checksum; /* crc32c(super_block) */
36 __le32 feature_compat;
37 __u8 blkszbits; /* support block_size == PAGE_SIZE only */
38 __u8 sb_extslots; /* superblock size = 128 + sb_extslots * 16 */
40 __le16 root_nid; /* nid of root directory */
41 __le64 inos; /* total valid ino # (== f_files - f_favail) */
43 __le64 build_time; /* inode v1 time derivation */
44 __le32 build_time_nsec; /* inode v1 time derivation in nano scale */
45 __le32 blocks; /* used for statfs */
46 __le32 meta_blkaddr; /* start block address of metadata area */
47 __le32 xattr_blkaddr; /* start block address of shared xattr area */
48 __u8 uuid[16]; /* 128-bit uuid for volume */
49 __u8 volume_name[16]; /* volume name */
50 __le32 feature_incompat;
52 /* bitmap for available compression algorithms */
53 __le16 available_compr_algs;
54 /* customized sliding window size instead of 64k by default */
55 __le16 lz4_max_distance;
61 * erofs inode datalayout (i_format in on-disk inode):
62 * 0 - inode plain without inline data A:
63 * inode, [xattrs], ... | ... | no-holed data
64 * 1 - inode VLE compression B (legacy):
65 * inode, [xattrs], extents ... | ...
66 * 2 - inode plain with inline data C:
67 * inode, [xattrs], last_inline_data, ... | ... | no-holed data
68 * 3 - inode compression D:
69 * inode, [xattrs], map_header, extents ... | ...
70 * 4 - inode chunk-based E:
71 * inode, [xattrs], chunk indexes ... | ...
75 EROFS_INODE_FLAT_PLAIN = 0,
76 EROFS_INODE_FLAT_COMPRESSION_LEGACY = 1,
77 EROFS_INODE_FLAT_INLINE = 2,
78 EROFS_INODE_FLAT_COMPRESSION = 3,
79 EROFS_INODE_CHUNK_BASED = 4,
80 EROFS_INODE_DATALAYOUT_MAX
83 static inline bool erofs_inode_is_data_compressed(unsigned int datamode)
85 return datamode == EROFS_INODE_FLAT_COMPRESSION ||
86 datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY;
89 /* bit definitions of inode i_advise */
90 #define EROFS_I_VERSION_BITS 1
91 #define EROFS_I_DATALAYOUT_BITS 3
93 #define EROFS_I_VERSION_BIT 0
94 #define EROFS_I_DATALAYOUT_BIT 1
97 ((1 << (EROFS_I_DATALAYOUT_BIT + EROFS_I_DATALAYOUT_BITS)) - 1)
99 /* indicate chunk blkbits, thus 'chunksize = blocksize << chunk blkbits' */
100 #define EROFS_CHUNK_FORMAT_BLKBITS_MASK 0x001F
101 /* with chunk indexes or just a 4-byte blkaddr array */
102 #define EROFS_CHUNK_FORMAT_INDEXES 0x0020
104 #define EROFS_CHUNK_FORMAT_ALL \
105 (EROFS_CHUNK_FORMAT_BLKBITS_MASK | EROFS_CHUNK_FORMAT_INDEXES)
107 struct erofs_inode_chunk_info {
108 __le16 format; /* chunk blkbits, etc. */
112 /* 32-byte reduced form of an ondisk inode */
113 struct erofs_inode_compact {
114 __le16 i_format; /* inode format hints */
116 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
117 __le16 i_xattr_icount;
123 /* file total compressed blocks for data mapping 1 */
124 __le32 compressed_blocks;
127 /* for device files, used to indicate old/new device # */
130 /* for chunk-based files, it contains the summary info */
131 struct erofs_inode_chunk_info c;
133 __le32 i_ino; /* only used for 32-bit stat compatibility */
139 /* 32 bytes on-disk inode */
140 #define EROFS_INODE_LAYOUT_COMPACT 0
141 /* 64 bytes on-disk inode */
142 #define EROFS_INODE_LAYOUT_EXTENDED 1
144 /* 64-byte complete form of an ondisk inode */
145 struct erofs_inode_extended {
146 __le16 i_format; /* inode format hints */
148 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
149 __le16 i_xattr_icount;
154 /* file total compressed blocks for data mapping 1 */
155 __le32 compressed_blocks;
158 /* for device files, used to indicate old/new device # */
161 /* for chunk-based files, it contains the summary info */
162 struct erofs_inode_chunk_info c;
165 /* only used for 32-bit stat compatibility */
173 __u8 i_reserved2[16];
176 #define EROFS_MAX_SHARED_XATTRS (128)
177 /* h_shared_count between 129 ... 255 are special # */
178 #define EROFS_SHARED_XATTR_EXTENT (255)
181 * inline xattrs (n == i_xattr_icount):
182 * erofs_xattr_ibody_header(1) + (n - 1) * 4 bytes
185 * /-----------------------\
186 * | erofs_xattr_entries+ |
187 * +-----------------------+
188 * inline xattrs must starts in erofs_xattr_ibody_header,
189 * for read-only fs, no need to introduce h_refcount
191 struct erofs_xattr_ibody_header {
195 __le32 h_shared_xattrs[0]; /* shared xattr id array */
199 #define EROFS_XATTR_INDEX_USER 1
200 #define EROFS_XATTR_INDEX_POSIX_ACL_ACCESS 2
201 #define EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT 3
202 #define EROFS_XATTR_INDEX_TRUSTED 4
203 #define EROFS_XATTR_INDEX_LUSTRE 5
204 #define EROFS_XATTR_INDEX_SECURITY 6
206 /* xattr entry (for both inline & shared xattrs) */
207 struct erofs_xattr_entry {
208 __u8 e_name_len; /* length of name */
209 __u8 e_name_index; /* attribute name index */
210 __le16 e_value_size; /* size of attribute value */
211 /* followed by e_name and e_value */
212 char e_name[0]; /* attribute name */
215 static inline unsigned int erofs_xattr_ibody_size(__le16 i_xattr_icount)
220 return sizeof(struct erofs_xattr_ibody_header) +
221 sizeof(__u32) * (le16_to_cpu(i_xattr_icount) - 1);
224 #define EROFS_XATTR_ALIGN(size) round_up(size, sizeof(struct erofs_xattr_entry))
226 static inline unsigned int erofs_xattr_entry_size(struct erofs_xattr_entry *e)
228 return EROFS_XATTR_ALIGN(sizeof(struct erofs_xattr_entry) +
229 e->e_name_len + le16_to_cpu(e->e_value_size));
232 /* represent a zeroed chunk (hole) */
233 #define EROFS_NULL_ADDR -1
235 /* 4-byte block address array */
236 #define EROFS_BLOCK_MAP_ENTRY_SIZE sizeof(__le32)
238 /* 8-byte inode chunk indexes */
239 struct erofs_inode_chunk_index {
240 __le16 advise; /* always 0, don't care for now */
241 __le16 device_id; /* back-end storage id, always 0 for now */
242 __le32 blkaddr; /* start block address of this inode chunk */
245 /* maximum supported size of a physical compression cluster */
246 #define Z_EROFS_PCLUSTER_MAX_SIZE (1024 * 1024)
248 /* available compression algorithm types (for h_algorithmtype) */
250 Z_EROFS_COMPRESSION_LZ4 = 0,
251 Z_EROFS_COMPRESSION_MAX
253 #define Z_EROFS_ALL_COMPR_ALGS (1 << (Z_EROFS_COMPRESSION_MAX - 1))
255 /* 14 bytes (+ length field = 16 bytes) */
256 struct z_erofs_lz4_cfgs {
258 __le16 max_pclusterblks;
263 * bit 0 : COMPACTED_2B indexes (0 - off; 1 - on)
264 * e.g. for 4k logical cluster size, 4B if compacted 2B is off;
265 * (4B) + 2B + (4B) if compacted 2B is on.
266 * bit 1 : HEAD1 big pcluster (0 - off; 1 - on)
267 * bit 2 : HEAD2 big pcluster (0 - off; 1 - on)
269 #define Z_EROFS_ADVISE_COMPACTED_2B 0x0001
270 #define Z_EROFS_ADVISE_BIG_PCLUSTER_1 0x0002
271 #define Z_EROFS_ADVISE_BIG_PCLUSTER_2 0x0004
273 struct z_erofs_map_header {
277 * bit 0-3 : algorithm type of head 1 (logical cluster type 01);
278 * bit 4-7 : algorithm type of head 2 (logical cluster type 11).
280 __u8 h_algorithmtype;
282 * bit 0-2 : logical cluster bits - 12, e.g. 0 for 4096;
283 * bit 3-7 : reserved.
288 #define Z_EROFS_VLE_LEGACY_HEADER_PADDING 8
291 * Fixed-sized output compression ondisk Logical Extent cluster type:
292 * 0 - literal (uncompressed) cluster
293 * 1 - compressed cluster (for the head logical cluster)
294 * 2 - compressed cluster (for the other logical clusters)
297 * 0 - literal (uncompressed) cluster,
299 * di_clusterofs = the literal data offset of the cluster
300 * di_blkaddr = the blkaddr of the literal cluster
302 * 1 - compressed cluster (for the head logical cluster)
304 * di_clusterofs = the decompressed data offset of the cluster
305 * di_blkaddr = the blkaddr of the compressed cluster
307 * 2 - compressed cluster (for the other logical clusters)
310 * the decompressed data offset in its own head cluster
311 * di_u.delta[0] = distance to its corresponding head cluster
312 * di_u.delta[1] = distance to its corresponding tail cluster
313 * (di_advise could be 0, 1 or 2)
316 Z_EROFS_VLE_CLUSTER_TYPE_PLAIN = 0,
317 Z_EROFS_VLE_CLUSTER_TYPE_HEAD = 1,
318 Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD = 2,
319 Z_EROFS_VLE_CLUSTER_TYPE_RESERVED = 3,
320 Z_EROFS_VLE_CLUSTER_TYPE_MAX
323 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS 2
324 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT 0
327 * D0_CBLKCNT will be marked _only_ at the 1st non-head lcluster to store the
328 * compressed block count of a compressed extent (in logical clusters, aka.
329 * block count of a pcluster).
331 #define Z_EROFS_VLE_DI_D0_CBLKCNT (1 << 11)
333 struct z_erofs_vle_decompressed_index {
335 /* where to decompress in the head cluster */
336 __le16 di_clusterofs;
339 /* for the head cluster */
342 * for the rest clusters
343 * eg. for 4k page-sized cluster, maximum 4K*64k = 256M)
344 * [0] - pointing to the head cluster
345 * [1] - pointing to the tail cluster
351 #define Z_EROFS_VLE_LEGACY_INDEX_ALIGN(size) \
352 (round_up(size, sizeof(struct z_erofs_vle_decompressed_index)) + \
353 sizeof(struct z_erofs_map_header) + Z_EROFS_VLE_LEGACY_HEADER_PADDING)
355 /* dirent sorts in alphabet order, thus we can do binary search */
356 struct erofs_dirent {
357 __le64 nid; /* node number */
358 __le16 nameoff; /* start offset of file name */
359 __u8 file_type; /* file type */
360 __u8 reserved; /* reserved */
364 * EROFS file types should match generic FT_* types and
365 * it seems no need to add BUILD_BUG_ONs since potential
366 * unmatchness will break other fses as well...
369 #define EROFS_NAME_LEN 255
371 /* check the EROFS on-disk layout strictly at compile time */
372 static inline void erofs_check_ondisk_layout_definitions(void)
374 BUILD_BUG_ON(sizeof(struct erofs_super_block) != 128);
375 BUILD_BUG_ON(sizeof(struct erofs_inode_compact) != 32);
376 BUILD_BUG_ON(sizeof(struct erofs_inode_extended) != 64);
377 BUILD_BUG_ON(sizeof(struct erofs_xattr_ibody_header) != 12);
378 BUILD_BUG_ON(sizeof(struct erofs_xattr_entry) != 4);
379 BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_info) != 4);
380 BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_index) != 8);
381 BUILD_BUG_ON(sizeof(struct z_erofs_map_header) != 8);
382 BUILD_BUG_ON(sizeof(struct z_erofs_vle_decompressed_index) != 8);
383 BUILD_BUG_ON(sizeof(struct erofs_dirent) != 12);
384 /* keep in sync between 2 index structures for better extendibility */
385 BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_index) !=
386 sizeof(struct z_erofs_vle_decompressed_index));
388 BUILD_BUG_ON(BIT(Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) <
389 Z_EROFS_VLE_CLUSTER_TYPE_MAX - 1);