1 /* SPDX-License-Identifier: GPL-2.0-only OR Apache-2.0 */
3 * linux/drivers/staging/erofs/erofs_fs.h
5 * Copyright (C) 2017-2018 HUAWEI, Inc.
6 * http://www.huawei.com/
7 * Created by Gao Xiang <gaoxiang25@huawei.com>
12 /* Enhanced(Extended) ROM File System */
13 #define EROFS_SUPER_MAGIC_V1 0xE0F5E1E2
14 #define EROFS_SUPER_OFFSET 1024
17 * Any bits that aren't in EROFS_ALL_REQUIREMENTS should be
18 * incompatible with this kernel version.
20 #define EROFS_REQUIREMENT_LZ4_0PADDING 0x00000001
21 #define EROFS_ALL_REQUIREMENTS EROFS_REQUIREMENT_LZ4_0PADDING
23 struct erofs_super_block {
24 /* 0 */__le32 magic; /* in the little endian */
25 /* 4 */__le32 checksum; /* crc32c(super_block) */
26 /* 8 */__le32 features; /* (aka. feature_compat) */
27 /* 12 */__u8 blkszbits; /* support block_size == PAGE_SIZE only */
28 /* 13 */__u8 reserved;
30 /* 14 */__le16 root_nid;
31 /* 16 */__le64 inos; /* total valid ino # (== f_files - f_favail) */
33 /* 24 */__le64 build_time; /* inode v1 time derivation */
34 /* 32 */__le32 build_time_nsec;
35 /* 36 */__le32 blocks; /* used for statfs */
36 /* 40 */__le32 meta_blkaddr;
37 /* 44 */__le32 xattr_blkaddr;
38 /* 48 */__u8 uuid[16]; /* 128-bit uuid for volume */
39 /* 64 */__u8 volume_name[16]; /* volume name */
40 /* 80 */__le32 requirements; /* (aka. feature_incompat) */
42 /* 84 */__u8 reserved2[44];
43 } __packed; /* 128 bytes */
46 * erofs inode data mapping:
47 * 0 - inode plain without inline data A:
48 * inode, [xattrs], ... | ... | no-holed data
49 * 1 - inode VLE compression B (legacy):
50 * inode, [xattrs], extents ... | ...
51 * 2 - inode plain with inline data C:
52 * inode, [xattrs], last_inline_data, ... | ... | no-holed data
53 * 3 - inode compression D:
54 * inode, [xattrs], map_header, extents ... | ...
58 EROFS_INODE_FLAT_PLAIN,
59 EROFS_INODE_FLAT_COMPRESSION_LEGACY,
60 EROFS_INODE_FLAT_INLINE,
61 EROFS_INODE_FLAT_COMPRESSION,
62 EROFS_INODE_LAYOUT_MAX
65 static inline bool erofs_inode_is_data_compressed(unsigned int datamode)
67 if (datamode == EROFS_INODE_FLAT_COMPRESSION)
69 return datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY;
72 /* bit definitions of inode i_advise */
73 #define EROFS_I_VERSION_BITS 1
74 #define EROFS_I_DATA_MAPPING_BITS 3
76 #define EROFS_I_VERSION_BIT 0
77 #define EROFS_I_DATA_MAPPING_BIT 1
79 struct erofs_inode_v1 {
80 /* 0 */__le16 i_advise;
82 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
83 /* 2 */__le16 i_xattr_icount;
85 /* 6 */__le16 i_nlink;
87 /* 12 */__le32 i_reserved;
89 /* file total compressed blocks for data mapping 1 */
90 __le32 compressed_blocks;
93 /* for device files, used to indicate old/new device # */
96 /* 20 */__le32 i_ino; /* only used for 32-bit stat compatibility */
99 /* 28 */__le32 i_reserved2;
102 /* 32 bytes on-disk inode */
103 #define EROFS_INODE_LAYOUT_V1 0
104 /* 64 bytes on-disk inode */
105 #define EROFS_INODE_LAYOUT_V2 1
107 struct erofs_inode_v2 {
108 /* 0 */__le16 i_advise;
110 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
111 /* 2 */__le16 i_xattr_icount;
112 /* 4 */__le16 i_mode;
113 /* 6 */__le16 i_reserved;
114 /* 8 */__le64 i_size;
116 /* file total compressed blocks for data mapping 1 */
117 __le32 compressed_blocks;
120 /* for device files, used to indicate old/new device # */
124 /* only used for 32-bit stat compatibility */
125 /* 20 */__le32 i_ino;
127 /* 24 */__le32 i_uid;
128 /* 28 */__le32 i_gid;
129 /* 32 */__le64 i_ctime;
130 /* 40 */__le32 i_ctime_nsec;
131 /* 44 */__le32 i_nlink;
132 /* 48 */__u8 i_reserved2[16];
133 } __packed; /* 64 bytes */
135 #define EROFS_MAX_SHARED_XATTRS (128)
136 /* h_shared_count between 129 ... 255 are special # */
137 #define EROFS_SHARED_XATTR_EXTENT (255)
140 * inline xattrs (n == i_xattr_icount):
141 * erofs_xattr_ibody_header(1) + (n - 1) * 4 bytes
144 * /-----------------------\
145 * | erofs_xattr_entries+ |
146 * +-----------------------+
147 * inline xattrs must starts in erofs_xattr_ibody_header,
148 * for read-only fs, no need to introduce h_refcount
150 struct erofs_xattr_ibody_header {
154 __le32 h_shared_xattrs[0]; /* shared xattr id array */
158 #define EROFS_XATTR_INDEX_USER 1
159 #define EROFS_XATTR_INDEX_POSIX_ACL_ACCESS 2
160 #define EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT 3
161 #define EROFS_XATTR_INDEX_TRUSTED 4
162 #define EROFS_XATTR_INDEX_LUSTRE 5
163 #define EROFS_XATTR_INDEX_SECURITY 6
165 /* xattr entry (for both inline & shared xattrs) */
166 struct erofs_xattr_entry {
167 __u8 e_name_len; /* length of name */
168 __u8 e_name_index; /* attribute name index */
169 __le16 e_value_size; /* size of attribute value */
170 /* followed by e_name and e_value */
171 char e_name[0]; /* attribute name */
174 #define ondisk_xattr_ibody_size(count) ({\
175 u32 __count = le16_to_cpu(count); \
176 ((__count) == 0) ? 0 : \
177 sizeof(struct erofs_xattr_ibody_header) + \
178 sizeof(__u32) * ((__count) - 1); })
180 #define EROFS_XATTR_ALIGN(size) round_up(size, sizeof(struct erofs_xattr_entry))
181 #define EROFS_XATTR_ENTRY_SIZE(entry) EROFS_XATTR_ALIGN( \
182 sizeof(struct erofs_xattr_entry) + \
183 (entry)->e_name_len + le16_to_cpu((entry)->e_value_size))
185 /* available compression algorithm types */
187 Z_EROFS_COMPRESSION_LZ4,
188 Z_EROFS_COMPRESSION_MAX
192 * bit 0 : COMPACTED_2B indexes (0 - off; 1 - on)
193 * e.g. for 4k logical cluster size, 4B if compacted 2B is off;
194 * (4B) + 2B + (4B) if compacted 2B is on.
196 #define Z_EROFS_ADVISE_COMPACTED_2B_BIT 0
198 #define Z_EROFS_ADVISE_COMPACTED_2B (1 << Z_EROFS_ADVISE_COMPACTED_2B_BIT)
200 struct z_erofs_map_header {
204 * bit 0-3 : algorithm type of head 1 (logical cluster type 01);
205 * bit 4-7 : algorithm type of head 2 (logical cluster type 11).
207 __u8 h_algorithmtype;
209 * bit 0-2 : logical cluster bits - 12, e.g. 0 for 4096;
210 * bit 3-4 : (physical - logical) cluster bits of head 1:
211 * For example, if logical clustersize = 4096, 1 for 8192.
212 * bit 5-7 : (physical - logical) cluster bits of head 2.
217 #define Z_EROFS_VLE_LEGACY_HEADER_PADDING 8
220 * Z_EROFS Variable-sized Logical Extent cluster type:
221 * 0 - literal (uncompressed) cluster
222 * 1 - compressed cluster (for the head logical cluster)
223 * 2 - compressed cluster (for the other logical clusters)
226 * 0 - literal (uncompressed) cluster,
228 * di_clusterofs = the literal data offset of the cluster
229 * di_blkaddr = the blkaddr of the literal cluster
231 * 1 - compressed cluster (for the head logical cluster)
233 * di_clusterofs = the decompressed data offset of the cluster
234 * di_blkaddr = the blkaddr of the compressed cluster
236 * 2 - compressed cluster (for the other logical clusters)
239 * the decompressed data offset in its own head cluster
240 * di_u.delta[0] = distance to its corresponding head cluster
241 * di_u.delta[1] = distance to its corresponding tail cluster
242 * (di_advise could be 0, 1 or 2)
245 Z_EROFS_VLE_CLUSTER_TYPE_PLAIN,
246 Z_EROFS_VLE_CLUSTER_TYPE_HEAD,
247 Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD,
248 Z_EROFS_VLE_CLUSTER_TYPE_RESERVED,
249 Z_EROFS_VLE_CLUSTER_TYPE_MAX
252 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS 2
253 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT 0
255 struct z_erofs_vle_decompressed_index {
257 /* where to decompress in the head cluster */
258 __le16 di_clusterofs;
261 /* for the head cluster */
264 * for the rest clusters
265 * eg. for 4k page-sized cluster, maximum 4K*64k = 256M)
266 * [0] - pointing to the head cluster
267 * [1] - pointing to the tail cluster
270 } di_u __packed; /* 8 bytes */
273 #define Z_EROFS_VLE_LEGACY_INDEX_ALIGN(size) \
274 (round_up(size, sizeof(struct z_erofs_vle_decompressed_index)) + \
275 sizeof(struct z_erofs_map_header) + Z_EROFS_VLE_LEGACY_HEADER_PADDING)
277 /* dirent sorts in alphabet order, thus we can do binary search */
278 struct erofs_dirent {
279 __le64 nid; /* 0, node number */
280 __le16 nameoff; /* 8, start offset of file name */
281 __u8 file_type; /* 10, file type */
282 __u8 reserved; /* 11, reserved */
286 * EROFS file types should match generic FT_* types and
287 * it seems no need to add BUILD_BUG_ONs since potential
288 * unmatchness will break other fses as well...
291 #define EROFS_NAME_LEN 255
293 /* check the EROFS on-disk layout strictly at compile time */
294 static inline void erofs_check_ondisk_layout_definitions(void)
296 BUILD_BUG_ON(sizeof(struct erofs_super_block) != 128);
297 BUILD_BUG_ON(sizeof(struct erofs_inode_v1) != 32);
298 BUILD_BUG_ON(sizeof(struct erofs_inode_v2) != 64);
299 BUILD_BUG_ON(sizeof(struct erofs_xattr_ibody_header) != 12);
300 BUILD_BUG_ON(sizeof(struct erofs_xattr_entry) != 4);
301 BUILD_BUG_ON(sizeof(struct z_erofs_map_header) != 8);
302 BUILD_BUG_ON(sizeof(struct z_erofs_vle_decompressed_index) != 8);
303 BUILD_BUG_ON(sizeof(struct erofs_dirent) != 12);
305 BUILD_BUG_ON(BIT(Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) <
306 Z_EROFS_VLE_CLUSTER_TYPE_MAX - 1);