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