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_ALL_REQUIREMENTS 0
26 struct erofs_super_block {
27 /* 0 */__le32 magic; /* in the little endian */
28 /* 4 */__le32 checksum; /* crc32c(super_block) */
29 /* 8 */__le32 features; /* (aka. feature_compat) */
30 /* 12 */__u8 blkszbits; /* support block_size == PAGE_SIZE only */
31 /* 13 */__u8 reserved;
33 /* 14 */__le16 root_nid;
34 /* 16 */__le64 inos; /* total valid ino # (== f_files - f_favail) */
36 /* 24 */__le64 build_time; /* inode v1 time derivation */
37 /* 32 */__le32 build_time_nsec;
38 /* 36 */__le32 blocks; /* used for statfs */
39 /* 40 */__le32 meta_blkaddr;
40 /* 44 */__le32 xattr_blkaddr;
41 /* 48 */__u8 uuid[16]; /* 128-bit uuid for volume */
42 /* 64 */__u8 volume_name[16]; /* volume name */
43 /* 80 */__le32 requirements; /* (aka. feature_incompat) */
45 /* 84 */__u8 reserved2[44];
46 } __packed; /* 128 bytes */
48 #define __EROFS_BIT(_prefix, _cur, _pre) enum { \
49 _prefix ## _cur ## _BIT = _prefix ## _pre ## _BIT + \
50 _prefix ## _pre ## _BITS }
53 * erofs inode data mapping:
54 * 0 - inode plain without inline data A:
55 * inode, [xattrs], ... | ... | no-holed data
56 * 1 - inode VLE compression B:
57 * inode, [xattrs], extents ... | ...
58 * 2 - inode plain with inline data C:
59 * inode, [xattrs], last_inline_data, ... | ... | no-holed data
63 EROFS_INODE_LAYOUT_PLAIN,
64 EROFS_INODE_LAYOUT_COMPRESSION,
65 EROFS_INODE_LAYOUT_INLINE,
66 EROFS_INODE_LAYOUT_MAX
68 #define EROFS_I_VERSION_BITS 1
69 #define EROFS_I_DATA_MAPPING_BITS 3
71 #define EROFS_I_VERSION_BIT 0
72 __EROFS_BIT(EROFS_I_, DATA_MAPPING, VERSION);
75 ((1 << (EROFS_I_DATA_MAPPING_BIT + EROFS_I_DATA_MAPPING_BITS)) - 1)
77 struct erofs_inode_v1 {
78 /* 0 */__le16 i_advise;
80 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
81 /* 2 */__le16 i_xattr_icount;
83 /* 6 */__le16 i_nlink;
85 /* 12 */__le32 i_reserved;
87 /* file total compressed blocks for data mapping 1 */
88 __le32 compressed_blocks;
91 /* for device files, used to indicate old/new device # */
94 /* 20 */__le32 i_ino; /* only used for 32-bit stat compatibility */
97 /* 28 */__le32 i_checksum;
100 /* 32 bytes on-disk inode */
101 #define EROFS_INODE_LAYOUT_V1 0
102 /* 64 bytes on-disk inode */
103 #define EROFS_INODE_LAYOUT_V2 1
105 struct erofs_inode_v2 {
108 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
109 __le16 i_xattr_icount;
111 __le16 i_reserved; /* 8 bytes */
112 __le64 i_size; /* 16 bytes */
114 /* file total compressed blocks for data mapping 1 */
115 __le32 compressed_blocks;
118 /* for device files, used to indicate old/new device # */
122 /* only used for 32-bit stat compatibility */
123 __le32 i_ino; /* 24 bytes */
127 __le64 i_ctime; /* 32 bytes */
130 __u8 i_reserved2[12];
131 __le32 i_checksum; /* 64 bytes */
134 #define EROFS_MAX_SHARED_XATTRS (128)
135 /* h_shared_count between 129 ... 255 are special # */
136 #define EROFS_SHARED_XATTR_EXTENT (255)
139 * inline xattrs (n == i_xattr_icount):
140 * erofs_xattr_ibody_header(1) + (n - 1) * 4 bytes
143 * /-----------------------\
144 * | erofs_xattr_entries+ |
145 * +-----------------------+
146 * inline xattrs must starts in erofs_xattr_ibody_header,
147 * for read-only fs, no need to introduce h_refcount
149 struct erofs_xattr_ibody_header {
153 __le32 h_shared_xattrs[0]; /* shared xattr id array */
157 #define EROFS_XATTR_INDEX_USER 1
158 #define EROFS_XATTR_INDEX_POSIX_ACL_ACCESS 2
159 #define EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT 3
160 #define EROFS_XATTR_INDEX_TRUSTED 4
161 #define EROFS_XATTR_INDEX_LUSTRE 5
162 #define EROFS_XATTR_INDEX_SECURITY 6
164 /* xattr entry (for both inline & shared xattrs) */
165 struct erofs_xattr_entry {
166 __u8 e_name_len; /* length of name */
167 __u8 e_name_index; /* attribute name index */
168 __le16 e_value_size; /* size of attribute value */
169 /* followed by e_name and e_value */
170 char e_name[0]; /* attribute name */
173 #define ondisk_xattr_ibody_size(count) ({\
174 u32 __count = le16_to_cpu(count); \
175 ((__count) == 0) ? 0 : \
176 sizeof(struct erofs_xattr_ibody_header) + \
177 sizeof(__u32) * ((__count) - 1); })
179 #define EROFS_XATTR_ALIGN(size) round_up(size, sizeof(struct erofs_xattr_entry))
180 #define EROFS_XATTR_ENTRY_SIZE(entry) EROFS_XATTR_ALIGN( \
181 sizeof(struct erofs_xattr_entry) + \
182 (entry)->e_name_len + le16_to_cpu((entry)->e_value_size))
184 /* have to be aligned with 8 bytes on disk */
185 struct erofs_extent_header {
187 __le32 eh_reserved[3];
191 * Z_EROFS Variable-sized Logical Extent cluster type:
192 * 0 - literal (uncompressed) cluster
193 * 1 - compressed cluster (for the head logical cluster)
194 * 2 - compressed cluster (for the other logical clusters)
197 * 0 - literal (uncompressed) cluster,
199 * di_clusterofs = the literal data offset of the cluster
200 * di_blkaddr = the blkaddr of the literal cluster
202 * 1 - compressed cluster (for the head logical cluster)
204 * di_clusterofs = the decompressed data offset of the cluster
205 * di_blkaddr = the blkaddr of the compressed cluster
207 * 2 - compressed cluster (for the other logical clusters)
210 * the decompressed data offset in its own head cluster
211 * di_u.delta[0] = distance to its corresponding head cluster
212 * di_u.delta[1] = distance to its corresponding tail cluster
213 * (di_advise could be 0, 1 or 2)
215 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS 2
216 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT 0
218 struct z_erofs_vle_decompressed_index {
220 /* where to decompress in the head cluster */
221 __le16 di_clusterofs;
224 /* for the head cluster */
227 * for the rest clusters
228 * eg. for 4k page-sized cluster, maximum 4K*64k = 256M)
229 * [0] - pointing to the head cluster
230 * [1] - pointing to the tail cluster
233 } di_u __packed; /* 8 bytes */
236 #define Z_EROFS_VLE_EXTENT_ALIGN(size) round_up(size, \
237 sizeof(struct z_erofs_vle_decompressed_index))
239 /* dirent sorts in alphabet order, thus we can do binary search */
240 struct erofs_dirent {
241 __le64 nid; /* 0, node number */
242 __le16 nameoff; /* 8, start offset of file name */
243 __u8 file_type; /* 10, file type */
244 __u8 reserved; /* 11, reserved */
247 /* file types used in inode_info->flags */
260 #define EROFS_NAME_LEN 255
262 /* check the EROFS on-disk layout strictly at compile time */
263 static inline void erofs_check_ondisk_layout_definitions(void)
265 BUILD_BUG_ON(sizeof(struct erofs_super_block) != 128);
266 BUILD_BUG_ON(sizeof(struct erofs_inode_v1) != 32);
267 BUILD_BUG_ON(sizeof(struct erofs_inode_v2) != 64);
268 BUILD_BUG_ON(sizeof(struct erofs_xattr_ibody_header) != 12);
269 BUILD_BUG_ON(sizeof(struct erofs_xattr_entry) != 4);
270 BUILD_BUG_ON(sizeof(struct erofs_extent_header) != 16);
271 BUILD_BUG_ON(sizeof(struct z_erofs_vle_decompressed_index) != 8);
272 BUILD_BUG_ON(sizeof(struct erofs_dirent) != 12);