GNU Linux-libre 4.19.281-gnu1
[releases.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_ALL_REQUIREMENTS  0
25
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;
32
33 /* 14 */__le16 root_nid;
34 /* 16 */__le64 inos;            /* total valid ino # (== f_files - f_favail) */
35
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) */
44
45 /* 84 */__u8 reserved2[44];
46 } __packed;                     /* 128 bytes */
47
48 #define __EROFS_BIT(_prefix, _cur, _pre)        enum {  \
49         _prefix ## _cur ## _BIT = _prefix ## _pre ## _BIT + \
50                 _prefix ## _pre ## _BITS }
51
52 /*
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
60  * 3~7 - reserved
61  */
62 enum {
63         EROFS_INODE_LAYOUT_PLAIN,
64         EROFS_INODE_LAYOUT_COMPRESSION,
65         EROFS_INODE_LAYOUT_INLINE,
66         EROFS_INODE_LAYOUT_MAX
67 };
68 #define EROFS_I_VERSION_BITS            1
69 #define EROFS_I_DATA_MAPPING_BITS       3
70
71 #define EROFS_I_VERSION_BIT             0
72 __EROFS_BIT(EROFS_I_, DATA_MAPPING, VERSION);
73
74 #define EROFS_I_ALL     \
75         ((1 << (EROFS_I_DATA_MAPPING_BIT + EROFS_I_DATA_MAPPING_BITS)) - 1)
76
77 struct erofs_inode_v1 {
78 /*  0 */__le16 i_advise;
79
80 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
81 /*  2 */__le16 i_xattr_icount;
82 /*  4 */__le16 i_mode;
83 /*  6 */__le16 i_nlink;
84 /*  8 */__le32 i_size;
85 /* 12 */__le32 i_reserved;
86 /* 16 */union {
87                 /* file total compressed blocks for data mapping 1 */
88                 __le32 compressed_blocks;
89                 __le32 raw_blkaddr;
90
91                 /* for device files, used to indicate old/new device # */
92                 __le32 rdev;
93         } i_u __packed;
94 /* 20 */__le32 i_ino;           /* only used for 32-bit stat compatibility */
95 /* 24 */__le16 i_uid;
96 /* 26 */__le16 i_gid;
97 /* 28 */__le32 i_checksum;
98 } __packed;
99
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
104
105 struct erofs_inode_v2 {
106         __le16 i_advise;
107
108         /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
109         __le16 i_xattr_icount;
110         __le16 i_mode;
111         __le16 i_reserved;      /* 8 bytes */
112         __le64 i_size;          /* 16 bytes */
113         union {
114                 /* file total compressed blocks for data mapping 1 */
115                 __le32 compressed_blocks;
116                 __le32 raw_blkaddr;
117
118                 /* for device files, used to indicate old/new device # */
119                 __le32 rdev;
120         } i_u __packed;
121
122         /* only used for 32-bit stat compatibility */
123         __le32 i_ino;           /* 24 bytes */
124
125         __le32 i_uid;
126         __le32 i_gid;
127         __le64 i_ctime;         /* 32 bytes */
128         __le32 i_ctime_nsec;
129         __le32 i_nlink;
130         __u8   i_reserved2[12];
131         __le32 i_checksum;      /* 64 bytes */
132 } __packed;
133
134 #define EROFS_MAX_SHARED_XATTRS         (128)
135 /* h_shared_count between 129 ... 255 are special # */
136 #define EROFS_SHARED_XATTR_EXTENT       (255)
137
138 /*
139  * inline xattrs (n == i_xattr_icount):
140  * erofs_xattr_ibody_header(1) + (n - 1) * 4 bytes
141  *          12 bytes           /                   \
142  *                            /                     \
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
148  */
149 struct erofs_xattr_ibody_header {
150         __le32 h_checksum;
151         __u8   h_shared_count;
152         __u8   h_reserved[7];
153         __le32 h_shared_xattrs[0];      /* shared xattr id array */
154 } __packed;
155
156 /* Name indexes */
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
163
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 */
171 } __packed;
172
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); })
178
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))
183
184 /* have to be aligned with 8 bytes on disk */
185 struct erofs_extent_header {
186         __le32 eh_checksum;
187         __le32 eh_reserved[3];
188 } __packed;
189
190 /*
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)
195  *
196  * In detail,
197  *    0 - literal (uncompressed) cluster,
198  *        di_advise = 0
199  *        di_clusterofs = the literal data offset of the cluster
200  *        di_blkaddr = the blkaddr of the literal cluster
201  *
202  *    1 - compressed cluster (for the head logical cluster)
203  *        di_advise = 1
204  *        di_clusterofs = the decompressed data offset of the cluster
205  *        di_blkaddr = the blkaddr of the compressed cluster
206  *
207  *    2 - compressed cluster (for the other logical clusters)
208  *        di_advise = 2
209  *        di_clusterofs =
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)
214  */
215 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS        2
216 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT         0
217
218 struct z_erofs_vle_decompressed_index {
219         __le16 di_advise;
220         /* where to decompress in the head cluster */
221         __le16 di_clusterofs;
222
223         union {
224                 /* for the head cluster */
225                 __le32 blkaddr;
226                 /*
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
231                  */
232                 __le16 delta[2];
233         } di_u __packed;                /* 8 bytes */
234 } __packed;
235
236 #define Z_EROFS_VLE_EXTENT_ALIGN(size) round_up(size, \
237         sizeof(struct z_erofs_vle_decompressed_index))
238
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 */
245 } __packed;
246
247 /* file types used in inode_info->flags */
248 enum {
249         EROFS_FT_UNKNOWN,
250         EROFS_FT_REG_FILE,
251         EROFS_FT_DIR,
252         EROFS_FT_CHRDEV,
253         EROFS_FT_BLKDEV,
254         EROFS_FT_FIFO,
255         EROFS_FT_SOCK,
256         EROFS_FT_SYMLINK,
257         EROFS_FT_MAX
258 };
259
260 #define EROFS_NAME_LEN      255
261
262 /* check the EROFS on-disk layout strictly at compile time */
263 static inline void erofs_check_ondisk_layout_definitions(void)
264 {
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);
273 }
274
275 #endif
276