blob: ecc3a0ea0bc4f15942b70a30bf8b276051c69492 [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 * Created by Gao Xiang <gaoxiang25@huawei.com>
Gao Xiangaea12862018-07-26 20:21:44 +08008 */
9#ifndef __EROFS_FS_H
10#define __EROFS_FS_H
11
Gao Xiangaea12862018-07-26 20:21:44 +080012#define EROFS_SUPER_OFFSET 1024
13
Pratik Shindeb858a482019-11-04 10:49:37 +080014#define EROFS_FEATURE_COMPAT_SB_CHKSUM 0x00000001
15
Gao Xiang5efe5132019-06-13 16:35:41 +080016/*
Gao Xiang426a9302019-09-04 10:08:53 +080017 * Any bits that aren't in EROFS_ALL_FEATURE_INCOMPAT should
18 * be incompatible with this kernel version.
Gao Xiang5efe5132019-06-13 16:35:41 +080019 */
Gao Xiang426a9302019-09-04 10:08:53 +080020#define EROFS_FEATURE_INCOMPAT_LZ4_0PADDING 0x00000001
Gao Xiang14373712021-03-29 18:00:12 +080021#define EROFS_FEATURE_INCOMPAT_COMPR_CFGS 0x00000002
Gao Xiang5404c33012021-04-07 12:39:22 +080022#define EROFS_FEATURE_INCOMPAT_BIG_PCLUSTER 0x00000002
Gao Xiang426a9302019-09-04 10:08:53 +080023#define EROFS_ALL_FEATURE_INCOMPAT EROFS_FEATURE_INCOMPAT_LZ4_0PADDING
Gao Xiang5efe5132019-06-13 16:35:41 +080024
Gao Xiang14373712021-03-29 18:00:12 +080025#define EROFS_SB_EXTSLOT_SIZE 16
26
27/* erofs on-disk super block (currently 128 bytes) */
Gao Xiangaea12862018-07-26 20:21:44 +080028struct erofs_super_block {
Gao Xiang4b66eb52019-09-04 10:08:48 +080029 __le32 magic; /* file system magic number */
30 __le32 checksum; /* crc32c(super_block) */
Gao Xiang426a9302019-09-04 10:08:53 +080031 __le32 feature_compat;
Gao Xiang4b66eb52019-09-04 10:08:48 +080032 __u8 blkszbits; /* support block_size == PAGE_SIZE only */
Gao Xiang14373712021-03-29 18:00:12 +080033 __u8 sb_extslots; /* superblock size = 128 + sb_extslots * 16 */
Gao Xiangaea12862018-07-26 20:21:44 +080034
Gao Xiang4b66eb52019-09-04 10:08:48 +080035 __le16 root_nid; /* nid of root directory */
36 __le64 inos; /* total valid ino # (== f_files - f_favail) */
Gao Xiangaea12862018-07-26 20:21:44 +080037
Gao Xiang4b66eb52019-09-04 10:08:48 +080038 __le64 build_time; /* inode v1 time derivation */
39 __le32 build_time_nsec; /* inode v1 time derivation in nano scale */
40 __le32 blocks; /* used for statfs */
41 __le32 meta_blkaddr; /* start block address of metadata area */
42 __le32 xattr_blkaddr; /* start block address of shared xattr area */
43 __u8 uuid[16]; /* 128-bit uuid for volume */
44 __u8 volume_name[16]; /* volume name */
Gao Xiang426a9302019-09-04 10:08:53 +080045 __le32 feature_incompat;
Gao Xiang14373712021-03-29 18:00:12 +080046 union {
47 /* bitmap for available compression algorithms */
48 __le16 available_compr_algs;
49 /* customized sliding window size instead of 64k by default */
50 __le16 lz4_max_distance;
51 } __packed u1;
Huang Jianan5d505382021-03-29 09:23:06 +080052 __u8 reserved2[42];
Gao Xianged34aa42019-09-04 10:08:51 +080053};
Gao Xiangaea12862018-07-26 20:21:44 +080054
Gao Xiangaea12862018-07-26 20:21:44 +080055/*
Gao Xiangea559e72019-09-04 10:08:57 +080056 * erofs inode datalayout (i_format in on-disk inode):
Gao Xiangaea12862018-07-26 20:21:44 +080057 * 0 - inode plain without inline data A:
58 * inode, [xattrs], ... | ... | no-holed data
Gao Xiangec8c2442019-06-24 15:22:51 +080059 * 1 - inode VLE compression B (legacy):
Gao Xiangaea12862018-07-26 20:21:44 +080060 * inode, [xattrs], extents ... | ...
61 * 2 - inode plain with inline data C:
62 * inode, [xattrs], last_inline_data, ... | ... | no-holed data
Gao Xiangec8c2442019-06-24 15:22:51 +080063 * 3 - inode compression D:
64 * inode, [xattrs], map_header, extents ... | ...
65 * 4~7 - reserved
Gao Xiangaea12862018-07-26 20:21:44 +080066 */
67enum {
Gao Xiang60a49ba2019-09-04 10:08:49 +080068 EROFS_INODE_FLAT_PLAIN = 0,
69 EROFS_INODE_FLAT_COMPRESSION_LEGACY = 1,
70 EROFS_INODE_FLAT_INLINE = 2,
71 EROFS_INODE_FLAT_COMPRESSION = 3,
Gao Xiang8a765682019-09-04 10:08:54 +080072 EROFS_INODE_DATALAYOUT_MAX
Gao Xiangaea12862018-07-26 20:21:44 +080073};
Gao Xiangccd9c192018-12-12 01:57:30 +080074
Gao Xiang3d2969f2019-08-13 10:30:52 +080075static inline bool erofs_inode_is_data_compressed(unsigned int datamode)
Gao Xiangec8c2442019-06-24 15:22:51 +080076{
Gao Xiangc39747f2019-09-04 10:08:52 +080077 return datamode == EROFS_INODE_FLAT_COMPRESSION ||
78 datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY;
Gao Xiangec8c2442019-06-24 15:22:51 +080079}
80
Gao Xiangccd9c192018-12-12 01:57:30 +080081/* bit definitions of inode i_advise */
Gao Xiangaea12862018-07-26 20:21:44 +080082#define EROFS_I_VERSION_BITS 1
Gao Xiang8a765682019-09-04 10:08:54 +080083#define EROFS_I_DATALAYOUT_BITS 3
Gao Xiangaea12862018-07-26 20:21:44 +080084
85#define EROFS_I_VERSION_BIT 0
Gao Xiang8a765682019-09-04 10:08:54 +080086#define EROFS_I_DATALAYOUT_BIT 1
Gao Xiangaea12862018-07-26 20:21:44 +080087
Gao Xiang24a806d2021-03-29 08:36:14 +080088#define EROFS_I_ALL \
89 ((1 << (EROFS_I_DATALAYOUT_BIT + EROFS_I_DATALAYOUT_BITS)) - 1)
90
Gao Xiang4b66eb52019-09-04 10:08:48 +080091/* 32-byte reduced form of an ondisk inode */
Gao Xiang8a765682019-09-04 10:08:54 +080092struct erofs_inode_compact {
93 __le16 i_format; /* inode format hints */
Gao Xiangaea12862018-07-26 20:21:44 +080094
95/* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
Gao Xiang4b66eb52019-09-04 10:08:48 +080096 __le16 i_xattr_icount;
97 __le16 i_mode;
98 __le16 i_nlink;
99 __le32 i_size;
100 __le32 i_reserved;
101 union {
Gao Xiangaea12862018-07-26 20:21:44 +0800102 /* file total compressed blocks for data mapping 1 */
103 __le32 compressed_blocks;
104 __le32 raw_blkaddr;
105
106 /* for device files, used to indicate old/new device # */
107 __le32 rdev;
Gao Xianged34aa42019-09-04 10:08:51 +0800108 } i_u;
Gao Xiang4b66eb52019-09-04 10:08:48 +0800109 __le32 i_ino; /* only used for 32-bit stat compatibility */
110 __le16 i_uid;
111 __le16 i_gid;
112 __le32 i_reserved2;
Gao Xianged34aa42019-09-04 10:08:51 +0800113};
Gao Xiangaea12862018-07-26 20:21:44 +0800114
115/* 32 bytes on-disk inode */
Gao Xiang8a765682019-09-04 10:08:54 +0800116#define EROFS_INODE_LAYOUT_COMPACT 0
Gao Xiangaea12862018-07-26 20:21:44 +0800117/* 64 bytes on-disk inode */
Gao Xiang8a765682019-09-04 10:08:54 +0800118#define EROFS_INODE_LAYOUT_EXTENDED 1
Gao Xiangaea12862018-07-26 20:21:44 +0800119
Gao Xiang4b66eb52019-09-04 10:08:48 +0800120/* 64-byte complete form of an ondisk inode */
Gao Xiang8a765682019-09-04 10:08:54 +0800121struct erofs_inode_extended {
122 __le16 i_format; /* inode format hints */
Gao Xiangaea12862018-07-26 20:21:44 +0800123
Gao Xiangcead56f2019-07-31 23:57:34 +0800124/* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
Gao Xiang4b66eb52019-09-04 10:08:48 +0800125 __le16 i_xattr_icount;
126 __le16 i_mode;
127 __le16 i_reserved;
128 __le64 i_size;
129 union {
Gao Xiangaea12862018-07-26 20:21:44 +0800130 /* file total compressed blocks for data mapping 1 */
131 __le32 compressed_blocks;
132 __le32 raw_blkaddr;
133
134 /* for device files, used to indicate old/new device # */
135 __le32 rdev;
Gao Xianged34aa42019-09-04 10:08:51 +0800136 } i_u;
Gao Xiangaea12862018-07-26 20:21:44 +0800137
138 /* only used for 32-bit stat compatibility */
Gao Xiang4b66eb52019-09-04 10:08:48 +0800139 __le32 i_ino;
Gao Xiangaea12862018-07-26 20:21:44 +0800140
Gao Xiang4b66eb52019-09-04 10:08:48 +0800141 __le32 i_uid;
142 __le32 i_gid;
143 __le64 i_ctime;
144 __le32 i_ctime_nsec;
145 __le32 i_nlink;
146 __u8 i_reserved2[16];
Gao Xianged34aa42019-09-04 10:08:51 +0800147};
Gao Xiangaea12862018-07-26 20:21:44 +0800148
149#define EROFS_MAX_SHARED_XATTRS (128)
150/* h_shared_count between 129 ... 255 are special # */
151#define EROFS_SHARED_XATTR_EXTENT (255)
152
153/*
154 * inline xattrs (n == i_xattr_icount):
155 * erofs_xattr_ibody_header(1) + (n - 1) * 4 bytes
156 * 12 bytes / \
157 * / \
158 * /-----------------------\
159 * | erofs_xattr_entries+ |
160 * +-----------------------+
161 * inline xattrs must starts in erofs_xattr_ibody_header,
162 * for read-only fs, no need to introduce h_refcount
163 */
164struct erofs_xattr_ibody_header {
Gao Xiangcead56f2019-07-31 23:57:34 +0800165 __le32 h_reserved;
Gao Xiangaea12862018-07-26 20:21:44 +0800166 __u8 h_shared_count;
Gao Xiangcead56f2019-07-31 23:57:34 +0800167 __u8 h_reserved2[7];
Gao Xiangaea12862018-07-26 20:21:44 +0800168 __le32 h_shared_xattrs[0]; /* shared xattr id array */
Gao Xianged34aa42019-09-04 10:08:51 +0800169};
Gao Xiangaea12862018-07-26 20:21:44 +0800170
171/* Name indexes */
172#define EROFS_XATTR_INDEX_USER 1
173#define EROFS_XATTR_INDEX_POSIX_ACL_ACCESS 2
174#define EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT 3
175#define EROFS_XATTR_INDEX_TRUSTED 4
176#define EROFS_XATTR_INDEX_LUSTRE 5
177#define EROFS_XATTR_INDEX_SECURITY 6
178
179/* xattr entry (for both inline & shared xattrs) */
180struct erofs_xattr_entry {
181 __u8 e_name_len; /* length of name */
182 __u8 e_name_index; /* attribute name index */
183 __le16 e_value_size; /* size of attribute value */
184 /* followed by e_name and e_value */
185 char e_name[0]; /* attribute name */
Gao Xianged34aa42019-09-04 10:08:51 +0800186};
Gao Xiangaea12862018-07-26 20:21:44 +0800187
Gao Xiangb6796ab2019-09-04 10:08:50 +0800188static inline unsigned int erofs_xattr_ibody_size(__le16 i_xattr_icount)
189{
190 if (!i_xattr_icount)
191 return 0;
192
193 return sizeof(struct erofs_xattr_ibody_header) +
194 sizeof(__u32) * (le16_to_cpu(i_xattr_icount) - 1);
195}
Gao Xiangaea12862018-07-26 20:21:44 +0800196
197#define EROFS_XATTR_ALIGN(size) round_up(size, sizeof(struct erofs_xattr_entry))
Gao Xiangb6796ab2019-09-04 10:08:50 +0800198
199static inline unsigned int erofs_xattr_entry_size(struct erofs_xattr_entry *e)
200{
201 return EROFS_XATTR_ALIGN(sizeof(struct erofs_xattr_entry) +
202 e->e_name_len + le16_to_cpu(e->e_value_size));
203}
Gao Xiangaea12862018-07-26 20:21:44 +0800204
Gao Xiang9f6cc762021-04-07 12:39:20 +0800205/* maximum supported size of a physical compression cluster */
206#define Z_EROFS_PCLUSTER_MAX_SIZE (1024 * 1024)
207
Gao Xiangea559e72019-09-04 10:08:57 +0800208/* available compression algorithm types (for h_algorithmtype) */
Gao Xiangec8c2442019-06-24 15:22:51 +0800209enum {
Gao Xiang60a49ba2019-09-04 10:08:49 +0800210 Z_EROFS_COMPRESSION_LZ4 = 0,
Gao Xiangec8c2442019-06-24 15:22:51 +0800211 Z_EROFS_COMPRESSION_MAX
212};
Gao Xiang14373712021-03-29 18:00:12 +0800213#define Z_EROFS_ALL_COMPR_ALGS (1 << (Z_EROFS_COMPRESSION_MAX - 1))
Gao Xiangec8c2442019-06-24 15:22:51 +0800214
Gao Xiang46249cd2021-03-29 09:23:07 +0800215/* 14 bytes (+ length field = 16 bytes) */
216struct z_erofs_lz4_cfgs {
217 __le16 max_distance;
Gao Xiang5404c33012021-04-07 12:39:22 +0800218 __le16 max_pclusterblks;
219 u8 reserved[10];
Gao Xiang46249cd2021-03-29 09:23:07 +0800220} __packed;
221
Gao Xiangec8c2442019-06-24 15:22:51 +0800222/*
223 * bit 0 : COMPACTED_2B indexes (0 - off; 1 - on)
224 * e.g. for 4k logical cluster size, 4B if compacted 2B is off;
225 * (4B) + 2B + (4B) if compacted 2B is on.
Gao Xiang5404c33012021-04-07 12:39:22 +0800226 * bit 1 : HEAD1 big pcluster (0 - off; 1 - on)
227 * bit 2 : HEAD2 big pcluster (0 - off; 1 - on)
Gao Xiangec8c2442019-06-24 15:22:51 +0800228 */
Gao Xiang5404c33012021-04-07 12:39:22 +0800229#define Z_EROFS_ADVISE_COMPACTED_2B 0x0001
230#define Z_EROFS_ADVISE_BIG_PCLUSTER_1 0x0002
231#define Z_EROFS_ADVISE_BIG_PCLUSTER_2 0x0004
Gao Xiangec8c2442019-06-24 15:22:51 +0800232
233struct z_erofs_map_header {
234 __le32 h_reserved1;
235 __le16 h_advise;
236 /*
237 * bit 0-3 : algorithm type of head 1 (logical cluster type 01);
238 * bit 4-7 : algorithm type of head 2 (logical cluster type 11).
239 */
240 __u8 h_algorithmtype;
241 /*
242 * bit 0-2 : logical cluster bits - 12, e.g. 0 for 4096;
Gao Xiang54e0b6c2021-04-07 12:39:18 +0800243 * bit 3-7 : reserved.
Gao Xiangec8c2442019-06-24 15:22:51 +0800244 */
245 __u8 h_clusterbits;
246};
247
248#define Z_EROFS_VLE_LEGACY_HEADER_PADDING 8
Gao Xiangaea12862018-07-26 20:21:44 +0800249
250/*
Gao Xiangea559e72019-09-04 10:08:57 +0800251 * Fixed-sized output compression ondisk Logical Extent cluster type:
Gao Xiangaea12862018-07-26 20:21:44 +0800252 * 0 - literal (uncompressed) cluster
253 * 1 - compressed cluster (for the head logical cluster)
254 * 2 - compressed cluster (for the other logical clusters)
255 *
256 * In detail,
257 * 0 - literal (uncompressed) cluster,
258 * di_advise = 0
259 * di_clusterofs = the literal data offset of the cluster
260 * di_blkaddr = the blkaddr of the literal cluster
261 *
262 * 1 - compressed cluster (for the head logical cluster)
263 * di_advise = 1
264 * di_clusterofs = the decompressed data offset of the cluster
265 * di_blkaddr = the blkaddr of the compressed cluster
266 *
267 * 2 - compressed cluster (for the other logical clusters)
268 * di_advise = 2
269 * di_clusterofs =
270 * the decompressed data offset in its own head cluster
271 * di_u.delta[0] = distance to its corresponding head cluster
272 * di_u.delta[1] = distance to its corresponding tail cluster
273 * (di_advise could be 0, 1 or 2)
274 */
Gao Xiang99691b42018-08-21 22:49:33 +0800275enum {
Gao Xiang60a49ba2019-09-04 10:08:49 +0800276 Z_EROFS_VLE_CLUSTER_TYPE_PLAIN = 0,
277 Z_EROFS_VLE_CLUSTER_TYPE_HEAD = 1,
278 Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD = 2,
279 Z_EROFS_VLE_CLUSTER_TYPE_RESERVED = 3,
Gao Xiang99691b42018-08-21 22:49:33 +0800280 Z_EROFS_VLE_CLUSTER_TYPE_MAX
281};
282
Gao Xiangaea12862018-07-26 20:21:44 +0800283#define Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS 2
284#define Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT 0
285
Gao Xiang5404c33012021-04-07 12:39:22 +0800286/*
287 * D0_CBLKCNT will be marked _only_ at the 1st non-head lcluster to store the
288 * compressed block count of a compressed extent (in logical clusters, aka.
289 * block count of a pcluster).
290 */
291#define Z_EROFS_VLE_DI_D0_CBLKCNT (1 << 11)
292
Gao Xiangaea12862018-07-26 20:21:44 +0800293struct z_erofs_vle_decompressed_index {
294 __le16 di_advise;
295 /* where to decompress in the head cluster */
296 __le16 di_clusterofs;
297
298 union {
299 /* for the head cluster */
300 __le32 blkaddr;
301 /*
302 * for the rest clusters
303 * eg. for 4k page-sized cluster, maximum 4K*64k = 256M)
304 * [0] - pointing to the head cluster
305 * [1] - pointing to the tail cluster
306 */
307 __le16 delta[2];
Gao Xianged34aa42019-09-04 10:08:51 +0800308 } di_u;
309};
Gao Xiangaea12862018-07-26 20:21:44 +0800310
Gao Xiangec8c2442019-06-24 15:22:51 +0800311#define Z_EROFS_VLE_LEGACY_INDEX_ALIGN(size) \
312 (round_up(size, sizeof(struct z_erofs_vle_decompressed_index)) + \
313 sizeof(struct z_erofs_map_header) + Z_EROFS_VLE_LEGACY_HEADER_PADDING)
Gao Xiangaea12862018-07-26 20:21:44 +0800314
315/* dirent sorts in alphabet order, thus we can do binary search */
316struct erofs_dirent {
Gao Xiang4b66eb52019-09-04 10:08:48 +0800317 __le64 nid; /* node number */
318 __le16 nameoff; /* start offset of file name */
319 __u8 file_type; /* file type */
320 __u8 reserved; /* reserved */
Gao Xiangaea12862018-07-26 20:21:44 +0800321} __packed;
322
Gao Xiang1d819c52019-08-16 15:11:42 +0800323/*
324 * EROFS file types should match generic FT_* types and
325 * it seems no need to add BUILD_BUG_ONs since potential
326 * unmatchness will break other fses as well...
327 */
Gao Xiangaea12862018-07-26 20:21:44 +0800328
329#define EROFS_NAME_LEN 255
330
331/* check the EROFS on-disk layout strictly at compile time */
332static inline void erofs_check_ondisk_layout_definitions(void)
333{
334 BUILD_BUG_ON(sizeof(struct erofs_super_block) != 128);
Gao Xiang8a765682019-09-04 10:08:54 +0800335 BUILD_BUG_ON(sizeof(struct erofs_inode_compact) != 32);
336 BUILD_BUG_ON(sizeof(struct erofs_inode_extended) != 64);
Gao Xiangaea12862018-07-26 20:21:44 +0800337 BUILD_BUG_ON(sizeof(struct erofs_xattr_ibody_header) != 12);
338 BUILD_BUG_ON(sizeof(struct erofs_xattr_entry) != 4);
Gao Xiangec8c2442019-06-24 15:22:51 +0800339 BUILD_BUG_ON(sizeof(struct z_erofs_map_header) != 8);
Gao Xiangaea12862018-07-26 20:21:44 +0800340 BUILD_BUG_ON(sizeof(struct z_erofs_vle_decompressed_index) != 8);
341 BUILD_BUG_ON(sizeof(struct erofs_dirent) != 12);
Gao Xiang99691b42018-08-21 22:49:33 +0800342
343 BUILD_BUG_ON(BIT(Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) <
344 Z_EROFS_VLE_CLUSTER_TYPE_MAX - 1);
Gao Xiangaea12862018-07-26 20:21:44 +0800345}
346
347#endif
348