ext4_types.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. /*
  2. * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
  3. *
  4. *
  5. * HelenOS:
  6. * Copyright (c) 2012 Martin Sucha
  7. * Copyright (c) 2012 Frantisek Princ
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * - The name of the author may not be used to endorse or promote products
  20. * derived from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  23. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  24. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  25. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  27. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  31. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. /** @addtogroup lwext4
  34. * @{
  35. */
  36. /**
  37. * @file ext4_types.h
  38. * @brief Ext4 data structure definitions.
  39. */
  40. #ifndef EXT4_TYPES_H_
  41. #define EXT4_TYPES_H_
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. #include <ext4_config.h>
  46. #include <ext4_blockdev.h>
  47. #include <misc/tree.h>
  48. #include <stddef.h>
  49. #include <stdint.h>
  50. /*
  51. * Types of blocks.
  52. */
  53. typedef uint32_t ext4_lblk_t;
  54. typedef uint64_t ext4_fsblk_t;
  55. #define EXT4_CHECKSUM_CRC32C 1
  56. #define UUID_SIZE 16
  57. #pragma pack(push, 1)
  58. /*
  59. * Structure of the super block
  60. */
  61. struct ext4_sblock {
  62. uint32_t inodes_count; /* I-nodes count */
  63. uint32_t blocks_count_lo; /* Blocks count */
  64. uint32_t reserved_blocks_count_lo; /* Reserved blocks count */
  65. uint32_t free_blocks_count_lo; /* Free blocks count */
  66. uint32_t free_inodes_count; /* Free inodes count */
  67. uint32_t first_data_block; /* First Data Block */
  68. uint32_t log_block_size; /* Block size */
  69. uint32_t log_cluster_size; /* Obsoleted fragment size */
  70. uint32_t blocks_per_group; /* Number of blocks per group */
  71. uint32_t frags_per_group; /* Obsoleted fragments per group */
  72. uint32_t inodes_per_group; /* Number of inodes per group */
  73. uint32_t mount_time; /* Mount time */
  74. uint32_t write_time; /* Write time */
  75. uint16_t mount_count; /* Mount count */
  76. uint16_t max_mount_count; /* Maximal mount count */
  77. uint16_t magic; /* Magic signature */
  78. uint16_t state; /* File system state */
  79. uint16_t errors; /* Behavior when detecting errors */
  80. uint16_t minor_rev_level; /* Minor revision level */
  81. uint32_t last_check_time; /* Time of last check */
  82. uint32_t check_interval; /* Maximum time between checks */
  83. uint32_t creator_os; /* Creator OS */
  84. uint32_t rev_level; /* Revision level */
  85. uint16_t def_resuid; /* Default uid for reserved blocks */
  86. uint16_t def_resgid; /* Default gid for reserved blocks */
  87. /* Fields for EXT4_DYNAMIC_REV superblocks only. */
  88. uint32_t first_inode; /* First non-reserved inode */
  89. uint16_t inode_size; /* Size of inode structure */
  90. uint16_t block_group_index; /* Block group index of this superblock */
  91. uint32_t features_compatible; /* Compatible feature set */
  92. uint32_t features_incompatible; /* Incompatible feature set */
  93. uint32_t features_read_only; /* Readonly-compatible feature set */
  94. uint8_t uuid[UUID_SIZE]; /* 128-bit uuid for volume */
  95. char volume_name[16]; /* Volume name */
  96. char last_mounted[64]; /* Directory where last mounted */
  97. uint32_t algorithm_usage_bitmap; /* For compression */
  98. /*
  99. * Performance hints. Directory preallocation should only
  100. * happen if the EXT4_FEATURE_COMPAT_DIR_PREALLOC flag is on.
  101. */
  102. uint8_t s_prealloc_blocks; /* Number of blocks to try to preallocate */
  103. uint8_t s_prealloc_dir_blocks; /* Number to preallocate for dirs */
  104. uint16_t s_reserved_gdt_blocks; /* Per group desc for online growth */
  105. /*
  106. * Journaling support valid if EXT4_FEATURE_COMPAT_HAS_JOURNAL set.
  107. */
  108. uint8_t journal_uuid[UUID_SIZE]; /* UUID of journal superblock */
  109. uint32_t journal_inode_number; /* Inode number of journal file */
  110. uint32_t journal_dev; /* Device number of journal file */
  111. uint32_t last_orphan; /* Head of list of inodes to delete */
  112. uint32_t hash_seed[4]; /* HTREE hash seed */
  113. uint8_t default_hash_version; /* Default hash version to use */
  114. uint8_t journal_backup_type;
  115. uint16_t desc_size; /* Size of group descriptor */
  116. uint32_t default_mount_opts; /* Default mount options */
  117. uint32_t first_meta_bg; /* First metablock block group */
  118. uint32_t mkfs_time; /* When the filesystem was created */
  119. uint32_t journal_blocks[17]; /* Backup of the journal inode */
  120. /* 64bit support valid if EXT4_FEATURE_COMPAT_64BIT */
  121. uint32_t blocks_count_hi; /* Blocks count */
  122. uint32_t reserved_blocks_count_hi; /* Reserved blocks count */
  123. uint32_t free_blocks_count_hi; /* Free blocks count */
  124. uint16_t min_extra_isize; /* All inodes have at least # bytes */
  125. uint16_t want_extra_isize; /* New inodes should reserve # bytes */
  126. uint32_t flags; /* Miscellaneous flags */
  127. uint16_t raid_stride; /* RAID stride */
  128. uint16_t mmp_interval; /* # seconds to wait in MMP checking */
  129. uint64_t mmp_block; /* Block for multi-mount protection */
  130. uint32_t raid_stripe_width; /* Blocks on all data disks (N * stride) */
  131. uint8_t log_groups_per_flex; /* FLEX_BG group size */
  132. uint8_t checksum_type;
  133. uint16_t reserved_pad;
  134. uint64_t kbytes_written; /* Number of lifetime kilobytes written */
  135. uint32_t snapshot_inum; /* I-node number of active snapshot */
  136. uint32_t snapshot_id; /* Sequential ID of active snapshot */
  137. uint64_t
  138. snapshot_r_blocks_count; /* Reserved blocks for active snapshot's
  139. future use */
  140. uint32_t
  141. snapshot_list; /* I-node number of the head of the on-disk snapshot
  142. list */
  143. uint32_t error_count; /* Number of file system errors */
  144. uint32_t first_error_time; /* First time an error happened */
  145. uint32_t first_error_ino; /* I-node involved in first error */
  146. uint64_t first_error_block; /* Block involved of first error */
  147. uint8_t first_error_func[32]; /* Function where the error happened */
  148. uint32_t first_error_line; /* Line number where error happened */
  149. uint32_t last_error_time; /* Most recent time of an error */
  150. uint32_t last_error_ino; /* I-node involved in last error */
  151. uint32_t last_error_line; /* Line number where error happened */
  152. uint64_t last_error_block; /* Block involved of last error */
  153. uint8_t last_error_func[32]; /* Function where the error happened */
  154. uint8_t mount_opts[64];
  155. uint32_t usr_quota_inum; /* inode for tracking user quota */
  156. uint32_t grp_quota_inum; /* inode for tracking group quota */
  157. uint32_t overhead_clusters; /* overhead blocks/clusters in fs */
  158. uint32_t backup_bgs[2]; /* groups with sparse_super2 SBs */
  159. uint8_t encrypt_algos[4]; /* Encryption algorithms in use */
  160. uint8_t encrypt_pw_salt[16]; /* Salt used for string2key algorithm */
  161. uint32_t lpf_ino; /* Location of the lost+found inode */
  162. uint32_t padding[100]; /* Padding to the end of the block */
  163. uint32_t checksum; /* crc32c(superblock) */
  164. };
  165. #pragma pack(pop)
  166. #define EXT4_SUPERBLOCK_MAGIC 0xEF53
  167. #define EXT4_SUPERBLOCK_SIZE 1024
  168. #define EXT4_SUPERBLOCK_OFFSET 1024
  169. #define EXT4_SUPERBLOCK_OS_LINUX 0
  170. #define EXT4_SUPERBLOCK_OS_HURD 1
  171. /*
  172. * Misc. filesystem flags
  173. */
  174. #define EXT4_SUPERBLOCK_FLAGS_SIGNED_HASH 0x0001
  175. #define EXT4_SUPERBLOCK_FLAGS_UNSIGNED_HASH 0x0002
  176. #define EXT4_SUPERBLOCK_FLAGS_TEST_FILESYS 0x0004
  177. /*
  178. * Filesystem states
  179. */
  180. #define EXT4_SUPERBLOCK_STATE_VALID_FS 0x0001 /* Unmounted cleanly */
  181. #define EXT4_SUPERBLOCK_STATE_ERROR_FS 0x0002 /* Errors detected */
  182. #define EXT4_SUPERBLOCK_STATE_ORPHAN_FS 0x0004 /* Orphans being recovered */
  183. /*
  184. * Behaviour when errors detected
  185. */
  186. #define EXT4_SUPERBLOCK_ERRORS_CONTINUE 1 /* Continue execution */
  187. #define EXT4_SUPERBLOCK_ERRORS_RO 2 /* Remount fs read-only */
  188. #define EXT4_SUPERBLOCK_ERRORS_PANIC 3 /* Panic */
  189. #define EXT4_SUPERBLOCK_ERRORS_DEFAULT EXT4_ERRORS_CONTINUE
  190. /*
  191. * Compatible features
  192. */
  193. #define EXT4_FCOM_DIR_PREALLOC 0x0001
  194. #define EXT4_FCOM_IMAGIC_INODES 0x0002
  195. #define EXT4_FCOM_HAS_JOURNAL 0x0004
  196. #define EXT4_FCOM_EXT_ATTR 0x0008
  197. #define EXT4_FCOM_RESIZE_INODE 0x0010
  198. #define EXT4_FCOM_DIR_INDEX 0x0020
  199. /*
  200. * Read-only compatible features
  201. */
  202. #define EXT4_FRO_COM_SPARSE_SUPER 0x0001
  203. #define EXT4_FRO_COM_LARGE_FILE 0x0002
  204. #define EXT4_FRO_COM_BTREE_DIR 0x0004
  205. #define EXT4_FRO_COM_HUGE_FILE 0x0008
  206. #define EXT4_FRO_COM_GDT_CSUM 0x0010
  207. #define EXT4_FRO_COM_DIR_NLINK 0x0020
  208. #define EXT4_FRO_COM_EXTRA_ISIZE 0x0040
  209. #define EXT4_FRO_COM_QUOTA 0x0100
  210. #define EXT4_FRO_COM_BIGALLOC 0x0200
  211. #define EXT4_FRO_COM_METADATA_CSUM 0x0400
  212. /*
  213. * Incompatible features
  214. */
  215. #define EXT4_FINCOM_COMPRESSION 0x0001
  216. #define EXT4_FINCOM_FILETYPE 0x0002
  217. #define EXT4_FINCOM_RECOVER 0x0004 /* Needs recovery */
  218. #define EXT4_FINCOM_JOURNAL_DEV 0x0008 /* Journal device */
  219. #define EXT4_FINCOM_META_BG 0x0010
  220. #define EXT4_FINCOM_EXTENTS 0x0040 /* extents support */
  221. #define EXT4_FINCOM_64BIT 0x0080
  222. #define EXT4_FINCOM_MMP 0x0100
  223. #define EXT4_FINCOM_FLEX_BG 0x0200
  224. #define EXT4_FINCOM_EA_INODE 0x0400 /* EA in inode */
  225. #define EXT4_FINCOM_DIRDATA 0x1000 /* data in dirent */
  226. #define EXT4_FINCOM_BG_USE_META_CSUM 0x2000 /* use crc32c for bg */
  227. #define EXT4_FINCOM_LARGEDIR 0x4000 /* >2GB or 3-lvl htree */
  228. #define EXT4_FINCOM_INLINE_DATA 0x8000 /* data in inode */
  229. /*
  230. * EXT2 supported feature set
  231. */
  232. #define EXT2_SUPPORTED_FCOM 0x0000
  233. #define EXT2_SUPPORTED_FINCOM \
  234. (EXT4_FINCOM_FILETYPE | EXT4_FINCOM_META_BG)
  235. #define EXT2_SUPPORTED_FRO_COM \
  236. (EXT4_FRO_COM_SPARSE_SUPER | \
  237. EXT4_FRO_COM_LARGE_FILE)
  238. /*
  239. * EXT3 supported feature set
  240. */
  241. #define EXT3_SUPPORTED_FCOM (EXT4_FCOM_DIR_INDEX)
  242. #define EXT3_SUPPORTED_FINCOM \
  243. (EXT4_FINCOM_FILETYPE | EXT4_FINCOM_META_BG)
  244. #define EXT3_SUPPORTED_FRO_COM \
  245. (EXT4_FRO_COM_SPARSE_SUPER | EXT4_FRO_COM_LARGE_FILE)
  246. /*
  247. * EXT4 supported feature set
  248. */
  249. #define EXT4_SUPPORTED_FCOM (EXT4_FCOM_DIR_INDEX)
  250. #define EXT4_SUPPORTED_FINCOM \
  251. (EXT4_FINCOM_FILETYPE | EXT4_FINCOM_META_BG | \
  252. EXT4_FINCOM_EXTENTS | EXT4_FINCOM_FLEX_BG | \
  253. EXT4_FINCOM_64BIT)
  254. #define EXT4_SUPPORTED_FRO_COM \
  255. (EXT4_FRO_COM_SPARSE_SUPER | \
  256. EXT4_FRO_COM_METADATA_CSUM | \
  257. EXT4_FRO_COM_LARGE_FILE | EXT4_FRO_COM_GDT_CSUM | \
  258. EXT4_FRO_COM_DIR_NLINK | \
  259. EXT4_FRO_COM_EXTRA_ISIZE | EXT4_FRO_COM_HUGE_FILE)
  260. /*Ignored features:
  261. * RECOVER - journaling in lwext4 is not supported
  262. * (probably won't be ever...)
  263. * MMP - multi-mout protection (impossible scenario)
  264. * */
  265. #define EXT_FINCOM_IGNORED \
  266. EXT4_FINCOM_RECOVER | EXT4_FINCOM_MMP
  267. #if 0
  268. /*TODO: Features incompatible to implement*/
  269. #define EXT4_SUPPORTED_FINCOM
  270. (EXT4_FINCOM_INLINE_DATA)
  271. /*TODO: Features read only to implement*/
  272. #define EXT4_SUPPORTED_FRO_COM
  273. EXT4_FRO_COM_BIGALLOC |\
  274. EXT4_FRO_COM_QUOTA)
  275. #endif
  276. /* Inode table/bitmap not in use */
  277. #define EXT4_BLOCK_GROUP_INODE_UNINIT 0x0001
  278. /* Block bitmap not in use */
  279. #define EXT4_BLOCK_GROUP_BLOCK_UNINIT 0x0002
  280. /* On-disk itable initialized to zero */
  281. #define EXT4_BLOCK_GROUP_ITABLE_ZEROED 0x0004
  282. /*
  283. * Structure of a blocks group descriptor
  284. */
  285. struct ext4_bgroup {
  286. uint32_t block_bitmap_lo; /* Blocks bitmap block */
  287. uint32_t inode_bitmap_lo; /* Inodes bitmap block */
  288. uint32_t inode_table_first_block_lo; /* Inodes table block */
  289. uint16_t free_blocks_count_lo; /* Free blocks count */
  290. uint16_t free_inodes_count_lo; /* Free inodes count */
  291. uint16_t used_dirs_count_lo; /* Directories count */
  292. uint16_t flags; /* EXT4_BG_flags (INODE_UNINIT, etc) */
  293. uint32_t exclude_bitmap_lo; /* Exclude bitmap for snapshots */
  294. uint16_t block_bitmap_csum_lo; /* crc32c(s_uuid+grp_num+bbitmap) LE */
  295. uint16_t inode_bitmap_csum_lo; /* crc32c(s_uuid+grp_num+ibitmap) LE */
  296. uint16_t itable_unused_lo; /* Unused inodes count */
  297. uint16_t checksum; /* crc16(sb_uuid+group+desc) */
  298. uint32_t block_bitmap_hi; /* Blocks bitmap block MSB */
  299. uint32_t inode_bitmap_hi; /* I-nodes bitmap block MSB */
  300. uint32_t inode_table_first_block_hi; /* I-nodes table block MSB */
  301. uint16_t free_blocks_count_hi; /* Free blocks count MSB */
  302. uint16_t free_inodes_count_hi; /* Free i-nodes count MSB */
  303. uint16_t used_dirs_count_hi; /* Directories count MSB */
  304. uint16_t itable_unused_hi; /* Unused inodes count MSB */
  305. uint32_t exclude_bitmap_hi; /* Exclude bitmap block MSB */
  306. uint16_t block_bitmap_csum_hi; /* crc32c(s_uuid+grp_num+bbitmap) BE */
  307. uint16_t inode_bitmap_csum_hi; /* crc32c(s_uuid+grp_num+ibitmap) BE */
  308. uint32_t reserved; /* Padding */
  309. };
  310. #define EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE 32
  311. #define EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE 64
  312. #define EXT4_MIN_BLOCK_SIZE 1024 /* 1 KiB */
  313. #define EXT4_MAX_BLOCK_SIZE 65536 /* 64 KiB */
  314. #define EXT4_REV0_INODE_SIZE 128
  315. #define EXT4_INODE_BLOCK_SIZE 512
  316. #define EXT4_INODE_DIRECT_BLOCK_COUNT 12
  317. #define EXT4_INODE_INDIRECT_BLOCK EXT4_INODE_DIRECT_BLOCK_COUNT
  318. #define EXT4_INODE_DOUBLE_INDIRECT_BLOCK (EXT4_INODE_INDIRECT_BLOCK + 1)
  319. #define EXT4_INODE_TRIPPLE_INDIRECT_BLOCK (EXT4_INODE_DOUBLE_INDIRECT_BLOCK + 1)
  320. #define EXT4_INODE_BLOCKS (EXT4_INODE_TRIPPLE_INDIRECT_BLOCK + 1)
  321. #define EXT4_INODE_INDIRECT_BLOCK_COUNT \
  322. (EXT4_INODE_BLOCKS - EXT4_INODE_DIRECT_BLOCK_COUNT)
  323. #pragma pack(push, 1)
  324. /*
  325. * Structure of an inode on the disk
  326. */
  327. struct ext4_inode {
  328. uint16_t mode; /* File mode */
  329. uint16_t uid; /* Low 16 bits of owner uid */
  330. uint32_t size_lo; /* Size in bytes */
  331. uint32_t access_time; /* Access time */
  332. uint32_t change_inode_time; /* I-node change time */
  333. uint32_t modification_time; /* Modification time */
  334. uint32_t deletion_time; /* Deletion time */
  335. uint16_t gid; /* Low 16 bits of group id */
  336. uint16_t links_count; /* Links count */
  337. uint32_t blocks_count_lo; /* Blocks count */
  338. uint32_t flags; /* File flags */
  339. uint32_t unused_osd1; /* OS dependent - not used in HelenOS */
  340. uint32_t blocks[EXT4_INODE_BLOCKS]; /* Pointers to blocks */
  341. uint32_t generation; /* File version (for NFS) */
  342. uint32_t file_acl_lo; /* File ACL */
  343. uint32_t size_hi;
  344. uint32_t obso_faddr; /* Obsoleted fragment address */
  345. union {
  346. struct {
  347. uint16_t blocks_high;
  348. uint16_t file_acl_high;
  349. uint16_t uid_high;
  350. uint16_t gid_high;
  351. uint16_t checksum_lo; /* crc32c(uuid+inum+inode) LE */
  352. uint16_t reserved2;
  353. } linux2;
  354. struct {
  355. uint16_t reserved1;
  356. uint16_t mode_high;
  357. uint16_t uid_high;
  358. uint16_t gid_high;
  359. uint32_t author;
  360. } hurd2;
  361. } osd2;
  362. uint16_t extra_isize;
  363. uint16_t checksum_hi; /* crc32c(uuid+inum+inode) BE */
  364. uint32_t ctime_extra; /* Extra change time (nsec << 2 | epoch) */
  365. uint32_t mtime_extra; /* Extra Modification time (nsec << 2 | epoch) */
  366. uint32_t atime_extra; /* Extra Access time (nsec << 2 | epoch) */
  367. uint32_t crtime; /* File creation time */
  368. uint32_t
  369. crtime_extra; /* Extra file creation time (nsec << 2 | epoch) */
  370. uint32_t version_hi; /* High 32 bits for 64-bit version */
  371. };
  372. #pragma pack(pop)
  373. #define EXT4_INODE_MODE_FIFO 0x1000
  374. #define EXT4_INODE_MODE_CHARDEV 0x2000
  375. #define EXT4_INODE_MODE_DIRECTORY 0x4000
  376. #define EXT4_INODE_MODE_BLOCKDEV 0x6000
  377. #define EXT4_INODE_MODE_FILE 0x8000
  378. #define EXT4_INODE_MODE_SOFTLINK 0xA000
  379. #define EXT4_INODE_MODE_SOCKET 0xC000
  380. #define EXT4_INODE_MODE_TYPE_MASK 0xF000
  381. /*
  382. * Inode flags
  383. */
  384. #define EXT4_INODE_FLAG_SECRM 0x00000001 /* Secure deletion */
  385. #define EXT4_INODE_FLAG_UNRM 0x00000002 /* Undelete */
  386. #define EXT4_INODE_FLAG_COMPR 0x00000004 /* Compress file */
  387. #define EXT4_INODE_FLAG_SYNC 0x00000008 /* Synchronous updates */
  388. #define EXT4_INODE_FLAG_IMMUTABLE 0x00000010 /* Immutable file */
  389. #define EXT4_INODE_FLAG_APPEND 0x00000020 /* writes to file may only append */
  390. #define EXT4_INODE_FLAG_NODUMP 0x00000040 /* do not dump file */
  391. #define EXT4_INODE_FLAG_NOATIME 0x00000080 /* do not update atime */
  392. /* Compression flags */
  393. #define EXT4_INODE_FLAG_DIRTY 0x00000100
  394. #define EXT4_INODE_FLAG_COMPRBLK \
  395. 0x00000200 /* One or more compressed clusters */
  396. #define EXT4_INODE_FLAG_NOCOMPR 0x00000400 /* Don't compress */
  397. #define EXT4_INODE_FLAG_ECOMPR 0x00000800 /* Compression error */
  398. #define EXT4_INODE_FLAG_INDEX 0x00001000 /* hash-indexed directory */
  399. #define EXT4_INODE_FLAG_IMAGIC 0x00002000 /* AFS directory */
  400. #define EXT4_INODE_FLAG_JOURNAL_DATA \
  401. 0x00004000 /* File data should be journaled */
  402. #define EXT4_INODE_FLAG_NOTAIL 0x00008000 /* File tail should not be merged */
  403. #define EXT4_INODE_FLAG_DIRSYNC \
  404. 0x00010000 /* Dirsync behaviour (directories only) */
  405. #define EXT4_INODE_FLAG_TOPDIR 0x00020000 /* Top of directory hierarchies */
  406. #define EXT4_INODE_FLAG_HUGE_FILE 0x00040000 /* Set to each huge file */
  407. #define EXT4_INODE_FLAG_EXTENTS 0x00080000 /* Inode uses extents */
  408. #define EXT4_INODE_FLAG_EA_INODE 0x00200000 /* Inode used for large EA */
  409. #define EXT4_INODE_FLAG_EOFBLOCKS 0x00400000 /* Blocks allocated beyond EOF */
  410. #define EXT4_INODE_FLAG_RESERVED 0x80000000 /* reserved for ext4 lib */
  411. #define EXT4_INODE_ROOT_INDEX 2
  412. #define EXT4_DIRECTORY_FILENAME_LEN 255
  413. /**@brief Directory entry types. */
  414. enum { EXT4_DE_UNKNOWN = 0,
  415. EXT4_DE_REG_FILE,
  416. EXT4_DE_DIR,
  417. EXT4_DE_CHRDEV,
  418. EXT4_DE_BLKDEV,
  419. EXT4_DE_FIFO,
  420. EXT4_DE_SOCK,
  421. EXT4_DE_SYMLINK };
  422. #define EXT4_DIRENTRY_DIR_CSUM 0xDE
  423. #pragma pack(push, 1)
  424. union ext4_dir_en_internal {
  425. uint8_t name_length_high; /* Higher 8 bits of name length */
  426. uint8_t inode_type; /* Type of referenced inode (in rev >= 0.5) */
  427. };
  428. /**
  429. * Linked list directory entry structure
  430. */
  431. struct ext4_dir_en {
  432. uint32_t inode; /* I-node for the entry */
  433. uint16_t entry_len; /* Distance to the next directory entry */
  434. uint8_t name_len; /* Lower 8 bits of name length */
  435. union ext4_dir_en_internal in;
  436. uint8_t name[]; /* Entry name */
  437. };
  438. /* Structures for indexed directory */
  439. struct ext4_dir_idx_climit {
  440. uint16_t limit;
  441. uint16_t count;
  442. };
  443. struct ext4_dir_idx_dot_en {
  444. uint32_t inode;
  445. uint16_t entry_length;
  446. uint8_t name_length;
  447. uint8_t inode_type;
  448. uint8_t name[4];
  449. };
  450. struct ext4_dir_idx_rinfo {
  451. uint32_t reserved_zero;
  452. uint8_t hash_version;
  453. uint8_t info_length;
  454. uint8_t indirect_levels;
  455. uint8_t unused_flags;
  456. };
  457. struct ext4_dir_idx_entry {
  458. uint32_t hash;
  459. uint32_t block;
  460. };
  461. struct ext4_dir_idx_root {
  462. struct ext4_dir_idx_dot_en dots[2];
  463. struct ext4_dir_idx_rinfo info;
  464. struct ext4_dir_idx_entry en[];
  465. };
  466. struct ext4_fake_dir_entry {
  467. uint32_t inode;
  468. uint16_t entry_length;
  469. uint8_t name_length;
  470. uint8_t inode_type;
  471. };
  472. struct ext4_dir_idx_node {
  473. struct ext4_fake_dir_entry fake;
  474. struct ext4_dir_idx_entry entries[];
  475. };
  476. /*
  477. * This goes at the end of each htree block.
  478. */
  479. struct ext4_dir_idx_tail {
  480. uint32_t reserved;
  481. uint32_t checksum; /* crc32c(uuid+inum+dirblock) */
  482. };
  483. /*
  484. * This is a bogus directory entry at the end of each leaf block that
  485. * records checksums.
  486. */
  487. struct ext4_dir_entry_tail {
  488. uint32_t reserved_zero1; /* Pretend to be unused */
  489. uint16_t rec_len; /* 12 */
  490. uint8_t reserved_zero2; /* Zero name length */
  491. uint8_t reserved_ft; /* 0xDE, fake file type */
  492. uint32_t checksum; /* crc32c(uuid+inum+dirblock) */
  493. };
  494. #pragma pack(pop)
  495. #define EXT4_DIRENT_TAIL(block, blocksize) \
  496. ((struct ext4_dir_entry_tail *)(((char *)(block)) + ((blocksize) - \
  497. sizeof(struct ext4_dir_entry_tail))))
  498. #define EXT4_ERR_BAD_DX_DIR (-25000)
  499. #define EXT4_LINK_MAX 65000
  500. #define EXT4_BAD_INO 1
  501. #define EXT4_ROOT_INO 2
  502. #define EXT4_BOOT_LOADER_INO 5
  503. #define EXT4_UNDEL_DIR_INO 6
  504. #define EXT4_RESIZE_INO 7
  505. #define EXT4_JOURNAL_INO 8
  506. #define EXT4_GOOD_OLD_FIRST_INO 11
  507. #define EXT_MAX_BLOCKS (ext4_lblk_t) (-1)
  508. #define IN_RANGE(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
  509. /******************************************************************************/
  510. /* EXT3 HTree directory indexing */
  511. #define EXT2_HTREE_LEGACY 0
  512. #define EXT2_HTREE_HALF_MD4 1
  513. #define EXT2_HTREE_TEA 2
  514. #define EXT2_HTREE_LEGACY_UNSIGNED 3
  515. #define EXT2_HTREE_HALF_MD4_UNSIGNED 4
  516. #define EXT2_HTREE_TEA_UNSIGNED 5
  517. #define EXT2_HTREE_EOF 0x7FFFFFFFUL
  518. #define EXT4_GOOD_OLD_INODE_SIZE 128
  519. /*****************************************************************************/
  520. /*
  521. * JBD stores integers in big endian.
  522. */
  523. #define JBD_MAGIC_NUMBER 0xc03b3998U /* The first 4 bytes of /dev/random! */
  524. /*
  525. * Descriptor block types:
  526. */
  527. #define JBD_DESCRIPTOR_BLOCK 1
  528. #define JBD_COMMIT_BLOCK 2
  529. #define JBD_SUPERBLOCK 3
  530. #define JBD_SUPERBLOCK_V2 4
  531. #define JBD_REVOKE_BLOCK 5
  532. #pragma pack(push, 1)
  533. /*
  534. * Standard header for all descriptor blocks:
  535. */
  536. struct jbd_bhdr {
  537. uint32_t magic;
  538. uint32_t blocktype;
  539. uint32_t sequence;
  540. };
  541. #pragma pack(pop)
  542. /*
  543. * Checksum types.
  544. */
  545. #define JBD_CRC32_CHKSUM 1
  546. #define JBD_MD5_CHKSUM 2
  547. #define JBD_SHA1_CHKSUM 3
  548. #define JBD_CRC32C_CHKSUM 4
  549. #define JBD_CRC32_CHKSUM_SIZE 4
  550. #define JBD_CHECKSUM_BYTES (32 / sizeof(uint32_t))
  551. #pragma pack(push, 1)
  552. /*
  553. * Commit block header for storing transactional checksums:
  554. *
  555. * NOTE: If FEATURE_COMPAT_CHECKSUM (checksum v1) is set, the h_chksum*
  556. * fields are used to store a checksum of the descriptor and data blocks.
  557. *
  558. * If FEATURE_INCOMPAT_CSUM_V2 (checksum v2) is set, then the h_chksum
  559. * field is used to store crc32c(uuid+commit_block). Each journal metadata
  560. * block gets its own checksum, and data block checksums are stored in
  561. * journal_block_tag (in the descriptor). The other h_chksum* fields are
  562. * not used.
  563. *
  564. * If FEATURE_INCOMPAT_CSUM_V3 is set, the descriptor block uses
  565. * journal_block_tag3_t to store a full 32-bit checksum. Everything else
  566. * is the same as v2.
  567. *
  568. * Checksum v1, v2, and v3 are mutually exclusive features.
  569. */
  570. struct jbd_commit_header {
  571. struct jbd_bhdr header;
  572. uint8_t chksum_type;
  573. uint8_t chksum_size;
  574. uint8_t padding[2];
  575. uint32_t chksum[JBD_CHECKSUM_BYTES];
  576. uint64_t commit_sec;
  577. uint32_t commit_nsec;
  578. };
  579. /*
  580. * The block tag: used to describe a single buffer in the journal
  581. */
  582. struct jbd_block_tag3 {
  583. uint32_t blocknr; /* The on-disk block number */
  584. uint32_t flags; /* See below */
  585. uint32_t blocknr_high; /* most-significant high 32bits. */
  586. uint32_t checksum; /* crc32c(uuid+seq+block) */
  587. };
  588. struct jbd_block_tag {
  589. uint32_t blocknr; /* The on-disk block number */
  590. uint16_t checksum; /* truncated crc32c(uuid+seq+block) */
  591. uint16_t flags; /* See below */
  592. uint32_t blocknr_high; /* most-significant high 32bits. */
  593. };
  594. #pragma pack(pop)
  595. /* Definitions for the journal tag flags word: */
  596. #define JBD_FLAG_ESCAPE 1 /* on-disk block is escaped */
  597. #define JBD_FLAG_SAME_UUID 2 /* block has same uuid as previous */
  598. #define JBD_FLAG_DELETED 4 /* block deleted by this transaction */
  599. #define JBD_FLAG_LAST_TAG 8 /* last tag in this descriptor block */
  600. #pragma pack(push, 1)
  601. /* Tail of descriptor block, for checksumming */
  602. struct jbd_block_tail {
  603. uint32_t checksum;
  604. };
  605. /*
  606. * The revoke descriptor: used on disk to describe a series of blocks to
  607. * be revoked from the log
  608. */
  609. struct jbd_revoke_header {
  610. struct jbd_bhdr header;
  611. uint32_t count; /* Count of bytes used in the block */
  612. };
  613. /* Tail of revoke block, for checksumming */
  614. struct jbd_revoke_tail {
  615. uint32_t checksum;
  616. };
  617. #pragma pack(pop)
  618. #define JBD_USERS_MAX 48
  619. #define JBD_USERS_SIZE (UUID_SIZE * JBD_USERS_MAX)
  620. #pragma pack(push, 1)
  621. /*
  622. * The journal superblock. All fields are in big-endian byte order.
  623. */
  624. struct jbd_sb {
  625. /* 0x0000 */
  626. struct jbd_bhdr header;
  627. /* 0x000C */
  628. /* Static information describing the journal */
  629. uint32_t blocksize; /* journal device blocksize */
  630. uint32_t maxlen; /* total blocks in journal file */
  631. uint32_t first; /* first block of log information */
  632. /* 0x0018 */
  633. /* Dynamic information describing the current state of the log */
  634. uint32_t sequence; /* first commit ID expected in log */
  635. uint32_t start; /* blocknr of start of log */
  636. /* 0x0020 */
  637. /* Error value, as set by journal_abort(). */
  638. int32_t error_val;
  639. /* 0x0024 */
  640. /* Remaining fields are only valid in a version-2 superblock */
  641. uint32_t feature_compat; /* compatible feature set */
  642. uint32_t feature_incompat; /* incompatible feature set */
  643. uint32_t feature_ro_compat; /* readonly-compatible feature set */
  644. /* 0x0030 */
  645. uint8_t uuid[UUID_SIZE]; /* 128-bit uuid for journal */
  646. /* 0x0040 */
  647. uint32_t nr_users; /* Nr of filesystems sharing log */
  648. uint32_t dynsuper; /* Blocknr of dynamic superblock copy*/
  649. /* 0x0048 */
  650. uint32_t max_transaction; /* Limit of journal blocks per trans.*/
  651. uint32_t max_trandata; /* Limit of data blocks per trans. */
  652. /* 0x0050 */
  653. uint8_t checksum_type; /* checksum type */
  654. uint8_t padding2[3];
  655. uint32_t padding[42];
  656. uint32_t checksum; /* crc32c(superblock) */
  657. /* 0x0100 */
  658. uint8_t users[JBD_USERS_SIZE]; /* ids of all fs'es sharing the log */
  659. /* 0x0400 */
  660. };
  661. #pragma pack(pop)
  662. #define JBD_SUPERBLOCK_SIZE sizeof(struct jbd_sb)
  663. #define JBD_HAS_COMPAT_FEATURE(jsb,mask) \
  664. ((jsb)->header.blocktype >= to_be32(2) && \
  665. ((jsb)->feature_compat & to_be32((mask))))
  666. #define JBD_HAS_RO_COMPAT_FEATURE(jsb,mask) \
  667. ((jsb)->header.blocktype >= to_be32(2) && \
  668. ((jsb)->feature_ro_compat & to_be32((mask))))
  669. #define JBD_HAS_INCOMPAT_FEATURE(jsb,mask) \
  670. ((jsb)->header.blocktype >= to_be32(2) && \
  671. ((jsb)->feature_incompat & to_be32((mask))))
  672. #define JBD_FEATURE_COMPAT_CHECKSUM 0x00000001
  673. #define JBD_FEATURE_INCOMPAT_REVOKE 0x00000001
  674. #define JBD_FEATURE_INCOMPAT_64BIT 0x00000002
  675. #define JBD_FEATURE_INCOMPAT_ASYNC_COMMIT 0x00000004
  676. #define JBD_FEATURE_INCOMPAT_CSUM_V2 0x00000008
  677. #define JBD_FEATURE_INCOMPAT_CSUM_V3 0x00000010
  678. /* Features known to this kernel version: */
  679. #define JBD_KNOWN_COMPAT_FEATURES 0
  680. #define JBD_KNOWN_ROCOMPAT_FEATURES 0
  681. #define JBD_KNOWN_INCOMPAT_FEATURES (JBD_FEATURE_INCOMPAT_REVOKE|\
  682. JBD_FEATURE_INCOMPAT_ASYNC_COMMIT|\
  683. JBD_FEATURE_INCOMPAT_64BIT|\
  684. JBD_FEATURE_INCOMPAT_CSUM_V2|\
  685. JBD_FEATURE_INCOMPAT_CSUM_V3)
  686. /*****************************************************************************/
  687. #define EXT4_CRC32_INIT (0xFFFFFFFFUL)
  688. /*****************************************************************************/
  689. #ifdef __cplusplus
  690. }
  691. #endif
  692. #if CONFIG_USE_USER_MALLOC
  693. #define ext4_malloc ext4_user_malloc
  694. #define ext4_calloc ext4_user_calloc
  695. #define ext4_realloc ext4_user_realloc
  696. #define ext4_free ext4_user_free
  697. #else
  698. #define ext4_malloc malloc
  699. #define ext4_calloc calloc
  700. #define ext4_realloc realloc
  701. #define ext4_free free
  702. #endif
  703. #endif /* EXT4_TYPES_H_ */
  704. /**
  705. * @}
  706. */