Fix issues with big-endian system
[lwext4.git] / include / 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 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49 #include "ext4_config.h"
50 #include "ext4_blockdev.h"
51 #include "misc/tree.h"
52
53 #include <stddef.h>
54 #include <stdint.h>
55
56 /*
57  * Types of blocks.
58  */
59 typedef uint32_t ext4_lblk_t;
60 typedef uint64_t ext4_fsblk_t;
61
62
63 #define EXT4_CHECKSUM_CRC32C 1
64
65 #define UUID_SIZE 16
66
67 #pragma pack(push, 1)
68
69 /*
70  * Structure of the super block
71  */
72 struct ext4_sblock {
73         uint32_t inodes_count;             /* I-nodes count */
74         uint32_t blocks_count_lo;         /* Blocks count */
75         uint32_t reserved_blocks_count_lo; /* Reserved blocks count */
76         uint32_t free_blocks_count_lo;     /* Free blocks count */
77         uint32_t free_inodes_count;     /* Free inodes count */
78         uint32_t first_data_block;       /* First Data Block */
79         uint32_t log_block_size;           /* Block size */
80         uint32_t log_cluster_size;       /* Obsoleted fragment size */
81         uint32_t blocks_per_group;       /* Number of blocks per group */
82         uint32_t frags_per_group;         /* Obsoleted fragments per group */
83         uint32_t inodes_per_group;       /* Number of inodes per group */
84         uint32_t mount_time;               /* Mount time */
85         uint32_t write_time;               /* Write time */
86         uint16_t mount_count;              /* Mount count */
87         uint16_t max_mount_count;         /* Maximal mount count */
88         uint16_t magic;                    /* Magic signature */
89         uint16_t state;                    /* File system state */
90         uint16_t errors;                   /* Behavior when detecting errors */
91         uint16_t minor_rev_level;         /* Minor revision level */
92         uint32_t last_check_time;         /* Time of last check */
93         uint32_t check_interval;           /* Maximum time between checks */
94         uint32_t creator_os;               /* Creator OS */
95         uint32_t rev_level;                /* Revision level */
96         uint16_t def_resuid;               /* Default uid for reserved blocks */
97         uint16_t def_resgid;               /* Default gid for reserved blocks */
98
99         /* Fields for EXT4_DYNAMIC_REV superblocks only. */
100         uint32_t first_inode;    /* First non-reserved inode */
101         uint16_t inode_size;      /* Size of inode structure */
102         uint16_t block_group_index;   /* Block group index of this superblock */
103         uint32_t features_compatible; /* Compatible feature set */
104         uint32_t features_incompatible;  /* Incompatible feature set */
105         uint32_t features_read_only;     /* Readonly-compatible feature set */
106         uint8_t uuid[UUID_SIZE];                 /* 128-bit uuid for volume */
107         char volume_name[16];            /* Volume name */
108         char last_mounted[64];           /* Directory where last mounted */
109         uint32_t algorithm_usage_bitmap; /* For compression */
110
111         /*
112          * Performance hints. Directory preallocation should only
113          * happen if the EXT4_FEATURE_COMPAT_DIR_PREALLOC flag is on.
114          */
115         uint8_t s_prealloc_blocks; /* Number of blocks to try to preallocate */
116         uint8_t s_prealloc_dir_blocks;  /* Number to preallocate for dirs */
117         uint16_t s_reserved_gdt_blocks; /* Per group desc for online growth */
118
119         /*
120          * Journaling support valid if EXT4_FEATURE_COMPAT_HAS_JOURNAL set.
121          */
122         uint8_t journal_uuid[UUID_SIZE];      /* UUID of journal superblock */
123         uint32_t journal_inode_number; /* Inode number of journal file */
124         uint32_t journal_dev;     /* Device number of journal file */
125         uint32_t last_orphan;     /* Head of list of inodes to delete */
126         uint32_t hash_seed[4];   /* HTREE hash seed */
127         uint8_t default_hash_version;  /* Default hash version to use */
128         uint8_t journal_backup_type;
129         uint16_t desc_size;       /* Size of group descriptor */
130         uint32_t default_mount_opts; /* Default mount options */
131         uint32_t first_meta_bg;      /* First metablock block group */
132         uint32_t mkfs_time;       /* When the filesystem was created */
133         uint32_t journal_blocks[17]; /* Backup of the journal inode */
134
135         /* 64bit support valid if EXT4_FEATURE_COMPAT_64BIT */
136         uint32_t blocks_count_hi;         /* Blocks count */
137         uint32_t reserved_blocks_count_hi; /* Reserved blocks count */
138         uint32_t free_blocks_count_hi;     /* Free blocks count */
139         uint16_t min_extra_isize;    /* All inodes have at least # bytes */
140         uint16_t want_extra_isize;   /* New inodes should reserve # bytes */
141         uint32_t flags;              /* Miscellaneous flags */
142         uint16_t raid_stride;   /* RAID stride */
143         uint16_t mmp_interval;       /* # seconds to wait in MMP checking */
144         uint64_t mmp_block;       /* Block for multi-mount protection */
145         uint32_t raid_stripe_width;  /* Blocks on all data disks (N * stride) */
146         uint8_t log_groups_per_flex; /* FLEX_BG group size */
147         uint8_t checksum_type;
148         uint16_t reserved_pad;
149         uint64_t kbytes_written; /* Number of lifetime kilobytes written */
150         uint32_t snapshot_inum;  /* I-node number of active snapshot */
151         uint32_t snapshot_id;    /* Sequential ID of active snapshot */
152         uint64_t
153             snapshot_r_blocks_count; /* Reserved blocks for active snapshot's
154                                         future use */
155         uint32_t
156             snapshot_list; /* I-node number of the head of the on-disk snapshot
157                               list */
158         uint32_t error_count;    /* Number of file system errors */
159         uint32_t first_error_time;    /* First time an error happened */
160         uint32_t first_error_ino;     /* I-node involved in first error */
161         uint64_t first_error_block;   /* Block involved of first error */
162         uint8_t first_error_func[32]; /* Function where the error happened */
163         uint32_t first_error_line;    /* Line number where error happened */
164         uint32_t last_error_time;     /* Most recent time of an error */
165         uint32_t last_error_ino;      /* I-node involved in last error */
166         uint32_t last_error_line;     /* Line number where error happened */
167         uint64_t last_error_block;    /* Block involved of last error */
168         uint8_t last_error_func[32];  /* Function where the error happened */
169         uint8_t mount_opts[64];
170         uint32_t usr_quota_inum;        /* inode for tracking user quota */
171         uint32_t grp_quota_inum;        /* inode for tracking group quota */
172         uint32_t overhead_clusters;     /* overhead blocks/clusters in fs */
173         uint32_t backup_bgs[2]; /* groups with sparse_super2 SBs */
174         uint8_t  encrypt_algos[4];      /* Encryption algorithms in use  */
175         uint8_t  encrypt_pw_salt[16];   /* Salt used for string2key algorithm */
176         uint32_t lpf_ino;               /* Location of the lost+found inode */
177         uint32_t padding[100];  /* Padding to the end of the block */
178         uint32_t checksum;              /* crc32c(superblock) */
179 };
180
181 #pragma pack(pop)
182
183 #define EXT4_SUPERBLOCK_MAGIC 0xEF53
184 #define EXT4_SUPERBLOCK_SIZE 1024
185 #define EXT4_SUPERBLOCK_OFFSET 1024
186
187 #define EXT4_SUPERBLOCK_OS_LINUX 0
188 #define EXT4_SUPERBLOCK_OS_HURD 1
189
190 /*
191  * Misc. filesystem flags
192  */
193 #define EXT4_SUPERBLOCK_FLAGS_SIGNED_HASH 0x0001
194 #define EXT4_SUPERBLOCK_FLAGS_UNSIGNED_HASH 0x0002
195 #define EXT4_SUPERBLOCK_FLAGS_TEST_FILESYS 0x0004
196 /*
197  * Filesystem states
198  */
199 #define EXT4_SUPERBLOCK_STATE_VALID_FS 0x0001  /* Unmounted cleanly */
200 #define EXT4_SUPERBLOCK_STATE_ERROR_FS 0x0002  /* Errors detected */
201 #define EXT4_SUPERBLOCK_STATE_ORPHAN_FS 0x0004 /* Orphans being recovered */
202
203 /*
204  * Behaviour when errors detected
205  */
206 #define EXT4_SUPERBLOCK_ERRORS_CONTINUE 1 /* Continue execution */
207 #define EXT4_SUPERBLOCK_ERRORS_RO 2       /* Remount fs read-only */
208 #define EXT4_SUPERBLOCK_ERRORS_PANIC 3    /* Panic */
209 #define EXT4_SUPERBLOCK_ERRORS_DEFAULT EXT4_ERRORS_CONTINUE
210
211 /*
212  * Compatible features
213  */
214 #define EXT4_FCOM_DIR_PREALLOC 0x0001
215 #define EXT4_FCOM_IMAGIC_INODES 0x0002
216 #define EXT4_FCOM_HAS_JOURNAL 0x0004
217 #define EXT4_FCOM_EXT_ATTR 0x0008
218 #define EXT4_FCOM_RESIZE_INODE 0x0010
219 #define EXT4_FCOM_DIR_INDEX 0x0020
220
221 /*
222  * Read-only compatible features
223  */
224 #define EXT4_FRO_COM_SPARSE_SUPER 0x0001
225 #define EXT4_FRO_COM_LARGE_FILE 0x0002
226 #define EXT4_FRO_COM_BTREE_DIR 0x0004
227 #define EXT4_FRO_COM_HUGE_FILE 0x0008
228 #define EXT4_FRO_COM_GDT_CSUM 0x0010
229 #define EXT4_FRO_COM_DIR_NLINK 0x0020
230 #define EXT4_FRO_COM_EXTRA_ISIZE 0x0040
231 #define EXT4_FRO_COM_QUOTA 0x0100
232 #define EXT4_FRO_COM_BIGALLOC 0x0200
233 #define EXT4_FRO_COM_METADATA_CSUM 0x0400
234
235 /*
236  * Incompatible features
237  */
238 #define EXT4_FINCOM_COMPRESSION 0x0001
239 #define EXT4_FINCOM_FILETYPE 0x0002
240 #define EXT4_FINCOM_RECOVER 0x0004     /* Needs recovery */
241 #define EXT4_FINCOM_JOURNAL_DEV 0x0008 /* Journal device */
242 #define EXT4_FINCOM_META_BG 0x0010
243 #define EXT4_FINCOM_EXTENTS 0x0040 /* extents support */
244 #define EXT4_FINCOM_64BIT 0x0080
245 #define EXT4_FINCOM_MMP 0x0100
246 #define EXT4_FINCOM_FLEX_BG 0x0200
247 #define EXT4_FINCOM_EA_INODE 0x0400      /* EA in inode */
248 #define EXT4_FINCOM_DIRDATA 0x1000        /* data in dirent */
249 #define EXT4_FINCOM_BG_USE_META_CSUM 0x2000 /* use crc32c for bg */
250 #define EXT4_FINCOM_LARGEDIR 0x4000      /* >2GB or 3-lvl htree */
251 #define EXT4_FINCOM_INLINE_DATA 0x8000      /* data in inode */
252
253 /*
254  * EXT2 supported feature set
255  */
256 #define EXT2_SUPPORTED_FCOM 0x0000
257
258 #define EXT2_SUPPORTED_FINCOM                                   \
259         (EXT4_FINCOM_FILETYPE | EXT4_FINCOM_META_BG)
260
261 #define EXT2_SUPPORTED_FRO_COM                                  \
262         (EXT4_FRO_COM_SPARSE_SUPER |                            \
263          EXT4_FRO_COM_LARGE_FILE)
264
265 /*
266  * EXT3 supported feature set
267  */
268 #define EXT3_SUPPORTED_FCOM (EXT4_FCOM_DIR_INDEX)
269
270 #define EXT3_SUPPORTED_FINCOM                                 \
271         (EXT4_FINCOM_FILETYPE | EXT4_FINCOM_META_BG)
272
273 #define EXT3_SUPPORTED_FRO_COM                                \
274         (EXT4_FRO_COM_SPARSE_SUPER | EXT4_FRO_COM_LARGE_FILE)
275
276 /*
277  * EXT4 supported feature set
278  */
279 #define EXT4_SUPPORTED_FCOM (EXT4_FCOM_DIR_INDEX)
280
281 #define EXT4_SUPPORTED_FINCOM                              \
282         (EXT4_FINCOM_FILETYPE | EXT4_FINCOM_META_BG |      \
283          EXT4_FINCOM_EXTENTS | EXT4_FINCOM_FLEX_BG |       \
284          EXT4_FINCOM_64BIT)
285
286 #define EXT4_SUPPORTED_FRO_COM                             \
287         (EXT4_FRO_COM_SPARSE_SUPER |                       \
288          EXT4_FRO_COM_METADATA_CSUM |                      \
289          EXT4_FRO_COM_LARGE_FILE | EXT4_FRO_COM_GDT_CSUM | \
290          EXT4_FRO_COM_DIR_NLINK |                          \
291          EXT4_FRO_COM_EXTRA_ISIZE | EXT4_FRO_COM_HUGE_FILE)
292
293 /*Ignored features:
294  * RECOVER - journaling in lwext4 is not supported
295  *           (probably won't be ever...)
296  * MMP - multi-mout protection (impossible scenario)
297  * */
298 #define EXT_FINCOM_IGNORED                                 \
299         EXT4_FINCOM_RECOVER | EXT4_FINCOM_MMP
300
301 #if 0
302 /*TODO: Features incompatible to implement*/
303 #define EXT4_SUPPORTED_FINCOM
304                      (EXT4_FINCOM_INLINE_DATA)
305
306 /*TODO: Features read only to implement*/
307 #define EXT4_SUPPORTED_FRO_COM
308                      EXT4_FRO_COM_BIGALLOC |\
309                      EXT4_FRO_COM_QUOTA)
310 #endif
311
312
313 /* Inode table/bitmap not in use */
314 #define EXT4_BLOCK_GROUP_INODE_UNINIT 0x0001
315 /* Block bitmap not in use */
316 #define EXT4_BLOCK_GROUP_BLOCK_UNINIT 0x0002
317 /* On-disk itable initialized to zero */
318 #define EXT4_BLOCK_GROUP_ITABLE_ZEROED 0x0004
319
320 /*
321  * Structure of a blocks group descriptor
322  */
323 struct ext4_bgroup {
324         uint32_t block_bitmap_lo;           /* Blocks bitmap block */
325         uint32_t inode_bitmap_lo;           /* Inodes bitmap block */
326         uint32_t inode_table_first_block_lo; /* Inodes table block */
327         uint16_t free_blocks_count_lo;       /* Free blocks count */
328         uint16_t free_inodes_count_lo;       /* Free inodes count */
329         uint16_t used_dirs_count_lo;     /* Directories count */
330         uint16_t flags;                /* EXT4_BG_flags (INODE_UNINIT, etc) */
331         uint32_t exclude_bitmap_lo;    /* Exclude bitmap for snapshots */
332         uint16_t block_bitmap_csum_lo; /* crc32c(s_uuid+grp_num+bbitmap) LE */
333         uint16_t inode_bitmap_csum_lo; /* crc32c(s_uuid+grp_num+ibitmap) LE */
334         uint16_t itable_unused_lo;     /* Unused inodes count */
335         uint16_t checksum;           /* crc16(sb_uuid+group+desc) */
336
337         uint32_t block_bitmap_hi;           /* Blocks bitmap block MSB */
338         uint32_t inode_bitmap_hi;           /* I-nodes bitmap block MSB */
339         uint32_t inode_table_first_block_hi; /* I-nodes table block MSB */
340         uint16_t free_blocks_count_hi;       /* Free blocks count MSB */
341         uint16_t free_inodes_count_hi;       /* Free i-nodes count MSB */
342         uint16_t used_dirs_count_hi;     /* Directories count MSB */
343         uint16_t itable_unused_hi;         /* Unused inodes count MSB */
344         uint32_t exclude_bitmap_hi;       /* Exclude bitmap block MSB */
345         uint16_t block_bitmap_csum_hi; /* crc32c(s_uuid+grp_num+bbitmap) BE */
346         uint16_t inode_bitmap_csum_hi; /* crc32c(s_uuid+grp_num+ibitmap) BE */
347         uint32_t reserved;           /* Padding */
348 };
349
350
351 #define EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE 32
352 #define EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE 64
353
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
357
358 #define EXT4_INODE_BLOCK_SIZE 512
359
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)
367
368 #pragma pack(push, 1)
369
370 /*
371  * Structure of an inode on the disk
372  */
373 struct ext4_inode {
374         uint16_t mode;              /* File mode */
375         uint16_t uid;               /* Low 16 bits of owner uid */
376         uint32_t size_lo;          /* Size in bytes */
377         uint32_t access_time;       /* Access time */
378         uint32_t change_inode_time; /* I-node change time */
379         uint32_t modification_time; /* Modification time */
380         uint32_t deletion_time;     /* Deletion time */
381         uint16_t gid;               /* Low 16 bits of group id */
382         uint16_t links_count;       /* Links count */
383         uint32_t blocks_count_lo;   /* Blocks count */
384         uint32_t flags;             /* File flags */
385         uint32_t unused_osd1;       /* OS dependent - not used in HelenOS */
386         uint32_t blocks[EXT4_INODE_BLOCKS]; /* Pointers to blocks */
387         uint32_t generation;                /* File version (for NFS) */
388         uint32_t file_acl_lo;               /* File ACL */
389         uint32_t size_hi;
390         uint32_t obso_faddr; /* Obsoleted fragment address */
391
392         union {
393                 struct {
394                         uint16_t blocks_high;
395                         uint16_t file_acl_high;
396                         uint16_t uid_high;
397                         uint16_t gid_high;
398                         uint16_t checksum_lo; /* crc32c(uuid+inum+inode) LE */
399                         uint16_t reserved2;
400                 } linux2;
401                 struct {
402                         uint16_t reserved1;
403                         uint16_t mode_high;
404                         uint16_t uid_high;
405                         uint16_t gid_high;
406                         uint32_t author;
407                 } hurd2;
408         } osd2;
409
410         uint16_t extra_isize;
411         uint16_t checksum_hi;   /* crc32c(uuid+inum+inode) BE */
412         uint32_t ctime_extra; /* Extra change time (nsec << 2 | epoch) */
413         uint32_t mtime_extra; /* Extra Modification time (nsec << 2 | epoch) */
414         uint32_t atime_extra; /* Extra Access time (nsec << 2 | epoch) */
415         uint32_t crtime;      /* File creation time */
416         uint32_t
417             crtime_extra;    /* Extra file creation time (nsec << 2 | epoch) */
418         uint32_t version_hi; /* High 32 bits for 64-bit version */
419 };
420
421 #pragma pack(pop)
422
423 #define EXT4_INODE_MODE_FIFO 0x1000
424 #define EXT4_INODE_MODE_CHARDEV 0x2000
425 #define EXT4_INODE_MODE_DIRECTORY 0x4000
426 #define EXT4_INODE_MODE_BLOCKDEV 0x6000
427 #define EXT4_INODE_MODE_FILE 0x8000
428 #define EXT4_INODE_MODE_SOFTLINK 0xA000
429 #define EXT4_INODE_MODE_SOCKET 0xC000
430 #define EXT4_INODE_MODE_TYPE_MASK 0xF000
431
432 /*
433  * Inode flags
434  */
435 #define EXT4_INODE_FLAG_SECRM 0x00000001     /* Secure deletion */
436 #define EXT4_INODE_FLAG_UNRM 0x00000002      /* Undelete */
437 #define EXT4_INODE_FLAG_COMPR 0x00000004     /* Compress file */
438 #define EXT4_INODE_FLAG_SYNC 0x00000008      /* Synchronous updates */
439 #define EXT4_INODE_FLAG_IMMUTABLE 0x00000010 /* Immutable file */
440 #define EXT4_INODE_FLAG_APPEND 0x00000020  /* writes to file may only append */
441 #define EXT4_INODE_FLAG_NODUMP 0x00000040  /* do not dump file */
442 #define EXT4_INODE_FLAG_NOATIME 0x00000080 /* do not update atime */
443
444 /* Compression flags */
445 #define EXT4_INODE_FLAG_DIRTY 0x00000100
446 #define EXT4_INODE_FLAG_COMPRBLK                                               \
447         0x00000200                         /* One or more compressed clusters */
448 #define EXT4_INODE_FLAG_NOCOMPR 0x00000400 /* Don't compress */
449 #define EXT4_INODE_FLAG_ECOMPR 0x00000800  /* Compression error */
450
451 #define EXT4_INODE_FLAG_INDEX 0x00001000  /* hash-indexed directory */
452 #define EXT4_INODE_FLAG_IMAGIC 0x00002000 /* AFS directory */
453 #define EXT4_INODE_FLAG_JOURNAL_DATA                                           \
454         0x00004000                        /* File data should be journaled */
455 #define EXT4_INODE_FLAG_NOTAIL 0x00008000 /* File tail should not be merged */
456 #define EXT4_INODE_FLAG_DIRSYNC                                                \
457         0x00010000 /* Dirsync behaviour (directories only) */
458 #define EXT4_INODE_FLAG_TOPDIR 0x00020000    /* Top of directory hierarchies */
459 #define EXT4_INODE_FLAG_HUGE_FILE 0x00040000 /* Set to each huge file */
460 #define EXT4_INODE_FLAG_EXTENTS 0x00080000   /* Inode uses extents */
461 #define EXT4_INODE_FLAG_EA_INODE 0x00200000  /* Inode used for large EA */
462 #define EXT4_INODE_FLAG_EOFBLOCKS 0x00400000 /* Blocks allocated beyond EOF */
463 #define EXT4_INODE_FLAG_RESERVED 0x80000000  /* reserved for ext4 lib */
464
465 #define EXT4_INODE_ROOT_INDEX 2
466
467
468 #define EXT4_DIRECTORY_FILENAME_LEN 255
469
470 /**@brief   Directory entry types. */
471 enum { EXT4_DE_UNKNOWN = 0,
472        EXT4_DE_REG_FILE,
473        EXT4_DE_DIR,
474        EXT4_DE_CHRDEV,
475        EXT4_DE_BLKDEV,
476        EXT4_DE_FIFO,
477        EXT4_DE_SOCK,
478        EXT4_DE_SYMLINK };
479
480 #define EXT4_DIRENTRY_DIR_CSUM 0xDE
481
482 #pragma pack(push, 1)
483
484 union ext4_dir_en_internal {
485         uint8_t name_length_high; /* Higher 8 bits of name length */
486         uint8_t inode_type;       /* Type of referenced inode (in rev >= 0.5) */
487 };
488
489 /**
490  * Linked list directory entry structure
491  */
492 struct ext4_dir_en {
493         uint32_t inode; /* I-node for the entry */
494         uint16_t entry_len; /* Distance to the next directory entry */
495         uint8_t name_len;   /* Lower 8 bits of name length */
496
497         union ext4_dir_en_internal in;
498         uint8_t name[]; /* Entry name */
499 };
500
501 /* Structures for indexed directory */
502
503 struct ext4_dir_idx_climit {
504         uint16_t limit;
505         uint16_t count;
506 };
507
508 struct ext4_dir_idx_dot_en {
509         uint32_t inode;
510         uint16_t entry_length;
511         uint8_t name_length;
512         uint8_t inode_type;
513         uint8_t name[4];
514 };
515
516 struct ext4_dir_idx_rinfo {
517         uint32_t reserved_zero;
518         uint8_t hash_version;
519         uint8_t info_length;
520         uint8_t indirect_levels;
521         uint8_t unused_flags;
522 };
523
524 struct ext4_dir_idx_entry {
525         uint32_t hash;
526         uint32_t block;
527 };
528
529 struct ext4_dir_idx_root {
530         struct ext4_dir_idx_dot_en dots[2];
531         struct ext4_dir_idx_rinfo info;
532         struct ext4_dir_idx_entry en[];
533 };
534
535 struct ext4_fake_dir_entry {
536         uint32_t inode;
537         uint16_t entry_length;
538         uint8_t name_length;
539         uint8_t inode_type;
540 };
541
542 struct ext4_dir_idx_node {
543         struct ext4_fake_dir_entry fake;
544         struct ext4_dir_idx_entry entries[];
545 };
546
547 /*
548  * This goes at the end of each htree block.
549  */
550 struct ext4_dir_idx_tail {
551         uint32_t reserved;
552         uint32_t checksum;      /* crc32c(uuid+inum+dirblock) */
553 };
554
555 /*
556  * This is a bogus directory entry at the end of each leaf block that
557  * records checksums.
558  */
559 struct ext4_dir_entry_tail {
560         uint32_t reserved_zero1;        /* Pretend to be unused */
561         uint16_t rec_len;               /* 12 */
562         uint8_t reserved_zero2; /* Zero name length */
563         uint8_t reserved_ft;    /* 0xDE, fake file type */
564         uint32_t checksum;              /* crc32c(uuid+inum+dirblock) */
565 };
566
567 #pragma pack(pop)
568
569 #define EXT4_DIRENT_TAIL(block, blocksize) \
570         ((struct ext4_dir_entry_tail *)(((char *)(block)) + ((blocksize) - \
571                                         sizeof(struct ext4_dir_entry_tail))))
572
573 #define EXT4_ERR_BAD_DX_DIR (-25000)
574
575 #define EXT4_LINK_MAX 65000
576
577 #define EXT4_BAD_INO 1
578 #define EXT4_ROOT_INO 2
579 #define EXT4_BOOT_LOADER_INO 5
580 #define EXT4_UNDEL_DIR_INO 6
581 #define EXT4_RESIZE_INO 7
582 #define EXT4_JOURNAL_INO 8
583
584 #define EXT4_GOOD_OLD_FIRST_INO 11
585
586 #define EXT4_EXT_UNWRITTEN_MASK (1L << 15)
587
588 #define EXT4_EXT_MAX_LEN_WRITTEN (1L << 15)
589 #define EXT4_EXT_MAX_LEN_UNWRITTEN \
590         (EXT4_EXT_MAX_LEN_WRITTEN - 1)
591
592 #define EXT4_EXT_GET_LEN(ex) to_le16((ex)->block_count)
593 #define EXT4_EXT_GET_LEN_UNWRITTEN(ex) \
594         (EXT4_EXT_GET_LEN(ex) &= ~(EXT4_EXT_UNWRITTEN_MASK))
595 #define EXT4_EXT_SET_LEN(ex, count) \
596         ((ex)->block_count = to_le16(count))
597
598 #define EXT4_EXT_IS_UNWRITTEN(ex) \
599         (EXT4_EXT_GET_LEN(ex) > EXT4_EXT_MAX_LEN_WRITTEN)
600 #define EXT4_EXT_SET_UNWRITTEN(ex) \
601         ((ex)->block_count |= to_le16(EXT4_EXT_UNWRITTEN_MASK))
602 #define EXT4_EXT_SET_WRITTEN(ex) \
603         ((ex)->block_count &= ~(to_le16(EXT4_EXT_UNWRITTEN_MASK)))
604
605 #pragma pack(push, 1)
606
607 /*
608  * This is the extent tail on-disk structure.
609  * All other extent structures are 12 bytes long.  It turns out that
610  * block_size % 12 >= 4 for at least all powers of 2 greater than 512, which
611  * covers all valid ext4 block sizes.  Therefore, this tail structure can be
612  * crammed into the end of the block without having to rebalance the tree.
613  */
614 struct ext4_extent_tail
615 {
616         uint32_t et_checksum; /* crc32c(uuid+inum+extent_block) */
617 };
618
619 /*
620  * This is the extent on-disk structure.
621  * It's used at the bottom of the tree.
622  */
623 struct ext4_extent {
624         uint32_t first_block; /* First logical block extent covers */
625         uint16_t block_count; /* Number of blocks covered by extent */
626         uint16_t start_hi;    /* High 16 bits of physical block */
627         uint32_t start_lo;    /* Low 32 bits of physical block */
628 };
629
630 /*
631  * This is index on-disk structure.
632  * It's used at all the levels except the bottom.
633  */
634 struct ext4_extent_index {
635         uint32_t first_block; /* Index covers logical blocks from 'block' */
636
637         /**
638          * Pointer to the physical block of the next
639          * level. leaf or next index could be there
640          * high 16 bits of physical block
641          */
642         uint32_t leaf_lo;
643         uint16_t leaf_hi;
644         uint16_t padding;
645 };
646
647 /*
648  * Each block (leaves and indexes), even inode-stored has header.
649  */
650 struct ext4_extent_header {
651         uint16_t magic;
652         uint16_t entries_count;     /* Number of valid entries */
653         uint16_t max_entries_count; /* Capacity of store in entries */
654         uint16_t depth;             /* Has tree real underlying blocks? */
655         uint32_t generation;    /* generation of the tree */
656 };
657
658 #pragma pack(pop)
659
660
661 #define EXT4_EXTENT_MAGIC 0xF30A
662
663 #define EXT4_EXTENT_FIRST(header)                                              \
664         ((struct ext4_extent *)(((char *)(header)) +                           \
665                                 sizeof(struct ext4_extent_header)))
666
667 #define EXT4_EXTENT_FIRST_INDEX(header)                                        \
668         ((struct ext4_extent_index *)(((char *)(header)) +                     \
669                                       sizeof(struct ext4_extent_header)))
670
671 /*
672  * EXT_INIT_MAX_LEN is the maximum number of blocks we can have in an
673  * initialized extent. This is 2^15 and not (2^16 - 1), since we use the
674  * MSB of ee_len field in the extent datastructure to signify if this
675  * particular extent is an initialized extent or an uninitialized (i.e.
676  * preallocated).
677  * EXT_UNINIT_MAX_LEN is the maximum number of blocks we can have in an
678  * uninitialized extent.
679  * If ee_len is <= 0x8000, it is an initialized extent. Otherwise, it is an
680  * uninitialized one. In other words, if MSB of ee_len is set, it is an
681  * uninitialized extent with only one special scenario when ee_len = 0x8000.
682  * In this case we can not have an uninitialized extent of zero length and
683  * thus we make it as a special case of initialized extent with 0x8000 length.
684  * This way we get better extent-to-group alignment for initialized extents.
685  * Hence, the maximum number of blocks we can have in an *initialized*
686  * extent is 2^15 (32768) and in an *uninitialized* extent is 2^15-1 (32767).
687  */
688 #define EXT_INIT_MAX_LEN (1L << 15)
689 #define EXT_UNWRITTEN_MAX_LEN (EXT_INIT_MAX_LEN - 1)
690
691 #define EXT_EXTENT_SIZE sizeof(struct ext4_extent)
692 #define EXT_INDEX_SIZE sizeof(struct ext4_extent_idx)
693
694 #define EXT_FIRST_EXTENT(__hdr__)                                              \
695         ((struct ext4_extent *)(((char *)(__hdr__)) +                          \
696                                 sizeof(struct ext4_extent_header)))
697 #define EXT_FIRST_INDEX(__hdr__)                                               \
698         ((struct ext4_extent_index *)(((char *)(__hdr__)) +                    \
699                                     sizeof(struct ext4_extent_header)))
700 #define EXT_HAS_FREE_INDEX(__path__)                                           \
701         (to_le16((__path__)->header->entries_count) <                                \
702                                     to_le16((__path__)->header->max_entries_count))
703 #define EXT_LAST_EXTENT(__hdr__)                                               \
704         (EXT_FIRST_EXTENT((__hdr__)) + to_le16((__hdr__)->entries_count) - 1)
705 #define EXT_LAST_INDEX(__hdr__)                                                \
706         (EXT_FIRST_INDEX((__hdr__)) + to_le16((__hdr__)->entries_count) - 1)
707 #define EXT_MAX_EXTENT(__hdr__)                                                \
708         (EXT_FIRST_EXTENT((__hdr__)) + to_le16((__hdr__)->max_entries_count) - 1)
709 #define EXT_MAX_INDEX(__hdr__)                                                 \
710         (EXT_FIRST_INDEX((__hdr__)) + to_le16((__hdr__)->max_entries_count) - 1)
711
712 #define EXT4_EXTENT_TAIL_OFFSET(hdr)                                           \
713         (sizeof(struct ext4_extent_header) +                                   \
714          (sizeof(struct ext4_extent) * to_le16((hdr)->max_entries_count)))
715
716 /*
717  * ext4_ext_next_allocated_block:
718  * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
719  * NOTE: it considers block number from index entry as
720  * allocated block. Thus, index entries have to be consistent
721  * with leaves.
722  */
723 #define EXT_MAX_BLOCKS (ext4_lblk_t) (-1)
724
725 #define IN_RANGE(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
726
727
728 /******************************************************************************/
729
730 /* EXT3 HTree directory indexing */
731 #define EXT2_HTREE_LEGACY 0
732 #define EXT2_HTREE_HALF_MD4 1
733 #define EXT2_HTREE_TEA 2
734 #define EXT2_HTREE_LEGACY_UNSIGNED 3
735 #define EXT2_HTREE_HALF_MD4_UNSIGNED 4
736 #define EXT2_HTREE_TEA_UNSIGNED 5
737
738 #define EXT2_HTREE_EOF 0x7FFFFFFFUL
739
740
741 /* Extended Attribute(EA) */
742
743 /* Magic value in attribute blocks */
744 #define EXT4_XATTR_MAGIC                0xEA020000
745
746 /* Maximum number of references to one attribute block */
747 #define EXT4_XATTR_REFCOUNT_MAX         1024
748
749 /* Name indexes */
750 #define EXT4_XATTR_INDEX_USER                   1
751 #define EXT4_XATTR_INDEX_POSIX_ACL_ACCESS       2
752 #define EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT      3
753 #define EXT4_XATTR_INDEX_TRUSTED                4
754 #define EXT4_XATTR_INDEX_LUSTRE                 5
755 #define EXT4_XATTR_INDEX_SECURITY               6
756 #define EXT4_XATTR_INDEX_SYSTEM                 7
757 #define EXT4_XATTR_INDEX_RICHACL                8
758 #define EXT4_XATTR_INDEX_ENCRYPTION             9
759
760 #pragma pack(push, 1)
761
762 struct ext4_xattr_header {
763         uint32_t h_magic;       /* magic number for identification */
764         uint32_t h_refcount;    /* reference count */
765         uint32_t h_blocks;      /* number of disk blocks used */
766         uint32_t h_hash;                /* hash value of all attributes */
767         uint32_t h_checksum;    /* crc32c(uuid+id+xattrblock) */
768                                 /* id = inum if refcount=1, blknum otherwise */
769         uint32_t h_reserved[3]; /* zero right now */
770 };
771
772 struct ext4_xattr_ibody_header {
773         uint32_t h_magic;       /* magic number for identification */
774 };
775
776 struct ext4_xattr_entry {
777         uint8_t e_name_len;     /* length of name */
778         uint8_t e_name_index;   /* attribute name index */
779         uint16_t e_value_offs;  /* offset in disk block of value */
780         uint32_t e_value_block; /* disk block attribute is stored on (n/i) */
781         uint32_t e_value_size;  /* size of attribute value */
782         uint32_t e_hash;                /* hash value of name and value */
783 };
784
785 #pragma pack(pop)
786
787 #define EXT4_GOOD_OLD_INODE_SIZE        128
788
789 /*****************************************************************************/
790
791 /*
792  * JBD stores integers in big endian.
793  */
794
795 #define JBD_MAGIC_NUMBER 0xc03b3998U /* The first 4 bytes of /dev/random! */
796
797 /*
798  * Descriptor block types:
799  */
800
801 #define JBD_DESCRIPTOR_BLOCK    1
802 #define JBD_COMMIT_BLOCK        2
803 #define JBD_SUPERBLOCK          3
804 #define JBD_SUPERBLOCK_V2       4
805 #define JBD_REVOKE_BLOCK        5
806
807 #pragma pack(push, 1)
808
809 /*
810  * Standard header for all descriptor blocks:
811  */
812 struct jbd_bhdr {
813         uint32_t                magic;
814         uint32_t                blocktype;
815         uint32_t                sequence;
816 };
817
818 #pragma pack(pop)
819
820 /*
821  * Checksum types.
822  */
823 #define JBD_CRC32_CHKSUM   1
824 #define JBD_MD5_CHKSUM     2
825 #define JBD_SHA1_CHKSUM    3
826 #define JBD_CRC32C_CHKSUM  4
827
828 #define JBD_CRC32_CHKSUM_SIZE 4
829
830 #define JBD_CHECKSUM_BYTES (32 / sizeof(uint32_t))
831
832 #pragma pack(push, 1)
833
834 /*
835  * Commit block header for storing transactional checksums:
836  *
837  * NOTE: If FEATURE_COMPAT_CHECKSUM (checksum v1) is set, the h_chksum*
838  * fields are used to store a checksum of the descriptor and data blocks.
839  *
840  * If FEATURE_INCOMPAT_CSUM_V2 (checksum v2) is set, then the h_chksum
841  * field is used to store crc32c(uuid+commit_block).  Each journal metadata
842  * block gets its own checksum, and data block checksums are stored in
843  * journal_block_tag (in the descriptor).  The other h_chksum* fields are
844  * not used.
845  *
846  * If FEATURE_INCOMPAT_CSUM_V3 is set, the descriptor block uses
847  * journal_block_tag3_t to store a full 32-bit checksum.  Everything else
848  * is the same as v2.
849  *
850  * Checksum v1, v2, and v3 are mutually exclusive features.
851  */
852
853 struct jbd_commit_header {
854         struct jbd_bhdr header;
855         uint8_t chksum_type;
856         uint8_t chksum_size;
857         uint8_t padding[2];
858         uint32_t                chksum[JBD_CHECKSUM_BYTES];
859         uint64_t                commit_sec;
860         uint32_t                commit_nsec;
861 };
862
863 /*
864  * The block tag: used to describe a single buffer in the journal
865  */
866 struct jbd_block_tag3 {
867         uint32_t                blocknr;        /* The on-disk block number */
868         uint32_t                flags;  /* See below */
869         uint32_t                blocknr_high; /* most-significant high 32bits. */
870         uint32_t                checksum;       /* crc32c(uuid+seq+block) */
871 };
872
873 struct jbd_block_tag {
874         uint32_t                blocknr;        /* The on-disk block number */
875         uint16_t                checksum;       /* truncated crc32c(uuid+seq+block) */
876         uint16_t                flags;  /* See below */
877         uint32_t                blocknr_high; /* most-significant high 32bits. */
878 };
879
880 #pragma pack(pop)
881
882 /* Definitions for the journal tag flags word: */
883 #define JBD_FLAG_ESCAPE         1       /* on-disk block is escaped */
884 #define JBD_FLAG_SAME_UUID      2       /* block has same uuid as previous */
885 #define JBD_FLAG_DELETED        4       /* block deleted by this transaction */
886 #define JBD_FLAG_LAST_TAG       8       /* last tag in this descriptor block */
887
888 #pragma pack(push, 1)
889
890 /* Tail of descriptor block, for checksumming */
891 struct jbd_block_tail {
892         uint32_t        checksum;
893 };
894
895 /*
896  * The revoke descriptor: used on disk to describe a series of blocks to
897  * be revoked from the log
898  */
899 struct jbd_revoke_header {
900         struct jbd_bhdr  header;
901         uint32_t         count; /* Count of bytes used in the block */
902 };
903
904 /* Tail of revoke block, for checksumming */
905 struct jbd_revoke_tail {
906         uint32_t                checksum;
907 };
908
909 #pragma pack(pop)
910
911 #define JBD_USERS_MAX 48
912 #define JBD_USERS_SIZE (UUID_SIZE * JBD_USERS_MAX)
913
914 #pragma pack(push, 1)
915
916 /*
917  * The journal superblock.  All fields are in big-endian byte order.
918  */
919 struct jbd_sb {
920 /* 0x0000 */
921         struct jbd_bhdr header;
922
923 /* 0x000C */
924         /* Static information describing the journal */
925         uint32_t        blocksize;              /* journal device blocksize */
926         uint32_t        maxlen;         /* total blocks in journal file */
927         uint32_t        first;          /* first block of log information */
928
929 /* 0x0018 */
930         /* Dynamic information describing the current state of the log */
931         uint32_t        sequence;               /* first commit ID expected in log */
932         uint32_t        start;          /* blocknr of start of log */
933
934 /* 0x0020 */
935         /* Error value, as set by journal_abort(). */
936         int32_t         error_val;
937
938 /* 0x0024 */
939         /* Remaining fields are only valid in a version-2 superblock */
940         uint32_t        feature_compat;         /* compatible feature set */
941         uint32_t        feature_incompat;       /* incompatible feature set */
942         uint32_t        feature_ro_compat;      /* readonly-compatible feature set */
943 /* 0x0030 */
944         uint8_t         uuid[UUID_SIZE];                /* 128-bit uuid for journal */
945
946 /* 0x0040 */
947         uint32_t        nr_users;               /* Nr of filesystems sharing log */
948
949         uint32_t        dynsuper;               /* Blocknr of dynamic superblock copy*/
950
951 /* 0x0048 */
952         uint32_t        max_transaction;        /* Limit of journal blocks per trans.*/
953         uint32_t        max_trandata;   /* Limit of data blocks per trans. */
954
955 /* 0x0050 */
956         uint8_t         checksum_type;  /* checksum type */
957         uint8_t         padding2[3];
958         uint32_t        padding[42];
959         uint32_t        checksum;               /* crc32c(superblock) */
960
961 /* 0x0100 */
962         uint8_t         users[JBD_USERS_SIZE];          /* ids of all fs'es sharing the log */
963
964 /* 0x0400 */
965 };
966
967 #pragma pack(pop)
968
969 #define JBD_SUPERBLOCK_SIZE sizeof(struct jbd_sb)
970
971 #define JBD_HAS_COMPAT_FEATURE(jsb,mask)                                        \
972         ((jsb)->header.blocktype >= to_be32(2) &&                               \
973          ((jsb)->feature_compat & to_be32((mask))))
974 #define JBD_HAS_RO_COMPAT_FEATURE(jsb,mask)                             \
975         ((jsb)->header.blocktype >= to_be32(2) &&                               \
976          ((jsb)->feature_ro_compat & to_be32((mask))))
977 #define JBD_HAS_INCOMPAT_FEATURE(jsb,mask)                              \
978         ((jsb)->header.blocktype >= to_be32(2) &&                               \
979          ((jsb)->feature_incompat & to_be32((mask))))
980
981 #define JBD_FEATURE_COMPAT_CHECKSUM     0x00000001
982
983 #define JBD_FEATURE_INCOMPAT_REVOKE             0x00000001
984 #define JBD_FEATURE_INCOMPAT_64BIT              0x00000002
985 #define JBD_FEATURE_INCOMPAT_ASYNC_COMMIT       0x00000004
986 #define JBD_FEATURE_INCOMPAT_CSUM_V2            0x00000008
987 #define JBD_FEATURE_INCOMPAT_CSUM_V3            0x00000010
988
989 /* Features known to this kernel version: */
990 #define JBD_KNOWN_COMPAT_FEATURES       0
991 #define JBD_KNOWN_ROCOMPAT_FEATURES     0
992 #define JBD_KNOWN_INCOMPAT_FEATURES     (JBD_FEATURE_INCOMPAT_REVOKE|\
993                                          JBD_FEATURE_INCOMPAT_ASYNC_COMMIT|\
994                                          JBD_FEATURE_INCOMPAT_64BIT|\
995                                          JBD_FEATURE_INCOMPAT_CSUM_V2|\
996                                          JBD_FEATURE_INCOMPAT_CSUM_V3)
997
998 /*****************************************************************************/
999
1000 #define EXT4_CRC32_INIT (0xFFFFFFFFUL)
1001
1002 /*****************************************************************************/
1003
1004 #ifdef __cplusplus
1005 }
1006 #endif
1007
1008
1009 #if CONFIG_USE_USER_MALLOC
1010
1011 #define ext4_malloc  ext4_user_malloc
1012 #define ext4_calloc  ext4_user_calloc
1013 #define ext4_realloc ext4_user_realloc
1014 #define ext4_free    ext4_user_free
1015
1016 #else
1017
1018 #define ext4_malloc  malloc
1019 #define ext4_calloc  calloc
1020 #define ext4_realloc realloc
1021 #define ext4_free    free
1022
1023 #endif
1024
1025
1026 #endif /* EXT4_TYPES_H_ */
1027
1028 /**
1029  * @}
1030  */