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