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