xref: /linux/fs/erofs/erofs_fs.h (revision 6f7e6393d1ce636bb7ec77a7fe7b77458fddf701)
1 /* SPDX-License-Identifier: MIT */
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 /* to allow for x86 boot sectors and other oddities. */
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 #define EROFS_FEATURE_COMPAT_SHARED_EA_IN_METABOX	0x00000008
19 #define EROFS_FEATURE_COMPAT_PLAIN_XATTR_PFX		0x00000010
20 #define EROFS_FEATURE_COMPAT_ISHARE_XATTRS		0x00000020
21 
22 /*
23  * Any bits that aren't in EROFS_ALL_FEATURE_INCOMPAT should
24  * be incompatible with this kernel version.
25  */
26 #define EROFS_FEATURE_INCOMPAT_LZ4_0PADDING	0x00000001
27 #define EROFS_FEATURE_INCOMPAT_COMPR_CFGS	0x00000002
28 #define EROFS_FEATURE_INCOMPAT_BIG_PCLUSTER	0x00000002
29 #define EROFS_FEATURE_INCOMPAT_CHUNKED_FILE	0x00000004
30 #define EROFS_FEATURE_INCOMPAT_DEVICE_TABLE	0x00000008
31 #define EROFS_FEATURE_INCOMPAT_COMPR_HEAD2	0x00000008
32 #define EROFS_FEATURE_INCOMPAT_ZTAILPACKING	0x00000010
33 #define EROFS_FEATURE_INCOMPAT_FRAGMENTS	0x00000020
34 #define EROFS_FEATURE_INCOMPAT_DEDUPE		0x00000020
35 #define EROFS_FEATURE_INCOMPAT_XATTR_PREFIXES	0x00000040
36 #define EROFS_FEATURE_INCOMPAT_48BIT		0x00000080
37 #define EROFS_FEATURE_INCOMPAT_METABOX		0x00000100
38 #define EROFS_ALL_FEATURE_INCOMPAT		\
39 	((EROFS_FEATURE_INCOMPAT_METABOX << 1) - 1)
40 
41 #define EROFS_SB_EXTSLOT_SIZE	16
42 
43 struct erofs_deviceslot {
44 	u8 tag[64];		/* digest(sha256), etc. */
45 	__le32 blocks_lo;	/* total blocks count of this device */
46 	__le32 uniaddr_lo;	/* unified starting block of this device */
47 	__le32 blocks_hi;	/* total blocks count MSB */
48 	__le16 uniaddr_hi;	/* unified starting block MSB */
49 	u8 reserved[50];
50 };
51 #define EROFS_DEVT_SLOT_SIZE	sizeof(struct erofs_deviceslot)
52 
53 /* erofs on-disk super block (currently 144 bytes at maximum) */
54 struct erofs_super_block {
55 	__le32 magic;           /* file system magic number */
56 	__le32 checksum;        /* crc32c to avoid unexpected on-disk overlap */
57 	__le32 feature_compat;
58 	__u8 blkszbits;         /* filesystem block size in bit shift */
59 	__u8 sb_extslots;	/* superblock size = 128 + sb_extslots * 16 */
60 	union {
61 		__le16 rootnid_2b;	/* nid of root directory */
62 		__le16 blocks_hi;	/* (48BIT on) blocks count MSB */
63 	} __packed rb;
64 	__le64 inos;            /* total valid ino # (== f_files - f_favail) */
65 	__le64 epoch;		/* base seconds used for compact inodes */
66 	__le32 fixed_nsec;	/* fixed nanoseconds for compact inodes */
67 	__le32 blocks_lo;	/* blocks count LSB */
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 ishare_xattr_prefix_id;
87 	__u8 reserved[2];
88 	__le32 build_time;	/* seconds added to epoch for mkfs time */
89 	__le64 rootnid_8b;	/* (48BIT on) nid of root directory */
90 	__le64 reserved2;
91 	__le64 metabox_nid;     /* (METABOX on) nid of the metabox inode */
92 	__le64 reserved3;	/* [align to extslot 1] */
93 };
94 
95 /*
96  * EROFS inode datalayout (i_format in on-disk inode):
97  * 0 - uncompressed flat inode without tail-packing inline data:
98  * 1 - compressed inode with non-compact indexes:
99  * 2 - uncompressed flat inode with tail-packing inline data:
100  * 3 - compressed inode with compact indexes:
101  * 4 - chunk-based inode with (optional) multi-device support:
102  * 5~7 - reserved
103  */
104 enum {
105 	EROFS_INODE_FLAT_PLAIN			= 0,
106 	EROFS_INODE_COMPRESSED_FULL		= 1,
107 	EROFS_INODE_FLAT_INLINE			= 2,
108 	EROFS_INODE_COMPRESSED_COMPACT		= 3,
109 	EROFS_INODE_CHUNK_BASED			= 4,
110 	EROFS_INODE_DATALAYOUT_MAX
111 };
112 
113 static inline bool erofs_inode_is_data_compressed(unsigned int datamode)
114 {
115 	return datamode == EROFS_INODE_COMPRESSED_COMPACT ||
116 		datamode == EROFS_INODE_COMPRESSED_FULL;
117 }
118 
119 /* bit definitions of inode i_format */
120 #define EROFS_I_VERSION_MASK            0x01
121 #define EROFS_I_DATALAYOUT_MASK         0x07
122 
123 #define EROFS_I_VERSION_BIT	0
124 #define EROFS_I_DATALAYOUT_BIT	1
125 #define EROFS_I_NLINK_1_BIT	4	/* non-directory compact inodes only */
126 #define EROFS_I_DOT_OMITTED_BIT	4	/* (directories) omit the `.` dirent */
127 #define EROFS_I_ALL		((1 << (EROFS_I_NLINK_1_BIT + 1)) - 1)
128 
129 /* indicate chunk blkbits, thus 'chunksize = blocksize << chunk blkbits' */
130 #define EROFS_CHUNK_FORMAT_BLKBITS_MASK		0x001F
131 /* with chunk indexes or just a 4-byte block array */
132 #define EROFS_CHUNK_FORMAT_INDEXES		0x0020
133 #define EROFS_CHUNK_FORMAT_48BIT		0x0040
134 
135 #define EROFS_CHUNK_FORMAT_ALL	((EROFS_CHUNK_FORMAT_48BIT << 1) - 1)
136 
137 /* 32-byte on-disk inode */
138 #define EROFS_INODE_LAYOUT_COMPACT	0
139 /* 64-byte on-disk inode */
140 #define EROFS_INODE_LAYOUT_EXTENDED	1
141 
142 struct erofs_inode_chunk_info {
143 	__le16 format;		/* chunk blkbits, etc. */
144 	__le16 reserved;
145 };
146 
147 union erofs_inode_i_u {
148 	__le32 blocks_lo;	/* total blocks count (if compressed inodes) */
149 	__le32 startblk_lo;	/* starting block number (if flat inodes) */
150 	__le32 rdev;		/* device ID (if special inodes) */
151 	struct erofs_inode_chunk_info c;
152 };
153 
154 union erofs_inode_i_nb {
155 	__le16 nlink;		/* if EROFS_I_NLINK_1_BIT is unset */
156 	__le16 blocks_hi;	/* total blocks count MSB */
157 	__le16 startblk_hi;	/* starting block number MSB */
158 } __packed;
159 
160 /* 32-byte reduced form of an ondisk inode */
161 struct erofs_inode_compact {
162 	__le16 i_format;	/* inode format hints */
163 	__le16 i_xattr_icount;
164 	__le16 i_mode;
165 	union erofs_inode_i_nb i_nb;
166 	__le32 i_size;
167 	__le32 i_mtime;
168 	union erofs_inode_i_u i_u;
169 
170 	__le32 i_ino;		/* only used for 32-bit stat compatibility */
171 	__le16 i_uid;
172 	__le16 i_gid;
173 	__le32 i_reserved;
174 };
175 
176 /* 64-byte complete form of an ondisk inode */
177 struct erofs_inode_extended {
178 	__le16 i_format;	/* inode format hints */
179 	__le16 i_xattr_icount;
180 	__le16 i_mode;
181 	union erofs_inode_i_nb i_nb;
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[];       /* 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[];        /* 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[];		/* infix apart from short prefix */
244 };
245 
246 static inline unsigned int erofs_xattr_ibody_size(__le16 i_xattr_icount)
247 {
248 	if (!i_xattr_icount)
249 		return 0;
250 
251 	/* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
252 	return sizeof(struct erofs_xattr_ibody_header) +
253 		sizeof(__u32) * (le16_to_cpu(i_xattr_icount) - 1);
254 }
255 
256 #define EROFS_XATTR_ALIGN(size) round_up(size, sizeof(struct erofs_xattr_entry))
257 
258 static inline unsigned int erofs_xattr_entry_size(struct erofs_xattr_entry *e)
259 {
260 	return EROFS_XATTR_ALIGN(sizeof(struct erofs_xattr_entry) +
261 				 e->e_name_len + le16_to_cpu(e->e_value_size));
262 }
263 
264 /* represent a zeroed chunk (hole) */
265 #define EROFS_NULL_ADDR			-1
266 
267 /* 4-byte block address array */
268 #define EROFS_BLOCK_MAP_ENTRY_SIZE	sizeof(__le32)
269 
270 /* 8-byte inode chunk index */
271 struct erofs_inode_chunk_index {
272 	__le16 startblk_hi;	/* starting block number MSB */
273 	__le16 device_id;	/* back-end storage id (with bits masked) */
274 	__le32 startblk_lo;	/* starting block number of this chunk */
275 };
276 
277 #define EROFS_DIRENT_NID_METABOX_BIT	63
278 #define EROFS_DIRENT_NID_MASK	(BIT_ULL(EROFS_DIRENT_NID_METABOX_BIT) - 1)
279 
280 /* dirent sorts in alphabet order, thus we can do binary search */
281 struct erofs_dirent {
282 	__le64 nid;     /* node number */
283 	__le16 nameoff; /* start offset of file name */
284 	__u8 file_type; /* file type */
285 	__u8 reserved;  /* reserved */
286 } __packed;
287 
288 /*
289  * EROFS file types should match generic FT_* types and
290  * it seems no need to add BUILD_BUG_ONs since potential
291  * unmatchness will break other fses as well...
292  */
293 
294 #define EROFS_NAME_LEN      255
295 
296 /* maximum supported encoded size of a physical compressed cluster */
297 #define Z_EROFS_PCLUSTER_MAX_SIZE	(1024 * 1024)
298 
299 /* maximum supported decoded size of a physical compressed cluster */
300 #define Z_EROFS_PCLUSTER_MAX_DSIZE	(12 * 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  * Enable COMPACTED_2B for EROFS_INODE_COMPRESSED_COMPACT inodes:
345  *   4B (disabled) vs 4B+2B+4B (enabled)
346  */
347 #define Z_EROFS_ADVISE_COMPACTED_2B		0x0001
348 /* Enable extent metadata for EROFS_INODE_COMPRESSED_FULL inodes */
349 #define Z_EROFS_ADVISE_EXTENTS			0x0001
350 #define Z_EROFS_ADVISE_BIG_PCLUSTER_1		0x0002
351 #define Z_EROFS_ADVISE_BIG_PCLUSTER_2		0x0004
352 #define Z_EROFS_ADVISE_INLINE_PCLUSTER		0x0008
353 #define Z_EROFS_ADVISE_INTERLACED_PCLUSTER	0x0010
354 #define Z_EROFS_ADVISE_FRAGMENT_PCLUSTER	0x0020
355 /* Indicate the record size for each extent if extent metadata is used */
356 #define Z_EROFS_ADVISE_EXTRECSZ_BIT		1
357 #define Z_EROFS_ADVISE_EXTRECSZ_MASK		0x3
358 
359 #define Z_EROFS_FRAGMENT_INODE_BIT              7
360 struct z_erofs_map_header {
361 	union {
362 		/* fragment data offset in the packed inode */
363 		__le32  h_fragmentoff;
364 		struct {
365 			__le16  h_reserved1;
366 			/* indicates the encoded size of tailpacking data */
367 			__le16  h_idata_size;
368 		};
369 		__le32 h_extents_lo;	/* extent count LSB */
370 	};
371 	__le16	h_advise;
372 	union {
373 		struct {
374 			/* algorithm type (bit 0-3: HEAD1; bit 4-7: HEAD2) */
375 			__u8	h_algorithmtype;
376 			/*
377 			 * bit 0-3 : logical cluster bits - blkszbits
378 			 * bit 4-6 : reserved
379 			 * bit 7   : pack the whole file into packed inode
380 			 */
381 			__u8	h_clusterbits;
382 		} __packed;
383 		__le16 h_extents_hi;	/* extent count MSB */
384 	} __packed;
385 };
386 
387 enum {
388 	Z_EROFS_LCLUSTER_TYPE_PLAIN	= 0,
389 	Z_EROFS_LCLUSTER_TYPE_HEAD1	= 1,
390 	Z_EROFS_LCLUSTER_TYPE_NONHEAD	= 2,
391 	Z_EROFS_LCLUSTER_TYPE_HEAD2	= 3,
392 	Z_EROFS_LCLUSTER_TYPE_MAX
393 };
394 
395 #define Z_EROFS_LI_LCLUSTER_TYPE_MASK	(Z_EROFS_LCLUSTER_TYPE_MAX - 1)
396 
397 /* (noncompact only, HEAD) This pcluster refers to partial decompressed data */
398 #define Z_EROFS_LI_PARTIAL_REF		(1 << 15)
399 
400 /* Set on 1st non-head lcluster to store compressed block counti (in blocks) */
401 #define Z_EROFS_LI_D0_CBLKCNT		(1 << 11)
402 
403 struct z_erofs_lcluster_index {
404 	__le16 di_advise;
405 	/* where to decompress in the head lcluster */
406 	__le16 di_clusterofs;
407 
408 	union {
409 		__le32 blkaddr;		/* for the HEAD lclusters */
410 		/*
411 		 * [0] - distance to its HEAD lcluster
412 		 * [1] - distance to the next HEAD lcluster
413 		 */
414 		__le16 delta[2];	/* for the NONHEAD lclusters */
415 	} di_u;
416 };
417 
418 #define Z_EROFS_MAP_HEADER_END(end)	\
419 	(ALIGN(end, 8) + sizeof(struct z_erofs_map_header))
420 #define Z_EROFS_FULL_INDEX_START(end)	(Z_EROFS_MAP_HEADER_END(end) + 8)
421 
422 #define Z_EROFS_EXTENT_PLEN_PARTIAL	BIT(27)
423 #define Z_EROFS_EXTENT_PLEN_FMT_BIT	28
424 #define Z_EROFS_EXTENT_PLEN_MASK	((Z_EROFS_PCLUSTER_MAX_SIZE << 1) - 1)
425 struct z_erofs_extent {
426 	__le32 plen;		/* encoded length */
427 	__le32 pstart_lo;	/* physical offset */
428 	__le32 pstart_hi;	/* physical offset MSB */
429 	__le32 lstart_lo;	/* logical offset */
430 	__le32 lstart_hi;	/* logical offset MSB (>= 4GiB inodes) */
431 	__u8 reserved[12];	/* for future use */
432 };
433 
434 static inline int z_erofs_extent_recsize(unsigned int advise)
435 {
436 	return 4 << ((advise >> Z_EROFS_ADVISE_EXTRECSZ_BIT) &
437 		Z_EROFS_ADVISE_EXTRECSZ_MASK);
438 }
439 
440 /* check the EROFS on-disk layout strictly at compile time */
441 static inline void erofs_check_ondisk_layout_definitions(void)
442 {
443 	const __le64 fmh = *(__le64 *)&(struct z_erofs_map_header) {
444 		.h_clusterbits = 1 << Z_EROFS_FRAGMENT_INODE_BIT
445 	};
446 
447 	BUILD_BUG_ON(sizeof(struct erofs_super_block) != 144);
448 	BUILD_BUG_ON(sizeof(struct erofs_inode_compact) != 32);
449 	BUILD_BUG_ON(sizeof(struct erofs_inode_extended) != 64);
450 	BUILD_BUG_ON(sizeof(struct erofs_xattr_ibody_header) != 12);
451 	BUILD_BUG_ON(sizeof(struct erofs_xattr_entry) != 4);
452 	BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_info) != 4);
453 	BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_index) != 8);
454 	BUILD_BUG_ON(sizeof(struct z_erofs_map_header) != 8);
455 	BUILD_BUG_ON(sizeof(struct z_erofs_lcluster_index) != 8);
456 	BUILD_BUG_ON(sizeof(struct erofs_dirent) != 12);
457 	/* keep in sync between 2 index structures for better extendibility */
458 	BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_index) !=
459 		     sizeof(struct z_erofs_lcluster_index));
460 	BUILD_BUG_ON(sizeof(struct erofs_deviceslot) != 128);
461 
462 	/* exclude old compiler versions like gcc 7.5.0 */
463 	BUILD_BUG_ON(__builtin_constant_p(fmh) ?
464 		     fmh != cpu_to_le64(1ULL << 63) : 0);
465 }
466 
467 #endif
468