1 /* SPDX-License-Identifier: GPL-2.0 OR Apache-2.0
2 *
3 * linux/drivers/staging/erofs/erofs_fs.h
4 *
5 * Copyright (C) 2017-2018 HUAWEI, Inc.
6 * http://www.huawei.com/
7 * Created by Gao Xiang <gaoxiang25@huawei.com>
8 *
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.
12 */
13 #ifndef __EROFS_FS_H
14 #define __EROFS_FS_H
15
16 /* Enhanced(Extended) ROM File System */
17 #define EROFS_SUPER_MAGIC_V1 0xE0F5E1E2
18 #define EROFS_SUPER_OFFSET 1024
19
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;
26
27 /* 14 */__le16 root_nid;
28 /* 16 */__le64 inos; /* total valid ino # (== f_files - f_favail) */
29
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 */
37
38 /* 80 */__u8 reserved2[48]; /* 128 bytes */
39 } __packed;
40
41 #define __EROFS_BIT(_prefix, _cur, _pre) enum { \
42 _prefix ## _cur ## _BIT = _prefix ## _pre ## _BIT + \
43 _prefix ## _pre ## _BITS }
44
45 /*
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
53 * 3~7 - reserved
54 */
55 enum {
56 EROFS_INODE_LAYOUT_PLAIN,
57 EROFS_INODE_LAYOUT_COMPRESSION,
58 EROFS_INODE_LAYOUT_INLINE,
59 EROFS_INODE_LAYOUT_MAX
60 };
61 #define EROFS_I_VERSION_BITS 1
62 #define EROFS_I_DATA_MAPPING_BITS 3
63
64 #define EROFS_I_VERSION_BIT 0
65 __EROFS_BIT(EROFS_I_, DATA_MAPPING, VERSION);
66
67 struct erofs_inode_v1 {
68 /* 0 */__le16 i_advise;
69
70 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
71 /* 2 */__le16 i_xattr_icount;
72 /* 4 */__le16 i_mode;
73 /* 6 */__le16 i_nlink;
74 /* 8 */__le32 i_size;
75 /* 12 */__le32 i_reserved;
76 /* 16 */union {
77 /* file total compressed blocks for data mapping 1 */
78 __le32 compressed_blocks;
79 __le32 raw_blkaddr;
80
81 /* for device files, used to indicate old/new device # */
82 __le32 rdev;
83 } i_u __packed;
84 /* 20 */__le32 i_ino; /* only used for 32-bit stat compatibility */
85 /* 24 */__le16 i_uid;
86 /* 26 */__le16 i_gid;
87 /* 28 */__le32 i_checksum;
88 } __packed;
89
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
94
95 struct erofs_inode_v2 {
96 __le16 i_advise;
97
98 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
99 __le16 i_xattr_icount;
100 __le16 i_mode;
101 __le16 i_reserved; /* 8 bytes */
102 __le64 i_size; /* 16 bytes */
103 union {
104 /* file total compressed blocks for data mapping 1 */
105 __le32 compressed_blocks;
106 __le32 raw_blkaddr;
107
108 /* for device files, used to indicate old/new device # */
109 __le32 rdev;
110 } i_u __packed;
111
112 /* only used for 32-bit stat compatibility */
113 __le32 i_ino; /* 24 bytes */
114
115 __le32 i_uid;
116 __le32 i_gid;
117 __le64 i_ctime; /* 32 bytes */
118 __le32 i_ctime_nsec;
119 __le32 i_nlink;
120 __u8 i_reserved2[12];
121 __le32 i_checksum; /* 64 bytes */
122 } __packed;
123
124 #define EROFS_MAX_SHARED_XATTRS (128)
125 /* h_shared_count between 129 ... 255 are special # */
126 #define EROFS_SHARED_XATTR_EXTENT (255)
127
128 /*
129 * inline xattrs (n == i_xattr_icount):
130 * erofs_xattr_ibody_header(1) + (n - 1) * 4 bytes
131 * 12 bytes / \
132 * / \
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
138 */
139 struct erofs_xattr_ibody_header {
140 __le32 h_checksum;
141 __u8 h_shared_count;
142 __u8 h_reserved[7];
143 __le32 h_shared_xattrs[0]; /* shared xattr id array */
144 } __packed;
145
146 /* Name indexes */
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
153
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 */
161 } __packed;
162
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); })
168
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))
173
174 /* have to be aligned with 8 bytes on disk */
175 struct erofs_extent_header {
176 __le32 eh_checksum;
177 __le32 eh_reserved[3];
178 } __packed;
179
180 /*
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)
185 *
186 * In detail,
187 * 0 - literal (uncompressed) cluster,
188 * di_advise = 0
189 * di_clusterofs = the literal data offset of the cluster
190 * di_blkaddr = the blkaddr of the literal cluster
191 *
192 * 1 - compressed cluster (for the head logical cluster)
193 * di_advise = 1
194 * di_clusterofs = the decompressed data offset of the cluster
195 * di_blkaddr = the blkaddr of the compressed cluster
196 *
197 * 2 - compressed cluster (for the other logical clusters)
198 * di_advise = 2
199 * di_clusterofs =
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)
204 */
205 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS 2
206 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT 0
207
208 struct z_erofs_vle_decompressed_index {
209 __le16 di_advise;
210 /* where to decompress in the head cluster */
211 __le16 di_clusterofs;
212
213 union {
214 /* for the head cluster */
215 __le32 blkaddr;
216 /*
217 * for the rest clusters
218 * eg. for 4k page-sized cluster, maximum 4K*64k = 256M)
219 * [0] - pointing to the head cluster
220 * [1] - pointing to the tail cluster
221 */
222 __le16 delta[2];
223 } di_u __packed; /* 8 bytes */
224 } __packed;
225
226 #define Z_EROFS_VLE_EXTENT_ALIGN(size) round_up(size, \
227 sizeof(struct z_erofs_vle_decompressed_index))
228
229 /* dirent sorts in alphabet order, thus we can do binary search */
230 struct erofs_dirent {
231 __le64 nid; /* 0, node number */
232 __le16 nameoff; /* 8, start offset of file name */
233 __u8 file_type; /* 10, file type */
234 __u8 reserved; /* 11, reserved */
235 } __packed;
236
237 /* file types used in inode_info->flags */
238 enum {
239 EROFS_FT_UNKNOWN,
240 EROFS_FT_REG_FILE,
241 EROFS_FT_DIR,
242 EROFS_FT_CHRDEV,
243 EROFS_FT_BLKDEV,
244 EROFS_FT_FIFO,
245 EROFS_FT_SOCK,
246 EROFS_FT_SYMLINK,
247 EROFS_FT_MAX
248 };
249
250 #define EROFS_NAME_LEN 255
251
252 /* check the EROFS on-disk layout strictly at compile time */
erofs_check_ondisk_layout_definitions(void)253 static inline void erofs_check_ondisk_layout_definitions(void)
254 {
255 BUILD_BUG_ON(sizeof(struct erofs_super_block) != 128);
256 BUILD_BUG_ON(sizeof(struct erofs_inode_v1) != 32);
257 BUILD_BUG_ON(sizeof(struct erofs_inode_v2) != 64);
258 BUILD_BUG_ON(sizeof(struct erofs_xattr_ibody_header) != 12);
259 BUILD_BUG_ON(sizeof(struct erofs_xattr_entry) != 4);
260 BUILD_BUG_ON(sizeof(struct erofs_extent_header) != 16);
261 BUILD_BUG_ON(sizeof(struct z_erofs_vle_decompressed_index) != 8);
262 BUILD_BUG_ON(sizeof(struct erofs_dirent) != 12);
263 }
264
265 #endif
266
267