xref: /titanic_52/usr/src/boot/sys/ufs/ffs/fs.h (revision 4a5d661a82b942b6538acd26209d959ce98b593a)
1*4a5d661aSToomas Soome /*-
2*4a5d661aSToomas Soome  * Copyright (c) 1982, 1986, 1993
3*4a5d661aSToomas Soome  *	The Regents of the University of California.  All rights reserved.
4*4a5d661aSToomas Soome  *
5*4a5d661aSToomas Soome  * Redistribution and use in source and binary forms, with or without
6*4a5d661aSToomas Soome  * modification, are permitted provided that the following conditions
7*4a5d661aSToomas Soome  * are met:
8*4a5d661aSToomas Soome  * 1. Redistributions of source code must retain the above copyright
9*4a5d661aSToomas Soome  *    notice, this list of conditions and the following disclaimer.
10*4a5d661aSToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
11*4a5d661aSToomas Soome  *    notice, this list of conditions and the following disclaimer in the
12*4a5d661aSToomas Soome  *    documentation and/or other materials provided with the distribution.
13*4a5d661aSToomas Soome  * 3. Neither the name of the University nor the names of its contributors
14*4a5d661aSToomas Soome  *    may be used to endorse or promote products derived from this software
15*4a5d661aSToomas Soome  *    without specific prior written permission.
16*4a5d661aSToomas Soome  *
17*4a5d661aSToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18*4a5d661aSToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*4a5d661aSToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*4a5d661aSToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21*4a5d661aSToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*4a5d661aSToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*4a5d661aSToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*4a5d661aSToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*4a5d661aSToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*4a5d661aSToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*4a5d661aSToomas Soome  * SUCH DAMAGE.
28*4a5d661aSToomas Soome  *
29*4a5d661aSToomas Soome  *	@(#)fs.h	8.13 (Berkeley) 3/21/95
30*4a5d661aSToomas Soome  * $FreeBSD$
31*4a5d661aSToomas Soome  */
32*4a5d661aSToomas Soome 
33*4a5d661aSToomas Soome #ifndef _UFS_FFS_FS_H_
34*4a5d661aSToomas Soome #define	_UFS_FFS_FS_H_
35*4a5d661aSToomas Soome 
36*4a5d661aSToomas Soome #include <sys/mount.h>
37*4a5d661aSToomas Soome #include <ufs/ufs/dinode.h>
38*4a5d661aSToomas Soome 
39*4a5d661aSToomas Soome /*
40*4a5d661aSToomas Soome  * Each disk drive contains some number of filesystems.
41*4a5d661aSToomas Soome  * A filesystem consists of a number of cylinder groups.
42*4a5d661aSToomas Soome  * Each cylinder group has inodes and data.
43*4a5d661aSToomas Soome  *
44*4a5d661aSToomas Soome  * A filesystem is described by its super-block, which in turn
45*4a5d661aSToomas Soome  * describes the cylinder groups.  The super-block is critical
46*4a5d661aSToomas Soome  * data and is replicated in each cylinder group to protect against
47*4a5d661aSToomas Soome  * catastrophic loss.  This is done at `newfs' time and the critical
48*4a5d661aSToomas Soome  * super-block data does not change, so the copies need not be
49*4a5d661aSToomas Soome  * referenced further unless disaster strikes.
50*4a5d661aSToomas Soome  *
51*4a5d661aSToomas Soome  * For filesystem fs, the offsets of the various blocks of interest
52*4a5d661aSToomas Soome  * are given in the super block as:
53*4a5d661aSToomas Soome  *	[fs->fs_sblkno]		Super-block
54*4a5d661aSToomas Soome  *	[fs->fs_cblkno]		Cylinder group block
55*4a5d661aSToomas Soome  *	[fs->fs_iblkno]		Inode blocks
56*4a5d661aSToomas Soome  *	[fs->fs_dblkno]		Data blocks
57*4a5d661aSToomas Soome  * The beginning of cylinder group cg in fs, is given by
58*4a5d661aSToomas Soome  * the ``cgbase(fs, cg)'' macro.
59*4a5d661aSToomas Soome  *
60*4a5d661aSToomas Soome  * Depending on the architecture and the media, the superblock may
61*4a5d661aSToomas Soome  * reside in any one of four places. For tiny media where every block
62*4a5d661aSToomas Soome  * counts, it is placed at the very front of the partition. Historically,
63*4a5d661aSToomas Soome  * UFS1 placed it 8K from the front to leave room for the disk label and
64*4a5d661aSToomas Soome  * a small bootstrap. For UFS2 it got moved to 64K from the front to leave
65*4a5d661aSToomas Soome  * room for the disk label and a bigger bootstrap, and for really piggy
66*4a5d661aSToomas Soome  * systems we check at 256K from the front if the first three fail. In
67*4a5d661aSToomas Soome  * all cases the size of the superblock will be SBLOCKSIZE. All values are
68*4a5d661aSToomas Soome  * given in byte-offset form, so they do not imply a sector size. The
69*4a5d661aSToomas Soome  * SBLOCKSEARCH specifies the order in which the locations should be searched.
70*4a5d661aSToomas Soome  */
71*4a5d661aSToomas Soome #define	SBLOCK_FLOPPY	     0
72*4a5d661aSToomas Soome #define	SBLOCK_UFS1	  8192
73*4a5d661aSToomas Soome #define	SBLOCK_UFS2	 65536
74*4a5d661aSToomas Soome #define	SBLOCK_PIGGY	262144
75*4a5d661aSToomas Soome #define	SBLOCKSIZE	  8192
76*4a5d661aSToomas Soome #define	SBLOCKSEARCH \
77*4a5d661aSToomas Soome 	{ SBLOCK_UFS2, SBLOCK_UFS1, SBLOCK_FLOPPY, SBLOCK_PIGGY, -1 }
78*4a5d661aSToomas Soome 
79*4a5d661aSToomas Soome /*
80*4a5d661aSToomas Soome  * Max number of fragments per block. This value is NOT tweakable.
81*4a5d661aSToomas Soome  */
82*4a5d661aSToomas Soome #define	MAXFRAG 	8
83*4a5d661aSToomas Soome 
84*4a5d661aSToomas Soome /*
85*4a5d661aSToomas Soome  * Addresses stored in inodes are capable of addressing fragments
86*4a5d661aSToomas Soome  * of `blocks'. File system blocks of at most size MAXBSIZE can
87*4a5d661aSToomas Soome  * be optionally broken into 2, 4, or 8 pieces, each of which is
88*4a5d661aSToomas Soome  * addressable; these pieces may be DEV_BSIZE, or some multiple of
89*4a5d661aSToomas Soome  * a DEV_BSIZE unit.
90*4a5d661aSToomas Soome  *
91*4a5d661aSToomas Soome  * Large files consist of exclusively large data blocks.  To avoid
92*4a5d661aSToomas Soome  * undue wasted disk space, the last data block of a small file may be
93*4a5d661aSToomas Soome  * allocated as only as many fragments of a large block as are
94*4a5d661aSToomas Soome  * necessary.  The filesystem format retains only a single pointer
95*4a5d661aSToomas Soome  * to such a fragment, which is a piece of a single large block that
96*4a5d661aSToomas Soome  * has been divided.  The size of such a fragment is determinable from
97*4a5d661aSToomas Soome  * information in the inode, using the ``blksize(fs, ip, lbn)'' macro.
98*4a5d661aSToomas Soome  *
99*4a5d661aSToomas Soome  * The filesystem records space availability at the fragment level;
100*4a5d661aSToomas Soome  * to determine block availability, aligned fragments are examined.
101*4a5d661aSToomas Soome  */
102*4a5d661aSToomas Soome 
103*4a5d661aSToomas Soome /*
104*4a5d661aSToomas Soome  * MINBSIZE is the smallest allowable block size.
105*4a5d661aSToomas Soome  * In order to insure that it is possible to create files of size
106*4a5d661aSToomas Soome  * 2^32 with only two levels of indirection, MINBSIZE is set to 4096.
107*4a5d661aSToomas Soome  * MINBSIZE must be big enough to hold a cylinder group block,
108*4a5d661aSToomas Soome  * thus changes to (struct cg) must keep its size within MINBSIZE.
109*4a5d661aSToomas Soome  * Note that super blocks are always of size SBLOCKSIZE,
110*4a5d661aSToomas Soome  * and that both SBLOCKSIZE and MAXBSIZE must be >= MINBSIZE.
111*4a5d661aSToomas Soome  */
112*4a5d661aSToomas Soome #define	MINBSIZE	4096
113*4a5d661aSToomas Soome 
114*4a5d661aSToomas Soome /*
115*4a5d661aSToomas Soome  * The path name on which the filesystem is mounted is maintained
116*4a5d661aSToomas Soome  * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in
117*4a5d661aSToomas Soome  * the super block for this name.
118*4a5d661aSToomas Soome  */
119*4a5d661aSToomas Soome #define	MAXMNTLEN	468
120*4a5d661aSToomas Soome 
121*4a5d661aSToomas Soome /*
122*4a5d661aSToomas Soome  * The volume name for this filesystem is maintained in fs_volname.
123*4a5d661aSToomas Soome  * MAXVOLLEN defines the length of the buffer allocated.
124*4a5d661aSToomas Soome  */
125*4a5d661aSToomas Soome #define	MAXVOLLEN	32
126*4a5d661aSToomas Soome 
127*4a5d661aSToomas Soome /*
128*4a5d661aSToomas Soome  * There is a 128-byte region in the superblock reserved for in-core
129*4a5d661aSToomas Soome  * pointers to summary information. Originally this included an array
130*4a5d661aSToomas Soome  * of pointers to blocks of struct csum; now there are just a few
131*4a5d661aSToomas Soome  * pointers and the remaining space is padded with fs_ocsp[].
132*4a5d661aSToomas Soome  *
133*4a5d661aSToomas Soome  * NOCSPTRS determines the size of this padding. One pointer (fs_csp)
134*4a5d661aSToomas Soome  * is taken away to point to a contiguous array of struct csum for
135*4a5d661aSToomas Soome  * all cylinder groups; a second (fs_maxcluster) points to an array
136*4a5d661aSToomas Soome  * of cluster sizes that is computed as cylinder groups are inspected,
137*4a5d661aSToomas Soome  * and the third points to an array that tracks the creation of new
138*4a5d661aSToomas Soome  * directories. A fourth pointer, fs_active, is used when creating
139*4a5d661aSToomas Soome  * snapshots; it points to a bitmap of cylinder groups for which the
140*4a5d661aSToomas Soome  * free-block bitmap has changed since the snapshot operation began.
141*4a5d661aSToomas Soome  */
142*4a5d661aSToomas Soome #define	NOCSPTRS	((128 / sizeof(void *)) - 4)
143*4a5d661aSToomas Soome 
144*4a5d661aSToomas Soome /*
145*4a5d661aSToomas Soome  * A summary of contiguous blocks of various sizes is maintained
146*4a5d661aSToomas Soome  * in each cylinder group. Normally this is set by the initial
147*4a5d661aSToomas Soome  * value of fs_maxcontig. To conserve space, a maximum summary size
148*4a5d661aSToomas Soome  * is set by FS_MAXCONTIG.
149*4a5d661aSToomas Soome  */
150*4a5d661aSToomas Soome #define	FS_MAXCONTIG	16
151*4a5d661aSToomas Soome 
152*4a5d661aSToomas Soome /*
153*4a5d661aSToomas Soome  * MINFREE gives the minimum acceptable percentage of filesystem
154*4a5d661aSToomas Soome  * blocks which may be free. If the freelist drops below this level
155*4a5d661aSToomas Soome  * only the superuser may continue to allocate blocks. This may
156*4a5d661aSToomas Soome  * be set to 0 if no reserve of free blocks is deemed necessary,
157*4a5d661aSToomas Soome  * however throughput drops by fifty percent if the filesystem
158*4a5d661aSToomas Soome  * is run at between 95% and 100% full; thus the minimum default
159*4a5d661aSToomas Soome  * value of fs_minfree is 5%. However, to get good clustering
160*4a5d661aSToomas Soome  * performance, 10% is a better choice. hence we use 10% as our
161*4a5d661aSToomas Soome  * default value. With 10% free space, fragmentation is not a
162*4a5d661aSToomas Soome  * problem, so we choose to optimize for time.
163*4a5d661aSToomas Soome  */
164*4a5d661aSToomas Soome #define	MINFREE		8
165*4a5d661aSToomas Soome #define	DEFAULTOPT	FS_OPTTIME
166*4a5d661aSToomas Soome 
167*4a5d661aSToomas Soome /*
168*4a5d661aSToomas Soome  * Grigoriy Orlov <gluk@ptci.ru> has done some extensive work to fine
169*4a5d661aSToomas Soome  * tune the layout preferences for directories within a filesystem.
170*4a5d661aSToomas Soome  * His algorithm can be tuned by adjusting the following parameters
171*4a5d661aSToomas Soome  * which tell the system the average file size and the average number
172*4a5d661aSToomas Soome  * of files per directory. These defaults are well selected for typical
173*4a5d661aSToomas Soome  * filesystems, but may need to be tuned for odd cases like filesystems
174*4a5d661aSToomas Soome  * being used for squid caches or news spools.
175*4a5d661aSToomas Soome  */
176*4a5d661aSToomas Soome #define	AVFILESIZ	16384	/* expected average file size */
177*4a5d661aSToomas Soome #define	AFPDIR		64	/* expected number of files per directory */
178*4a5d661aSToomas Soome 
179*4a5d661aSToomas Soome /*
180*4a5d661aSToomas Soome  * The maximum number of snapshot nodes that can be associated
181*4a5d661aSToomas Soome  * with each filesystem. This limit affects only the number of
182*4a5d661aSToomas Soome  * snapshot files that can be recorded within the superblock so
183*4a5d661aSToomas Soome  * that they can be found when the filesystem is mounted. However,
184*4a5d661aSToomas Soome  * maintaining too many will slow the filesystem performance, so
185*4a5d661aSToomas Soome  * having this limit is a good idea.
186*4a5d661aSToomas Soome  */
187*4a5d661aSToomas Soome #define	FSMAXSNAP 20
188*4a5d661aSToomas Soome 
189*4a5d661aSToomas Soome /*
190*4a5d661aSToomas Soome  * Used to identify special blocks in snapshots:
191*4a5d661aSToomas Soome  *
192*4a5d661aSToomas Soome  * BLK_NOCOPY - A block that was unallocated at the time the snapshot
193*4a5d661aSToomas Soome  *	was taken, hence does not need to be copied when written.
194*4a5d661aSToomas Soome  * BLK_SNAP - A block held by another snapshot that is not needed by this
195*4a5d661aSToomas Soome  *	snapshot. When the other snapshot is freed, the BLK_SNAP entries
196*4a5d661aSToomas Soome  *	are converted to BLK_NOCOPY. These are needed to allow fsck to
197*4a5d661aSToomas Soome  *	identify blocks that are in use by other snapshots (which are
198*4a5d661aSToomas Soome  *	expunged from this snapshot).
199*4a5d661aSToomas Soome  */
200*4a5d661aSToomas Soome #define	BLK_NOCOPY ((ufs2_daddr_t)(1))
201*4a5d661aSToomas Soome #define	BLK_SNAP ((ufs2_daddr_t)(2))
202*4a5d661aSToomas Soome 
203*4a5d661aSToomas Soome /*
204*4a5d661aSToomas Soome  * Sysctl values for the fast filesystem.
205*4a5d661aSToomas Soome  */
206*4a5d661aSToomas Soome #define	FFS_ADJ_REFCNT		 1	/* adjust inode reference count */
207*4a5d661aSToomas Soome #define	FFS_ADJ_BLKCNT		 2	/* adjust inode used block count */
208*4a5d661aSToomas Soome #define	FFS_BLK_FREE		 3	/* free range of blocks in map */
209*4a5d661aSToomas Soome #define	FFS_DIR_FREE		 4	/* free specified dir inodes in map */
210*4a5d661aSToomas Soome #define	FFS_FILE_FREE		 5	/* free specified file inodes in map */
211*4a5d661aSToomas Soome #define	FFS_SET_FLAGS		 6	/* set filesystem flags */
212*4a5d661aSToomas Soome #define	FFS_ADJ_NDIR		 7	/* adjust number of directories */
213*4a5d661aSToomas Soome #define	FFS_ADJ_NBFREE		 8	/* adjust number of free blocks */
214*4a5d661aSToomas Soome #define	FFS_ADJ_NIFREE		 9	/* adjust number of free inodes */
215*4a5d661aSToomas Soome #define	FFS_ADJ_NFFREE		10 	/* adjust number of free frags */
216*4a5d661aSToomas Soome #define	FFS_ADJ_NUMCLUSTERS	11	/* adjust number of free clusters */
217*4a5d661aSToomas Soome #define	FFS_SET_CWD		12	/* set current directory */
218*4a5d661aSToomas Soome #define	FFS_SET_DOTDOT		13	/* set inode number for ".." */
219*4a5d661aSToomas Soome #define	FFS_UNLINK		14	/* remove a name in the filesystem */
220*4a5d661aSToomas Soome #define	FFS_SET_INODE		15	/* update an on-disk inode */
221*4a5d661aSToomas Soome #define	FFS_SET_BUFOUTPUT	16	/* set buffered writing on descriptor */
222*4a5d661aSToomas Soome #define	FFS_MAXID		16	/* number of valid ffs ids */
223*4a5d661aSToomas Soome 
224*4a5d661aSToomas Soome /*
225*4a5d661aSToomas Soome  * Command structure passed in to the filesystem to adjust filesystem values.
226*4a5d661aSToomas Soome  */
227*4a5d661aSToomas Soome #define	FFS_CMD_VERSION		0x19790518	/* version ID */
228*4a5d661aSToomas Soome struct fsck_cmd {
229*4a5d661aSToomas Soome 	int32_t	version;	/* version of command structure */
230*4a5d661aSToomas Soome 	int32_t	handle;		/* reference to filesystem to be changed */
231*4a5d661aSToomas Soome 	int64_t	value;		/* inode or block number to be affected */
232*4a5d661aSToomas Soome 	int64_t	size;		/* amount or range to be adjusted */
233*4a5d661aSToomas Soome 	int64_t	spare;		/* reserved for future use */
234*4a5d661aSToomas Soome };
235*4a5d661aSToomas Soome 
236*4a5d661aSToomas Soome /*
237*4a5d661aSToomas Soome  * Per cylinder group information; summarized in blocks allocated
238*4a5d661aSToomas Soome  * from first cylinder group data blocks.  These blocks have to be
239*4a5d661aSToomas Soome  * read in from fs_csaddr (size fs_cssize) in addition to the
240*4a5d661aSToomas Soome  * super block.
241*4a5d661aSToomas Soome  */
242*4a5d661aSToomas Soome struct csum {
243*4a5d661aSToomas Soome 	int32_t	cs_ndir;		/* number of directories */
244*4a5d661aSToomas Soome 	int32_t	cs_nbfree;		/* number of free blocks */
245*4a5d661aSToomas Soome 	int32_t	cs_nifree;		/* number of free inodes */
246*4a5d661aSToomas Soome 	int32_t	cs_nffree;		/* number of free frags */
247*4a5d661aSToomas Soome };
248*4a5d661aSToomas Soome struct csum_total {
249*4a5d661aSToomas Soome 	int64_t	cs_ndir;		/* number of directories */
250*4a5d661aSToomas Soome 	int64_t	cs_nbfree;		/* number of free blocks */
251*4a5d661aSToomas Soome 	int64_t	cs_nifree;		/* number of free inodes */
252*4a5d661aSToomas Soome 	int64_t	cs_nffree;		/* number of free frags */
253*4a5d661aSToomas Soome 	int64_t	cs_numclusters;		/* number of free clusters */
254*4a5d661aSToomas Soome 	int64_t	cs_spare[3];		/* future expansion */
255*4a5d661aSToomas Soome };
256*4a5d661aSToomas Soome 
257*4a5d661aSToomas Soome /*
258*4a5d661aSToomas Soome  * Super block for an FFS filesystem.
259*4a5d661aSToomas Soome  */
260*4a5d661aSToomas Soome struct fs {
261*4a5d661aSToomas Soome 	int32_t	 fs_firstfield;		/* historic filesystem linked list, */
262*4a5d661aSToomas Soome 	int32_t	 fs_unused_1;		/*     used for incore super blocks */
263*4a5d661aSToomas Soome 	int32_t	 fs_sblkno;		/* offset of super-block in filesys */
264*4a5d661aSToomas Soome 	int32_t	 fs_cblkno;		/* offset of cyl-block in filesys */
265*4a5d661aSToomas Soome 	int32_t	 fs_iblkno;		/* offset of inode-blocks in filesys */
266*4a5d661aSToomas Soome 	int32_t	 fs_dblkno;		/* offset of first data after cg */
267*4a5d661aSToomas Soome 	int32_t	 fs_old_cgoffset;	/* cylinder group offset in cylinder */
268*4a5d661aSToomas Soome 	int32_t	 fs_old_cgmask;		/* used to calc mod fs_ntrak */
269*4a5d661aSToomas Soome 	int32_t  fs_old_time;		/* last time written */
270*4a5d661aSToomas Soome 	int32_t	 fs_old_size;		/* number of blocks in fs */
271*4a5d661aSToomas Soome 	int32_t	 fs_old_dsize;		/* number of data blocks in fs */
272*4a5d661aSToomas Soome 	u_int32_t fs_ncg;		/* number of cylinder groups */
273*4a5d661aSToomas Soome 	int32_t	 fs_bsize;		/* size of basic blocks in fs */
274*4a5d661aSToomas Soome 	int32_t	 fs_fsize;		/* size of frag blocks in fs */
275*4a5d661aSToomas Soome 	int32_t	 fs_frag;		/* number of frags in a block in fs */
276*4a5d661aSToomas Soome /* these are configuration parameters */
277*4a5d661aSToomas Soome 	int32_t	 fs_minfree;		/* minimum percentage of free blocks */
278*4a5d661aSToomas Soome 	int32_t	 fs_old_rotdelay;	/* num of ms for optimal next block */
279*4a5d661aSToomas Soome 	int32_t	 fs_old_rps;		/* disk revolutions per second */
280*4a5d661aSToomas Soome /* these fields can be computed from the others */
281*4a5d661aSToomas Soome 	int32_t	 fs_bmask;		/* ``blkoff'' calc of blk offsets */
282*4a5d661aSToomas Soome 	int32_t	 fs_fmask;		/* ``fragoff'' calc of frag offsets */
283*4a5d661aSToomas Soome 	int32_t	 fs_bshift;		/* ``lblkno'' calc of logical blkno */
284*4a5d661aSToomas Soome 	int32_t	 fs_fshift;		/* ``numfrags'' calc number of frags */
285*4a5d661aSToomas Soome /* these are configuration parameters */
286*4a5d661aSToomas Soome 	int32_t	 fs_maxcontig;		/* max number of contiguous blks */
287*4a5d661aSToomas Soome 	int32_t	 fs_maxbpg;		/* max number of blks per cyl group */
288*4a5d661aSToomas Soome /* these fields can be computed from the others */
289*4a5d661aSToomas Soome 	int32_t	 fs_fragshift;		/* block to frag shift */
290*4a5d661aSToomas Soome 	int32_t	 fs_fsbtodb;		/* fsbtodb and dbtofsb shift constant */
291*4a5d661aSToomas Soome 	int32_t	 fs_sbsize;		/* actual size of super block */
292*4a5d661aSToomas Soome 	int32_t	 fs_spare1[2];		/* old fs_csmask */
293*4a5d661aSToomas Soome 					/* old fs_csshift */
294*4a5d661aSToomas Soome 	int32_t	 fs_nindir;		/* value of NINDIR */
295*4a5d661aSToomas Soome 	u_int32_t fs_inopb;		/* value of INOPB */
296*4a5d661aSToomas Soome 	int32_t	 fs_old_nspf;		/* value of NSPF */
297*4a5d661aSToomas Soome /* yet another configuration parameter */
298*4a5d661aSToomas Soome 	int32_t	 fs_optim;		/* optimization preference, see below */
299*4a5d661aSToomas Soome 	int32_t	 fs_old_npsect;		/* # sectors/track including spares */
300*4a5d661aSToomas Soome 	int32_t	 fs_old_interleave;	/* hardware sector interleave */
301*4a5d661aSToomas Soome 	int32_t	 fs_old_trackskew;	/* sector 0 skew, per track */
302*4a5d661aSToomas Soome 	int32_t	 fs_id[2];		/* unique filesystem id */
303*4a5d661aSToomas Soome /* sizes determined by number of cylinder groups and their sizes */
304*4a5d661aSToomas Soome 	int32_t	 fs_old_csaddr;		/* blk addr of cyl grp summary area */
305*4a5d661aSToomas Soome 	int32_t	 fs_cssize;		/* size of cyl grp summary area */
306*4a5d661aSToomas Soome 	int32_t	 fs_cgsize;		/* cylinder group size */
307*4a5d661aSToomas Soome 	int32_t	 fs_spare2;		/* old fs_ntrak */
308*4a5d661aSToomas Soome 	int32_t	 fs_old_nsect;		/* sectors per track */
309*4a5d661aSToomas Soome 	int32_t  fs_old_spc;		/* sectors per cylinder */
310*4a5d661aSToomas Soome 	int32_t	 fs_old_ncyl;		/* cylinders in filesystem */
311*4a5d661aSToomas Soome 	int32_t	 fs_old_cpg;		/* cylinders per group */
312*4a5d661aSToomas Soome 	u_int32_t fs_ipg;		/* inodes per group */
313*4a5d661aSToomas Soome 	int32_t	 fs_fpg;		/* blocks per group * fs_frag */
314*4a5d661aSToomas Soome /* this data must be re-computed after crashes */
315*4a5d661aSToomas Soome 	struct	csum fs_old_cstotal;	/* cylinder summary information */
316*4a5d661aSToomas Soome /* these fields are cleared at mount time */
317*4a5d661aSToomas Soome 	int8_t   fs_fmod;		/* super block modified flag */
318*4a5d661aSToomas Soome 	int8_t   fs_clean;		/* filesystem is clean flag */
319*4a5d661aSToomas Soome 	int8_t 	 fs_ronly;		/* mounted read-only flag */
320*4a5d661aSToomas Soome 	int8_t   fs_old_flags;		/* old FS_ flags */
321*4a5d661aSToomas Soome 	u_char	 fs_fsmnt[MAXMNTLEN];	/* name mounted on */
322*4a5d661aSToomas Soome 	u_char	 fs_volname[MAXVOLLEN];	/* volume name */
323*4a5d661aSToomas Soome 	u_int64_t fs_swuid;		/* system-wide uid */
324*4a5d661aSToomas Soome 	int32_t  fs_pad;		/* due to alignment of fs_swuid */
325*4a5d661aSToomas Soome /* these fields retain the current block allocation info */
326*4a5d661aSToomas Soome 	int32_t	 fs_cgrotor;		/* last cg searched */
327*4a5d661aSToomas Soome 	void 	*fs_ocsp[NOCSPTRS];	/* padding; was list of fs_cs buffers */
328*4a5d661aSToomas Soome 	u_int8_t *fs_contigdirs;	/* (u) # of contig. allocated dirs */
329*4a5d661aSToomas Soome 	struct	csum *fs_csp;		/* (u) cg summary info buffer */
330*4a5d661aSToomas Soome 	int32_t	*fs_maxcluster;		/* (u) max cluster in each cyl group */
331*4a5d661aSToomas Soome 	u_int	*fs_active;		/* (u) used by snapshots to track fs */
332*4a5d661aSToomas Soome 	int32_t	 fs_old_cpc;		/* cyl per cycle in postbl */
333*4a5d661aSToomas Soome 	int32_t	 fs_maxbsize;		/* maximum blocking factor permitted */
334*4a5d661aSToomas Soome 	int64_t	 fs_unrefs;		/* number of unreferenced inodes */
335*4a5d661aSToomas Soome 	int64_t  fs_providersize;	/* size of underlying GEOM provider */
336*4a5d661aSToomas Soome 	int64_t	 fs_metaspace;		/* size of area reserved for metadata */
337*4a5d661aSToomas Soome 	int64_t	 fs_sparecon64[14];	/* old rotation block list head */
338*4a5d661aSToomas Soome 	int64_t	 fs_sblockloc;		/* byte offset of standard superblock */
339*4a5d661aSToomas Soome 	struct	csum_total fs_cstotal;	/* (u) cylinder summary information */
340*4a5d661aSToomas Soome 	ufs_time_t fs_time;		/* last time written */
341*4a5d661aSToomas Soome 	int64_t	 fs_size;		/* number of blocks in fs */
342*4a5d661aSToomas Soome 	int64_t	 fs_dsize;		/* number of data blocks in fs */
343*4a5d661aSToomas Soome 	ufs2_daddr_t fs_csaddr;		/* blk addr of cyl grp summary area */
344*4a5d661aSToomas Soome 	int64_t	 fs_pendingblocks;	/* (u) blocks being freed */
345*4a5d661aSToomas Soome 	u_int32_t fs_pendinginodes;	/* (u) inodes being freed */
346*4a5d661aSToomas Soome 	uint32_t fs_snapinum[FSMAXSNAP];/* list of snapshot inode numbers */
347*4a5d661aSToomas Soome 	u_int32_t fs_avgfilesize;	/* expected average file size */
348*4a5d661aSToomas Soome 	u_int32_t fs_avgfpdir;		/* expected # of files per directory */
349*4a5d661aSToomas Soome 	int32_t	 fs_save_cgsize;	/* save real cg size to use fs_bsize */
350*4a5d661aSToomas Soome 	ufs_time_t fs_mtime;		/* Last mount or fsck time. */
351*4a5d661aSToomas Soome 	int32_t  fs_sujfree;		/* SUJ free list */
352*4a5d661aSToomas Soome 	int32_t	 fs_sparecon32[23];	/* reserved for future constants */
353*4a5d661aSToomas Soome 	int32_t  fs_flags;		/* see FS_ flags below */
354*4a5d661aSToomas Soome 	int32_t	 fs_contigsumsize;	/* size of cluster summary array */
355*4a5d661aSToomas Soome 	int32_t	 fs_maxsymlinklen;	/* max length of an internal symlink */
356*4a5d661aSToomas Soome 	int32_t	 fs_old_inodefmt;	/* format of on-disk inodes */
357*4a5d661aSToomas Soome 	u_int64_t fs_maxfilesize;	/* maximum representable file size */
358*4a5d661aSToomas Soome 	int64_t	 fs_qbmask;		/* ~fs_bmask for use with 64-bit size */
359*4a5d661aSToomas Soome 	int64_t	 fs_qfmask;		/* ~fs_fmask for use with 64-bit size */
360*4a5d661aSToomas Soome 	int32_t	 fs_state;		/* validate fs_clean field */
361*4a5d661aSToomas Soome 	int32_t	 fs_old_postblformat;	/* format of positional layout tables */
362*4a5d661aSToomas Soome 	int32_t	 fs_old_nrpos;		/* number of rotational positions */
363*4a5d661aSToomas Soome 	int32_t	 fs_spare5[2];		/* old fs_postbloff */
364*4a5d661aSToomas Soome 					/* old fs_rotbloff */
365*4a5d661aSToomas Soome 	int32_t	 fs_magic;		/* magic number */
366*4a5d661aSToomas Soome };
367*4a5d661aSToomas Soome 
368*4a5d661aSToomas Soome /* Sanity checking. */
369*4a5d661aSToomas Soome #ifdef CTASSERT
370*4a5d661aSToomas Soome CTASSERT(sizeof(struct fs) == 1376);
371*4a5d661aSToomas Soome #endif
372*4a5d661aSToomas Soome 
373*4a5d661aSToomas Soome /*
374*4a5d661aSToomas Soome  * Filesystem identification
375*4a5d661aSToomas Soome  */
376*4a5d661aSToomas Soome #define	FS_UFS1_MAGIC	0x011954	/* UFS1 fast filesystem magic number */
377*4a5d661aSToomas Soome #define	FS_UFS2_MAGIC	0x19540119	/* UFS2 fast filesystem magic number */
378*4a5d661aSToomas Soome #define	FS_BAD_MAGIC	0x19960408	/* UFS incomplete newfs magic number */
379*4a5d661aSToomas Soome #define	FS_OKAY		0x7c269d38	/* superblock checksum */
380*4a5d661aSToomas Soome #define	FS_42INODEFMT	-1		/* 4.2BSD inode format */
381*4a5d661aSToomas Soome #define	FS_44INODEFMT	2		/* 4.4BSD inode format */
382*4a5d661aSToomas Soome 
383*4a5d661aSToomas Soome /*
384*4a5d661aSToomas Soome  * Preference for optimization.
385*4a5d661aSToomas Soome  */
386*4a5d661aSToomas Soome #define	FS_OPTTIME	0	/* minimize allocation time */
387*4a5d661aSToomas Soome #define	FS_OPTSPACE	1	/* minimize disk fragmentation */
388*4a5d661aSToomas Soome 
389*4a5d661aSToomas Soome /*
390*4a5d661aSToomas Soome  * Filesystem flags.
391*4a5d661aSToomas Soome  *
392*4a5d661aSToomas Soome  * The FS_UNCLEAN flag is set by the kernel when the filesystem was
393*4a5d661aSToomas Soome  * mounted with fs_clean set to zero. The FS_DOSOFTDEP flag indicates
394*4a5d661aSToomas Soome  * that the filesystem should be managed by the soft updates code.
395*4a5d661aSToomas Soome  * Note that the FS_NEEDSFSCK flag is set and cleared only by the
396*4a5d661aSToomas Soome  * fsck utility. It is set when background fsck finds an unexpected
397*4a5d661aSToomas Soome  * inconsistency which requires a traditional foreground fsck to be
398*4a5d661aSToomas Soome  * run. Such inconsistencies should only be found after an uncorrectable
399*4a5d661aSToomas Soome  * disk error. A foreground fsck will clear the FS_NEEDSFSCK flag when
400*4a5d661aSToomas Soome  * it has successfully cleaned up the filesystem. The kernel uses this
401*4a5d661aSToomas Soome  * flag to enforce that inconsistent filesystems be mounted read-only.
402*4a5d661aSToomas Soome  * The FS_INDEXDIRS flag when set indicates that the kernel maintains
403*4a5d661aSToomas Soome  * on-disk auxiliary indexes (such as B-trees) for speeding directory
404*4a5d661aSToomas Soome  * accesses. Kernels that do not support auxiliary indicies clear the
405*4a5d661aSToomas Soome  * flag to indicate that the indicies need to be rebuilt (by fsck) before
406*4a5d661aSToomas Soome  * they can be used.
407*4a5d661aSToomas Soome  *
408*4a5d661aSToomas Soome  * FS_ACLS indicates that POSIX.1e ACLs are administratively enabled
409*4a5d661aSToomas Soome  * for the file system, so they should be loaded from extended attributes,
410*4a5d661aSToomas Soome  * observed for access control purposes, and be administered by object
411*4a5d661aSToomas Soome  * owners.  FS_NFS4ACLS indicates that NFSv4 ACLs are administratively
412*4a5d661aSToomas Soome  * enabled.  This flag is mutually exclusive with FS_ACLS.  FS_MULTILABEL
413*4a5d661aSToomas Soome  * indicates that the TrustedBSD MAC Framework should attempt to back MAC
414*4a5d661aSToomas Soome  * labels into extended attributes on the file system rather than maintain
415*4a5d661aSToomas Soome  * a single mount label for all objects.
416*4a5d661aSToomas Soome  */
417*4a5d661aSToomas Soome #define	FS_UNCLEAN	0x0001	/* filesystem not clean at mount */
418*4a5d661aSToomas Soome #define	FS_DOSOFTDEP	0x0002	/* filesystem using soft dependencies */
419*4a5d661aSToomas Soome #define	FS_NEEDSFSCK	0x0004	/* filesystem needs sync fsck before mount */
420*4a5d661aSToomas Soome #define	FS_SUJ       	0x0008	/* Filesystem using softupdate journal */
421*4a5d661aSToomas Soome #define	FS_ACLS		0x0010	/* file system has POSIX.1e ACLs enabled */
422*4a5d661aSToomas Soome #define	FS_MULTILABEL	0x0020	/* file system is MAC multi-label */
423*4a5d661aSToomas Soome #define	FS_GJOURNAL	0x0040	/* gjournaled file system */
424*4a5d661aSToomas Soome #define	FS_FLAGS_UPDATED 0x0080	/* flags have been moved to new location */
425*4a5d661aSToomas Soome #define	FS_NFS4ACLS	0x0100	/* file system has NFSv4 ACLs enabled */
426*4a5d661aSToomas Soome #define	FS_INDEXDIRS	0x0200	/* kernel supports indexed directories */
427*4a5d661aSToomas Soome #define	FS_TRIM		0x0400	/* issue BIO_DELETE for deleted blocks */
428*4a5d661aSToomas Soome 
429*4a5d661aSToomas Soome /*
430*4a5d661aSToomas Soome  * Macros to access bits in the fs_active array.
431*4a5d661aSToomas Soome  */
432*4a5d661aSToomas Soome #define	ACTIVECGNUM(fs, cg)	((fs)->fs_active[(cg) / (NBBY * sizeof(int))])
433*4a5d661aSToomas Soome #define	ACTIVECGOFF(cg)		(1 << ((cg) % (NBBY * sizeof(int))))
434*4a5d661aSToomas Soome #define	ACTIVESET(fs, cg)	do {					\
435*4a5d661aSToomas Soome 	if ((fs)->fs_active)						\
436*4a5d661aSToomas Soome 		ACTIVECGNUM((fs), (cg)) |= ACTIVECGOFF((cg));		\
437*4a5d661aSToomas Soome } while (0)
438*4a5d661aSToomas Soome #define	ACTIVECLEAR(fs, cg)	do {					\
439*4a5d661aSToomas Soome 	if ((fs)->fs_active)						\
440*4a5d661aSToomas Soome 		ACTIVECGNUM((fs), (cg)) &= ~ACTIVECGOFF((cg));		\
441*4a5d661aSToomas Soome } while (0)
442*4a5d661aSToomas Soome 
443*4a5d661aSToomas Soome /*
444*4a5d661aSToomas Soome  * The size of a cylinder group is calculated by CGSIZE. The maximum size
445*4a5d661aSToomas Soome  * is limited by the fact that cylinder groups are at most one block.
446*4a5d661aSToomas Soome  * Its size is derived from the size of the maps maintained in the
447*4a5d661aSToomas Soome  * cylinder group and the (struct cg) size.
448*4a5d661aSToomas Soome  */
449*4a5d661aSToomas Soome #define	CGSIZE(fs) \
450*4a5d661aSToomas Soome     /* base cg */	(sizeof(struct cg) + sizeof(int32_t) + \
451*4a5d661aSToomas Soome     /* old btotoff */	(fs)->fs_old_cpg * sizeof(int32_t) + \
452*4a5d661aSToomas Soome     /* old boff */	(fs)->fs_old_cpg * sizeof(u_int16_t) + \
453*4a5d661aSToomas Soome     /* inode map */	howmany((fs)->fs_ipg, NBBY) + \
454*4a5d661aSToomas Soome     /* block map */	howmany((fs)->fs_fpg, NBBY) +\
455*4a5d661aSToomas Soome     /* if present */	((fs)->fs_contigsumsize <= 0 ? 0 : \
456*4a5d661aSToomas Soome     /* cluster sum */	(fs)->fs_contigsumsize * sizeof(int32_t) + \
457*4a5d661aSToomas Soome     /* cluster map */	howmany(fragstoblks(fs, (fs)->fs_fpg), NBBY)))
458*4a5d661aSToomas Soome 
459*4a5d661aSToomas Soome /*
460*4a5d661aSToomas Soome  * The minimal number of cylinder groups that should be created.
461*4a5d661aSToomas Soome  */
462*4a5d661aSToomas Soome #define	MINCYLGRPS	4
463*4a5d661aSToomas Soome 
464*4a5d661aSToomas Soome /*
465*4a5d661aSToomas Soome  * Convert cylinder group to base address of its global summary info.
466*4a5d661aSToomas Soome  */
467*4a5d661aSToomas Soome #define	fs_cs(fs, indx) fs_csp[indx]
468*4a5d661aSToomas Soome 
469*4a5d661aSToomas Soome /*
470*4a5d661aSToomas Soome  * Cylinder group block for a filesystem.
471*4a5d661aSToomas Soome  */
472*4a5d661aSToomas Soome #define	CG_MAGIC	0x090255
473*4a5d661aSToomas Soome struct cg {
474*4a5d661aSToomas Soome 	int32_t	 cg_firstfield;		/* historic cyl groups linked list */
475*4a5d661aSToomas Soome 	int32_t	 cg_magic;		/* magic number */
476*4a5d661aSToomas Soome 	int32_t  cg_old_time;		/* time last written */
477*4a5d661aSToomas Soome 	u_int32_t cg_cgx;		/* we are the cgx'th cylinder group */
478*4a5d661aSToomas Soome 	int16_t	 cg_old_ncyl;		/* number of cyl's this cg */
479*4a5d661aSToomas Soome 	int16_t  cg_old_niblk;		/* number of inode blocks this cg */
480*4a5d661aSToomas Soome 	u_int32_t cg_ndblk;		/* number of data blocks this cg */
481*4a5d661aSToomas Soome 	struct	 csum cg_cs;		/* cylinder summary information */
482*4a5d661aSToomas Soome 	u_int32_t cg_rotor;		/* position of last used block */
483*4a5d661aSToomas Soome 	u_int32_t cg_frotor;		/* position of last used frag */
484*4a5d661aSToomas Soome 	u_int32_t cg_irotor;		/* position of last used inode */
485*4a5d661aSToomas Soome 	u_int32_t cg_frsum[MAXFRAG];	/* counts of available frags */
486*4a5d661aSToomas Soome 	int32_t	 cg_old_btotoff;	/* (int32) block totals per cylinder */
487*4a5d661aSToomas Soome 	int32_t	 cg_old_boff;		/* (u_int16) free block positions */
488*4a5d661aSToomas Soome 	u_int32_t cg_iusedoff;		/* (u_int8) used inode map */
489*4a5d661aSToomas Soome 	u_int32_t cg_freeoff;		/* (u_int8) free block map */
490*4a5d661aSToomas Soome 	u_int32_t cg_nextfreeoff;	/* (u_int8) next available space */
491*4a5d661aSToomas Soome 	u_int32_t cg_clustersumoff;	/* (u_int32) counts of avail clusters */
492*4a5d661aSToomas Soome 	u_int32_t cg_clusteroff;		/* (u_int8) free cluster map */
493*4a5d661aSToomas Soome 	u_int32_t cg_nclusterblks;	/* number of clusters this cg */
494*4a5d661aSToomas Soome 	u_int32_t cg_niblk;		/* number of inode blocks this cg */
495*4a5d661aSToomas Soome 	u_int32_t cg_initediblk;		/* last initialized inode */
496*4a5d661aSToomas Soome 	u_int32_t cg_unrefs;		/* number of unreferenced inodes */
497*4a5d661aSToomas Soome 	int32_t	 cg_sparecon32[2];	/* reserved for future use */
498*4a5d661aSToomas Soome 	ufs_time_t cg_time;		/* time last written */
499*4a5d661aSToomas Soome 	int64_t	 cg_sparecon64[3];	/* reserved for future use */
500*4a5d661aSToomas Soome 	u_int8_t cg_space[1];		/* space for cylinder group maps */
501*4a5d661aSToomas Soome /* actually longer */
502*4a5d661aSToomas Soome };
503*4a5d661aSToomas Soome 
504*4a5d661aSToomas Soome /*
505*4a5d661aSToomas Soome  * Macros for access to cylinder group array structures
506*4a5d661aSToomas Soome  */
507*4a5d661aSToomas Soome #define	cg_chkmagic(cgp) ((cgp)->cg_magic == CG_MAGIC)
508*4a5d661aSToomas Soome #define	cg_inosused(cgp) \
509*4a5d661aSToomas Soome     ((u_int8_t *)((u_int8_t *)(cgp) + (cgp)->cg_iusedoff))
510*4a5d661aSToomas Soome #define	cg_blksfree(cgp) \
511*4a5d661aSToomas Soome     ((u_int8_t *)((u_int8_t *)(cgp) + (cgp)->cg_freeoff))
512*4a5d661aSToomas Soome #define	cg_clustersfree(cgp) \
513*4a5d661aSToomas Soome     ((u_int8_t *)((u_int8_t *)(cgp) + (cgp)->cg_clusteroff))
514*4a5d661aSToomas Soome #define	cg_clustersum(cgp) \
515*4a5d661aSToomas Soome     ((int32_t *)((uintptr_t)(cgp) + (cgp)->cg_clustersumoff))
516*4a5d661aSToomas Soome 
517*4a5d661aSToomas Soome /*
518*4a5d661aSToomas Soome  * Turn filesystem block numbers into disk block addresses.
519*4a5d661aSToomas Soome  * This maps filesystem blocks to device size blocks.
520*4a5d661aSToomas Soome  */
521*4a5d661aSToomas Soome #define	fsbtodb(fs, b)	((daddr_t)(b) << (fs)->fs_fsbtodb)
522*4a5d661aSToomas Soome #define	dbtofsb(fs, b)	((b) >> (fs)->fs_fsbtodb)
523*4a5d661aSToomas Soome 
524*4a5d661aSToomas Soome /*
525*4a5d661aSToomas Soome  * Cylinder group macros to locate things in cylinder groups.
526*4a5d661aSToomas Soome  * They calc filesystem addresses of cylinder group data structures.
527*4a5d661aSToomas Soome  */
528*4a5d661aSToomas Soome #define	cgbase(fs, c)	(((ufs2_daddr_t)(fs)->fs_fpg) * (c))
529*4a5d661aSToomas Soome #define	cgdata(fs, c)	(cgdmin(fs, c) + (fs)->fs_metaspace)	/* data zone */
530*4a5d661aSToomas Soome #define	cgmeta(fs, c)	(cgdmin(fs, c))				/* meta data */
531*4a5d661aSToomas Soome #define	cgdmin(fs, c)	(cgstart(fs, c) + (fs)->fs_dblkno)	/* 1st data */
532*4a5d661aSToomas Soome #define	cgimin(fs, c)	(cgstart(fs, c) + (fs)->fs_iblkno)	/* inode blk */
533*4a5d661aSToomas Soome #define	cgsblock(fs, c)	(cgstart(fs, c) + (fs)->fs_sblkno)	/* super blk */
534*4a5d661aSToomas Soome #define	cgtod(fs, c)	(cgstart(fs, c) + (fs)->fs_cblkno)	/* cg block */
535*4a5d661aSToomas Soome #define	cgstart(fs, c)							\
536*4a5d661aSToomas Soome        ((fs)->fs_magic == FS_UFS2_MAGIC ? cgbase(fs, c) :		\
537*4a5d661aSToomas Soome        (cgbase(fs, c) + (fs)->fs_old_cgoffset * ((c) & ~((fs)->fs_old_cgmask))))
538*4a5d661aSToomas Soome 
539*4a5d661aSToomas Soome /*
540*4a5d661aSToomas Soome  * Macros for handling inode numbers:
541*4a5d661aSToomas Soome  *     inode number to filesystem block offset.
542*4a5d661aSToomas Soome  *     inode number to cylinder group number.
543*4a5d661aSToomas Soome  *     inode number to filesystem block address.
544*4a5d661aSToomas Soome  */
545*4a5d661aSToomas Soome #define	ino_to_cg(fs, x)	(((ino_t)(x)) / (fs)->fs_ipg)
546*4a5d661aSToomas Soome #define	ino_to_fsba(fs, x)						\
547*4a5d661aSToomas Soome 	((ufs2_daddr_t)(cgimin(fs, ino_to_cg(fs, (ino_t)(x))) +		\
548*4a5d661aSToomas Soome 	    (blkstofrags((fs), ((((ino_t)(x)) % (fs)->fs_ipg) / INOPB(fs))))))
549*4a5d661aSToomas Soome #define	ino_to_fsbo(fs, x)	(((ino_t)(x)) % INOPB(fs))
550*4a5d661aSToomas Soome 
551*4a5d661aSToomas Soome /*
552*4a5d661aSToomas Soome  * Give cylinder group number for a filesystem block.
553*4a5d661aSToomas Soome  * Give cylinder group block number for a filesystem block.
554*4a5d661aSToomas Soome  */
555*4a5d661aSToomas Soome #define	dtog(fs, d)	((d) / (fs)->fs_fpg)
556*4a5d661aSToomas Soome #define	dtogd(fs, d)	((d) % (fs)->fs_fpg)
557*4a5d661aSToomas Soome 
558*4a5d661aSToomas Soome /*
559*4a5d661aSToomas Soome  * Extract the bits for a block from a map.
560*4a5d661aSToomas Soome  * Compute the cylinder and rotational position of a cyl block addr.
561*4a5d661aSToomas Soome  */
562*4a5d661aSToomas Soome #define	blkmap(fs, map, loc) \
563*4a5d661aSToomas Soome     (((map)[(loc) / NBBY] >> ((loc) % NBBY)) & (0xff >> (NBBY - (fs)->fs_frag)))
564*4a5d661aSToomas Soome 
565*4a5d661aSToomas Soome /*
566*4a5d661aSToomas Soome  * The following macros optimize certain frequently calculated
567*4a5d661aSToomas Soome  * quantities by using shifts and masks in place of divisions
568*4a5d661aSToomas Soome  * modulos and multiplications.
569*4a5d661aSToomas Soome  */
570*4a5d661aSToomas Soome #define	blkoff(fs, loc)		/* calculates (loc % fs->fs_bsize) */ \
571*4a5d661aSToomas Soome 	((loc) & ~((fs)->fs_bmask))
572*4a5d661aSToomas Soome #define	fragoff(fs, loc)	/* calculates (loc % fs->fs_fsize) */ \
573*4a5d661aSToomas Soome 	((loc) & ~((fs)->fs_fmask))
574*4a5d661aSToomas Soome #define	lfragtosize(fs, frag)	/* calculates ((off_t)frag * fs->fs_fsize) */ \
575*4a5d661aSToomas Soome 	(((off_t)(frag)) << (fs)->fs_fshift)
576*4a5d661aSToomas Soome #define	lblktosize(fs, blk)	/* calculates ((off_t)blk * fs->fs_bsize) */ \
577*4a5d661aSToomas Soome 	(((off_t)(blk)) << (fs)->fs_bshift)
578*4a5d661aSToomas Soome /* Use this only when `blk' is known to be small, e.g., < NDADDR. */
579*4a5d661aSToomas Soome #define	smalllblktosize(fs, blk)    /* calculates (blk * fs->fs_bsize) */ \
580*4a5d661aSToomas Soome 	((blk) << (fs)->fs_bshift)
581*4a5d661aSToomas Soome #define	lblkno(fs, loc)		/* calculates (loc / fs->fs_bsize) */ \
582*4a5d661aSToomas Soome 	((loc) >> (fs)->fs_bshift)
583*4a5d661aSToomas Soome #define	numfrags(fs, loc)	/* calculates (loc / fs->fs_fsize) */ \
584*4a5d661aSToomas Soome 	((loc) >> (fs)->fs_fshift)
585*4a5d661aSToomas Soome #define	blkroundup(fs, size)	/* calculates roundup(size, fs->fs_bsize) */ \
586*4a5d661aSToomas Soome 	(((size) + (fs)->fs_bsize - 1) & (fs)->fs_bmask)
587*4a5d661aSToomas Soome #define	fragroundup(fs, size)	/* calculates roundup(size, fs->fs_fsize) */ \
588*4a5d661aSToomas Soome 	(((size) + (fs)->fs_fsize - 1) & (fs)->fs_fmask)
589*4a5d661aSToomas Soome #define	fragstoblks(fs, frags)	/* calculates (frags / fs->fs_frag) */ \
590*4a5d661aSToomas Soome 	((frags) >> (fs)->fs_fragshift)
591*4a5d661aSToomas Soome #define	blkstofrags(fs, blks)	/* calculates (blks * fs->fs_frag) */ \
592*4a5d661aSToomas Soome 	((blks) << (fs)->fs_fragshift)
593*4a5d661aSToomas Soome #define	fragnum(fs, fsb)	/* calculates (fsb % fs->fs_frag) */ \
594*4a5d661aSToomas Soome 	((fsb) & ((fs)->fs_frag - 1))
595*4a5d661aSToomas Soome #define	blknum(fs, fsb)		/* calculates rounddown(fsb, fs->fs_frag) */ \
596*4a5d661aSToomas Soome 	((fsb) &~ ((fs)->fs_frag - 1))
597*4a5d661aSToomas Soome 
598*4a5d661aSToomas Soome /*
599*4a5d661aSToomas Soome  * Determine the number of available frags given a
600*4a5d661aSToomas Soome  * percentage to hold in reserve.
601*4a5d661aSToomas Soome  */
602*4a5d661aSToomas Soome #define	freespace(fs, percentreserved) \
603*4a5d661aSToomas Soome 	(blkstofrags((fs), (fs)->fs_cstotal.cs_nbfree) + \
604*4a5d661aSToomas Soome 	(fs)->fs_cstotal.cs_nffree - \
605*4a5d661aSToomas Soome 	(((off_t)((fs)->fs_dsize)) * (percentreserved) / 100))
606*4a5d661aSToomas Soome 
607*4a5d661aSToomas Soome /*
608*4a5d661aSToomas Soome  * Determining the size of a file block in the filesystem.
609*4a5d661aSToomas Soome  */
610*4a5d661aSToomas Soome #define	blksize(fs, ip, lbn) \
611*4a5d661aSToomas Soome 	(((lbn) >= NDADDR || (ip)->i_size >= smalllblktosize(fs, (lbn) + 1)) \
612*4a5d661aSToomas Soome 	    ? (fs)->fs_bsize \
613*4a5d661aSToomas Soome 	    : (fragroundup(fs, blkoff(fs, (ip)->i_size))))
614*4a5d661aSToomas Soome #define	sblksize(fs, size, lbn) \
615*4a5d661aSToomas Soome 	(((lbn) >= NDADDR || (size) >= ((lbn) + 1) << (fs)->fs_bshift) \
616*4a5d661aSToomas Soome 	  ? (fs)->fs_bsize \
617*4a5d661aSToomas Soome 	  : (fragroundup(fs, blkoff(fs, (size)))))
618*4a5d661aSToomas Soome 
619*4a5d661aSToomas Soome /*
620*4a5d661aSToomas Soome  * Number of indirects in a filesystem block.
621*4a5d661aSToomas Soome  */
622*4a5d661aSToomas Soome #define	NINDIR(fs)	((fs)->fs_nindir)
623*4a5d661aSToomas Soome 
624*4a5d661aSToomas Soome /*
625*4a5d661aSToomas Soome  * Indirect lbns are aligned on NDADDR addresses where single indirects
626*4a5d661aSToomas Soome  * are the negated address of the lowest lbn reachable, double indirects
627*4a5d661aSToomas Soome  * are this lbn - 1 and triple indirects are this lbn - 2.  This yields
628*4a5d661aSToomas Soome  * an unusual bit order to determine level.
629*4a5d661aSToomas Soome  */
630*4a5d661aSToomas Soome static inline int
631*4a5d661aSToomas Soome lbn_level(ufs_lbn_t lbn)
632*4a5d661aSToomas Soome {
633*4a5d661aSToomas Soome 	if (lbn >= 0)
634*4a5d661aSToomas Soome 		return 0;
635*4a5d661aSToomas Soome 	switch (lbn & 0x3) {
636*4a5d661aSToomas Soome 	case 0:
637*4a5d661aSToomas Soome 		return (0);
638*4a5d661aSToomas Soome 	case 1:
639*4a5d661aSToomas Soome 		break;
640*4a5d661aSToomas Soome 	case 2:
641*4a5d661aSToomas Soome 		return (2);
642*4a5d661aSToomas Soome 	case 3:
643*4a5d661aSToomas Soome 		return (1);
644*4a5d661aSToomas Soome 	default:
645*4a5d661aSToomas Soome 		break;
646*4a5d661aSToomas Soome 	}
647*4a5d661aSToomas Soome 	return (-1);
648*4a5d661aSToomas Soome }
649*4a5d661aSToomas Soome 
650*4a5d661aSToomas Soome static inline ufs_lbn_t
651*4a5d661aSToomas Soome lbn_offset(struct fs *fs, int level)
652*4a5d661aSToomas Soome {
653*4a5d661aSToomas Soome 	ufs_lbn_t res;
654*4a5d661aSToomas Soome 
655*4a5d661aSToomas Soome 	for (res = 1; level > 0; level--)
656*4a5d661aSToomas Soome 		res *= NINDIR(fs);
657*4a5d661aSToomas Soome 	return (res);
658*4a5d661aSToomas Soome }
659*4a5d661aSToomas Soome 
660*4a5d661aSToomas Soome /*
661*4a5d661aSToomas Soome  * Number of inodes in a secondary storage block/fragment.
662*4a5d661aSToomas Soome  */
663*4a5d661aSToomas Soome #define	INOPB(fs)	((fs)->fs_inopb)
664*4a5d661aSToomas Soome #define	INOPF(fs)	((fs)->fs_inopb >> (fs)->fs_fragshift)
665*4a5d661aSToomas Soome 
666*4a5d661aSToomas Soome /*
667*4a5d661aSToomas Soome  * Softdep journal record format.
668*4a5d661aSToomas Soome  */
669*4a5d661aSToomas Soome 
670*4a5d661aSToomas Soome #define	JOP_ADDREF	1	/* Add a reference to an inode. */
671*4a5d661aSToomas Soome #define	JOP_REMREF	2	/* Remove a reference from an inode. */
672*4a5d661aSToomas Soome #define	JOP_NEWBLK	3	/* Allocate a block. */
673*4a5d661aSToomas Soome #define	JOP_FREEBLK	4	/* Free a block or a tree of blocks. */
674*4a5d661aSToomas Soome #define	JOP_MVREF	5	/* Move a reference from one off to another. */
675*4a5d661aSToomas Soome #define	JOP_TRUNC	6	/* Partial truncation record. */
676*4a5d661aSToomas Soome #define	JOP_SYNC	7	/* fsync() complete record. */
677*4a5d661aSToomas Soome 
678*4a5d661aSToomas Soome #define	JREC_SIZE	32	/* Record and segment header size. */
679*4a5d661aSToomas Soome 
680*4a5d661aSToomas Soome #define	SUJ_MIN		(4 * 1024 * 1024)	/* Minimum journal size */
681*4a5d661aSToomas Soome #define	SUJ_MAX		(32 * 1024 * 1024)	/* Maximum journal size */
682*4a5d661aSToomas Soome #define	SUJ_FILE	".sujournal"		/* Journal file name */
683*4a5d661aSToomas Soome 
684*4a5d661aSToomas Soome /*
685*4a5d661aSToomas Soome  * Size of the segment record header.  There is at most one for each disk
686*4a5d661aSToomas Soome  * block in the journal.  The segment header is followed by an array of
687*4a5d661aSToomas Soome  * records.  fsck depends on the first element in each record being 'op'
688*4a5d661aSToomas Soome  * and the second being 'ino'.  Segments may span multiple disk blocks but
689*4a5d661aSToomas Soome  * the header is present on each.
690*4a5d661aSToomas Soome  */
691*4a5d661aSToomas Soome struct jsegrec {
692*4a5d661aSToomas Soome 	uint64_t	jsr_seq;	/* Our sequence number */
693*4a5d661aSToomas Soome 	uint64_t	jsr_oldest;	/* Oldest valid sequence number */
694*4a5d661aSToomas Soome 	uint16_t	jsr_cnt;	/* Count of valid records */
695*4a5d661aSToomas Soome 	uint16_t	jsr_blocks;	/* Count of device bsize blocks. */
696*4a5d661aSToomas Soome 	uint32_t	jsr_crc;	/* 32bit crc of the valid space */
697*4a5d661aSToomas Soome 	ufs_time_t	jsr_time;	/* timestamp for mount instance */
698*4a5d661aSToomas Soome };
699*4a5d661aSToomas Soome 
700*4a5d661aSToomas Soome /*
701*4a5d661aSToomas Soome  * Reference record.  Records a single link count modification.
702*4a5d661aSToomas Soome  */
703*4a5d661aSToomas Soome struct jrefrec {
704*4a5d661aSToomas Soome 	uint32_t	jr_op;
705*4a5d661aSToomas Soome 	uint32_t	jr_ino;
706*4a5d661aSToomas Soome 	uint32_t	jr_parent;
707*4a5d661aSToomas Soome 	uint16_t	jr_nlink;
708*4a5d661aSToomas Soome 	uint16_t	jr_mode;
709*4a5d661aSToomas Soome 	int64_t		jr_diroff;
710*4a5d661aSToomas Soome 	uint64_t	jr_unused;
711*4a5d661aSToomas Soome };
712*4a5d661aSToomas Soome 
713*4a5d661aSToomas Soome /*
714*4a5d661aSToomas Soome  * Move record.  Records a reference moving within a directory block.  The
715*4a5d661aSToomas Soome  * nlink is unchanged but we must search both locations.
716*4a5d661aSToomas Soome  */
717*4a5d661aSToomas Soome struct jmvrec {
718*4a5d661aSToomas Soome 	uint32_t	jm_op;
719*4a5d661aSToomas Soome 	uint32_t	jm_ino;
720*4a5d661aSToomas Soome 	uint32_t	jm_parent;
721*4a5d661aSToomas Soome 	uint16_t	jm_unused;
722*4a5d661aSToomas Soome 	int64_t		jm_oldoff;
723*4a5d661aSToomas Soome 	int64_t		jm_newoff;
724*4a5d661aSToomas Soome };
725*4a5d661aSToomas Soome 
726*4a5d661aSToomas Soome /*
727*4a5d661aSToomas Soome  * Block record.  A set of frags or tree of blocks starting at an indirect are
728*4a5d661aSToomas Soome  * freed or a set of frags are allocated.
729*4a5d661aSToomas Soome  */
730*4a5d661aSToomas Soome struct jblkrec {
731*4a5d661aSToomas Soome 	uint32_t	jb_op;
732*4a5d661aSToomas Soome 	uint32_t	jb_ino;
733*4a5d661aSToomas Soome 	ufs2_daddr_t	jb_blkno;
734*4a5d661aSToomas Soome 	ufs_lbn_t	jb_lbn;
735*4a5d661aSToomas Soome 	uint16_t	jb_frags;
736*4a5d661aSToomas Soome 	uint16_t	jb_oldfrags;
737*4a5d661aSToomas Soome 	uint32_t	jb_unused;
738*4a5d661aSToomas Soome };
739*4a5d661aSToomas Soome 
740*4a5d661aSToomas Soome /*
741*4a5d661aSToomas Soome  * Truncation record.  Records a partial truncation so that it may be
742*4a5d661aSToomas Soome  * completed at check time.  Also used for sync records.
743*4a5d661aSToomas Soome  */
744*4a5d661aSToomas Soome struct jtrncrec {
745*4a5d661aSToomas Soome 	uint32_t	jt_op;
746*4a5d661aSToomas Soome 	uint32_t	jt_ino;
747*4a5d661aSToomas Soome 	int64_t		jt_size;
748*4a5d661aSToomas Soome 	uint32_t	jt_extsize;
749*4a5d661aSToomas Soome 	uint32_t	jt_pad[3];
750*4a5d661aSToomas Soome };
751*4a5d661aSToomas Soome 
752*4a5d661aSToomas Soome union jrec {
753*4a5d661aSToomas Soome 	struct jsegrec	rec_jsegrec;
754*4a5d661aSToomas Soome 	struct jrefrec	rec_jrefrec;
755*4a5d661aSToomas Soome 	struct jmvrec	rec_jmvrec;
756*4a5d661aSToomas Soome 	struct jblkrec	rec_jblkrec;
757*4a5d661aSToomas Soome 	struct jtrncrec	rec_jtrncrec;
758*4a5d661aSToomas Soome };
759*4a5d661aSToomas Soome 
760*4a5d661aSToomas Soome #ifdef CTASSERT
761*4a5d661aSToomas Soome CTASSERT(sizeof(struct jsegrec) == JREC_SIZE);
762*4a5d661aSToomas Soome CTASSERT(sizeof(struct jrefrec) == JREC_SIZE);
763*4a5d661aSToomas Soome CTASSERT(sizeof(struct jmvrec) == JREC_SIZE);
764*4a5d661aSToomas Soome CTASSERT(sizeof(struct jblkrec) == JREC_SIZE);
765*4a5d661aSToomas Soome CTASSERT(sizeof(struct jtrncrec) == JREC_SIZE);
766*4a5d661aSToomas Soome CTASSERT(sizeof(union jrec) == JREC_SIZE);
767*4a5d661aSToomas Soome #endif
768*4a5d661aSToomas Soome 
769*4a5d661aSToomas Soome extern int inside[], around[];
770*4a5d661aSToomas Soome extern u_char *fragtbl[];
771*4a5d661aSToomas Soome 
772*4a5d661aSToomas Soome /*
773*4a5d661aSToomas Soome  * IOCTLs used for filesystem write suspension.
774*4a5d661aSToomas Soome  */
775*4a5d661aSToomas Soome #define	UFSSUSPEND	_IOW('U', 1, fsid_t)
776*4a5d661aSToomas Soome #define	UFSRESUME	_IO('U', 2)
777*4a5d661aSToomas Soome 
778*4a5d661aSToomas Soome #endif
779