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