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, 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 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(u_int8_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(u_int8_t); 240 fs->fs_contigdirs = (u_int8_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 if ((error = validate_sblock(fs, flags)) > 0) 275 return (error); 276 /* 277 * If the filesystem has been run on a kernel without 278 * metadata check hashes, disable them. 279 */ 280 if ((fs->fs_flags & FS_METACKHASH) == 0) 281 fs->fs_metackhash = 0; 282 /* 283 * Clear any check-hashes that are not maintained 284 * by this kernel. Also clear any unsupported flags. 285 */ 286 fs->fs_metackhash &= CK_SUPPORTED; 287 fs->fs_flags &= FS_SUPPORTED; 288 if (fs->fs_ckhash != (ckhash = ffs_calc_sbhash(fs))) { 289 if ((flags & (UFS_NOMSG | UFS_NOHASHFAIL)) == 290 (UFS_NOMSG | UFS_NOHASHFAIL)) 291 return (0); 292 if ((flags & UFS_NOMSG) != 0) 293 return (EINTEGRITY); 294 #ifdef _KERNEL 295 res = uprintf("Superblock check-hash failed: recorded " 296 "check-hash 0x%x != computed check-hash 0x%x%s\n", 297 fs->fs_ckhash, ckhash, 298 (flags & UFS_NOHASHFAIL) != 0 ? " (Ignored)" : ""); 299 #else 300 res = 0; 301 #endif 302 /* 303 * Print check-hash failure if no controlling terminal 304 * in kernel or always if in user-mode (libufs). 305 */ 306 if (res == 0) 307 printf("Superblock check-hash failed: recorded " 308 "check-hash 0x%x != computed check-hash " 309 "0x%x%s\n", fs->fs_ckhash, ckhash, 310 (flags & UFS_NOHASHFAIL) ? " (Ignored)" : ""); 311 if ((flags & UFS_NOHASHFAIL) != 0) 312 return (0); 313 return (EINTEGRITY); 314 } 315 /* Have to set for old filesystems that predate this field */ 316 fs->fs_sblockactualloc = sblockloc; 317 /* Not yet any summary information */ 318 fs->fs_si = NULL; 319 return (0); 320 } 321 322 /* 323 * Verify the filesystem values. 324 */ 325 #define ILOG2(num) (fls(num) - 1) 326 #ifdef STANDALONE_SMALL 327 #define MPRINT(...) do { } while (0) 328 #else 329 #define MPRINT(...) if (prtmsg) printf(__VA_ARGS__) 330 #endif 331 #define FCHK(lhs, op, rhs, fmt) \ 332 if (lhs op rhs) { \ 333 MPRINT("UFS%d superblock failed: %s (" #fmt ") %s %s (" \ 334 #fmt ")\n", fs->fs_magic == FS_UFS1_MAGIC ? 1 : 2, \ 335 #lhs, (intmax_t)lhs, #op, #rhs, (intmax_t)rhs); \ 336 if (error < 0) \ 337 return (ENOENT); \ 338 if (error == 0) \ 339 error = ENOENT; \ 340 } 341 #define WCHK(lhs, op, rhs, fmt) \ 342 if (lhs op rhs) { \ 343 MPRINT("UFS%d superblock failed: %s (" #fmt ") %s %s (" \ 344 #fmt ")%s\n", fs->fs_magic == FS_UFS1_MAGIC ? 1 : 2,\ 345 #lhs, (intmax_t)lhs, #op, #rhs, (intmax_t)rhs, wmsg);\ 346 if (error == 0) \ 347 error = warnerr; \ 348 } 349 #define FCHK2(lhs1, op1, rhs1, lhs2, op2, rhs2, fmt) \ 350 if (lhs1 op1 rhs1 && lhs2 op2 rhs2) { \ 351 MPRINT("UFS%d superblock failed: %s (" #fmt ") %s %s (" \ 352 #fmt ") && %s (" #fmt ") %s %s (" #fmt ")\n", \ 353 fs->fs_magic == FS_UFS1_MAGIC ? 1 : 2, #lhs1, \ 354 (intmax_t)lhs1, #op1, #rhs1, (intmax_t)rhs1, #lhs2, \ 355 (intmax_t)lhs2, #op2, #rhs2, (intmax_t)rhs2); \ 356 if (error < 0) \ 357 return (ENOENT); \ 358 if (error == 0) \ 359 error = ENOENT; \ 360 } 361 #define WCHK2(lhs1, op1, rhs1, lhs2, op2, rhs2, fmt) \ 362 if (lhs1 op1 rhs1 && lhs2 op2 rhs2) { \ 363 MPRINT("UFS%d superblock failed: %s (" #fmt ") %s %s (" \ 364 #fmt ") && %s (" #fmt ") %s %s (" #fmt ")%s\n", \ 365 fs->fs_magic == FS_UFS1_MAGIC ? 1 : 2, #lhs1, \ 366 (intmax_t)lhs1, #op1, #rhs1, (intmax_t)rhs1, #lhs2, \ 367 (intmax_t)lhs2, #op2, #rhs2, (intmax_t)rhs2, wmsg); \ 368 if (error == 0) \ 369 error = warnerr; \ 370 } 371 372 static int 373 validate_sblock(struct fs *fs, int flags) 374 { 375 u_long i, sectorsize; 376 u_int64_t maxfilesize, sizepb; 377 int error, prtmsg, warnerr; 378 char *wmsg; 379 380 error = 0; 381 sectorsize = dbtob(1); 382 prtmsg = ((flags & UFS_NOMSG) == 0); 383 warnerr = (flags & UFS_NOWARNFAIL) == UFS_NOWARNFAIL ? 0 : ENOENT; 384 wmsg = warnerr ? "" : " (Ignored)"; 385 /* 386 * If just validating for recovery, then do just the minimal 387 * checks needed for the superblock fields needed to find 388 * alternate superblocks. 389 */ 390 if ((flags & UFS_FSRONLY) == UFS_FSRONLY && 391 (fs->fs_magic == FS_UFS1_MAGIC || fs->fs_magic == FS_UFS2_MAGIC)) { 392 error = -1; /* fail on first error */ 393 if (fs->fs_magic == FS_UFS2_MAGIC) { 394 FCHK(fs->fs_sblockloc, !=, SBLOCK_UFS2, %#jx); 395 } else if (fs->fs_magic == FS_UFS1_MAGIC) { 396 FCHK(fs->fs_sblockloc, <, 0, %jd); 397 FCHK(fs->fs_sblockloc, >, SBLOCK_UFS1, %jd); 398 } 399 FCHK(fs->fs_frag, <, 1, %jd); 400 FCHK(fs->fs_frag, >, MAXFRAG, %jd); 401 FCHK(fs->fs_bsize, <, MINBSIZE, %jd); 402 FCHK(fs->fs_bsize, >, MAXBSIZE, %jd); 403 FCHK(fs->fs_bsize, <, roundup(sizeof(struct fs), DEV_BSIZE), 404 %jd); 405 FCHK(fs->fs_fsize, <, sectorsize, %jd); 406 FCHK(fs->fs_fsize * fs->fs_frag, !=, fs->fs_bsize, %jd); 407 FCHK(powerof2(fs->fs_fsize), ==, 0, %jd); 408 FCHK(fs->fs_sbsize, >, SBLOCKSIZE, %jd); 409 FCHK(fs->fs_sbsize, <, (signed)sizeof(struct fs), %jd); 410 FCHK(fs->fs_sbsize % fs->fs_fsize, !=, 0, %jd); 411 FCHK(fs->fs_fpg, <, 3 * fs->fs_frag, %jd); 412 FCHK(fs->fs_ncg, <, 1, %jd); 413 FCHK(fs->fs_fsbtodb, !=, ILOG2(fs->fs_fsize / sectorsize), %jd); 414 FCHK(fs->fs_old_cgoffset, <, 0, %jd); 415 FCHK2(fs->fs_old_cgoffset, >, 0, ~fs->fs_old_cgmask, <, 0, %jd); 416 FCHK(fs->fs_old_cgoffset * (~fs->fs_old_cgmask), >, fs->fs_fpg, 417 %jd); 418 FCHK(fs->fs_sblkno, !=, roundup( 419 howmany(fs->fs_sblockloc + SBLOCKSIZE, fs->fs_fsize), 420 fs->fs_frag), %jd); 421 return (error); 422 } 423 if (fs->fs_magic == FS_UFS2_MAGIC) { 424 if ((flags & UFS_ALTSBLK) == 0) 425 FCHK2(fs->fs_sblockactualloc, !=, SBLOCK_UFS2, 426 fs->fs_sblockactualloc, !=, 0, %jd); 427 FCHK(fs->fs_sblockloc, !=, SBLOCK_UFS2, %#jx); 428 FCHK(fs->fs_maxsymlinklen, !=, ((UFS_NDADDR + UFS_NIADDR) * 429 sizeof(ufs2_daddr_t)), %jd); 430 FCHK(fs->fs_nindir, !=, fs->fs_bsize / sizeof(ufs2_daddr_t), 431 %jd); 432 FCHK(fs->fs_inopb, !=, 433 fs->fs_bsize / sizeof(struct ufs2_dinode), %jd); 434 } else if (fs->fs_magic == FS_UFS1_MAGIC) { 435 if ((flags & UFS_ALTSBLK) == 0) 436 FCHK(fs->fs_sblockactualloc, >, SBLOCK_UFS1, %jd); 437 FCHK(fs->fs_sblockloc, <, 0, %jd); 438 FCHK(fs->fs_sblockloc, >, SBLOCK_UFS1, %jd); 439 FCHK(fs->fs_nindir, !=, fs->fs_bsize / sizeof(ufs1_daddr_t), 440 %jd); 441 FCHK(fs->fs_inopb, !=, 442 fs->fs_bsize / sizeof(struct ufs1_dinode), %jd); 443 FCHK(fs->fs_maxsymlinklen, !=, ((UFS_NDADDR + UFS_NIADDR) * 444 sizeof(ufs1_daddr_t)), %jd); 445 WCHK(fs->fs_old_inodefmt, !=, FS_44INODEFMT, %jd); 446 WCHK(fs->fs_old_rotdelay, !=, 0, %jd); 447 WCHK(fs->fs_old_rps, !=, 60, %jd); 448 WCHK(fs->fs_old_nspf, !=, fs->fs_fsize / sectorsize, %jd); 449 FCHK(fs->fs_old_cpg, !=, 1, %jd); 450 WCHK(fs->fs_old_interleave, !=, 1, %jd); 451 WCHK(fs->fs_old_trackskew, !=, 0, %jd); 452 WCHK(fs->fs_old_cpc, !=, 0, %jd); 453 WCHK(fs->fs_old_postblformat, !=, 1, %jd); 454 FCHK(fs->fs_old_nrpos, !=, 1, %jd); 455 WCHK(fs->fs_old_spc, !=, fs->fs_fpg * fs->fs_old_nspf, %jd); 456 WCHK(fs->fs_old_nsect, !=, fs->fs_old_spc, %jd); 457 WCHK(fs->fs_old_npsect, !=, fs->fs_old_spc, %jd); 458 FCHK(fs->fs_old_ncyl, !=, fs->fs_ncg, %jd); 459 } else { 460 /* Bad magic number, so assume not a superblock */ 461 return (ENOENT); 462 } 463 FCHK(fs->fs_bsize, <, MINBSIZE, %jd); 464 FCHK(fs->fs_bsize, >, MAXBSIZE, %jd); 465 FCHK(fs->fs_bsize, <, roundup(sizeof(struct fs), DEV_BSIZE), %jd); 466 FCHK(powerof2(fs->fs_bsize), ==, 0, %jd); 467 FCHK(fs->fs_frag, <, 1, %jd); 468 FCHK(fs->fs_frag, >, MAXFRAG, %jd); 469 FCHK(fs->fs_frag, !=, numfrags(fs, fs->fs_bsize), %jd); 470 FCHK(fs->fs_fsize, <, sectorsize, %jd); 471 FCHK(fs->fs_fsize * fs->fs_frag, !=, fs->fs_bsize, %jd); 472 FCHK(powerof2(fs->fs_fsize), ==, 0, %jd); 473 FCHK(fs->fs_fpg, <, 3 * fs->fs_frag, %jd); 474 FCHK(fs->fs_ncg, <, 1, %jd); 475 FCHK(fs->fs_ipg, <, fs->fs_inopb, %jd); 476 FCHK(fs->fs_ipg * fs->fs_ncg, >, (((int64_t)(1)) << 32) - INOPB(fs), 477 %jd); 478 FCHK(fs->fs_cstotal.cs_nifree, <, 0, %jd); 479 FCHK(fs->fs_cstotal.cs_nifree, >, fs->fs_ipg * fs->fs_ncg, %jd); 480 FCHK(fs->fs_cstotal.cs_ndir, <, 0, %jd); 481 FCHK(fs->fs_cstotal.cs_ndir, >, 482 (fs->fs_ipg * fs->fs_ncg) - fs->fs_cstotal.cs_nifree, %jd); 483 FCHK(fs->fs_sbsize, >, SBLOCKSIZE, %jd); 484 FCHK(fs->fs_sbsize, <, (signed)sizeof(struct fs), %jd); 485 FCHK(fs->fs_maxbsize, <, fs->fs_bsize, %jd); 486 FCHK(powerof2(fs->fs_maxbsize), ==, 0, %jd); 487 FCHK(fs->fs_maxbsize, >, FS_MAXCONTIG * fs->fs_bsize, %jd); 488 FCHK(fs->fs_bmask, !=, ~(fs->fs_bsize - 1), %#jx); 489 FCHK(fs->fs_fmask, !=, ~(fs->fs_fsize - 1), %#jx); 490 FCHK(fs->fs_qbmask, !=, ~fs->fs_bmask, %#jx); 491 FCHK(fs->fs_qfmask, !=, ~fs->fs_fmask, %#jx); 492 FCHK(fs->fs_bshift, !=, ILOG2(fs->fs_bsize), %jd); 493 FCHK(fs->fs_fshift, !=, ILOG2(fs->fs_fsize), %jd); 494 FCHK(fs->fs_fragshift, !=, ILOG2(fs->fs_frag), %jd); 495 FCHK(fs->fs_fsbtodb, !=, ILOG2(fs->fs_fsize / sectorsize), %jd); 496 FCHK(fs->fs_old_cgoffset, <, 0, %jd); 497 FCHK2(fs->fs_old_cgoffset, >, 0, ~fs->fs_old_cgmask, <, 0, %jd); 498 FCHK(fs->fs_old_cgoffset * (~fs->fs_old_cgmask), >, fs->fs_fpg, %jd); 499 /* 500 * If anything has failed up to this point, it is usafe to proceed 501 * as checks below may divide by zero or make other fatal calculations. 502 * So if we have any errors at this point, give up. 503 */ 504 if (error) 505 return (error); 506 FCHK(fs->fs_sbsize % fs->fs_fsize, !=, 0, %jd); 507 FCHK(fs->fs_ipg % fs->fs_inopb, !=, 0, %jd); 508 FCHK(fs->fs_sblkno, !=, roundup( 509 howmany(fs->fs_sblockloc + SBLOCKSIZE, fs->fs_fsize), 510 fs->fs_frag), %jd); 511 FCHK(fs->fs_cblkno, !=, fs->fs_sblkno + 512 roundup(howmany(SBLOCKSIZE, fs->fs_fsize), fs->fs_frag), %jd); 513 FCHK(fs->fs_iblkno, !=, fs->fs_cblkno + fs->fs_frag, %jd); 514 FCHK(fs->fs_dblkno, !=, fs->fs_iblkno + fs->fs_ipg / INOPF(fs), %jd); 515 FCHK(fs->fs_cgsize, >, fs->fs_bsize, %jd); 516 FCHK(fs->fs_cgsize, <, fs->fs_fsize, %jd); 517 FCHK(fs->fs_cgsize % fs->fs_fsize, !=, 0, %jd); 518 /* 519 * This test is valid, however older versions of growfs failed 520 * to correctly update fs_dsize so will fail this test. Thus we 521 * exclude it from the requirements. 522 */ 523 #ifdef notdef 524 WCHK(fs->fs_dsize, !=, fs->fs_size - fs->fs_sblkno - 525 fs->fs_ncg * (fs->fs_dblkno - fs->fs_sblkno) - 526 howmany(fs->fs_cssize, fs->fs_fsize), %jd); 527 #endif 528 WCHK(fs->fs_metaspace, <, 0, %jd); 529 WCHK(fs->fs_metaspace, >, fs->fs_fpg / 2, %jd); 530 WCHK(fs->fs_minfree, >, 99, %jd%%); 531 maxfilesize = fs->fs_bsize * UFS_NDADDR - 1; 532 for (sizepb = fs->fs_bsize, i = 0; i < UFS_NIADDR; i++) { 533 sizepb *= NINDIR(fs); 534 maxfilesize += sizepb; 535 } 536 WCHK(fs->fs_maxfilesize, !=, maxfilesize, %jd); 537 /* 538 * These values have a tight interaction with each other that 539 * makes it hard to tightly bound them. So we can only check 540 * that they are within a broader possible range. 541 * 542 * The size cannot always be accurately determined, but ensure 543 * that it is consistent with the number of cylinder groups (fs_ncg) 544 * and the number of fragments per cylinder group (fs_fpg). Ensure 545 * that the summary information size is correct and that it starts 546 * and ends in the data area of the same cylinder group. 547 */ 548 FCHK(fs->fs_size, <, 8 * fs->fs_frag, %jd); 549 FCHK(fs->fs_size, <=, (fs->fs_ncg - 1) * fs->fs_fpg, %jd); 550 FCHK(fs->fs_size, >, fs->fs_ncg * fs->fs_fpg, %jd); 551 /* 552 * If we are not requested to read in the csum data stop here 553 * as the correctness of the remaining values is only important 554 * to bound the space needed to be allocated to hold the csum data. 555 */ 556 if ((flags & UFS_NOCSUM) != 0) 557 return (error); 558 FCHK(fs->fs_csaddr, <, 0, %jd); 559 FCHK(fs->fs_cssize, !=, 560 fragroundup(fs, fs->fs_ncg * sizeof(struct csum)), %jd); 561 FCHK(dtog(fs, fs->fs_csaddr), >, fs->fs_ncg, %jd); 562 FCHK(fs->fs_csaddr, <, cgdmin(fs, dtog(fs, fs->fs_csaddr)), %jd); 563 FCHK(dtog(fs, fs->fs_csaddr + howmany(fs->fs_cssize, fs->fs_fsize)), >, 564 dtog(fs, fs->fs_csaddr), %jd); 565 /* 566 * With file system clustering it is possible to allocate 567 * many contiguous blocks. The kernel variable maxphys defines 568 * the maximum transfer size permitted by the controller and/or 569 * buffering. The fs_maxcontig parameter controls the maximum 570 * number of blocks that the filesystem will read or write 571 * in a single transfer. It is calculated when the filesystem 572 * is created as maxphys / fs_bsize. The loader uses a maxphys 573 * of 128K even when running on a system that supports larger 574 * values. If the filesystem was built on a system that supports 575 * a larger maxphys (1M is typical) it will have configured 576 * fs_maxcontig for that larger system. So we bound the upper 577 * allowable limit for fs_maxconfig to be able to at least 578 * work with a 1M maxphys on the smallest block size filesystem: 579 * 1M / 4096 == 256. There is no harm in allowing the mounting of 580 * filesystems that make larger than maxphys I/O requests because 581 * those (mostly 32-bit machines) can (very slowly) handle I/O 582 * requests that exceed maxphys. 583 */ 584 WCHK(fs->fs_maxcontig, <, 0, %jd); 585 WCHK(fs->fs_maxcontig, >, MAX(256, maxphys / fs->fs_bsize), %jd); 586 FCHK2(fs->fs_maxcontig, ==, 0, fs->fs_contigsumsize, !=, 0, %jd); 587 FCHK2(fs->fs_maxcontig, >, 1, fs->fs_contigsumsize, !=, 588 MIN(fs->fs_maxcontig, FS_MAXCONTIG), %jd); 589 return (error); 590 } 591 592 /* 593 * Make an extensive search to find a superblock. If the superblock 594 * in the standard place cannot be used, try looking for one of the 595 * backup superblocks. 596 * 597 * Flags are made up of the following or'ed together options: 598 * 599 * UFS_NOMSG indicates that superblock inconsistency error messages 600 * should not be printed. 601 * 602 * UFS_NOCSUM causes only the superblock itself to be returned, but does 603 * not read in any auxillary data structures like the cylinder group 604 * summary information. 605 */ 606 int 607 ffs_sbsearch(void *devfd, struct fs **fsp, int reqflags, 608 struct malloc_type *filltype, 609 int (*readfunc)(void *devfd, off_t loc, void **bufp, int size)) 610 { 611 struct fsrecovery *fsr; 612 struct fs *protofs; 613 void *fsrbuf; 614 char *cp; 615 long nocsum, flags, msg, cg; 616 off_t sblk, secsize; 617 int error; 618 619 msg = (reqflags & UFS_NOMSG) == 0; 620 nocsum = reqflags & UFS_NOCSUM; 621 /* 622 * Try normal superblock read and return it if it works. 623 * 624 * Suppress messages if it fails until we find out if 625 * failure can be avoided. 626 */ 627 flags = UFS_NOMSG | nocsum; 628 if (ffs_sbget(devfd, fsp, UFS_STDSB, flags, filltype, readfunc) == 0) 629 return (0); 630 /* 631 * First try: ignoring hash failures. 632 */ 633 flags |= UFS_NOHASHFAIL; 634 if (msg) 635 flags &= ~UFS_NOMSG; 636 if (ffs_sbget(devfd, fsp, UFS_STDSB, flags, filltype, readfunc) == 0) 637 return (0); 638 /* 639 * Next up is to check if fields of the superblock that are 640 * needed to find backup superblocks are usable. 641 */ 642 if (msg) 643 printf("Attempted recovery for standard superblock: failed\n"); 644 flags = UFS_FSRONLY | UFS_NOHASHFAIL | UFS_NOMSG; 645 if (ffs_sbget(devfd, &protofs, UFS_STDSB, flags, filltype, 646 readfunc) == 0) { 647 if (msg) 648 printf("Attempt extraction of recovery data from " 649 "standard superblock.\n"); 650 } else { 651 /* 652 * Final desperation is to see if alternate superblock 653 * parameters have been saved in the boot area. 654 */ 655 if (msg) 656 printf("Attempted extraction of recovery data from " 657 "standard superblock: failed\nAttempt to find " 658 "boot zone recovery data.\n"); 659 /* 660 * Look to see if recovery information has been saved. 661 * If so we can generate a prototype superblock based 662 * on that information. 663 * 664 * We need fragments-per-group, number of cylinder groups, 665 * location of the superblock within the cylinder group, and 666 * the conversion from filesystem fragments to disk blocks. 667 * 668 * When building a UFS2 filesystem, newfs(8) stores these 669 * details at the end of the boot block area at the start 670 * of the filesystem partition. If they have been overwritten 671 * by a boot block, we fail. But usually they are there 672 * and we can use them. 673 * 674 * We could ask the underlying device for its sector size, 675 * but some devices lie. So we just try a plausible range. 676 */ 677 error = ENOENT; 678 for (secsize = dbtob(1); secsize <= SBLOCKSIZE; secsize *= 2) 679 if ((error = (*readfunc)(devfd, (SBLOCK_UFS2 - secsize), 680 &fsrbuf, secsize)) == 0) 681 break; 682 if (error != 0) 683 goto trynowarn; 684 cp = fsrbuf; /* type change to keep compiler happy */ 685 fsr = (struct fsrecovery *)&cp[secsize - sizeof *fsr]; 686 if (fsr->fsr_magic != FS_UFS2_MAGIC || 687 (protofs = UFS_MALLOC(SBLOCKSIZE, filltype, M_NOWAIT)) 688 == NULL) { 689 UFS_FREE(fsrbuf, filltype); 690 goto trynowarn; 691 } 692 memset(protofs, 0, sizeof(struct fs)); 693 protofs->fs_fpg = fsr->fsr_fpg; 694 protofs->fs_fsbtodb = fsr->fsr_fsbtodb; 695 protofs->fs_sblkno = fsr->fsr_sblkno; 696 protofs->fs_magic = fsr->fsr_magic; 697 protofs->fs_ncg = fsr->fsr_ncg; 698 UFS_FREE(fsrbuf, filltype); 699 } 700 /* 701 * Scan looking for alternative superblocks. 702 */ 703 flags = nocsum; 704 if (!msg) 705 flags |= UFS_NOMSG; 706 for (cg = 0; cg < protofs->fs_ncg; cg++) { 707 sblk = fsbtodb(protofs, cgsblock(protofs, cg)); 708 if (msg) 709 printf("Try cg %ld at sblock loc %jd\n", cg, 710 (intmax_t)sblk); 711 if (ffs_sbget(devfd, fsp, dbtob(sblk), flags, filltype, 712 readfunc) == 0) { 713 if (msg) 714 printf("Succeeded with alternate superblock " 715 "at %jd\n", (intmax_t)sblk); 716 UFS_FREE(protofs, filltype); 717 return (0); 718 } 719 } 720 UFS_FREE(protofs, filltype); 721 /* 722 * Our alternate superblock strategies failed. Our last ditch effort 723 * is to see if the standard superblock has only non-critical errors. 724 */ 725 trynowarn: 726 flags = UFS_NOWARNFAIL | UFS_NOMSG | nocsum; 727 if (msg) { 728 printf("Finding an alternate superblock failed.\nCheck for " 729 "only non-critical errors in standard superblock\n"); 730 flags &= ~UFS_NOMSG; 731 } 732 if (ffs_sbget(devfd, fsp, UFS_STDSB, flags, filltype, readfunc) != 0) { 733 if (msg) 734 printf("Failed, superblock has critical errors\n"); 735 return (ENOENT); 736 } 737 if (msg) 738 printf("Success, using standard superblock with " 739 "non-critical errors.\n"); 740 return (0); 741 } 742 743 /* 744 * Write a superblock to the devfd device from the memory pointed to by fs. 745 * Write out the superblock summary information if it is present. 746 * 747 * If the write is successful, zero is returned. Otherwise one of the 748 * following error values is returned: 749 * EIO: failed to write superblock. 750 * EIO: failed to write superblock summary information. 751 */ 752 int 753 ffs_sbput(void *devfd, struct fs *fs, off_t loc, 754 int (*writefunc)(void *devfd, off_t loc, void *buf, int size)) 755 { 756 int i, error, blks, size; 757 uint8_t *space; 758 759 /* 760 * If there is summary information, write it first, so if there 761 * is an error, the superblock will not be marked as clean. 762 */ 763 if (fs->fs_si != NULL && fs->fs_csp != NULL) { 764 blks = howmany(fs->fs_cssize, fs->fs_fsize); 765 space = (uint8_t *)fs->fs_csp; 766 for (i = 0; i < blks; i += fs->fs_frag) { 767 size = fs->fs_bsize; 768 if (i + fs->fs_frag > blks) 769 size = (blks - i) * fs->fs_fsize; 770 if ((error = (*writefunc)(devfd, 771 dbtob(fsbtodb(fs, fs->fs_csaddr + i)), 772 space, size)) != 0) 773 return (error); 774 space += size; 775 } 776 } 777 fs->fs_fmod = 0; 778 #ifndef _KERNEL 779 { 780 struct fs_summary_info *fs_si; 781 782 fs->fs_time = time(NULL); 783 /* Clear the pointers for the duration of writing. */ 784 fs_si = fs->fs_si; 785 fs->fs_si = NULL; 786 fs->fs_ckhash = ffs_calc_sbhash(fs); 787 error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize); 788 fs->fs_si = fs_si; 789 } 790 #else /* _KERNEL */ 791 fs->fs_time = time_second; 792 fs->fs_ckhash = ffs_calc_sbhash(fs); 793 error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize); 794 #endif /* _KERNEL */ 795 return (error); 796 } 797 798 /* 799 * Calculate the check-hash for a superblock. 800 */ 801 uint32_t 802 ffs_calc_sbhash(struct fs *fs) 803 { 804 uint32_t ckhash, save_ckhash; 805 806 /* 807 * A filesystem that was using a superblock ckhash may be moved 808 * to an older kernel that does not support ckhashes. The 809 * older kernel will clear the FS_METACKHASH flag indicating 810 * that it does not update hashes. When the disk is moved back 811 * to a kernel capable of ckhashes it disables them on mount: 812 * 813 * if ((fs->fs_flags & FS_METACKHASH) == 0) 814 * fs->fs_metackhash = 0; 815 * 816 * This leaves (fs->fs_metackhash & CK_SUPERBLOCK) == 0) with an 817 * old stale value in the fs->fs_ckhash field. Thus the need to 818 * just accept what is there. 819 */ 820 if ((fs->fs_metackhash & CK_SUPERBLOCK) == 0) 821 return (fs->fs_ckhash); 822 823 save_ckhash = fs->fs_ckhash; 824 fs->fs_ckhash = 0; 825 /* 826 * If newly read from disk, the caller is responsible for 827 * verifying that fs->fs_sbsize <= SBLOCKSIZE. 828 */ 829 ckhash = calculate_crc32c(~0L, (void *)fs, fs->fs_sbsize); 830 fs->fs_ckhash = save_ckhash; 831 return (ckhash); 832 } 833 834 /* 835 * Update the frsum fields to reflect addition or deletion 836 * of some frags. 837 */ 838 void 839 ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt) 840 { 841 int inblk; 842 int field, subfield; 843 int siz, pos; 844 845 inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1; 846 fragmap <<= 1; 847 for (siz = 1; siz < fs->fs_frag; siz++) { 848 if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0) 849 continue; 850 field = around[siz]; 851 subfield = inside[siz]; 852 for (pos = siz; pos <= fs->fs_frag; pos++) { 853 if ((fragmap & field) == subfield) { 854 fraglist[siz] += cnt; 855 pos += siz; 856 field <<= siz; 857 subfield <<= siz; 858 } 859 field <<= 1; 860 subfield <<= 1; 861 } 862 } 863 } 864 865 /* 866 * block operations 867 * 868 * check if a block is available 869 */ 870 int 871 ffs_isblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h) 872 { 873 unsigned char mask; 874 875 switch ((int)fs->fs_frag) { 876 case 8: 877 return (cp[h] == 0xff); 878 case 4: 879 mask = 0x0f << ((h & 0x1) << 2); 880 return ((cp[h >> 1] & mask) == mask); 881 case 2: 882 mask = 0x03 << ((h & 0x3) << 1); 883 return ((cp[h >> 2] & mask) == mask); 884 case 1: 885 mask = 0x01 << (h & 0x7); 886 return ((cp[h >> 3] & mask) == mask); 887 default: 888 #ifdef _KERNEL 889 panic("ffs_isblock"); 890 #endif 891 break; 892 } 893 return (0); 894 } 895 896 /* 897 * check if a block is free 898 */ 899 int 900 ffs_isfreeblock(struct fs *fs, u_char *cp, ufs1_daddr_t h) 901 { 902 903 switch ((int)fs->fs_frag) { 904 case 8: 905 return (cp[h] == 0); 906 case 4: 907 return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0); 908 case 2: 909 return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0); 910 case 1: 911 return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0); 912 default: 913 #ifdef _KERNEL 914 panic("ffs_isfreeblock"); 915 #endif 916 break; 917 } 918 return (0); 919 } 920 921 /* 922 * take a block out of the map 923 */ 924 void 925 ffs_clrblock(struct fs *fs, u_char *cp, ufs1_daddr_t h) 926 { 927 928 switch ((int)fs->fs_frag) { 929 case 8: 930 cp[h] = 0; 931 return; 932 case 4: 933 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2)); 934 return; 935 case 2: 936 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1)); 937 return; 938 case 1: 939 cp[h >> 3] &= ~(0x01 << (h & 0x7)); 940 return; 941 default: 942 #ifdef _KERNEL 943 panic("ffs_clrblock"); 944 #endif 945 break; 946 } 947 } 948 949 /* 950 * put a block into the map 951 */ 952 void 953 ffs_setblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h) 954 { 955 956 switch ((int)fs->fs_frag) { 957 case 8: 958 cp[h] = 0xff; 959 return; 960 case 4: 961 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2)); 962 return; 963 case 2: 964 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1)); 965 return; 966 case 1: 967 cp[h >> 3] |= (0x01 << (h & 0x7)); 968 return; 969 default: 970 #ifdef _KERNEL 971 panic("ffs_setblock"); 972 #endif 973 break; 974 } 975 } 976 977 /* 978 * Update the cluster map because of an allocation or free. 979 * 980 * Cnt == 1 means free; cnt == -1 means allocating. 981 */ 982 void 983 ffs_clusteracct(struct fs *fs, struct cg *cgp, ufs1_daddr_t blkno, int cnt) 984 { 985 int32_t *sump; 986 int32_t *lp; 987 u_char *freemapp, *mapp; 988 int i, start, end, forw, back, map; 989 u_int bit; 990 991 if (fs->fs_contigsumsize <= 0) 992 return; 993 freemapp = cg_clustersfree(cgp); 994 sump = cg_clustersum(cgp); 995 /* 996 * Allocate or clear the actual block. 997 */ 998 if (cnt > 0) 999 setbit(freemapp, blkno); 1000 else 1001 clrbit(freemapp, blkno); 1002 /* 1003 * Find the size of the cluster going forward. 1004 */ 1005 start = blkno + 1; 1006 end = start + fs->fs_contigsumsize; 1007 if (end >= cgp->cg_nclusterblks) 1008 end = cgp->cg_nclusterblks; 1009 mapp = &freemapp[start / NBBY]; 1010 map = *mapp++; 1011 bit = 1U << (start % NBBY); 1012 for (i = start; i < end; i++) { 1013 if ((map & bit) == 0) 1014 break; 1015 if ((i & (NBBY - 1)) != (NBBY - 1)) { 1016 bit <<= 1; 1017 } else { 1018 map = *mapp++; 1019 bit = 1; 1020 } 1021 } 1022 forw = i - start; 1023 /* 1024 * Find the size of the cluster going backward. 1025 */ 1026 start = blkno - 1; 1027 end = start - fs->fs_contigsumsize; 1028 if (end < 0) 1029 end = -1; 1030 mapp = &freemapp[start / NBBY]; 1031 map = *mapp--; 1032 bit = 1U << (start % NBBY); 1033 for (i = start; i > end; i--) { 1034 if ((map & bit) == 0) 1035 break; 1036 if ((i & (NBBY - 1)) != 0) { 1037 bit >>= 1; 1038 } else { 1039 map = *mapp--; 1040 bit = 1U << (NBBY - 1); 1041 } 1042 } 1043 back = start - i; 1044 /* 1045 * Account for old cluster and the possibly new forward and 1046 * back clusters. 1047 */ 1048 i = back + forw + 1; 1049 if (i > fs->fs_contigsumsize) 1050 i = fs->fs_contigsumsize; 1051 sump[i] += cnt; 1052 if (back > 0) 1053 sump[back] -= cnt; 1054 if (forw > 0) 1055 sump[forw] -= cnt; 1056 /* 1057 * Update cluster summary information. 1058 */ 1059 lp = &sump[fs->fs_contigsumsize]; 1060 for (i = fs->fs_contigsumsize; i > 0; i--) 1061 if (*lp-- > 0) 1062 break; 1063 fs->fs_maxcluster[cgp->cg_cgx] = i; 1064 } 1065