1 /* SPDX-License-Identifier: GPL-2.0-only OR Apache-2.0 */
2 /*
3 * EROFS (Enhanced ROM File System) on-disk format definition
4 *
5 * Copyright (C) 2017-2018 HUAWEI, Inc.
6 * https://www.huawei.com/
7 * Created by Gao Xiang <gaoxiang25@huawei.com>
8 */
9 #ifndef __EROFS_FS_H
10 #define __EROFS_FS_H
11
12 #define EROFS_SUPER_OFFSET 1024
13
14 #define EROFS_FEATURE_COMPAT_SB_CHKSUM 0x00000001
15
16 /*
17 * Any bits that aren't in EROFS_ALL_FEATURE_INCOMPAT should
18 * be incompatible with this kernel version.
19 */
20 #define EROFS_FEATURE_INCOMPAT_LZ4_0PADDING 0x00000001
21 #define EROFS_ALL_FEATURE_INCOMPAT EROFS_FEATURE_INCOMPAT_LZ4_0PADDING
22
23 /* 128-byte erofs on-disk super block */
24 struct erofs_super_block {
25 __le32 magic; /* file system magic number */
26 __le32 checksum; /* crc32c(super_block) */
27 __le32 feature_compat;
28 __u8 blkszbits; /* support block_size == PAGE_SIZE only */
29 __u8 reserved;
30
31 __le16 root_nid; /* nid of root directory */
32 __le64 inos; /* total valid ino # (== f_files - f_favail) */
33
34 __le64 build_time; /* inode v1 time derivation */
35 __le32 build_time_nsec; /* inode v1 time derivation in nano scale */
36 __le32 blocks; /* used for statfs */
37 __le32 meta_blkaddr; /* start block address of metadata area */
38 __le32 xattr_blkaddr; /* start block address of shared xattr area */
39 __u8 uuid[16]; /* 128-bit uuid for volume */
40 __u8 volume_name[16]; /* volume name */
41 __le32 feature_incompat;
42 __u8 reserved2[44];
43 };
44
45 /*
46 * erofs inode datalayout (i_format in on-disk inode):
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 ... | ...
55 * 4~7 - reserved
56 */
57 enum {
58 EROFS_INODE_FLAT_PLAIN = 0,
59 EROFS_INODE_FLAT_COMPRESSION_LEGACY = 1,
60 EROFS_INODE_FLAT_INLINE = 2,
61 EROFS_INODE_FLAT_COMPRESSION = 3,
62 EROFS_INODE_DATALAYOUT_MAX
63 };
64
erofs_inode_is_data_compressed(unsigned int datamode)65 static inline bool erofs_inode_is_data_compressed(unsigned int datamode)
66 {
67 return datamode == EROFS_INODE_FLAT_COMPRESSION ||
68 datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY;
69 }
70
71 /* bit definitions of inode i_advise */
72 #define EROFS_I_VERSION_BITS 1
73 #define EROFS_I_DATALAYOUT_BITS 3
74
75 #define EROFS_I_VERSION_BIT 0
76 #define EROFS_I_DATALAYOUT_BIT 1
77
78 #define EROFS_I_ALL \
79 ((1 << (EROFS_I_DATALAYOUT_BIT + EROFS_I_DATALAYOUT_BITS)) - 1)
80
81 /* 32-byte reduced form of an ondisk inode */
82 struct erofs_inode_compact {
83 __le16 i_format; /* inode format hints */
84
85 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
86 __le16 i_xattr_icount;
87 __le16 i_mode;
88 __le16 i_nlink;
89 __le32 i_size;
90 __le32 i_reserved;
91 union {
92 /* file total compressed blocks for data mapping 1 */
93 __le32 compressed_blocks;
94 __le32 raw_blkaddr;
95
96 /* for device files, used to indicate old/new device # */
97 __le32 rdev;
98 } i_u;
99 __le32 i_ino; /* only used for 32-bit stat compatibility */
100 __le16 i_uid;
101 __le16 i_gid;
102 __le32 i_reserved2;
103 };
104
105 /* 32 bytes on-disk inode */
106 #define EROFS_INODE_LAYOUT_COMPACT 0
107 /* 64 bytes on-disk inode */
108 #define EROFS_INODE_LAYOUT_EXTENDED 1
109
110 /* 64-byte complete form of an ondisk inode */
111 struct erofs_inode_extended {
112 __le16 i_format; /* inode format hints */
113
114 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
115 __le16 i_xattr_icount;
116 __le16 i_mode;
117 __le16 i_reserved;
118 __le64 i_size;
119 union {
120 /* file total compressed blocks for data mapping 1 */
121 __le32 compressed_blocks;
122 __le32 raw_blkaddr;
123
124 /* for device files, used to indicate old/new device # */
125 __le32 rdev;
126 } i_u;
127
128 /* only used for 32-bit stat compatibility */
129 __le32 i_ino;
130
131 __le32 i_uid;
132 __le32 i_gid;
133 __le64 i_ctime;
134 __le32 i_ctime_nsec;
135 __le32 i_nlink;
136 __u8 i_reserved2[16];
137 };
138
139 #define EROFS_MAX_SHARED_XATTRS (128)
140 /* h_shared_count between 129 ... 255 are special # */
141 #define EROFS_SHARED_XATTR_EXTENT (255)
142
143 /*
144 * inline xattrs (n == i_xattr_icount):
145 * erofs_xattr_ibody_header(1) + (n - 1) * 4 bytes
146 * 12 bytes / \
147 * / \
148 * /-----------------------\
149 * | erofs_xattr_entries+ |
150 * +-----------------------+
151 * inline xattrs must starts in erofs_xattr_ibody_header,
152 * for read-only fs, no need to introduce h_refcount
153 */
154 struct erofs_xattr_ibody_header {
155 __le32 h_reserved;
156 __u8 h_shared_count;
157 __u8 h_reserved2[7];
158 __le32 h_shared_xattrs[0]; /* shared xattr id array */
159 };
160
161 /* Name indexes */
162 #define EROFS_XATTR_INDEX_USER 1
163 #define EROFS_XATTR_INDEX_POSIX_ACL_ACCESS 2
164 #define EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT 3
165 #define EROFS_XATTR_INDEX_TRUSTED 4
166 #define EROFS_XATTR_INDEX_LUSTRE 5
167 #define EROFS_XATTR_INDEX_SECURITY 6
168
169 /* xattr entry (for both inline & shared xattrs) */
170 struct erofs_xattr_entry {
171 __u8 e_name_len; /* length of name */
172 __u8 e_name_index; /* attribute name index */
173 __le16 e_value_size; /* size of attribute value */
174 /* followed by e_name and e_value */
175 char e_name[0]; /* attribute name */
176 };
177
erofs_xattr_ibody_size(__le16 i_xattr_icount)178 static inline unsigned int erofs_xattr_ibody_size(__le16 i_xattr_icount)
179 {
180 if (!i_xattr_icount)
181 return 0;
182
183 return sizeof(struct erofs_xattr_ibody_header) +
184 sizeof(__u32) * (le16_to_cpu(i_xattr_icount) - 1);
185 }
186
187 #define EROFS_XATTR_ALIGN(size) round_up(size, sizeof(struct erofs_xattr_entry))
188
erofs_xattr_entry_size(struct erofs_xattr_entry * e)189 static inline unsigned int erofs_xattr_entry_size(struct erofs_xattr_entry *e)
190 {
191 return EROFS_XATTR_ALIGN(sizeof(struct erofs_xattr_entry) +
192 e->e_name_len + le16_to_cpu(e->e_value_size));
193 }
194
195 /* available compression algorithm types (for h_algorithmtype) */
196 enum {
197 Z_EROFS_COMPRESSION_LZ4 = 0,
198 Z_EROFS_COMPRESSION_MAX
199 };
200
201 /*
202 * bit 0 : COMPACTED_2B indexes (0 - off; 1 - on)
203 * e.g. for 4k logical cluster size, 4B if compacted 2B is off;
204 * (4B) + 2B + (4B) if compacted 2B is on.
205 */
206 #define Z_EROFS_ADVISE_COMPACTED_2B_BIT 0
207
208 #define Z_EROFS_ADVISE_COMPACTED_2B (1 << Z_EROFS_ADVISE_COMPACTED_2B_BIT)
209
210 struct z_erofs_map_header {
211 __le32 h_reserved1;
212 __le16 h_advise;
213 /*
214 * bit 0-3 : algorithm type of head 1 (logical cluster type 01);
215 * bit 4-7 : algorithm type of head 2 (logical cluster type 11).
216 */
217 __u8 h_algorithmtype;
218 /*
219 * bit 0-2 : logical cluster bits - 12, e.g. 0 for 4096;
220 * bit 3-4 : (physical - logical) cluster bits of head 1:
221 * For example, if logical clustersize = 4096, 1 for 8192.
222 * bit 5-7 : (physical - logical) cluster bits of head 2.
223 */
224 __u8 h_clusterbits;
225 };
226
227 #define Z_EROFS_VLE_LEGACY_HEADER_PADDING 8
228
229 /*
230 * Fixed-sized output compression ondisk Logical Extent cluster type:
231 * 0 - literal (uncompressed) cluster
232 * 1 - compressed cluster (for the head logical cluster)
233 * 2 - compressed cluster (for the other logical clusters)
234 *
235 * In detail,
236 * 0 - literal (uncompressed) cluster,
237 * di_advise = 0
238 * di_clusterofs = the literal data offset of the cluster
239 * di_blkaddr = the blkaddr of the literal cluster
240 *
241 * 1 - compressed cluster (for the head logical cluster)
242 * di_advise = 1
243 * di_clusterofs = the decompressed data offset of the cluster
244 * di_blkaddr = the blkaddr of the compressed cluster
245 *
246 * 2 - compressed cluster (for the other logical clusters)
247 * di_advise = 2
248 * di_clusterofs =
249 * the decompressed data offset in its own head cluster
250 * di_u.delta[0] = distance to its corresponding head cluster
251 * di_u.delta[1] = distance to its corresponding tail cluster
252 * (di_advise could be 0, 1 or 2)
253 */
254 enum {
255 Z_EROFS_VLE_CLUSTER_TYPE_PLAIN = 0,
256 Z_EROFS_VLE_CLUSTER_TYPE_HEAD = 1,
257 Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD = 2,
258 Z_EROFS_VLE_CLUSTER_TYPE_RESERVED = 3,
259 Z_EROFS_VLE_CLUSTER_TYPE_MAX
260 };
261
262 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS 2
263 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT 0
264
265 struct z_erofs_vle_decompressed_index {
266 __le16 di_advise;
267 /* where to decompress in the head cluster */
268 __le16 di_clusterofs;
269
270 union {
271 /* for the head cluster */
272 __le32 blkaddr;
273 /*
274 * for the rest clusters
275 * eg. for 4k page-sized cluster, maximum 4K*64k = 256M)
276 * [0] - pointing to the head cluster
277 * [1] - pointing to the tail cluster
278 */
279 __le16 delta[2];
280 } di_u;
281 };
282
283 #define Z_EROFS_VLE_LEGACY_INDEX_ALIGN(size) \
284 (round_up(size, sizeof(struct z_erofs_vle_decompressed_index)) + \
285 sizeof(struct z_erofs_map_header) + Z_EROFS_VLE_LEGACY_HEADER_PADDING)
286
287 /* dirent sorts in alphabet order, thus we can do binary search */
288 struct erofs_dirent {
289 __le64 nid; /* node number */
290 __le16 nameoff; /* start offset of file name */
291 __u8 file_type; /* file type */
292 __u8 reserved; /* reserved */
293 } __packed;
294
295 /*
296 * EROFS file types should match generic FT_* types and
297 * it seems no need to add BUILD_BUG_ONs since potential
298 * unmatchness will break other fses as well...
299 */
300
301 #define EROFS_NAME_LEN 255
302
303 /* check the EROFS on-disk layout strictly at compile time */
erofs_check_ondisk_layout_definitions(void)304 static inline void erofs_check_ondisk_layout_definitions(void)
305 {
306 BUILD_BUG_ON(sizeof(struct erofs_super_block) != 128);
307 BUILD_BUG_ON(sizeof(struct erofs_inode_compact) != 32);
308 BUILD_BUG_ON(sizeof(struct erofs_inode_extended) != 64);
309 BUILD_BUG_ON(sizeof(struct erofs_xattr_ibody_header) != 12);
310 BUILD_BUG_ON(sizeof(struct erofs_xattr_entry) != 4);
311 BUILD_BUG_ON(sizeof(struct z_erofs_map_header) != 8);
312 BUILD_BUG_ON(sizeof(struct z_erofs_vle_decompressed_index) != 8);
313 BUILD_BUG_ON(sizeof(struct erofs_dirent) != 12);
314
315 BUILD_BUG_ON(BIT(Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) <
316 Z_EROFS_VLE_CLUSTER_TYPE_MAX - 1);
317 }
318
319 #endif
320
321