1/* 2 * Copyright (c) 1980, 1986, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30#if 0 31#ifndef lint 32static const char sccsid[] = "@(#)setup.c 8.10 (Berkeley) 5/9/95"; 33#endif /* not lint */ 34#endif 35#include <sys/cdefs.h> 36__FBSDID("$FreeBSD: stable/11/sbin/fsck_ffs/setup.c 359754 2020-04-09 20:38:36Z kevans $"); 37 38#include <sys/param.h> 39#include <sys/disk.h> 40#include <sys/stat.h> 41#define FSTYPENAMES 42#include <sys/disklabel.h> 43#include <sys/file.h> 44#include <sys/sysctl.h> 45 46#include <ufs/ufs/dinode.h> 47#include <ufs/ffs/fs.h> 48 49#include <ctype.h> 50#include <err.h> 51#include <errno.h> 52#include <limits.h> 53#include <stdint.h> 54#include <string.h> 55 56#include "fsck.h" 57 58struct inoinfo **inphead, **inpsort; 59 60struct bufarea asblk; 61#define altsblock (*asblk.b_un.b_fs) 62#define POWEROF2(num) (((num) & ((num) - 1)) == 0) 63 64static int calcsb(char *dev, int devfd, struct fs *fs); 65static void saverecovery(int readfd, int writefd); 66static int chkrecovery(int devfd); 67 68/* 69 * Read in a superblock finding an alternate if necessary. 70 * Return 1 if successful, 0 if unsuccessful, -1 if file system 71 * is already clean (ckclean and preen mode only). 72 */ 73int 74setup(char *dev) 75{ 76 long cg, asked, i, j; 77 long bmapsize; 78 struct stat statb; 79 struct fs proto; 80 size_t size; 81 82 havesb = 0; 83 fswritefd = -1; 84 cursnapshot = 0; 85 if (stat(dev, &statb) < 0) { 86 printf("Can't stat %s: %s\n", dev, strerror(errno)); 87 if (bkgrdflag) { 88 unlink(snapname); 89 bkgrdflag = 0; 90 } 91 return (0); 92 } 93 if ((statb.st_mode & S_IFMT) != S_IFCHR && 94 (statb.st_mode & S_IFMT) != S_IFBLK) { 95 if (bkgrdflag != 0 && (statb.st_flags & SF_SNAPSHOT) == 0) { 96 unlink(snapname); 97 printf("background fsck lacks a snapshot\n"); 98 exit(EEXIT); 99 } 100 if ((statb.st_flags & SF_SNAPSHOT) != 0 && cvtlevel == 0) { 101 cursnapshot = statb.st_ino; 102 } else { 103 if (cvtlevel == 0 || 104 (statb.st_flags & SF_SNAPSHOT) == 0) { 105 if (preen && bkgrdflag) { 106 unlink(snapname); 107 bkgrdflag = 0; 108 } 109 pfatal("%s is not a disk device", dev); 110 if (reply("CONTINUE") == 0) { 111 if (bkgrdflag) { 112 unlink(snapname); 113 bkgrdflag = 0; 114 } 115 return (0); 116 } 117 } else { 118 if (bkgrdflag) { 119 unlink(snapname); 120 bkgrdflag = 0; 121 } 122 pfatal("cannot convert a snapshot"); 123 exit(EEXIT); 124 } 125 } 126 } 127 if ((fsreadfd = open(dev, O_RDONLY)) < 0) { 128 if (bkgrdflag) { 129 unlink(snapname); 130 bkgrdflag = 0; 131 } 132 printf("Can't open %s: %s\n", dev, strerror(errno)); 133 return (0); 134 } 135 if (bkgrdflag) { 136 unlink(snapname); 137 size = MIBSIZE; 138 if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0|| 139 sysctlnametomib("vfs.ffs.adjblkcnt", adjblkcnt, &size) < 0|| 140 sysctlnametomib("vfs.ffs.setsize", setsize, &size) < 0 || 141 sysctlnametomib("vfs.ffs.freefiles", freefiles, &size) < 0|| 142 sysctlnametomib("vfs.ffs.freedirs", freedirs, &size) < 0 || 143 sysctlnametomib("vfs.ffs.freeblks", freeblks, &size) < 0) { 144 pfatal("kernel lacks background fsck support\n"); 145 exit(EEXIT); 146 } 147 /* 148 * When kernel is lack of runtime bgfsck superblock summary 149 * adjustment functionality, it does not mean we can not 150 * continue, as old kernels will recompute the summary at 151 * mount time. However, it will be an unexpected softupdates 152 * inconsistency if it turns out that the summary is still 153 * incorrect. Set a flag so subsequent operation can know 154 * this. 155 */ 156 bkgrdsumadj = 1; 157 if (sysctlnametomib("vfs.ffs.adjndir", adjndir, &size) < 0 || 158 sysctlnametomib("vfs.ffs.adjnbfree", adjnbfree, &size) < 0 || 159 sysctlnametomib("vfs.ffs.adjnifree", adjnifree, &size) < 0 || 160 sysctlnametomib("vfs.ffs.adjnffree", adjnffree, &size) < 0 || 161 sysctlnametomib("vfs.ffs.adjnumclusters", adjnumclusters, &size) < 0) { 162 bkgrdsumadj = 0; 163 pwarn("kernel lacks runtime superblock summary adjustment support"); 164 } 165 cmd.version = FFS_CMD_VERSION; 166 cmd.handle = fsreadfd; 167 fswritefd = -1; 168 } 169 if (preen == 0) 170 printf("** %s", dev); 171 if (bkgrdflag == 0 && 172 (nflag || (fswritefd = open(dev, O_WRONLY)) < 0)) { 173 fswritefd = -1; 174 if (preen) 175 pfatal("NO WRITE ACCESS"); 176 printf(" (NO WRITE)"); 177 } 178 if (preen == 0) 179 printf("\n"); 180 /* 181 * Read in the superblock, looking for alternates if necessary 182 */ 183 if (readsb(1) == 0) { 184 skipclean = 0; 185 if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0) 186 return(0); 187 if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0) 188 return (0); 189 for (cg = 0; cg < proto.fs_ncg; cg++) { 190 bflag = fsbtodb(&proto, cgsblock(&proto, cg)); 191 if (readsb(0) != 0) 192 break; 193 } 194 if (cg >= proto.fs_ncg) { 195 printf("%s %s\n%s %s\n%s %s\n", 196 "SEARCH FOR ALTERNATE SUPER-BLOCK", 197 "FAILED. YOU MUST USE THE", 198 "-b OPTION TO FSCK TO SPECIFY THE", 199 "LOCATION OF AN ALTERNATE", 200 "SUPER-BLOCK TO SUPPLY NEEDED", 201 "INFORMATION; SEE fsck_ffs(8)."); 202 bflag = 0; 203 return(0); 204 } 205 pwarn("USING ALTERNATE SUPERBLOCK AT %jd\n", bflag); 206 bflag = 0; 207 } 208 if (skipclean && ckclean && sblock.fs_clean) { 209 pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n"); 210 return (-1); 211 } 212 maxfsblock = sblock.fs_size; 213 maxino = sblock.fs_ncg * sblock.fs_ipg; 214 /* 215 * Check and potentially fix certain fields in the super block. 216 */ 217 if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) { 218 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK"); 219 if (reply("SET TO DEFAULT") == 1) { 220 sblock.fs_optim = FS_OPTTIME; 221 sbdirty(); 222 } 223 } 224 if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) { 225 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK", 226 sblock.fs_minfree); 227 if (reply("SET TO DEFAULT") == 1) { 228 sblock.fs_minfree = 10; 229 sbdirty(); 230 } 231 } 232 if (sblock.fs_magic == FS_UFS1_MAGIC && 233 sblock.fs_old_inodefmt < FS_44INODEFMT) { 234 pwarn("Format of file system is too old.\n"); 235 pwarn("Must update to modern format using a version of fsck\n"); 236 pfatal("from before 2002 with the command ``fsck -c 2''\n"); 237 exit(EEXIT); 238 } 239 if (asblk.b_dirty && !bflag) { 240 memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize); 241 flush(fswritefd, &asblk); 242 } 243 if (preen == 0 && yflag == 0 && sblock.fs_magic == FS_UFS2_MAGIC && 244 fswritefd != -1 && chkrecovery(fsreadfd) == 0 && 245 reply("SAVE DATA TO FIND ALTERNATE SUPERBLOCKS") != 0) 246 saverecovery(fsreadfd, fswritefd); 247 /* 248 * read in the summary info. 249 */ 250 asked = 0; 251 sblock.fs_csp = Calloc(1, sblock.fs_cssize); 252 if (sblock.fs_csp == NULL) { 253 printf("cannot alloc %u bytes for cg summary info\n", 254 (unsigned)sblock.fs_cssize); 255 goto badsb; 256 } 257 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) { 258 size = MIN(sblock.fs_cssize - i, sblock.fs_bsize); 259 readcnt[sblk.b_type]++; 260 if (blread(fsreadfd, (char *)sblock.fs_csp + i, 261 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag), 262 size) != 0 && !asked) { 263 pfatal("BAD SUMMARY INFORMATION"); 264 if (reply("CONTINUE") == 0) { 265 ckfini(0); 266 exit(EEXIT); 267 } 268 asked++; 269 } 270 } 271 /* 272 * allocate and initialize the necessary maps 273 */ 274 bmapsize = roundup(howmany(maxfsblock, CHAR_BIT), sizeof(short)); 275 blockmap = Calloc((unsigned)bmapsize, sizeof (char)); 276 if (blockmap == NULL) { 277 printf("cannot alloc %u bytes for blockmap\n", 278 (unsigned)bmapsize); 279 goto badsb; 280 } 281 inostathead = Calloc(sblock.fs_ncg, sizeof(struct inostatlist)); 282 if (inostathead == NULL) { 283 printf("cannot alloc %u bytes for inostathead\n", 284 (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg))); 285 goto badsb; 286 } 287 numdirs = MAX(sblock.fs_cstotal.cs_ndir, 128); 288 dirhash = numdirs; 289 inplast = 0; 290 listmax = numdirs + 10; 291 inpsort = (struct inoinfo **)Calloc(listmax, sizeof(struct inoinfo *)); 292 inphead = (struct inoinfo **)Calloc(numdirs, sizeof(struct inoinfo *)); 293 if (inpsort == NULL || inphead == NULL) { 294 printf("cannot alloc %ju bytes for inphead\n", 295 (uintmax_t)numdirs * sizeof(struct inoinfo *)); 296 goto badsb; 297 } 298 bufinit(); 299 if (sblock.fs_flags & FS_DOSOFTDEP) 300 usedsoftdep = 1; 301 else 302 usedsoftdep = 0; 303 return (1); 304 305badsb: 306 ckfini(0); 307 return (0); 308} 309 310/* 311 * Possible superblock locations ordered from most to least likely. 312 */ 313static int sblock_try[] = SBLOCKSEARCH; 314 315#define BAD_MAGIC_MSG \ 316"The previous newfs operation on this volume did not complete.\n" \ 317"You must complete newfs before mounting this volume.\n" 318 319/* 320 * Read in the super block and its summary info. 321 */ 322int 323readsb(int listerr) 324{ 325 ufs2_daddr_t super; 326 int i, bad; 327 328 if (bflag) { 329 super = bflag; 330 readcnt[sblk.b_type]++; 331 if ((blread(fsreadfd, (char *)&sblock, super, (long)SBLOCKSIZE))) 332 return (0); 333 if (sblock.fs_magic == FS_BAD_MAGIC) { 334 fprintf(stderr, BAD_MAGIC_MSG); 335 exit(11); 336 } 337 if (sblock.fs_magic != FS_UFS1_MAGIC && 338 sblock.fs_magic != FS_UFS2_MAGIC) { 339 fprintf(stderr, "%jd is not a file system superblock\n", 340 bflag); 341 return (0); 342 } 343 } else { 344 for (i = 0; sblock_try[i] != -1; i++) { 345 super = sblock_try[i] / dev_bsize; 346 readcnt[sblk.b_type]++; 347 if ((blread(fsreadfd, (char *)&sblock, super, 348 (long)SBLOCKSIZE))) 349 return (0); 350 if (sblock.fs_magic == FS_BAD_MAGIC) { 351 fprintf(stderr, BAD_MAGIC_MSG); 352 exit(11); 353 } 354 if ((sblock.fs_magic == FS_UFS1_MAGIC || 355 (sblock.fs_magic == FS_UFS2_MAGIC && 356 sblock.fs_sblockloc == sblock_try[i])) && 357 sblock.fs_ncg >= 1 && 358 sblock.fs_bsize >= MINBSIZE && 359 sblock.fs_sbsize >= roundup(sizeof(struct fs), dev_bsize)) 360 break; 361 } 362 if (sblock_try[i] == -1) { 363 fprintf(stderr, "Cannot find file system superblock\n"); 364 return (0); 365 } 366 } 367 /* 368 * Compute block size that the file system is based on, 369 * according to fsbtodb, and adjust superblock block number 370 * so we can tell if this is an alternate later. 371 */ 372 super *= dev_bsize; 373 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1); 374 sblk.b_bno = super / dev_bsize; 375 sblk.b_size = SBLOCKSIZE; 376 /* 377 * Compare all fields that should not differ in alternate super block. 378 * When an alternate super-block is specified this check is skipped. 379 */ 380 if (bflag) 381 goto out; 382 getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize); 383 if (asblk.b_errs) 384 return (0); 385 bad = 0; 386#define CHK(x, y) \ 387 if (altsblock.x != sblock.x) { \ 388 bad++; \ 389 if (listerr && debug) \ 390 printf("SUPER BLOCK VS ALTERNATE MISMATCH %s: " y " vs " y "\n", \ 391 #x, (intmax_t)sblock.x, (intmax_t)altsblock.x); \ 392 } 393 CHK(fs_sblkno, "%jd"); 394 CHK(fs_cblkno, "%jd"); 395 CHK(fs_iblkno, "%jd"); 396 CHK(fs_dblkno, "%jd"); 397 CHK(fs_ncg, "%jd"); 398 CHK(fs_bsize, "%jd"); 399 CHK(fs_fsize, "%jd"); 400 CHK(fs_frag, "%jd"); 401 CHK(fs_bmask, "%#jx"); 402 CHK(fs_fmask, "%#jx"); 403 CHK(fs_bshift, "%jd"); 404 CHK(fs_fshift, "%jd"); 405 CHK(fs_fragshift, "%jd"); 406 CHK(fs_fsbtodb, "%jd"); 407 CHK(fs_sbsize, "%jd"); 408 CHK(fs_nindir, "%jd"); 409 CHK(fs_inopb, "%jd"); 410 CHK(fs_cssize, "%jd"); 411 CHK(fs_ipg, "%jd"); 412 CHK(fs_fpg, "%jd"); 413 CHK(fs_magic, "%#jx"); 414#undef CHK 415 if (bad) { 416 if (listerr == 0) 417 return (0); 418 if (preen) 419 printf("%s: ", cdevname); 420 printf( 421 "VALUES IN SUPER BLOCK LSB=%jd DISAGREE WITH THOSE IN\n" 422 "LAST ALTERNATE LSB=%jd\n", 423 sblk.b_bno, asblk.b_bno); 424 if (reply("IGNORE ALTERNATE SUPER BLOCK") == 0) 425 return (0); 426 } 427out: 428 /* 429 * If not yet done, update UFS1 superblock with new wider fields. 430 */ 431 if (sblock.fs_magic == FS_UFS1_MAGIC && 432 sblock.fs_maxbsize != sblock.fs_bsize) { 433 sblock.fs_maxbsize = sblock.fs_bsize; 434 sblock.fs_time = sblock.fs_old_time; 435 sblock.fs_size = sblock.fs_old_size; 436 sblock.fs_dsize = sblock.fs_old_dsize; 437 sblock.fs_csaddr = sblock.fs_old_csaddr; 438 sblock.fs_cstotal.cs_ndir = sblock.fs_old_cstotal.cs_ndir; 439 sblock.fs_cstotal.cs_nbfree = sblock.fs_old_cstotal.cs_nbfree; 440 sblock.fs_cstotal.cs_nifree = sblock.fs_old_cstotal.cs_nifree; 441 sblock.fs_cstotal.cs_nffree = sblock.fs_old_cstotal.cs_nffree; 442 } 443 havesb = 1; 444 return (1); 445} 446 447void 448sblock_init(void) 449{ 450 451 fswritefd = -1; 452 fsmodified = 0; 453 lfdir = 0; 454 initbarea(&sblk, BT_SUPERBLK); 455 initbarea(&asblk, BT_SUPERBLK); 456 sblk.b_un.b_buf = Malloc(SBLOCKSIZE); 457 asblk.b_un.b_buf = Malloc(SBLOCKSIZE); 458 if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL) 459 errx(EEXIT, "cannot allocate space for superblock"); 460 dev_bsize = secsize = DEV_BSIZE; 461} 462 463/* 464 * Calculate a prototype superblock based on information in the boot area. 465 * When done the cgsblock macro can be calculated and the fs_ncg field 466 * can be used. Do NOT attempt to use other macros without verifying that 467 * their needed information is available! 468 */ 469static int 470calcsb(char *dev, int devfd, struct fs *fs) 471{ 472 struct fsrecovery *fsr; 473 char *fsrbuf; 474 u_int secsize; 475 476 /* 477 * We need fragments-per-group and the partition-size. 478 * 479 * Newfs stores these details at the end of the boot block area 480 * at the start of the filesystem partition. If they have been 481 * overwritten by a boot block, we fail. But usually they are 482 * there and we can use them. 483 */ 484 if (ioctl(devfd, DIOCGSECTORSIZE, &secsize) == -1) 485 return (0); 486 fsrbuf = Malloc(secsize); 487 if (fsrbuf == NULL) 488 errx(EEXIT, "calcsb: cannot allocate recovery buffer"); 489 if (blread(devfd, fsrbuf, 490 (SBLOCK_UFS2 - secsize) / dev_bsize, secsize) != 0) 491 return (0); 492 fsr = (struct fsrecovery *)&fsrbuf[secsize - sizeof *fsr]; 493 if (fsr->fsr_magic != FS_UFS2_MAGIC) 494 return (0); 495 memset(fs, 0, sizeof(struct fs)); 496 fs->fs_fpg = fsr->fsr_fpg; 497 fs->fs_fsbtodb = fsr->fsr_fsbtodb; 498 fs->fs_sblkno = fsr->fsr_sblkno; 499 fs->fs_magic = fsr->fsr_magic; 500 fs->fs_ncg = fsr->fsr_ncg; 501 free(fsrbuf); 502 return (1); 503} 504 505/* 506 * Check to see if recovery information exists. 507 * Return 1 if it exists or cannot be created. 508 * Return 0 if it does not exist and can be created. 509 */ 510static int 511chkrecovery(int devfd) 512{ 513 struct fsrecovery *fsr; 514 char *fsrbuf; 515 u_int secsize; 516 517 /* 518 * Could not determine if backup material exists, so do not 519 * offer to create it. 520 */ 521 if (ioctl(devfd, DIOCGSECTORSIZE, &secsize) == -1 || 522 (fsrbuf = Malloc(secsize)) == NULL || 523 blread(devfd, fsrbuf, (SBLOCK_UFS2 - secsize) / dev_bsize, 524 secsize) != 0) 525 return (1); 526 /* 527 * Recovery material has already been created, so do not 528 * need to create it again. 529 */ 530 fsr = (struct fsrecovery *)&fsrbuf[secsize - sizeof *fsr]; 531 if (fsr->fsr_magic == FS_UFS2_MAGIC) { 532 free(fsrbuf); 533 return (1); 534 } 535 /* 536 * Recovery material has not been created and can be if desired. 537 */ 538 free(fsrbuf); 539 return (0); 540} 541 542/* 543 * Read the last sector of the boot block, replace the last 544 * 20 bytes with the recovery information, then write it back. 545 * The recovery information only works for UFS2 filesystems. 546 */ 547static void 548saverecovery(int readfd, int writefd) 549{ 550 struct fsrecovery *fsr; 551 char *fsrbuf; 552 u_int secsize; 553 554 if (sblock.fs_magic != FS_UFS2_MAGIC || 555 ioctl(readfd, DIOCGSECTORSIZE, &secsize) == -1 || 556 (fsrbuf = Malloc(secsize)) == NULL || 557 blread(readfd, fsrbuf, (SBLOCK_UFS2 - secsize) / dev_bsize, 558 secsize) != 0) { 559 printf("RECOVERY DATA COULD NOT BE CREATED\n"); 560 return; 561 } 562 fsr = (struct fsrecovery *)&fsrbuf[secsize - sizeof *fsr]; 563 fsr->fsr_magic = sblock.fs_magic; 564 fsr->fsr_fpg = sblock.fs_fpg; 565 fsr->fsr_fsbtodb = sblock.fs_fsbtodb; 566 fsr->fsr_sblkno = sblock.fs_sblkno; 567 fsr->fsr_ncg = sblock.fs_ncg; 568 blwrite(writefd, fsrbuf, (SBLOCK_UFS2 - secsize) / secsize, secsize); 569 free(fsrbuf); 570} 571