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