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