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