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