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_ALL_FEATURE_INCOMPAT \
31 (EROFS_FEATURE_INCOMPAT_ZERO_PADDING | \
32 EROFS_FEATURE_INCOMPAT_COMPR_CFGS | \
33 EROFS_FEATURE_INCOMPAT_BIG_PCLUSTER | \
34 EROFS_FEATURE_INCOMPAT_CHUNKED_FILE | \
35 EROFS_FEATURE_INCOMPAT_DEVICE_TABLE | \
36 EROFS_FEATURE_INCOMPAT_COMPR_HEAD2 | \
37 EROFS_FEATURE_INCOMPAT_ZTAILPACKING | \
38 EROFS_FEATURE_INCOMPAT_FRAGMENTS | \
39 EROFS_FEATURE_INCOMPAT_DEDUPE)
41 #define EROFS_SB_EXTSLOT_SIZE 16
43 struct erofs_deviceslot {
44 u8 tag[64]; /* digest(sha256), etc. */
45 __le32 blocks; /* total fs blocks of this device */
46 __le32 mapped_blkaddr; /* map starting at mapped_blkaddr */
49 #define EROFS_DEVT_SLOT_SIZE sizeof(struct erofs_deviceslot)
51 /* erofs on-disk super block (currently 128 bytes) */
52 struct erofs_super_block {
53 __le32 magic; /* file system magic number */
54 __le32 checksum; /* crc32c(super_block) */
55 __le32 feature_compat;
56 __u8 blkszbits; /* support block_size == PAGE_SIZE only */
57 __u8 sb_extslots; /* superblock size = 128 + sb_extslots * 16 */
59 __le16 root_nid; /* nid of root directory */
60 __le64 inos; /* total valid ino # (== f_files - f_favail) */
62 __le64 build_time; /* compact inode time derivation */
63 __le32 build_time_nsec; /* compact inode time derivation in ns scale */
64 __le32 blocks; /* used for statfs */
65 __le32 meta_blkaddr; /* start block address of metadata area */
66 __le32 xattr_blkaddr; /* start block address of shared xattr area */
67 __u8 uuid[16]; /* 128-bit uuid for volume */
68 __u8 volume_name[16]; /* volume name */
69 __le32 feature_incompat;
71 /* bitmap for available compression algorithms */
72 __le16 available_compr_algs;
73 /* customized sliding window size instead of 64k by default */
74 __le16 lz4_max_distance;
76 __le16 extra_devices; /* # of devices besides the primary device */
77 __le16 devt_slotoff; /* startoff = devt_slotoff * devt_slotsize */
79 __le64 packed_nid; /* nid of the special packed inode */
84 * erofs inode datalayout (i_format in on-disk inode):
85 * 0 - uncompressed flat inode without tail-packing inline data:
86 * inode, [xattrs], ... | ... | no-holed data
87 * 1 - compressed inode with non-compact indexes:
88 * inode, [xattrs], [map_header], extents ... | ...
89 * 2 - uncompressed flat inode with tail-packing inline data:
90 * inode, [xattrs], tailpacking data, ... | ... | no-holed data
91 * 3 - compressed inode with compact indexes:
92 * inode, [xattrs], map_header, extents ... | ...
93 * 4 - chunk-based inode with (optional) multi-device support:
94 * inode, [xattrs], chunk indexes ... | ...
98 EROFS_INODE_FLAT_PLAIN = 0,
99 EROFS_INODE_FLAT_COMPRESSION_LEGACY = 1,
100 EROFS_INODE_FLAT_INLINE = 2,
101 EROFS_INODE_FLAT_COMPRESSION = 3,
102 EROFS_INODE_CHUNK_BASED = 4,
103 EROFS_INODE_DATALAYOUT_MAX
106 static inline bool erofs_inode_is_data_compressed(unsigned int datamode)
108 return datamode == EROFS_INODE_FLAT_COMPRESSION ||
109 datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY;
112 /* bit definitions of inode i_format */
113 #define EROFS_I_VERSION_BITS 1
114 #define EROFS_I_DATALAYOUT_BITS 3
116 #define EROFS_I_VERSION_BIT 0
117 #define EROFS_I_DATALAYOUT_BIT 1
119 #define EROFS_I_ALL \
120 ((1 << (EROFS_I_DATALAYOUT_BIT + EROFS_I_DATALAYOUT_BITS)) - 1)
122 /* indicate chunk blkbits, thus 'chunksize = blocksize << chunk blkbits' */
123 #define EROFS_CHUNK_FORMAT_BLKBITS_MASK 0x001F
124 /* with chunk indexes or just a 4-byte blkaddr array */
125 #define EROFS_CHUNK_FORMAT_INDEXES 0x0020
127 #define EROFS_CHUNK_FORMAT_ALL \
128 (EROFS_CHUNK_FORMAT_BLKBITS_MASK | EROFS_CHUNK_FORMAT_INDEXES)
130 struct erofs_inode_chunk_info {
131 __le16 format; /* chunk blkbits, etc. */
135 /* 32-byte reduced form of an ondisk inode */
136 struct erofs_inode_compact {
137 __le16 i_format; /* inode format hints */
139 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
140 __le16 i_xattr_icount;
146 /* total compressed blocks for compressed inodes */
147 __le32 compressed_blocks;
148 /* block address for uncompressed flat inodes */
151 /* for device files, used to indicate old/new device # */
154 /* for chunk-based files, it contains the summary info */
155 struct erofs_inode_chunk_info c;
157 __le32 i_ino; /* only used for 32-bit stat compatibility */
163 /* 32-byte on-disk inode */
164 #define EROFS_INODE_LAYOUT_COMPACT 0
165 /* 64-byte on-disk inode */
166 #define EROFS_INODE_LAYOUT_EXTENDED 1
168 /* 64-byte complete form of an ondisk inode */
169 struct erofs_inode_extended {
170 __le16 i_format; /* inode format hints */
172 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
173 __le16 i_xattr_icount;
178 /* total compressed blocks for compressed inodes */
179 __le32 compressed_blocks;
180 /* block address for uncompressed flat inodes */
183 /* for device files, used to indicate old/new device # */
186 /* for chunk-based files, it contains the summary info */
187 struct erofs_inode_chunk_info c;
190 /* only used for 32-bit stat compatibility */
198 __u8 i_reserved2[16];
201 #define EROFS_MAX_SHARED_XATTRS (128)
202 /* h_shared_count between 129 ... 255 are special # */
203 #define EROFS_SHARED_XATTR_EXTENT (255)
206 * inline xattrs (n == i_xattr_icount):
207 * erofs_xattr_ibody_header(1) + (n - 1) * 4 bytes
210 * /-----------------------\
211 * | erofs_xattr_entries+ |
212 * +-----------------------+
213 * inline xattrs must starts in erofs_xattr_ibody_header,
214 * for read-only fs, no need to introduce h_refcount
216 struct erofs_xattr_ibody_header {
220 __le32 h_shared_xattrs[]; /* shared xattr id array */
224 #define EROFS_XATTR_INDEX_USER 1
225 #define EROFS_XATTR_INDEX_POSIX_ACL_ACCESS 2
226 #define EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT 3
227 #define EROFS_XATTR_INDEX_TRUSTED 4
228 #define EROFS_XATTR_INDEX_LUSTRE 5
229 #define EROFS_XATTR_INDEX_SECURITY 6
231 /* xattr entry (for both inline & shared xattrs) */
232 struct erofs_xattr_entry {
233 __u8 e_name_len; /* length of name */
234 __u8 e_name_index; /* attribute name index */
235 __le16 e_value_size; /* size of attribute value */
236 /* followed by e_name and e_value */
237 char e_name[]; /* attribute name */
240 static inline unsigned int erofs_xattr_ibody_size(__le16 i_xattr_icount)
245 return sizeof(struct erofs_xattr_ibody_header) +
246 sizeof(__u32) * (le16_to_cpu(i_xattr_icount) - 1);
249 #define EROFS_XATTR_ALIGN(size) round_up(size, sizeof(struct erofs_xattr_entry))
251 static inline unsigned int erofs_xattr_entry_size(struct erofs_xattr_entry *e)
253 return EROFS_XATTR_ALIGN(sizeof(struct erofs_xattr_entry) +
254 e->e_name_len + le16_to_cpu(e->e_value_size));
257 /* represent a zeroed chunk (hole) */
258 #define EROFS_NULL_ADDR -1
260 /* 4-byte block address array */
261 #define EROFS_BLOCK_MAP_ENTRY_SIZE sizeof(__le32)
263 /* 8-byte inode chunk indexes */
264 struct erofs_inode_chunk_index {
265 __le16 advise; /* always 0, don't care for now */
266 __le16 device_id; /* back-end storage id (with bits masked) */
267 __le32 blkaddr; /* start block address of this inode chunk */
270 /* maximum supported size of a physical compression cluster */
271 #define Z_EROFS_PCLUSTER_MAX_SIZE (1024 * 1024)
273 /* available compression algorithm types (for h_algorithmtype) */
275 Z_EROFS_COMPRESSION_LZ4 = 0,
276 Z_EROFS_COMPRESSION_LZMA = 1,
277 Z_EROFS_COMPRESSION_MAX
279 #define Z_EROFS_ALL_COMPR_ALGS ((1 << Z_EROFS_COMPRESSION_MAX) - 1)
281 /* 14 bytes (+ length field = 16 bytes) */
282 struct z_erofs_lz4_cfgs {
284 __le16 max_pclusterblks;
288 /* 14 bytes (+ length field = 16 bytes) */
289 struct z_erofs_lzma_cfgs {
295 #define Z_EROFS_LZMA_MAX_DICT_SIZE (8 * Z_EROFS_PCLUSTER_MAX_SIZE)
298 * bit 0 : COMPACTED_2B indexes (0 - off; 1 - on)
299 * e.g. for 4k logical cluster size, 4B if compacted 2B is off;
300 * (4B) + 2B + (4B) if compacted 2B is on.
301 * bit 1 : HEAD1 big pcluster (0 - off; 1 - on)
302 * bit 2 : HEAD2 big pcluster (0 - off; 1 - on)
303 * bit 3 : tailpacking inline pcluster (0 - off; 1 - on)
304 * bit 4 : interlaced plain pcluster (0 - off; 1 - on)
305 * bit 5 : fragment pcluster (0 - off; 1 - on)
307 #define Z_EROFS_ADVISE_COMPACTED_2B 0x0001
308 #define Z_EROFS_ADVISE_BIG_PCLUSTER_1 0x0002
309 #define Z_EROFS_ADVISE_BIG_PCLUSTER_2 0x0004
310 #define Z_EROFS_ADVISE_INLINE_PCLUSTER 0x0008
311 #define Z_EROFS_ADVISE_INTERLACED_PCLUSTER 0x0010
312 #define Z_EROFS_ADVISE_FRAGMENT_PCLUSTER 0x0020
314 #define Z_EROFS_FRAGMENT_INODE_BIT 7
315 struct z_erofs_map_header {
317 /* fragment data offset in the packed inode */
318 __le32 h_fragmentoff;
321 /* indicates the encoded size of tailpacking data */
327 * bit 0-3 : algorithm type of head 1 (logical cluster type 01);
328 * bit 4-7 : algorithm type of head 2 (logical cluster type 11).
330 __u8 h_algorithmtype;
332 * bit 0-2 : logical cluster bits - 12, e.g. 0 for 4096;
333 * bit 3-6 : reserved;
334 * bit 7 : move the whole file into packed inode or not.
339 #define Z_EROFS_VLE_LEGACY_HEADER_PADDING 8
342 * Fixed-sized output compression on-disk logical cluster type:
343 * 0 - literal (uncompressed) lcluster
344 * 1,3 - compressed lcluster (for HEAD lclusters)
345 * 2 - compressed lcluster (for NONHEAD lclusters)
348 * 0 - literal (uncompressed) lcluster,
350 * di_clusterofs = the literal data offset of the lcluster
351 * di_blkaddr = the blkaddr of the literal pcluster
353 * 1,3 - compressed lcluster (for HEAD lclusters)
355 * di_clusterofs = the decompressed data offset of the lcluster
356 * di_blkaddr = the blkaddr of the compressed pcluster
358 * 2 - compressed lcluster (for NONHEAD lclusters)
361 * the decompressed data offset in its own HEAD lcluster
362 * di_u.delta[0] = distance to this HEAD lcluster
363 * di_u.delta[1] = distance to the next HEAD lcluster
366 Z_EROFS_VLE_CLUSTER_TYPE_PLAIN = 0,
367 Z_EROFS_VLE_CLUSTER_TYPE_HEAD1 = 1,
368 Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD = 2,
369 Z_EROFS_VLE_CLUSTER_TYPE_HEAD2 = 3,
370 Z_EROFS_VLE_CLUSTER_TYPE_MAX
373 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS 2
374 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT 0
376 /* (noncompact only, HEAD) This pcluster refers to partial decompressed data */
377 #define Z_EROFS_VLE_DI_PARTIAL_REF (1 << 15)
380 * D0_CBLKCNT will be marked _only_ at the 1st non-head lcluster to store the
381 * compressed block count of a compressed extent (in logical clusters, aka.
382 * block count of a pcluster).
384 #define Z_EROFS_VLE_DI_D0_CBLKCNT (1 << 11)
386 struct z_erofs_vle_decompressed_index {
388 /* where to decompress in the head lcluster */
389 __le16 di_clusterofs;
392 /* for the HEAD lclusters */
395 * for the NONHEAD lclusters
396 * [0] - distance to its HEAD lcluster
397 * [1] - distance to the next HEAD lcluster
403 #define Z_EROFS_VLE_LEGACY_INDEX_ALIGN(size) \
404 (round_up(size, sizeof(struct z_erofs_vle_decompressed_index)) + \
405 sizeof(struct z_erofs_map_header) + Z_EROFS_VLE_LEGACY_HEADER_PADDING)
407 /* dirent sorts in alphabet order, thus we can do binary search */
408 struct erofs_dirent {
409 __le64 nid; /* node number */
410 __le16 nameoff; /* start offset of file name */
411 __u8 file_type; /* file type */
412 __u8 reserved; /* reserved */
416 * EROFS file types should match generic FT_* types and
417 * it seems no need to add BUILD_BUG_ONs since potential
418 * unmatchness will break other fses as well...
421 #define EROFS_NAME_LEN 255
423 /* check the EROFS on-disk layout strictly at compile time */
424 static inline void erofs_check_ondisk_layout_definitions(void)
426 const __le64 fmh = *(__le64 *)&(struct z_erofs_map_header) {
427 .h_clusterbits = 1 << Z_EROFS_FRAGMENT_INODE_BIT
430 BUILD_BUG_ON(sizeof(struct erofs_super_block) != 128);
431 BUILD_BUG_ON(sizeof(struct erofs_inode_compact) != 32);
432 BUILD_BUG_ON(sizeof(struct erofs_inode_extended) != 64);
433 BUILD_BUG_ON(sizeof(struct erofs_xattr_ibody_header) != 12);
434 BUILD_BUG_ON(sizeof(struct erofs_xattr_entry) != 4);
435 BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_info) != 4);
436 BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_index) != 8);
437 BUILD_BUG_ON(sizeof(struct z_erofs_map_header) != 8);
438 BUILD_BUG_ON(sizeof(struct z_erofs_vle_decompressed_index) != 8);
439 BUILD_BUG_ON(sizeof(struct erofs_dirent) != 12);
440 /* keep in sync between 2 index structures for better extendibility */
441 BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_index) !=
442 sizeof(struct z_erofs_vle_decompressed_index));
443 BUILD_BUG_ON(sizeof(struct erofs_deviceslot) != 128);
445 BUILD_BUG_ON(BIT(Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) <
446 Z_EROFS_VLE_CLUSTER_TYPE_MAX - 1);
447 /* exclude old compiler versions like gcc 7.5.0 */
448 BUILD_BUG_ON(__builtin_constant_p(fmh) ?
449 fmh != cpu_to_le64(1ULL << 63) : 0);