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