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