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