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