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