blob: b1ee5654750d8bbdcab42b6afc6ab10a9bf0518e [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
Gao Xiang5efe5132019-06-13 16:35:41 +080014/*
Gao Xiang426a9302019-09-04 10:08:53 +080015 * Any bits that aren't in EROFS_ALL_FEATURE_INCOMPAT should
16 * be incompatible with this kernel version.
Gao Xiang5efe5132019-06-13 16:35:41 +080017 */
Gao Xiang426a9302019-09-04 10:08:53 +080018#define EROFS_FEATURE_INCOMPAT_LZ4_0PADDING 0x00000001
19#define EROFS_ALL_FEATURE_INCOMPAT EROFS_FEATURE_INCOMPAT_LZ4_0PADDING
Gao Xiang5efe5132019-06-13 16:35:41 +080020
Gao Xiang4b66eb52019-09-04 10:08:48 +080021/* 128-byte erofs on-disk super block */
Gao Xiangaea12862018-07-26 20:21:44 +080022struct erofs_super_block {
Gao Xiang4b66eb52019-09-04 10:08:48 +080023 __le32 magic; /* file system magic number */
24 __le32 checksum; /* crc32c(super_block) */
Gao Xiang426a9302019-09-04 10:08:53 +080025 __le32 feature_compat;
Gao Xiang4b66eb52019-09-04 10:08:48 +080026 __u8 blkszbits; /* support block_size == PAGE_SIZE only */
27 __u8 reserved;
Gao Xiangaea12862018-07-26 20:21:44 +080028
Gao Xiang4b66eb52019-09-04 10:08:48 +080029 __le16 root_nid; /* nid of root directory */
30 __le64 inos; /* total valid ino # (== f_files - f_favail) */
Gao Xiangaea12862018-07-26 20:21:44 +080031
Gao Xiang4b66eb52019-09-04 10:08:48 +080032 __le64 build_time; /* inode v1 time derivation */
33 __le32 build_time_nsec; /* inode v1 time derivation in nano scale */
34 __le32 blocks; /* used for statfs */
35 __le32 meta_blkaddr; /* start block address of metadata area */
36 __le32 xattr_blkaddr; /* start block address of shared xattr area */
37 __u8 uuid[16]; /* 128-bit uuid for volume */
38 __u8 volume_name[16]; /* volume name */
Gao Xiang426a9302019-09-04 10:08:53 +080039 __le32 feature_incompat;
Gao Xiangaea12862018-07-26 20:21:44 +080040
Gao Xiang4b66eb52019-09-04 10:08:48 +080041 __u8 reserved2[44];
Gao Xianged34aa42019-09-04 10:08:51 +080042};
Gao Xiangaea12862018-07-26 20:21:44 +080043
Gao Xiangaea12862018-07-26 20:21:44 +080044/*
Gao Xiangea559e72019-09-04 10:08:57 +080045 * erofs inode datalayout (i_format in on-disk inode):
Gao Xiangaea12862018-07-26 20:21:44 +080046 * 0 - inode plain without inline data A:
47 * inode, [xattrs], ... | ... | no-holed data
Gao Xiangec8c2442019-06-24 15:22:51 +080048 * 1 - inode VLE compression B (legacy):
Gao Xiangaea12862018-07-26 20:21:44 +080049 * inode, [xattrs], extents ... | ...
50 * 2 - inode plain with inline data C:
51 * inode, [xattrs], last_inline_data, ... | ... | no-holed data
Gao Xiangec8c2442019-06-24 15:22:51 +080052 * 3 - inode compression D:
53 * inode, [xattrs], map_header, extents ... | ...
54 * 4~7 - reserved
Gao Xiangaea12862018-07-26 20:21:44 +080055 */
56enum {
Gao Xiang60a49ba2019-09-04 10:08:49 +080057 EROFS_INODE_FLAT_PLAIN = 0,
58 EROFS_INODE_FLAT_COMPRESSION_LEGACY = 1,
59 EROFS_INODE_FLAT_INLINE = 2,
60 EROFS_INODE_FLAT_COMPRESSION = 3,
Gao Xiang8a765682019-09-04 10:08:54 +080061 EROFS_INODE_DATALAYOUT_MAX
Gao Xiangaea12862018-07-26 20:21:44 +080062};
Gao Xiangccd9c192018-12-12 01:57:30 +080063
Gao Xiang3d2969f2019-08-13 10:30:52 +080064static inline bool erofs_inode_is_data_compressed(unsigned int datamode)
Gao Xiangec8c2442019-06-24 15:22:51 +080065{
Gao Xiangc39747f2019-09-04 10:08:52 +080066 return datamode == EROFS_INODE_FLAT_COMPRESSION ||
67 datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY;
Gao Xiangec8c2442019-06-24 15:22:51 +080068}
69
Gao Xiangccd9c192018-12-12 01:57:30 +080070/* bit definitions of inode i_advise */
Gao Xiangaea12862018-07-26 20:21:44 +080071#define EROFS_I_VERSION_BITS 1
Gao Xiang8a765682019-09-04 10:08:54 +080072#define EROFS_I_DATALAYOUT_BITS 3
Gao Xiangaea12862018-07-26 20:21:44 +080073
74#define EROFS_I_VERSION_BIT 0
Gao Xiang8a765682019-09-04 10:08:54 +080075#define EROFS_I_DATALAYOUT_BIT 1
Gao Xiangaea12862018-07-26 20:21:44 +080076
Gao Xiang4b66eb52019-09-04 10:08:48 +080077/* 32-byte reduced form of an ondisk inode */
Gao Xiang8a765682019-09-04 10:08:54 +080078struct erofs_inode_compact {
79 __le16 i_format; /* inode format hints */
Gao Xiangaea12862018-07-26 20:21:44 +080080
81/* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
Gao Xiang4b66eb52019-09-04 10:08:48 +080082 __le16 i_xattr_icount;
83 __le16 i_mode;
84 __le16 i_nlink;
85 __le32 i_size;
86 __le32 i_reserved;
87 union {
Gao Xiangaea12862018-07-26 20:21:44 +080088 /* file total compressed blocks for data mapping 1 */
89 __le32 compressed_blocks;
90 __le32 raw_blkaddr;
91
92 /* for device files, used to indicate old/new device # */
93 __le32 rdev;
Gao Xianged34aa42019-09-04 10:08:51 +080094 } i_u;
Gao Xiang4b66eb52019-09-04 10:08:48 +080095 __le32 i_ino; /* only used for 32-bit stat compatibility */
96 __le16 i_uid;
97 __le16 i_gid;
98 __le32 i_reserved2;
Gao Xianged34aa42019-09-04 10:08:51 +080099};
Gao Xiangaea12862018-07-26 20:21:44 +0800100
101/* 32 bytes on-disk inode */
Gao Xiang8a765682019-09-04 10:08:54 +0800102#define EROFS_INODE_LAYOUT_COMPACT 0
Gao Xiangaea12862018-07-26 20:21:44 +0800103/* 64 bytes on-disk inode */
Gao Xiang8a765682019-09-04 10:08:54 +0800104#define EROFS_INODE_LAYOUT_EXTENDED 1
Gao Xiangaea12862018-07-26 20:21:44 +0800105
Gao Xiang4b66eb52019-09-04 10:08:48 +0800106/* 64-byte complete form of an ondisk inode */
Gao Xiang8a765682019-09-04 10:08:54 +0800107struct erofs_inode_extended {
108 __le16 i_format; /* inode format hints */
Gao Xiangaea12862018-07-26 20:21:44 +0800109
Gao Xiangcead56f2019-07-31 23:57:34 +0800110/* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
Gao Xiang4b66eb52019-09-04 10:08:48 +0800111 __le16 i_xattr_icount;
112 __le16 i_mode;
113 __le16 i_reserved;
114 __le64 i_size;
115 union {
Gao Xiangaea12862018-07-26 20:21:44 +0800116 /* file total compressed blocks for data mapping 1 */
117 __le32 compressed_blocks;
118 __le32 raw_blkaddr;
119
120 /* for device files, used to indicate old/new device # */
121 __le32 rdev;
Gao Xianged34aa42019-09-04 10:08:51 +0800122 } i_u;
Gao Xiangaea12862018-07-26 20:21:44 +0800123
124 /* only used for 32-bit stat compatibility */
Gao Xiang4b66eb52019-09-04 10:08:48 +0800125 __le32 i_ino;
Gao Xiangaea12862018-07-26 20:21:44 +0800126
Gao Xiang4b66eb52019-09-04 10:08:48 +0800127 __le32 i_uid;
128 __le32 i_gid;
129 __le64 i_ctime;
130 __le32 i_ctime_nsec;
131 __le32 i_nlink;
132 __u8 i_reserved2[16];
Gao Xianged34aa42019-09-04 10:08:51 +0800133};
Gao Xiangaea12862018-07-26 20:21:44 +0800134
135#define EROFS_MAX_SHARED_XATTRS (128)
136/* h_shared_count between 129 ... 255 are special # */
137#define EROFS_SHARED_XATTR_EXTENT (255)
138
139/*
140 * inline xattrs (n == i_xattr_icount):
141 * erofs_xattr_ibody_header(1) + (n - 1) * 4 bytes
142 * 12 bytes / \
143 * / \
144 * /-----------------------\
145 * | erofs_xattr_entries+ |
146 * +-----------------------+
147 * inline xattrs must starts in erofs_xattr_ibody_header,
148 * for read-only fs, no need to introduce h_refcount
149 */
150struct erofs_xattr_ibody_header {
Gao Xiangcead56f2019-07-31 23:57:34 +0800151 __le32 h_reserved;
Gao Xiangaea12862018-07-26 20:21:44 +0800152 __u8 h_shared_count;
Gao Xiangcead56f2019-07-31 23:57:34 +0800153 __u8 h_reserved2[7];
Gao Xiangaea12862018-07-26 20:21:44 +0800154 __le32 h_shared_xattrs[0]; /* shared xattr id array */
Gao Xianged34aa42019-09-04 10:08:51 +0800155};
Gao Xiangaea12862018-07-26 20:21:44 +0800156
157/* Name indexes */
158#define EROFS_XATTR_INDEX_USER 1
159#define EROFS_XATTR_INDEX_POSIX_ACL_ACCESS 2
160#define EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT 3
161#define EROFS_XATTR_INDEX_TRUSTED 4
162#define EROFS_XATTR_INDEX_LUSTRE 5
163#define EROFS_XATTR_INDEX_SECURITY 6
164
165/* xattr entry (for both inline & shared xattrs) */
166struct erofs_xattr_entry {
167 __u8 e_name_len; /* length of name */
168 __u8 e_name_index; /* attribute name index */
169 __le16 e_value_size; /* size of attribute value */
170 /* followed by e_name and e_value */
171 char e_name[0]; /* attribute name */
Gao Xianged34aa42019-09-04 10:08:51 +0800172};
Gao Xiangaea12862018-07-26 20:21:44 +0800173
Gao Xiangb6796ab2019-09-04 10:08:50 +0800174static inline unsigned int erofs_xattr_ibody_size(__le16 i_xattr_icount)
175{
176 if (!i_xattr_icount)
177 return 0;
178
179 return sizeof(struct erofs_xattr_ibody_header) +
180 sizeof(__u32) * (le16_to_cpu(i_xattr_icount) - 1);
181}
Gao Xiangaea12862018-07-26 20:21:44 +0800182
183#define EROFS_XATTR_ALIGN(size) round_up(size, sizeof(struct erofs_xattr_entry))
Gao Xiangb6796ab2019-09-04 10:08:50 +0800184
185static inline unsigned int erofs_xattr_entry_size(struct erofs_xattr_entry *e)
186{
187 return EROFS_XATTR_ALIGN(sizeof(struct erofs_xattr_entry) +
188 e->e_name_len + le16_to_cpu(e->e_value_size));
189}
Gao Xiangaea12862018-07-26 20:21:44 +0800190
Gao Xiangea559e72019-09-04 10:08:57 +0800191/* available compression algorithm types (for h_algorithmtype) */
Gao Xiangec8c2442019-06-24 15:22:51 +0800192enum {
Gao Xiang60a49ba2019-09-04 10:08:49 +0800193 Z_EROFS_COMPRESSION_LZ4 = 0,
Gao Xiangec8c2442019-06-24 15:22:51 +0800194 Z_EROFS_COMPRESSION_MAX
195};
196
197/*
198 * bit 0 : COMPACTED_2B indexes (0 - off; 1 - on)
199 * e.g. for 4k logical cluster size, 4B if compacted 2B is off;
200 * (4B) + 2B + (4B) if compacted 2B is on.
201 */
202#define Z_EROFS_ADVISE_COMPACTED_2B_BIT 0
203
204#define Z_EROFS_ADVISE_COMPACTED_2B (1 << Z_EROFS_ADVISE_COMPACTED_2B_BIT)
205
206struct z_erofs_map_header {
207 __le32 h_reserved1;
208 __le16 h_advise;
209 /*
210 * bit 0-3 : algorithm type of head 1 (logical cluster type 01);
211 * bit 4-7 : algorithm type of head 2 (logical cluster type 11).
212 */
213 __u8 h_algorithmtype;
214 /*
215 * bit 0-2 : logical cluster bits - 12, e.g. 0 for 4096;
216 * bit 3-4 : (physical - logical) cluster bits of head 1:
217 * For example, if logical clustersize = 4096, 1 for 8192.
218 * bit 5-7 : (physical - logical) cluster bits of head 2.
219 */
220 __u8 h_clusterbits;
221};
222
223#define Z_EROFS_VLE_LEGACY_HEADER_PADDING 8
Gao Xiangaea12862018-07-26 20:21:44 +0800224
225/*
Gao Xiangea559e72019-09-04 10:08:57 +0800226 * Fixed-sized output compression ondisk Logical Extent cluster type:
Gao Xiangaea12862018-07-26 20:21:44 +0800227 * 0 - literal (uncompressed) cluster
228 * 1 - compressed cluster (for the head logical cluster)
229 * 2 - compressed cluster (for the other logical clusters)
230 *
231 * In detail,
232 * 0 - literal (uncompressed) cluster,
233 * di_advise = 0
234 * di_clusterofs = the literal data offset of the cluster
235 * di_blkaddr = the blkaddr of the literal cluster
236 *
237 * 1 - compressed cluster (for the head logical cluster)
238 * di_advise = 1
239 * di_clusterofs = the decompressed data offset of the cluster
240 * di_blkaddr = the blkaddr of the compressed cluster
241 *
242 * 2 - compressed cluster (for the other logical clusters)
243 * di_advise = 2
244 * di_clusterofs =
245 * the decompressed data offset in its own head cluster
246 * di_u.delta[0] = distance to its corresponding head cluster
247 * di_u.delta[1] = distance to its corresponding tail cluster
248 * (di_advise could be 0, 1 or 2)
249 */
Gao Xiang99691b42018-08-21 22:49:33 +0800250enum {
Gao Xiang60a49ba2019-09-04 10:08:49 +0800251 Z_EROFS_VLE_CLUSTER_TYPE_PLAIN = 0,
252 Z_EROFS_VLE_CLUSTER_TYPE_HEAD = 1,
253 Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD = 2,
254 Z_EROFS_VLE_CLUSTER_TYPE_RESERVED = 3,
Gao Xiang99691b42018-08-21 22:49:33 +0800255 Z_EROFS_VLE_CLUSTER_TYPE_MAX
256};
257
Gao Xiangaea12862018-07-26 20:21:44 +0800258#define Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS 2
259#define Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT 0
260
261struct z_erofs_vle_decompressed_index {
262 __le16 di_advise;
263 /* where to decompress in the head cluster */
264 __le16 di_clusterofs;
265
266 union {
267 /* for the head cluster */
268 __le32 blkaddr;
269 /*
270 * for the rest clusters
271 * eg. for 4k page-sized cluster, maximum 4K*64k = 256M)
272 * [0] - pointing to the head cluster
273 * [1] - pointing to the tail cluster
274 */
275 __le16 delta[2];
Gao Xianged34aa42019-09-04 10:08:51 +0800276 } di_u;
277};
Gao Xiangaea12862018-07-26 20:21:44 +0800278
Gao Xiangec8c2442019-06-24 15:22:51 +0800279#define Z_EROFS_VLE_LEGACY_INDEX_ALIGN(size) \
280 (round_up(size, sizeof(struct z_erofs_vle_decompressed_index)) + \
281 sizeof(struct z_erofs_map_header) + Z_EROFS_VLE_LEGACY_HEADER_PADDING)
Gao Xiangaea12862018-07-26 20:21:44 +0800282
283/* dirent sorts in alphabet order, thus we can do binary search */
284struct erofs_dirent {
Gao Xiang4b66eb52019-09-04 10:08:48 +0800285 __le64 nid; /* node number */
286 __le16 nameoff; /* start offset of file name */
287 __u8 file_type; /* file type */
288 __u8 reserved; /* reserved */
Gao Xiangaea12862018-07-26 20:21:44 +0800289} __packed;
290
Gao Xiang1d819c52019-08-16 15:11:42 +0800291/*
292 * EROFS file types should match generic FT_* types and
293 * it seems no need to add BUILD_BUG_ONs since potential
294 * unmatchness will break other fses as well...
295 */
Gao Xiangaea12862018-07-26 20:21:44 +0800296
297#define EROFS_NAME_LEN 255
298
299/* check the EROFS on-disk layout strictly at compile time */
300static inline void erofs_check_ondisk_layout_definitions(void)
301{
302 BUILD_BUG_ON(sizeof(struct erofs_super_block) != 128);
Gao Xiang8a765682019-09-04 10:08:54 +0800303 BUILD_BUG_ON(sizeof(struct erofs_inode_compact) != 32);
304 BUILD_BUG_ON(sizeof(struct erofs_inode_extended) != 64);
Gao Xiangaea12862018-07-26 20:21:44 +0800305 BUILD_BUG_ON(sizeof(struct erofs_xattr_ibody_header) != 12);
306 BUILD_BUG_ON(sizeof(struct erofs_xattr_entry) != 4);
Gao Xiangec8c2442019-06-24 15:22:51 +0800307 BUILD_BUG_ON(sizeof(struct z_erofs_map_header) != 8);
Gao Xiangaea12862018-07-26 20:21:44 +0800308 BUILD_BUG_ON(sizeof(struct z_erofs_vle_decompressed_index) != 8);
309 BUILD_BUG_ON(sizeof(struct erofs_dirent) != 12);
Gao Xiang99691b42018-08-21 22:49:33 +0800310
311 BUILD_BUG_ON(BIT(Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) <
312 Z_EROFS_VLE_CLUSTER_TYPE_MAX - 1);
Gao Xiangaea12862018-07-26 20:21:44 +0800313}
314
315#endif
316