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