blob: 6b62c7a4f5f569da8a4111a4ca92244ed7dea22c [file] [log] [blame]
Huang Jianan024fb2f2022-02-26 15:05:47 +08001/* SPDX-License-Identifier: GPL-2.0-only OR Apache-2.0 */
2/*
3 * EROFS (Enhanced ROM File System) on-disk format definition
4 *
5 * Copyright (C) 2017-2018 HUAWEI, Inc.
6 * http://www.huawei.com/
7 * Copyright (C) 2021, Alibaba Cloud
8 */
9#ifndef __EROFS_FS_H
10#define __EROFS_FS_H
11
12#include <asm/unaligned.h>
13#include <fs.h>
14#include <part.h>
15#include <stdint.h>
16#include <compiler.h>
17
18#define EROFS_SUPER_MAGIC_V1 0xE0F5E1E2
19#define EROFS_SUPER_OFFSET 1024
20
21#define EROFS_FEATURE_COMPAT_SB_CHKSUM 0x00000001
22
23/*
24 * Any bits that aren't in EROFS_ALL_FEATURE_INCOMPAT should
25 * be incompatible with this kernel version.
26 */
27#define EROFS_FEATURE_INCOMPAT_LZ4_0PADDING 0x00000001
28#define EROFS_FEATURE_INCOMPAT_COMPR_CFGS 0x00000002
29#define EROFS_FEATURE_INCOMPAT_BIG_PCLUSTER 0x00000002
30#define EROFS_FEATURE_INCOMPAT_CHUNKED_FILE 0x00000004
31#define EROFS_FEATURE_INCOMPAT_DEVICE_TABLE 0x00000008
32#define EROFS_ALL_FEATURE_INCOMPAT \
33 (EROFS_FEATURE_INCOMPAT_LZ4_0PADDING | \
34 EROFS_FEATURE_INCOMPAT_COMPR_CFGS | \
35 EROFS_FEATURE_INCOMPAT_BIG_PCLUSTER | \
36 EROFS_FEATURE_INCOMPAT_CHUNKED_FILE | \
37 EROFS_FEATURE_INCOMPAT_DEVICE_TABLE)
38
39#define EROFS_SB_EXTSLOT_SIZE 16
40
41struct erofs_deviceslot {
42 union {
43 u8 uuid[16]; /* used for device manager later */
44 u8 userdata[64]; /* digest(sha256), etc. */
45 } u;
46 __le32 blocks; /* total fs blocks of this device */
47 __le32 mapped_blkaddr; /* map starting at mapped_blkaddr */
48 u8 reserved[56];
49};
50
51#define EROFS_DEVT_SLOT_SIZE sizeof(struct erofs_deviceslot)
52
53/* erofs on-disk super block (currently 128 bytes) */
54struct erofs_super_block {
55 __le32 magic; /* file system magic number */
56 __le32 checksum; /* crc32c(super_block) */
57 __le32 feature_compat;
58 __u8 blkszbits; /* support block_size == PAGE_SIZE only */
59 __u8 sb_extslots; /* superblock size = 128 + sb_extslots * 16 */
60
61 __le16 root_nid; /* nid of root directory */
62 __le64 inos; /* total valid ino # (== f_files - f_favail) */
63
64 __le64 build_time; /* inode v1 time derivation */
65 __le32 build_time_nsec; /* inode v1 time derivation in nano scale */
66 __le32 blocks; /* used for statfs */
67 __le32 meta_blkaddr; /* start block address of metadata area */
68 __le32 xattr_blkaddr; /* start block address of shared xattr area */
69 __u8 uuid[16]; /* 128-bit uuid for volume */
70 __u8 volume_name[16]; /* volume name */
71 __le32 feature_incompat;
72 union {
73 /* bitmap for available compression algorithms */
74 __le16 available_compr_algs;
75 /* customized sliding window size instead of 64k by default */
76 __le16 lz4_max_distance;
77 } __packed u1;
78 __le16 extra_devices; /* # of devices besides the primary device */
79 __le16 devt_slotoff; /* startoff = devt_slotoff * devt_slotsize */
80 __u8 reserved2[38];
81};
82
83/*
84 * erofs inode datalayout (i_format in on-disk inode):
85 * 0 - inode plain without inline data A:
86 * inode, [xattrs], ... | ... | no-holed data
87 * 1 - inode VLE compression B (legacy):
88 * inode, [xattrs], extents ... | ...
89 * 2 - inode plain with inline data C:
90 * inode, [xattrs], last_inline_data, ... | ... | no-holed data
91 * 3 - inode compression D:
92 * inode, [xattrs], map_header, extents ... | ...
93 * 4 - inode chunk-based E:
94 * inode, [xattrs], chunk indexes ... | ...
95 * 5~7 - reserved
96 */
97enum {
98 EROFS_INODE_FLAT_PLAIN = 0,
99 EROFS_INODE_FLAT_COMPRESSION_LEGACY = 1,
100 EROFS_INODE_FLAT_INLINE = 2,
101 EROFS_INODE_FLAT_COMPRESSION = 3,
102 EROFS_INODE_CHUNK_BASED = 4,
103 EROFS_INODE_DATALAYOUT_MAX
104};
105
106static inline bool erofs_inode_is_data_compressed(unsigned int datamode)
107{
108 return datamode == EROFS_INODE_FLAT_COMPRESSION ||
109 datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY;
110}
111
112/* bit definitions of inode i_advise */
113#define EROFS_I_VERSION_BITS 1
114#define EROFS_I_DATALAYOUT_BITS 3
115
116#define EROFS_I_VERSION_BIT 0
117#define EROFS_I_DATALAYOUT_BIT 1
118
119#define EROFS_I_ALL \
120 ((1 << (EROFS_I_DATALAYOUT_BIT + EROFS_I_DATALAYOUT_BITS)) - 1)
121
122/* indicate chunk blkbits, thus 'chunksize = blocksize << chunk blkbits' */
123#define EROFS_CHUNK_FORMAT_BLKBITS_MASK 0x001F
124/* with chunk indexes or just a 4-byte blkaddr array */
125#define EROFS_CHUNK_FORMAT_INDEXES 0x0020
126
127#define EROFS_CHUNK_FORMAT_ALL \
128 (EROFS_CHUNK_FORMAT_BLKBITS_MASK | EROFS_CHUNK_FORMAT_INDEXES)
129
130struct erofs_inode_chunk_info {
131 __le16 format; /* chunk blkbits, etc. */
132 __le16 reserved;
133};
134
135/* 32-byte reduced form of an ondisk inode */
136struct erofs_inode_compact {
137 __le16 i_format; /* inode format hints */
138
139/* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
140 __le16 i_xattr_icount;
141 __le16 i_mode;
142 __le16 i_nlink;
143 __le32 i_size;
144 __le32 i_reserved;
145 union {
146 /* file total compressed blocks for data mapping 1 */
147 __le32 compressed_blocks;
148 __le32 raw_blkaddr;
149
150 /* for device files, used to indicate old/new device # */
151 __le32 rdev;
152
153 /* for chunk-based files, it contains the summary info */
154 struct erofs_inode_chunk_info c;
155 } i_u;
156 __le32 i_ino; /* only used for 32-bit stat compatibility */
157 __le16 i_uid;
158 __le16 i_gid;
159 __le32 i_reserved2;
160};
161
162/* 32 bytes on-disk inode */
163#define EROFS_INODE_LAYOUT_COMPACT 0
164/* 64 bytes on-disk inode */
165#define EROFS_INODE_LAYOUT_EXTENDED 1
166
167/* 64-byte complete form of an ondisk inode */
168struct erofs_inode_extended {
169 __le16 i_format; /* inode format hints */
170
171/* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
172 __le16 i_xattr_icount;
173 __le16 i_mode;
174 __le16 i_reserved;
175 __le64 i_size;
176 union {
177 /* file total compressed blocks for data mapping 1 */
178 __le32 compressed_blocks;
179 __le32 raw_blkaddr;
180
181 /* for device files, used to indicate old/new device # */
182 __le32 rdev;
183
184 /* for chunk-based files, it contains the summary info */
185 struct erofs_inode_chunk_info c;
186 } i_u;
187
188 /* only used for 32-bit stat compatibility */
189 __le32 i_ino;
190
191 __le32 i_uid;
192 __le32 i_gid;
193 __le64 i_ctime;
194 __le32 i_ctime_nsec;
195 __le32 i_nlink;
196 __u8 i_reserved2[16];
197};
198
199#define EROFS_MAX_SHARED_XATTRS (128)
200/* h_shared_count between 129 ... 255 are special # */
201#define EROFS_SHARED_XATTR_EXTENT (255)
202
203/*
204 * inline xattrs (n == i_xattr_icount):
205 * erofs_xattr_ibody_header(1) + (n - 1) * 4 bytes
206 * 12 bytes / \
207 * / \
208 * /-----------------------\
209 * | erofs_xattr_entries+ |
210 * +-----------------------+
211 * inline xattrs must starts in erofs_xattr_ibody_header,
212 * for read-only fs, no need to introduce h_refcount
213 */
214struct erofs_xattr_ibody_header {
215 __le32 h_reserved;
216 __u8 h_shared_count;
217 __u8 h_reserved2[7];
218 __le32 h_shared_xattrs[0]; /* shared xattr id array */
219};
220
221/* Name indexes */
222#define EROFS_XATTR_INDEX_USER 1
223#define EROFS_XATTR_INDEX_POSIX_ACL_ACCESS 2
224#define EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT 3
225#define EROFS_XATTR_INDEX_TRUSTED 4
226#define EROFS_XATTR_INDEX_LUSTRE 5
227#define EROFS_XATTR_INDEX_SECURITY 6
228
229/* xattr entry (for both inline & shared xattrs) */
230struct erofs_xattr_entry {
231 __u8 e_name_len; /* length of name */
232 __u8 e_name_index; /* attribute name index */
233 __le16 e_value_size; /* size of attribute value */
234 /* followed by e_name and e_value */
235 char e_name[0]; /* attribute name */
236};
237
238static inline unsigned int erofs_xattr_ibody_size(__le16 i_xattr_icount)
239{
240 if (!i_xattr_icount)
241 return 0;
242
243 return sizeof(struct erofs_xattr_ibody_header) +
244 sizeof(__u32) * (le16_to_cpu(i_xattr_icount) - 1);
245}
246
247#define EROFS_XATTR_ALIGN(size) round_up(size, sizeof(struct erofs_xattr_entry))
248
249static inline unsigned int erofs_xattr_entry_size(struct erofs_xattr_entry *e)
250{
251 return EROFS_XATTR_ALIGN(sizeof(struct erofs_xattr_entry) +
252 e->e_name_len + le16_to_cpu(e->e_value_size));
253}
254
255/* represent a zeroed chunk (hole) */
256#define EROFS_NULL_ADDR -1
257
258/* 4-byte block address array */
259#define EROFS_BLOCK_MAP_ENTRY_SIZE sizeof(__le32)
260
261/* 8-byte inode chunk indexes */
262struct erofs_inode_chunk_index {
263 __le16 advise; /* always 0, don't care for now */
264 __le16 device_id; /* back-end storage id (with bits masked) */
265 __le32 blkaddr; /* start block address of this inode chunk */
266};
267
268/* maximum supported size of a physical compression cluster */
269#define Z_EROFS_PCLUSTER_MAX_SIZE (1024 * 1024)
270
271/* available compression algorithm types (for h_algorithmtype) */
272enum {
273 Z_EROFS_COMPRESSION_LZ4 = 0,
274 Z_EROFS_COMPRESSION_LZMA = 1,
275 Z_EROFS_COMPRESSION_MAX
276};
277
278#define Z_EROFS_ALL_COMPR_ALGS (1 << (Z_EROFS_COMPRESSION_MAX - 1))
279
280/* 14 bytes (+ length field = 16 bytes) */
281struct z_erofs_lz4_cfgs {
282 __le16 max_distance;
283 __le16 max_pclusterblks;
284 u8 reserved[10];
285} __packed;
286
287/* 14 bytes (+ length field = 16 bytes) */
288struct z_erofs_lzma_cfgs {
289 __le32 dict_size;
290 __le16 format;
291 u8 reserved[8];
292} __packed;
293#define Z_EROFS_LZMA_MAX_DICT_SIZE (8 * Z_EROFS_PCLUSTER_MAX_SIZE)
294
295/*
296 * bit 0 : COMPACTED_2B indexes (0 - off; 1 - on)
297 * e.g. for 4k logical cluster size, 4B if compacted 2B is off;
298 * (4B) + 2B + (4B) if compacted 2B is on.
299 * bit 1 : HEAD1 big pcluster (0 - off; 1 - on)
300 * bit 2 : HEAD2 big pcluster (0 - off; 1 - on)
301 */
302#define Z_EROFS_ADVISE_COMPACTED_2B 0x0001
303#define Z_EROFS_ADVISE_BIG_PCLUSTER_1 0x0002
304#define Z_EROFS_ADVISE_BIG_PCLUSTER_2 0x0004
305
306struct z_erofs_map_header {
307 __le32 h_reserved1;
308 __le16 h_advise;
309 /*
310 * bit 0-3 : algorithm type of head 1 (logical cluster type 01);
311 * bit 4-7 : algorithm type of head 2 (logical cluster type 11).
312 */
313 __u8 h_algorithmtype;
314 /*
315 * bit 0-2 : logical cluster bits - 12, e.g. 0 for 4096;
316 * bit 3-7 : reserved.
317 */
318 __u8 h_clusterbits;
319};
320
321#define Z_EROFS_VLE_LEGACY_HEADER_PADDING 8
322
323/*
324 * Fixed-sized output compression ondisk Logical Extent cluster type:
325 * 0 - literal (uncompressed) cluster
326 * 1 - compressed cluster (for the head logical cluster)
327 * 2 - compressed cluster (for the other logical clusters)
328 *
329 * In detail,
330 * 0 - literal (uncompressed) cluster,
331 * di_advise = 0
332 * di_clusterofs = the literal data offset of the cluster
333 * di_blkaddr = the blkaddr of the literal cluster
334 *
335 * 1 - compressed cluster (for the head logical cluster)
336 * di_advise = 1
337 * di_clusterofs = the decompressed data offset of the cluster
338 * di_blkaddr = the blkaddr of the compressed cluster
339 *
340 * 2 - compressed cluster (for the other logical clusters)
341 * di_advise = 2
342 * di_clusterofs =
343 * the decompressed data offset in its own head cluster
344 * di_u.delta[0] = distance to its corresponding head cluster
345 * di_u.delta[1] = distance to its corresponding tail cluster
346 * (di_advise could be 0, 1 or 2)
347 */
348enum {
349 Z_EROFS_VLE_CLUSTER_TYPE_PLAIN = 0,
350 Z_EROFS_VLE_CLUSTER_TYPE_HEAD = 1,
351 Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD = 2,
352 Z_EROFS_VLE_CLUSTER_TYPE_RESERVED = 3,
353 Z_EROFS_VLE_CLUSTER_TYPE_MAX
354};
355
356#define Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS 2
357#define Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT 0
358
359/*
360 * D0_CBLKCNT will be marked _only_ at the 1st non-head lcluster to store the
361 * compressed block count of a compressed extent (in logical clusters, aka.
362 * block count of a pcluster).
363 */
364#define Z_EROFS_VLE_DI_D0_CBLKCNT (1 << 11)
365
366struct z_erofs_vle_decompressed_index {
367 __le16 di_advise;
368 /* where to decompress in the head cluster */
369 __le16 di_clusterofs;
370
371 union {
372 /* for the head cluster */
373 __le32 blkaddr;
374 /*
375 * for the rest clusters
376 * eg. for 4k page-sized cluster, maximum 4K*64k = 256M)
377 * [0] - pointing to the head cluster
378 * [1] - pointing to the tail cluster
379 */
380 __le16 delta[2];
381 } di_u;
382};
383
384#define Z_EROFS_VLE_LEGACY_INDEX_ALIGN(size) \
385 (round_up(size, sizeof(struct z_erofs_vle_decompressed_index)) + \
386 sizeof(struct z_erofs_map_header) + Z_EROFS_VLE_LEGACY_HEADER_PADDING)
387
388#define Z_EROFS_VLE_EXTENT_ALIGN(size) round_up(size, \
389 sizeof(struct z_erofs_vle_decompressed_index))
390
391/* dirent sorts in alphabet order, thus we can do binary search */
392struct erofs_dirent {
393 __le64 nid; /* node number */
394 __le16 nameoff; /* start offset of file name */
395 __u8 file_type; /* file type */
396 __u8 reserved; /* reserved */
397} __packed;
398
399/* file types used in inode_info->flags */
400enum {
401 EROFS_FT_UNKNOWN,
402 EROFS_FT_REG_FILE,
403 EROFS_FT_DIR,
404 EROFS_FT_CHRDEV,
405 EROFS_FT_BLKDEV,
406 EROFS_FT_FIFO,
407 EROFS_FT_SOCK,
408 EROFS_FT_SYMLINK,
409 EROFS_FT_MAX
410};
411
412#define EROFS_NAME_LEN 255
413
414/* check the EROFS on-disk layout strictly at compile time */
415static inline void erofs_check_ondisk_layout_definitions(void)
416{
417 BUILD_BUG_ON(sizeof(struct erofs_super_block) != 128);
418 BUILD_BUG_ON(sizeof(struct erofs_inode_compact) != 32);
419 BUILD_BUG_ON(sizeof(struct erofs_inode_extended) != 64);
420 BUILD_BUG_ON(sizeof(struct erofs_xattr_ibody_header) != 12);
421 BUILD_BUG_ON(sizeof(struct erofs_xattr_entry) != 4);
422 BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_info) != 4);
423 BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_index) != 8);
424 BUILD_BUG_ON(sizeof(struct z_erofs_map_header) != 8);
425 BUILD_BUG_ON(sizeof(struct z_erofs_vle_decompressed_index) != 8);
426 BUILD_BUG_ON(sizeof(struct erofs_dirent) != 12);
427 /* keep in sync between 2 index structures for better extendibility */
428 BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_index) !=
429 sizeof(struct z_erofs_vle_decompressed_index));
430 BUILD_BUG_ON(sizeof(struct erofs_deviceslot) != 128);
431
432 BUILD_BUG_ON(BIT(Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) <
433 Z_EROFS_VLE_CLUSTER_TYPE_MAX - 1);
434}
435
436#endif