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