1 /* 2 * Copyright (c) 1980 Regents of the University of California. 3 * All rights reserved. The Berkeley software License Agreement 4 * specifies the terms and conditions for redistribution. 5 */ 6 7 #ifndef lint 8 static char sccsid[] = "@(#)setup.c 5.23 (Berkeley) 10/24/89"; 9 #endif not lint 10 11 #define DKTYPENAMES 12 #include <sys/param.h> 13 #include <ufs/dinode.h> 14 #include <ufs/fs.h> 15 #include <sys/stat.h> 16 #include <sys/ioctl.h> 17 #include <sys/disklabel.h> 18 #include <sys/file.h> 19 #include <machine/endian.h> 20 #include <ctype.h> 21 #include "fsck.h" 22 23 BUFAREA asblk; 24 #define altsblock (*asblk.b_un.b_fs) 25 #define POWEROF2(num) (((num) & ((num) - 1)) == 0) 26 27 /* 28 * The size of a cylinder group is calculated by CGSIZE. The maximum size 29 * is limited by the fact that cylinder groups are at most one block. 30 * Its size is derived from the size of the maps maintained in the 31 * cylinder group and the (struct cg) size. 32 */ 33 #define CGSIZE(fs) \ 34 /* base cg */ (sizeof(struct cg) + \ 35 /* blktot size */ (fs)->fs_cpg * sizeof(long) + \ 36 /* blks size */ (fs)->fs_cpg * (fs)->fs_nrpos * sizeof(short) + \ 37 /* inode map */ howmany((fs)->fs_ipg, NBBY) + \ 38 /* block map */ howmany((fs)->fs_cpg * (fs)->fs_spc / NSPF(fs), NBBY)) 39 40 char *malloc(), *calloc(); 41 char *index(); 42 struct disklabel *getdisklabel(); 43 44 setup(dev) 45 char *dev; 46 { 47 dev_t rootdev; 48 long cg, ncg, size, asked, i, j; 49 struct disklabel *lp; 50 struct stat statb; 51 struct fs proto; 52 53 havesb = 0; 54 if (stat("/", &statb) < 0) 55 errexit("Can't stat root\n"); 56 rootdev = statb.st_dev; 57 if (stat(dev, &statb) < 0) { 58 perror(dev); 59 printf("Can't stat %s\n", dev); 60 return (0); 61 } 62 rawflg = 0; 63 if ((statb.st_mode & S_IFMT) == S_IFBLK) 64 ; 65 else if ((statb.st_mode & S_IFMT) == S_IFCHR) 66 rawflg++; 67 else { 68 if (reply("file is not a block or character device; OK") == 0) 69 return (0); 70 } 71 if (rootdev == statb.st_rdev) 72 hotroot++; 73 if ((dfile.rfdes = open(dev, O_RDONLY)) < 0) { 74 perror(dev); 75 printf("Can't open %s\n", dev); 76 return (0); 77 } 78 if (preen == 0) 79 printf("** %s", dev); 80 if (nflag || (dfile.wfdes = open(dev, O_WRONLY)) < 0) { 81 dfile.wfdes = -1; 82 if (preen) 83 pfatal("NO WRITE ACCESS"); 84 printf(" (NO WRITE)"); 85 } 86 if (preen == 0) 87 printf("\n"); 88 dfile.mod = 0; 89 lfdir = 0; 90 initbarea(&sblk); 91 initbarea(&asblk); 92 sblk.b_un.b_buf = malloc(SBSIZE); 93 asblk.b_un.b_buf = malloc(SBSIZE); 94 if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL) 95 errexit("cannot allocate space for superblock\n"); 96 if (lp = getdisklabel((char *)NULL, dfile.rfdes)) 97 dev_bsize = secsize = lp->d_secsize; 98 else 99 dev_bsize = secsize = DEV_BSIZE; 100 /* 101 * Read in the superblock, looking for alternates if necessary 102 */ 103 if (readsb(1) == 0) { 104 if (bflag || preen || calcsb(dev, dfile.rfdes, &proto) == 0) 105 return(0); 106 if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0) 107 return (0); 108 for (cg = 0; cg < proto.fs_ncg; cg++) { 109 bflag = fsbtodb(&proto, cgsblock(&proto, cg)); 110 if (readsb(0) != 0) 111 break; 112 } 113 if (cg >= proto.fs_ncg) { 114 printf("%s %s\n%s %s\n%s %s\n", 115 "SEARCH FOR ALTERNATE SUPER-BLOCK", 116 "FAILED. YOU MUST USE THE", 117 "-b OPTION TO FSCK TO SPECIFY THE", 118 "LOCATION OF AN ALTERNATE", 119 "SUPER-BLOCK TO SUPPLY NEEDED", 120 "INFORMATION; SEE fsck(8)."); 121 return(0); 122 } 123 pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag); 124 } 125 fmax = sblock.fs_size; 126 imax = sblock.fs_ncg * sblock.fs_ipg; 127 /* 128 * Check and potentially fix certain fields in the super block. 129 */ 130 if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) { 131 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK"); 132 if (reply("SET TO DEFAULT") == 1) { 133 sblock.fs_optim = FS_OPTTIME; 134 sbdirty(); 135 } 136 } 137 if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) { 138 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK", 139 sblock.fs_minfree); 140 if (reply("SET TO DEFAULT") == 1) { 141 sblock.fs_minfree = 10; 142 sbdirty(); 143 } 144 } 145 if (sblock.fs_interleave < 1) { 146 pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK", 147 sblock.fs_interleave); 148 sblock.fs_interleave = 1; 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_npsect < sblock.fs_nsect) { 157 pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK", 158 sblock.fs_npsect); 159 sblock.fs_npsect = sblock.fs_nsect; 160 if (preen) 161 printf(" (FIXED)\n"); 162 if (preen || reply("SET TO DEFAULT") == 1) { 163 sbdirty(); 164 dirty(&asblk); 165 } 166 } 167 if (cvtflag) { 168 if (sblock.fs_postblformat == FS_42POSTBLFMT) { 169 /* 170 * Requested to convert from old format to new format 171 */ 172 if (preen) 173 pwarn("CONVERTING TO NEW FILE SYSTEM FORMAT\n"); 174 else if (!reply("CONVERT TO NEW FILE SYSTEM FORMAT")) 175 return(0); 176 sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT; 177 sblock.fs_nrpos = 8; 178 sblock.fs_postbloff = 179 (char *)(&sblock.fs_opostbl[0][0]) - 180 (char *)(&sblock.fs_link); 181 sblock.fs_rotbloff = &sblock.fs_space[0] - 182 (u_char *)(&sblock.fs_link); 183 sblock.fs_cgsize = 184 fragroundup(&sblock, CGSIZE(&sblock)); 185 /* 186 * Planning now for future expansion. 187 */ 188 # if (BYTE_ORDER == BIG_ENDIAN) 189 sblock.fs_qbmask.val[0] = 0; 190 sblock.fs_qbmask.val[1] = ~sblock.fs_bmask; 191 sblock.fs_qfmask.val[0] = 0; 192 sblock.fs_qfmask.val[1] = ~sblock.fs_fmask; 193 # endif /* BIG_ENDIAN */ 194 # if (BYTE_ORDER == LITTLE_ENDIAN) 195 sblock.fs_qbmask.val[0] = ~sblock.fs_bmask; 196 sblock.fs_qbmask.val[1] = 0; 197 sblock.fs_qfmask.val[0] = ~sblock.fs_fmask; 198 sblock.fs_qfmask.val[1] = 0; 199 # endif /* LITTLE_ENDIAN */ 200 sbdirty(); 201 dirty(&asblk); 202 } else if (sblock.fs_postblformat == FS_DYNAMICPOSTBLFMT) { 203 /* 204 * Requested to convert from new format to old format 205 */ 206 if (sblock.fs_nrpos != 8 || sblock.fs_ipg > 2048 || 207 sblock.fs_cpg > 32 || sblock.fs_cpc > 16) { 208 printf( 209 "PARAMETERS OF CURRENT FILE SYSTEM DO NOT\n\t"); 210 errexit( 211 "ALLOW CONVERSION TO OLD FILE SYSTEM FORMAT\n"); 212 } 213 if (preen) 214 pwarn("CONVERTING TO OLD FILE SYSTEM FORMAT\n"); 215 else if (!reply("CONVERT TO OLD FILE SYSTEM FORMAT")) 216 return(0); 217 sblock.fs_postblformat = FS_42POSTBLFMT; 218 sblock.fs_cgsize = fragroundup(&sblock, 219 sizeof(struct ocg) + howmany(sblock.fs_fpg, NBBY)); 220 sbdirty(); 221 dirty(&asblk); 222 } else { 223 errexit("UNKNOWN FILE SYSTEM FORMAT\n"); 224 } 225 } 226 if (asblk.b_dirty) { 227 bcopy((char *)&sblock, (char *)&altsblock, sblock.fs_sbsize); 228 flush(&dfile, &asblk); 229 } 230 /* 231 * read in the summary info. 232 */ 233 asked = 0; 234 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) { 235 size = sblock.fs_cssize - i < sblock.fs_bsize ? 236 sblock.fs_cssize - i : sblock.fs_bsize; 237 sblock.fs_csp[j] = (struct csum *)calloc(1, (unsigned)size); 238 if (bread(&dfile, (char *)sblock.fs_csp[j], 239 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag), 240 size) != 0 && !asked) { 241 pfatal("BAD SUMMARY INFORMATION"); 242 if (reply("CONTINUE") == 0) 243 errexit(""); 244 asked++; 245 } 246 } 247 /* 248 * allocate and initialize the necessary maps 249 */ 250 bmapsz = roundup(howmany(fmax, NBBY), sizeof(short)); 251 blockmap = calloc((unsigned)bmapsz, sizeof (char)); 252 if (blockmap == NULL) { 253 printf("cannot alloc %d bytes for blockmap\n", bmapsz); 254 goto badsb; 255 } 256 statemap = calloc((unsigned)(imax + 1), sizeof(char)); 257 if (statemap == NULL) { 258 printf("cannot alloc %d bytes for statemap\n", imax + 1); 259 goto badsb; 260 } 261 lncntp = (short *)calloc((unsigned)(imax + 1), sizeof(short)); 262 if (lncntp == NULL) { 263 printf("cannot alloc %d bytes for lncntp\n", 264 (imax + 1) * sizeof(short)); 265 goto badsb; 266 } 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 readsb(listerr) 280 int listerr; 281 { 282 off_t sboff; 283 daddr_t super = bflag ? bflag : SBOFF / dev_bsize; 284 285 if (bread(&dfile, (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 /* 312 * Set all possible fields that could differ, then do check 313 * of whole super block against an alternate super block. 314 * When an alternate super-block is specified this check is skipped. 315 */ 316 getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize); 317 if (asblk.b_errs) 318 return (0); 319 if (bflag) { 320 havesb = 1; 321 return (1); 322 } 323 altsblock.fs_link = sblock.fs_link; 324 altsblock.fs_rlink = sblock.fs_rlink; 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 bcopy((char *)sblock.fs_fsmnt, (char *)altsblock.fs_fsmnt, 340 sizeof sblock.fs_fsmnt); 341 bcopy((char *)sblock.fs_sparecon, (char *)altsblock.fs_sparecon, 342 sizeof sblock.fs_sparecon); 343 /* 344 * The following should not have to be copied. 345 */ 346 altsblock.fs_fsbtodb = sblock.fs_fsbtodb; 347 altsblock.fs_interleave = sblock.fs_interleave; 348 altsblock.fs_npsect = sblock.fs_npsect; 349 altsblock.fs_nrpos = sblock.fs_nrpos; 350 if (bcmp((char *)&sblock, (char *)&altsblock, (int)sblock.fs_sbsize)) { 351 badsb(listerr, 352 "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE"); 353 return (0); 354 } 355 havesb = 1; 356 return (1); 357 } 358 359 badsb(listerr, s) 360 int listerr; 361 char *s; 362 { 363 364 if (!listerr) 365 return; 366 if (preen) 367 printf("%s: ", devname); 368 pfatal("BAD SUPER BLOCK: %s\n", s); 369 } 370 371 /* 372 * Calculate a prototype superblock based on information in the disk label. 373 * When done the cgsblock macro can be calculated and the fs_ncg field 374 * can be used. Do NOT attempt to use other macros without verifying that 375 * their needed information is available! 376 */ 377 calcsb(dev, devfd, fs) 378 char *dev; 379 int devfd; 380 register struct fs *fs; 381 { 382 register struct disklabel *lp; 383 register struct partition *pp; 384 register char *cp; 385 int i; 386 387 cp = index(dev, '\0') - 1; 388 if (cp == (char *)-1 || (*cp < 'a' || *cp > 'h') && !isdigit(*cp)) { 389 pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev); 390 return (0); 391 } 392 lp = getdisklabel(dev, devfd); 393 if (isdigit(*cp)) 394 pp = &lp->d_partitions[0]; 395 else 396 pp = &lp->d_partitions[*cp - 'a']; 397 if (pp->p_fstype != FS_BSDFFS) { 398 pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n", 399 dev, pp->p_fstype < FSMAXTYPES ? 400 fstypenames[pp->p_fstype] : "unknown"); 401 return (0); 402 } 403 bzero(fs, sizeof(struct fs)); 404 fs->fs_fsize = pp->p_fsize; 405 fs->fs_frag = pp->p_frag; 406 fs->fs_cpg = pp->p_cpg; 407 fs->fs_size = pp->p_size; 408 fs->fs_ntrak = lp->d_ntracks; 409 fs->fs_nsect = lp->d_nsectors; 410 fs->fs_spc = lp->d_secpercyl; 411 fs->fs_nspf = fs->fs_fsize / lp->d_secsize; 412 fs->fs_sblkno = roundup( 413 howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize), 414 fs->fs_frag); 415 fs->fs_cgmask = 0xffffffff; 416 for (i = fs->fs_ntrak; i > 1; i >>= 1) 417 fs->fs_cgmask <<= 1; 418 if (!POWEROF2(fs->fs_ntrak)) 419 fs->fs_cgmask <<= 1; 420 fs->fs_cgoffset = roundup( 421 howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag); 422 fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs); 423 fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg); 424 for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1) 425 fs->fs_fsbtodb++; 426 dev_bsize = lp->d_secsize; 427 return (1); 428 } 429 430 struct disklabel * 431 getdisklabel(s, fd) 432 char *s; 433 int fd; 434 { 435 static struct disklabel lab; 436 437 if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) { 438 if (s == NULL) 439 return ((struct disklabel *)NULL); 440 pwarn(""); 441 perror("ioctl (GDINFO)"); 442 errexit("%s: can't read disk label", s); 443 } 444 return (&lab); 445 } 446