]> code.delx.au - refind/blob - filesystems/fsw_ext4_disk.h
Initial commit of new driver for Ext4. Updated the on disk layout structures
[refind] / filesystems / fsw_ext4_disk.h
1 /**
2 * \file fsw_ext4_disk.h
3 * ext4 file system on-disk structures.
4 */
5
6 /*-
7 * Copyright (c) 2012 Stefan Agner
8 * Portions Copyright (c) 2006 Christoph Pfisterer
9 * Portions Copyright (c) 1991-2012 by various Linux kernel contributors
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 */
25
26 #ifndef _FSW_EXT4_DISK_H_
27 #define _FSW_EXT4_DISK_H_
28
29 // types
30
31 typedef fsw_s8 __s8;
32 typedef fsw_u8 __u8;
33 typedef fsw_s16 __s16;
34 typedef fsw_u16 __u16;
35 typedef fsw_s32 __s32;
36 typedef fsw_u32 __u32;
37 typedef fsw_s64 __s64;
38 typedef fsw_u64 __u64;
39
40 typedef __u16 __le16;
41 typedef __u32 __le32;
42 typedef __u64 __le64;
43
44 //
45 // from Linux kernel, fs/ext4/ext4.h
46 //
47
48 /*
49 * Special inode numbers
50 */
51 #define EXT4_BAD_INO 1 /* Bad blocks inode */
52 #define EXT4_ROOT_INO 2 /* Root inode */
53 #define EXT4_USR_QUOTA_INO 3 /* User quota inode */
54 #define EXT4_GRP_QUOTA_INO 4 /* Group quota inode */
55 #define EXT4_BOOT_LOADER_INO 5 /* Boot loader inode */
56 #define EXT4_UNDEL_DIR_INO 6 /* Undelete directory inode */
57 #define EXT4_RESIZE_INO 7 /* Reserved group descriptors inode */
58 #define EXT4_JOURNAL_INO 8 /* Journal inode */
59
60 /*
61 * The second extended file system magic number
62 */
63 #define EXT4_SUPER_MAGIC 0xEF53
64
65 /*
66 * Macro-instructions used to manage several block sizes
67 */
68 #define EXT4_MIN_BLOCK_SIZE 1024
69 #define EXT4_MAX_BLOCK_SIZE 4096
70 #define EXT4_MIN_BLOCK_LOG_SIZE 10
71 #define EXT4_BLOCK_SIZE(s) (EXT4_MIN_BLOCK_SIZE << (s)->s_log_block_size)
72 #define EXT4_ADDR_PER_BLOCK(s) (EXT4_BLOCK_SIZE(s) / sizeof (__u32))
73 #define EXT4_BLOCK_SIZE_BITS(s) ((s)->s_log_block_size + 10)
74 #define EXT4_INODE_SIZE(s) (((s)->s_rev_level == EXT4_GOOD_OLD_REV) ? \
75 EXT4_GOOD_OLD_INODE_SIZE : \
76 (s)->s_inode_size)
77
78 /*
79 * Structure of a blocks group descriptor
80 */
81 struct ext4_group_desc
82 {
83 __le32 bg_block_bitmap_lo; /* Blocks bitmap block */
84 __le32 bg_inode_bitmap_lo; /* Inodes bitmap block */
85 __le32 bg_inode_table_lo; /* Inodes table block */
86 __le16 bg_free_blocks_count_lo;/* Free blocks count */
87 __le16 bg_free_inodes_count_lo;/* Free inodes count */
88 __le16 bg_used_dirs_count_lo; /* Directories count */
89 __le16 bg_flags; /* EXT4_BG_flags (INODE_UNINIT, etc) */
90 __le32 bg_exclude_bitmap_lo; /* Exclude bitmap for snapshots */
91 __le16 bg_block_bitmap_csum_lo;/* crc32c(s_uuid+grp_num+bbitmap) LE */
92 __le16 bg_inode_bitmap_csum_lo;/* crc32c(s_uuid+grp_num+ibitmap) LE */
93 __le16 bg_itable_unused_lo; /* Unused inodes count */
94 __le16 bg_checksum; /* crc16(sb_uuid+group+desc) */
95 __le32 bg_block_bitmap_hi; /* Blocks bitmap block MSB */
96 __le32 bg_inode_bitmap_hi; /* Inodes bitmap block MSB */
97 __le32 bg_inode_table_hi; /* Inodes table block MSB */
98 __le16 bg_free_blocks_count_hi;/* Free blocks count MSB */
99 __le16 bg_free_inodes_count_hi;/* Free inodes count MSB */
100 __le16 bg_used_dirs_count_hi; /* Directories count MSB */
101 __le16 bg_itable_unused_hi; /* Unused inodes count MSB */
102 __le32 bg_exclude_bitmap_hi; /* Exclude bitmap block MSB */
103 __le16 bg_block_bitmap_csum_hi;/* crc32c(s_uuid+grp_num+bbitmap) BE */
104 __le16 bg_inode_bitmap_csum_hi;/* crc32c(s_uuid+grp_num+ibitmap) BE */
105 __u32 bg_reserved;
106 };
107
108
109 /*
110 * Macro-instructions used to manage group descriptors
111 */
112 #define EXT4_DESC_SIZE(s) ((s)->s_desc_size)
113 #define EXT4_BLOCKS_PER_GROUP(s) ((s)->s_blocks_per_group)
114 #define EXT4_DESC_PER_BLOCK(s) (EXT4_BLOCK_SIZE(s) / EXT4_DESC_SIZE(s))
115 #define EXT4_INODES_PER_GROUP(s) ((s)->s_inodes_per_group)
116
117 /*
118 * Constants relative to the data blocks
119 */
120 #define EXT4_NDIR_BLOCKS 12
121 #define EXT4_IND_BLOCK EXT4_NDIR_BLOCKS
122 #define EXT4_DIND_BLOCK (EXT4_IND_BLOCK + 1)
123 #define EXT4_TIND_BLOCK (EXT4_DIND_BLOCK + 1)
124 #define EXT4_N_BLOCKS (EXT4_TIND_BLOCK + 1)
125
126 /*
127 * Inode flags
128 */
129 #define EXT4_SECRM_FL 0x00000001 /* Secure deletion */
130 #define EXT4_UNRM_FL 0x00000002 /* Undelete */
131 #define EXT4_COMPR_FL 0x00000004 /* Compress file */
132 #define EXT4_SYNC_FL 0x00000008 /* Synchronous updates */
133 #define EXT4_IMMUTABLE_FL 0x00000010 /* Immutable file */
134 #define EXT4_APPEND_FL 0x00000020 /* writes to file may only append */
135 #define EXT4_NODUMP_FL 0x00000040 /* do not dump file */
136 #define EXT4_NOATIME_FL 0x00000080 /* do not update atime */
137 /* Reserved for compression usage... */
138 #define EXT4_DIRTY_FL 0x00000100
139 #define EXT4_COMPRBLK_FL 0x00000200 /* One or more compressed clusters */
140 #define EXT4_NOCOMP_FL 0x00000400 /* Don't compress */
141 #define EXT4_ECOMPR_FL 0x00000800 /* Compression error */
142 /* End compression flags --- maybe not all used */
143 #define EXT4_INDEX_FL 0x00001000 /* hash-indexed directory */
144 #define EXT4_IMAGIC_FL 0x00002000 /* AFS directory */
145 #define EXT4_JOURNAL_DATA_FL 0x00004000 /* Reserved for ext3 */
146 #define EXT4_NOTAIL_FL 0x00008000 /* file tail should not be merged */
147 #define EXT4_DIRSYNC_FL 0x00010000 /* dirsync behaviour (directories only) */
148 #define EXT4_TOPDIR_FL 0x00020000 /* Top of directory hierarchies*/
149 #define EXT4_HUGE_FILE_FL 0x00040000 /* Set to each huge file */
150 #define EXT4_EXTENTS_FL 0x00080000 /* Inode uses extents */
151 #define EXT4_EA_INODE_FL 0x00200000 /* Inode used for large EA */
152 #define EXT4_EOFBLOCKS_FL 0x00400000 /* Blocks allocated beyond EOF */
153 #define EXT4_RESERVED_FL 0x80000000 /* reserved for ext4 lib */
154
155 #define EXT4_FL_USER_VISIBLE 0x004BDFFF /* User visible flags */
156 #define EXT4_FL_USER_MODIFIABLE 0x004B80FF /* User modifiable flags */
157
158
159 /*
160 * Structure of an inode on the disk
161 */
162 struct ext4_inode {
163 __le16 i_mode; /* File mode */
164 __le16 i_uid; /* Low 16 bits of Owner Uid */
165 __le32 i_size_lo; /* Size in bytes */
166 __le32 i_atime; /* Access time */
167 __le32 i_ctime; /* Inode Change time */
168 __le32 i_mtime; /* Modification time */
169 __le32 i_dtime; /* Deletion Time */
170 __le16 i_gid; /* Low 16 bits of Group Id */
171 __le16 i_links_count; /* Links count */
172 __le32 i_blocks_lo; /* Blocks count */
173 __le32 i_flags; /* File flags */
174 union {
175 struct {
176 __le32 l_i_version;
177 } linux1;
178 struct {
179 __u32 h_i_translator;
180 } hurd1;
181 struct {
182 __u32 m_i_reserved1;
183 } masix1;
184 } osd1; /* OS dependent 1 */
185 __le32 i_block[EXT4_N_BLOCKS];/* Pointers to blocks */
186 __le32 i_generation; /* File version (for NFS) */
187 __le32 i_file_acl_lo; /* File ACL */
188 __le32 i_size_high;
189 __le32 i_obso_faddr; /* Obsoleted fragment address */
190 union {
191 struct {
192 __le16 l_i_blocks_high; /* were l_i_reserved1 */
193 __le16 l_i_file_acl_high;
194 __le16 l_i_uid_high; /* these 2 fields */
195 __le16 l_i_gid_high; /* were reserved2[0] */
196 __le16 l_i_checksum_lo;/* crc32c(uuid+inum+inode) LE */
197 __le16 l_i_reserved;
198 } linux2;
199 struct {
200 __le16 h_i_reserved1; /* Obsoleted fragment number/size which are removed in ext4 */
201 __u16 h_i_mode_high;
202 __u16 h_i_uid_high;
203 __u16 h_i_gid_high;
204 __u32 h_i_author;
205 } hurd2;
206 struct {
207 __le16 h_i_reserved1; /* Obsoleted fragment number/size which are removed in ext4 */
208 __le16 m_i_file_acl_high;
209 __u32 m_i_reserved2[2];
210 } masix2;
211 } osd2; /* OS dependent 2 */
212 __le16 i_extra_isize;
213 __le16 i_checksum_hi; /* crc32c(uuid+inum+inode) BE */
214 __le32 i_ctime_extra; /* extra Change time (nsec << 2 | epoch) */
215 __le32 i_mtime_extra; /* extra Modification time(nsec << 2 | epoch) */
216 __le32 i_atime_extra; /* extra Access time (nsec << 2 | epoch) */
217 __le32 i_crtime; /* File Creation time */
218 __le32 i_crtime_extra; /* extra FileCreationtime (nsec << 2 | epoch) */
219 __le32 i_version_hi; /* high 32 bits for 64-bit version */
220 };
221
222 /*
223 * Structure of the super block
224 */
225 struct ext4_super_block {
226 /*00*/ __le32 s_inodes_count; /* Inodes count */
227 __le32 s_blocks_count_lo; /* Blocks count */
228 __le32 s_r_blocks_count_lo; /* Reserved blocks count */
229 __le32 s_free_blocks_count_lo; /* Free blocks count */
230 /*10*/ __le32 s_free_inodes_count; /* Free inodes count */
231 __le32 s_first_data_block; /* First Data Block */
232 __le32 s_log_block_size; /* Block size */
233 __le32 s_log_cluster_size; /* Allocation cluster size */
234 /*20*/ __le32 s_blocks_per_group; /* # Blocks per group */
235 __le32 s_clusters_per_group; /* # Clusters per group */
236 __le32 s_inodes_per_group; /* # Inodes per group */
237 __le32 s_mtime; /* Mount time */
238 /*30*/ __le32 s_wtime; /* Write time */
239 __le16 s_mnt_count; /* Mount count */
240 __le16 s_max_mnt_count; /* Maximal mount count */
241 __le16 s_magic; /* Magic signature */
242 __le16 s_state; /* File system state */
243 __le16 s_errors; /* Behaviour when detecting errors */
244 __le16 s_minor_rev_level; /* minor revision level */
245 /*40*/ __le32 s_lastcheck; /* time of last check */
246 __le32 s_checkinterval; /* max. time between checks */
247 __le32 s_creator_os; /* OS */
248 __le32 s_rev_level; /* Revision level */
249 /*50*/ __le16 s_def_resuid; /* Default uid for reserved blocks */
250 __le16 s_def_resgid; /* Default gid for reserved blocks */
251 /*
252 * These fields are for EXT4_DYNAMIC_REV superblocks only.
253 *
254 * Note: the difference between the compatible feature set and
255 * the incompatible feature set is that if there is a bit set
256 * in the incompatible feature set that the kernel doesn't
257 * know about, it should refuse to mount the filesystem.
258 *
259 * e2fsck's requirements are more strict; if it doesn't know
260 * about a feature in either the compatible or incompatible
261 * feature set, it must abort and not try to meddle with
262 * things it doesn't understand...
263 */
264 __le32 s_first_ino; /* First non-reserved inode */
265 __le16 s_inode_size; /* size of inode structure */
266 __le16 s_block_group_nr; /* block group # of this superblock */
267 __le32 s_feature_compat; /* compatible feature set */
268 /*60*/ __le32 s_feature_incompat; /* incompatible feature set */
269 __le32 s_feature_ro_compat; /* readonly-compatible feature set */
270 /*68*/ __u8 s_uuid[16]; /* 128-bit uuid for volume */
271 /*78*/ char s_volume_name[16]; /* volume name */
272 /*88*/ char s_last_mounted[64]; /* directory where last mounted */
273 /*C8*/ __le32 s_algorithm_usage_bitmap; /* For compression */
274 /*
275 * Performance hints. Directory preallocation should only
276 * happen if the EXT4_FEATURE_COMPAT_DIR_PREALLOC flag is on.
277 */
278 __u8 s_prealloc_blocks; /* Nr of blocks to try to preallocate*/
279 __u8 s_prealloc_dir_blocks; /* Nr to preallocate for dirs */
280 __le16 s_reserved_gdt_blocks; /* Per group desc for online growth */
281 /*
282 * Journaling support valid if EXT4_FEATURE_COMPAT_HAS_JOURNAL set.
283 */
284 /*D0*/ __u8 s_journal_uuid[16]; /* uuid of journal superblock */
285 /*E0*/ __le32 s_journal_inum; /* inode number of journal file */
286 __le32 s_journal_dev; /* device number of journal file */
287 __le32 s_last_orphan; /* start of list of inodes to delete */
288 __le32 s_hash_seed[4]; /* HTREE hash seed */
289 __u8 s_def_hash_version; /* Default hash version to use */
290 __u8 s_jnl_backup_type;
291 __le16 s_desc_size; /* size of group descriptor */
292 /*100*/ __le32 s_default_mount_opts;
293 __le32 s_first_meta_bg; /* First metablock block group */
294 __le32 s_mkfs_time; /* When the filesystem was created */
295 __le32 s_jnl_blocks[17]; /* Backup of the journal inode */
296 /* 64bit support valid if EXT4_FEATURE_COMPAT_64BIT */
297 /*150*/ __le32 s_blocks_count_hi; /* Blocks count */
298 __le32 s_r_blocks_count_hi; /* Reserved blocks count */
299 __le32 s_free_blocks_count_hi; /* Free blocks count */
300 __le16 s_min_extra_isize; /* All inodes have at least # bytes */
301 __le16 s_want_extra_isize; /* New inodes should reserve # bytes */
302 __le32 s_flags; /* Miscellaneous flags */
303 __le16 s_raid_stride; /* RAID stride */
304 __le16 s_mmp_update_interval; /* # seconds to wait in MMP checking */
305 __le64 s_mmp_block; /* Block for multi-mount protection */
306 __le32 s_raid_stripe_width; /* blocks on all data disks (N*stride)*/
307 __u8 s_log_groups_per_flex; /* FLEX_BG group size */
308 __u8 s_checksum_type; /* metadata checksum algorithm used */
309 __le16 s_reserved_pad;
310 __le64 s_kbytes_written; /* nr of lifetime kilobytes written */
311 __le32 s_snapshot_inum; /* Inode number of active snapshot */
312 __le32 s_snapshot_id; /* sequential ID of active snapshot */
313 __le64 s_snapshot_r_blocks_count; /* reserved blocks for active
314 snapshot's future use */
315 __le32 s_snapshot_list; /* inode number of the head of the
316 on-disk snapshot list */
317 #define EXT4_S_ERR_START offsetof(struct ext4_super_block, s_error_count)
318 __le32 s_error_count; /* number of fs errors */
319 __le32 s_first_error_time; /* first time an error happened */
320 __le32 s_first_error_ino; /* inode involved in first error */
321 __le64 s_first_error_block; /* block involved of first error */
322 __u8 s_first_error_func[32]; /* function where the error happened */
323 __le32 s_first_error_line; /* line number where error happened */
324 __le32 s_last_error_time; /* most recent time of an error */
325 __le32 s_last_error_ino; /* inode involved in last error */
326 __le32 s_last_error_line; /* line number where error happened */
327 __le64 s_last_error_block; /* block involved of last error */
328 __u8 s_last_error_func[32]; /* function where the error happened */
329 #define EXT4_S_ERR_END offsetof(struct ext4_super_block, s_mount_opts)
330 __u8 s_mount_opts[64];
331 __le32 s_usr_quota_inum; /* inode for tracking user quota */
332 __le32 s_grp_quota_inum; /* inode for tracking group quota */
333 __le32 s_overhead_clusters; /* overhead blocks/clusters in fs */
334 __le32 s_reserved[108]; /* Padding to the end of the block */
335 __le32 s_checksum; /* crc32c(superblock) */
336 };
337 /*
338 * Revision levels
339 */
340 #define EXT4_GOOD_OLD_REV 0 /* The good old (original) format */
341 #define EXT4_DYNAMIC_REV 1 /* V2 format w/ dynamic inode sizes */
342
343 #define EXT4_CURRENT_REV EXT4_GOOD_OLD_REV
344 #define EXT4_MAX_SUPP_REV EXT4_DYNAMIC_REV
345
346 #define EXT4_GOOD_OLD_INODE_SIZE 128
347
348 /*
349 * Feature set definitions (only the once we need for read support)
350 */
351
352 #define EXT4_FEATURE_INCOMPAT_COMPRESSION 0x0001
353 #define EXT4_FEATURE_INCOMPAT_FILETYPE 0x0002
354 #define EXT4_FEATURE_INCOMPAT_RECOVER 0x0004 /* Needs recovery */
355 #define EXT4_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008 /* Journal device */
356 #define EXT4_FEATURE_INCOMPAT_META_BG 0x0010
357 #define EXT4_FEATURE_INCOMPAT_EXTENTS 0x0040 /* extents support */
358 #define EXT4_FEATURE_INCOMPAT_64BIT 0x0080
359 #define EXT4_FEATURE_INCOMPAT_MMP 0x0100
360 #define EXT4_FEATURE_INCOMPAT_FLEX_BG 0x0200
361 #define EXT4_FEATURE_INCOMPAT_EA_INODE 0x0400 /* EA in inode */
362 #define EXT4_FEATURE_INCOMPAT_DIRDATA 0x1000 /* data in dirent */
363 #define EXT4_FEATURE_INCOMPAT_BG_USE_META_CSUM 0x2000 /* use crc32c for bg */
364 #define EXT4_FEATURE_INCOMPAT_LARGEDIR 0x4000 /* >2GB or 3-lvl htree */
365 #define EXT4_FEATURE_INCOMPAT_INLINEDATA 0x8000 /* data in inode */
366
367 #define EXT4_FEATURE_INCOMPAT_SUPP (EXT4_FEATURE_INCOMPAT_FILETYPE| \
368 EXT4_FEATURE_INCOMPAT_RECOVER| \
369 EXT4_FEATURE_INCOMPAT_META_BG| \
370 EXT4_FEATURE_INCOMPAT_EXTENTS| \
371 EXT4_FEATURE_INCOMPAT_64BIT| \
372 EXT4_FEATURE_INCOMPAT_FLEX_BG| \
373 EXT4_FEATURE_INCOMPAT_MMP)
374
375 /*
376 * Structure of a directory entry
377 */
378 #define EXT4_NAME_LEN 255
379
380 struct ext4_dir_entry {
381 __le32 inode; /* Inode number */
382 __le16 rec_len; /* Directory entry length */
383 __u8 name_len; /* Name length */
384 __u8 file_type;
385 char name[EXT4_NAME_LEN]; /* File name */
386 };
387 // NOTE: The original Linux kernel header defines ext4_dir_entry with the original
388 // layout and ext4_dir_entry_2 with the revised layout. We simply use the revised one.
389
390 /*
391 * Ext2 directory file types. Only the low 3 bits are used. The
392 * other bits are reserved for now.
393 */
394 enum {
395 EXT4_FT_UNKNOWN,
396 EXT4_FT_REG_FILE,
397 EXT4_FT_DIR,
398 EXT4_FT_CHRDEV,
399 EXT4_FT_BLKDEV,
400 EXT4_FT_FIFO,
401 EXT4_FT_SOCK,
402 EXT4_FT_SYMLINK,
403 EXT4_FT_MAX
404 };
405
406
407 #endif