blob: 385fa49c7749fd7897fe91f13ab9c7823dabb714 [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.
6 * http://www.huawei.com/
7 * 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
21#define EROFS_ALL_FEATURE_INCOMPAT EROFS_FEATURE_INCOMPAT_LZ4_0PADDING
Gao Xiang5efe5132019-06-13 16:35:41 +080022
Gao Xiang4b66eb52019-09-04 10:08:48 +080023/* 128-byte erofs on-disk super block */
Gao Xiangaea12862018-07-26 20:21:44 +080024struct erofs_super_block {
Gao Xiang4b66eb52019-09-04 10:08:48 +080025 __le32 magic; /* file system magic number */
26 __le32 checksum; /* crc32c(super_block) */
Gao Xiang426a9302019-09-04 10:08:53 +080027 __le32 feature_compat;
Gao Xiang4b66eb52019-09-04 10:08:48 +080028 __u8 blkszbits; /* support block_size == PAGE_SIZE only */
29 __u8 reserved;
Gao Xiangaea12862018-07-26 20:21:44 +080030
Gao Xiang4b66eb52019-09-04 10:08:48 +080031 __le16 root_nid; /* nid of root directory */
32 __le64 inos; /* total valid ino # (== f_files - f_favail) */
Gao Xiangaea12862018-07-26 20:21:44 +080033
Gao Xiang4b66eb52019-09-04 10:08:48 +080034 __le64 build_time; /* inode v1 time derivation */
35 __le32 build_time_nsec; /* inode v1 time derivation in nano scale */
36 __le32 blocks; /* used for statfs */
37 __le32 meta_blkaddr; /* start block address of metadata area */
38 __le32 xattr_blkaddr; /* start block address of shared xattr area */
39 __u8 uuid[16]; /* 128-bit uuid for volume */
40 __u8 volume_name[16]; /* volume name */
Gao Xiang426a9302019-09-04 10:08:53 +080041 __le32 feature_incompat;
Gao Xiang4b66eb52019-09-04 10:08:48 +080042 __u8 reserved2[44];
Gao Xianged34aa42019-09-04 10:08:51 +080043};
Gao Xiangaea12862018-07-26 20:21:44 +080044
Gao Xiangaea12862018-07-26 20:21:44 +080045/*
Gao Xiangea559e72019-09-04 10:08:57 +080046 * erofs inode datalayout (i_format in on-disk inode):
Gao Xiangaea12862018-07-26 20:21:44 +080047 * 0 - inode plain without inline data A:
48 * inode, [xattrs], ... | ... | no-holed data
Gao Xiangec8c2442019-06-24 15:22:51 +080049 * 1 - inode VLE compression B (legacy):
Gao Xiangaea12862018-07-26 20:21:44 +080050 * inode, [xattrs], extents ... | ...
51 * 2 - inode plain with inline data C:
52 * inode, [xattrs], last_inline_data, ... | ... | no-holed data
Gao Xiangec8c2442019-06-24 15:22:51 +080053 * 3 - inode compression D:
54 * inode, [xattrs], map_header, extents ... | ...
55 * 4~7 - reserved
Gao Xiangaea12862018-07-26 20:21:44 +080056 */
57enum {
Gao Xiang60a49ba2019-09-04 10:08:49 +080058 EROFS_INODE_FLAT_PLAIN = 0,
59 EROFS_INODE_FLAT_COMPRESSION_LEGACY = 1,
60 EROFS_INODE_FLAT_INLINE = 2,
61 EROFS_INODE_FLAT_COMPRESSION = 3,
Gao Xiang8a765682019-09-04 10:08:54 +080062 EROFS_INODE_DATALAYOUT_MAX
Gao Xiangaea12862018-07-26 20:21:44 +080063};
Gao Xiangccd9c192018-12-12 01:57:30 +080064
Gao Xiang3d2969f2019-08-13 10:30:52 +080065static inline bool erofs_inode_is_data_compressed(unsigned int datamode)
Gao Xiangec8c2442019-06-24 15:22:51 +080066{
Gao Xiangc39747f2019-09-04 10:08:52 +080067 return datamode == EROFS_INODE_FLAT_COMPRESSION ||
68 datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY;
Gao Xiangec8c2442019-06-24 15:22:51 +080069}
70
Gao Xiangccd9c192018-12-12 01:57:30 +080071/* bit definitions of inode i_advise */
Gao Xiangaea12862018-07-26 20:21:44 +080072#define EROFS_I_VERSION_BITS 1
Gao Xiang8a765682019-09-04 10:08:54 +080073#define EROFS_I_DATALAYOUT_BITS 3
Gao Xiangaea12862018-07-26 20:21:44 +080074
75#define EROFS_I_VERSION_BIT 0
Gao Xiang8a765682019-09-04 10:08:54 +080076#define EROFS_I_DATALAYOUT_BIT 1
Gao Xiangaea12862018-07-26 20:21:44 +080077
Gao Xiang4b66eb52019-09-04 10:08:48 +080078/* 32-byte reduced form of an ondisk inode */
Gao Xiang8a765682019-09-04 10:08:54 +080079struct erofs_inode_compact {
80 __le16 i_format; /* inode format hints */
Gao Xiangaea12862018-07-26 20:21:44 +080081
82/* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
Gao Xiang4b66eb52019-09-04 10:08:48 +080083 __le16 i_xattr_icount;
84 __le16 i_mode;
85 __le16 i_nlink;
86 __le32 i_size;
87 __le32 i_reserved;
88 union {
Gao Xiangaea12862018-07-26 20:21:44 +080089 /* file total compressed blocks for data mapping 1 */
90 __le32 compressed_blocks;
91 __le32 raw_blkaddr;
92
93 /* for device files, used to indicate old/new device # */
94 __le32 rdev;
Gao Xianged34aa42019-09-04 10:08:51 +080095 } i_u;
Gao Xiang4b66eb52019-09-04 10:08:48 +080096 __le32 i_ino; /* only used for 32-bit stat compatibility */
97 __le16 i_uid;
98 __le16 i_gid;
99 __le32 i_reserved2;
Gao Xianged34aa42019-09-04 10:08:51 +0800100};
Gao Xiangaea12862018-07-26 20:21:44 +0800101
102/* 32 bytes on-disk inode */
Gao Xiang8a765682019-09-04 10:08:54 +0800103#define EROFS_INODE_LAYOUT_COMPACT 0
Gao Xiangaea12862018-07-26 20:21:44 +0800104/* 64 bytes on-disk inode */
Gao Xiang8a765682019-09-04 10:08:54 +0800105#define EROFS_INODE_LAYOUT_EXTENDED 1
Gao Xiangaea12862018-07-26 20:21:44 +0800106
Gao Xiang4b66eb52019-09-04 10:08:48 +0800107/* 64-byte complete form of an ondisk inode */
Gao Xiang8a765682019-09-04 10:08:54 +0800108struct erofs_inode_extended {
109 __le16 i_format; /* inode format hints */
Gao Xiangaea12862018-07-26 20:21:44 +0800110
Gao Xiangcead56f2019-07-31 23:57:34 +0800111/* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
Gao Xiang4b66eb52019-09-04 10:08:48 +0800112 __le16 i_xattr_icount;
113 __le16 i_mode;
114 __le16 i_reserved;
115 __le64 i_size;
116 union {
Gao Xiangaea12862018-07-26 20:21:44 +0800117 /* file total compressed blocks for data mapping 1 */
118 __le32 compressed_blocks;
119 __le32 raw_blkaddr;
120
121 /* for device files, used to indicate old/new device # */
122 __le32 rdev;
Gao Xianged34aa42019-09-04 10:08:51 +0800123 } i_u;
Gao Xiangaea12862018-07-26 20:21:44 +0800124
125 /* only used for 32-bit stat compatibility */
Gao Xiang4b66eb52019-09-04 10:08:48 +0800126 __le32 i_ino;
Gao Xiangaea12862018-07-26 20:21:44 +0800127
Gao Xiang4b66eb52019-09-04 10:08:48 +0800128 __le32 i_uid;
129 __le32 i_gid;
130 __le64 i_ctime;
131 __le32 i_ctime_nsec;
132 __le32 i_nlink;
133 __u8 i_reserved2[16];
Gao Xianged34aa42019-09-04 10:08:51 +0800134};
Gao Xiangaea12862018-07-26 20:21:44 +0800135
136#define EROFS_MAX_SHARED_XATTRS (128)
137/* h_shared_count between 129 ... 255 are special # */
138#define EROFS_SHARED_XATTR_EXTENT (255)
139
140/*
141 * inline xattrs (n == i_xattr_icount):
142 * erofs_xattr_ibody_header(1) + (n - 1) * 4 bytes
143 * 12 bytes / \
144 * / \
145 * /-----------------------\
146 * | erofs_xattr_entries+ |
147 * +-----------------------+
148 * inline xattrs must starts in erofs_xattr_ibody_header,
149 * for read-only fs, no need to introduce h_refcount
150 */
151struct erofs_xattr_ibody_header {
Gao Xiangcead56f2019-07-31 23:57:34 +0800152 __le32 h_reserved;
Gao Xiangaea12862018-07-26 20:21:44 +0800153 __u8 h_shared_count;
Gao Xiangcead56f2019-07-31 23:57:34 +0800154 __u8 h_reserved2[7];
Gao Xiangaea12862018-07-26 20:21:44 +0800155 __le32 h_shared_xattrs[0]; /* shared xattr id array */
Gao Xianged34aa42019-09-04 10:08:51 +0800156};
Gao Xiangaea12862018-07-26 20:21:44 +0800157
158/* Name indexes */
159#define EROFS_XATTR_INDEX_USER 1
160#define EROFS_XATTR_INDEX_POSIX_ACL_ACCESS 2
161#define EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT 3
162#define EROFS_XATTR_INDEX_TRUSTED 4
163#define EROFS_XATTR_INDEX_LUSTRE 5
164#define EROFS_XATTR_INDEX_SECURITY 6
165
166/* xattr entry (for both inline & shared xattrs) */
167struct erofs_xattr_entry {
168 __u8 e_name_len; /* length of name */
169 __u8 e_name_index; /* attribute name index */
170 __le16 e_value_size; /* size of attribute value */
171 /* followed by e_name and e_value */
172 char e_name[0]; /* attribute name */
Gao Xianged34aa42019-09-04 10:08:51 +0800173};
Gao Xiangaea12862018-07-26 20:21:44 +0800174
Gao Xiangb6796ab2019-09-04 10:08:50 +0800175static inline unsigned int erofs_xattr_ibody_size(__le16 i_xattr_icount)
176{
177 if (!i_xattr_icount)
178 return 0;
179
180 return sizeof(struct erofs_xattr_ibody_header) +
181 sizeof(__u32) * (le16_to_cpu(i_xattr_icount) - 1);
182}
Gao Xiangaea12862018-07-26 20:21:44 +0800183
184#define EROFS_XATTR_ALIGN(size) round_up(size, sizeof(struct erofs_xattr_entry))
Gao Xiangb6796ab2019-09-04 10:08:50 +0800185
186static inline unsigned int erofs_xattr_entry_size(struct erofs_xattr_entry *e)
187{
188 return EROFS_XATTR_ALIGN(sizeof(struct erofs_xattr_entry) +
189 e->e_name_len + le16_to_cpu(e->e_value_size));
190}
Gao Xiangaea12862018-07-26 20:21:44 +0800191
Gao Xiangea559e72019-09-04 10:08:57 +0800192/* available compression algorithm types (for h_algorithmtype) */
Gao Xiangec8c2442019-06-24 15:22:51 +0800193enum {
Gao Xiang60a49ba2019-09-04 10:08:49 +0800194 Z_EROFS_COMPRESSION_LZ4 = 0,
Gao Xiangec8c2442019-06-24 15:22:51 +0800195 Z_EROFS_COMPRESSION_MAX
196};
197
198/*
199 * bit 0 : COMPACTED_2B indexes (0 - off; 1 - on)
200 * e.g. for 4k logical cluster size, 4B if compacted 2B is off;
201 * (4B) + 2B + (4B) if compacted 2B is on.
202 */
203#define Z_EROFS_ADVISE_COMPACTED_2B_BIT 0
204
205#define Z_EROFS_ADVISE_COMPACTED_2B (1 << Z_EROFS_ADVISE_COMPACTED_2B_BIT)
206
207struct z_erofs_map_header {
208 __le32 h_reserved1;
209 __le16 h_advise;
210 /*
211 * bit 0-3 : algorithm type of head 1 (logical cluster type 01);
212 * bit 4-7 : algorithm type of head 2 (logical cluster type 11).
213 */
214 __u8 h_algorithmtype;
215 /*
216 * bit 0-2 : logical cluster bits - 12, e.g. 0 for 4096;
217 * bit 3-4 : (physical - logical) cluster bits of head 1:
218 * For example, if logical clustersize = 4096, 1 for 8192.
219 * bit 5-7 : (physical - logical) cluster bits of head 2.
220 */
221 __u8 h_clusterbits;
222};
223
224#define Z_EROFS_VLE_LEGACY_HEADER_PADDING 8
Gao Xiangaea12862018-07-26 20:21:44 +0800225
226/*
Gao Xiangea559e72019-09-04 10:08:57 +0800227 * Fixed-sized output compression ondisk Logical Extent cluster type:
Gao Xiangaea12862018-07-26 20:21:44 +0800228 * 0 - literal (uncompressed) cluster
229 * 1 - compressed cluster (for the head logical cluster)
230 * 2 - compressed cluster (for the other logical clusters)
231 *
232 * In detail,
233 * 0 - literal (uncompressed) cluster,
234 * di_advise = 0
235 * di_clusterofs = the literal data offset of the cluster
236 * di_blkaddr = the blkaddr of the literal cluster
237 *
238 * 1 - compressed cluster (for the head logical cluster)
239 * di_advise = 1
240 * di_clusterofs = the decompressed data offset of the cluster
241 * di_blkaddr = the blkaddr of the compressed cluster
242 *
243 * 2 - compressed cluster (for the other logical clusters)
244 * di_advise = 2
245 * di_clusterofs =
246 * the decompressed data offset in its own head cluster
247 * di_u.delta[0] = distance to its corresponding head cluster
248 * di_u.delta[1] = distance to its corresponding tail cluster
249 * (di_advise could be 0, 1 or 2)
250 */
Gao Xiang99691b42018-08-21 22:49:33 +0800251enum {
Gao Xiang60a49ba2019-09-04 10:08:49 +0800252 Z_EROFS_VLE_CLUSTER_TYPE_PLAIN = 0,
253 Z_EROFS_VLE_CLUSTER_TYPE_HEAD = 1,
254 Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD = 2,
255 Z_EROFS_VLE_CLUSTER_TYPE_RESERVED = 3,
Gao Xiang99691b42018-08-21 22:49:33 +0800256 Z_EROFS_VLE_CLUSTER_TYPE_MAX
257};
258
Gao Xiangaea12862018-07-26 20:21:44 +0800259#define Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS 2
260#define Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT 0
261
262struct z_erofs_vle_decompressed_index {
263 __le16 di_advise;
264 /* where to decompress in the head cluster */
265 __le16 di_clusterofs;
266
267 union {
268 /* for the head cluster */
269 __le32 blkaddr;
270 /*
271 * for the rest clusters
272 * eg. for 4k page-sized cluster, maximum 4K*64k = 256M)
273 * [0] - pointing to the head cluster
274 * [1] - pointing to the tail cluster
275 */
276 __le16 delta[2];
Gao Xianged34aa42019-09-04 10:08:51 +0800277 } di_u;
278};
Gao Xiangaea12862018-07-26 20:21:44 +0800279
Gao Xiangec8c2442019-06-24 15:22:51 +0800280#define Z_EROFS_VLE_LEGACY_INDEX_ALIGN(size) \
281 (round_up(size, sizeof(struct z_erofs_vle_decompressed_index)) + \
282 sizeof(struct z_erofs_map_header) + Z_EROFS_VLE_LEGACY_HEADER_PADDING)
Gao Xiangaea12862018-07-26 20:21:44 +0800283
284/* dirent sorts in alphabet order, thus we can do binary search */
285struct erofs_dirent {
Gao Xiang4b66eb52019-09-04 10:08:48 +0800286 __le64 nid; /* node number */
287 __le16 nameoff; /* start offset of file name */
288 __u8 file_type; /* file type */
289 __u8 reserved; /* reserved */
Gao Xiangaea12862018-07-26 20:21:44 +0800290} __packed;
291
Gao Xiang1d819c52019-08-16 15:11:42 +0800292/*
293 * EROFS file types should match generic FT_* types and
294 * it seems no need to add BUILD_BUG_ONs since potential
295 * unmatchness will break other fses as well...
296 */
Gao Xiangaea12862018-07-26 20:21:44 +0800297
298#define EROFS_NAME_LEN 255
299
300/* check the EROFS on-disk layout strictly at compile time */
301static inline void erofs_check_ondisk_layout_definitions(void)
302{
303 BUILD_BUG_ON(sizeof(struct erofs_super_block) != 128);
Gao Xiang8a765682019-09-04 10:08:54 +0800304 BUILD_BUG_ON(sizeof(struct erofs_inode_compact) != 32);
305 BUILD_BUG_ON(sizeof(struct erofs_inode_extended) != 64);
Gao Xiangaea12862018-07-26 20:21:44 +0800306 BUILD_BUG_ON(sizeof(struct erofs_xattr_ibody_header) != 12);
307 BUILD_BUG_ON(sizeof(struct erofs_xattr_entry) != 4);
Gao Xiangec8c2442019-06-24 15:22:51 +0800308 BUILD_BUG_ON(sizeof(struct z_erofs_map_header) != 8);
Gao Xiangaea12862018-07-26 20:21:44 +0800309 BUILD_BUG_ON(sizeof(struct z_erofs_vle_decompressed_index) != 8);
310 BUILD_BUG_ON(sizeof(struct erofs_dirent) != 12);
Gao Xiang99691b42018-08-21 22:49:33 +0800311
312 BUILD_BUG_ON(BIT(Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) <
313 Z_EROFS_VLE_CLUSTER_TYPE_MAX - 1);
Gao Xiangaea12862018-07-26 20:21:44 +0800314}
315
316#endif
317