]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/staging/erofs/erofs_fs.h
Merge branch 'for-linus-5.3' of git://git.kernel.org/pub/scm/linux/kernel/git/konrad/ibft
[linux.git] / drivers / staging / erofs / erofs_fs.h
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 /*
21  * Any bits that aren't in EROFS_ALL_REQUIREMENTS should be
22  * incompatible with this kernel version.
23  */
24 #define EROFS_REQUIREMENT_LZ4_0PADDING  0x00000001
25 #define EROFS_ALL_REQUIREMENTS          EROFS_REQUIREMENT_LZ4_0PADDING
26
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;
33
34 /* 14 */__le16 root_nid;
35 /* 16 */__le64 inos;            /* total valid ino # (== f_files - f_favail) */
36
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) */
45
46 /* 84 */__u8 reserved2[44];
47 } __packed;                     /* 128 bytes */
48
49 /*
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 ... | ...
59  * 4~7 - reserved
60  */
61 enum {
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
67 };
68
69 static bool erofs_inode_is_data_compressed(unsigned int datamode)
70 {
71         if (datamode == EROFS_INODE_FLAT_COMPRESSION)
72                 return true;
73         return datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY;
74 }
75
76 /* bit definitions of inode i_advise */
77 #define EROFS_I_VERSION_BITS            1
78 #define EROFS_I_DATA_MAPPING_BITS       3
79
80 #define EROFS_I_VERSION_BIT             0
81 #define EROFS_I_DATA_MAPPING_BIT        1
82
83 struct erofs_inode_v1 {
84 /*  0 */__le16 i_advise;
85
86 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
87 /*  2 */__le16 i_xattr_icount;
88 /*  4 */__le16 i_mode;
89 /*  6 */__le16 i_nlink;
90 /*  8 */__le32 i_size;
91 /* 12 */__le32 i_reserved;
92 /* 16 */union {
93                 /* file total compressed blocks for data mapping 1 */
94                 __le32 compressed_blocks;
95                 __le32 raw_blkaddr;
96
97                 /* for device files, used to indicate old/new device # */
98                 __le32 rdev;
99         } i_u __packed;
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;
104 } __packed;
105
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
110
111 struct erofs_inode_v2 {
112         __le16 i_advise;
113
114         /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
115         __le16 i_xattr_icount;
116         __le16 i_mode;
117         __le16 i_reserved;      /* 8 bytes */
118         __le64 i_size;          /* 16 bytes */
119         union {
120                 /* file total compressed blocks for data mapping 1 */
121                 __le32 compressed_blocks;
122                 __le32 raw_blkaddr;
123
124                 /* for device files, used to indicate old/new device # */
125                 __le32 rdev;
126         } i_u __packed;
127
128         /* only used for 32-bit stat compatibility */
129         __le32 i_ino;           /* 24 bytes */
130
131         __le32 i_uid;
132         __le32 i_gid;
133         __le64 i_ctime;         /* 32 bytes */
134         __le32 i_ctime_nsec;
135         __le32 i_nlink;
136         __u8   i_reserved2[12];
137         __le32 i_checksum;      /* 64 bytes */
138 } __packed;
139
140 #define EROFS_MAX_SHARED_XATTRS         (128)
141 /* h_shared_count between 129 ... 255 are special # */
142 #define EROFS_SHARED_XATTR_EXTENT       (255)
143
144 /*
145  * inline xattrs (n == i_xattr_icount):
146  * erofs_xattr_ibody_header(1) + (n - 1) * 4 bytes
147  *          12 bytes           /                   \
148  *                            /                     \
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
154  */
155 struct erofs_xattr_ibody_header {
156         __le32 h_checksum;
157         __u8   h_shared_count;
158         __u8   h_reserved[7];
159         __le32 h_shared_xattrs[0];      /* shared xattr id array */
160 } __packed;
161
162 /* Name indexes */
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
169
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 */
177 } __packed;
178
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); })
184
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))
189
190 /* available compression algorithm types */
191 enum {
192         Z_EROFS_COMPRESSION_LZ4,
193         Z_EROFS_COMPRESSION_MAX
194 };
195
196 /*
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.
200  */
201 #define Z_EROFS_ADVISE_COMPACTED_2B_BIT         0
202
203 #define Z_EROFS_ADVISE_COMPACTED_2B     (1 << Z_EROFS_ADVISE_COMPACTED_2B_BIT)
204
205 struct z_erofs_map_header {
206         __le32  h_reserved1;
207         __le16  h_advise;
208         /*
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).
211          */
212         __u8    h_algorithmtype;
213         /*
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.
218          */
219         __u8    h_clusterbits;
220 };
221
222 #define Z_EROFS_VLE_LEGACY_HEADER_PADDING       8
223
224 /*
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)
229  *
230  * In detail,
231  *    0 - literal (uncompressed) cluster,
232  *        di_advise = 0
233  *        di_clusterofs = the literal data offset of the cluster
234  *        di_blkaddr = the blkaddr of the literal cluster
235  *
236  *    1 - compressed cluster (for the head logical cluster)
237  *        di_advise = 1
238  *        di_clusterofs = the decompressed data offset of the cluster
239  *        di_blkaddr = the blkaddr of the compressed cluster
240  *
241  *    2 - compressed cluster (for the other logical clusters)
242  *        di_advise = 2
243  *        di_clusterofs =
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)
248  */
249 enum {
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
255 };
256
257 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS        2
258 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT         0
259
260 struct z_erofs_vle_decompressed_index {
261         __le16 di_advise;
262         /* where to decompress in the head cluster */
263         __le16 di_clusterofs;
264
265         union {
266                 /* for the head cluster */
267                 __le32 blkaddr;
268                 /*
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
273                  */
274                 __le16 delta[2];
275         } di_u __packed;                /* 8 bytes */
276 } __packed;
277
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)
281
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 */
288 } __packed;
289
290 /* file types used in inode_info->flags */
291 enum {
292         EROFS_FT_UNKNOWN,
293         EROFS_FT_REG_FILE,
294         EROFS_FT_DIR,
295         EROFS_FT_CHRDEV,
296         EROFS_FT_BLKDEV,
297         EROFS_FT_FIFO,
298         EROFS_FT_SOCK,
299         EROFS_FT_SYMLINK,
300         EROFS_FT_MAX
301 };
302
303 #define EROFS_NAME_LEN      255
304
305 /* check the EROFS on-disk layout strictly at compile time */
306 static inline void erofs_check_ondisk_layout_definitions(void)
307 {
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);
316
317         BUILD_BUG_ON(BIT(Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) <
318                      Z_EROFS_VLE_CLUSTER_TYPE_MAX - 1);
319 }
320
321 #endif
322