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