xref: /minix3/sys/ufs/ext2fs/ext2fs.h (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /*	$NetBSD: ext2fs.h,v 1.36 2013/06/23 07:28:37 dholland Exp $	*/
2d65f6f70SBen Gras 
3d65f6f70SBen Gras /*
4d65f6f70SBen Gras  * Copyright (c) 1982, 1986, 1993
5d65f6f70SBen Gras  *	The Regents of the University of California.  All rights reserved.
6d65f6f70SBen Gras  *
7d65f6f70SBen Gras  * Redistribution and use in source and binary forms, with or without
8d65f6f70SBen Gras  * modification, are permitted provided that the following conditions
9d65f6f70SBen Gras  * are met:
10d65f6f70SBen Gras  * 1. Redistributions of source code must retain the above copyright
11d65f6f70SBen Gras  *    notice, this list of conditions and the following disclaimer.
12d65f6f70SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
13d65f6f70SBen Gras  *    notice, this list of conditions and the following disclaimer in the
14d65f6f70SBen Gras  *    documentation and/or other materials provided with the distribution.
15d65f6f70SBen Gras  * 3. Neither the name of the University nor the names of its contributors
16d65f6f70SBen Gras  *    may be used to endorse or promote products derived from this software
17d65f6f70SBen Gras  *    without specific prior written permission.
18d65f6f70SBen Gras  *
19d65f6f70SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20d65f6f70SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21d65f6f70SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22d65f6f70SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23d65f6f70SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24d65f6f70SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25d65f6f70SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26d65f6f70SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27d65f6f70SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28d65f6f70SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29d65f6f70SBen Gras  * SUCH DAMAGE.
30d65f6f70SBen Gras  *
31d65f6f70SBen Gras  *	@(#)fs.h	8.10 (Berkeley) 10/27/94
32d65f6f70SBen Gras  *  Modified for ext2fs by Manuel Bouyer.
33d65f6f70SBen Gras  */
34d65f6f70SBen Gras 
35d65f6f70SBen Gras /*
36d65f6f70SBen Gras  * Copyright (c) 1997 Manuel Bouyer.
37d65f6f70SBen Gras  *
38d65f6f70SBen Gras  * Redistribution and use in source and binary forms, with or without
39d65f6f70SBen Gras  * modification, are permitted provided that the following conditions
40d65f6f70SBen Gras  * are met:
41d65f6f70SBen Gras  * 1. Redistributions of source code must retain the above copyright
42d65f6f70SBen Gras  *    notice, this list of conditions and the following disclaimer.
43d65f6f70SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
44d65f6f70SBen Gras  *    notice, this list of conditions and the following disclaimer in the
45d65f6f70SBen Gras  *    documentation and/or other materials provided with the distribution.
46d65f6f70SBen Gras  *
47d65f6f70SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48d65f6f70SBen Gras  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49d65f6f70SBen Gras  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50d65f6f70SBen Gras  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51d65f6f70SBen Gras  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52d65f6f70SBen Gras  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53d65f6f70SBen Gras  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54d65f6f70SBen Gras  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55d65f6f70SBen Gras  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56d65f6f70SBen Gras  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57d65f6f70SBen Gras  *
58d65f6f70SBen Gras  *	@(#)fs.h	8.10 (Berkeley) 10/27/94
59d65f6f70SBen Gras  *  Modified for ext2fs by Manuel Bouyer.
60d65f6f70SBen Gras  */
61d65f6f70SBen Gras 
62d65f6f70SBen Gras #ifndef _UFS_EXT2FS_EXT2FS_H_
63d65f6f70SBen Gras #define _UFS_EXT2FS_EXT2FS_H_
64d65f6f70SBen Gras 
65d65f6f70SBen Gras #include <sys/bswap.h>
66d65f6f70SBen Gras 
67d65f6f70SBen Gras /*
68d65f6f70SBen Gras  * Each disk drive contains some number of file systems.
69d65f6f70SBen Gras  * A file system consists of a number of cylinder groups.
70d65f6f70SBen Gras  * Each cylinder group has inodes and data.
71d65f6f70SBen Gras  *
72d65f6f70SBen Gras  * A file system is described by its super-block, which in turn
73d65f6f70SBen Gras  * describes the cylinder groups.  The super-block is critical
74d65f6f70SBen Gras  * data and is replicated in each cylinder group to protect against
75d65f6f70SBen Gras  * catastrophic loss.  This is done at `newfs' time and the critical
76d65f6f70SBen Gras  * super-block data does not change, so the copies need not be
77d65f6f70SBen Gras  * referenced further unless disaster strikes.
78d65f6f70SBen Gras  *
79d65f6f70SBen Gras  * The first boot and super blocks are given in absolute disk addresses.
80d65f6f70SBen Gras  * The byte-offset forms are preferred, as they don't imply a sector size.
81d65f6f70SBen Gras  */
82d65f6f70SBen Gras #define BBSIZE		1024
83d65f6f70SBen Gras #define SBSIZE		1024
84d65f6f70SBen Gras #define	BBOFF		((off_t)(0))
85d65f6f70SBen Gras #define	SBOFF		((off_t)(BBOFF + BBSIZE))
86d65f6f70SBen Gras #define	BBLOCK		((daddr_t)(0))
87d65f6f70SBen Gras #define	SBLOCK		((daddr_t)(BBLOCK + BBSIZE / DEV_BSIZE))
88d65f6f70SBen Gras 
89d65f6f70SBen Gras /*
90d65f6f70SBen Gras  * Addresses stored in inodes are capable of addressing blocks
91d65f6f70SBen Gras  * XXX
92d65f6f70SBen Gras  */
93d65f6f70SBen Gras 
94d65f6f70SBen Gras /*
95d65f6f70SBen Gras  * MINBSIZE is the smallest allowable block size.
96d65f6f70SBen Gras  * MINBSIZE must be big enough to hold a cylinder group block,
97d65f6f70SBen Gras  * thus changes to (struct cg) must keep its size within MINBSIZE.
98d65f6f70SBen Gras  * Note that super blocks are always of size SBSIZE,
99d65f6f70SBen Gras  * and that both SBSIZE and MAXBSIZE must be >= MINBSIZE.
100d65f6f70SBen Gras  */
101d65f6f70SBen Gras #define LOG_MINBSIZE	10
102d65f6f70SBen Gras #define MINBSIZE	(1 << LOG_MINBSIZE)
103d65f6f70SBen Gras 
104d65f6f70SBen Gras /*
105d65f6f70SBen Gras  * The path name on which the file system is mounted is maintained
106d65f6f70SBen Gras  * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in
107d65f6f70SBen Gras  * the super block for this name.
108d65f6f70SBen Gras  */
109d65f6f70SBen Gras #define MAXMNTLEN	512
110d65f6f70SBen Gras 
111d65f6f70SBen Gras /*
112d65f6f70SBen Gras  * MINFREE gives the minimum acceptable percentage of file system
113d65f6f70SBen Gras  * blocks which may be free. If the freelist drops below this level
114d65f6f70SBen Gras  * only the superuser may continue to allocate blocks. This may
115d65f6f70SBen Gras  * be set to 0 if no reserve of free blocks is deemed necessary,
116d65f6f70SBen Gras  * however throughput drops by fifty percent if the file system
117d65f6f70SBen Gras  * is run at between 95% and 100% full; thus the minimum default
118d65f6f70SBen Gras  * value of fs_minfree is 5%. However, to get good clustering
119d65f6f70SBen Gras  * performance, 10% is a better choice. hence we use 10% as our
120d65f6f70SBen Gras  * default value. With 10% free space, fragmentation is not a
121d65f6f70SBen Gras  * problem, so we choose to optimize for time.
122d65f6f70SBen Gras  */
123d65f6f70SBen Gras #define MINFREE		5
124d65f6f70SBen Gras 
125d65f6f70SBen Gras /*
126d65f6f70SBen Gras  * Super block for an ext2fs file system.
127d65f6f70SBen Gras  */
128d65f6f70SBen Gras struct ext2fs {
129d65f6f70SBen Gras 	uint32_t  e2fs_icount;		/* Inode count */
130d65f6f70SBen Gras 	uint32_t  e2fs_bcount;		/* blocks count */
131d65f6f70SBen Gras 	uint32_t  e2fs_rbcount;		/* reserved blocks count */
132d65f6f70SBen Gras 	uint32_t  e2fs_fbcount;		/* free blocks count */
133d65f6f70SBen Gras 	uint32_t  e2fs_ficount;		/* free inodes count */
134d65f6f70SBen Gras 	uint32_t  e2fs_first_dblock;	/* first data block */
135d65f6f70SBen Gras 	uint32_t  e2fs_log_bsize;	/* block size = 1024*(2^e2fs_log_bsize) */
136d65f6f70SBen Gras 	uint32_t  e2fs_fsize;		/* fragment size */
137d65f6f70SBen Gras 	uint32_t  e2fs_bpg;		/* blocks per group */
138d65f6f70SBen Gras 	uint32_t  e2fs_fpg;		/* frags per group */
139d65f6f70SBen Gras 	uint32_t  e2fs_ipg;		/* inodes per group */
140d65f6f70SBen Gras 	uint32_t  e2fs_mtime;		/* mount time */
141d65f6f70SBen Gras 	uint32_t  e2fs_wtime;		/* write time */
142d65f6f70SBen Gras 	uint16_t  e2fs_mnt_count;	/* mount count */
143d65f6f70SBen Gras 	uint16_t  e2fs_max_mnt_count;	/* max mount count */
144d65f6f70SBen Gras 	uint16_t  e2fs_magic;		/* magic number */
145d65f6f70SBen Gras 	uint16_t  e2fs_state;		/* file system state */
146d65f6f70SBen Gras 	uint16_t  e2fs_beh;		/* behavior on errors */
147d65f6f70SBen Gras 	uint16_t  e2fs_minrev;		/* minor revision level */
148d65f6f70SBen Gras 	uint32_t  e2fs_lastfsck;	/* time of last fsck */
149d65f6f70SBen Gras 	uint32_t  e2fs_fsckintv;	/* max time between fscks */
150d65f6f70SBen Gras 	uint32_t  e2fs_creator;		/* creator OS */
151d65f6f70SBen Gras 	uint32_t  e2fs_rev;		/* revision level */
152d65f6f70SBen Gras 	uint16_t  e2fs_ruid;		/* default uid for reserved blocks */
153d65f6f70SBen Gras 	uint16_t  e2fs_rgid;		/* default gid for reserved blocks */
154d65f6f70SBen Gras 	/* EXT2_DYNAMIC_REV superblocks */
155d65f6f70SBen Gras 	uint32_t  e2fs_first_ino;	/* first non-reserved inode */
156d65f6f70SBen Gras 	uint16_t  e2fs_inode_size;	/* size of inode structure */
157d65f6f70SBen Gras 	uint16_t  e2fs_block_group_nr;	/* block grp number of this sblk*/
158d65f6f70SBen Gras 	uint32_t  e2fs_features_compat;	/*  compatible feature set */
159d65f6f70SBen Gras 	uint32_t  e2fs_features_incompat; /* incompatible feature set */
160d65f6f70SBen Gras 	uint32_t  e2fs_features_rocompat; /* RO-compatible feature set */
161d65f6f70SBen Gras 	uint8_t   e2fs_uuid[16];	/* 128-bit uuid for volume */
162d65f6f70SBen Gras 	char      e2fs_vname[16];	/* volume name */
163d65f6f70SBen Gras 	char      e2fs_fsmnt[64];	/* name mounted on */
164d65f6f70SBen Gras 	uint32_t  e2fs_algo;		/* For compression */
165d65f6f70SBen Gras 	uint8_t   e2fs_prealloc;	/* # of blocks to preallocate */
166d65f6f70SBen Gras 	uint8_t   e2fs_dir_prealloc;	/* # of blocks to preallocate for dir */
167d65f6f70SBen Gras 	uint16_t  e2fs_reserved_ngdb;	/* # of reserved gd blocks for resize */
168d65f6f70SBen Gras 	uint32_t  reserved2[204];
169d65f6f70SBen Gras };
170d65f6f70SBen Gras 
171d65f6f70SBen Gras 
172d65f6f70SBen Gras /* in-memory data for ext2fs */
173d65f6f70SBen Gras struct m_ext2fs {
174d65f6f70SBen Gras 	struct ext2fs e2fs;
175d65f6f70SBen Gras 	u_char	e2fs_fsmnt[MAXMNTLEN];	/* name mounted on */
176d65f6f70SBen Gras 	int8_t	e2fs_ronly;	/* mounted read-only flag */
177d65f6f70SBen Gras 	int8_t	e2fs_fmod;	/* super block modified flag */
178d65f6f70SBen Gras 	int32_t	e2fs_bsize;	/* block size */
179d65f6f70SBen Gras 	int32_t e2fs_bshift;	/* ``lblkno'' calc of logical blkno */
180d65f6f70SBen Gras 	int32_t e2fs_bmask;	/* ``blkoff'' calc of blk offsets */
181d65f6f70SBen Gras 	int64_t e2fs_qbmask;	/* ~fs_bmask - for use with quad size */
182d65f6f70SBen Gras 	int32_t	e2fs_fsbtodb;	/* fsbtodb and dbtofsb shift constant */
183d65f6f70SBen Gras 	int32_t	e2fs_ncg;	/* number of cylinder groups */
184d65f6f70SBen Gras 	int32_t	e2fs_ngdb;	/* number of group descriptor block */
185d65f6f70SBen Gras 	int32_t	e2fs_ipb;	/* number of inodes per block */
186d65f6f70SBen Gras 	int32_t	e2fs_itpg;	/* number of inode table per group */
187d65f6f70SBen Gras 	struct	ext2_gd *e2fs_gd; /* group descripors */
188d65f6f70SBen Gras };
189d65f6f70SBen Gras 
190d65f6f70SBen Gras 
191d65f6f70SBen Gras 
192d65f6f70SBen Gras /*
193d65f6f70SBen Gras  * Filesystem identification
194d65f6f70SBen Gras  */
195d65f6f70SBen Gras #define	E2FS_MAGIC	0xef53	/* the ext2fs magic number */
196d65f6f70SBen Gras #define E2FS_REV0	0	/* GOOD_OLD revision */
197d65f6f70SBen Gras #define E2FS_REV1	1	/* Support compat/incompat features */
198d65f6f70SBen Gras 
199d65f6f70SBen Gras /* compatible/incompatible features */
200d65f6f70SBen Gras #define EXT2F_COMPAT_PREALLOC		0x0001
201*84d9c625SLionel Sambuc #define EXT2F_COMPAT_AFS		0x0002
202d65f6f70SBen Gras #define EXT2F_COMPAT_HASJOURNAL		0x0004
203*84d9c625SLionel Sambuc #define EXT2F_COMPAT_EXTATTR		0x0008
204d65f6f70SBen Gras #define EXT2F_COMPAT_RESIZE		0x0010
205*84d9c625SLionel Sambuc #define EXT2F_COMPAT_DIRHASHINDEX	0x0020
206*84d9c625SLionel Sambuc #define	EXT2F_COMPAT_BITS \
207*84d9c625SLionel Sambuc 	"\20" \
208*84d9c625SLionel Sambuc 	"\06COMPAT_DIRHASHINDEX" \
209*84d9c625SLionel Sambuc 	"\05COMPAT_RESIZE" \
210*84d9c625SLionel Sambuc 	"\04COMPAT_EXTATTR" \
211*84d9c625SLionel Sambuc 	"\03COMPAT_HASJOURNAL" \
212*84d9c625SLionel Sambuc 	"\02COMPAT_AFS" \
213*84d9c625SLionel Sambuc 	"\01COMPAT_PREALLOC"
214d65f6f70SBen Gras 
215d65f6f70SBen Gras #define EXT2F_ROCOMPAT_SPARSESUPER	0x0001
216d65f6f70SBen Gras #define EXT2F_ROCOMPAT_LARGEFILE	0x0002
217d65f6f70SBen Gras #define EXT2F_ROCOMPAT_BTREE_DIR	0x0004
218*84d9c625SLionel Sambuc #define EXT2F_ROCOMPAT_HUGE_FILE	0x0008
219*84d9c625SLionel Sambuc #define EXT2F_ROCOMPAT_GDT_CSUM		0x0010
220*84d9c625SLionel Sambuc #define EXT2F_ROCOMPAT_DIR_NLINK	0x0020
221*84d9c625SLionel Sambuc #define EXT2F_ROCOMPAT_EXTRA_ISIZE	0x0040
222*84d9c625SLionel Sambuc #define	EXT2F_ROCOMPAT_BITS \
223*84d9c625SLionel Sambuc 	"\20" \
224*84d9c625SLionel Sambuc 	"\07ROCOMPAT_EXTRA_ISIZE" \
225*84d9c625SLionel Sambuc 	"\06ROCOMPAT_DIR_NLINK" \
226*84d9c625SLionel Sambuc 	"\05ROCOMPAT_GDT_CSUM" \
227*84d9c625SLionel Sambuc 	"\04ROCOMPAT_HUGE_FILE" \
228*84d9c625SLionel Sambuc 	"\03ROCOMPAT_BTREE_DIR" \
229*84d9c625SLionel Sambuc 	"\02ROCOMPAT_LARGEFILE" \
230*84d9c625SLionel Sambuc 	"\01ROCOMPAT_SPARSESUPER"
231d65f6f70SBen Gras 
232d65f6f70SBen Gras #define EXT2F_INCOMPAT_COMP		0x0001
233d65f6f70SBen Gras #define EXT2F_INCOMPAT_FTYPE		0x0002
234*84d9c625SLionel Sambuc #define	EXT2F_INCOMPAT_REPLAY_JOURNAL	0x0004
235*84d9c625SLionel Sambuc #define	EXT2F_INCOMPAT_USES_JOURNAL	0x0008
236*84d9c625SLionel Sambuc #define EXT2F_INCOMPAT_META_BG		0x0010
237*84d9c625SLionel Sambuc #define EXT2F_INCOMPAT_EXTENTS		0x0040
238*84d9c625SLionel Sambuc #define EXT2F_INCOMPAT_64BIT		0x0080
239*84d9c625SLionel Sambuc #define EXT2F_INCOMPAT_MMP		0x0100
240*84d9c625SLionel Sambuc #define EXT2F_INCOMPAT_FLEX_BG		0x0200
241*84d9c625SLionel Sambuc #define	EXT2F_INCOMPAT_BITS \
242*84d9c625SLionel Sambuc 	"\20" \
243*84d9c625SLionel Sambuc 	"\012INCOMPAT_FLEX_BG" \
244*84d9c625SLionel Sambuc 	"\011INCOMPAT_MMP" \
245*84d9c625SLionel Sambuc 	"\010INCOMPAT_64BIT" \
246*84d9c625SLionel Sambuc 	"\07INCOMPAT_EXTENTS" \
247*84d9c625SLionel Sambuc 	"\05INCOMPAT_META_BG" \
248*84d9c625SLionel Sambuc 	"\04INCOMPAT_USES_JOURNAL" \
249*84d9c625SLionel Sambuc 	"\03INCOMPAT_REPLAY_JOURNAL" \
250*84d9c625SLionel Sambuc 	"\02INCOMPAT_FTYPE" \
251*84d9c625SLionel Sambuc 	"\01INCOMPAT_COMP"
252d65f6f70SBen Gras 
253d65f6f70SBen Gras /*
254d65f6f70SBen Gras  * Features supported in this implementation
255d65f6f70SBen Gras  *
256d65f6f70SBen Gras  * We support the following REV1 features:
257d65f6f70SBen Gras  * - EXT2F_ROCOMPAT_SPARSESUPER
258d65f6f70SBen Gras  *    superblock backups stored only in cg_has_sb(bno) groups
259d65f6f70SBen Gras  * - EXT2F_ROCOMPAT_LARGEFILE
260d65f6f70SBen Gras  *    use e2di_dacl in struct ext2fs_dinode to store
261d65f6f70SBen Gras  *    upper 32bit of size for >2GB files
262d65f6f70SBen Gras  * - EXT2F_INCOMPAT_FTYPE
263d65f6f70SBen Gras  *    store file type to e2d_type in struct ext2fs_direct
264d65f6f70SBen Gras  *    (on REV0 e2d_namlen is uint16_t and no e2d_type, like ffs)
265d65f6f70SBen Gras  */
266d65f6f70SBen Gras #define EXT2F_COMPAT_SUPP		0x0000
267d65f6f70SBen Gras #define EXT2F_ROCOMPAT_SUPP		(EXT2F_ROCOMPAT_SPARSESUPER \
268*84d9c625SLionel Sambuc 					 | EXT2F_ROCOMPAT_LARGEFILE \
269*84d9c625SLionel Sambuc 					 | EXT2F_ROCOMPAT_HUGE_FILE)
270d65f6f70SBen Gras #define EXT2F_INCOMPAT_SUPP		EXT2F_INCOMPAT_FTYPE
271d65f6f70SBen Gras 
272d65f6f70SBen Gras /*
273d65f6f70SBen Gras  * Definitions of behavior on errors
274d65f6f70SBen Gras  */
275d65f6f70SBen Gras #define E2FS_BEH_CONTINUE	1	/* continue operation */
276d65f6f70SBen Gras #define E2FS_BEH_READONLY	2	/* remount fs read only */
277d65f6f70SBen Gras #define E2FS_BEH_PANIC		3	/* cause panic */
278d65f6f70SBen Gras #define E2FS_BEH_DEFAULT	E2FS_BEH_CONTINUE
279d65f6f70SBen Gras 
280d65f6f70SBen Gras /*
281d65f6f70SBen Gras  * OS identification
282d65f6f70SBen Gras  */
283d65f6f70SBen Gras #define E2FS_OS_LINUX	0
284d65f6f70SBen Gras #define E2FS_OS_HURD	1
285d65f6f70SBen Gras #define E2FS_OS_MASIX	2
286d65f6f70SBen Gras #define E2FS_OS_FREEBSD	3
287d65f6f70SBen Gras #define E2FS_OS_LITES	4
288d65f6f70SBen Gras 
289d65f6f70SBen Gras /*
290d65f6f70SBen Gras  * Filesystem clean flags
291d65f6f70SBen Gras  */
292d65f6f70SBen Gras #define	E2FS_ISCLEAN	0x01
293d65f6f70SBen Gras #define	E2FS_ERRORS	0x02
294d65f6f70SBen Gras 
295d65f6f70SBen Gras /* ext2 file system block group descriptor */
296d65f6f70SBen Gras 
297d65f6f70SBen Gras struct ext2_gd {
298d65f6f70SBen Gras 	uint32_t ext2bgd_b_bitmap;	/* blocks bitmap block */
299d65f6f70SBen Gras 	uint32_t ext2bgd_i_bitmap;	/* inodes bitmap block */
300d65f6f70SBen Gras 	uint32_t ext2bgd_i_tables;	/* inodes table block  */
301d65f6f70SBen Gras 	uint16_t ext2bgd_nbfree;	/* number of free blocks */
302d65f6f70SBen Gras 	uint16_t ext2bgd_nifree;	/* number of free inodes */
303d65f6f70SBen Gras 	uint16_t ext2bgd_ndirs;		/* number of directories */
304d65f6f70SBen Gras 	uint16_t reserved;
305d65f6f70SBen Gras 	uint32_t reserved2[3];
306d65f6f70SBen Gras };
307d65f6f70SBen Gras 
308d65f6f70SBen Gras 
309d65f6f70SBen Gras /*
310d65f6f70SBen Gras  * If the EXT2F_ROCOMPAT_SPARSESUPER flag is set, the cylinder group has a
311d65f6f70SBen Gras  * copy of the super and cylinder group descriptors blocks only if it's
312d65f6f70SBen Gras  * 1, a power of 3, 5 or 7
313d65f6f70SBen Gras  */
314d65f6f70SBen Gras 
315d65f6f70SBen Gras static __inline int cg_has_sb(int) __unused;
316d65f6f70SBen Gras static __inline int
cg_has_sb(int i)317d65f6f70SBen Gras cg_has_sb(int i)
318d65f6f70SBen Gras {
319d65f6f70SBen Gras 	int a3, a5, a7;
320d65f6f70SBen Gras 
321d65f6f70SBen Gras 	if (i == 0 || i == 1)
322d65f6f70SBen Gras 		return 1;
323d65f6f70SBen Gras 	for (a3 = 3, a5 = 5, a7 = 7;
324d65f6f70SBen Gras 	    a3 <= i || a5 <= i || a7 <= i;
325d65f6f70SBen Gras 	    a3 *= 3, a5 *= 5, a7 *= 7)
326d65f6f70SBen Gras 		if (i == a3 || i == a5 || i == a7)
327d65f6f70SBen Gras 			return 1;
328d65f6f70SBen Gras 	return 0;
329d65f6f70SBen Gras }
330d65f6f70SBen Gras 
331d65f6f70SBen Gras /* EXT2FS metadatas are stored in little-endian byte order. These macros
332d65f6f70SBen Gras  * helps reading theses metadatas
333d65f6f70SBen Gras  */
334d65f6f70SBen Gras 
335d65f6f70SBen Gras #if BYTE_ORDER == LITTLE_ENDIAN
336d65f6f70SBen Gras #	define h2fs16(x) (x)
337d65f6f70SBen Gras #	define h2fs32(x) (x)
338d65f6f70SBen Gras #	define h2fs64(x) (x)
339d65f6f70SBen Gras #	define fs2h16(x) (x)
340d65f6f70SBen Gras #	define fs2h32(x) (x)
341d65f6f70SBen Gras #	define fs2h64(x) (x)
342d65f6f70SBen Gras #	define e2fs_sbload(old, new) memcpy((new), (old), SBSIZE);
343d65f6f70SBen Gras #	define e2fs_cgload(old, new, size) memcpy((new), (old), (size));
344d65f6f70SBen Gras #	define e2fs_sbsave(old, new) memcpy((new), (old), SBSIZE);
345d65f6f70SBen Gras #	define e2fs_cgsave(old, new, size) memcpy((new), (old), (size));
346d65f6f70SBen Gras #else
347d65f6f70SBen Gras void e2fs_sb_bswap(struct ext2fs *, struct ext2fs *);
348d65f6f70SBen Gras void e2fs_cg_bswap(struct ext2_gd *, struct ext2_gd *, int);
349d65f6f70SBen Gras #	define h2fs16(x) bswap16(x)
350d65f6f70SBen Gras #	define h2fs32(x) bswap32(x)
351d65f6f70SBen Gras #	define h2fs64(x) bswap64(x)
352d65f6f70SBen Gras #	define fs2h16(x) bswap16(x)
353d65f6f70SBen Gras #	define fs2h32(x) bswap32(x)
354d65f6f70SBen Gras #	define fs2h64(x) bswap64(x)
355d65f6f70SBen Gras #	define e2fs_sbload(old, new) e2fs_sb_bswap((old), (new))
356d65f6f70SBen Gras #	define e2fs_cgload(old, new, size) e2fs_cg_bswap((old), (new), (size));
357d65f6f70SBen Gras #	define e2fs_sbsave(old, new) e2fs_sb_bswap((old), (new))
358d65f6f70SBen Gras #	define e2fs_cgsave(old, new, size) e2fs_cg_bswap((old), (new), (size));
359d65f6f70SBen Gras #endif
360d65f6f70SBen Gras 
361d65f6f70SBen Gras /*
362d65f6f70SBen Gras  * Turn file system block numbers into disk block addresses.
363d65f6f70SBen Gras  * This maps file system blocks to device size blocks.
364d65f6f70SBen Gras  */
365*84d9c625SLionel Sambuc #define EXT2_FSBTODB(fs, b)	((b) << (fs)->e2fs_fsbtodb)
366*84d9c625SLionel Sambuc #define EXT2_DBTOFSB(fs, b)	((b) >> (fs)->e2fs_fsbtodb)
367d65f6f70SBen Gras 
368d65f6f70SBen Gras /*
369d65f6f70SBen Gras  * Macros for handling inode numbers:
370d65f6f70SBen Gras  *	 inode number to file system block offset.
371d65f6f70SBen Gras  *	 inode number to cylinder group number.
372d65f6f70SBen Gras  *	 inode number to file system block address.
373d65f6f70SBen Gras  */
374d65f6f70SBen Gras #define	ino_to_cg(fs, x)	(((x) - 1) / (fs)->e2fs.e2fs_ipg)
375d65f6f70SBen Gras #define	ino_to_fsba(fs, x)						\
376d65f6f70SBen Gras 	((fs)->e2fs_gd[ino_to_cg((fs), (x))].ext2bgd_i_tables +		\
377d65f6f70SBen Gras 	(((x) - 1) % (fs)->e2fs.e2fs_ipg) / (fs)->e2fs_ipb)
378d65f6f70SBen Gras #define	ino_to_fsbo(fs, x)	(((x) - 1) % (fs)->e2fs_ipb)
379d65f6f70SBen Gras 
380d65f6f70SBen Gras /*
381d65f6f70SBen Gras  * Give cylinder group number for a file system block.
382d65f6f70SBen Gras  * Give cylinder group block number for a file system block.
383d65f6f70SBen Gras  */
384d65f6f70SBen Gras #define	dtog(fs, d) (((d) - (fs)->e2fs.e2fs_first_dblock) / (fs)->e2fs.e2fs_fpg)
385d65f6f70SBen Gras #define	dtogd(fs, d) \
386d65f6f70SBen Gras 	(((d) - (fs)->e2fs.e2fs_first_dblock) % (fs)->e2fs.e2fs_fpg)
387d65f6f70SBen Gras 
388d65f6f70SBen Gras /*
389d65f6f70SBen Gras  * The following macros optimize certain frequently calculated
390d65f6f70SBen Gras  * quantities by using shifts and masks in place of divisions
391d65f6f70SBen Gras  * modulos and multiplications.
392d65f6f70SBen Gras  */
393*84d9c625SLionel Sambuc #define ext2_blkoff(fs, loc)	/* calculates (loc % fs->e2fs_bsize) */ \
394d65f6f70SBen Gras 	((loc) & (fs)->e2fs_qbmask)
395*84d9c625SLionel Sambuc #define ext2_lblktosize(fs, blk) /* calculates (blk * fs->e2fs_bsize) */ \
396d65f6f70SBen Gras 	((blk) << (fs)->e2fs_bshift)
397*84d9c625SLionel Sambuc #define ext2_lblkno(fs, loc)	/* calculates (loc / fs->e2fs_bsize) */ \
398d65f6f70SBen Gras 	((loc) >> (fs)->e2fs_bshift)
399*84d9c625SLionel Sambuc #define ext2_blkroundup(fs, size) /* calculates roundup(size, fs->e2fs_bsize) */ \
400d65f6f70SBen Gras 	(((size) + (fs)->e2fs_qbmask) & (fs)->e2fs_bmask)
401*84d9c625SLionel Sambuc #define ext2_fragroundup(fs, size) /* calculates roundup(size, fs->e2fs_bsize) */ \
402d65f6f70SBen Gras 	(((size) + (fs)->e2fs_qbmask) & (fs)->e2fs_bmask)
403d65f6f70SBen Gras /*
404d65f6f70SBen Gras  * Determine the number of available frags given a
405d65f6f70SBen Gras  * percentage to hold in reserve.
406d65f6f70SBen Gras  */
407d65f6f70SBen Gras #define freespace(fs) \
408d65f6f70SBen Gras    ((fs)->e2fs.e2fs_fbcount - (fs)->e2fs.e2fs_rbcount)
409d65f6f70SBen Gras 
410d65f6f70SBen Gras /*
411d65f6f70SBen Gras  * Number of indirects in a file system block.
412d65f6f70SBen Gras  */
413*84d9c625SLionel Sambuc #define	EXT2_NINDIR(fs)	((fs)->e2fs_bsize / sizeof(uint32_t))
414d65f6f70SBen Gras 
415d65f6f70SBen Gras #endif /* !_UFS_EXT2FS_EXT2FS_H_ */
416