1 /* SPDX-License-Identifier: GPL-2.0 OR Apache-2.0
3 * linux/drivers/staging/erofs/erofs_fs.h
5 * Copyright (C) 2017-2018 HUAWEI, Inc.
6 * http://www.huawei.com/
9 * This file is dual-licensed; you may select either the GNU General Public
10 * License version 2 or Apache License, Version 2.0. See the file COPYING
11 * in the main directory of the Linux distribution for more details.
16 /* Enhanced(Extended) ROM File System */
17 #define EROFS_SUPER_MAGIC_V1 0xE0F5E1E2
18 #define EROFS_SUPER_OFFSET 1024
20 struct erofs_super_block {
21 /* 0 */__le32 magic; /* in the little endian */
22 /* 4 */__le32 checksum; /* crc32c(super_block) */
23 /* 8 */__le32 features;
24 /* 12 */__u8 blkszbits; /* support block_size == PAGE_SIZE only */
25 /* 13 */__u8 reserved;
27 /* 14 */__le16 root_nid;
28 /* 16 */__le64 inos; /* total valid ino # (== f_files - f_favail) */
30 /* 24 */__le64 build_time; /* inode v1 time derivation */
31 /* 32 */__le32 build_time_nsec;
32 /* 36 */__le32 blocks; /* used for statfs */
33 /* 40 */__le32 meta_blkaddr;
34 /* 44 */__le32 xattr_blkaddr;
35 /* 48 */__u8 uuid[16]; /* 128-bit uuid for volume */
36 /* 64 */__u8 volume_name[16]; /* volume name */
38 /* 80 */__u8 reserved2[48]; /* 128 bytes */
41 #define __EROFS_BIT(_prefix, _cur, _pre) enum { \
42 _prefix ## _cur ## _BIT = _prefix ## _pre ## _BIT + \
43 _prefix ## _pre ## _BITS }
46 * erofs inode data mapping:
47 * 0 - inode plain without inline data A:
48 * inode, [xattrs], ... | ... | no-holed data
49 * 1 - inode VLE compression B:
50 * inode, [xattrs], extents ... | ...
51 * 2 - inode plain with inline data C:
52 * inode, [xattrs], last_inline_data, ... | ... | no-holed data
56 EROFS_INODE_LAYOUT_PLAIN,
57 EROFS_INODE_LAYOUT_COMPRESSION,
58 EROFS_INODE_LAYOUT_INLINE,
59 EROFS_INODE_LAYOUT_MAX
61 #define EROFS_I_VERSION_BITS 1
62 #define EROFS_I_DATA_MAPPING_BITS 3
64 #define EROFS_I_VERSION_BIT 0
65 __EROFS_BIT(EROFS_I_, DATA_MAPPING, VERSION);
67 struct erofs_inode_v1 {
68 /* 0 */__le16 i_advise;
70 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
71 /* 2 */__le16 i_xattr_icount;
73 /* 6 */__le16 i_nlink;
75 /* 12 */__le32 i_reserved;
77 /* file total compressed blocks for data mapping 1 */
78 __le32 compressed_blocks;
81 /* for device files, used to indicate old/new device # */
84 /* 20 */__le32 i_ino; /* only used for 32-bit stat compatibility */
87 /* 28 */__le32 i_checksum;
90 /* 32 bytes on-disk inode */
91 #define EROFS_INODE_LAYOUT_V1 0
92 /* 64 bytes on-disk inode */
93 #define EROFS_INODE_LAYOUT_V2 1
95 struct erofs_inode_v2 {
98 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
99 __le16 i_xattr_icount;
101 __le16 i_reserved; /* 8 bytes */
102 __le64 i_size; /* 16 bytes */
104 /* file total compressed blocks for data mapping 1 */
105 __le32 compressed_blocks;
108 /* for device files, used to indicate old/new device # */
112 /* only used for 32-bit stat compatibility */
113 __le32 i_ino; /* 24 bytes */
117 __le64 i_ctime; /* 32 bytes */
120 __u8 i_reserved2[12];
121 __le32 i_checksum; /* 64 bytes */
124 #define EROFS_MAX_SHARED_XATTRS (128)
125 /* h_shared_count between 129 ... 255 are special # */
126 #define EROFS_SHARED_XATTR_EXTENT (255)
129 * inline xattrs (n == i_xattr_icount):
130 * erofs_xattr_ibody_header(1) + (n - 1) * 4 bytes
133 * /-----------------------\
134 * | erofs_xattr_entries+ |
135 * +-----------------------+
136 * inline xattrs must starts in erofs_xattr_ibody_header,
137 * for read-only fs, no need to introduce h_refcount
139 struct erofs_xattr_ibody_header {
143 __le32 h_shared_xattrs[0]; /* shared xattr id array */
147 #define EROFS_XATTR_INDEX_USER 1
148 #define EROFS_XATTR_INDEX_POSIX_ACL_ACCESS 2
149 #define EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT 3
150 #define EROFS_XATTR_INDEX_TRUSTED 4
151 #define EROFS_XATTR_INDEX_LUSTRE 5
152 #define EROFS_XATTR_INDEX_SECURITY 6
154 /* xattr entry (for both inline & shared xattrs) */
155 struct erofs_xattr_entry {
156 __u8 e_name_len; /* length of name */
157 __u8 e_name_index; /* attribute name index */
158 __le16 e_value_size; /* size of attribute value */
159 /* followed by e_name and e_value */
160 char e_name[0]; /* attribute name */
163 #define ondisk_xattr_ibody_size(count) ({\
164 u32 __count = le16_to_cpu(count); \
165 ((__count) == 0) ? 0 : \
166 sizeof(struct erofs_xattr_ibody_header) + \
167 sizeof(__u32) * ((__count) - 1); })
169 #define EROFS_XATTR_ALIGN(size) round_up(size, sizeof(struct erofs_xattr_entry))
170 #define EROFS_XATTR_ENTRY_SIZE(entry) EROFS_XATTR_ALIGN( \
171 sizeof(struct erofs_xattr_entry) + \
172 (entry)->e_name_len + le16_to_cpu((entry)->e_value_size))
174 /* have to be aligned with 8 bytes on disk */
175 struct erofs_extent_header {
177 __le32 eh_reserved[3];
181 * Z_EROFS Variable-sized Logical Extent cluster type:
182 * 0 - literal (uncompressed) cluster
183 * 1 - compressed cluster (for the head logical cluster)
184 * 2 - compressed cluster (for the other logical clusters)
187 * 0 - literal (uncompressed) cluster,
189 * di_clusterofs = the literal data offset of the cluster
190 * di_blkaddr = the blkaddr of the literal cluster
192 * 1 - compressed cluster (for the head logical cluster)
194 * di_clusterofs = the decompressed data offset of the cluster
195 * di_blkaddr = the blkaddr of the compressed cluster
197 * 2 - compressed cluster (for the other logical clusters)
200 * the decompressed data offset in its own head cluster
201 * di_u.delta[0] = distance to its corresponding head cluster
202 * di_u.delta[1] = distance to its corresponding tail cluster
203 * (di_advise could be 0, 1 or 2)
206 Z_EROFS_VLE_CLUSTER_TYPE_PLAIN,
207 Z_EROFS_VLE_CLUSTER_TYPE_HEAD,
208 Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD,
209 Z_EROFS_VLE_CLUSTER_TYPE_RESERVED,
210 Z_EROFS_VLE_CLUSTER_TYPE_MAX
213 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS 2
214 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT 0
216 struct z_erofs_vle_decompressed_index {
218 /* where to decompress in the head cluster */
219 __le16 di_clusterofs;
222 /* for the head cluster */
225 * for the rest clusters
226 * eg. for 4k page-sized cluster, maximum 4K*64k = 256M)
227 * [0] - pointing to the head cluster
228 * [1] - pointing to the tail cluster
231 } di_u __packed; /* 8 bytes */
234 #define Z_EROFS_VLE_EXTENT_ALIGN(size) round_up(size, \
235 sizeof(struct z_erofs_vle_decompressed_index))
237 /* dirent sorts in alphabet order, thus we can do binary search */
238 struct erofs_dirent {
239 __le64 nid; /* 0, node number */
240 __le16 nameoff; /* 8, start offset of file name */
241 __u8 file_type; /* 10, file type */
242 __u8 reserved; /* 11, reserved */
245 /* file types used in inode_info->flags */
258 #define EROFS_NAME_LEN 255
260 /* check the EROFS on-disk layout strictly at compile time */
261 static inline void erofs_check_ondisk_layout_definitions(void)
263 BUILD_BUG_ON(sizeof(struct erofs_super_block) != 128);
264 BUILD_BUG_ON(sizeof(struct erofs_inode_v1) != 32);
265 BUILD_BUG_ON(sizeof(struct erofs_inode_v2) != 64);
266 BUILD_BUG_ON(sizeof(struct erofs_xattr_ibody_header) != 12);
267 BUILD_BUG_ON(sizeof(struct erofs_xattr_entry) != 4);
268 BUILD_BUG_ON(sizeof(struct erofs_extent_header) != 16);
269 BUILD_BUG_ON(sizeof(struct z_erofs_vle_decompressed_index) != 8);
270 BUILD_BUG_ON(sizeof(struct erofs_dirent) != 12);
272 BUILD_BUG_ON(BIT(Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) <
273 Z_EROFS_VLE_CLUSTER_TYPE_MAX - 1);