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