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