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