xref: /openbsd-src/sys/ufs/ext2fs/ext2fs.h (revision d570f06c3878bd770eaa704cc6a7912b04dd6da6)
1*d570f06cSmartijn /*	$OpenBSD: ext2fs.h,v 1.27 2024/07/15 13:27:36 martijn Exp $	*/
26b38e8dfSart /*	$NetBSD: ext2fs.h,v 1.10 2000/01/28 16:00:23 bouyer Exp $	*/
35ac2d602Sdownsj 
45ac2d602Sdownsj /*
51f3ff51cSdownsj  * Copyright (c) 1997 Manuel Bouyer.
65ac2d602Sdownsj  * Copyright (c) 1982, 1986, 1993
75ac2d602Sdownsj  *	The Regents of the University of California.  All rights reserved.
85ac2d602Sdownsj  *
95ac2d602Sdownsj  * Redistribution and use in source and binary forms, with or without
105ac2d602Sdownsj  * modification, are permitted provided that the following conditions
115ac2d602Sdownsj  * are met:
125ac2d602Sdownsj  * 1. Redistributions of source code must retain the above copyright
135ac2d602Sdownsj  *    notice, this list of conditions and the following disclaimer.
145ac2d602Sdownsj  * 2. Redistributions in binary form must reproduce the above copyright
155ac2d602Sdownsj  *    notice, this list of conditions and the following disclaimer in the
165ac2d602Sdownsj  *    documentation and/or other materials provided with the distribution.
1729295d1cSmillert  * 3. Neither the name of the University nor the names of its contributors
185ac2d602Sdownsj  *    may be used to endorse or promote products derived from this software
195ac2d602Sdownsj  *    without specific prior written permission.
205ac2d602Sdownsj  *
215ac2d602Sdownsj  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
225ac2d602Sdownsj  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
235ac2d602Sdownsj  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
245ac2d602Sdownsj  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
255ac2d602Sdownsj  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
265ac2d602Sdownsj  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
275ac2d602Sdownsj  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
285ac2d602Sdownsj  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
295ac2d602Sdownsj  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
305ac2d602Sdownsj  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
315ac2d602Sdownsj  * SUCH DAMAGE.
325ac2d602Sdownsj  *
335ac2d602Sdownsj  *	@(#)fs.h	8.10 (Berkeley) 10/27/94
341f3ff51cSdownsj  *  Modified for ext2fs by Manuel Bouyer.
355ac2d602Sdownsj  */
365ac2d602Sdownsj 
379b18ffb8Sguenther #include <sys/endian.h>
386b38e8dfSart 
395ac2d602Sdownsj /*
405ac2d602Sdownsj  * Each disk drive contains some number of file systems.
415ac2d602Sdownsj  * A file system consists of a number of cylinder groups.
425ac2d602Sdownsj  * Each cylinder group has inodes and data.
435ac2d602Sdownsj  *
445ac2d602Sdownsj  * A file system is described by its super-block, which in turn
455ac2d602Sdownsj  * describes the cylinder groups.  The super-block is critical
465ac2d602Sdownsj  * data and is replicated in each cylinder group to protect against
475ac2d602Sdownsj  * catastrophic loss.  This is done at `newfs' time and the critical
485ac2d602Sdownsj  * super-block data does not change, so the copies need not be
495ac2d602Sdownsj  * referenced further unless disaster strikes.
505ac2d602Sdownsj  *
515ac2d602Sdownsj  * The first boot and super blocks are given in absolute disk addresses.
525ac2d602Sdownsj  * The byte-offset forms are preferred, as they don't imply a sector size.
535ac2d602Sdownsj  */
545ac2d602Sdownsj #define BBSIZE		1024
555ac2d602Sdownsj #define SBSIZE		1024
565ac2d602Sdownsj #define	BBOFF		((off_t)(0))
575ac2d602Sdownsj #define	SBOFF		((off_t)(BBOFF + BBSIZE))
581abdbfdeSderaadt #define	BBLOCK		((daddr_t)(0))
591abdbfdeSderaadt #define	SBLOCK		((daddr_t)(BBLOCK + BBSIZE / DEV_BSIZE))
605ac2d602Sdownsj 
615ac2d602Sdownsj /*
625970a65fSpelikan  * Inodes are, like in UFS, 32-bit unsigned integers and therefore ufsino_t.
635970a65fSpelikan  * Disk blocks are 32-bit, if the filesystem isn't operating in 64-bit mode
645970a65fSpelikan  * (the incompatible ext4 64BIT flag).  More work is needed to properly use
655970a65fSpelikan  * daddr_t as the disk block data type on both BE and LE architectures.
665970a65fSpelikan  * XXX disk blocks are simply u_int32_t for now.
675ac2d602Sdownsj  */
685ac2d602Sdownsj 
695ac2d602Sdownsj /*
705ac2d602Sdownsj  * MINBSIZE is the smallest allowable block size.
715ac2d602Sdownsj  * MINBSIZE must be big enough to hold a cylinder group block,
725ac2d602Sdownsj  * thus changes to (struct cg) must keep its size within MINBSIZE.
735ac2d602Sdownsj  * Note that super blocks are always of size SBSIZE,
745ac2d602Sdownsj  * and that both SBSIZE and MAXBSIZE must be >= MINBSIZE.
75ea0c9824Spelikan  * FSIZE means fragment size.
765ac2d602Sdownsj  */
775ac2d602Sdownsj #define LOG_MINBSIZE	10
785ac2d602Sdownsj #define MINBSIZE	(1 << LOG_MINBSIZE)
79ea0c9824Spelikan #define LOG_MINFSIZE	10
80ea0c9824Spelikan #define MINFSIZE	(1 << LOG_MINFSIZE)
815ac2d602Sdownsj 
825ac2d602Sdownsj /*
835ac2d602Sdownsj  * The path name on which the file system is mounted is maintained
845ac2d602Sdownsj  * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in
855ac2d602Sdownsj  * the super block for this name.
865ac2d602Sdownsj  */
875ac2d602Sdownsj #define MAXMNTLEN	512
885ac2d602Sdownsj 
895ac2d602Sdownsj /*
905ac2d602Sdownsj  * MINFREE gives the minimum acceptable percentage of file system
915ac2d602Sdownsj  * blocks which may be free. If the freelist drops below this level
925ac2d602Sdownsj  * only the superuser may continue to allocate blocks. This may
935ac2d602Sdownsj  * be set to 0 if no reserve of free blocks is deemed necessary,
945ac2d602Sdownsj  * however throughput drops by fifty percent if the file system
955ac2d602Sdownsj  * is run at between 95% and 100% full; thus the minimum default
965ac2d602Sdownsj  * value of fs_minfree is 5%. However, to get good clustering
975ac2d602Sdownsj  * performance, 10% is a better choice. hence we use 10% as our
985ac2d602Sdownsj  * default value. With 10% free space, fragmentation is not a
995ac2d602Sdownsj  * problem, so we choose to optimize for time.
1005ac2d602Sdownsj  */
1015ac2d602Sdownsj #define MINFREE		5
1025ac2d602Sdownsj 
1035ac2d602Sdownsj /*
1045ac2d602Sdownsj  * Super block for an ext2fs file system.
1055ac2d602Sdownsj  */
1065ac2d602Sdownsj struct ext2fs {
1075ac2d602Sdownsj 	u_int32_t  e2fs_icount;		/* Inode count */
1085ac2d602Sdownsj 	u_int32_t  e2fs_bcount;		/* blocks count */
1095ac2d602Sdownsj 	u_int32_t  e2fs_rbcount;	/* reserved blocks count */
1105ac2d602Sdownsj 	u_int32_t  e2fs_fbcount;	/* free blocks count */
1115ac2d602Sdownsj 	u_int32_t  e2fs_ficount;	/* free inodes count */
1125ac2d602Sdownsj 	u_int32_t  e2fs_first_dblock;	/* first data block */
1135ac2d602Sdownsj 	u_int32_t  e2fs_log_bsize;	/* block size = 1024*(2^e2fs_log_bsize) */
114ea0c9824Spelikan 	u_int32_t  e2fs_log_fsize;	/* fragment size log2 */
1155ac2d602Sdownsj 	u_int32_t  e2fs_bpg;		/* blocks per group */
1165ac2d602Sdownsj 	u_int32_t  e2fs_fpg;		/* frags per group */
1175ac2d602Sdownsj 	u_int32_t  e2fs_ipg;		/* inodes per group */
1185ac2d602Sdownsj 	u_int32_t  e2fs_mtime;		/* mount time */
1195ac2d602Sdownsj 	u_int32_t  e2fs_wtime;		/* write time */
1205ac2d602Sdownsj 	u_int16_t  e2fs_mnt_count;	/* mount count */
1215ac2d602Sdownsj 	u_int16_t  e2fs_max_mnt_count;	/* max mount count */
1225ac2d602Sdownsj 	u_int16_t  e2fs_magic;		/* magic number */
1235ac2d602Sdownsj 	u_int16_t  e2fs_state;		/* file system state */
1245ac2d602Sdownsj 	u_int16_t  e2fs_beh;		/* behavior on errors */
125f5ee6277Sjasoni 	u_int16_t  e2fs_minrev;		/* minor revision level */
1265ac2d602Sdownsj 	u_int32_t  e2fs_lastfsck;	/* time of last fsck */
1275ac2d602Sdownsj 	u_int32_t  e2fs_fsckintv;	/* max time between fscks */
1285ac2d602Sdownsj 	u_int32_t  e2fs_creator;	/* creator OS */
1295ac2d602Sdownsj 	u_int32_t  e2fs_rev;		/* revision level */
1305ac2d602Sdownsj 	u_int16_t  e2fs_ruid;		/* default uid for reserved blocks */
1315ac2d602Sdownsj 	u_int16_t  e2fs_rgid;		/* default gid for reserved blocks */
132f5ee6277Sjasoni 	/* EXT2_DYNAMIC_REV superblocks */
133f5ee6277Sjasoni 	u_int32_t  e2fs_first_ino;	/* first non-reserved inode */
134f5ee6277Sjasoni 	u_int16_t  e2fs_inode_size;	/* size of inode structure */
135f5ee6277Sjasoni 	u_int16_t  e2fs_block_group_nr;	/* block grp number of this sblk*/
136f5ee6277Sjasoni 	u_int32_t  e2fs_features_compat; /*  compatible feature set */
137f5ee6277Sjasoni 	u_int32_t  e2fs_features_incompat; /* incompatible feature set */
138f5ee6277Sjasoni 	u_int32_t  e2fs_features_rocompat; /* RO-compatible feature set */
139f5ee6277Sjasoni 	u_int8_t   e2fs_uuid[16];	/* 128-bit uuid for volume */
140f5ee6277Sjasoni 	char       e2fs_vname[16];	/* volume name */
141f5ee6277Sjasoni 	char       e2fs_fsmnt[64];	/* name mounted on */
142f5ee6277Sjasoni 	u_int32_t  e2fs_algo;		/* For compression */
143f5ee6277Sjasoni 	u_int8_t   e2fs_prealloc;	/* # of blocks to preallocate */
144f5ee6277Sjasoni 	u_int8_t   e2fs_dir_prealloc;	/* # of blocks to preallocate for dir */
145976bae86Sotto 	u_int16_t  e2fs_reserved_ngdb;	/* # of reserved gd blocks for resize */
1465970a65fSpelikan 	/* Ext3 JBD2 journaling. */
1475970a65fSpelikan 	u_int8_t   e2fs_journal_uuid[16];
1485970a65fSpelikan 	u_int32_t  e2fs_journal_ino;
1495970a65fSpelikan 	u_int32_t  e2fs_journal_dev;
1505970a65fSpelikan 	u_int32_t  e2fs_last_orphan;	/* start of list of inodes to delete */
1515970a65fSpelikan 	u_int32_t  e2fs_hash_seed[4];	/* htree hash seed */
1525970a65fSpelikan 	u_int8_t   e2fs_def_hash_version;
1535970a65fSpelikan 	u_int8_t   e2fs_journal_backup_type;
1545970a65fSpelikan 	u_int16_t  e2fs_gdesc_size;
1555970a65fSpelikan 	u_int32_t  e2fs_default_mount_opts;
1565970a65fSpelikan 	u_int32_t  e2fs_first_meta_bg;
1575970a65fSpelikan 	u_int32_t  e2fs_mkfs_time;
1585970a65fSpelikan 	u_int32_t  e2fs_journal_backup[17];
159*d570f06cSmartijn 	u_int32_t  e2fs_bcount_hi;	/* high bits of blocks count */
160*d570f06cSmartijn 	u_int32_t  e2fs_rbcount_hi;	/* high bits of reserved blocks count */
161*d570f06cSmartijn 	u_int32_t  e2fs_fbcount_hi;	/* high bits of free blocks count */
162*d570f06cSmartijn 	u_int16_t  e2fs_min_extra_isize; /* all inodes have some bytes */
163*d570f06cSmartijn 	u_int16_t  e2fs_want_extra_isize;/* inodes must reserve some bytes */
164*d570f06cSmartijn 	u_int32_t  e2fs_flags;		/* miscellaneous flags */
165*d570f06cSmartijn 	u_int16_t  e2fs_raid_stride;	/* RAID stride */
166*d570f06cSmartijn 	u_int16_t  e2fs_mmpintv;		/* seconds to wait in MMP checking */
167*d570f06cSmartijn 	u_int64_t  e2fs_mmpblk;		/* block for multi-mount protection */
168*d570f06cSmartijn 	u_int32_t  e2fs_raid_stripe_wid; /* blocks on data disks (N * stride) */
169*d570f06cSmartijn 	u_int8_t   e2fs_log_gpf;		/* FLEX_BG group size */
170*d570f06cSmartijn 	u_int8_t   e2fs_chksum_type;	/* metadata checksum algorithm used */
171*d570f06cSmartijn 	u_int8_t   e2fs_encrypt;		/* versioning level for encryption */
172*d570f06cSmartijn 	u_int8_t   e2fs_reserved_pad;
173*d570f06cSmartijn 	u_int64_t  e2fs_kbytes_written;	/* number of lifetime kilobytes */
174*d570f06cSmartijn 	u_int32_t  e2fs_snapinum;	/* inode number of active snapshot */
175*d570f06cSmartijn 	u_int32_t  e2fs_snapid;		/* sequential ID of active snapshot */
176*d570f06cSmartijn 	u_int64_t  e2fs_snaprbcount;	/* rsvd blocks for active snapshot */
177*d570f06cSmartijn 	u_int32_t  e2fs_snaplist;	/* inode number for on-disk snapshot */
178*d570f06cSmartijn 	u_int32_t  e2fs_errcount;	/* number of file system errors */
179*d570f06cSmartijn 	u_int32_t  e2fs_first_errtime;	/* first time an error happened */
180*d570f06cSmartijn 	u_int32_t  e2fs_first_errino;	/* inode involved in first error */
181*d570f06cSmartijn 	u_int64_t  e2fs_first_errblk;	/* block involved of first error */
182*d570f06cSmartijn 	u_int8_t   e2fs_first_errfunc[32];/* function where error happened */
183*d570f06cSmartijn 	u_int32_t  e2fs_first_errline;	/* line number where error happened */
184*d570f06cSmartijn 	u_int32_t  e2fs_last_errtime;	/* most recent time of an error */
185*d570f06cSmartijn 	u_int32_t  e2fs_last_errino;	/* inode involved in last error */
186*d570f06cSmartijn 	u_int32_t  e2fs_last_errline;	/* line number where error happened */
187*d570f06cSmartijn 	u_int64_t  e2fs_last_errblk;	/* block involved of last error */
188*d570f06cSmartijn 	u_int8_t   e2fs_last_errfunc[32];/* function where error happened */
189*d570f06cSmartijn 	u_int8_t   e2fs_mount_opts[64];
190*d570f06cSmartijn 	u_int32_t  e2fs_usrquota_inum;	/* inode for tracking user quota */
191*d570f06cSmartijn 	u_int32_t  e2fs_grpquota_inum;	/* inode for tracking group quota */
192*d570f06cSmartijn 	u_int32_t  e2fs_overhead_clusters;/* overhead blocks/clusters */
193*d570f06cSmartijn 	u_int32_t  e2fs_backup_bgs[2];	/* groups with sparse_super2 SBs */
194*d570f06cSmartijn 	u_int8_t   e2fs_encrypt_algos[4];/* encryption algorithms in use */
195*d570f06cSmartijn 	u_int8_t   e2fs_encrypt_pw_salt[16];/* salt used for string2key */
196*d570f06cSmartijn 	u_int32_t  e2fs_lpf_ino;		/* location of the lost+found inode */
197*d570f06cSmartijn 	u_int32_t  e2fs_proj_quota_inum;	/* inode for tracking project quota */
198*d570f06cSmartijn 	u_int32_t  e2fs_chksum_seed;	/* checksum seed */
199*d570f06cSmartijn 	u_int32_t  e2fs_reserved[98];	/* padding to the end of the block */
200*d570f06cSmartijn 	u_int32_t  e2fs_sbchksum;	/* superblock checksum */
2015ac2d602Sdownsj };
2025ac2d602Sdownsj 
2035ac2d602Sdownsj 
2045ac2d602Sdownsj /* in-memory data for ext2fs */
2055ac2d602Sdownsj struct m_ext2fs {
2065ac2d602Sdownsj 	struct ext2fs e2fs;
2075ac2d602Sdownsj 	u_char	e2fs_fsmnt[MAXMNTLEN];	/* name mounted on */
2085ac2d602Sdownsj 	int8_t	e2fs_ronly;	/* mounted read-only flag */
2095ac2d602Sdownsj 	int8_t	e2fs_fmod;	/* super block modified flag */
210ea0c9824Spelikan 	int32_t e2fs_fsize;	/* fragment size */
2115ac2d602Sdownsj 	int32_t	e2fs_bsize;	/* block size */
2125ac2d602Sdownsj 	int32_t e2fs_bshift;	/* ``lblkno'' calc of logical blkno */
2135ac2d602Sdownsj 	int32_t e2fs_bmask;	/* ``blkoff'' calc of blk offsets */
2145ac2d602Sdownsj 	int64_t e2fs_qbmask;	/* ~fs_bmask - for use with quad size */
2155ac2d602Sdownsj 	int32_t	e2fs_fsbtodb;	/* fsbtodb and dbtofsb shift constant */
2165ac2d602Sdownsj 	int32_t	e2fs_ncg;	/* number of cylinder groups */
2175ac2d602Sdownsj 	int32_t	e2fs_ngdb;	/* number of group descriptor block */
2185ac2d602Sdownsj 	int32_t	e2fs_ipb;	/* number of inodes per block */
2195ac2d602Sdownsj 	int32_t	e2fs_itpg;	/* number of inode table per group */
220c7fd3f62Spelikan 	off_t	e2fs_maxfilesize;	/* depends on LARGE/HUGE flags */
221ff12e8beSpedro 	struct	ext2_gd *e2fs_gd; /* group descriptors */
2225ac2d602Sdownsj };
2235ac2d602Sdownsj 
224c7fd3f62Spelikan static inline int
e2fs_overflow(struct m_ext2fs * fs,off_t lower,off_t value)225c7fd3f62Spelikan e2fs_overflow(struct m_ext2fs *fs, off_t lower, off_t value)
226c7fd3f62Spelikan {
227c7fd3f62Spelikan 	return (value < lower || value > fs->e2fs_maxfilesize);
228c7fd3f62Spelikan }
2295ac2d602Sdownsj 
2305ac2d602Sdownsj /*
2315ac2d602Sdownsj  * Filesystem identification
2325ac2d602Sdownsj  */
2335ac2d602Sdownsj #define	E2FS_MAGIC	0xef53	/* the ext2fs magic number */
2346b38e8dfSart #define E2FS_REV0	0	/* revision levels */
2356b38e8dfSart #define E2FS_REV1	1	/* revision levels */
236f5ee6277Sjasoni 
237b66b9ef8Sjsg /* compatible/incompatible features */
238f5ee6277Sjasoni #define EXT2F_COMPAT_PREALLOC		0x0001
239a87a19d1Skevlo #define EXT2F_COMPAT_IMAGIC_INODES	0x0002
240a87a19d1Skevlo #define EXT2F_COMPAT_HAS_JOURNAL	0x0004
241a87a19d1Skevlo #define EXT2F_COMPAT_EXT_ATTR		0x0008
242976bae86Sotto #define EXT2F_COMPAT_RESIZE		0x0010
243a87a19d1Skevlo #define EXT2F_COMPAT_DIR_INDEX		0x0020
244a87a19d1Skevlo #define EXT2F_COMPAT_SPARSE_SUPER2	0x0200
245976bae86Sotto 
246a87a19d1Skevlo #define EXT2F_ROCOMPAT_SPARSE_SUPER	0x0001
247a87a19d1Skevlo #define EXT2F_ROCOMPAT_LARGE_FILE	0x0002
248f5ee6277Sjasoni #define EXT2F_ROCOMPAT_BTREE_DIR	0x0004
2492d7df345Spelikan #define EXT2F_ROCOMPAT_HUGE_FILE	0x0008
250a87a19d1Skevlo #define EXT2F_ROCOMPAT_GDT_CSUM		0x0010
251a87a19d1Skevlo #define EXT2F_ROCOMPAT_DIR_NLINK	0x0020
252a87a19d1Skevlo #define EXT2F_ROCOMPAT_EXTRA_ISIZE	0x0040
253a87a19d1Skevlo #define EXT2F_ROCOMPAT_QUOTA		0x0100
254a87a19d1Skevlo #define EXT2F_ROCOMPAT_BIGALLOC		0x0200
255a87a19d1Skevlo #define EXT2F_ROCOMPAT_METADATA_CKSUM	0x0400
256a87a19d1Skevlo #define EXT2F_ROCOMPAT_READONLY		0x1000
257a87a19d1Skevlo #define EXT2F_ROCOMPAT_PROJECT		0x2000
258f5ee6277Sjasoni 
259f5ee6277Sjasoni #define EXT2F_INCOMPAT_COMP		0x0001
260f5ee6277Sjasoni #define EXT2F_INCOMPAT_FTYPE		0x0002
261f43a7801Sfgsch #define EXT2F_INCOMPAT_RECOVER		0x0004
262f43a7801Sfgsch #define EXT2F_INCOMPAT_JOURNAL_DEV	0x0008
2632d7df345Spelikan #define EXT2F_INCOMPAT_META_BG		0x0010
2642d7df345Spelikan #define EXT2F_INCOMPAT_EXTENTS		0x0040
265a87a19d1Skevlo #define EXT2F_INCOMPAT_64BIT		0x0080
266a87a19d1Skevlo #define EXT2F_INCOMPAT_MMP		0x0100
2672d7df345Spelikan #define EXT2F_INCOMPAT_FLEX_BG		0x0200
268a87a19d1Skevlo #define EXT2F_INCOMPAT_EA_INODE		0x0400
269a87a19d1Skevlo #define EXT2F_INCOMPAT_DIRDATA		0x1000
270a87a19d1Skevlo #define EXT2F_INCOMPAT_CSUM_SEED	0x2000
271a87a19d1Skevlo #define EXT2F_INCOMPAT_LARGEDIR		0x4000
272a87a19d1Skevlo #define EXT2F_INCOMPAT_INLINE_DATA	0x8000
273a87a19d1Skevlo #define EXT2F_INCOMPAT_ENCRYPT		0x10000
274a87a19d1Skevlo 
275a87a19d1Skevlo struct ext2_feature {
276a87a19d1Skevlo 	uint32_t mask;
277a87a19d1Skevlo 	const char *name;
278a87a19d1Skevlo };
279a87a19d1Skevlo 
280a87a19d1Skevlo static const struct ext2_feature ro_compat[] = {
281a87a19d1Skevlo 	{ EXT2F_ROCOMPAT_SPARSE_SUPER,		"sparse_super" },
282a87a19d1Skevlo 	{ EXT2F_ROCOMPAT_LARGE_FILE,		"large_file" },
283a87a19d1Skevlo 	{ EXT2F_ROCOMPAT_BTREE_DIR,		"btree_dir" },
284a87a19d1Skevlo 	{ EXT2F_ROCOMPAT_HUGE_FILE,		"huge_file" },
285a87a19d1Skevlo 	{ EXT2F_ROCOMPAT_GDT_CSUM,		"uninit_bg" },
286a87a19d1Skevlo 	{ EXT2F_ROCOMPAT_DIR_NLINK,		"dir_nlink" },
287a87a19d1Skevlo 	{ EXT2F_ROCOMPAT_EXTRA_ISIZE,		"extra_isize" },
288a87a19d1Skevlo 	{ EXT2F_ROCOMPAT_QUOTA,			"quota" },
289a87a19d1Skevlo 	{ EXT2F_ROCOMPAT_BIGALLOC,		"bigalloc" },
290a87a19d1Skevlo 	{ EXT2F_ROCOMPAT_METADATA_CKSUM,	"metadata_csum" },
291a87a19d1Skevlo 	{ EXT2F_ROCOMPAT_READONLY,		"read-only" },
292a87a19d1Skevlo 	{ EXT2F_ROCOMPAT_PROJECT,		"project" }
293a87a19d1Skevlo };
294a87a19d1Skevlo 
295a87a19d1Skevlo static const struct ext2_feature incompat[] = {
296a87a19d1Skevlo 	{ EXT2F_INCOMPAT_COMP,		"compression" },
297a87a19d1Skevlo 	{ EXT2F_INCOMPAT_FTYPE,		"filetype" },
298a87a19d1Skevlo 	{ EXT2F_INCOMPAT_RECOVER,	"needs_recovery" },
299a87a19d1Skevlo 	{ EXT2F_INCOMPAT_JOURNAL_DEV,	"journal_dev" },
300a87a19d1Skevlo 	{ EXT2F_INCOMPAT_META_BG,	"meta_bg" },
301a87a19d1Skevlo 	{ EXT2F_INCOMPAT_EXTENTS,	"extents" },
302a87a19d1Skevlo 	{ EXT2F_INCOMPAT_64BIT,		"64bit" },
303a87a19d1Skevlo 	{ EXT2F_INCOMPAT_MMP,		"mmp" },
304a87a19d1Skevlo 	{ EXT2F_INCOMPAT_FLEX_BG,	"flex_bg" },
305a87a19d1Skevlo 	{ EXT2F_INCOMPAT_EA_INODE,	"ea_inode" },
306a87a19d1Skevlo 	{ EXT2F_INCOMPAT_DIRDATA,	"dirdata" },
307a87a19d1Skevlo 	{ EXT2F_INCOMPAT_CSUM_SEED,	"metadata_csum_seed" },
308a87a19d1Skevlo 	{ EXT2F_INCOMPAT_LARGEDIR,	"large_dir" },
309a87a19d1Skevlo 	{ EXT2F_INCOMPAT_INLINE_DATA,	"inline_data" },
310a87a19d1Skevlo 	{ EXT2F_INCOMPAT_ENCRYPT,	"encrypt" }
311a87a19d1Skevlo };
312f5ee6277Sjasoni 
313f5ee6277Sjasoni /* features supported in this implementation */
314f5ee6277Sjasoni #define EXT2F_COMPAT_SUPP		0x0000
315a87a19d1Skevlo #define EXT2F_ROCOMPAT_SUPP		(EXT2F_ROCOMPAT_SPARSE_SUPER | \
316a87a19d1Skevlo 					 EXT2F_ROCOMPAT_LARGE_FILE)
3172d7df345Spelikan #define EXT2F_INCOMPAT_SUPP		(EXT2F_INCOMPAT_FTYPE)
3182d7df345Spelikan #define EXT4F_RO_INCOMPAT_SUPP		(EXT2F_INCOMPAT_EXTENTS | \
3192d7df345Spelikan 					 EXT2F_INCOMPAT_FLEX_BG | \
3202d7df345Spelikan 					 EXT2F_INCOMPAT_META_BG | \
3212d7df345Spelikan 					 EXT2F_INCOMPAT_RECOVER)
3225ac2d602Sdownsj 
3235ac2d602Sdownsj /*
324976bae86Sotto  * Definitions of behavior on errors
325976bae86Sotto  */
326976bae86Sotto #define E2FS_BEH_CONTINUE	1	/* continue operation */
327976bae86Sotto #define E2FS_BEH_READONLY	2	/* remount fs read only */
328976bae86Sotto #define E2FS_BEH_PANIC		3	/* cause panic */
329976bae86Sotto #define E2FS_BEH_DEFAULT	E2FS_BEH_CONTINUE
330976bae86Sotto 
331976bae86Sotto /*
3325ac2d602Sdownsj  * OS identification
3335ac2d602Sdownsj  */
3345ac2d602Sdownsj #define E2FS_OS_LINUX 0
3355ac2d602Sdownsj #define E2FS_OS_HURD  1
3365ac2d602Sdownsj #define E2FS_OS_MASIX 2
3375ac2d602Sdownsj 
3385ac2d602Sdownsj /*
3395ac2d602Sdownsj  * Filesystem clean flags
3405ac2d602Sdownsj  */
3415ac2d602Sdownsj #define	E2FS_ISCLEAN	0x01
3425ac2d602Sdownsj #define	E2FS_ERRORS	0x02
3435ac2d602Sdownsj 
3445ac2d602Sdownsj /* ext2 file system block group descriptor */
3455ac2d602Sdownsj 
3465ac2d602Sdownsj struct ext2_gd {
3475ac2d602Sdownsj 	u_int32_t ext2bgd_b_bitmap;	/* blocks bitmap block */
3485ac2d602Sdownsj 	u_int32_t ext2bgd_i_bitmap;	/* inodes bitmap block */
3495ac2d602Sdownsj 	u_int32_t ext2bgd_i_tables;	/* inodes table block  */
3505ac2d602Sdownsj 	u_int16_t ext2bgd_nbfree;	/* number of free blocks */
3515ac2d602Sdownsj 	u_int16_t ext2bgd_nifree;	/* number of free inodes */
3525ac2d602Sdownsj 	u_int16_t ext2bgd_ndirs;	/* number of directories */
3535ac2d602Sdownsj 	u_int16_t reserved;
3545ac2d602Sdownsj 	u_int32_t reserved2[3];
3555ac2d602Sdownsj };
3565ac2d602Sdownsj 
3575ac2d602Sdownsj /*
358a87a19d1Skevlo  * If the EXT2F_ROCOMPAT_SPARSE_SUPER flag is set, the cylinder group has a
359f5ee6277Sjasoni  * copy of the super and cylinder group descriptors blocks only if it's
360f5ee6277Sjasoni  * a power of 3, 5 or 7
361f5ee6277Sjasoni  */
362f5ee6277Sjasoni 
363c4071fd1Smillert static __inline__ int cg_has_sb(int) __attribute__((__unused__));
364f5ee6277Sjasoni static __inline int
cg_has_sb(int i)365214e55adSjsg cg_has_sb(int i)
366f5ee6277Sjasoni {
367f5ee6277Sjasoni 	int a3 ,a5 , a7;
368f5ee6277Sjasoni 
369f5ee6277Sjasoni 	if (i == 0 || i == 1)
370f5ee6277Sjasoni 		return 1;
371f5ee6277Sjasoni 	for (a3 = 3, a5 = 5, a7 = 7;
372f5ee6277Sjasoni 	    a3 <= i || a5 <= i || a7 <= i;
373f5ee6277Sjasoni 	    a3 *= 3, a5 *= 5, a7 *= 7)
374f5ee6277Sjasoni 		if (i == a3 || i == a5 || i == a7)
375f5ee6277Sjasoni 			return 1;
376f5ee6277Sjasoni 	return 0;
377f5ee6277Sjasoni }
378f5ee6277Sjasoni 
379f5ee6277Sjasoni /*
380f7dbefaaSpelikan  * Ext2 metadata is stored in little-endian byte order.
381f7dbefaaSpelikan  * JBD2 journal used in ext3 and ext4 is big-endian!
382f5ee6277Sjasoni  */
3836b38e8dfSart #if BYTE_ORDER == LITTLE_ENDIAN
3846b38e8dfSart #define e2fs_sbload(old, new) memcpy((new), (old), SBSIZE);
3856b38e8dfSart #define e2fs_cgload(old, new, size) memcpy((new), (old), (size));
3866b38e8dfSart #define e2fs_sbsave(old, new) memcpy((new), (old), SBSIZE);
3876b38e8dfSart #define e2fs_cgsave(old, new, size) memcpy((new), (old), (size));
3886b38e8dfSart #else
389c4071fd1Smillert void e2fs_sb_bswap(struct ext2fs *, struct ext2fs *);
390c4071fd1Smillert void e2fs_cg_bswap(struct ext2_gd *, struct ext2_gd *, int);
3916b38e8dfSart #define e2fs_sbload(old, new) e2fs_sb_bswap((old), (new))
3926b38e8dfSart #define e2fs_cgload(old, new, size) e2fs_cg_bswap((old), (new), (size));
3936b38e8dfSart #define e2fs_sbsave(old, new) e2fs_sb_bswap((old), (new))
3946b38e8dfSart #define e2fs_cgsave(old, new, size) e2fs_cg_bswap((old), (new), (size));
3956b38e8dfSart #endif
396f5ee6277Sjasoni 
397f5ee6277Sjasoni /*
3985ac2d602Sdownsj  * Turn file system block numbers into disk block addresses.
3995ac2d602Sdownsj  * This maps file system blocks to device size blocks.
4005ac2d602Sdownsj  */
4015ac2d602Sdownsj #define fsbtodb(fs, b)	((b) << (fs)->e2fs_fsbtodb)
4025ac2d602Sdownsj #define dbtofsb(fs, b)	((b) >> (fs)->e2fs_fsbtodb)
4035ac2d602Sdownsj 
4045ac2d602Sdownsj /*
4055ac2d602Sdownsj  * Macros for handling inode numbers:
4065ac2d602Sdownsj  *	 inode number to file system block offset.
4075ac2d602Sdownsj  *	 inode number to cylinder group number.
4085ac2d602Sdownsj  *	 inode number to file system block address.
4095ac2d602Sdownsj  */
4105ac2d602Sdownsj #define	ino_to_cg(fs, x)	(((x) - 1) / (fs)->e2fs.e2fs_ipg)
4115ac2d602Sdownsj #define	ino_to_fsba(fs, x)						\
4125ac2d602Sdownsj 	((fs)->e2fs_gd[ino_to_cg(fs, x)].ext2bgd_i_tables + \
4135ac2d602Sdownsj 	(((x)-1) % (fs)->e2fs.e2fs_ipg)/(fs)->e2fs_ipb)
4145ac2d602Sdownsj #define	ino_to_fsbo(fs, x)	(((x)-1) % (fs)->e2fs_ipb)
4155ac2d602Sdownsj 
4165ac2d602Sdownsj /*
4175ac2d602Sdownsj  * Give cylinder group number for a file system block.
4185ac2d602Sdownsj  * Give cylinder group block number for a file system block.
4195ac2d602Sdownsj  */
4205ac2d602Sdownsj #define	dtog(fs, d) (((d) - (fs)->e2fs.e2fs_first_dblock) / (fs)->e2fs.e2fs_fpg)
4215ac2d602Sdownsj #define	dtogd(fs, d) \
4225ac2d602Sdownsj 	(((d) - (fs)->e2fs.e2fs_first_dblock) % (fs)->e2fs.e2fs_fpg)
4235ac2d602Sdownsj 
4245ac2d602Sdownsj /*
4255ac2d602Sdownsj  * The following macros optimize certain frequently calculated
4265ac2d602Sdownsj  * quantities by using shifts and masks in place of divisions
4275ac2d602Sdownsj  * modulos and multiplications.
4285ac2d602Sdownsj  */
4295ac2d602Sdownsj #define blkoff(fs, loc)		/* calculates (loc % fs->e2fs_bsize) */ \
4305ac2d602Sdownsj 	((loc) & (fs)->e2fs_qbmask)
4315ac2d602Sdownsj #define lblktosize(fs, blk)	/* calculates (blk * fs->e2fs_bsize) */ \
4325ac2d602Sdownsj 	((blk) << (fs)->e2fs_bshift)
4335ac2d602Sdownsj #define lblkno(fs, loc)		/* calculates (loc / fs->e2fs_bsize) */ \
4345ac2d602Sdownsj 	((loc) >> (fs)->e2fs_bshift)
4355ac2d602Sdownsj #define blkroundup(fs, size)	/* calculates roundup(size, fs->e2fs_bsize) */ \
4365ac2d602Sdownsj 	(((size) + (fs)->e2fs_qbmask) & (fs)->e2fs_bmask)
4375ac2d602Sdownsj #define fragroundup(fs, size)	/* calculates roundup(size, fs->e2fs_bsize) */ \
4385ac2d602Sdownsj 	(((size) + (fs)->e2fs_qbmask) & (fs)->e2fs_bmask)
4395ac2d602Sdownsj /*
4405ac2d602Sdownsj  * Determine the number of available frags given a
4415ac2d602Sdownsj  * percentage to hold in reserve.
4425ac2d602Sdownsj  */
4435ac2d602Sdownsj #define freespace(fs) \
4445ac2d602Sdownsj    ((fs)->e2fs.e2fs_fbcount - (fs)->e2fs.e2fs_rbcount)
4455ac2d602Sdownsj 
4465ac2d602Sdownsj /*
4475ac2d602Sdownsj  * Number of indirects in a file system block.
4485ac2d602Sdownsj  */
4495ac2d602Sdownsj #define	NINDIR(fs)	((fs)->e2fs_bsize / sizeof(u_int32_t))
450