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