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.9 (Berkeley) 04/28/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 memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize); 208 flush(fswritefd, &asblk); 209 } 210 /* 211 * read in the summary info. 212 */ 213 asked = 0; 214 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) { 215 size = sblock.fs_cssize - i < sblock.fs_bsize ? 216 sblock.fs_cssize - i : sblock.fs_bsize; 217 sblock.fs_csp[j] = (struct csum *)calloc(1, (unsigned)size); 218 if (bread(fsreadfd, (char *)sblock.fs_csp[j], 219 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag), 220 size) != 0 && !asked) { 221 pfatal("BAD SUMMARY INFORMATION"); 222 if (reply("CONTINUE") == 0) 223 exit(EEXIT); 224 asked++; 225 } 226 } 227 /* 228 * allocate and initialize the necessary maps 229 */ 230 bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(short)); 231 blockmap = calloc((unsigned)bmapsize, sizeof (char)); 232 if (blockmap == NULL) { 233 printf("cannot alloc %u bytes for blockmap\n", 234 (unsigned)bmapsize); 235 goto badsb; 236 } 237 statemap = calloc((unsigned)(maxino + 1), sizeof(char)); 238 if (statemap == NULL) { 239 printf("cannot alloc %u bytes for statemap\n", 240 (unsigned)(maxino + 1)); 241 goto badsb; 242 } 243 typemap = calloc((unsigned)(maxino + 1), sizeof(char)); 244 if (typemap == NULL) { 245 printf("cannot alloc %u bytes for typemap\n", 246 (unsigned)(maxino + 1)); 247 goto badsb; 248 } 249 lncntp = (short *)calloc((unsigned)(maxino + 1), sizeof(short)); 250 if (lncntp == NULL) { 251 printf("cannot alloc %u bytes for lncntp\n", 252 (unsigned)(maxino + 1) * sizeof(short)); 253 goto badsb; 254 } 255 numdirs = sblock.fs_cstotal.cs_ndir; 256 inplast = 0; 257 listmax = numdirs + 10; 258 inpsort = (struct inoinfo **)calloc((unsigned)listmax, 259 sizeof(struct inoinfo *)); 260 inphead = (struct inoinfo **)calloc((unsigned)numdirs, 261 sizeof(struct inoinfo *)); 262 if (inpsort == NULL || inphead == NULL) { 263 printf("cannot alloc %u bytes for inphead\n", 264 (unsigned)numdirs * sizeof(struct inoinfo *)); 265 goto badsb; 266 } 267 bufinit(); 268 return (1); 269 270 badsb: 271 ckfini(); 272 return (0); 273 } 274 275 /* 276 * Read in the super block and its summary info. 277 */ 278 static int 279 readsb(listerr) 280 int listerr; 281 { 282 ufs_daddr_t super = bflag ? bflag : SBOFF / dev_bsize; 283 284 if (bread(fsreadfd, (char *)&sblock, super, (long)SBSIZE) != 0) 285 return (0); 286 sblk.b_bno = super; 287 sblk.b_size = SBSIZE; 288 /* 289 * run a few consistency checks of the super block 290 */ 291 if (sblock.fs_magic != FS_MAGIC) 292 { badsb(listerr, "MAGIC NUMBER WRONG"); return (0); } 293 if (sblock.fs_ncg < 1) 294 { badsb(listerr, "NCG OUT OF RANGE"); return (0); } 295 if (sblock.fs_cpg < 1) 296 { badsb(listerr, "CPG OUT OF RANGE"); return (0); } 297 if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl || 298 (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl) 299 { badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); } 300 if (sblock.fs_sbsize > SBSIZE) 301 { badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); } 302 /* 303 * Compute block size that the filesystem is based on, 304 * according to fsbtodb, and adjust superblock block number 305 * so we can tell if this is an alternate later. 306 */ 307 super *= dev_bsize; 308 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1); 309 sblk.b_bno = super / dev_bsize; 310 if (bflag) { 311 havesb = 1; 312 return (1); 313 } 314 /* 315 * Set all possible fields that could differ, then do check 316 * of whole super block against an alternate super block. 317 * When an alternate super-block is specified this check is skipped. 318 */ 319 getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize); 320 if (asblk.b_errs) 321 return (0); 322 altsblock.fs_firstfield = sblock.fs_firstfield; 323 altsblock.fs_unused_1 = sblock.fs_unused_1; 324 altsblock.fs_time = sblock.fs_time; 325 altsblock.fs_cstotal = sblock.fs_cstotal; 326 altsblock.fs_cgrotor = sblock.fs_cgrotor; 327 altsblock.fs_fmod = sblock.fs_fmod; 328 altsblock.fs_clean = sblock.fs_clean; 329 altsblock.fs_ronly = sblock.fs_ronly; 330 altsblock.fs_flags = sblock.fs_flags; 331 altsblock.fs_maxcontig = sblock.fs_maxcontig; 332 altsblock.fs_minfree = sblock.fs_minfree; 333 altsblock.fs_optim = sblock.fs_optim; 334 altsblock.fs_rotdelay = sblock.fs_rotdelay; 335 altsblock.fs_maxbpg = sblock.fs_maxbpg; 336 memmove(altsblock.fs_csp, sblock.fs_csp, sizeof sblock.fs_csp); 337 altsblock.fs_maxcluster = sblock.fs_maxcluster; 338 memmove(altsblock.fs_fsmnt, sblock.fs_fsmnt, sizeof sblock.fs_fsmnt); 339 memmove(altsblock.fs_sparecon, 340 sblock.fs_sparecon, sizeof sblock.fs_sparecon); 341 /* 342 * The following should not have to be copied. 343 */ 344 altsblock.fs_fsbtodb = sblock.fs_fsbtodb; 345 altsblock.fs_interleave = sblock.fs_interleave; 346 altsblock.fs_npsect = sblock.fs_npsect; 347 altsblock.fs_nrpos = sblock.fs_nrpos; 348 altsblock.fs_state = sblock.fs_state; 349 altsblock.fs_qbmask = sblock.fs_qbmask; 350 altsblock.fs_qfmask = sblock.fs_qfmask; 351 altsblock.fs_state = sblock.fs_state; 352 altsblock.fs_maxfilesize = sblock.fs_maxfilesize; 353 if (memcmp(&sblock, &altsblock, (int)sblock.fs_sbsize)) { 354 if (debug) { 355 long *nlp, *olp, *endlp; 356 357 printf("superblock mismatches\n"); 358 nlp = (long *)&altsblock; 359 olp = (long *)&sblock; 360 endlp = olp + (sblock.fs_sbsize / sizeof *olp); 361 for ( ; olp < endlp; olp++, nlp++) { 362 if (*olp == *nlp) 363 continue; 364 printf("offset %d, original %d, alternate %d\n", 365 olp - (long *)&sblock, *olp, *nlp); 366 } 367 } 368 badsb(listerr, 369 "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE"); 370 return (0); 371 } 372 havesb = 1; 373 return (1); 374 } 375 376 static void 377 badsb(listerr, s) 378 int listerr; 379 char *s; 380 { 381 382 if (!listerr) 383 return; 384 if (preen) 385 printf("%s: ", cdevname); 386 pfatal("BAD SUPER BLOCK: %s\n", s); 387 } 388 389 /* 390 * Calculate a prototype superblock based on information in the disk label. 391 * When done the cgsblock macro can be calculated and the fs_ncg field 392 * can be used. Do NOT attempt to use other macros without verifying that 393 * their needed information is available! 394 */ 395 static int 396 calcsb(dev, devfd, fs) 397 char *dev; 398 int devfd; 399 register struct fs *fs; 400 { 401 register struct disklabel *lp; 402 register struct partition *pp; 403 register char *cp; 404 int i; 405 406 cp = strchr(dev, '\0') - 1; 407 if (cp == (char *)-1 || ((*cp < 'a' || *cp > 'h') && !isdigit(*cp))) { 408 pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev); 409 return (0); 410 } 411 lp = getdisklabel(dev, devfd); 412 if (isdigit(*cp)) 413 pp = &lp->d_partitions[0]; 414 else 415 pp = &lp->d_partitions[*cp - 'a']; 416 if (pp->p_fstype != FS_BSDFFS) { 417 pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n", 418 dev, pp->p_fstype < FSMAXTYPES ? 419 fstypenames[pp->p_fstype] : "unknown"); 420 return (0); 421 } 422 memset(fs, 0, sizeof(struct fs)); 423 fs->fs_fsize = pp->p_fsize; 424 fs->fs_frag = pp->p_frag; 425 fs->fs_cpg = pp->p_cpg; 426 fs->fs_size = pp->p_size; 427 fs->fs_ntrak = lp->d_ntracks; 428 fs->fs_nsect = lp->d_nsectors; 429 fs->fs_spc = lp->d_secpercyl; 430 fs->fs_nspf = fs->fs_fsize / lp->d_secsize; 431 fs->fs_sblkno = roundup( 432 howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize), 433 fs->fs_frag); 434 fs->fs_cgmask = 0xffffffff; 435 for (i = fs->fs_ntrak; i > 1; i >>= 1) 436 fs->fs_cgmask <<= 1; 437 if (!POWEROF2(fs->fs_ntrak)) 438 fs->fs_cgmask <<= 1; 439 fs->fs_cgoffset = roundup( 440 howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag); 441 fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs); 442 fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg); 443 for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1) 444 fs->fs_fsbtodb++; 445 dev_bsize = lp->d_secsize; 446 return (1); 447 } 448 449 static struct disklabel * 450 getdisklabel(s, fd) 451 char *s; 452 int fd; 453 { 454 static struct disklabel lab; 455 456 if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) { 457 if (s == NULL) 458 return ((struct disklabel *)NULL); 459 pwarn("ioctl (GCINFO): %s\n", strerror(errno)); 460 errx(EEXIT, "%s: can't read disk label", s); 461 } 462 return (&lab); 463 } 464