Home | History | Annotate | Line # | Download | only in fsck_lfs
setup.c revision 1.5
      1  1.5  perseant /* $NetBSD: setup.c,v 1.5 2000/05/23 01:48:55 perseant Exp $	 */
      2  1.1  perseant 
      3  1.1  perseant /*
      4  1.1  perseant  * Copyright (c) 1980, 1986, 1993
      5  1.1  perseant  *	The Regents of the University of California.  All rights reserved.
      6  1.1  perseant  *
      7  1.1  perseant  * Redistribution and use in source and binary forms, with or without
      8  1.1  perseant  * modification, are permitted provided that the following conditions
      9  1.1  perseant  * are met:
     10  1.1  perseant  * 1. Redistributions of source code must retain the above copyright
     11  1.1  perseant  *    notice, this list of conditions and the following disclaimer.
     12  1.1  perseant  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  perseant  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  perseant  *    documentation and/or other materials provided with the distribution.
     15  1.1  perseant  * 3. All advertising materials mentioning features or use of this software
     16  1.1  perseant  *    must display the following acknowledgement:
     17  1.1  perseant  *	This product includes software developed by the University of
     18  1.1  perseant  *	California, Berkeley and its contributors.
     19  1.1  perseant  * 4. Neither the name of the University nor the names of its contributors
     20  1.1  perseant  *    may be used to endorse or promote products derived from this software
     21  1.1  perseant  *    without specific prior written permission.
     22  1.1  perseant  *
     23  1.1  perseant  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1  perseant  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1  perseant  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1  perseant  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1  perseant  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1  perseant  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1  perseant  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1  perseant  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1  perseant  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1  perseant  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1  perseant  * SUCH DAMAGE.
     34  1.1  perseant  */
     35  1.1  perseant 
     36  1.1  perseant /* #define DKTYPENAMES */
     37  1.1  perseant #define FSTYPENAMES
     38  1.1  perseant #include <sys/param.h>
     39  1.1  perseant #include <sys/time.h>
     40  1.1  perseant #include <sys/stat.h>
     41  1.1  perseant #include <sys/ioctl.h>
     42  1.1  perseant #include <sys/disklabel.h>
     43  1.1  perseant #include <sys/file.h>
     44  1.1  perseant 
     45  1.1  perseant #include <ufs/ufs/dinode.h>
     46  1.5  perseant #include <sys/mount.h>		/* XXX ufs/lfs/lfs.h should include this for
     47  1.5  perseant 				 * us */
     48  1.1  perseant #include <ufs/lfs/lfs.h>
     49  1.1  perseant #include <ufs/lfs/lfs_extern.h>
     50  1.1  perseant 
     51  1.1  perseant #include <ctype.h>
     52  1.1  perseant #include <err.h>
     53  1.1  perseant #include <errno.h>
     54  1.1  perseant #include <stdio.h>
     55  1.1  perseant #include <stdlib.h>
     56  1.1  perseant #include <string.h>
     57  1.1  perseant 
     58  1.1  perseant #include "fsck.h"
     59  1.1  perseant #include "extern.h"
     60  1.1  perseant #include "fsutil.h"
     61  1.1  perseant 
     62  1.5  perseant struct bufarea  asblk;
     63  1.5  perseant daddr_t        *din_table;
     64  1.5  perseant SEGUSE         *seg_table;
     65  1.1  perseant #define altsblock (*asblk.b_un.b_fs)
     66  1.1  perseant #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
     67  1.1  perseant 
     68  1.5  perseant void            badsb(int, char *);
     69  1.5  perseant int             calcsb(const char *, int, struct lfs *);
     70  1.5  perseant static struct disklabel *getdisklabel(const char *, int);
     71  1.5  perseant static int      readsb(int);
     72  1.5  perseant int             lfs_maxino(void);
     73  1.1  perseant 
     74  1.5  perseant static daddr_t  try_verify(struct lfs *, struct lfs *);
     75  1.4  perseant 
     76  1.1  perseant #ifdef DKTYPENAMES
     77  1.5  perseant int             useless(void);
     78  1.1  perseant 
     79  1.1  perseant int
     80  1.1  perseant useless(void)
     81  1.1  perseant {
     82  1.5  perseant 	char          **foo = (char **)dktypenames;
     83  1.5  perseant 	char          **bar = (char **)fscknames;
     84  1.1  perseant 
     85  1.5  perseant 	return foo - bar;
     86  1.1  perseant }
     87  1.1  perseant #endif
     88  1.1  perseant 
     89  1.5  perseant static          daddr_t
     90  1.5  perseant try_verify(struct lfs * osb, struct lfs * nsb)
     91  1.4  perseant {
     92  1.5  perseant 	daddr_t         daddr;
     93  1.5  perseant 	SEGSUM         *sp;
     94  1.5  perseant 	char            summary[LFS_SUMMARY_SIZE];
     95  1.5  perseant 	int             bc, flag;
     96  1.5  perseant 
     97  1.4  perseant 	daddr = osb->lfs_offset;
     98  1.5  perseant 	while (daddr != nsb->lfs_offset) {
     99  1.4  perseant 		flag = 0;
    100  1.5  perseant oncemore:
    101  1.4  perseant 		/* Read in summary block */
    102  1.4  perseant 		bread(fsreadfd, summary, daddr, LFS_SUMMARY_SIZE);
    103  1.4  perseant 		sp = (SEGSUM *)summary;
    104  1.4  perseant 
    105  1.4  perseant 		/*
    106  1.4  perseant 		 * Could be a superblock instead of a segment summary.
    107  1.4  perseant 		 * XXX should use gseguse, but right now we need to do more
    108  1.4  perseant 		 * setup before we can...fix this
    109  1.4  perseant 		 */
    110  1.5  perseant 		if (sp->ss_magic != SS_MAGIC ||
    111  1.5  perseant 		  sp->ss_sumsum != cksum(&sp->ss_datasum, LFS_SUMMARY_SIZE -
    112  1.5  perseant 					 sizeof(sp->ss_sumsum))) {
    113  1.5  perseant 			if (flag == 0) {
    114  1.5  perseant 				daddr += LFS_SBPAD / dev_bsize;
    115  1.4  perseant 				goto oncemore;
    116  1.4  perseant 			}
    117  1.4  perseant 			return 0x0;
    118  1.4  perseant 		}
    119  1.4  perseant 		bc = check_summary(osb, sp, daddr);
    120  1.5  perseant 		if (bc == 0)
    121  1.4  perseant 			break;
    122  1.4  perseant 		daddr += (LFS_SUMMARY_SIZE + bc) / dev_bsize;
    123  1.5  perseant 		if (datosn(osb, daddr) != datosn(osb, daddr + (LFS_SUMMARY_SIZE + (1 << osb->lfs_bshift)) / dev_bsize)) {
    124  1.4  perseant 			daddr = ((SEGSUM *)summary)->ss_next;
    125  1.4  perseant 		}
    126  1.4  perseant 	}
    127  1.4  perseant 	return daddr;
    128  1.4  perseant }
    129  1.4  perseant 
    130  1.1  perseant int
    131  1.5  perseant setup(const char *dev)
    132  1.1  perseant {
    133  1.5  perseant 	long            bmapsize;
    134  1.1  perseant 	struct disklabel *lp;
    135  1.1  perseant #if 0
    136  1.5  perseant 	long            i;
    137  1.5  perseant 	off_t           sizepb;
    138  1.1  perseant #endif
    139  1.5  perseant 	struct stat     statb;
    140  1.5  perseant 	daddr_t         daddr;
    141  1.5  perseant 	struct lfs      proto;
    142  1.5  perseant 	int             doskipclean;
    143  1.5  perseant 	u_int64_t       maxfilesize;
    144  1.5  perseant 	struct lfs     *sb0, *sb1, *osb, *nsb;
    145  1.5  perseant 	struct dinode  *idinode;
    146  1.1  perseant 
    147  1.1  perseant 	havesb = 0;
    148  1.1  perseant 	fswritefd = -1;
    149  1.1  perseant 	doskipclean = skipclean;
    150  1.1  perseant 	if (stat(dev, &statb) < 0) {
    151  1.1  perseant 		printf("Can't stat %s: %s\n", dev, strerror(errno));
    152  1.1  perseant 		return (0);
    153  1.1  perseant 	}
    154  1.1  perseant 	if (!S_ISCHR(statb.st_mode)) {
    155  1.1  perseant 		pfatal("%s is not a character device", dev);
    156  1.1  perseant 		if (reply("CONTINUE") == 0)
    157  1.1  perseant 			return (0);
    158  1.1  perseant 	}
    159  1.1  perseant 	if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
    160  1.1  perseant 		printf("Can't open %s: %s\n", dev, strerror(errno));
    161  1.1  perseant 		return (0);
    162  1.1  perseant 	}
    163  1.1  perseant 	if (preen == 0)
    164  1.1  perseant 		printf("** %s", dev);
    165  1.1  perseant 	if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
    166  1.1  perseant 		fswritefd = -1;
    167  1.1  perseant 		if (preen)
    168  1.1  perseant 			pfatal("NO WRITE ACCESS");
    169  1.1  perseant 		printf(" (NO WRITE)");
    170  1.1  perseant 	}
    171  1.1  perseant 	if (preen == 0)
    172  1.1  perseant 		printf("\n");
    173  1.1  perseant 	fsmodified = 0;
    174  1.1  perseant 	lfdir = 0;
    175  1.1  perseant 	initbarea(&sblk);
    176  1.1  perseant 	initbarea(&asblk);
    177  1.1  perseant 	sblk.b_un.b_buf = malloc(LFS_SBPAD);
    178  1.1  perseant 	asblk.b_un.b_buf = malloc(LFS_SBPAD);
    179  1.1  perseant 	if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
    180  1.1  perseant 		errexit("cannot allocate space for superblock\n");
    181  1.5  perseant 	if ((lp = getdisklabel((char *) NULL, fsreadfd)) != NULL)
    182  1.1  perseant 		dev_bsize = secsize = lp->d_secsize;
    183  1.1  perseant 	else
    184  1.1  perseant 		dev_bsize = secsize = DEV_BSIZE;
    185  1.1  perseant 
    186  1.1  perseant 	/*
    187  1.1  perseant 	 * Read in the superblock, looking for alternates if necessary
    188  1.1  perseant 	 */
    189  1.1  perseant 	if (readsb(1) == 0) {
    190  1.1  perseant 		if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
    191  1.5  perseant 			return (0);
    192  1.1  perseant 		if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
    193  1.1  perseant 			return (0);
    194  1.5  perseant #if 0				/* XXX find the LFS way to do this */
    195  1.1  perseant 		for (cg = 0; cg < proto.lfs_ncg; cg++) {
    196  1.1  perseant 			bflag = fsbtodb(&proto, cgsblock(&proto, cg));
    197  1.1  perseant 			if (readsb(0) != 0)
    198  1.1  perseant 				break;
    199  1.1  perseant 		}
    200  1.1  perseant 		if (cg >= proto.lfs_ncg) {
    201  1.1  perseant 			printf("%s %s\n%s %s\n%s %s\n",
    202  1.5  perseant 			       "SEARCH FOR ALTERNATE SUPER-BLOCK",
    203  1.5  perseant 			       "FAILED. YOU MUST USE THE",
    204  1.5  perseant 			       "-b OPTION TO FSCK_FFS TO SPECIFY THE",
    205  1.5  perseant 			       "LOCATION OF AN ALTERNATE",
    206  1.5  perseant 			       "SUPER-BLOCK TO SUPPLY NEEDED",
    207  1.5  perseant 			       "INFORMATION; SEE fsck_ffs(8).");
    208  1.5  perseant 			return (0);
    209  1.1  perseant 		}
    210  1.1  perseant #else
    211  1.1  perseant 		pwarn("XXX Can't look for alternate superblocks yet\n");
    212  1.5  perseant 		return (0);
    213  1.1  perseant #endif
    214  1.1  perseant 		doskipclean = 0;
    215  1.1  perseant 		pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
    216  1.1  perseant 	}
    217  1.4  perseant 	bufinit();
    218  1.4  perseant 
    219  1.5  perseant 	if (bflag == 0) {
    220  1.1  perseant 		/*
    221  1.1  perseant 		 * Even if that superblock read in properly, it may not
    222  1.1  perseant 		 * be guaranteed to point to a complete checkpoint.
    223  1.1  perseant 		 * Read in the second superblock too, and take whichever
    224  1.1  perseant 		 * of the two is *less* recent. --ks
    225  1.1  perseant 		 */
    226  1.1  perseant 		sb0 = malloc(sizeof(*sb0));
    227  1.4  perseant 		sb1 = malloc(sizeof(*sb1));
    228  1.5  perseant 		memcpy(sb0, &sblock, sizeof(*sb0));
    229  1.4  perseant 		bflag = sblock.lfs_sboffs[1];
    230  1.5  perseant 		if (readsb(1) == 0) {
    231  1.5  perseant 			pwarn("COULDN'T READ ALT SUPERBLOCK AT BLK %d", bflag);
    232  1.4  perseant 			if (reply("ASSUME PRIMARY SUPERBLOCK IS GOOD") == 0) {
    233  1.4  perseant 				return (0);
    234  1.5  perseant 			} else {/* use primary as good */
    235  1.5  perseant 				memcpy(&sblock, sb0, sizeof(*sb0));	/* XXX cheating? */
    236  1.4  perseant 			}
    237  1.4  perseant 		} else {
    238  1.5  perseant 			memcpy(sb1, &sblock, sizeof(*sb1));
    239  1.5  perseant 			if (debug)
    240  1.5  perseant 				pwarn("sb0 %d, sb1 %d\n", sb0->lfs_tstamp, sblock.lfs_tstamp);
    241  1.4  perseant 			/*
    242  1.4  perseant 			 * Verify the checkpoint of the newer superblock,
    243  1.4  perseant 			 * if the timestamp of the two superblocks is
    244  1.4  perseant 			 * different.  XXX use lfs_offset instead, discover
    245  1.4  perseant 			 * how to quickly discover "newness" based on that.
    246  1.4  perseant 			 */
    247  1.5  perseant 			if (sb0->lfs_tstamp != sb1->lfs_tstamp) {
    248  1.5  perseant 				if (sb0->lfs_tstamp > sb1->lfs_tstamp) {
    249  1.4  perseant 					osb = sb1;
    250  1.4  perseant 					nsb = sb0;
    251  1.4  perseant 				} else {
    252  1.4  perseant 					osb = sb0;
    253  1.4  perseant 					nsb = sb1;
    254  1.1  perseant 				}
    255  1.4  perseant 				daddr = try_verify(osb, nsb);
    256  1.4  perseant 
    257  1.4  perseant 				if (debug)
    258  1.4  perseant 					printf("done.\n");
    259  1.4  perseant 				if (daddr == nsb->lfs_offset) {
    260  1.4  perseant 					pwarn("Checkpoint verified, recovered %d seconds of data\n", nsb->lfs_tstamp - osb->lfs_tstamp);
    261  1.5  perseant 					memcpy(&sblock, nsb, sizeof(*nsb));
    262  1.4  perseant 					sbdirty();
    263  1.1  perseant 				} else {
    264  1.4  perseant 					pwarn("Checkpoint invalid, lost %d seconds of data\n", nsb->lfs_tstamp - osb->lfs_tstamp);
    265  1.5  perseant 					memcpy(&sblock, osb, sizeof(*osb));
    266  1.1  perseant 				}
    267  1.1  perseant 			}
    268  1.4  perseant 		}
    269  1.1  perseant 		free(sb0);
    270  1.4  perseant 		free(sb1);
    271  1.1  perseant 	}
    272  1.5  perseant 	if (debug) {
    273  1.5  perseant 		printf("dev_bsize = %lu\n", dev_bsize);
    274  1.5  perseant 		printf("lfs_bsize = %lu\n", (unsigned long)sblock.lfs_bsize);
    275  1.5  perseant 		printf("lfs_fsize = %lu\n", (unsigned long)sblock.lfs_fsize);
    276  1.5  perseant 		printf("lfs_frag  = %lu\n", (unsigned long)sblock.lfs_frag);
    277  1.5  perseant 		printf("INOPB(fs) = %lu\n", (unsigned long) INOPB(&sblock));
    278  1.5  perseant 		/* printf("fsbtodb(fs,1) = %lu\n",fsbtodb(&sblock,1)); */
    279  1.1  perseant 	}
    280  1.5  perseant #if 0				/* FFS-specific fs-clean check */
    281  1.1  perseant 	if (debug)
    282  1.1  perseant 		printf("clean = %d\n", sblock.lfs_clean);
    283  1.1  perseant 	if (sblock.lfs_clean & FS_ISCLEAN) {
    284  1.1  perseant 		if (doskipclean) {
    285  1.1  perseant 			pwarn("%sile system is clean; not checking\n",
    286  1.5  perseant 			      preen ? "f" : "** F");
    287  1.1  perseant 			return (-1);
    288  1.1  perseant 		}
    289  1.1  perseant 		if (!preen)
    290  1.1  perseant 			pwarn("** File system is already clean\n");
    291  1.1  perseant 	}
    292  1.1  perseant 	maxino = sblock.lfs_ncg * sblock.lfs_ipg;
    293  1.1  perseant #else
    294  1.1  perseant #if 0
    295  1.5  perseant 	/* XXX - count the number of inodes here */
    296  1.5  perseant 	maxino = sblock.lfs_nfiles + 2;
    297  1.1  perseant #else
    298  1.5  perseant 	initbarea(&iblk);
    299  1.5  perseant 	iblk.b_un.b_buf = malloc(sblock.lfs_bsize);
    300  1.5  perseant 	if (bread(fsreadfd, (char *)iblk.b_un.b_buf,
    301  1.5  perseant 		  sblock.lfs_idaddr,
    302  1.5  perseant 		  (long)sblock.lfs_bsize) != 0) {
    303  1.5  perseant 		printf("Couldn't read disk block %d\n", sblock.lfs_idaddr);
    304  1.5  perseant 		exit(1);
    305  1.5  perseant 	}
    306  1.5  perseant 	idinode = lfs_difind(&sblock, sblock.lfs_ifile, &ifblock);
    307  1.4  perseant 	maxino = ((idinode->di_size
    308  1.5  perseant 	    - (sblock.lfs_cleansz + sblock.lfs_segtabsz) * sblock.lfs_bsize)
    309  1.5  perseant 		  / sblock.lfs_bsize) * sblock.lfs_ifpb - 1;
    310  1.1  perseant #endif
    311  1.1  perseant 	if (debug)
    312  1.5  perseant 		printf("maxino=%d\n", maxino);
    313  1.5  perseant 	din_table = (daddr_t *)malloc(maxino * sizeof(*din_table));
    314  1.5  perseant 	memset(din_table, 0, maxino * sizeof(*din_table));
    315  1.5  perseant 	seg_table = (SEGUSE *)malloc(sblock.lfs_nseg * sizeof(SEGUSE));
    316  1.5  perseant 	memset(seg_table, 0, sblock.lfs_nseg * sizeof(SEGUSE));
    317  1.1  perseant #endif
    318  1.1  perseant 	maxfsblock = sblock.lfs_size * (sblock.lfs_bsize / dev_bsize);
    319  1.1  perseant #if 0
    320  1.1  perseant 	sizepb = sblock.lfs_bsize;
    321  1.1  perseant 	maxfilesize = sblock.lfs_bsize * NDADDR - 1;
    322  1.1  perseant 	for (i = 0; i < NIADDR; i++) {
    323  1.1  perseant 		sizepb *= NINDIR(&sblock);
    324  1.1  perseant 		maxfilesize += sizepb;
    325  1.1  perseant 	}
    326  1.5  perseant 	maxfilesize++;		/* XXX */
    327  1.5  perseant #else				/* LFS way */
    328  1.5  perseant 	{
    329  1.5  perseant 		u_quad_t        maxtable[] = {
    330  1.5  perseant 			 /* 1 */ -1,
    331  1.5  perseant 			 /* 2 */ -1,
    332  1.5  perseant 			 /* 4 */ -1,
    333  1.5  perseant 			 /* 8 */ -1,
    334  1.5  perseant 			 /* 16 */ -1,
    335  1.5  perseant 			 /* 32 */ -1,
    336  1.5  perseant 			 /* 64 */ -1,
    337  1.5  perseant 			 /* 128 */ -1,
    338  1.5  perseant 			 /* 256 */ -1,
    339  1.5  perseant 			 /* 512 */ NDADDR + 128 + 128 * 128 + 128 * 128 * 128,
    340  1.5  perseant 			 /* 1024 */ NDADDR + 256 + 256 * 256 + 256 * 256 * 256,
    341  1.5  perseant 			 /* 2048 */ NDADDR + 512 + 512 * 512 + 512 * 512 * 512,
    342  1.5  perseant 			 /* 4096 */ NDADDR + 1024 + 1024 * 1024 + 1024 * 1024 * 1024,
    343  1.5  perseant 			 /* 8192 */ 1 << 31,
    344  1.5  perseant 			 /* 16 K */ 1 << 31,
    345  1.5  perseant 			 /* 32 K */ 1 << 31,
    346  1.5  perseant 		};
    347  1.5  perseant 		maxfilesize = maxtable[sblock.lfs_bshift] << sblock.lfs_bshift;
    348  1.5  perseant 	}
    349  1.1  perseant #endif
    350  1.1  perseant 	if ((sblock.lfs_minfree < 0 || sblock.lfs_minfree > 99)) {
    351  1.1  perseant 		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
    352  1.5  perseant 		       sblock.lfs_minfree);
    353  1.1  perseant 		if (reply("SET TO DEFAULT") == 1) {
    354  1.1  perseant 			sblock.lfs_minfree = 10;
    355  1.1  perseant 			sbdirty();
    356  1.1  perseant 		}
    357  1.1  perseant 	}
    358  1.5  perseant 	/* XXX used to be ~(sblock.lfs_bsize - 1) */
    359  1.1  perseant 	if (sblock.lfs_bmask != sblock.lfs_bsize - 1) {
    360  1.1  perseant 		pwarn("INCORRECT BMASK=%x IN SUPERBLOCK (should be %x)",
    361  1.5  perseant 		      (unsigned int)sblock.lfs_bmask,
    362  1.5  perseant 		      (unsigned int)sblock.lfs_bsize - 1);
    363  1.1  perseant 		sblock.lfs_bmask = sblock.lfs_bsize - 1;
    364  1.1  perseant 		if (preen)
    365  1.1  perseant 			printf(" (FIXED)\n");
    366  1.1  perseant 		if (preen || reply("FIX") == 1) {
    367  1.1  perseant 			sbdirty();
    368  1.1  perseant 			dirty(&asblk);
    369  1.1  perseant 		}
    370  1.1  perseant 	}
    371  1.5  perseant #if 0				/* FFS-specific checks */
    372  1.1  perseant 	if (sblock.lfs_fmask != ~(sblock.lfs_fsize - 1)) {
    373  1.1  perseant 		pwarn("INCORRECT FMASK=%x IN SUPERBLOCK",
    374  1.5  perseant 		      sblock.lfs_fmask);
    375  1.1  perseant 		sblock.lfs_fmask = ~(sblock.lfs_fsize - 1);
    376  1.1  perseant 		if (preen)
    377  1.1  perseant 			printf(" (FIXED)\n");
    378  1.1  perseant 		if (preen || reply("FIX") == 1) {
    379  1.1  perseant 			sbdirty();
    380  1.1  perseant 			dirty(&asblk);
    381  1.1  perseant 		}
    382  1.1  perseant 	}
    383  1.1  perseant #endif
    384  1.5  perseant 	if (sblock.lfs_maxfilesize != maxfilesize) {
    385  1.5  perseant 		pwarn("INCORRECT MAXFILESIZE=%qu IN SUPERBLOCK (should be %qu)",
    386  1.5  perseant 		      (unsigned long long)sblock.lfs_maxfilesize,
    387  1.5  perseant 		      (unsigned long long)maxfilesize);
    388  1.5  perseant 		sblock.lfs_maxfilesize = maxfilesize;
    389  1.5  perseant 		if (preen)
    390  1.5  perseant 			printf(" (FIXED)\n");
    391  1.5  perseant 		if (preen || reply("FIX") == 1) {
    392  1.5  perseant 			sbdirty();
    393  1.5  perseant 			dirty(&asblk);
    394  1.1  perseant 		}
    395  1.5  perseant 	}
    396  1.5  perseant 	if (sblock.lfs_maxsymlinklen != MAXSYMLINKLEN) {
    397  1.5  perseant 		pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK",
    398  1.5  perseant 		      sblock.lfs_maxsymlinklen);
    399  1.5  perseant 		sblock.lfs_maxsymlinklen = MAXSYMLINKLEN;
    400  1.5  perseant 		if (preen)
    401  1.5  perseant 			printf(" (FIXED)\n");
    402  1.5  perseant 		if (preen || reply("FIX") == 1) {
    403  1.5  perseant 			sbdirty();
    404  1.5  perseant 			dirty(&asblk);
    405  1.1  perseant 		}
    406  1.5  perseant 	}
    407  1.5  perseant 	newinofmt = 1;
    408  1.1  perseant 	/*
    409  1.1  perseant 	 * allocate and initialize the necessary maps
    410  1.1  perseant 	 */
    411  1.1  perseant #ifndef VERBOSE_BLOCKMAP
    412  1.1  perseant 	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
    413  1.5  perseant 	blockmap = malloc((unsigned)bmapsize * sizeof(char));
    414  1.5  perseant 	bzero(blockmap, bmapsize * sizeof(char));
    415  1.1  perseant #else
    416  1.5  perseant 	bmapsize = maxfsblock * sizeof(ino_t);
    417  1.5  perseant 	blockmap = (ino_t *)malloc(maxfsblock * sizeof(ino_t));
    418  1.5  perseant 	bzero(blockmap, maxfsblock * sizeof(ino_t));
    419  1.1  perseant #endif
    420  1.1  perseant 	if (blockmap == NULL) {
    421  1.1  perseant 		printf("cannot alloc %u bytes for blockmap\n",
    422  1.5  perseant 		       (unsigned)bmapsize);
    423  1.1  perseant 		goto badsblabel;
    424  1.1  perseant 	}
    425  1.1  perseant 	statemap = calloc((unsigned)(maxino + 1), sizeof(char));
    426  1.1  perseant 	if (statemap == NULL) {
    427  1.1  perseant 		printf("cannot alloc %u bytes for statemap\n",
    428  1.5  perseant 		       (unsigned)(maxino + 1));
    429  1.1  perseant 		goto badsblabel;
    430  1.1  perseant 	}
    431  1.1  perseant 	typemap = calloc((unsigned)(maxino + 1), sizeof(char));
    432  1.1  perseant 	if (typemap == NULL) {
    433  1.1  perseant 		printf("cannot alloc %u bytes for typemap\n",
    434  1.5  perseant 		       (unsigned)(maxino + 1));
    435  1.1  perseant 		goto badsblabel;
    436  1.1  perseant 	}
    437  1.1  perseant 	lncntp = (int16_t *)calloc((unsigned)(maxino + 1), sizeof(int16_t));
    438  1.1  perseant 	if (lncntp == NULL) {
    439  1.5  perseant 		printf("cannot alloc %lu bytes for lncntp\n",
    440  1.5  perseant 		       (unsigned long)(maxino + 1) * sizeof(int16_t));
    441  1.1  perseant 		goto badsblabel;
    442  1.1  perseant 	}
    443  1.1  perseant 	return (1);
    444  1.1  perseant 
    445  1.1  perseant badsblabel:
    446  1.1  perseant 	ckfini(0);
    447  1.1  perseant 	return (0);
    448  1.1  perseant }
    449  1.1  perseant 
    450  1.1  perseant /*
    451  1.1  perseant  * Read in the LFS super block and its summary info.
    452  1.1  perseant  */
    453  1.1  perseant static int
    454  1.5  perseant readsb(int listerr)
    455  1.1  perseant {
    456  1.5  perseant 	daddr_t         super = bflag ? bflag : LFS_LABELPAD / dev_bsize;
    457  1.5  perseant 	u_int32_t       checksum;
    458  1.1  perseant 
    459  1.5  perseant 	if (bread(fsreadfd, (char *) &sblock, super, (long) LFS_SBPAD) != 0)
    460  1.1  perseant 		return (0);
    461  1.1  perseant 
    462  1.1  perseant 	sblk.b_bno = super;
    463  1.1  perseant 	sblk.b_size = LFS_SBPAD;
    464  1.1  perseant 	/*
    465  1.1  perseant 	 * run a few consistency checks of the super block
    466  1.1  perseant 	 */
    467  1.5  perseant 	if (sblock.lfs_magic != LFS_MAGIC) {
    468  1.5  perseant 		badsb(listerr, "MAGIC NUMBER WRONG");
    469  1.5  perseant 		return (0);
    470  1.5  perseant 	}
    471  1.5  perseant 	/* checksum */
    472  1.5  perseant 	checksum = lfs_sb_cksum(&(sblock.lfs_dlfs));
    473  1.5  perseant 	if (sblock.lfs_cksum != checksum) {
    474  1.5  perseant 		printf("Superblock checksum (%lu)does not match computed checksum %lu\n",
    475  1.5  perseant 		(unsigned long)sblock.lfs_cksum, (unsigned long) checksum);
    476  1.5  perseant 	}
    477  1.5  perseant #if 0				/* XXX - replace these checks with
    478  1.5  perseant 				 * appropriate LFS sanity checks */
    479  1.5  perseant 	if (sblock.lfs_ncg < 1) {
    480  1.5  perseant 		badsb(listerr, "NCG OUT OF RANGE");
    481  1.5  perseant 		return (0);
    482  1.5  perseant 	}
    483  1.5  perseant 	if (sblock.lfs_cpg < 1) {
    484  1.5  perseant 		badsb(listerr, "CPG OUT OF RANGE");
    485  1.5  perseant 		return (0);
    486  1.5  perseant 	}
    487  1.1  perseant 	if (sblock.lfs_ncg * sblock.lfs_cpg < sblock.lfs_ncyl ||
    488  1.5  perseant 	    (sblock.lfs_ncg - 1) * sblock.lfs_cpg >= sblock.lfs_ncyl) {
    489  1.5  perseant 		badsb(listerr, "NCYL LESS THAN NCG*CPG");
    490  1.5  perseant 		return (0);
    491  1.5  perseant 	}
    492  1.5  perseant 	if (sblock.lfs_sbsize > SBSIZE) {
    493  1.5  perseant 		badsb(listerr, "SIZE PREPOSTEROUSLY LARGE");
    494  1.5  perseant 		return (0);
    495  1.5  perseant 	}
    496  1.1  perseant #endif
    497  1.1  perseant 	/*
    498  1.1  perseant 	 * Compute block size that the filesystem is based on,
    499  1.1  perseant 	 * according to fsbtodb, and adjust superblock block number
    500  1.1  perseant 	 * so we can tell if this is an alternate later.
    501  1.1  perseant 	 */
    502  1.1  perseant 	super *= dev_bsize;
    503  1.1  perseant #if 0
    504  1.1  perseant 	dev_bsize = sblock.lfs_bsize / fsbtodb(&sblock, 1);
    505  1.1  perseant #endif
    506  1.1  perseant 	sblk.b_bno = super / dev_bsize;
    507  1.1  perseant 	if (bflag) {
    508  1.1  perseant 		havesb = 1;
    509  1.1  perseant 		return (1);
    510  1.1  perseant 	}
    511  1.5  perseant #if 0				/* XXX - for now skip the alt. superblock
    512  1.5  perseant 				 * test as well */
    513  1.1  perseant 	/*
    514  1.1  perseant 	 * Set all possible fields that could differ, then do check
    515  1.1  perseant 	 * of whole super block against an alternate super block.
    516  1.1  perseant 	 * When an alternate super-block is specified this check is skipped.
    517  1.1  perseant 	 */
    518  1.1  perseant 	getblk(&asblk, cgsblock(&sblock, sblock.lfs_ncg - 1), sblock.lfs_sbsize);
    519  1.1  perseant 	if (asblk.b_errs)
    520  1.1  perseant 		return (0);
    521  1.1  perseant 	altsblock.lfs_firstfield = sblock.lfs_firstfield;
    522  1.1  perseant 	altsblock.lfs_fscktime = sblock.lfs_fscktime;
    523  1.1  perseant 	altsblock.lfs_time = sblock.lfs_time;
    524  1.1  perseant 	altsblock.lfs_cstotal = sblock.lfs_cstotal;
    525  1.1  perseant 	altsblock.lfs_cgrotor = sblock.lfs_cgrotor;
    526  1.1  perseant 	altsblock.lfs_fmod = sblock.lfs_fmod;
    527  1.1  perseant 	altsblock.lfs_clean = sblock.lfs_clean;
    528  1.1  perseant 	altsblock.lfs_ronly = sblock.lfs_ronly;
    529  1.1  perseant 	altsblock.lfs_flags = sblock.lfs_flags;
    530  1.1  perseant 	altsblock.lfs_maxcontig = sblock.lfs_maxcontig;
    531  1.1  perseant 	altsblock.lfs_minfree = sblock.lfs_minfree;
    532  1.1  perseant 	altsblock.lfs_optim = sblock.lfs_optim;
    533  1.1  perseant 	altsblock.lfs_rotdelay = sblock.lfs_rotdelay;
    534  1.1  perseant 	altsblock.lfs_maxbpg = sblock.lfs_maxbpg;
    535  1.1  perseant 	memcpy(altsblock.lfs_csp, sblock.lfs_csp,
    536  1.5  perseant 	       sizeof sblock.lfs_csp);
    537  1.1  perseant 	altsblock.lfs_maxcluster = sblock.lfs_maxcluster;
    538  1.1  perseant 	memcpy(altsblock.lfs_fsmnt, sblock.lfs_fsmnt,
    539  1.5  perseant 	       sizeof sblock.lfs_fsmnt);
    540  1.1  perseant 	memcpy(altsblock.lfs_sparecon, sblock.lfs_sparecon,
    541  1.5  perseant 	       sizeof sblock.lfs_sparecon);
    542  1.1  perseant 	/*
    543  1.1  perseant 	 * The following should not have to be copied.
    544  1.1  perseant 	 */
    545  1.1  perseant 	altsblock.lfs_fsbtodb = sblock.lfs_fsbtodb;
    546  1.1  perseant 	altsblock.lfs_interleave = sblock.lfs_interleave;
    547  1.1  perseant 	altsblock.lfs_npsect = sblock.lfs_npsect;
    548  1.1  perseant 	altsblock.lfs_nrpos = sblock.lfs_nrpos;
    549  1.1  perseant 	altsblock.lfs_state = sblock.lfs_state;
    550  1.1  perseant 	altsblock.lfs_qbmask = sblock.lfs_qbmask;
    551  1.1  perseant 	altsblock.lfs_qfmask = sblock.lfs_qfmask;
    552  1.1  perseant 	altsblock.lfs_state = sblock.lfs_state;
    553  1.1  perseant 	altsblock.lfs_maxfilesize = sblock.lfs_maxfilesize;
    554  1.1  perseant 	if (memcmp(&sblock, &altsblock, (int)sblock.lfs_sbsize)) {
    555  1.1  perseant 		if (debug) {
    556  1.5  perseant 			long           *nlp, *olp, *endlp;
    557  1.1  perseant 
    558  1.1  perseant 			printf("superblock mismatches\n");
    559  1.5  perseant 			nlp = (long *) &altsblock;
    560  1.5  perseant 			olp = (long *) &sblock;
    561  1.1  perseant 			endlp = olp + (sblock.lfs_sbsize / sizeof *olp);
    562  1.5  perseant 			for (; olp < endlp; olp++, nlp++) {
    563  1.1  perseant 				if (*olp == *nlp)
    564  1.1  perseant 					continue;
    565  1.1  perseant 				printf("offset %d, original %ld, alternate %ld\n",
    566  1.5  perseant 				       olp - (long *) &sblock, *olp, *nlp);
    567  1.1  perseant 			}
    568  1.1  perseant 		}
    569  1.1  perseant 		badsb(listerr,
    570  1.5  perseant 		      "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
    571  1.1  perseant 		return (0);
    572  1.1  perseant 	}
    573  1.1  perseant #endif
    574  1.1  perseant 	havesb = 1;
    575  1.1  perseant 	return (1);
    576  1.1  perseant }
    577  1.1  perseant 
    578  1.1  perseant void
    579  1.5  perseant badsb(int listerr, char *s)
    580  1.1  perseant {
    581  1.1  perseant 
    582  1.1  perseant 	if (!listerr)
    583  1.1  perseant 		return;
    584  1.1  perseant 	if (preen)
    585  1.1  perseant 		printf("%s: ", cdevname());
    586  1.1  perseant 	pfatal("BAD SUPER BLOCK: %s\n", s);
    587  1.1  perseant }
    588  1.1  perseant 
    589  1.1  perseant /*
    590  1.1  perseant  * Calculate a prototype superblock based on information in the disk label.
    591  1.1  perseant  * When done the cgsblock macro can be calculated and the fs_ncg field
    592  1.1  perseant  * can be used. Do NOT attempt to use other macros without verifying that
    593  1.1  perseant  * their needed information is available!
    594  1.1  perseant  */
    595  1.1  perseant int
    596  1.5  perseant calcsb(const char *dev, int devfd, struct lfs * fs)
    597  1.1  perseant {
    598  1.1  perseant 	register struct disklabel *lp;
    599  1.1  perseant 	register struct partition *pp;
    600  1.5  perseant 	register char  *cp;
    601  1.5  perseant 	int             i;
    602  1.1  perseant 
    603  1.1  perseant 	cp = strchr(dev, '\0') - 1;
    604  1.5  perseant 	if ((cp == (char *) -1 || (*cp < 'a' || *cp > 'h')) && !isdigit(*cp)) {
    605  1.1  perseant 		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
    606  1.1  perseant 		return (0);
    607  1.1  perseant 	}
    608  1.1  perseant 	lp = getdisklabel(dev, devfd);
    609  1.5  perseant 	if (lp == NULL) {
    610  1.4  perseant 		dev_bsize = DEV_BSIZE;
    611  1.4  perseant 	} else {
    612  1.4  perseant 		if (isdigit(*cp))
    613  1.4  perseant 			pp = &lp->d_partitions[0];
    614  1.4  perseant 		else
    615  1.4  perseant 			pp = &lp->d_partitions[*cp - 'a'];
    616  1.4  perseant 		if (pp->p_fstype != FS_BSDLFS) {
    617  1.4  perseant 			pfatal("%s: NOT LABELED AS AN LFS FILE SYSTEM (%s)\n",
    618  1.5  perseant 			       dev, pp->p_fstype < FSMAXTYPES ?
    619  1.5  perseant 			       fstypenames[pp->p_fstype] : "unknown");
    620  1.4  perseant 			return (0);
    621  1.4  perseant 		}
    622  1.4  perseant 		memset(fs, 0, sizeof(struct lfs));
    623  1.4  perseant 		fs->lfs_fsize = pp->p_fsize;
    624  1.4  perseant 		fs->lfs_frag = pp->p_frag;
    625  1.4  perseant 		fs->lfs_size = pp->p_size;
    626  1.4  perseant 		fs->lfs_nspf = fs->lfs_fsize / lp->d_secsize;
    627  1.4  perseant 		dev_bsize = lp->d_secsize;
    628  1.4  perseant 		for (fs->lfs_fsbtodb = 0, i = fs->lfs_nspf; i > 1; i >>= 1)
    629  1.4  perseant 			fs->lfs_fsbtodb++;
    630  1.1  perseant 	}
    631  1.1  perseant 	return (1);
    632  1.1  perseant }
    633  1.1  perseant 
    634  1.1  perseant static struct disklabel *
    635  1.5  perseant getdisklabel(const char *s, int fd)
    636  1.1  perseant {
    637  1.1  perseant 	static struct disklabel lab;
    638  1.1  perseant 
    639  1.5  perseant 	if (ioctl(fd, DIOCGDINFO, (char *) &lab) < 0) {
    640  1.1  perseant 		if (s == NULL)
    641  1.5  perseant 			return ((struct disklabel *) NULL);
    642  1.1  perseant 		pwarn("ioctl (GCINFO): %s\n", strerror(errno));
    643  1.4  perseant #if 0
    644  1.1  perseant 		errexit("%s: can't read disk label\n", s);
    645  1.4  perseant #else
    646  1.4  perseant 		return NULL;
    647  1.4  perseant #endif
    648  1.1  perseant 	}
    649  1.1  perseant 	return (&lab);
    650  1.1  perseant }
    651