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