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 * http://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 /* 15 * Any bits that aren't in EROFS_ALL_FEATURE_INCOMPAT should 16 * be incompatible with this kernel version. 17 */ 18 #define EROFS_FEATURE_INCOMPAT_LZ4_0PADDING 0x00000001 19 #define EROFS_ALL_FEATURE_INCOMPAT EROFS_FEATURE_INCOMPAT_LZ4_0PADDING 20 21 /* 128-byte erofs on-disk super block */ 22 struct erofs_super_block { 23 __le32 magic; /* file system magic number */ 24 __le32 checksum; /* crc32c(super_block) */ 25 __le32 feature_compat; 26 __u8 blkszbits; /* support block_size == PAGE_SIZE only */ 27 __u8 reserved; 28 29 __le16 root_nid; /* nid of root directory */ 30 __le64 inos; /* total valid ino # (== f_files - f_favail) */ 31 32 __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 */ 39 __le32 feature_incompat; 40 41 __u8 reserved2[44]; 42 }; 43 44 /* 45 * erofs inode datalayout (i_format in on-disk inode): 46 * 0 - inode plain without inline data A: 47 * inode, [xattrs], ... | ... | no-holed data 48 * 1 - inode VLE compression B (legacy): 49 * inode, [xattrs], extents ... | ... 50 * 2 - inode plain with inline data C: 51 * inode, [xattrs], last_inline_data, ... | ... | no-holed data 52 * 3 - inode compression D: 53 * inode, [xattrs], map_header, extents ... | ... 54 * 4~7 - reserved 55 */ 56 enum { 57 EROFS_INODE_FLAT_PLAIN = 0, 58 EROFS_INODE_FLAT_COMPRESSION_LEGACY = 1, 59 EROFS_INODE_FLAT_INLINE = 2, 60 EROFS_INODE_FLAT_COMPRESSION = 3, 61 EROFS_INODE_DATALAYOUT_MAX 62 }; 63 64 static inline bool erofs_inode_is_data_compressed(unsigned int datamode) 65 { 66 return datamode == EROFS_INODE_FLAT_COMPRESSION || 67 datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY; 68 } 69 70 /* bit definitions of inode i_advise */ 71 #define EROFS_I_VERSION_BITS 1 72 #define EROFS_I_DATALAYOUT_BITS 3 73 74 #define EROFS_I_VERSION_BIT 0 75 #define EROFS_I_DATALAYOUT_BIT 1 76 77 /* 32-byte reduced form of an ondisk inode */ 78 struct erofs_inode_compact { 79 __le16 i_format; /* inode format hints */ 80 81 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */ 82 __le16 i_xattr_icount; 83 __le16 i_mode; 84 __le16 i_nlink; 85 __le32 i_size; 86 __le32 i_reserved; 87 union { 88 /* 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; 94 } i_u; 95 __le32 i_ino; /* only used for 32-bit stat compatibility */ 96 __le16 i_uid; 97 __le16 i_gid; 98 __le32 i_reserved2; 99 }; 100 101 /* 32 bytes on-disk inode */ 102 #define EROFS_INODE_LAYOUT_COMPACT 0 103 /* 64 bytes on-disk inode */ 104 #define EROFS_INODE_LAYOUT_EXTENDED 1 105 106 /* 64-byte complete form of an ondisk inode */ 107 struct erofs_inode_extended { 108 __le16 i_format; /* inode format hints */ 109 110 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */ 111 __le16 i_xattr_icount; 112 __le16 i_mode; 113 __le16 i_reserved; 114 __le64 i_size; 115 union { 116 /* 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; 122 } i_u; 123 124 /* only used for 32-bit stat compatibility */ 125 __le32 i_ino; 126 127 __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]; 133 }; 134 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 */ 150 struct erofs_xattr_ibody_header { 151 __le32 h_reserved; 152 __u8 h_shared_count; 153 __u8 h_reserved2[7]; 154 __le32 h_shared_xattrs[0]; /* shared xattr id array */ 155 }; 156 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) */ 166 struct 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 */ 172 }; 173 174 static 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 } 182 183 #define EROFS_XATTR_ALIGN(size) round_up(size, sizeof(struct erofs_xattr_entry)) 184 185 static 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 } 190 191 /* available compression algorithm types (for h_algorithmtype) */ 192 enum { 193 Z_EROFS_COMPRESSION_LZ4 = 0, 194 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 206 struct 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 224 225 /* 226 * Fixed-sized output compression ondisk Logical Extent cluster type: 227 * 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 */ 250 enum { 251 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, 255 Z_EROFS_VLE_CLUSTER_TYPE_MAX 256 }; 257 258 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS 2 259 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT 0 260 261 struct 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]; 276 } di_u; 277 }; 278 279 #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) 282 283 /* dirent sorts in alphabet order, thus we can do binary search */ 284 struct erofs_dirent { 285 __le64 nid; /* node number */ 286 __le16 nameoff; /* start offset of file name */ 287 __u8 file_type; /* file type */ 288 __u8 reserved; /* reserved */ 289 } __packed; 290 291 /* 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 */ 296 297 #define EROFS_NAME_LEN 255 298 299 /* check the EROFS on-disk layout strictly at compile time */ 300 static inline void erofs_check_ondisk_layout_definitions(void) 301 { 302 BUILD_BUG_ON(sizeof(struct erofs_super_block) != 128); 303 BUILD_BUG_ON(sizeof(struct erofs_inode_compact) != 32); 304 BUILD_BUG_ON(sizeof(struct erofs_inode_extended) != 64); 305 BUILD_BUG_ON(sizeof(struct erofs_xattr_ibody_header) != 12); 306 BUILD_BUG_ON(sizeof(struct erofs_xattr_entry) != 4); 307 BUILD_BUG_ON(sizeof(struct z_erofs_map_header) != 8); 308 BUILD_BUG_ON(sizeof(struct z_erofs_vle_decompressed_index) != 8); 309 BUILD_BUG_ON(sizeof(struct erofs_dirent) != 12); 310 311 BUILD_BUG_ON(BIT(Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) < 312 Z_EROFS_VLE_CLUSTER_TYPE_MAX - 1); 313 } 314 315 #endif 316 317