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