xref: /freebsd-src/sys/fs/ext2fs/ext2fs.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
1e09c00caSUlf Lilleengen /*-
2e09c00caSUlf Lilleengen  *  modified for EXT2FS support in Lites 1.1
3e09c00caSUlf Lilleengen  *
4e09c00caSUlf Lilleengen  *  Aug 1995, Godmar Back (gback@cs.utah.edu)
5e09c00caSUlf Lilleengen  *  University of Utah, Department of Computer Science
6e09c00caSUlf Lilleengen  */
7e09c00caSUlf Lilleengen /*-
8*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
9d63027b6SPedro F. Giffuni  *
10e09c00caSUlf Lilleengen  * Copyright (c) 2009 Aditya Sarawgi
11e09c00caSUlf Lilleengen  * All rights reserved.
12e09c00caSUlf Lilleengen  *
13e09c00caSUlf Lilleengen  * Redistribution and use in source and binary forms, with or without
14e09c00caSUlf Lilleengen  * modification, are permitted provided that the following conditions
15e09c00caSUlf Lilleengen  * are met:
16e09c00caSUlf Lilleengen  * 1. Redistributions of source code must retain the above copyright
17e09c00caSUlf Lilleengen  *    notice, this list of conditions and the following disclaimer.
18e09c00caSUlf Lilleengen  * 2. Redistributions in binary form must reproduce the above copyright
19e09c00caSUlf Lilleengen  *    notice, this list of conditions and the following disclaimer in the
20e09c00caSUlf Lilleengen  *    documentation and/or other materials provided with the distribution.
21e09c00caSUlf Lilleengen  *
22e09c00caSUlf Lilleengen  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23e09c00caSUlf Lilleengen  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24e09c00caSUlf Lilleengen  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25e09c00caSUlf Lilleengen  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26e09c00caSUlf Lilleengen  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27e09c00caSUlf Lilleengen  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28e09c00caSUlf Lilleengen  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29e09c00caSUlf Lilleengen  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30e09c00caSUlf Lilleengen  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31e09c00caSUlf Lilleengen  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32e09c00caSUlf Lilleengen  * SUCH DAMAGE.
33e09c00caSUlf Lilleengen  *
34e09c00caSUlf Lilleengen  *
35e09c00caSUlf Lilleengen  */
36e09c00caSUlf Lilleengen 
375ed5554fSPedro F. Giffuni #ifndef _FS_EXT2FS_EXT2FS_H_
385ed5554fSPedro F. Giffuni #define	_FS_EXT2FS_EXT2FS_H_
39e09c00caSUlf Lilleengen 
40e09c00caSUlf Lilleengen #include <sys/types.h>
41e09c00caSUlf Lilleengen 
42e09c00caSUlf Lilleengen /*
43e09c00caSUlf Lilleengen  * Super block for an ext2fs file system.
44e09c00caSUlf Lilleengen  */
45e09c00caSUlf Lilleengen struct ext2fs {
464d2ede67SJohn Baldwin 	uint32_t  e2fs_icount;		/* Inode count */
474d2ede67SJohn Baldwin 	uint32_t  e2fs_bcount;		/* blocks count */
484d2ede67SJohn Baldwin 	uint32_t  e2fs_rbcount;		/* reserved blocks count */
494d2ede67SJohn Baldwin 	uint32_t  e2fs_fbcount;		/* free blocks count */
504d2ede67SJohn Baldwin 	uint32_t  e2fs_ficount;		/* free inodes count */
514d2ede67SJohn Baldwin 	uint32_t  e2fs_first_dblock;	/* first data block */
524d2ede67SJohn Baldwin 	uint32_t  e2fs_log_bsize;	/* block size = 1024*(2^e2fs_log_bsize) */
534d2ede67SJohn Baldwin 	uint32_t  e2fs_log_fsize;	/* fragment size */
544d2ede67SJohn Baldwin 	uint32_t  e2fs_bpg;		/* blocks per group */
554d2ede67SJohn Baldwin 	uint32_t  e2fs_fpg;		/* frags per group */
564d2ede67SJohn Baldwin 	uint32_t  e2fs_ipg;		/* inodes per group */
574d2ede67SJohn Baldwin 	uint32_t  e2fs_mtime;		/* mount time */
584d2ede67SJohn Baldwin 	uint32_t  e2fs_wtime;		/* write time */
594d2ede67SJohn Baldwin 	uint16_t  e2fs_mnt_count;	/* mount count */
604d2ede67SJohn Baldwin 	uint16_t  e2fs_max_mnt_count;	/* max mount count */
614d2ede67SJohn Baldwin 	uint16_t  e2fs_magic;		/* magic number */
624d2ede67SJohn Baldwin 	uint16_t  e2fs_state;		/* file system state */
634d2ede67SJohn Baldwin 	uint16_t  e2fs_beh;		/* behavior on errors */
644d2ede67SJohn Baldwin 	uint16_t  e2fs_minrev;		/* minor revision level */
654d2ede67SJohn Baldwin 	uint32_t  e2fs_lastfsck;	/* time of last fsck */
664d2ede67SJohn Baldwin 	uint32_t  e2fs_fsckintv;	/* max time between fscks */
674d2ede67SJohn Baldwin 	uint32_t  e2fs_creator;		/* creator OS */
684d2ede67SJohn Baldwin 	uint32_t  e2fs_rev;		/* revision level */
694d2ede67SJohn Baldwin 	uint16_t  e2fs_ruid;		/* default uid for reserved blocks */
704d2ede67SJohn Baldwin 	uint16_t  e2fs_rgid;		/* default gid for reserved blocks */
71e09c00caSUlf Lilleengen 	/* EXT2_DYNAMIC_REV superblocks */
724d2ede67SJohn Baldwin 	uint32_t  e2fs_first_ino;	/* first non-reserved inode */
734d2ede67SJohn Baldwin 	uint16_t  e2fs_inode_size;	/* size of inode structure */
744d2ede67SJohn Baldwin 	uint16_t  e2fs_block_group_nr;	/* block grp number of this sblk*/
754d2ede67SJohn Baldwin 	uint32_t  e2fs_features_compat;	/* compatible feature set */
764d2ede67SJohn Baldwin 	uint32_t  e2fs_features_incompat; /* incompatible feature set */
774d2ede67SJohn Baldwin 	uint32_t  e2fs_features_rocompat; /* RO-compatible feature set */
784d2ede67SJohn Baldwin 	uint8_t	  e2fs_uuid[16];	/* 128-bit uuid for volume */
79e09c00caSUlf Lilleengen 	char      e2fs_vname[16];	/* volume name */
80e09c00caSUlf Lilleengen 	char      e2fs_fsmnt[64];	/* name mounted on */
814d2ede67SJohn Baldwin 	uint32_t  e2fs_algo;		/* For compression */
824d2ede67SJohn Baldwin 	uint8_t   e2fs_prealloc;	/* # of blocks for old prealloc */
834d2ede67SJohn Baldwin 	uint8_t   e2fs_dir_prealloc;	/* # of blocks for old prealloc dirs */
844d2ede67SJohn Baldwin 	uint16_t  e2fs_reserved_ngdb;	/* # of reserved gd blocks for resize */
854d2ede67SJohn Baldwin 	char      e3fs_journal_uuid[16]; /* uuid of journal superblock */
864d2ede67SJohn Baldwin 	uint32_t  e3fs_journal_inum;	/* inode number of journal file */
874d2ede67SJohn Baldwin 	uint32_t  e3fs_journal_dev;	/* device number of journal file */
884d2ede67SJohn Baldwin 	uint32_t  e3fs_last_orphan;	/* start of list of inodes to delete */
894d2ede67SJohn Baldwin 	uint32_t  e3fs_hash_seed[4];	/* HTREE hash seed */
904d2ede67SJohn Baldwin 	char      e3fs_def_hash_version;/* Default hash version to use */
9198a76859SKevin Lo 	char      e3fs_jnl_backup_type;
9298a76859SKevin Lo 	uint16_t  e3fs_desc_size;	/* size of group descriptor */
934d2ede67SJohn Baldwin 	uint32_t  e3fs_default_mount_opts;
944d2ede67SJohn Baldwin 	uint32_t  e3fs_first_meta_bg;	/* First metablock block group */
953cc6ae1fSPedro F. Giffuni 	uint32_t  e3fs_mkfs_time;	/* when the fs was created */
963cc6ae1fSPedro F. Giffuni 	uint32_t  e3fs_jnl_blks[17];	/* backup of the journal inode */
9798a76859SKevin Lo 	uint32_t  e4fs_bcount_hi;	/* high bits of blocks count */
9898a76859SKevin Lo 	uint32_t  e4fs_rbcount_hi;	/* high bits of reserved blocks count */
9998a76859SKevin Lo 	uint32_t  e4fs_fbcount_hi;	/* high bits of free blocks count */
10096e9f467SPedro F. Giffuni 	uint16_t  e4fs_min_extra_isize; /* all inodes have some bytes */
101035e4e04SPedro F. Giffuni 	uint16_t  e4fs_want_extra_isize;/* inodes must reserve some bytes */
1023cc6ae1fSPedro F. Giffuni 	uint32_t  e4fs_flags;		/* miscellaneous flags */
1033cc6ae1fSPedro F. Giffuni 	uint16_t  e4fs_raid_stride;	/* RAID stride */
10496e9f467SPedro F. Giffuni 	uint16_t  e4fs_mmpintv;		/* seconds to wait in MMP checking */
1053cc6ae1fSPedro F. Giffuni 	uint64_t  e4fs_mmpblk;		/* block for multi-mount protection */
10696e9f467SPedro F. Giffuni 	uint32_t  e4fs_raid_stripe_wid; /* blocks on data disks (N * stride) */
1073cc6ae1fSPedro F. Giffuni 	uint8_t   e4fs_log_gpf;		/* FLEX_BG group size */
10898a76859SKevin Lo 	uint8_t   e4fs_chksum_type;	/* metadata checksum algorithm used */
10998a76859SKevin Lo 	uint8_t   e4fs_encrypt;		/* versioning level for encryption */
11098a76859SKevin Lo 	uint8_t   e4fs_reserved_pad;
11198a76859SKevin Lo 	uint64_t  e4fs_kbytes_written;	/* number of lifetime kilobytes */
11298a76859SKevin Lo 	uint32_t  e4fs_snapinum;	/* inode number of active snapshot */
11398a76859SKevin Lo 	uint32_t  e4fs_snapid;		/* sequential ID of active snapshot */
11498a76859SKevin Lo 	uint64_t  e4fs_snaprbcount;	/* reserved blocks for active snapshot */
11598a76859SKevin Lo 	uint32_t  e4fs_snaplist;	/* inode number for on-disk snapshot */
11698a76859SKevin Lo 	uint32_t  e4fs_errcount;	/* number of file system errors */
11798a76859SKevin Lo 	uint32_t  e4fs_first_errtime;	/* first time an error happened */
11898a76859SKevin Lo 	uint32_t  e4fs_first_errino;	/* inode involved in first error */
11998a76859SKevin Lo 	uint64_t  e4fs_first_errblk;	/* block involved of first error */
12098a76859SKevin Lo 	uint8_t   e4fs_first_errfunc[32];/* function where error happened */
12198a76859SKevin Lo 	uint32_t  e4fs_first_errline;	/* line number where error happened */
12298a76859SKevin Lo 	uint32_t  e4fs_last_errtime;	/* most recent time of an error */
12398a76859SKevin Lo 	uint32_t  e4fs_last_errino;	/* inode involved in last error */
12498a76859SKevin Lo 	uint32_t  e4fs_last_errline;	/* line number where error happened */
12598a76859SKevin Lo 	uint64_t  e4fs_last_errblk;	/* block involved of last error */
12698a76859SKevin Lo 	uint8_t   e4fs_last_errfunc[32]; /* function where error happened */
12798a76859SKevin Lo 	uint8_t   e4fs_mount_opts[64];
12898a76859SKevin Lo 	uint32_t  e4fs_usrquota_inum;	/* inode for tracking user quota */
12998a76859SKevin Lo 	uint32_t  e4fs_grpquota_inum;	/* inode for tracking group quota */
13098a76859SKevin Lo 	uint32_t  e4fs_overhead_clusters;/* overhead blocks/clusters */
13198a76859SKevin Lo 	uint32_t  e4fs_backup_bgs[2];	/* groups with sparse_super2 SBs */
13298a76859SKevin Lo 	uint8_t   e4fs_encrypt_algos[4];/* encryption algorithms in use */
13398a76859SKevin Lo 	uint8_t   e4fs_encrypt_pw_salt[16];/* salt used for string2key */
13498a76859SKevin Lo 	uint32_t  e4fs_lpf_ino;		/* location of the lost+found inode */
13598a76859SKevin Lo 	uint32_t  e4fs_proj_quota_inum;	/* inode for tracking project quota */
13698a76859SKevin Lo 	uint32_t  e4fs_chksum_seed;	/* checksum seed */
13798a76859SKevin Lo 	uint32_t  e4fs_reserved[98];	/* padding to the end of the block */
13898a76859SKevin Lo 	uint32_t  e4fs_sbchksum;	/* superblock checksum */
139e09c00caSUlf Lilleengen };
140e09c00caSUlf Lilleengen 
1419e43acf6SPedro F. Giffuni /*
1429e43acf6SPedro F. Giffuni  * The path name on which the file system is mounted is maintained
1439e43acf6SPedro F. Giffuni  * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in
1449e43acf6SPedro F. Giffuni  * the super block for this name.
1459e43acf6SPedro F. Giffuni  */
1469e43acf6SPedro F. Giffuni #define	MAXMNTLEN 512
147e09c00caSUlf Lilleengen 
148e09c00caSUlf Lilleengen /*
149e09c00caSUlf Lilleengen  * In-Memory Superblock
150e09c00caSUlf Lilleengen  */
151e09c00caSUlf Lilleengen 
152e09c00caSUlf Lilleengen struct m_ext2fs {
153e09c00caSUlf Lilleengen 	struct ext2fs * e2fs;
154e09c00caSUlf Lilleengen 	char     e2fs_fsmnt[MAXMNTLEN];/* name mounted on */
155e09c00caSUlf Lilleengen 	char     e2fs_ronly;	  /* mounted read-only flag */
156e09c00caSUlf Lilleengen 	char     e2fs_fmod;	  /* super block modified flag */
1573acd9182SFedor Uporov 	uint64_t e2fs_bcount;	  /* blocks count */
1583acd9182SFedor Uporov 	uint64_t e2fs_rbcount;	  /* reserved blocks count */
1593acd9182SFedor Uporov 	uint64_t e2fs_fbcount;	  /* free blocks count */
160cd3acfe7SFedor Uporov 	uint32_t e2fs_ficount;	  /* free inodes count */
161e09c00caSUlf Lilleengen 	uint32_t e2fs_bsize;	  /* Block size */
162e09c00caSUlf Lilleengen 	uint32_t e2fs_bshift;	  /* calc of logical block no */
163f7d4b4d3SPedro F. Giffuni 	uint32_t e2fs_bpg;	  /* Number of blocks per group */
164e09c00caSUlf Lilleengen 	int64_t  e2fs_qbmask;	  /* = s_blocksize -1 */
165e09c00caSUlf Lilleengen 	uint32_t e2fs_fsbtodb;	  /* Shift to get disk block */
166e09c00caSUlf Lilleengen 	uint32_t e2fs_ipg;	  /* Number of inodes per group */
167e09c00caSUlf Lilleengen 	uint32_t e2fs_ipb;	  /* Number of inodes per block */
168e09c00caSUlf Lilleengen 	uint32_t e2fs_itpg;	  /* Number of inode table per group */
169e09c00caSUlf Lilleengen 	uint32_t e2fs_fsize;	  /* Size of fragments per block */
170e09c00caSUlf Lilleengen 	uint32_t e2fs_fpb;	  /* Number of fragments per block */
171e09c00caSUlf Lilleengen 	uint32_t e2fs_fpg;	  /* Number of fragments per group */
172e09c00caSUlf Lilleengen 	uint32_t e2fs_gdbcount;	  /* Number of group descriptors */
173e09c00caSUlf Lilleengen 	uint32_t e2fs_gcount;	  /* Number of groups */
174f7d4b4d3SPedro F. Giffuni 	uint32_t e2fs_isize;	  /* Size of inode */
175e09c00caSUlf Lilleengen 	uint32_t e2fs_total_dir;  /* Total number of directories */
176cd2895aaSJohn Baldwin 	uint8_t	*e2fs_contigdirs; /* (u) # of contig. allocated dirs */
177e09c00caSUlf Lilleengen 	char     e2fs_wasvalid;	  /* valid at mount time */
178e09c00caSUlf Lilleengen 	off_t    e2fs_maxfilesize;
179e09c00caSUlf Lilleengen 	struct   ext2_gd *e2fs_gd; /* Group Descriptors */
1805b63c125SPedro F. Giffuni 	int32_t  e2fs_contigsumsize;    /* size of cluster summary array */
1815b63c125SPedro F. Giffuni 	int32_t *e2fs_maxcluster;       /* max cluster in each cyl group */
1825b63c125SPedro F. Giffuni 	struct   csum *e2fs_clustersum; /* cluster summary in each cyl group */
1839824e4adSPedro F. Giffuni 	int32_t  e2fs_uhash;	  /* 3 if hash should be signed, 0 if not */
184512f29d1SFedor Uporov 	uint32_t e2fs_csum_seed;  /* sb checksum seed */
185f784da88SKonstantin Belousov 	uint64_t e2fs_maxsymlinklen; /* max size of short symlink */
186e09c00caSUlf Lilleengen };
187e09c00caSUlf Lilleengen 
188b6113fb3SPedro F. Giffuni /* cluster summary information */
189b6113fb3SPedro F. Giffuni 
190b6113fb3SPedro F. Giffuni struct csum {
191b6113fb3SPedro F. Giffuni 	int8_t   cs_init; /* cluster summary has been initialized */
192b6113fb3SPedro F. Giffuni 	int32_t *cs_sum;  /* cluster summary array */
193b6113fb3SPedro F. Giffuni };
194e09c00caSUlf Lilleengen 
195cd2895aaSJohn Baldwin /*
196cd2895aaSJohn Baldwin  * The second extended file system magic number
197cd2895aaSJohn Baldwin  */
198cd2895aaSJohn Baldwin #define	E2FS_MAGIC		0xEF53
199cd2895aaSJohn Baldwin 
200e09c00caSUlf Lilleengen /*
201e09c00caSUlf Lilleengen  * Revision levels
202e09c00caSUlf Lilleengen  */
203e09c00caSUlf Lilleengen #define	E2FS_REV0		0	/* The good old (original) format */
204e09c00caSUlf Lilleengen #define	E2FS_REV1		1	/* V2 format w/ dynamic inode sizes */
205e09c00caSUlf Lilleengen 
206e09c00caSUlf Lilleengen #define	E2FS_REV0_INODE_SIZE 128
207e09c00caSUlf Lilleengen 
208e09c00caSUlf Lilleengen /*
209512f29d1SFedor Uporov  * Metadata checksum algorithm codes
210512f29d1SFedor Uporov  */
211512f29d1SFedor Uporov #define EXT4_CRC32C_CHKSUM		1
212512f29d1SFedor Uporov 
213512f29d1SFedor Uporov /*
214e09c00caSUlf Lilleengen  * compatible/incompatible features
215e09c00caSUlf Lilleengen  */
216e09c00caSUlf Lilleengen #define	EXT2F_COMPAT_PREALLOC		0x0001
2173d851dbeSPedro F. Giffuni #define	EXT2F_COMPAT_IMAGIC_INODES	0x0002
218cd2895aaSJohn Baldwin #define	EXT2F_COMPAT_HASJOURNAL		0x0004
219ac506a8fSPedro F. Giffuni #define	EXT2F_COMPAT_EXT_ATTR		0x0008
220e09c00caSUlf Lilleengen #define	EXT2F_COMPAT_RESIZE		0x0010
221371f338bSPedro F. Giffuni #define	EXT2F_COMPAT_DIRHASHINDEX	0x0020
2223d851dbeSPedro F. Giffuni #define	EXT2F_COMPAT_LAZY_BG		0x0040
2233d851dbeSPedro F. Giffuni #define	EXT2F_COMPAT_EXCLUDE_BITMAP	0x0100
22425efc7c8SPedro F. Giffuni #define	EXT2F_COMPAT_SPARSESUPER2	0x0200
225e09c00caSUlf Lilleengen 
226e09c00caSUlf Lilleengen #define	EXT2F_ROCOMPAT_SPARSESUPER	0x0001
227e09c00caSUlf Lilleengen #define	EXT2F_ROCOMPAT_LARGEFILE	0x0002
228e09c00caSUlf Lilleengen #define	EXT2F_ROCOMPAT_BTREE_DIR	0x0004
229e28f5d52SPedro F. Giffuni #define	EXT2F_ROCOMPAT_HUGE_FILE	0x0008
230e28f5d52SPedro F. Giffuni #define	EXT2F_ROCOMPAT_GDT_CSUM		0x0010
231e28f5d52SPedro F. Giffuni #define	EXT2F_ROCOMPAT_DIR_NLINK	0x0020
232e28f5d52SPedro F. Giffuni #define	EXT2F_ROCOMPAT_EXTRA_ISIZE	0x0040
2333d851dbeSPedro F. Giffuni #define	EXT2F_ROCOMPAT_HAS_SNAPSHOT	0x0080
23425efc7c8SPedro F. Giffuni #define	EXT2F_ROCOMPAT_QUOTA		0x0100
23525efc7c8SPedro F. Giffuni #define	EXT2F_ROCOMPAT_BIGALLOC		0x0200
23625efc7c8SPedro F. Giffuni #define	EXT2F_ROCOMPAT_METADATA_CKSUM	0x0400
2373d851dbeSPedro F. Giffuni #define	EXT2F_ROCOMPAT_REPLICA		0x0800
23825efc7c8SPedro F. Giffuni #define	EXT2F_ROCOMPAT_READONLY		0x1000
23925efc7c8SPedro F. Giffuni #define	EXT2F_ROCOMPAT_PROJECT		0x2000
240e09c00caSUlf Lilleengen 
241e09c00caSUlf Lilleengen #define	EXT2F_INCOMPAT_COMP		0x0001
242e09c00caSUlf Lilleengen #define	EXT2F_INCOMPAT_FTYPE		0x0002
24326069aecSPedro F. Giffuni #define	EXT2F_INCOMPAT_RECOVER		0x0004
2443d851dbeSPedro F. Giffuni #define	EXT2F_INCOMPAT_JOURNAL_DEV	0x0008
245e28f5d52SPedro F. Giffuni #define	EXT2F_INCOMPAT_META_BG		0x0010
246e28f5d52SPedro F. Giffuni #define	EXT2F_INCOMPAT_EXTENTS		0x0040
247e28f5d52SPedro F. Giffuni #define	EXT2F_INCOMPAT_64BIT		0x0080
248e28f5d52SPedro F. Giffuni #define	EXT2F_INCOMPAT_MMP		0x0100
249e28f5d52SPedro F. Giffuni #define	EXT2F_INCOMPAT_FLEX_BG		0x0200
25025efc7c8SPedro F. Giffuni #define	EXT2F_INCOMPAT_EA_INODE		0x0400
25125efc7c8SPedro F. Giffuni #define	EXT2F_INCOMPAT_DIRDATA		0x1000
25225efc7c8SPedro F. Giffuni #define	EXT2F_INCOMPAT_CSUM_SEED	0x2000
25325efc7c8SPedro F. Giffuni #define	EXT2F_INCOMPAT_LARGEDIR		0x4000
25425efc7c8SPedro F. Giffuni #define	EXT2F_INCOMPAT_INLINE_DATA	0x8000
25525efc7c8SPedro F. Giffuni #define	EXT2F_INCOMPAT_ENCRYPT		0x10000
256e09c00caSUlf Lilleengen 
2573d851dbeSPedro F. Giffuni struct ext2_feature
2583d851dbeSPedro F. Giffuni {
2593d851dbeSPedro F. Giffuni 	int mask;
2603d851dbeSPedro F. Giffuni 	const char *name;
2613d851dbeSPedro F. Giffuni };
2623d851dbeSPedro F. Giffuni 
2633d851dbeSPedro F. Giffuni static const struct ext2_feature compat[] = {
2643d851dbeSPedro F. Giffuni 	{ EXT2F_COMPAT_PREALLOC,       "dir_prealloc"    },
2653d851dbeSPedro F. Giffuni 	{ EXT2F_COMPAT_IMAGIC_INODES,  "imagic_inodes"   },
2663d851dbeSPedro F. Giffuni 	{ EXT2F_COMPAT_HASJOURNAL,     "has_journal"     },
2673d851dbeSPedro F. Giffuni 	{ EXT2F_COMPAT_EXT_ATTR,       "ext_attr"        },
2683d851dbeSPedro F. Giffuni 	{ EXT2F_COMPAT_RESIZE,         "resize_inode"    },
2693d851dbeSPedro F. Giffuni 	{ EXT2F_COMPAT_DIRHASHINDEX,   "dir_index"       },
2703d851dbeSPedro F. Giffuni 	{ EXT2F_COMPAT_EXCLUDE_BITMAP, "snapshot_bitmap" },
2713d851dbeSPedro F. Giffuni 	{ EXT2F_COMPAT_SPARSESUPER2,   "sparse_super2"   }
2723d851dbeSPedro F. Giffuni };
2733d851dbeSPedro F. Giffuni 
2743d851dbeSPedro F. Giffuni static const struct ext2_feature ro_compat[] = {
2753d851dbeSPedro F. Giffuni 	{ EXT2F_ROCOMPAT_SPARSESUPER,    "sparse_super"  },
2763d851dbeSPedro F. Giffuni 	{ EXT2F_ROCOMPAT_LARGEFILE,      "large_file"    },
2773d851dbeSPedro F. Giffuni 	{ EXT2F_ROCOMPAT_BTREE_DIR,      "btree_dir"     },
2783d851dbeSPedro F. Giffuni 	{ EXT2F_ROCOMPAT_HUGE_FILE,      "huge_file"     },
2793d851dbeSPedro F. Giffuni 	{ EXT2F_ROCOMPAT_GDT_CSUM,       "uninit_groups" },
2803d851dbeSPedro F. Giffuni 	{ EXT2F_ROCOMPAT_DIR_NLINK,      "dir_nlink"     },
2813d851dbeSPedro F. Giffuni 	{ EXT2F_ROCOMPAT_EXTRA_ISIZE,    "extra_isize"   },
2823d851dbeSPedro F. Giffuni 	{ EXT2F_ROCOMPAT_HAS_SNAPSHOT,   "snapshot"      },
2833d851dbeSPedro F. Giffuni 	{ EXT2F_ROCOMPAT_QUOTA,          "quota"         },
2843d851dbeSPedro F. Giffuni 	{ EXT2F_ROCOMPAT_BIGALLOC,       "bigalloc"      },
2853d851dbeSPedro F. Giffuni 	{ EXT2F_ROCOMPAT_METADATA_CKSUM, "metadata_csum" },
2863d851dbeSPedro F. Giffuni 	{ EXT2F_ROCOMPAT_REPLICA,        "replica"       },
2873d851dbeSPedro F. Giffuni 	{ EXT2F_ROCOMPAT_READONLY,       "ro"            },
2883d851dbeSPedro F. Giffuni 	{ EXT2F_ROCOMPAT_PROJECT,        "project"       }
2893d851dbeSPedro F. Giffuni };
2903d851dbeSPedro F. Giffuni 
2913d851dbeSPedro F. Giffuni static const struct ext2_feature incompat[] = {
2923d851dbeSPedro F. Giffuni 	{ EXT2F_INCOMPAT_COMP,        "compression"        },
2933d851dbeSPedro F. Giffuni 	{ EXT2F_INCOMPAT_FTYPE,       "filetype"           },
2943d851dbeSPedro F. Giffuni 	{ EXT2F_INCOMPAT_RECOVER,     "needs_recovery"     },
2953d851dbeSPedro F. Giffuni 	{ EXT2F_INCOMPAT_JOURNAL_DEV, "journal_dev"        },
2963d851dbeSPedro F. Giffuni 	{ EXT2F_INCOMPAT_META_BG,     "meta_bg"            },
2973d851dbeSPedro F. Giffuni 	{ EXT2F_INCOMPAT_EXTENTS,     "extents"            },
2983d851dbeSPedro F. Giffuni 	{ EXT2F_INCOMPAT_64BIT,       "64bit"              },
2993d851dbeSPedro F. Giffuni 	{ EXT2F_INCOMPAT_MMP,         "mmp"                },
3003d851dbeSPedro F. Giffuni 	{ EXT2F_INCOMPAT_FLEX_BG,     "flex_bg"            },
3013d851dbeSPedro F. Giffuni 	{ EXT2F_INCOMPAT_EA_INODE,    "ea_inode"           },
3023d851dbeSPedro F. Giffuni 	{ EXT2F_INCOMPAT_DIRDATA,     "dirdata"            },
3033d851dbeSPedro F. Giffuni 	{ EXT2F_INCOMPAT_CSUM_SEED,   "metadata_csum_seed" },
3043d851dbeSPedro F. Giffuni 	{ EXT2F_INCOMPAT_LARGEDIR,    "large_dir"          },
3053d851dbeSPedro F. Giffuni 	{ EXT2F_INCOMPAT_INLINE_DATA, "inline_data"        },
3063d851dbeSPedro F. Giffuni 	{ EXT2F_INCOMPAT_ENCRYPT,     "encrypt"            }
3073d851dbeSPedro F. Giffuni };
3083d851dbeSPedro F. Giffuni 
309e09c00caSUlf Lilleengen /*
310e09c00caSUlf Lilleengen  * Features supported in this implementation
311e09c00caSUlf Lilleengen  *
312e09c00caSUlf Lilleengen  * We support the following REV1 features:
313e09c00caSUlf Lilleengen  * - EXT2F_ROCOMPAT_SPARSESUPER
314e09c00caSUlf Lilleengen  * - EXT2F_ROCOMPAT_LARGEFILE
31567da48a1SPedro F. Giffuni  * - EXT2F_ROCOMPAT_EXTRA_ISIZE
316e09c00caSUlf Lilleengen  * - EXT2F_INCOMPAT_FTYPE
317d7511a40SPedro F. Giffuni  *
318d7511a40SPedro F. Giffuni  * We partially (read-only) support the following EXT4 features:
319d7511a40SPedro F. Giffuni  * - EXT2F_ROCOMPAT_HUGE_FILE
320d7511a40SPedro F. Giffuni  * - EXT2F_INCOMPAT_EXTENTS
32167da48a1SPedro F. Giffuni  *
322e09c00caSUlf Lilleengen  */
3239824e4adSPedro F. Giffuni #define	EXT2F_COMPAT_SUPP		EXT2F_COMPAT_DIRHASHINDEX
324035e4e04SPedro F. Giffuni #define	EXT2F_ROCOMPAT_SUPP		(EXT2F_ROCOMPAT_SPARSESUPER | \
325035e4e04SPedro F. Giffuni 					 EXT2F_ROCOMPAT_LARGEFILE | \
326d23db91eSPedro F. Giffuni 					 EXT2F_ROCOMPAT_GDT_CSUM | \
327512f29d1SFedor Uporov 					 EXT2F_ROCOMPAT_METADATA_CKSUM | \
328a821bdcfSPedro F. Giffuni 					 EXT2F_ROCOMPAT_DIR_NLINK | \
3295342d6e0SPedro F. Giffuni 					 EXT2F_ROCOMPAT_HUGE_FILE | \
330e28f5d52SPedro F. Giffuni 					 EXT2F_ROCOMPAT_EXTRA_ISIZE)
3313acd9182SFedor Uporov #define	EXT2F_INCOMPAT_SUPP		(EXT2F_INCOMPAT_FTYPE | \
332c0f16c65SFedor Uporov 					 EXT2F_INCOMPAT_META_BG | \
333512f29d1SFedor Uporov 					 EXT2F_INCOMPAT_EXTENTS | \
334512f29d1SFedor Uporov 					 EXT2F_INCOMPAT_64BIT | \
33567da48a1SPedro F. Giffuni 					 EXT2F_INCOMPAT_FLEX_BG | \
336c0f16c65SFedor Uporov 					 EXT2F_INCOMPAT_CSUM_SEED)
337e09c00caSUlf Lilleengen 
338a3ebd026SJohn Baldwin /* Assume that user mode programs are passing in an ext2fs superblock, not
339a3ebd026SJohn Baldwin  * a kernel struct super_block.  This will allow us to call the feature-test
340a3ebd026SJohn Baldwin  * macros from user land. */
341a3ebd026SJohn Baldwin #define	EXT2_SB(sb)	(sb)
342a3ebd026SJohn Baldwin 
343e09c00caSUlf Lilleengen /*
344e09c00caSUlf Lilleengen  * Feature set definitions
345e09c00caSUlf Lilleengen  */
346e09c00caSUlf Lilleengen #define	EXT2_HAS_COMPAT_FEATURE(sb,mask)			\
347cd3acfe7SFedor Uporov 	( le32toh(EXT2_SB(sb)->e2fs->e2fs_features_compat) & mask)
348e09c00caSUlf Lilleengen #define	EXT2_HAS_RO_COMPAT_FEATURE(sb,mask)			\
349cd3acfe7SFedor Uporov 	( le32toh(EXT2_SB(sb)->e2fs->e2fs_features_rocompat) & mask)
350e09c00caSUlf Lilleengen #define	EXT2_HAS_INCOMPAT_FEATURE(sb,mask)			\
351cd3acfe7SFedor Uporov 	( le32toh(EXT2_SB(sb)->e2fs->e2fs_features_incompat) & mask)
352e09c00caSUlf Lilleengen 
353e09c00caSUlf Lilleengen /*
354e09c00caSUlf Lilleengen  * File clean flags
355e09c00caSUlf Lilleengen  */
356e09c00caSUlf Lilleengen #define	E2FS_ISCLEAN			0x0001	/* Unmounted cleanly */
357e09c00caSUlf Lilleengen #define	E2FS_ERRORS			0x0002	/* Errors detected */
358e09c00caSUlf Lilleengen 
3599824e4adSPedro F. Giffuni /*
3609824e4adSPedro F. Giffuni  * Filesystem miscellaneous flags
3619824e4adSPedro F. Giffuni  */
3629824e4adSPedro F. Giffuni #define	E2FS_SIGNED_HASH	0x0001
3639824e4adSPedro F. Giffuni #define	E2FS_UNSIGNED_HASH	0x0002
3649824e4adSPedro F. Giffuni 
365d23db91eSPedro F. Giffuni #define	EXT2_BG_INODE_UNINIT	0x0001	/* Inode table/bitmap not in use */
366d23db91eSPedro F. Giffuni #define	EXT2_BG_BLOCK_UNINIT	0x0002	/* Block bitmap not in use */
367d23db91eSPedro F. Giffuni #define	EXT2_BG_INODE_ZEROED	0x0004	/* On-disk itable initialized to zero */
368d23db91eSPedro F. Giffuni 
369e09c00caSUlf Lilleengen /* ext2 file system block group descriptor */
370e09c00caSUlf Lilleengen 
371e09c00caSUlf Lilleengen struct ext2_gd {
3724d2ede67SJohn Baldwin 	uint32_t ext2bgd_b_bitmap;	/* blocks bitmap block */
3734d2ede67SJohn Baldwin 	uint32_t ext2bgd_i_bitmap;	/* inodes bitmap block */
3744d2ede67SJohn Baldwin 	uint32_t ext2bgd_i_tables;	/* inodes table block  */
3754d2ede67SJohn Baldwin 	uint16_t ext2bgd_nbfree;	/* number of free blocks */
3764d2ede67SJohn Baldwin 	uint16_t ext2bgd_nifree;	/* number of free inodes */
3774d2ede67SJohn Baldwin 	uint16_t ext2bgd_ndirs;		/* number of directories */
3783f5747b6SPedro F. Giffuni 	uint16_t ext4bgd_flags;		/* block group flags */
3793f5747b6SPedro F. Giffuni 	uint32_t ext4bgd_x_bitmap;	/* snapshot exclusion bitmap loc. */
3803f5747b6SPedro F. Giffuni 	uint16_t ext4bgd_b_bmap_csum;	/* block bitmap checksum */
3813f5747b6SPedro F. Giffuni 	uint16_t ext4bgd_i_bmap_csum;	/* inode bitmap checksum */
3823f5747b6SPedro F. Giffuni 	uint16_t ext4bgd_i_unused;	/* unused inode count */
3833f5747b6SPedro F. Giffuni 	uint16_t ext4bgd_csum;		/* group descriptor checksum */
3843acd9182SFedor Uporov 	uint32_t ext4bgd_b_bitmap_hi;	/* high bits of blocks bitmap block */
3853acd9182SFedor Uporov 	uint32_t ext4bgd_i_bitmap_hi;	/* high bits of inodes bitmap block */
3863acd9182SFedor Uporov 	uint32_t ext4bgd_i_tables_hi;	/* high bits of inodes table block */
3873acd9182SFedor Uporov 	uint16_t ext4bgd_nbfree_hi;	/* high bits of number of free blocks */
3883acd9182SFedor Uporov 	uint16_t ext4bgd_nifree_hi;	/* high bits of number of free inodes */
3893acd9182SFedor Uporov 	uint16_t ext4bgd_ndirs_hi;	/* high bits of number of directories */
3903acd9182SFedor Uporov 	uint16_t ext4bgd_i_unused_hi;	/* high bits of unused inode count */
3913acd9182SFedor Uporov 	uint32_t ext4bgd_x_bitmap_hi;   /* high bits of snapshot exclusion */
3923acd9182SFedor Uporov 	uint16_t ext4bgd_b_bmap_csum_hi;/* high bits of block bitmap checksum */
3933acd9182SFedor Uporov 	uint16_t ext4bgd_i_bmap_csum_hi;/* high bits of inode bitmap checksum */
3943acd9182SFedor Uporov 	uint32_t ext4bgd_reserved;
395e09c00caSUlf Lilleengen };
396e09c00caSUlf Lilleengen 
3973acd9182SFedor Uporov #define	E2FS_REV0_GD_SIZE (sizeof(struct ext2_gd) / 2)
3986e38bf94SFedor Uporov #define	E2FS_64BIT_GD_SIZE (sizeof(struct ext2_gd))
399a3ebd026SJohn Baldwin 
400e09c00caSUlf Lilleengen /*
401e09c00caSUlf Lilleengen  * Macro-instructions used to manage several block sizes
402e09c00caSUlf Lilleengen  */
403e09c00caSUlf Lilleengen #define	EXT2_MIN_BLOCK_LOG_SIZE		  10
404e09c00caSUlf Lilleengen #define	EXT2_BLOCK_SIZE(s)		((s)->e2fs_bsize)
405e09c00caSUlf Lilleengen #define	EXT2_ADDR_PER_BLOCK(s)		(EXT2_BLOCK_SIZE(s) / sizeof(uint32_t))
406e09c00caSUlf Lilleengen #define	EXT2_INODE_SIZE(s)		(EXT2_SB(s)->e2fs_isize)
407e09c00caSUlf Lilleengen 
408e09c00caSUlf Lilleengen /*
409e09c00caSUlf Lilleengen  * Macro-instructions used to manage fragments
410e09c00caSUlf Lilleengen  */
411e09c00caSUlf Lilleengen #define	EXT2_MIN_FRAG_SIZE		1024
412e09c00caSUlf Lilleengen #define	EXT2_MIN_FRAG_LOG_SIZE		10
4136e38bf94SFedor Uporov #define	EXT2_MAX_FRAG_LOG_SIZE		30
414e09c00caSUlf Lilleengen #define	EXT2_FRAG_SIZE(s)		(EXT2_SB(s)->e2fs_fsize)
415e09c00caSUlf Lilleengen #define	EXT2_FRAGS_PER_BLOCK(s)		(EXT2_SB(s)->e2fs_fpb)
416e09c00caSUlf Lilleengen 
417e09c00caSUlf Lilleengen /*
418e09c00caSUlf Lilleengen  * Macro-instructions used to manage group descriptors
419e09c00caSUlf Lilleengen  */
420e09c00caSUlf Lilleengen #define	EXT2_BLOCKS_PER_GROUP(s)	(EXT2_SB(s)->e2fs_bpg)
421c0f16c65SFedor Uporov #define	EXT2_DESCS_PER_BLOCK(s)		(EXT2_HAS_INCOMPAT_FEATURE((s),  \
422c0f16c65SFedor Uporov     EXT2F_INCOMPAT_64BIT) ? ((s)->e2fs_bsize / sizeof(struct ext2_gd)) : \
423c0f16c65SFedor Uporov     ((s)->e2fs_bsize / E2FS_REV0_GD_SIZE))
424e09c00caSUlf Lilleengen 
425daa2d62dSFedor Uporov /*
426daa2d62dSFedor Uporov  * Macro-instructions used to manage inodes
427daa2d62dSFedor Uporov  */
428cd3acfe7SFedor Uporov #define	EXT2_FIRST_INO(s)	(le32toh((EXT2_SB(s)->e2fs->e2fs_rev) == \
429cd3acfe7SFedor Uporov     E2FS_REV0) ? EXT2_FIRSTINO : le32toh(EXT2_SB(s)->e2fs->e2fs_first_ino))
430daa2d62dSFedor Uporov 
4311ed5f62dSFedor Uporov /*
4321ed5f62dSFedor Uporov  * Linux major/minor values limits
4331ed5f62dSFedor Uporov  */
4341ed5f62dSFedor Uporov #define	EXT2_MINORBITS	(20)
4351ed5f62dSFedor Uporov #define	EXT2_MAJOR_MAX	(0xffffffff >> EXT2_MINORBITS)
4361ed5f62dSFedor Uporov #define	EXT2_MINOR_MAX	((1 << EXT2_MINORBITS) - 1)
4371ed5f62dSFedor Uporov 
43845641afbSJohn Baldwin #endif	/* !_FS_EXT2FS_EXT2FS_H_ */
439