Home | History | Annotate | Line # | Download | only in fsck_ffs
setup.c revision 1.22
      1 /*	$NetBSD: setup.c,v 1.22 1995/07/12 01:49:23 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.22 1995/07/12 01:49:23 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 	int doskipclean;
     82 
     83 	havesb = 0;
     84 	fswritefd = -1;
     85 	doskipclean = skipclean;
     86 	if (stat(dev, &statb) < 0) {
     87 		printf("Can't stat %s: %s\n", dev, strerror(errno));
     88 		return (0);
     89 	}
     90 	if (!S_ISCHR(statb.st_mode)) {
     91 		pfatal("%s is not a character device", dev);
     92 		if (reply("CONTINUE") == 0)
     93 			return (0);
     94 	}
     95 	if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
     96 		printf("Can't open %s: %s\n", dev, strerror(errno));
     97 		return (0);
     98 	}
     99 	if (preen == 0)
    100 		printf("** %s", dev);
    101 	if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
    102 		fswritefd = -1;
    103 		if (preen)
    104 			pfatal("NO WRITE ACCESS");
    105 		printf(" (NO WRITE)");
    106 	}
    107 	if (preen == 0)
    108 		printf("\n");
    109 	fsmodified = 0;
    110 	lfdir = 0;
    111 	initbarea(&sblk);
    112 	initbarea(&asblk);
    113 	sblk.b_un.b_buf = malloc(SBSIZE);
    114 	asblk.b_un.b_buf = malloc(SBSIZE);
    115 	if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
    116 		errexit("cannot allocate space for superblock\n");
    117 	if (lp = getdisklabel((char *)NULL, fsreadfd))
    118 		dev_bsize = secsize = lp->d_secsize;
    119 	else
    120 		dev_bsize = secsize = DEV_BSIZE;
    121 	/*
    122 	 * Read in the superblock, looking for alternates if necessary
    123 	 */
    124 	if (readsb(1) == 0) {
    125 		if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
    126 			return(0);
    127 		if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
    128 			return (0);
    129 		for (cg = 0; cg < proto.fs_ncg; cg++) {
    130 			bflag = fsbtodb(&proto, cgsblock(&proto, cg));
    131 			if (readsb(0) != 0)
    132 				break;
    133 		}
    134 		if (cg >= proto.fs_ncg) {
    135 			printf("%s %s\n%s %s\n%s %s\n",
    136 				"SEARCH FOR ALTERNATE SUPER-BLOCK",
    137 				"FAILED. YOU MUST USE THE",
    138 				"-b OPTION TO FSCK TO SPECIFY THE",
    139 				"LOCATION OF AN ALTERNATE",
    140 				"SUPER-BLOCK TO SUPPLY NEEDED",
    141 				"INFORMATION; SEE fsck(8).");
    142 			return(0);
    143 		}
    144 		doskipclean = 0;
    145 		pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
    146 	}
    147 	if (debug)
    148 		printf("clean = %d\n", sblock.fs_clean);
    149 	if (sblock.fs_clean & FS_ISCLEAN) {
    150 		if (doskipclean) {
    151 			pwarn("%sile system is clean; not checking\n",
    152 			    preen ? "f" : "** F");
    153 			return (-1);
    154 		}
    155 		if (!preen)
    156 			pwarn("** File system is already clean\n");
    157 	}
    158 	maxfsblock = sblock.fs_size;
    159 	maxino = sblock.fs_ncg * sblock.fs_ipg;
    160 	/*
    161 	 * Check and potentially fix certain fields in the super block.
    162 	 */
    163 	if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
    164 		pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
    165 		if (reply("SET TO DEFAULT") == 1) {
    166 			sblock.fs_optim = FS_OPTTIME;
    167 			sbdirty();
    168 		}
    169 	}
    170 	if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
    171 		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
    172 			sblock.fs_minfree);
    173 		if (reply("SET TO DEFAULT") == 1) {
    174 			sblock.fs_minfree = 10;
    175 			sbdirty();
    176 		}
    177 	}
    178 	if (sblock.fs_interleave < 1 ||
    179 	    sblock.fs_interleave > sblock.fs_nsect) {
    180 		pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
    181 			sblock.fs_interleave);
    182 		sblock.fs_interleave = 1;
    183 		if (preen)
    184 			printf(" (FIXED)\n");
    185 		if (preen || reply("SET TO DEFAULT") == 1) {
    186 			sbdirty();
    187 			dirty(&asblk);
    188 		}
    189 	}
    190 	if (sblock.fs_npsect < sblock.fs_nsect ||
    191 	    sblock.fs_npsect > sblock.fs_nsect*2) {
    192 		pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
    193 			sblock.fs_npsect);
    194 		sblock.fs_npsect = sblock.fs_nsect;
    195 		if (preen)
    196 			printf(" (FIXED)\n");
    197 		if (preen || reply("SET TO DEFAULT") == 1) {
    198 			sbdirty();
    199 			dirty(&asblk);
    200 		}
    201 	}
    202 	if (sblock.fs_inodefmt >= FS_44INODEFMT) {
    203 		newinofmt = 1;
    204 	} else {
    205 		sblock.fs_qbmask = ~sblock.fs_bmask;
    206 		sblock.fs_qfmask = ~sblock.fs_fmask;
    207 		newinofmt = 0;
    208 	}
    209 	/*
    210 	 * Convert to new inode format.
    211 	 */
    212 	if (cvtlevel >= 2 && sblock.fs_inodefmt < FS_44INODEFMT) {
    213 		if (preen)
    214 			pwarn("CONVERTING TO NEW INODE FORMAT\n");
    215 		else if (!reply("CONVERT TO NEW INODE FORMAT"))
    216 			return(0);
    217 		doinglevel2++;
    218 		sblock.fs_inodefmt = FS_44INODEFMT;
    219 		sizepb = sblock.fs_bsize;
    220 		sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
    221 		for (i = 0; i < NIADDR; i++) {
    222 			sizepb *= NINDIR(&sblock);
    223 			sblock.fs_maxfilesize += sizepb;
    224 		}
    225 		sblock.fs_maxsymlinklen = MAXSYMLINKLEN;
    226 		sblock.fs_qbmask = ~sblock.fs_bmask;
    227 		sblock.fs_qfmask = ~sblock.fs_fmask;
    228 		sbdirty();
    229 		dirty(&asblk);
    230 	}
    231 	/*
    232 	 * Convert to new cylinder group format.
    233 	 */
    234 	if (cvtlevel >= 1 && sblock.fs_postblformat == FS_42POSTBLFMT) {
    235 		if (preen)
    236 			pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n");
    237 		else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT"))
    238 			return(0);
    239 		doinglevel1++;
    240 		sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
    241 		sblock.fs_nrpos = 8;
    242 		sblock.fs_postbloff =
    243 		    (char *)(&sblock.fs_opostbl[0][0]) -
    244 		    (char *)(&sblock.fs_firstfield);
    245 		sblock.fs_rotbloff = &sblock.fs_space[0] -
    246 		    (u_char *)(&sblock.fs_firstfield);
    247 		sblock.fs_cgsize =
    248 			fragroundup(&sblock, CGSIZE(&sblock));
    249 		sbdirty();
    250 		dirty(&asblk);
    251 	}
    252 	if (asblk.b_dirty && !bflag) {
    253 		memcpy(&altsblock, &sblock, (size_t)sblock.fs_sbsize);
    254 		flush(fswritefd, &asblk);
    255 	}
    256 	/*
    257 	 * read in the summary info.
    258 	 */
    259 	asked = 0;
    260 	for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
    261 		size = sblock.fs_cssize - i < sblock.fs_bsize ?
    262 		    sblock.fs_cssize - i : sblock.fs_bsize;
    263 		sblock.fs_csp[j] = (struct csum *)calloc(1, (unsigned)size);
    264 		if (bread(fsreadfd, (char *)sblock.fs_csp[j],
    265 		    fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
    266 		    size) != 0 && !asked) {
    267 			pfatal("BAD SUMMARY INFORMATION");
    268 			if (reply("CONTINUE") == 0)
    269 				errexit("");
    270 			asked++;
    271 		}
    272 	}
    273 	/*
    274 	 * allocate and initialize the necessary maps
    275 	 */
    276 	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
    277 	blockmap = calloc((unsigned)bmapsize, sizeof (char));
    278 	if (blockmap == NULL) {
    279 		printf("cannot alloc %u bytes for blockmap\n",
    280 		    (unsigned)bmapsize);
    281 		goto badsblabel;
    282 	}
    283 	statemap = calloc((unsigned)(maxino + 1), sizeof(char));
    284 	if (statemap == NULL) {
    285 		printf("cannot alloc %u bytes for statemap\n",
    286 		    (unsigned)(maxino + 1));
    287 		goto badsblabel;
    288 	}
    289 	typemap = calloc((unsigned)(maxino + 1), sizeof(char));
    290 	if (typemap == NULL) {
    291 		printf("cannot alloc %u bytes for typemap\n",
    292 		    (unsigned)(maxino + 1));
    293 		goto badsblabel;
    294 	}
    295 	lncntp = (int16_t *)calloc((unsigned)(maxino + 1), sizeof(int16_t));
    296 	if (lncntp == NULL) {
    297 		printf("cannot alloc %u bytes for lncntp\n",
    298 		    (unsigned)(maxino + 1) * sizeof(int16_t));
    299 		goto badsblabel;
    300 	}
    301 	numdirs = sblock.fs_cstotal.cs_ndir;
    302 	inplast = 0;
    303 	listmax = numdirs + 10;
    304 	inpsort = (struct inoinfo **)calloc((unsigned)listmax,
    305 	    sizeof(struct inoinfo *));
    306 	inphead = (struct inoinfo **)calloc((unsigned)numdirs,
    307 	    sizeof(struct inoinfo *));
    308 	if (inpsort == NULL || inphead == NULL) {
    309 		printf("cannot alloc %u bytes for inphead\n",
    310 		    (unsigned)numdirs * sizeof(struct inoinfo *));
    311 		goto badsblabel;
    312 	}
    313 	bufinit();
    314 	return (1);
    315 
    316 badsblabel:
    317 	ckfini(0);
    318 	return (0);
    319 }
    320 
    321 /*
    322  * Read in the super block and its summary info.
    323  */
    324 int
    325 readsb(listerr)
    326 	int listerr;
    327 {
    328 	daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
    329 
    330 	if (bread(fsreadfd, (char *)&sblock, super, (long)SBSIZE) != 0)
    331 		return (0);
    332 	sblk.b_bno = super;
    333 	sblk.b_size = SBSIZE;
    334 	/*
    335 	 * run a few consistency checks of the super block
    336 	 */
    337 	if (sblock.fs_magic != FS_MAGIC)
    338 		{ badsb(listerr, "MAGIC NUMBER WRONG"); return (0); }
    339 	if (sblock.fs_ncg < 1)
    340 		{ badsb(listerr, "NCG OUT OF RANGE"); return (0); }
    341 	if (sblock.fs_cpg < 1)
    342 		{ badsb(listerr, "CPG OUT OF RANGE"); return (0); }
    343 	if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl ||
    344 	    (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl)
    345 		{ badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); }
    346 	if (sblock.fs_sbsize > SBSIZE)
    347 		{ badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
    348 	/*
    349 	 * Compute block size that the filesystem is based on,
    350 	 * according to fsbtodb, and adjust superblock block number
    351 	 * so we can tell if this is an alternate later.
    352 	 */
    353 	super *= dev_bsize;
    354 	dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
    355 	sblk.b_bno = super / dev_bsize;
    356 	if (bflag) {
    357 		havesb = 1;
    358 		return (1);
    359 	}
    360 	/*
    361 	 * Set all possible fields that could differ, then do check
    362 	 * of whole super block against an alternate super block.
    363 	 * When an alternate super-block is specified this check is skipped.
    364 	 */
    365 	getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
    366 	if (asblk.b_errs)
    367 		return (0);
    368 	altsblock.fs_firstfield = sblock.fs_firstfield;
    369 	altsblock.fs_fscktime = sblock.fs_fscktime;
    370 	altsblock.fs_time = sblock.fs_time;
    371 	altsblock.fs_cstotal = sblock.fs_cstotal;
    372 	altsblock.fs_cgrotor = sblock.fs_cgrotor;
    373 	altsblock.fs_fmod = sblock.fs_fmod;
    374 	altsblock.fs_clean = sblock.fs_clean;
    375 	altsblock.fs_ronly = sblock.fs_ronly;
    376 	altsblock.fs_flags = sblock.fs_flags;
    377 	altsblock.fs_maxcontig = sblock.fs_maxcontig;
    378 	altsblock.fs_minfree = sblock.fs_minfree;
    379 	altsblock.fs_optim = sblock.fs_optim;
    380 	altsblock.fs_rotdelay = sblock.fs_rotdelay;
    381 	altsblock.fs_maxbpg = sblock.fs_maxbpg;
    382 	memcpy(altsblock.fs_csp, sblock.fs_csp,
    383 		sizeof sblock.fs_csp);
    384 	altsblock.fs_maxcluster = sblock.fs_maxcluster;
    385 	memcpy(altsblock.fs_fsmnt, sblock.fs_fsmnt,
    386 		sizeof sblock.fs_fsmnt);
    387 	memcpy(altsblock.fs_sparecon, sblock.fs_sparecon,
    388 		sizeof sblock.fs_sparecon);
    389 	/*
    390 	 * The following should not have to be copied.
    391 	 */
    392 	altsblock.fs_fsbtodb = sblock.fs_fsbtodb;
    393 	altsblock.fs_interleave = sblock.fs_interleave;
    394 	altsblock.fs_npsect = sblock.fs_npsect;
    395 	altsblock.fs_nrpos = sblock.fs_nrpos;
    396 	altsblock.fs_state = sblock.fs_state;
    397 	altsblock.fs_qbmask = sblock.fs_qbmask;
    398 	altsblock.fs_qfmask = sblock.fs_qfmask;
    399 	altsblock.fs_state = sblock.fs_state;
    400 	altsblock.fs_maxfilesize = sblock.fs_maxfilesize;
    401 	if (memcmp(&sblock, &altsblock, (int)sblock.fs_sbsize)) {
    402 		if (debug) {
    403 			long *nlp, *olp, *endlp;
    404 
    405 			printf("superblock mismatches\n");
    406 			nlp = (long *)&altsblock;
    407 			olp = (long *)&sblock;
    408 			endlp = olp + (sblock.fs_sbsize / sizeof *olp);
    409 			for ( ; olp < endlp; olp++, nlp++) {
    410 				if (*olp == *nlp)
    411 					continue;
    412 				printf("offset %d, original %d, alternate %d\n",
    413 				    olp - (long *)&sblock, *olp, *nlp);
    414 			}
    415 		}
    416 		badsb(listerr,
    417 		"VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
    418 		return (0);
    419 	}
    420 	havesb = 1;
    421 	return (1);
    422 }
    423 
    424 void
    425 badsb(listerr, s)
    426 	int listerr;
    427 	char *s;
    428 {
    429 
    430 	if (!listerr)
    431 		return;
    432 	if (preen)
    433 		printf("%s: ", cdevname);
    434 	pfatal("BAD SUPER BLOCK: %s\n", s);
    435 }
    436 
    437 /*
    438  * Calculate a prototype superblock based on information in the disk label.
    439  * When done the cgsblock macro can be calculated and the fs_ncg field
    440  * can be used. Do NOT attempt to use other macros without verifying that
    441  * their needed information is available!
    442  */
    443 int
    444 calcsb(dev, devfd, fs)
    445 	char *dev;
    446 	int devfd;
    447 	register struct fs *fs;
    448 {
    449 	register struct disklabel *lp;
    450 	register struct partition *pp;
    451 	register char *cp;
    452 	int i;
    453 
    454 	cp = strchr(dev, '\0') - 1;
    455 	if (cp == (char *)-1 || (*cp < 'a' || *cp > 'h') && !isdigit(*cp)) {
    456 		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
    457 		return (0);
    458 	}
    459 	lp = getdisklabel(dev, devfd);
    460 	if (isdigit(*cp))
    461 		pp = &lp->d_partitions[0];
    462 	else
    463 		pp = &lp->d_partitions[*cp - 'a'];
    464 	if (pp->p_fstype != FS_BSDFFS) {
    465 		pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
    466 			dev, pp->p_fstype < FSMAXTYPES ?
    467 			fstypenames[pp->p_fstype] : "unknown");
    468 		return (0);
    469 	}
    470 	memset(fs, 0, sizeof(struct fs));
    471 	fs->fs_fsize = pp->p_fsize;
    472 	fs->fs_frag = pp->p_frag;
    473 	fs->fs_cpg = pp->p_cpg;
    474 	fs->fs_size = pp->p_size;
    475 	fs->fs_ntrak = lp->d_ntracks;
    476 	fs->fs_nsect = lp->d_nsectors;
    477 	fs->fs_spc = lp->d_secpercyl;
    478 	fs->fs_nspf = fs->fs_fsize / lp->d_secsize;
    479 	fs->fs_sblkno = roundup(
    480 		howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize),
    481 		fs->fs_frag);
    482 	fs->fs_cgmask = 0xffffffff;
    483 	for (i = fs->fs_ntrak; i > 1; i >>= 1)
    484 		fs->fs_cgmask <<= 1;
    485 	if (!POWEROF2(fs->fs_ntrak))
    486 		fs->fs_cgmask <<= 1;
    487 	fs->fs_cgoffset = roundup(
    488 		howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag);
    489 	fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs);
    490 	fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg);
    491 	for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1)
    492 		fs->fs_fsbtodb++;
    493 	dev_bsize = lp->d_secsize;
    494 	return (1);
    495 }
    496 
    497 struct disklabel *
    498 getdisklabel(s, fd)
    499 	char *s;
    500 	int	fd;
    501 {
    502 	static struct disklabel lab;
    503 
    504 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
    505 		if (s == NULL)
    506 			return ((struct disklabel *)NULL);
    507 		pwarn("ioctl (GCINFO): %s\n", strerror(errno));
    508 		errexit("%s: can't read disk label\n", s);
    509 	}
    510 	return (&lab);
    511 }
    512