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