xref: /openbsd/sys/ufs/ffs/fs.h (revision 81fb472f)
1 /*	$OpenBSD: fs.h,v 1.45 2024/02/03 18:51:58 beck Exp $	*/
2 /*	$NetBSD: fs.h,v 1.6 1995/04/12 21:21:02 mycroft Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)fs.h	8.10 (Berkeley) 10/27/94
33  */
34 
35 /*
36  * Each disk drive contains some number of file systems.
37  * A file system consists of a number of cylinder groups.
38  * Each cylinder group has inodes and data.
39  *
40  * A file system is described by its super-block, which in turn
41  * describes the cylinder groups.  The super-block is critical
42  * data and is replicated in each cylinder group to protect against
43  * catastrophic loss.  This is done at `newfs' time and the critical
44  * super-block data does not change, so the copies need not be
45  * referenced further unless disaster strikes.
46  *
47  * For file system fs, the offsets of the various blocks of interest
48  * are given in the super block as:
49  *	[fs->fs_sblkno]		Super-block
50  *	[fs->fs_cblkno]		Cylinder group block
51  *	[fs->fs_iblkno]		Inode blocks
52  *	[fs->fs_dblkno]		Data blocks
53  * The beginning of cylinder group cg in fs, is given by
54  * the ``cgbase(fs, cg)'' macro.
55  *
56  * The first boot and super blocks are given in absolute disk addresses.
57  * The byte-offset forms are preferred, as they don't imply a sector size.
58  */
59 #define BBSIZE		8192
60 #define SBSIZE		8192
61 #define	BBOFF		((off_t)(0))
62 #define	SBOFF		((off_t)(BBOFF + BBSIZE))
63 #define	BBLOCK		((daddr_t)(0))
64 #define	SBLOCK		((daddr_t)(BBLOCK + BBSIZE / DEV_BSIZE))
65 #define	SBLOCK_UFS1	8192
66 #define	SBLOCK_UFS2	65536
67 #define	SBLOCK_PIGGY	262144
68 #define	SBLOCKSIZE	8192
69 #define	SBLOCKSEARCH \
70 	{ SBLOCK_UFS2, SBLOCK_UFS1, SBLOCK_PIGGY, -1 }
71 
72 /*
73  * Addresses stored in inodes are capable of addressing fragments
74  * of `blocks'. File system blocks of at most size MAXBSIZE can
75  * be optionally broken into 2, 4, or 8 pieces, each of which is
76  * addressable; these pieces may be DEV_BSIZE, or some multiple of
77  * a DEV_BSIZE unit.
78  *
79  * Large files consist of exclusively large data blocks.  To avoid
80  * undue wasted disk space, the last data block of a small file may be
81  * allocated as only as many fragments of a large block as are
82  * necessary.  The file system format retains only a single pointer
83  * to such a fragment, which is a piece of a single large block that
84  * has been divided.  The size of such a fragment is determinable from
85  * information in the inode, using the ``blksize(fs, ip, lbn)'' macro.
86  *
87  * The file system records space availability at the fragment level;
88  * to determine block availability, aligned fragments are examined.
89  */
90 
91 #define MAXFRAG 	8
92 
93 /*
94  * MINBSIZE is the smallest allowable block size.
95  * In order to insure that it is possible to create files of size
96  * 2^32 with only two levels of indirection, MINBSIZE is set to 4096.
97  * MINBSIZE must be big enough to hold a cylinder group block,
98  * thus changes to (struct cg) must keep its size within MINBSIZE.
99  * Note that super blocks are always of size SBSIZE,
100  * and that both SBSIZE and MAXBSIZE must be >= MINBSIZE.
101  */
102 #define MINBSIZE	4096
103 
104 /*
105  * The path name on which the file system is mounted is maintained
106  * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in
107  * the super block for this name.
108  */
109 #define MAXMNTLEN	468
110 
111 /*
112  * The volume name for this file system is kept in fs_volname.
113  * MAXVOLLEN defines the length of the buffer allocated.
114  */
115 #define MAXVOLLEN	32
116 
117 /*
118  * There is a 128-byte region in the superblock reserved for in-core
119  * pointers to summary information. Originally this included an array
120  * of pointers to blocks of struct csum; now there are just three
121  * pointers and the remaining space is padded with fs_ocsp[].
122  *
123  * NOCSPTRS determines the size of this padding. One pointer (fs_csp)
124  * is taken away to point to a contiguous array of struct csum for
125  * all cylinder groups; a second (fs_maxcluster) points to an array
126  * of cluster sizes that is computed as cylinder groups are inspected,
127  * and the third points to an array that tracks the creation of new
128  * directories.
129  */
130 #define NOCSPTRS	((128 / sizeof(void *)) - 4)
131 
132 /*
133  * A summary of contiguous blocks of various sizes is maintained
134  * in each cylinder group. Normally this is set by the initial
135  * value of fs_maxcontig. To conserve space, a maximum summary size
136  * is set by FS_MAXCONTIG.
137  */
138 #define FS_MAXCONTIG	16
139 
140 /*
141  * MINFREE gives the minimum acceptable percentage of file system
142  * blocks which may be free. If the freelist drops below this level
143  * only the superuser may continue to allocate blocks. This may
144  * be set to 0 if no reserve of free blocks is deemed necessary,
145  * however throughput drops by fifty percent if the file system
146  * is run at between 95% and 100% full; thus the minimum default
147  * value of fs_minfree is 5%. However, to get good clustering
148  * performance, 10% is a better choice. With 5% free space,
149  * fragmentation is not a problem, so we choose to optimize for time.
150  */
151 #define MINFREE		5
152 #define DEFAULTOPT	FS_OPTTIME
153 
154 /*
155  * The directory preference algorithm(dirpref) can be tuned by adjusting
156  * the following parameters which tell the system the average file size
157  * and the average number of files per directory. These defaults are well
158  * selected for typical filesystems, but may need to be tuned for odd
159  * cases like filesystems being used for squid caches or news spools.
160  */
161 #define AVFILESIZ	16384	/* expected average file size */
162 #define AFPDIR		64	/* expected number of files per directory */
163 
164 /*
165  * Size of superblock space reserved for snapshots.
166  */
167 #define FSMAXSNAP	20
168 
169 /*
170  * Per cylinder group information; summarized in blocks allocated
171  * from first cylinder group data blocks.  These blocks have to be
172  * read in from fs_csaddr (size fs_cssize) in addition to the
173  * super block.
174  */
175 struct csum {
176 	int32_t	cs_ndir;		/* number of directories */
177 	int32_t	cs_nbfree;		/* number of free blocks */
178 	int32_t	cs_nifree;		/* number of free inodes */
179 	int32_t	cs_nffree;		/* number of free frags */
180 };
181 
182 struct csum_total {
183 	int64_t cs_ndir;		/* number of directories */
184 	int64_t cs_nbfree;		/* number of free blocks */
185 	int64_t cs_nifree;		/* number of free inodes */
186 	int64_t cs_nffree;		/* number of free frags */
187 	int64_t cs_spare[4];		/* future expansion */
188 };
189 
190 /*
191  * Super block for an FFS file system.
192  */
193 struct fs {
194 	int32_t	 fs_firstfield;		/* historic file system linked list, */
195 	int32_t	 fs_unused_1;		/*     used for incore super blocks */
196 	int32_t	 fs_sblkno;		/* addr of super-block / frags */
197 	int32_t	 fs_cblkno;		/* offset of cyl-block / frags */
198 	int32_t	 fs_iblkno;		/* offset of inode-blocks / frags */
199 	int32_t	 fs_dblkno;		/* offset of first data / frags */
200 	int32_t	 fs_cgoffset;		/* cylinder group offset in cylinder */
201 	int32_t	 fs_cgmask;		/* used to calc mod fs_ntrak */
202 	int32_t	 fs_ffs1_time;		/* last time written */
203 	int32_t	 fs_ffs1_size;		/* # of blocks in fs / frags */
204 	int32_t	 fs_ffs1_dsize;		/* # of data blocks in fs */
205 	u_int32_t fs_ncg;		/* # of cylinder groups */
206 	int32_t	 fs_bsize;		/* size of basic blocks / bytes */
207 	int32_t	 fs_fsize;		/* size of frag blocks / bytes */
208 	int32_t	 fs_frag;		/* # of frags in a block in fs */
209 /* these are configuration parameters */
210 	int32_t	 fs_minfree;		/* minimum percentage of free blocks */
211 	int32_t	 fs_rotdelay;		/* # of ms for optimal next block */
212 	int32_t	 fs_rps;		/* disk revolutions per second */
213 /* these fields can be computed from the others */
214 	int32_t	 fs_bmask;		/* ``blkoff'' calc of blk offsets */
215 	int32_t	 fs_fmask;		/* ``fragoff'' calc of frag offsets */
216 	int32_t	 fs_bshift;		/* ``lblkno'' calc of logical blkno */
217 	int32_t	 fs_fshift;		/* ``numfrags'' calc # of frags */
218 /* these are configuration parameters */
219 	int32_t	 fs_maxcontig;		/* max # of contiguous blks */
220 	int32_t	 fs_maxbpg;		/* max # of blks per cyl group */
221 /* these fields can be computed from the others */
222 	int32_t	 fs_fragshift;		/* block to frag shift */
223 	int32_t	 fs_fsbtodb;		/* fsbtodb and dbtofsb shift constant */
224 	int32_t	 fs_sbsize;		/* actual size of super block */
225 	int32_t	 fs_csmask;		/* csum block offset (now unused) */
226 	int32_t	 fs_csshift;		/* csum block number (now unused) */
227 	int32_t	 fs_nindir;		/* value of NINDIR */
228 	u_int32_t fs_inopb;		/* inodes per file system block */
229 	int32_t	 fs_nspf;		/* DEV_BSIZE sectors per frag */
230 /* yet another configuration parameter */
231 	int32_t	 fs_optim;		/* optimization preference, see below */
232 /* these fields are derived from the hardware */
233 	int32_t	 fs_npsect;		/* DEV_BSIZE sectors/track + spares */
234 	int32_t	 fs_interleave;		/* DEV_BSIZE sector interleave */
235 	int32_t	 fs_trackskew;		/* sector 0 skew, per track */
236 /* fs_id takes the space of the unused fs_headswitch and fs_trkseek fields */
237 	int32_t  fs_id[2];		/* unique filesystem id */
238 /* sizes determined by number of cylinder groups and their sizes */
239 	int32_t	 fs_ffs1_csaddr;	/* blk addr of cyl grp summary area */
240 	int32_t	 fs_cssize;		/* cyl grp summary area size / bytes */
241 	int32_t	 fs_cgsize;		/* cyl grp block size / bytes */
242 /* these fields are derived from the hardware */
243 	int32_t	 fs_ntrak;		/* tracks per cylinder */
244 	int32_t	 fs_nsect;		/* DEV_BSIZE sectors per track */
245 	int32_t	 fs_spc;		/* DEV_BSIZE sectors per cylinder */
246 /* this comes from the disk driver partitioning */
247 	int32_t	 fs_ncyl;		/* cylinders in file system */
248 /* these fields can be computed from the others */
249 	int32_t	 fs_cpg;		/* cylinders per group */
250 	u_int32_t fs_ipg;		/* inodes per group */
251 	int32_t	 fs_fpg;		/* blocks per group * fs_frag */
252 /* this data must be re-computed after crashes */
253 	struct	csum fs_ffs1_cstotal;	/* cylinder summary information */
254 /* these fields are cleared at mount time */
255 	int8_t	 fs_fmod;		/* super block modified flag */
256 	int8_t	 fs_clean;		/* file system is clean flag */
257 	int8_t	 fs_ronly;		/* mounted read-only flag */
258 	int8_t	 fs_ffs1_flags;		/* see FS_ below */
259 	u_char	 fs_fsmnt[MAXMNTLEN];	/* name mounted on */
260 	u_char	 fs_volname[MAXVOLLEN];	/* volume name */
261 	u_int64_t fs_swuid;		/* system-wide uid */
262 	int32_t	 fs_pad;		/* due to alignment of fs_swuid */
263 /* these fields retain the current block allocation info */
264 	int32_t	 fs_cgrotor;		/* last cg searched */
265 	void    *fs_ocsp[NOCSPTRS];	/* padding; was list of fs_cs buffers */
266 	u_int8_t *fs_contigdirs;	/* # of contiguously allocated dirs */
267 	struct csum *fs_csp;		/* cg summary info buffer for fs_cs */
268 	int32_t	*fs_maxcluster;		/* max cluster in each cyl group */
269 	u_char	*fs_active;		/* reserved for snapshots */
270 	int32_t	 fs_cpc;		/* cyl per cycle in postbl */
271 /* this area is only allocated if fs_ffs1_flags & FS_FLAGS_UPDATED */
272 	int32_t	 fs_maxbsize;           /* maximum blocking factor permitted */
273 	int64_t	 fs_spareconf64[17];    /* old rotation block list head */
274 	int64_t	 fs_sblockloc;          /* offset of standard super block */
275 	struct	csum_total fs_cstotal;  /* cylinder summary information */
276 	int64_t	 fs_time;               /* time last written */
277 	int64_t	 fs_size;               /* number of blocks in fs */
278 	int64_t	 fs_dsize;              /* number of data blocks in fs */
279 	int64_t	 fs_csaddr;             /* blk addr of cyl grp summary area */
280 	int64_t	 fs_pendingblocks;      /* blocks in process of being freed */
281 	u_int32_t fs_pendinginodes;     /* inodes in process of being freed */
282 	u_int32_t fs_snapinum[FSMAXSNAP];/* space reserved for snapshots */
283 /* back to stuff that has been around a while */
284 	u_int32_t fs_avgfilesize;	/* expected average file size */
285 	u_int32_t fs_avgfpdir;		/* expected # of files per directory */
286 	int32_t	 fs_sparecon[26];	/* reserved for future constants */
287 	u_int32_t fs_flags;		/* see FS_ flags below */
288 	int32_t	 fs_fscktime;		/* last time fsck(8)ed */
289 	int32_t	 fs_contigsumsize;	/* size of cluster summary array */
290 	int32_t	 fs_maxsymlinklen;	/* max length of an internal symlink */
291 	int32_t	 fs_inodefmt;		/* format of on-disk inodes */
292 	u_int64_t fs_maxfilesize;	/* maximum representable file size */
293 	int64_t	 fs_qbmask;		/* ~fs_bmask - for use with quad size */
294 	int64_t	 fs_qfmask;		/* ~fs_fmask - for use with quad size */
295 	int32_t	 fs_state;		/* validate fs_clean field */
296 	int32_t	 fs_postblformat;	/* format of positional layout tables */
297 	int32_t	 fs_nrpos;		/* number of rotational positions */
298 	int32_t	 fs_postbloff;		/* (u_int16) rotation block list head */
299 	int32_t	 fs_rotbloff;		/* (u_int8) blocks for each rotation */
300 	int32_t	 fs_magic;		/* magic number */
301 	u_int8_t fs_space[1];		/* list of blocks for each rotation */
302 /* actually longer */
303 };
304 
305 /*
306  * Filesystem identification
307  */
308 #define	FS_MAGIC	0x011954	/* the fast filesystem magic number */
309 #define	FS_UFS1_MAGIC	0x011954	/* the fast filesystem magic number */
310 #define	FS_UFS2_MAGIC	0x19540119	/* UFS fast filesystem magic number */
311 #define	FS_OKAY		0x7c269d38	/* superblock checksum */
312 #define FS_42INODEFMT	-1		/* 4.2BSD inode format */
313 #define FS_44INODEFMT	2		/* 4.4BSD inode format */
314 
315 /*
316  * Filesystem clean flags
317  */
318 #define	FS_ISCLEAN	0x01
319 #define	FS_WASCLEAN	0x02
320 
321 /*
322  * Preference for optimization.
323  */
324 #define FS_OPTTIME	0	/* minimize allocation time */
325 #define FS_OPTSPACE	1	/* minimize disk fragmentation */
326 
327 /*
328  * Filesystem flags.
329  */
330 #define FS_UNCLEAN	0x01	/* filesystem not clean at mount */
331 /*
332  * The following flag is used to detect a FFS1 file system that had its flags
333  * moved to the new (FFS2) location for compatibility.
334  */
335 #define FS_FLAGS_UPDATED	0x80	/* file system has FFS2-like flags */
336 
337 /*
338  * Rotational layout table format types
339  */
340 #define FS_42POSTBLFMT		-1	/* 4.2BSD rotational table format */
341 #define FS_DYNAMICPOSTBLFMT	1	/* dynamic rotational table format */
342 /*
343  * Macros for access to superblock array structures
344  */
345 #define fs_rotbl(fs) \
346     (((fs)->fs_postblformat == FS_42POSTBLFMT) \
347     ? ((fs)->fs_space) \
348     : ((u_int8_t *)((u_int8_t *)(fs) + (fs)->fs_rotbloff)))
349 
350 /*
351  * The size of a cylinder group is calculated by CGSIZE. The maximum size
352  * is limited by the fact that cylinder groups are at most one block.
353  * Its size is derived from the size of the maps maintained in the
354  * cylinder group and the (struct cg) size.
355  */
356 #define CGSIZE(fs) \
357     /* base cg */	(sizeof(struct cg) + sizeof(int32_t) + \
358     /* blktot size */	(fs)->fs_cpg * sizeof(int32_t) + \
359     /* blks size */	(fs)->fs_cpg * (fs)->fs_nrpos * sizeof(int16_t) + \
360     /* inode map */	howmany((fs)->fs_ipg, NBBY) + \
361     /* block map */	howmany((fs)->fs_fpg, NBBY) + \
362     /* if present */	((fs)->fs_contigsumsize <= 0 ? 0 : \
363     /* cluster sum */	(fs)->fs_contigsumsize * sizeof(int32_t) + \
364     /* cluster map */	howmany(fragstoblks(fs, (fs)->fs_fpg), NBBY)))
365 
366 /*
367  * Convert cylinder group to base address of its global summary info.
368  */
369 #define fs_cs(fs, indx) fs_csp[indx]
370 
371 /*
372  * Cylinder group block for a file system.
373  */
374 #define	CG_MAGIC	0x090255
375 struct cg {
376 	int32_t	 cg_firstfield;		/* historic cyl groups linked list */
377 	int32_t	 cg_magic;		/* magic number */
378 	int32_t	 cg_time;		/* time last written */
379 	u_int32_t cg_cgx;		/* we are the cgx'th cylinder group */
380 	int16_t	 cg_ncyl;		/* number of cyl's this cg */
381 	int16_t	 cg_niblk;		/* number of inode blocks this cg */
382 	u_int32_t cg_ndblk;		/* number of data blocks this cg */
383 	struct	csum cg_cs;		/* cylinder summary information */
384 	u_int32_t cg_rotor;		/* position of last used block */
385 	u_int32_t cg_frotor;		/* position of last used frag */
386 	u_int32_t cg_irotor;		/* position of last used inode */
387 	u_int32_t cg_frsum[MAXFRAG];	/* counts of available frags */
388 	int32_t	 cg_btotoff;		/* (int32) block totals per cylinder */
389 	int32_t	 cg_boff;		/* (u_int16) free block positions */
390 	u_int32_t cg_iusedoff;		/* (u_int8) used inode map */
391 	u_int32_t cg_freeoff;		/* (u_int8) free block map */
392 	u_int32_t cg_nextfreeoff;	/* (u_int8) next available space */
393 	u_int32_t cg_clustersumoff;	/* (u_int32) counts of avail clusters */
394 	u_int32_t cg_clusteroff;	/* (u_int8) free cluster map */
395 	u_int32_t cg_nclusterblks;	/* number of clusters this cg */
396 	u_int32_t cg_ffs2_niblk;	/* number of inode blocks this cg */
397 	u_int32_t cg_initediblk;	/* last initialized inode */
398 	int32_t	 cg_sparecon32[3];	/* reserved for future use */
399 	int64_t	 cg_ffs2_time;		/* time last written */
400 	int64_t	 cg_sparecon64[3];	/* reserved for future use */
401 /* actually longer */
402 };
403 
404 /*
405  * Macros for access to cylinder group array structures
406  */
407 #define cg_blktot(cgp) \
408     (((cgp)->cg_magic != CG_MAGIC) \
409     ? (((struct ocg *)(cgp))->cg_btot) \
410     : ((int32_t *)((u_int8_t *)(cgp) + (cgp)->cg_btotoff)))
411 #define cg_blks(fs, cgp, cylno) \
412     (((cgp)->cg_magic != CG_MAGIC) \
413     ? (((struct ocg *)(cgp))->cg_b[cylno]) \
414     : ((int16_t *)((u_int8_t *)(cgp) + \
415 	(cgp)->cg_boff) + (cylno) * (fs)->fs_nrpos))
416 #define cg_inosused(cgp) \
417     (((cgp)->cg_magic != CG_MAGIC) \
418     ? (((struct ocg *)(cgp))->cg_iused) \
419     : ((u_int8_t *)((u_int8_t *)(cgp) + (cgp)->cg_iusedoff)))
420 #define cg_blksfree(cgp) \
421     (((cgp)->cg_magic != CG_MAGIC) \
422     ? (((struct ocg *)(cgp))->cg_free) \
423     : ((u_int8_t *)((u_int8_t *)(cgp) + (cgp)->cg_freeoff)))
424 #define cg_chkmagic(cgp) \
425     ((cgp)->cg_magic == CG_MAGIC || ((struct ocg *)(cgp))->cg_magic == CG_MAGIC)
426 #define cg_clustersfree(cgp) \
427     ((u_int8_t *)((u_int8_t *)(cgp) + (cgp)->cg_clusteroff))
428 #define cg_clustersum(cgp) \
429     ((int32_t *)((u_int8_t *)(cgp) + (cgp)->cg_clustersumoff))
430 
431 /*
432  * The following structure is defined
433  * for compatibility with old file systems.
434  */
435 struct ocg {
436 	int32_t	 cg_firstfield;		/* historic linked list of cyl groups */
437 	int32_t	 cg_unused_1;		/*     used for incore cyl groups */
438 	int32_t	 cg_time;		/* time last written */
439 	int32_t	 cg_cgx;		/* we are the cgx'th cylinder group */
440 	int16_t	 cg_ncyl;		/* number of cyl's this cg */
441 	int16_t	 cg_niblk;		/* number of inode blocks this cg */
442 	int32_t	 cg_ndblk;		/* number of data blocks this cg */
443 	struct	csum cg_cs;		/* cylinder summary information */
444 	int32_t	 cg_rotor;		/* position of last used block */
445 	int32_t	 cg_frotor;		/* position of last used frag */
446 	int32_t	 cg_irotor;		/* position of last used inode */
447 	int32_t	 cg_frsum[8];		/* counts of available frags */
448 	int32_t	 cg_btot[32];		/* block totals per cylinder */
449 	int16_t	 cg_b[32][8];		/* positions of free blocks */
450 	u_int8_t cg_iused[256];		/* used inode map */
451 	int32_t	 cg_magic;		/* magic number */
452 	u_int8_t cg_free[1];		/* free block map */
453 /* actually longer */
454 };
455 
456 /*
457  * Turn file system block numbers into disk block addresses.
458  * This maps file system blocks to DEV_BSIZE (a.k.a. 512-byte) size disk
459  * blocks.
460  */
461 #define fsbtodb(fs, b)	((b) << (fs)->fs_fsbtodb)
462 #define	dbtofsb(fs, b)	((b) >> (fs)->fs_fsbtodb)
463 
464 /*
465  * Cylinder group macros to locate things in cylinder groups.
466  * They calc file system addresses of cylinder group data structures.
467  */
468 #define	cgbase(fs, c)	((daddr_t)(fs)->fs_fpg * (c))
469 #define	cgdata(fs, c)	(cgdmin(fs, c) + (fs)->fs_minfree)	/* data zone */
470 #define	cgmeta(fs, c)	(cgdmin(fs, c))				/* meta data */
471 #define	cgdmin(fs, c)	(cgstart(fs, c) + (fs)->fs_dblkno)	/* 1st data */
472 #define	cgimin(fs, c)	(cgstart(fs, c) + (fs)->fs_iblkno)	/* inode blk */
473 #define	cgsblock(fs, c)	(cgstart(fs, c) + (fs)->fs_sblkno)	/* super blk */
474 #define	cgtod(fs, c)	(cgstart(fs, c) + (fs)->fs_cblkno)	/* cg block */
475 #define cgstart(fs, c)							\
476 	(cgbase(fs, c) + (fs)->fs_cgoffset * ((c) & ~((fs)->fs_cgmask)))
477 
478 /*
479  * Macros for handling inode numbers:
480  *     inode number to file system block offset.
481  *     inode number to cylinder group number.
482  *     inode number to file system block address.
483  */
484 #define	ino_to_cg(fs, x)	((x) / (fs)->fs_ipg)
485 #define	ino_to_fsba(fs, x)						\
486 	((daddr_t)(cgimin(fs, ino_to_cg(fs, x)) +			\
487 	    (blkstofrags((fs), (((x) % (fs)->fs_ipg) / INOPB(fs))))))
488 #define	ino_to_fsbo(fs, x)	((x) % INOPB(fs))
489 
490 /*
491  * Give cylinder group number for a file system block.
492  * Give frag block number in cylinder group for a file system block.
493  */
494 #define	dtog(fs, d)	((d) / (fs)->fs_fpg)
495 #define	dtogd(fs, d)	((d) % (fs)->fs_fpg)
496 
497 /*
498  * Extract the bits for a block from a map.
499  * Compute the cylinder and rotational position of a cyl block addr.
500  */
501 #define blkmap(fs, map, loc) \
502     (((map)[(loc) / NBBY] >> ((loc) % NBBY)) & (0xff >> (NBBY - (fs)->fs_frag)))
503 #define cbtocylno(fs, bno) \
504     (fsbtodb(fs, bno) / (fs)->fs_spc)
505 #define cbtorpos(fs, bno) \
506     ((fs)->fs_nrpos <= 1 ? 0 : \
507      (fsbtodb(fs, bno) % (fs)->fs_spc / (fs)->fs_nsect * (fs)->fs_trackskew + \
508      fsbtodb(fs, bno) % (fs)->fs_spc % (fs)->fs_nsect * (fs)->fs_interleave) % \
509      (fs)->fs_nsect * (fs)->fs_nrpos / (fs)->fs_npsect)
510 
511 /*
512  * The following macros optimize certain frequently calculated
513  * quantities by using shifts and masks in place of divisions
514  * modulos and multiplications.
515  */
516 #define blkoff(fs, loc)		/* calculates (loc % fs->fs_bsize) */ \
517 	((loc) & (fs)->fs_qbmask)
518 #define fragoff(fs, loc)	/* calculates (loc % fs->fs_fsize) */ \
519 	((loc) & (fs)->fs_qfmask)
520 #define lblktosize(fs, blk)	/* calculates ((off_t)blk * fs->fs_bsize) */ \
521 	((off_t)(blk) << (fs)->fs_bshift)
522 #define lblkno(fs, loc)		/* calculates (loc / fs->fs_bsize) */ \
523 	((loc) >> (fs)->fs_bshift)
524 #define numfrags(fs, loc)	/* calculates (loc / fs->fs_fsize) */ \
525 	((loc) >> (fs)->fs_fshift)
526 #define blkroundup(fs, size)	/* calculates roundup(size, fs->fs_bsize) */ \
527 	(((size) + (fs)->fs_qbmask) & (fs)->fs_bmask)
528 #define fragroundup(fs, size)	/* calculates roundup(size, fs->fs_fsize) */ \
529 	(((size) + (fs)->fs_qfmask) & (fs)->fs_fmask)
530 #define fragstoblks(fs, frags)	/* calculates (frags / fs->fs_frag) */ \
531 	((frags) >> (fs)->fs_fragshift)
532 #define blkstofrags(fs, blks)	/* calculates (blks * fs->fs_frag) */ \
533 	((blks) << (fs)->fs_fragshift)
534 #define fragnum(fs, fsb)	/* calculates (fsb % fs->fs_frag) */ \
535 	((fsb) & ((fs)->fs_frag - 1))
536 #define blknum(fs, fsb)		/* calculates rounddown(fsb, fs->fs_frag) */ \
537 	((fsb) &~ ((fs)->fs_frag - 1))
538 
539 /*
540  * Determine the number of available frags given a
541  * percentage to hold in reserve.
542  */
543 #define freespace(fs, percentreserved) \
544 	(blkstofrags((fs), (fs)->fs_cstotal.cs_nbfree) + \
545 	(fs)->fs_cstotal.cs_nffree - ((fs)->fs_dsize * (percentreserved) / 100))
546 
547 /*
548  * Determining the size of a file block in the file system.
549  */
550 #define blksize(fs, ip, lbn) \
551 	(((lbn) >= NDADDR || DIP((ip), size) >= ((lbn) + 1) << (fs)->fs_bshift) \
552 	    ? (u_int64_t)(fs)->fs_bsize \
553 	    : (fragroundup(fs, blkoff(fs, DIP((ip), size)))))
554 #define dblksize(fs, dip, lbn) \
555 	(((lbn) >= NDADDR || (dip)->di_size >= ((lbn) + 1) << (fs)->fs_bshift) \
556 	    ? (u_int64_t)(fs)->fs_bsize \
557 	    : (fragroundup(fs, blkoff(fs, (dip)->di_size))))
558 
559 #define sblksize(fs, size, lbn) \
560         (((lbn) >= NDADDR || (size) >= ((lbn) + 1) << (fs)->fs_bshift) \
561             ? (u_int64_t)(fs)->fs_bsize \
562             : (fragroundup(fs, blkoff(fs, (size)))))
563 
564 
565 /*
566  * Number of disk sectors per block/fragment; assumes DEV_BSIZE byte
567  * sector size.
568  */
569 #define	NSPB(fs)	((fs)->fs_nspf << (fs)->fs_fragshift)
570 #define	NSPF(fs)	((fs)->fs_nspf)
571 
572 /* Number of inodes per file system block (fs->fs_bsize) */
573 #define	INOPB(fs)	((fs)->fs_inopb)
574 /* Number of inodes per file system fragment (fs->fs_fsize) */
575 #define	INOPF(fs)	((fs)->fs_inopb >> (fs)->fs_fragshift)
576 
577 /*
578  * Number of indirects in a file system block.
579  */
580 #define	NINDIR(fs)	((fs)->fs_nindir)
581 
582 /* Maximum file size the kernel allows.
583  * Even though ffs can handle files up to 16TB, we do limit the max file
584  * to 2^31 pages to prevent overflow of a 32-bit unsigned int.  The buffer
585  * cache has its own checks but a little added paranoia never hurts.
586  */
587 #define FS_KERNMAXFILESIZE(pgsiz, fs)	((u_int64_t)0x80000000 * \
588     MIN((pgsiz), (fs)->fs_bsize) - 1)
589 
590 extern const int inside[], around[];
591 extern const u_char *fragtbl[];
592