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 #include <sys/limits.h> 39 40 #ifndef _KERNEL 41 #include <stdio.h> 42 #include <string.h> 43 #include <stdlib.h> 44 #include <time.h> 45 #include <sys/errno.h> 46 #include <ufs/ufs/dinode.h> 47 #include <ufs/ffs/fs.h> 48 49 uint32_t calculate_crc32c(uint32_t, const void *, size_t); 50 uint32_t ffs_calc_sbhash(struct fs *); 51 struct malloc_type; 52 #define UFS_MALLOC(size, type, flags) malloc(size) 53 #define UFS_FREE(ptr, type) free(ptr) 54 #define maxphys MAXPHYS 55 56 #else /* _KERNEL */ 57 #include <sys/systm.h> 58 #include <sys/gsb_crc32.h> 59 #include <sys/lock.h> 60 #include <sys/malloc.h> 61 #include <sys/mount.h> 62 #include <sys/vnode.h> 63 #include <sys/bio.h> 64 #include <sys/buf.h> 65 #include <sys/ucred.h> 66 67 #include <ufs/ufs/quota.h> 68 #include <ufs/ufs/inode.h> 69 #include <ufs/ufs/extattr.h> 70 #include <ufs/ufs/ufsmount.h> 71 #include <ufs/ufs/ufs_extern.h> 72 #include <ufs/ffs/ffs_extern.h> 73 #include <ufs/ffs/fs.h> 74 75 #define UFS_MALLOC(size, type, flags) malloc(size, type, flags) 76 #define UFS_FREE(ptr, type) free(ptr, type) 77 78 #endif /* _KERNEL */ 79 80 /* 81 * Verify an inode check-hash. 82 */ 83 int 84 ffs_verify_dinode_ckhash(struct fs *fs, struct ufs2_dinode *dip) 85 { 86 uint32_t ckhash, save_ckhash; 87 88 /* 89 * Return success if unallocated or we are not doing inode check-hash. 90 */ 91 if (dip->di_mode == 0 || (fs->fs_metackhash & CK_INODE) == 0) 92 return (0); 93 /* 94 * Exclude di_ckhash from the crc32 calculation, e.g., always use 95 * a check-hash value of zero when calculating the check-hash. 96 */ 97 save_ckhash = dip->di_ckhash; 98 dip->di_ckhash = 0; 99 ckhash = calculate_crc32c(~0L, (void *)dip, sizeof(*dip)); 100 dip->di_ckhash = save_ckhash; 101 if (save_ckhash == ckhash) 102 return (0); 103 return (EINVAL); 104 } 105 106 /* 107 * Update an inode check-hash. 108 */ 109 void 110 ffs_update_dinode_ckhash(struct fs *fs, struct ufs2_dinode *dip) 111 { 112 113 if (dip->di_mode == 0 || (fs->fs_metackhash & CK_INODE) == 0) 114 return; 115 /* 116 * Exclude old di_ckhash from the crc32 calculation, e.g., always use 117 * a check-hash value of zero when calculating the new check-hash. 118 */ 119 dip->di_ckhash = 0; 120 dip->di_ckhash = calculate_crc32c(~0L, (void *)dip, sizeof(*dip)); 121 } 122 123 /* 124 * These are the low-level functions that actually read and write 125 * the superblock and its associated data. 126 */ 127 static off_t sblock_try[] = SBLOCKSEARCH; 128 static int readsuper(void *, struct fs **, off_t, int, int, 129 int (*)(void *, off_t, void **, int)); 130 static int validate_sblock(struct fs *, int); 131 132 /* 133 * Read a superblock from the devfd device. 134 * 135 * If an alternate superblock is specified, it is read. Otherwise the 136 * set of locations given in the SBLOCKSEARCH list is searched for a 137 * superblock. Memory is allocated for the superblock by the readfunc and 138 * is returned. If filltype is non-NULL, additional memory is allocated 139 * of type filltype and filled in with the superblock summary information. 140 * All memory is freed when any error is returned. 141 * 142 * If a superblock is found, zero is returned. Otherwise one of the 143 * following error values is returned: 144 * EIO: non-existent or truncated superblock. 145 * EIO: error reading summary information. 146 * ENOENT: no usable known superblock found. 147 * ENOMEM: failed to allocate space for the superblock. 148 * EINVAL: The previous newfs operation on this volume did not complete. 149 * The administrator must complete newfs before using this volume. 150 */ 151 int 152 ffs_sbget(void *devfd, struct fs **fsp, off_t altsblock, 153 struct malloc_type *filltype, 154 int (*readfunc)(void *devfd, off_t loc, void **bufp, int size)) 155 { 156 struct fs *fs; 157 struct fs_summary_info *fs_si; 158 int i, error; 159 uint64_t size, blks; 160 uint8_t *space; 161 int32_t *lp; 162 char *buf; 163 164 fs = NULL; 165 *fsp = NULL; 166 if (altsblock >= 0) { 167 if ((error = readsuper(devfd, &fs, altsblock, 1, 0, 168 readfunc)) != 0) { 169 if (fs != NULL) 170 UFS_FREE(fs, filltype); 171 return (error); 172 } 173 } else { 174 for (i = 0; sblock_try[i] != -1; i++) { 175 if ((error = readsuper(devfd, &fs, sblock_try[i], 0, 176 altsblock, readfunc)) == 0) 177 break; 178 if (fs != NULL) { 179 UFS_FREE(fs, filltype); 180 fs = NULL; 181 } 182 if (error == ENOENT) 183 continue; 184 return (error); 185 } 186 if (sblock_try[i] == -1) 187 return (ENOENT); 188 } 189 /* 190 * Read in the superblock summary information. 191 */ 192 size = fs->fs_cssize; 193 blks = howmany(size, fs->fs_fsize); 194 if (fs->fs_contigsumsize > 0) 195 size += fs->fs_ncg * sizeof(int32_t); 196 size += fs->fs_ncg * sizeof(u_int8_t); 197 if ((fs_si = UFS_MALLOC(sizeof(*fs_si), filltype, M_NOWAIT)) == NULL) { 198 UFS_FREE(fs, filltype); 199 return (ENOMEM); 200 } 201 bzero(fs_si, sizeof(*fs_si)); 202 fs->fs_si = fs_si; 203 if ((space = UFS_MALLOC(size, filltype, M_NOWAIT)) == NULL) { 204 UFS_FREE(fs->fs_si, filltype); 205 UFS_FREE(fs, filltype); 206 return (ENOMEM); 207 } 208 fs->fs_csp = (struct csum *)space; 209 for (i = 0; i < blks; i += fs->fs_frag) { 210 size = fs->fs_bsize; 211 if (i + fs->fs_frag > blks) 212 size = (blks - i) * fs->fs_fsize; 213 buf = NULL; 214 error = (*readfunc)(devfd, 215 dbtob(fsbtodb(fs, fs->fs_csaddr + i)), (void **)&buf, size); 216 if (error) { 217 if (buf != NULL) 218 UFS_FREE(buf, filltype); 219 UFS_FREE(fs->fs_csp, filltype); 220 UFS_FREE(fs->fs_si, filltype); 221 UFS_FREE(fs, filltype); 222 return (error); 223 } 224 memcpy(space, buf, size); 225 UFS_FREE(buf, filltype); 226 space += size; 227 } 228 if (fs->fs_contigsumsize > 0) { 229 fs->fs_maxcluster = lp = (int32_t *)space; 230 for (i = 0; i < fs->fs_ncg; i++) 231 *lp++ = fs->fs_contigsumsize; 232 space = (uint8_t *)lp; 233 } 234 size = fs->fs_ncg * sizeof(u_int8_t); 235 fs->fs_contigdirs = (u_int8_t *)space; 236 bzero(fs->fs_contigdirs, size); 237 *fsp = fs; 238 return (0); 239 } 240 241 /* 242 * Try to read a superblock from the location specified by sblockloc. 243 * Return zero on success or an errno on failure. 244 */ 245 static int 246 readsuper(void *devfd, struct fs **fsp, off_t sblockloc, int isaltsblk, 247 int chkhash, int (*readfunc)(void *devfd, off_t loc, void **bufp, int size)) 248 { 249 struct fs *fs; 250 int error, res; 251 uint32_t ckhash; 252 253 error = (*readfunc)(devfd, sblockloc, (void **)fsp, SBLOCKSIZE); 254 if (error != 0) 255 return (error); 256 fs = *fsp; 257 if (fs->fs_magic == FS_BAD_MAGIC) 258 return (EINVAL); 259 if ((error = validate_sblock(fs, isaltsblk)) != 0) 260 return (error); 261 /* 262 * If the filesystem has been run on a kernel without 263 * metadata check hashes, disable them. 264 */ 265 if ((fs->fs_flags & FS_METACKHASH) == 0) 266 fs->fs_metackhash = 0; 267 /* 268 * Clear any check-hashes that are not maintained 269 * by this kernel. Also clear any unsupported flags. 270 */ 271 fs->fs_metackhash &= CK_SUPPORTED; 272 fs->fs_flags &= FS_SUPPORTED; 273 if (fs->fs_ckhash != (ckhash = ffs_calc_sbhash(fs))) { 274 if (chkhash == STDSB_NOMSG) 275 return (EINTEGRITY); 276 if (chkhash == STDSB_NOHASHFAIL_NOMSG) 277 return (0); 278 #ifdef _KERNEL 279 res = uprintf("Superblock check-hash failed: recorded " 280 "check-hash 0x%x != computed check-hash 0x%x%s\n", 281 fs->fs_ckhash, ckhash, 282 chkhash == STDSB_NOHASHFAIL ? " (Ignored)" : ""); 283 #else 284 res = 0; 285 #endif 286 /* 287 * Print check-hash failure if no controlling terminal 288 * in kernel or always if in user-mode (libufs). 289 */ 290 if (res == 0) 291 printf("Superblock check-hash failed: recorded " 292 "check-hash 0x%x != computed check-hash " 293 "0x%x%s\n", fs->fs_ckhash, ckhash, 294 chkhash == STDSB_NOHASHFAIL ? 295 " (Ignored)" : ""); 296 if (chkhash == STDSB) 297 return (EINTEGRITY); 298 /* chkhash == STDSB_NOHASHFAIL */ 299 return (0); 300 } 301 /* Have to set for old filesystems that predate this field */ 302 fs->fs_sblockactualloc = sblockloc; 303 /* Not yet any summary information */ 304 fs->fs_si = NULL; 305 return (0); 306 } 307 308 /* 309 * Verify the filesystem values. 310 */ 311 #define ILOG2(num) (fls(num) - 1) 312 313 static int 314 validate_sblock(struct fs *fs, int isaltsblk) 315 { 316 int i, sectorsize; 317 u_int64_t maxfilesize, minfpg, sizepb; 318 319 sectorsize = dbtob(1); 320 if (fs->fs_magic == FS_UFS2_MAGIC) { 321 if ((!isaltsblk && (fs->fs_sblockloc != SBLOCK_UFS2 || 322 fs->fs_sblockactualloc != SBLOCK_UFS2)) || 323 fs->fs_maxsymlinklen != ((UFS_NDADDR + UFS_NIADDR) * 324 sizeof(ufs2_daddr_t)) || 325 fs->fs_nindir != fs->fs_bsize / sizeof(ufs2_daddr_t) || 326 fs->fs_inopb != fs->fs_bsize / sizeof(struct ufs2_dinode)) 327 return (ENOENT); 328 } else if (fs->fs_magic == FS_UFS1_MAGIC) { 329 if ((!isaltsblk && (fs->fs_sblockloc > SBLOCK_UFS1 || 330 fs->fs_sblockactualloc != SBLOCK_UFS1)) || 331 fs->fs_nindir != fs->fs_bsize / sizeof(ufs1_daddr_t) || 332 fs->fs_inopb != fs->fs_bsize / sizeof(struct ufs1_dinode) || 333 fs->fs_maxsymlinklen != ((UFS_NDADDR + UFS_NIADDR) * 334 sizeof(ufs1_daddr_t)) || 335 fs->fs_old_inodefmt != FS_44INODEFMT || 336 fs->fs_old_cgoffset != 0 || 337 fs->fs_old_cgmask != 0xffffffff || 338 fs->fs_old_size != fs->fs_size || 339 fs->fs_old_rotdelay != 0 || 340 fs->fs_old_rps != 60 || 341 fs->fs_old_nspf != fs->fs_fsize / sectorsize || 342 fs->fs_old_cpg != 1 || 343 fs->fs_old_interleave != 1 || 344 fs->fs_old_trackskew != 0 || 345 fs->fs_old_cpc != 0 || 346 fs->fs_old_postblformat != 1 || 347 fs->fs_old_nrpos != 1 || 348 fs->fs_old_spc != fs->fs_fpg * fs->fs_old_nspf || 349 fs->fs_old_nsect != fs->fs_old_spc || 350 fs->fs_old_npsect != fs->fs_old_spc || 351 fs->fs_old_dsize != fs->fs_dsize || 352 fs->fs_old_ncyl != fs->fs_ncg) 353 return (ENOENT); 354 } else { 355 return (ENOENT); 356 } 357 if (fs->fs_bsize < MINBSIZE || fs->fs_bsize > MAXBSIZE || 358 fs->fs_bsize < roundup(sizeof(struct fs), DEV_BSIZE) || 359 fs->fs_sbsize > SBLOCKSIZE || fs->fs_sbsize < fs->fs_fsize || 360 !powerof2(fs->fs_bsize)) 361 return (ENOENT); 362 if (fs->fs_fsize < sectorsize || fs->fs_fsize > fs->fs_bsize || 363 fs->fs_fsize * MAXFRAG < fs->fs_bsize || !powerof2(fs->fs_fsize)) 364 return (ENOENT); 365 if (fs->fs_maxbsize < fs->fs_bsize || !powerof2(fs->fs_maxbsize) || 366 fs->fs_maxbsize > FS_MAXCONTIG * fs->fs_bsize) 367 return (ENOENT); 368 if (fs->fs_bmask != ~(fs->fs_bsize - 1) || 369 fs->fs_fmask != ~(fs->fs_fsize - 1) || 370 fs->fs_qbmask != ~fs->fs_bmask || 371 fs->fs_qfmask != ~fs->fs_fmask || 372 fs->fs_bshift != ILOG2(fs->fs_bsize) || 373 fs->fs_fshift != ILOG2(fs->fs_fsize) || 374 fs->fs_frag != numfrags(fs, fs->fs_bsize) || 375 fs->fs_fragshift != ILOG2(fs->fs_frag) || 376 fs->fs_frag > MAXFRAG || 377 fs->fs_fsbtodb != ILOG2(fs->fs_fsize / sectorsize)) 378 return (ENOENT); 379 if (fs->fs_sblkno != 380 roundup(howmany(fs->fs_sblockloc + SBLOCKSIZE, fs->fs_fsize), 381 fs->fs_frag) || 382 fs->fs_cblkno != fs->fs_sblkno + 383 roundup(howmany(SBLOCKSIZE, fs->fs_fsize), fs->fs_frag) || 384 fs->fs_iblkno != fs->fs_cblkno + fs->fs_frag || 385 fs->fs_dblkno != fs->fs_iblkno + fs->fs_ipg / INOPF(fs) || 386 fs->fs_cgsize != fragroundup(fs, CGSIZE(fs))) 387 return (ENOENT); 388 if (fs->fs_csaddr != cgdmin(fs, 0) || 389 fs->fs_cssize != 390 fragroundup(fs, fs->fs_ncg * sizeof(struct csum)) || 391 fs->fs_dsize != fs->fs_size - fs->fs_sblkno - 392 fs->fs_ncg * (fs->fs_dblkno - fs->fs_sblkno) - 393 howmany(fs->fs_cssize, fs->fs_fsize) || 394 fs->fs_metaspace < 0 || fs->fs_metaspace > fs->fs_fpg / 2 || 395 fs->fs_minfree > 99) 396 return (ENOENT); 397 maxfilesize = fs->fs_bsize * UFS_NDADDR - 1; 398 for (sizepb = fs->fs_bsize, i = 0; i < UFS_NIADDR; i++) { 399 sizepb *= NINDIR(fs); 400 maxfilesize += sizepb; 401 } 402 if (fs->fs_maxfilesize != maxfilesize) 403 return (ENOENT); 404 /* 405 * These values have a tight interaction with each other that 406 * makes it hard to tightly bound them. So we can only check 407 * that they are within a broader possible range. 408 * 409 * Calculate minfpg, the minimum number of fragments that can be 410 * in a cylinder group. The value 12289 is calculated in newfs(8) 411 * when creating the smallest block size UFS version 1 filesystem 412 * (4096 block size) with no fragments (4096 fragment size). That 413 * number may be depressed even further for very small filesystems 414 * since newfs(8) strives to have at least four cylinder groups. 415 */ 416 minfpg = MIN(12289, fs->fs_size / 4); 417 if (fs->fs_ncg < 1 || fs->fs_ncg > (fs->fs_size / minfpg) + 1 || 418 fs->fs_fpg < minfpg || fs->fs_fpg > fs->fs_size || 419 fs->fs_ipg * fs->fs_ncg > (((int64_t)(1)) << 32) - INOPB(fs) || 420 fs->fs_ipg > fs->fs_fpg || fs->fs_size < 8 * fs->fs_frag) 421 return (ENOENT); 422 if (fs->fs_size <= (fs->fs_ncg - 1) * fs->fs_fpg || 423 fs->fs_size > fs->fs_ncg * fs->fs_fpg) 424 return (ENOENT); 425 /* 426 * Maxcontig sets the default for the maximum number of blocks 427 * that may be allocated sequentially. With file system clustering 428 * it is possible to allocate contiguous blocks up to the maximum 429 * transfer size permitted by the controller or buffering. 430 */ 431 if (fs->fs_maxcontig < 1 || 432 fs->fs_maxcontig > MAX(1, maxphys / fs->fs_bsize)) 433 return (ENOENT); 434 if (fs->fs_maxcontig < 0 || 435 (fs->fs_maxcontig == 0 && fs->fs_contigsumsize != 0) || 436 (fs->fs_maxcontig > 1 && 437 fs->fs_contigsumsize != MIN(fs->fs_maxcontig, FS_MAXCONTIG))) 438 return (ENOENT); 439 return (0); 440 } 441 442 /* 443 * Write a superblock to the devfd device from the memory pointed to by fs. 444 * Write out the superblock summary information if it is present. 445 * 446 * If the write is successful, zero is returned. Otherwise one of the 447 * following error values is returned: 448 * EIO: failed to write superblock. 449 * EIO: failed to write superblock summary information. 450 */ 451 int 452 ffs_sbput(void *devfd, struct fs *fs, off_t loc, 453 int (*writefunc)(void *devfd, off_t loc, void *buf, int size)) 454 { 455 int i, error, blks, size; 456 uint8_t *space; 457 458 /* 459 * If there is summary information, write it first, so if there 460 * is an error, the superblock will not be marked as clean. 461 */ 462 if (fs->fs_si != NULL && fs->fs_csp != NULL) { 463 blks = howmany(fs->fs_cssize, fs->fs_fsize); 464 space = (uint8_t *)fs->fs_csp; 465 for (i = 0; i < blks; i += fs->fs_frag) { 466 size = fs->fs_bsize; 467 if (i + fs->fs_frag > blks) 468 size = (blks - i) * fs->fs_fsize; 469 if ((error = (*writefunc)(devfd, 470 dbtob(fsbtodb(fs, fs->fs_csaddr + i)), 471 space, size)) != 0) 472 return (error); 473 space += size; 474 } 475 } 476 fs->fs_fmod = 0; 477 #ifndef _KERNEL 478 { 479 struct fs_summary_info *fs_si; 480 481 fs->fs_time = time(NULL); 482 /* Clear the pointers for the duration of writing. */ 483 fs_si = fs->fs_si; 484 fs->fs_si = NULL; 485 fs->fs_ckhash = ffs_calc_sbhash(fs); 486 error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize); 487 fs->fs_si = fs_si; 488 } 489 #else /* _KERNEL */ 490 fs->fs_time = time_second; 491 fs->fs_ckhash = ffs_calc_sbhash(fs); 492 error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize); 493 #endif /* _KERNEL */ 494 return (error); 495 } 496 497 /* 498 * Calculate the check-hash for a superblock. 499 */ 500 uint32_t 501 ffs_calc_sbhash(struct fs *fs) 502 { 503 uint32_t ckhash, save_ckhash; 504 505 /* 506 * A filesystem that was using a superblock ckhash may be moved 507 * to an older kernel that does not support ckhashes. The 508 * older kernel will clear the FS_METACKHASH flag indicating 509 * that it does not update hashes. When the disk is moved back 510 * to a kernel capable of ckhashes it disables them on mount: 511 * 512 * if ((fs->fs_flags & FS_METACKHASH) == 0) 513 * fs->fs_metackhash = 0; 514 * 515 * This leaves (fs->fs_metackhash & CK_SUPERBLOCK) == 0) with an 516 * old stale value in the fs->fs_ckhash field. Thus the need to 517 * just accept what is there. 518 */ 519 if ((fs->fs_metackhash & CK_SUPERBLOCK) == 0) 520 return (fs->fs_ckhash); 521 522 save_ckhash = fs->fs_ckhash; 523 fs->fs_ckhash = 0; 524 /* 525 * If newly read from disk, the caller is responsible for 526 * verifying that fs->fs_sbsize <= SBLOCKSIZE. 527 */ 528 ckhash = calculate_crc32c(~0L, (void *)fs, fs->fs_sbsize); 529 fs->fs_ckhash = save_ckhash; 530 return (ckhash); 531 } 532 533 /* 534 * Update the frsum fields to reflect addition or deletion 535 * of some frags. 536 */ 537 void 538 ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt) 539 { 540 int inblk; 541 int field, subfield; 542 int siz, pos; 543 544 inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1; 545 fragmap <<= 1; 546 for (siz = 1; siz < fs->fs_frag; siz++) { 547 if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0) 548 continue; 549 field = around[siz]; 550 subfield = inside[siz]; 551 for (pos = siz; pos <= fs->fs_frag; pos++) { 552 if ((fragmap & field) == subfield) { 553 fraglist[siz] += cnt; 554 pos += siz; 555 field <<= siz; 556 subfield <<= siz; 557 } 558 field <<= 1; 559 subfield <<= 1; 560 } 561 } 562 } 563 564 /* 565 * block operations 566 * 567 * check if a block is available 568 */ 569 int 570 ffs_isblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h) 571 { 572 unsigned char mask; 573 574 switch ((int)fs->fs_frag) { 575 case 8: 576 return (cp[h] == 0xff); 577 case 4: 578 mask = 0x0f << ((h & 0x1) << 2); 579 return ((cp[h >> 1] & mask) == mask); 580 case 2: 581 mask = 0x03 << ((h & 0x3) << 1); 582 return ((cp[h >> 2] & mask) == mask); 583 case 1: 584 mask = 0x01 << (h & 0x7); 585 return ((cp[h >> 3] & mask) == mask); 586 default: 587 #ifdef _KERNEL 588 panic("ffs_isblock"); 589 #endif 590 break; 591 } 592 return (0); 593 } 594 595 /* 596 * check if a block is free 597 */ 598 int 599 ffs_isfreeblock(struct fs *fs, u_char *cp, ufs1_daddr_t h) 600 { 601 602 switch ((int)fs->fs_frag) { 603 case 8: 604 return (cp[h] == 0); 605 case 4: 606 return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0); 607 case 2: 608 return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0); 609 case 1: 610 return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0); 611 default: 612 #ifdef _KERNEL 613 panic("ffs_isfreeblock"); 614 #endif 615 break; 616 } 617 return (0); 618 } 619 620 /* 621 * take a block out of the map 622 */ 623 void 624 ffs_clrblock(struct fs *fs, u_char *cp, ufs1_daddr_t h) 625 { 626 627 switch ((int)fs->fs_frag) { 628 case 8: 629 cp[h] = 0; 630 return; 631 case 4: 632 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2)); 633 return; 634 case 2: 635 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1)); 636 return; 637 case 1: 638 cp[h >> 3] &= ~(0x01 << (h & 0x7)); 639 return; 640 default: 641 #ifdef _KERNEL 642 panic("ffs_clrblock"); 643 #endif 644 break; 645 } 646 } 647 648 /* 649 * put a block into the map 650 */ 651 void 652 ffs_setblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h) 653 { 654 655 switch ((int)fs->fs_frag) { 656 case 8: 657 cp[h] = 0xff; 658 return; 659 case 4: 660 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2)); 661 return; 662 case 2: 663 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1)); 664 return; 665 case 1: 666 cp[h >> 3] |= (0x01 << (h & 0x7)); 667 return; 668 default: 669 #ifdef _KERNEL 670 panic("ffs_setblock"); 671 #endif 672 break; 673 } 674 } 675 676 /* 677 * Update the cluster map because of an allocation or free. 678 * 679 * Cnt == 1 means free; cnt == -1 means allocating. 680 */ 681 void 682 ffs_clusteracct(struct fs *fs, struct cg *cgp, ufs1_daddr_t blkno, int cnt) 683 { 684 int32_t *sump; 685 int32_t *lp; 686 u_char *freemapp, *mapp; 687 int i, start, end, forw, back, map; 688 u_int bit; 689 690 if (fs->fs_contigsumsize <= 0) 691 return; 692 freemapp = cg_clustersfree(cgp); 693 sump = cg_clustersum(cgp); 694 /* 695 * Allocate or clear the actual block. 696 */ 697 if (cnt > 0) 698 setbit(freemapp, blkno); 699 else 700 clrbit(freemapp, blkno); 701 /* 702 * Find the size of the cluster going forward. 703 */ 704 start = blkno + 1; 705 end = start + fs->fs_contigsumsize; 706 if (end >= cgp->cg_nclusterblks) 707 end = cgp->cg_nclusterblks; 708 mapp = &freemapp[start / NBBY]; 709 map = *mapp++; 710 bit = 1U << (start % NBBY); 711 for (i = start; i < end; i++) { 712 if ((map & bit) == 0) 713 break; 714 if ((i & (NBBY - 1)) != (NBBY - 1)) { 715 bit <<= 1; 716 } else { 717 map = *mapp++; 718 bit = 1; 719 } 720 } 721 forw = i - start; 722 /* 723 * Find the size of the cluster going backward. 724 */ 725 start = blkno - 1; 726 end = start - fs->fs_contigsumsize; 727 if (end < 0) 728 end = -1; 729 mapp = &freemapp[start / NBBY]; 730 map = *mapp--; 731 bit = 1U << (start % NBBY); 732 for (i = start; i > end; i--) { 733 if ((map & bit) == 0) 734 break; 735 if ((i & (NBBY - 1)) != 0) { 736 bit >>= 1; 737 } else { 738 map = *mapp--; 739 bit = 1U << (NBBY - 1); 740 } 741 } 742 back = start - i; 743 /* 744 * Account for old cluster and the possibly new forward and 745 * back clusters. 746 */ 747 i = back + forw + 1; 748 if (i > fs->fs_contigsumsize) 749 i = fs->fs_contigsumsize; 750 sump[i] += cnt; 751 if (back > 0) 752 sump[back] -= cnt; 753 if (forw > 0) 754 sump[forw] -= cnt; 755 /* 756 * Update cluster summary information. 757 */ 758 lp = &sump[fs->fs_contigsumsize]; 759 for (i = fs->fs_contigsumsize; i > 0; i--) 760 if (*lp-- > 0) 761 break; 762 fs->fs_maxcluster[cgp->cg_cgx] = i; 763 } 764