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