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.14 (Berkeley) 04/07/87"; 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 <ctype.h> 20 #include "fsck.h" 21 22 #define POWEROF2(num) (((num) & ((num) - 1)) == 0) 23 24 char *calloc(); 25 char *index(); 26 27 setup(dev) 28 char *dev; 29 { 30 dev_t rootdev; 31 long cg, ncg, size, i, j; 32 struct disklabel *getdisklabel(), *lp; 33 struct stat statb; 34 struct fs proto; 35 36 havesb = 0; 37 if (stat("/", &statb) < 0) 38 errexit("Can't stat root\n"); 39 rootdev = statb.st_dev; 40 if (stat(dev, &statb) < 0) { 41 perror(dev); 42 printf("Can't stat %s\n", dev); 43 return (0); 44 } 45 rawflg = 0; 46 if ((statb.st_mode & S_IFMT) == S_IFBLK) 47 ; 48 else if ((statb.st_mode & S_IFMT) == S_IFCHR) 49 rawflg++; 50 else { 51 if (reply("file is not a block or character device; OK") == 0) 52 return (0); 53 } 54 if (rootdev == statb.st_rdev) 55 hotroot++; 56 if ((dfile.rfdes = open(dev, O_RDONLY)) < 0) { 57 perror(dev); 58 printf("Can't open %s\n", dev); 59 return (0); 60 } 61 if (preen == 0) 62 printf("** %s", dev); 63 if (nflag || (dfile.wfdes = open(dev, O_WRONLY)) < 0) { 64 dfile.wfdes = -1; 65 if (preen) 66 pfatal("NO WRITE ACCESS"); 67 printf(" (NO WRITE)"); 68 } 69 if (preen == 0) 70 printf("\n"); 71 dfile.mod = 0; 72 lfdir = 0; 73 initbarea(&sblk); 74 initbarea(&fileblk); 75 initbarea(&inoblk); 76 initbarea(&cgblk); 77 if (lp = getdisklabel((char *)NULL, dfile.rfdes)) 78 dev_bsize = secsize = lp->d_secsize; 79 else { 80 dev_bsize = DEV_BSIZE; 81 secsize = 0; /* guess later */ 82 } 83 /* 84 * Read in the superblock, looking for alternates if necessary 85 */ 86 if (readsb(1) == 0) { 87 if (bflag || preen || calcsb(dev, dfile.rfdes, &proto) == 0) 88 return(0); 89 if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0) 90 return (0); 91 for (cg = 0; cg < proto.fs_ncg; cg++) { 92 bflag = fsbtodb(&proto, cgsblock(&proto, cg)); 93 if (readsb(0) != 0) 94 break; 95 } 96 if (cg >= proto.fs_ncg) { 97 printf("%s %s\n%s %s\n%s %s\n", 98 "SEARCH FOR ALTERNATE SUPER-BLOCK", 99 "FAILED. YOU MUST USE THE", 100 "-b OPTION TO FSCK TO SPECIFY THE", 101 "LOCATION OF AN ALTERNATE", 102 "SUPER-BLOCK TO SUPPLY NEEDED", 103 "INFORMATION; SEE fsck(8)."); 104 return(0); 105 } 106 pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag); 107 } 108 fmax = sblock.fs_size; 109 imax = sblock.fs_ncg * sblock.fs_ipg; 110 /* 111 * Check and potentially fix certain fields in the super block. 112 */ 113 if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) { 114 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK"); 115 if (reply("SET TO DEFAULT") == 1) { 116 sblock.fs_optim = FS_OPTTIME; 117 sbdirty(); 118 } 119 } 120 if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) { 121 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK", 122 sblock.fs_minfree); 123 if (reply("SET TO DEFAULT") == 1) { 124 sblock.fs_minfree = 10; 125 sbdirty(); 126 } 127 } 128 if (sblock.fs_interleave < 1) { 129 pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK", 130 sblock.fs_interleave); 131 sblock.fs_interleave = 1; 132 if (preen) 133 printf(" (FIXED)\n"); 134 if (preen || reply("SET TO DEFAULT") == 1) 135 sbdirty(); 136 } 137 if (sblock.fs_npsect < sblock.fs_nsect) { 138 pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK", 139 sblock.fs_npsect); 140 sblock.fs_npsect = sblock.fs_nsect; 141 if (preen) 142 printf(" (FIXED)\n"); 143 if (preen || reply("SET TO DEFAULT") == 1) 144 sbdirty(); 145 } 146 /* 147 * read in the summary info. 148 */ 149 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) { 150 size = sblock.fs_cssize - i < sblock.fs_bsize ? 151 sblock.fs_cssize - i : sblock.fs_bsize; 152 sblock.fs_csp[j] = (struct csum *)calloc(1, (unsigned)size); 153 if (bread(&dfile, (char *)sblock.fs_csp[j], 154 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag), 155 size) != 0) 156 return (0); 157 } 158 /* 159 * allocate and initialize the necessary maps 160 */ 161 bmapsz = roundup(howmany(fmax, NBBY), sizeof(short)); 162 blockmap = calloc((unsigned)bmapsz, sizeof (char)); 163 if (blockmap == NULL) { 164 printf("cannot alloc %d bytes for blockmap\n", bmapsz); 165 goto badsb; 166 } 167 statemap = calloc((unsigned)(imax + 1), sizeof(char)); 168 if (statemap == NULL) { 169 printf("cannot alloc %d bytes for statemap\n", imax + 1); 170 goto badsb; 171 } 172 lncntp = (short *)calloc((unsigned)(imax + 1), sizeof(short)); 173 if (lncntp == NULL) { 174 printf("cannot alloc %d bytes for lncntp\n", 175 (imax + 1) * sizeof(short)); 176 goto badsb; 177 } 178 179 return (1); 180 181 badsb: 182 ckfini(); 183 return (0); 184 } 185 186 /* 187 * Read in the super block and its summary info. 188 */ 189 readsb(listerr) 190 int listerr; 191 { 192 BUFAREA asblk; 193 # define altsblock asblk.b_un.b_fs 194 off_t sboff; 195 daddr_t super = bflag ? bflag : SBOFF / dev_bsize; 196 197 initbarea(&asblk); 198 if (bread(&dfile, (char *)&sblock, super, (long)SBSIZE) != 0) 199 return (0); 200 sblk.b_bno = super; 201 sblk.b_size = SBSIZE; 202 /* 203 * run a few consistency checks of the super block 204 */ 205 if (sblock.fs_magic != FS_MAGIC) 206 { badsb(listerr, "MAGIC NUMBER WRONG"); return (0); } 207 if (sblock.fs_ncg < 1) 208 { badsb(listerr, "NCG OUT OF RANGE"); return (0); } 209 if (sblock.fs_cpg < 1 || sblock.fs_cpg > MAXCPG) 210 { badsb(listerr, "CPG OUT OF RANGE"); return (0); } 211 if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl || 212 (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl) 213 { badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); } 214 if (sblock.fs_sbsize > SBSIZE) 215 { badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); } 216 /* 217 * Compute block size that the filesystem is based on, 218 * according to fsbtodb, and adjust superblock block number 219 * so we can tell if this is an alternate later. 220 */ 221 if (sblock.fs_dbsize && secsize == 0) 222 secsize = sblock.fs_dbsize; 223 super *= dev_bsize; 224 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1); 225 sblk.b_bno = super / dev_bsize; 226 /* 227 * Set all possible fields that could differ, then do check 228 * of whole super block against an alternate super block. 229 * When an alternate super-block is specified this check is skipped. 230 */ 231 if (bflag) { 232 havesb = 1; 233 return (1); 234 } 235 getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize); 236 if (asblk.b_errs != NULL) 237 return (0); 238 altsblock.fs_fsbtodb = sblock.fs_fsbtodb; 239 altsblock.fs_dbsize = sblock.fs_dbsize; 240 altsblock.fs_link = sblock.fs_link; 241 altsblock.fs_rlink = sblock.fs_rlink; 242 altsblock.fs_time = sblock.fs_time; 243 altsblock.fs_cstotal = sblock.fs_cstotal; 244 altsblock.fs_cgrotor = sblock.fs_cgrotor; 245 altsblock.fs_fmod = sblock.fs_fmod; 246 altsblock.fs_clean = sblock.fs_clean; 247 altsblock.fs_ronly = sblock.fs_ronly; 248 altsblock.fs_flags = sblock.fs_flags; 249 altsblock.fs_maxcontig = sblock.fs_maxcontig; 250 altsblock.fs_minfree = sblock.fs_minfree; 251 altsblock.fs_optim = sblock.fs_optim; 252 altsblock.fs_rotdelay = sblock.fs_rotdelay; 253 altsblock.fs_maxbpg = sblock.fs_maxbpg; 254 altsblock.fs_npsect = sblock.fs_npsect; 255 altsblock.fs_interleave = sblock.fs_interleave; 256 bcopy((char *)sblock.fs_csp, (char *)altsblock.fs_csp, 257 sizeof sblock.fs_csp); 258 bcopy((char *)sblock.fs_fsmnt, (char *)altsblock.fs_fsmnt, 259 sizeof sblock.fs_fsmnt); 260 bcopy((char *)sblock.fs_sparecon, (char *)altsblock.fs_sparecon, 261 sizeof sblock.fs_sparecon); 262 if (bcmp((char *)&sblock, (char *)&altsblock, (int)sblock.fs_sbsize)) { 263 badsb(listerr, 264 "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE"); 265 return (0); 266 } 267 havesb = 1; 268 return (1); 269 # undef altsblock 270 } 271 272 badsb(listerr, s) 273 int listerr; 274 char *s; 275 { 276 277 if (!listerr) 278 return; 279 if (preen) 280 printf("%s: ", devname); 281 pfatal("BAD SUPER BLOCK: %s\n", s); 282 } 283 284 /* 285 * Calculate a prototype superblock based on information in the disk label. 286 * When done the cgsblock macro can be calculated and the fs_ncg field 287 * can be used. Do NOT attempt to use other macros without verifying that 288 * their needed information is available! 289 */ 290 calcsb(dev, devfd, fs) 291 char *dev; 292 int devfd; 293 register struct fs *fs; 294 { 295 register struct disklabel *lp; 296 register struct partition *pp; 297 register char *cp; 298 int i; 299 300 cp = index(dev, '\0') - 1; 301 if (cp == (char *)-1 || (*cp < 'a' || *cp > 'h') && !isdigit(*cp)) { 302 pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev); 303 return (0); 304 } 305 lp = getdisklabel(dev, devfd); 306 if (isdigit(*cp)) 307 pp = &lp->d_partitions[0]; 308 else 309 pp = &lp->d_partitions[*cp - 'a']; 310 if (pp->p_fstype != FS_BSDFFS) { 311 pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n", 312 dev, pp->p_fstype < FSMAXTYPES ? 313 fstypenames[pp->p_fstype] : "unknown"); 314 return (0); 315 } 316 bzero(fs, sizeof(struct fs)); 317 fs->fs_fsize = pp->p_fsize; 318 fs->fs_frag = pp->p_frag; 319 fs->fs_cpg = pp->p_cpg; 320 fs->fs_size = pp->p_size; 321 fs->fs_ntrak = lp->d_ntracks; 322 fs->fs_nsect = lp->d_nsectors; 323 fs->fs_spc = lp->d_secpercyl; 324 fs->fs_nspf = fs->fs_fsize / lp->d_secsize; 325 fs->fs_sblkno = roundup( 326 howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize), 327 fs->fs_frag); 328 fs->fs_cgmask = 0xffffffff; 329 for (i = fs->fs_ntrak; i > 1; i >>= 1) 330 fs->fs_cgmask <<= 1; 331 if (!POWEROF2(fs->fs_ntrak)) 332 fs->fs_cgmask <<= 1; 333 fs->fs_cgoffset = roundup( 334 howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag); 335 fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs); 336 fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg); 337 for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1) 338 fs->fs_fsbtodb++; 339 dev_bsize = lp->d_secsize; 340 return (1); 341 } 342 343 struct disklabel * 344 getdisklabel(s, fd) 345 char *s; 346 int fd; 347 { 348 static struct disklabel lab; 349 350 if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) { 351 if (s == NULL) 352 return ((struct disklabel *)NULL); 353 pwarn(""); 354 perror("ioctl (GDINFO)"); 355 errexit("%s: can't read disk label", s); 356 } 357 return (&lab); 358 } 359