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 * Copyright (C) 2021, Alibaba Cloud
8 */
9 #ifndef __EROFS_FS_H
10 #define __EROFS_FS_H
11
12 #define EROFS_SUPER_MAGIC_V1 0xE0F5E1E2
13 #define EROFS_SUPER_OFFSET 1024
14
15 #define EROFS_FEATURE_COMPAT_SB_CHKSUM 0x00000001
16 #define EROFS_FEATURE_COMPAT_MTIME 0x00000002
17 #define EROFS_FEATURE_COMPAT_XATTR_FILTER 0x00000004
18
19 /*
20 * Any bits that aren't in EROFS_ALL_FEATURE_INCOMPAT should
21 * be incompatible with this kernel version.
22 */
23 #define EROFS_FEATURE_INCOMPAT_ZERO_PADDING 0x00000001
24 #define EROFS_FEATURE_INCOMPAT_COMPR_CFGS 0x00000002
25 #define EROFS_FEATURE_INCOMPAT_BIG_PCLUSTER 0x00000002
26 #define EROFS_FEATURE_INCOMPAT_CHUNKED_FILE 0x00000004
27 #define EROFS_FEATURE_INCOMPAT_DEVICE_TABLE 0x00000008
28 #define EROFS_FEATURE_INCOMPAT_COMPR_HEAD2 0x00000008
29 #define EROFS_FEATURE_INCOMPAT_ZTAILPACKING 0x00000010
30 #define EROFS_FEATURE_INCOMPAT_FRAGMENTS 0x00000020
31 #define EROFS_FEATURE_INCOMPAT_DEDUPE 0x00000020
32 #define EROFS_FEATURE_INCOMPAT_XATTR_PREFIXES 0x00000040
33 #define EROFS_ALL_FEATURE_INCOMPAT \
34 (EROFS_FEATURE_INCOMPAT_ZERO_PADDING | \
35 EROFS_FEATURE_INCOMPAT_COMPR_CFGS | \
36 EROFS_FEATURE_INCOMPAT_BIG_PCLUSTER | \
37 EROFS_FEATURE_INCOMPAT_CHUNKED_FILE | \
38 EROFS_FEATURE_INCOMPAT_DEVICE_TABLE | \
39 EROFS_FEATURE_INCOMPAT_COMPR_HEAD2 | \
40 EROFS_FEATURE_INCOMPAT_ZTAILPACKING | \
41 EROFS_FEATURE_INCOMPAT_FRAGMENTS | \
42 EROFS_FEATURE_INCOMPAT_DEDUPE | \
43 EROFS_FEATURE_INCOMPAT_XATTR_PREFIXES)
44
45 #define EROFS_SB_EXTSLOT_SIZE 16
46
47 struct erofs_deviceslot {
48 u8 tag[64]; /* digest(sha256), etc. */
49 __le32 blocks; /* total fs blocks of this device */
50 __le32 mapped_blkaddr; /* map starting at mapped_blkaddr */
51 u8 reserved[56];
52 };
53 #define EROFS_DEVT_SLOT_SIZE sizeof(struct erofs_deviceslot)
54
55 /* erofs on-disk super block (currently 128 bytes) */
56 struct erofs_super_block {
57 __le32 magic; /* file system magic number */
58 __le32 checksum; /* crc32c(super_block) */
59 __le32 feature_compat;
60 __u8 blkszbits; /* filesystem block size in bit shift */
61 __u8 sb_extslots; /* superblock size = 128 + sb_extslots * 16 */
62
63 __le16 root_nid; /* nid of root directory */
64 __le64 inos; /* total valid ino # (== f_files - f_favail) */
65
66 __le64 build_time; /* compact inode time derivation */
67 __le32 build_time_nsec; /* compact inode time derivation in ns scale */
68 __le32 blocks; /* used for statfs */
69 __le32 meta_blkaddr; /* start block address of metadata area */
70 __le32 xattr_blkaddr; /* start block address of shared xattr area */
71 __u8 uuid[16]; /* 128-bit uuid for volume */
72 __u8 volume_name[16]; /* volume name */
73 __le32 feature_incompat;
74 union {
75 /* bitmap for available compression algorithms */
76 __le16 available_compr_algs;
77 /* customized sliding window size instead of 64k by default */
78 __le16 lz4_max_distance;
79 } __packed u1;
80 __le16 extra_devices; /* # of devices besides the primary device */
81 __le16 devt_slotoff; /* startoff = devt_slotoff * devt_slotsize */
82 __u8 dirblkbits; /* directory block size in bit shift */
83 __u8 xattr_prefix_count; /* # of long xattr name prefixes */
84 __le32 xattr_prefix_start; /* start of long xattr prefixes */
85 __le64 packed_nid; /* nid of the special packed inode */
86 __u8 xattr_filter_reserved; /* reserved for xattr name filter */
87 __u8 reserved2[23];
88 };
89
90 /*
91 * EROFS inode datalayout (i_format in on-disk inode):
92 * 0 - uncompressed flat inode without tail-packing inline data:
93 * 1 - compressed inode with non-compact indexes:
94 * 2 - uncompressed flat inode with tail-packing inline data:
95 * 3 - compressed inode with compact indexes:
96 * 4 - chunk-based inode with (optional) multi-device support:
97 * 5~7 - reserved
98 */
99 enum {
100 EROFS_INODE_FLAT_PLAIN = 0,
101 EROFS_INODE_COMPRESSED_FULL = 1,
102 EROFS_INODE_FLAT_INLINE = 2,
103 EROFS_INODE_COMPRESSED_COMPACT = 3,
104 EROFS_INODE_CHUNK_BASED = 4,
105 EROFS_INODE_DATALAYOUT_MAX
106 };
107
erofs_inode_is_data_compressed(unsigned int datamode)108 static inline bool erofs_inode_is_data_compressed(unsigned int datamode)
109 {
110 return datamode == EROFS_INODE_COMPRESSED_COMPACT ||
111 datamode == EROFS_INODE_COMPRESSED_FULL;
112 }
113
114 /* bit definitions of inode i_format */
115 #define EROFS_I_VERSION_BITS 1
116 #define EROFS_I_DATALAYOUT_BITS 3
117
118 #define EROFS_I_VERSION_BIT 0
119 #define EROFS_I_DATALAYOUT_BIT 1
120
121 #define EROFS_I_ALL \
122 ((1 << (EROFS_I_DATALAYOUT_BIT + EROFS_I_DATALAYOUT_BITS)) - 1)
123
124 /* indicate chunk blkbits, thus 'chunksize = blocksize << chunk blkbits' */
125 #define EROFS_CHUNK_FORMAT_BLKBITS_MASK 0x001F
126 /* with chunk indexes or just a 4-byte blkaddr array */
127 #define EROFS_CHUNK_FORMAT_INDEXES 0x0020
128
129 #define EROFS_CHUNK_FORMAT_ALL \
130 (EROFS_CHUNK_FORMAT_BLKBITS_MASK | EROFS_CHUNK_FORMAT_INDEXES)
131
132 /* 32-byte on-disk inode */
133 #define EROFS_INODE_LAYOUT_COMPACT 0
134 /* 64-byte on-disk inode */
135 #define EROFS_INODE_LAYOUT_EXTENDED 1
136
137 struct erofs_inode_chunk_info {
138 __le16 format; /* chunk blkbits, etc. */
139 __le16 reserved;
140 };
141
142 union erofs_inode_i_u {
143 /* total compressed blocks for compressed inodes */
144 __le32 compressed_blocks;
145
146 /* block address for uncompressed flat inodes */
147 __le32 raw_blkaddr;
148
149 /* for device files, used to indicate old/new device # */
150 __le32 rdev;
151
152 /* for chunk-based files, it contains the summary info */
153 struct erofs_inode_chunk_info c;
154 };
155
156 /* 32-byte reduced form of an ondisk inode */
157 struct erofs_inode_compact {
158 __le16 i_format; /* inode format hints */
159
160 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
161 __le16 i_xattr_icount;
162 __le16 i_mode;
163 __le16 i_nlink;
164 __le32 i_size;
165 __le32 i_reserved;
166 union erofs_inode_i_u i_u;
167
168 __le32 i_ino; /* only used for 32-bit stat compatibility */
169 __le16 i_uid;
170 __le16 i_gid;
171 __le32 i_reserved2;
172 };
173
174 /* 64-byte complete form of an ondisk inode */
175 struct erofs_inode_extended {
176 __le16 i_format; /* inode format hints */
177
178 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
179 __le16 i_xattr_icount;
180 __le16 i_mode;
181 __le16 i_reserved;
182 __le64 i_size;
183 union erofs_inode_i_u i_u;
184
185 __le32 i_ino; /* only used for 32-bit stat compatibility */
186 __le32 i_uid;
187 __le32 i_gid;
188 __le64 i_mtime;
189 __le32 i_mtime_nsec;
190 __le32 i_nlink;
191 __u8 i_reserved2[16];
192 };
193
194 /*
195 * inline xattrs (n == i_xattr_icount):
196 * erofs_xattr_ibody_header(1) + (n - 1) * 4 bytes
197 * 12 bytes / \
198 * / \
199 * /-----------------------\
200 * | erofs_xattr_entries+ |
201 * +-----------------------+
202 * inline xattrs must starts in erofs_xattr_ibody_header,
203 * for read-only fs, no need to introduce h_refcount
204 */
205 struct erofs_xattr_ibody_header {
206 __le32 h_name_filter; /* bit value 1 indicates not-present */
207 __u8 h_shared_count;
208 __u8 h_reserved2[7];
209 __le32 h_shared_xattrs[0]; /* shared xattr id array */
210 };
211
212 /* Name indexes */
213 #define EROFS_XATTR_INDEX_USER 1
214 #define EROFS_XATTR_INDEX_POSIX_ACL_ACCESS 2
215 #define EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT 3
216 #define EROFS_XATTR_INDEX_TRUSTED 4
217 #define EROFS_XATTR_INDEX_LUSTRE 5
218 #define EROFS_XATTR_INDEX_SECURITY 6
219
220 /*
221 * bit 7 of e_name_index is set when it refers to a long xattr name prefix,
222 * while the remained lower bits represent the index of the prefix.
223 */
224 #define EROFS_XATTR_LONG_PREFIX 0x80
225 #define EROFS_XATTR_LONG_PREFIX_MASK 0x7f
226
227 #define EROFS_XATTR_FILTER_BITS 32
228 #define EROFS_XATTR_FILTER_DEFAULT UINT32_MAX
229 #define EROFS_XATTR_FILTER_SEED 0x25BBE08F
230
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[0]; /* attribute name */
238 };
239
240 /* long xattr name prefix */
241 struct erofs_xattr_long_prefix {
242 __u8 base_index; /* short xattr name prefix index */
243 char infix[0]; /* infix apart from short prefix */
244 };
245
erofs_xattr_ibody_size(__le16 i_xattr_icount)246 static inline unsigned int erofs_xattr_ibody_size(__le16 i_xattr_icount)
247 {
248 if (!i_xattr_icount)
249 return 0;
250
251 return sizeof(struct erofs_xattr_ibody_header) +
252 sizeof(__u32) * (le16_to_cpu(i_xattr_icount) - 1);
253 }
254
255 #define EROFS_XATTR_ALIGN(size) round_up(size, sizeof(struct erofs_xattr_entry))
256
erofs_xattr_entry_size(struct erofs_xattr_entry * e)257 static inline unsigned int erofs_xattr_entry_size(struct erofs_xattr_entry *e)
258 {
259 return EROFS_XATTR_ALIGN(sizeof(struct erofs_xattr_entry) +
260 e->e_name_len + le16_to_cpu(e->e_value_size));
261 }
262
263 /* represent a zeroed chunk (hole) */
264 #define EROFS_NULL_ADDR -1
265
266 /* 4-byte block address array */
267 #define EROFS_BLOCK_MAP_ENTRY_SIZE sizeof(__le32)
268
269 /* 8-byte inode chunk indexes */
270 struct erofs_inode_chunk_index {
271 __le16 advise; /* always 0, don't care for now */
272 __le16 device_id; /* back-end storage id (with bits masked) */
273 __le32 blkaddr; /* start block address of this inode chunk */
274 };
275
276 /* dirent sorts in alphabet order, thus we can do binary search */
277 struct erofs_dirent {
278 __le64 nid; /* node number */
279 __le16 nameoff; /* start offset of file name */
280 __u8 file_type; /* file type */
281 __u8 reserved; /* reserved */
282 } __packed;
283
284 /* file types used in inode_info->flags */
285 enum {
286 EROFS_FT_UNKNOWN,
287 EROFS_FT_REG_FILE,
288 EROFS_FT_DIR,
289 EROFS_FT_CHRDEV,
290 EROFS_FT_BLKDEV,
291 EROFS_FT_FIFO,
292 EROFS_FT_SOCK,
293 EROFS_FT_SYMLINK,
294 EROFS_FT_MAX
295 };
296
297 #define EROFS_NAME_LEN 255
298
299 /* maximum supported size of a physical compression cluster */
300 #define Z_EROFS_PCLUSTER_MAX_SIZE (1024 * 1024)
301
302 /* available compression algorithm types (for h_algorithmtype) */
303 enum {
304 Z_EROFS_COMPRESSION_LZ4 = 0,
305 Z_EROFS_COMPRESSION_LZMA = 1,
306 Z_EROFS_COMPRESSION_DEFLATE = 2,
307 Z_EROFS_COMPRESSION_ZSTD = 3,
308 Z_EROFS_COMPRESSION_MAX
309 };
310 #define Z_EROFS_ALL_COMPR_ALGS ((1 << Z_EROFS_COMPRESSION_MAX) - 1)
311
312 /* 14 bytes (+ length field = 16 bytes) */
313 struct z_erofs_lz4_cfgs {
314 __le16 max_distance;
315 __le16 max_pclusterblks;
316 u8 reserved[10];
317 } __packed;
318
319 /* 14 bytes (+ length field = 16 bytes) */
320 struct z_erofs_lzma_cfgs {
321 __le32 dict_size;
322 __le16 format;
323 u8 reserved[8];
324 } __packed;
325
326 #define Z_EROFS_LZMA_MAX_DICT_SIZE (8 * Z_EROFS_PCLUSTER_MAX_SIZE)
327
328 /* 6 bytes (+ length field = 8 bytes) */
329 struct z_erofs_deflate_cfgs {
330 u8 windowbits; /* 8..15 for DEFLATE */
331 u8 reserved[5];
332 } __packed;
333
334 /* 6 bytes (+ length field = 8 bytes) */
335 struct z_erofs_zstd_cfgs {
336 u8 format;
337 u8 windowlog; /* windowLog - ZSTD_WINDOWLOG_ABSOLUTEMIN(10) */
338 u8 reserved[4];
339 } __packed;
340
341 #define Z_EROFS_ZSTD_MAX_DICT_SIZE Z_EROFS_PCLUSTER_MAX_SIZE
342
343 /*
344 * bit 0 : COMPACTED_2B indexes (0 - off; 1 - on)
345 * e.g. for 4k logical cluster size, 4B if compacted 2B is off;
346 * (4B) + 2B + (4B) if compacted 2B is on.
347 * bit 1 : HEAD1 big pcluster (0 - off; 1 - on)
348 * bit 2 : HEAD2 big pcluster (0 - off; 1 - on)
349 * bit 3 : tailpacking inline pcluster (0 - off; 1 - on)
350 * bit 4 : interlaced plain pcluster (0 - off; 1 - on)
351 * bit 5 : fragment pcluster (0 - off; 1 - on)
352 */
353 #define Z_EROFS_ADVISE_COMPACTED_2B 0x0001
354 #define Z_EROFS_ADVISE_BIG_PCLUSTER_1 0x0002
355 #define Z_EROFS_ADVISE_BIG_PCLUSTER_2 0x0004
356 #define Z_EROFS_ADVISE_INLINE_PCLUSTER 0x0008
357 #define Z_EROFS_ADVISE_INTERLACED_PCLUSTER 0x0010
358 #define Z_EROFS_ADVISE_FRAGMENT_PCLUSTER 0x0020
359
360 #define Z_EROFS_FRAGMENT_INODE_BIT 7
361 struct z_erofs_map_header {
362 union {
363 /* fragment data offset in the packed inode */
364 __le32 h_fragmentoff;
365 struct {
366 __le16 h_reserved1;
367 /* indicates the encoded size of tailpacking data */
368 __le16 h_idata_size;
369 };
370 };
371 __le16 h_advise;
372 /*
373 * bit 0-3 : algorithm type of head 1 (logical cluster type 01);
374 * bit 4-7 : algorithm type of head 2 (logical cluster type 11).
375 */
376 __u8 h_algorithmtype;
377 /*
378 * bit 0-2 : logical cluster bits - 12, e.g. 0 for 4096;
379 * bit 3-6 : reserved;
380 * bit 7 : move the whole file into packed inode or not.
381 */
382 __u8 h_clusterbits;
383 };
384
385 /*
386 * On-disk logical cluster type:
387 * 0 - literal (uncompressed) lcluster
388 * 1,3 - compressed lcluster (for HEAD lclusters)
389 * 2 - compressed lcluster (for NONHEAD lclusters)
390 *
391 * In detail,
392 * 0 - literal (uncompressed) lcluster,
393 * di_advise = 0
394 * di_clusterofs = the literal data offset of the lcluster
395 * di_blkaddr = the blkaddr of the literal pcluster
396 *
397 * 1,3 - compressed lcluster (for HEAD lclusters)
398 * di_advise = 1 or 3
399 * di_clusterofs = the decompressed data offset of the lcluster
400 * di_blkaddr = the blkaddr of the compressed pcluster
401 *
402 * 2 - compressed lcluster (for NONHEAD lclusters)
403 * di_advise = 2
404 * di_clusterofs =
405 * the decompressed data offset in its own HEAD lcluster
406 * di_u.delta[0] = distance to this HEAD lcluster
407 * di_u.delta[1] = distance to the next HEAD lcluster
408 */
409 enum {
410 Z_EROFS_LCLUSTER_TYPE_PLAIN = 0,
411 Z_EROFS_LCLUSTER_TYPE_HEAD1 = 1,
412 Z_EROFS_LCLUSTER_TYPE_NONHEAD = 2,
413 Z_EROFS_LCLUSTER_TYPE_HEAD2 = 3,
414 Z_EROFS_LCLUSTER_TYPE_MAX
415 };
416
417 #define Z_EROFS_LI_LCLUSTER_TYPE_BITS 2
418 #define Z_EROFS_LI_LCLUSTER_TYPE_BIT 0
419
420 /* (noncompact only, HEAD) This pcluster refers to partial decompressed data */
421 #define Z_EROFS_LI_PARTIAL_REF (1 << 15)
422
423 /*
424 * D0_CBLKCNT will be marked _only_ at the 1st non-head lcluster to store the
425 * compressed block count of a compressed extent (in logical clusters, aka.
426 * block count of a pcluster).
427 */
428 #define Z_EROFS_LI_D0_CBLKCNT (1 << 11)
429
430 struct z_erofs_lcluster_index {
431 __le16 di_advise;
432 /* where to decompress in the head lcluster */
433 __le16 di_clusterofs;
434
435 union {
436 /* for the HEAD lclusters */
437 __le32 blkaddr;
438 /*
439 * for the NONHEAD lclusters
440 * [0] - distance to its HEAD lcluster
441 * [1] - distance to the next HEAD lcluster
442 */
443 __le16 delta[2];
444 } di_u;
445 };
446
447 #define Z_EROFS_FULL_INDEX_ALIGN(end) \
448 (round_up(end, 8) + sizeof(struct z_erofs_map_header) + 8)
449
450 /* check the EROFS on-disk layout strictly at compile time */
erofs_check_ondisk_layout_definitions(void)451 static inline void erofs_check_ondisk_layout_definitions(void)
452 {
453 #ifndef __cplusplus
454 const union {
455 struct z_erofs_map_header h;
456 __le64 v;
457 } fmh __maybe_unused = {
458 .h.h_clusterbits = 1 << Z_EROFS_FRAGMENT_INODE_BIT,
459 };
460 #endif
461
462 BUILD_BUG_ON(sizeof(struct erofs_super_block) != 128);
463 BUILD_BUG_ON(sizeof(struct erofs_inode_compact) != 32);
464 BUILD_BUG_ON(sizeof(struct erofs_inode_extended) != 64);
465 BUILD_BUG_ON(sizeof(struct erofs_xattr_ibody_header) != 12);
466 BUILD_BUG_ON(sizeof(struct erofs_xattr_entry) != 4);
467 BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_info) != 4);
468 BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_index) != 8);
469 BUILD_BUG_ON(sizeof(struct z_erofs_map_header) != 8);
470 BUILD_BUG_ON(sizeof(struct z_erofs_lcluster_index) != 8);
471 BUILD_BUG_ON(sizeof(struct erofs_dirent) != 12);
472 /* keep in sync between 2 index structures for better extendibility */
473 BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_index) !=
474 sizeof(struct z_erofs_lcluster_index));
475 BUILD_BUG_ON(sizeof(struct erofs_deviceslot) != 128);
476
477 BUILD_BUG_ON(BIT(Z_EROFS_LI_LCLUSTER_TYPE_BITS) <
478 Z_EROFS_LCLUSTER_TYPE_MAX - 1);
479 #ifndef __cplusplus
480 /* exclude old compiler versions like gcc 7.5.0 */
481 BUILD_BUG_ON(__builtin_constant_p(fmh.v) ?
482 fmh.v != cpu_to_le64(1ULL << 63) : 0);
483 #endif
484 }
485
486 #endif
487