xref: /freebsd/sys/ufs/ffs/ffs_subr.c (revision 5bc926af)
160727d8bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1993
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
8df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
9df8bae1dSRodney W. Grimes  * are met:
10df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
12df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
14df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1515c377c3SEd Maste  * 3. Neither the name of the University nor the names of its contributors
16df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
17df8bae1dSRodney W. Grimes  *    without specific prior written permission.
18df8bae1dSRodney W. Grimes  *
19df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
30df8bae1dSRodney W. Grimes  *
31996c772fSJohn Dyson  *	@(#)ffs_subr.c	8.5 (Berkeley) 3/21/95
32df8bae1dSRodney W. Grimes  */
33df8bae1dSRodney W. Grimes 
34f4636c59SDavid E. O'Brien #include <sys/cdefs.h>
35f4636c59SDavid E. O'Brien __FBSDID("$FreeBSD$");
36f4636c59SDavid E. O'Brien 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
38076002f2SKirk McKusick #include <sys/limits.h>
39df8bae1dSRodney W. Grimes 
40c4473420SPeter Wemm #ifndef _KERNEL
41dffce215SKirk McKusick #include <stdio.h>
42dffce215SKirk McKusick #include <string.h>
43dffce215SKirk McKusick #include <stdlib.h>
44dffce215SKirk McKusick #include <time.h>
45dffce215SKirk McKusick #include <sys/errno.h>
46996c772fSJohn Dyson #include <ufs/ufs/dinode.h>
471c85e6a3SKirk McKusick #include <ufs/ffs/fs.h>
48dffce215SKirk McKusick 
49ec888383SKirk McKusick uint32_t calculate_crc32c(uint32_t, const void *, size_t);
50ade67b50SKirk McKusick uint32_t ffs_calc_sbhash(struct fs *);
51dffce215SKirk McKusick struct malloc_type;
52dffce215SKirk McKusick #define UFS_MALLOC(size, type, flags) malloc(size)
53dffce215SKirk McKusick #define UFS_FREE(ptr, type) free(ptr)
54076002f2SKirk McKusick #define maxphys MAXPHYS
55dffce215SKirk McKusick 
56dffce215SKirk McKusick #else /* _KERNEL */
57df8bae1dSRodney W. Grimes #include <sys/systm.h>
58f89d2072SXin LI #include <sys/gsb_crc32.h>
591cd52ec3SBruce Evans #include <sys/lock.h>
601c85e6a3SKirk McKusick #include <sys/malloc.h>
611c85e6a3SKirk McKusick #include <sys/mount.h>
62df8bae1dSRodney W. Grimes #include <sys/vnode.h>
639626b608SPoul-Henning Kamp #include <sys/bio.h>
64df8bae1dSRodney W. Grimes #include <sys/buf.h>
6508637435SBruce Evans #include <sys/ucred.h>
6608637435SBruce Evans 
67df8bae1dSRodney W. Grimes #include <ufs/ufs/quota.h>
68df8bae1dSRodney W. Grimes #include <ufs/ufs/inode.h>
691c85e6a3SKirk McKusick #include <ufs/ufs/extattr.h>
701c85e6a3SKirk McKusick #include <ufs/ufs/ufsmount.h>
711c85e6a3SKirk McKusick #include <ufs/ufs/ufs_extern.h>
72996c772fSJohn Dyson #include <ufs/ffs/ffs_extern.h>
731c85e6a3SKirk McKusick #include <ufs/ffs/fs.h>
74df8bae1dSRodney W. Grimes 
75dffce215SKirk McKusick #define UFS_MALLOC(size, type, flags) malloc(size, type, flags)
76dffce215SKirk McKusick #define UFS_FREE(ptr, type) free(ptr, type)
77dffce215SKirk McKusick 
78fb14e73cSKirk McKusick #endif /* _KERNEL */
79df8bae1dSRodney W. Grimes 
80df8bae1dSRodney W. Grimes /*
818f829a5cSKirk McKusick  * Verify an inode check-hash.
828f829a5cSKirk McKusick  */
838f829a5cSKirk McKusick int
848f829a5cSKirk McKusick ffs_verify_dinode_ckhash(struct fs *fs, struct ufs2_dinode *dip)
858f829a5cSKirk McKusick {
86c8f55fc4SKirk McKusick 	uint32_t ckhash, save_ckhash;
878f829a5cSKirk McKusick 
888f829a5cSKirk McKusick 	/*
898f829a5cSKirk McKusick 	 * Return success if unallocated or we are not doing inode check-hash.
908f829a5cSKirk McKusick 	 */
918f829a5cSKirk McKusick 	if (dip->di_mode == 0 || (fs->fs_metackhash & CK_INODE) == 0)
928f829a5cSKirk McKusick 		return (0);
938f829a5cSKirk McKusick 	/*
948f829a5cSKirk McKusick 	 * Exclude di_ckhash from the crc32 calculation, e.g., always use
958f829a5cSKirk McKusick 	 * a check-hash value of zero when calculating the check-hash.
968f829a5cSKirk McKusick 	 */
978f829a5cSKirk McKusick 	save_ckhash = dip->di_ckhash;
988f829a5cSKirk McKusick 	dip->di_ckhash = 0;
99c8f55fc4SKirk McKusick 	ckhash = calculate_crc32c(~0L, (void *)dip, sizeof(*dip));
1008f829a5cSKirk McKusick 	dip->di_ckhash = save_ckhash;
101c8f55fc4SKirk McKusick 	if (save_ckhash == ckhash)
1028f829a5cSKirk McKusick 		return (0);
103c8f55fc4SKirk McKusick 	return (EINVAL);
1048f829a5cSKirk McKusick }
1058f829a5cSKirk McKusick 
1068f829a5cSKirk McKusick /*
1078f829a5cSKirk McKusick  * Update an inode check-hash.
1088f829a5cSKirk McKusick  */
1098f829a5cSKirk McKusick void
1108f829a5cSKirk McKusick ffs_update_dinode_ckhash(struct fs *fs, struct ufs2_dinode *dip)
1118f829a5cSKirk McKusick {
1128f829a5cSKirk McKusick 
1138f829a5cSKirk McKusick 	if (dip->di_mode == 0 || (fs->fs_metackhash & CK_INODE) == 0)
1148f829a5cSKirk McKusick 		return;
1158f829a5cSKirk McKusick 	/*
1168f829a5cSKirk McKusick 	 * Exclude old di_ckhash from the crc32 calculation, e.g., always use
1178f829a5cSKirk McKusick 	 * a check-hash value of zero when calculating the new check-hash.
1188f829a5cSKirk McKusick 	 */
1198f829a5cSKirk McKusick 	dip->di_ckhash = 0;
1208f829a5cSKirk McKusick 	dip->di_ckhash = calculate_crc32c(~0L, (void *)dip, sizeof(*dip));
1218f829a5cSKirk McKusick }
1228f829a5cSKirk McKusick 
1238f829a5cSKirk McKusick /*
124dffce215SKirk McKusick  * These are the low-level functions that actually read and write
125dffce215SKirk McKusick  * the superblock and its associated data.
126dffce215SKirk McKusick  */
127dffce215SKirk McKusick static off_t sblock_try[] = SBLOCKSEARCH;
128fb14e73cSKirk McKusick static int readsuper(void *, struct fs **, off_t, int, int,
129dffce215SKirk McKusick 	int (*)(void *, off_t, void **, int));
130076002f2SKirk McKusick static int validate_sblock(struct fs *, int);
131dffce215SKirk McKusick 
132dffce215SKirk McKusick /*
133dffce215SKirk McKusick  * Read a superblock from the devfd device.
134dffce215SKirk McKusick  *
135dffce215SKirk McKusick  * If an alternate superblock is specified, it is read. Otherwise the
136dffce215SKirk McKusick  * set of locations given in the SBLOCKSEARCH list is searched for a
137dffce215SKirk McKusick  * superblock. Memory is allocated for the superblock by the readfunc and
138dffce215SKirk McKusick  * is returned. If filltype is non-NULL, additional memory is allocated
139dffce215SKirk McKusick  * of type filltype and filled in with the superblock summary information.
140efbf3964SKirk McKusick  * All memory is freed when any error is returned.
141dffce215SKirk McKusick  *
142dffce215SKirk McKusick  * If a superblock is found, zero is returned. Otherwise one of the
143dffce215SKirk McKusick  * following error values is returned:
144dffce215SKirk McKusick  *     EIO: non-existent or truncated superblock.
145dffce215SKirk McKusick  *     EIO: error reading summary information.
146dffce215SKirk McKusick  *     ENOENT: no usable known superblock found.
147076002f2SKirk McKusick  *     ENOMEM: failed to allocate space for the superblock.
148dffce215SKirk McKusick  *     EINVAL: The previous newfs operation on this volume did not complete.
149dffce215SKirk McKusick  *         The administrator must complete newfs before using this volume.
150dffce215SKirk McKusick  */
151dffce215SKirk McKusick int
1524cbd996aSKirk McKusick ffs_sbget(void *devfd, struct fs **fsp, off_t altsblock,
153dffce215SKirk McKusick     struct malloc_type *filltype,
154dffce215SKirk McKusick     int (*readfunc)(void *devfd, off_t loc, void **bufp, int size))
155dffce215SKirk McKusick {
156dffce215SKirk McKusick 	struct fs *fs;
15734816cb9SKirk McKusick 	struct fs_summary_info *fs_si;
158076002f2SKirk McKusick 	int i, error;
159076002f2SKirk McKusick 	uint64_t size, blks;
160dffce215SKirk McKusick 	uint8_t *space;
161dffce215SKirk McKusick 	int32_t *lp;
162dffce215SKirk McKusick 	char *buf;
163dffce215SKirk McKusick 
164efbf3964SKirk McKusick 	fs = NULL;
16516759360SMark Johnston 	*fsp = NULL;
166fb14e73cSKirk McKusick 	if (altsblock >= 0) {
167996d40f9SKirk McKusick 		if ((error = readsuper(devfd, &fs, altsblock, 1, 0,
168efbf3964SKirk McKusick 		     readfunc)) != 0) {
169efbf3964SKirk McKusick 			if (fs != NULL)
170efbf3964SKirk McKusick 				UFS_FREE(fs, filltype);
1714cbd996aSKirk McKusick 			return (error);
172efbf3964SKirk McKusick 		}
173dffce215SKirk McKusick 	} else {
174dffce215SKirk McKusick 		for (i = 0; sblock_try[i] != -1; i++) {
175efbf3964SKirk McKusick 			if ((error = readsuper(devfd, &fs, sblock_try[i], 0,
176f2b39152SKirk McKusick 			     altsblock, readfunc)) == 0)
177dffce215SKirk McKusick 				break;
178efbf3964SKirk McKusick 			if (fs != NULL) {
179efbf3964SKirk McKusick 				UFS_FREE(fs, filltype);
180efbf3964SKirk McKusick 				fs = NULL;
181efbf3964SKirk McKusick 			}
1824cbd996aSKirk McKusick 			if (error == ENOENT)
183dffce215SKirk McKusick 				continue;
1844cbd996aSKirk McKusick 			return (error);
185dffce215SKirk McKusick 		}
186dffce215SKirk McKusick 		if (sblock_try[i] == -1)
187dffce215SKirk McKusick 			return (ENOENT);
188dffce215SKirk McKusick 	}
189dffce215SKirk McKusick 	/*
190dffce215SKirk McKusick 	 * Read in the superblock summary information.
191dffce215SKirk McKusick 	 */
192dffce215SKirk McKusick 	size = fs->fs_cssize;
193dffce215SKirk McKusick 	blks = howmany(size, fs->fs_fsize);
194dffce215SKirk McKusick 	if (fs->fs_contigsumsize > 0)
195dffce215SKirk McKusick 		size += fs->fs_ncg * sizeof(int32_t);
196dffce215SKirk McKusick 	size += fs->fs_ncg * sizeof(u_int8_t);
197076002f2SKirk McKusick 	if ((fs_si = UFS_MALLOC(sizeof(*fs_si), filltype, M_NOWAIT)) == NULL) {
19834816cb9SKirk McKusick 		UFS_FREE(fs, filltype);
199076002f2SKirk McKusick 		return (ENOMEM);
20034816cb9SKirk McKusick 	}
20134816cb9SKirk McKusick 	bzero(fs_si, sizeof(*fs_si));
20234816cb9SKirk McKusick 	fs->fs_si = fs_si;
203076002f2SKirk McKusick 	if ((space = UFS_MALLOC(size, filltype, M_NOWAIT)) == NULL) {
20434816cb9SKirk McKusick 		UFS_FREE(fs->fs_si, filltype);
205efbf3964SKirk McKusick 		UFS_FREE(fs, filltype);
206076002f2SKirk McKusick 		return (ENOMEM);
207efbf3964SKirk McKusick 	}
208dffce215SKirk McKusick 	fs->fs_csp = (struct csum *)space;
209dffce215SKirk McKusick 	for (i = 0; i < blks; i += fs->fs_frag) {
210dffce215SKirk McKusick 		size = fs->fs_bsize;
211dffce215SKirk McKusick 		if (i + fs->fs_frag > blks)
212dffce215SKirk McKusick 			size = (blks - i) * fs->fs_fsize;
21316759360SMark Johnston 		buf = NULL;
2144cbd996aSKirk McKusick 		error = (*readfunc)(devfd,
215dffce215SKirk McKusick 		    dbtob(fsbtodb(fs, fs->fs_csaddr + i)), (void **)&buf, size);
2164cbd996aSKirk McKusick 		if (error) {
217efbf3964SKirk McKusick 			if (buf != NULL)
21816759360SMark Johnston 				UFS_FREE(buf, filltype);
219dffce215SKirk McKusick 			UFS_FREE(fs->fs_csp, filltype);
22034816cb9SKirk McKusick 			UFS_FREE(fs->fs_si, filltype);
221efbf3964SKirk McKusick 			UFS_FREE(fs, filltype);
2224cbd996aSKirk McKusick 			return (error);
223dffce215SKirk McKusick 		}
224dffce215SKirk McKusick 		memcpy(space, buf, size);
225dffce215SKirk McKusick 		UFS_FREE(buf, filltype);
226dffce215SKirk McKusick 		space += size;
227dffce215SKirk McKusick 	}
228dffce215SKirk McKusick 	if (fs->fs_contigsumsize > 0) {
229dffce215SKirk McKusick 		fs->fs_maxcluster = lp = (int32_t *)space;
230dffce215SKirk McKusick 		for (i = 0; i < fs->fs_ncg; i++)
231dffce215SKirk McKusick 			*lp++ = fs->fs_contigsumsize;
232dffce215SKirk McKusick 		space = (uint8_t *)lp;
233dffce215SKirk McKusick 	}
234dffce215SKirk McKusick 	size = fs->fs_ncg * sizeof(u_int8_t);
235dffce215SKirk McKusick 	fs->fs_contigdirs = (u_int8_t *)space;
236dffce215SKirk McKusick 	bzero(fs->fs_contigdirs, size);
237efbf3964SKirk McKusick 	*fsp = fs;
238dffce215SKirk McKusick 	return (0);
239dffce215SKirk McKusick }
240dffce215SKirk McKusick 
241dffce215SKirk McKusick /*
242dffce215SKirk McKusick  * Try to read a superblock from the location specified by sblockloc.
243dffce215SKirk McKusick  * Return zero on success or an errno on failure.
244dffce215SKirk McKusick  */
245dffce215SKirk McKusick static int
246528833faSKirk McKusick readsuper(void *devfd, struct fs **fsp, off_t sblockloc, int isaltsblk,
247fb14e73cSKirk McKusick     int chkhash, int (*readfunc)(void *devfd, off_t loc, void **bufp, int size))
248dffce215SKirk McKusick {
249dffce215SKirk McKusick 	struct fs *fs;
250ec888383SKirk McKusick 	int error, res;
251ec888383SKirk McKusick 	uint32_t ckhash;
252dffce215SKirk McKusick 
253dffce215SKirk McKusick 	error = (*readfunc)(devfd, sblockloc, (void **)fsp, SBLOCKSIZE);
254dffce215SKirk McKusick 	if (error != 0)
255dffce215SKirk McKusick 		return (error);
256dffce215SKirk McKusick 	fs = *fsp;
257dffce215SKirk McKusick 	if (fs->fs_magic == FS_BAD_MAGIC)
258dffce215SKirk McKusick 		return (EINVAL);
259076002f2SKirk McKusick 	if ((error = validate_sblock(fs, isaltsblk)) != 0)
260076002f2SKirk McKusick 		return (error);
261a02bd3e3SKirk McKusick 	/*
262a02bd3e3SKirk McKusick 	 * If the filesystem has been run on a kernel without
263a02bd3e3SKirk McKusick 	 * metadata check hashes, disable them.
264a02bd3e3SKirk McKusick 	 */
265a02bd3e3SKirk McKusick 	if ((fs->fs_flags & FS_METACKHASH) == 0)
266a02bd3e3SKirk McKusick 		fs->fs_metackhash = 0;
267996d40f9SKirk McKusick 	/*
268996d40f9SKirk McKusick 	 * Clear any check-hashes that are not maintained
269996d40f9SKirk McKusick 	 * by this kernel. Also clear any unsupported flags.
270996d40f9SKirk McKusick 	 */
271996d40f9SKirk McKusick 	fs->fs_metackhash &= CK_SUPPORTED;
272996d40f9SKirk McKusick 	fs->fs_flags &= FS_SUPPORTED;
273ade67b50SKirk McKusick 	if (fs->fs_ckhash != (ckhash = ffs_calc_sbhash(fs))) {
274f2b39152SKirk McKusick 		if (chkhash == STDSB_NOMSG)
275f2b39152SKirk McKusick 			return (EINTEGRITY);
2767ef56fb0SKirk McKusick 		if (chkhash == STDSB_NOHASHFAIL_NOMSG)
277f2b39152SKirk McKusick 			return (0);
278ec888383SKirk McKusick #ifdef _KERNEL
279ec888383SKirk McKusick 		res = uprintf("Superblock check-hash failed: recorded "
280fb14e73cSKirk McKusick 		    "check-hash 0x%x != computed check-hash 0x%x%s\n",
281fb14e73cSKirk McKusick 		    fs->fs_ckhash, ckhash,
2829b8eb1c5SKirk McKusick 		    chkhash == STDSB_NOHASHFAIL ? " (Ignored)" : "");
283ec888383SKirk McKusick #else
284ec888383SKirk McKusick 		res = 0;
285ec888383SKirk McKusick #endif
286ec888383SKirk McKusick 		/*
287ec888383SKirk McKusick 		 * Print check-hash failure if no controlling terminal
288ec888383SKirk McKusick 		 * in kernel or always if in user-mode (libufs).
289ec888383SKirk McKusick 		 */
290ec888383SKirk McKusick 		if (res == 0)
291ec888383SKirk McKusick 			printf("Superblock check-hash failed: recorded "
292ec888383SKirk McKusick 			    "check-hash 0x%x != computed check-hash "
293fb14e73cSKirk McKusick 			    "0x%x%s\n", fs->fs_ckhash, ckhash,
2949b8eb1c5SKirk McKusick 			    chkhash == STDSB_NOHASHFAIL ?
2959b8eb1c5SKirk McKusick 			    " (Ignored)" : "");
296f2b39152SKirk McKusick 		if (chkhash == STDSB)
297f2b39152SKirk McKusick 			return (EINTEGRITY);
298f2b39152SKirk McKusick 		/* chkhash == STDSB_NOHASHFAIL */
299fb14e73cSKirk McKusick 		return (0);
300fb14e73cSKirk McKusick 	}
301dffce215SKirk McKusick 	/* Have to set for old filesystems that predate this field */
302dffce215SKirk McKusick 	fs->fs_sblockactualloc = sblockloc;
303efbf3964SKirk McKusick 	/* Not yet any summary information */
30434816cb9SKirk McKusick 	fs->fs_si = NULL;
305dffce215SKirk McKusick 	return (0);
306dffce215SKirk McKusick }
307dffce215SKirk McKusick 
308dffce215SKirk McKusick /*
309076002f2SKirk McKusick  * Verify the filesystem values.
310076002f2SKirk McKusick  */
311076002f2SKirk McKusick #define ILOG2(num) (fls(num) - 1)
312ce6296caSKirk McKusick #undef CHK
31350dc4c7dSKirk McKusick #define CHK(lhs, op, rhs, fmt)						\
31450dc4c7dSKirk McKusick 	if (lhs op rhs) {						\
31550dc4c7dSKirk McKusick 		printf("UFS%d superblock failed: %s (" #fmt ") %s %s ("	\
31650dc4c7dSKirk McKusick 		    #fmt ")\n", fs->fs_magic == FS_UFS1_MAGIC ? 1 : 2,	\
31750dc4c7dSKirk McKusick 		    #lhs, (intmax_t)lhs, #op, #rhs, (intmax_t)rhs);	\
31850dc4c7dSKirk McKusick 		return (ENOENT);					\
31950dc4c7dSKirk McKusick 	}
32050dc4c7dSKirk McKusick #define CHK2(lhs1, op1, rhs1, lhs2, op2, rhs2, fmt)			\
32150dc4c7dSKirk McKusick 	if (lhs1 op1 rhs1 && lhs2 op2 rhs2) {				\
32250dc4c7dSKirk McKusick 		printf("UFS%d superblock failed: %s (" #fmt ") %s %s ("	\
32350dc4c7dSKirk McKusick 		    #fmt ") && %s (" #fmt ") %s %s (" #fmt ")\n",	\
32450dc4c7dSKirk McKusick 		    fs->fs_magic == FS_UFS1_MAGIC ? 1 : 2, #lhs1, 	\
32550dc4c7dSKirk McKusick 		    (intmax_t)lhs1, #op1, #rhs1, (intmax_t)rhs1, #lhs2,	\
32650dc4c7dSKirk McKusick 		    (intmax_t)lhs2, #op2, #rhs2, (intmax_t)rhs2);	\
32750dc4c7dSKirk McKusick 		return (ENOENT);					\
32850dc4c7dSKirk McKusick 	}
329076002f2SKirk McKusick 
330076002f2SKirk McKusick static int
331076002f2SKirk McKusick validate_sblock(struct fs *fs, int isaltsblk)
332076002f2SKirk McKusick {
333f3f5368dSKirk McKusick 	u_long i, sectorsize, cgnum;
334f3f5368dSKirk McKusick 	u_int64_t maxfilesize, sizepb;
335076002f2SKirk McKusick 
336076002f2SKirk McKusick 	sectorsize = dbtob(1);
337076002f2SKirk McKusick 	if (fs->fs_magic == FS_UFS2_MAGIC) {
33850dc4c7dSKirk McKusick 		if (!isaltsblk) {
33950dc4c7dSKirk McKusick 			CHK(fs->fs_sblockloc, !=, SBLOCK_UFS2, %#jx);
34050dc4c7dSKirk McKusick 			CHK2(fs->fs_sblockactualloc, !=, SBLOCK_UFS2,
34150dc4c7dSKirk McKusick 			    fs->fs_sblockactualloc, !=, 0, %jd);
34250dc4c7dSKirk McKusick 		}
34350dc4c7dSKirk McKusick 		CHK(fs->fs_maxsymlinklen, !=, ((UFS_NDADDR + UFS_NIADDR) *
34450dc4c7dSKirk McKusick 			sizeof(ufs2_daddr_t)), %jd);
34550dc4c7dSKirk McKusick 		CHK(fs->fs_nindir, !=, fs->fs_bsize / sizeof(ufs2_daddr_t),
34650dc4c7dSKirk McKusick 		    %jd);
34750dc4c7dSKirk McKusick 		CHK(fs->fs_inopb, !=, fs->fs_bsize / sizeof(struct ufs2_dinode),
34850dc4c7dSKirk McKusick 		    %jd);
349076002f2SKirk McKusick 	} else if (fs->fs_magic == FS_UFS1_MAGIC) {
35050dc4c7dSKirk McKusick 		if (!isaltsblk) {
3519e1f44d0SKirk McKusick 			CHK(fs->fs_sblockloc, >, SBLOCK_UFS1, %jd);
3529e1f44d0SKirk McKusick 			/*
3539e1f44d0SKirk McKusick 			 * For UFS1 the with a 65536 block size, the first
3549e1f44d0SKirk McKusick 			 * backup superblock is at the same location as the
3559e1f44d0SKirk McKusick 			 * UFS2 superblock. Since SBLOCK_UFS2 is the first
3569e1f44d0SKirk McKusick 			 * location checked, the first backup is the
3579e1f44d0SKirk McKusick 			 * superblock that will be accessed.
3589e1f44d0SKirk McKusick 			 */
3599e1f44d0SKirk McKusick 			if (fs->fs_bsize == SBLOCK_UFS2) {
3609e1f44d0SKirk McKusick 				CHK(fs->fs_sblockactualloc, >, SBLOCK_UFS2,
3619e1f44d0SKirk McKusick 				    %jd);
3629e1f44d0SKirk McKusick 			} else {
36350dc4c7dSKirk McKusick 				CHK2(fs->fs_sblockactualloc, !=, SBLOCK_UFS1,
36450dc4c7dSKirk McKusick 				    fs->fs_sblockactualloc, !=, 0, %jd);
36550dc4c7dSKirk McKusick 			}
3669e1f44d0SKirk McKusick 		}
36750dc4c7dSKirk McKusick 		CHK(fs->fs_nindir, !=, fs->fs_bsize / sizeof(ufs1_daddr_t),
36850dc4c7dSKirk McKusick 		    %jd);
36950dc4c7dSKirk McKusick 		CHK(fs->fs_inopb, !=, fs->fs_bsize / sizeof(struct ufs1_dinode),
37050dc4c7dSKirk McKusick 		    %jd);
37150dc4c7dSKirk McKusick 		CHK(fs->fs_maxsymlinklen, !=, ((UFS_NDADDR + UFS_NIADDR) *
37250dc4c7dSKirk McKusick 			sizeof(ufs1_daddr_t)), %jd);
37350dc4c7dSKirk McKusick 		CHK(fs->fs_old_inodefmt, !=, FS_44INODEFMT, %jd);
37450dc4c7dSKirk McKusick 		CHK(fs->fs_old_cgoffset, !=, 0, %jd);
37550dc4c7dSKirk McKusick 		CHK(fs->fs_old_cgmask, !=, 0xffffffff, %#jx);
37650dc4c7dSKirk McKusick 		CHK(fs->fs_old_rotdelay, !=, 0, %jd);
37750dc4c7dSKirk McKusick 		CHK(fs->fs_old_rps, !=, 60, %jd);
37850dc4c7dSKirk McKusick 		CHK(fs->fs_old_nspf, !=, fs->fs_fsize / sectorsize, %jd);
37950dc4c7dSKirk McKusick 		CHK(fs->fs_old_cpg, !=, 1, %jd);
38050dc4c7dSKirk McKusick 		CHK(fs->fs_old_interleave, !=, 1, %jd);
38150dc4c7dSKirk McKusick 		CHK(fs->fs_old_trackskew, !=, 0, %jd);
38250dc4c7dSKirk McKusick 		CHK(fs->fs_old_cpc, !=, 0, %jd);
38350dc4c7dSKirk McKusick 		CHK(fs->fs_old_postblformat, !=, 1, %jd);
38450dc4c7dSKirk McKusick 		CHK(fs->fs_old_nrpos, !=, 1, %jd);
38550dc4c7dSKirk McKusick 		CHK(fs->fs_old_spc, !=, fs->fs_fpg * fs->fs_old_nspf, %jd);
38650dc4c7dSKirk McKusick 		CHK(fs->fs_old_nsect, !=, fs->fs_old_spc, %jd);
38750dc4c7dSKirk McKusick 		CHK(fs->fs_old_npsect, !=, fs->fs_old_spc, %jd);
38850dc4c7dSKirk McKusick 		CHK(fs->fs_old_ncyl, !=, fs->fs_ncg, %jd);
389076002f2SKirk McKusick 	} else {
39050dc4c7dSKirk McKusick 		/* Bad magic number, so assume not a superblock */
391076002f2SKirk McKusick 		return (ENOENT);
392076002f2SKirk McKusick 	}
39350dc4c7dSKirk McKusick 	CHK(fs->fs_bsize, <, MINBSIZE, %jd);
39450dc4c7dSKirk McKusick 	CHK(fs->fs_bsize, >, MAXBSIZE, %jd);
39550dc4c7dSKirk McKusick 	CHK(fs->fs_bsize, <, roundup(sizeof(struct fs), DEV_BSIZE), %jd);
39650dc4c7dSKirk McKusick 	CHK(fs->fs_sbsize, >, SBLOCKSIZE, %jd);
39750dc4c7dSKirk McKusick 	CHK(powerof2(fs->fs_bsize), ==, 0, %jd);
39850dc4c7dSKirk McKusick 	CHK(fs->fs_fsize, <, sectorsize, %jd);
39950dc4c7dSKirk McKusick 	CHK(fs->fs_fsize, >, fs->fs_bsize, %jd);
40050dc4c7dSKirk McKusick 	CHK(fs->fs_fsize * MAXFRAG, <, fs->fs_bsize, %jd);
40150dc4c7dSKirk McKusick 	CHK(powerof2(fs->fs_fsize), ==, 0, %jd);
40250dc4c7dSKirk McKusick 	CHK(fs->fs_maxbsize, <, fs->fs_bsize, %jd);
40350dc4c7dSKirk McKusick 	CHK(powerof2(fs->fs_maxbsize), ==, 0, %jd);
40450dc4c7dSKirk McKusick 	CHK(fs->fs_maxbsize, >, FS_MAXCONTIG * fs->fs_bsize, %jd);
40550dc4c7dSKirk McKusick 	CHK(fs->fs_bmask, !=, ~(fs->fs_bsize - 1), %#jx);
40650dc4c7dSKirk McKusick 	CHK(fs->fs_fmask, !=, ~(fs->fs_fsize - 1), %#jx);
40750dc4c7dSKirk McKusick 	CHK(fs->fs_qbmask, !=, ~fs->fs_bmask, %#jx);
40850dc4c7dSKirk McKusick 	CHK(fs->fs_qfmask, !=, ~fs->fs_fmask, %#jx);
40950dc4c7dSKirk McKusick 	CHK(fs->fs_bshift, !=, ILOG2(fs->fs_bsize), %jd);
41050dc4c7dSKirk McKusick 	CHK(fs->fs_fshift, !=, ILOG2(fs->fs_fsize), %jd);
41150dc4c7dSKirk McKusick 	CHK(fs->fs_frag, !=, numfrags(fs, fs->fs_bsize), %jd);
41250dc4c7dSKirk McKusick 	CHK(fs->fs_fragshift, !=, ILOG2(fs->fs_frag), %jd);
41350dc4c7dSKirk McKusick 	CHK(fs->fs_frag, >, MAXFRAG, %jd);
41450dc4c7dSKirk McKusick 	CHK(fs->fs_fsbtodb, !=, ILOG2(fs->fs_fsize / sectorsize), %jd);
41550dc4c7dSKirk McKusick 	CHK(fs->fs_sblkno, !=, roundup(
41650dc4c7dSKirk McKusick 	    howmany(fs->fs_sblockloc + SBLOCKSIZE, fs->fs_fsize),
41750dc4c7dSKirk McKusick 	    fs->fs_frag), %jd);
41850dc4c7dSKirk McKusick 	CHK(fs->fs_cblkno, !=, fs->fs_sblkno +
41950dc4c7dSKirk McKusick 	    roundup(howmany(SBLOCKSIZE, fs->fs_fsize), fs->fs_frag), %jd);
42050dc4c7dSKirk McKusick 	CHK(fs->fs_iblkno, !=, fs->fs_cblkno + fs->fs_frag, %jd);
42150dc4c7dSKirk McKusick 	CHK(fs->fs_dblkno, !=, fs->fs_iblkno + fs->fs_ipg / INOPF(fs), %jd);
42250dc4c7dSKirk McKusick 	CHK(fs->fs_cgsize, >, fs->fs_bsize, %jd);
42350dc4c7dSKirk McKusick 	CHK(fs->fs_cssize, !=,
42450dc4c7dSKirk McKusick 		fragroundup(fs, fs->fs_ncg * sizeof(struct csum)), %jd);
4255bc926afSKirk McKusick 	/*
4265bc926afSKirk McKusick 	 * This test is valid, however older versions of growfs failed
4275bc926afSKirk McKusick 	 * to correctly update fs_dsize so will fail this test. Thus we
4285bc926afSKirk McKusick 	 * exclude it from the requirements.
4295bc926afSKirk McKusick 	 */
4305bc926afSKirk McKusick #ifdef notdef
43150dc4c7dSKirk McKusick 	CHK(fs->fs_dsize, !=, fs->fs_size - fs->fs_sblkno -
432076002f2SKirk McKusick 		fs->fs_ncg * (fs->fs_dblkno - fs->fs_sblkno) -
43350dc4c7dSKirk McKusick 		howmany(fs->fs_cssize, fs->fs_fsize), %jd);
4345bc926afSKirk McKusick #endif
43550dc4c7dSKirk McKusick 	CHK(fs->fs_metaspace, <, 0, %jd);
43650dc4c7dSKirk McKusick 	CHK(fs->fs_metaspace, >, fs->fs_fpg / 2, %jd);
43750dc4c7dSKirk McKusick 	CHK(fs->fs_minfree, >, 99, %jd%%);
438076002f2SKirk McKusick 	maxfilesize = fs->fs_bsize * UFS_NDADDR - 1;
439076002f2SKirk McKusick 	for (sizepb = fs->fs_bsize, i = 0; i < UFS_NIADDR; i++) {
440076002f2SKirk McKusick 		sizepb *= NINDIR(fs);
441076002f2SKirk McKusick 		maxfilesize += sizepb;
442076002f2SKirk McKusick 	}
44350dc4c7dSKirk McKusick 	CHK(fs->fs_maxfilesize, !=, maxfilesize, %jd);
444076002f2SKirk McKusick 	/*
445076002f2SKirk McKusick 	 * These values have a tight interaction with each other that
446076002f2SKirk McKusick 	 * makes it hard to tightly bound them. So we can only check
447076002f2SKirk McKusick 	 * that they are within a broader possible range.
448076002f2SKirk McKusick 	 *
449f3f5368dSKirk McKusick 	 * The size cannot always be accurately determined, but ensure
450f3f5368dSKirk McKusick 	 * that it is consistent with the number of cylinder groups (fs_ncg)
451f3f5368dSKirk McKusick 	 * and the number of fragments per cylinder group (fs_fpg). Ensure
452f3f5368dSKirk McKusick 	 * that the summary information size is correct and that it starts
453f3f5368dSKirk McKusick 	 * and ends in the data area of the same cylinder group.
454076002f2SKirk McKusick 	 */
45550dc4c7dSKirk McKusick 	CHK(fs->fs_ncg, <, 1, %jd);
45650dc4c7dSKirk McKusick 	CHK(fs->fs_size, <, 8 * fs->fs_frag, %jd);
45750dc4c7dSKirk McKusick 	CHK(fs->fs_size, <=, (fs->fs_ncg - 1) * fs->fs_fpg, %jd);
45850dc4c7dSKirk McKusick 	CHK(fs->fs_size, >, fs->fs_ncg * fs->fs_fpg, %jd);
459f3f5368dSKirk McKusick 	CHK(fs->fs_cssize, !=,
460f3f5368dSKirk McKusick 	    fragroundup(fs, fs->fs_ncg * sizeof(struct csum)), %jd);
461f3f5368dSKirk McKusick 	cgnum = dtog(fs, fs->fs_csaddr);
462f3f5368dSKirk McKusick 	CHK(fs->fs_csaddr, <, cgdmin(fs, cgnum), %jd);
463f3f5368dSKirk McKusick 	CHK(dtog(fs, fs->fs_csaddr + howmany(fs->fs_cssize, fs->fs_fsize)), >,
464f3f5368dSKirk McKusick 	    cgnum, %jd);
465f3f5368dSKirk McKusick 	CHK(fs->fs_ipg * fs->fs_ncg, >, (((int64_t)(1)) << 32) - INOPB(fs),
466f3f5368dSKirk McKusick 	    %jd);
467076002f2SKirk McKusick 	/*
468bc218d89SKirk McKusick 	 * With file system clustering it is possible to allocate
469bc218d89SKirk McKusick 	 * many contiguous blocks. The kernel variable maxphys defines
470bc218d89SKirk McKusick 	 * the maximum transfer size permitted by the controller and/or
471bc218d89SKirk McKusick 	 * buffering. The fs_maxcontig parameter controls the maximum
472bc218d89SKirk McKusick 	 * number of blocks that the filesystem will read or write
473bc218d89SKirk McKusick 	 * in a single transfer. It is calculated when the filesystem
474bc218d89SKirk McKusick 	 * is created as maxphys / fs_bsize. The loader uses a maxphys
475bc218d89SKirk McKusick 	 * of 128K even when running on a system that supports larger
476bc218d89SKirk McKusick 	 * values. If the filesystem was built on a system that supports
477bc218d89SKirk McKusick 	 * a larger maxphys (1M is typical) it will have configured
478bc218d89SKirk McKusick 	 * fs_maxcontig for that larger system. So we bound the upper
479bc218d89SKirk McKusick 	 * allowable limit for fs_maxconfig to be able to at least
480bc218d89SKirk McKusick 	 * work with a 1M maxphys on the smallest block size filesystem:
481bc218d89SKirk McKusick 	 * 1M / 4096 == 256. There is no harm in allowing the mounting of
482bc218d89SKirk McKusick 	 * filesystems that make larger than maxphys I/O requests because
483bc218d89SKirk McKusick 	 * those (mostly 32-bit machines) can (very slowly) handle I/O
484bc218d89SKirk McKusick 	 * requests that exceed maxphys.
485076002f2SKirk McKusick 	 */
48650dc4c7dSKirk McKusick 	CHK(fs->fs_maxcontig, <, 0, %jd);
487f3f5368dSKirk McKusick 	CHK(fs->fs_maxcontig, >, MAX(256, maxphys / fs->fs_bsize), %jd);
48850dc4c7dSKirk McKusick 	CHK2(fs->fs_maxcontig, ==, 0, fs->fs_contigsumsize, !=, 0, %jd);
48950dc4c7dSKirk McKusick 	CHK2(fs->fs_maxcontig, >, 1, fs->fs_contigsumsize, !=,
49050dc4c7dSKirk McKusick 	    MIN(fs->fs_maxcontig, FS_MAXCONTIG), %jd);
491076002f2SKirk McKusick 	return (0);
492076002f2SKirk McKusick }
493076002f2SKirk McKusick 
494076002f2SKirk McKusick /*
495dffce215SKirk McKusick  * Write a superblock to the devfd device from the memory pointed to by fs.
496dffce215SKirk McKusick  * Write out the superblock summary information if it is present.
497dffce215SKirk McKusick  *
498dffce215SKirk McKusick  * If the write is successful, zero is returned. Otherwise one of the
499dffce215SKirk McKusick  * following error values is returned:
500dffce215SKirk McKusick  *     EIO: failed to write superblock.
501dffce215SKirk McKusick  *     EIO: failed to write superblock summary information.
502dffce215SKirk McKusick  */
503dffce215SKirk McKusick int
504dffce215SKirk McKusick ffs_sbput(void *devfd, struct fs *fs, off_t loc,
505dffce215SKirk McKusick     int (*writefunc)(void *devfd, off_t loc, void *buf, int size))
506dffce215SKirk McKusick {
507dffce215SKirk McKusick 	int i, error, blks, size;
508dffce215SKirk McKusick 	uint8_t *space;
509dffce215SKirk McKusick 
510dffce215SKirk McKusick 	/*
511dffce215SKirk McKusick 	 * If there is summary information, write it first, so if there
512dffce215SKirk McKusick 	 * is an error, the superblock will not be marked as clean.
513dffce215SKirk McKusick 	 */
51434816cb9SKirk McKusick 	if (fs->fs_si != NULL && fs->fs_csp != NULL) {
515dffce215SKirk McKusick 		blks = howmany(fs->fs_cssize, fs->fs_fsize);
516dffce215SKirk McKusick 		space = (uint8_t *)fs->fs_csp;
517dffce215SKirk McKusick 		for (i = 0; i < blks; i += fs->fs_frag) {
518dffce215SKirk McKusick 			size = fs->fs_bsize;
519dffce215SKirk McKusick 			if (i + fs->fs_frag > blks)
520dffce215SKirk McKusick 				size = (blks - i) * fs->fs_fsize;
521dffce215SKirk McKusick 			if ((error = (*writefunc)(devfd,
522dffce215SKirk McKusick 			     dbtob(fsbtodb(fs, fs->fs_csaddr + i)),
523dffce215SKirk McKusick 			     space, size)) != 0)
524dffce215SKirk McKusick 				return (error);
525dffce215SKirk McKusick 			space += size;
526dffce215SKirk McKusick 		}
527dffce215SKirk McKusick 	}
528dffce215SKirk McKusick 	fs->fs_fmod = 0;
52993440bbeSKirk McKusick #ifndef _KERNEL
53093440bbeSKirk McKusick 	{
53193440bbeSKirk McKusick 		struct fs_summary_info *fs_si;
53293440bbeSKirk McKusick 
53393440bbeSKirk McKusick 		fs->fs_time = time(NULL);
53493440bbeSKirk McKusick 		/* Clear the pointers for the duration of writing. */
53593440bbeSKirk McKusick 		fs_si = fs->fs_si;
53693440bbeSKirk McKusick 		fs->fs_si = NULL;
537ade67b50SKirk McKusick 		fs->fs_ckhash = ffs_calc_sbhash(fs);
53893440bbeSKirk McKusick 		error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize);
53993440bbeSKirk McKusick 		fs->fs_si = fs_si;
54093440bbeSKirk McKusick 	}
54193440bbeSKirk McKusick #else /* _KERNEL */
54293440bbeSKirk McKusick 	fs->fs_time = time_second;
54393440bbeSKirk McKusick 	fs->fs_ckhash = ffs_calc_sbhash(fs);
54493440bbeSKirk McKusick 	error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize);
54593440bbeSKirk McKusick #endif /* _KERNEL */
546dffce215SKirk McKusick 	return (error);
547dffce215SKirk McKusick }
548dffce215SKirk McKusick 
549dffce215SKirk McKusick /*
550ec888383SKirk McKusick  * Calculate the check-hash for a superblock.
551ec888383SKirk McKusick  */
552ade67b50SKirk McKusick uint32_t
553ade67b50SKirk McKusick ffs_calc_sbhash(struct fs *fs)
554ec888383SKirk McKusick {
555ec888383SKirk McKusick 	uint32_t ckhash, save_ckhash;
556ec888383SKirk McKusick 
557ec888383SKirk McKusick 	/*
558ec888383SKirk McKusick 	 * A filesystem that was using a superblock ckhash may be moved
559ec888383SKirk McKusick 	 * to an older kernel that does not support ckhashes. The
560ec888383SKirk McKusick 	 * older kernel will clear the FS_METACKHASH flag indicating
561ec888383SKirk McKusick 	 * that it does not update hashes. When the disk is moved back
562ec888383SKirk McKusick 	 * to a kernel capable of ckhashes it disables them on mount:
563ec888383SKirk McKusick 	 *
564ec888383SKirk McKusick 	 *	if ((fs->fs_flags & FS_METACKHASH) == 0)
565ec888383SKirk McKusick 	 *		fs->fs_metackhash = 0;
566ec888383SKirk McKusick 	 *
567ec888383SKirk McKusick 	 * This leaves (fs->fs_metackhash & CK_SUPERBLOCK) == 0) with an
568ec888383SKirk McKusick 	 * old stale value in the fs->fs_ckhash field. Thus the need to
569ec888383SKirk McKusick 	 * just accept what is there.
570ec888383SKirk McKusick 	 */
571ec888383SKirk McKusick 	if ((fs->fs_metackhash & CK_SUPERBLOCK) == 0)
572ec888383SKirk McKusick 		return (fs->fs_ckhash);
573ec888383SKirk McKusick 
574ec888383SKirk McKusick 	save_ckhash = fs->fs_ckhash;
575ec888383SKirk McKusick 	fs->fs_ckhash = 0;
576ec888383SKirk McKusick 	/*
577ec888383SKirk McKusick 	 * If newly read from disk, the caller is responsible for
578ec888383SKirk McKusick 	 * verifying that fs->fs_sbsize <= SBLOCKSIZE.
579ec888383SKirk McKusick 	 */
580ec888383SKirk McKusick 	ckhash = calculate_crc32c(~0L, (void *)fs, fs->fs_sbsize);
581ec888383SKirk McKusick 	fs->fs_ckhash = save_ckhash;
582ec888383SKirk McKusick 	return (ckhash);
583ec888383SKirk McKusick }
584ec888383SKirk McKusick 
585ec888383SKirk McKusick /*
586df8bae1dSRodney W. Grimes  * Update the frsum fields to reflect addition or deletion
587df8bae1dSRodney W. Grimes  * of some frags.
588df8bae1dSRodney W. Grimes  */
589df8bae1dSRodney W. Grimes void
59015c377c3SEd Maste ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt)
591df8bae1dSRodney W. Grimes {
592df8bae1dSRodney W. Grimes 	int inblk;
59305f4ff5dSPoul-Henning Kamp 	int field, subfield;
59405f4ff5dSPoul-Henning Kamp 	int siz, pos;
595df8bae1dSRodney W. Grimes 
596df8bae1dSRodney W. Grimes 	inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
597df8bae1dSRodney W. Grimes 	fragmap <<= 1;
598df8bae1dSRodney W. Grimes 	for (siz = 1; siz < fs->fs_frag; siz++) {
599df8bae1dSRodney W. Grimes 		if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
600df8bae1dSRodney W. Grimes 			continue;
601df8bae1dSRodney W. Grimes 		field = around[siz];
602df8bae1dSRodney W. Grimes 		subfield = inside[siz];
603df8bae1dSRodney W. Grimes 		for (pos = siz; pos <= fs->fs_frag; pos++) {
604df8bae1dSRodney W. Grimes 			if ((fragmap & field) == subfield) {
605df8bae1dSRodney W. Grimes 				fraglist[siz] += cnt;
606df8bae1dSRodney W. Grimes 				pos += siz;
607df8bae1dSRodney W. Grimes 				field <<= siz;
608df8bae1dSRodney W. Grimes 				subfield <<= siz;
609df8bae1dSRodney W. Grimes 			}
610df8bae1dSRodney W. Grimes 			field <<= 1;
611df8bae1dSRodney W. Grimes 			subfield <<= 1;
612df8bae1dSRodney W. Grimes 		}
613df8bae1dSRodney W. Grimes 	}
614df8bae1dSRodney W. Grimes }
615df8bae1dSRodney W. Grimes 
616df8bae1dSRodney W. Grimes /*
617df8bae1dSRodney W. Grimes  * block operations
618df8bae1dSRodney W. Grimes  *
619df8bae1dSRodney W. Grimes  * check if a block is available
620df8bae1dSRodney W. Grimes  */
621df8bae1dSRodney W. Grimes int
62215c377c3SEd Maste ffs_isblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
623df8bae1dSRodney W. Grimes {
624df8bae1dSRodney W. Grimes 	unsigned char mask;
625df8bae1dSRodney W. Grimes 
626df8bae1dSRodney W. Grimes 	switch ((int)fs->fs_frag) {
627df8bae1dSRodney W. Grimes 	case 8:
628df8bae1dSRodney W. Grimes 		return (cp[h] == 0xff);
629df8bae1dSRodney W. Grimes 	case 4:
630df8bae1dSRodney W. Grimes 		mask = 0x0f << ((h & 0x1) << 2);
631df8bae1dSRodney W. Grimes 		return ((cp[h >> 1] & mask) == mask);
632df8bae1dSRodney W. Grimes 	case 2:
633df8bae1dSRodney W. Grimes 		mask = 0x03 << ((h & 0x3) << 1);
634df8bae1dSRodney W. Grimes 		return ((cp[h >> 2] & mask) == mask);
635df8bae1dSRodney W. Grimes 	case 1:
636df8bae1dSRodney W. Grimes 		mask = 0x01 << (h & 0x7);
637df8bae1dSRodney W. Grimes 		return ((cp[h >> 3] & mask) == mask);
638df8bae1dSRodney W. Grimes 	default:
639113db2ddSJeff Roberson #ifdef _KERNEL
640df8bae1dSRodney W. Grimes 		panic("ffs_isblock");
641113db2ddSJeff Roberson #endif
642113db2ddSJeff Roberson 		break;
643113db2ddSJeff Roberson 	}
644113db2ddSJeff Roberson 	return (0);
645113db2ddSJeff Roberson }
646113db2ddSJeff Roberson 
647113db2ddSJeff Roberson /*
648113db2ddSJeff Roberson  * check if a block is free
649113db2ddSJeff Roberson  */
650113db2ddSJeff Roberson int
65115c377c3SEd Maste ffs_isfreeblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
652113db2ddSJeff Roberson {
653113db2ddSJeff Roberson 
654113db2ddSJeff Roberson 	switch ((int)fs->fs_frag) {
655113db2ddSJeff Roberson 	case 8:
656113db2ddSJeff Roberson 		return (cp[h] == 0);
657113db2ddSJeff Roberson 	case 4:
658113db2ddSJeff Roberson 		return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
659113db2ddSJeff Roberson 	case 2:
660113db2ddSJeff Roberson 		return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
661113db2ddSJeff Roberson 	case 1:
662113db2ddSJeff Roberson 		return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
663113db2ddSJeff Roberson 	default:
664113db2ddSJeff Roberson #ifdef _KERNEL
665113db2ddSJeff Roberson 		panic("ffs_isfreeblock");
666113db2ddSJeff Roberson #endif
667113db2ddSJeff Roberson 		break;
668df8bae1dSRodney W. Grimes 	}
669589c7af9SKirk McKusick 	return (0);
670df8bae1dSRodney W. Grimes }
671df8bae1dSRodney W. Grimes 
672df8bae1dSRodney W. Grimes /*
673df8bae1dSRodney W. Grimes  * take a block out of the map
674df8bae1dSRodney W. Grimes  */
675df8bae1dSRodney W. Grimes void
67615c377c3SEd Maste ffs_clrblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
677df8bae1dSRodney W. Grimes {
678df8bae1dSRodney W. Grimes 
679df8bae1dSRodney W. Grimes 	switch ((int)fs->fs_frag) {
680df8bae1dSRodney W. Grimes 	case 8:
681df8bae1dSRodney W. Grimes 		cp[h] = 0;
682df8bae1dSRodney W. Grimes 		return;
683df8bae1dSRodney W. Grimes 	case 4:
684df8bae1dSRodney W. Grimes 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
685df8bae1dSRodney W. Grimes 		return;
686df8bae1dSRodney W. Grimes 	case 2:
687df8bae1dSRodney W. Grimes 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
688df8bae1dSRodney W. Grimes 		return;
689df8bae1dSRodney W. Grimes 	case 1:
690df8bae1dSRodney W. Grimes 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
691df8bae1dSRodney W. Grimes 		return;
692df8bae1dSRodney W. Grimes 	default:
693113db2ddSJeff Roberson #ifdef _KERNEL
694df8bae1dSRodney W. Grimes 		panic("ffs_clrblock");
695113db2ddSJeff Roberson #endif
696113db2ddSJeff Roberson 		break;
697df8bae1dSRodney W. Grimes 	}
698df8bae1dSRodney W. Grimes }
699df8bae1dSRodney W. Grimes 
700df8bae1dSRodney W. Grimes /*
701df8bae1dSRodney W. Grimes  * put a block into the map
702df8bae1dSRodney W. Grimes  */
703df8bae1dSRodney W. Grimes void
70415c377c3SEd Maste ffs_setblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
705df8bae1dSRodney W. Grimes {
706df8bae1dSRodney W. Grimes 
707df8bae1dSRodney W. Grimes 	switch ((int)fs->fs_frag) {
708df8bae1dSRodney W. Grimes 	case 8:
709df8bae1dSRodney W. Grimes 		cp[h] = 0xff;
710df8bae1dSRodney W. Grimes 		return;
711df8bae1dSRodney W. Grimes 	case 4:
712df8bae1dSRodney W. Grimes 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
713df8bae1dSRodney W. Grimes 		return;
714df8bae1dSRodney W. Grimes 	case 2:
715df8bae1dSRodney W. Grimes 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
716df8bae1dSRodney W. Grimes 		return;
717df8bae1dSRodney W. Grimes 	case 1:
718df8bae1dSRodney W. Grimes 		cp[h >> 3] |= (0x01 << (h & 0x7));
719df8bae1dSRodney W. Grimes 		return;
720df8bae1dSRodney W. Grimes 	default:
721113db2ddSJeff Roberson #ifdef _KERNEL
722df8bae1dSRodney W. Grimes 		panic("ffs_setblock");
723113db2ddSJeff Roberson #endif
724113db2ddSJeff Roberson 		break;
725df8bae1dSRodney W. Grimes 	}
726df8bae1dSRodney W. Grimes }
727113db2ddSJeff Roberson 
728113db2ddSJeff Roberson /*
729113db2ddSJeff Roberson  * Update the cluster map because of an allocation or free.
730113db2ddSJeff Roberson  *
731113db2ddSJeff Roberson  * Cnt == 1 means free; cnt == -1 means allocating.
732113db2ddSJeff Roberson  */
733113db2ddSJeff Roberson void
73415c377c3SEd Maste ffs_clusteracct(struct fs *fs, struct cg *cgp, ufs1_daddr_t blkno, int cnt)
735113db2ddSJeff Roberson {
736113db2ddSJeff Roberson 	int32_t *sump;
737113db2ddSJeff Roberson 	int32_t *lp;
738113db2ddSJeff Roberson 	u_char *freemapp, *mapp;
739e1c27cf7SKirk McKusick 	int i, start, end, forw, back, map;
740e1c27cf7SKirk McKusick 	u_int bit;
741113db2ddSJeff Roberson 
742113db2ddSJeff Roberson 	if (fs->fs_contigsumsize <= 0)
743113db2ddSJeff Roberson 		return;
744113db2ddSJeff Roberson 	freemapp = cg_clustersfree(cgp);
745113db2ddSJeff Roberson 	sump = cg_clustersum(cgp);
746113db2ddSJeff Roberson 	/*
747113db2ddSJeff Roberson 	 * Allocate or clear the actual block.
748113db2ddSJeff Roberson 	 */
749113db2ddSJeff Roberson 	if (cnt > 0)
750113db2ddSJeff Roberson 		setbit(freemapp, blkno);
751113db2ddSJeff Roberson 	else
752113db2ddSJeff Roberson 		clrbit(freemapp, blkno);
753113db2ddSJeff Roberson 	/*
754113db2ddSJeff Roberson 	 * Find the size of the cluster going forward.
755113db2ddSJeff Roberson 	 */
756113db2ddSJeff Roberson 	start = blkno + 1;
757113db2ddSJeff Roberson 	end = start + fs->fs_contigsumsize;
758113db2ddSJeff Roberson 	if (end >= cgp->cg_nclusterblks)
759113db2ddSJeff Roberson 		end = cgp->cg_nclusterblks;
760113db2ddSJeff Roberson 	mapp = &freemapp[start / NBBY];
761113db2ddSJeff Roberson 	map = *mapp++;
762e1c27cf7SKirk McKusick 	bit = 1U << (start % NBBY);
763113db2ddSJeff Roberson 	for (i = start; i < end; i++) {
764113db2ddSJeff Roberson 		if ((map & bit) == 0)
765113db2ddSJeff Roberson 			break;
766113db2ddSJeff Roberson 		if ((i & (NBBY - 1)) != (NBBY - 1)) {
767113db2ddSJeff Roberson 			bit <<= 1;
768113db2ddSJeff Roberson 		} else {
769113db2ddSJeff Roberson 			map = *mapp++;
770113db2ddSJeff Roberson 			bit = 1;
771113db2ddSJeff Roberson 		}
772113db2ddSJeff Roberson 	}
773113db2ddSJeff Roberson 	forw = i - start;
774113db2ddSJeff Roberson 	/*
775113db2ddSJeff Roberson 	 * Find the size of the cluster going backward.
776113db2ddSJeff Roberson 	 */
777113db2ddSJeff Roberson 	start = blkno - 1;
778113db2ddSJeff Roberson 	end = start - fs->fs_contigsumsize;
779113db2ddSJeff Roberson 	if (end < 0)
780113db2ddSJeff Roberson 		end = -1;
781113db2ddSJeff Roberson 	mapp = &freemapp[start / NBBY];
782113db2ddSJeff Roberson 	map = *mapp--;
783e1c27cf7SKirk McKusick 	bit = 1U << (start % NBBY);
784113db2ddSJeff Roberson 	for (i = start; i > end; i--) {
785113db2ddSJeff Roberson 		if ((map & bit) == 0)
786113db2ddSJeff Roberson 			break;
787113db2ddSJeff Roberson 		if ((i & (NBBY - 1)) != 0) {
788113db2ddSJeff Roberson 			bit >>= 1;
789113db2ddSJeff Roberson 		} else {
790113db2ddSJeff Roberson 			map = *mapp--;
791e1c27cf7SKirk McKusick 			bit = 1U << (NBBY - 1);
792113db2ddSJeff Roberson 		}
793113db2ddSJeff Roberson 	}
794113db2ddSJeff Roberson 	back = start - i;
795113db2ddSJeff Roberson 	/*
796113db2ddSJeff Roberson 	 * Account for old cluster and the possibly new forward and
797113db2ddSJeff Roberson 	 * back clusters.
798113db2ddSJeff Roberson 	 */
799113db2ddSJeff Roberson 	i = back + forw + 1;
800113db2ddSJeff Roberson 	if (i > fs->fs_contigsumsize)
801113db2ddSJeff Roberson 		i = fs->fs_contigsumsize;
802113db2ddSJeff Roberson 	sump[i] += cnt;
803113db2ddSJeff Roberson 	if (back > 0)
804113db2ddSJeff Roberson 		sump[back] -= cnt;
805113db2ddSJeff Roberson 	if (forw > 0)
806113db2ddSJeff Roberson 		sump[forw] -= cnt;
807113db2ddSJeff Roberson 	/*
808113db2ddSJeff Roberson 	 * Update cluster summary information.
809113db2ddSJeff Roberson 	 */
810113db2ddSJeff Roberson 	lp = &sump[fs->fs_contigsumsize];
811113db2ddSJeff Roberson 	for (i = fs->fs_contigsumsize; i > 0; i--)
812113db2ddSJeff Roberson 		if (*lp-- > 0)
813113db2ddSJeff Roberson 			break;
814113db2ddSJeff Roberson 	fs->fs_maxcluster[cgp->cg_cgx] = i;
815113db2ddSJeff Roberson }
816