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.20 (Berkeley) 06/26/89"; 9 #endif not lint 10 11 #define DKTYPENAMES 12 #include <sys/param.h> 13 #include <sys/inode.h> 14 #include <sys/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 *calloc(); 41 char *index(); 42 43 setup(dev) 44 char *dev; 45 { 46 dev_t rootdev; 47 long cg, ncg, size, asked, i, j; 48 struct disklabel *getdisklabel(), *lp; 49 struct stat statb; 50 struct fs proto; 51 52 havesb = 0; 53 if (stat("/", &statb) < 0) 54 errexit("Can't stat root\n"); 55 rootdev = statb.st_dev; 56 if (stat(dev, &statb) < 0) { 57 perror(dev); 58 printf("Can't stat %s\n", dev); 59 return (0); 60 } 61 rawflg = 0; 62 if ((statb.st_mode & S_IFMT) == S_IFBLK) 63 ; 64 else if ((statb.st_mode & S_IFMT) == S_IFCHR) 65 rawflg++; 66 else { 67 if (reply("file is not a block or character device; OK") == 0) 68 return (0); 69 } 70 if (rootdev == statb.st_rdev) 71 hotroot++; 72 if ((dfile.rfdes = open(dev, O_RDONLY)) < 0) { 73 perror(dev); 74 printf("Can't open %s\n", dev); 75 return (0); 76 } 77 if (preen == 0) 78 printf("** %s", dev); 79 if (nflag || (dfile.wfdes = open(dev, O_WRONLY)) < 0) { 80 dfile.wfdes = -1; 81 if (preen) 82 pfatal("NO WRITE ACCESS"); 83 printf(" (NO WRITE)"); 84 } 85 if (preen == 0) 86 printf("\n"); 87 dfile.mod = 0; 88 lfdir = 0; 89 initbarea(&sblk); 90 initbarea(&asblk); 91 sblk.b_un.b_buf = (char *)malloc(SBSIZE); 92 asblk.b_un.b_buf = (char *)malloc(SBSIZE); 93 if (sblk.b_un.b_buf == 0 || asblk.b_un.b_buf == 0) 94 errexit("cannot allocate space for superblock\n"); 95 if (lp = getdisklabel((char *)NULL, dfile.rfdes)) 96 dev_bsize = secsize = lp->d_secsize; 97 else 98 dev_bsize = secsize = DEV_BSIZE; 99 /* 100 * Read in the superblock, looking for alternates if necessary 101 */ 102 if (readsb(1) == 0) { 103 if (bflag || preen || calcsb(dev, dfile.rfdes, &proto) == 0) 104 return(0); 105 if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0) 106 return (0); 107 for (cg = 0; cg < proto.fs_ncg; cg++) { 108 bflag = fsbtodb(&proto, cgsblock(&proto, cg)); 109 if (readsb(0) != 0) 110 break; 111 } 112 if (cg >= proto.fs_ncg) { 113 printf("%s %s\n%s %s\n%s %s\n", 114 "SEARCH FOR ALTERNATE SUPER-BLOCK", 115 "FAILED. YOU MUST USE THE", 116 "-b OPTION TO FSCK TO SPECIFY THE", 117 "LOCATION OF AN ALTERNATE", 118 "SUPER-BLOCK TO SUPPLY NEEDED", 119 "INFORMATION; SEE fsck(8)."); 120 return(0); 121 } 122 pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag); 123 } 124 fmax = sblock.fs_size; 125 imax = sblock.fs_ncg * sblock.fs_ipg; 126 /* 127 * Check and potentially fix certain fields in the super block. 128 */ 129 if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) { 130 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK"); 131 if (reply("SET TO DEFAULT") == 1) { 132 sblock.fs_optim = FS_OPTTIME; 133 sbdirty(); 134 } 135 } 136 if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) { 137 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK", 138 sblock.fs_minfree); 139 if (reply("SET TO DEFAULT") == 1) { 140 sblock.fs_minfree = 10; 141 sbdirty(); 142 } 143 } 144 if (sblock.fs_interleave < 1) { 145 pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK", 146 sblock.fs_interleave); 147 sblock.fs_interleave = 1; 148 if (preen) 149 printf(" (FIXED)\n"); 150 if (preen || reply("SET TO DEFAULT") == 1) { 151 sbdirty(); 152 dirty(&asblk); 153 } 154 } 155 if (sblock.fs_npsect < sblock.fs_nsect) { 156 pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK", 157 sblock.fs_npsect); 158 sblock.fs_npsect = sblock.fs_nsect; 159 if (preen) 160 printf(" (FIXED)\n"); 161 if (preen || reply("SET TO DEFAULT") == 1) { 162 sbdirty(); 163 dirty(&asblk); 164 } 165 } 166 if (cvtflag) { 167 if (sblock.fs_postblformat == FS_42POSTBLFMT) { 168 /* 169 * Requested to convert from old format to new format 170 */ 171 if (preen) 172 pwarn("CONVERTING TO NEW FILE SYSTEM FORMAT\n"); 173 else if (!reply("CONVERT TO NEW FILE SYSTEM FORMAT")) 174 return(0); 175 sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT; 176 sblock.fs_nrpos = 8; 177 sblock.fs_postbloff = 178 (char *)(&sblock.fs_opostbl[0][0]) - 179 (char *)(&sblock.fs_link); 180 sblock.fs_rotbloff = &sblock.fs_space[0] - 181 (u_char *)(&sblock.fs_link); 182 sblock.fs_cgsize = 183 fragroundup(&sblock, CGSIZE(&sblock)); 184 /* 185 * Planning now for future expansion. 186 */ 187 # if (BYTE_ORDER == BIG_ENDIAN) 188 sblock.fs_qbmask.val[0] = 0; 189 sblock.fs_qbmask.val[1] = ~sblock.fs_bmask; 190 sblock.fs_qfmask.val[0] = 0; 191 sblock.fs_qfmask.val[1] = ~sblock.fs_fmask; 192 # endif /* BIG_ENDIAN */ 193 # if (BYTE_ORDER == LITTLE_ENDIAN) 194 sblock.fs_qbmask.val[0] = ~sblock.fs_bmask; 195 sblock.fs_qbmask.val[1] = 0; 196 sblock.fs_qfmask.val[0] = ~sblock.fs_fmask; 197 sblock.fs_qfmask.val[1] = 0; 198 # endif /* LITTLE_ENDIAN */ 199 sbdirty(); 200 dirty(&asblk); 201 } else if (sblock.fs_postblformat == FS_DYNAMICPOSTBLFMT) { 202 /* 203 * Requested to convert from new format to old format 204 */ 205 if (sblock.fs_nrpos != 8 || sblock.fs_ipg > 2048 || 206 sblock.fs_cpg > 32 || sblock.fs_cpc > 16) { 207 printf( 208 "PARAMETERS OF CURRENT FILE SYSTEM DO NOT\n\t"); 209 errexit( 210 "ALLOW CONVERSION TO OLD FILE SYSTEM FORMAT\n"); 211 } 212 if (preen) 213 pwarn("CONVERTING TO OLD FILE SYSTEM FORMAT\n"); 214 else if (!reply("CONVERT TO OLD FILE SYSTEM FORMAT")) 215 return(0); 216 sblock.fs_postblformat = FS_42POSTBLFMT; 217 sblock.fs_cgsize = fragroundup(&sblock, 218 sizeof(struct ocg) + howmany(sblock.fs_fpg, NBBY)); 219 sbdirty(); 220 dirty(&asblk); 221 } else { 222 errexit("UNKNOWN FILE SYSTEM FORMAT\n"); 223 } 224 } 225 if (asblk.b_dirty) { 226 bcopy((char *)&sblock, (char *)&altsblock, sblock.fs_sbsize); 227 flush(&dfile, &asblk); 228 } 229 /* 230 * read in the summary info. 231 */ 232 asked = 0; 233 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) { 234 size = sblock.fs_cssize - i < sblock.fs_bsize ? 235 sblock.fs_cssize - i : sblock.fs_bsize; 236 sblock.fs_csp[j] = (struct csum *)calloc(1, (unsigned)size); 237 if (bread(&dfile, (char *)sblock.fs_csp[j], 238 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag), 239 size) != 0 && !asked) { 240 pfatal("BAD SUMMARY INFORMATION"); 241 if (reply("CONTINUE") == 0) 242 errexit(""); 243 asked++; 244 } 245 } 246 /* 247 * allocate and initialize the necessary maps 248 */ 249 bmapsz = roundup(howmany(fmax, NBBY), sizeof(short)); 250 blockmap = calloc((unsigned)bmapsz, sizeof (char)); 251 if (blockmap == NULL) { 252 printf("cannot alloc %d bytes for blockmap\n", bmapsz); 253 goto badsb; 254 } 255 statemap = calloc((unsigned)(imax + 1), sizeof(char)); 256 if (statemap == NULL) { 257 printf("cannot alloc %d bytes for statemap\n", imax + 1); 258 goto badsb; 259 } 260 lncntp = (short *)calloc((unsigned)(imax + 1), sizeof(short)); 261 if (lncntp == NULL) { 262 printf("cannot alloc %d bytes for lncntp\n", 263 (imax + 1) * sizeof(short)); 264 goto badsb; 265 } 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 readsb(listerr) 279 int listerr; 280 { 281 off_t sboff; 282 daddr_t super = bflag ? bflag : SBOFF / dev_bsize; 283 284 if (bread(&dfile, (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 /* 311 * Set all possible fields that could differ, then do check 312 * of whole super block against an alternate super block. 313 * When an alternate super-block is specified this check is skipped. 314 */ 315 getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize); 316 if (asblk.b_errs != NULL) 317 return (0); 318 if (bflag) { 319 havesb = 1; 320 return (1); 321 } 322 altsblock.fs_link = sblock.fs_link; 323 altsblock.fs_rlink = sblock.fs_rlink; 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 bcopy((char *)sblock.fs_csp, (char *)altsblock.fs_csp, 337 sizeof sblock.fs_csp); 338 bcopy((char *)sblock.fs_fsmnt, (char *)altsblock.fs_fsmnt, 339 sizeof sblock.fs_fsmnt); 340 bcopy((char *)sblock.fs_sparecon, (char *)altsblock.fs_sparecon, 341 sizeof sblock.fs_sparecon); 342 /* 343 * The following should not have to be copied. 344 */ 345 altsblock.fs_fsbtodb = sblock.fs_fsbtodb; 346 altsblock.fs_interleave = sblock.fs_interleave; 347 altsblock.fs_npsect = sblock.fs_npsect; 348 altsblock.fs_nrpos = sblock.fs_nrpos; 349 if (bcmp((char *)&sblock, (char *)&altsblock, (int)sblock.fs_sbsize)) { 350 badsb(listerr, 351 "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE"); 352 return (0); 353 } 354 havesb = 1; 355 return (1); 356 } 357 358 badsb(listerr, s) 359 int listerr; 360 char *s; 361 { 362 363 if (!listerr) 364 return; 365 if (preen) 366 printf("%s: ", devname); 367 pfatal("BAD SUPER BLOCK: %s\n", s); 368 } 369 370 /* 371 * Calculate a prototype superblock based on information in the disk label. 372 * When done the cgsblock macro can be calculated and the fs_ncg field 373 * can be used. Do NOT attempt to use other macros without verifying that 374 * their needed information is available! 375 */ 376 calcsb(dev, devfd, fs) 377 char *dev; 378 int devfd; 379 register struct fs *fs; 380 { 381 register struct disklabel *lp; 382 register struct partition *pp; 383 register char *cp; 384 int i; 385 struct disklabel *getdisklabel(); 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