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