setup.c revision 103949
1219820Sjeff/* 2219820Sjeff * Copyright (c) 1980, 1986, 1993 3219820Sjeff * The Regents of the University of California. All rights reserved. 4219820Sjeff * 5289683Shselasky * Redistribution and use in source and binary forms, with or without 6219820Sjeff * modification, are permitted provided that the following conditions 7219820Sjeff * are met: 8219820Sjeff * 1. Redistributions of source code must retain the above copyright 9219820Sjeff * notice, this list of conditions and the following disclaimer. 10219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright 11219820Sjeff * notice, this list of conditions and the following disclaimer in the 12219820Sjeff * documentation and/or other materials provided with the distribution. 13219820Sjeff * 3. All advertising materials mentioning features or use of this software 14219820Sjeff * must display the following acknowledgement: 15219820Sjeff * This product includes software developed by the University of 16219820Sjeff * California, Berkeley and its contributors. 17219820Sjeff * 4. Neither the name of the University nor the names of its contributors 18219820Sjeff * may be used to endorse or promote products derived from this software 19219820Sjeff * without specific prior written permission. 20219820Sjeff * 21219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22219820Sjeff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23219820Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24219820Sjeff * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25219820Sjeff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26219820Sjeff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27219820Sjeff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28289644Shselasky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29289644Shselasky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30219820Sjeff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31219820Sjeff * SUCH DAMAGE. 32219820Sjeff */ 33219820Sjeff 34219820Sjeff#ifndef lint 35219820Sjeff#if 0 36219820Sjeffstatic const char sccsid[] = "@(#)setup.c 8.10 (Berkeley) 5/9/95"; 37219820Sjeff#endif 38219820Sjeffstatic const char rcsid[] = 39219820Sjeff "$FreeBSD: head/sbin/fsck_ffs/setup.c 103949 2002-09-25 04:06:37Z mike $"; 40219820Sjeff#endif /* not lint */ 41219820Sjeff 42219820Sjeff#include <sys/param.h> 43219820Sjeff#include <sys/stat.h> 44219820Sjeff#define FSTYPENAMES 45219820Sjeff#include <sys/disklabel.h> 46219820Sjeff#include <sys/file.h> 47219820Sjeff#include <sys/sysctl.h> 48219820Sjeff 49219820Sjeff#include <ufs/ufs/dinode.h> 50219820Sjeff#include <ufs/ffs/fs.h> 51219820Sjeff 52219820Sjeff#include <ctype.h> 53331756Semaste#include <err.h> 54219820Sjeff#include <errno.h> 55219820Sjeff#include <limits.h> 56289683Shselasky#include <stdint.h> 57255932Salfred#include <string.h> 58255932Salfred 59289683Shselasky#include "fsck.h" 60255932Salfred 61255932Salfredstruct bufarea asblk; 62255932Salfred#define altsblock (*asblk.b_un.b_fs) 63255932Salfred#define POWEROF2(num) (((num) & ((num) - 1)) == 0) 64255932Salfred 65255932Salfredstatic void badsb(int listerr, const char *s); 66255932Salfredstatic int calcsb(char *dev, int devfd, struct fs *fs); 67255932Salfredstatic struct disklabel *getdisklabel(char *s, int fd); 68255932Salfred 69255932Salfred/* 70255932Salfred * Read in a superblock finding an alternate if necessary. 71255932Salfred * Return 1 if successful, 0 if unsuccessful, -1 if file system 72255932Salfred * is already clean (preen mode only). 73255932Salfred */ 74255932Salfredint 75255932Salfredsetup(char *dev) 76255932Salfred{ 77255932Salfred long cg, asked, i, j; 78255932Salfred long bmapsize; 79255932Salfred struct stat statb; 80255932Salfred struct fs proto; 81255932Salfred size_t size; 82255932Salfred 83255932Salfred havesb = 0; 84255932Salfred fswritefd = -1; 85255932Salfred cursnapshot = 0; 86255932Salfred if (stat(dev, &statb) < 0) { 87255932Salfred printf("Can't stat %s: %s\n", dev, strerror(errno)); 88255932Salfred if (bkgrdflag) { 89255932Salfred unlink(snapname); 90255932Salfred bkgrdflag = 0; 91255932Salfred } 92255932Salfred return (0); 93255932Salfred } 94255932Salfred if ((statb.st_mode & S_IFMT) != S_IFCHR && 95255932Salfred (statb.st_mode & S_IFMT) != S_IFBLK) { 96255932Salfred if (bkgrdflag != 0 && (statb.st_flags & SF_SNAPSHOT) == 0) { 97255932Salfred unlink(snapname); 98255932Salfred printf("background fsck lacks a snapshot\n"); 99255932Salfred exit(EEXIT); 100255932Salfred } 101255932Salfred if ((statb.st_flags & SF_SNAPSHOT) != 0 && cvtlevel == 0) { 102255932Salfred cursnapshot = statb.st_ino; 103255932Salfred } else { 104255932Salfred if (cvtlevel == 0 || 105255932Salfred (statb.st_flags & SF_SNAPSHOT) == 0) { 106255932Salfred if (preen && bkgrdflag) { 107255932Salfred unlink(snapname); 108255932Salfred bkgrdflag = 0; 109255932Salfred } 110255932Salfred pfatal("%s is not a disk device", dev); 111255932Salfred if (reply("CONTINUE") == 0) { 112255932Salfred if (bkgrdflag) { 113255932Salfred unlink(snapname); 114255932Salfred bkgrdflag = 0; 115255932Salfred } 116255932Salfred return (0); 117255932Salfred } 118255932Salfred } else { 119255932Salfred if (bkgrdflag) { 120255932Salfred unlink(snapname); 121255932Salfred bkgrdflag = 0; 122255932Salfred } 123255932Salfred pfatal("cannot convert a snapshot"); 124289683Shselasky exit(EEXIT); 125255932Salfred } 126289683Shselasky } 127289683Shselasky } 128255932Salfred if ((fsreadfd = open(dev, O_RDONLY)) < 0) { 129279205Shselasky if (bkgrdflag) { 130279205Shselasky unlink(snapname); 131219820Sjeff bkgrdflag = 0; 132 } 133 printf("Can't open %s: %s\n", dev, strerror(errno)); 134 return (0); 135 } 136 if (bkgrdflag) { 137 unlink(snapname); 138 size = MIBSIZE; 139 if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0|| 140 sysctlnametomib("vfs.ffs.adjblkcnt", adjblkcnt, &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 cmd.version = FFS_CMD_VERSION; 148 cmd.handle = fsreadfd; 149 fswritefd = -1; 150 } 151 if (preen == 0) 152 printf("** %s", dev); 153 if (bkgrdflag == 0 && 154 (nflag || (fswritefd = open(dev, O_WRONLY)) < 0)) { 155 fswritefd = -1; 156 if (preen) 157 pfatal("NO WRITE ACCESS"); 158 printf(" (NO WRITE)"); 159 } 160 if (preen == 0) 161 printf("\n"); 162 /* 163 * Read in the superblock, looking for alternates if necessary 164 */ 165 if (readsb(1) == 0) { 166 skipclean = 0; 167 if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0) 168 return(0); 169 if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0) 170 return (0); 171 for (cg = 0; cg < proto.fs_ncg; cg++) { 172 bflag = fsbtodb(&proto, cgsblock(&proto, cg)); 173 if (readsb(0) != 0) 174 break; 175 } 176 if (cg >= proto.fs_ncg) { 177 printf("%s %s\n%s %s\n%s %s\n", 178 "SEARCH FOR ALTERNATE SUPER-BLOCK", 179 "FAILED. YOU MUST USE THE", 180 "-b OPTION TO FSCK TO SPECIFY THE", 181 "LOCATION OF AN ALTERNATE", 182 "SUPER-BLOCK TO SUPPLY NEEDED", 183 "INFORMATION; SEE fsck(8)."); 184 bflag = 0; 185 return(0); 186 } 187 pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag); 188 bflag = 0; 189 } 190 if (skipclean && preen && sblock.fs_clean) { 191 pwarn("FILESYSTEM CLEAN; SKIPPING CHECKS\n"); 192 return (-1); 193 } 194 maxfsblock = sblock.fs_size; 195 maxino = sblock.fs_ncg * sblock.fs_ipg; 196 /* 197 * Check and potentially fix certain fields in the super block. 198 */ 199 if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) { 200 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK"); 201 if (reply("SET TO DEFAULT") == 1) { 202 sblock.fs_optim = FS_OPTTIME; 203 sbdirty(); 204 } 205 } 206 if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) { 207 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK", 208 sblock.fs_minfree); 209 if (reply("SET TO DEFAULT") == 1) { 210 sblock.fs_minfree = 10; 211 sbdirty(); 212 } 213 } 214 if (sblock.fs_magic == FS_UFS1_MAGIC && 215 sblock.fs_old_inodefmt < FS_44INODEFMT) { 216 pwarn("Format of file system is too old.\n"); 217 pwarn("Must update to modern format using a version of fsck\n"); 218 pfatal("from before 2002 with the command ``fsck -c 2''\n"); 219 exit(EEXIT); 220 } 221 if (asblk.b_dirty && !bflag) { 222 memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize); 223 flush(fswritefd, &asblk); 224 } 225 /* 226 * read in the summary info. 227 */ 228 asked = 0; 229 sblock.fs_csp = calloc(1, sblock.fs_cssize); 230 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) { 231 size = sblock.fs_cssize - i < sblock.fs_bsize ? 232 sblock.fs_cssize - i : sblock.fs_bsize; 233 if (bread(fsreadfd, (char *)sblock.fs_csp + i, 234 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag), 235 size) != 0 && !asked) { 236 pfatal("BAD SUMMARY INFORMATION"); 237 if (reply("CONTINUE") == 0) { 238 ckfini(0); 239 exit(EEXIT); 240 } 241 asked++; 242 } 243 } 244 /* 245 * allocate and initialize the necessary maps 246 */ 247 bmapsize = roundup(howmany(maxfsblock, CHAR_BIT), sizeof(short)); 248 blockmap = calloc((unsigned)bmapsize, sizeof (char)); 249 if (blockmap == NULL) { 250 printf("cannot alloc %u bytes for blockmap\n", 251 (unsigned)bmapsize); 252 goto badsb; 253 } 254 inostathead = calloc((unsigned)(sblock.fs_ncg), 255 sizeof(struct inostatlist)); 256 if (inostathead == NULL) { 257 printf("cannot alloc %u bytes for inostathead\n", 258 (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg))); 259 goto badsb; 260 } 261 numdirs = sblock.fs_cstotal.cs_ndir; 262 dirhash = numdirs; 263 if (numdirs == 0) { 264 printf("numdirs is zero, try using an alternate superblock\n"); 265 goto badsb; 266 } 267 inplast = 0; 268 listmax = numdirs + 10; 269 inpsort = (struct inoinfo **)calloc((unsigned)listmax, 270 sizeof(struct inoinfo *)); 271 inphead = (struct inoinfo **)calloc((unsigned)numdirs, 272 sizeof(struct inoinfo *)); 273 if (inpsort == NULL || inphead == NULL) { 274 printf("cannot alloc %ju bytes for inphead\n", 275 (uintmax_t)numdirs * sizeof(struct inoinfo *)); 276 goto badsb; 277 } 278 bufinit(); 279 if (sblock.fs_flags & FS_DOSOFTDEP) 280 usedsoftdep = 1; 281 else 282 usedsoftdep = 0; 283 return (1); 284 285badsb: 286 ckfini(0); 287 return (0); 288} 289 290/* 291 * Possible superblock locations ordered from most to least likely. 292 */ 293static int sblock_try[] = SBLOCKSEARCH; 294 295/* 296 * Read in the super block and its summary info. 297 */ 298int 299readsb(int listerr) 300{ 301 ufs2_daddr_t super; 302 int i; 303 304 if (bflag) { 305 super = bflag; 306 if ((bread(fsreadfd, (char *)&sblock, super, (long)SBLOCKSIZE))) 307 return (0); 308 } else { 309 for (i = 0; sblock_try[i] != -1; i++) { 310 super = sblock_try[i] / dev_bsize; 311 if ((bread(fsreadfd, (char *)&sblock, super, 312 (long)SBLOCKSIZE))) 313 return (0); 314 if ((sblock.fs_magic == FS_UFS1_MAGIC || 315 (sblock.fs_magic == FS_UFS2_MAGIC && 316 sblock.fs_sblockloc == 317 numfrags(&sblock, sblock_try[i]))) && 318 sblock.fs_ncg >= 1 && 319 sblock.fs_bsize >= SBLOCKSIZE && 320 sblock.fs_bsize >= sizeof(struct fs)) 321 break; 322 } 323 if (sblock_try[i] == -1) { 324 fprintf(stderr, "Cannot find file system superblock\n"); 325 return (0); 326 } 327 } 328 /* 329 * Compute block size that the file system is based on, 330 * according to fsbtodb, and adjust superblock block number 331 * so we can tell if this is an alternate later. 332 */ 333 super *= dev_bsize; 334 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1); 335 sblk.b_bno = super / dev_bsize; 336 sblk.b_size = SBLOCKSIZE; 337 if (bflag) { 338 havesb = 1; 339 return (1); 340 } 341 /* 342 * Compare all fields that should not differ in alternate super block. 343 * When an alternate super-block is specified this check is skipped. 344 */ 345 getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize); 346 if (asblk.b_errs) 347 return (0); 348 if (altsblock.fs_sblkno != sblock.fs_sblkno || 349 altsblock.fs_cblkno != sblock.fs_cblkno || 350 altsblock.fs_iblkno != sblock.fs_iblkno || 351 altsblock.fs_dblkno != sblock.fs_dblkno || 352 altsblock.fs_ncg != sblock.fs_ncg || 353 altsblock.fs_bsize != sblock.fs_bsize || 354 altsblock.fs_fsize != sblock.fs_fsize || 355 altsblock.fs_frag != sblock.fs_frag || 356 altsblock.fs_bmask != sblock.fs_bmask || 357 altsblock.fs_fmask != sblock.fs_fmask || 358 altsblock.fs_bshift != sblock.fs_bshift || 359 altsblock.fs_fshift != sblock.fs_fshift || 360 altsblock.fs_fragshift != sblock.fs_fragshift || 361 altsblock.fs_fsbtodb != sblock.fs_fsbtodb || 362 altsblock.fs_sbsize != sblock.fs_sbsize || 363 altsblock.fs_nindir != sblock.fs_nindir || 364 altsblock.fs_inopb != sblock.fs_inopb || 365 altsblock.fs_cssize != sblock.fs_cssize || 366 altsblock.fs_ipg != sblock.fs_ipg || 367 altsblock.fs_fpg != sblock.fs_fpg || 368 altsblock.fs_magic != sblock.fs_magic) { 369 badsb(listerr, 370 "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE"); 371 return (0); 372 } 373 /* 374 * If not yet done, update UFS1 superblock with new wider fields. 375 */ 376 if (sblock.fs_magic == FS_UFS1_MAGIC && 377 sblock.fs_maxbsize != sblock.fs_bsize) { 378 sblock.fs_maxbsize = sblock.fs_bsize; 379 sblock.fs_time = sblock.fs_old_time; 380 sblock.fs_size = sblock.fs_old_size; 381 sblock.fs_dsize = sblock.fs_old_dsize; 382 sblock.fs_csaddr = sblock.fs_old_csaddr; 383 sblock.fs_cstotal.cs_ndir = sblock.fs_old_cstotal.cs_ndir; 384 sblock.fs_cstotal.cs_nbfree = sblock.fs_old_cstotal.cs_nbfree; 385 sblock.fs_cstotal.cs_nifree = sblock.fs_old_cstotal.cs_nifree; 386 sblock.fs_cstotal.cs_nffree = sblock.fs_old_cstotal.cs_nffree; 387 } 388 havesb = 1; 389 return (1); 390} 391 392static void 393badsb(int listerr, const char *s) 394{ 395 396 if (!listerr) 397 return; 398 if (preen) 399 printf("%s: ", cdevname); 400 pfatal("BAD SUPER BLOCK: %s\n", s); 401} 402 403void 404sblock_init(void) 405{ 406 struct disklabel *lp; 407 408 fswritefd = -1; 409 fsmodified = 0; 410 lfdir = 0; 411 initbarea(&sblk); 412 initbarea(&asblk); 413 sblk.b_un.b_buf = malloc(SBLOCKSIZE); 414 asblk.b_un.b_buf = malloc(SBLOCKSIZE); 415 if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL) 416 errx(EEXIT, "cannot allocate space for superblock"); 417 if ((lp = getdisklabel(NULL, fsreadfd))) 418 dev_bsize = secsize = lp->d_secsize; 419 else 420 dev_bsize = secsize = DEV_BSIZE; 421} 422 423/* 424 * Calculate a prototype superblock based on information in the disk label. 425 * When done the cgsblock macro can be calculated and the fs_ncg field 426 * can be used. Do NOT attempt to use other macros without verifying that 427 * their needed information is available! 428 */ 429static int 430calcsb(char *dev, int devfd, struct fs *fs) 431{ 432 struct disklabel *lp; 433 struct partition *pp; 434 char *cp; 435 int i, nspf; 436 437 cp = strchr(dev, '\0') - 1; 438 if (cp == (char *)-1 || ((*cp < 'a' || *cp > 'h') && !isdigit(*cp))) { 439 pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev); 440 return (0); 441 } 442 lp = getdisklabel(dev, devfd); 443 if (isdigit(*cp)) 444 pp = &lp->d_partitions[0]; 445 else 446 pp = &lp->d_partitions[*cp - 'a']; 447 if (pp->p_fstype != FS_BSDFFS) { 448 pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n", 449 dev, pp->p_fstype < FSMAXTYPES ? 450 fstypenames[pp->p_fstype] : "unknown"); 451 return (0); 452 } 453 if (pp->p_fsize == 0 || pp->p_frag == 0 || 454 pp->p_cpg == 0 || pp->p_size == 0) { 455 pfatal("%s: %s: type %s fsize %d, frag %d, cpg %d, size %d\n", 456 dev, "INCOMPLETE LABEL", fstypenames[pp->p_fstype], 457 pp->p_fsize, pp->p_frag, pp->p_cpg, pp->p_size); 458 return (0); 459 } 460 memset(fs, 0, sizeof(struct fs)); 461 fs->fs_fsize = pp->p_fsize; 462 fs->fs_frag = pp->p_frag; 463 fs->fs_size = pp->p_size; 464 fs->fs_sblkno = roundup( 465 howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize), 466 fs->fs_frag); 467 nspf = fs->fs_fsize / lp->d_secsize; 468 for (fs->fs_fsbtodb = 0, i = nspf; i > 1; i >>= 1) 469 fs->fs_fsbtodb++; 470 dev_bsize = lp->d_secsize; 471 if (fs->fs_magic == FS_UFS2_MAGIC) { 472 fs->fs_fpg = pp->p_cpg; 473 fs->fs_ncg = howmany(fs->fs_size, fs->fs_fpg); 474 } else /* if (fs->fs_magic == FS_UFS1_MAGIC) */ { 475 fs->fs_old_cpg = pp->p_cpg; 476 fs->fs_old_cgmask = 0xffffffff; 477 for (i = lp->d_ntracks; i > 1; i >>= 1) 478 fs->fs_old_cgmask <<= 1; 479 if (!POWEROF2(lp->d_ntracks)) 480 fs->fs_old_cgmask <<= 1; 481 fs->fs_old_cgoffset = roundup(howmany(lp->d_nsectors, nspf), 482 fs->fs_frag); 483 fs->fs_fpg = (fs->fs_old_cpg * lp->d_secpercyl) / nspf; 484 fs->fs_ncg = howmany(fs->fs_size / lp->d_secpercyl, 485 fs->fs_old_cpg); 486 } 487 return (1); 488} 489 490static struct disklabel * 491getdisklabel(char *s, int fd) 492{ 493 static struct disklabel lab; 494 495 if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) { 496 if (s == NULL) 497 return ((struct disklabel *)NULL); 498 pwarn("ioctl (GCINFO): %s\n", strerror(errno)); 499 errx(EEXIT, "%s: can't read disk label", s); 500 } 501 return (&lab); 502} 503