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