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