1 /* SPDX-License-Identifier: GPL-2.0-only OR Apache-2.0 */ 2 /* 3 * EROFS (Enhanced ROM File System) on-disk format definition 4 * 5 * Copyright (C) 2017-2018 HUAWEI, Inc. 6 * https://www.huawei.com/ 7 * Created by Gao Xiang <gaoxiang25@huawei.com> 8 */ 9 #ifndef __EROFS_FS_H 10 #define __EROFS_FS_H 11 12 #define EROFS_SUPER_OFFSET 1024 13 14 #define EROFS_FEATURE_COMPAT_SB_CHKSUM 0x00000001 15 16 /* 17 * Any bits that aren't in EROFS_ALL_FEATURE_INCOMPAT should 18 * be incompatible with this kernel version. 19 */ 20 #define EROFS_FEATURE_INCOMPAT_LZ4_0PADDING 0x00000001 21 #define EROFS_FEATURE_INCOMPAT_COMPR_CFGS 0x00000002 22 #define EROFS_ALL_FEATURE_INCOMPAT EROFS_FEATURE_INCOMPAT_LZ4_0PADDING 23 24 #define EROFS_SB_EXTSLOT_SIZE 16 25 26 /* erofs on-disk super block (currently 128 bytes) */ 27 struct erofs_super_block { 28 __le32 magic; /* file system magic number */ 29 __le32 checksum; /* crc32c(super_block) */ 30 __le32 feature_compat; 31 __u8 blkszbits; /* support block_size == PAGE_SIZE only */ 32 __u8 sb_extslots; /* superblock size = 128 + sb_extslots * 16 */ 33 34 __le16 root_nid; /* nid of root directory */ 35 __le64 inos; /* total valid ino # (== f_files - f_favail) */ 36 37 __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 */ 44 __le32 feature_incompat; 45 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; 51 __u8 reserved2[42]; 52 }; 53 54 /* 55 * erofs inode datalayout (i_format in on-disk inode): 56 * 0 - inode plain without inline data A: 57 * inode, [xattrs], ... | ... | no-holed data 58 * 1 - inode VLE compression B (legacy): 59 * inode, [xattrs], extents ... | ... 60 * 2 - inode plain with inline data C: 61 * inode, [xattrs], last_inline_data, ... | ... | no-holed data 62 * 3 - inode compression D: 63 * inode, [xattrs], map_header, extents ... | ... 64 * 4~7 - reserved 65 */ 66 enum { 67 EROFS_INODE_FLAT_PLAIN = 0, 68 EROFS_INODE_FLAT_COMPRESSION_LEGACY = 1, 69 EROFS_INODE_FLAT_INLINE = 2, 70 EROFS_INODE_FLAT_COMPRESSION = 3, 71 EROFS_INODE_DATALAYOUT_MAX 72 }; 73 74 static inline bool erofs_inode_is_data_compressed(unsigned int datamode) 75 { 76 return datamode == EROFS_INODE_FLAT_COMPRESSION || 77 datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY; 78 } 79 80 /* bit definitions of inode i_advise */ 81 #define EROFS_I_VERSION_BITS 1 82 #define EROFS_I_DATALAYOUT_BITS 3 83 84 #define EROFS_I_VERSION_BIT 0 85 #define EROFS_I_DATALAYOUT_BIT 1 86 87 #define EROFS_I_ALL \ 88 ((1 << (EROFS_I_DATALAYOUT_BIT + EROFS_I_DATALAYOUT_BITS)) - 1) 89 90 /* 32-byte reduced form of an ondisk inode */ 91 struct erofs_inode_compact { 92 __le16 i_format; /* inode format hints */ 93 94 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */ 95 __le16 i_xattr_icount; 96 __le16 i_mode; 97 __le16 i_nlink; 98 __le32 i_size; 99 __le32 i_reserved; 100 union { 101 /* 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; 107 } i_u; 108 __le32 i_ino; /* only used for 32-bit stat compatibility */ 109 __le16 i_uid; 110 __le16 i_gid; 111 __le32 i_reserved2; 112 }; 113 114 /* 32 bytes on-disk inode */ 115 #define EROFS_INODE_LAYOUT_COMPACT 0 116 /* 64 bytes on-disk inode */ 117 #define EROFS_INODE_LAYOUT_EXTENDED 1 118 119 /* 64-byte complete form of an ondisk inode */ 120 struct erofs_inode_extended { 121 __le16 i_format; /* inode format hints */ 122 123 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */ 124 __le16 i_xattr_icount; 125 __le16 i_mode; 126 __le16 i_reserved; 127 __le64 i_size; 128 union { 129 /* 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; 135 } i_u; 136 137 /* only used for 32-bit stat compatibility */ 138 __le32 i_ino; 139 140 __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]; 146 }; 147 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 */ 163 struct erofs_xattr_ibody_header { 164 __le32 h_reserved; 165 __u8 h_shared_count; 166 __u8 h_reserved2[7]; 167 __le32 h_shared_xattrs[0]; /* shared xattr id array */ 168 }; 169 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) */ 179 struct 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 */ 185 }; 186 187 static 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 } 195 196 #define EROFS_XATTR_ALIGN(size) round_up(size, sizeof(struct erofs_xattr_entry)) 197 198 static 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 } 203 204 /* maximum supported size of a physical compression cluster */ 205 #define Z_EROFS_PCLUSTER_MAX_SIZE (1024 * 1024) 206 207 /* available compression algorithm types (for h_algorithmtype) */ 208 enum { 209 Z_EROFS_COMPRESSION_LZ4 = 0, 210 Z_EROFS_COMPRESSION_MAX 211 }; 212 #define Z_EROFS_ALL_COMPR_ALGS (1 << (Z_EROFS_COMPRESSION_MAX - 1)) 213 214 /* 14 bytes (+ length field = 16 bytes) */ 215 struct z_erofs_lz4_cfgs { 216 __le16 max_distance; 217 u8 reserved[12]; 218 } __packed; 219 220 /* 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 229 struct 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; 239 * bit 3-7 : reserved. 240 */ 241 __u8 h_clusterbits; 242 }; 243 244 #define Z_EROFS_VLE_LEGACY_HEADER_PADDING 8 245 246 /* 247 * Fixed-sized output compression ondisk Logical Extent cluster type: 248 * 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 */ 271 enum { 272 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, 276 Z_EROFS_VLE_CLUSTER_TYPE_MAX 277 }; 278 279 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS 2 280 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT 0 281 282 struct 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]; 297 } di_u; 298 }; 299 300 #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) 303 304 /* dirent sorts in alphabet order, thus we can do binary search */ 305 struct erofs_dirent { 306 __le64 nid; /* node number */ 307 __le16 nameoff; /* start offset of file name */ 308 __u8 file_type; /* file type */ 309 __u8 reserved; /* reserved */ 310 } __packed; 311 312 /* 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 */ 317 318 #define EROFS_NAME_LEN 255 319 320 /* check the EROFS on-disk layout strictly at compile time */ 321 static inline void erofs_check_ondisk_layout_definitions(void) 322 { 323 BUILD_BUG_ON(sizeof(struct erofs_super_block) != 128); 324 BUILD_BUG_ON(sizeof(struct erofs_inode_compact) != 32); 325 BUILD_BUG_ON(sizeof(struct erofs_inode_extended) != 64); 326 BUILD_BUG_ON(sizeof(struct erofs_xattr_ibody_header) != 12); 327 BUILD_BUG_ON(sizeof(struct erofs_xattr_entry) != 4); 328 BUILD_BUG_ON(sizeof(struct z_erofs_map_header) != 8); 329 BUILD_BUG_ON(sizeof(struct z_erofs_vle_decompressed_index) != 8); 330 BUILD_BUG_ON(sizeof(struct erofs_dirent) != 12); 331 332 BUILD_BUG_ON(BIT(Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) < 333 Z_EROFS_VLE_CLUSTER_TYPE_MAX - 1); 334 } 335 336 #endif 337 338