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 */ 8 #ifndef __EROFS_FS_H 9 #define __EROFS_FS_H 10 11 #define EROFS_SUPER_OFFSET 1024 12 13 #define EROFS_FEATURE_COMPAT_SB_CHKSUM 0x00000001 14 15 /* 16 * Any bits that aren't in EROFS_ALL_FEATURE_INCOMPAT should 17 * be incompatible with this kernel version. 18 */ 19 #define EROFS_FEATURE_INCOMPAT_LZ4_0PADDING 0x00000001 20 #define EROFS_FEATURE_INCOMPAT_COMPR_CFGS 0x00000002 21 #define EROFS_FEATURE_INCOMPAT_BIG_PCLUSTER 0x00000002 22 #define EROFS_ALL_FEATURE_INCOMPAT \ 23 (EROFS_FEATURE_INCOMPAT_LZ4_0PADDING | \ 24 EROFS_FEATURE_INCOMPAT_COMPR_CFGS | \ 25 EROFS_FEATURE_INCOMPAT_BIG_PCLUSTER) 26 27 #define EROFS_SB_EXTSLOT_SIZE 16 28 29 /* erofs on-disk super block (currently 128 bytes) */ 30 struct erofs_super_block { 31 __le32 magic; /* file system magic number */ 32 __le32 checksum; /* crc32c(super_block) */ 33 __le32 feature_compat; 34 __u8 blkszbits; /* support block_size == PAGE_SIZE only */ 35 __u8 sb_extslots; /* superblock size = 128 + sb_extslots * 16 */ 36 37 __le16 root_nid; /* nid of root directory */ 38 __le64 inos; /* total valid ino # (== f_files - f_favail) */ 39 40 __le64 build_time; /* inode v1 time derivation */ 41 __le32 build_time_nsec; /* inode v1 time derivation in nano scale */ 42 __le32 blocks; /* used for statfs */ 43 __le32 meta_blkaddr; /* start block address of metadata area */ 44 __le32 xattr_blkaddr; /* start block address of shared xattr area */ 45 __u8 uuid[16]; /* 128-bit uuid for volume */ 46 __u8 volume_name[16]; /* volume name */ 47 __le32 feature_incompat; 48 union { 49 /* bitmap for available compression algorithms */ 50 __le16 available_compr_algs; 51 /* customized sliding window size instead of 64k by default */ 52 __le16 lz4_max_distance; 53 } __packed u1; 54 __u8 reserved2[42]; 55 }; 56 57 /* 58 * erofs inode datalayout (i_format in on-disk inode): 59 * 0 - inode plain without inline data A: 60 * inode, [xattrs], ... | ... | no-holed data 61 * 1 - inode VLE compression B (legacy): 62 * inode, [xattrs], extents ... | ... 63 * 2 - inode plain with inline data C: 64 * inode, [xattrs], last_inline_data, ... | ... | no-holed data 65 * 3 - inode compression D: 66 * inode, [xattrs], map_header, extents ... | ... 67 * 4~7 - reserved 68 */ 69 enum { 70 EROFS_INODE_FLAT_PLAIN = 0, 71 EROFS_INODE_FLAT_COMPRESSION_LEGACY = 1, 72 EROFS_INODE_FLAT_INLINE = 2, 73 EROFS_INODE_FLAT_COMPRESSION = 3, 74 EROFS_INODE_DATALAYOUT_MAX 75 }; 76 77 static inline bool erofs_inode_is_data_compressed(unsigned int datamode) 78 { 79 return datamode == EROFS_INODE_FLAT_COMPRESSION || 80 datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY; 81 } 82 83 /* bit definitions of inode i_advise */ 84 #define EROFS_I_VERSION_BITS 1 85 #define EROFS_I_DATALAYOUT_BITS 3 86 87 #define EROFS_I_VERSION_BIT 0 88 #define EROFS_I_DATALAYOUT_BIT 1 89 90 #define EROFS_I_ALL \ 91 ((1 << (EROFS_I_DATALAYOUT_BIT + EROFS_I_DATALAYOUT_BITS)) - 1) 92 93 /* 32-byte reduced form of an ondisk inode */ 94 struct erofs_inode_compact { 95 __le16 i_format; /* inode format hints */ 96 97 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */ 98 __le16 i_xattr_icount; 99 __le16 i_mode; 100 __le16 i_nlink; 101 __le32 i_size; 102 __le32 i_reserved; 103 union { 104 /* file total compressed blocks for data mapping 1 */ 105 __le32 compressed_blocks; 106 __le32 raw_blkaddr; 107 108 /* for device files, used to indicate old/new device # */ 109 __le32 rdev; 110 } i_u; 111 __le32 i_ino; /* only used for 32-bit stat compatibility */ 112 __le16 i_uid; 113 __le16 i_gid; 114 __le32 i_reserved2; 115 }; 116 117 /* 32 bytes on-disk inode */ 118 #define EROFS_INODE_LAYOUT_COMPACT 0 119 /* 64 bytes on-disk inode */ 120 #define EROFS_INODE_LAYOUT_EXTENDED 1 121 122 /* 64-byte complete form of an ondisk inode */ 123 struct erofs_inode_extended { 124 __le16 i_format; /* inode format hints */ 125 126 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */ 127 __le16 i_xattr_icount; 128 __le16 i_mode; 129 __le16 i_reserved; 130 __le64 i_size; 131 union { 132 /* file total compressed blocks for data mapping 1 */ 133 __le32 compressed_blocks; 134 __le32 raw_blkaddr; 135 136 /* for device files, used to indicate old/new device # */ 137 __le32 rdev; 138 } i_u; 139 140 /* only used for 32-bit stat compatibility */ 141 __le32 i_ino; 142 143 __le32 i_uid; 144 __le32 i_gid; 145 __le64 i_ctime; 146 __le32 i_ctime_nsec; 147 __le32 i_nlink; 148 __u8 i_reserved2[16]; 149 }; 150 151 #define EROFS_MAX_SHARED_XATTRS (128) 152 /* h_shared_count between 129 ... 255 are special # */ 153 #define EROFS_SHARED_XATTR_EXTENT (255) 154 155 /* 156 * inline xattrs (n == i_xattr_icount): 157 * erofs_xattr_ibody_header(1) + (n - 1) * 4 bytes 158 * 12 bytes / \ 159 * / \ 160 * /-----------------------\ 161 * | erofs_xattr_entries+ | 162 * +-----------------------+ 163 * inline xattrs must starts in erofs_xattr_ibody_header, 164 * for read-only fs, no need to introduce h_refcount 165 */ 166 struct erofs_xattr_ibody_header { 167 __le32 h_reserved; 168 __u8 h_shared_count; 169 __u8 h_reserved2[7]; 170 __le32 h_shared_xattrs[0]; /* shared xattr id array */ 171 }; 172 173 /* Name indexes */ 174 #define EROFS_XATTR_INDEX_USER 1 175 #define EROFS_XATTR_INDEX_POSIX_ACL_ACCESS 2 176 #define EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT 3 177 #define EROFS_XATTR_INDEX_TRUSTED 4 178 #define EROFS_XATTR_INDEX_LUSTRE 5 179 #define EROFS_XATTR_INDEX_SECURITY 6 180 181 /* xattr entry (for both inline & shared xattrs) */ 182 struct erofs_xattr_entry { 183 __u8 e_name_len; /* length of name */ 184 __u8 e_name_index; /* attribute name index */ 185 __le16 e_value_size; /* size of attribute value */ 186 /* followed by e_name and e_value */ 187 char e_name[0]; /* attribute name */ 188 }; 189 190 static inline unsigned int erofs_xattr_ibody_size(__le16 i_xattr_icount) 191 { 192 if (!i_xattr_icount) 193 return 0; 194 195 return sizeof(struct erofs_xattr_ibody_header) + 196 sizeof(__u32) * (le16_to_cpu(i_xattr_icount) - 1); 197 } 198 199 #define EROFS_XATTR_ALIGN(size) round_up(size, sizeof(struct erofs_xattr_entry)) 200 201 static inline unsigned int erofs_xattr_entry_size(struct erofs_xattr_entry *e) 202 { 203 return EROFS_XATTR_ALIGN(sizeof(struct erofs_xattr_entry) + 204 e->e_name_len + le16_to_cpu(e->e_value_size)); 205 } 206 207 /* maximum supported size of a physical compression cluster */ 208 #define Z_EROFS_PCLUSTER_MAX_SIZE (1024 * 1024) 209 210 /* available compression algorithm types (for h_algorithmtype) */ 211 enum { 212 Z_EROFS_COMPRESSION_LZ4 = 0, 213 Z_EROFS_COMPRESSION_MAX 214 }; 215 #define Z_EROFS_ALL_COMPR_ALGS (1 << (Z_EROFS_COMPRESSION_MAX - 1)) 216 217 /* 14 bytes (+ length field = 16 bytes) */ 218 struct z_erofs_lz4_cfgs { 219 __le16 max_distance; 220 __le16 max_pclusterblks; 221 u8 reserved[10]; 222 } __packed; 223 224 /* 225 * bit 0 : COMPACTED_2B indexes (0 - off; 1 - on) 226 * e.g. for 4k logical cluster size, 4B if compacted 2B is off; 227 * (4B) + 2B + (4B) if compacted 2B is on. 228 * bit 1 : HEAD1 big pcluster (0 - off; 1 - on) 229 * bit 2 : HEAD2 big pcluster (0 - off; 1 - on) 230 */ 231 #define Z_EROFS_ADVISE_COMPACTED_2B 0x0001 232 #define Z_EROFS_ADVISE_BIG_PCLUSTER_1 0x0002 233 #define Z_EROFS_ADVISE_BIG_PCLUSTER_2 0x0004 234 235 struct z_erofs_map_header { 236 __le32 h_reserved1; 237 __le16 h_advise; 238 /* 239 * bit 0-3 : algorithm type of head 1 (logical cluster type 01); 240 * bit 4-7 : algorithm type of head 2 (logical cluster type 11). 241 */ 242 __u8 h_algorithmtype; 243 /* 244 * bit 0-2 : logical cluster bits - 12, e.g. 0 for 4096; 245 * bit 3-7 : reserved. 246 */ 247 __u8 h_clusterbits; 248 }; 249 250 #define Z_EROFS_VLE_LEGACY_HEADER_PADDING 8 251 252 /* 253 * Fixed-sized output compression ondisk Logical Extent cluster type: 254 * 0 - literal (uncompressed) cluster 255 * 1 - compressed cluster (for the head logical cluster) 256 * 2 - compressed cluster (for the other logical clusters) 257 * 258 * In detail, 259 * 0 - literal (uncompressed) cluster, 260 * di_advise = 0 261 * di_clusterofs = the literal data offset of the cluster 262 * di_blkaddr = the blkaddr of the literal cluster 263 * 264 * 1 - compressed cluster (for the head logical cluster) 265 * di_advise = 1 266 * di_clusterofs = the decompressed data offset of the cluster 267 * di_blkaddr = the blkaddr of the compressed cluster 268 * 269 * 2 - compressed cluster (for the other logical clusters) 270 * di_advise = 2 271 * di_clusterofs = 272 * the decompressed data offset in its own head cluster 273 * di_u.delta[0] = distance to its corresponding head cluster 274 * di_u.delta[1] = distance to its corresponding tail cluster 275 * (di_advise could be 0, 1 or 2) 276 */ 277 enum { 278 Z_EROFS_VLE_CLUSTER_TYPE_PLAIN = 0, 279 Z_EROFS_VLE_CLUSTER_TYPE_HEAD = 1, 280 Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD = 2, 281 Z_EROFS_VLE_CLUSTER_TYPE_RESERVED = 3, 282 Z_EROFS_VLE_CLUSTER_TYPE_MAX 283 }; 284 285 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS 2 286 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT 0 287 288 /* 289 * D0_CBLKCNT will be marked _only_ at the 1st non-head lcluster to store the 290 * compressed block count of a compressed extent (in logical clusters, aka. 291 * block count of a pcluster). 292 */ 293 #define Z_EROFS_VLE_DI_D0_CBLKCNT (1 << 11) 294 295 struct z_erofs_vle_decompressed_index { 296 __le16 di_advise; 297 /* where to decompress in the head cluster */ 298 __le16 di_clusterofs; 299 300 union { 301 /* for the head cluster */ 302 __le32 blkaddr; 303 /* 304 * for the rest clusters 305 * eg. for 4k page-sized cluster, maximum 4K*64k = 256M) 306 * [0] - pointing to the head cluster 307 * [1] - pointing to the tail cluster 308 */ 309 __le16 delta[2]; 310 } di_u; 311 }; 312 313 #define Z_EROFS_VLE_LEGACY_INDEX_ALIGN(size) \ 314 (round_up(size, sizeof(struct z_erofs_vle_decompressed_index)) + \ 315 sizeof(struct z_erofs_map_header) + Z_EROFS_VLE_LEGACY_HEADER_PADDING) 316 317 /* dirent sorts in alphabet order, thus we can do binary search */ 318 struct erofs_dirent { 319 __le64 nid; /* node number */ 320 __le16 nameoff; /* start offset of file name */ 321 __u8 file_type; /* file type */ 322 __u8 reserved; /* reserved */ 323 } __packed; 324 325 /* 326 * EROFS file types should match generic FT_* types and 327 * it seems no need to add BUILD_BUG_ONs since potential 328 * unmatchness will break other fses as well... 329 */ 330 331 #define EROFS_NAME_LEN 255 332 333 /* check the EROFS on-disk layout strictly at compile time */ 334 static inline void erofs_check_ondisk_layout_definitions(void) 335 { 336 BUILD_BUG_ON(sizeof(struct erofs_super_block) != 128); 337 BUILD_BUG_ON(sizeof(struct erofs_inode_compact) != 32); 338 BUILD_BUG_ON(sizeof(struct erofs_inode_extended) != 64); 339 BUILD_BUG_ON(sizeof(struct erofs_xattr_ibody_header) != 12); 340 BUILD_BUG_ON(sizeof(struct erofs_xattr_entry) != 4); 341 BUILD_BUG_ON(sizeof(struct z_erofs_map_header) != 8); 342 BUILD_BUG_ON(sizeof(struct z_erofs_vle_decompressed_index) != 8); 343 BUILD_BUG_ON(sizeof(struct erofs_dirent) != 12); 344 345 BUILD_BUG_ON(BIT(Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) < 346 Z_EROFS_VLE_CLUSTER_TYPE_MAX - 1); 347 } 348 349 #endif 350