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