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