1 /* $NetBSD: setup.c,v 1.3 1997/07/10 04:53:35 mikel Exp $ */ 2 3 /* 4 * Copyright (c) 1997 Manuel Bouyer. 5 * Copyright (c) 1980, 1986, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #ifndef lint 38 #if 0 39 static char sccsid[] = "@(#)setup.c 8.5 (Berkeley) 11/23/94"; 40 #else 41 static char rcsid[] = "$NetBSD: setup.c,v 1.3 1997/07/10 04:53:35 mikel Exp $"; 42 #endif 43 #endif /* not lint */ 44 45 #define DKTYPENAMES 46 #include <sys/param.h> 47 #include <sys/time.h> 48 #include <ufs/ext2fs/ext2fs_dinode.h> 49 #include <ufs/ext2fs/ext2fs.h> 50 #include <sys/stat.h> 51 #include <sys/ioctl.h> 52 #include <sys/disklabel.h> 53 #include <sys/file.h> 54 55 #include <errno.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <string.h> 59 #include <ctype.h> 60 61 #include "fsck.h" 62 #include "extern.h" 63 #include "fsutil.h" 64 65 struct bufarea asblk; 66 #define altsblock (*asblk.b_un.b_fs) 67 #define POWEROF2(num) (((num) & ((num) - 1)) == 0) 68 69 void badsb __P((int, char *)); 70 int calcsb __P((char *, int, struct m_ext2fs *)); 71 static struct disklabel *getdisklabel __P((char *, int)); 72 static int readsb __P((int)); 73 74 int 75 setup(dev) 76 char *dev; 77 { 78 long cg, size, asked, i, j; 79 long bmapsize; 80 struct disklabel *lp; 81 off_t sizepb; 82 struct stat statb; 83 struct m_ext2fs proto; 84 int doskipclean; 85 u_int64_t maxfilesize; 86 87 havesb = 0; 88 fswritefd = -1; 89 doskipclean = skipclean; 90 if (stat(dev, &statb) < 0) { 91 printf("Can't stat %s: %s\n", dev, strerror(errno)); 92 return (0); 93 } 94 if (!S_ISCHR(statb.st_mode)) { 95 pfatal("%s is not a character device", dev); 96 if (reply("CONTINUE") == 0) 97 return (0); 98 } 99 if ((fsreadfd = open(dev, O_RDONLY)) < 0) { 100 printf("Can't open %s: %s\n", dev, strerror(errno)); 101 return (0); 102 } 103 if (preen == 0) 104 printf("** %s", dev); 105 if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) { 106 fswritefd = -1; 107 if (preen) 108 pfatal("NO WRITE ACCESS"); 109 printf(" (NO WRITE)"); 110 } 111 if (preen == 0) 112 printf("\n"); 113 fsmodified = 0; 114 lfdir = 0; 115 initbarea(&sblk); 116 initbarea(&asblk); 117 sblk.b_un.b_buf = malloc(sizeof(struct m_ext2fs)); 118 asblk.b_un.b_buf = malloc(sizeof(struct m_ext2fs)); 119 if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL) 120 errexit("cannot allocate space for superblock\n"); 121 if ((lp = getdisklabel((char *)NULL, fsreadfd)) != NULL) 122 dev_bsize = secsize = lp->d_secsize; 123 else 124 dev_bsize = secsize = DEV_BSIZE; 125 /* 126 * Read in the superblock, looking for alternates if necessary 127 */ 128 if (readsb(1) == 0) { 129 if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0) 130 return(0); 131 if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0) 132 return (0); 133 for (cg = 1; cg < proto.e2fs_ncg; cg++) { 134 bflag = fsbtodb(&proto, 135 cg * proto.e2fs.e2fs_bpg + proto.e2fs.e2fs_first_dblock); 136 if (readsb(0) != 0) 137 break; 138 } 139 if (cg >= proto.e2fs_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_FFS TO SPECIFY THE", 144 "LOCATION OF AN ALTERNATE", 145 "SUPER-BLOCK TO SUPPLY NEEDED", 146 "INFORMATION; SEE fsck_ext2fs(8)."); 147 return(0); 148 } 149 doskipclean = 0; 150 pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag); 151 } 152 if (debug) 153 printf("state = %d\n", sblock.e2fs.e2fs_state); 154 if (sblock.e2fs.e2fs_state == E2FS_ISCLEAN) { 155 if (doskipclean) { 156 pwarn("%sile system is clean; not checking\n", 157 preen ? "f" : "** F"); 158 return (-1); 159 } 160 if (!preen) 161 pwarn("** File system is already clean\n"); 162 } 163 maxfsblock = sblock.e2fs.e2fs_bcount; 164 maxino = sblock.e2fs_ncg * sblock.e2fs.e2fs_ipg; 165 sizepb = sblock.e2fs_bsize; 166 maxfilesize = sblock.e2fs_bsize * NDADDR - 1; 167 for (i = 0; i < NIADDR; i++) { 168 sizepb *= NINDIR(&sblock); 169 maxfilesize += sizepb; 170 } 171 /* 172 * Check and potentially fix certain fields in the super block. 173 */ 174 if ((sblock.e2fs.e2fs_rbcount < 0) || 175 (sblock.e2fs.e2fs_rbcount > sblock.e2fs.e2fs_bcount)) { 176 pfatal("IMPOSSIBLE RESERVED BLOCK COUNT=%d IN SUPERBLOCK", 177 sblock.e2fs.e2fs_rbcount); 178 if (reply("SET TO DEFAULT") == 1) { 179 sblock.e2fs.e2fs_rbcount = sblock.e2fs.e2fs_bcount * 0.1; 180 sbdirty(); 181 } 182 } 183 if (sblock.e2fs.e2fs_bpg != sblock.e2fs.e2fs_fpg) { 184 pfatal("WRONG FPG=%d (BPG=%d) IN SUPERBLOCK", 185 sblock.e2fs.e2fs_fpg, sblock.e2fs.e2fs_bpg); 186 return 0; 187 } 188 if (asblk.b_dirty && !bflag) { 189 memcpy(&altsblock, &sblock, (size_t)SBSIZE); 190 flush(fswritefd, &asblk); 191 } 192 /* 193 * read in the summary info. 194 */ 195 196 sblock.e2fs_gd = malloc(sblock.e2fs_ngdb * sblock.e2fs_bsize); 197 asked = 0; 198 for (i=0; i < sblock.e2fs_ngdb; i++) { 199 if (bread(fsreadfd,(char *) 200 &sblock.e2fs_gd[i* sblock.e2fs_bsize / sizeof(struct ext2_gd)], 201 fsbtodb(&sblock, ((sblock.e2fs_bsize>1024)?0:1)+i+1), 202 sblock.e2fs_bsize) != 0 && !asked) { 203 pfatal("BAD SUMMARY INFORMATION"); 204 if (reply("CONTINUE") == 0) 205 errexit("%s\n", ""); 206 asked++; 207 } 208 } 209 /* 210 * allocate and initialize the necessary maps 211 */ 212 bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t)); 213 blockmap = calloc((unsigned)bmapsize, sizeof (char)); 214 if (blockmap == NULL) { 215 printf("cannot alloc %u bytes for blockmap\n", 216 (unsigned)bmapsize); 217 goto badsblabel; 218 } 219 statemap = calloc((unsigned)(maxino + 2), sizeof(char)); 220 if (statemap == NULL) { 221 printf("cannot alloc %u bytes for statemap\n", 222 (unsigned)(maxino + 1)); 223 goto badsblabel; 224 } 225 lncntp = (int16_t *)calloc((unsigned)(maxino + 1), sizeof(int16_t)); 226 if (lncntp == NULL) { 227 printf("cannot alloc %u bytes for lncntp\n", 228 (unsigned)(maxino + 1) * sizeof(int16_t)); 229 goto badsblabel; 230 } 231 for (numdirs = 0, cg = 0; cg < sblock.e2fs_ncg; cg++) { 232 numdirs += sblock.e2fs_gd[cg].ext2bgd_ndirs; 233 } 234 inplast = 0; 235 listmax = numdirs + 10; 236 inpsort = (struct inoinfo **)calloc((unsigned)listmax, 237 sizeof(struct inoinfo *)); 238 inphead = (struct inoinfo **)calloc((unsigned)numdirs, 239 sizeof(struct inoinfo *)); 240 if (inpsort == NULL || inphead == NULL) { 241 printf("cannot alloc %u bytes for inphead\n", 242 (unsigned)numdirs * sizeof(struct inoinfo *)); 243 goto badsblabel; 244 } 245 bufinit(); 246 return (1); 247 248 badsblabel: 249 ckfini(0); 250 return (0); 251 } 252 253 /* 254 * Read in the super block and its summary info. 255 */ 256 static int 257 readsb(listerr) 258 int listerr; 259 { 260 daddr_t super = bflag ? bflag : SBOFF / dev_bsize; 261 262 if (bread(fsreadfd, (char *)&sblock.e2fs, super, (long)SBSIZE) != 0) 263 return (0); 264 sblk.b_bno = super; 265 sblk.b_size = SBSIZE; 266 /* 267 * run a few consistency checks of the super block 268 */ 269 if (sblock.e2fs.e2fs_magic != E2FS_MAGIC) { 270 badsb(listerr, "MAGIC NUMBER WRONG"); return (0); 271 } 272 if (sblock.e2fs.e2fs_log_bsize > 2) { 273 badsb(listerr, "BAD LOG_BSIZE"); return (0); 274 } 275 276 /* compute the dynamic fields of the in-memory sb */ 277 /* compute dynamic sb infos */ 278 sblock.e2fs_ncg = 279 howmany(sblock.e2fs.e2fs_bcount - sblock.e2fs.e2fs_first_dblock, 280 sblock.e2fs.e2fs_bpg); 281 /* XXX assume hw bsize = 512 */ 282 sblock.e2fs_fsbtodb = sblock.e2fs.e2fs_log_bsize + 1; 283 sblock.e2fs_bsize = 1024 << sblock.e2fs.e2fs_log_bsize; 284 sblock.e2fs_bshift = LOG_MINBSIZE + sblock.e2fs.e2fs_log_bsize; 285 sblock.e2fs_qbmask = sblock.e2fs_bsize - 1; 286 sblock.e2fs_bmask = ~sblock.e2fs_qbmask; 287 sblock.e2fs_ngdb = howmany(sblock.e2fs_ncg, 288 sblock.e2fs_bsize / sizeof(struct ext2_gd)); 289 sblock.e2fs_ipb = sblock.e2fs_bsize / sizeof(struct ext2fs_dinode); 290 sblock.e2fs_itpg = sblock.e2fs.e2fs_ipg/sblock.e2fs_ipb; 291 292 cgoverhead = 1 /* super block */ + 293 sblock.e2fs_ngdb + 294 1 /* block bitmap */ + 295 1 /* inode bitmap */ + 296 sblock.e2fs_itpg; 297 298 if (debug) /* DDD */ 299 printf("cg overhead %d blocks \n", cgoverhead); 300 /* 301 * Compute block size that the filesystem is based on, 302 * according to fsbtodb, and adjust superblock block number 303 * so we can tell if this is an alternate later. 304 */ 305 super *= dev_bsize; 306 dev_bsize = sblock.e2fs_bsize / fsbtodb(&sblock, 1); 307 sblk.b_bno = super / dev_bsize; 308 if (bflag) { 309 havesb = 1; 310 return (1); 311 } 312 313 /* 314 * Set all possible fields that could differ, then do check 315 * of whole super block against an alternate super block. 316 * When an alternate super-block is specified this check is skipped. 317 */ 318 getblk(&asblk, 1 * sblock.e2fs.e2fs_bpg + sblock.e2fs.e2fs_first_dblock, 319 (long)SBSIZE); 320 if (asblk.b_errs) 321 return (0); 322 altsblock.e2fs.e2fs_rbcount = sblock.e2fs.e2fs_rbcount; 323 altsblock.e2fs.e2fs_fbcount = sblock.e2fs.e2fs_fbcount; 324 altsblock.e2fs.e2fs_ficount = sblock.e2fs.e2fs_ficount; 325 altsblock.e2fs.e2fs_mtime = sblock.e2fs.e2fs_mtime; 326 altsblock.e2fs.e2fs_wtime = sblock.e2fs.e2fs_wtime; 327 altsblock.e2fs.e2fs_mnt_count = sblock.e2fs.e2fs_mnt_count; 328 altsblock.e2fs.e2fs_max_mnt_count = sblock.e2fs.e2fs_max_mnt_count; 329 altsblock.e2fs.e2fs_state = sblock.e2fs.e2fs_state; 330 altsblock.e2fs.e2fs_beh = sblock.e2fs.e2fs_beh; 331 altsblock.e2fs.e2fs_lastfsck = sblock.e2fs.e2fs_lastfsck; 332 altsblock.e2fs.e2fs_fsckintv = sblock.e2fs.e2fs_fsckintv; 333 altsblock.e2fs.e2fs_ruid = sblock.e2fs.e2fs_ruid; 334 altsblock.e2fs.e2fs_rgid = sblock.e2fs.e2fs_rgid; 335 if (memcmp(&(sblock.e2fs), &(altsblock.e2fs), (int)SBSIZE)) { 336 if (debug) { 337 long *nlp, *olp, *endlp; 338 339 printf("superblock mismatches\n"); 340 nlp = (long *)&altsblock; 341 olp = (long *)&sblock; 342 endlp = olp + (SBSIZE / sizeof *olp); 343 for ( ; olp < endlp; olp++, nlp++) { 344 if (*olp == *nlp) 345 continue; 346 printf("offset %d, original %ld, alternate %ld\n", 347 olp - (long *)&sblock, *olp, *nlp); 348 } 349 } 350 badsb(listerr, 351 "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE"); 352 return (0); 353 } 354 havesb = 1; 355 return (1); 356 } 357 358 void 359 badsb(listerr, s) 360 int listerr; 361 char *s; 362 { 363 364 if (!listerr) 365 return; 366 if (preen) 367 printf("%s: ", cdevname()); 368 pfatal("BAD SUPER BLOCK: %s\n", s); 369 } 370 371 /* 372 * Calculate a prototype superblock based on information in the disk label. 373 * When done the cgsblock macro can be calculated and the fs_ncg field 374 * can be used. Do NOT attempt to use other macros without verifying that 375 * their needed information is available! 376 */ 377 378 int 379 calcsb(dev, devfd, fs) 380 char *dev; 381 int devfd; 382 register struct m_ext2fs *fs; 383 { 384 register struct disklabel *lp; 385 register struct partition *pp; 386 register char *cp; 387 int i; 388 389 cp = strchr(dev, '\0') - 1; 390 if ((cp == (char *)-1 || (*cp < 'a' || *cp > 'h')) && !isdigit(*cp)) { 391 pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev); 392 return (0); 393 } 394 lp = getdisklabel(dev, devfd); 395 if (isdigit(*cp)) 396 pp = &lp->d_partitions[0]; 397 else 398 pp = &lp->d_partitions[*cp - 'a']; 399 if (pp->p_fstype != FS_BSDFFS) { 400 pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n", 401 dev, pp->p_fstype < FSMAXTYPES ? 402 fstypenames[pp->p_fstype] : "unknown"); 403 return (0); 404 } 405 memset(fs, 0, sizeof(struct m_ext2fs)); 406 fs->e2fs_bsize = 1024; /* XXX to look for altenate SP */ 407 fs->e2fs.e2fs_log_bsize = 0; 408 fs->e2fs.e2fs_bcount = (pp->p_size * DEV_BSIZE) / fs->e2fs_bsize; 409 fs->e2fs.e2fs_first_dblock = 1; 410 fs->e2fs.e2fs_bpg = fs->e2fs_bsize * NBBY; 411 fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs.e2fs_log_bsize; 412 fs->e2fs_qbmask = fs->e2fs_bsize - 1; 413 fs->e2fs_bmask = ~fs->e2fs_qbmask; 414 fs->e2fs_ncg = 415 howmany(fs->e2fs.e2fs_bcount - fs->e2fs.e2fs_first_dblock, 416 fs->e2fs.e2fs_bpg); 417 fs->e2fs_fsbtodb = fs->e2fs.e2fs_log_bsize + 1; 418 fs->e2fs_ngdb = howmany(fs->e2fs_ncg, 419 fs->e2fs_bsize / sizeof(struct ext2_gd)); 420 421 422 423 return (1); 424 } 425 426 static struct disklabel * 427 getdisklabel(s, fd) 428 char *s; 429 int fd; 430 { 431 static struct disklabel lab; 432 433 if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) { 434 if (s == NULL) 435 return ((struct disklabel *)NULL); 436 pwarn("ioctl (GCINFO): %s\n", strerror(errno)); 437 errexit("%s: can't read disk label\n", s); 438 } 439 return (&lab); 440 } 441