Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only OR Apache-2.0 */
0002 /*
0003  * EROFS (Enhanced ROM File System) on-disk format definition
0004  *
0005  * Copyright (C) 2017-2018 HUAWEI, Inc.
0006  *             https://www.huawei.com/
0007  * Copyright (C) 2021, Alibaba Cloud
0008  */
0009 #ifndef __EROFS_FS_H
0010 #define __EROFS_FS_H
0011 
0012 #define EROFS_SUPER_OFFSET      1024
0013 
0014 #define EROFS_FEATURE_COMPAT_SB_CHKSUM          0x00000001
0015 #define EROFS_FEATURE_COMPAT_MTIME              0x00000002
0016 
0017 /*
0018  * Any bits that aren't in EROFS_ALL_FEATURE_INCOMPAT should
0019  * be incompatible with this kernel version.
0020  */
0021 #define EROFS_FEATURE_INCOMPAT_ZERO_PADDING 0x00000001
0022 #define EROFS_FEATURE_INCOMPAT_COMPR_CFGS   0x00000002
0023 #define EROFS_FEATURE_INCOMPAT_BIG_PCLUSTER 0x00000002
0024 #define EROFS_FEATURE_INCOMPAT_CHUNKED_FILE 0x00000004
0025 #define EROFS_FEATURE_INCOMPAT_DEVICE_TABLE 0x00000008
0026 #define EROFS_FEATURE_INCOMPAT_COMPR_HEAD2  0x00000008
0027 #define EROFS_FEATURE_INCOMPAT_ZTAILPACKING 0x00000010
0028 #define EROFS_ALL_FEATURE_INCOMPAT      \
0029     (EROFS_FEATURE_INCOMPAT_ZERO_PADDING | \
0030      EROFS_FEATURE_INCOMPAT_COMPR_CFGS | \
0031      EROFS_FEATURE_INCOMPAT_BIG_PCLUSTER | \
0032      EROFS_FEATURE_INCOMPAT_CHUNKED_FILE | \
0033      EROFS_FEATURE_INCOMPAT_DEVICE_TABLE | \
0034      EROFS_FEATURE_INCOMPAT_COMPR_HEAD2 | \
0035      EROFS_FEATURE_INCOMPAT_ZTAILPACKING)
0036 
0037 #define EROFS_SB_EXTSLOT_SIZE   16
0038 
0039 struct erofs_deviceslot {
0040     u8 tag[64];     /* digest(sha256), etc. */
0041     __le32 blocks;      /* total fs blocks of this device */
0042     __le32 mapped_blkaddr;  /* map starting at mapped_blkaddr */
0043     u8 reserved[56];
0044 };
0045 #define EROFS_DEVT_SLOT_SIZE    sizeof(struct erofs_deviceslot)
0046 
0047 /* erofs on-disk super block (currently 128 bytes) */
0048 struct erofs_super_block {
0049     __le32 magic;           /* file system magic number */
0050     __le32 checksum;        /* crc32c(super_block) */
0051     __le32 feature_compat;
0052     __u8 blkszbits;         /* support block_size == PAGE_SIZE only */
0053     __u8 sb_extslots;   /* superblock size = 128 + sb_extslots * 16 */
0054 
0055     __le16 root_nid;    /* nid of root directory */
0056     __le64 inos;            /* total valid ino # (== f_files - f_favail) */
0057 
0058     __le64 build_time;      /* compact inode time derivation */
0059     __le32 build_time_nsec; /* compact inode time derivation in ns scale */
0060     __le32 blocks;          /* used for statfs */
0061     __le32 meta_blkaddr;    /* start block address of metadata area */
0062     __le32 xattr_blkaddr;   /* start block address of shared xattr area */
0063     __u8 uuid[16];          /* 128-bit uuid for volume */
0064     __u8 volume_name[16];   /* volume name */
0065     __le32 feature_incompat;
0066     union {
0067         /* bitmap for available compression algorithms */
0068         __le16 available_compr_algs;
0069         /* customized sliding window size instead of 64k by default */
0070         __le16 lz4_max_distance;
0071     } __packed u1;
0072     __le16 extra_devices;   /* # of devices besides the primary device */
0073     __le16 devt_slotoff;    /* startoff = devt_slotoff * devt_slotsize */
0074     __u8 reserved2[38];
0075 };
0076 
0077 /*
0078  * erofs inode datalayout (i_format in on-disk inode):
0079  * 0 - uncompressed flat inode without tail-packing inline data:
0080  * inode, [xattrs], ... | ... | no-holed data
0081  * 1 - compressed inode with non-compact indexes:
0082  * inode, [xattrs], [map_header], extents ... | ...
0083  * 2 - uncompressed flat inode with tail-packing inline data:
0084  * inode, [xattrs], tailpacking data, ... | ... | no-holed data
0085  * 3 - compressed inode with compact indexes:
0086  * inode, [xattrs], map_header, extents ... | ...
0087  * 4 - chunk-based inode with (optional) multi-device support:
0088  * inode, [xattrs], chunk indexes ... | ...
0089  * 5~7 - reserved
0090  */
0091 enum {
0092     EROFS_INODE_FLAT_PLAIN          = 0,
0093     EROFS_INODE_FLAT_COMPRESSION_LEGACY = 1,
0094     EROFS_INODE_FLAT_INLINE         = 2,
0095     EROFS_INODE_FLAT_COMPRESSION        = 3,
0096     EROFS_INODE_CHUNK_BASED         = 4,
0097     EROFS_INODE_DATALAYOUT_MAX
0098 };
0099 
0100 static inline bool erofs_inode_is_data_compressed(unsigned int datamode)
0101 {
0102     return datamode == EROFS_INODE_FLAT_COMPRESSION ||
0103         datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY;
0104 }
0105 
0106 /* bit definitions of inode i_format */
0107 #define EROFS_I_VERSION_BITS            1
0108 #define EROFS_I_DATALAYOUT_BITS         3
0109 
0110 #define EROFS_I_VERSION_BIT             0
0111 #define EROFS_I_DATALAYOUT_BIT          1
0112 
0113 #define EROFS_I_ALL \
0114     ((1 << (EROFS_I_DATALAYOUT_BIT + EROFS_I_DATALAYOUT_BITS)) - 1)
0115 
0116 /* indicate chunk blkbits, thus 'chunksize = blocksize << chunk blkbits' */
0117 #define EROFS_CHUNK_FORMAT_BLKBITS_MASK     0x001F
0118 /* with chunk indexes or just a 4-byte blkaddr array */
0119 #define EROFS_CHUNK_FORMAT_INDEXES      0x0020
0120 
0121 #define EROFS_CHUNK_FORMAT_ALL  \
0122     (EROFS_CHUNK_FORMAT_BLKBITS_MASK | EROFS_CHUNK_FORMAT_INDEXES)
0123 
0124 struct erofs_inode_chunk_info {
0125     __le16 format;      /* chunk blkbits, etc. */
0126     __le16 reserved;
0127 };
0128 
0129 /* 32-byte reduced form of an ondisk inode */
0130 struct erofs_inode_compact {
0131     __le16 i_format;    /* inode format hints */
0132 
0133 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
0134     __le16 i_xattr_icount;
0135     __le16 i_mode;
0136     __le16 i_nlink;
0137     __le32 i_size;
0138     __le32 i_reserved;
0139     union {
0140         /* total compressed blocks for compressed inodes */
0141         __le32 compressed_blocks;
0142         /* block address for uncompressed flat inodes */
0143         __le32 raw_blkaddr;
0144 
0145         /* for device files, used to indicate old/new device # */
0146         __le32 rdev;
0147 
0148         /* for chunk-based files, it contains the summary info */
0149         struct erofs_inode_chunk_info c;
0150     } i_u;
0151     __le32 i_ino;           /* only used for 32-bit stat compatibility */
0152     __le16 i_uid;
0153     __le16 i_gid;
0154     __le32 i_reserved2;
0155 };
0156 
0157 /* 32-byte on-disk inode */
0158 #define EROFS_INODE_LAYOUT_COMPACT  0
0159 /* 64-byte on-disk inode */
0160 #define EROFS_INODE_LAYOUT_EXTENDED 1
0161 
0162 /* 64-byte complete form of an ondisk inode */
0163 struct erofs_inode_extended {
0164     __le16 i_format;    /* inode format hints */
0165 
0166 /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
0167     __le16 i_xattr_icount;
0168     __le16 i_mode;
0169     __le16 i_reserved;
0170     __le64 i_size;
0171     union {
0172         /* total compressed blocks for compressed inodes */
0173         __le32 compressed_blocks;
0174         /* block address for uncompressed flat inodes */
0175         __le32 raw_blkaddr;
0176 
0177         /* for device files, used to indicate old/new device # */
0178         __le32 rdev;
0179 
0180         /* for chunk-based files, it contains the summary info */
0181         struct erofs_inode_chunk_info c;
0182     } i_u;
0183 
0184     /* only used for 32-bit stat compatibility */
0185     __le32 i_ino;
0186 
0187     __le32 i_uid;
0188     __le32 i_gid;
0189     __le64 i_mtime;
0190     __le32 i_mtime_nsec;
0191     __le32 i_nlink;
0192     __u8   i_reserved2[16];
0193 };
0194 
0195 #define EROFS_MAX_SHARED_XATTRS         (128)
0196 /* h_shared_count between 129 ... 255 are special # */
0197 #define EROFS_SHARED_XATTR_EXTENT       (255)
0198 
0199 /*
0200  * inline xattrs (n == i_xattr_icount):
0201  * erofs_xattr_ibody_header(1) + (n - 1) * 4 bytes
0202  *          12 bytes           /                   \
0203  *                            /                     \
0204  *                           /-----------------------\
0205  *                           |  erofs_xattr_entries+ |
0206  *                           +-----------------------+
0207  * inline xattrs must starts in erofs_xattr_ibody_header,
0208  * for read-only fs, no need to introduce h_refcount
0209  */
0210 struct erofs_xattr_ibody_header {
0211     __le32 h_reserved;
0212     __u8   h_shared_count;
0213     __u8   h_reserved2[7];
0214     __le32 h_shared_xattrs[];       /* shared xattr id array */
0215 };
0216 
0217 /* Name indexes */
0218 #define EROFS_XATTR_INDEX_USER              1
0219 #define EROFS_XATTR_INDEX_POSIX_ACL_ACCESS  2
0220 #define EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT 3
0221 #define EROFS_XATTR_INDEX_TRUSTED           4
0222 #define EROFS_XATTR_INDEX_LUSTRE            5
0223 #define EROFS_XATTR_INDEX_SECURITY          6
0224 
0225 /* xattr entry (for both inline & shared xattrs) */
0226 struct erofs_xattr_entry {
0227     __u8   e_name_len;      /* length of name */
0228     __u8   e_name_index;    /* attribute name index */
0229     __le16 e_value_size;    /* size of attribute value */
0230     /* followed by e_name and e_value */
0231     char   e_name[];        /* attribute name */
0232 };
0233 
0234 static inline unsigned int erofs_xattr_ibody_size(__le16 i_xattr_icount)
0235 {
0236     if (!i_xattr_icount)
0237         return 0;
0238 
0239     return sizeof(struct erofs_xattr_ibody_header) +
0240         sizeof(__u32) * (le16_to_cpu(i_xattr_icount) - 1);
0241 }
0242 
0243 #define EROFS_XATTR_ALIGN(size) round_up(size, sizeof(struct erofs_xattr_entry))
0244 
0245 static inline unsigned int erofs_xattr_entry_size(struct erofs_xattr_entry *e)
0246 {
0247     return EROFS_XATTR_ALIGN(sizeof(struct erofs_xattr_entry) +
0248                  e->e_name_len + le16_to_cpu(e->e_value_size));
0249 }
0250 
0251 /* represent a zeroed chunk (hole) */
0252 #define EROFS_NULL_ADDR         -1
0253 
0254 /* 4-byte block address array */
0255 #define EROFS_BLOCK_MAP_ENTRY_SIZE  sizeof(__le32)
0256 
0257 /* 8-byte inode chunk indexes */
0258 struct erofs_inode_chunk_index {
0259     __le16 advise;      /* always 0, don't care for now */
0260     __le16 device_id;   /* back-end storage id (with bits masked) */
0261     __le32 blkaddr;     /* start block address of this inode chunk */
0262 };
0263 
0264 /* maximum supported size of a physical compression cluster */
0265 #define Z_EROFS_PCLUSTER_MAX_SIZE   (1024 * 1024)
0266 
0267 /* available compression algorithm types (for h_algorithmtype) */
0268 enum {
0269     Z_EROFS_COMPRESSION_LZ4     = 0,
0270     Z_EROFS_COMPRESSION_LZMA    = 1,
0271     Z_EROFS_COMPRESSION_MAX
0272 };
0273 #define Z_EROFS_ALL_COMPR_ALGS      ((1 << Z_EROFS_COMPRESSION_MAX) - 1)
0274 
0275 /* 14 bytes (+ length field = 16 bytes) */
0276 struct z_erofs_lz4_cfgs {
0277     __le16 max_distance;
0278     __le16 max_pclusterblks;
0279     u8 reserved[10];
0280 } __packed;
0281 
0282 /* 14 bytes (+ length field = 16 bytes) */
0283 struct z_erofs_lzma_cfgs {
0284     __le32 dict_size;
0285     __le16 format;
0286     u8 reserved[8];
0287 } __packed;
0288 
0289 #define Z_EROFS_LZMA_MAX_DICT_SIZE  (8 * Z_EROFS_PCLUSTER_MAX_SIZE)
0290 
0291 /*
0292  * bit 0 : COMPACTED_2B indexes (0 - off; 1 - on)
0293  *  e.g. for 4k logical cluster size,      4B        if compacted 2B is off;
0294  *                                  (4B) + 2B + (4B) if compacted 2B is on.
0295  * bit 1 : HEAD1 big pcluster (0 - off; 1 - on)
0296  * bit 2 : HEAD2 big pcluster (0 - off; 1 - on)
0297  * bit 3 : tailpacking inline pcluster (0 - off; 1 - on)
0298  */
0299 #define Z_EROFS_ADVISE_COMPACTED_2B     0x0001
0300 #define Z_EROFS_ADVISE_BIG_PCLUSTER_1       0x0002
0301 #define Z_EROFS_ADVISE_BIG_PCLUSTER_2       0x0004
0302 #define Z_EROFS_ADVISE_INLINE_PCLUSTER      0x0008
0303 
0304 struct z_erofs_map_header {
0305     __le16  h_reserved1;
0306     /* indicates the encoded size of tailpacking data */
0307     __le16  h_idata_size;
0308     __le16  h_advise;
0309     /*
0310      * bit 0-3 : algorithm type of head 1 (logical cluster type 01);
0311      * bit 4-7 : algorithm type of head 2 (logical cluster type 11).
0312      */
0313     __u8    h_algorithmtype;
0314     /*
0315      * bit 0-2 : logical cluster bits - 12, e.g. 0 for 4096;
0316      * bit 3-7 : reserved.
0317      */
0318     __u8    h_clusterbits;
0319 };
0320 
0321 #define Z_EROFS_VLE_LEGACY_HEADER_PADDING       8
0322 
0323 /*
0324  * Fixed-sized output compression on-disk logical cluster type:
0325  *    0   - literal (uncompressed) lcluster
0326  *    1,3 - compressed lcluster (for HEAD lclusters)
0327  *    2   - compressed lcluster (for NONHEAD lclusters)
0328  *
0329  * In detail,
0330  *    0 - literal (uncompressed) lcluster,
0331  *        di_advise = 0
0332  *        di_clusterofs = the literal data offset of the lcluster
0333  *        di_blkaddr = the blkaddr of the literal pcluster
0334  *
0335  *    1,3 - compressed lcluster (for HEAD lclusters)
0336  *        di_advise = 1 or 3
0337  *        di_clusterofs = the decompressed data offset of the lcluster
0338  *        di_blkaddr = the blkaddr of the compressed pcluster
0339  *
0340  *    2 - compressed lcluster (for NONHEAD lclusters)
0341  *        di_advise = 2
0342  *        di_clusterofs =
0343  *           the decompressed data offset in its own HEAD lcluster
0344  *        di_u.delta[0] = distance to this HEAD lcluster
0345  *        di_u.delta[1] = distance to the next HEAD lcluster
0346  */
0347 enum {
0348     Z_EROFS_VLE_CLUSTER_TYPE_PLAIN      = 0,
0349     Z_EROFS_VLE_CLUSTER_TYPE_HEAD1      = 1,
0350     Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD    = 2,
0351     Z_EROFS_VLE_CLUSTER_TYPE_HEAD2      = 3,
0352     Z_EROFS_VLE_CLUSTER_TYPE_MAX
0353 };
0354 
0355 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS        2
0356 #define Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT         0
0357 
0358 /*
0359  * D0_CBLKCNT will be marked _only_ at the 1st non-head lcluster to store the
0360  * compressed block count of a compressed extent (in logical clusters, aka.
0361  * block count of a pcluster).
0362  */
0363 #define Z_EROFS_VLE_DI_D0_CBLKCNT       (1 << 11)
0364 
0365 struct z_erofs_vle_decompressed_index {
0366     __le16 di_advise;
0367     /* where to decompress in the head lcluster */
0368     __le16 di_clusterofs;
0369 
0370     union {
0371         /* for the HEAD lclusters */
0372         __le32 blkaddr;
0373         /*
0374          * for the NONHEAD lclusters
0375          * [0] - distance to its HEAD lcluster
0376          * [1] - distance to the next HEAD lcluster
0377          */
0378         __le16 delta[2];
0379     } di_u;
0380 };
0381 
0382 #define Z_EROFS_VLE_LEGACY_INDEX_ALIGN(size) \
0383     (round_up(size, sizeof(struct z_erofs_vle_decompressed_index)) + \
0384      sizeof(struct z_erofs_map_header) + Z_EROFS_VLE_LEGACY_HEADER_PADDING)
0385 
0386 /* dirent sorts in alphabet order, thus we can do binary search */
0387 struct erofs_dirent {
0388     __le64 nid;     /* node number */
0389     __le16 nameoff; /* start offset of file name */
0390     __u8 file_type; /* file type */
0391     __u8 reserved;  /* reserved */
0392 } __packed;
0393 
0394 /*
0395  * EROFS file types should match generic FT_* types and
0396  * it seems no need to add BUILD_BUG_ONs since potential
0397  * unmatchness will break other fses as well...
0398  */
0399 
0400 #define EROFS_NAME_LEN      255
0401 
0402 /* check the EROFS on-disk layout strictly at compile time */
0403 static inline void erofs_check_ondisk_layout_definitions(void)
0404 {
0405     BUILD_BUG_ON(sizeof(struct erofs_super_block) != 128);
0406     BUILD_BUG_ON(sizeof(struct erofs_inode_compact) != 32);
0407     BUILD_BUG_ON(sizeof(struct erofs_inode_extended) != 64);
0408     BUILD_BUG_ON(sizeof(struct erofs_xattr_ibody_header) != 12);
0409     BUILD_BUG_ON(sizeof(struct erofs_xattr_entry) != 4);
0410     BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_info) != 4);
0411     BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_index) != 8);
0412     BUILD_BUG_ON(sizeof(struct z_erofs_map_header) != 8);
0413     BUILD_BUG_ON(sizeof(struct z_erofs_vle_decompressed_index) != 8);
0414     BUILD_BUG_ON(sizeof(struct erofs_dirent) != 12);
0415     /* keep in sync between 2 index structures for better extendibility */
0416     BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_index) !=
0417              sizeof(struct z_erofs_vle_decompressed_index));
0418     BUILD_BUG_ON(sizeof(struct erofs_deviceslot) != 128);
0419 
0420     BUILD_BUG_ON(BIT(Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) <
0421              Z_EROFS_VLE_CLUSTER_TYPE_MAX - 1);
0422 }
0423 
0424 #endif