erofs_fs.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. * Copyright (C) 2021, Alibaba Cloud
  8. */
  9. #ifndef __EROFS_FS_H
  10. #define __EROFS_FS_H
  11. #define EROFS_SUPER_OFFSET 1024
  12. #define EROFS_FEATURE_COMPAT_SB_CHKSUM 0x00000001
  13. #define EROFS_FEATURE_COMPAT_MTIME 0x00000002
  14. #define EROFS_FEATURE_COMPAT_XATTR_FILTER 0x00000004
  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_ZERO_PADDING 0x00000001
  20. #define EROFS_FEATURE_INCOMPAT_COMPR_CFGS 0x00000002
  21. #define EROFS_FEATURE_INCOMPAT_BIG_PCLUSTER 0x00000002
  22. #define EROFS_FEATURE_INCOMPAT_CHUNKED_FILE 0x00000004
  23. #define EROFS_FEATURE_INCOMPAT_DEVICE_TABLE 0x00000008
  24. #define EROFS_FEATURE_INCOMPAT_COMPR_HEAD2 0x00000008
  25. #define EROFS_FEATURE_INCOMPAT_ZTAILPACKING 0x00000010
  26. #define EROFS_FEATURE_INCOMPAT_FRAGMENTS 0x00000020
  27. #define EROFS_FEATURE_INCOMPAT_DEDUPE 0x00000020
  28. #define EROFS_FEATURE_INCOMPAT_XATTR_PREFIXES 0x00000040
  29. #define EROFS_ALL_FEATURE_INCOMPAT \
  30. (EROFS_FEATURE_INCOMPAT_ZERO_PADDING | \
  31. EROFS_FEATURE_INCOMPAT_COMPR_CFGS | \
  32. EROFS_FEATURE_INCOMPAT_BIG_PCLUSTER | \
  33. EROFS_FEATURE_INCOMPAT_CHUNKED_FILE | \
  34. EROFS_FEATURE_INCOMPAT_DEVICE_TABLE | \
  35. EROFS_FEATURE_INCOMPAT_COMPR_HEAD2 | \
  36. EROFS_FEATURE_INCOMPAT_ZTAILPACKING | \
  37. EROFS_FEATURE_INCOMPAT_FRAGMENTS | \
  38. EROFS_FEATURE_INCOMPAT_DEDUPE | \
  39. EROFS_FEATURE_INCOMPAT_XATTR_PREFIXES)
  40. #define EROFS_SB_EXTSLOT_SIZE 16
  41. struct erofs_deviceslot {
  42. u8 tag[64]; /* digest(sha256), etc. */
  43. __le32 blocks; /* total fs blocks of this device */
  44. __le32 mapped_blkaddr; /* map starting at mapped_blkaddr */
  45. u8 reserved[56];
  46. };
  47. #define EROFS_DEVT_SLOT_SIZE sizeof(struct erofs_deviceslot)
  48. /* erofs on-disk super block (currently 128 bytes) */
  49. struct erofs_super_block {
  50. __le32 magic; /* file system magic number */
  51. __le32 checksum; /* crc32c(super_block) */
  52. __le32 feature_compat;
  53. __u8 blkszbits; /* filesystem block size in bit shift */
  54. __u8 sb_extslots; /* superblock size = 128 + sb_extslots * 16 */
  55. __le16 root_nid; /* nid of root directory */
  56. __le64 inos; /* total valid ino # (== f_files - f_favail) */
  57. __le64 build_time; /* compact inode time derivation */
  58. __le32 build_time_nsec; /* compact inode time derivation in ns scale */
  59. __le32 blocks; /* used for statfs */
  60. __le32 meta_blkaddr; /* start block address of metadata area */
  61. __le32 xattr_blkaddr; /* start block address of shared xattr area */
  62. __u8 uuid[16]; /* 128-bit uuid for volume */
  63. __u8 volume_name[16]; /* volume name */
  64. __le32 feature_incompat;
  65. union {
  66. /* bitmap for available compression algorithms */
  67. __le16 available_compr_algs;
  68. /* customized sliding window size instead of 64k by default */
  69. __le16 lz4_max_distance;
  70. } __packed u1;
  71. __le16 extra_devices; /* # of devices besides the primary device */
  72. __le16 devt_slotoff; /* startoff = devt_slotoff * devt_slotsize */
  73. __u8 dirblkbits; /* directory block size in bit shift */
  74. __u8 xattr_prefix_count; /* # of long xattr name prefixes */
  75. __le32 xattr_prefix_start; /* start of long xattr prefixes */
  76. __le64 packed_nid; /* nid of the special packed inode */
  77. __u8 xattr_filter_reserved; /* reserved for xattr name filter */
  78. __u8 reserved2[23];
  79. };
  80. /*
  81. * EROFS inode datalayout (i_format in on-disk inode):
  82. * 0 - uncompressed flat inode without tail-packing inline data:
  83. * 1 - compressed inode with non-compact indexes:
  84. * 2 - uncompressed flat inode with tail-packing inline data:
  85. * 3 - compressed inode with compact indexes:
  86. * 4 - chunk-based inode with (optional) multi-device support:
  87. * 5~7 - reserved
  88. */
  89. enum {
  90. EROFS_INODE_FLAT_PLAIN = 0,
  91. EROFS_INODE_COMPRESSED_FULL = 1,
  92. EROFS_INODE_FLAT_INLINE = 2,
  93. EROFS_INODE_COMPRESSED_COMPACT = 3,
  94. EROFS_INODE_CHUNK_BASED = 4,
  95. EROFS_INODE_DATALAYOUT_MAX
  96. };
  97. static inline bool erofs_inode_is_data_compressed(unsigned int datamode)
  98. {
  99. return datamode == EROFS_INODE_COMPRESSED_COMPACT ||
  100. datamode == EROFS_INODE_COMPRESSED_FULL;
  101. }
  102. /* bit definitions of inode i_format */
  103. #define EROFS_I_VERSION_MASK 0x01
  104. #define EROFS_I_DATALAYOUT_MASK 0x07
  105. #define EROFS_I_VERSION_BIT 0
  106. #define EROFS_I_DATALAYOUT_BIT 1
  107. #define EROFS_I_ALL_BIT 4
  108. #define EROFS_I_ALL ((1 << EROFS_I_ALL_BIT) - 1)
  109. /* indicate chunk blkbits, thus 'chunksize = blocksize << chunk blkbits' */
  110. #define EROFS_CHUNK_FORMAT_BLKBITS_MASK 0x001F
  111. /* with chunk indexes or just a 4-byte blkaddr array */
  112. #define EROFS_CHUNK_FORMAT_INDEXES 0x0020
  113. #define EROFS_CHUNK_FORMAT_ALL \
  114. (EROFS_CHUNK_FORMAT_BLKBITS_MASK | EROFS_CHUNK_FORMAT_INDEXES)
  115. /* 32-byte on-disk inode */
  116. #define EROFS_INODE_LAYOUT_COMPACT 0
  117. /* 64-byte on-disk inode */
  118. #define EROFS_INODE_LAYOUT_EXTENDED 1
  119. struct erofs_inode_chunk_info {
  120. __le16 format; /* chunk blkbits, etc. */
  121. __le16 reserved;
  122. };
  123. union erofs_inode_i_u {
  124. /* total compressed blocks for compressed inodes */
  125. __le32 compressed_blocks;
  126. /* block address for uncompressed flat inodes */
  127. __le32 raw_blkaddr;
  128. /* for device files, used to indicate old/new device # */
  129. __le32 rdev;
  130. /* for chunk-based files, it contains the summary info */
  131. struct erofs_inode_chunk_info c;
  132. };
  133. /* 32-byte reduced form of an ondisk inode */
  134. struct erofs_inode_compact {
  135. __le16 i_format; /* inode format hints */
  136. /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
  137. __le16 i_xattr_icount;
  138. __le16 i_mode;
  139. __le16 i_nlink;
  140. __le32 i_size;
  141. __le32 i_reserved;
  142. union erofs_inode_i_u i_u;
  143. __le32 i_ino; /* only used for 32-bit stat compatibility */
  144. __le16 i_uid;
  145. __le16 i_gid;
  146. __le32 i_reserved2;
  147. };
  148. /* 64-byte complete form of an ondisk inode */
  149. struct erofs_inode_extended {
  150. __le16 i_format; /* inode format hints */
  151. /* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
  152. __le16 i_xattr_icount;
  153. __le16 i_mode;
  154. __le16 i_reserved;
  155. __le64 i_size;
  156. union erofs_inode_i_u i_u;
  157. __le32 i_ino; /* only used for 32-bit stat compatibility */
  158. __le32 i_uid;
  159. __le32 i_gid;
  160. __le64 i_mtime;
  161. __le32 i_mtime_nsec;
  162. __le32 i_nlink;
  163. __u8 i_reserved2[16];
  164. };
  165. /*
  166. * inline xattrs (n == i_xattr_icount):
  167. * erofs_xattr_ibody_header(1) + (n - 1) * 4 bytes
  168. * 12 bytes / \
  169. * / \
  170. * /-----------------------\
  171. * | erofs_xattr_entries+ |
  172. * +-----------------------+
  173. * inline xattrs must starts in erofs_xattr_ibody_header,
  174. * for read-only fs, no need to introduce h_refcount
  175. */
  176. struct erofs_xattr_ibody_header {
  177. __le32 h_name_filter; /* bit value 1 indicates not-present */
  178. __u8 h_shared_count;
  179. __u8 h_reserved2[7];
  180. __le32 h_shared_xattrs[]; /* shared xattr id array */
  181. };
  182. /* Name indexes */
  183. #define EROFS_XATTR_INDEX_USER 1
  184. #define EROFS_XATTR_INDEX_POSIX_ACL_ACCESS 2
  185. #define EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT 3
  186. #define EROFS_XATTR_INDEX_TRUSTED 4
  187. #define EROFS_XATTR_INDEX_LUSTRE 5
  188. #define EROFS_XATTR_INDEX_SECURITY 6
  189. /*
  190. * bit 7 of e_name_index is set when it refers to a long xattr name prefix,
  191. * while the remained lower bits represent the index of the prefix.
  192. */
  193. #define EROFS_XATTR_LONG_PREFIX 0x80
  194. #define EROFS_XATTR_LONG_PREFIX_MASK 0x7f
  195. #define EROFS_XATTR_FILTER_BITS 32
  196. #define EROFS_XATTR_FILTER_DEFAULT UINT32_MAX
  197. #define EROFS_XATTR_FILTER_SEED 0x25BBE08F
  198. /* xattr entry (for both inline & shared xattrs) */
  199. struct erofs_xattr_entry {
  200. __u8 e_name_len; /* length of name */
  201. __u8 e_name_index; /* attribute name index */
  202. __le16 e_value_size; /* size of attribute value */
  203. /* followed by e_name and e_value */
  204. char e_name[]; /* attribute name */
  205. };
  206. /* long xattr name prefix */
  207. struct erofs_xattr_long_prefix {
  208. __u8 base_index; /* short xattr name prefix index */
  209. char infix[]; /* infix apart from short prefix */
  210. };
  211. static inline unsigned int erofs_xattr_ibody_size(__le16 i_xattr_icount)
  212. {
  213. if (!i_xattr_icount)
  214. return 0;
  215. return sizeof(struct erofs_xattr_ibody_header) +
  216. sizeof(__u32) * (le16_to_cpu(i_xattr_icount) - 1);
  217. }
  218. #define EROFS_XATTR_ALIGN(size) round_up(size, sizeof(struct erofs_xattr_entry))
  219. static inline unsigned int erofs_xattr_entry_size(struct erofs_xattr_entry *e)
  220. {
  221. return EROFS_XATTR_ALIGN(sizeof(struct erofs_xattr_entry) +
  222. e->e_name_len + le16_to_cpu(e->e_value_size));
  223. }
  224. /* represent a zeroed chunk (hole) */
  225. #define EROFS_NULL_ADDR -1
  226. /* 4-byte block address array */
  227. #define EROFS_BLOCK_MAP_ENTRY_SIZE sizeof(__le32)
  228. /* 8-byte inode chunk indexes */
  229. struct erofs_inode_chunk_index {
  230. __le16 advise; /* always 0, don't care for now */
  231. __le16 device_id; /* back-end storage id (with bits masked) */
  232. __le32 blkaddr; /* start block address of this inode chunk */
  233. };
  234. /* dirent sorts in alphabet order, thus we can do binary search */
  235. struct erofs_dirent {
  236. __le64 nid; /* node number */
  237. __le16 nameoff; /* start offset of file name */
  238. __u8 file_type; /* file type */
  239. __u8 reserved; /* reserved */
  240. } __packed;
  241. /*
  242. * EROFS file types should match generic FT_* types and
  243. * it seems no need to add BUILD_BUG_ONs since potential
  244. * unmatchness will break other fses as well...
  245. */
  246. #define EROFS_NAME_LEN 255
  247. /* maximum supported encoded size of a physical compressed cluster */
  248. #define Z_EROFS_PCLUSTER_MAX_SIZE (1024 * 1024)
  249. /* maximum supported decoded size of a physical compressed cluster */
  250. #define Z_EROFS_PCLUSTER_MAX_DSIZE (12 * 1024 * 1024)
  251. /* available compression algorithm types (for h_algorithmtype) */
  252. enum {
  253. Z_EROFS_COMPRESSION_LZ4 = 0,
  254. Z_EROFS_COMPRESSION_LZMA = 1,
  255. Z_EROFS_COMPRESSION_DEFLATE = 2,
  256. Z_EROFS_COMPRESSION_ZSTD = 3,
  257. Z_EROFS_COMPRESSION_MAX
  258. };
  259. #define Z_EROFS_ALL_COMPR_ALGS ((1 << Z_EROFS_COMPRESSION_MAX) - 1)
  260. /* 14 bytes (+ length field = 16 bytes) */
  261. struct z_erofs_lz4_cfgs {
  262. __le16 max_distance;
  263. __le16 max_pclusterblks;
  264. u8 reserved[10];
  265. } __packed;
  266. /* 14 bytes (+ length field = 16 bytes) */
  267. struct z_erofs_lzma_cfgs {
  268. __le32 dict_size;
  269. __le16 format;
  270. u8 reserved[8];
  271. } __packed;
  272. #define Z_EROFS_LZMA_MAX_DICT_SIZE (8 * Z_EROFS_PCLUSTER_MAX_SIZE)
  273. /* 6 bytes (+ length field = 8 bytes) */
  274. struct z_erofs_deflate_cfgs {
  275. u8 windowbits; /* 8..15 for DEFLATE */
  276. u8 reserved[5];
  277. } __packed;
  278. /* 6 bytes (+ length field = 8 bytes) */
  279. struct z_erofs_zstd_cfgs {
  280. u8 format;
  281. u8 windowlog; /* windowLog - ZSTD_WINDOWLOG_ABSOLUTEMIN(10) */
  282. u8 reserved[4];
  283. } __packed;
  284. #define Z_EROFS_ZSTD_MAX_DICT_SIZE Z_EROFS_PCLUSTER_MAX_SIZE
  285. /*
  286. * bit 0 : COMPACTED_2B indexes (0 - off; 1 - on)
  287. * e.g. for 4k logical cluster size, 4B if compacted 2B is off;
  288. * (4B) + 2B + (4B) if compacted 2B is on.
  289. * bit 1 : HEAD1 big pcluster (0 - off; 1 - on)
  290. * bit 2 : HEAD2 big pcluster (0 - off; 1 - on)
  291. * bit 3 : tailpacking inline pcluster (0 - off; 1 - on)
  292. * bit 4 : interlaced plain pcluster (0 - off; 1 - on)
  293. * bit 5 : fragment pcluster (0 - off; 1 - on)
  294. */
  295. #define Z_EROFS_ADVISE_COMPACTED_2B 0x0001
  296. #define Z_EROFS_ADVISE_BIG_PCLUSTER_1 0x0002
  297. #define Z_EROFS_ADVISE_BIG_PCLUSTER_2 0x0004
  298. #define Z_EROFS_ADVISE_INLINE_PCLUSTER 0x0008
  299. #define Z_EROFS_ADVISE_INTERLACED_PCLUSTER 0x0010
  300. #define Z_EROFS_ADVISE_FRAGMENT_PCLUSTER 0x0020
  301. #define Z_EROFS_FRAGMENT_INODE_BIT 7
  302. struct z_erofs_map_header {
  303. union {
  304. /* fragment data offset in the packed inode */
  305. __le32 h_fragmentoff;
  306. struct {
  307. __le16 h_reserved1;
  308. /* indicates the encoded size of tailpacking data */
  309. __le16 h_idata_size;
  310. };
  311. };
  312. __le16 h_advise;
  313. /*
  314. * bit 0-3 : algorithm type of head 1 (logical cluster type 01);
  315. * bit 4-7 : algorithm type of head 2 (logical cluster type 11).
  316. */
  317. __u8 h_algorithmtype;
  318. /*
  319. * bit 0-2 : logical cluster bits - 12, e.g. 0 for 4096;
  320. * bit 3-6 : reserved;
  321. * bit 7 : move the whole file into packed inode or not.
  322. */
  323. __u8 h_clusterbits;
  324. };
  325. /*
  326. * On-disk logical cluster type:
  327. * 0 - literal (uncompressed) lcluster
  328. * 1,3 - compressed lcluster (for HEAD lclusters)
  329. * 2 - compressed lcluster (for NONHEAD lclusters)
  330. *
  331. * In detail,
  332. * 0 - literal (uncompressed) lcluster,
  333. * di_advise = 0
  334. * di_clusterofs = the literal data offset of the lcluster
  335. * di_blkaddr = the blkaddr of the literal pcluster
  336. *
  337. * 1,3 - compressed lcluster (for HEAD lclusters)
  338. * di_advise = 1 or 3
  339. * di_clusterofs = the decompressed data offset of the lcluster
  340. * di_blkaddr = the blkaddr of the compressed pcluster
  341. *
  342. * 2 - compressed lcluster (for NONHEAD lclusters)
  343. * di_advise = 2
  344. * di_clusterofs =
  345. * the decompressed data offset in its own HEAD lcluster
  346. * di_u.delta[0] = distance to this HEAD lcluster
  347. * di_u.delta[1] = distance to the next HEAD lcluster
  348. */
  349. enum {
  350. Z_EROFS_LCLUSTER_TYPE_PLAIN = 0,
  351. Z_EROFS_LCLUSTER_TYPE_HEAD1 = 1,
  352. Z_EROFS_LCLUSTER_TYPE_NONHEAD = 2,
  353. Z_EROFS_LCLUSTER_TYPE_HEAD2 = 3,
  354. Z_EROFS_LCLUSTER_TYPE_MAX
  355. };
  356. #define Z_EROFS_LI_LCLUSTER_TYPE_MASK (Z_EROFS_LCLUSTER_TYPE_MAX - 1)
  357. /* (noncompact only, HEAD) This pcluster refers to partial decompressed data */
  358. #define Z_EROFS_LI_PARTIAL_REF (1 << 15)
  359. /*
  360. * D0_CBLKCNT will be marked _only_ at the 1st non-head lcluster to store the
  361. * compressed block count of a compressed extent (in logical clusters, aka.
  362. * block count of a pcluster).
  363. */
  364. #define Z_EROFS_LI_D0_CBLKCNT (1 << 11)
  365. struct z_erofs_lcluster_index {
  366. __le16 di_advise;
  367. /* where to decompress in the head lcluster */
  368. __le16 di_clusterofs;
  369. union {
  370. /* for the HEAD lclusters */
  371. __le32 blkaddr;
  372. /*
  373. * for the NONHEAD lclusters
  374. * [0] - distance to its HEAD lcluster
  375. * [1] - distance to the next HEAD lcluster
  376. */
  377. __le16 delta[2];
  378. } di_u;
  379. };
  380. #define Z_EROFS_FULL_INDEX_ALIGN(end) \
  381. (ALIGN(end, 8) + sizeof(struct z_erofs_map_header) + 8)
  382. /* check the EROFS on-disk layout strictly at compile time */
  383. static inline void erofs_check_ondisk_layout_definitions(void)
  384. {
  385. const __le64 fmh = *(__le64 *)&(struct z_erofs_map_header) {
  386. .h_clusterbits = 1 << Z_EROFS_FRAGMENT_INODE_BIT
  387. };
  388. BUILD_BUG_ON(sizeof(struct erofs_super_block) != 128);
  389. BUILD_BUG_ON(sizeof(struct erofs_inode_compact) != 32);
  390. BUILD_BUG_ON(sizeof(struct erofs_inode_extended) != 64);
  391. BUILD_BUG_ON(sizeof(struct erofs_xattr_ibody_header) != 12);
  392. BUILD_BUG_ON(sizeof(struct erofs_xattr_entry) != 4);
  393. BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_info) != 4);
  394. BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_index) != 8);
  395. BUILD_BUG_ON(sizeof(struct z_erofs_map_header) != 8);
  396. BUILD_BUG_ON(sizeof(struct z_erofs_lcluster_index) != 8);
  397. BUILD_BUG_ON(sizeof(struct erofs_dirent) != 12);
  398. /* keep in sync between 2 index structures for better extendibility */
  399. BUILD_BUG_ON(sizeof(struct erofs_inode_chunk_index) !=
  400. sizeof(struct z_erofs_lcluster_index));
  401. BUILD_BUG_ON(sizeof(struct erofs_deviceslot) != 128);
  402. /* exclude old compiler versions like gcc 7.5.0 */
  403. BUILD_BUG_ON(__builtin_constant_p(fmh) ?
  404. fmh != cpu_to_le64(1ULL << 63) : 0);
  405. }
  406. #endif