xref: /freebsd/sys/ufs/ffs/ffs_subr.c (revision 406a584d7e80c2617dc035ede0d922215a12141c)
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/lock.h>
63 #include <sys/malloc.h>
64 #include <sys/mount.h>
65 #include <sys/vnode.h>
66 #include <sys/bio.h>
67 #include <sys/buf.h>
68 #include <sys/ucred.h>
69 
70 #include <ufs/ufs/quota.h>
71 #include <ufs/ufs/inode.h>
72 #include <ufs/ufs/extattr.h>
73 #include <ufs/ufs/ufsmount.h>
74 #include <ufs/ufs/ufs_extern.h>
75 #include <ufs/ffs/ffs_extern.h>
76 #include <ufs/ffs/fs.h>
77 
78 #define UFS_MALLOC(size, type, flags) malloc(size, type, flags)
79 #define UFS_FREE(ptr, type) free(ptr, type)
80 #define UFS_TIME time_second
81 
82 /*
83  * Return buffer with the contents of block "offset" from the beginning of
84  * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
85  * remaining space in the directory.
86  */
87 int
88 ffs_blkatoff(struct vnode *vp, off_t offset, char **res, struct buf **bpp)
89 {
90 	struct inode *ip;
91 	struct fs *fs;
92 	struct buf *bp;
93 	ufs_lbn_t lbn;
94 	int bsize, error;
95 
96 	ip = VTOI(vp);
97 	fs = ITOFS(ip);
98 	lbn = lblkno(fs, offset);
99 	bsize = blksize(fs, ip, lbn);
100 
101 	*bpp = NULL;
102 	error = bread(vp, lbn, bsize, NOCRED, &bp);
103 	if (error) {
104 		brelse(bp);
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 #endif /* _KERNEL */
157 
158 /*
159  * Verify an inode check-hash.
160  */
161 int
162 ffs_verify_dinode_ckhash(struct fs *fs, struct ufs2_dinode *dip)
163 {
164 	uint32_t ckhash, save_ckhash;
165 
166 	/*
167 	 * Return success if unallocated or we are not doing inode check-hash.
168 	 */
169 	if (dip->di_mode == 0 || (fs->fs_metackhash & CK_INODE) == 0)
170 		return (0);
171 	/*
172 	 * Exclude di_ckhash from the crc32 calculation, e.g., always use
173 	 * a check-hash value of zero when calculating the check-hash.
174 	 */
175 	save_ckhash = dip->di_ckhash;
176 	dip->di_ckhash = 0;
177 	ckhash = calculate_crc32c(~0L, (void *)dip, sizeof(*dip));
178 	dip->di_ckhash = save_ckhash;
179 	if (save_ckhash == ckhash)
180 		return (0);
181 	return (EINVAL);
182 }
183 
184 /*
185  * Update an inode check-hash.
186  */
187 void
188 ffs_update_dinode_ckhash(struct fs *fs, struct ufs2_dinode *dip)
189 {
190 
191 	if (dip->di_mode == 0 || (fs->fs_metackhash & CK_INODE) == 0)
192 		return;
193 	/*
194 	 * Exclude old di_ckhash from the crc32 calculation, e.g., always use
195 	 * a check-hash value of zero when calculating the new check-hash.
196 	 */
197 	dip->di_ckhash = 0;
198 	dip->di_ckhash = calculate_crc32c(~0L, (void *)dip, sizeof(*dip));
199 }
200 
201 /*
202  * These are the low-level functions that actually read and write
203  * the superblock and its associated data.
204  */
205 static off_t sblock_try[] = SBLOCKSEARCH;
206 static int readsuper(void *, struct fs **, off_t, int, int,
207 	int (*)(void *, off_t, void **, int));
208 
209 /*
210  * Read a superblock from the devfd device.
211  *
212  * If an alternate superblock is specified, it is read. Otherwise the
213  * set of locations given in the SBLOCKSEARCH list is searched for a
214  * superblock. Memory is allocated for the superblock by the readfunc and
215  * is returned. If filltype is non-NULL, additional memory is allocated
216  * of type filltype and filled in with the superblock summary information.
217  * All memory is freed when any error is returned.
218  *
219  * If a superblock is found, zero is returned. Otherwise one of the
220  * following error values is returned:
221  *     EIO: non-existent or truncated superblock.
222  *     EIO: error reading summary information.
223  *     ENOENT: no usable known superblock found.
224  *     ENOSPC: failed to allocate space for the superblock.
225  *     EINVAL: The previous newfs operation on this volume did not complete.
226  *         The administrator must complete newfs before using this volume.
227  */
228 int
229 ffs_sbget(void *devfd, struct fs **fsp, off_t altsblock,
230     struct malloc_type *filltype,
231     int (*readfunc)(void *devfd, off_t loc, void **bufp, int size))
232 {
233 	struct fs *fs;
234 	int i, error, size, blks;
235 	uint8_t *space;
236 	int32_t *lp;
237 	int chkhash;
238 	char *buf;
239 
240 	fs = NULL;
241 	*fsp = NULL;
242 	chkhash = 1;
243 	if (altsblock >= 0) {
244 		if ((error = readsuper(devfd, &fs, altsblock, 1, chkhash,
245 		     readfunc)) != 0) {
246 			if (fs != NULL)
247 				UFS_FREE(fs, filltype);
248 			return (error);
249 		}
250 	} else {
251 		if (altsblock == STDSB_NOHASHFAIL)
252 			chkhash = 0;
253 		for (i = 0; sblock_try[i] != -1; i++) {
254 			if ((error = readsuper(devfd, &fs, sblock_try[i], 0,
255 			     chkhash, readfunc)) == 0)
256 				break;
257 			if (fs != NULL) {
258 				UFS_FREE(fs, filltype);
259 				fs = NULL;
260 			}
261 			if (error == ENOENT)
262 				continue;
263 			return (error);
264 		}
265 		if (sblock_try[i] == -1)
266 			return (ENOENT);
267 	}
268 	/*
269 	 * Read in the superblock summary information.
270 	 */
271 	size = fs->fs_cssize;
272 	blks = howmany(size, fs->fs_fsize);
273 	if (fs->fs_contigsumsize > 0)
274 		size += fs->fs_ncg * sizeof(int32_t);
275 	size += fs->fs_ncg * sizeof(u_int8_t);
276 	/* When running in libufs or libsa, UFS_MALLOC may fail */
277 	if ((space = UFS_MALLOC(size, filltype, M_WAITOK)) == NULL) {
278 		UFS_FREE(fs, filltype);
279 		return (ENOSPC);
280 	}
281 	fs->fs_csp = (struct csum *)space;
282 	for (i = 0; i < blks; i += fs->fs_frag) {
283 		size = fs->fs_bsize;
284 		if (i + fs->fs_frag > blks)
285 			size = (blks - i) * fs->fs_fsize;
286 		buf = NULL;
287 		error = (*readfunc)(devfd,
288 		    dbtob(fsbtodb(fs, fs->fs_csaddr + i)), (void **)&buf, size);
289 		if (error) {
290 			if (buf != NULL)
291 				UFS_FREE(buf, filltype);
292 			UFS_FREE(fs->fs_csp, filltype);
293 			UFS_FREE(fs, filltype);
294 			return (error);
295 		}
296 		memcpy(space, buf, size);
297 		UFS_FREE(buf, filltype);
298 		space += size;
299 	}
300 	if (fs->fs_contigsumsize > 0) {
301 		fs->fs_maxcluster = lp = (int32_t *)space;
302 		for (i = 0; i < fs->fs_ncg; i++)
303 			*lp++ = fs->fs_contigsumsize;
304 		space = (uint8_t *)lp;
305 	}
306 	size = fs->fs_ncg * sizeof(u_int8_t);
307 	fs->fs_contigdirs = (u_int8_t *)space;
308 	bzero(fs->fs_contigdirs, size);
309 	*fsp = fs;
310 	return (0);
311 }
312 
313 /*
314  * Try to read a superblock from the location specified by sblockloc.
315  * Return zero on success or an errno on failure.
316  */
317 static int
318 readsuper(void *devfd, struct fs **fsp, off_t sblockloc, int isaltsblk,
319     int chkhash, int (*readfunc)(void *devfd, off_t loc, void **bufp, int size))
320 {
321 	struct fs *fs;
322 	int error, res;
323 	uint32_t ckhash;
324 
325 	error = (*readfunc)(devfd, sblockloc, (void **)fsp, SBLOCKSIZE);
326 	if (error != 0)
327 		return (error);
328 	fs = *fsp;
329 	if (fs->fs_magic == FS_BAD_MAGIC)
330 		return (EINVAL);
331 	if (((fs->fs_magic == FS_UFS1_MAGIC && (isaltsblk ||
332 	      sblockloc <= SBLOCK_UFS1)) ||
333 	     (fs->fs_magic == FS_UFS2_MAGIC && (isaltsblk ||
334 	      sblockloc == fs->fs_sblockloc))) &&
335 	    fs->fs_ncg >= 1 &&
336 	    fs->fs_bsize >= MINBSIZE &&
337 	    fs->fs_bsize <= MAXBSIZE &&
338 	    fs->fs_bsize >= roundup(sizeof(struct fs), DEV_BSIZE) &&
339 	    fs->fs_sbsize <= SBLOCKSIZE) {
340 		/*
341 		 * If the filesystem has been run on a kernel without
342 		 * metadata check hashes, disable them.
343 		 */
344 		if ((fs->fs_flags & FS_METACKHASH) == 0)
345 			fs->fs_metackhash = 0;
346 		if (fs->fs_ckhash != (ckhash = ffs_calc_sbhash(fs))) {
347 #ifdef _KERNEL
348 			res = uprintf("Superblock check-hash failed: recorded "
349 			    "check-hash 0x%x != computed check-hash 0x%x%s\n",
350 			    fs->fs_ckhash, ckhash,
351 			    chkhash == 0 ? " (Ignored)" : "");
352 #else
353 			res = 0;
354 #endif
355 			/*
356 			 * Print check-hash failure if no controlling terminal
357 			 * in kernel or always if in user-mode (libufs).
358 			 */
359 			if (res == 0)
360 				printf("Superblock check-hash failed: recorded "
361 				    "check-hash 0x%x != computed check-hash "
362 				    "0x%x%s\n", fs->fs_ckhash, ckhash,
363 				    chkhash == 0 ? " (Ignored)" : "");
364 			if (chkhash == 0) {
365 				fs->fs_flags |= FS_NEEDSFSCK;
366 				fs->fs_fmod = 1;
367 				return (0);
368 			}
369 			fs->fs_fmod = 0;
370 			return (EINVAL);
371 		}
372 		/* Have to set for old filesystems that predate this field */
373 		fs->fs_sblockactualloc = sblockloc;
374 		/* Not yet any summary information */
375 		fs->fs_csp = NULL;
376 		return (0);
377 	}
378 	return (ENOENT);
379 }
380 
381 /*
382  * Write a superblock to the devfd device from the memory pointed to by fs.
383  * Write out the superblock summary information if it is present.
384  *
385  * If the write is successful, zero is returned. Otherwise one of the
386  * following error values is returned:
387  *     EIO: failed to write superblock.
388  *     EIO: failed to write superblock summary information.
389  */
390 int
391 ffs_sbput(void *devfd, struct fs *fs, off_t loc,
392     int (*writefunc)(void *devfd, off_t loc, void *buf, int size))
393 {
394 	int i, error, blks, size;
395 	uint8_t *space;
396 
397 	/*
398 	 * If there is summary information, write it first, so if there
399 	 * is an error, the superblock will not be marked as clean.
400 	 */
401 	if (fs->fs_csp != NULL) {
402 		blks = howmany(fs->fs_cssize, fs->fs_fsize);
403 		space = (uint8_t *)fs->fs_csp;
404 		for (i = 0; i < blks; i += fs->fs_frag) {
405 			size = fs->fs_bsize;
406 			if (i + fs->fs_frag > blks)
407 				size = (blks - i) * fs->fs_fsize;
408 			if ((error = (*writefunc)(devfd,
409 			     dbtob(fsbtodb(fs, fs->fs_csaddr + i)),
410 			     space, size)) != 0)
411 				return (error);
412 			space += size;
413 		}
414 	}
415 	fs->fs_fmod = 0;
416 	fs->fs_time = UFS_TIME;
417 	fs->fs_ckhash = ffs_calc_sbhash(fs);
418 	if ((error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize)) != 0)
419 		return (error);
420 	return (0);
421 }
422 
423 /*
424  * Calculate the check-hash for a superblock.
425  */
426 uint32_t
427 ffs_calc_sbhash(struct fs *fs)
428 {
429 	uint32_t ckhash, save_ckhash;
430 
431 	/*
432 	 * A filesystem that was using a superblock ckhash may be moved
433 	 * to an older kernel that does not support ckhashes. The
434 	 * older kernel will clear the FS_METACKHASH flag indicating
435 	 * that it does not update hashes. When the disk is moved back
436 	 * to a kernel capable of ckhashes it disables them on mount:
437 	 *
438 	 *	if ((fs->fs_flags & FS_METACKHASH) == 0)
439 	 *		fs->fs_metackhash = 0;
440 	 *
441 	 * This leaves (fs->fs_metackhash & CK_SUPERBLOCK) == 0) with an
442 	 * old stale value in the fs->fs_ckhash field. Thus the need to
443 	 * just accept what is there.
444 	 */
445 	if ((fs->fs_metackhash & CK_SUPERBLOCK) == 0)
446 		return (fs->fs_ckhash);
447 
448 	save_ckhash = fs->fs_ckhash;
449 	fs->fs_ckhash = 0;
450 	/*
451 	 * If newly read from disk, the caller is responsible for
452 	 * verifying that fs->fs_sbsize <= SBLOCKSIZE.
453 	 */
454 	ckhash = calculate_crc32c(~0L, (void *)fs, fs->fs_sbsize);
455 	fs->fs_ckhash = save_ckhash;
456 	return (ckhash);
457 }
458 
459 /*
460  * Update the frsum fields to reflect addition or deletion
461  * of some frags.
462  */
463 void
464 ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt)
465 {
466 	int inblk;
467 	int field, subfield;
468 	int siz, pos;
469 
470 	inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
471 	fragmap <<= 1;
472 	for (siz = 1; siz < fs->fs_frag; siz++) {
473 		if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
474 			continue;
475 		field = around[siz];
476 		subfield = inside[siz];
477 		for (pos = siz; pos <= fs->fs_frag; pos++) {
478 			if ((fragmap & field) == subfield) {
479 				fraglist[siz] += cnt;
480 				pos += siz;
481 				field <<= siz;
482 				subfield <<= siz;
483 			}
484 			field <<= 1;
485 			subfield <<= 1;
486 		}
487 	}
488 }
489 
490 /*
491  * block operations
492  *
493  * check if a block is available
494  */
495 int
496 ffs_isblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
497 {
498 	unsigned char mask;
499 
500 	switch ((int)fs->fs_frag) {
501 	case 8:
502 		return (cp[h] == 0xff);
503 	case 4:
504 		mask = 0x0f << ((h & 0x1) << 2);
505 		return ((cp[h >> 1] & mask) == mask);
506 	case 2:
507 		mask = 0x03 << ((h & 0x3) << 1);
508 		return ((cp[h >> 2] & mask) == mask);
509 	case 1:
510 		mask = 0x01 << (h & 0x7);
511 		return ((cp[h >> 3] & mask) == mask);
512 	default:
513 #ifdef _KERNEL
514 		panic("ffs_isblock");
515 #endif
516 		break;
517 	}
518 	return (0);
519 }
520 
521 /*
522  * check if a block is free
523  */
524 int
525 ffs_isfreeblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
526 {
527 
528 	switch ((int)fs->fs_frag) {
529 	case 8:
530 		return (cp[h] == 0);
531 	case 4:
532 		return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
533 	case 2:
534 		return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
535 	case 1:
536 		return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
537 	default:
538 #ifdef _KERNEL
539 		panic("ffs_isfreeblock");
540 #endif
541 		break;
542 	}
543 	return (0);
544 }
545 
546 /*
547  * take a block out of the map
548  */
549 void
550 ffs_clrblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
551 {
552 
553 	switch ((int)fs->fs_frag) {
554 	case 8:
555 		cp[h] = 0;
556 		return;
557 	case 4:
558 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
559 		return;
560 	case 2:
561 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
562 		return;
563 	case 1:
564 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
565 		return;
566 	default:
567 #ifdef _KERNEL
568 		panic("ffs_clrblock");
569 #endif
570 		break;
571 	}
572 }
573 
574 /*
575  * put a block into the map
576  */
577 void
578 ffs_setblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
579 {
580 
581 	switch ((int)fs->fs_frag) {
582 
583 	case 8:
584 		cp[h] = 0xff;
585 		return;
586 	case 4:
587 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
588 		return;
589 	case 2:
590 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
591 		return;
592 	case 1:
593 		cp[h >> 3] |= (0x01 << (h & 0x7));
594 		return;
595 	default:
596 #ifdef _KERNEL
597 		panic("ffs_setblock");
598 #endif
599 		break;
600 	}
601 }
602 
603 /*
604  * Update the cluster map because of an allocation or free.
605  *
606  * Cnt == 1 means free; cnt == -1 means allocating.
607  */
608 void
609 ffs_clusteracct(struct fs *fs, struct cg *cgp, ufs1_daddr_t blkno, int cnt)
610 {
611 	int32_t *sump;
612 	int32_t *lp;
613 	u_char *freemapp, *mapp;
614 	int i, start, end, forw, back, map;
615 	u_int bit;
616 
617 	if (fs->fs_contigsumsize <= 0)
618 		return;
619 	freemapp = cg_clustersfree(cgp);
620 	sump = cg_clustersum(cgp);
621 	/*
622 	 * Allocate or clear the actual block.
623 	 */
624 	if (cnt > 0)
625 		setbit(freemapp, blkno);
626 	else
627 		clrbit(freemapp, blkno);
628 	/*
629 	 * Find the size of the cluster going forward.
630 	 */
631 	start = blkno + 1;
632 	end = start + fs->fs_contigsumsize;
633 	if (end >= cgp->cg_nclusterblks)
634 		end = cgp->cg_nclusterblks;
635 	mapp = &freemapp[start / NBBY];
636 	map = *mapp++;
637 	bit = 1U << (start % NBBY);
638 	for (i = start; i < end; i++) {
639 		if ((map & bit) == 0)
640 			break;
641 		if ((i & (NBBY - 1)) != (NBBY - 1)) {
642 			bit <<= 1;
643 		} else {
644 			map = *mapp++;
645 			bit = 1;
646 		}
647 	}
648 	forw = i - start;
649 	/*
650 	 * Find the size of the cluster going backward.
651 	 */
652 	start = blkno - 1;
653 	end = start - fs->fs_contigsumsize;
654 	if (end < 0)
655 		end = -1;
656 	mapp = &freemapp[start / NBBY];
657 	map = *mapp--;
658 	bit = 1U << (start % NBBY);
659 	for (i = start; i > end; i--) {
660 		if ((map & bit) == 0)
661 			break;
662 		if ((i & (NBBY - 1)) != 0) {
663 			bit >>= 1;
664 		} else {
665 			map = *mapp--;
666 			bit = 1U << (NBBY - 1);
667 		}
668 	}
669 	back = start - i;
670 	/*
671 	 * Account for old cluster and the possibly new forward and
672 	 * back clusters.
673 	 */
674 	i = back + forw + 1;
675 	if (i > fs->fs_contigsumsize)
676 		i = fs->fs_contigsumsize;
677 	sump[i] += cnt;
678 	if (back > 0)
679 		sump[back] -= cnt;
680 	if (forw > 0)
681 		sump[forw] -= cnt;
682 	/*
683 	 * Update cluster summary information.
684 	 */
685 	lp = &sump[fs->fs_contigsumsize];
686 	for (i = fs->fs_contigsumsize; i > 0; i--)
687 		if (*lp-- > 0)
688 			break;
689 	fs->fs_maxcluster[cgp->cg_cgx] = i;
690 }
691