]> asedeno.scripts.mit.edu Git - linux.git/blob - fs/erofs/erofs_fs.h
erofs: move erofs out of staging
[linux.git] / fs / erofs / erofs_fs.h
1 /* SPDX-License-Identifier: GPL-2.0-only OR Apache-2.0 */
2 /*
3  * Copyright (C) 2017-2018 HUAWEI, Inc.
4  *             http://www.huawei.com/
5  * Created by Gao Xiang <gaoxiang25@huawei.com>
6  */
7 #ifndef __EROFS_FS_H
8 #define __EROFS_FS_H
9
10 /* Enhanced(Extended) ROM File System */
11 #define EROFS_SUPER_OFFSET      1024
12
13 /*
14  * Any bits that aren't in EROFS_ALL_REQUIREMENTS should be
15  * incompatible with this kernel version.
16  */
17 #define EROFS_REQUIREMENT_LZ4_0PADDING  0x00000001
18 #define EROFS_ALL_REQUIREMENTS          EROFS_REQUIREMENT_LZ4_0PADDING
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;        /* (aka. feature_compat) */
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 /* 80 */__le32 requirements;    /* (aka. feature_incompat) */
38
39 /* 84 */__u8 reserved2[44];
40 } __packed;                     /* 128 bytes */
41
42 /*
43  * erofs inode data mapping:
44  * 0 - inode plain without inline data A:
45  * inode, [xattrs], ... | ... | no-holed data
46  * 1 - inode VLE compression B (legacy):
47  * inode, [xattrs], extents ... | ...
48  * 2 - inode plain with inline data C:
49  * inode, [xattrs], last_inline_data, ... | ... | no-holed data
50  * 3 - inode compression D:
51  * inode, [xattrs], map_header, extents ... | ...
52  * 4~7 - reserved
53  */
54 enum {
55         EROFS_INODE_FLAT_PLAIN,
56         EROFS_INODE_FLAT_COMPRESSION_LEGACY,
57         EROFS_INODE_FLAT_INLINE,
58         EROFS_INODE_FLAT_COMPRESSION,
59         EROFS_INODE_LAYOUT_MAX
60 };
61
62 static inline bool erofs_inode_is_data_compressed(unsigned int datamode)
63 {
64         if (datamode == EROFS_INODE_FLAT_COMPRESSION)
65                 return true;
66         return datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY;
67 }
68
69 /* bit definitions of inode i_advise */
70 #define EROFS_I_VERSION_BITS            1
71 #define EROFS_I_DATA_MAPPING_BITS       3
72
73 #define EROFS_I_VERSION_BIT             0
74 #define EROFS_I_DATA_MAPPING_BIT        1
75
76 struct erofs_inode_v1 {
77 /*  0 */__le16 i_advise;
78
79 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
80 /*  2 */__le16 i_xattr_icount;
81 /*  4 */__le16 i_mode;
82 /*  6 */__le16 i_nlink;
83 /*  8 */__le32 i_size;
84 /* 12 */__le32 i_reserved;
85 /* 16 */union {
86                 /* file total compressed blocks for data mapping 1 */
87                 __le32 compressed_blocks;
88                 __le32 raw_blkaddr;
89
90                 /* for device files, used to indicate old/new device # */
91                 __le32 rdev;
92         } i_u __packed;
93 /* 20 */__le32 i_ino;           /* only used for 32-bit stat compatibility */
94 /* 24 */__le16 i_uid;
95 /* 26 */__le16 i_gid;
96 /* 28 */__le32 i_reserved2;
97 } __packed;
98
99 /* 32 bytes on-disk inode */
100 #define EROFS_INODE_LAYOUT_V1   0
101 /* 64 bytes on-disk inode */
102 #define EROFS_INODE_LAYOUT_V2   1
103
104 struct erofs_inode_v2 {
105 /*  0 */__le16 i_advise;
106
107 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
108 /*  2 */__le16 i_xattr_icount;
109 /*  4 */__le16 i_mode;
110 /*  6 */__le16 i_reserved;
111 /*  8 */__le64 i_size;
112 /* 16 */union {
113                 /* file total compressed blocks for data mapping 1 */
114                 __le32 compressed_blocks;
115                 __le32 raw_blkaddr;
116
117                 /* for device files, used to indicate old/new device # */
118                 __le32 rdev;
119         } i_u __packed;
120
121         /* only used for 32-bit stat compatibility */
122 /* 20 */__le32 i_ino;
123
124 /* 24 */__le32 i_uid;
125 /* 28 */__le32 i_gid;
126 /* 32 */__le64 i_ctime;
127 /* 40 */__le32 i_ctime_nsec;
128 /* 44 */__le32 i_nlink;
129 /* 48 */__u8   i_reserved2[16];
130 } __packed;                     /* 64 bytes */
131
132 #define EROFS_MAX_SHARED_XATTRS         (128)
133 /* h_shared_count between 129 ... 255 are special # */
134 #define EROFS_SHARED_XATTR_EXTENT       (255)
135
136 /*
137  * inline xattrs (n == i_xattr_icount):
138  * erofs_xattr_ibody_header(1) + (n - 1) * 4 bytes
139  *          12 bytes           /                   \
140  *                            /                     \
141  *                           /-----------------------\
142  *                           |  erofs_xattr_entries+ |
143  *                           +-----------------------+
144  * inline xattrs must starts in erofs_xattr_ibody_header,
145  * for read-only fs, no need to introduce h_refcount
146  */
147 struct erofs_xattr_ibody_header {
148         __le32 h_reserved;
149         __u8   h_shared_count;
150         __u8   h_reserved2[7];
151         __le32 h_shared_xattrs[0];      /* shared xattr id array */
152 } __packed;
153
154 /* Name indexes */
155 #define EROFS_XATTR_INDEX_USER              1
156 #define EROFS_XATTR_INDEX_POSIX_ACL_ACCESS  2
157 #define EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT 3
158 #define EROFS_XATTR_INDEX_TRUSTED           4
159 #define EROFS_XATTR_INDEX_LUSTRE            5
160 #define EROFS_XATTR_INDEX_SECURITY          6
161
162 /* xattr entry (for both inline & shared xattrs) */
163 struct erofs_xattr_entry {
164         __u8   e_name_len;      /* length of name */
165         __u8   e_name_index;    /* attribute name index */
166         __le16 e_value_size;    /* size of attribute value */
167         /* followed by e_name and e_value */
168         char   e_name[0];       /* attribute name */
169 } __packed;
170
171 #define ondisk_xattr_ibody_size(count)  ({\
172         u32 __count = le16_to_cpu(count); \
173         ((__count) == 0) ? 0 : \
174         sizeof(struct erofs_xattr_ibody_header) + \
175                 sizeof(__u32) * ((__count) - 1); })
176
177 #define EROFS_XATTR_ALIGN(size) round_up(size, sizeof(struct erofs_xattr_entry))
178 #define EROFS_XATTR_ENTRY_SIZE(entry) EROFS_XATTR_ALIGN( \
179         sizeof(struct erofs_xattr_entry) + \
180         (entry)->e_name_len + le16_to_cpu((entry)->e_value_size))
181
182 /* available compression algorithm types */
183 enum {
184         Z_EROFS_COMPRESSION_LZ4,
185         Z_EROFS_COMPRESSION_MAX
186 };
187
188 /*
189  * bit 0 : COMPACTED_2B indexes (0 - off; 1 - on)
190  *  e.g. for 4k logical cluster size,      4B        if compacted 2B is off;
191  *                                  (4B) + 2B + (4B) if compacted 2B is on.
192  */
193 #define Z_EROFS_ADVISE_COMPACTED_2B_BIT         0
194
195 #define Z_EROFS_ADVISE_COMPACTED_2B     (1 << Z_EROFS_ADVISE_COMPACTED_2B_BIT)
196
197 struct z_erofs_map_header {
198         __le32  h_reserved1;
199         __le16  h_advise;
200         /*
201          * bit 0-3 : algorithm type of head 1 (logical cluster type 01);
202          * bit 4-7 : algorithm type of head 2 (logical cluster type 11).
203          */
204         __u8    h_algorithmtype;
205         /*
206          * bit 0-2 : logical cluster bits - 12, e.g. 0 for 4096;
207          * bit 3-4 : (physical - logical) cluster bits of head 1:
208          *       For example, if logical clustersize = 4096, 1 for 8192.
209          * bit 5-7 : (physical - logical) cluster bits of head 2.
210          */
211         __u8    h_clusterbits;
212 };
213
214 #define Z_EROFS_VLE_LEGACY_HEADER_PADDING       8
215
216 /*
217  * Z_EROFS Variable-sized Logical Extent cluster type:
218  *    0 - literal (uncompressed) cluster
219  *    1 - compressed cluster (for the head logical cluster)
220  *    2 - compressed cluster (for the other logical clusters)
221  *
222  * In detail,
223  *    0 - literal (uncompressed) cluster,
224  *        di_advise = 0
225  *        di_clusterofs = the literal data offset of the cluster
226  *        di_blkaddr = the blkaddr of the literal cluster
227  *
228  *    1 - compressed cluster (for the head logical cluster)
229  *        di_advise = 1
230  *        di_clusterofs = the decompressed data offset of the cluster
231  *        di_blkaddr = the blkaddr of the compressed cluster
232  *
233  *    2 - compressed cluster (for the other logical clusters)
234  *        di_advise = 2
235  *        di_clusterofs =
236  *           the decompressed data offset in its own head cluster
237  *        di_u.delta[0] = distance to its corresponding head cluster
238  *        di_u.delta[1] = distance to its corresponding tail cluster
239  *                (di_advise could be 0, 1 or 2)
240  */
241 enum {
242         Z_EROFS_VLE_CLUSTER_TYPE_PLAIN,
243         Z_EROFS_VLE_CLUSTER_TYPE_HEAD,
244         Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD,
245         Z_EROFS_VLE_CLUSTER_TYPE_RESERVED,
246         Z_EROFS_VLE_CLUSTER_TYPE_MAX
247 };
248
249 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS        2
250 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT         0
251
252 struct z_erofs_vle_decompressed_index {
253         __le16 di_advise;
254         /* where to decompress in the head cluster */
255         __le16 di_clusterofs;
256
257         union {
258                 /* for the head cluster */
259                 __le32 blkaddr;
260                 /*
261                  * for the rest clusters
262                  * eg. for 4k page-sized cluster, maximum 4K*64k = 256M)
263                  * [0] - pointing to the head cluster
264                  * [1] - pointing to the tail cluster
265                  */
266                 __le16 delta[2];
267         } di_u __packed;                /* 8 bytes */
268 } __packed;
269
270 #define Z_EROFS_VLE_LEGACY_INDEX_ALIGN(size) \
271         (round_up(size, sizeof(struct z_erofs_vle_decompressed_index)) + \
272          sizeof(struct z_erofs_map_header) + Z_EROFS_VLE_LEGACY_HEADER_PADDING)
273
274 /* dirent sorts in alphabet order, thus we can do binary search */
275 struct erofs_dirent {
276         __le64 nid;     /*  0, node number */
277         __le16 nameoff; /*  8, start offset of file name */
278         __u8 file_type; /* 10, file type */
279         __u8 reserved;  /* 11, reserved */
280 } __packed;
281
282 /*
283  * EROFS file types should match generic FT_* types and
284  * it seems no need to add BUILD_BUG_ONs since potential
285  * unmatchness will break other fses as well...
286  */
287
288 #define EROFS_NAME_LEN      255
289
290 /* check the EROFS on-disk layout strictly at compile time */
291 static inline void erofs_check_ondisk_layout_definitions(void)
292 {
293         BUILD_BUG_ON(sizeof(struct erofs_super_block) != 128);
294         BUILD_BUG_ON(sizeof(struct erofs_inode_v1) != 32);
295         BUILD_BUG_ON(sizeof(struct erofs_inode_v2) != 64);
296         BUILD_BUG_ON(sizeof(struct erofs_xattr_ibody_header) != 12);
297         BUILD_BUG_ON(sizeof(struct erofs_xattr_entry) != 4);
298         BUILD_BUG_ON(sizeof(struct z_erofs_map_header) != 8);
299         BUILD_BUG_ON(sizeof(struct z_erofs_vle_decompressed_index) != 8);
300         BUILD_BUG_ON(sizeof(struct erofs_dirent) != 12);
301
302         BUILD_BUG_ON(BIT(Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) <
303                      Z_EROFS_VLE_CLUSTER_TYPE_MAX - 1);
304 }
305
306 #endif
307