ext2fs.h revision 297480
1158782Sume/*- 2269867Sume * modified for EXT2FS support in Lites 1.1 3158782Sume * 4158782Sume * Aug 1995, Godmar Back (gback@cs.utah.edu) 5158782Sume * University of Utah, Department of Computer Science 6158782Sume * 7158782Sume * $FreeBSD: stable/10/sys/fs/ext2fs/ext2fs.h 297480 2016-04-01 03:46:16Z kevlo $ 8158782Sume */ 9158782Sume/*- 10158782Sume * Copyright (c) 2009 Aditya Sarawgi 11158782Sume * All rights reserved. 12158782Sume * 13158782Sume * Redistribution and use in source and binary forms, with or without 14158782Sume * modification, are permitted provided that the following conditions 15158782Sume * are met: 16158782Sume * 1. Redistributions of source code must retain the above copyright 17158782Sume * notice, this list of conditions and the following disclaimer. 18158782Sume * 2. Redistributions in binary form must reproduce the above copyright 19158782Sume * notice, this list of conditions and the following disclaimer in the 20158782Sume * documentation and/or other materials provided with the distribution. 21158782Sume * 22170244Sume * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 23170244Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24158782Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25170244Sume * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 26158782Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27158782Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28158787Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29158787Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30158787Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31158782Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32158782Sume * SUCH DAMAGE. 33158782Sume * 34158782Sume * 35158782Sume */ 36158782Sume 37158782Sume#ifndef _FS_EXT2FS_EXT2FS_H_ 38158782Sume#define _FS_EXT2FS_EXT2FS_H_ 39158782Sume 40158782Sume#include <sys/types.h> 41158782Sume 42158782Sume/* 43158782Sume * Super block for an ext2fs file system. 44158782Sume */ 45158782Sumestruct ext2fs { 46158782Sume uint32_t e2fs_icount; /* Inode count */ 47158782Sume uint32_t e2fs_bcount; /* blocks count */ 48158782Sume uint32_t e2fs_rbcount; /* reserved blocks count */ 49158782Sume uint32_t e2fs_fbcount; /* free blocks count */ 50158782Sume uint32_t e2fs_ficount; /* free inodes count */ 51158782Sume uint32_t e2fs_first_dblock; /* first data block */ 52158782Sume uint32_t e2fs_log_bsize; /* block size = 1024*(2^e2fs_log_bsize) */ 53158782Sume uint32_t e2fs_log_fsize; /* fragment size */ 54158782Sume uint32_t e2fs_bpg; /* blocks per group */ 55158782Sume uint32_t e2fs_fpg; /* frags per group */ 56170244Sume uint32_t e2fs_ipg; /* inodes per group */ 57158782Sume uint32_t e2fs_mtime; /* mount time */ 58158782Sume uint32_t e2fs_wtime; /* write time */ 59158782Sume uint16_t e2fs_mnt_count; /* mount count */ 60158782Sume uint16_t e2fs_max_mnt_count; /* max mount count */ 61158782Sume uint16_t e2fs_magic; /* magic number */ 62158782Sume uint16_t e2fs_state; /* file system state */ 63158782Sume uint16_t e2fs_beh; /* behavior on errors */ 64158782Sume uint16_t e2fs_minrev; /* minor revision level */ 65158782Sume uint32_t e2fs_lastfsck; /* time of last fsck */ 66158782Sume uint32_t e2fs_fsckintv; /* max time between fscks */ 67158782Sume uint32_t e2fs_creator; /* creator OS */ 68158782Sume uint32_t e2fs_rev; /* revision level */ 69158782Sume uint16_t e2fs_ruid; /* default uid for reserved blocks */ 70158782Sume uint16_t e2fs_rgid; /* default gid for reserved blocks */ 71158782Sume /* EXT2_DYNAMIC_REV superblocks */ 72158782Sume uint32_t e2fs_first_ino; /* first non-reserved inode */ 73158782Sume uint16_t e2fs_inode_size; /* size of inode structure */ 74158782Sume uint16_t e2fs_block_group_nr; /* block grp number of this sblk*/ 75158782Sume uint32_t e2fs_features_compat; /* compatible feature set */ 76158782Sume uint32_t e2fs_features_incompat; /* incompatible feature set */ 77158782Sume uint32_t e2fs_features_rocompat; /* RO-compatible feature set */ 78158782Sume uint8_t e2fs_uuid[16]; /* 128-bit uuid for volume */ 79158782Sume char e2fs_vname[16]; /* volume name */ 80158782Sume char e2fs_fsmnt[64]; /* name mounted on */ 81158782Sume uint32_t e2fs_algo; /* For compression */ 82158782Sume uint8_t e2fs_prealloc; /* # of blocks for old prealloc */ 83158782Sume uint8_t e2fs_dir_prealloc; /* # of blocks for old prealloc dirs */ 84158782Sume uint16_t e2fs_reserved_ngdb; /* # of reserved gd blocks for resize */ 85158782Sume char e3fs_journal_uuid[16]; /* uuid of journal superblock */ 86158782Sume uint32_t e3fs_journal_inum; /* inode number of journal file */ 87158782Sume uint32_t e3fs_journal_dev; /* device number of journal file */ 88158782Sume uint32_t e3fs_last_orphan; /* start of list of inodes to delete */ 89158782Sume uint32_t e3fs_hash_seed[4]; /* HTREE hash seed */ 90158782Sume char e3fs_def_hash_version; /* Default hash version to use */ 91158782Sume char e3fs_jnl_backup_type; 92158782Sume uint16_t e3fs_desc_size; /* size of group descriptor */ 93158782Sume uint32_t e3fs_default_mount_opts; 94158782Sume uint32_t e3fs_first_meta_bg; /* First metablock block group */ 95158782Sume uint32_t e3fs_mkfs_time; /* when the fs was created */ 96158782Sume uint32_t e3fs_jnl_blks[17]; /* backup of the journal inode */ 97158782Sume uint32_t e4fs_bcount_hi; /* high bits of blocks count */ 98158782Sume uint32_t e4fs_rbcount_hi; /* high bits of reserved blocks count */ 99158782Sume uint32_t e4fs_fbcount_hi; /* high bits of free blocks count */ 100158782Sume uint16_t e4fs_min_extra_isize; /* all inodes have at least some bytes */ 101158782Sume uint16_t e4fs_want_extra_isize; /* inodes must reserve some bytes */ 102158782Sume uint32_t e4fs_flags; /* miscellaneous flags */ 103158782Sume uint16_t e4fs_raid_stride; /* RAID stride */ 104158782Sume uint16_t e4fs_mmpintv; /* number of seconds to wait in MMP checking */ 105158782Sume uint64_t e4fs_mmpblk; /* block for multi-mount protection */ 106158782Sume uint32_t e4fs_raid_stripe_wid; /* blocks on all data disks (N * stride) */ 107158782Sume uint8_t e4fs_log_gpf; /* FLEX_BG group size */ 108158782Sume uint8_t e4fs_chksum_type; /* metadata checksum algorithm used */ 109158782Sume uint8_t e4fs_encrypt; /* versioning level for encryption */ 110158782Sume uint8_t e4fs_reserved_pad; 111158782Sume uint64_t e4fs_kbytes_written; /* number of lifetime kilobytes */ 112158782Sume uint32_t e4fs_snapinum; /* inode number of active snapshot */ 113158782Sume uint32_t e4fs_snapid; /* sequential ID of active snapshot */ 114158782Sume uint64_t e4fs_snaprbcount; /* reserved blocks for active snapshot */ 115158782Sume uint32_t e4fs_snaplist; /* inode number for on-disk snapshot */ 116158782Sume uint32_t e4fs_errcount; /* number of file system errors */ 117158782Sume uint32_t e4fs_first_errtime; /* first time an error happened */ 118158782Sume uint32_t e4fs_first_errino; /* inode involved in first error */ 119158782Sume uint64_t e4fs_first_errblk; /* block involved of first error */ 120158782Sume uint8_t e4fs_first_errfunc[32]; /* function where error happened */ 121158782Sume uint32_t e4fs_first_errline; /* line number where error happened */ 122158782Sume uint32_t e4fs_last_errtime; /* most recent time of an error */ 123158782Sume uint32_t e4fs_last_errino; /* inode involved in last error */ 124158782Sume uint32_t e4fs_last_errline; /* line number where error happened */ 125158782Sume uint64_t e4fs_last_errblk; /* block involved of last error */ 126158782Sume uint8_t e4fs_last_errfunc[32]; /* function where error happened */ 127158782Sume uint8_t e4fs_mount_opts[64]; 128158782Sume uint32_t e4fs_usrquota_inum; /* inode for tracking user quota */ 129158782Sume uint32_t e4fs_grpquota_inum; /* inode for tracking group quota */ 130158782Sume uint32_t e4fs_overhead_clusters; /* overhead blocks/clusters */ 131158782Sume uint32_t e4fs_backup_bgs[2]; /* groups with sparse_super2 SBs */ 132158782Sume uint8_t e4fs_encrypt_algos[4]; /* encryption algorithms in use */ 133158782Sume uint8_t e4fs_encrypt_pw_salt[16]; /* salt used for string2key */ 134158782Sume uint32_t e4fs_lpf_ino; /* location of the lost+found inode */ 135158782Sume uint32_t e4fs_proj_quota_inum; /* inode for tracking project quota */ 136158782Sume uint32_t e4fs_chksum_seed; /* checksum seed */ 137158782Sume uint32_t e4fs_reserved[98]; /* padding to the end of the block */ 138158782Sume uint32_t e4fs_sbchksum; /* superblock checksum */ 139158782Sume}; 140158782Sume 141158782Sume/* 142158782Sume * The path name on which the file system is mounted is maintained 143158782Sume * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in 144158782Sume * the super block for this name. 145158782Sume */ 146158782Sume#define MAXMNTLEN 512 147158782Sume 148158782Sume/* 149158782Sume * In-Memory Superblock 150158782Sume */ 151158782Sume 152158782Sumestruct m_ext2fs { 153158782Sume struct ext2fs * e2fs; 154158782Sume char e2fs_fsmnt[MAXMNTLEN];/* name mounted on */ 155158782Sume char e2fs_ronly; /* mounted read-only flag */ 156158782Sume char e2fs_fmod; /* super block modified flag */ 157158782Sume uint32_t e2fs_bsize; /* Block size */ 158158782Sume uint32_t e2fs_bshift; /* calc of logical block no */ 159158782Sume uint32_t e2fs_bpg; /* Number of blocks per group */ 160158782Sume int64_t e2fs_qbmask; /* = s_blocksize -1 */ 161158782Sume uint32_t e2fs_fsbtodb; /* Shift to get disk block */ 162158782Sume uint32_t e2fs_ipg; /* Number of inodes per group */ 163158782Sume uint32_t e2fs_ipb; /* Number of inodes per block */ 164158782Sume uint32_t e2fs_itpg; /* Number of inode table per group */ 165158782Sume uint32_t e2fs_fsize; /* Size of fragments per block */ 166158782Sume uint32_t e2fs_fpb; /* Number of fragments per block */ 167158782Sume uint32_t e2fs_fpg; /* Number of fragments per group */ 168158782Sume uint32_t e2fs_gdbcount; /* Number of group descriptors */ 169158782Sume uint32_t e2fs_gcount; /* Number of groups */ 170158782Sume uint32_t e2fs_isize; /* Size of inode */ 171158782Sume uint32_t e2fs_total_dir; /* Total number of directories */ 172158782Sume uint8_t *e2fs_contigdirs; /* (u) # of contig. allocated dirs */ 173158787Sume char e2fs_wasvalid; /* valid at mount time */ 174158787Sume off_t e2fs_maxfilesize; 175158787Sume struct ext2_gd *e2fs_gd; /* Group Descriptors */ 176158787Sume int32_t e2fs_contigsumsize; /* size of cluster summary array */ 177158787Sume int32_t *e2fs_maxcluster; /* max cluster in each cyl group */ 178158787Sume struct csum *e2fs_clustersum; /* cluster summary in each cyl group */ 179158782Sume int32_t e2fs_uhash; /* 3 if hash should be signed, 0 if not */ 180158782Sume}; 181158787Sume 182158787Sume/* cluster summary information */ 183158782Sume 184158782Sumestruct csum { 185158782Sume int8_t cs_init; /* cluster summary has been initialized */ 186158782Sume int32_t *cs_sum; /* cluster summary array */ 187158782Sume}; 188158782Sume 189158782Sume/* 190158782Sume * The second extended file system magic number 191158782Sume */ 192158782Sume#define E2FS_MAGIC 0xEF53 193158782Sume 194158782Sume/* 195158782Sume * Revision levels 196158782Sume */ 197158782Sume#define E2FS_REV0 0 /* The good old (original) format */ 198158782Sume#define E2FS_REV1 1 /* V2 format w/ dynamic inode sizes */ 199158782Sume 200158782Sume#define E2FS_REV0_INODE_SIZE 128 201158782Sume 202158782Sume/* 203158782Sume * compatible/incompatible features 204158782Sume */ 205158782Sume#define EXT2F_COMPAT_PREALLOC 0x0001 206158782Sume#define EXT2F_COMPAT_HASJOURNAL 0x0004 207158782Sume#define EXT2F_COMPAT_RESIZE 0x0010 208158782Sume#define EXT2F_COMPAT_DIRHASHINDEX 0x0020 209158782Sume 210158782Sume#define EXT2F_ROCOMPAT_SPARSESUPER 0x0001 211158782Sume#define EXT2F_ROCOMPAT_LARGEFILE 0x0002 212158782Sume#define EXT2F_ROCOMPAT_BTREE_DIR 0x0004 213158782Sume#define EXT2F_ROCOMPAT_HUGE_FILE 0x0008 214158782Sume#define EXT2F_ROCOMPAT_GDT_CSUM 0x0010 215158782Sume#define EXT2F_ROCOMPAT_DIR_NLINK 0x0020 216158782Sume#define EXT2F_ROCOMPAT_EXTRA_ISIZE 0x0040 217158782Sume 218158782Sume#define EXT2F_INCOMPAT_COMP 0x0001 219158782Sume#define EXT2F_INCOMPAT_FTYPE 0x0002 220158782Sume#define EXT2F_INCOMPAT_RECOVER 0x0004 221158782Sume#define EXT2F_INCOMPAT_META_BG 0x0010 222158782Sume#define EXT2F_INCOMPAT_EXTENTS 0x0040 223#define EXT2F_INCOMPAT_64BIT 0x0080 224#define EXT2F_INCOMPAT_MMP 0x0100 225#define EXT2F_INCOMPAT_FLEX_BG 0x0200 226 227/* 228 * Features supported in this implementation 229 * 230 * We support the following REV1 features: 231 * - EXT2F_ROCOMPAT_SPARSESUPER 232 * - EXT2F_ROCOMPAT_LARGEFILE 233 * - EXT2F_ROCOMPAT_EXTRA_ISIZE 234 * - EXT2F_INCOMPAT_FTYPE 235 * 236 * We partially support (read-only) the following EXT4 features: 237 * - EXT2F_ROCOMPAT_HUGE_FILE 238 * - EXT2F_INCOMPAT_EXTENTS 239 * 240 * We do not support these EXT4 features but they are irrelevant 241 * for read-only support: 242 * - EXT2F_INCOMPAT_RECOVER 243 * - EXT2F_INCOMPAT_FLEX_BG 244 * - EXT2F_INCOMPAT_META_BG 245 */ 246#define EXT2F_COMPAT_SUPP EXT2F_COMPAT_DIRHASHINDEX 247#define EXT2F_ROCOMPAT_SUPP (EXT2F_ROCOMPAT_SPARSESUPER | \ 248 EXT2F_ROCOMPAT_LARGEFILE | \ 249 EXT2F_ROCOMPAT_EXTRA_ISIZE) 250#define EXT2F_INCOMPAT_SUPP EXT2F_INCOMPAT_FTYPE 251#define EXT4F_RO_INCOMPAT_SUPP (EXT2F_INCOMPAT_EXTENTS | \ 252 EXT2F_INCOMPAT_RECOVER | \ 253 EXT2F_INCOMPAT_FLEX_BG | \ 254 EXT2F_INCOMPAT_META_BG ) 255 256/* Assume that user mode programs are passing in an ext2fs superblock, not 257 * a kernel struct super_block. This will allow us to call the feature-test 258 * macros from user land. */ 259#define EXT2_SB(sb) (sb) 260 261/* 262 * Feature set definitions 263 */ 264#define EXT2_HAS_COMPAT_FEATURE(sb,mask) \ 265 ( EXT2_SB(sb)->e2fs->e2fs_features_compat & htole32(mask) ) 266#define EXT2_HAS_RO_COMPAT_FEATURE(sb,mask) \ 267 ( EXT2_SB(sb)->e2fs->e2fs_features_rocompat & htole32(mask) ) 268#define EXT2_HAS_INCOMPAT_FEATURE(sb,mask) \ 269 ( EXT2_SB(sb)->e2fs->e2fs_features_incompat & htole32(mask) ) 270 271/* 272 * File clean flags 273 */ 274#define E2FS_ISCLEAN 0x0001 /* Unmounted cleanly */ 275#define E2FS_ERRORS 0x0002 /* Errors detected */ 276 277/* 278 * Filesystem miscellaneous flags 279 */ 280#define E2FS_SIGNED_HASH 0x0001 281#define E2FS_UNSIGNED_HASH 0x0002 282 283/* ext2 file system block group descriptor */ 284 285struct ext2_gd { 286 uint32_t ext2bgd_b_bitmap; /* blocks bitmap block */ 287 uint32_t ext2bgd_i_bitmap; /* inodes bitmap block */ 288 uint32_t ext2bgd_i_tables; /* inodes table block */ 289 uint16_t ext2bgd_nbfree; /* number of free blocks */ 290 uint16_t ext2bgd_nifree; /* number of free inodes */ 291 uint16_t ext2bgd_ndirs; /* number of directories */ 292 uint16_t ext4bgd_flags; /* block group flags */ 293 uint32_t ext4bgd_x_bitmap; /* snapshot exclusion bitmap loc. */ 294 uint16_t ext4bgd_b_bmap_csum; /* block bitmap checksum */ 295 uint16_t ext4bgd_i_bmap_csum; /* inode bitmap checksum */ 296 uint16_t ext4bgd_i_unused; /* unused inode count */ 297 uint16_t ext4bgd_csum; /* group descriptor checksum */ 298}; 299 300 301/* EXT2FS metadatas are stored in little-endian byte order. These macros 302 * helps reading these metadatas 303 */ 304 305#define e2fs_cgload(old, new, size) memcpy((new), (old), (size)); 306#define e2fs_cgsave(old, new, size) memcpy((new), (old), (size)); 307 308/* 309 * Macro-instructions used to manage several block sizes 310 */ 311#define EXT2_MAX_BLOCK_SIZE 4096 312#define EXT2_MIN_BLOCK_LOG_SIZE 10 313#define EXT2_BLOCK_SIZE(s) ((s)->e2fs_bsize) 314#define EXT2_ADDR_PER_BLOCK(s) (EXT2_BLOCK_SIZE(s) / sizeof(uint32_t)) 315#define EXT2_INODE_SIZE(s) (EXT2_SB(s)->e2fs_isize) 316 317/* 318 * Macro-instructions used to manage fragments 319 */ 320#define EXT2_MIN_FRAG_SIZE 1024 321#define EXT2_MAX_FRAG_SIZE 4096 322#define EXT2_MIN_FRAG_LOG_SIZE 10 323#define EXT2_FRAG_SIZE(s) (EXT2_SB(s)->e2fs_fsize) 324#define EXT2_FRAGS_PER_BLOCK(s) (EXT2_SB(s)->e2fs_fpb) 325 326/* 327 * Macro-instructions used to manage group descriptors 328 */ 329#define EXT2_BLOCKS_PER_GROUP(s) (EXT2_SB(s)->e2fs_bpg) 330 331#endif /* !_FS_EXT2FS_EXT2FS_H_ */ 332