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