1 /* 2 * Copyright (c) 1980, 1986, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char sccsid[] = "@(#)setup.c 8.8 (Berkeley) 04/27/95"; 10 #endif /* not lint */ 11 12 #define DKTYPENAMES 13 #include <sys/param.h> 14 #include <sys/time.h> 15 #include <sys/stat.h> 16 #include <sys/ioctl.h> 17 #include <sys/disklabel.h> 18 #include <sys/file.h> 19 20 #include <ufs/ufs/dinode.h> 21 #include <ufs/ffs/fs.h> 22 23 #include <ctype.h> 24 #include <err.h> 25 #include <errno.h> 26 #include <string.h> 27 28 #include "fsck.h" 29 30 struct bufarea asblk; 31 #define altsblock (*asblk.b_un.b_fs) 32 #define POWEROF2(num) (((num) & ((num) - 1)) == 0) 33 34 static void badsb __P((int listerr, char *s)); 35 static int calcsb __P((char *dev, int devfd, struct fs *fs)); 36 static struct disklabel *getdisklabel __P((char *s, int fd)); 37 static int readsb __P((int listerr)); 38 39 int 40 setup(dev) 41 char *dev; 42 { 43 long cg, size, asked, i, j; 44 long bmapsize; 45 struct disklabel *lp; 46 off_t sizepb; 47 struct stat statb; 48 struct fs proto; 49 50 havesb = 0; 51 fswritefd = -1; 52 if (stat(dev, &statb) < 0) { 53 printf("Can't stat %s: %s\n", dev, strerror(errno)); 54 return (0); 55 } 56 if ((statb.st_mode & S_IFMT) != S_IFCHR) { 57 pfatal("%s is not a character device", dev); 58 if (reply("CONTINUE") == 0) 59 return (0); 60 } 61 if ((fsreadfd = open(dev, O_RDONLY)) < 0) { 62 printf("Can't open %s: %s\n", dev, strerror(errno)); 63 return (0); 64 } 65 if (preen == 0) 66 printf("** %s", dev); 67 if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) { 68 fswritefd = -1; 69 if (preen) 70 pfatal("NO WRITE ACCESS"); 71 printf(" (NO WRITE)"); 72 } 73 if (preen == 0) 74 printf("\n"); 75 fsmodified = 0; 76 lfdir = 0; 77 initbarea(&sblk); 78 initbarea(&asblk); 79 sblk.b_un.b_buf = malloc(SBSIZE); 80 asblk.b_un.b_buf = malloc(SBSIZE); 81 if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL) 82 errx(EEXIT, "cannot allocate space for superblock"); 83 if (lp = getdisklabel(NULL, fsreadfd)) 84 dev_bsize = secsize = lp->d_secsize; 85 else 86 dev_bsize = secsize = DEV_BSIZE; 87 /* 88 * Read in the superblock, looking for alternates if necessary 89 */ 90 if (readsb(1) == 0) { 91 if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0) 92 return(0); 93 if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0) 94 return (0); 95 for (cg = 0; cg < proto.fs_ncg; cg++) { 96 bflag = fsbtodb(&proto, cgsblock(&proto, cg)); 97 if (readsb(0) != 0) 98 break; 99 } 100 if (cg >= proto.fs_ncg) { 101 printf("%s %s\n%s %s\n%s %s\n", 102 "SEARCH FOR ALTERNATE SUPER-BLOCK", 103 "FAILED. YOU MUST USE THE", 104 "-b OPTION TO FSCK TO SPECIFY THE", 105 "LOCATION OF AN ALTERNATE", 106 "SUPER-BLOCK TO SUPPLY NEEDED", 107 "INFORMATION; SEE fsck(8)."); 108 return(0); 109 } 110 pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag); 111 } 112 maxfsblock = sblock.fs_size; 113 maxino = sblock.fs_ncg * sblock.fs_ipg; 114 /* 115 * Check and potentially fix certain fields in the super block. 116 */ 117 if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) { 118 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK"); 119 if (reply("SET TO DEFAULT") == 1) { 120 sblock.fs_optim = FS_OPTTIME; 121 sbdirty(); 122 } 123 } 124 if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) { 125 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK", 126 sblock.fs_minfree); 127 if (reply("SET TO DEFAULT") == 1) { 128 sblock.fs_minfree = 10; 129 sbdirty(); 130 } 131 } 132 if (sblock.fs_interleave < 1 || 133 sblock.fs_interleave > sblock.fs_nsect) { 134 pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK", 135 sblock.fs_interleave); 136 sblock.fs_interleave = 1; 137 if (preen) 138 printf(" (FIXED)\n"); 139 if (preen || reply("SET TO DEFAULT") == 1) { 140 sbdirty(); 141 dirty(&asblk); 142 } 143 } 144 if (sblock.fs_npsect < sblock.fs_nsect || 145 sblock.fs_npsect > sblock.fs_nsect*2) { 146 pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK", 147 sblock.fs_npsect); 148 sblock.fs_npsect = sblock.fs_nsect; 149 if (preen) 150 printf(" (FIXED)\n"); 151 if (preen || reply("SET TO DEFAULT") == 1) { 152 sbdirty(); 153 dirty(&asblk); 154 } 155 } 156 if (sblock.fs_inodefmt >= FS_44INODEFMT) { 157 newinofmt = 1; 158 } else { 159 sblock.fs_qbmask = ~sblock.fs_bmask; 160 sblock.fs_qfmask = ~sblock.fs_fmask; 161 newinofmt = 0; 162 } 163 /* 164 * Convert to new inode format. 165 */ 166 if (cvtlevel >= 2 && sblock.fs_inodefmt < FS_44INODEFMT) { 167 if (preen) 168 pwarn("CONVERTING TO NEW INODE FORMAT\n"); 169 else if (!reply("CONVERT TO NEW INODE FORMAT")) 170 return(0); 171 doinglevel2++; 172 sblock.fs_inodefmt = FS_44INODEFMT; 173 sizepb = sblock.fs_bsize; 174 sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1; 175 for (i = 0; i < NIADDR; i++) { 176 sizepb *= NINDIR(&sblock); 177 sblock.fs_maxfilesize += sizepb; 178 } 179 sblock.fs_maxsymlinklen = MAXSYMLINKLEN; 180 sblock.fs_qbmask = ~sblock.fs_bmask; 181 sblock.fs_qfmask = ~sblock.fs_fmask; 182 sbdirty(); 183 dirty(&asblk); 184 } 185 /* 186 * Convert to new cylinder group format. 187 */ 188 if (cvtlevel >= 1 && sblock.fs_postblformat == FS_42POSTBLFMT) { 189 if (preen) 190 pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n"); 191 else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT")) 192 return(0); 193 doinglevel1++; 194 sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT; 195 sblock.fs_nrpos = 8; 196 sblock.fs_postbloff = 197 (char *)(&sblock.fs_opostbl[0][0]) - 198 (char *)(&sblock.fs_firstfield); 199 sblock.fs_rotbloff = &sblock.fs_space[0] - 200 (u_char *)(&sblock.fs_firstfield); 201 sblock.fs_cgsize = 202 fragroundup(&sblock, CGSIZE(&sblock)); 203 sbdirty(); 204 dirty(&asblk); 205 } 206 if (asblk.b_dirty && !bflag) { 207 bcopy((char *)&sblock, (char *)&altsblock, 208 (size_t)sblock.fs_sbsize); 209 flush(fswritefd, &asblk); 210 } 211 /* 212 * read in the summary info. 213 */ 214 asked = 0; 215 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) { 216 size = sblock.fs_cssize - i < sblock.fs_bsize ? 217 sblock.fs_cssize - i : sblock.fs_bsize; 218 sblock.fs_csp[j] = (struct csum *)calloc(1, (unsigned)size); 219 if (bread(fsreadfd, (char *)sblock.fs_csp[j], 220 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag), 221 size) != 0 && !asked) { 222 pfatal("BAD SUMMARY INFORMATION"); 223 if (reply("CONTINUE") == 0) 224 exit(EEXIT); 225 asked++; 226 } 227 } 228 /* 229 * allocate and initialize the necessary maps 230 */ 231 bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(short)); 232 blockmap = calloc((unsigned)bmapsize, sizeof (char)); 233 if (blockmap == NULL) { 234 printf("cannot alloc %u bytes for blockmap\n", 235 (unsigned)bmapsize); 236 goto badsb; 237 } 238 statemap = calloc((unsigned)(maxino + 1), sizeof(char)); 239 if (statemap == NULL) { 240 printf("cannot alloc %u bytes for statemap\n", 241 (unsigned)(maxino + 1)); 242 goto badsb; 243 } 244 typemap = calloc((unsigned)(maxino + 1), sizeof(char)); 245 if (typemap == NULL) { 246 printf("cannot alloc %u bytes for typemap\n", 247 (unsigned)(maxino + 1)); 248 goto badsb; 249 } 250 lncntp = (short *)calloc((unsigned)(maxino + 1), sizeof(short)); 251 if (lncntp == NULL) { 252 printf("cannot alloc %u bytes for lncntp\n", 253 (unsigned)(maxino + 1) * sizeof(short)); 254 goto badsb; 255 } 256 numdirs = sblock.fs_cstotal.cs_ndir; 257 inplast = 0; 258 listmax = numdirs + 10; 259 inpsort = (struct inoinfo **)calloc((unsigned)listmax, 260 sizeof(struct inoinfo *)); 261 inphead = (struct inoinfo **)calloc((unsigned)numdirs, 262 sizeof(struct inoinfo *)); 263 if (inpsort == NULL || inphead == NULL) { 264 printf("cannot alloc %u bytes for inphead\n", 265 (unsigned)numdirs * sizeof(struct inoinfo *)); 266 goto badsb; 267 } 268 bufinit(); 269 return (1); 270 271 badsb: 272 ckfini(); 273 return (0); 274 } 275 276 /* 277 * Read in the super block and its summary info. 278 */ 279 static int 280 readsb(listerr) 281 int listerr; 282 { 283 ufs_daddr_t super = bflag ? bflag : SBOFF / dev_bsize; 284 285 if (bread(fsreadfd, (char *)&sblock, super, (long)SBSIZE) != 0) 286 return (0); 287 sblk.b_bno = super; 288 sblk.b_size = SBSIZE; 289 /* 290 * run a few consistency checks of the super block 291 */ 292 if (sblock.fs_magic != FS_MAGIC) 293 { badsb(listerr, "MAGIC NUMBER WRONG"); return (0); } 294 if (sblock.fs_ncg < 1) 295 { badsb(listerr, "NCG OUT OF RANGE"); return (0); } 296 if (sblock.fs_cpg < 1) 297 { badsb(listerr, "CPG OUT OF RANGE"); return (0); } 298 if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl || 299 (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl) 300 { badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); } 301 if (sblock.fs_sbsize > SBSIZE) 302 { badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); } 303 /* 304 * Compute block size that the filesystem is based on, 305 * according to fsbtodb, and adjust superblock block number 306 * so we can tell if this is an alternate later. 307 */ 308 super *= dev_bsize; 309 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1); 310 sblk.b_bno = super / dev_bsize; 311 if (bflag) { 312 havesb = 1; 313 return (1); 314 } 315 /* 316 * Set all possible fields that could differ, then do check 317 * of whole super block against an alternate super block. 318 * When an alternate super-block is specified this check is skipped. 319 */ 320 getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize); 321 if (asblk.b_errs) 322 return (0); 323 altsblock.fs_firstfield = sblock.fs_firstfield; 324 altsblock.fs_unused_1 = sblock.fs_unused_1; 325 altsblock.fs_time = sblock.fs_time; 326 altsblock.fs_cstotal = sblock.fs_cstotal; 327 altsblock.fs_cgrotor = sblock.fs_cgrotor; 328 altsblock.fs_fmod = sblock.fs_fmod; 329 altsblock.fs_clean = sblock.fs_clean; 330 altsblock.fs_ronly = sblock.fs_ronly; 331 altsblock.fs_flags = sblock.fs_flags; 332 altsblock.fs_maxcontig = sblock.fs_maxcontig; 333 altsblock.fs_minfree = sblock.fs_minfree; 334 altsblock.fs_optim = sblock.fs_optim; 335 altsblock.fs_rotdelay = sblock.fs_rotdelay; 336 altsblock.fs_maxbpg = sblock.fs_maxbpg; 337 bcopy((char *)sblock.fs_csp, (char *)altsblock.fs_csp, 338 sizeof sblock.fs_csp); 339 altsblock.fs_maxcluster = sblock.fs_maxcluster; 340 bcopy((char *)sblock.fs_fsmnt, (char *)altsblock.fs_fsmnt, 341 sizeof sblock.fs_fsmnt); 342 bcopy((char *)sblock.fs_sparecon, (char *)altsblock.fs_sparecon, 343 sizeof sblock.fs_sparecon); 344 /* 345 * The following should not have to be copied. 346 */ 347 altsblock.fs_fsbtodb = sblock.fs_fsbtodb; 348 altsblock.fs_interleave = sblock.fs_interleave; 349 altsblock.fs_npsect = sblock.fs_npsect; 350 altsblock.fs_nrpos = sblock.fs_nrpos; 351 altsblock.fs_state = sblock.fs_state; 352 altsblock.fs_qbmask = sblock.fs_qbmask; 353 altsblock.fs_qfmask = sblock.fs_qfmask; 354 altsblock.fs_state = sblock.fs_state; 355 altsblock.fs_maxfilesize = sblock.fs_maxfilesize; 356 if (bcmp((char *)&sblock, (char *)&altsblock, (int)sblock.fs_sbsize)) { 357 if (debug) { 358 long *nlp, *olp, *endlp; 359 360 printf("superblock mismatches\n"); 361 nlp = (long *)&altsblock; 362 olp = (long *)&sblock; 363 endlp = olp + (sblock.fs_sbsize / sizeof *olp); 364 for ( ; olp < endlp; olp++, nlp++) { 365 if (*olp == *nlp) 366 continue; 367 printf("offset %d, original %d, alternate %d\n", 368 olp - (long *)&sblock, *olp, *nlp); 369 } 370 } 371 badsb(listerr, 372 "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE"); 373 return (0); 374 } 375 havesb = 1; 376 return (1); 377 } 378 379 static void 380 badsb(listerr, s) 381 int listerr; 382 char *s; 383 { 384 385 if (!listerr) 386 return; 387 if (preen) 388 printf("%s: ", cdevname); 389 pfatal("BAD SUPER BLOCK: %s\n", s); 390 } 391 392 /* 393 * Calculate a prototype superblock based on information in the disk label. 394 * When done the cgsblock macro can be calculated and the fs_ncg field 395 * can be used. Do NOT attempt to use other macros without verifying that 396 * their needed information is available! 397 */ 398 static int 399 calcsb(dev, devfd, fs) 400 char *dev; 401 int devfd; 402 register struct fs *fs; 403 { 404 register struct disklabel *lp; 405 register struct partition *pp; 406 register char *cp; 407 int i; 408 409 cp = index(dev, '\0') - 1; 410 if (cp == (char *)-1 || ((*cp < 'a' || *cp > 'h') && !isdigit(*cp))) { 411 pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev); 412 return (0); 413 } 414 lp = getdisklabel(dev, devfd); 415 if (isdigit(*cp)) 416 pp = &lp->d_partitions[0]; 417 else 418 pp = &lp->d_partitions[*cp - 'a']; 419 if (pp->p_fstype != FS_BSDFFS) { 420 pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n", 421 dev, pp->p_fstype < FSMAXTYPES ? 422 fstypenames[pp->p_fstype] : "unknown"); 423 return (0); 424 } 425 bzero((char *)fs, sizeof(struct fs)); 426 fs->fs_fsize = pp->p_fsize; 427 fs->fs_frag = pp->p_frag; 428 fs->fs_cpg = pp->p_cpg; 429 fs->fs_size = pp->p_size; 430 fs->fs_ntrak = lp->d_ntracks; 431 fs->fs_nsect = lp->d_nsectors; 432 fs->fs_spc = lp->d_secpercyl; 433 fs->fs_nspf = fs->fs_fsize / lp->d_secsize; 434 fs->fs_sblkno = roundup( 435 howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize), 436 fs->fs_frag); 437 fs->fs_cgmask = 0xffffffff; 438 for (i = fs->fs_ntrak; i > 1; i >>= 1) 439 fs->fs_cgmask <<= 1; 440 if (!POWEROF2(fs->fs_ntrak)) 441 fs->fs_cgmask <<= 1; 442 fs->fs_cgoffset = roundup( 443 howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag); 444 fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs); 445 fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg); 446 for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1) 447 fs->fs_fsbtodb++; 448 dev_bsize = lp->d_secsize; 449 return (1); 450 } 451 452 static struct disklabel * 453 getdisklabel(s, fd) 454 char *s; 455 int fd; 456 { 457 static struct disklabel lab; 458 459 if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) { 460 if (s == NULL) 461 return ((struct disklabel *)NULL); 462 pwarn("ioctl (GCINFO): %s\n", strerror(errno)); 463 errx(EEXIT, "%s: can't read disk label", s); 464 } 465 return (&lab); 466 } 467