blob: 0f8da74570b440706de8d47ed0792fde3515be8e [file] [log] [blame]
Gao Xiang29b24f62019-07-31 23:57:31 +08001/* SPDX-License-Identifier: GPL-2.0-only OR Apache-2.0 */
2/*
Gao Xiangea559e72019-09-04 10:08:57 +08003 * EROFS (Enhanced ROM File System) on-disk format definition
4 *
Gao Xiangaea12862018-07-26 20:21:44 +08005 * Copyright (C) 2017-2018 HUAWEI, Inc.
Alexander A. Klimov592e7cd2020-07-13 15:09:44 +02006 * https://www.huawei.com/
Gao Xiangaea12862018-07-26 20:21:44 +08007 */
8#ifndef __EROFS_FS_H
9#define __EROFS_FS_H
10
Gao Xiangaea12862018-07-26 20:21:44 +080011#define EROFS_SUPER_OFFSET 1024
12
Pratik Shindeb858a482019-11-04 10:49:37 +080013#define EROFS_FEATURE_COMPAT_SB_CHKSUM 0x00000001
14
Gao Xiang5efe5132019-06-13 16:35:41 +080015/*
Gao Xiang426a9302019-09-04 10:08:53 +080016 * Any bits that aren't in EROFS_ALL_FEATURE_INCOMPAT should
17 * be incompatible with this kernel version.
Gao Xiang5efe5132019-06-13 16:35:41 +080018 */
Gao Xiang426a9302019-09-04 10:08:53 +080019#define EROFS_FEATURE_INCOMPAT_LZ4_0PADDING 0x00000001
Gao Xiang14373712021-03-29 18:00:12 +080020#define EROFS_FEATURE_INCOMPAT_COMPR_CFGS 0x00000002
Gao Xiang5404c33012021-04-07 12:39:22 +080021#define EROFS_FEATURE_INCOMPAT_BIG_PCLUSTER 0x00000002
Gao Xiang8e6c8fa2021-04-07 12:39:27 +080022#define EROFS_ALL_FEATURE_INCOMPAT \
23 (EROFS_FEATURE_INCOMPAT_LZ4_0PADDING | \
24 EROFS_FEATURE_INCOMPAT_COMPR_CFGS | \
25 EROFS_FEATURE_INCOMPAT_BIG_PCLUSTER)
Gao Xiang5efe5132019-06-13 16:35:41 +080026
Gao Xiang14373712021-03-29 18:00:12 +080027#define EROFS_SB_EXTSLOT_SIZE 16
28
29/* erofs on-disk super block (currently 128 bytes) */
Gao Xiangaea12862018-07-26 20:21:44 +080030struct erofs_super_block {
Gao Xiang4b66eb52019-09-04 10:08:48 +080031 __le32 magic; /* file system magic number */
32 __le32 checksum; /* crc32c(super_block) */
Gao Xiang426a9302019-09-04 10:08:53 +080033 __le32 feature_compat;
Gao Xiang4b66eb52019-09-04 10:08:48 +080034 __u8 blkszbits; /* support block_size == PAGE_SIZE only */
Gao Xiang14373712021-03-29 18:00:12 +080035 __u8 sb_extslots; /* superblock size = 128 + sb_extslots * 16 */
Gao Xiangaea12862018-07-26 20:21:44 +080036
Gao Xiang4b66eb52019-09-04 10:08:48 +080037 __le16 root_nid; /* nid of root directory */
38 __le64 inos; /* total valid ino # (== f_files - f_favail) */
Gao Xiangaea12862018-07-26 20:21:44 +080039
Gao Xiang4b66eb52019-09-04 10:08:48 +080040 __le64 build_time; /* inode v1 time derivation */
41 __le32 build_time_nsec; /* inode v1 time derivation in nano scale */
42 __le32 blocks; /* used for statfs */
43 __le32 meta_blkaddr; /* start block address of metadata area */
44 __le32 xattr_blkaddr; /* start block address of shared xattr area */
45 __u8 uuid[16]; /* 128-bit uuid for volume */
46 __u8 volume_name[16]; /* volume name */
Gao Xiang426a9302019-09-04 10:08:53 +080047 __le32 feature_incompat;
Gao Xiang14373712021-03-29 18:00:12 +080048 union {
49 /* bitmap for available compression algorithms */
50 __le16 available_compr_algs;
51 /* customized sliding window size instead of 64k by default */
52 __le16 lz4_max_distance;
53 } __packed u1;
Huang Jianan5d505382021-03-29 09:23:06 +080054 __u8 reserved2[42];
Gao Xianged34aa42019-09-04 10:08:51 +080055};
Gao Xiangaea12862018-07-26 20:21:44 +080056
Gao Xiangaea12862018-07-26 20:21:44 +080057/*
Gao Xiangea559e72019-09-04 10:08:57 +080058 * erofs inode datalayout (i_format in on-disk inode):
Gao Xiangaea12862018-07-26 20:21:44 +080059 * 0 - inode plain without inline data A:
60 * inode, [xattrs], ... | ... | no-holed data
Gao Xiangec8c2442019-06-24 15:22:51 +080061 * 1 - inode VLE compression B (legacy):
Gao Xiangaea12862018-07-26 20:21:44 +080062 * inode, [xattrs], extents ... | ...
63 * 2 - inode plain with inline data C:
64 * inode, [xattrs], last_inline_data, ... | ... | no-holed data
Gao Xiangec8c2442019-06-24 15:22:51 +080065 * 3 - inode compression D:
66 * inode, [xattrs], map_header, extents ... | ...
67 * 4~7 - reserved
Gao Xiangaea12862018-07-26 20:21:44 +080068 */
69enum {
Gao Xiang60a49ba2019-09-04 10:08:49 +080070 EROFS_INODE_FLAT_PLAIN = 0,
71 EROFS_INODE_FLAT_COMPRESSION_LEGACY = 1,
72 EROFS_INODE_FLAT_INLINE = 2,
73 EROFS_INODE_FLAT_COMPRESSION = 3,
Gao Xiang8a765682019-09-04 10:08:54 +080074 EROFS_INODE_DATALAYOUT_MAX
Gao Xiangaea12862018-07-26 20:21:44 +080075};
Gao Xiangccd9c192018-12-12 01:57:30 +080076
Gao Xiang3d2969f2019-08-13 10:30:52 +080077static inline bool erofs_inode_is_data_compressed(unsigned int datamode)
Gao Xiangec8c2442019-06-24 15:22:51 +080078{
Gao Xiangc39747f2019-09-04 10:08:52 +080079 return datamode == EROFS_INODE_FLAT_COMPRESSION ||
80 datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY;
Gao Xiangec8c2442019-06-24 15:22:51 +080081}
82
Gao Xiangccd9c192018-12-12 01:57:30 +080083/* bit definitions of inode i_advise */
Gao Xiangaea12862018-07-26 20:21:44 +080084#define EROFS_I_VERSION_BITS 1
Gao Xiang8a765682019-09-04 10:08:54 +080085#define EROFS_I_DATALAYOUT_BITS 3
Gao Xiangaea12862018-07-26 20:21:44 +080086
87#define EROFS_I_VERSION_BIT 0
Gao Xiang8a765682019-09-04 10:08:54 +080088#define EROFS_I_DATALAYOUT_BIT 1
Gao Xiangaea12862018-07-26 20:21:44 +080089
Gao Xiang24a806d2021-03-29 08:36:14 +080090#define EROFS_I_ALL \
91 ((1 << (EROFS_I_DATALAYOUT_BIT + EROFS_I_DATALAYOUT_BITS)) - 1)
92
Gao Xiang4b66eb52019-09-04 10:08:48 +080093/* 32-byte reduced form of an ondisk inode */
Gao Xiang8a765682019-09-04 10:08:54 +080094struct erofs_inode_compact {
95 __le16 i_format; /* inode format hints */
Gao Xiangaea12862018-07-26 20:21:44 +080096
97/* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
Gao Xiang4b66eb52019-09-04 10:08:48 +080098 __le16 i_xattr_icount;
99 __le16 i_mode;
100 __le16 i_nlink;
101 __le32 i_size;
102 __le32 i_reserved;
103 union {
Gao Xiangaea12862018-07-26 20:21:44 +0800104 /* file total compressed blocks for data mapping 1 */
105 __le32 compressed_blocks;
106 __le32 raw_blkaddr;
107
108 /* for device files, used to indicate old/new device # */
109 __le32 rdev;
Gao Xianged34aa42019-09-04 10:08:51 +0800110 } i_u;
Gao Xiang4b66eb52019-09-04 10:08:48 +0800111 __le32 i_ino; /* only used for 32-bit stat compatibility */
112 __le16 i_uid;
113 __le16 i_gid;
114 __le32 i_reserved2;
Gao Xianged34aa42019-09-04 10:08:51 +0800115};
Gao Xiangaea12862018-07-26 20:21:44 +0800116
117/* 32 bytes on-disk inode */
Gao Xiang8a765682019-09-04 10:08:54 +0800118#define EROFS_INODE_LAYOUT_COMPACT 0
Gao Xiangaea12862018-07-26 20:21:44 +0800119/* 64 bytes on-disk inode */
Gao Xiang8a765682019-09-04 10:08:54 +0800120#define EROFS_INODE_LAYOUT_EXTENDED 1
Gao Xiangaea12862018-07-26 20:21:44 +0800121
Gao Xiang4b66eb52019-09-04 10:08:48 +0800122/* 64-byte complete form of an ondisk inode */
Gao Xiang8a765682019-09-04 10:08:54 +0800123struct erofs_inode_extended {
124 __le16 i_format; /* inode format hints */
Gao Xiangaea12862018-07-26 20:21:44 +0800125
Gao Xiangcead56f2019-07-31 23:57:34 +0800126/* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
Gao Xiang4b66eb52019-09-04 10:08:48 +0800127 __le16 i_xattr_icount;
128 __le16 i_mode;
129 __le16 i_reserved;
130 __le64 i_size;
131 union {
Gao Xiangaea12862018-07-26 20:21:44 +0800132 /* file total compressed blocks for data mapping 1 */
133 __le32 compressed_blocks;
134 __le32 raw_blkaddr;
135
136 /* for device files, used to indicate old/new device # */
137 __le32 rdev;
Gao Xianged34aa42019-09-04 10:08:51 +0800138 } i_u;
Gao Xiangaea12862018-07-26 20:21:44 +0800139
140 /* only used for 32-bit stat compatibility */
Gao Xiang4b66eb52019-09-04 10:08:48 +0800141 __le32 i_ino;
Gao Xiangaea12862018-07-26 20:21:44 +0800142
Gao Xiang4b66eb52019-09-04 10:08:48 +0800143 __le32 i_uid;
144 __le32 i_gid;
145 __le64 i_ctime;
146 __le32 i_ctime_nsec;
147 __le32 i_nlink;
148 __u8 i_reserved2[16];
Gao Xianged34aa42019-09-04 10:08:51 +0800149};
Gao Xiangaea12862018-07-26 20:21:44 +0800150
151#define EROFS_MAX_SHARED_XATTRS (128)
152/* h_shared_count between 129 ... 255 are special # */
153#define EROFS_SHARED_XATTR_EXTENT (255)
154
155/*
156 * inline xattrs (n == i_xattr_icount):
157 * erofs_xattr_ibody_header(1) + (n - 1) * 4 bytes
158 * 12 bytes / \
159 * / \
160 * /-----------------------\
161 * | erofs_xattr_entries+ |
162 * +-----------------------+
163 * inline xattrs must starts in erofs_xattr_ibody_header,
164 * for read-only fs, no need to introduce h_refcount
165 */
166struct erofs_xattr_ibody_header {
Gao Xiangcead56f2019-07-31 23:57:34 +0800167 __le32 h_reserved;
Gao Xiangaea12862018-07-26 20:21:44 +0800168 __u8 h_shared_count;
Gao Xiangcead56f2019-07-31 23:57:34 +0800169 __u8 h_reserved2[7];
Gao Xiangaea12862018-07-26 20:21:44 +0800170 __le32 h_shared_xattrs[0]; /* shared xattr id array */
Gao Xianged34aa42019-09-04 10:08:51 +0800171};
Gao Xiangaea12862018-07-26 20:21:44 +0800172
173/* Name indexes */
174#define EROFS_XATTR_INDEX_USER 1
175#define EROFS_XATTR_INDEX_POSIX_ACL_ACCESS 2
176#define EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT 3
177#define EROFS_XATTR_INDEX_TRUSTED 4
178#define EROFS_XATTR_INDEX_LUSTRE 5
179#define EROFS_XATTR_INDEX_SECURITY 6
180
181/* xattr entry (for both inline & shared xattrs) */
182struct erofs_xattr_entry {
183 __u8 e_name_len; /* length of name */
184 __u8 e_name_index; /* attribute name index */
185 __le16 e_value_size; /* size of attribute value */
186 /* followed by e_name and e_value */
187 char e_name[0]; /* attribute name */
Gao Xianged34aa42019-09-04 10:08:51 +0800188};
Gao Xiangaea12862018-07-26 20:21:44 +0800189
Gao Xiangb6796ab2019-09-04 10:08:50 +0800190static inline unsigned int erofs_xattr_ibody_size(__le16 i_xattr_icount)
191{
192 if (!i_xattr_icount)
193 return 0;
194
195 return sizeof(struct erofs_xattr_ibody_header) +
196 sizeof(__u32) * (le16_to_cpu(i_xattr_icount) - 1);
197}
Gao Xiangaea12862018-07-26 20:21:44 +0800198
199#define EROFS_XATTR_ALIGN(size) round_up(size, sizeof(struct erofs_xattr_entry))
Gao Xiangb6796ab2019-09-04 10:08:50 +0800200
201static inline unsigned int erofs_xattr_entry_size(struct erofs_xattr_entry *e)
202{
203 return EROFS_XATTR_ALIGN(sizeof(struct erofs_xattr_entry) +
204 e->e_name_len + le16_to_cpu(e->e_value_size));
205}
Gao Xiangaea12862018-07-26 20:21:44 +0800206
Gao Xiang9f6cc762021-04-07 12:39:20 +0800207/* maximum supported size of a physical compression cluster */
208#define Z_EROFS_PCLUSTER_MAX_SIZE (1024 * 1024)
209
Gao Xiangea559e72019-09-04 10:08:57 +0800210/* available compression algorithm types (for h_algorithmtype) */
Gao Xiangec8c2442019-06-24 15:22:51 +0800211enum {
Gao Xiang60a49ba2019-09-04 10:08:49 +0800212 Z_EROFS_COMPRESSION_LZ4 = 0,
Gao Xiangec8c2442019-06-24 15:22:51 +0800213 Z_EROFS_COMPRESSION_MAX
214};
Gao Xiang14373712021-03-29 18:00:12 +0800215#define Z_EROFS_ALL_COMPR_ALGS (1 << (Z_EROFS_COMPRESSION_MAX - 1))
Gao Xiangec8c2442019-06-24 15:22:51 +0800216
Gao Xiang46249cd2021-03-29 09:23:07 +0800217/* 14 bytes (+ length field = 16 bytes) */
218struct z_erofs_lz4_cfgs {
219 __le16 max_distance;
Gao Xiang5404c33012021-04-07 12:39:22 +0800220 __le16 max_pclusterblks;
221 u8 reserved[10];
Gao Xiang46249cd2021-03-29 09:23:07 +0800222} __packed;
223
Gao Xiangec8c2442019-06-24 15:22:51 +0800224/*
225 * bit 0 : COMPACTED_2B indexes (0 - off; 1 - on)
226 * e.g. for 4k logical cluster size, 4B if compacted 2B is off;
227 * (4B) + 2B + (4B) if compacted 2B is on.
Gao Xiang5404c33012021-04-07 12:39:22 +0800228 * bit 1 : HEAD1 big pcluster (0 - off; 1 - on)
229 * bit 2 : HEAD2 big pcluster (0 - off; 1 - on)
Gao Xiangec8c2442019-06-24 15:22:51 +0800230 */
Gao Xiang5404c33012021-04-07 12:39:22 +0800231#define Z_EROFS_ADVISE_COMPACTED_2B 0x0001
232#define Z_EROFS_ADVISE_BIG_PCLUSTER_1 0x0002
233#define Z_EROFS_ADVISE_BIG_PCLUSTER_2 0x0004
Gao Xiangec8c2442019-06-24 15:22:51 +0800234
235struct z_erofs_map_header {
236 __le32 h_reserved1;
237 __le16 h_advise;
238 /*
239 * bit 0-3 : algorithm type of head 1 (logical cluster type 01);
240 * bit 4-7 : algorithm type of head 2 (logical cluster type 11).
241 */
242 __u8 h_algorithmtype;
243 /*
244 * bit 0-2 : logical cluster bits - 12, e.g. 0 for 4096;
Gao Xiang54e0b6c2021-04-07 12:39:18 +0800245 * bit 3-7 : reserved.
Gao Xiangec8c2442019-06-24 15:22:51 +0800246 */
247 __u8 h_clusterbits;
248};
249
250#define Z_EROFS_VLE_LEGACY_HEADER_PADDING 8
Gao Xiangaea12862018-07-26 20:21:44 +0800251
252/*
Gao Xiangea559e72019-09-04 10:08:57 +0800253 * Fixed-sized output compression ondisk Logical Extent cluster type:
Gao Xiangaea12862018-07-26 20:21:44 +0800254 * 0 - literal (uncompressed) cluster
255 * 1 - compressed cluster (for the head logical cluster)
256 * 2 - compressed cluster (for the other logical clusters)
257 *
258 * In detail,
259 * 0 - literal (uncompressed) cluster,
260 * di_advise = 0
261 * di_clusterofs = the literal data offset of the cluster
262 * di_blkaddr = the blkaddr of the literal cluster
263 *
264 * 1 - compressed cluster (for the head logical cluster)
265 * di_advise = 1
266 * di_clusterofs = the decompressed data offset of the cluster
267 * di_blkaddr = the blkaddr of the compressed cluster
268 *
269 * 2 - compressed cluster (for the other logical clusters)
270 * di_advise = 2
271 * di_clusterofs =
272 * the decompressed data offset in its own head cluster
273 * di_u.delta[0] = distance to its corresponding head cluster
274 * di_u.delta[1] = distance to its corresponding tail cluster
275 * (di_advise could be 0, 1 or 2)
276 */
Gao Xiang99691b42018-08-21 22:49:33 +0800277enum {
Gao Xiang60a49ba2019-09-04 10:08:49 +0800278 Z_EROFS_VLE_CLUSTER_TYPE_PLAIN = 0,
279 Z_EROFS_VLE_CLUSTER_TYPE_HEAD = 1,
280 Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD = 2,
281 Z_EROFS_VLE_CLUSTER_TYPE_RESERVED = 3,
Gao Xiang99691b42018-08-21 22:49:33 +0800282 Z_EROFS_VLE_CLUSTER_TYPE_MAX
283};
284
Gao Xiangaea12862018-07-26 20:21:44 +0800285#define Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS 2
286#define Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT 0
287
Gao Xiang5404c33012021-04-07 12:39:22 +0800288/*
289 * D0_CBLKCNT will be marked _only_ at the 1st non-head lcluster to store the
290 * compressed block count of a compressed extent (in logical clusters, aka.
291 * block count of a pcluster).
292 */
293#define Z_EROFS_VLE_DI_D0_CBLKCNT (1 << 11)
294
Gao Xiangaea12862018-07-26 20:21:44 +0800295struct z_erofs_vle_decompressed_index {
296 __le16 di_advise;
297 /* where to decompress in the head cluster */
298 __le16 di_clusterofs;
299
300 union {
301 /* for the head cluster */
302 __le32 blkaddr;
303 /*
304 * for the rest clusters
305 * eg. for 4k page-sized cluster, maximum 4K*64k = 256M)
306 * [0] - pointing to the head cluster
307 * [1] - pointing to the tail cluster
308 */
309 __le16 delta[2];
Gao Xianged34aa42019-09-04 10:08:51 +0800310 } di_u;
311};
Gao Xiangaea12862018-07-26 20:21:44 +0800312
Gao Xiangec8c2442019-06-24 15:22:51 +0800313#define Z_EROFS_VLE_LEGACY_INDEX_ALIGN(size) \
314 (round_up(size, sizeof(struct z_erofs_vle_decompressed_index)) + \
315 sizeof(struct z_erofs_map_header) + Z_EROFS_VLE_LEGACY_HEADER_PADDING)
Gao Xiangaea12862018-07-26 20:21:44 +0800316
317/* dirent sorts in alphabet order, thus we can do binary search */
318struct erofs_dirent {
Gao Xiang4b66eb52019-09-04 10:08:48 +0800319 __le64 nid; /* node number */
320 __le16 nameoff; /* start offset of file name */
321 __u8 file_type; /* file type */
322 __u8 reserved; /* reserved */
Gao Xiangaea12862018-07-26 20:21:44 +0800323} __packed;
324
Gao Xiang1d819c52019-08-16 15:11:42 +0800325/*
326 * EROFS file types should match generic FT_* types and
327 * it seems no need to add BUILD_BUG_ONs since potential
328 * unmatchness will break other fses as well...
329 */
Gao Xiangaea12862018-07-26 20:21:44 +0800330
331#define EROFS_NAME_LEN 255
332
333/* check the EROFS on-disk layout strictly at compile time */
334static inline void erofs_check_ondisk_layout_definitions(void)
335{
336 BUILD_BUG_ON(sizeof(struct erofs_super_block) != 128);
Gao Xiang8a765682019-09-04 10:08:54 +0800337 BUILD_BUG_ON(sizeof(struct erofs_inode_compact) != 32);
338 BUILD_BUG_ON(sizeof(struct erofs_inode_extended) != 64);
Gao Xiangaea12862018-07-26 20:21:44 +0800339 BUILD_BUG_ON(sizeof(struct erofs_xattr_ibody_header) != 12);
340 BUILD_BUG_ON(sizeof(struct erofs_xattr_entry) != 4);
Gao Xiangec8c2442019-06-24 15:22:51 +0800341 BUILD_BUG_ON(sizeof(struct z_erofs_map_header) != 8);
Gao Xiangaea12862018-07-26 20:21:44 +0800342 BUILD_BUG_ON(sizeof(struct z_erofs_vle_decompressed_index) != 8);
343 BUILD_BUG_ON(sizeof(struct erofs_dirent) != 12);
Gao Xiang99691b42018-08-21 22:49:33 +0800344
345 BUILD_BUG_ON(BIT(Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) <
346 Z_EROFS_VLE_CLUSTER_TYPE_MAX - 1);
Gao Xiangaea12862018-07-26 20:21:44 +0800347}
348
349#endif