blob: 76777673eb63cf2ed923f54af1f7d98d6ed315d0 [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 Xiang426a9302019-09-04 10:08:53 +080022#define EROFS_ALL_FEATURE_INCOMPAT EROFS_FEATURE_INCOMPAT_LZ4_0PADDING
Gao Xiang5efe5132019-06-13 16:35:41 +080023
Gao Xiang14373712021-03-29 18:00:12 +080024#define EROFS_SB_EXTSLOT_SIZE 16
25
26/* erofs on-disk super block (currently 128 bytes) */
Gao Xiangaea12862018-07-26 20:21:44 +080027struct erofs_super_block {
Gao Xiang4b66eb52019-09-04 10:08:48 +080028 __le32 magic; /* file system magic number */
29 __le32 checksum; /* crc32c(super_block) */
Gao Xiang426a9302019-09-04 10:08:53 +080030 __le32 feature_compat;
Gao Xiang4b66eb52019-09-04 10:08:48 +080031 __u8 blkszbits; /* support block_size == PAGE_SIZE only */
Gao Xiang14373712021-03-29 18:00:12 +080032 __u8 sb_extslots; /* superblock size = 128 + sb_extslots * 16 */
Gao Xiangaea12862018-07-26 20:21:44 +080033
Gao Xiang4b66eb52019-09-04 10:08:48 +080034 __le16 root_nid; /* nid of root directory */
35 __le64 inos; /* total valid ino # (== f_files - f_favail) */
Gao Xiangaea12862018-07-26 20:21:44 +080036
Gao Xiang4b66eb52019-09-04 10:08:48 +080037 __le64 build_time; /* inode v1 time derivation */
38 __le32 build_time_nsec; /* inode v1 time derivation in nano scale */
39 __le32 blocks; /* used for statfs */
40 __le32 meta_blkaddr; /* start block address of metadata area */
41 __le32 xattr_blkaddr; /* start block address of shared xattr area */
42 __u8 uuid[16]; /* 128-bit uuid for volume */
43 __u8 volume_name[16]; /* volume name */
Gao Xiang426a9302019-09-04 10:08:53 +080044 __le32 feature_incompat;
Gao Xiang14373712021-03-29 18:00:12 +080045 union {
46 /* bitmap for available compression algorithms */
47 __le16 available_compr_algs;
48 /* customized sliding window size instead of 64k by default */
49 __le16 lz4_max_distance;
50 } __packed u1;
Huang Jianan5d505382021-03-29 09:23:06 +080051 __u8 reserved2[42];
Gao Xianged34aa42019-09-04 10:08:51 +080052};
Gao Xiangaea12862018-07-26 20:21:44 +080053
Gao Xiangaea12862018-07-26 20:21:44 +080054/*
Gao Xiangea559e72019-09-04 10:08:57 +080055 * erofs inode datalayout (i_format in on-disk inode):
Gao Xiangaea12862018-07-26 20:21:44 +080056 * 0 - inode plain without inline data A:
57 * inode, [xattrs], ... | ... | no-holed data
Gao Xiangec8c2442019-06-24 15:22:51 +080058 * 1 - inode VLE compression B (legacy):
Gao Xiangaea12862018-07-26 20:21:44 +080059 * inode, [xattrs], extents ... | ...
60 * 2 - inode plain with inline data C:
61 * inode, [xattrs], last_inline_data, ... | ... | no-holed data
Gao Xiangec8c2442019-06-24 15:22:51 +080062 * 3 - inode compression D:
63 * inode, [xattrs], map_header, extents ... | ...
64 * 4~7 - reserved
Gao Xiangaea12862018-07-26 20:21:44 +080065 */
66enum {
Gao Xiang60a49ba2019-09-04 10:08:49 +080067 EROFS_INODE_FLAT_PLAIN = 0,
68 EROFS_INODE_FLAT_COMPRESSION_LEGACY = 1,
69 EROFS_INODE_FLAT_INLINE = 2,
70 EROFS_INODE_FLAT_COMPRESSION = 3,
Gao Xiang8a765682019-09-04 10:08:54 +080071 EROFS_INODE_DATALAYOUT_MAX
Gao Xiangaea12862018-07-26 20:21:44 +080072};
Gao Xiangccd9c192018-12-12 01:57:30 +080073
Gao Xiang3d2969f2019-08-13 10:30:52 +080074static inline bool erofs_inode_is_data_compressed(unsigned int datamode)
Gao Xiangec8c2442019-06-24 15:22:51 +080075{
Gao Xiangc39747f2019-09-04 10:08:52 +080076 return datamode == EROFS_INODE_FLAT_COMPRESSION ||
77 datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY;
Gao Xiangec8c2442019-06-24 15:22:51 +080078}
79
Gao Xiangccd9c192018-12-12 01:57:30 +080080/* bit definitions of inode i_advise */
Gao Xiangaea12862018-07-26 20:21:44 +080081#define EROFS_I_VERSION_BITS 1
Gao Xiang8a765682019-09-04 10:08:54 +080082#define EROFS_I_DATALAYOUT_BITS 3
Gao Xiangaea12862018-07-26 20:21:44 +080083
84#define EROFS_I_VERSION_BIT 0
Gao Xiang8a765682019-09-04 10:08:54 +080085#define EROFS_I_DATALAYOUT_BIT 1
Gao Xiangaea12862018-07-26 20:21:44 +080086
Gao Xiang24a806d2021-03-29 08:36:14 +080087#define EROFS_I_ALL \
88 ((1 << (EROFS_I_DATALAYOUT_BIT + EROFS_I_DATALAYOUT_BITS)) - 1)
89
Gao Xiang4b66eb52019-09-04 10:08:48 +080090/* 32-byte reduced form of an ondisk inode */
Gao Xiang8a765682019-09-04 10:08:54 +080091struct erofs_inode_compact {
92 __le16 i_format; /* inode format hints */
Gao Xiangaea12862018-07-26 20:21:44 +080093
94/* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
Gao Xiang4b66eb52019-09-04 10:08:48 +080095 __le16 i_xattr_icount;
96 __le16 i_mode;
97 __le16 i_nlink;
98 __le32 i_size;
99 __le32 i_reserved;
100 union {
Gao Xiangaea12862018-07-26 20:21:44 +0800101 /* file total compressed blocks for data mapping 1 */
102 __le32 compressed_blocks;
103 __le32 raw_blkaddr;
104
105 /* for device files, used to indicate old/new device # */
106 __le32 rdev;
Gao Xianged34aa42019-09-04 10:08:51 +0800107 } i_u;
Gao Xiang4b66eb52019-09-04 10:08:48 +0800108 __le32 i_ino; /* only used for 32-bit stat compatibility */
109 __le16 i_uid;
110 __le16 i_gid;
111 __le32 i_reserved2;
Gao Xianged34aa42019-09-04 10:08:51 +0800112};
Gao Xiangaea12862018-07-26 20:21:44 +0800113
114/* 32 bytes on-disk inode */
Gao Xiang8a765682019-09-04 10:08:54 +0800115#define EROFS_INODE_LAYOUT_COMPACT 0
Gao Xiangaea12862018-07-26 20:21:44 +0800116/* 64 bytes on-disk inode */
Gao Xiang8a765682019-09-04 10:08:54 +0800117#define EROFS_INODE_LAYOUT_EXTENDED 1
Gao Xiangaea12862018-07-26 20:21:44 +0800118
Gao Xiang4b66eb52019-09-04 10:08:48 +0800119/* 64-byte complete form of an ondisk inode */
Gao Xiang8a765682019-09-04 10:08:54 +0800120struct erofs_inode_extended {
121 __le16 i_format; /* inode format hints */
Gao Xiangaea12862018-07-26 20:21:44 +0800122
Gao Xiangcead56f2019-07-31 23:57:34 +0800123/* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
Gao Xiang4b66eb52019-09-04 10:08:48 +0800124 __le16 i_xattr_icount;
125 __le16 i_mode;
126 __le16 i_reserved;
127 __le64 i_size;
128 union {
Gao Xiangaea12862018-07-26 20:21:44 +0800129 /* file total compressed blocks for data mapping 1 */
130 __le32 compressed_blocks;
131 __le32 raw_blkaddr;
132
133 /* for device files, used to indicate old/new device # */
134 __le32 rdev;
Gao Xianged34aa42019-09-04 10:08:51 +0800135 } i_u;
Gao Xiangaea12862018-07-26 20:21:44 +0800136
137 /* only used for 32-bit stat compatibility */
Gao Xiang4b66eb52019-09-04 10:08:48 +0800138 __le32 i_ino;
Gao Xiangaea12862018-07-26 20:21:44 +0800139
Gao Xiang4b66eb52019-09-04 10:08:48 +0800140 __le32 i_uid;
141 __le32 i_gid;
142 __le64 i_ctime;
143 __le32 i_ctime_nsec;
144 __le32 i_nlink;
145 __u8 i_reserved2[16];
Gao Xianged34aa42019-09-04 10:08:51 +0800146};
Gao Xiangaea12862018-07-26 20:21:44 +0800147
148#define EROFS_MAX_SHARED_XATTRS (128)
149/* h_shared_count between 129 ... 255 are special # */
150#define EROFS_SHARED_XATTR_EXTENT (255)
151
152/*
153 * inline xattrs (n == i_xattr_icount):
154 * erofs_xattr_ibody_header(1) + (n - 1) * 4 bytes
155 * 12 bytes / \
156 * / \
157 * /-----------------------\
158 * | erofs_xattr_entries+ |
159 * +-----------------------+
160 * inline xattrs must starts in erofs_xattr_ibody_header,
161 * for read-only fs, no need to introduce h_refcount
162 */
163struct erofs_xattr_ibody_header {
Gao Xiangcead56f2019-07-31 23:57:34 +0800164 __le32 h_reserved;
Gao Xiangaea12862018-07-26 20:21:44 +0800165 __u8 h_shared_count;
Gao Xiangcead56f2019-07-31 23:57:34 +0800166 __u8 h_reserved2[7];
Gao Xiangaea12862018-07-26 20:21:44 +0800167 __le32 h_shared_xattrs[0]; /* shared xattr id array */
Gao Xianged34aa42019-09-04 10:08:51 +0800168};
Gao Xiangaea12862018-07-26 20:21:44 +0800169
170/* Name indexes */
171#define EROFS_XATTR_INDEX_USER 1
172#define EROFS_XATTR_INDEX_POSIX_ACL_ACCESS 2
173#define EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT 3
174#define EROFS_XATTR_INDEX_TRUSTED 4
175#define EROFS_XATTR_INDEX_LUSTRE 5
176#define EROFS_XATTR_INDEX_SECURITY 6
177
178/* xattr entry (for both inline & shared xattrs) */
179struct erofs_xattr_entry {
180 __u8 e_name_len; /* length of name */
181 __u8 e_name_index; /* attribute name index */
182 __le16 e_value_size; /* size of attribute value */
183 /* followed by e_name and e_value */
184 char e_name[0]; /* attribute name */
Gao Xianged34aa42019-09-04 10:08:51 +0800185};
Gao Xiangaea12862018-07-26 20:21:44 +0800186
Gao Xiangb6796ab2019-09-04 10:08:50 +0800187static inline unsigned int erofs_xattr_ibody_size(__le16 i_xattr_icount)
188{
189 if (!i_xattr_icount)
190 return 0;
191
192 return sizeof(struct erofs_xattr_ibody_header) +
193 sizeof(__u32) * (le16_to_cpu(i_xattr_icount) - 1);
194}
Gao Xiangaea12862018-07-26 20:21:44 +0800195
196#define EROFS_XATTR_ALIGN(size) round_up(size, sizeof(struct erofs_xattr_entry))
Gao Xiangb6796ab2019-09-04 10:08:50 +0800197
198static inline unsigned int erofs_xattr_entry_size(struct erofs_xattr_entry *e)
199{
200 return EROFS_XATTR_ALIGN(sizeof(struct erofs_xattr_entry) +
201 e->e_name_len + le16_to_cpu(e->e_value_size));
202}
Gao Xiangaea12862018-07-26 20:21:44 +0800203
Gao Xiang9f6cc762021-04-07 12:39:20 +0800204/* maximum supported size of a physical compression cluster */
205#define Z_EROFS_PCLUSTER_MAX_SIZE (1024 * 1024)
206
Gao Xiangea559e72019-09-04 10:08:57 +0800207/* available compression algorithm types (for h_algorithmtype) */
Gao Xiangec8c2442019-06-24 15:22:51 +0800208enum {
Gao Xiang60a49ba2019-09-04 10:08:49 +0800209 Z_EROFS_COMPRESSION_LZ4 = 0,
Gao Xiangec8c2442019-06-24 15:22:51 +0800210 Z_EROFS_COMPRESSION_MAX
211};
Gao Xiang14373712021-03-29 18:00:12 +0800212#define Z_EROFS_ALL_COMPR_ALGS (1 << (Z_EROFS_COMPRESSION_MAX - 1))
Gao Xiangec8c2442019-06-24 15:22:51 +0800213
Gao Xiang46249cd2021-03-29 09:23:07 +0800214/* 14 bytes (+ length field = 16 bytes) */
215struct z_erofs_lz4_cfgs {
216 __le16 max_distance;
217 u8 reserved[12];
218} __packed;
219
Gao Xiangec8c2442019-06-24 15:22:51 +0800220/*
221 * bit 0 : COMPACTED_2B indexes (0 - off; 1 - on)
222 * e.g. for 4k logical cluster size, 4B if compacted 2B is off;
223 * (4B) + 2B + (4B) if compacted 2B is on.
224 */
225#define Z_EROFS_ADVISE_COMPACTED_2B_BIT 0
226
227#define Z_EROFS_ADVISE_COMPACTED_2B (1 << Z_EROFS_ADVISE_COMPACTED_2B_BIT)
228
229struct z_erofs_map_header {
230 __le32 h_reserved1;
231 __le16 h_advise;
232 /*
233 * bit 0-3 : algorithm type of head 1 (logical cluster type 01);
234 * bit 4-7 : algorithm type of head 2 (logical cluster type 11).
235 */
236 __u8 h_algorithmtype;
237 /*
238 * bit 0-2 : logical cluster bits - 12, e.g. 0 for 4096;
Gao Xiang54e0b6c2021-04-07 12:39:18 +0800239 * bit 3-7 : reserved.
Gao Xiangec8c2442019-06-24 15:22:51 +0800240 */
241 __u8 h_clusterbits;
242};
243
244#define Z_EROFS_VLE_LEGACY_HEADER_PADDING 8
Gao Xiangaea12862018-07-26 20:21:44 +0800245
246/*
Gao Xiangea559e72019-09-04 10:08:57 +0800247 * Fixed-sized output compression ondisk Logical Extent cluster type:
Gao Xiangaea12862018-07-26 20:21:44 +0800248 * 0 - literal (uncompressed) cluster
249 * 1 - compressed cluster (for the head logical cluster)
250 * 2 - compressed cluster (for the other logical clusters)
251 *
252 * In detail,
253 * 0 - literal (uncompressed) cluster,
254 * di_advise = 0
255 * di_clusterofs = the literal data offset of the cluster
256 * di_blkaddr = the blkaddr of the literal cluster
257 *
258 * 1 - compressed cluster (for the head logical cluster)
259 * di_advise = 1
260 * di_clusterofs = the decompressed data offset of the cluster
261 * di_blkaddr = the blkaddr of the compressed cluster
262 *
263 * 2 - compressed cluster (for the other logical clusters)
264 * di_advise = 2
265 * di_clusterofs =
266 * the decompressed data offset in its own head cluster
267 * di_u.delta[0] = distance to its corresponding head cluster
268 * di_u.delta[1] = distance to its corresponding tail cluster
269 * (di_advise could be 0, 1 or 2)
270 */
Gao Xiang99691b42018-08-21 22:49:33 +0800271enum {
Gao Xiang60a49ba2019-09-04 10:08:49 +0800272 Z_EROFS_VLE_CLUSTER_TYPE_PLAIN = 0,
273 Z_EROFS_VLE_CLUSTER_TYPE_HEAD = 1,
274 Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD = 2,
275 Z_EROFS_VLE_CLUSTER_TYPE_RESERVED = 3,
Gao Xiang99691b42018-08-21 22:49:33 +0800276 Z_EROFS_VLE_CLUSTER_TYPE_MAX
277};
278
Gao Xiangaea12862018-07-26 20:21:44 +0800279#define Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS 2
280#define Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT 0
281
282struct z_erofs_vle_decompressed_index {
283 __le16 di_advise;
284 /* where to decompress in the head cluster */
285 __le16 di_clusterofs;
286
287 union {
288 /* for the head cluster */
289 __le32 blkaddr;
290 /*
291 * for the rest clusters
292 * eg. for 4k page-sized cluster, maximum 4K*64k = 256M)
293 * [0] - pointing to the head cluster
294 * [1] - pointing to the tail cluster
295 */
296 __le16 delta[2];
Gao Xianged34aa42019-09-04 10:08:51 +0800297 } di_u;
298};
Gao Xiangaea12862018-07-26 20:21:44 +0800299
Gao Xiangec8c2442019-06-24 15:22:51 +0800300#define Z_EROFS_VLE_LEGACY_INDEX_ALIGN(size) \
301 (round_up(size, sizeof(struct z_erofs_vle_decompressed_index)) + \
302 sizeof(struct z_erofs_map_header) + Z_EROFS_VLE_LEGACY_HEADER_PADDING)
Gao Xiangaea12862018-07-26 20:21:44 +0800303
304/* dirent sorts in alphabet order, thus we can do binary search */
305struct erofs_dirent {
Gao Xiang4b66eb52019-09-04 10:08:48 +0800306 __le64 nid; /* node number */
307 __le16 nameoff; /* start offset of file name */
308 __u8 file_type; /* file type */
309 __u8 reserved; /* reserved */
Gao Xiangaea12862018-07-26 20:21:44 +0800310} __packed;
311
Gao Xiang1d819c52019-08-16 15:11:42 +0800312/*
313 * EROFS file types should match generic FT_* types and
314 * it seems no need to add BUILD_BUG_ONs since potential
315 * unmatchness will break other fses as well...
316 */
Gao Xiangaea12862018-07-26 20:21:44 +0800317
318#define EROFS_NAME_LEN 255
319
320/* check the EROFS on-disk layout strictly at compile time */
321static inline void erofs_check_ondisk_layout_definitions(void)
322{
323 BUILD_BUG_ON(sizeof(struct erofs_super_block) != 128);
Gao Xiang8a765682019-09-04 10:08:54 +0800324 BUILD_BUG_ON(sizeof(struct erofs_inode_compact) != 32);
325 BUILD_BUG_ON(sizeof(struct erofs_inode_extended) != 64);
Gao Xiangaea12862018-07-26 20:21:44 +0800326 BUILD_BUG_ON(sizeof(struct erofs_xattr_ibody_header) != 12);
327 BUILD_BUG_ON(sizeof(struct erofs_xattr_entry) != 4);
Gao Xiangec8c2442019-06-24 15:22:51 +0800328 BUILD_BUG_ON(sizeof(struct z_erofs_map_header) != 8);
Gao Xiangaea12862018-07-26 20:21:44 +0800329 BUILD_BUG_ON(sizeof(struct z_erofs_vle_decompressed_index) != 8);
330 BUILD_BUG_ON(sizeof(struct erofs_dirent) != 12);
Gao Xiang99691b42018-08-21 22:49:33 +0800331
332 BUILD_BUG_ON(BIT(Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) <
333 Z_EROFS_VLE_CLUSTER_TYPE_MAX - 1);
Gao Xiangaea12862018-07-26 20:21:44 +0800334}
335
336#endif
337