xref: /openbsd/sys/ufs/ext2fs/ext2fs.h (revision 898184e3)
1 /*	$OpenBSD: ext2fs.h,v 1.14 2010/02/16 08:24:13 otto Exp $	*/
2 /*	$NetBSD: ext2fs.h,v 1.10 2000/01/28 16:00:23 bouyer Exp $	*/
3 
4 /*
5  * Copyright (c) 1997 Manuel Bouyer.
6  * Copyright (c) 1982, 1986, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)fs.h	8.10 (Berkeley) 10/27/94
34  *  Modified for ext2fs by Manuel Bouyer.
35  */
36 
37 #include <machine/endian.h>
38 
39 /*
40  * Each disk drive contains some number of file systems.
41  * A file system consists of a number of cylinder groups.
42  * Each cylinder group has inodes and data.
43  *
44  * A file system is described by its super-block, which in turn
45  * describes the cylinder groups.  The super-block is critical
46  * data and is replicated in each cylinder group to protect against
47  * catastrophic loss.  This is done at `newfs' time and the critical
48  * super-block data does not change, so the copies need not be
49  * referenced further unless disaster strikes.
50  *
51  * The first boot and super blocks are given in absolute disk addresses.
52  * The byte-offset forms are preferred, as they don't imply a sector size.
53  */
54 #define BBSIZE		1024
55 #define SBSIZE		1024
56 #define	BBOFF		((off_t)(0))
57 #define	SBOFF		((off_t)(BBOFF + BBSIZE))
58 #define	BBLOCK		((daddr64_t)(0))
59 #define	SBLOCK		((daddr64_t)(BBLOCK + BBSIZE / DEV_BSIZE))
60 
61 /*
62  * Addresses stored in inodes are capable of addressing blocks
63  * XXX
64  */
65 
66 /*
67  * MINBSIZE is the smallest allowable block size.
68  * MINBSIZE must be big enough to hold a cylinder group block,
69  * thus changes to (struct cg) must keep its size within MINBSIZE.
70  * Note that super blocks are always of size SBSIZE,
71  * and that both SBSIZE and MAXBSIZE must be >= MINBSIZE.
72  */
73 #define LOG_MINBSIZE 10
74 #define MINBSIZE	(1 << LOG_MINBSIZE)
75 
76 /*
77  * The path name on which the file system is mounted is maintained
78  * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in
79  * the super block for this name.
80  */
81 #define MAXMNTLEN	512
82 
83 /*
84  * MINFREE gives the minimum acceptable percentage of file system
85  * blocks which may be free. If the freelist drops below this level
86  * only the superuser may continue to allocate blocks. This may
87  * be set to 0 if no reserve of free blocks is deemed necessary,
88  * however throughput drops by fifty percent if the file system
89  * is run at between 95% and 100% full; thus the minimum default
90  * value of fs_minfree is 5%. However, to get good clustering
91  * performance, 10% is a better choice. hence we use 10% as our
92  * default value. With 10% free space, fragmentation is not a
93  * problem, so we choose to optimize for time.
94  */
95 #define MINFREE		5
96 
97 /*
98  * Super block for an ext2fs file system.
99  */
100 struct ext2fs {
101 	u_int32_t  e2fs_icount;		/* Inode count */
102 	u_int32_t  e2fs_bcount;		/* blocks count */
103 	u_int32_t  e2fs_rbcount;	/* reserved blocks count */
104 	u_int32_t  e2fs_fbcount;	/* free blocks count */
105 	u_int32_t  e2fs_ficount;	/* free inodes count */
106 	u_int32_t  e2fs_first_dblock;	/* first data block */
107 	u_int32_t  e2fs_log_bsize;	/* block size = 1024*(2^e2fs_log_bsize) */
108 	u_int32_t  e2fs_fsize;		/* fragment size */
109 	u_int32_t  e2fs_bpg;		/* blocks per group */
110 	u_int32_t  e2fs_fpg;		/* frags per group */
111 	u_int32_t  e2fs_ipg;		/* inodes per group */
112 	u_int32_t  e2fs_mtime;		/* mount time */
113 	u_int32_t  e2fs_wtime;		/* write time */
114 	u_int16_t  e2fs_mnt_count;	/* mount count */
115 	u_int16_t  e2fs_max_mnt_count;	/* max mount count */
116 	u_int16_t  e2fs_magic;		/* magic number */
117 	u_int16_t  e2fs_state;		/* file system state */
118 	u_int16_t  e2fs_beh;		/* behavior on errors */
119 	u_int16_t  e2fs_minrev;		/* minor revision level */
120 	u_int32_t  e2fs_lastfsck;	/* time of last fsck */
121 	u_int32_t  e2fs_fsckintv;	/* max time between fscks */
122 	u_int32_t  e2fs_creator;	/* creator OS */
123 	u_int32_t  e2fs_rev;		/* revision level */
124 	u_int16_t  e2fs_ruid;		/* default uid for reserved blocks */
125 	u_int16_t  e2fs_rgid;		/* default gid for reserved blocks */
126 	/* EXT2_DYNAMIC_REV superblocks */
127 	u_int32_t  e2fs_first_ino;	/* first non-reserved inode */
128 	u_int16_t  e2fs_inode_size;	/* size of inode structure */
129 	u_int16_t  e2fs_block_group_nr;	/* block grp number of this sblk*/
130 	u_int32_t  e2fs_features_compat; /*  compatible feature set */
131 	u_int32_t  e2fs_features_incompat; /* incompatible feature set */
132 	u_int32_t  e2fs_features_rocompat; /* RO-compatible feature set */
133 	u_int8_t   e2fs_uuid[16];	/* 128-bit uuid for volume */
134 	char       e2fs_vname[16];	/* volume name */
135 	char       e2fs_fsmnt[64]; 	/* name mounted on */
136 	u_int32_t  e2fs_algo;		/* For compression */
137 	u_int8_t   e2fs_prealloc;	/* # of blocks to preallocate */
138 	u_int8_t   e2fs_dir_prealloc;	/* # of blocks to preallocate for dir */
139 	u_int16_t  e2fs_reserved_ngdb;	/* # of reserved gd blocks for resize */
140 	u_int32_t  reserved2[204];
141 };
142 
143 
144 /* in-memory data for ext2fs */
145 struct m_ext2fs {
146 	struct ext2fs e2fs;
147 	u_char	e2fs_fsmnt[MAXMNTLEN];	/* name mounted on */
148 	int8_t	e2fs_ronly;	/* mounted read-only flag */
149 	int8_t	e2fs_fmod;	/* super block modified flag */
150 	int32_t	e2fs_bsize;	/* block size */
151 	int32_t e2fs_bshift;	/* ``lblkno'' calc of logical blkno */
152 	int32_t e2fs_bmask;	/* ``blkoff'' calc of blk offsets */
153 	int64_t e2fs_qbmask;	/* ~fs_bmask - for use with quad size */
154 	int32_t	e2fs_fsbtodb;	/* fsbtodb and dbtofsb shift constant */
155 	int32_t	e2fs_ncg;	/* number of cylinder groups */
156 	int32_t	e2fs_ngdb;	/* number of group descriptor block */
157 	int32_t	e2fs_ipb;	/* number of inodes per block */
158 	int32_t	e2fs_itpg;	/* number of inode table per group */
159 	struct	ext2_gd *e2fs_gd; /* group descriptors */
160 };
161 
162 
163 
164 /*
165  * Filesystem identification
166  */
167 #define	E2FS_MAGIC	0xef53	/* the ext2fs magic number */
168 #define E2FS_REV0	0	/* revision levels */
169 #define E2FS_REV1	1	/* revision levels */
170 
171 /* compatible/imcompatible features */
172 #define EXT2F_COMPAT_PREALLOC		0x0001
173 #define EXT2F_COMPAT_HASJOURNAL		0x0004
174 #define EXT2F_COMPAT_RESIZE		0x0010
175 
176 
177 #define EXT2F_ROCOMPAT_SPARSESUPER	0x0001
178 #define EXT2F_ROCOMPAT_LARGEFILE	0x0002
179 #define EXT2F_ROCOMPAT_BTREE_DIR	0x0004
180 
181 #define EXT2F_INCOMPAT_COMP		0x0001
182 #define EXT2F_INCOMPAT_FTYPE		0x0002
183 #define EXT2F_INCOMPAT_RECOVER		0x0004
184 #define EXT2F_INCOMPAT_JOURNAL_DEV	0x0008
185 
186 /* features supported in this implementation */
187 #define EXT2F_COMPAT_SUPP		0x0000
188 #define EXT2F_ROCOMPAT_SUPP		(EXT2F_ROCOMPAT_SPARSESUPER \
189 					| EXT2F_ROCOMPAT_LARGEFILE)
190 #define EXT2F_INCOMPAT_SUPP		EXT2F_INCOMPAT_FTYPE
191 
192 /*
193  * Definitions of behavior on errors
194  */
195 #define E2FS_BEH_CONTINUE	1	/* continue operation */
196 #define E2FS_BEH_READONLY	2	/* remount fs read only */
197 #define E2FS_BEH_PANIC		3	/* cause panic */
198 #define E2FS_BEH_DEFAULT	E2FS_BEH_CONTINUE
199 
200 /*
201  * OS identification
202  */
203 #define E2FS_OS_LINUX 0
204 #define E2FS_OS_HURD  1
205 #define E2FS_OS_MASIX 2
206 
207 /*
208  * Filesystem clean flags
209  */
210 #define	E2FS_ISCLEAN	0x01
211 #define	E2FS_ERRORS	0x02
212 
213 /* ext2 file system block group descriptor */
214 
215 struct ext2_gd {
216 	u_int32_t ext2bgd_b_bitmap;	/* blocks bitmap block */
217 	u_int32_t ext2bgd_i_bitmap;	/* inodes bitmap block */
218 	u_int32_t ext2bgd_i_tables;	/* inodes table block  */
219 	u_int16_t ext2bgd_nbfree;	/* number of free blocks */
220 	u_int16_t ext2bgd_nifree;	/* number of free inodes */
221 	u_int16_t ext2bgd_ndirs;	/* number of directories */
222 	u_int16_t reserved;
223 	u_int32_t reserved2[3];
224 };
225 
226 /*
227  * If the EXT2F_ROCOMPAT_SPARSESUPER flag is set, the cylinder group has a
228  * copy of the super and cylinder group descriptors blocks only if it's
229  * a power of 3, 5 or 7
230  */
231 
232 static __inline__ int cg_has_sb(int) __attribute__((__unused__));
233 static __inline int
234 cg_has_sb(i)
235 	int i;
236 {
237 	int a3 ,a5 , a7;
238 
239 	if (i == 0 || i == 1)
240 		return 1;
241 	for (a3 = 3, a5 = 5, a7 = 7;
242 	    a3 <= i || a5 <= i || a7 <= i;
243 	    a3 *= 3, a5 *= 5, a7 *= 7)
244 		if (i == a3 || i == a5 || i == a7)
245 			return 1;
246 	return 0;
247 }
248 
249 /*
250  * EXT2FS metadatas are stored in little-endian byte order. These macros
251  * helps reading theses metadatas
252  */
253 
254 #define h2fs16(x) htole16(x)
255 #define h2fs32(x) htole32(x)
256 #define fs2h16(x) letoh16(x)
257 #define fs2h32(x) letoh32(x)
258 #if BYTE_ORDER == LITTLE_ENDIAN
259 #define e2fs_sbload(old, new) memcpy((new), (old), SBSIZE);
260 #define e2fs_cgload(old, new, size) memcpy((new), (old), (size));
261 #define e2fs_sbsave(old, new) memcpy((new), (old), SBSIZE);
262 #define e2fs_cgsave(old, new, size) memcpy((new), (old), (size));
263 #else
264 void e2fs_sb_bswap(struct ext2fs *, struct ext2fs *);
265 void e2fs_cg_bswap(struct ext2_gd *, struct ext2_gd *, int);
266 #define e2fs_sbload(old, new) e2fs_sb_bswap((old), (new))
267 #define e2fs_cgload(old, new, size) e2fs_cg_bswap((old), (new), (size));
268 #define e2fs_sbsave(old, new) e2fs_sb_bswap((old), (new))
269 #define e2fs_cgsave(old, new, size) e2fs_cg_bswap((old), (new), (size));
270 #endif
271 
272 /*
273  * Turn file system block numbers into disk block addresses.
274  * This maps file system blocks to device size blocks.
275  */
276 #define fsbtodb(fs, b)	((b) << (fs)->e2fs_fsbtodb)
277 #define dbtofsb(fs, b)	((b) >> (fs)->e2fs_fsbtodb)
278 
279 /*
280  * Macros for handling inode numbers:
281  *	 inode number to file system block offset.
282  *	 inode number to cylinder group number.
283  *	 inode number to file system block address.
284  */
285 #define	ino_to_cg(fs, x)	(((x) - 1) / (fs)->e2fs.e2fs_ipg)
286 #define	ino_to_fsba(fs, x)						\
287 	((fs)->e2fs_gd[ino_to_cg(fs, x)].ext2bgd_i_tables + \
288 	(((x)-1) % (fs)->e2fs.e2fs_ipg)/(fs)->e2fs_ipb)
289 #define	ino_to_fsbo(fs, x)	(((x)-1) % (fs)->e2fs_ipb)
290 
291 /*
292  * Give cylinder group number for a file system block.
293  * Give cylinder group block number for a file system block.
294  */
295 #define	dtog(fs, d) (((d) - (fs)->e2fs.e2fs_first_dblock) / (fs)->e2fs.e2fs_fpg)
296 #define	dtogd(fs, d) \
297 	(((d) - (fs)->e2fs.e2fs_first_dblock) % (fs)->e2fs.e2fs_fpg)
298 
299 /*
300  * The following macros optimize certain frequently calculated
301  * quantities by using shifts and masks in place of divisions
302  * modulos and multiplications.
303  */
304 #define blkoff(fs, loc)		/* calculates (loc % fs->e2fs_bsize) */ \
305 	((loc) & (fs)->e2fs_qbmask)
306 #define lblktosize(fs, blk)	/* calculates (blk * fs->e2fs_bsize) */ \
307 	((blk) << (fs)->e2fs_bshift)
308 #define lblkno(fs, loc)		/* calculates (loc / fs->e2fs_bsize) */ \
309 	((loc) >> (fs)->e2fs_bshift)
310 #define blkroundup(fs, size)	/* calculates roundup(size, fs->e2fs_bsize) */ \
311 	(((size) + (fs)->e2fs_qbmask) & (fs)->e2fs_bmask)
312 #define fragroundup(fs, size)	/* calculates roundup(size, fs->e2fs_bsize) */ \
313 	(((size) + (fs)->e2fs_qbmask) & (fs)->e2fs_bmask)
314 /*
315  * Determine the number of available frags given a
316  * percentage to hold in reserve.
317  */
318 #define freespace(fs) \
319    ((fs)->e2fs.e2fs_fbcount - (fs)->e2fs.e2fs_rbcount)
320 
321 /*
322  * Number of indirects in a file system block.
323  */
324 #define	NINDIR(fs)	((fs)->e2fs_bsize / sizeof(u_int32_t))
325