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