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_DECL(_vfs_ffs); 409 SYSCTL_INT(_vfs_ffs, OID_AUTO, prttimechgs, CTLFLAG_RWTUN, &prttimechgs, 0, 410 "print UFS1 time changes made to inodes"); 411 #endif /* _KERNEL */ 412 bool 413 ffs_oldfscompat_inode_read(struct fs *fs, union dinodep dp, time_t now) 414 { 415 bool change; 416 417 change = false; 418 switch (fs->fs_magic) { 419 case FS_UFS2_MAGIC: 420 /* No changes for now */ 421 break; 422 423 case FS_UFS1_MAGIC: 424 /* 425 * With the change to unsigned time values in UFS1, times set 426 * before Jan 1, 1970 will appear to be in the future. Check 427 * for future times and set them to be the current time. 428 */ 429 if (dp.dp1->di_ctime > now) { 430 if (prttimechgs) 431 printf("ctime %ud changed to %ld\n", 432 dp.dp1->di_ctime, (long)now); 433 dp.dp1->di_ctime = now; 434 change = true; 435 } 436 if (dp.dp1->di_mtime > now) { 437 if (prttimechgs) 438 printf("mtime %ud changed to %ld\n", 439 dp.dp1->di_mtime, (long)now); 440 dp.dp1->di_mtime = now; 441 dp.dp1->di_ctime = now; 442 change = true; 443 } 444 if (dp.dp1->di_atime > now) { 445 if (prttimechgs) 446 printf("atime %ud changed to %ld\n", 447 dp.dp1->di_atime, (long)now); 448 dp.dp1->di_atime = now; 449 dp.dp1->di_ctime = now; 450 change = true; 451 } 452 break; 453 } 454 return (change); 455 } 456 457 /* 458 * Verify the filesystem values. 459 */ 460 #define ILOG2(num) (fls(num) - 1) 461 #ifdef STANDALONE_SMALL 462 #define MPRINT(...) do { } while (0) 463 #else 464 #define MPRINT(...) if (prtmsg) printf(__VA_ARGS__) 465 #endif 466 #define FCHK(lhs, op, rhs, fmt) \ 467 if (lhs op rhs) { \ 468 MPRINT("UFS%d superblock failed: %s (" #fmt ") %s %s (" \ 469 #fmt ")\n", fs->fs_magic == FS_UFS1_MAGIC ? 1 : 2, \ 470 #lhs, (intmax_t)lhs, #op, #rhs, (intmax_t)rhs); \ 471 if (error < 0) \ 472 return (ENOENT); \ 473 if (error == 0) \ 474 error = ENOENT; \ 475 } 476 #define WCHK(lhs, op, rhs, fmt) \ 477 if (lhs op rhs) { \ 478 MPRINT("UFS%d superblock failed: %s (" #fmt ") %s %s (" \ 479 #fmt ")%s\n", fs->fs_magic == FS_UFS1_MAGIC ? 1 : 2,\ 480 #lhs, (intmax_t)lhs, #op, #rhs, (intmax_t)rhs, wmsg);\ 481 if (error == 0) \ 482 error = warnerr; \ 483 if (warnerr == 0) \ 484 lhs = rhs; \ 485 } 486 #define FCHK2(lhs1, op1, rhs1, lhs2, op2, rhs2, fmt) \ 487 if (lhs1 op1 rhs1 && lhs2 op2 rhs2) { \ 488 MPRINT("UFS%d superblock failed: %s (" #fmt ") %s %s (" \ 489 #fmt ") && %s (" #fmt ") %s %s (" #fmt ")\n", \ 490 fs->fs_magic == FS_UFS1_MAGIC ? 1 : 2, #lhs1, \ 491 (intmax_t)lhs1, #op1, #rhs1, (intmax_t)rhs1, #lhs2, \ 492 (intmax_t)lhs2, #op2, #rhs2, (intmax_t)rhs2); \ 493 if (error < 0) \ 494 return (ENOENT); \ 495 if (error == 0) \ 496 error = ENOENT; \ 497 } 498 499 static int 500 validate_sblock(struct fs *fs, int flags) 501 { 502 uint64_t i, sectorsize; 503 uint64_t maxfilesize, sizepb; 504 int error, prtmsg, warnerr; 505 char *wmsg; 506 507 error = 0; 508 sectorsize = dbtob(1); 509 prtmsg = ((flags & UFS_NOMSG) == 0); 510 warnerr = (flags & UFS_NOWARNFAIL) == UFS_NOWARNFAIL ? 0 : ENOENT; 511 wmsg = warnerr ? "" : " (Ignored)"; 512 /* 513 * Check for endian mismatch between machine and filesystem. 514 */ 515 if (((fs->fs_magic != FS_UFS2_MAGIC) && 516 (bswap32(fs->fs_magic) == FS_UFS2_MAGIC)) || 517 ((fs->fs_magic != FS_UFS1_MAGIC) && 518 (bswap32(fs->fs_magic) == FS_UFS1_MAGIC))) { 519 MPRINT("UFS superblock failed due to endian mismatch " 520 "between machine and filesystem\n"); 521 return(EILSEQ); 522 } 523 /* 524 * If just validating for recovery, then do just the minimal 525 * checks needed for the superblock fields needed to find 526 * alternate superblocks. 527 */ 528 if ((flags & UFS_FSRONLY) == UFS_FSRONLY && 529 (fs->fs_magic == FS_UFS1_MAGIC || fs->fs_magic == FS_UFS2_MAGIC)) { 530 error = -1; /* fail on first error */ 531 if (fs->fs_magic == FS_UFS2_MAGIC) { 532 FCHK(fs->fs_sblockloc, !=, SBLOCK_UFS2, %#jx); 533 } else if (fs->fs_magic == FS_UFS1_MAGIC) { 534 FCHK(fs->fs_sblockloc, <, 0, %jd); 535 FCHK(fs->fs_sblockloc, >, SBLOCK_UFS1, %jd); 536 } 537 FCHK(fs->fs_frag, <, 1, %jd); 538 FCHK(fs->fs_frag, >, MAXFRAG, %jd); 539 FCHK(fs->fs_bsize, <, MINBSIZE, %jd); 540 FCHK(fs->fs_bsize, >, MAXBSIZE, %jd); 541 FCHK(fs->fs_bsize, <, roundup(sizeof(struct fs), DEV_BSIZE), 542 %jd); 543 FCHK(fs->fs_fsize, <, sectorsize, %jd); 544 FCHK(fs->fs_fsize * fs->fs_frag, !=, fs->fs_bsize, %jd); 545 FCHK(powerof2(fs->fs_fsize), ==, 0, %jd); 546 FCHK(fs->fs_sbsize, >, SBLOCKSIZE, %jd); 547 FCHK(fs->fs_sbsize, <, (signed)sizeof(struct fs), %jd); 548 FCHK(fs->fs_sbsize % sectorsize, !=, 0, %jd); 549 FCHK(fs->fs_fpg, <, 3 * fs->fs_frag, %jd); 550 FCHK(fs->fs_ncg, <, 1, %jd); 551 FCHK(fs->fs_fsbtodb, !=, ILOG2(fs->fs_fsize / sectorsize), %jd); 552 FCHK(fs->fs_old_cgoffset, <, 0, %jd); 553 FCHK2(fs->fs_old_cgoffset, >, 0, ~fs->fs_old_cgmask, <, 0, %jd); 554 FCHK(fs->fs_old_cgoffset * (~fs->fs_old_cgmask), >, fs->fs_fpg, 555 %jd); 556 FCHK(fs->fs_sblkno, !=, roundup( 557 howmany(fs->fs_sblockloc + SBLOCKSIZE, fs->fs_fsize), 558 fs->fs_frag), %jd); 559 FCHK(CGSIZE(fs), >, fs->fs_bsize, %jd); 560 /* Only need to validate these if reading in csum data */ 561 if ((flags & UFS_NOCSUM) != 0) 562 return (error); 563 FCHK((uint64_t)fs->fs_ipg * fs->fs_ncg, >, 564 (((int64_t)(1)) << 32) - INOPB(fs), %jd); 565 FCHK(fs->fs_cstotal.cs_nifree, <, 0, %jd); 566 FCHK(fs->fs_cstotal.cs_nifree, >, 567 (uint64_t)fs->fs_ipg * fs->fs_ncg, %jd); 568 FCHK(fs->fs_cstotal.cs_ndir, >, 569 ((uint64_t)fs->fs_ipg * fs->fs_ncg) - 570 fs->fs_cstotal.cs_nifree, %jd); 571 FCHK(fs->fs_size, <, 8 * fs->fs_frag, %jd); 572 FCHK(fs->fs_size, <=, ((int64_t)fs->fs_ncg - 1) * fs->fs_fpg, 573 %jd); 574 FCHK(fs->fs_size, >, (int64_t)fs->fs_ncg * fs->fs_fpg, %jd); 575 FCHK(fs->fs_csaddr, <, 0, %jd); 576 FCHK(fs->fs_cssize, !=, 577 fragroundup(fs, fs->fs_ncg * sizeof(struct csum)), %jd); 578 FCHK(fs->fs_csaddr + howmany(fs->fs_cssize, fs->fs_fsize), >, 579 fs->fs_size, %jd); 580 FCHK(fs->fs_csaddr, <, cgdmin(fs, dtog(fs, fs->fs_csaddr)), 581 %jd); 582 FCHK(dtog(fs, fs->fs_csaddr + howmany(fs->fs_cssize, 583 fs->fs_fsize)), >, dtog(fs, fs->fs_csaddr), %jd); 584 return (error); 585 } 586 if (fs->fs_magic == FS_UFS2_MAGIC) { 587 if ((flags & UFS_ALTSBLK) == 0) 588 FCHK2(fs->fs_sblockactualloc, !=, SBLOCK_UFS2, 589 fs->fs_sblockactualloc, !=, 0, %jd); 590 FCHK(fs->fs_sblockloc, !=, SBLOCK_UFS2, %#jx); 591 FCHK(fs->fs_maxsymlinklen, !=, ((UFS_NDADDR + UFS_NIADDR) * 592 sizeof(ufs2_daddr_t)), %jd); 593 FCHK(fs->fs_nindir, !=, fs->fs_bsize / sizeof(ufs2_daddr_t), 594 %jd); 595 FCHK(fs->fs_inopb, !=, 596 fs->fs_bsize / sizeof(struct ufs2_dinode), %jd); 597 } else if (fs->fs_magic == FS_UFS1_MAGIC) { 598 if ((flags & UFS_ALTSBLK) == 0) 599 FCHK(fs->fs_sblockactualloc, >, SBLOCK_UFS1, %jd); 600 FCHK(fs->fs_sblockloc, <, 0, %jd); 601 FCHK(fs->fs_sblockloc, >, SBLOCK_UFS1, %jd); 602 FCHK(fs->fs_nindir, !=, fs->fs_bsize / sizeof(ufs1_daddr_t), 603 %jd); 604 FCHK(fs->fs_inopb, !=, 605 fs->fs_bsize / sizeof(struct ufs1_dinode), %jd); 606 FCHK(fs->fs_maxsymlinklen, !=, ((UFS_NDADDR + UFS_NIADDR) * 607 sizeof(ufs1_daddr_t)), %jd); 608 WCHK(fs->fs_old_inodefmt, !=, FS_44INODEFMT, %jd); 609 WCHK(fs->fs_old_rotdelay, !=, 0, %jd); 610 WCHK(fs->fs_old_rps, !=, 60, %jd); 611 WCHK(fs->fs_old_nspf, !=, fs->fs_fsize / sectorsize, %jd); 612 WCHK(fs->fs_old_interleave, !=, 1, %jd); 613 WCHK(fs->fs_old_trackskew, !=, 0, %jd); 614 WCHK(fs->fs_old_cpc, !=, 0, %jd); 615 WCHK(fs->fs_old_postblformat, !=, 1, %jd); 616 FCHK(fs->fs_old_nrpos, !=, 1, %jd); 617 WCHK(fs->fs_old_nsect, !=, fs->fs_old_spc, %jd); 618 WCHK(fs->fs_old_npsect, !=, fs->fs_old_spc, %jd); 619 } else { 620 /* Bad magic number, so assume not a superblock */ 621 return (ENOENT); 622 } 623 FCHK(fs->fs_bsize, <, MINBSIZE, %jd); 624 FCHK(fs->fs_bsize, >, MAXBSIZE, %jd); 625 FCHK(fs->fs_bsize, <, roundup(sizeof(struct fs), DEV_BSIZE), %jd); 626 FCHK(powerof2(fs->fs_bsize), ==, 0, %jd); 627 FCHK(fs->fs_frag, <, 1, %jd); 628 FCHK(fs->fs_frag, >, MAXFRAG, %jd); 629 FCHK(fs->fs_frag, !=, numfrags(fs, fs->fs_bsize), %jd); 630 FCHK(fs->fs_fsize, <, sectorsize, %jd); 631 FCHK(fs->fs_fsize * fs->fs_frag, !=, fs->fs_bsize, %jd); 632 FCHK(powerof2(fs->fs_fsize), ==, 0, %jd); 633 FCHK(fs->fs_fpg, <, 3 * fs->fs_frag, %jd); 634 FCHK(fs->fs_ncg, <, 1, %jd); 635 FCHK(fs->fs_ipg, <, fs->fs_inopb, %jd); 636 FCHK((uint64_t)fs->fs_ipg * fs->fs_ncg, >, 637 (((int64_t)(1)) << 32) - INOPB(fs), %jd); 638 FCHK(fs->fs_cstotal.cs_nifree, <, 0, %jd); 639 FCHK(fs->fs_cstotal.cs_nifree, >, (uint64_t)fs->fs_ipg * fs->fs_ncg, 640 %jd); 641 FCHK(fs->fs_cstotal.cs_ndir, <, 0, %jd); 642 FCHK(fs->fs_cstotal.cs_ndir, >, 643 ((uint64_t)fs->fs_ipg * fs->fs_ncg) - fs->fs_cstotal.cs_nifree, 644 %jd); 645 FCHK(fs->fs_sbsize, >, SBLOCKSIZE, %jd); 646 FCHK(fs->fs_sbsize, <, (signed)sizeof(struct fs), %jd); 647 /* fix for misconfigured filesystems */ 648 if (fs->fs_maxbsize == 0) 649 fs->fs_maxbsize = fs->fs_bsize; 650 FCHK(fs->fs_maxbsize, <, fs->fs_bsize, %jd); 651 FCHK(powerof2(fs->fs_maxbsize), ==, 0, %jd); 652 FCHK(fs->fs_maxbsize, >, FS_MAXCONTIG * fs->fs_bsize, %jd); 653 FCHK(fs->fs_bmask, !=, ~(fs->fs_bsize - 1), %#jx); 654 FCHK(fs->fs_fmask, !=, ~(fs->fs_fsize - 1), %#jx); 655 FCHK(fs->fs_qbmask, !=, ~fs->fs_bmask, %#jx); 656 FCHK(fs->fs_qfmask, !=, ~fs->fs_fmask, %#jx); 657 FCHK(fs->fs_bshift, !=, ILOG2(fs->fs_bsize), %jd); 658 FCHK(fs->fs_fshift, !=, ILOG2(fs->fs_fsize), %jd); 659 FCHK(fs->fs_fragshift, !=, ILOG2(fs->fs_frag), %jd); 660 FCHK(fs->fs_fsbtodb, !=, ILOG2(fs->fs_fsize / sectorsize), %jd); 661 FCHK(fs->fs_old_cgoffset, <, 0, %jd); 662 FCHK2(fs->fs_old_cgoffset, >, 0, ~fs->fs_old_cgmask, <, 0, %jd); 663 FCHK(fs->fs_old_cgoffset * (~fs->fs_old_cgmask), >, fs->fs_fpg, %jd); 664 FCHK(CGSIZE(fs), >, fs->fs_bsize, %jd); 665 /* 666 * If anything has failed up to this point, it is usafe to proceed 667 * as checks below may divide by zero or make other fatal calculations. 668 * So if we have any errors at this point, give up. 669 */ 670 if (error) 671 return (error); 672 FCHK(fs->fs_sbsize % sectorsize, !=, 0, %jd); 673 FCHK(fs->fs_ipg % fs->fs_inopb, !=, 0, %jd); 674 FCHK(fs->fs_sblkno, !=, roundup( 675 howmany(fs->fs_sblockloc + SBLOCKSIZE, fs->fs_fsize), 676 fs->fs_frag), %jd); 677 FCHK(fs->fs_cblkno, !=, fs->fs_sblkno + 678 roundup(howmany(SBLOCKSIZE, fs->fs_fsize), fs->fs_frag), %jd); 679 FCHK(fs->fs_iblkno, !=, fs->fs_cblkno + fs->fs_frag, %jd); 680 FCHK(fs->fs_dblkno, !=, fs->fs_iblkno + fs->fs_ipg / INOPF(fs), %jd); 681 FCHK(fs->fs_cgsize, >, fs->fs_bsize, %jd); 682 FCHK(fs->fs_cgsize, <, fs->fs_fsize, %jd); 683 FCHK(fs->fs_cgsize % fs->fs_fsize, !=, 0, %jd); 684 /* 685 * This test is valid, however older versions of growfs failed 686 * to correctly update fs_dsize so will fail this test. Thus we 687 * exclude it from the requirements. 688 */ 689 #ifdef notdef 690 WCHK(fs->fs_dsize, !=, fs->fs_size - fs->fs_sblkno - 691 fs->fs_ncg * (fs->fs_dblkno - fs->fs_sblkno) - 692 howmany(fs->fs_cssize, fs->fs_fsize), %jd); 693 #endif 694 WCHK(fs->fs_metaspace, <, 0, %jd); 695 WCHK(fs->fs_metaspace, >, fs->fs_fpg / 2, %jd); 696 WCHK(fs->fs_minfree, >, 99, %jd%%); 697 maxfilesize = fs->fs_bsize * UFS_NDADDR - 1; 698 for (sizepb = fs->fs_bsize, i = 0; i < UFS_NIADDR; i++) { 699 sizepb *= NINDIR(fs); 700 maxfilesize += sizepb; 701 } 702 WCHK(fs->fs_maxfilesize, >, maxfilesize, %jd); 703 /* 704 * These values have a tight interaction with each other that 705 * makes it hard to tightly bound them. So we can only check 706 * that they are within a broader possible range. 707 * 708 * The size cannot always be accurately determined, but ensure 709 * that it is consistent with the number of cylinder groups (fs_ncg) 710 * and the number of fragments per cylinder group (fs_fpg). Ensure 711 * that the summary information size is correct and that it starts 712 * and ends in the data area of the same cylinder group. 713 */ 714 FCHK(fs->fs_size, <, 8 * fs->fs_frag, %jd); 715 FCHK(fs->fs_size, <=, ((int64_t)fs->fs_ncg - 1) * fs->fs_fpg, %jd); 716 FCHK(fs->fs_size, >, (int64_t)fs->fs_ncg * fs->fs_fpg, %jd); 717 /* 718 * If we are not requested to read in the csum data stop here 719 * as the correctness of the remaining values is only important 720 * to bound the space needed to be allocated to hold the csum data. 721 */ 722 if ((flags & UFS_NOCSUM) != 0) 723 return (error); 724 FCHK(fs->fs_csaddr, <, 0, %jd); 725 FCHK(fs->fs_cssize, !=, 726 fragroundup(fs, fs->fs_ncg * sizeof(struct csum)), %jd); 727 FCHK(fs->fs_csaddr + howmany(fs->fs_cssize, fs->fs_fsize), >, 728 fs->fs_size, %jd); 729 FCHK(fs->fs_csaddr, <, cgdmin(fs, dtog(fs, fs->fs_csaddr)), %jd); 730 FCHK(dtog(fs, fs->fs_csaddr + howmany(fs->fs_cssize, fs->fs_fsize)), >, 731 dtog(fs, fs->fs_csaddr), %jd); 732 /* 733 * With file system clustering it is possible to allocate 734 * many contiguous blocks. The kernel variable maxphys defines 735 * the maximum transfer size permitted by the controller and/or 736 * buffering. The fs_maxcontig parameter controls the maximum 737 * number of blocks that the filesystem will read or write 738 * in a single transfer. It is calculated when the filesystem 739 * is created as maxphys / fs_bsize. The loader uses a maxphys 740 * of 128K even when running on a system that supports larger 741 * values. If the filesystem was built on a system that supports 742 * a larger maxphys (1M is typical) it will have configured 743 * fs_maxcontig for that larger system. So we bound the upper 744 * allowable limit for fs_maxconfig to be able to at least 745 * work with a 1M maxphys on the smallest block size filesystem: 746 * 1M / 4096 == 256. There is no harm in allowing the mounting of 747 * filesystems that make larger than maxphys I/O requests because 748 * those (mostly 32-bit machines) can (very slowly) handle I/O 749 * requests that exceed maxphys. 750 */ 751 WCHK(fs->fs_maxcontig, <, 0, %jd); 752 WCHK(fs->fs_maxcontig, >, MAX(256, maxphys / fs->fs_bsize), %jd); 753 FCHK2(fs->fs_maxcontig, ==, 0, fs->fs_contigsumsize, !=, 0, %jd); 754 FCHK2(fs->fs_maxcontig, >, 1, fs->fs_contigsumsize, !=, 755 MIN(fs->fs_maxcontig, FS_MAXCONTIG), %jd); 756 return (error); 757 } 758 759 /* 760 * Make an extensive search to find a superblock. If the superblock 761 * in the standard place cannot be used, try looking for one of the 762 * backup superblocks. 763 * 764 * Flags are made up of the following or'ed together options: 765 * 766 * UFS_NOMSG indicates that superblock inconsistency error messages 767 * should not be printed. 768 * 769 * UFS_NOCSUM causes only the superblock itself to be returned, but does 770 * not read in any auxillary data structures like the cylinder group 771 * summary information. 772 */ 773 int 774 ffs_sbsearch(void *devfd, struct fs **fsp, int reqflags, 775 struct malloc_type *filltype, 776 int (*readfunc)(void *devfd, off_t loc, void **bufp, int size)) 777 { 778 struct fsrecovery *fsr; 779 struct fs *protofs; 780 void *fsrbuf; 781 char *cp; 782 long nocsum, flags, msg, cg; 783 off_t sblk, secsize; 784 int error; 785 786 msg = (reqflags & UFS_NOMSG) == 0; 787 nocsum = reqflags & UFS_NOCSUM; 788 /* 789 * Try normal superblock read and return it if it works. 790 * 791 * Suppress messages if it fails until we find out if 792 * failure can be avoided. 793 */ 794 flags = UFS_NOMSG | nocsum; 795 error = ffs_sbget(devfd, fsp, UFS_STDSB, flags, filltype, readfunc); 796 /* 797 * If successful or endian error, no need to try further. 798 */ 799 if (error == 0 || error == EILSEQ) { 800 if (msg && error == EILSEQ) 801 printf("UFS superblock failed due to endian mismatch " 802 "between machine and filesystem\n"); 803 return (error); 804 } 805 /* 806 * First try: ignoring hash failures. 807 */ 808 flags |= UFS_NOHASHFAIL; 809 if (msg) 810 flags &= ~UFS_NOMSG; 811 if (ffs_sbget(devfd, fsp, UFS_STDSB, flags, filltype, readfunc) == 0) 812 return (0); 813 /* 814 * Next up is to check if fields of the superblock that are 815 * needed to find backup superblocks are usable. 816 */ 817 if (msg) 818 printf("Attempted recovery for standard superblock: failed\n"); 819 flags = UFS_FSRONLY | UFS_NOHASHFAIL | UFS_NOCSUM | UFS_NOMSG; 820 if (ffs_sbget(devfd, &protofs, UFS_STDSB, flags, filltype, 821 readfunc) == 0) { 822 if (msg) 823 printf("Attempt extraction of recovery data from " 824 "standard superblock.\n"); 825 } else { 826 /* 827 * Final desperation is to see if alternate superblock 828 * parameters have been saved in the boot area. 829 */ 830 if (msg) 831 printf("Attempted extraction of recovery data from " 832 "standard superblock: failed\nAttempt to find " 833 "boot zone recovery data.\n"); 834 /* 835 * Look to see if recovery information has been saved. 836 * If so we can generate a prototype superblock based 837 * on that information. 838 * 839 * We need fragments-per-group, number of cylinder groups, 840 * location of the superblock within the cylinder group, and 841 * the conversion from filesystem fragments to disk blocks. 842 * 843 * When building a UFS2 filesystem, newfs(8) stores these 844 * details at the end of the boot block area at the start 845 * of the filesystem partition. If they have been overwritten 846 * by a boot block, we fail. But usually they are there 847 * and we can use them. 848 * 849 * We could ask the underlying device for its sector size, 850 * but some devices lie. So we just try a plausible range. 851 */ 852 error = ENOENT; 853 fsrbuf = NULL; 854 for (secsize = dbtob(1); secsize <= SBLOCKSIZE; secsize *= 2) 855 if ((error = (*readfunc)(devfd, (SBLOCK_UFS2 - secsize), 856 &fsrbuf, secsize)) == 0) 857 break; 858 if (error != 0) 859 goto trynowarn; 860 cp = fsrbuf; /* type change to keep compiler happy */ 861 fsr = (struct fsrecovery *)&cp[secsize - sizeof *fsr]; 862 if (fsr->fsr_magic != FS_UFS2_MAGIC || 863 (protofs = UFS_MALLOC(SBLOCKSIZE, filltype, M_NOWAIT)) 864 == NULL) { 865 UFS_FREE(fsrbuf, filltype); 866 goto trynowarn; 867 } 868 memset(protofs, 0, sizeof(struct fs)); 869 protofs->fs_fpg = fsr->fsr_fpg; 870 protofs->fs_fsbtodb = fsr->fsr_fsbtodb; 871 protofs->fs_sblkno = fsr->fsr_sblkno; 872 protofs->fs_magic = fsr->fsr_magic; 873 protofs->fs_ncg = fsr->fsr_ncg; 874 UFS_FREE(fsrbuf, filltype); 875 } 876 /* 877 * Scan looking for alternative superblocks. 878 */ 879 flags = nocsum; 880 if (!msg) 881 flags |= UFS_NOMSG; 882 for (cg = 0; cg < protofs->fs_ncg; cg++) { 883 sblk = fsbtodb(protofs, cgsblock(protofs, cg)); 884 if (msg) 885 printf("Try cg %ld at sblock loc %jd\n", cg, 886 (intmax_t)sblk); 887 if (ffs_sbget(devfd, fsp, dbtob(sblk), flags, filltype, 888 readfunc) == 0) { 889 if (msg) 890 printf("Succeeded with alternate superblock " 891 "at %jd\n", (intmax_t)sblk); 892 UFS_FREE(protofs, filltype); 893 return (0); 894 } 895 } 896 UFS_FREE(protofs, filltype); 897 /* 898 * Our alternate superblock strategies failed. Our last ditch effort 899 * is to see if the standard superblock has only non-critical errors. 900 */ 901 trynowarn: 902 flags = UFS_NOWARNFAIL | UFS_NOMSG | nocsum; 903 if (msg) { 904 printf("Finding an alternate superblock failed.\nCheck for " 905 "only non-critical errors in standard superblock\n"); 906 flags &= ~UFS_NOMSG; 907 } 908 if (ffs_sbget(devfd, fsp, UFS_STDSB, flags, filltype, readfunc) != 0) { 909 if (msg) 910 printf("Failed, superblock has critical errors\n"); 911 return (ENOENT); 912 } 913 if (msg) 914 printf("Success, using standard superblock with " 915 "non-critical errors.\n"); 916 return (0); 917 } 918 919 /* 920 * Write a superblock to the devfd device from the memory pointed to by fs. 921 * Write out the superblock summary information if it is present. 922 * 923 * If the write is successful, zero is returned. Otherwise one of the 924 * following error values is returned: 925 * EIO: failed to write superblock. 926 * EIO: failed to write superblock summary information. 927 */ 928 int 929 ffs_sbput(void *devfd, struct fs *fs, off_t loc, 930 int (*writefunc)(void *devfd, off_t loc, void *buf, int size)) 931 { 932 int i, error, blks, size; 933 uint8_t *space; 934 935 /* 936 * If there is summary information, write it first, so if there 937 * is an error, the superblock will not be marked as clean. 938 */ 939 if (fs->fs_si != NULL && fs->fs_csp != NULL) { 940 blks = howmany(fs->fs_cssize, fs->fs_fsize); 941 space = (uint8_t *)fs->fs_csp; 942 for (i = 0; i < blks; i += fs->fs_frag) { 943 size = fs->fs_bsize; 944 if (i + fs->fs_frag > blks) 945 size = (blks - i) * fs->fs_fsize; 946 if ((error = (*writefunc)(devfd, 947 dbtob(fsbtodb(fs, fs->fs_csaddr + i)), 948 space, size)) != 0) 949 return (error); 950 space += size; 951 } 952 } 953 fs->fs_fmod = 0; 954 #ifndef _KERNEL 955 { 956 struct fs_summary_info *fs_si; 957 958 fs->fs_time = time(NULL); 959 /* Clear the pointers for the duration of writing. */ 960 fs_si = fs->fs_si; 961 fs->fs_si = NULL; 962 fs->fs_ckhash = ffs_calc_sbhash(fs); 963 error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize); 964 fs->fs_si = fs_si; 965 } 966 #else /* _KERNEL */ 967 fs->fs_time = time_second; 968 fs->fs_ckhash = ffs_calc_sbhash(fs); 969 error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize); 970 #endif /* _KERNEL */ 971 return (error); 972 } 973 974 /* 975 * Calculate the check-hash for a superblock. 976 */ 977 uint32_t 978 ffs_calc_sbhash(struct fs *fs) 979 { 980 uint32_t ckhash, save_ckhash; 981 982 /* 983 * A filesystem that was using a superblock ckhash may be moved 984 * to an older kernel that does not support ckhashes. The 985 * older kernel will clear the FS_METACKHASH flag indicating 986 * that it does not update hashes. When the disk is moved back 987 * to a kernel capable of ckhashes it disables them on mount: 988 * 989 * if ((fs->fs_flags & FS_METACKHASH) == 0) 990 * fs->fs_metackhash = 0; 991 * 992 * This leaves (fs->fs_metackhash & CK_SUPERBLOCK) == 0) with an 993 * old stale value in the fs->fs_ckhash field. Thus the need to 994 * just accept what is there. 995 */ 996 if ((fs->fs_metackhash & CK_SUPERBLOCK) == 0) 997 return (fs->fs_ckhash); 998 999 save_ckhash = fs->fs_ckhash; 1000 fs->fs_ckhash = 0; 1001 /* 1002 * If newly read from disk, the caller is responsible for 1003 * verifying that fs->fs_sbsize <= SBLOCKSIZE. 1004 */ 1005 ckhash = calculate_crc32c(~0L, (void *)fs, fs->fs_sbsize); 1006 fs->fs_ckhash = save_ckhash; 1007 return (ckhash); 1008 } 1009 1010 /* 1011 * Update the frsum fields to reflect addition or deletion 1012 * of some frags. 1013 */ 1014 void 1015 ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt) 1016 { 1017 int inblk; 1018 int field, subfield; 1019 int siz, pos; 1020 1021 inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1; 1022 fragmap <<= 1; 1023 for (siz = 1; siz < fs->fs_frag; siz++) { 1024 if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0) 1025 continue; 1026 field = around[siz]; 1027 subfield = inside[siz]; 1028 for (pos = siz; pos <= fs->fs_frag; pos++) { 1029 if ((fragmap & field) == subfield) { 1030 fraglist[siz] += cnt; 1031 pos += siz; 1032 field <<= siz; 1033 subfield <<= siz; 1034 } 1035 field <<= 1; 1036 subfield <<= 1; 1037 } 1038 } 1039 } 1040 1041 /* 1042 * block operations 1043 * 1044 * check if a block is available 1045 */ 1046 int 1047 ffs_isblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h) 1048 { 1049 unsigned char mask; 1050 1051 switch ((int)fs->fs_frag) { 1052 case 8: 1053 return (cp[h] == 0xff); 1054 case 4: 1055 mask = 0x0f << ((h & 0x1) << 2); 1056 return ((cp[h >> 1] & mask) == mask); 1057 case 2: 1058 mask = 0x03 << ((h & 0x3) << 1); 1059 return ((cp[h >> 2] & mask) == mask); 1060 case 1: 1061 mask = 0x01 << (h & 0x7); 1062 return ((cp[h >> 3] & mask) == mask); 1063 default: 1064 #ifdef _KERNEL 1065 panic("ffs_isblock"); 1066 #endif 1067 break; 1068 } 1069 return (0); 1070 } 1071 1072 /* 1073 * check if a block is free 1074 */ 1075 int 1076 ffs_isfreeblock(struct fs *fs, uint8_t *cp, ufs1_daddr_t h) 1077 { 1078 1079 switch ((int)fs->fs_frag) { 1080 case 8: 1081 return (cp[h] == 0); 1082 case 4: 1083 return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0); 1084 case 2: 1085 return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0); 1086 case 1: 1087 return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0); 1088 default: 1089 #ifdef _KERNEL 1090 panic("ffs_isfreeblock"); 1091 #endif 1092 break; 1093 } 1094 return (0); 1095 } 1096 1097 /* 1098 * take a block out of the map 1099 */ 1100 void 1101 ffs_clrblock(struct fs *fs, uint8_t *cp, ufs1_daddr_t h) 1102 { 1103 1104 switch ((int)fs->fs_frag) { 1105 case 8: 1106 cp[h] = 0; 1107 return; 1108 case 4: 1109 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2)); 1110 return; 1111 case 2: 1112 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1)); 1113 return; 1114 case 1: 1115 cp[h >> 3] &= ~(0x01 << (h & 0x7)); 1116 return; 1117 default: 1118 #ifdef _KERNEL 1119 panic("ffs_clrblock"); 1120 #endif 1121 break; 1122 } 1123 } 1124 1125 /* 1126 * put a block into the map 1127 */ 1128 void 1129 ffs_setblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h) 1130 { 1131 1132 switch ((int)fs->fs_frag) { 1133 case 8: 1134 cp[h] = 0xff; 1135 return; 1136 case 4: 1137 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2)); 1138 return; 1139 case 2: 1140 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1)); 1141 return; 1142 case 1: 1143 cp[h >> 3] |= (0x01 << (h & 0x7)); 1144 return; 1145 default: 1146 #ifdef _KERNEL 1147 panic("ffs_setblock"); 1148 #endif 1149 break; 1150 } 1151 } 1152 1153 /* 1154 * Update the cluster map because of an allocation or free. 1155 * 1156 * Cnt == 1 means free; cnt == -1 means allocating. 1157 */ 1158 void 1159 ffs_clusteracct(struct fs *fs, struct cg *cgp, ufs1_daddr_t blkno, int cnt) 1160 { 1161 int32_t *sump; 1162 int32_t *lp; 1163 uint8_t *freemapp, *mapp; 1164 int i, start, end, forw, back, map; 1165 uint64_t bit; 1166 1167 if (fs->fs_contigsumsize <= 0) 1168 return; 1169 freemapp = cg_clustersfree(cgp); 1170 sump = cg_clustersum(cgp); 1171 /* 1172 * Allocate or clear the actual block. 1173 */ 1174 if (cnt > 0) 1175 setbit(freemapp, blkno); 1176 else 1177 clrbit(freemapp, blkno); 1178 /* 1179 * Find the size of the cluster going forward. 1180 */ 1181 start = blkno + 1; 1182 end = start + fs->fs_contigsumsize; 1183 if (end >= cgp->cg_nclusterblks) 1184 end = cgp->cg_nclusterblks; 1185 mapp = &freemapp[start / NBBY]; 1186 map = *mapp++; 1187 bit = 1U << (start % NBBY); 1188 for (i = start; i < end; i++) { 1189 if ((map & bit) == 0) 1190 break; 1191 if ((i & (NBBY - 1)) != (NBBY - 1)) { 1192 bit <<= 1; 1193 } else { 1194 map = *mapp++; 1195 bit = 1; 1196 } 1197 } 1198 forw = i - start; 1199 /* 1200 * Find the size of the cluster going backward. 1201 */ 1202 start = blkno - 1; 1203 end = start - fs->fs_contigsumsize; 1204 if (end < 0) 1205 end = -1; 1206 mapp = &freemapp[start / NBBY]; 1207 map = *mapp--; 1208 bit = 1U << (start % NBBY); 1209 for (i = start; i > end; i--) { 1210 if ((map & bit) == 0) 1211 break; 1212 if ((i & (NBBY - 1)) != 0) { 1213 bit >>= 1; 1214 } else { 1215 map = *mapp--; 1216 bit = 1U << (NBBY - 1); 1217 } 1218 } 1219 back = start - i; 1220 /* 1221 * Account for old cluster and the possibly new forward and 1222 * back clusters. 1223 */ 1224 i = back + forw + 1; 1225 if (i > fs->fs_contigsumsize) 1226 i = fs->fs_contigsumsize; 1227 sump[i] += cnt; 1228 if (back > 0) 1229 sump[back] -= cnt; 1230 if (forw > 0) 1231 sump[forw] -= cnt; 1232 /* 1233 * Update cluster summary information. 1234 */ 1235 lp = &sump[fs->fs_contigsumsize]; 1236 for (i = fs->fs_contigsumsize; i > 0; i--) 1237 if (*lp-- > 0) 1238 break; 1239 fs->fs_maxcluster[cgp->cg_cgx] = i; 1240 } 1241