2 * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
6 * Copyright (c) 2012 Martin Sucha
7 * Copyright (c) 2012 Frantisek Princ
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
34 /** @addtogroup lwext4
39 * @brief Ext4 data structure definitions.
45 #include "ext4_config.h"
46 #include "ext4_blockdev.h"
51 #define EXT4_CHECKSUM_CRC32C 1
54 * Structure of the super block
57 uint32_t inodes_count; /* I-nodes count */
58 uint32_t blocks_count_lo; /* Blocks count */
59 uint32_t reserved_blocks_count_lo; /* Reserved blocks count */
60 uint32_t free_blocks_count_lo; /* Free blocks count */
61 uint32_t free_inodes_count; /* Free inodes count */
62 uint32_t first_data_block; /* First Data Block */
63 uint32_t log_block_size; /* Block size */
64 uint32_t log_cluster_size; /* Obsoleted fragment size */
65 uint32_t blocks_per_group; /* Number of blocks per group */
66 uint32_t frags_per_group; /* Obsoleted fragments per group */
67 uint32_t inodes_per_group; /* Number of inodes per group */
68 uint32_t mount_time; /* Mount time */
69 uint32_t write_time; /* Write time */
70 uint16_t mount_count; /* Mount count */
71 uint16_t max_mount_count; /* Maximal mount count */
72 uint16_t magic; /* Magic signature */
73 uint16_t state; /* File system state */
74 uint16_t errors; /* Behavior when detecting errors */
75 uint16_t minor_rev_level; /* Minor revision level */
76 uint32_t last_check_time; /* Time of last check */
77 uint32_t check_interval; /* Maximum time between checks */
78 uint32_t creator_os; /* Creator OS */
79 uint32_t rev_level; /* Revision level */
80 uint16_t def_resuid; /* Default uid for reserved blocks */
81 uint16_t def_resgid; /* Default gid for reserved blocks */
83 /* Fields for EXT4_DYNAMIC_REV superblocks only. */
84 uint32_t first_inode; /* First non-reserved inode */
85 uint16_t inode_size; /* Size of inode structure */
86 uint16_t block_group_index; /* Block group index of this superblock */
87 uint32_t features_compatible; /* Compatible feature set */
88 uint32_t features_incompatible; /* Incompatible feature set */
89 uint32_t features_read_only; /* Readonly-compatible feature set */
90 uint8_t uuid[16]; /* 128-bit uuid for volume */
91 char volume_name[16]; /* Volume name */
92 char last_mounted[64]; /* Directory where last mounted */
93 uint32_t algorithm_usage_bitmap; /* For compression */
96 * Performance hints. Directory preallocation should only
97 * happen if the EXT4_FEATURE_COMPAT_DIR_PREALLOC flag is on.
99 uint8_t s_prealloc_blocks; /* Number of blocks to try to preallocate */
100 uint8_t s_prealloc_dir_blocks; /* Number to preallocate for dirs */
101 uint16_t s_reserved_gdt_blocks; /* Per group desc for online growth */
104 * Journaling support valid if EXT4_FEATURE_COMPAT_HAS_JOURNAL set.
106 uint8_t journal_uuid[16]; /* UUID of journal superblock */
107 uint32_t journal_inode_number; /* Inode number of journal file */
108 uint32_t journal_dev; /* Device number of journal file */
109 uint32_t last_orphan; /* Head of list of inodes to delete */
110 uint32_t hash_seed[4]; /* HTREE hash seed */
111 uint8_t default_hash_version; /* Default hash version to use */
112 uint8_t journal_backup_type;
113 uint16_t desc_size; /* Size of group descriptor */
114 uint32_t default_mount_opts; /* Default mount options */
115 uint32_t first_meta_bg; /* First metablock block group */
116 uint32_t mkfs_time; /* When the filesystem was created */
117 uint32_t journal_blocks[17]; /* Backup of the journal inode */
119 /* 64bit support valid if EXT4_FEATURE_COMPAT_64BIT */
120 uint32_t blocks_count_hi; /* Blocks count */
121 uint32_t reserved_blocks_count_hi; /* Reserved blocks count */
122 uint32_t free_blocks_count_hi; /* Free blocks count */
123 uint16_t min_extra_isize; /* All inodes have at least # bytes */
124 uint16_t want_extra_isize; /* New inodes should reserve # bytes */
125 uint32_t flags; /* Miscellaneous flags */
126 uint16_t raid_stride; /* RAID stride */
127 uint16_t mmp_interval; /* # seconds to wait in MMP checking */
128 uint64_t mmp_block; /* Block for multi-mount protection */
129 uint32_t raid_stripe_width; /* Blocks on all data disks (N * stride) */
130 uint8_t log_groups_per_flex; /* FLEX_BG group size */
131 uint8_t checksum_type;
132 uint16_t reserved_pad;
133 uint64_t kbytes_written; /* Number of lifetime kilobytes written */
134 uint32_t snapshot_inum; /* I-node number of active snapshot */
135 uint32_t snapshot_id; /* Sequential ID of active snapshot */
137 snapshot_r_blocks_count; /* Reserved blocks for active snapshot's
140 snapshot_list; /* I-node number of the head of the on-disk snapshot
142 uint32_t error_count; /* Number of file system errors */
143 uint32_t first_error_time; /* First time an error happened */
144 uint32_t first_error_ino; /* I-node involved in first error */
145 uint64_t first_error_block; /* Block involved of first error */
146 uint8_t first_error_func[32]; /* Function where the error happened */
147 uint32_t first_error_line; /* Line number where error happened */
148 uint32_t last_error_time; /* Most recent time of an error */
149 uint32_t last_error_ino; /* I-node involved in last error */
150 uint32_t last_error_line; /* Line number where error happened */
151 uint64_t last_error_block; /* Block involved of last error */
152 uint8_t last_error_func[32]; /* Function where the error happened */
153 uint8_t mount_opts[64];
154 uint32_t usr_quota_inum; /* inode for tracking user quota */
155 uint32_t grp_quota_inum; /* inode for tracking group quota */
156 uint32_t overhead_clusters; /* overhead blocks/clusters in fs */
157 uint32_t backup_bgs[2]; /* groups with sparse_super2 SBs */
158 uint8_t encrypt_algos[4]; /* Encryption algorithms in use */
159 uint8_t encrypt_pw_salt[16]; /* Salt used for string2key algorithm */
160 uint32_t lpf_ino; /* Location of the lost+found inode */
161 uint32_t padding[100]; /* Padding to the end of the block */
162 uint32_t checksum; /* crc32c(superblock) */
163 } __attribute__((packed));
165 #define EXT4_SUPERBLOCK_MAGIC 0xEF53
166 #define EXT4_SUPERBLOCK_SIZE 1024
167 #define EXT4_SUPERBLOCK_OFFSET 1024
169 #define EXT4_SUPERBLOCK_OS_LINUX 0
170 #define EXT4_SUPERBLOCK_OS_HURD 1
173 * Misc. filesystem flags
175 #define EXT4_SUPERBLOCK_FLAGS_SIGNED_HASH 0x0001
176 #define EXT4_SUPERBLOCK_FLAGS_UNSIGNED_HASH 0x0002
177 #define EXT4_SUPERBLOCK_FLAGS_TEST_FILESYS 0x0004
181 #define EXT4_SUPERBLOCK_STATE_VALID_FS 0x0001 /* Unmounted cleanly */
182 #define EXT4_SUPERBLOCK_STATE_ERROR_FS 0x0002 /* Errors detected */
183 #define EXT4_SUPERBLOCK_STATE_ORPHAN_FS 0x0004 /* Orphans being recovered */
186 * Behaviour when errors detected
188 #define EXT4_SUPERBLOCK_ERRORS_CONTINUE 1 /* Continue execution */
189 #define EXT4_SUPERBLOCK_ERRORS_RO 2 /* Remount fs read-only */
190 #define EXT4_SUPERBLOCK_ERRORS_PANIC 3 /* Panic */
191 #define EXT4_SUPERBLOCK_ERRORS_DEFAULT EXT4_ERRORS_CONTINUE
194 * Compatible features
196 #define EXT4_FCOM_DIR_PREALLOC 0x0001
197 #define EXT4_FCOM_IMAGIC_INODES 0x0002
198 #define EXT4_FCOM_HAS_JOURNAL 0x0004
199 #define EXT4_FCOM_EXT_ATTR 0x0008
200 #define EXT4_FCOM_RESIZE_INODE 0x0010
201 #define EXT4_FCOM_DIR_INDEX 0x0020
204 * Read-only compatible features
206 #define EXT4_FRO_COM_SPARSE_SUPER 0x0001
207 #define EXT4_FRO_COM_LARGE_FILE 0x0002
208 #define EXT4_FRO_COM_BTREE_DIR 0x0004
209 #define EXT4_FRO_COM_HUGE_FILE 0x0008
210 #define EXT4_FRO_COM_GDT_CSUM 0x0010
211 #define EXT4_FRO_COM_DIR_NLINK 0x0020
212 #define EXT4_FRO_COM_EXTRA_ISIZE 0x0040
213 #define EXT4_FRO_COM_QUOTA 0x0100
214 #define EXT4_FRO_COM_BIGALLOC 0x0200
215 #define EXT4_FRO_COM_METADATA_CSUM 0x0400
218 * Incompatible features
220 #define EXT4_FINCOM_COMPRESSION 0x0001
221 #define EXT4_FINCOM_FILETYPE 0x0002
222 #define EXT4_FINCOM_RECOVER 0x0004 /* Needs recovery */
223 #define EXT4_FINCOM_JOURNAL_DEV 0x0008 /* Journal device */
224 #define EXT4_FINCOM_META_BG 0x0010
225 #define EXT4_FINCOM_EXTENTS 0x0040 /* extents support */
226 #define EXT4_FINCOM_64BIT 0x0080
227 #define EXT4_FINCOM_MMP 0x0100
228 #define EXT4_FINCOM_FLEX_BG 0x0200
229 #define EXT4_FINCOM_EA_INODE 0x0400 /* EA in inode */
230 #define EXT4_FINCOM_DIRDATA 0x1000 /* data in dirent */
231 #define EXT4_FINCOM_BG_USE_META_CSUM 0x2000 /* use crc32c for bg */
232 #define EXT4_FINCOM_LARGEDIR 0x4000 /* >2GB or 3-lvl htree */
233 #define EXT4_FINCOM_INLINE_DATA 0x8000 /* data in inode */
236 * EXT2 supported feature set
238 #define EXT2_SUPPORTED_FCOM 0x0000
240 #define EXT2_SUPPORTED_FINCOM \
241 (EXT4_FINCOM_FILETYPE | EXT4_FINCOM_META_BG)
243 #define EXT2_SUPPORTED_FRO_COM \
244 (EXT4_FRO_COM_SPARSE_SUPER | \
245 EXT4_FRO_COM_LARGE_FILE | EXT4_FRO_COM_BTREE_DIR)
248 * EXT3 supported feature set
250 #define EXT3_SUPPORTED_FCOM (EXT4_FCOM_DIR_INDEX)
252 #define EXT3_SUPPORTED_FINCOM \
253 (EXT4_FINCOM_FILETYPE | EXT4_FINCOM_META_BG)
255 #define EXT3_SUPPORTED_FRO_COM \
256 (EXT4_FRO_COM_SPARSE_SUPER | \
257 EXT4_FRO_COM_LARGE_FILE | EXT4_FRO_COM_BTREE_DIR)
260 * EXT4 supported feature set
262 #define EXT4_SUPPORTED_FCOM (EXT4_FCOM_DIR_INDEX)
264 #define EXT4_SUPPORTED_FINCOM \
265 (EXT4_FINCOM_FILETYPE | EXT4_FINCOM_META_BG | \
266 EXT4_FINCOM_EXTENTS | EXT4_FINCOM_FLEX_BG | \
269 #define EXT4_SUPPORTED_FRO_COM \
270 (EXT4_FRO_COM_SPARSE_SUPER | \
271 EXT4_FRO_COM_METADATA_CSUM | \
272 EXT4_FRO_COM_LARGE_FILE | EXT4_FRO_COM_GDT_CSUM | \
273 EXT4_FRO_COM_DIR_NLINK | \
274 EXT4_FRO_COM_EXTRA_ISIZE | \
275 EXT4_FRO_COM_BTREE_DIR | EXT4_FRO_COM_HUGE_FILE)
278 * RECOVER - journaling in lwext4 is not supported
279 * (probably won't be ever...)
280 * MMP - multi-mout protection (impossible scenario)
282 #define EXT_FINCOM_IGNORED \
283 EXT4_FINCOM_RECOVER | EXT4_FINCOM_MMP
286 /*TODO: Features incompatible to implement*/
287 #define EXT4_SUPPORTED_FINCOM
288 (EXT4_FINCOM_INLINE_DATA)
290 /*TODO: Features read only to implement*/
291 #define EXT4_SUPPORTED_FRO_COM
292 EXT4_FRO_COM_BIGALLOC |\
297 struct ext4_blockdev *bdev;
298 struct ext4_sblock sb;
300 uint64_t inode_block_limits[4];
301 uint64_t inode_blocks_per_level[4];
303 uint32_t last_inode_bg_id;
306 /* Inode table/bitmap not in use */
307 #define EXT4_BLOCK_GROUP_INODE_UNINIT 0x0001
308 /* Block bitmap not in use */
309 #define EXT4_BLOCK_GROUP_BLOCK_UNINIT 0x0002
310 /* On-disk itable initialized to zero */
311 #define EXT4_BLOCK_GROUP_ITABLE_ZEROED 0x0004
314 * Structure of a blocks group descriptor
317 uint32_t block_bitmap_lo; /* Blocks bitmap block */
318 uint32_t inode_bitmap_lo; /* Inodes bitmap block */
319 uint32_t inode_table_first_block_lo; /* Inodes table block */
320 uint16_t free_blocks_count_lo; /* Free blocks count */
321 uint16_t free_inodes_count_lo; /* Free inodes count */
322 uint16_t used_dirs_count_lo; /* Directories count */
323 uint16_t flags; /* EXT4_BG_flags (INODE_UNINIT, etc) */
324 uint32_t exclude_bitmap_lo; /* Exclude bitmap for snapshots */
325 uint16_t block_bitmap_csum_lo; /* crc32c(s_uuid+grp_num+bbitmap) LE */
326 uint16_t inode_bitmap_csum_lo; /* crc32c(s_uuid+grp_num+ibitmap) LE */
327 uint16_t itable_unused_lo; /* Unused inodes count */
328 uint16_t checksum; /* crc16(sb_uuid+group+desc) */
330 uint32_t block_bitmap_hi; /* Blocks bitmap block MSB */
331 uint32_t inode_bitmap_hi; /* I-nodes bitmap block MSB */
332 uint32_t inode_table_first_block_hi; /* I-nodes table block MSB */
333 uint16_t free_blocks_count_hi; /* Free blocks count MSB */
334 uint16_t free_inodes_count_hi; /* Free i-nodes count MSB */
335 uint16_t used_dirs_count_hi; /* Directories count MSB */
336 uint16_t itable_unused_hi; /* Unused inodes count MSB */
337 uint32_t exclude_bitmap_hi; /* Exclude bitmap block MSB */
338 uint16_t block_bitmap_csum_hi; /* crc32c(s_uuid+grp_num+bbitmap) BE */
339 uint16_t inode_bitmap_csum_hi; /* crc32c(s_uuid+grp_num+ibitmap) BE */
340 uint32_t reserved; /* Padding */
343 struct ext4_block_group_ref {
344 struct ext4_block block;
345 struct ext4_bgroup *block_group;
351 #define EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE 32
352 #define EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE 64
354 #define EXT4_MIN_BLOCK_SIZE 1024 /* 1 KiB */
355 #define EXT4_MAX_BLOCK_SIZE 65536 /* 64 KiB */
356 #define EXT4_REV0_INODE_SIZE 128
358 #define EXT4_INODE_BLOCK_SIZE 512
360 #define EXT4_INODE_DIRECT_BLOCK_COUNT 12
361 #define EXT4_INODE_INDIRECT_BLOCK EXT4_INODE_DIRECT_BLOCK_COUNT
362 #define EXT4_INODE_DOUBLE_INDIRECT_BLOCK (EXT4_INODE_INDIRECT_BLOCK + 1)
363 #define EXT4_INODE_TRIPPLE_INDIRECT_BLOCK (EXT4_INODE_DOUBLE_INDIRECT_BLOCK + 1)
364 #define EXT4_INODE_BLOCKS (EXT4_INODE_TRIPPLE_INDIRECT_BLOCK + 1)
365 #define EXT4_INODE_INDIRECT_BLOCK_COUNT \
366 (EXT4_INODE_BLOCKS - EXT4_INODE_DIRECT_BLOCK_COUNT)
369 * Structure of an inode on the disk
372 uint16_t mode; /* File mode */
373 uint16_t uid; /* Low 16 bits of owner uid */
374 uint32_t size_lo; /* Size in bytes */
375 uint32_t access_time; /* Access time */
376 uint32_t change_inode_time; /* I-node change time */
377 uint32_t modification_time; /* Modification time */
378 uint32_t deletion_time; /* Deletion time */
379 uint16_t gid; /* Low 16 bits of group id */
380 uint16_t links_count; /* Links count */
381 uint32_t blocks_count_lo; /* Blocks count */
382 uint32_t flags; /* File flags */
383 uint32_t unused_osd1; /* OS dependent - not used in HelenOS */
384 uint32_t blocks[EXT4_INODE_BLOCKS]; /* Pointers to blocks */
385 uint32_t generation; /* File version (for NFS) */
386 uint32_t file_acl_lo; /* File ACL */
388 uint32_t obso_faddr; /* Obsoleted fragment address */
392 uint16_t blocks_high;
393 uint16_t file_acl_high;
396 uint16_t checksum_lo; /* crc32c(uuid+inum+inode) LE */
406 } __attribute__((packed)) osd2;
408 uint16_t extra_isize;
409 uint16_t checksum_hi; /* crc32c(uuid+inum+inode) BE */
410 uint32_t ctime_extra; /* Extra change time (nsec << 2 | epoch) */
411 uint32_t mtime_extra; /* Extra Modification time (nsec << 2 | epoch) */
412 uint32_t atime_extra; /* Extra Access time (nsec << 2 | epoch) */
413 uint32_t crtime; /* File creation time */
415 crtime_extra; /* Extra file creation time (nsec << 2 | epoch) */
416 uint32_t version_hi; /* High 32 bits for 64-bit version */
417 } __attribute__((packed));
419 #define EXT4_INODE_MODE_FIFO 0x1000
420 #define EXT4_INODE_MODE_CHARDEV 0x2000
421 #define EXT4_INODE_MODE_DIRECTORY 0x4000
422 #define EXT4_INODE_MODE_BLOCKDEV 0x6000
423 #define EXT4_INODE_MODE_FILE 0x8000
424 #define EXT4_INODE_MODE_SOFTLINK 0xA000
425 #define EXT4_INODE_MODE_SOCKET 0xC000
426 #define EXT4_INODE_MODE_TYPE_MASK 0xF000
431 #define EXT4_INODE_FLAG_SECRM 0x00000001 /* Secure deletion */
432 #define EXT4_INODE_FLAG_UNRM 0x00000002 /* Undelete */
433 #define EXT4_INODE_FLAG_COMPR 0x00000004 /* Compress file */
434 #define EXT4_INODE_FLAG_SYNC 0x00000008 /* Synchronous updates */
435 #define EXT4_INODE_FLAG_IMMUTABLE 0x00000010 /* Immutable file */
436 #define EXT4_INODE_FLAG_APPEND 0x00000020 /* writes to file may only append */
437 #define EXT4_INODE_FLAG_NODUMP 0x00000040 /* do not dump file */
438 #define EXT4_INODE_FLAG_NOATIME 0x00000080 /* do not update atime */
440 /* Compression flags */
441 #define EXT4_INODE_FLAG_DIRTY 0x00000100
442 #define EXT4_INODE_FLAG_COMPRBLK \
443 0x00000200 /* One or more compressed clusters */
444 #define EXT4_INODE_FLAG_NOCOMPR 0x00000400 /* Don't compress */
445 #define EXT4_INODE_FLAG_ECOMPR 0x00000800 /* Compression error */
447 #define EXT4_INODE_FLAG_INDEX 0x00001000 /* hash-indexed directory */
448 #define EXT4_INODE_FLAG_IMAGIC 0x00002000 /* AFS directory */
449 #define EXT4_INODE_FLAG_JOURNAL_DATA \
450 0x00004000 /* File data should be journaled */
451 #define EXT4_INODE_FLAG_NOTAIL 0x00008000 /* File tail should not be merged */
452 #define EXT4_INODE_FLAG_DIRSYNC \
453 0x00010000 /* Dirsync behaviour (directories only) */
454 #define EXT4_INODE_FLAG_TOPDIR 0x00020000 /* Top of directory hierarchies */
455 #define EXT4_INODE_FLAG_HUGE_FILE 0x00040000 /* Set to each huge file */
456 #define EXT4_INODE_FLAG_EXTENTS 0x00080000 /* Inode uses extents */
457 #define EXT4_INODE_FLAG_EA_INODE 0x00200000 /* Inode used for large EA */
458 #define EXT4_INODE_FLAG_EOFBLOCKS 0x00400000 /* Blocks allocated beyond EOF */
459 #define EXT4_INODE_FLAG_RESERVED 0x80000000 /* reserved for ext4 lib */
461 #define EXT4_INODE_ROOT_INDEX 2
463 struct ext4_inode_ref {
464 struct ext4_block block;
465 struct ext4_inode *inode;
471 #define EXT4_DIRECTORY_FILENAME_LEN 255
473 /**@brief Directory entry types. */
474 enum { EXT4_DIRENTRY_UNKNOWN = 0,
475 EXT4_DIRENTRY_REG_FILE,
477 EXT4_DIRENTRY_CHRDEV,
478 EXT4_DIRENTRY_BLKDEV,
481 EXT4_DIRENTRY_SYMLINK };
483 #define EXT4_DIRENTRY_DIR_CSUM 0xDE
485 union ext4_directory_entry_ll_internal {
486 uint8_t name_length_high; /* Higher 8 bits of name length */
487 uint8_t inode_type; /* Type of referenced inode (in rev >= 0.5) */
488 } __attribute__((packed));
491 * Linked list directory entry structure
493 struct ext4_directory_entry_ll {
494 uint32_t inode; /* I-node for the entry */
495 uint16_t entry_length; /* Distance to the next directory entry */
496 uint8_t name_length; /* Lower 8 bits of name length */
498 union ext4_directory_entry_ll_internal in;
500 uint8_t name[EXT4_DIRECTORY_FILENAME_LEN]; /* Entry name */
501 } __attribute__((packed));
503 struct ext4_directory_iterator {
504 struct ext4_inode_ref *inode_ref;
505 struct ext4_block current_block;
506 uint64_t current_offset;
507 struct ext4_directory_entry_ll *current;
510 struct ext4_directory_search_result {
511 struct ext4_block block;
512 struct ext4_directory_entry_ll *dentry;
515 /* Structures for indexed directory */
517 struct ext4_directory_dx_countlimit {
522 struct ext4_directory_dx_dot_entry {
524 uint16_t entry_length;
530 struct ext4_directory_dx_root_info {
531 uint32_t reserved_zero;
532 uint8_t hash_version;
534 uint8_t indirect_levels;
535 uint8_t unused_flags;
538 struct ext4_directory_dx_entry {
543 struct ext4_directory_dx_root {
544 struct ext4_directory_dx_dot_entry dots[2];
545 struct ext4_directory_dx_root_info info;
546 struct ext4_directory_dx_entry entries[];
549 struct ext4_fake_directory_entry {
551 uint16_t entry_length;
556 struct ext4_directory_dx_node {
557 struct ext4_fake_directory_entry fake;
558 struct ext4_directory_dx_entry entries[];
561 struct ext4_directory_dx_block {
562 struct ext4_block block;
563 struct ext4_directory_dx_entry *entries;
564 struct ext4_directory_dx_entry *position;
568 * This goes at the end of each htree block.
570 struct ext4_directory_dx_tail {
572 uint32_t checksum; /* crc32c(uuid+inum+dirblock) */
576 * This is a bogus directory entry at the end of each leaf block that
579 struct ext4_directory_entry_tail {
580 uint32_t reserved_zero1; /* Pretend to be unused */
581 uint16_t rec_len; /* 12 */
582 uint8_t reserved_zero2; /* Zero name length */
583 uint8_t reserved_ft; /* 0xDE, fake file type */
584 uint32_t checksum; /* crc32c(uuid+inum+dirblock) */
587 #define EXT4_DIRENT_TAIL(block, blocksize) \
588 ((struct ext4_directory_entry_tail *)(((char *)(block)) + \
590 sizeof(struct ext4_directory_entry_tail))))
592 #define EXT4_ERR_BAD_DX_DIR (-25000)
594 #define EXT4_LINK_MAX 65000
596 #define EXT4_BAD_INO 1
597 #define EXT4_ROOT_INO 2
598 #define EXT4_BOOT_LOADER_INO 5
599 #define EXT4_UNDEL_DIR_INO 6
600 #define EXT4_RESIZE_INO 7
601 #define EXT4_JOURNAL_INO 8
603 #define EXT4_GOOD_OLD_FIRST_INO 11
605 #define EXT4_EXT_UNWRITTEN_MASK (1L << 15)
607 #define EXT4_EXT_MAX_LEN_WRITTEN (1L << 15)
608 #define EXT4_EXT_MAX_LEN_UNWRITTEN \
609 (EXT4_EXT_MAX_LEN_WRITTEN - 1)
611 #define EXT4_EXT_GET_LEN(ex) to_le16((ex)->block_count)
612 #define EXT4_EXT_GET_LEN_UNWRITTEN(ex) \
613 (EXT4_EXT_GET_LEN(ex) &= ~(EXT4_EXT_UNWRITTEN_MASK))
614 #define EXT4_EXT_SET_LEN(ex, count) \
615 ((ex)->block_count = to_le16(count))
617 #define EXT4_EXT_IS_UNWRITTEN(ex) \
618 (EXT4_EXT_GET_LEN(ex) > EXT4_EXT_MAX_LEN_WRITTEN)
619 #define EXT4_EXT_SET_UNWRITTEN(ex) \
620 ((ex)->block_count |= to_le16(EXT4_EXT_UNWRITTEN_MASK))
621 #define EXT4_EXT_SET_WRITTEN(ex) \
622 ((ex)->block_count &= ~(to_le16(EXT4_EXT_UNWRITTEN_MASK)))
624 * This is the extent tail on-disk structure.
625 * All other extent structures are 12 bytes long. It turns out that
626 * block_size % 12 >= 4 for at least all powers of 2 greater than 512, which
627 * covers all valid ext4 block sizes. Therefore, this tail structure can be
628 * crammed into the end of the block without having to rebalance the tree.
630 struct ext4_extent_tail
632 uint32_t et_checksum; /* crc32c(uuid+inum+extent_block) */
636 * This is the extent on-disk structure.
637 * It's used at the bottom of the tree.
640 uint32_t first_block; /* First logical block extent covers */
641 uint16_t block_count; /* Number of blocks covered by extent */
642 uint16_t start_hi; /* High 16 bits of physical block */
643 uint32_t start_lo; /* Low 32 bits of physical block */
647 * This is index on-disk structure.
648 * It's used at all the levels except the bottom.
650 struct ext4_extent_index {
651 uint32_t first_block; /* Index covers logical blocks from 'block' */
654 * Pointer to the physical block of the next
655 * level. leaf or next index could be there
656 * high 16 bits of physical block
664 * Each block (leaves and indexes), even inode-stored has header.
666 struct ext4_extent_header {
668 uint16_t entries_count; /* Number of valid entries */
669 uint16_t max_entries_count; /* Capacity of store in entries */
670 uint16_t depth; /* Has tree real underlying blocks? */
671 uint32_t generation; /* generation of the tree */
678 typedef uint32_t ext4_lblk_t;
679 typedef uint64_t ext4_fsblk_t;
682 * Array of ext4_ext_path contains path to some extent.
683 * Creation/lookup routines use it for traversal/splitting/etc.
684 * Truncate uses it to simulate recursive walking.
686 struct ext4_extent_path {
687 ext4_fsblk_t p_block;
688 struct ext4_block block;
691 struct ext4_extent_header *header;
692 struct ext4_extent_index *index;
693 struct ext4_extent *extent;
698 #define EXT4_EXTENT_MAGIC 0xF30A
700 #define EXT4_EXTENT_FIRST(header) \
701 ((struct ext4_extent *)(((char *)(header)) + \
702 sizeof(struct ext4_extent_header)))
704 #define EXT4_EXTENT_FIRST_INDEX(header) \
705 ((struct ext4_extent_index *)(((char *)(header)) + \
706 sizeof(struct ext4_extent_header)))
709 * EXT_INIT_MAX_LEN is the maximum number of blocks we can have in an
710 * initialized extent. This is 2^15 and not (2^16 - 1), since we use the
711 * MSB of ee_len field in the extent datastructure to signify if this
712 * particular extent is an initialized extent or an uninitialized (i.e.
714 * EXT_UNINIT_MAX_LEN is the maximum number of blocks we can have in an
715 * uninitialized extent.
716 * If ee_len is <= 0x8000, it is an initialized extent. Otherwise, it is an
717 * uninitialized one. In other words, if MSB of ee_len is set, it is an
718 * uninitialized extent with only one special scenario when ee_len = 0x8000.
719 * In this case we can not have an uninitialized extent of zero length and
720 * thus we make it as a special case of initialized extent with 0x8000 length.
721 * This way we get better extent-to-group alignment for initialized extents.
722 * Hence, the maximum number of blocks we can have in an *initialized*
723 * extent is 2^15 (32768) and in an *uninitialized* extent is 2^15-1 (32767).
725 #define EXT_INIT_MAX_LEN (1L << 15)
726 #define EXT_UNWRITTEN_MAX_LEN (EXT_INIT_MAX_LEN - 1)
728 #define EXT_EXTENT_SIZE sizeof(struct ext4_extent)
729 #define EXT_INDEX_SIZE sizeof(struct ext4_extent_idx)
731 #define EXT_FIRST_EXTENT(__hdr__) \
732 ((struct ext4_extent *)(((char *)(__hdr__)) + \
733 sizeof(struct ext4_extent_header)))
734 #define EXT_FIRST_INDEX(__hdr__) \
735 ((struct ext4_extent_index *)(((char *)(__hdr__)) + \
736 sizeof(struct ext4_extent_header)))
737 #define EXT_HAS_FREE_INDEX(__path__) \
738 ((__path__)->header->entries_count < (__path__)->header->max_entries_count)
739 #define EXT_LAST_EXTENT(__hdr__) \
740 (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->entries_count - 1)
741 #define EXT_LAST_INDEX(__hdr__) \
742 (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->entries_count - 1)
743 #define EXT_MAX_EXTENT(__hdr__) \
744 (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->max_entries_count - 1)
745 #define EXT_MAX_INDEX(__hdr__) \
746 (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->max_entries_count - 1)
748 #define EXT4_EXTENT_TAIL_OFFSET(hdr) \
749 (sizeof(struct ext4_extent_header) + \
750 (sizeof(struct ext4_extent) * (hdr)->max_entries_count))
753 * ext4_ext_next_allocated_block:
754 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
755 * NOTE: it considers block number from index entry as
756 * allocated block. Thus, index entries have to be consistent
759 #define EXT_MAX_BLOCKS (ext4_lblk_t) (-1)
761 #define IN_RANGE(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
764 /******************************************************************************/
766 /* EXT3 HTree directory indexing */
767 #define EXT2_HTREE_LEGACY 0
768 #define EXT2_HTREE_HALF_MD4 1
769 #define EXT2_HTREE_TEA 2
770 #define EXT2_HTREE_LEGACY_UNSIGNED 3
771 #define EXT2_HTREE_HALF_MD4_UNSIGNED 4
772 #define EXT2_HTREE_TEA_UNSIGNED 5
774 #define EXT2_HTREE_EOF 0x7FFFFFFFUL
776 struct ext4_hash_info {
779 uint32_t hash_version;
780 const uint32_t *seed;
783 /* Extended Attribute(EA) */
785 /* Magic value in attribute blocks */
786 #define EXT4_XATTR_MAGIC 0xEA020000
788 /* Maximum number of references to one attribute block */
789 #define EXT4_XATTR_REFCOUNT_MAX 1024
792 #define EXT4_XATTR_INDEX_USER 1
793 #define EXT4_XATTR_INDEX_POSIX_ACL_ACCESS 2
794 #define EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT 3
795 #define EXT4_XATTR_INDEX_TRUSTED 4
796 #define EXT4_XATTR_INDEX_LUSTRE 5
797 #define EXT4_XATTR_INDEX_SECURITY 6
798 #define EXT4_XATTR_INDEX_SYSTEM 7
799 #define EXT4_XATTR_INDEX_RICHACL 8
800 #define EXT4_XATTR_INDEX_ENCRYPTION 9
802 struct ext4_xattr_header {
803 uint32_t h_magic; /* magic number for identification */
804 uint32_t h_refcount; /* reference count */
805 uint32_t h_blocks; /* number of disk blocks used */
806 uint32_t h_hash; /* hash value of all attributes */
807 uint32_t h_checksum; /* crc32c(uuid+id+xattrblock) */
808 /* id = inum if refcount=1, blknum otherwise */
809 uint32_t h_reserved[3]; /* zero right now */
810 } __attribute__((packed));
812 struct ext4_xattr_ibody_header {
813 uint32_t h_magic; /* magic number for identification */
814 } __attribute__((packed));
816 struct ext4_xattr_entry {
817 uint8_t e_name_len; /* length of name */
818 uint8_t e_name_index; /* attribute name index */
819 uint16_t e_value_offs; /* offset in disk block of value */
820 uint32_t e_value_block; /* disk block attribute is stored on (n/i) */
821 uint32_t e_value_size; /* size of attribute value */
822 uint32_t e_hash; /* hash value of name and value */
823 } __attribute__((packed));
825 struct ext4_xattr_item {
832 RB_ENTRY(ext4_xattr_item) node;
835 struct ext4_xattr_ref {
837 struct ext4_block block;
838 struct ext4_inode_ref *inode_ref;
844 struct ext4_xattr_item *iter_from;
846 RB_HEAD(ext4_xattr_tree,
847 ext4_xattr_item) root;
850 #define EXT4_XATTR_ITERATE_CONT 0
851 #define EXT4_XATTR_ITERATE_STOP 1
852 #define EXT4_XATTR_ITERATE_PAUSE 2
854 #define EXT4_GOOD_OLD_INODE_SIZE 128
856 #define EXT4_XATTR_PAD_BITS 2
857 #define EXT4_XATTR_PAD (1<<EXT4_XATTR_PAD_BITS)
858 #define EXT4_XATTR_ROUND (EXT4_XATTR_PAD-1)
859 #define EXT4_XATTR_LEN(name_len) \
860 (((name_len) + EXT4_XATTR_ROUND + \
861 sizeof(struct ext4_xattr_entry)) & ~EXT4_XATTR_ROUND)
862 #define EXT4_XATTR_NEXT(entry) \
863 ((struct ext4_xattr_entry *)( \
864 (char *)(entry) + EXT4_XATTR_LEN((entry)->e_name_len)))
865 #define EXT4_XATTR_SIZE(size) \
866 (((size) + EXT4_XATTR_ROUND) & ~EXT4_XATTR_ROUND)
867 #define EXT4_XATTR_NAME(entry) \
868 ((char *)((entry) + 1))
870 #define EXT4_XATTR_IHDR(raw_inode) \
871 ((struct ext4_xattr_ibody_header *) \
872 ((char *)raw_inode + \
873 EXT4_GOOD_OLD_INODE_SIZE + \
874 (raw_inode)->extra_isize))
875 #define EXT4_XATTR_IFIRST(hdr) \
876 ((struct ext4_xattr_entry *)((hdr)+1))
878 #define EXT4_XATTR_BHDR(block) \
879 ((struct ext4_xattr_header *)((block)->data))
880 #define EXT4_XATTR_ENTRY(ptr) \
881 ((struct ext4_xattr_entry *)(ptr))
882 #define EXT4_XATTR_BFIRST(block) \
883 EXT4_XATTR_ENTRY(EXT4_XATTR_BHDR(block)+1)
884 #define EXT4_XATTR_IS_LAST_ENTRY(entry) \
885 (*(uint32_t *)(entry) == 0)
887 #define EXT4_ZERO_XATTR_VALUE ((void *)-1)
889 /*****************************************************************************/
891 #ifdef CONFIG_BIG_ENDIAN
892 static inline uint64_t to_le64(uint64_t n)
894 return ((n & 0xff) << 56) | ((n & 0xff00) << 40) |
895 ((n & 0xff0000) << 24) | ((n & 0xff000000LL) << 8) |
896 ((n & 0xff00000000LL) >> 8) | ((n & 0xff0000000000LL) >> 24) |
897 ((n & 0xff000000000000LL) >> 40) |
898 ((n & 0xff00000000000000LL) >> 56);
901 static inline uint32_t to_le32(uint32_t n)
903 return ((n & 0xff) << 24) | ((n & 0xff00) << 8) |
904 ((n & 0xff0000) >> 8) | ((n & 0xff000000) >> 24);
907 static inline uint16_t to_le16(uint16_t n)
909 return ((n & 0xff) << 8) | ((n & 0xff00) >> 8);
913 #define to_le64(_n) _n
914 #define to_le32(_n) _n
915 #define to_le16(_n) _n
918 /****************************Access macros to ext4 structures*****************/
920 #define ext4_get32(s, f) to_le32((s)->f)
921 #define ext4_get16(s, f) to_le16((s)->f)
922 #define ext4_get8(s, f) (s)->f
924 #define ext4_set32(s, f, v) \
926 (s)->f = to_le32(v); \
928 #define ext4_set16(s, f, v) \
930 (s)->f = to_le16(v); \
933 (s, f, v) do { (s)->f = (v); } \
936 #endif /* EXT4_TYPES_H_ */