ext4_extent_full.c: print warning messages when extent block checksum verification...
[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 | EXT4_FRO_COM_BTREE_DIR)
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 |                          \
258          EXT4_FRO_COM_LARGE_FILE | EXT4_FRO_COM_BTREE_DIR)
259
260 /*
261  * EXT4 supported feature set
262  */
263 #define EXT4_SUPPORTED_FCOM (EXT4_FCOM_DIR_INDEX)
264
265 #define EXT4_SUPPORTED_FINCOM                              \
266         (EXT4_FINCOM_FILETYPE | EXT4_FINCOM_META_BG |      \
267          EXT4_FINCOM_EXTENTS | EXT4_FINCOM_FLEX_BG |       \
268          EXT4_FINCOM_64BIT)
269
270 #define EXT4_SUPPORTED_FRO_COM                             \
271         (EXT4_FRO_COM_SPARSE_SUPER |                       \
272          EXT4_FRO_COM_METADATA_CSUM |                      \
273          EXT4_FRO_COM_LARGE_FILE | EXT4_FRO_COM_GDT_CSUM | \
274          EXT4_FRO_COM_DIR_NLINK |                          \
275          EXT4_FRO_COM_EXTRA_ISIZE |                        \
276          EXT4_FRO_COM_BTREE_DIR | EXT4_FRO_COM_HUGE_FILE)
277
278 /*Ignored features:
279  * RECOVER - journaling in lwext4 is not supported
280  *           (probably won't be ever...)
281  * MMP - multi-mout protection (impossible scenario)
282  * */
283 #define EXT_FINCOM_IGNORED                                 \
284         EXT4_FINCOM_RECOVER | EXT4_FINCOM_MMP
285
286 #if 0
287 /*TODO: Features incompatible to implement*/
288 #define EXT4_SUPPORTED_FINCOM
289                      (EXT4_FINCOM_INLINE_DATA)
290
291 /*TODO: Features read only to implement*/
292 #define EXT4_SUPPORTED_FRO_COM
293                      EXT4_FRO_COM_BIGALLOC |\
294                      EXT4_FRO_COM_QUOTA)
295 #endif
296
297 struct ext4_fs {
298         struct ext4_blockdev *bdev;
299         struct ext4_sblock sb;
300
301         uint64_t inode_block_limits[4];
302         uint64_t inode_blocks_per_level[4];
303
304         uint32_t last_inode_bg_id;
305 };
306
307 /* Inode table/bitmap not in use */
308 #define EXT4_BLOCK_GROUP_INODE_UNINIT 0x0001
309 /* Block bitmap not in use */
310 #define EXT4_BLOCK_GROUP_BLOCK_UNINIT 0x0002
311 /* On-disk itable initialized to zero */
312 #define EXT4_BLOCK_GROUP_ITABLE_ZEROED 0x0004
313
314 /*
315  * Structure of a blocks group descriptor
316  */
317 struct ext4_bgroup {
318         uint32_t block_bitmap_lo;           /* Blocks bitmap block */
319         uint32_t inode_bitmap_lo;           /* Inodes bitmap block */
320         uint32_t inode_table_first_block_lo; /* Inodes table block */
321         uint16_t free_blocks_count_lo;       /* Free blocks count */
322         uint16_t free_inodes_count_lo;       /* Free inodes count */
323         uint16_t used_dirs_count_lo;     /* Directories count */
324         uint16_t flags;                /* EXT4_BG_flags (INODE_UNINIT, etc) */
325         uint32_t exclude_bitmap_lo;    /* Exclude bitmap for snapshots */
326         uint16_t block_bitmap_csum_lo; /* crc32c(s_uuid+grp_num+bbitmap) LE */
327         uint16_t inode_bitmap_csum_lo; /* crc32c(s_uuid+grp_num+ibitmap) LE */
328         uint16_t itable_unused_lo;     /* Unused inodes count */
329         uint16_t checksum;           /* crc16(sb_uuid+group+desc) */
330
331         uint32_t block_bitmap_hi;           /* Blocks bitmap block MSB */
332         uint32_t inode_bitmap_hi;           /* I-nodes bitmap block MSB */
333         uint32_t inode_table_first_block_hi; /* I-nodes table block MSB */
334         uint16_t free_blocks_count_hi;       /* Free blocks count MSB */
335         uint16_t free_inodes_count_hi;       /* Free i-nodes count MSB */
336         uint16_t used_dirs_count_hi;     /* Directories count MSB */
337         uint16_t itable_unused_hi;         /* Unused inodes count MSB */
338         uint32_t exclude_bitmap_hi;       /* Exclude bitmap block MSB */
339         uint16_t block_bitmap_csum_hi; /* crc32c(s_uuid+grp_num+bbitmap) BE */
340         uint16_t inode_bitmap_csum_hi; /* crc32c(s_uuid+grp_num+ibitmap) BE */
341         uint32_t reserved;           /* Padding */
342 };
343
344 struct ext4_block_group_ref {
345         struct ext4_block block;
346         struct ext4_bgroup *block_group;
347         struct ext4_fs *fs;
348         uint32_t index;
349         bool dirty;
350 };
351
352 #define EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE 32
353 #define EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE 64
354
355 #define EXT4_MIN_BLOCK_SIZE 1024  /* 1 KiB */
356 #define EXT4_MAX_BLOCK_SIZE 65536 /* 64 KiB */
357 #define EXT4_REV0_INODE_SIZE 128
358
359 #define EXT4_INODE_BLOCK_SIZE 512
360
361 #define EXT4_INODE_DIRECT_BLOCK_COUNT 12
362 #define EXT4_INODE_INDIRECT_BLOCK EXT4_INODE_DIRECT_BLOCK_COUNT
363 #define EXT4_INODE_DOUBLE_INDIRECT_BLOCK (EXT4_INODE_INDIRECT_BLOCK + 1)
364 #define EXT4_INODE_TRIPPLE_INDIRECT_BLOCK (EXT4_INODE_DOUBLE_INDIRECT_BLOCK + 1)
365 #define EXT4_INODE_BLOCKS (EXT4_INODE_TRIPPLE_INDIRECT_BLOCK + 1)
366 #define EXT4_INODE_INDIRECT_BLOCK_COUNT                                        \
367         (EXT4_INODE_BLOCKS - EXT4_INODE_DIRECT_BLOCK_COUNT)
368
369 /*
370  * Structure of an inode on the disk
371  */
372 struct ext4_inode {
373         uint16_t mode;              /* File mode */
374         uint16_t uid;               /* Low 16 bits of owner uid */
375         uint32_t size_lo;          /* Size in bytes */
376         uint32_t access_time;       /* Access time */
377         uint32_t change_inode_time; /* I-node change time */
378         uint32_t modification_time; /* Modification time */
379         uint32_t deletion_time;     /* Deletion time */
380         uint16_t gid;               /* Low 16 bits of group id */
381         uint16_t links_count;       /* Links count */
382         uint32_t blocks_count_lo;   /* Blocks count */
383         uint32_t flags;             /* File flags */
384         uint32_t unused_osd1;       /* OS dependent - not used in HelenOS */
385         uint32_t blocks[EXT4_INODE_BLOCKS]; /* Pointers to blocks */
386         uint32_t generation;                /* File version (for NFS) */
387         uint32_t file_acl_lo;               /* File ACL */
388         uint32_t size_hi;
389         uint32_t obso_faddr; /* Obsoleted fragment address */
390
391         union {
392                 struct {
393                         uint16_t blocks_high;
394                         uint16_t file_acl_high;
395                         uint16_t uid_high;
396                         uint16_t gid_high;
397                         uint16_t checksum_lo; /* crc32c(uuid+inum+inode) LE */
398                         uint16_t reserved2;
399                 } linux2;
400                 struct {
401                         uint16_t reserved1;
402                         uint16_t mode_high;
403                         uint16_t uid_high;
404                         uint16_t gid_high;
405                         uint32_t author;
406                 } hurd2;
407         } __attribute__((packed)) osd2;
408
409         uint16_t extra_isize;
410         uint16_t checksum_hi;   /* crc32c(uuid+inum+inode) BE */
411         uint32_t ctime_extra; /* Extra change time (nsec << 2 | epoch) */
412         uint32_t mtime_extra; /* Extra Modification time (nsec << 2 | epoch) */
413         uint32_t atime_extra; /* Extra Access time (nsec << 2 | epoch) */
414         uint32_t crtime;      /* File creation time */
415         uint32_t
416             crtime_extra;    /* Extra file creation time (nsec << 2 | epoch) */
417         uint32_t version_hi; /* High 32 bits for 64-bit version */
418 } __attribute__((packed));
419
420 #define EXT4_INODE_MODE_FIFO 0x1000
421 #define EXT4_INODE_MODE_CHARDEV 0x2000
422 #define EXT4_INODE_MODE_DIRECTORY 0x4000
423 #define EXT4_INODE_MODE_BLOCKDEV 0x6000
424 #define EXT4_INODE_MODE_FILE 0x8000
425 #define EXT4_INODE_MODE_SOFTLINK 0xA000
426 #define EXT4_INODE_MODE_SOCKET 0xC000
427 #define EXT4_INODE_MODE_TYPE_MASK 0xF000
428
429 /*
430  * Inode flags
431  */
432 #define EXT4_INODE_FLAG_SECRM 0x00000001     /* Secure deletion */
433 #define EXT4_INODE_FLAG_UNRM 0x00000002      /* Undelete */
434 #define EXT4_INODE_FLAG_COMPR 0x00000004     /* Compress file */
435 #define EXT4_INODE_FLAG_SYNC 0x00000008      /* Synchronous updates */
436 #define EXT4_INODE_FLAG_IMMUTABLE 0x00000010 /* Immutable file */
437 #define EXT4_INODE_FLAG_APPEND 0x00000020  /* writes to file may only append */
438 #define EXT4_INODE_FLAG_NODUMP 0x00000040  /* do not dump file */
439 #define EXT4_INODE_FLAG_NOATIME 0x00000080 /* do not update atime */
440
441 /* Compression flags */
442 #define EXT4_INODE_FLAG_DIRTY 0x00000100
443 #define EXT4_INODE_FLAG_COMPRBLK                                               \
444         0x00000200                         /* One or more compressed clusters */
445 #define EXT4_INODE_FLAG_NOCOMPR 0x00000400 /* Don't compress */
446 #define EXT4_INODE_FLAG_ECOMPR 0x00000800  /* Compression error */
447
448 #define EXT4_INODE_FLAG_INDEX 0x00001000  /* hash-indexed directory */
449 #define EXT4_INODE_FLAG_IMAGIC 0x00002000 /* AFS directory */
450 #define EXT4_INODE_FLAG_JOURNAL_DATA                                           \
451         0x00004000                        /* File data should be journaled */
452 #define EXT4_INODE_FLAG_NOTAIL 0x00008000 /* File tail should not be merged */
453 #define EXT4_INODE_FLAG_DIRSYNC                                                \
454         0x00010000 /* Dirsync behaviour (directories only) */
455 #define EXT4_INODE_FLAG_TOPDIR 0x00020000    /* Top of directory hierarchies */
456 #define EXT4_INODE_FLAG_HUGE_FILE 0x00040000 /* Set to each huge file */
457 #define EXT4_INODE_FLAG_EXTENTS 0x00080000   /* Inode uses extents */
458 #define EXT4_INODE_FLAG_EA_INODE 0x00200000  /* Inode used for large EA */
459 #define EXT4_INODE_FLAG_EOFBLOCKS 0x00400000 /* Blocks allocated beyond EOF */
460 #define EXT4_INODE_FLAG_RESERVED 0x80000000  /* reserved for ext4 lib */
461
462 #define EXT4_INODE_ROOT_INDEX 2
463
464 struct ext4_inode_ref {
465         struct ext4_block block;
466         struct ext4_inode *inode;
467         struct ext4_fs *fs;
468         uint32_t index;
469         bool dirty;
470 };
471
472 #define EXT4_DIRECTORY_FILENAME_LEN 255
473
474 /**@brief   Directory entry types. */
475 enum { EXT4_DIRENTRY_UNKNOWN = 0,
476        EXT4_DIRENTRY_REG_FILE,
477        EXT4_DIRENTRY_DIR,
478        EXT4_DIRENTRY_CHRDEV,
479        EXT4_DIRENTRY_BLKDEV,
480        EXT4_DIRENTRY_FIFO,
481        EXT4_DIRENTRY_SOCK,
482        EXT4_DIRENTRY_SYMLINK };
483
484 #define EXT4_DIRENTRY_DIR_CSUM 0xDE
485
486 union ext4_directory_entry_ll_internal {
487         uint8_t name_length_high; /* Higher 8 bits of name length */
488         uint8_t inode_type;       /* Type of referenced inode (in rev >= 0.5) */
489 } __attribute__((packed));
490
491 /**
492  * Linked list directory entry structure
493  */
494 struct ext4_directory_entry_ll {
495         uint32_t inode; /* I-node for the entry */
496         uint16_t entry_length; /* Distance to the next directory entry */
497         uint8_t name_length;   /* Lower 8 bits of name length */
498
499         union ext4_directory_entry_ll_internal in;
500
501         uint8_t name[EXT4_DIRECTORY_FILENAME_LEN]; /* Entry name */
502 } __attribute__((packed));
503
504 struct ext4_directory_iterator {
505         struct ext4_inode_ref *inode_ref;
506         struct ext4_block current_block;
507         uint64_t current_offset;
508         struct ext4_directory_entry_ll *current;
509 };
510
511 struct ext4_directory_search_result {
512         struct ext4_block block;
513         struct ext4_directory_entry_ll *dentry;
514 };
515
516 /* Structures for indexed directory */
517
518 struct ext4_directory_dx_countlimit {
519         uint16_t limit;
520         uint16_t count;
521 };
522
523 struct ext4_directory_dx_dot_entry {
524         uint32_t inode;
525         uint16_t entry_length;
526         uint8_t name_length;
527         uint8_t inode_type;
528         uint8_t name[4];
529 };
530
531 struct ext4_directory_dx_root_info {
532         uint32_t reserved_zero;
533         uint8_t hash_version;
534         uint8_t info_length;
535         uint8_t indirect_levels;
536         uint8_t unused_flags;
537 };
538
539 struct ext4_directory_dx_entry {
540         uint32_t hash;
541         uint32_t block;
542 };
543
544 struct ext4_directory_dx_root {
545         struct ext4_directory_dx_dot_entry dots[2];
546         struct ext4_directory_dx_root_info info;
547         struct ext4_directory_dx_entry entries[];
548 };
549
550 struct ext4_fake_directory_entry {
551         uint32_t inode;
552         uint16_t entry_length;
553         uint8_t name_length;
554         uint8_t inode_type;
555 };
556
557 struct ext4_directory_dx_node {
558         struct ext4_fake_directory_entry fake;
559         struct ext4_directory_dx_entry entries[];
560 };
561
562 struct ext4_directory_dx_block {
563         struct ext4_block block;
564         struct ext4_directory_dx_entry *entries;
565         struct ext4_directory_dx_entry *position;
566 };
567
568 /*
569  * This goes at the end of each htree block.
570  */
571 struct ext4_directory_dx_tail {
572         uint32_t reserved;
573         uint32_t checksum;      /* crc32c(uuid+inum+dirblock) */
574 };
575
576 /*
577  * This is a bogus directory entry at the end of each leaf block that
578  * records checksums.
579  */
580 struct ext4_directory_entry_tail {
581         uint32_t reserved_zero1;        /* Pretend to be unused */
582         uint16_t rec_len;               /* 12 */
583         uint8_t reserved_zero2; /* Zero name length */
584         uint8_t reserved_ft;    /* 0xDE, fake file type */
585         uint32_t checksum;              /* crc32c(uuid+inum+dirblock) */
586 };
587
588 #define EXT4_DIRENT_TAIL(block, blocksize) \
589         ((struct ext4_directory_entry_tail *)(((char *)(block)) + \
590                                              ((blocksize) - \
591                                              sizeof(struct ext4_directory_entry_tail))))
592
593 #define EXT4_ERR_BAD_DX_DIR (-25000)
594
595 #define EXT4_LINK_MAX 65000
596
597 #define EXT4_BAD_INO 1
598 #define EXT4_ROOT_INO 2
599 #define EXT4_BOOT_LOADER_INO 5
600 #define EXT4_UNDEL_DIR_INO 6
601 #define EXT4_RESIZE_INO 7
602 #define EXT4_JOURNAL_INO 8
603
604 #define EXT4_GOOD_OLD_FIRST_INO 11
605
606 #define EXT4_EXT_UNWRITTEN_MASK (1L << 15)
607
608 #define EXT4_EXT_MAX_LEN_WRITTEN (1L << 15)
609 #define EXT4_EXT_MAX_LEN_UNWRITTEN \
610         (EXT4_EXT_MAX_LEN_WRITTEN - 1)
611
612 #define EXT4_EXT_GET_LEN(ex) to_le16((ex)->block_count)
613 #define EXT4_EXT_GET_LEN_UNWRITTEN(ex) \
614         (EXT4_EXT_GET_LEN(ex) &= ~(EXT4_EXT_UNWRITTEN_MASK))
615 #define EXT4_EXT_SET_LEN(ex, count) \
616         ((ex)->block_count = to_le16(count))
617
618 #define EXT4_EXT_IS_UNWRITTEN(ex) \
619         (EXT4_EXT_GET_LEN(ex) > EXT4_EXT_MAX_LEN_WRITTEN)
620 #define EXT4_EXT_SET_UNWRITTEN(ex) \
621         ((ex)->block_count |= to_le16(EXT4_EXT_UNWRITTEN_MASK))
622 #define EXT4_EXT_SET_WRITTEN(ex) \
623         ((ex)->block_count &= ~(to_le16(EXT4_EXT_UNWRITTEN_MASK)))
624 /*
625  * This is the extent tail on-disk structure.
626  * All other extent structures are 12 bytes long.  It turns out that
627  * block_size % 12 >= 4 for at least all powers of 2 greater than 512, which
628  * covers all valid ext4 block sizes.  Therefore, this tail structure can be
629  * crammed into the end of the block without having to rebalance the tree.
630  */
631 struct ext4_extent_tail
632 {
633         uint32_t et_checksum; /* crc32c(uuid+inum+extent_block) */
634 };
635
636 /*
637  * This is the extent on-disk structure.
638  * It's used at the bottom of the tree.
639  */
640 struct ext4_extent {
641         uint32_t first_block; /* First logical block extent covers */
642         uint16_t block_count; /* Number of blocks covered by extent */
643         uint16_t start_hi;    /* High 16 bits of physical block */
644         uint32_t start_lo;    /* Low 32 bits of physical block */
645 };
646
647 /*
648  * This is index on-disk structure.
649  * It's used at all the levels except the bottom.
650  */
651 struct ext4_extent_index {
652         uint32_t first_block; /* Index covers logical blocks from 'block' */
653
654         /**
655          * Pointer to the physical block of the next
656          * level. leaf or next index could be there
657          * high 16 bits of physical block
658          */
659         uint32_t leaf_lo;
660         uint16_t leaf_hi;
661         uint16_t padding;
662 };
663
664 /*
665  * Each block (leaves and indexes), even inode-stored has header.
666  */
667 struct ext4_extent_header {
668         uint16_t magic;
669         uint16_t entries_count;     /* Number of valid entries */
670         uint16_t max_entries_count; /* Capacity of store in entries */
671         uint16_t depth;             /* Has tree real underlying blocks? */
672         uint32_t generation;    /* generation of the tree */
673 };
674
675
676 /*
677  * Types of blocks.
678  */
679 typedef uint32_t ext4_lblk_t;
680 typedef uint64_t ext4_fsblk_t;
681
682 /*
683  * Array of ext4_ext_path contains path to some extent.
684  * Creation/lookup routines use it for traversal/splitting/etc.
685  * Truncate uses it to simulate recursive walking.
686  */
687 struct ext4_extent_path {
688         ext4_fsblk_t p_block;
689         struct ext4_block block;
690         int32_t depth;
691         int32_t maxdepth;
692         struct ext4_extent_header *header;
693         struct ext4_extent_index *index;
694         struct ext4_extent *extent;
695
696 };
697
698
699 #define EXT4_EXTENT_MAGIC 0xF30A
700
701 #define EXT4_EXTENT_FIRST(header)                                              \
702         ((struct ext4_extent *)(((char *)(header)) +                           \
703                                 sizeof(struct ext4_extent_header)))
704
705 #define EXT4_EXTENT_FIRST_INDEX(header)                                        \
706         ((struct ext4_extent_index *)(((char *)(header)) +                     \
707                                       sizeof(struct ext4_extent_header)))
708
709 /*
710  * EXT_INIT_MAX_LEN is the maximum number of blocks we can have in an
711  * initialized extent. This is 2^15 and not (2^16 - 1), since we use the
712  * MSB of ee_len field in the extent datastructure to signify if this
713  * particular extent is an initialized extent or an uninitialized (i.e.
714  * preallocated).
715  * EXT_UNINIT_MAX_LEN is the maximum number of blocks we can have in an
716  * uninitialized extent.
717  * If ee_len is <= 0x8000, it is an initialized extent. Otherwise, it is an
718  * uninitialized one. In other words, if MSB of ee_len is set, it is an
719  * uninitialized extent with only one special scenario when ee_len = 0x8000.
720  * In this case we can not have an uninitialized extent of zero length and
721  * thus we make it as a special case of initialized extent with 0x8000 length.
722  * This way we get better extent-to-group alignment for initialized extents.
723  * Hence, the maximum number of blocks we can have in an *initialized*
724  * extent is 2^15 (32768) and in an *uninitialized* extent is 2^15-1 (32767).
725  */
726 #define EXT_INIT_MAX_LEN (1L << 15)
727 #define EXT_UNWRITTEN_MAX_LEN (EXT_INIT_MAX_LEN - 1)
728
729 #define EXT_EXTENT_SIZE sizeof(struct ext4_extent)
730 #define EXT_INDEX_SIZE sizeof(struct ext4_extent_idx)
731
732 #define EXT_FIRST_EXTENT(__hdr__)                                              \
733         ((struct ext4_extent *)(((char *)(__hdr__)) +                          \
734                                 sizeof(struct ext4_extent_header)))
735 #define EXT_FIRST_INDEX(__hdr__)                                               \
736         ((struct ext4_extent_index *)(((char *)(__hdr__)) +                    \
737                                     sizeof(struct ext4_extent_header)))
738 #define EXT_HAS_FREE_INDEX(__path__)                                           \
739         ((__path__)->header->entries_count < (__path__)->header->max_entries_count)
740 #define EXT_LAST_EXTENT(__hdr__)                                               \
741         (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->entries_count - 1)
742 #define EXT_LAST_INDEX(__hdr__)                                                \
743         (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->entries_count - 1)
744 #define EXT_MAX_EXTENT(__hdr__)                                                \
745         (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->max_entries_count - 1)
746 #define EXT_MAX_INDEX(__hdr__)                                                 \
747         (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->max_entries_count - 1)
748
749 #define EXT4_EXTENT_TAIL_OFFSET(hdr)                                           \
750         (sizeof(struct ext4_extent_header) +                                   \
751          (sizeof(struct ext4_extent) * (hdr)->max_entries_count))
752
753 /*
754  * ext4_ext_next_allocated_block:
755  * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
756  * NOTE: it considers block number from index entry as
757  * allocated block. Thus, index entries have to be consistent
758  * with leaves.
759  */
760 #define EXT_MAX_BLOCKS (ext4_lblk_t) (-1)
761
762 #define IN_RANGE(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
763
764
765 /******************************************************************************/
766
767 /* EXT3 HTree directory indexing */
768 #define EXT2_HTREE_LEGACY 0
769 #define EXT2_HTREE_HALF_MD4 1
770 #define EXT2_HTREE_TEA 2
771 #define EXT2_HTREE_LEGACY_UNSIGNED 3
772 #define EXT2_HTREE_HALF_MD4_UNSIGNED 4
773 #define EXT2_HTREE_TEA_UNSIGNED 5
774
775 #define EXT2_HTREE_EOF 0x7FFFFFFFUL
776
777 struct ext4_hash_info {
778         uint32_t hash;
779         uint32_t minor_hash;
780         uint32_t hash_version;
781         const uint32_t *seed;
782 };
783
784 /* Extended Attribute(EA) */
785
786 /* Magic value in attribute blocks */
787 #define EXT4_XATTR_MAGIC                0xEA020000
788
789 /* Maximum number of references to one attribute block */
790 #define EXT4_XATTR_REFCOUNT_MAX         1024
791
792 /* Name indexes */
793 #define EXT4_XATTR_INDEX_USER                   1
794 #define EXT4_XATTR_INDEX_POSIX_ACL_ACCESS       2
795 #define EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT      3
796 #define EXT4_XATTR_INDEX_TRUSTED                4
797 #define EXT4_XATTR_INDEX_LUSTRE                 5
798 #define EXT4_XATTR_INDEX_SECURITY               6
799 #define EXT4_XATTR_INDEX_SYSTEM                 7
800 #define EXT4_XATTR_INDEX_RICHACL                8
801 #define EXT4_XATTR_INDEX_ENCRYPTION             9
802
803 struct ext4_xattr_header {
804         uint32_t h_magic;       /* magic number for identification */
805         uint32_t h_refcount;    /* reference count */
806         uint32_t h_blocks;      /* number of disk blocks used */
807         uint32_t h_hash;                /* hash value of all attributes */
808         uint32_t h_checksum;    /* crc32c(uuid+id+xattrblock) */
809                                 /* id = inum if refcount=1, blknum otherwise */
810         uint32_t h_reserved[3]; /* zero right now */
811 } __attribute__((packed));
812
813 struct ext4_xattr_ibody_header {
814         uint32_t h_magic;       /* magic number for identification */
815 } __attribute__((packed));
816
817 struct ext4_xattr_entry {
818         uint8_t e_name_len;     /* length of name */
819         uint8_t e_name_index;   /* attribute name index */
820         uint16_t e_value_offs;  /* offset in disk block of value */
821         uint32_t e_value_block; /* disk block attribute is stored on (n/i) */
822         uint32_t e_value_size;  /* size of attribute value */
823         uint32_t e_hash;                /* hash value of name and value */
824 } __attribute__((packed));
825
826 struct ext4_xattr_item {
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  */