Add crc32c routine, update block group descriptor
[lwext4.git] / lwext4 / ext4_types.h
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
34 /** @addtogroup lwext4
35  * @{
36  */
37 /**
38  * @file  ext4_types.h
39  * @brief Ext4 data structure definitions.
40  */
41
42 #ifndef EXT4_TYPES_H_
43 #define EXT4_TYPES_H_
44
45 #include <ext4_config.h>
46 #include <ext4_blockdev.h>
47
48 #include <stdint.h>
49
50
51 /*
52  * Structure of the super block
53  */
54 struct ext4_sblock {
55     uint32_t inodes_count;              /* I-nodes count */
56     uint32_t blocks_count_lo;           /* Blocks count */
57     uint32_t reserved_blocks_count_lo;  /* Reserved blocks count */
58     uint32_t free_blocks_count_lo;      /* Free blocks count */
59     uint32_t free_inodes_count;         /* Free inodes count */
60     uint32_t first_data_block;          /* First Data Block */
61     uint32_t log_block_size;            /* Block size */
62     uint32_t log_frag_size;             /* Obsoleted fragment size */
63     uint32_t blocks_per_group;          /* Number of blocks per group */
64     uint32_t frags_per_group;           /* Obsoleted fragments per group */
65     uint32_t inodes_per_group;          /* Number of inodes per group */
66     uint32_t mount_time;                /* Mount time */
67     uint32_t write_time;                /* Write time */
68     uint16_t mount_count;               /* Mount count */
69     uint16_t max_mount_count;           /* Maximal mount count */
70     uint16_t magic;                     /* Magic signature */
71     uint16_t state;                     /* File system state */
72     uint16_t errors;                    /* Behaviour when detecting errors */
73     uint16_t minor_rev_level;           /* Minor revision level */
74     uint32_t last_check_time;           /* Time of last check */
75     uint32_t check_interval;            /* Maximum time between checks */
76     uint32_t creator_os;                /* Creator OS */
77     uint32_t rev_level;                 /* Revision level */
78     uint16_t def_resuid;                /* Default uid for reserved blocks */
79     uint16_t def_resgid;                /* Default gid for reserved blocks */
80
81     /* Fields for EXT4_DYNAMIC_REV superblocks only. */
82     uint32_t first_inode;             /* First non-reserved inode */
83     uint16_t inode_size;              /* Size of inode structure */
84     uint16_t block_group_index;       /* Block group index of this superblock */
85     uint32_t features_compatible;     /* Compatible feature set */
86     uint32_t features_incompatible;   /* Incompatible feature set */
87     uint32_t features_read_only;      /* Readonly-compatible feature set */
88     uint8_t uuid[16];                 /* 128-bit uuid for volume */
89     char volume_name[16];             /* Volume name */
90     char last_mounted[64];            /* Directory where last mounted */
91     uint32_t algorithm_usage_bitmap;  /* For compression */
92
93     /*
94      * Performance hints. Directory preallocation should only
95      * happen if the EXT4_FEATURE_COMPAT_DIR_PREALLOC flag is on.
96      */
97     uint8_t s_prealloc_blocks;       /* Number of blocks to try to preallocate */
98     uint8_t s_prealloc_dir_blocks;   /* Number to preallocate for dirs */
99     uint16_t s_reserved_gdt_blocks;  /* Per group desc for online growth */
100
101     /*
102      * Journaling support valid if EXT4_FEATURE_COMPAT_HAS_JOURNAL set.
103      */
104     uint8_t journal_uuid[16];       /* UUID of journal superblock */
105     uint32_t journal_inode_number;  /* Inode number of journal file */
106     uint32_t journal_dev;           /* Device number of journal file */
107     uint32_t last_orphan;           /* Head of list of inodes to delete */
108     uint32_t hash_seed[4];          /* HTREE hash seed */
109     uint8_t default_hash_version;   /* Default hash version to use */
110     uint8_t journal_backup_type;
111     uint16_t desc_size;             /* Size of group descriptor */
112     uint32_t default_mount_opts;    /* Default mount options */
113     uint32_t first_meta_bg;         /* First metablock block group */
114     uint32_t mkfs_time;             /* When the filesystem was created */
115     uint32_t journal_blocks[17];    /* Backup of the journal inode */
116
117     /* 64bit support valid if EXT4_FEATURE_COMPAT_64BIT */
118     uint32_t blocks_count_hi;           /* Blocks count */
119     uint32_t reserved_blocks_count_hi;  /* Reserved blocks count */
120     uint32_t free_blocks_count_hi;      /* Free blocks count */
121     uint16_t min_extra_isize;           /* All inodes have at least # bytes */
122     uint16_t want_extra_isize;          /* New inodes should reserve # bytes */
123     uint32_t flags;                     /* Miscellaneous flags */
124     uint16_t raid_stride;               /* RAID stride */
125     uint16_t mmp_interval;              /* # seconds to wait in MMP checking */
126     uint64_t mmp_block;                 /* Block for multi-mount protection */
127     uint32_t raid_stripe_width;         /* Blocks on all data disks (N * stride) */
128     uint8_t log_groups_per_flex;        /* FLEX_BG group size */
129     uint8_t reserved_char_pad;
130     uint16_t reserved_pad;
131     uint64_t kbytes_written;            /* Number of lifetime kilobytes written */
132     uint32_t snapshot_inum;             /* I-node number of active snapshot */
133     uint32_t snapshot_id;               /* Sequential ID of active snapshot */
134     uint64_t snapshot_r_blocks_count;   /* Reserved blocks for active snapshot's future use */
135     uint32_t snapshot_list;             /* I-node number of the head of the on-disk snapshot list */
136     uint32_t error_count;               /* Number of file system errors */
137     uint32_t first_error_time;          /* First time an error happened */
138     uint32_t first_error_ino;           /* I-node involved in first error */
139     uint64_t first_error_block;         /* Block involved of first error */
140     uint8_t first_error_func[32];       /* Function where the error happened */
141     uint32_t first_error_line;          /* Line number where error happened */
142     uint32_t last_error_time;           /* Most recent time of an error */
143     uint32_t last_error_ino;            /* I-node involved in last error */
144     uint32_t last_error_line;           /* Line number where error happened */
145     uint64_t last_error_block;          /* Block involved of last error */
146     uint8_t last_error_func[32];        /* Function where the error happened */
147     uint8_t mount_opts[64];
148     uint32_t padding[112];              /* Padding to the end of the block */
149 } __attribute__((packed));
150
151
152 #define EXT4_SUPERBLOCK_MAGIC   0xEF53
153 #define EXT4_SUPERBLOCK_SIZE    1024
154 #define EXT4_SUPERBLOCK_OFFSET  1024
155
156 #define EXT4_SUPERBLOCK_OS_LINUX  0
157 #define EXT4_SUPERBLOCK_OS_HURD   1
158
159 /*
160  * Misc. filesystem flags
161  */
162 #define EXT4_SUPERBLOCK_FLAGS_SIGNED_HASH    0x0001  /* Signed dirhash in use */
163 #define EXT4_SUPERBLOCK_FLAGS_UNSIGNED_HASH  0x0002  /* Unsigned dirhash in use */
164 #define EXT4_SUPERBLOCK_FLAGS_TEST_FILESYS   0x0004  /* to test development code */
165
166 /*
167  * Filesystem states
168  */
169 #define EXT4_SUPERBLOCK_STATE_VALID_FS   0x0001  /* Unmounted cleanly */
170 #define EXT4_SUPERBLOCK_STATE_ERROR_FS   0x0002  /* Errors detected */
171 #define EXT4_SUPERBLOCK_STATE_ORPHAN_FS  0x0004  /* Orphans being recovered */
172
173 /*
174  * Behaviour when errors detected
175  */
176 #define EXT4_SUPERBLOCK_ERRORS_CONTINUE  1  /* Continue execution */
177 #define EXT4_SUPERBLOCK_ERRORS_RO        2  /* Remount fs read-only */
178 #define EXT4_SUPERBLOCK_ERRORS_PANIC     3  /* Panic */
179 #define EXT4_SUPERBLOCK_ERRORS_DEFAULT   EXT4_ERRORS_CONTINUE
180
181 /*
182  * Compatible features
183  */
184 #define EXT4_FEATURE_COMPAT_DIR_PREALLOC   0x0001
185 #define EXT4_FEATURE_COMPAT_IMAGIC_INODES  0x0002
186 #define EXT4_FEATURE_COMPAT_HAS_JOURNAL    0x0004
187 #define EXT4_FEATURE_COMPAT_EXT_ATTR       0x0008
188 #define EXT4_FEATURE_COMPAT_RESIZE_INODE   0x0010
189 #define EXT4_FEATURE_COMPAT_DIR_INDEX      0x0020
190
191
192 /*
193  * Read-only compatible features
194  */
195 #define EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER  0x0001
196 #define EXT4_FEATURE_RO_COMPAT_LARGE_FILE    0x0002
197 #define EXT4_FEATURE_RO_COMPAT_BTREE_DIR     0x0004
198 #define EXT4_FEATURE_RO_COMPAT_HUGE_FILE     0x0008
199 #define EXT4_FEATURE_RO_COMPAT_GDT_CSUM      0x0010
200 #define EXT4_FEATURE_RO_COMPAT_DIR_NLINK     0x0020
201 #define EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE   0x0040
202 #define EXT4_FEATURE_RO_COMPAT_QUOTA         0x0100
203 #define EXT4_FEATURE_RO_COMPAT_BIGALLOC      0x0200
204 #define EXT4_FEATURE_RO_COMPAT_METADATA_CSUM    0x0400
205
206 /*
207  * Incompatible features
208  */
209 #define EXT4_FEATURE_INCOMPAT_COMPRESSION  0x0001
210 #define EXT4_FEATURE_INCOMPAT_FILETYPE     0x0002
211 #define EXT4_FEATURE_INCOMPAT_RECOVER      0x0004  /* Needs recovery */
212 #define EXT4_FEATURE_INCOMPAT_JOURNAL_DEV  0x0008  /* Journal device */
213 #define EXT4_FEATURE_INCOMPAT_META_BG      0x0010
214 #define EXT4_FEATURE_INCOMPAT_EXTENTS      0x0040  /* extents support */
215 #define EXT4_FEATURE_INCOMPAT_64BIT        0x0080
216 #define EXT4_FEATURE_INCOMPAT_MMP          0x0100
217 #define EXT4_FEATURE_INCOMPAT_FLEX_BG      0x0200
218 #define EXT4_FEATURE_INCOMPAT_EA_INODE     0x0400  /* EA in inode */
219 #define EXT4_FEATURE_INCOMPAT_DIRDATA      0x1000  /* data in dirent */
220 #define EXT4_FEATURE_INCOMPAT_BG_USE_META_CSUM  0x2000 /* use crc32c for bg */
221 #define EXT4_FEATURE_INCOMPAT_LARGEDIR          0x4000 /* >2GB or 3-lvl htree */
222 #define EXT4_FEATURE_INCOMPAT_INLINE_DATA       0x8000 /* data in inode */
223
224
225 #define EXT4_FEATURE_COMPAT_SUPP  (EXT4_FEATURE_COMPAT_DIR_INDEX)
226
227 #define EXT4_FEATURE_INCOMPAT_SUPP \
228         (EXT4_FEATURE_INCOMPAT_FILETYPE | \
229                 EXT4_FEATURE_INCOMPAT_EXTENTS | \
230                 EXT4_FEATURE_INCOMPAT_64BIT)
231
232 #define EXT4_FEATURE_RO_COMPAT_SUPP \
233         (EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER | \
234                 EXT4_FEATURE_RO_COMPAT_DIR_NLINK | \
235                 EXT4_FEATURE_RO_COMPAT_HUGE_FILE | \
236                 EXT4_FEATURE_RO_COMPAT_LARGE_FILE | \
237                 EXT4_FEATURE_RO_COMPAT_GDT_CSUM | \
238                 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)
239
240 struct ext4_fs {
241     struct ext4_blockdev*bdev;
242     struct ext4_sblock sb;
243
244     uint64_t inode_block_limits[4];
245     uint64_t inode_blocks_per_level[4];
246
247     uint32_t last_inode_bg_id;
248 };
249
250
251 #define EXT4_BLOCK_GROUP_INODE_UNINIT   0x0001  /* Inode table/bitmap not in use */
252 #define EXT4_BLOCK_GROUP_BLOCK_UNINIT   0x0002  /* Block bitmap not in use */
253 #define EXT4_BLOCK_GROUP_ITABLE_ZEROED  0x0004  /* On-disk itable initialized to zero */
254
255 /*
256  * Structure of a blocks group descriptor
257  */
258 struct ext4_bgroup {
259     uint32_t block_bitmap_lo;             /* Blocks bitmap block */
260     uint32_t inode_bitmap_lo;             /* Inodes bitmap block */
261     uint32_t inode_table_first_block_lo;  /* Inodes table block */
262     uint16_t free_blocks_count_lo;        /* Free blocks count */
263     uint16_t free_inodes_count_lo;        /* Free inodes count */
264     uint16_t used_dirs_count_lo;          /* Directories count */
265     uint16_t flags;                       /* EXT4_BG_flags (INODE_UNINIT, etc) */
266     uint32_t exclude_bitmap_lo;           /* Exclude bitmap for snapshots */
267         uint16_t block_bitmap_csum_lo;        /* crc32c(s_uuid+grp_num+bbitmap) LE */
268         uint16_t inode_bitmap_csum_lo;        /* crc32c(s_uuid+grp_num+ibitmap) LE */
269     uint16_t itable_unused_lo;            /* Unused inodes count */
270     uint16_t checksum;                    /* crc16(sb_uuid+group+desc) */
271
272     uint32_t block_bitmap_hi;             /* Blocks bitmap block MSB */
273     uint32_t inode_bitmap_hi;             /* I-nodes bitmap block MSB */
274     uint32_t inode_table_first_block_hi;  /* I-nodes table block MSB */
275     uint16_t free_blocks_count_hi;        /* Free blocks count MSB */
276     uint16_t free_inodes_count_hi;        /* Free i-nodes count MSB */
277     uint16_t used_dirs_count_hi;          /* Directories count MSB */
278     uint16_t itable_unused_hi;            /* Unused inodes count MSB */
279     uint32_t exclude_bitmap_hi;           /* Exclude bitmap block MSB */
280         uint16_t block_bitmap_csum_hi;        /* crc32c(s_uuid+grp_num+bbitmap) BE */
281         uint16_t inode_bitmap_csum_hi;        /* crc32c(s_uuid+grp_num+ibitmap) BE */
282         uint32_t reserved;                    /* Padding */
283 } ;
284
285 struct ext4_block_group_ref {
286     struct ext4_block block;
287     struct ext4_bgroup *block_group;
288     struct ext4_fs *fs;
289     uint32_t index;
290     bool dirty;
291 };
292
293 #define EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE  32
294 #define EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE  64
295
296 #define EXT4_MIN_BLOCK_SIZE   1024   /* 1 KiB */
297 #define EXT4_MAX_BLOCK_SIZE   65536  /* 64 KiB */
298 #define EXT4_REV0_INODE_SIZE  128
299
300 #define EXT4_INODE_BLOCK_SIZE  512
301
302 #define EXT4_INODE_DIRECT_BLOCK_COUNT      12
303 #define EXT4_INODE_INDIRECT_BLOCK          EXT4_INODE_DIRECT_BLOCK_COUNT
304 #define EXT4_INODE_DOUBLE_INDIRECT_BLOCK   (EXT4_INODE_INDIRECT_BLOCK + 1)
305 #define EXT4_INODE_TRIPPLE_INDIRECT_BLOCK  (EXT4_INODE_DOUBLE_INDIRECT_BLOCK + 1)
306 #define EXT4_INODE_BLOCKS                  (EXT4_INODE_TRIPPLE_INDIRECT_BLOCK + 1)
307 #define EXT4_INODE_INDIRECT_BLOCK_COUNT    (EXT4_INODE_BLOCKS - EXT4_INODE_DIRECT_BLOCK_COUNT)
308
309 /*
310  * Structure of an inode on the disk
311  */
312 struct ext4_inode {
313     uint16_t mode;                       /* File mode */
314     uint16_t uid;                        /* Low 16 bits of owner uid */
315     uint32_t size_lo;                    /* Size in bytes */
316     uint32_t access_time;                /* Access time */
317     uint32_t change_inode_time;          /* I-node change time */
318     uint32_t modification_time;          /* Modification time */
319     uint32_t deletion_time;              /* Deletion time */
320     uint16_t gid;                        /* Low 16 bits of group id */
321     uint16_t links_count;                /* Links count */
322     uint32_t blocks_count_lo;            /* Blocks count */
323     uint32_t flags;                      /* File flags */
324     uint32_t unused_osd1;                /* OS dependent - not used in HelenOS */
325     uint32_t blocks[EXT4_INODE_BLOCKS];  /* Pointers to blocks */
326     uint32_t generation;                 /* File version (for NFS) */
327     uint32_t file_acl_lo;                /* File ACL */
328     uint32_t size_hi;
329     uint32_t obso_faddr;                 /* Obsoleted fragment address */
330
331     union {
332         struct {
333             uint16_t blocks_high;
334             uint16_t file_acl_high;
335             uint16_t uid_high;
336             uint16_t gid_high;
337             uint32_t reserved2;
338         } linux2;
339         struct {
340             uint16_t reserved1;
341             uint16_t mode_high;
342             uint16_t uid_high;
343             uint16_t gid_high;
344             uint32_t author;
345         } hurd2;
346     } __attribute__ ((packed)) osd2;
347
348     uint16_t extra_isize;
349     uint16_t pad1;
350     uint32_t ctime_extra;   /* Extra change time (nsec << 2 | epoch) */
351     uint32_t mtime_extra;   /* Extra Modification time (nsec << 2 | epoch) */
352     uint32_t atime_extra;   /* Extra Access time (nsec << 2 | epoch) */
353     uint32_t crtime;        /* File creation time */
354     uint32_t crtime_extra;  /* Extra file creation time (nsec << 2 | epoch) */
355     uint32_t version_hi;    /* High 32 bits for 64-bit version */
356 } __attribute__ ((packed)) ;
357
358 #define EXT4_INODE_MODE_FIFO       0x1000
359 #define EXT4_INODE_MODE_CHARDEV    0x2000
360 #define EXT4_INODE_MODE_DIRECTORY  0x4000
361 #define EXT4_INODE_MODE_BLOCKDEV   0x6000
362 #define EXT4_INODE_MODE_FILE       0x8000
363 #define EXT4_INODE_MODE_SOFTLINK   0xA000
364 #define EXT4_INODE_MODE_SOCKET     0xC000
365 #define EXT4_INODE_MODE_TYPE_MASK  0xF000
366
367 /*
368  * Inode flags
369  */
370 #define EXT4_INODE_FLAG_SECRM      0x00000001  /* Secure deletion */
371 #define EXT4_INODE_FLAG_UNRM       0x00000002  /* Undelete */
372 #define EXT4_INODE_FLAG_COMPR      0x00000004  /* Compress file */
373 #define EXT4_INODE_FLAG_SYNC       0x00000008  /* Synchronous updates */
374 #define EXT4_INODE_FLAG_IMMUTABLE  0x00000010  /* Immutable file */
375 #define EXT4_INODE_FLAG_APPEND     0x00000020  /* writes to file may only append */
376 #define EXT4_INODE_FLAG_NODUMP     0x00000040  /* do not dump file */
377 #define EXT4_INODE_FLAG_NOATIME    0x00000080  /* do not update atime */
378
379 /* Compression flags */
380 #define EXT4_INODE_FLAG_DIRTY     0x00000100
381 #define EXT4_INODE_FLAG_COMPRBLK  0x00000200  /* One or more compressed clusters */
382 #define EXT4_INODE_FLAG_NOCOMPR   0x00000400  /* Don't compress */
383 #define EXT4_INODE_FLAG_ECOMPR    0x00000800  /* Compression error */
384
385 #define EXT4_INODE_FLAG_INDEX         0x00001000  /* hash-indexed directory */
386 #define EXT4_INODE_FLAG_IMAGIC        0x00002000  /* AFS directory */
387 #define EXT4_INODE_FLAG_JOURNAL_DATA  0x00004000  /* File data should be journaled */
388 #define EXT4_INODE_FLAG_NOTAIL        0x00008000  /* File tail should not be merged */
389 #define EXT4_INODE_FLAG_DIRSYNC       0x00010000  /* Dirsync behaviour (directories only) */
390 #define EXT4_INODE_FLAG_TOPDIR        0x00020000  /* Top of directory hierarchies */
391 #define EXT4_INODE_FLAG_HUGE_FILE     0x00040000  /* Set to each huge file */
392 #define EXT4_INODE_FLAG_EXTENTS       0x00080000  /* Inode uses extents */
393 #define EXT4_INODE_FLAG_EA_INODE      0x00200000  /* Inode used for large EA */
394 #define EXT4_INODE_FLAG_EOFBLOCKS     0x00400000  /* Blocks allocated beyond EOF */
395 #define EXT4_INODE_FLAG_RESERVED      0x80000000  /* reserved for ext4 lib */
396
397 #define EXT4_INODE_ROOT_INDEX  2
398
399 struct ext4_inode_ref {
400     struct ext4_block  block;
401     struct ext4_inode *inode;
402     struct ext4_fs  *fs;
403     uint32_t index;
404     bool dirty;
405 } ;
406
407
408 #define EXT4_DIRECTORY_FILENAME_LEN  255
409
410 #define EXT4_DIRECTORY_FILETYPE_UNKNOWN   0
411 #define EXT4_DIRECTORY_FILETYPE_REG_FILE  1
412 #define EXT4_DIRECTORY_FILETYPE_DIR       2
413 #define EXT4_DIRECTORY_FILETYPE_CHRDEV    3
414 #define EXT4_DIRECTORY_FILETYPE_BLKDEV    4
415 #define EXT4_DIRECTORY_FILETYPE_FIFO      5
416 #define EXT4_DIRECTORY_FILETYPE_SOCK      6
417 #define EXT4_DIRECTORY_FILETYPE_SYMLINK   7
418
419 union ext4_directory_entry_ll_internal{
420     uint8_t name_length_high;  /* Higher 8 bits of name length */
421     uint8_t inode_type;        /* Type of referenced inode (in rev >= 0.5) */
422 } __attribute__ ((packed));
423
424 /**
425  * Linked list directory entry structure
426  */
427 struct ext4_directory_entry_ll {
428     uint32_t inode;         /* I-node for the entry */
429     uint16_t entry_length;  /* Distance to the next directory entry */
430     uint8_t name_length;    /* Lower 8 bits of name length */
431
432     union ext4_directory_entry_ll_internal in;
433
434     uint8_t name[EXT4_DIRECTORY_FILENAME_LEN];  /* Entry name */
435 } __attribute__((packed)) ;
436
437 struct ext4_directory_iterator {
438     struct ext4_inode_ref *inode_ref;
439     struct ext4_block current_block;
440     uint64_t current_offset;
441     struct ext4_directory_entry_ll  *current;
442 };
443
444 struct ext4_directory_search_result {
445     struct ext4_block block;
446     struct ext4_directory_entry_ll *dentry;
447 };
448
449 /* Structures for indexed directory */
450
451 struct ext4_directory_dx_countlimit {
452     uint16_t limit;
453     uint16_t count;
454 } ;
455
456 struct ext4_directory_dx_dot_entry {
457     uint32_t inode;
458     uint16_t entry_length;
459     uint8_t name_length;
460     uint8_t inode_type;
461     uint8_t name[4];
462 } ;
463
464 struct ext4_directory_dx_root_info {
465     uint32_t reserved_zero;
466     uint8_t hash_version;
467     uint8_t info_length;
468     uint8_t indirect_levels;
469     uint8_t unused_flags;
470 } ;
471
472 struct ext4_directory_dx_entry {
473     uint32_t hash;
474     uint32_t block;
475 } ;
476
477 struct ext4_directory_dx_root {
478     struct ext4_directory_dx_dot_entry dots[2];
479     struct ext4_directory_dx_root_info info;
480     struct ext4_directory_dx_entry entries[];
481 };
482
483 struct ext4_fake_directory_entry {
484     uint32_t inode;
485     uint16_t entry_length;
486     uint8_t name_length;
487     uint8_t inode_type;
488 };
489
490 struct ext4_directory_dx_node {
491     struct ext4_fake_directory_entry fake;
492     struct ext4_directory_dx_entry entries[];
493 };
494
495 struct ext4_directory_dx_block {
496     struct ext4_block block;
497     struct ext4_directory_dx_entry *entries;
498     struct ext4_directory_dx_entry *position;
499 } ;
500
501 #define EXT4_ERR_BAD_DX_DIR       (-25000)
502
503
504 /*
505  * This is the extent on-disk structure.
506  * It's used at the bottom of the tree.
507  */
508 struct ext4_extent {
509     uint32_t first_block;  /* First logical block extent covers */
510     uint16_t block_count;  /* Number of blocks covered by extent */
511     uint16_t start_hi;     /* High 16 bits of physical block */
512     uint32_t start_lo;     /* Low 32 bits of physical block */
513 } ;
514
515 /*
516  * This is index on-disk structure.
517  * It's used at all the levels except the bottom.
518  */
519 struct ext4_extent_index {
520     uint32_t first_block;  /* Index covers logical blocks from 'block' */
521
522     /**
523      * Pointer to the physical block of the next
524      * level. leaf or next index could be there
525      * high 16 bits of physical block
526      */
527     uint32_t leaf_lo;
528     uint16_t leaf_hi;
529     uint16_t padding;
530 } ;
531
532 /*
533  * Each block (leaves and indexes), even inode-stored has header.
534  */
535 struct ext4_extent_header {
536     uint16_t magic;
537     uint16_t entries_count;      /* Number of valid entries */
538     uint16_t max_entries_count;  /* Capacity of store in entries */
539     uint16_t depth;              /* Has tree real underlying blocks? */
540     uint32_t generation;         /* generation of the tree */
541 } ;
542
543 struct ext4_extent_path {
544     struct ext4_block block;
545     uint16_t depth;
546     struct ext4_extent_header *header;
547     struct ext4_extent_index *index;
548     struct ext4_extent *extent;
549 } ;
550
551 #define EXT4_EXTENT_MAGIC  0xF30A
552
553 #define EXT4_EXTENT_FIRST(header) \
554         ((struct ext4_extent *) (((char *) (header)) + sizeof(struct ext4_extent_header)))
555
556 #define EXT4_EXTENT_FIRST_INDEX(header) \
557         ((struct ext4_extent_index *) (((char *) (header)) + sizeof(struct ext4_extent_header)))
558
559
560 /* EXT3 HTree directory indexing */
561 #define EXT2_HTREE_LEGACY                   0
562 #define EXT2_HTREE_HALF_MD4                 1
563 #define EXT2_HTREE_TEA                      2
564 #define EXT2_HTREE_LEGACY_UNSIGNED          3
565 #define EXT2_HTREE_HALF_MD4_UNSIGNED        4
566 #define EXT2_HTREE_TEA_UNSIGNED             5
567
568 #define EXT2_HTREE_EOF                      0x7FFFFFFFUL
569
570
571 struct ext4_hash_info {
572     uint32_t hash;
573     uint32_t minor_hash;
574     uint32_t hash_version;
575     const uint32_t *seed;
576 };
577
578 /*****************************************************************************/
579
580
581 #ifdef CONFIG_BIG_ENDIAN
582 static inline uint64_t to_le64(uint64_t n)
583 {
584     return  ((n & 0xff) << 56) |
585             ((n & 0xff00) << 40) |
586             ((n & 0xff0000) << 24) |
587             ((n & 0xff000000LL) << 8) |
588             ((n & 0xff00000000LL) >> 8) |
589             ((n & 0xff0000000000LL) >> 24) |
590             ((n & 0xff000000000000LL) >> 40) |
591             ((n & 0xff00000000000000LL) >> 56);
592 }
593
594 static inline uint32_t to_le32(uint32_t n)
595 {
596     return  ((n & 0xff) << 24) |
597             ((n & 0xff00) << 8) |
598             ((n & 0xff0000) >> 8) |
599             ((n & 0xff000000) >> 24);
600 }
601
602 static inline uint16_t to_le16(uint16_t n)
603 {
604     return  ((n & 0xff) << 8) |
605             ((n & 0xff00) >> 8);
606 }
607
608
609 #else
610 #define to_le64(_n) _n
611 #define to_le32(_n) _n
612 #define to_le16(_n) _n
613 #endif
614
615 /****************************Access macros to ext4 structures*****************/
616
617 #define ext4_get32(s, f)        to_le32((s)->f)
618 #define ext4_get16(s, f)        to_le16((s)->f)
619 #define ext4_get8(s, f)         (s)->f
620
621
622 #define ext4_set32(s, f, v)     do { (s)->f = to_le32(v); }while(0)
623 #define ext4_set16(s, f, v)     do { (s)->f = to_le16(v); }while(0)
624 #define ext4_set8 (s, f, v)     do { (s)->f = (v);        }while(0)
625
626 #endif /* EXT4_TYPES_H_ */
627
628 /**
629  * @}
630  */