ext2fs.h revision 311232
159078Smdodd/*- 259078Smdodd * modified for EXT2FS support in Lites 1.1 359078Smdodd * 434480Sjulian * Aug 1995, Godmar Back (gback@cs.utah.edu) 534480Sjulian * University of Utah, Department of Computer Science 634480Sjulian * 734480Sjulian * $FreeBSD: stable/10/sys/fs/ext2fs/ext2fs.h 311232 2017-01-04 02:43:33Z pfg $ 834480Sjulian */ 959078Smdodd/*- 1034480Sjulian * Copyright (c) 2009 Aditya Sarawgi 1134480Sjulian * All rights reserved. 1234480Sjulian * 1334480Sjulian * Redistribution and use in source and binary forms, with or without 1434480Sjulian * modification, are permitted provided that the following conditions 1534480Sjulian * are met: 1634480Sjulian * 1. Redistributions of source code must retain the above copyright 1759078Smdodd * notice, this list of conditions and the following disclaimer. 1859078Smdodd * 2. Redistributions in binary form must reproduce the above copyright 1934480Sjulian * notice, this list of conditions and the following disclaimer in the 2034480Sjulian * documentation and/or other materials provided with the distribution. 2134480Sjulian * 2234480Sjulian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 2334480Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2434480Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2559078Smdodd * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2634480Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2734480Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28119418Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29119418Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30119418Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31166091Smarius * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32166091Smarius * SUCH DAMAGE. 3334480Sjulian * 3434480Sjulian * 3534480Sjulian */ 3645791Speter 37117126Sscottl#ifndef _FS_EXT2FS_EXT2FS_H_ 38117126Sscottl#define _FS_EXT2FS_EXT2FS_H_ 3945791Speter 4034480Sjulian#include <sys/types.h> 4139234Sgibbs 4245791Speter/* 4345791Speter * Super block for an ext2fs file system. 4434480Sjulian */ 4559078Smdoddstruct ext2fs { 4659078Smdodd uint32_t e2fs_icount; /* Inode count */ 4739234Sgibbs uint32_t e2fs_bcount; /* blocks count */ 4839234Sgibbs uint32_t e2fs_rbcount; /* reserved blocks count */ 4939234Sgibbs uint32_t e2fs_fbcount; /* free blocks count */ 5039234Sgibbs uint32_t e2fs_ficount; /* free inodes count */ 51112780Smdodd uint32_t e2fs_first_dblock; /* first data block */ 5252042Smdodd uint32_t e2fs_log_bsize; /* block size = 1024*(2^e2fs_log_bsize) */ 5352042Smdodd uint32_t e2fs_log_fsize; /* fragment size */ 5452042Smdodd uint32_t e2fs_bpg; /* blocks per group */ 5559078Smdodd uint32_t e2fs_fpg; /* frags per group */ 5659078Smdodd uint32_t e2fs_ipg; /* inodes per group */ 5759078Smdodd uint32_t e2fs_mtime; /* mount time */ 5859078Smdodd uint32_t e2fs_wtime; /* write time */ 5959078Smdodd uint16_t e2fs_mnt_count; /* mount count */ 6059078Smdodd uint16_t e2fs_max_mnt_count; /* max mount count */ 6159078Smdodd uint16_t e2fs_magic; /* magic number */ 6259078Smdodd uint16_t e2fs_state; /* file system state */ 6359078Smdodd uint16_t e2fs_beh; /* behavior on errors */ 6459078Smdodd uint16_t e2fs_minrev; /* minor revision level */ 6559078Smdodd uint32_t e2fs_lastfsck; /* time of last fsck */ 6659078Smdodd uint32_t e2fs_fsckintv; /* max time between fscks */ 6759078Smdodd uint32_t e2fs_creator; /* creator OS */ 6852042Smdodd uint32_t e2fs_rev; /* revision level */ 6934480Sjulian uint16_t e2fs_ruid; /* default uid for reserved blocks */ 7034480Sjulian uint16_t e2fs_rgid; /* default gid for reserved blocks */ 7159078Smdodd /* EXT2_DYNAMIC_REV superblocks */ 7259078Smdodd uint32_t e2fs_first_ino; /* first non-reserved inode */ 7359078Smdodd uint16_t e2fs_inode_size; /* size of inode structure */ 7434480Sjulian uint16_t e2fs_block_group_nr; /* block grp number of this sblk*/ 7539234Sgibbs uint32_t e2fs_features_compat; /* compatible feature set */ 7659078Smdodd uint32_t e2fs_features_incompat; /* incompatible feature set */ 7734480Sjulian uint32_t e2fs_features_rocompat; /* RO-compatible feature set */ 7852042Smdodd uint8_t e2fs_uuid[16]; /* 128-bit uuid for volume */ 7952042Smdodd char e2fs_vname[16]; /* volume name */ 8052042Smdodd char e2fs_fsmnt[64]; /* name mounted on */ 8134480Sjulian uint32_t e2fs_algo; /* For compression */ 8245791Speter uint8_t e2fs_prealloc; /* # of blocks for old prealloc */ 8345791Speter uint8_t e2fs_dir_prealloc; /* # of blocks for old prealloc dirs */ 8445791Speter uint16_t e2fs_reserved_ngdb; /* # of reserved gd blocks for resize */ 8545791Speter char e3fs_journal_uuid[16]; /* uuid of journal superblock */ 8634480Sjulian uint32_t e3fs_journal_inum; /* inode number of journal file */ 87112780Smdodd uint32_t e3fs_journal_dev; /* device number of journal file */ 88112780Smdodd uint32_t e3fs_last_orphan; /* start of list of inodes to delete */ 89112780Smdodd uint32_t e3fs_hash_seed[4]; /* HTREE hash seed */ 9045791Speter char e3fs_def_hash_version; /* Default hash version to use */ 91112780Smdodd char e3fs_jnl_backup_type; 9252042Smdodd uint16_t e3fs_desc_size; /* size of group descriptor */ 9352042Smdodd uint32_t e3fs_default_mount_opts; 9452042Smdodd uint32_t e3fs_first_meta_bg; /* First metablock block group */ 9545791Speter uint32_t e3fs_mkfs_time; /* when the fs was created */ 9634480Sjulian uint32_t e3fs_jnl_blks[17]; /* backup of the journal inode */ 9752042Smdodd uint32_t e4fs_bcount_hi; /* high bits of blocks count */ 9852042Smdodd uint32_t e4fs_rbcount_hi; /* high bits of reserved blocks count */ 9952042Smdodd uint32_t e4fs_fbcount_hi; /* high bits of free blocks count */ 10045791Speter uint16_t e4fs_min_extra_isize; /* all inodes have at least some bytes */ 10145791Speter uint16_t e4fs_want_extra_isize; /* inodes must reserve some bytes */ 10234480Sjulian uint32_t e4fs_flags; /* miscellaneous flags */ 10334480Sjulian uint16_t e4fs_raid_stride; /* RAID stride */ 10445791Speter uint16_t e4fs_mmpintv; /* number of seconds to wait in MMP checking */ 10559078Smdodd uint64_t e4fs_mmpblk; /* block for multi-mount protection */ 10634480Sjulian uint32_t e4fs_raid_stripe_wid; /* blocks on all data disks (N * stride) */ 107112780Smdodd uint8_t e4fs_log_gpf; /* FLEX_BG group size */ 10859078Smdodd uint8_t e4fs_chksum_type; /* metadata checksum algorithm used */ 10934480Sjulian uint8_t e4fs_encrypt; /* versioning level for encryption */ 110112780Smdodd uint8_t e4fs_reserved_pad; 111170872Sscottl uint64_t e4fs_kbytes_written; /* number of lifetime kilobytes */ 112241593Sjhb uint32_t e4fs_snapinum; /* inode number of active snapshot */ 11334480Sjulian uint32_t e4fs_snapid; /* sequential ID of active snapshot */ 114112780Smdodd uint64_t e4fs_snaprbcount; /* reserved blocks for active snapshot */ 115112780Smdodd uint32_t e4fs_snaplist; /* inode number for on-disk snapshot */ 116112780Smdodd uint32_t e4fs_errcount; /* number of file system errors */ 11759078Smdodd uint32_t e4fs_first_errtime; /* first time an error happened */ 118112780Smdodd uint32_t e4fs_first_errino; /* inode involved in first error */ 119112780Smdodd uint64_t e4fs_first_errblk; /* block involved of first error */ 12045791Speter uint8_t e4fs_first_errfunc[32]; /* function where error happened */ 12159078Smdodd uint32_t e4fs_first_errline; /* line number where error happened */ 12234480Sjulian uint32_t e4fs_last_errtime; /* most recent time of an error */ 12339234Sgibbs uint32_t e4fs_last_errino; /* inode involved in last error */ 124241593Sjhb uint32_t e4fs_last_errline; /* line number where error happened */ 12559078Smdodd uint64_t e4fs_last_errblk; /* block involved of last error */ 12659078Smdodd uint8_t e4fs_last_errfunc[32]; /* function where error happened */ 12759078Smdodd uint8_t e4fs_mount_opts[64]; 12859078Smdodd uint32_t e4fs_usrquota_inum; /* inode for tracking user quota */ 12959078Smdodd uint32_t e4fs_grpquota_inum; /* inode for tracking group quota */ 13059078Smdodd uint32_t e4fs_overhead_clusters; /* overhead blocks/clusters */ 13159078Smdodd uint32_t e4fs_backup_bgs[2]; /* groups with sparse_super2 SBs */ 132104710Speter uint8_t e4fs_encrypt_algos[4]; /* encryption algorithms in use */ 13359078Smdodd uint8_t e4fs_encrypt_pw_salt[16]; /* salt used for string2key */ 134117126Sscottl uint32_t e4fs_lpf_ino; /* location of the lost+found inode */ 135241593Sjhb uint32_t e4fs_proj_quota_inum; /* inode for tracking project quota */ 136241593Sjhb uint32_t e4fs_chksum_seed; /* checksum seed */ 13759078Smdodd uint32_t e4fs_reserved[98]; /* padding to the end of the block */ 13859078Smdodd uint32_t e4fs_sbchksum; /* superblock checksum */ 13945791Speter}; 14034480Sjulian 14134480Sjulian/* 14239234Sgibbs * The path name on which the file system is mounted is maintained 14359078Smdodd * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in 14445791Speter * the super block for this name. 14534480Sjulian */ 14634480Sjulian#define MAXMNTLEN 512 14739234Sgibbs 14839234Sgibbs/* 14945791Speter * In-Memory Superblock 150241593Sjhb */ 151241593Sjhb 15259078Smdoddstruct m_ext2fs { 15359078Smdodd struct ext2fs * e2fs; 15459078Smdodd char e2fs_fsmnt[MAXMNTLEN];/* name mounted on */ 15559078Smdodd char e2fs_ronly; /* mounted read-only flag */ 15645791Speter char e2fs_fmod; /* super block modified flag */ 15759078Smdodd uint32_t e2fs_bsize; /* Block size */ 15859078Smdodd uint32_t e2fs_bshift; /* calc of logical block no */ 15945791Speter uint32_t e2fs_bpg; /* Number of blocks per group */ 160112780Smdodd int64_t e2fs_qbmask; /* = s_blocksize -1 */ 16159078Smdodd uint32_t e2fs_fsbtodb; /* Shift to get disk block */ 162142357Ssam uint32_t e2fs_ipg; /* Number of inodes per group */ 163112780Smdodd uint32_t e2fs_ipb; /* Number of inodes per block */ 16459078Smdodd uint32_t e2fs_itpg; /* Number of inode table per group */ 16534480Sjulian uint32_t e2fs_fsize; /* Size of fragments per block */ 16634480Sjulian uint32_t e2fs_fpb; /* Number of fragments per block */ 16734480Sjulian uint32_t e2fs_fpg; /* Number of fragments per group */ 16834480Sjulian uint32_t e2fs_gdbcount; /* Number of group descriptors */ 16934480Sjulian uint32_t e2fs_gcount; /* Number of groups */ 17034480Sjulian uint32_t e2fs_isize; /* Size of inode */ 17134480Sjulian uint32_t e2fs_total_dir; /* Total number of directories */ 17259078Smdodd uint8_t *e2fs_contigdirs; /* (u) # of contig. allocated dirs */ 17359078Smdodd char e2fs_wasvalid; /* valid at mount time */ 17459078Smdodd off_t e2fs_maxfilesize; 17559078Smdodd struct ext2_gd *e2fs_gd; /* Group Descriptors */ 17659078Smdodd int32_t e2fs_contigsumsize; /* size of cluster summary array */ 17759078Smdodd int32_t *e2fs_maxcluster; /* max cluster in each cyl group */ 17859078Smdodd struct csum *e2fs_clustersum; /* cluster summary in each cyl group */ 17959078Smdodd int32_t e2fs_uhash; /* 3 if hash should be signed, 0 if not */ 18059078Smdodd}; 18159078Smdodd 18259078Smdodd/* cluster summary information */ 18359078Smdodd 18459078Smdoddstruct csum { 18559078Smdodd int8_t cs_init; /* cluster summary has been initialized */ 18634480Sjulian int32_t *cs_sum; /* cluster summary array */ 18734480Sjulian}; 18834480Sjulian 18934480Sjulian/* 19034480Sjulian * The second extended file system magic number 19134480Sjulian */ 19234480Sjulian#define E2FS_MAGIC 0xEF53 19334480Sjulian 19445791Speter/* 19545791Speter * Revision levels 19645791Speter */ 19745791Speter#define E2FS_REV0 0 /* The good old (original) format */ 198112780Smdodd#define E2FS_REV1 1 /* V2 format w/ dynamic inode sizes */ 19945791Speter 20045791Speter#define E2FS_REV0_INODE_SIZE 128 20145791Speter 20245791Speter/* 20345791Speter * compatible/incompatible features 20445791Speter */ 20545791Speter#define EXT2F_COMPAT_PREALLOC 0x0001 20659078Smdodd#define EXT2F_COMPAT_HASJOURNAL 0x0004 20745791Speter#define EXT2F_COMPAT_RESIZE 0x0010 20845791Speter#define EXT2F_COMPAT_DIRHASHINDEX 0x0020 20945791Speter#define EXT2F_COMPAT_SPARSESUPER2 0x0200 210165102Smjacob 211165102Smjacob#define EXT2F_ROCOMPAT_SPARSESUPER 0x0001 212#define EXT2F_ROCOMPAT_LARGEFILE 0x0002 213#define EXT2F_ROCOMPAT_BTREE_DIR 0x0004 214#define EXT2F_ROCOMPAT_HUGE_FILE 0x0008 215#define EXT2F_ROCOMPAT_GDT_CSUM 0x0010 216#define EXT2F_ROCOMPAT_DIR_NLINK 0x0020 217#define EXT2F_ROCOMPAT_EXTRA_ISIZE 0x0040 218#define EXT2F_ROCOMPAT_QUOTA 0x0100 219#define EXT2F_ROCOMPAT_BIGALLOC 0x0200 220#define EXT2F_ROCOMPAT_METADATA_CKSUM 0x0400 221#define EXT2F_ROCOMPAT_READONLY 0x1000 222#define EXT2F_ROCOMPAT_PROJECT 0x2000 223 224#define EXT2F_INCOMPAT_COMP 0x0001 225#define EXT2F_INCOMPAT_FTYPE 0x0002 226#define EXT2F_INCOMPAT_RECOVER 0x0004 227#define EXT2F_INCOMPAT_META_BG 0x0010 228#define EXT2F_INCOMPAT_EXTENTS 0x0040 229#define EXT2F_INCOMPAT_64BIT 0x0080 230#define EXT2F_INCOMPAT_MMP 0x0100 231#define EXT2F_INCOMPAT_FLEX_BG 0x0200 232#define EXT2F_INCOMPAT_EA_INODE 0x0400 233#define EXT2F_INCOMPAT_DIRDATA 0x1000 234#define EXT2F_INCOMPAT_CSUM_SEED 0x2000 235#define EXT2F_INCOMPAT_LARGEDIR 0x4000 236#define EXT2F_INCOMPAT_INLINE_DATA 0x8000 237#define EXT2F_INCOMPAT_ENCRYPT 0x10000 238 239/* 240 * Features supported in this implementation 241 * 242 * We support the following REV1 features: 243 * - EXT2F_ROCOMPAT_SPARSESUPER 244 * - EXT2F_ROCOMPAT_LARGEFILE 245 * - EXT2F_ROCOMPAT_EXTRA_ISIZE 246 * - EXT2F_INCOMPAT_FTYPE 247 * 248 * We partially support (read-only) the following EXT4 features: 249 * - EXT2F_ROCOMPAT_HUGE_FILE 250 * - EXT2F_INCOMPAT_EXTENTS 251 * 252 * We do not support these EXT4 features but they are irrelevant 253 * for read-only support: 254 * - EXT2F_INCOMPAT_RECOVER 255 * - EXT2F_INCOMPAT_FLEX_BG 256 * - EXT2F_INCOMPAT_META_BG 257 */ 258#define EXT2F_COMPAT_SUPP EXT2F_COMPAT_DIRHASHINDEX 259#define EXT2F_ROCOMPAT_SUPP (EXT2F_ROCOMPAT_SPARSESUPER | \ 260 EXT2F_ROCOMPAT_LARGEFILE | \ 261 EXT2F_ROCOMPAT_EXTRA_ISIZE) 262#define EXT2F_INCOMPAT_SUPP EXT2F_INCOMPAT_FTYPE 263#define EXT4F_RO_INCOMPAT_SUPP (EXT2F_INCOMPAT_EXTENTS | \ 264 EXT2F_INCOMPAT_RECOVER | \ 265 EXT2F_INCOMPAT_FLEX_BG | \ 266 EXT2F_INCOMPAT_META_BG ) 267 268/* Assume that user mode programs are passing in an ext2fs superblock, not 269 * a kernel struct super_block. This will allow us to call the feature-test 270 * macros from user land. */ 271#define EXT2_SB(sb) (sb) 272 273/* 274 * Feature set definitions 275 */ 276#define EXT2_HAS_COMPAT_FEATURE(sb,mask) \ 277 ( EXT2_SB(sb)->e2fs->e2fs_features_compat & htole32(mask) ) 278#define EXT2_HAS_RO_COMPAT_FEATURE(sb,mask) \ 279 ( EXT2_SB(sb)->e2fs->e2fs_features_rocompat & htole32(mask) ) 280#define EXT2_HAS_INCOMPAT_FEATURE(sb,mask) \ 281 ( EXT2_SB(sb)->e2fs->e2fs_features_incompat & htole32(mask) ) 282 283/* 284 * File clean flags 285 */ 286#define E2FS_ISCLEAN 0x0001 /* Unmounted cleanly */ 287#define E2FS_ERRORS 0x0002 /* Errors detected */ 288 289/* 290 * Filesystem miscellaneous flags 291 */ 292#define E2FS_SIGNED_HASH 0x0001 293#define E2FS_UNSIGNED_HASH 0x0002 294 295/* ext2 file system block group descriptor */ 296 297struct ext2_gd { 298 uint32_t ext2bgd_b_bitmap; /* blocks bitmap block */ 299 uint32_t ext2bgd_i_bitmap; /* inodes bitmap block */ 300 uint32_t ext2bgd_i_tables; /* inodes table block */ 301 uint16_t ext2bgd_nbfree; /* number of free blocks */ 302 uint16_t ext2bgd_nifree; /* number of free inodes */ 303 uint16_t ext2bgd_ndirs; /* number of directories */ 304 uint16_t ext4bgd_flags; /* block group flags */ 305 uint32_t ext4bgd_x_bitmap; /* snapshot exclusion bitmap loc. */ 306 uint16_t ext4bgd_b_bmap_csum; /* block bitmap checksum */ 307 uint16_t ext4bgd_i_bmap_csum; /* inode bitmap checksum */ 308 uint16_t ext4bgd_i_unused; /* unused inode count */ 309 uint16_t ext4bgd_csum; /* group descriptor checksum */ 310}; 311 312 313/* EXT2FS metadatas are stored in little-endian byte order. These macros 314 * helps reading these metadatas 315 */ 316 317#define e2fs_cgload(old, new, size) memcpy((new), (old), (size)); 318#define e2fs_cgsave(old, new, size) memcpy((new), (old), (size)); 319 320/* 321 * Macro-instructions used to manage several block sizes 322 */ 323#define EXT2_MAX_BLOCK_SIZE 4096 324#define EXT2_MIN_BLOCK_LOG_SIZE 10 325#define EXT2_BLOCK_SIZE(s) ((s)->e2fs_bsize) 326#define EXT2_ADDR_PER_BLOCK(s) (EXT2_BLOCK_SIZE(s) / sizeof(uint32_t)) 327#define EXT2_INODE_SIZE(s) (EXT2_SB(s)->e2fs_isize) 328 329/* 330 * Macro-instructions used to manage fragments 331 */ 332#define EXT2_MIN_FRAG_SIZE 1024 333#define EXT2_MAX_FRAG_SIZE 4096 334#define EXT2_MIN_FRAG_LOG_SIZE 10 335#define EXT2_FRAG_SIZE(s) (EXT2_SB(s)->e2fs_fsize) 336#define EXT2_FRAGS_PER_BLOCK(s) (EXT2_SB(s)->e2fs_fpb) 337 338/* 339 * Macro-instructions used to manage group descriptors 340 */ 341#define EXT2_BLOCKS_PER_GROUP(s) (EXT2_SB(s)->e2fs_bpg) 342 343#endif /* !_FS_EXT2FS_EXT2FS_H_ */ 344