Home | History | Annotate | Line # | Download | only in fsck_lfs
setup.c revision 1.29
      1  1.29  perseant /* $NetBSD: setup.c,v 1.29 2006/07/18 23:37:13 perseant Exp $ */
      2   1.1  perseant 
      3  1.13  perseant /*-
      4  1.13  perseant  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5  1.13  perseant  * All rights reserved.
      6  1.13  perseant  *
      7  1.13  perseant  * This code is derived from software contributed to The NetBSD Foundation
      8  1.13  perseant  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      9  1.13  perseant  *
     10  1.13  perseant  * Redistribution and use in source and binary forms, with or without
     11  1.13  perseant  * modification, are permitted provided that the following conditions
     12  1.13  perseant  * are met:
     13  1.13  perseant  * 1. Redistributions of source code must retain the above copyright
     14  1.13  perseant  *    notice, this list of conditions and the following disclaimer.
     15  1.13  perseant  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.13  perseant  *    notice, this list of conditions and the following disclaimer in the
     17  1.13  perseant  *    documentation and/or other materials provided with the distribution.
     18  1.13  perseant  * 3. All advertising materials mentioning features or use of this software
     19  1.13  perseant  *    must display the following acknowledgement:
     20  1.13  perseant  *	This product includes software developed by the NetBSD
     21  1.13  perseant  *	Foundation, Inc. and its contributors.
     22  1.13  perseant  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.13  perseant  *    contributors may be used to endorse or promote products derived
     24  1.13  perseant  *    from this software without specific prior written permission.
     25  1.13  perseant  *
     26  1.13  perseant  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.13  perseant  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.13  perseant  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.13  perseant  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.13  perseant  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.13  perseant  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.13  perseant  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.13  perseant  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.13  perseant  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.13  perseant  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.13  perseant  * POSSIBILITY OF SUCH DAMAGE.
     37  1.13  perseant  */
     38   1.1  perseant /*
     39   1.1  perseant  * Copyright (c) 1980, 1986, 1993
     40   1.1  perseant  *	The Regents of the University of California.  All rights reserved.
     41   1.1  perseant  *
     42   1.1  perseant  * Redistribution and use in source and binary forms, with or without
     43   1.1  perseant  * modification, are permitted provided that the following conditions
     44   1.1  perseant  * are met:
     45   1.1  perseant  * 1. Redistributions of source code must retain the above copyright
     46   1.1  perseant  *    notice, this list of conditions and the following disclaimer.
     47   1.1  perseant  * 2. Redistributions in binary form must reproduce the above copyright
     48   1.1  perseant  *    notice, this list of conditions and the following disclaimer in the
     49   1.1  perseant  *    documentation and/or other materials provided with the distribution.
     50  1.17       agc  * 3. Neither the name of the University nor the names of its contributors
     51   1.1  perseant  *    may be used to endorse or promote products derived from this software
     52   1.1  perseant  *    without specific prior written permission.
     53   1.1  perseant  *
     54   1.1  perseant  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55   1.1  perseant  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56   1.1  perseant  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57   1.1  perseant  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58   1.1  perseant  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59   1.1  perseant  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60   1.1  perseant  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61   1.1  perseant  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62   1.1  perseant  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63   1.1  perseant  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64   1.1  perseant  * SUCH DAMAGE.
     65   1.1  perseant  */
     66   1.1  perseant 
     67   1.1  perseant /* #define DKTYPENAMES */
     68   1.1  perseant #define FSTYPENAMES
     69  1.13  perseant #include <sys/types.h>
     70   1.1  perseant #include <sys/param.h>
     71   1.1  perseant #include <sys/time.h>
     72  1.13  perseant #include <sys/buf.h>
     73  1.13  perseant #include <sys/mount.h>
     74  1.13  perseant #include <sys/queue.h>
     75   1.1  perseant #include <sys/stat.h>
     76   1.1  perseant #include <sys/ioctl.h>
     77   1.1  perseant #include <sys/disklabel.h>
     78   1.1  perseant #include <sys/file.h>
     79   1.1  perseant 
     80  1.13  perseant #include <ufs/ufs/inode.h>
     81  1.13  perseant #include <ufs/ufs/ufsmount.h>
     82  1.13  perseant #define vnode uvnode
     83   1.1  perseant #include <ufs/lfs/lfs.h>
     84  1.13  perseant #undef vnode
     85   1.1  perseant 
     86   1.1  perseant #include <ctype.h>
     87   1.1  perseant #include <err.h>
     88   1.1  perseant #include <errno.h>
     89   1.1  perseant #include <stdio.h>
     90   1.1  perseant #include <stdlib.h>
     91   1.1  perseant #include <string.h>
     92  1.29  perseant #include <time.h>
     93   1.1  perseant 
     94  1.13  perseant #include "bufcache.h"
     95  1.13  perseant #include "vnode.h"
     96  1.26  christos #include "lfs_user.h"
     97  1.13  perseant 
     98   1.1  perseant #include "fsck.h"
     99   1.1  perseant #include "extern.h"
    100   1.1  perseant #include "fsutil.h"
    101   1.1  perseant 
    102  1.29  perseant extern u_int32_t cksum(void *, size_t);
    103   1.5  perseant static struct disklabel *getdisklabel(const char *, int);
    104  1.18   minoura static uint64_t calcmaxfilesize(int);
    105   1.1  perseant 
    106  1.13  perseant ufs_daddr_t *din_table;
    107  1.13  perseant SEGUSE *seg_table;
    108   1.4  perseant 
    109   1.1  perseant #ifdef DKTYPENAMES
    110  1.13  perseant int useless(void);
    111   1.1  perseant 
    112   1.1  perseant int
    113   1.1  perseant useless(void)
    114   1.1  perseant {
    115  1.13  perseant 	char **foo = (char **) dktypenames;
    116  1.13  perseant 	char **bar = (char **) fscknames;
    117   1.1  perseant 
    118   1.5  perseant 	return foo - bar;
    119   1.1  perseant }
    120   1.1  perseant #endif
    121   1.1  perseant 
    122  1.18   minoura /*
    123  1.18   minoura  * calculate the maximum file size allowed with the specified block shift.
    124  1.18   minoura  */
    125  1.18   minoura static uint64_t
    126  1.18   minoura calcmaxfilesize(int bshift)
    127  1.18   minoura {
    128  1.18   minoura 	uint64_t nptr; /* number of block pointers per block */
    129  1.18   minoura 	uint64_t maxblock;
    130  1.18   minoura 
    131  1.18   minoura 	nptr = (1 << bshift) / sizeof(uint32_t);
    132  1.18   minoura 	maxblock = NDADDR + nptr + nptr * nptr + nptr * nptr * nptr;
    133  1.18   minoura 
    134  1.18   minoura 	return maxblock << bshift;
    135  1.18   minoura }
    136  1.20  perseant 
    137  1.20  perseant void
    138  1.20  perseant reset_maxino(ino_t len)
    139  1.20  perseant {
    140  1.29  perseant 	if (debug)
    141  1.29  perseant 		pwarn("maxino reset from %lld to %lld\n", (long long)maxino,
    142  1.29  perseant 			(long long)len);
    143  1.29  perseant 
    144  1.20  perseant 	din_table = (ufs_daddr_t *) realloc(din_table, len * sizeof(*din_table));
    145  1.20  perseant 	statemap = realloc(statemap, len * sizeof(char));
    146  1.20  perseant 	typemap = realloc(typemap, len * sizeof(char));
    147  1.20  perseant 	lncntp = (int16_t *) realloc(lncntp, len * sizeof(int16_t));
    148  1.20  perseant 
    149  1.20  perseant 	if (din_table == NULL || statemap == NULL || typemap == NULL ||
    150  1.20  perseant 	    lncntp == NULL) {
    151  1.20  perseant 		errexit("expanding tables failed: out of memory");
    152  1.20  perseant 	}
    153  1.20  perseant 
    154  1.20  perseant 	memset(din_table + maxino, 0, (len - maxino) * sizeof(*din_table));
    155  1.29  perseant 	memset(statemap + maxino, USTATE, (len - maxino) * sizeof(char));
    156  1.20  perseant 	memset(typemap + maxino, 0, (len - maxino) * sizeof(char));
    157  1.20  perseant 	memset(lncntp + maxino, 0, (len - maxino) * sizeof(int16_t));
    158  1.20  perseant 
    159  1.24  perseant 	maxino = len;
    160  1.24  perseant 
    161  1.29  perseant 	/*
    162  1.29  perseant 	 * We can't roll forward after allocating new inodes in previous
    163  1.29  perseant 	 * phases, or thy would conflict (lost+found, for example, might
    164  1.29  perseant 	 * disappear to be replaced by a file found in roll-forward).
    165  1.29  perseant 	 */
    166  1.29  perseant 	no_roll_forward = 1;
    167  1.29  perseant 
    168  1.20  perseant 	return;
    169  1.20  perseant }
    170  1.18   minoura 
    171  1.29  perseant extern time_t write_time;
    172  1.29  perseant 
    173   1.1  perseant int
    174   1.5  perseant setup(const char *dev)
    175   1.1  perseant {
    176  1.13  perseant 	long bmapsize;
    177   1.1  perseant 	struct disklabel *lp;
    178  1.13  perseant 	struct stat statb;
    179  1.13  perseant 	int doskipclean;
    180  1.13  perseant 	u_int64_t maxfilesize;
    181  1.13  perseant 	int open_flags;
    182  1.13  perseant 	struct uvnode *ivp;
    183  1.13  perseant 	struct ubuf *bp;
    184  1.29  perseant 	int i, isdirty;
    185  1.29  perseant 	long sn, curseg;
    186  1.13  perseant 	SEGUSE *sup;
    187   1.1  perseant 
    188   1.1  perseant 	havesb = 0;
    189   1.1  perseant 	doskipclean = skipclean;
    190   1.1  perseant 	if (stat(dev, &statb) < 0) {
    191  1.29  perseant 		pfatal("Can't stat %s: %s\n", dev, strerror(errno));
    192   1.1  perseant 		return (0);
    193   1.1  perseant 	}
    194  1.29  perseant 	if (!S_ISCHR(statb.st_mode) && skipclean) {
    195   1.1  perseant 		pfatal("%s is not a character device", dev);
    196   1.1  perseant 		if (reply("CONTINUE") == 0)
    197   1.1  perseant 			return (0);
    198   1.1  perseant 	}
    199  1.13  perseant 	if (nflag)
    200  1.13  perseant 		open_flags = O_RDONLY;
    201  1.13  perseant 	else
    202  1.13  perseant 		open_flags = O_RDWR;
    203  1.13  perseant 
    204  1.13  perseant 	if ((fsreadfd = open(dev, open_flags)) < 0) {
    205  1.29  perseant 		pfatal("Can't open %s: %s\n", dev, strerror(errno));
    206   1.1  perseant 		return (0);
    207   1.1  perseant 	}
    208  1.13  perseant 	if (nflag) {
    209   1.1  perseant 		if (preen)
    210   1.1  perseant 			pfatal("NO WRITE ACCESS");
    211  1.21  perseant 		printf("** %s (NO WRITE)\n", dev);
    212  1.21  perseant 		quiet = 0;
    213  1.21  perseant 	} else if (!preen && !quiet)
    214  1.21  perseant 		printf("** %s\n", dev);
    215  1.21  perseant 
    216   1.1  perseant 	fsmodified = 0;
    217   1.1  perseant 	lfdir = 0;
    218  1.13  perseant 
    219  1.29  perseant 	/* Initialize time in case we have to write */
    220  1.29  perseant 	time(&write_time);
    221  1.29  perseant 
    222  1.20  perseant 	bufinit(0); /* XXX we could make a better guess */
    223  1.19  perseant 	fs = lfs_init(fsreadfd, bflag, idaddr, 0, debug);
    224  1.13  perseant 	if (fs == NULL) {
    225  1.13  perseant 		if (preen)
    226  1.13  perseant 			printf("%s: ", cdevname());
    227  1.28  perseant 		errexit("BAD SUPER BLOCK OR IFILE INODE NOT FOUND\n");
    228  1.13  perseant 	}
    229   1.5  perseant 	if ((lp = getdisklabel((char *) NULL, fsreadfd)) != NULL)
    230   1.1  perseant 		dev_bsize = secsize = lp->d_secsize;
    231   1.1  perseant 	else
    232   1.1  perseant 		dev_bsize = secsize = DEV_BSIZE;
    233   1.1  perseant 
    234  1.20  perseant         /* Resize buffer cache now that we have a superblock to guess from. */
    235  1.20  perseant         bufrehash((fs->lfs_segtabsz + maxino / fs->lfs_ifpb) << 4);
    236  1.20  perseant 
    237  1.13  perseant 	if (fs->lfs_pflags & LFS_PF_CLEAN) {
    238  1.13  perseant 		if (doskipclean) {
    239  1.21  perseant 			if (!quiet)
    240  1.21  perseant 				pwarn("%sile system is clean; not checking\n",
    241  1.21  perseant 				      preen ? "f" : "** F");
    242  1.13  perseant 			return (-1);
    243   1.1  perseant 		}
    244  1.13  perseant 		if (!preen)
    245  1.13  perseant 			pwarn("** File system is already clean\n");
    246  1.13  perseant 	}
    247   1.9  perseant 
    248  1.29  perseant 	if (idaddr) {
    249  1.29  perseant 		daddr_t tdaddr;
    250  1.29  perseant 		SEGSUM *sp;
    251  1.29  perseant 		FINFO *fp;
    252  1.29  perseant 		int bc;
    253  1.29  perseant 
    254  1.29  perseant 		if (debug)
    255  1.29  perseant 			pwarn("adjusting offset, serial for -i 0x%lx\n",
    256  1.29  perseant 				(unsigned long)idaddr);
    257  1.29  perseant 		tdaddr = sntod(fs, dtosn(fs, idaddr));
    258  1.29  perseant 		if (sntod(fs, dtosn(fs, tdaddr)) == tdaddr) {
    259  1.29  perseant 			for (i = 0; i < LFS_MAXNUMSB; i++) {
    260  1.29  perseant 				if (fs->lfs_sboffs[i] == tdaddr)
    261  1.29  perseant 					tdaddr += btofsb(fs, LFS_SBPAD);
    262  1.29  perseant 				if (fs->lfs_sboffs[i] > tdaddr)
    263  1.29  perseant 					break;
    264  1.29  perseant 			}
    265  1.29  perseant 		}
    266  1.29  perseant 		fs->lfs_offset = tdaddr;
    267  1.29  perseant 		if (debug)
    268  1.29  perseant 			pwarn("begin with offset/serial 0x%x/%d\n",
    269  1.29  perseant 				(int)fs->lfs_offset, (int)fs->lfs_serial);
    270  1.29  perseant 		while (tdaddr < idaddr) {
    271  1.29  perseant 			bread(fs->lfs_devvp, fsbtodb(fs, tdaddr),
    272  1.29  perseant 			      fs->lfs_sumsize,
    273  1.29  perseant 			      NULL, &bp);
    274  1.29  perseant 			sp = (SEGSUM *)bp->b_data;
    275  1.29  perseant 			if (sp->ss_sumsum != cksum(&sp->ss_datasum,
    276  1.29  perseant 						   fs->lfs_sumsize -
    277  1.29  perseant 						   sizeof(sp->ss_sumsum))) {
    278  1.29  perseant 				brelse(bp);
    279  1.29  perseant 				break;
    280  1.29  perseant 			}
    281  1.29  perseant 			fp = (FINFO *)(sp + 1);
    282  1.29  perseant 			bc = howmany(sp->ss_ninos, INOPB(fs)) <<
    283  1.29  perseant 				(fs->lfs_version > 1 ? fs->lfs_ffshift :
    284  1.29  perseant 						       fs->lfs_bshift);
    285  1.29  perseant 			for (i = 0; i < sp->ss_nfinfo; i++) {
    286  1.29  perseant 				bc += fp->fi_lastlength + ((fp->fi_nblocks - 1)
    287  1.29  perseant 					<< fs->lfs_bshift);
    288  1.29  perseant 				fp = (FINFO *)(fp->fi_blocks + fp->fi_nblocks);
    289  1.29  perseant 			}
    290  1.29  perseant 
    291  1.29  perseant 			tdaddr += btofsb(fs, bc) + 1;
    292  1.29  perseant 			fs->lfs_offset = tdaddr;
    293  1.29  perseant 			fs->lfs_serial = sp->ss_serial + 1;
    294  1.29  perseant 			brelse(bp);
    295  1.29  perseant 		}
    296  1.29  perseant 
    297  1.29  perseant 		/*
    298  1.29  perseant 		 * Set curseg, nextseg appropriately -- inlined from
    299  1.29  perseant 		 * lfs_newseg()
    300  1.29  perseant 		 */
    301  1.29  perseant 		curseg = dtosn(fs, fs->lfs_offset);
    302  1.29  perseant 		fs->lfs_curseg = sntod(fs, curseg);
    303  1.29  perseant 		for (sn = curseg + fs->lfs_interleave;;) {
    304  1.29  perseant 			sn = (sn + 1) % fs->lfs_nseg;
    305  1.29  perseant 			if (sn == curseg)
    306  1.29  perseant 				errx(1, "init: no clean segments");
    307  1.29  perseant 			LFS_SEGENTRY(sup, fs, sn, bp);
    308  1.29  perseant 			isdirty = sup->su_flags & SEGUSE_DIRTY;
    309  1.29  perseant 			brelse(bp);
    310  1.29  perseant 
    311  1.29  perseant 			if (!isdirty)
    312  1.29  perseant 				break;
    313  1.29  perseant 		}
    314  1.29  perseant 
    315  1.29  perseant 		/* Skip superblock if necessary */
    316  1.29  perseant 		for (i = 0; i < LFS_MAXNUMSB; i++)
    317  1.29  perseant 			if (fs->lfs_offset == fs->lfs_sboffs[i])
    318  1.29  perseant 				fs->lfs_offset += btofsb(fs, LFS_SBPAD);
    319  1.29  perseant 
    320  1.29  perseant 		++fs->lfs_nactive;
    321  1.29  perseant 		fs->lfs_nextseg = sntod(fs, sn);
    322  1.29  perseant 		if (debug) {
    323  1.29  perseant 			pwarn("offset = 0x%" PRIx32 ", serial = %" PRId64 "\n",
    324  1.29  perseant 				fs->lfs_offset, fs->lfs_serial);
    325  1.29  perseant 			pwarn("curseg = %" PRIx32 ", nextseg = %" PRIx32 "\n",
    326  1.29  perseant 				fs->lfs_curseg, fs->lfs_nextseg);
    327  1.29  perseant 		}
    328  1.29  perseant 
    329  1.29  perseant 		if (!nflag && !skipclean) {
    330  1.29  perseant 			fs->lfs_idaddr = idaddr;
    331  1.29  perseant 			fsmodified = 1;
    332  1.29  perseant 			sbdirty();
    333  1.29  perseant 		}
    334  1.29  perseant 	}
    335  1.29  perseant 
    336   1.5  perseant 	if (debug) {
    337  1.29  perseant 		pwarn("idaddr    = 0x%lx\n", idaddr ? (unsigned long)idaddr :
    338  1.15  perseant 			(unsigned long)fs->lfs_idaddr);
    339  1.29  perseant 		pwarn("dev_bsize = %lu\n", dev_bsize);
    340  1.29  perseant 		pwarn("lfs_bsize = %lu\n", (unsigned long) fs->lfs_bsize);
    341  1.29  perseant 		pwarn("lfs_fsize = %lu\n", (unsigned long) fs->lfs_fsize);
    342  1.29  perseant 		pwarn("lfs_frag  = %lu\n", (unsigned long) fs->lfs_frag);
    343  1.29  perseant 		pwarn("lfs_inopb = %lu\n", (unsigned long) fs->lfs_inopb);
    344  1.13  perseant 	}
    345  1.13  perseant 	if (fs->lfs_version == 1)
    346  1.13  perseant 		maxfsblock = fs->lfs_size * (fs->lfs_bsize / dev_bsize);
    347  1.13  perseant 	else
    348  1.13  perseant 		maxfsblock = fs->lfs_size;
    349  1.18   minoura 	maxfilesize = calcmaxfilesize(fs->lfs_bshift);
    350  1.13  perseant 	if ((fs->lfs_minfree < 0 || fs->lfs_minfree > 99)) {
    351   1.1  perseant 		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
    352  1.13  perseant 		    fs->lfs_minfree);
    353   1.1  perseant 		if (reply("SET TO DEFAULT") == 1) {
    354  1.13  perseant 			fs->lfs_minfree = 10;
    355   1.1  perseant 			sbdirty();
    356   1.1  perseant 		}
    357   1.1  perseant 	}
    358  1.13  perseant 	if (fs->lfs_bmask != fs->lfs_bsize - 1) {
    359  1.29  perseant 		pwarn("INCORRECT BMASK=0x%x IN SUPERBLOCK (SHOULD BE 0x%x)",
    360  1.13  perseant 		    (unsigned int) fs->lfs_bmask,
    361  1.13  perseant 		    (unsigned int) fs->lfs_bsize - 1);
    362  1.13  perseant 		fs->lfs_bmask = fs->lfs_bsize - 1;
    363   1.1  perseant 		if (preen)
    364   1.1  perseant 			printf(" (FIXED)\n");
    365   1.1  perseant 		if (preen || reply("FIX") == 1) {
    366   1.1  perseant 			sbdirty();
    367   1.1  perseant 		}
    368   1.1  perseant 	}
    369  1.13  perseant 	if (fs->lfs_ffmask != fs->lfs_fsize - 1) {
    370  1.13  perseant 		pwarn("INCORRECT FFMASK=%" PRId64 " IN SUPERBLOCK",
    371  1.13  perseant 		    fs->lfs_ffmask);
    372  1.13  perseant 		fs->lfs_ffmask = fs->lfs_fsize - 1;
    373   1.1  perseant 		if (preen)
    374   1.1  perseant 			printf(" (FIXED)\n");
    375   1.1  perseant 		if (preen || reply("FIX") == 1) {
    376   1.1  perseant 			sbdirty();
    377   1.1  perseant 		}
    378   1.1  perseant 	}
    379  1.13  perseant 	if (fs->lfs_fbmask != (1 << fs->lfs_fbshift) - 1) {
    380  1.13  perseant 		pwarn("INCORRECT FFMASK=%" PRId64 " IN SUPERBLOCK",
    381  1.13  perseant 		    fs->lfs_ffmask);
    382  1.13  perseant 		fs->lfs_fbmask = (1 << fs->lfs_fbshift) - 1;
    383  1.13  perseant 		if (preen)
    384  1.13  perseant 			printf(" (FIXED)\n");
    385  1.13  perseant 		if (preen || reply("FIX") == 1) {
    386  1.13  perseant 			sbdirty();
    387  1.13  perseant 		}
    388  1.13  perseant 	}
    389  1.13  perseant 	if (fs->lfs_maxfilesize != maxfilesize) {
    390   1.8     lukem 		pwarn(
    391  1.29  perseant 		    "INCORRECT MAXFILESIZE=%llu IN SUPERBLOCK (SHOULD BE %llu WITH BSHIFT %d)",
    392  1.13  perseant 		    (unsigned long long) fs->lfs_maxfilesize,
    393  1.20  perseant 		    (unsigned long long) maxfilesize, (int)fs->lfs_bshift);
    394   1.5  perseant 		if (preen)
    395   1.5  perseant 			printf(" (FIXED)\n");
    396   1.5  perseant 		if (preen || reply("FIX") == 1) {
    397  1.20  perseant 			fs->lfs_maxfilesize = maxfilesize;
    398   1.5  perseant 			sbdirty();
    399   1.1  perseant 		}
    400   1.5  perseant 	}
    401  1.16      fvdl 	if (fs->lfs_maxsymlinklen != MAXSYMLINKLEN_UFS1) {
    402   1.5  perseant 		pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK",
    403  1.13  perseant 		    fs->lfs_maxsymlinklen);
    404  1.16      fvdl 		fs->lfs_maxsymlinklen = MAXSYMLINKLEN_UFS1;
    405   1.5  perseant 		if (preen)
    406   1.5  perseant 			printf(" (FIXED)\n");
    407   1.5  perseant 		if (preen || reply("FIX") == 1) {
    408   1.5  perseant 			sbdirty();
    409   1.1  perseant 		}
    410   1.5  perseant 	}
    411  1.13  perseant 
    412  1.13  perseant 	/*
    413  1.13  perseant 	 * Read in the Ifile; we'll be using it a lot.
    414  1.13  perseant 	 * XXX If the Ifile is corrupted we are in bad shape.  We need to
    415  1.13  perseant 	 * XXX run through the segment headers of the entire disk to
    416  1.13  perseant 	 * XXX reconstruct the inode table, then pretend all segments are
    417  1.13  perseant 	 * XXX dirty while we do the rest.
    418  1.13  perseant 	 */
    419  1.13  perseant 	ivp = fs->lfs_ivnode;
    420  1.16      fvdl 	maxino = ((VTOI(ivp)->i_ffs1_size - (fs->lfs_cleansz + fs->lfs_segtabsz)
    421  1.13  perseant 		* fs->lfs_bsize) / fs->lfs_bsize) * fs->lfs_ifpb;
    422  1.13  perseant 	if (debug)
    423  1.29  perseant 		pwarn("maxino    = %llu\n", (unsigned long long)maxino);
    424  1.16      fvdl 	for (i = 0; i < VTOI(ivp)->i_ffs1_size; i += fs->lfs_bsize) {
    425  1.13  perseant 		bread(ivp, i >> fs->lfs_bshift, fs->lfs_bsize, NOCRED, &bp);
    426  1.13  perseant 		/* XXX check B_ERROR */
    427  1.13  perseant 		brelse(bp);
    428  1.13  perseant 	}
    429  1.13  perseant 
    430   1.1  perseant 	/*
    431   1.1  perseant 	 * allocate and initialize the necessary maps
    432   1.1  perseant 	 */
    433  1.13  perseant 	din_table = (ufs_daddr_t *) malloc(maxino * sizeof(*din_table));
    434  1.22  perseant 	if (din_table == NULL) {
    435  1.29  perseant 		pwarn("cannot alloc %lu bytes for din_table\n",
    436  1.23       agc 		    (unsigned long) maxino * sizeof(*din_table));
    437  1.22  perseant 		goto badsblabel;
    438  1.22  perseant 	}
    439  1.13  perseant 	memset(din_table, 0, maxino * sizeof(*din_table));
    440  1.13  perseant 	seg_table = (SEGUSE *) malloc(fs->lfs_nseg * sizeof(SEGUSE));
    441  1.22  perseant 	if (seg_table == NULL) {
    442  1.29  perseant 		pwarn("cannot alloc %lu bytes for seg_table\n",
    443  1.23       agc 		    (unsigned long) fs->lfs_nseg * sizeof(SEGUSE));
    444  1.22  perseant 		goto badsblabel;
    445  1.22  perseant 	}
    446  1.13  perseant 	memset(seg_table, 0, fs->lfs_nseg * sizeof(SEGUSE));
    447  1.13  perseant 	/* Get segment flags */
    448  1.13  perseant 	for (i = 0; i < fs->lfs_nseg; i++) {
    449  1.13  perseant 		LFS_SEGENTRY(sup, fs, i, bp);
    450  1.13  perseant 		seg_table[i].su_flags = sup->su_flags & ~SEGUSE_ACTIVE;
    451  1.14  perseant 		if (preen)
    452  1.14  perseant 			seg_table[i].su_nbytes = sup->su_nbytes;
    453  1.13  perseant 		brelse(bp);
    454  1.13  perseant 	}
    455  1.13  perseant 
    456  1.13  perseant 	/* Initialize Ifile entry */
    457  1.13  perseant 	din_table[fs->lfs_ifile] = fs->lfs_idaddr;
    458  1.16      fvdl 	seg_table[dtosn(fs, fs->lfs_idaddr)].su_nbytes += DINODE1_SIZE;
    459  1.13  perseant 
    460   1.1  perseant #ifndef VERBOSE_BLOCKMAP
    461   1.1  perseant 	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
    462  1.13  perseant 	blockmap = calloc((unsigned) bmapsize, sizeof(char));
    463   1.1  perseant #else
    464   1.5  perseant 	bmapsize = maxfsblock * sizeof(ino_t);
    465  1.13  perseant 	blockmap = (ino_t *) calloc(maxfsblock, sizeof(ino_t));
    466   1.1  perseant #endif
    467   1.1  perseant 	if (blockmap == NULL) {
    468  1.29  perseant 		pwarn("cannot alloc %u bytes for blockmap\n",
    469  1.13  perseant 		    (unsigned) bmapsize);
    470   1.1  perseant 		goto badsblabel;
    471   1.1  perseant 	}
    472  1.13  perseant 	statemap = calloc((unsigned) maxino, sizeof(char));
    473   1.1  perseant 	if (statemap == NULL) {
    474  1.29  perseant 		pwarn("cannot alloc %u bytes for statemap\n",
    475  1.13  perseant 		    (unsigned) maxino);
    476   1.1  perseant 		goto badsblabel;
    477   1.1  perseant 	}
    478  1.13  perseant 	typemap = calloc((unsigned) maxino, sizeof(char));
    479   1.1  perseant 	if (typemap == NULL) {
    480  1.29  perseant 		pwarn("cannot alloc %u bytes for typemap\n",
    481  1.13  perseant 		    (unsigned) maxino);
    482   1.1  perseant 		goto badsblabel;
    483   1.1  perseant 	}
    484  1.13  perseant 	lncntp = (int16_t *) calloc((unsigned) maxino, sizeof(int16_t));
    485   1.1  perseant 	if (lncntp == NULL) {
    486  1.29  perseant 		pwarn("cannot alloc %lu bytes for lncntp\n",
    487  1.13  perseant 		    (unsigned long) maxino * sizeof(int16_t));
    488   1.1  perseant 		goto badsblabel;
    489   1.1  perseant 	}
    490  1.14  perseant 
    491  1.14  perseant 	if (preen) {
    492  1.14  perseant 		n_files = fs->lfs_nfiles;
    493  1.14  perseant 		n_blks  = fs->lfs_dsize - fs->lfs_bfree;
    494  1.14  perseant 		numdirs = maxino;
    495  1.14  perseant 		inplast = 0;
    496  1.14  perseant 		listmax = numdirs + 10;
    497  1.14  perseant 		inpsort = (struct inoinfo **) calloc((unsigned) listmax,
    498  1.14  perseant 			sizeof(struct inoinfo *));
    499  1.14  perseant 		inphead = (struct inoinfo **) calloc((unsigned) numdirs,
    500  1.14  perseant 			sizeof(struct inoinfo *));
    501  1.14  perseant 		if (inpsort == NULL || inphead == NULL) {
    502  1.29  perseant 			pwarn("cannot alloc %lu bytes for inphead\n",
    503  1.14  perseant 				(unsigned long) numdirs * sizeof(struct inoinfo *));
    504  1.14  perseant 			exit(1);
    505  1.14  perseant 		}
    506  1.14  perseant 	}
    507  1.14  perseant 
    508   1.1  perseant 	return (1);
    509   1.1  perseant 
    510   1.1  perseant badsblabel:
    511   1.1  perseant 	ckfini(0);
    512   1.1  perseant 	return (0);
    513   1.1  perseant }
    514   1.1  perseant 
    515   1.1  perseant static struct disklabel *
    516   1.5  perseant getdisklabel(const char *s, int fd)
    517   1.1  perseant {
    518   1.1  perseant 	static struct disklabel lab;
    519   1.1  perseant 
    520   1.5  perseant 	if (ioctl(fd, DIOCGDINFO, (char *) &lab) < 0) {
    521   1.1  perseant 		if (s == NULL)
    522   1.5  perseant 			return ((struct disklabel *) NULL);
    523   1.1  perseant 		pwarn("ioctl (GCINFO): %s\n", strerror(errno));
    524   1.4  perseant 		return NULL;
    525   1.1  perseant 	}
    526   1.1  perseant 	return (&lab);
    527   1.1  perseant }
    528