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 32 #include <sys/param.h> 33 #include <sys/endian.h> 34 #include <sys/limits.h> 35 36 #ifndef _KERNEL 37 #include <stdbool.h> 38 #include <stdio.h> 39 #include <string.h> 40 #include <stdlib.h> 41 #include <time.h> 42 #include <sys/errno.h> 43 #include <ufs/ufs/dinode.h> 44 #include <ufs/ffs/fs.h> 45 46 uint32_t calculate_crc32c(uint32_t, const void *, size_t); 47 uint32_t ffs_calc_sbhash(struct fs *); 48 struct malloc_type; 49 #define UFS_MALLOC(size, type, flags) malloc(size) 50 #define UFS_FREE(ptr, type) free(ptr) 51 #define maxphys MAXPHYS 52 53 #else /* _KERNEL */ 54 #include <sys/systm.h> 55 #include <sys/gsb_crc32.h> 56 #include <sys/lock.h> 57 #include <sys/malloc.h> 58 #include <sys/mount.h> 59 #include <sys/vnode.h> 60 #include <sys/bio.h> 61 #include <sys/buf.h> 62 #include <sys/ucred.h> 63 #include <sys/sysctl.h> 64 65 #include <ufs/ufs/quota.h> 66 #include <ufs/ufs/inode.h> 67 #include <ufs/ufs/extattr.h> 68 #include <ufs/ufs/ufsmount.h> 69 #include <ufs/ufs/ufs_extern.h> 70 #include <ufs/ffs/ffs_extern.h> 71 #include <ufs/ffs/fs.h> 72 73 #define UFS_MALLOC(size, type, flags) malloc(size, type, flags) 74 #define UFS_FREE(ptr, type) free(ptr, type) 75 76 #endif /* _KERNEL */ 77 78 /* 79 * Verify an inode check-hash. 80 */ 81 int 82 ffs_verify_dinode_ckhash(struct fs *fs, struct ufs2_dinode *dip) 83 { 84 uint32_t ckhash, save_ckhash; 85 86 /* 87 * Return success if unallocated or we are not doing inode check-hash. 88 */ 89 if (dip->di_mode == 0 || (fs->fs_metackhash & CK_INODE) == 0) 90 return (0); 91 /* 92 * Exclude di_ckhash from the crc32 calculation, e.g., always use 93 * a check-hash value of zero when calculating the check-hash. 94 */ 95 save_ckhash = dip->di_ckhash; 96 dip->di_ckhash = 0; 97 ckhash = calculate_crc32c(~0L, (void *)dip, sizeof(*dip)); 98 dip->di_ckhash = save_ckhash; 99 if (save_ckhash == ckhash) 100 return (0); 101 return (EINVAL); 102 } 103 104 /* 105 * Update an inode check-hash. 106 */ 107 void 108 ffs_update_dinode_ckhash(struct fs *fs, struct ufs2_dinode *dip) 109 { 110 111 if (dip->di_mode == 0 || (fs->fs_metackhash & CK_INODE) == 0) 112 return; 113 /* 114 * Exclude old di_ckhash from the crc32 calculation, e.g., always use 115 * a check-hash value of zero when calculating the new check-hash. 116 */ 117 dip->di_ckhash = 0; 118 dip->di_ckhash = calculate_crc32c(~0L, (void *)dip, sizeof(*dip)); 119 } 120 121 /* 122 * These are the low-level functions that actually read and write 123 * the superblock and its associated data. 124 */ 125 static off_t sblock_try[] = SBLOCKSEARCH; 126 static int readsuper(void *, struct fs **, off_t, int, 127 int (*)(void *, off_t, void **, int)); 128 static void ffs_oldfscompat_read(struct fs *, ufs2_daddr_t); 129 static int validate_sblock(struct fs *, int); 130 131 /* 132 * Read a superblock from the devfd device. 133 * 134 * If an alternate superblock is specified, it is read. Otherwise the 135 * set of locations given in the SBLOCKSEARCH list is searched for a 136 * superblock. Memory is allocated for the superblock by the readfunc and 137 * is returned. If filltype is non-NULL, additional memory is allocated 138 * of type filltype and filled in with the superblock summary information. 139 * All memory is freed when any error is returned. 140 * 141 * If a superblock is found, zero is returned. Otherwise one of the 142 * following error values is returned: 143 * EIO: non-existent or truncated superblock. 144 * EIO: error reading summary information. 145 * ENOENT: no usable known superblock found. 146 * EILSEQ: filesystem with wrong byte order 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 sblock, int flags, 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 (sblock != UFS_STDSB) { 167 if ((error = readsuper(devfd, &fs, sblock, 168 flags | UFS_ALTSBLK, 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], 176 flags, readfunc)) == 0) { 177 if ((flags & UFS_NOCSUM) != 0) { 178 *fsp = fs; 179 return (0); 180 } 181 break; 182 } 183 if (fs != NULL) { 184 UFS_FREE(fs, filltype); 185 fs = NULL; 186 } 187 if (error == ENOENT) 188 continue; 189 return (error); 190 } 191 if (sblock_try[i] == -1) 192 return (ENOENT); 193 } 194 /* 195 * Read in the superblock summary information. 196 */ 197 size = fs->fs_cssize; 198 blks = howmany(size, fs->fs_fsize); 199 if (fs->fs_contigsumsize > 0) 200 size += fs->fs_ncg * sizeof(int32_t); 201 size += fs->fs_ncg * sizeof(uint8_t); 202 if ((fs_si = UFS_MALLOC(sizeof(*fs_si), filltype, M_NOWAIT)) == NULL) { 203 UFS_FREE(fs, filltype); 204 return (ENOMEM); 205 } 206 bzero(fs_si, sizeof(*fs_si)); 207 fs->fs_si = fs_si; 208 if ((space = UFS_MALLOC(size, filltype, M_NOWAIT)) == NULL) { 209 UFS_FREE(fs->fs_si, filltype); 210 UFS_FREE(fs, filltype); 211 return (ENOMEM); 212 } 213 fs->fs_csp = (struct csum *)space; 214 for (i = 0; i < blks; i += fs->fs_frag) { 215 size = fs->fs_bsize; 216 if (i + fs->fs_frag > blks) 217 size = (blks - i) * fs->fs_fsize; 218 buf = NULL; 219 error = (*readfunc)(devfd, 220 dbtob(fsbtodb(fs, fs->fs_csaddr + i)), (void **)&buf, size); 221 if (error) { 222 if (buf != NULL) 223 UFS_FREE(buf, filltype); 224 UFS_FREE(fs->fs_csp, filltype); 225 UFS_FREE(fs->fs_si, filltype); 226 UFS_FREE(fs, filltype); 227 return (error); 228 } 229 memcpy(space, buf, size); 230 UFS_FREE(buf, filltype); 231 space += size; 232 } 233 if (fs->fs_contigsumsize > 0) { 234 fs->fs_maxcluster = lp = (int32_t *)space; 235 for (i = 0; i < fs->fs_ncg; i++) 236 *lp++ = fs->fs_contigsumsize; 237 space = (uint8_t *)lp; 238 } 239 size = fs->fs_ncg * sizeof(uint8_t); 240 fs->fs_contigdirs = (uint8_t *)space; 241 bzero(fs->fs_contigdirs, size); 242 *fsp = fs; 243 return (0); 244 } 245 246 /* 247 * Try to read a superblock from the location specified by sblockloc. 248 * Return zero on success or an errno on failure. 249 */ 250 static int 251 readsuper(void *devfd, struct fs **fsp, off_t sblockloc, int flags, 252 int (*readfunc)(void *devfd, off_t loc, void **bufp, int size)) 253 { 254 struct fs *fs; 255 int error, res; 256 uint32_t ckhash; 257 258 error = (*readfunc)(devfd, sblockloc, (void **)fsp, SBLOCKSIZE); 259 if (error != 0) 260 return (error); 261 fs = *fsp; 262 if (fs->fs_magic == FS_BAD_MAGIC) 263 return (EINVAL); 264 /* 265 * For UFS1 with a 65536 block size, the first backup superblock 266 * is at the same location as the UFS2 superblock. Since SBLOCK_UFS2 267 * is the first location checked, the first backup is the superblock 268 * that will be accessed. Here we fail the lookup so that we can 269 * retry with the correct location for the UFS1 superblock. 270 */ 271 if (fs->fs_magic == FS_UFS1_MAGIC && (flags & UFS_ALTSBLK) == 0 && 272 fs->fs_bsize == SBLOCK_UFS2 && sblockloc == SBLOCK_UFS2) 273 return (ENOENT); 274 ffs_oldfscompat_read(fs, sblockloc); 275 if ((error = validate_sblock(fs, flags)) > 0) 276 return (error); 277 /* 278 * If the filesystem has been run on a kernel without 279 * metadata check hashes, disable them. 280 */ 281 if ((fs->fs_flags & FS_METACKHASH) == 0) 282 fs->fs_metackhash = 0; 283 /* 284 * Clear any check-hashes that are not maintained 285 * by this kernel. Also clear any unsupported flags. 286 */ 287 fs->fs_metackhash &= CK_SUPPORTED; 288 fs->fs_flags &= FS_SUPPORTED; 289 if (fs->fs_ckhash != (ckhash = ffs_calc_sbhash(fs))) { 290 if ((flags & (UFS_NOMSG | UFS_NOHASHFAIL)) == 291 (UFS_NOMSG | UFS_NOHASHFAIL)) 292 return (0); 293 if ((flags & UFS_NOMSG) != 0) 294 return (EINTEGRITY); 295 #ifdef _KERNEL 296 res = uprintf("Superblock check-hash failed: recorded " 297 "check-hash 0x%x != computed check-hash 0x%x%s\n", 298 fs->fs_ckhash, ckhash, 299 (flags & UFS_NOHASHFAIL) != 0 ? " (Ignored)" : ""); 300 #else 301 res = 0; 302 #endif 303 /* 304 * Print check-hash failure if no controlling terminal 305 * in kernel or always if in user-mode (libufs). 306 */ 307 if (res == 0) 308 printf("Superblock check-hash failed: recorded " 309 "check-hash 0x%x != computed check-hash " 310 "0x%x%s\n", fs->fs_ckhash, ckhash, 311 (flags & UFS_NOHASHFAIL) ? " (Ignored)" : ""); 312 if ((flags & UFS_NOHASHFAIL) != 0) 313 return (0); 314 return (EINTEGRITY); 315 } 316 /* Have to set for old filesystems that predate this field */ 317 fs->fs_sblockactualloc = sblockloc; 318 /* Not yet any summary information */ 319 fs->fs_si = NULL; 320 return (0); 321 } 322 323 /* 324 * Sanity checks for loading old filesystem superblocks. 325 * See ffs_oldfscompat_write below for unwound actions. 326 * 327 * XXX - Parts get retired eventually. 328 * Unfortunately new bits get added. 329 */ 330 static void 331 ffs_oldfscompat_read(struct fs *fs, ufs2_daddr_t sblockloc) 332 { 333 uint64_t maxfilesize; 334 335 /* 336 * If not yet done, update fs_flags location and value of fs_sblockloc. 337 */ 338 if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) { 339 fs->fs_flags = fs->fs_old_flags; 340 fs->fs_old_flags |= FS_FLAGS_UPDATED; 341 fs->fs_sblockloc = sblockloc; 342 } 343 /* 344 * If not yet done, update UFS1 superblock with new wider fields. 345 */ 346 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) { 347 fs->fs_maxbsize = fs->fs_bsize; 348 fs->fs_time = fs->fs_old_time; 349 fs->fs_size = fs->fs_old_size; 350 fs->fs_dsize = fs->fs_old_dsize; 351 fs->fs_csaddr = fs->fs_old_csaddr; 352 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir; 353 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree; 354 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree; 355 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree; 356 } 357 if (fs->fs_magic == FS_UFS1_MAGIC && 358 fs->fs_old_inodefmt < FS_44INODEFMT) { 359 fs->fs_maxfilesize = ((uint64_t)1 << 31) - 1; 360 fs->fs_qbmask = ~fs->fs_bmask; 361 fs->fs_qfmask = ~fs->fs_fmask; 362 } 363 if (fs->fs_magic == FS_UFS1_MAGIC) { 364 fs->fs_save_maxfilesize = fs->fs_maxfilesize; 365 maxfilesize = (uint64_t)0x80000000 * fs->fs_bsize - 1; 366 if (fs->fs_maxfilesize > maxfilesize) 367 fs->fs_maxfilesize = maxfilesize; 368 } 369 /* Compatibility for old filesystems */ 370 if (fs->fs_avgfilesize <= 0) 371 fs->fs_avgfilesize = AVFILESIZ; 372 if (fs->fs_avgfpdir <= 0) 373 fs->fs_avgfpdir = AFPDIR; 374 } 375 376 /* 377 * Unwinding superblock updates for old filesystems. 378 * See ffs_oldfscompat_read above for details. 379 * 380 * XXX - Parts get retired eventually. 381 * Unfortunately new bits get added. 382 */ 383 void 384 ffs_oldfscompat_write(struct fs *fs) 385 { 386 387 /* 388 * Copy back UFS2 updated fields that UFS1 inspects. 389 */ 390 if (fs->fs_magic == FS_UFS1_MAGIC) { 391 fs->fs_old_time = fs->fs_time; 392 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir; 393 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree; 394 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree; 395 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree; 396 fs->fs_maxfilesize = fs->fs_save_maxfilesize; 397 } 398 } 399 400 /* 401 * Sanity checks for loading old filesystem inodes. 402 * 403 * XXX - Parts get retired eventually. 404 * Unfortunately new bits get added. 405 */ 406 static int prttimechgs = 0; 407 #ifdef _KERNEL 408 SYSCTL_NODE(_vfs, OID_AUTO, ffs, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 409 "FFS filesystem"); 410 411 SYSCTL_INT(_vfs_ffs, OID_AUTO, prttimechgs, CTLFLAG_RWTUN, &prttimechgs, 0, 412 "print UFS1 time changes made to inodes"); 413 #endif /* _KERNEL */ 414 bool 415 ffs_oldfscompat_inode_read(struct fs *fs, union dinodep dp, time_t now) 416 { 417 bool change; 418 419 change = false; 420 switch (fs->fs_magic) { 421 case FS_UFS2_MAGIC: 422 /* No changes for now */ 423 break; 424 425 case FS_UFS1_MAGIC: 426 /* 427 * With the change to unsigned time values in UFS1, times set 428 * before Jan 1, 1970 will appear to be in the future. Check 429 * for future times and set them to be the current time. 430 */ 431 if (dp.dp1->di_ctime > now) { 432 if (prttimechgs) 433 printf("ctime %ud changed to %ld\n", 434 dp.dp1->di_ctime, (long)now); 435 dp.dp1->di_ctime = now; 436 change = true; 437 } 438 if (dp.dp1->di_mtime > now) { 439 if (prttimechgs) 440 printf("mtime %ud changed to %ld\n", 441 dp.dp1->di_mtime, (long)now); 442 dp.dp1->di_mtime = now; 443 dp.dp1->di_ctime = now; 444 change = true; 445 } 446 if (dp.dp1->di_atime > now) { 447 if (prttimechgs) 448 printf("atime %ud changed to %ld\n", 449 dp.dp1->di_atime, (long)now); 450 dp.dp1->di_atime = now; 451 dp.dp1->di_ctime = now; 452 change = true; 453 } 454 break; 455 } 456 return (change); 457 } 458 459 /* 460 * Verify the filesystem values. 461 */ 462 #define ILOG2(num) (fls(num) - 1) 463 #ifdef STANDALONE_SMALL 464 #define MPRINT(...) do { } while (0) 465 #else 466 #define MPRINT(...) if (prtmsg) printf(__VA_ARGS__) 467 #endif 468 #define FCHK(lhs, op, rhs, fmt) \ 469 if (lhs op rhs) { \ 470 MPRINT("UFS%d superblock failed: %s (" #fmt ") %s %s (" \ 471 #fmt ")\n", fs->fs_magic == FS_UFS1_MAGIC ? 1 : 2, \ 472 #lhs, (intmax_t)lhs, #op, #rhs, (intmax_t)rhs); \ 473 if (error < 0) \ 474 return (ENOENT); \ 475 if (error == 0) \ 476 error = ENOENT; \ 477 } 478 #define WCHK(lhs, op, rhs, fmt) \ 479 if (lhs op rhs) { \ 480 MPRINT("UFS%d superblock failed: %s (" #fmt ") %s %s (" \ 481 #fmt ")%s\n", fs->fs_magic == FS_UFS1_MAGIC ? 1 : 2,\ 482 #lhs, (intmax_t)lhs, #op, #rhs, (intmax_t)rhs, wmsg);\ 483 if (error == 0) \ 484 error = warnerr; \ 485 if (warnerr == 0) \ 486 lhs = rhs; \ 487 } 488 #define FCHK2(lhs1, op1, rhs1, lhs2, op2, rhs2, fmt) \ 489 if (lhs1 op1 rhs1 && lhs2 op2 rhs2) { \ 490 MPRINT("UFS%d superblock failed: %s (" #fmt ") %s %s (" \ 491 #fmt ") && %s (" #fmt ") %s %s (" #fmt ")\n", \ 492 fs->fs_magic == FS_UFS1_MAGIC ? 1 : 2, #lhs1, \ 493 (intmax_t)lhs1, #op1, #rhs1, (intmax_t)rhs1, #lhs2, \ 494 (intmax_t)lhs2, #op2, #rhs2, (intmax_t)rhs2); \ 495 if (error < 0) \ 496 return (ENOENT); \ 497 if (error == 0) \ 498 error = ENOENT; \ 499 } 500 501 static int 502 validate_sblock(struct fs *fs, int flags) 503 { 504 uint64_t i, sectorsize; 505 uint64_t maxfilesize, sizepb; 506 int error, prtmsg, warnerr; 507 char *wmsg; 508 509 error = 0; 510 sectorsize = dbtob(1); 511 prtmsg = ((flags & UFS_NOMSG) == 0); 512 warnerr = (flags & UFS_NOWARNFAIL) == UFS_NOWARNFAIL ? 0 : ENOENT; 513 wmsg = warnerr ? "" : " (Ignored)"; 514 /* 515 * Check for endian mismatch between machine and filesystem. 516 */ 517 if (((fs->fs_magic != FS_UFS2_MAGIC) && 518 (bswap32(fs->fs_magic) == FS_UFS2_MAGIC)) || 519 ((fs->fs_magic != FS_UFS1_MAGIC) && 520 (bswap32(fs->fs_magic) == FS_UFS1_MAGIC))) { 521 MPRINT("UFS superblock failed due to endian mismatch " 522 "between machine and filesystem\n"); 523 return(EILSEQ); 524 } 525 /* 526 * If just validating for recovery, then do just the minimal 527 * checks needed for the superblock fields needed to find 528 * alternate superblocks. 529 */ 530 if ((flags & UFS_FSRONLY) == UFS_FSRONLY && 531 (fs->fs_magic == FS_UFS1_MAGIC || fs->fs_magic == FS_UFS2_MAGIC)) { 532 error = -1; /* fail on first error */ 533 if (fs->fs_magic == FS_UFS2_MAGIC) { 534 FCHK(fs->fs_sblockloc, !=, SBLOCK_UFS2, %#jx); 535 } else if (fs->fs_magic == FS_UFS1_MAGIC) { 536 FCHK(fs->fs_sblockloc, <, 0, %jd); 537 FCHK(fs->fs_sblockloc, >, SBLOCK_UFS1, %jd); 538 } 539 FCHK(fs->fs_frag, <, 1, %jd); 540 FCHK(fs->fs_frag, >, MAXFRAG, %jd); 541 FCHK(fs->fs_bsize, <, MINBSIZE, %jd); 542 FCHK(fs->fs_bsize, >, MAXBSIZE, %jd); 543 FCHK(fs->fs_bsize, <, roundup(sizeof(struct fs), DEV_BSIZE), 544 %jd); 545 FCHK(fs->fs_fsize, <, sectorsize, %jd); 546 FCHK(fs->fs_fsize * fs->fs_frag, !=, fs->fs_bsize, %jd); 547 FCHK(powerof2(fs->fs_fsize), ==, 0, %jd); 548 FCHK(fs->fs_sbsize, >, SBLOCKSIZE, %jd); 549 FCHK(fs->fs_sbsize, <, (signed)sizeof(struct fs), %jd); 550 FCHK(fs->fs_sbsize % sectorsize, !=, 0, %jd); 551 FCHK(fs->fs_fpg, <, 3 * fs->fs_frag, %jd); 552 FCHK(fs->fs_ncg, <, 1, %jd); 553 FCHK(fs->fs_fsbtodb, !=, ILOG2(fs->fs_fsize / sectorsize), %jd); 554 FCHK(fs->fs_old_cgoffset, <, 0, %jd); 555 FCHK2(fs->fs_old_cgoffset, >, 0, ~fs->fs_old_cgmask, <, 0, %jd); 556 FCHK(fs->fs_old_cgoffset * (~fs->fs_old_cgmask), >, fs->fs_fpg, 557 %jd); 558 FCHK(fs->fs_sblkno, !=, roundup( 559 howmany(fs->fs_sblockloc + SBLOCKSIZE, fs->fs_fsize), 560 fs->fs_frag), %jd); 561 FCHK(CGSIZE(fs), >, fs->fs_bsize, %jd); 562 /* Only need to validate these if reading in csum data */ 563 if ((flags & UFS_NOCSUM) != 0) 564 return (error); 565 FCHK((uint64_t)fs->fs_ipg * fs->fs_ncg, >, 566 (((int64_t)(1)) << 32) - INOPB(fs), %jd); 567 FCHK(fs->fs_cstotal.cs_nifree, <, 0, %jd); 568 FCHK(fs->fs_cstotal.cs_nifree, >, 569 (uint64_t)fs->fs_ipg * fs->fs_ncg, %jd); 570 FCHK(fs->fs_cstotal.cs_ndir, >, 571 ((uint64_t)fs->fs_ipg * fs->fs_ncg) - 572 fs->fs_cstotal.cs_nifree, %jd); 573 FCHK(fs->fs_size, <, 8 * fs->fs_frag, %jd); 574 FCHK(fs->fs_size, <=, ((int64_t)fs->fs_ncg - 1) * fs->fs_fpg, 575 %jd); 576 FCHK(fs->fs_size, >, (int64_t)fs->fs_ncg * fs->fs_fpg, %jd); 577 FCHK(fs->fs_csaddr, <, 0, %jd); 578 FCHK(fs->fs_cssize, !=, 579 fragroundup(fs, fs->fs_ncg * sizeof(struct csum)), %jd); 580 FCHK(fs->fs_csaddr + howmany(fs->fs_cssize, fs->fs_fsize), >, 581 fs->fs_size, %jd); 582 FCHK(fs->fs_csaddr, <, cgdmin(fs, dtog(fs, fs->fs_csaddr)), 583 %jd); 584 FCHK(dtog(fs, fs->fs_csaddr + howmany(fs->fs_cssize, 585 fs->fs_fsize)), >, dtog(fs, fs->fs_csaddr), %jd); 586 return (error); 587 } 588 if (fs->fs_magic == FS_UFS2_MAGIC) { 589 if ((flags & UFS_ALTSBLK) == 0) 590 FCHK2(fs->fs_sblockactualloc, !=, SBLOCK_UFS2, 591 fs->fs_sblockactualloc, !=, 0, %jd); 592 FCHK(fs->fs_sblockloc, !=, SBLOCK_UFS2, %#jx); 593 FCHK(fs->fs_maxsymlinklen, !=, ((UFS_NDADDR + UFS_NIADDR) * 594 sizeof(ufs2_daddr_t)), %jd); 595 FCHK(fs->fs_nindir, !=, fs->fs_bsize / sizeof(ufs2_daddr_t), 596 %jd); 597 FCHK(fs->fs_inopb, !=, 598 fs->fs_bsize / sizeof(struct ufs2_dinode), %jd); 599 } else if (fs->fs_magic == FS_UFS1_MAGIC) { 600 if ((flags & UFS_ALTSBLK) == 0) 601 FCHK(fs->fs_sblockactualloc, >, SBLOCK_UFS1, %jd); 602 FCHK(fs->fs_sblockloc, <, 0, %jd); 603 FCHK(fs->fs_sblockloc, >, SBLOCK_UFS1, %jd); 604 FCHK(fs->fs_nindir, !=, fs->fs_bsize / sizeof(ufs1_daddr_t), 605 %jd); 606 FCHK(fs->fs_inopb, !=, 607 fs->fs_bsize / sizeof(struct ufs1_dinode), %jd); 608 FCHK(fs->fs_maxsymlinklen, !=, ((UFS_NDADDR + UFS_NIADDR) * 609 sizeof(ufs1_daddr_t)), %jd); 610 WCHK(fs->fs_old_inodefmt, !=, FS_44INODEFMT, %jd); 611 WCHK(fs->fs_old_rotdelay, !=, 0, %jd); 612 WCHK(fs->fs_old_rps, !=, 60, %jd); 613 WCHK(fs->fs_old_nspf, !=, fs->fs_fsize / sectorsize, %jd); 614 WCHK(fs->fs_old_interleave, !=, 1, %jd); 615 WCHK(fs->fs_old_trackskew, !=, 0, %jd); 616 WCHK(fs->fs_old_cpc, !=, 0, %jd); 617 WCHK(fs->fs_old_postblformat, !=, 1, %jd); 618 FCHK(fs->fs_old_nrpos, !=, 1, %jd); 619 WCHK(fs->fs_old_nsect, !=, fs->fs_old_spc, %jd); 620 WCHK(fs->fs_old_npsect, !=, fs->fs_old_spc, %jd); 621 } else { 622 /* Bad magic number, so assume not a superblock */ 623 return (ENOENT); 624 } 625 FCHK(fs->fs_bsize, <, MINBSIZE, %jd); 626 FCHK(fs->fs_bsize, >, MAXBSIZE, %jd); 627 FCHK(fs->fs_bsize, <, roundup(sizeof(struct fs), DEV_BSIZE), %jd); 628 FCHK(powerof2(fs->fs_bsize), ==, 0, %jd); 629 FCHK(fs->fs_frag, <, 1, %jd); 630 FCHK(fs->fs_frag, >, MAXFRAG, %jd); 631 FCHK(fs->fs_frag, !=, numfrags(fs, fs->fs_bsize), %jd); 632 FCHK(fs->fs_fsize, <, sectorsize, %jd); 633 FCHK(fs->fs_fsize * fs->fs_frag, !=, fs->fs_bsize, %jd); 634 FCHK(powerof2(fs->fs_fsize), ==, 0, %jd); 635 FCHK(fs->fs_fpg, <, 3 * fs->fs_frag, %jd); 636 FCHK(fs->fs_ncg, <, 1, %jd); 637 FCHK(fs->fs_ipg, <, fs->fs_inopb, %jd); 638 FCHK((uint64_t)fs->fs_ipg * fs->fs_ncg, >, 639 (((int64_t)(1)) << 32) - INOPB(fs), %jd); 640 FCHK(fs->fs_cstotal.cs_nifree, <, 0, %jd); 641 FCHK(fs->fs_cstotal.cs_nifree, >, (uint64_t)fs->fs_ipg * fs->fs_ncg, 642 %jd); 643 FCHK(fs->fs_cstotal.cs_ndir, <, 0, %jd); 644 FCHK(fs->fs_cstotal.cs_ndir, >, 645 ((uint64_t)fs->fs_ipg * fs->fs_ncg) - fs->fs_cstotal.cs_nifree, 646 %jd); 647 FCHK(fs->fs_sbsize, >, SBLOCKSIZE, %jd); 648 FCHK(fs->fs_sbsize, <, (signed)sizeof(struct fs), %jd); 649 /* fix for misconfigured filesystems */ 650 if (fs->fs_maxbsize == 0) 651 fs->fs_maxbsize = fs->fs_bsize; 652 FCHK(fs->fs_maxbsize, <, fs->fs_bsize, %jd); 653 FCHK(powerof2(fs->fs_maxbsize), ==, 0, %jd); 654 FCHK(fs->fs_maxbsize, >, FS_MAXCONTIG * fs->fs_bsize, %jd); 655 FCHK(fs->fs_bmask, !=, ~(fs->fs_bsize - 1), %#jx); 656 FCHK(fs->fs_fmask, !=, ~(fs->fs_fsize - 1), %#jx); 657 FCHK(fs->fs_qbmask, !=, ~fs->fs_bmask, %#jx); 658 FCHK(fs->fs_qfmask, !=, ~fs->fs_fmask, %#jx); 659 FCHK(fs->fs_bshift, !=, ILOG2(fs->fs_bsize), %jd); 660 FCHK(fs->fs_fshift, !=, ILOG2(fs->fs_fsize), %jd); 661 FCHK(fs->fs_fragshift, !=, ILOG2(fs->fs_frag), %jd); 662 FCHK(fs->fs_fsbtodb, !=, ILOG2(fs->fs_fsize / sectorsize), %jd); 663 FCHK(fs->fs_old_cgoffset, <, 0, %jd); 664 FCHK2(fs->fs_old_cgoffset, >, 0, ~fs->fs_old_cgmask, <, 0, %jd); 665 FCHK(fs->fs_old_cgoffset * (~fs->fs_old_cgmask), >, fs->fs_fpg, %jd); 666 FCHK(CGSIZE(fs), >, fs->fs_bsize, %jd); 667 /* 668 * If anything has failed up to this point, it is usafe to proceed 669 * as checks below may divide by zero or make other fatal calculations. 670 * So if we have any errors at this point, give up. 671 */ 672 if (error) 673 return (error); 674 FCHK(fs->fs_sbsize % sectorsize, !=, 0, %jd); 675 FCHK(fs->fs_ipg % fs->fs_inopb, !=, 0, %jd); 676 FCHK(fs->fs_sblkno, !=, roundup( 677 howmany(fs->fs_sblockloc + SBLOCKSIZE, fs->fs_fsize), 678 fs->fs_frag), %jd); 679 FCHK(fs->fs_cblkno, !=, fs->fs_sblkno + 680 roundup(howmany(SBLOCKSIZE, fs->fs_fsize), fs->fs_frag), %jd); 681 FCHK(fs->fs_iblkno, !=, fs->fs_cblkno + fs->fs_frag, %jd); 682 FCHK(fs->fs_dblkno, !=, fs->fs_iblkno + fs->fs_ipg / INOPF(fs), %jd); 683 FCHK(fs->fs_cgsize, >, fs->fs_bsize, %jd); 684 FCHK(fs->fs_cgsize, <, fs->fs_fsize, %jd); 685 FCHK(fs->fs_cgsize % fs->fs_fsize, !=, 0, %jd); 686 /* 687 * This test is valid, however older versions of growfs failed 688 * to correctly update fs_dsize so will fail this test. Thus we 689 * exclude it from the requirements. 690 */ 691 #ifdef notdef 692 WCHK(fs->fs_dsize, !=, fs->fs_size - fs->fs_sblkno - 693 fs->fs_ncg * (fs->fs_dblkno - fs->fs_sblkno) - 694 howmany(fs->fs_cssize, fs->fs_fsize), %jd); 695 #endif 696 WCHK(fs->fs_metaspace, <, 0, %jd); 697 WCHK(fs->fs_metaspace, >, fs->fs_fpg / 2, %jd); 698 WCHK(fs->fs_minfree, >, 99, %jd%%); 699 maxfilesize = fs->fs_bsize * UFS_NDADDR - 1; 700 for (sizepb = fs->fs_bsize, i = 0; i < UFS_NIADDR; i++) { 701 sizepb *= NINDIR(fs); 702 maxfilesize += sizepb; 703 } 704 WCHK(fs->fs_maxfilesize, >, maxfilesize, %jd); 705 /* 706 * These values have a tight interaction with each other that 707 * makes it hard to tightly bound them. So we can only check 708 * that they are within a broader possible range. 709 * 710 * The size cannot always be accurately determined, but ensure 711 * that it is consistent with the number of cylinder groups (fs_ncg) 712 * and the number of fragments per cylinder group (fs_fpg). Ensure 713 * that the summary information size is correct and that it starts 714 * and ends in the data area of the same cylinder group. 715 */ 716 FCHK(fs->fs_size, <, 8 * fs->fs_frag, %jd); 717 FCHK(fs->fs_size, <=, ((int64_t)fs->fs_ncg - 1) * fs->fs_fpg, %jd); 718 FCHK(fs->fs_size, >, (int64_t)fs->fs_ncg * fs->fs_fpg, %jd); 719 /* 720 * If we are not requested to read in the csum data stop here 721 * as the correctness of the remaining values is only important 722 * to bound the space needed to be allocated to hold the csum data. 723 */ 724 if ((flags & UFS_NOCSUM) != 0) 725 return (error); 726 FCHK(fs->fs_csaddr, <, 0, %jd); 727 FCHK(fs->fs_cssize, !=, 728 fragroundup(fs, fs->fs_ncg * sizeof(struct csum)), %jd); 729 FCHK(fs->fs_csaddr + howmany(fs->fs_cssize, fs->fs_fsize), >, 730 fs->fs_size, %jd); 731 FCHK(fs->fs_csaddr, <, cgdmin(fs, dtog(fs, fs->fs_csaddr)), %jd); 732 FCHK(dtog(fs, fs->fs_csaddr + howmany(fs->fs_cssize, fs->fs_fsize)), >, 733 dtog(fs, fs->fs_csaddr), %jd); 734 /* 735 * With file system clustering it is possible to allocate 736 * many contiguous blocks. The kernel variable maxphys defines 737 * the maximum transfer size permitted by the controller and/or 738 * buffering. The fs_maxcontig parameter controls the maximum 739 * number of blocks that the filesystem will read or write 740 * in a single transfer. It is calculated when the filesystem 741 * is created as maxphys / fs_bsize. The loader uses a maxphys 742 * of 128K even when running on a system that supports larger 743 * values. If the filesystem was built on a system that supports 744 * a larger maxphys (1M is typical) it will have configured 745 * fs_maxcontig for that larger system. So we bound the upper 746 * allowable limit for fs_maxconfig to be able to at least 747 * work with a 1M maxphys on the smallest block size filesystem: 748 * 1M / 4096 == 256. There is no harm in allowing the mounting of 749 * filesystems that make larger than maxphys I/O requests because 750 * those (mostly 32-bit machines) can (very slowly) handle I/O 751 * requests that exceed maxphys. 752 */ 753 WCHK(fs->fs_maxcontig, <, 0, %jd); 754 WCHK(fs->fs_maxcontig, >, MAX(256, maxphys / fs->fs_bsize), %jd); 755 FCHK2(fs->fs_maxcontig, ==, 0, fs->fs_contigsumsize, !=, 0, %jd); 756 FCHK2(fs->fs_maxcontig, >, 1, fs->fs_contigsumsize, !=, 757 MIN(fs->fs_maxcontig, FS_MAXCONTIG), %jd); 758 return (error); 759 } 760 761 /* 762 * Make an extensive search to find a superblock. If the superblock 763 * in the standard place cannot be used, try looking for one of the 764 * backup superblocks. 765 * 766 * Flags are made up of the following or'ed together options: 767 * 768 * UFS_NOMSG indicates that superblock inconsistency error messages 769 * should not be printed. 770 * 771 * UFS_NOCSUM causes only the superblock itself to be returned, but does 772 * not read in any auxillary data structures like the cylinder group 773 * summary information. 774 */ 775 int 776 ffs_sbsearch(void *devfd, struct fs **fsp, int reqflags, 777 struct malloc_type *filltype, 778 int (*readfunc)(void *devfd, off_t loc, void **bufp, int size)) 779 { 780 struct fsrecovery *fsr; 781 struct fs *protofs; 782 void *fsrbuf; 783 char *cp; 784 long nocsum, flags, msg, cg; 785 off_t sblk, secsize; 786 int error; 787 788 msg = (reqflags & UFS_NOMSG) == 0; 789 nocsum = reqflags & UFS_NOCSUM; 790 /* 791 * Try normal superblock read and return it if it works. 792 * 793 * Suppress messages if it fails until we find out if 794 * failure can be avoided. 795 */ 796 flags = UFS_NOMSG | nocsum; 797 error = ffs_sbget(devfd, fsp, UFS_STDSB, flags, filltype, readfunc); 798 /* 799 * If successful or endian error, no need to try further. 800 */ 801 if (error == 0 || error == EILSEQ) { 802 if (msg && error == EILSEQ) 803 printf("UFS superblock failed due to endian mismatch " 804 "between machine and filesystem\n"); 805 return (error); 806 } 807 /* 808 * First try: ignoring hash failures. 809 */ 810 flags |= UFS_NOHASHFAIL; 811 if (msg) 812 flags &= ~UFS_NOMSG; 813 if (ffs_sbget(devfd, fsp, UFS_STDSB, flags, filltype, readfunc) == 0) 814 return (0); 815 /* 816 * Next up is to check if fields of the superblock that are 817 * needed to find backup superblocks are usable. 818 */ 819 if (msg) 820 printf("Attempted recovery for standard superblock: failed\n"); 821 flags = UFS_FSRONLY | UFS_NOHASHFAIL | UFS_NOCSUM | UFS_NOMSG; 822 if (ffs_sbget(devfd, &protofs, UFS_STDSB, flags, filltype, 823 readfunc) == 0) { 824 if (msg) 825 printf("Attempt extraction of recovery data from " 826 "standard superblock.\n"); 827 } else { 828 /* 829 * Final desperation is to see if alternate superblock 830 * parameters have been saved in the boot area. 831 */ 832 if (msg) 833 printf("Attempted extraction of recovery data from " 834 "standard superblock: failed\nAttempt to find " 835 "boot zone recovery data.\n"); 836 /* 837 * Look to see if recovery information has been saved. 838 * If so we can generate a prototype superblock based 839 * on that information. 840 * 841 * We need fragments-per-group, number of cylinder groups, 842 * location of the superblock within the cylinder group, and 843 * the conversion from filesystem fragments to disk blocks. 844 * 845 * When building a UFS2 filesystem, newfs(8) stores these 846 * details at the end of the boot block area at the start 847 * of the filesystem partition. If they have been overwritten 848 * by a boot block, we fail. But usually they are there 849 * and we can use them. 850 * 851 * We could ask the underlying device for its sector size, 852 * but some devices lie. So we just try a plausible range. 853 */ 854 error = ENOENT; 855 fsrbuf = NULL; 856 for (secsize = dbtob(1); secsize <= SBLOCKSIZE; secsize *= 2) 857 if ((error = (*readfunc)(devfd, (SBLOCK_UFS2 - secsize), 858 &fsrbuf, secsize)) == 0) 859 break; 860 if (error != 0) 861 goto trynowarn; 862 cp = fsrbuf; /* type change to keep compiler happy */ 863 fsr = (struct fsrecovery *)&cp[secsize - sizeof *fsr]; 864 if (fsr->fsr_magic != FS_UFS2_MAGIC || 865 (protofs = UFS_MALLOC(SBLOCKSIZE, filltype, M_NOWAIT)) 866 == NULL) { 867 UFS_FREE(fsrbuf, filltype); 868 goto trynowarn; 869 } 870 memset(protofs, 0, sizeof(struct fs)); 871 protofs->fs_fpg = fsr->fsr_fpg; 872 protofs->fs_fsbtodb = fsr->fsr_fsbtodb; 873 protofs->fs_sblkno = fsr->fsr_sblkno; 874 protofs->fs_magic = fsr->fsr_magic; 875 protofs->fs_ncg = fsr->fsr_ncg; 876 UFS_FREE(fsrbuf, filltype); 877 } 878 /* 879 * Scan looking for alternative superblocks. 880 */ 881 flags = nocsum; 882 if (!msg) 883 flags |= UFS_NOMSG; 884 for (cg = 0; cg < protofs->fs_ncg; cg++) { 885 sblk = fsbtodb(protofs, cgsblock(protofs, cg)); 886 if (msg) 887 printf("Try cg %ld at sblock loc %jd\n", cg, 888 (intmax_t)sblk); 889 if (ffs_sbget(devfd, fsp, dbtob(sblk), flags, filltype, 890 readfunc) == 0) { 891 if (msg) 892 printf("Succeeded with alternate superblock " 893 "at %jd\n", (intmax_t)sblk); 894 UFS_FREE(protofs, filltype); 895 return (0); 896 } 897 } 898 UFS_FREE(protofs, filltype); 899 /* 900 * Our alternate superblock strategies failed. Our last ditch effort 901 * is to see if the standard superblock has only non-critical errors. 902 */ 903 trynowarn: 904 flags = UFS_NOWARNFAIL | UFS_NOMSG | nocsum; 905 if (msg) { 906 printf("Finding an alternate superblock failed.\nCheck for " 907 "only non-critical errors in standard superblock\n"); 908 flags &= ~UFS_NOMSG; 909 } 910 if (ffs_sbget(devfd, fsp, UFS_STDSB, flags, filltype, readfunc) != 0) { 911 if (msg) 912 printf("Failed, superblock has critical errors\n"); 913 return (ENOENT); 914 } 915 if (msg) 916 printf("Success, using standard superblock with " 917 "non-critical errors.\n"); 918 return (0); 919 } 920 921 /* 922 * Write a superblock to the devfd device from the memory pointed to by fs. 923 * Write out the superblock summary information if it is present. 924 * 925 * If the write is successful, zero is returned. Otherwise one of the 926 * following error values is returned: 927 * EIO: failed to write superblock. 928 * EIO: failed to write superblock summary information. 929 */ 930 int 931 ffs_sbput(void *devfd, struct fs *fs, off_t loc, 932 int (*writefunc)(void *devfd, off_t loc, void *buf, int size)) 933 { 934 int i, error, blks, size; 935 uint8_t *space; 936 937 /* 938 * If there is summary information, write it first, so if there 939 * is an error, the superblock will not be marked as clean. 940 */ 941 if (fs->fs_si != NULL && fs->fs_csp != NULL) { 942 blks = howmany(fs->fs_cssize, fs->fs_fsize); 943 space = (uint8_t *)fs->fs_csp; 944 for (i = 0; i < blks; i += fs->fs_frag) { 945 size = fs->fs_bsize; 946 if (i + fs->fs_frag > blks) 947 size = (blks - i) * fs->fs_fsize; 948 if ((error = (*writefunc)(devfd, 949 dbtob(fsbtodb(fs, fs->fs_csaddr + i)), 950 space, size)) != 0) 951 return (error); 952 space += size; 953 } 954 } 955 fs->fs_fmod = 0; 956 #ifndef _KERNEL 957 { 958 struct fs_summary_info *fs_si; 959 960 fs->fs_time = time(NULL); 961 /* Clear the pointers for the duration of writing. */ 962 fs_si = fs->fs_si; 963 fs->fs_si = NULL; 964 fs->fs_ckhash = ffs_calc_sbhash(fs); 965 error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize); 966 fs->fs_si = fs_si; 967 } 968 #else /* _KERNEL */ 969 fs->fs_time = time_second; 970 fs->fs_ckhash = ffs_calc_sbhash(fs); 971 error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize); 972 #endif /* _KERNEL */ 973 return (error); 974 } 975 976 /* 977 * Calculate the check-hash for a superblock. 978 */ 979 uint32_t 980 ffs_calc_sbhash(struct fs *fs) 981 { 982 uint32_t ckhash, save_ckhash; 983 984 /* 985 * A filesystem that was using a superblock ckhash may be moved 986 * to an older kernel that does not support ckhashes. The 987 * older kernel will clear the FS_METACKHASH flag indicating 988 * that it does not update hashes. When the disk is moved back 989 * to a kernel capable of ckhashes it disables them on mount: 990 * 991 * if ((fs->fs_flags & FS_METACKHASH) == 0) 992 * fs->fs_metackhash = 0; 993 * 994 * This leaves (fs->fs_metackhash & CK_SUPERBLOCK) == 0) with an 995 * old stale value in the fs->fs_ckhash field. Thus the need to 996 * just accept what is there. 997 */ 998 if ((fs->fs_metackhash & CK_SUPERBLOCK) == 0) 999 return (fs->fs_ckhash); 1000 1001 save_ckhash = fs->fs_ckhash; 1002 fs->fs_ckhash = 0; 1003 /* 1004 * If newly read from disk, the caller is responsible for 1005 * verifying that fs->fs_sbsize <= SBLOCKSIZE. 1006 */ 1007 ckhash = calculate_crc32c(~0L, (void *)fs, fs->fs_sbsize); 1008 fs->fs_ckhash = save_ckhash; 1009 return (ckhash); 1010 } 1011 1012 /* 1013 * Update the frsum fields to reflect addition or deletion 1014 * of some frags. 1015 */ 1016 void 1017 ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt) 1018 { 1019 int inblk; 1020 int field, subfield; 1021 int siz, pos; 1022 1023 inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1; 1024 fragmap <<= 1; 1025 for (siz = 1; siz < fs->fs_frag; siz++) { 1026 if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0) 1027 continue; 1028 field = around[siz]; 1029 subfield = inside[siz]; 1030 for (pos = siz; pos <= fs->fs_frag; pos++) { 1031 if ((fragmap & field) == subfield) { 1032 fraglist[siz] += cnt; 1033 pos += siz; 1034 field <<= siz; 1035 subfield <<= siz; 1036 } 1037 field <<= 1; 1038 subfield <<= 1; 1039 } 1040 } 1041 } 1042 1043 /* 1044 * block operations 1045 * 1046 * check if a block is available 1047 */ 1048 int 1049 ffs_isblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h) 1050 { 1051 unsigned char mask; 1052 1053 switch ((int)fs->fs_frag) { 1054 case 8: 1055 return (cp[h] == 0xff); 1056 case 4: 1057 mask = 0x0f << ((h & 0x1) << 2); 1058 return ((cp[h >> 1] & mask) == mask); 1059 case 2: 1060 mask = 0x03 << ((h & 0x3) << 1); 1061 return ((cp[h >> 2] & mask) == mask); 1062 case 1: 1063 mask = 0x01 << (h & 0x7); 1064 return ((cp[h >> 3] & mask) == mask); 1065 default: 1066 #ifdef _KERNEL 1067 panic("ffs_isblock"); 1068 #endif 1069 break; 1070 } 1071 return (0); 1072 } 1073 1074 /* 1075 * check if a block is free 1076 */ 1077 int 1078 ffs_isfreeblock(struct fs *fs, uint8_t *cp, ufs1_daddr_t h) 1079 { 1080 1081 switch ((int)fs->fs_frag) { 1082 case 8: 1083 return (cp[h] == 0); 1084 case 4: 1085 return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0); 1086 case 2: 1087 return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0); 1088 case 1: 1089 return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0); 1090 default: 1091 #ifdef _KERNEL 1092 panic("ffs_isfreeblock"); 1093 #endif 1094 break; 1095 } 1096 return (0); 1097 } 1098 1099 /* 1100 * take a block out of the map 1101 */ 1102 void 1103 ffs_clrblock(struct fs *fs, uint8_t *cp, ufs1_daddr_t h) 1104 { 1105 1106 switch ((int)fs->fs_frag) { 1107 case 8: 1108 cp[h] = 0; 1109 return; 1110 case 4: 1111 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2)); 1112 return; 1113 case 2: 1114 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1)); 1115 return; 1116 case 1: 1117 cp[h >> 3] &= ~(0x01 << (h & 0x7)); 1118 return; 1119 default: 1120 #ifdef _KERNEL 1121 panic("ffs_clrblock"); 1122 #endif 1123 break; 1124 } 1125 } 1126 1127 /* 1128 * put a block into the map 1129 */ 1130 void 1131 ffs_setblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h) 1132 { 1133 1134 switch ((int)fs->fs_frag) { 1135 case 8: 1136 cp[h] = 0xff; 1137 return; 1138 case 4: 1139 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2)); 1140 return; 1141 case 2: 1142 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1)); 1143 return; 1144 case 1: 1145 cp[h >> 3] |= (0x01 << (h & 0x7)); 1146 return; 1147 default: 1148 #ifdef _KERNEL 1149 panic("ffs_setblock"); 1150 #endif 1151 break; 1152 } 1153 } 1154 1155 /* 1156 * Update the cluster map because of an allocation or free. 1157 * 1158 * Cnt == 1 means free; cnt == -1 means allocating. 1159 */ 1160 void 1161 ffs_clusteracct(struct fs *fs, struct cg *cgp, ufs1_daddr_t blkno, int cnt) 1162 { 1163 int32_t *sump; 1164 int32_t *lp; 1165 uint8_t *freemapp, *mapp; 1166 int i, start, end, forw, back, map; 1167 uint64_t bit; 1168 1169 if (fs->fs_contigsumsize <= 0) 1170 return; 1171 freemapp = cg_clustersfree(cgp); 1172 sump = cg_clustersum(cgp); 1173 /* 1174 * Allocate or clear the actual block. 1175 */ 1176 if (cnt > 0) 1177 setbit(freemapp, blkno); 1178 else 1179 clrbit(freemapp, blkno); 1180 /* 1181 * Find the size of the cluster going forward. 1182 */ 1183 start = blkno + 1; 1184 end = start + fs->fs_contigsumsize; 1185 if (end >= cgp->cg_nclusterblks) 1186 end = cgp->cg_nclusterblks; 1187 mapp = &freemapp[start / NBBY]; 1188 map = *mapp++; 1189 bit = 1U << (start % NBBY); 1190 for (i = start; i < end; i++) { 1191 if ((map & bit) == 0) 1192 break; 1193 if ((i & (NBBY - 1)) != (NBBY - 1)) { 1194 bit <<= 1; 1195 } else { 1196 map = *mapp++; 1197 bit = 1; 1198 } 1199 } 1200 forw = i - start; 1201 /* 1202 * Find the size of the cluster going backward. 1203 */ 1204 start = blkno - 1; 1205 end = start - fs->fs_contigsumsize; 1206 if (end < 0) 1207 end = -1; 1208 mapp = &freemapp[start / NBBY]; 1209 map = *mapp--; 1210 bit = 1U << (start % NBBY); 1211 for (i = start; i > end; i--) { 1212 if ((map & bit) == 0) 1213 break; 1214 if ((i & (NBBY - 1)) != 0) { 1215 bit >>= 1; 1216 } else { 1217 map = *mapp--; 1218 bit = 1U << (NBBY - 1); 1219 } 1220 } 1221 back = start - i; 1222 /* 1223 * Account for old cluster and the possibly new forward and 1224 * back clusters. 1225 */ 1226 i = back + forw + 1; 1227 if (i > fs->fs_contigsumsize) 1228 i = fs->fs_contigsumsize; 1229 sump[i] += cnt; 1230 if (back > 0) 1231 sump[back] -= cnt; 1232 if (forw > 0) 1233 sump[forw] -= cnt; 1234 /* 1235 * Update cluster summary information. 1236 */ 1237 lp = &sump[fs->fs_contigsumsize]; 1238 for (i = fs->fs_contigsumsize; i > 0; i--) 1239 if (*lp-- > 0) 1240 break; 1241 fs->fs_maxcluster[cgp->cg_cgx] = i; 1242 } 1243