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/
7 * Created by Gao Xiang <gaoxiang25@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
21 * Any bits that aren't in EROFS_ALL_REQUIREMENTS should be
22 * incompatible with this kernel version.
24 #define EROFS_REQUIREMENT_LZ4_0PADDING 0x00000001
25 #define EROFS_ALL_REQUIREMENTS EROFS_REQUIREMENT_LZ4_0PADDING
27 struct erofs_super_block {
28 /* 0 */__le32 magic; /* in the little endian */
29 /* 4 */__le32 checksum; /* crc32c(super_block) */
30 /* 8 */__le32 features; /* (aka. feature_compat) */
31 /* 12 */__u8 blkszbits; /* support block_size == PAGE_SIZE only */
32 /* 13 */__u8 reserved;
34 /* 14 */__le16 root_nid;
35 /* 16 */__le64 inos; /* total valid ino # (== f_files - f_favail) */
37 /* 24 */__le64 build_time; /* inode v1 time derivation */
38 /* 32 */__le32 build_time_nsec;
39 /* 36 */__le32 blocks; /* used for statfs */
40 /* 40 */__le32 meta_blkaddr;
41 /* 44 */__le32 xattr_blkaddr;
42 /* 48 */__u8 uuid[16]; /* 128-bit uuid for volume */
43 /* 64 */__u8 volume_name[16]; /* volume name */
44 /* 80 */__le32 requirements; /* (aka. feature_incompat) */
46 /* 84 */__u8 reserved2[44];
47 } __packed; /* 128 bytes */
50 * erofs inode data mapping:
51 * 0 - inode plain without inline data A:
52 * inode, [xattrs], ... | ... | no-holed data
53 * 1 - inode VLE compression B (legacy):
54 * inode, [xattrs], extents ... | ...
55 * 2 - inode plain with inline data C:
56 * inode, [xattrs], last_inline_data, ... | ... | no-holed data
57 * 3 - inode compression D:
58 * inode, [xattrs], map_header, extents ... | ...
62 EROFS_INODE_FLAT_PLAIN,
63 EROFS_INODE_FLAT_COMPRESSION_LEGACY,
64 EROFS_INODE_FLAT_INLINE,
65 EROFS_INODE_FLAT_COMPRESSION,
66 EROFS_INODE_LAYOUT_MAX
69 static bool erofs_inode_is_data_compressed(unsigned int datamode)
71 if (datamode == EROFS_INODE_FLAT_COMPRESSION)
73 return datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY;
76 /* bit definitions of inode i_advise */
77 #define EROFS_I_VERSION_BITS 1
78 #define EROFS_I_DATA_MAPPING_BITS 3
80 #define EROFS_I_VERSION_BIT 0
81 #define EROFS_I_DATA_MAPPING_BIT 1
83 struct erofs_inode_v1 {
84 /* 0 */__le16 i_advise;
86 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
87 /* 2 */__le16 i_xattr_icount;
89 /* 6 */__le16 i_nlink;
91 /* 12 */__le32 i_reserved;
93 /* file total compressed blocks for data mapping 1 */
94 __le32 compressed_blocks;
97 /* for device files, used to indicate old/new device # */
100 /* 20 */__le32 i_ino; /* only used for 32-bit stat compatibility */
101 /* 24 */__le16 i_uid;
102 /* 26 */__le16 i_gid;
103 /* 28 */__le32 i_checksum;
106 /* 32 bytes on-disk inode */
107 #define EROFS_INODE_LAYOUT_V1 0
108 /* 64 bytes on-disk inode */
109 #define EROFS_INODE_LAYOUT_V2 1
111 struct erofs_inode_v2 {
114 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
115 __le16 i_xattr_icount;
117 __le16 i_reserved; /* 8 bytes */
118 __le64 i_size; /* 16 bytes */
120 /* file total compressed blocks for data mapping 1 */
121 __le32 compressed_blocks;
124 /* for device files, used to indicate old/new device # */
128 /* only used for 32-bit stat compatibility */
129 __le32 i_ino; /* 24 bytes */
133 __le64 i_ctime; /* 32 bytes */
136 __u8 i_reserved2[12];
137 __le32 i_checksum; /* 64 bytes */
140 #define EROFS_MAX_SHARED_XATTRS (128)
141 /* h_shared_count between 129 ... 255 are special # */
142 #define EROFS_SHARED_XATTR_EXTENT (255)
145 * inline xattrs (n == i_xattr_icount):
146 * erofs_xattr_ibody_header(1) + (n - 1) * 4 bytes
149 * /-----------------------\
150 * | erofs_xattr_entries+ |
151 * +-----------------------+
152 * inline xattrs must starts in erofs_xattr_ibody_header,
153 * for read-only fs, no need to introduce h_refcount
155 struct erofs_xattr_ibody_header {
159 __le32 h_shared_xattrs[0]; /* shared xattr id array */
163 #define EROFS_XATTR_INDEX_USER 1
164 #define EROFS_XATTR_INDEX_POSIX_ACL_ACCESS 2
165 #define EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT 3
166 #define EROFS_XATTR_INDEX_TRUSTED 4
167 #define EROFS_XATTR_INDEX_LUSTRE 5
168 #define EROFS_XATTR_INDEX_SECURITY 6
170 /* xattr entry (for both inline & shared xattrs) */
171 struct erofs_xattr_entry {
172 __u8 e_name_len; /* length of name */
173 __u8 e_name_index; /* attribute name index */
174 __le16 e_value_size; /* size of attribute value */
175 /* followed by e_name and e_value */
176 char e_name[0]; /* attribute name */
179 #define ondisk_xattr_ibody_size(count) ({\
180 u32 __count = le16_to_cpu(count); \
181 ((__count) == 0) ? 0 : \
182 sizeof(struct erofs_xattr_ibody_header) + \
183 sizeof(__u32) * ((__count) - 1); })
185 #define EROFS_XATTR_ALIGN(size) round_up(size, sizeof(struct erofs_xattr_entry))
186 #define EROFS_XATTR_ENTRY_SIZE(entry) EROFS_XATTR_ALIGN( \
187 sizeof(struct erofs_xattr_entry) + \
188 (entry)->e_name_len + le16_to_cpu((entry)->e_value_size))
190 /* available compression algorithm types */
192 Z_EROFS_COMPRESSION_LZ4,
193 Z_EROFS_COMPRESSION_MAX
197 * bit 0 : COMPACTED_2B indexes (0 - off; 1 - on)
198 * e.g. for 4k logical cluster size, 4B if compacted 2B is off;
199 * (4B) + 2B + (4B) if compacted 2B is on.
201 #define Z_EROFS_ADVISE_COMPACTED_2B_BIT 0
203 #define Z_EROFS_ADVISE_COMPACTED_2B (1 << Z_EROFS_ADVISE_COMPACTED_2B_BIT)
205 struct z_erofs_map_header {
209 * bit 0-3 : algorithm type of head 1 (logical cluster type 01);
210 * bit 4-7 : algorithm type of head 2 (logical cluster type 11).
212 __u8 h_algorithmtype;
214 * bit 0-2 : logical cluster bits - 12, e.g. 0 for 4096;
215 * bit 3-4 : (physical - logical) cluster bits of head 1:
216 * For example, if logical clustersize = 4096, 1 for 8192.
217 * bit 5-7 : (physical - logical) cluster bits of head 2.
222 #define Z_EROFS_VLE_LEGACY_HEADER_PADDING 8
225 * Z_EROFS Variable-sized Logical Extent cluster type:
226 * 0 - literal (uncompressed) cluster
227 * 1 - compressed cluster (for the head logical cluster)
228 * 2 - compressed cluster (for the other logical clusters)
231 * 0 - literal (uncompressed) cluster,
233 * di_clusterofs = the literal data offset of the cluster
234 * di_blkaddr = the blkaddr of the literal cluster
236 * 1 - compressed cluster (for the head logical cluster)
238 * di_clusterofs = the decompressed data offset of the cluster
239 * di_blkaddr = the blkaddr of the compressed cluster
241 * 2 - compressed cluster (for the other logical clusters)
244 * the decompressed data offset in its own head cluster
245 * di_u.delta[0] = distance to its corresponding head cluster
246 * di_u.delta[1] = distance to its corresponding tail cluster
247 * (di_advise could be 0, 1 or 2)
250 Z_EROFS_VLE_CLUSTER_TYPE_PLAIN,
251 Z_EROFS_VLE_CLUSTER_TYPE_HEAD,
252 Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD,
253 Z_EROFS_VLE_CLUSTER_TYPE_RESERVED,
254 Z_EROFS_VLE_CLUSTER_TYPE_MAX
257 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS 2
258 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT 0
260 struct z_erofs_vle_decompressed_index {
262 /* where to decompress in the head cluster */
263 __le16 di_clusterofs;
266 /* for the head cluster */
269 * for the rest clusters
270 * eg. for 4k page-sized cluster, maximum 4K*64k = 256M)
271 * [0] - pointing to the head cluster
272 * [1] - pointing to the tail cluster
275 } di_u __packed; /* 8 bytes */
278 #define Z_EROFS_VLE_LEGACY_INDEX_ALIGN(size) \
279 (round_up(size, sizeof(struct z_erofs_vle_decompressed_index)) + \
280 sizeof(struct z_erofs_map_header) + Z_EROFS_VLE_LEGACY_HEADER_PADDING)
282 /* dirent sorts in alphabet order, thus we can do binary search */
283 struct erofs_dirent {
284 __le64 nid; /* 0, node number */
285 __le16 nameoff; /* 8, start offset of file name */
286 __u8 file_type; /* 10, file type */
287 __u8 reserved; /* 11, reserved */
290 /* file types used in inode_info->flags */
303 #define EROFS_NAME_LEN 255
305 /* check the EROFS on-disk layout strictly at compile time */
306 static inline void erofs_check_ondisk_layout_definitions(void)
308 BUILD_BUG_ON(sizeof(struct erofs_super_block) != 128);
309 BUILD_BUG_ON(sizeof(struct erofs_inode_v1) != 32);
310 BUILD_BUG_ON(sizeof(struct erofs_inode_v2) != 64);
311 BUILD_BUG_ON(sizeof(struct erofs_xattr_ibody_header) != 12);
312 BUILD_BUG_ON(sizeof(struct erofs_xattr_entry) != 4);
313 BUILD_BUG_ON(sizeof(struct z_erofs_map_header) != 8);
314 BUILD_BUG_ON(sizeof(struct z_erofs_vle_decompressed_index) != 8);
315 BUILD_BUG_ON(sizeof(struct erofs_dirent) != 12);
317 BUILD_BUG_ON(BIT(Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) <
318 Z_EROFS_VLE_CLUSTER_TYPE_MAX - 1);