xref: /linux/fs/erofs/erofs_fs.h (revision 99302ebd3af7895cb5312e80e65d4db5aed5a72d)
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;      /* compact inode time derivation */
62 	__le32 build_time_nsec;	/* compact inode time derivation in ns 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 - uncompressed flat inode without tail-packing inline data:
83  * inode, [xattrs], ... | ... | no-holed data
84  * 1 - compressed inode with non-compact indexes:
85  * inode, [xattrs], [map_header], extents ... | ...
86  * 2 - uncompressed flat inode with tail-packing inline data:
87  * inode, [xattrs], tailpacking data, ... | ... | no-holed data
88  * 3 - compressed inode with compact indexes:
89  * inode, [xattrs], map_header, extents ... | ...
90  * 4 - chunk-based inode with (optional) multi-device support:
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_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 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 		/* total compressed blocks for compressed inodes */
144 		__le32 compressed_blocks;
145 		/* block address for uncompressed flat inodes */
146 		__le32 raw_blkaddr;
147 
148 		/* for device files, used to indicate old/new device # */
149 		__le32 rdev;
150 
151 		/* for chunk-based files, it contains the summary info */
152 		struct erofs_inode_chunk_info c;
153 	} i_u;
154 	__le32 i_ino;           /* only used for 32-bit stat compatibility */
155 	__le16 i_uid;
156 	__le16 i_gid;
157 	__le32 i_reserved2;
158 };
159 
160 /* 32-byte on-disk inode */
161 #define EROFS_INODE_LAYOUT_COMPACT	0
162 /* 64-byte on-disk inode */
163 #define EROFS_INODE_LAYOUT_EXTENDED	1
164 
165 /* 64-byte complete form of an ondisk inode */
166 struct erofs_inode_extended {
167 	__le16 i_format;	/* inode format hints */
168 
169 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
170 	__le16 i_xattr_icount;
171 	__le16 i_mode;
172 	__le16 i_reserved;
173 	__le64 i_size;
174 	union {
175 		/* total compressed blocks for compressed inodes */
176 		__le32 compressed_blocks;
177 		/* block address for uncompressed flat inodes */
178 		__le32 raw_blkaddr;
179 
180 		/* for device files, used to indicate old/new device # */
181 		__le32 rdev;
182 
183 		/* for chunk-based files, it contains the summary info */
184 		struct erofs_inode_chunk_info c;
185 	} i_u;
186 
187 	/* only used for 32-bit stat compatibility */
188 	__le32 i_ino;
189 
190 	__le32 i_uid;
191 	__le32 i_gid;
192 	__le64 i_mtime;
193 	__le32 i_mtime_nsec;
194 	__le32 i_nlink;
195 	__u8   i_reserved2[16];
196 };
197 
198 #define EROFS_MAX_SHARED_XATTRS         (128)
199 /* h_shared_count between 129 ... 255 are special # */
200 #define EROFS_SHARED_XATTR_EXTENT       (255)
201 
202 /*
203  * inline xattrs (n == i_xattr_icount):
204  * erofs_xattr_ibody_header(1) + (n - 1) * 4 bytes
205  *          12 bytes           /                   \
206  *                            /                     \
207  *                           /-----------------------\
208  *                           |  erofs_xattr_entries+ |
209  *                           +-----------------------+
210  * inline xattrs must starts in erofs_xattr_ibody_header,
211  * for read-only fs, no need to introduce h_refcount
212  */
213 struct erofs_xattr_ibody_header {
214 	__le32 h_reserved;
215 	__u8   h_shared_count;
216 	__u8   h_reserved2[7];
217 	__le32 h_shared_xattrs[];       /* shared xattr id array */
218 };
219 
220 /* Name indexes */
221 #define EROFS_XATTR_INDEX_USER              1
222 #define EROFS_XATTR_INDEX_POSIX_ACL_ACCESS  2
223 #define EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT 3
224 #define EROFS_XATTR_INDEX_TRUSTED           4
225 #define EROFS_XATTR_INDEX_LUSTRE            5
226 #define EROFS_XATTR_INDEX_SECURITY          6
227 
228 /* xattr entry (for both inline & shared xattrs) */
229 struct erofs_xattr_entry {
230 	__u8   e_name_len;      /* length of name */
231 	__u8   e_name_index;    /* attribute name index */
232 	__le16 e_value_size;    /* size of attribute value */
233 	/* followed by e_name and e_value */
234 	char   e_name[];        /* attribute name */
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 /* maximum supported size of a physical compression cluster */
268 #define Z_EROFS_PCLUSTER_MAX_SIZE	(1024 * 1024)
269 
270 /* available compression algorithm types (for h_algorithmtype) */
271 enum {
272 	Z_EROFS_COMPRESSION_LZ4		= 0,
273 	Z_EROFS_COMPRESSION_LZMA	= 1,
274 	Z_EROFS_COMPRESSION_MAX
275 };
276 #define Z_EROFS_ALL_COMPR_ALGS		((1 << Z_EROFS_COMPRESSION_MAX) - 1)
277 
278 /* 14 bytes (+ length field = 16 bytes) */
279 struct z_erofs_lz4_cfgs {
280 	__le16 max_distance;
281 	__le16 max_pclusterblks;
282 	u8 reserved[10];
283 } __packed;
284 
285 /* 14 bytes (+ length field = 16 bytes) */
286 struct z_erofs_lzma_cfgs {
287 	__le32 dict_size;
288 	__le16 format;
289 	u8 reserved[8];
290 } __packed;
291 
292 #define Z_EROFS_LZMA_MAX_DICT_SIZE	(8 * Z_EROFS_PCLUSTER_MAX_SIZE)
293 
294 /*
295  * bit 0 : COMPACTED_2B indexes (0 - off; 1 - on)
296  *  e.g. for 4k logical cluster size,      4B        if compacted 2B is off;
297  *                                  (4B) + 2B + (4B) if compacted 2B is on.
298  * bit 1 : HEAD1 big pcluster (0 - off; 1 - on)
299  * bit 2 : HEAD2 big pcluster (0 - off; 1 - on)
300  * bit 3 : tailpacking inline pcluster (0 - off; 1 - on)
301  */
302 #define Z_EROFS_ADVISE_COMPACTED_2B		0x0001
303 #define Z_EROFS_ADVISE_BIG_PCLUSTER_1		0x0002
304 #define Z_EROFS_ADVISE_BIG_PCLUSTER_2		0x0004
305 #define Z_EROFS_ADVISE_INLINE_PCLUSTER		0x0008
306 
307 struct z_erofs_map_header {
308 	__le16	h_reserved1;
309 	/* indicates the encoded size of tailpacking data */
310 	__le16  h_idata_size;
311 	__le16	h_advise;
312 	/*
313 	 * bit 0-3 : algorithm type of head 1 (logical cluster type 01);
314 	 * bit 4-7 : algorithm type of head 2 (logical cluster type 11).
315 	 */
316 	__u8	h_algorithmtype;
317 	/*
318 	 * bit 0-2 : logical cluster bits - 12, e.g. 0 for 4096;
319 	 * bit 3-7 : reserved.
320 	 */
321 	__u8	h_clusterbits;
322 };
323 
324 #define Z_EROFS_VLE_LEGACY_HEADER_PADDING       8
325 
326 /*
327  * Fixed-sized output compression on-disk logical cluster type:
328  *    0   - literal (uncompressed) lcluster
329  *    1,3 - compressed lcluster (for HEAD lclusters)
330  *    2   - compressed lcluster (for NONHEAD lclusters)
331  *
332  * In detail,
333  *    0 - literal (uncompressed) lcluster,
334  *        di_advise = 0
335  *        di_clusterofs = the literal data offset of the lcluster
336  *        di_blkaddr = the blkaddr of the literal pcluster
337  *
338  *    1,3 - compressed lcluster (for HEAD lclusters)
339  *        di_advise = 1 or 3
340  *        di_clusterofs = the decompressed data offset of the lcluster
341  *        di_blkaddr = the blkaddr of the compressed pcluster
342  *
343  *    2 - compressed lcluster (for NONHEAD lclusters)
344  *        di_advise = 2
345  *        di_clusterofs =
346  *           the decompressed data offset in its own HEAD lcluster
347  *        di_u.delta[0] = distance to this HEAD lcluster
348  *        di_u.delta[1] = distance to the next HEAD lcluster
349  */
350 enum {
351 	Z_EROFS_VLE_CLUSTER_TYPE_PLAIN		= 0,
352 	Z_EROFS_VLE_CLUSTER_TYPE_HEAD1		= 1,
353 	Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD	= 2,
354 	Z_EROFS_VLE_CLUSTER_TYPE_HEAD2		= 3,
355 	Z_EROFS_VLE_CLUSTER_TYPE_MAX
356 };
357 
358 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS        2
359 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT         0
360 
361 /*
362  * D0_CBLKCNT will be marked _only_ at the 1st non-head lcluster to store the
363  * compressed block count of a compressed extent (in logical clusters, aka.
364  * block count of a pcluster).
365  */
366 #define Z_EROFS_VLE_DI_D0_CBLKCNT		(1 << 11)
367 
368 struct z_erofs_vle_decompressed_index {
369 	__le16 di_advise;
370 	/* where to decompress in the head lcluster */
371 	__le16 di_clusterofs;
372 
373 	union {
374 		/* for the HEAD lclusters */
375 		__le32 blkaddr;
376 		/*
377 		 * for the NONHEAD lclusters
378 		 * [0] - distance to its HEAD lcluster
379 		 * [1] - distance to the next HEAD lcluster
380 		 */
381 		__le16 delta[2];
382 	} di_u;
383 };
384 
385 #define Z_EROFS_VLE_LEGACY_INDEX_ALIGN(size) \
386 	(round_up(size, sizeof(struct z_erofs_vle_decompressed_index)) + \
387 	 sizeof(struct z_erofs_map_header) + Z_EROFS_VLE_LEGACY_HEADER_PADDING)
388 
389 /* dirent sorts in alphabet order, thus we can do binary search */
390 struct erofs_dirent {
391 	__le64 nid;     /* node number */
392 	__le16 nameoff; /* start offset of file name */
393 	__u8 file_type; /* file type */
394 	__u8 reserved;  /* reserved */
395 } __packed;
396 
397 /*
398  * EROFS file types should match generic FT_* types and
399  * it seems no need to add BUILD_BUG_ONs since potential
400  * unmatchness will break other fses as well...
401  */
402 
403 #define EROFS_NAME_LEN      255
404 
405 /* check the EROFS on-disk layout strictly at compile time */
406 static inline void erofs_check_ondisk_layout_definitions(void)
407 {
408 	BUILD_BUG_ON(sizeof(struct erofs_super_block) != 128);
409 	BUILD_BUG_ON(sizeof(struct erofs_inode_compact) != 32);
410 	BUILD_BUG_ON(sizeof(struct erofs_inode_extended) != 64);
411 	BUILD_BUG_ON(sizeof(struct erofs_xattr_ibody_header) != 12);
412 	BUILD_BUG_ON(sizeof(struct erofs_xattr_entry) != 4);
413 	BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_info) != 4);
414 	BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_index) != 8);
415 	BUILD_BUG_ON(sizeof(struct z_erofs_map_header) != 8);
416 	BUILD_BUG_ON(sizeof(struct z_erofs_vle_decompressed_index) != 8);
417 	BUILD_BUG_ON(sizeof(struct erofs_dirent) != 12);
418 	/* keep in sync between 2 index structures for better extendibility */
419 	BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_index) !=
420 		     sizeof(struct z_erofs_vle_decompressed_index));
421 	BUILD_BUG_ON(sizeof(struct erofs_deviceslot) != 128);
422 
423 	BUILD_BUG_ON(BIT(Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) <
424 		     Z_EROFS_VLE_CLUSTER_TYPE_MAX - 1);
425 }
426 
427 #endif
428