1 /* SPDX-License-Identifier: GPL-2.0-only OR Apache-2.0 */
3 * Copyright (C) 2017-2018 HUAWEI, Inc.
4 * http://www.huawei.com/
5 * Created by Gao Xiang <gaoxiang25@huawei.com>
10 /* Enhanced(Extended) ROM File System */
11 #define EROFS_SUPER_OFFSET 1024
14 * Any bits that aren't in EROFS_ALL_REQUIREMENTS should be
15 * incompatible with this kernel version.
17 #define EROFS_REQUIREMENT_LZ4_0PADDING 0x00000001
18 #define EROFS_ALL_REQUIREMENTS EROFS_REQUIREMENT_LZ4_0PADDING
20 struct erofs_super_block {
21 /* 0 */__le32 magic; /* in the little endian */
22 /* 4 */__le32 checksum; /* crc32c(super_block) */
23 /* 8 */__le32 features; /* (aka. feature_compat) */
24 /* 12 */__u8 blkszbits; /* support block_size == PAGE_SIZE only */
25 /* 13 */__u8 reserved;
27 /* 14 */__le16 root_nid;
28 /* 16 */__le64 inos; /* total valid ino # (== f_files - f_favail) */
30 /* 24 */__le64 build_time; /* inode v1 time derivation */
31 /* 32 */__le32 build_time_nsec;
32 /* 36 */__le32 blocks; /* used for statfs */
33 /* 40 */__le32 meta_blkaddr;
34 /* 44 */__le32 xattr_blkaddr;
35 /* 48 */__u8 uuid[16]; /* 128-bit uuid for volume */
36 /* 64 */__u8 volume_name[16]; /* volume name */
37 /* 80 */__le32 requirements; /* (aka. feature_incompat) */
39 /* 84 */__u8 reserved2[44];
40 } __packed; /* 128 bytes */
43 * erofs inode data mapping:
44 * 0 - inode plain without inline data A:
45 * inode, [xattrs], ... | ... | no-holed data
46 * 1 - inode VLE compression B (legacy):
47 * inode, [xattrs], extents ... | ...
48 * 2 - inode plain with inline data C:
49 * inode, [xattrs], last_inline_data, ... | ... | no-holed data
50 * 3 - inode compression D:
51 * inode, [xattrs], map_header, extents ... | ...
55 EROFS_INODE_FLAT_PLAIN,
56 EROFS_INODE_FLAT_COMPRESSION_LEGACY,
57 EROFS_INODE_FLAT_INLINE,
58 EROFS_INODE_FLAT_COMPRESSION,
59 EROFS_INODE_LAYOUT_MAX
62 static inline bool erofs_inode_is_data_compressed(unsigned int datamode)
64 if (datamode == EROFS_INODE_FLAT_COMPRESSION)
66 return datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY;
69 /* bit definitions of inode i_advise */
70 #define EROFS_I_VERSION_BITS 1
71 #define EROFS_I_DATA_MAPPING_BITS 3
73 #define EROFS_I_VERSION_BIT 0
74 #define EROFS_I_DATA_MAPPING_BIT 1
76 struct erofs_inode_v1 {
77 /* 0 */__le16 i_advise;
79 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
80 /* 2 */__le16 i_xattr_icount;
82 /* 6 */__le16 i_nlink;
84 /* 12 */__le32 i_reserved;
86 /* file total compressed blocks for data mapping 1 */
87 __le32 compressed_blocks;
90 /* for device files, used to indicate old/new device # */
93 /* 20 */__le32 i_ino; /* only used for 32-bit stat compatibility */
96 /* 28 */__le32 i_reserved2;
99 /* 32 bytes on-disk inode */
100 #define EROFS_INODE_LAYOUT_V1 0
101 /* 64 bytes on-disk inode */
102 #define EROFS_INODE_LAYOUT_V2 1
104 struct erofs_inode_v2 {
105 /* 0 */__le16 i_advise;
107 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
108 /* 2 */__le16 i_xattr_icount;
109 /* 4 */__le16 i_mode;
110 /* 6 */__le16 i_reserved;
111 /* 8 */__le64 i_size;
113 /* file total compressed blocks for data mapping 1 */
114 __le32 compressed_blocks;
117 /* for device files, used to indicate old/new device # */
121 /* only used for 32-bit stat compatibility */
122 /* 20 */__le32 i_ino;
124 /* 24 */__le32 i_uid;
125 /* 28 */__le32 i_gid;
126 /* 32 */__le64 i_ctime;
127 /* 40 */__le32 i_ctime_nsec;
128 /* 44 */__le32 i_nlink;
129 /* 48 */__u8 i_reserved2[16];
130 } __packed; /* 64 bytes */
132 #define EROFS_MAX_SHARED_XATTRS (128)
133 /* h_shared_count between 129 ... 255 are special # */
134 #define EROFS_SHARED_XATTR_EXTENT (255)
137 * inline xattrs (n == i_xattr_icount):
138 * erofs_xattr_ibody_header(1) + (n - 1) * 4 bytes
141 * /-----------------------\
142 * | erofs_xattr_entries+ |
143 * +-----------------------+
144 * inline xattrs must starts in erofs_xattr_ibody_header,
145 * for read-only fs, no need to introduce h_refcount
147 struct erofs_xattr_ibody_header {
151 __le32 h_shared_xattrs[0]; /* shared xattr id array */
155 #define EROFS_XATTR_INDEX_USER 1
156 #define EROFS_XATTR_INDEX_POSIX_ACL_ACCESS 2
157 #define EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT 3
158 #define EROFS_XATTR_INDEX_TRUSTED 4
159 #define EROFS_XATTR_INDEX_LUSTRE 5
160 #define EROFS_XATTR_INDEX_SECURITY 6
162 /* xattr entry (for both inline & shared xattrs) */
163 struct erofs_xattr_entry {
164 __u8 e_name_len; /* length of name */
165 __u8 e_name_index; /* attribute name index */
166 __le16 e_value_size; /* size of attribute value */
167 /* followed by e_name and e_value */
168 char e_name[0]; /* attribute name */
171 #define ondisk_xattr_ibody_size(count) ({\
172 u32 __count = le16_to_cpu(count); \
173 ((__count) == 0) ? 0 : \
174 sizeof(struct erofs_xattr_ibody_header) + \
175 sizeof(__u32) * ((__count) - 1); })
177 #define EROFS_XATTR_ALIGN(size) round_up(size, sizeof(struct erofs_xattr_entry))
178 #define EROFS_XATTR_ENTRY_SIZE(entry) EROFS_XATTR_ALIGN( \
179 sizeof(struct erofs_xattr_entry) + \
180 (entry)->e_name_len + le16_to_cpu((entry)->e_value_size))
182 /* available compression algorithm types */
184 Z_EROFS_COMPRESSION_LZ4,
185 Z_EROFS_COMPRESSION_MAX
189 * bit 0 : COMPACTED_2B indexes (0 - off; 1 - on)
190 * e.g. for 4k logical cluster size, 4B if compacted 2B is off;
191 * (4B) + 2B + (4B) if compacted 2B is on.
193 #define Z_EROFS_ADVISE_COMPACTED_2B_BIT 0
195 #define Z_EROFS_ADVISE_COMPACTED_2B (1 << Z_EROFS_ADVISE_COMPACTED_2B_BIT)
197 struct z_erofs_map_header {
201 * bit 0-3 : algorithm type of head 1 (logical cluster type 01);
202 * bit 4-7 : algorithm type of head 2 (logical cluster type 11).
204 __u8 h_algorithmtype;
206 * bit 0-2 : logical cluster bits - 12, e.g. 0 for 4096;
207 * bit 3-4 : (physical - logical) cluster bits of head 1:
208 * For example, if logical clustersize = 4096, 1 for 8192.
209 * bit 5-7 : (physical - logical) cluster bits of head 2.
214 #define Z_EROFS_VLE_LEGACY_HEADER_PADDING 8
217 * Z_EROFS Variable-sized Logical Extent cluster type:
218 * 0 - literal (uncompressed) cluster
219 * 1 - compressed cluster (for the head logical cluster)
220 * 2 - compressed cluster (for the other logical clusters)
223 * 0 - literal (uncompressed) cluster,
225 * di_clusterofs = the literal data offset of the cluster
226 * di_blkaddr = the blkaddr of the literal cluster
228 * 1 - compressed cluster (for the head logical cluster)
230 * di_clusterofs = the decompressed data offset of the cluster
231 * di_blkaddr = the blkaddr of the compressed cluster
233 * 2 - compressed cluster (for the other logical clusters)
236 * the decompressed data offset in its own head cluster
237 * di_u.delta[0] = distance to its corresponding head cluster
238 * di_u.delta[1] = distance to its corresponding tail cluster
239 * (di_advise could be 0, 1 or 2)
242 Z_EROFS_VLE_CLUSTER_TYPE_PLAIN,
243 Z_EROFS_VLE_CLUSTER_TYPE_HEAD,
244 Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD,
245 Z_EROFS_VLE_CLUSTER_TYPE_RESERVED,
246 Z_EROFS_VLE_CLUSTER_TYPE_MAX
249 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS 2
250 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT 0
252 struct z_erofs_vle_decompressed_index {
254 /* where to decompress in the head cluster */
255 __le16 di_clusterofs;
258 /* for the head cluster */
261 * for the rest clusters
262 * eg. for 4k page-sized cluster, maximum 4K*64k = 256M)
263 * [0] - pointing to the head cluster
264 * [1] - pointing to the tail cluster
267 } di_u __packed; /* 8 bytes */
270 #define Z_EROFS_VLE_LEGACY_INDEX_ALIGN(size) \
271 (round_up(size, sizeof(struct z_erofs_vle_decompressed_index)) + \
272 sizeof(struct z_erofs_map_header) + Z_EROFS_VLE_LEGACY_HEADER_PADDING)
274 /* dirent sorts in alphabet order, thus we can do binary search */
275 struct erofs_dirent {
276 __le64 nid; /* 0, node number */
277 __le16 nameoff; /* 8, start offset of file name */
278 __u8 file_type; /* 10, file type */
279 __u8 reserved; /* 11, reserved */
283 * EROFS file types should match generic FT_* types and
284 * it seems no need to add BUILD_BUG_ONs since potential
285 * unmatchness will break other fses as well...
288 #define EROFS_NAME_LEN 255
290 /* check the EROFS on-disk layout strictly at compile time */
291 static inline void erofs_check_ondisk_layout_definitions(void)
293 BUILD_BUG_ON(sizeof(struct erofs_super_block) != 128);
294 BUILD_BUG_ON(sizeof(struct erofs_inode_v1) != 32);
295 BUILD_BUG_ON(sizeof(struct erofs_inode_v2) != 64);
296 BUILD_BUG_ON(sizeof(struct erofs_xattr_ibody_header) != 12);
297 BUILD_BUG_ON(sizeof(struct erofs_xattr_entry) != 4);
298 BUILD_BUG_ON(sizeof(struct z_erofs_map_header) != 8);
299 BUILD_BUG_ON(sizeof(struct z_erofs_vle_decompressed_index) != 8);
300 BUILD_BUG_ON(sizeof(struct erofs_dirent) != 12);
302 BUILD_BUG_ON(BIT(Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) <
303 Z_EROFS_VLE_CLUSTER_TYPE_MAX - 1);