xref: /freebsd/sys/ufs/ffs/ffs_subr.c (revision a8675d927bbde29d5a4dc6efddf2f0dc6d6d6983)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1989, 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  *	@(#)ffs_subr.c	8.5 (Berkeley) 3/21/95
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include <sys/param.h>
38 
39 #ifndef _KERNEL
40 #include <stdio.h>
41 #include <string.h>
42 #include <stdlib.h>
43 #include <time.h>
44 #include <sys/errno.h>
45 #include <ufs/ufs/dinode.h>
46 #include <ufs/ffs/fs.h>
47 
48 uint32_t calculate_crc32c(uint32_t, const void *, size_t);
49 uint32_t ffs_calc_sbhash(struct fs *);
50 struct malloc_type;
51 #define UFS_MALLOC(size, type, flags) malloc(size)
52 #define UFS_FREE(ptr, type) free(ptr)
53 #define UFS_TIME time(NULL)
54 /*
55  * Request standard superblock location in ffs_sbget
56  */
57 #define	STDSB			-1	/* Fail if check-hash is bad */
58 #define	STDSB_NOHASHFAIL	-2	/* Ignore check-hash failure */
59 
60 #else /* _KERNEL */
61 #include <sys/systm.h>
62 #include <sys/gsb_crc32.h>
63 #include <sys/lock.h>
64 #include <sys/malloc.h>
65 #include <sys/mount.h>
66 #include <sys/vnode.h>
67 #include <sys/bio.h>
68 #include <sys/buf.h>
69 #include <sys/ucred.h>
70 
71 #include <ufs/ufs/quota.h>
72 #include <ufs/ufs/inode.h>
73 #include <ufs/ufs/extattr.h>
74 #include <ufs/ufs/ufsmount.h>
75 #include <ufs/ufs/ufs_extern.h>
76 #include <ufs/ffs/ffs_extern.h>
77 #include <ufs/ffs/fs.h>
78 
79 #define UFS_MALLOC(size, type, flags) malloc(size, type, flags)
80 #define UFS_FREE(ptr, type) free(ptr, type)
81 #define UFS_TIME time_second
82 
83 /*
84  * Return buffer with the contents of block "offset" from the beginning of
85  * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
86  * remaining space in the directory.
87  */
88 int
89 ffs_blkatoff(struct vnode *vp, off_t offset, char **res, struct buf **bpp)
90 {
91 	struct inode *ip;
92 	struct fs *fs;
93 	struct buf *bp;
94 	ufs_lbn_t lbn;
95 	int bsize, error;
96 
97 	ip = VTOI(vp);
98 	fs = ITOFS(ip);
99 	lbn = lblkno(fs, offset);
100 	bsize = blksize(fs, ip, lbn);
101 
102 	*bpp = NULL;
103 	error = bread(vp, lbn, bsize, NOCRED, &bp);
104 	if (error) {
105 		return (error);
106 	}
107 	if (res)
108 		*res = (char *)bp->b_data + blkoff(fs, offset);
109 	*bpp = bp;
110 	return (0);
111 }
112 
113 /*
114  * Load up the contents of an inode and copy the appropriate pieces
115  * to the incore copy.
116  */
117 int
118 ffs_load_inode(struct buf *bp, struct inode *ip, struct fs *fs, ino_t ino)
119 {
120 	struct ufs1_dinode *dip1;
121 	struct ufs2_dinode *dip2;
122 	int error;
123 
124 	if (I_IS_UFS1(ip)) {
125 		dip1 = ip->i_din1;
126 		*dip1 =
127 		    *((struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
128 		ip->i_mode = dip1->di_mode;
129 		ip->i_nlink = dip1->di_nlink;
130 		ip->i_effnlink = dip1->di_nlink;
131 		ip->i_size = dip1->di_size;
132 		ip->i_flags = dip1->di_flags;
133 		ip->i_gen = dip1->di_gen;
134 		ip->i_uid = dip1->di_uid;
135 		ip->i_gid = dip1->di_gid;
136 		return (0);
137 	}
138 	dip2 = ((struct ufs2_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
139 	if ((error = ffs_verify_dinode_ckhash(fs, dip2)) != 0) {
140 		printf("%s: inode %jd: check-hash failed\n", fs->fs_fsmnt,
141 		    (intmax_t)ino);
142 		return (error);
143 	}
144 	*ip->i_din2 = *dip2;
145 	dip2 = ip->i_din2;
146 	ip->i_mode = dip2->di_mode;
147 	ip->i_nlink = dip2->di_nlink;
148 	ip->i_effnlink = dip2->di_nlink;
149 	ip->i_size = dip2->di_size;
150 	ip->i_flags = dip2->di_flags;
151 	ip->i_gen = dip2->di_gen;
152 	ip->i_uid = dip2->di_uid;
153 	ip->i_gid = dip2->di_gid;
154 	return (0);
155 }
156 
157 /*
158  * Verify that a filesystem block number is a valid data block.
159  * This routine is only called on untrusted filesystems.
160  */
161 int
162 ffs_check_blkno(struct mount *mp, ino_t inum, ufs2_daddr_t daddr, int blksize)
163 {
164 	struct fs *fs;
165 	struct ufsmount *ump;
166 	ufs2_daddr_t end_daddr;
167 	int cg, havemtx;
168 
169 	KASSERT((mp->mnt_flag & MNT_UNTRUSTED) != 0,
170 	    ("ffs_check_blkno called on a trusted file system"));
171 	ump = VFSTOUFS(mp);
172 	fs = ump->um_fs;
173 	cg = dtog(fs, daddr);
174 	end_daddr = daddr + numfrags(fs, blksize);
175 	/*
176 	 * Verify that the block number is a valid data block. Also check
177 	 * that it does not point to an inode block or a superblock. Accept
178 	 * blocks that are unalloacted (0) or part of snapshot metadata
179 	 * (BLK_NOCOPY or BLK_SNAP).
180 	 *
181 	 * Thus, the block must be in a valid range for the filesystem and
182 	 * either in the space before a backup superblock (except the first
183 	 * cylinder group where that space is used by the bootstrap code) or
184 	 * after the inode blocks and before the end of the cylinder group.
185 	 */
186 	if ((uint64_t)daddr <= BLK_SNAP ||
187 	    ((uint64_t)end_daddr <= fs->fs_size &&
188 	    ((cg > 0 && end_daddr <= cgsblock(fs, cg)) ||
189 	    (daddr >= cgdmin(fs, cg) &&
190 	    end_daddr <= cgbase(fs, cg) + fs->fs_fpg))))
191 		return (0);
192 	if ((havemtx = mtx_owned(UFS_MTX(ump))) == 0)
193 		UFS_LOCK(ump);
194 	if (ppsratecheck(&ump->um_last_integritymsg,
195 	    &ump->um_secs_integritymsg, 1)) {
196 		UFS_UNLOCK(ump);
197 		uprintf("\n%s: inode %jd, out-of-range indirect block "
198 		    "number %jd\n", mp->mnt_stat.f_mntonname, inum, daddr);
199 		if (havemtx)
200 			UFS_LOCK(ump);
201 	} else if (!havemtx)
202 		UFS_UNLOCK(ump);
203 	return (EINTEGRITY);
204 }
205 #endif /* _KERNEL */
206 
207 /*
208  * Verify an inode check-hash.
209  */
210 int
211 ffs_verify_dinode_ckhash(struct fs *fs, struct ufs2_dinode *dip)
212 {
213 	uint32_t ckhash, save_ckhash;
214 
215 	/*
216 	 * Return success if unallocated or we are not doing inode check-hash.
217 	 */
218 	if (dip->di_mode == 0 || (fs->fs_metackhash & CK_INODE) == 0)
219 		return (0);
220 	/*
221 	 * Exclude di_ckhash from the crc32 calculation, e.g., always use
222 	 * a check-hash value of zero when calculating the check-hash.
223 	 */
224 	save_ckhash = dip->di_ckhash;
225 	dip->di_ckhash = 0;
226 	ckhash = calculate_crc32c(~0L, (void *)dip, sizeof(*dip));
227 	dip->di_ckhash = save_ckhash;
228 	if (save_ckhash == ckhash)
229 		return (0);
230 	return (EINVAL);
231 }
232 
233 /*
234  * Update an inode check-hash.
235  */
236 void
237 ffs_update_dinode_ckhash(struct fs *fs, struct ufs2_dinode *dip)
238 {
239 
240 	if (dip->di_mode == 0 || (fs->fs_metackhash & CK_INODE) == 0)
241 		return;
242 	/*
243 	 * Exclude old di_ckhash from the crc32 calculation, e.g., always use
244 	 * a check-hash value of zero when calculating the new check-hash.
245 	 */
246 	dip->di_ckhash = 0;
247 	dip->di_ckhash = calculate_crc32c(~0L, (void *)dip, sizeof(*dip));
248 }
249 
250 /*
251  * These are the low-level functions that actually read and write
252  * the superblock and its associated data.
253  */
254 static off_t sblock_try[] = SBLOCKSEARCH;
255 static int readsuper(void *, struct fs **, off_t, int, int,
256 	int (*)(void *, off_t, void **, int));
257 
258 /*
259  * Read a superblock from the devfd device.
260  *
261  * If an alternate superblock is specified, it is read. Otherwise the
262  * set of locations given in the SBLOCKSEARCH list is searched for a
263  * superblock. Memory is allocated for the superblock by the readfunc and
264  * is returned. If filltype is non-NULL, additional memory is allocated
265  * of type filltype and filled in with the superblock summary information.
266  * All memory is freed when any error is returned.
267  *
268  * If a superblock is found, zero is returned. Otherwise one of the
269  * following error values is returned:
270  *     EIO: non-existent or truncated superblock.
271  *     EIO: error reading summary information.
272  *     ENOENT: no usable known superblock found.
273  *     ENOSPC: failed to allocate space for the superblock.
274  *     EINVAL: The previous newfs operation on this volume did not complete.
275  *         The administrator must complete newfs before using this volume.
276  */
277 int
278 ffs_sbget(void *devfd, struct fs **fsp, off_t altsblock,
279     struct malloc_type *filltype,
280     int (*readfunc)(void *devfd, off_t loc, void **bufp, int size))
281 {
282 	struct fs *fs;
283 	int i, error, size, blks;
284 	uint8_t *space;
285 	int32_t *lp;
286 	int chkhash;
287 	char *buf;
288 
289 	fs = NULL;
290 	*fsp = NULL;
291 	chkhash = 1;
292 	if (altsblock >= 0) {
293 		if ((error = readsuper(devfd, &fs, altsblock, 1, chkhash,
294 		     readfunc)) != 0) {
295 			if (fs != NULL)
296 				UFS_FREE(fs, filltype);
297 			return (error);
298 		}
299 	} else {
300 		if (altsblock == STDSB_NOHASHFAIL)
301 			chkhash = 0;
302 		for (i = 0; sblock_try[i] != -1; i++) {
303 			if ((error = readsuper(devfd, &fs, sblock_try[i], 0,
304 			     chkhash, readfunc)) == 0)
305 				break;
306 			if (fs != NULL) {
307 				UFS_FREE(fs, filltype);
308 				fs = NULL;
309 			}
310 			if (error == ENOENT)
311 				continue;
312 			return (error);
313 		}
314 		if (sblock_try[i] == -1)
315 			return (ENOENT);
316 	}
317 	/*
318 	 * Read in the superblock summary information.
319 	 */
320 	size = fs->fs_cssize;
321 	blks = howmany(size, fs->fs_fsize);
322 	if (fs->fs_contigsumsize > 0)
323 		size += fs->fs_ncg * sizeof(int32_t);
324 	size += fs->fs_ncg * sizeof(u_int8_t);
325 	/* When running in libufs or libsa, UFS_MALLOC may fail */
326 	if ((space = UFS_MALLOC(size, filltype, M_WAITOK)) == NULL) {
327 		UFS_FREE(fs, filltype);
328 		return (ENOSPC);
329 	}
330 	fs->fs_csp = (struct csum *)space;
331 	for (i = 0; i < blks; i += fs->fs_frag) {
332 		size = fs->fs_bsize;
333 		if (i + fs->fs_frag > blks)
334 			size = (blks - i) * fs->fs_fsize;
335 		buf = NULL;
336 		error = (*readfunc)(devfd,
337 		    dbtob(fsbtodb(fs, fs->fs_csaddr + i)), (void **)&buf, size);
338 		if (error) {
339 			if (buf != NULL)
340 				UFS_FREE(buf, filltype);
341 			UFS_FREE(fs->fs_csp, filltype);
342 			UFS_FREE(fs, filltype);
343 			return (error);
344 		}
345 		memcpy(space, buf, size);
346 		UFS_FREE(buf, filltype);
347 		space += size;
348 	}
349 	if (fs->fs_contigsumsize > 0) {
350 		fs->fs_maxcluster = lp = (int32_t *)space;
351 		for (i = 0; i < fs->fs_ncg; i++)
352 			*lp++ = fs->fs_contigsumsize;
353 		space = (uint8_t *)lp;
354 	}
355 	size = fs->fs_ncg * sizeof(u_int8_t);
356 	fs->fs_contigdirs = (u_int8_t *)space;
357 	bzero(fs->fs_contigdirs, size);
358 	*fsp = fs;
359 	return (0);
360 }
361 
362 /*
363  * Try to read a superblock from the location specified by sblockloc.
364  * Return zero on success or an errno on failure.
365  */
366 static int
367 readsuper(void *devfd, struct fs **fsp, off_t sblockloc, int isaltsblk,
368     int chkhash, int (*readfunc)(void *devfd, off_t loc, void **bufp, int size))
369 {
370 	struct fs *fs;
371 	int error, res;
372 	uint32_t ckhash;
373 
374 	error = (*readfunc)(devfd, sblockloc, (void **)fsp, SBLOCKSIZE);
375 	if (error != 0)
376 		return (error);
377 	fs = *fsp;
378 	if (fs->fs_magic == FS_BAD_MAGIC)
379 		return (EINVAL);
380 	if (((fs->fs_magic == FS_UFS1_MAGIC && (isaltsblk ||
381 	      sblockloc <= SBLOCK_UFS1)) ||
382 	     (fs->fs_magic == FS_UFS2_MAGIC && (isaltsblk ||
383 	      sblockloc == fs->fs_sblockloc))) &&
384 	    fs->fs_ncg >= 1 &&
385 	    fs->fs_bsize >= MINBSIZE &&
386 	    fs->fs_bsize <= MAXBSIZE &&
387 	    fs->fs_bsize >= roundup(sizeof(struct fs), DEV_BSIZE) &&
388 	    fs->fs_sbsize <= SBLOCKSIZE) {
389 		/*
390 		 * If the filesystem has been run on a kernel without
391 		 * metadata check hashes, disable them.
392 		 */
393 		if ((fs->fs_flags & FS_METACKHASH) == 0)
394 			fs->fs_metackhash = 0;
395 		if (fs->fs_ckhash != (ckhash = ffs_calc_sbhash(fs))) {
396 #ifdef _KERNEL
397 			res = uprintf("Superblock check-hash failed: recorded "
398 			    "check-hash 0x%x != computed check-hash 0x%x%s\n",
399 			    fs->fs_ckhash, ckhash,
400 			    chkhash == 0 ? " (Ignored)" : "");
401 #else
402 			res = 0;
403 #endif
404 			/*
405 			 * Print check-hash failure if no controlling terminal
406 			 * in kernel or always if in user-mode (libufs).
407 			 */
408 			if (res == 0)
409 				printf("Superblock check-hash failed: recorded "
410 				    "check-hash 0x%x != computed check-hash "
411 				    "0x%x%s\n", fs->fs_ckhash, ckhash,
412 				    chkhash == 0 ? " (Ignored)" : "");
413 			if (chkhash == 0) {
414 				fs->fs_flags |= FS_NEEDSFSCK;
415 				fs->fs_fmod = 1;
416 				return (0);
417 			}
418 			fs->fs_fmod = 0;
419 			return (EINTEGRITY);
420 		}
421 		/* Have to set for old filesystems that predate this field */
422 		fs->fs_sblockactualloc = sblockloc;
423 		/* Not yet any summary information */
424 		fs->fs_csp = NULL;
425 		return (0);
426 	}
427 	return (ENOENT);
428 }
429 
430 /*
431  * Write a superblock to the devfd device from the memory pointed to by fs.
432  * Write out the superblock summary information if it is present.
433  *
434  * If the write is successful, zero is returned. Otherwise one of the
435  * following error values is returned:
436  *     EIO: failed to write superblock.
437  *     EIO: failed to write superblock summary information.
438  */
439 int
440 ffs_sbput(void *devfd, struct fs *fs, off_t loc,
441     int (*writefunc)(void *devfd, off_t loc, void *buf, int size))
442 {
443 	int i, error, blks, size;
444 	uint8_t *space;
445 
446 	/*
447 	 * If there is summary information, write it first, so if there
448 	 * is an error, the superblock will not be marked as clean.
449 	 */
450 	if (fs->fs_csp != NULL) {
451 		blks = howmany(fs->fs_cssize, fs->fs_fsize);
452 		space = (uint8_t *)fs->fs_csp;
453 		for (i = 0; i < blks; i += fs->fs_frag) {
454 			size = fs->fs_bsize;
455 			if (i + fs->fs_frag > blks)
456 				size = (blks - i) * fs->fs_fsize;
457 			if ((error = (*writefunc)(devfd,
458 			     dbtob(fsbtodb(fs, fs->fs_csaddr + i)),
459 			     space, size)) != 0)
460 				return (error);
461 			space += size;
462 		}
463 	}
464 	fs->fs_fmod = 0;
465 	fs->fs_time = UFS_TIME;
466 	fs->fs_ckhash = ffs_calc_sbhash(fs);
467 	if ((error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize)) != 0)
468 		return (error);
469 	return (0);
470 }
471 
472 /*
473  * Calculate the check-hash for a superblock.
474  */
475 uint32_t
476 ffs_calc_sbhash(struct fs *fs)
477 {
478 	uint32_t ckhash, save_ckhash;
479 
480 	/*
481 	 * A filesystem that was using a superblock ckhash may be moved
482 	 * to an older kernel that does not support ckhashes. The
483 	 * older kernel will clear the FS_METACKHASH flag indicating
484 	 * that it does not update hashes. When the disk is moved back
485 	 * to a kernel capable of ckhashes it disables them on mount:
486 	 *
487 	 *	if ((fs->fs_flags & FS_METACKHASH) == 0)
488 	 *		fs->fs_metackhash = 0;
489 	 *
490 	 * This leaves (fs->fs_metackhash & CK_SUPERBLOCK) == 0) with an
491 	 * old stale value in the fs->fs_ckhash field. Thus the need to
492 	 * just accept what is there.
493 	 */
494 	if ((fs->fs_metackhash & CK_SUPERBLOCK) == 0)
495 		return (fs->fs_ckhash);
496 
497 	save_ckhash = fs->fs_ckhash;
498 	fs->fs_ckhash = 0;
499 	/*
500 	 * If newly read from disk, the caller is responsible for
501 	 * verifying that fs->fs_sbsize <= SBLOCKSIZE.
502 	 */
503 	ckhash = calculate_crc32c(~0L, (void *)fs, fs->fs_sbsize);
504 	fs->fs_ckhash = save_ckhash;
505 	return (ckhash);
506 }
507 
508 /*
509  * Update the frsum fields to reflect addition or deletion
510  * of some frags.
511  */
512 void
513 ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt)
514 {
515 	int inblk;
516 	int field, subfield;
517 	int siz, pos;
518 
519 	inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
520 	fragmap <<= 1;
521 	for (siz = 1; siz < fs->fs_frag; siz++) {
522 		if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
523 			continue;
524 		field = around[siz];
525 		subfield = inside[siz];
526 		for (pos = siz; pos <= fs->fs_frag; pos++) {
527 			if ((fragmap & field) == subfield) {
528 				fraglist[siz] += cnt;
529 				pos += siz;
530 				field <<= siz;
531 				subfield <<= siz;
532 			}
533 			field <<= 1;
534 			subfield <<= 1;
535 		}
536 	}
537 }
538 
539 /*
540  * block operations
541  *
542  * check if a block is available
543  */
544 int
545 ffs_isblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
546 {
547 	unsigned char mask;
548 
549 	switch ((int)fs->fs_frag) {
550 	case 8:
551 		return (cp[h] == 0xff);
552 	case 4:
553 		mask = 0x0f << ((h & 0x1) << 2);
554 		return ((cp[h >> 1] & mask) == mask);
555 	case 2:
556 		mask = 0x03 << ((h & 0x3) << 1);
557 		return ((cp[h >> 2] & mask) == mask);
558 	case 1:
559 		mask = 0x01 << (h & 0x7);
560 		return ((cp[h >> 3] & mask) == mask);
561 	default:
562 #ifdef _KERNEL
563 		panic("ffs_isblock");
564 #endif
565 		break;
566 	}
567 	return (0);
568 }
569 
570 /*
571  * check if a block is free
572  */
573 int
574 ffs_isfreeblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
575 {
576 
577 	switch ((int)fs->fs_frag) {
578 	case 8:
579 		return (cp[h] == 0);
580 	case 4:
581 		return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
582 	case 2:
583 		return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
584 	case 1:
585 		return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
586 	default:
587 #ifdef _KERNEL
588 		panic("ffs_isfreeblock");
589 #endif
590 		break;
591 	}
592 	return (0);
593 }
594 
595 /*
596  * take a block out of the map
597  */
598 void
599 ffs_clrblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
600 {
601 
602 	switch ((int)fs->fs_frag) {
603 	case 8:
604 		cp[h] = 0;
605 		return;
606 	case 4:
607 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
608 		return;
609 	case 2:
610 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
611 		return;
612 	case 1:
613 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
614 		return;
615 	default:
616 #ifdef _KERNEL
617 		panic("ffs_clrblock");
618 #endif
619 		break;
620 	}
621 }
622 
623 /*
624  * put a block into the map
625  */
626 void
627 ffs_setblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
628 {
629 
630 	switch ((int)fs->fs_frag) {
631 
632 	case 8:
633 		cp[h] = 0xff;
634 		return;
635 	case 4:
636 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
637 		return;
638 	case 2:
639 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
640 		return;
641 	case 1:
642 		cp[h >> 3] |= (0x01 << (h & 0x7));
643 		return;
644 	default:
645 #ifdef _KERNEL
646 		panic("ffs_setblock");
647 #endif
648 		break;
649 	}
650 }
651 
652 /*
653  * Update the cluster map because of an allocation or free.
654  *
655  * Cnt == 1 means free; cnt == -1 means allocating.
656  */
657 void
658 ffs_clusteracct(struct fs *fs, struct cg *cgp, ufs1_daddr_t blkno, int cnt)
659 {
660 	int32_t *sump;
661 	int32_t *lp;
662 	u_char *freemapp, *mapp;
663 	int i, start, end, forw, back, map;
664 	u_int bit;
665 
666 	if (fs->fs_contigsumsize <= 0)
667 		return;
668 	freemapp = cg_clustersfree(cgp);
669 	sump = cg_clustersum(cgp);
670 	/*
671 	 * Allocate or clear the actual block.
672 	 */
673 	if (cnt > 0)
674 		setbit(freemapp, blkno);
675 	else
676 		clrbit(freemapp, blkno);
677 	/*
678 	 * Find the size of the cluster going forward.
679 	 */
680 	start = blkno + 1;
681 	end = start + fs->fs_contigsumsize;
682 	if (end >= cgp->cg_nclusterblks)
683 		end = cgp->cg_nclusterblks;
684 	mapp = &freemapp[start / NBBY];
685 	map = *mapp++;
686 	bit = 1U << (start % NBBY);
687 	for (i = start; i < end; i++) {
688 		if ((map & bit) == 0)
689 			break;
690 		if ((i & (NBBY - 1)) != (NBBY - 1)) {
691 			bit <<= 1;
692 		} else {
693 			map = *mapp++;
694 			bit = 1;
695 		}
696 	}
697 	forw = i - start;
698 	/*
699 	 * Find the size of the cluster going backward.
700 	 */
701 	start = blkno - 1;
702 	end = start - fs->fs_contigsumsize;
703 	if (end < 0)
704 		end = -1;
705 	mapp = &freemapp[start / NBBY];
706 	map = *mapp--;
707 	bit = 1U << (start % NBBY);
708 	for (i = start; i > end; i--) {
709 		if ((map & bit) == 0)
710 			break;
711 		if ((i & (NBBY - 1)) != 0) {
712 			bit >>= 1;
713 		} else {
714 			map = *mapp--;
715 			bit = 1U << (NBBY - 1);
716 		}
717 	}
718 	back = start - i;
719 	/*
720 	 * Account for old cluster and the possibly new forward and
721 	 * back clusters.
722 	 */
723 	i = back + forw + 1;
724 	if (i > fs->fs_contigsumsize)
725 		i = fs->fs_contigsumsize;
726 	sump[i] += cnt;
727 	if (back > 0)
728 		sump[back] -= cnt;
729 	if (forw > 0)
730 		sump[forw] -= cnt;
731 	/*
732 	 * Update cluster summary information.
733 	 */
734 	lp = &sump[fs->fs_contigsumsize];
735 	for (i = fs->fs_contigsumsize; i > 0; i--)
736 		if (*lp-- > 0)
737 			break;
738 	fs->fs_maxcluster[cgp->cg_cgx] = i;
739 }
740