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