Home | History | Annotate | Line # | Download | only in fsck_ffs
setup.c revision 1.88
      1  1.88    bouyer /*	$NetBSD: setup.c,v 1.88 2009/09/13 14:25:28 bouyer Exp $	*/
      2  1.19       cgd 
      3   1.1       cgd /*
      4  1.10   mycroft  * Copyright (c) 1980, 1986, 1993
      5  1.10   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.62       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32  1.28     lukem #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34  1.19       cgd #if 0
     35  1.30     lukem static char sccsid[] = "@(#)setup.c	8.10 (Berkeley) 5/9/95";
     36  1.19       cgd #else
     37  1.88    bouyer __RCSID("$NetBSD: setup.c,v 1.88 2009/09/13 14:25:28 bouyer Exp $");
     38  1.19       cgd #endif
     39   1.1       cgd #endif /* not lint */
     40   1.1       cgd 
     41   1.1       cgd #include <sys/param.h>
     42   1.8       cgd #include <sys/time.h>
     43   1.1       cgd #include <sys/stat.h>
     44   1.1       cgd #include <sys/ioctl.h>
     45   1.1       cgd #include <sys/file.h>
     46  1.80  christos #include <sys/disk.h>
     47  1.15       cgd 
     48  1.30     lukem #include <ufs/ufs/dinode.h>
     49  1.54       dbj #include <ufs/ufs/dir.h>
     50  1.33    bouyer #include <ufs/ufs/ufs_bswap.h>
     51  1.30     lukem #include <ufs/ffs/fs.h>
     52  1.33    bouyer #include <ufs/ffs/ffs_extern.h>
     53  1.30     lukem 
     54  1.30     lukem #include <ctype.h>
     55  1.30     lukem #include <err.h>
     56   1.1       cgd #include <errno.h>
     57  1.31     lukem #include <stdio.h>
     58  1.31     lukem #include <stdlib.h>
     59   1.1       cgd #include <string.h>
     60  1.26  christos 
     61   1.1       cgd #include "fsck.h"
     62  1.15       cgd #include "extern.h"
     63  1.27  christos #include "fsutil.h"
     64  1.80  christos #include "partutil.h"
     65  1.82  christos #include "exitvalues.h"
     66   1.1       cgd 
     67   1.1       cgd #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
     68   1.1       cgd 
     69  1.78  christos static void badsb(int, const char *);
     70  1.67       mrg static int calcsb(const char *, int, struct fs *);
     71  1.67       mrg static int readsb(int);
     72  1.67       mrg static int readappleufs(void);
     73   1.1       cgd 
     74  1.65       dbj int16_t sblkpostbl[256];
     75  1.65       dbj 
     76  1.30     lukem /*
     77  1.30     lukem  * Read in a superblock finding an alternate if necessary.
     78  1.30     lukem  * Return 1 if successful, 0 if unsuccessful, -1 if filesystem
     79  1.30     lukem  * is already clean (preen mode only).
     80  1.30     lukem  */
     81  1.15       cgd int
     82  1.84    bouyer setup(const char *dev, const char *origdev)
     83   1.1       cgd {
     84   1.1       cgd 	long cg, size, asked, i, j;
     85   1.1       cgd 	long bmapsize;
     86  1.80  christos 	struct disk_geom geo;
     87  1.80  christos 	struct dkwedge_info dkw;
     88  1.10   mycroft 	off_t sizepb;
     89  1.10   mycroft 	struct stat statb;
     90   1.1       cgd 	struct fs proto;
     91  1.21   mycroft 	int doskipclean;
     92  1.24   mycroft 	u_int64_t maxfilesize;
     93  1.46     lukem 	struct csum *ccsp;
     94  1.84    bouyer 	int fd;
     95   1.1       cgd 
     96   1.1       cgd 	havesb = 0;
     97  1.10   mycroft 	fswritefd = -1;
     98  1.21   mycroft 	doskipclean = skipclean;
     99   1.1       cgd 	if (stat(dev, &statb) < 0) {
    100   1.1       cgd 		printf("Can't stat %s: %s\n", dev, strerror(errno));
    101   1.1       cgd 		return (0);
    102   1.1       cgd 	}
    103  1.51     lukem 	if (!forceimage && !S_ISCHR(statb.st_mode)) {
    104   1.1       cgd 		pfatal("%s is not a character device", dev);
    105   1.1       cgd 		if (reply("CONTINUE") == 0)
    106   1.1       cgd 			return (0);
    107   1.1       cgd 	}
    108   1.1       cgd 	if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
    109   1.1       cgd 		printf("Can't open %s: %s\n", dev, strerror(errno));
    110   1.1       cgd 		return (0);
    111   1.1       cgd 	}
    112   1.1       cgd 	if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
    113   1.1       cgd 		fswritefd = -1;
    114   1.1       cgd 		if (preen)
    115   1.1       cgd 			pfatal("NO WRITE ACCESS");
    116  1.63       dsl 		printf("** %s (NO WRITE)\n", dev);
    117  1.63       dsl 		quiet = 0;
    118  1.63       dsl 	} else
    119  1.63       dsl 		if (!preen && !quiet)
    120  1.63       dsl 			printf("** %s\n", dev);
    121   1.1       cgd 	fsmodified = 0;
    122   1.1       cgd 	lfdir = 0;
    123   1.1       cgd 	initbarea(&sblk);
    124   1.1       cgd 	initbarea(&asblk);
    125  1.58      fvdl 	sblk.b_un.b_buf = malloc(SBLOCKSIZE);
    126  1.58      fvdl 	sblock = malloc(SBLOCKSIZE);
    127  1.58      fvdl 	asblk.b_un.b_buf = malloc(SBLOCKSIZE);
    128  1.58      fvdl 	altsblock = malloc(SBLOCKSIZE);
    129  1.33    bouyer 	if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL ||
    130  1.33    bouyer 		sblock == NULL || altsblock == NULL)
    131  1.82  christos 		errexit("Cannot allocate space for superblock");
    132  1.84    bouyer 	if (strcmp(dev, origdev) && !forceimage) {
    133  1.84    bouyer 		/*
    134  1.84    bouyer 		 * dev isn't the original fs (for example it's a snapshot)
    135  1.84    bouyer 		 * do getdiskinfo on the original device
    136  1.84    bouyer 		 */
    137  1.84    bouyer 		 fd = open(origdev, O_RDONLY);
    138  1.84    bouyer 		 if (fd < 0) {
    139  1.84    bouyer 			warn("Can't open %s", origdev);
    140  1.84    bouyer 			return (0);
    141  1.84    bouyer 		}
    142  1.84    bouyer 	} else {
    143  1.84    bouyer 		fd = fsreadfd;
    144  1.84    bouyer 	}
    145  1.84    bouyer 	if (!forceimage && getdiskinfo(origdev, fd, NULL, &geo, &dkw) != -1)
    146  1.80  christos 		dev_bsize = secsize = geo.dg_secsize;
    147   1.1       cgd 	else
    148   1.1       cgd 		dev_bsize = secsize = DEV_BSIZE;
    149   1.1       cgd 	/*
    150   1.1       cgd 	 * Read in the superblock, looking for alternates if necessary
    151   1.1       cgd 	 */
    152   1.1       cgd 	if (readsb(1) == 0) {
    153  1.51     lukem 		if (bflag || preen || forceimage ||
    154  1.51     lukem 		    calcsb(dev, fsreadfd, &proto) == 0)
    155   1.1       cgd 			return(0);
    156   1.1       cgd 		if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
    157   1.1       cgd 			return (0);
    158   1.1       cgd 		for (cg = 0; cg < proto.fs_ncg; cg++) {
    159   1.1       cgd 			bflag = fsbtodb(&proto, cgsblock(&proto, cg));
    160   1.1       cgd 			if (readsb(0) != 0)
    161   1.1       cgd 				break;
    162   1.1       cgd 		}
    163   1.1       cgd 		if (cg >= proto.fs_ncg) {
    164   1.1       cgd 			printf("%s %s\n%s %s\n%s %s\n",
    165   1.1       cgd 				"SEARCH FOR ALTERNATE SUPER-BLOCK",
    166   1.1       cgd 				"FAILED. YOU MUST USE THE",
    167  1.43   hubertf 				"-b OPTION TO fsck_ffs TO SPECIFY THE",
    168   1.1       cgd 				"LOCATION OF AN ALTERNATE",
    169   1.1       cgd 				"SUPER-BLOCK TO SUPPLY NEEDED",
    170  1.23       cgd 				"INFORMATION; SEE fsck_ffs(8).");
    171   1.1       cgd 			return(0);
    172   1.1       cgd 		}
    173  1.21   mycroft 		doskipclean = 0;
    174   1.1       cgd 		pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
    175   1.1       cgd 	}
    176  1.88    bouyer 	/* ffs_superblock_layout() == 2 */
    177  1.88    bouyer 	if (sblock->fs_magic != FS_UFS1_MAGIC ||
    178  1.88    bouyer 	    (sblock->fs_old_flags & FS_FLAGS_UPDATED) != 0) {
    179  1.88    bouyer 		/* can have WAPBL */
    180  1.88    bouyer 		if (check_wapbl() != 0) {
    181  1.88    bouyer 			doskipclean = 0;
    182  1.88    bouyer 		}
    183  1.88    bouyer 		if (sblock->fs_flags & FS_DOWAPBL) {
    184  1.88    bouyer 			if (preen) {
    185  1.88    bouyer 				if (!quiet)
    186  1.88    bouyer 					pwarn("file system is journaled; "
    187  1.88    bouyer 					    "not checking\n");
    188  1.88    bouyer 				return (-1);
    189  1.88    bouyer 			}
    190  1.83    simonb 			if (!quiet)
    191  1.88    bouyer 				pwarn("** File system is journaled; "
    192  1.88    bouyer 				    "replaying journal\n");
    193  1.88    bouyer 			replay_wapbl();
    194  1.88    bouyer 			doskipclean = 0;
    195  1.88    bouyer 			sblock->fs_flags &= ~FS_DOWAPBL;
    196  1.88    bouyer 			sbdirty();
    197  1.88    bouyer 			/* Although we may have updated the superblock from
    198  1.88    bouyer 			 * the journal, we are still going to do a full check,
    199  1.88    bouyer 			 * so we don't bother to re-read the superblock from
    200  1.88    bouyer 			 * the journal.
    201  1.88    bouyer 			 * XXX, instead we could re-read the superblock and
    202  1.88    bouyer 			 * then not force doskipclean = 0
    203  1.88    bouyer 			 */
    204  1.83    simonb 		}
    205  1.83    simonb 	}
    206  1.21   mycroft 	if (debug)
    207  1.33    bouyer 		printf("clean = %d\n", sblock->fs_clean);
    208  1.33    bouyer 	if (doswap)
    209  1.33    bouyer 		doskipclean = 0;
    210  1.33    bouyer 	if (sblock->fs_clean & FS_ISCLEAN) {
    211  1.22       cgd 		if (doskipclean) {
    212  1.63       dsl 			if (!quiet)
    213  1.63       dsl 				pwarn("%sile system is clean; not checking\n",
    214  1.63       dsl 				    preen ? "f" : "** F");
    215  1.21   mycroft 			return (-1);
    216  1.21   mycroft 		}
    217  1.33    bouyer 		if (!preen && !doswap)
    218  1.21   mycroft 			pwarn("** File system is already clean\n");
    219  1.21   mycroft 	}
    220  1.33    bouyer 	maxfsblock = sblock->fs_size;
    221  1.33    bouyer 	maxino = sblock->fs_ncg * sblock->fs_ipg;
    222  1.33    bouyer 	sizepb = sblock->fs_bsize;
    223  1.33    bouyer 	maxfilesize = sblock->fs_bsize * NDADDR - 1;
    224  1.24   mycroft 	for (i = 0; i < NIADDR; i++) {
    225  1.33    bouyer 		sizepb *= NINDIR(sblock);
    226  1.24   mycroft 		maxfilesize += sizepb;
    227  1.24   mycroft 	}
    228  1.65       dbj 	if ((!is_ufs2 && cvtlevel >= 4) &&
    229  1.65       dbj 			(sblock->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
    230  1.65       dbj 		if (preen)
    231  1.68       dbj 			pwarn("CONVERTING TO NEW SUPERBLOCK LAYOUT\n");
    232  1.68       dbj 		else if (!reply("CONVERT TO NEW SUPERBLOCK LAYOUT"))
    233  1.65       dbj 			return(0);
    234  1.65       dbj 		sblock->fs_old_flags |= FS_FLAGS_UPDATED;
    235  1.65       dbj 		/* Disable the postbl tables */
    236  1.65       dbj 		sblock->fs_old_cpc = 0;
    237  1.72       dbj 		sblock->fs_old_nrpos = 1;
    238  1.65       dbj 		sblock->fs_old_trackskew = 0;
    239  1.65       dbj 		/* The other fields have already been updated by
    240  1.65       dbj 		 * sb_oldfscompat_read
    241  1.65       dbj 		 */
    242  1.65       dbj 		sbdirty();
    243  1.65       dbj 	}
    244  1.73       dbj 	if (!is_ufs2 && cvtlevel == 3 &&
    245  1.73       dbj 	    (sblock->fs_old_flags & FS_FLAGS_UPDATED)) {
    246  1.73       dbj 		if (preen)
    247  1.73       dbj 			pwarn("DOWNGRADING TO OLD SUPERBLOCK LAYOUT\n");
    248  1.73       dbj 		else if (!reply("DOWNGRADE TO OLD SUPERBLOCK LAYOUT"))
    249  1.73       dbj 			return(0);
    250  1.73       dbj 		sblock->fs_old_flags &= ~FS_FLAGS_UPDATED;
    251  1.73       dbj 		sb_oldfscompat_write(sblock, sblock);
    252  1.73       dbj 		sblock->fs_old_flags &= ~FS_FLAGS_UPDATED; /* just in case */
    253  1.73       dbj 		/* Leave postbl tables disabled, but blank its superblock region anyway */
    254  1.73       dbj 		sblock->fs_old_postblformat = FS_DYNAMICPOSTBLFMT;
    255  1.73       dbj 		sblock->fs_old_cpc = 0;
    256  1.73       dbj 		sblock->fs_old_nrpos = 1;
    257  1.73       dbj 		sblock->fs_old_trackskew = 0;
    258  1.73       dbj 		memset(&sblock->fs_old_postbl_start, 0xff, 256);
    259  1.73       dbj 		sb_oldfscompat_read(sblock, &sblocksave);
    260  1.73       dbj 		sbdirty();
    261  1.73       dbj 	}
    262   1.1       cgd 	/*
    263   1.1       cgd 	 * Check and potentially fix certain fields in the super block.
    264   1.1       cgd 	 */
    265  1.83    simonb 	if (sblock->fs_flags & ~(FS_KNOWN_FLAGS)) {
    266  1.83    simonb 		pfatal("UNKNOWN FLAGS=0x%08x IN SUPERBLOCK", sblock->fs_flags);
    267  1.83    simonb 		if (reply("CLEAR") == 1) {
    268  1.83    simonb 			sblock->fs_flags &= FS_KNOWN_FLAGS;
    269  1.83    simonb 			sbdirty();
    270  1.83    simonb 		}
    271  1.83    simonb 	}
    272  1.33    bouyer 	if (sblock->fs_optim != FS_OPTTIME && sblock->fs_optim != FS_OPTSPACE) {
    273   1.1       cgd 		pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
    274   1.1       cgd 		if (reply("SET TO DEFAULT") == 1) {
    275  1.33    bouyer 			sblock->fs_optim = FS_OPTTIME;
    276   1.1       cgd 			sbdirty();
    277   1.1       cgd 		}
    278   1.1       cgd 	}
    279  1.33    bouyer 	if ((sblock->fs_minfree < 0 || sblock->fs_minfree > 99)) {
    280   1.1       cgd 		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
    281  1.33    bouyer 			sblock->fs_minfree);
    282   1.1       cgd 		if (reply("SET TO DEFAULT") == 1) {
    283  1.33    bouyer 			sblock->fs_minfree = 10;
    284   1.1       cgd 			sbdirty();
    285   1.1       cgd 		}
    286   1.1       cgd 	}
    287  1.58      fvdl 	if (!is_ufs2 && sblock->fs_old_postblformat != FS_42POSTBLFMT &&
    288  1.58      fvdl 	    (sblock->fs_old_interleave < 1 ||
    289  1.58      fvdl 	    sblock->fs_old_interleave > sblock->fs_old_nsect)) {
    290   1.1       cgd 		pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
    291  1.58      fvdl 			sblock->fs_old_interleave);
    292  1.58      fvdl 		sblock->fs_old_interleave = 1;
    293   1.1       cgd 		if (preen)
    294   1.1       cgd 			printf(" (FIXED)\n");
    295   1.1       cgd 		if (preen || reply("SET TO DEFAULT") == 1) {
    296   1.1       cgd 			sbdirty();
    297   1.1       cgd 			dirty(&asblk);
    298   1.1       cgd 		}
    299   1.1       cgd 	}
    300  1.58      fvdl 	if (!is_ufs2 && sblock->fs_old_postblformat != FS_42POSTBLFMT &&
    301  1.58      fvdl 	    (sblock->fs_old_npsect < sblock->fs_old_nsect ||
    302  1.58      fvdl 	    sblock->fs_old_npsect > sblock->fs_old_nsect*2)) {
    303   1.1       cgd 		pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
    304  1.58      fvdl 			sblock->fs_old_npsect);
    305  1.58      fvdl 		sblock->fs_old_npsect = sblock->fs_old_nsect;
    306   1.1       cgd 		if (preen)
    307   1.1       cgd 			printf(" (FIXED)\n");
    308   1.1       cgd 		if (preen || reply("SET TO DEFAULT") == 1) {
    309   1.1       cgd 			sbdirty();
    310   1.1       cgd 			dirty(&asblk);
    311   1.1       cgd 		}
    312   1.1       cgd 	}
    313  1.33    bouyer 	if (sblock->fs_bmask != ~(sblock->fs_bsize - 1)) {
    314  1.28     lukem 		pwarn("INCORRECT BMASK=0x%x IN SUPERBLOCK",
    315  1.33    bouyer 			sblock->fs_bmask);
    316  1.33    bouyer 		sblock->fs_bmask = ~(sblock->fs_bsize - 1);
    317  1.24   mycroft 		if (preen)
    318  1.24   mycroft 			printf(" (FIXED)\n");
    319  1.24   mycroft 		if (preen || reply("FIX") == 1) {
    320  1.24   mycroft 			sbdirty();
    321  1.24   mycroft 			dirty(&asblk);
    322  1.24   mycroft 		}
    323  1.24   mycroft 	}
    324  1.33    bouyer 	if (sblock->fs_fmask != ~(sblock->fs_fsize - 1)) {
    325  1.28     lukem 		pwarn("INCORRECT FMASK=0x%x IN SUPERBLOCK",
    326  1.33    bouyer 			sblock->fs_fmask);
    327  1.33    bouyer 		sblock->fs_fmask = ~(sblock->fs_fsize - 1);
    328  1.24   mycroft 		if (preen)
    329  1.24   mycroft 			printf(" (FIXED)\n");
    330  1.24   mycroft 		if (preen || reply("FIX") == 1) {
    331  1.24   mycroft 			sbdirty();
    332  1.24   mycroft 			dirty(&asblk);
    333  1.24   mycroft 		}
    334  1.24   mycroft 	}
    335  1.77       dbj 	if (is_ufs2 || sblock->fs_old_inodefmt >= FS_44INODEFMT) {
    336  1.33    bouyer 		if (sblock->fs_maxfilesize != maxfilesize) {
    337  1.38     lukem 			pwarn("INCORRECT MAXFILESIZE=%lld IN SUPERBLOCK",
    338  1.33    bouyer 			    (unsigned long long)sblock->fs_maxfilesize);
    339  1.33    bouyer 			sblock->fs_maxfilesize = maxfilesize;
    340  1.24   mycroft 			if (preen)
    341  1.24   mycroft 				printf(" (FIXED)\n");
    342  1.24   mycroft 			if (preen || reply("FIX") == 1) {
    343  1.24   mycroft 				sbdirty();
    344  1.24   mycroft 				dirty(&asblk);
    345  1.24   mycroft 			}
    346  1.24   mycroft 		}
    347  1.58      fvdl 		if ((is_ufs2 && sblock->fs_maxsymlinklen != MAXSYMLINKLEN_UFS2)
    348  1.58      fvdl 		    ||
    349  1.58      fvdl 		   (!is_ufs2 && sblock->fs_maxsymlinklen != MAXSYMLINKLEN_UFS1))
    350  1.58      fvdl 		    {
    351  1.24   mycroft 			pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK",
    352  1.33    bouyer 				sblock->fs_maxsymlinklen);
    353  1.58      fvdl 			sblock->fs_maxsymlinklen = is_ufs2 ?
    354  1.58      fvdl 			    MAXSYMLINKLEN_UFS2 : MAXSYMLINKLEN_UFS1;
    355  1.24   mycroft 			if (preen)
    356  1.24   mycroft 				printf(" (FIXED)\n");
    357  1.24   mycroft 			if (preen || reply("FIX") == 1) {
    358  1.24   mycroft 				sbdirty();
    359  1.24   mycroft 				dirty(&asblk);
    360  1.24   mycroft 			}
    361  1.24   mycroft 		}
    362  1.33    bouyer 		if (sblock->fs_qbmask != ~sblock->fs_bmask) {
    363  1.66       dbj 			pwarn("INCORRECT QBMASK=%#llx IN SUPERBLOCK",
    364  1.33    bouyer 			    (unsigned long long)sblock->fs_qbmask);
    365  1.33    bouyer 			sblock->fs_qbmask = ~sblock->fs_bmask;
    366  1.24   mycroft 			if (preen)
    367  1.24   mycroft 				printf(" (FIXED)\n");
    368  1.24   mycroft 			if (preen || reply("FIX") == 1) {
    369  1.24   mycroft 				sbdirty();
    370  1.24   mycroft 				dirty(&asblk);
    371  1.24   mycroft 			}
    372  1.24   mycroft 		}
    373  1.33    bouyer 		if (sblock->fs_qfmask != ~sblock->fs_fmask) {
    374  1.66       dbj 			pwarn("INCORRECT QFMASK=%#llx IN SUPERBLOCK",
    375  1.33    bouyer 			    (unsigned long long)sblock->fs_qfmask);
    376  1.33    bouyer 			sblock->fs_qfmask = ~sblock->fs_fmask;
    377  1.24   mycroft 			if (preen)
    378  1.24   mycroft 				printf(" (FIXED)\n");
    379  1.24   mycroft 			if (preen || reply("FIX") == 1) {
    380  1.24   mycroft 				sbdirty();
    381  1.24   mycroft 				dirty(&asblk);
    382  1.24   mycroft 			}
    383  1.24   mycroft 		}
    384  1.10   mycroft 		newinofmt = 1;
    385  1.10   mycroft 	} else {
    386  1.33    bouyer 		sblock->fs_qbmask = ~sblock->fs_bmask;
    387  1.33    bouyer 		sblock->fs_qfmask = ~sblock->fs_fmask;
    388  1.10   mycroft 		newinofmt = 0;
    389  1.10   mycroft 	}
    390  1.10   mycroft 	/*
    391  1.10   mycroft 	 * Convert to new inode format.
    392  1.10   mycroft 	 */
    393  1.58      fvdl 	if (!is_ufs2 && cvtlevel >= 2 &&
    394  1.58      fvdl 	    sblock->fs_old_inodefmt < FS_44INODEFMT) {
    395  1.10   mycroft 		if (preen)
    396  1.10   mycroft 			pwarn("CONVERTING TO NEW INODE FORMAT\n");
    397  1.10   mycroft 		else if (!reply("CONVERT TO NEW INODE FORMAT"))
    398  1.10   mycroft 			return(0);
    399  1.10   mycroft 		doinglevel2++;
    400  1.58      fvdl 		sblock->fs_old_inodefmt = FS_44INODEFMT;
    401  1.33    bouyer 		sblock->fs_maxfilesize = maxfilesize;
    402  1.58      fvdl 		sblock->fs_maxsymlinklen = MAXSYMLINKLEN_UFS1;
    403  1.33    bouyer 		sblock->fs_qbmask = ~sblock->fs_bmask;
    404  1.33    bouyer 		sblock->fs_qfmask = ~sblock->fs_fmask;
    405  1.10   mycroft 		sbdirty();
    406  1.10   mycroft 		dirty(&asblk);
    407  1.10   mycroft 	}
    408  1.10   mycroft 	/*
    409  1.10   mycroft 	 * Convert to new cylinder group format.
    410  1.10   mycroft 	 */
    411  1.58      fvdl 	if (!is_ufs2 && cvtlevel >= 1 &&
    412  1.58      fvdl 	    sblock->fs_old_postblformat == FS_42POSTBLFMT) {
    413  1.10   mycroft 		if (preen)
    414  1.10   mycroft 			pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n");
    415  1.10   mycroft 		else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT"))
    416  1.10   mycroft 			return(0);
    417  1.10   mycroft 		doinglevel1++;
    418  1.58      fvdl 		sblock->fs_old_postblformat = FS_DYNAMICPOSTBLFMT;
    419  1.65       dbj 		sblock->fs_old_nrpos = 8;
    420  1.65       dbj 		sblock->fs_old_postbloff =
    421  1.65       dbj 		    (char *)(&sblock->fs_old_postbl_start) -
    422  1.65       dbj 		    (char *)(&sblock->fs_firstfield);
    423  1.65       dbj 		sblock->fs_old_rotbloff =
    424  1.65       dbj 				(char *)(&sblock->fs_magic+1) -
    425  1.65       dbj 				(char *)(&sblock->fs_firstfield);
    426  1.65       dbj 		sblock->fs_cgsize =
    427  1.65       dbj 			fragroundup(sblock, CGSIZE(sblock));
    428  1.10   mycroft 		sbdirty();
    429  1.10   mycroft 		dirty(&asblk);
    430   1.1       cgd 	}
    431  1.11        ws 	if (asblk.b_dirty && !bflag) {
    432  1.65       dbj 		memmove(sblk.b_un.b_fs, sblock, SBLOCKSIZE);
    433  1.65       dbj 		sb_oldfscompat_write(sblk.b_un.b_fs, sblocksave);
    434  1.33    bouyer 		if (needswap)
    435  1.65       dbj 			ffs_sb_swap(sblk.b_un.b_fs, sblk.b_un.b_fs);
    436  1.33    bouyer 		memmove(asblk.b_un.b_fs, sblk.b_un.b_fs, (size_t)sblock->fs_sbsize);
    437   1.1       cgd 		flush(fswritefd, &asblk);
    438   1.1       cgd 	}
    439   1.1       cgd 	/*
    440   1.1       cgd 	 * read in the summary info.
    441   1.1       cgd 	 */
    442   1.1       cgd 	asked = 0;
    443  1.46     lukem 	sblock->fs_csp = (struct csum *)calloc(1, sblock->fs_cssize);
    444  1.79    rumble 	if (sblock->fs_csp == NULL) {
    445  1.79    rumble 		pwarn("cannot alloc %u bytes for summary info\n",
    446  1.79    rumble 		    sblock->fs_cssize);
    447  1.79    rumble 		goto badsblabel;
    448  1.79    rumble 	}
    449  1.33    bouyer 	for (i = 0, j = 0; i < sblock->fs_cssize; i += sblock->fs_bsize, j++) {
    450  1.33    bouyer 		size = sblock->fs_cssize - i < sblock->fs_bsize ?
    451  1.33    bouyer 		    sblock->fs_cssize - i : sblock->fs_bsize;
    452  1.46     lukem 		ccsp = (struct csum *)((char *)sblock->fs_csp + i);
    453  1.46     lukem 		if (bread(fsreadfd, (char *)ccsp,
    454  1.33    bouyer 		    fsbtodb(sblock, sblock->fs_csaddr + j * sblock->fs_frag),
    455   1.1       cgd 		    size) != 0 && !asked) {
    456   1.1       cgd 			pfatal("BAD SUMMARY INFORMATION");
    457  1.37      fvdl 			if (reply("CONTINUE") == 0) {
    458  1.37      fvdl 				markclean = 0;
    459  1.82  christos 				exit(FSCK_EXIT_CHECK_FAILED);
    460  1.37      fvdl 			}
    461   1.1       cgd 			asked++;
    462   1.1       cgd 		}
    463  1.33    bouyer 		if (doswap) {
    464  1.46     lukem 			ffs_csum_swap(ccsp, ccsp, size);
    465  1.46     lukem 			bwrite(fswritefd, (char *)ccsp,
    466  1.37      fvdl 			    fsbtodb(sblock,
    467  1.37      fvdl 				sblock->fs_csaddr + j * sblock->fs_frag),
    468  1.37      fvdl 			    size);
    469  1.33    bouyer 		}
    470  1.46     lukem 		if (needswap)
    471  1.46     lukem 			ffs_csum_swap(ccsp, ccsp, size);
    472   1.1       cgd 	}
    473   1.1       cgd 	/*
    474   1.1       cgd 	 * allocate and initialize the necessary maps
    475   1.1       cgd 	 */
    476  1.20       cgd 	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
    477   1.1       cgd 	blockmap = calloc((unsigned)bmapsize, sizeof (char));
    478   1.1       cgd 	if (blockmap == NULL) {
    479  1.63       dsl 		pwarn("cannot alloc %u bytes for blockmap\n",
    480   1.1       cgd 		    (unsigned)bmapsize);
    481  1.15       cgd 		goto badsblabel;
    482   1.1       cgd 	}
    483  1.58      fvdl 	inostathead = calloc((unsigned)(sblock->fs_ncg),
    484  1.58      fvdl 	    sizeof(struct inostatlist));
    485  1.58      fvdl 	if (inostathead == NULL) {
    486  1.63       dsl 		pwarn("cannot alloc %u bytes for inostathead\n",
    487  1.58      fvdl 		    (unsigned)(sizeof(struct inostatlist) * (sblock->fs_ncg)));
    488  1.15       cgd 		goto badsblabel;
    489   1.1       cgd 	}
    490  1.39   mycroft 	/*
    491  1.39   mycroft 	 * cs_ndir may be inaccurate, particularly if we're using the -b
    492  1.39   mycroft 	 * option, so set a minimum to prevent bogus subdirectory reconnects
    493  1.39   mycroft 	 * and really inefficient directory scans.
    494  1.39   mycroft 	 * Also set a maximum in case the value is too large.
    495  1.39   mycroft 	 */
    496  1.33    bouyer 	numdirs = sblock->fs_cstotal.cs_ndir;
    497  1.39   mycroft 	if (numdirs < 1024)
    498  1.39   mycroft 		numdirs = 1024;
    499  1.39   mycroft 	if (numdirs > maxino + 1)
    500  1.39   mycroft 		numdirs = maxino + 1;
    501  1.58      fvdl 	dirhash = numdirs;
    502   1.1       cgd 	inplast = 0;
    503   1.1       cgd 	listmax = numdirs + 10;
    504   1.1       cgd 	inpsort = (struct inoinfo **)calloc((unsigned)listmax,
    505   1.1       cgd 	    sizeof(struct inoinfo *));
    506   1.1       cgd 	inphead = (struct inoinfo **)calloc((unsigned)numdirs,
    507   1.1       cgd 	    sizeof(struct inoinfo *));
    508   1.1       cgd 	if (inpsort == NULL || inphead == NULL) {
    509  1.63       dsl 		pwarn("cannot alloc %u bytes for inphead\n",
    510  1.29       mrg 		    (unsigned)(numdirs * sizeof(struct inoinfo *)));
    511  1.15       cgd 		goto badsblabel;
    512   1.1       cgd 	}
    513  1.37      fvdl 	cgrp = malloc(sblock->fs_cgsize);
    514  1.37      fvdl 	if (cgrp == NULL) {
    515  1.63       dsl 		pwarn("cannot alloc %u bytes for cylinder group\n",
    516  1.37      fvdl 		    sblock->fs_cgsize);
    517  1.37      fvdl 		goto badsblabel;
    518  1.37      fvdl 	}
    519   1.1       cgd 	bufinit();
    520  1.37      fvdl 	if (sblock->fs_flags & FS_DOSOFTDEP)
    521  1.37      fvdl 		usedsoftdep = 1;
    522  1.37      fvdl 	else
    523  1.37      fvdl 		usedsoftdep = 0;
    524  1.54       dbj 
    525  1.80  christos 	if (!forceimage && dkw.dkw_parent[0])
    526  1.80  christos 		if (strcmp(dkw.dkw_ptype, DKW_PTYPE_APPLEUFS) == 0)
    527  1.54       dbj 			isappleufs = 1;
    528  1.80  christos 
    529  1.80  christos 	if (readappleufs())
    530  1.54       dbj 		isappleufs = 1;
    531  1.54       dbj 
    532  1.54       dbj 	dirblksiz = DIRBLKSIZ;
    533  1.54       dbj 	if (isappleufs)
    534  1.54       dbj 		dirblksiz = APPLEUFS_DIRBLKSIZ;
    535  1.54       dbj 
    536  1.54       dbj 	if (debug)
    537  1.54       dbj 		printf("isappleufs = %d, dirblksiz = %d\n", isappleufs, dirblksiz);
    538  1.54       dbj 
    539   1.1       cgd 	return (1);
    540   1.1       cgd 
    541  1.15       cgd badsblabel:
    542  1.33    bouyer 	markclean=0;
    543  1.33    bouyer 	ckfini();
    544   1.1       cgd 	return (0);
    545   1.1       cgd }
    546   1.1       cgd 
    547  1.54       dbj static int
    548  1.75   xtraeme readappleufs(void)
    549  1.54       dbj {
    550  1.56      fvdl 	daddr_t label = APPLEUFS_LABEL_OFFSET / dev_bsize;
    551  1.54       dbj 	struct appleufslabel *appleufs;
    552  1.54       dbj 	int i;
    553  1.54       dbj 
    554  1.54       dbj 	/* XXX do we have to deal with APPLEUFS_LABEL_OFFSET not
    555  1.54       dbj 	 * being block aligned (CD's?)
    556  1.54       dbj 	 */
    557  1.67       mrg 	if (bread(fsreadfd, (char *)appleufsblk.b_un.b_fs, label,
    558  1.67       mrg 	    (long)APPLEUFS_LABEL_SIZE) != 0)
    559  1.54       dbj 		return 0;
    560  1.54       dbj 	appleufsblk.b_bno = label;
    561  1.54       dbj 	appleufsblk.b_size = APPLEUFS_LABEL_SIZE;
    562  1.54       dbj 
    563  1.54       dbj 	appleufs = appleufsblk.b_un.b_appleufs;
    564  1.54       dbj 
    565  1.54       dbj 	if (ntohl(appleufs->ul_magic) != APPLEUFS_LABEL_MAGIC) {
    566  1.54       dbj 		if (!isappleufs) {
    567  1.54       dbj 			return 0;
    568  1.54       dbj 		} else {
    569  1.54       dbj 			pfatal("MISSING APPLEUFS VOLUME LABEL\n");
    570  1.54       dbj 			if (reply("FIX") == 0) {
    571  1.54       dbj 				return 1;
    572  1.54       dbj 			}
    573  1.64       dbj 			ffs_appleufs_set(appleufs, NULL, -1, 0);
    574  1.54       dbj 			appleufsdirty();
    575  1.54       dbj 		}
    576  1.54       dbj 	}
    577  1.54       dbj 
    578  1.54       dbj 	if (ntohl(appleufs->ul_version) != APPLEUFS_LABEL_VERSION) {
    579  1.54       dbj 		pwarn("INCORRECT APPLE UFS VERSION NUMBER (%d should be %d)",
    580  1.54       dbj 			ntohl(appleufs->ul_version),APPLEUFS_LABEL_VERSION);
    581  1.54       dbj 		if (preen) {
    582  1.54       dbj 			printf(" (CORRECTED)\n");
    583  1.54       dbj 		}
    584  1.54       dbj 		if (preen || reply("CORRECT")) {
    585  1.54       dbj 			appleufs->ul_version = htonl(APPLEUFS_LABEL_VERSION);
    586  1.54       dbj 			appleufsdirty();
    587  1.54       dbj 		}
    588  1.54       dbj 	}
    589  1.54       dbj 
    590  1.54       dbj 	if (ntohs(appleufs->ul_namelen) > APPLEUFS_MAX_LABEL_NAME) {
    591  1.54       dbj 		pwarn("APPLE UFS LABEL NAME TOO LONG");
    592  1.54       dbj 		if (preen) {
    593  1.54       dbj 			printf(" (TRUNCATED)\n");
    594  1.54       dbj 		}
    595  1.54       dbj 		if (preen || reply("TRUNCATE")) {
    596  1.54       dbj 			appleufs->ul_namelen = htons(APPLEUFS_MAX_LABEL_NAME);
    597  1.54       dbj 			appleufsdirty();
    598  1.54       dbj 		}
    599  1.54       dbj 	}
    600  1.54       dbj 
    601  1.54       dbj 	if (ntohs(appleufs->ul_namelen) == 0) {
    602  1.54       dbj 		pwarn("MISSING APPLE UFS LABEL NAME");
    603  1.54       dbj 		if (preen) {
    604  1.54       dbj 			printf(" (FIXED)\n");
    605  1.54       dbj 		}
    606  1.54       dbj 		if (preen || reply("FIX")) {
    607  1.64       dbj 			ffs_appleufs_set(appleufs, NULL, -1, 0);
    608  1.54       dbj 			appleufsdirty();
    609  1.54       dbj 		}
    610  1.54       dbj 	}
    611  1.54       dbj 
    612  1.54       dbj 	/* Scan name for first illegal character */
    613  1.54       dbj 	for (i=0;i<ntohs(appleufs->ul_namelen);i++) {
    614  1.54       dbj 		if ((appleufs->ul_name[i] == '\0') ||
    615  1.54       dbj 			(appleufs->ul_name[i] == ':') ||
    616  1.54       dbj 			(appleufs->ul_name[i] == '/')) {
    617  1.54       dbj 			pwarn("APPLE UFS LABEL NAME CONTAINS ILLEGAL CHARACTER");
    618  1.54       dbj 			if (preen) {
    619  1.54       dbj 				printf(" (TRUNCATED)\n");
    620  1.54       dbj 			}
    621  1.54       dbj 			if (preen || reply("TRUNCATE")) {
    622  1.54       dbj 				appleufs->ul_namelen = i+1;
    623  1.54       dbj 				appleufsdirty();
    624  1.54       dbj 			}
    625  1.54       dbj 			break;
    626  1.54       dbj 		}
    627  1.54       dbj 	}
    628  1.54       dbj 
    629  1.54       dbj 	/* Check the checksum last, because if anything else was wrong,
    630  1.54       dbj 	 * then the checksum gets reset anyway.
    631  1.54       dbj 	 */
    632  1.54       dbj 	appleufs->ul_checksum = 0;
    633  1.54       dbj 	appleufs->ul_checksum = ffs_appleufs_cksum(appleufs);
    634  1.54       dbj 	if (appleufsblk.b_un.b_appleufs->ul_checksum != appleufs->ul_checksum) {
    635  1.54       dbj 		pwarn("INVALID APPLE UFS CHECKSUM (%#04x should be %#04x)",
    636  1.54       dbj 			appleufsblk.b_un.b_appleufs->ul_checksum, appleufs->ul_checksum);
    637  1.54       dbj 		if (preen) {
    638  1.54       dbj 			printf(" (CORRECTED)\n");
    639  1.54       dbj 		}
    640  1.54       dbj 		if (preen || reply("CORRECT")) {
    641  1.54       dbj 			appleufsdirty();
    642  1.54       dbj 		} else {
    643  1.54       dbj 			/* put the incorrect checksum back in place */
    644  1.54       dbj 			appleufs->ul_checksum = appleufsblk.b_un.b_appleufs->ul_checksum;
    645  1.54       dbj 		}
    646  1.54       dbj 	}
    647  1.54       dbj 	return 1;
    648  1.54       dbj }
    649  1.54       dbj 
    650   1.1       cgd /*
    651  1.58      fvdl  * Detect byte order. Return 0 if valid magic found, -1 otherwise.
    652   1.1       cgd  */
    653  1.26  christos static int
    654  1.70       dsl detect_byteorder(struct fs *fs, int sblockoff)
    655   1.1       cgd {
    656  1.70       dsl 	if (sblockoff == SBLOCK_UFS2 && (fs->fs_magic == FS_UFS1_MAGIC ||
    657  1.70       dsl 	    fs->fs_magic == bswap32(FS_UFS1_MAGIC)))
    658  1.70       dsl 		/* Likely to be the first alternate of a fs with 64k blocks */
    659  1.70       dsl 		return -1;
    660  1.58      fvdl 	if (fs->fs_magic == FS_UFS1_MAGIC || fs->fs_magic == FS_UFS2_MAGIC) {
    661  1.58      fvdl 		if (endian == 0 || BYTE_ORDER == endian) {
    662  1.58      fvdl 			needswap = 0;
    663  1.58      fvdl 			doswap = do_blkswap = do_dirswap = 0;
    664  1.58      fvdl 		} else {
    665  1.58      fvdl 			needswap = 1;
    666  1.58      fvdl 			doswap = do_blkswap = do_dirswap = 1;
    667  1.58      fvdl 		}
    668  1.58      fvdl 		return 0;
    669  1.61     enami 	} else if (fs->fs_magic == bswap32(FS_UFS1_MAGIC) ||
    670  1.58      fvdl 		   fs->fs_magic == bswap32(FS_UFS2_MAGIC)) {
    671  1.33    bouyer 		if (endian == 0 || BYTE_ORDER != endian) {
    672  1.33    bouyer 			needswap = 1;
    673  1.33    bouyer 			doswap = do_blkswap = do_dirswap = 0;
    674  1.33    bouyer 		} else {
    675  1.33    bouyer 			needswap = 0;
    676  1.33    bouyer 			doswap = do_blkswap = do_dirswap = 1;
    677  1.33    bouyer 		}
    678  1.58      fvdl 		return 0;
    679  1.58      fvdl 	}
    680  1.58      fvdl 	return -1;
    681  1.58      fvdl }
    682  1.58      fvdl 
    683  1.58      fvdl /*
    684  1.58      fvdl  * Possible superblock locations ordered from most to least likely.
    685  1.58      fvdl  */
    686  1.58      fvdl static off_t sblock_try[] = SBLOCKSEARCH;
    687  1.58      fvdl 
    688  1.58      fvdl /*
    689  1.58      fvdl  * Read in the super block and its summary info.
    690  1.58      fvdl  */
    691  1.58      fvdl static int
    692  1.75   xtraeme readsb(int listerr)
    693  1.58      fvdl {
    694  1.76     lukem 	daddr_t super = 0;
    695  1.58      fvdl 	struct fs *fs;
    696  1.58      fvdl 	int i;
    697  1.58      fvdl 
    698  1.58      fvdl 	if (bflag) {
    699  1.58      fvdl 		super = bflag;
    700  1.58      fvdl 		if (bread(fsreadfd, (char *)sblk.b_un.b_fs, super,
    701  1.58      fvdl 		    (long)SBLOCKSIZE) != 0)
    702  1.58      fvdl 			return (0);
    703  1.58      fvdl 		fs = sblk.b_un.b_fs;
    704  1.70       dsl 		if (detect_byteorder(fs, -1) < 0) {
    705  1.58      fvdl 			badsb(listerr, "MAGIC NUMBER WRONG");
    706  1.58      fvdl 			return (0);
    707  1.58      fvdl 		}
    708  1.33    bouyer 	} else {
    709  1.58      fvdl 		for (i = 0; sblock_try[i] != -1; i++) {
    710  1.58      fvdl 			super = sblock_try[i] / dev_bsize;
    711  1.58      fvdl 			if (bread(fsreadfd, (char *)sblk.b_un.b_fs,
    712  1.58      fvdl 			    super, (long)SBLOCKSIZE) != 0)
    713  1.58      fvdl 				continue;
    714  1.58      fvdl 			fs = sblk.b_un.b_fs;
    715  1.70       dsl 			if (detect_byteorder(fs, sblock_try[i]) == 0)
    716  1.58      fvdl 				break;
    717  1.58      fvdl 		}
    718  1.58      fvdl 		if (sblock_try[i] == -1) {
    719  1.58      fvdl 			badsb(listerr, "CAN'T FIND SUPERBLOCK");
    720  1.58      fvdl 			return (0);
    721  1.58      fvdl 		}
    722  1.33    bouyer 	}
    723  1.33    bouyer 	if (doswap) {
    724  1.33    bouyer 		if (preen)
    725  1.82  christos 			errx(FSCK_EXIT_USAGE,
    726  1.82  christos 			    "Incompatible options -B and -p");
    727  1.33    bouyer 		if (nflag)
    728  1.82  christos 			errx(FSCK_EXIT_USAGE,
    729  1.82  christos 			    "Incompatible options -B and -n");
    730  1.33    bouyer 		if (endian == LITTLE_ENDIAN) {
    731  1.36        is 			if (!reply("CONVERT TO LITTLE ENDIAN"))
    732  1.33    bouyer 				return 0;
    733  1.33    bouyer 		} else if (endian == BIG_ENDIAN) {
    734  1.36        is 			if (!reply("CONVERT TO BIG ENDIAN"))
    735  1.33    bouyer 				return 0;
    736  1.33    bouyer 		} else
    737  1.33    bouyer 			pfatal("INTERNAL ERROR: unknown endian");
    738  1.33    bouyer 	}
    739  1.33    bouyer 	if (needswap)
    740  1.63       dsl 		pwarn("** Swapped byte order\n");
    741  1.33    bouyer 	/* swap SB byte order if asked */
    742  1.33    bouyer 	if (doswap)
    743  1.45     lukem 		ffs_sb_swap(sblk.b_un.b_fs, sblk.b_un.b_fs);
    744  1.33    bouyer 
    745  1.58      fvdl 	memmove(sblock, sblk.b_un.b_fs, SBLOCKSIZE);
    746  1.33    bouyer 	if (needswap)
    747  1.45     lukem 		ffs_sb_swap(sblk.b_un.b_fs, sblock);
    748  1.33    bouyer 
    749  1.58      fvdl 	is_ufs2 = sblock->fs_magic == FS_UFS2_MAGIC;
    750  1.58      fvdl 
    751   1.1       cgd 	/*
    752   1.1       cgd 	 * run a few consistency checks of the super block
    753   1.1       cgd 	 */
    754  1.58      fvdl 	if (sblock->fs_sbsize > SBLOCKSIZE)
    755   1.1       cgd 		{ badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
    756   1.1       cgd 	/*
    757   1.1       cgd 	 * Compute block size that the filesystem is based on,
    758   1.1       cgd 	 * according to fsbtodb, and adjust superblock block number
    759   1.1       cgd 	 * so we can tell if this is an alternate later.
    760   1.1       cgd 	 */
    761   1.1       cgd 	super *= dev_bsize;
    762  1.33    bouyer 	dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
    763   1.1       cgd 	sblk.b_bno = super / dev_bsize;
    764  1.58      fvdl 	sblk.b_size = SBLOCKSIZE;
    765  1.58      fvdl 	if (bflag)
    766  1.58      fvdl 		goto out;
    767   1.1       cgd 	/*
    768  1.58      fvdl 	 * Set all possible fields that could differ, then do check
    769  1.58      fvdl 	 * of whole super block against an alternate super block->
    770   1.1       cgd 	 * When an alternate super-block is specified this check is skipped.
    771   1.1       cgd 	 */
    772  1.33    bouyer 	getblk(&asblk, cgsblock(sblock, sblock->fs_ncg - 1), sblock->fs_sbsize);
    773   1.1       cgd 	if (asblk.b_errs)
    774   1.1       cgd 		return (0);
    775  1.33    bouyer 	/* swap SB byte order if asked */
    776  1.33    bouyer 	if (doswap)
    777  1.45     lukem 		ffs_sb_swap(asblk.b_un.b_fs, asblk.b_un.b_fs);
    778  1.33    bouyer 
    779  1.33    bouyer 	memmove(altsblock, asblk.b_un.b_fs, sblock->fs_sbsize);
    780  1.33    bouyer 	if (needswap)
    781  1.45     lukem 		ffs_sb_swap(asblk.b_un.b_fs, altsblock);
    782  1.41   thorpej 	if (cmpsblks(sblock, altsblock)) {
    783  1.65       dbj 		if (debug) {
    784  1.65       dbj 			uint32_t *nlp, *olp, *endlp;
    785  1.65       dbj 
    786  1.65       dbj 			printf("superblock mismatches\n");
    787  1.65       dbj 			nlp = (uint32_t *)altsblock;
    788  1.65       dbj 			olp = (uint32_t *)sblock;
    789  1.65       dbj 			endlp = olp + (sblock->fs_sbsize / sizeof *olp);
    790  1.65       dbj 			for ( ; olp < endlp; olp++, nlp++) {
    791  1.65       dbj 				if (*olp == *nlp)
    792  1.65       dbj 					continue;
    793  1.71       dbj 				printf("offset %#x, original 0x%08x, alternate "
    794  1.67       mrg 				       "0x%08x\n",
    795  1.67       mrg 				    (int)((uint8_t *)olp-(uint8_t *)sblock),
    796  1.67       mrg 				    *olp, *nlp);
    797  1.65       dbj 			}
    798  1.65       dbj 		}
    799   1.1       cgd 		badsb(listerr,
    800   1.1       cgd 		"VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
    801  1.80  christos /*
    802   1.1       cgd 		return (0);
    803  1.80  christos */
    804   1.1       cgd 	}
    805  1.58      fvdl out:
    806  1.65       dbj 
    807  1.65       dbj 	sb_oldfscompat_read(sblock, &sblocksave);
    808  1.58      fvdl 
    809  1.33    bouyer 	/* Now we know the SB is valid, we can write it back if needed */
    810  1.33    bouyer 	if (doswap) {
    811  1.33    bouyer 		sbdirty();
    812  1.33    bouyer 		dirty(&asblk);
    813  1.33    bouyer 	}
    814   1.1       cgd 	havesb = 1;
    815   1.1       cgd 	return (1);
    816  1.41   thorpej }
    817  1.41   thorpej 
    818  1.41   thorpej int
    819  1.41   thorpej cmpsblks(const struct fs *sb, struct fs *asb)
    820  1.41   thorpej {
    821  1.65       dbj 	if (!is_ufs2 && ((sb->fs_old_flags & FS_FLAGS_UPDATED) == 0)) {
    822  1.65       dbj 		if (sb->fs_old_postblformat < FS_DYNAMICPOSTBLFMT)
    823  1.65       dbj 			return cmpsblks42(sb, asb);
    824  1.65       dbj 		else
    825  1.65       dbj 			return cmpsblks44(sb, asb);
    826  1.65       dbj 	}
    827  1.65       dbj 	if (asb->fs_sblkno != sb->fs_sblkno ||
    828  1.57      fvdl 	    asb->fs_cblkno != sb->fs_cblkno ||
    829  1.57      fvdl 	    asb->fs_iblkno != sb->fs_iblkno ||
    830  1.57      fvdl 	    asb->fs_dblkno != sb->fs_dblkno ||
    831  1.57      fvdl 	    asb->fs_ncg != sb->fs_ncg ||
    832  1.57      fvdl 	    asb->fs_bsize != sb->fs_bsize ||
    833  1.57      fvdl 	    asb->fs_fsize != sb->fs_fsize ||
    834  1.57      fvdl 	    asb->fs_frag != sb->fs_frag ||
    835  1.57      fvdl 	    asb->fs_bmask != sb->fs_bmask ||
    836  1.57      fvdl 	    asb->fs_fmask != sb->fs_fmask ||
    837  1.57      fvdl 	    asb->fs_bshift != sb->fs_bshift ||
    838  1.57      fvdl 	    asb->fs_fshift != sb->fs_fshift ||
    839  1.57      fvdl 	    asb->fs_fragshift != sb->fs_fragshift ||
    840  1.57      fvdl 	    asb->fs_fsbtodb != sb->fs_fsbtodb ||
    841  1.57      fvdl 	    asb->fs_sbsize != sb->fs_sbsize ||
    842  1.57      fvdl 	    asb->fs_nindir != sb->fs_nindir ||
    843  1.57      fvdl 	    asb->fs_inopb != sb->fs_inopb ||
    844  1.57      fvdl 	    asb->fs_cssize != sb->fs_cssize ||
    845  1.57      fvdl 	    asb->fs_ipg != sb->fs_ipg ||
    846  1.57      fvdl 	    asb->fs_fpg != sb->fs_fpg ||
    847  1.57      fvdl 	    asb->fs_magic != sb->fs_magic)
    848  1.58      fvdl 		return 1;
    849  1.58      fvdl 	return 0;
    850   1.1       cgd }
    851   1.1       cgd 
    852  1.65       dbj /* BSD 4.2 performed the following superblock comparison
    853  1.65       dbj  * It should correspond to FS_42POSTBLFMT
    854  1.65       dbj  * (although note that in 4.2, the fs_old_postblformat
    855  1.65       dbj  * field didn't exist and the corresponding bits are
    856  1.65       dbj  * located near the end of the postbl itself, where they
    857  1.65       dbj  * are not likely to be used.)
    858  1.65       dbj  */
    859  1.65       dbj int
    860  1.65       dbj cmpsblks42(const struct fs *sb, struct fs *asb)
    861  1.65       dbj {
    862  1.65       dbj 	asb->fs_firstfield = sb->fs_firstfield; /* fs_link */
    863  1.65       dbj 	asb->fs_unused_1 = sb->fs_unused_1; /* fs_rlink */
    864  1.65       dbj 	asb->fs_old_time = sb->fs_old_time; /* fs_time */
    865  1.65       dbj 	asb->fs_old_cstotal = sb->fs_old_cstotal; /* fs_cstotal */
    866  1.65       dbj 	asb->fs_cgrotor = sb->fs_cgrotor;
    867  1.65       dbj 	asb->fs_fmod = sb->fs_fmod;
    868  1.65       dbj 	asb->fs_clean = sb->fs_clean;
    869  1.65       dbj 	asb->fs_ronly = sb->fs_ronly;
    870  1.65       dbj 	asb->fs_old_flags = sb->fs_old_flags;
    871  1.65       dbj 	asb->fs_maxcontig = sb->fs_maxcontig;
    872  1.65       dbj 	asb->fs_minfree = sb->fs_minfree;
    873  1.65       dbj 	asb->fs_old_rotdelay = sb->fs_old_rotdelay;
    874  1.65       dbj 	asb->fs_maxbpg = sb->fs_maxbpg;
    875  1.65       dbj 
    876  1.65       dbj 	/* The former fs_csp, totaling 128 bytes  */
    877  1.65       dbj 	memmove(asb->fs_ocsp, sb->fs_ocsp, sizeof sb->fs_ocsp);
    878  1.65       dbj 	asb->fs_contigdirs = sb->fs_contigdirs;
    879  1.65       dbj 	asb->fs_csp = sb->fs_csp;
    880  1.65       dbj 	asb->fs_maxcluster = sb->fs_maxcluster;
    881  1.65       dbj 	asb->fs_active = sb->fs_active;
    882  1.65       dbj 
    883  1.65       dbj 	/* The former fs_fsmnt, totaling 512 bytes */
    884  1.65       dbj 	memmove(asb->fs_fsmnt, sb->fs_fsmnt, sizeof sb->fs_fsmnt);
    885  1.65       dbj 	memmove(asb->fs_volname, sb->fs_volname, sizeof sb->fs_volname);
    886  1.65       dbj 
    887  1.65       dbj 	return memcmp(sb, asb, sb->fs_sbsize);
    888  1.65       dbj }
    889  1.65       dbj 
    890  1.65       dbj /* BSD 4.4 performed the following superblock comparison
    891  1.65       dbj  * This was used in NetBSD through 1.6.1
    892  1.65       dbj  *
    893  1.65       dbj  * Note that this implementation is destructive to asb.
    894  1.65       dbj  */
    895  1.65       dbj int
    896  1.65       dbj cmpsblks44(const struct fs *sb, struct fs *asb)
    897  1.65       dbj {
    898  1.65       dbj 	/*
    899  1.65       dbj 	 * "Copy fields which we don't care if they're different in the
    900  1.65       dbj 	 * alternate superblocks, as they're either likely to be
    901  1.65       dbj 	 * different because they're per-cylinder-group specific, or
    902  1.65       dbj 	 * because they're transient details which are only maintained
    903  1.65       dbj 	 * in the primary superblock."
    904  1.65       dbj 	 */
    905  1.65       dbj 	asb->fs_firstfield = sb->fs_firstfield;
    906  1.65       dbj 	asb->fs_unused_1 = sb->fs_unused_1;
    907  1.65       dbj 	asb->fs_old_time = sb->fs_old_time;
    908  1.65       dbj 	asb->fs_old_cstotal = sb->fs_old_cstotal;
    909  1.65       dbj 	asb->fs_cgrotor = sb->fs_cgrotor;
    910  1.65       dbj 	asb->fs_fmod = sb->fs_fmod;
    911  1.65       dbj 	asb->fs_clean = sb->fs_clean;
    912  1.65       dbj 	asb->fs_ronly = sb->fs_ronly;
    913  1.65       dbj 	asb->fs_old_flags = sb->fs_old_flags;
    914  1.65       dbj 	asb->fs_maxcontig = sb->fs_maxcontig;
    915  1.65       dbj 	asb->fs_minfree = sb->fs_minfree;
    916  1.65       dbj 	asb->fs_optim = sb->fs_optim;
    917  1.65       dbj 	asb->fs_old_rotdelay = sb->fs_old_rotdelay;
    918  1.65       dbj 	asb->fs_maxbpg = sb->fs_maxbpg;
    919  1.65       dbj 
    920  1.65       dbj 	/* The former fs_csp and fs_maxcluster, totaling 128 bytes */
    921  1.65       dbj 	memmove(asb->fs_ocsp, sb->fs_ocsp, sizeof sb->fs_ocsp);
    922  1.65       dbj 	asb->fs_contigdirs = sb->fs_contigdirs;
    923  1.65       dbj 	asb->fs_csp = sb->fs_csp;
    924  1.65       dbj 	asb->fs_maxcluster = sb->fs_maxcluster;
    925  1.65       dbj 	asb->fs_active = sb->fs_active;
    926  1.65       dbj 
    927  1.65       dbj 	/* The former fs_fsmnt, totaling 512 bytes */
    928  1.65       dbj 	memmove(asb->fs_fsmnt, sb->fs_fsmnt, sizeof sb->fs_fsmnt);
    929  1.65       dbj 	memmove(asb->fs_volname, sb->fs_volname, sizeof sb->fs_volname);
    930  1.65       dbj 
    931  1.65       dbj 	/* The former fs_sparecon, totaling 200 bytes */
    932  1.65       dbj 	memmove(asb->fs_snapinum,
    933  1.65       dbj 		sb->fs_snapinum, sizeof sb->fs_snapinum);
    934  1.65       dbj 	asb->fs_avgfilesize = sb->fs_avgfilesize;
    935  1.65       dbj 	asb->fs_avgfpdir = sb->fs_avgfpdir;
    936  1.65       dbj 	asb->fs_save_cgsize = sb->fs_save_cgsize;
    937  1.65       dbj 	memmove(asb->fs_sparecon32,
    938  1.65       dbj 		sb->fs_sparecon32, sizeof sb->fs_sparecon32);
    939  1.65       dbj 	asb->fs_flags = sb->fs_flags;
    940  1.65       dbj 
    941  1.65       dbj 	/* Original comment:
    942  1.65       dbj 	 * "The following should not have to be copied, but need to be."
    943  1.65       dbj 	 */
    944  1.65       dbj 	asb->fs_fsbtodb = sb->fs_fsbtodb;
    945  1.65       dbj 	asb->fs_old_interleave = sb->fs_old_interleave;
    946  1.65       dbj 	asb->fs_old_npsect = sb->fs_old_npsect;
    947  1.65       dbj 	asb->fs_old_nrpos = sb->fs_old_nrpos;
    948  1.65       dbj 	asb->fs_state = sb->fs_state;
    949  1.65       dbj 	asb->fs_qbmask = sb->fs_qbmask;
    950  1.65       dbj 	asb->fs_qfmask = sb->fs_qfmask;
    951  1.65       dbj 	asb->fs_state = sb->fs_state;
    952  1.65       dbj 	asb->fs_maxfilesize = sb->fs_maxfilesize;
    953  1.65       dbj 
    954  1.65       dbj 	/*
    955  1.65       dbj 	 * "Compare the superblocks, effectively checking every other
    956  1.65       dbj 	 * field to see if they differ."
    957  1.65       dbj 	 */
    958  1.65       dbj 	return memcmp(sb, asb, sb->fs_sbsize);
    959  1.65       dbj }
    960  1.65       dbj 
    961  1.65       dbj 
    962  1.30     lukem static void
    963  1.78  christos badsb(int listerr, const char *s)
    964   1.1       cgd {
    965   1.1       cgd 
    966   1.1       cgd 	if (!listerr)
    967   1.1       cgd 		return;
    968   1.1       cgd 	if (preen)
    969  1.26  christos 		printf("%s: ", cdevname());
    970   1.1       cgd 	pfatal("BAD SUPER BLOCK: %s\n", s);
    971   1.1       cgd }
    972   1.1       cgd 
    973   1.1       cgd /*
    974   1.1       cgd  * Calculate a prototype superblock based on information in the disk label.
    975   1.1       cgd  * When done the cgsblock macro can be calculated and the fs_ncg field
    976   1.1       cgd  * can be used. Do NOT attempt to use other macros without verifying that
    977   1.1       cgd  * their needed information is available!
    978   1.1       cgd  */
    979  1.30     lukem static int
    980  1.75   xtraeme calcsb(const char *dev, int devfd, struct fs *fs)
    981   1.1       cgd {
    982  1.80  christos 	struct dkwedge_info dkw;
    983  1.80  christos 	struct disk_geom geo;
    984  1.58      fvdl 	int i, nspf;
    985   1.1       cgd 
    986  1.80  christos 	if (getdiskinfo(dev, fsreadfd, NULL, &geo, &dkw) == -1)
    987  1.80  christos 		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
    988  1.80  christos 	if (dkw.dkw_parent[0] == '\0') {
    989   1.1       cgd 		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
    990   1.1       cgd 		return (0);
    991   1.1       cgd 	}
    992  1.80  christos 	if (strcmp(dkw.dkw_ptype, DKW_PTYPE_FFS) &&
    993  1.80  christos 	    strcmp(dkw.dkw_ptype, DKW_PTYPE_APPLEUFS)) {
    994   1.1       cgd 		pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
    995  1.80  christos 		    dev, dkw.dkw_ptype);
    996  1.33    bouyer 		return (0);
    997  1.69       dbj 	}
    998  1.81  christos 	if (geo.dg_secsize == 0) {
    999  1.81  christos 		pfatal("%s: CANNOT FIGURE OUT SECTOR SIZE\n", dev);
   1000  1.81  christos 		return 0;
   1001  1.81  christos 	}
   1002  1.81  christos 	if (geo.dg_secpercyl == 0) {
   1003  1.81  christos 		pfatal("%s: CANNOT FIGURE OUT SECTORS PER CYLINDER\n", dev);
   1004  1.81  christos 		return 0;
   1005  1.81  christos 	}
   1006  1.81  christos 	if (sblk.b_un.b_fs->fs_fsize == 0) {
   1007  1.81  christos 		pfatal("%s: CANNOT FIGURE OUT FRAG BLOCK SIZE\n", dev);
   1008  1.81  christos 		return 0;
   1009  1.81  christos 	}
   1010  1.81  christos 	if (sblk.b_un.b_fs->fs_fpg == 0) {
   1011  1.81  christos 		pfatal("%s: CANNOT FIGURE OUT FRAGS PER GROUP\n", dev);
   1012  1.81  christos 		return 0;
   1013  1.81  christos 	}
   1014  1.81  christos 	if (sblk.b_un.b_fs->fs_old_cpg == 0) {
   1015  1.81  christos 		pfatal("%s: CANNOT FIGURE OUT OLD CYLINDERS PER GROUP\n", dev);
   1016  1.81  christos 		return 0;
   1017  1.81  christos 	}
   1018  1.85  christos 	memcpy(fs, sblk.b_un.b_fs, sizeof(struct fs));
   1019  1.80  christos 	nspf = fs->fs_fsize / geo.dg_secsize;
   1020  1.58      fvdl 	fs->fs_old_nspf = nspf;
   1021  1.58      fvdl 	for (fs->fs_fsbtodb = 0, i = nspf; i > 1; i >>= 1)
   1022   1.1       cgd 		fs->fs_fsbtodb++;
   1023  1.80  christos 	dev_bsize = geo.dg_secsize;
   1024  1.58      fvdl 	if (fs->fs_magic == FS_UFS2_MAGIC) {
   1025  1.58      fvdl 		fs->fs_ncg = howmany(fs->fs_size, fs->fs_fpg);
   1026  1.58      fvdl 	} else /* if (fs->fs_magic == FS_UFS1_MAGIC) */ {
   1027  1.58      fvdl 		fs->fs_old_cgmask = 0xffffffff;
   1028  1.80  christos 		for (i = geo.dg_ntracks; i > 1; i >>= 1)
   1029  1.58      fvdl 			fs->fs_old_cgmask <<= 1;
   1030  1.80  christos 		if (!POWEROF2(geo.dg_ntracks))
   1031  1.58      fvdl 			fs->fs_old_cgmask <<= 1;
   1032  1.58      fvdl 		fs->fs_old_cgoffset = roundup(
   1033  1.80  christos 			howmany(geo.dg_nsectors, nspf), fs->fs_frag);
   1034  1.80  christos 		fs->fs_fpg = (fs->fs_old_cpg * geo.dg_secpercyl) / nspf;
   1035  1.80  christos 		fs->fs_ncg = howmany(fs->fs_size / geo.dg_secpercyl,
   1036  1.58      fvdl 		    fs->fs_old_cpg);
   1037  1.58      fvdl 	}
   1038   1.1       cgd 	return (1);
   1039   1.1       cgd }
   1040