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