Home | History | Annotate | Line # | Download | only in fsck_ffs
setup.c revision 1.62
      1  1.62       agc /*	$NetBSD: setup.c,v 1.62 2003/08/07 10:04:21 agc 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.62       agc __RCSID("$NetBSD: setup.c,v 1.62 2003/08/07 10:04:21 agc 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.35  christos #define FSTYPENAMES
     46   1.1       cgd #include <sys/disklabel.h>
     47   1.1       cgd #include <sys/file.h>
     48  1.15       cgd 
     49  1.30     lukem #include <ufs/ufs/dinode.h>
     50  1.54       dbj #include <ufs/ufs/dir.h>
     51  1.33    bouyer #include <ufs/ufs/ufs_bswap.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.1       cgd 
     66   1.1       cgd #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
     67   1.1       cgd 
     68  1.30     lukem static void badsb __P((int, char *));
     69  1.34   mycroft static int calcsb __P((const char *, int, struct fs *));
     70  1.34   mycroft static struct disklabel *getdisklabel __P((const char *, int));
     71  1.54       dbj static struct partition *getdisklabelpart __P((const char *, struct disklabel *));
     72  1.26  christos static int readsb __P((int));
     73  1.54       dbj static int readappleufs __P((void));
     74   1.1       cgd 
     75  1.30     lukem /*
     76  1.30     lukem  * Read in a superblock finding an alternate if necessary.
     77  1.30     lukem  * Return 1 if successful, 0 if unsuccessful, -1 if filesystem
     78  1.30     lukem  * is already clean (preen mode only).
     79  1.30     lukem  */
     80  1.15       cgd int
     81   1.1       cgd setup(dev)
     82  1.34   mycroft 	const char *dev;
     83   1.1       cgd {
     84   1.1       cgd 	long cg, size, asked, i, j;
     85   1.1       cgd 	long bmapsize;
     86   1.1       cgd 	struct disklabel *lp;
     87  1.10   mycroft 	off_t sizepb;
     88  1.10   mycroft 	struct stat statb;
     89   1.1       cgd 	struct fs proto;
     90  1.21   mycroft 	int doskipclean;
     91  1.24   mycroft 	u_int64_t maxfilesize;
     92  1.46     lukem 	struct csum *ccsp;
     93   1.1       cgd 
     94   1.1       cgd 	havesb = 0;
     95  1.10   mycroft 	fswritefd = -1;
     96  1.21   mycroft 	doskipclean = skipclean;
     97   1.1       cgd 	if (stat(dev, &statb) < 0) {
     98   1.1       cgd 		printf("Can't stat %s: %s\n", dev, strerror(errno));
     99   1.1       cgd 		return (0);
    100   1.1       cgd 	}
    101  1.51     lukem 	if (!forceimage && !S_ISCHR(statb.st_mode)) {
    102   1.1       cgd 		pfatal("%s is not a character device", dev);
    103   1.1       cgd 		if (reply("CONTINUE") == 0)
    104   1.1       cgd 			return (0);
    105   1.1       cgd 	}
    106   1.1       cgd 	if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
    107   1.1       cgd 		printf("Can't open %s: %s\n", dev, strerror(errno));
    108   1.1       cgd 		return (0);
    109   1.1       cgd 	}
    110   1.1       cgd 	if (preen == 0)
    111   1.1       cgd 		printf("** %s", dev);
    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.1       cgd 		printf(" (NO WRITE)");
    117   1.1       cgd 	}
    118   1.1       cgd 	if (preen == 0)
    119   1.1       cgd 		printf("\n");
    120   1.1       cgd 	fsmodified = 0;
    121   1.1       cgd 	lfdir = 0;
    122   1.1       cgd 	initbarea(&sblk);
    123   1.1       cgd 	initbarea(&asblk);
    124  1.58      fvdl 	sblk.b_un.b_buf = malloc(SBLOCKSIZE);
    125  1.58      fvdl 	sblock = malloc(SBLOCKSIZE);
    126  1.58      fvdl 	asblk.b_un.b_buf = malloc(SBLOCKSIZE);
    127  1.58      fvdl 	altsblock = malloc(SBLOCKSIZE);
    128  1.33    bouyer 	if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL ||
    129  1.33    bouyer 		sblock == NULL || altsblock == NULL)
    130  1.30     lukem 		errx(EEXIT, "cannot allocate space for superblock");
    131  1.51     lukem 	if (!forceimage && (lp = getdisklabel(NULL, fsreadfd)) != NULL)
    132   1.1       cgd 		dev_bsize = secsize = lp->d_secsize;
    133   1.1       cgd 	else
    134   1.1       cgd 		dev_bsize = secsize = DEV_BSIZE;
    135   1.1       cgd 	/*
    136   1.1       cgd 	 * Read in the superblock, looking for alternates if necessary
    137   1.1       cgd 	 */
    138   1.1       cgd 	if (readsb(1) == 0) {
    139  1.51     lukem 		if (bflag || preen || forceimage ||
    140  1.51     lukem 		    calcsb(dev, fsreadfd, &proto) == 0)
    141   1.1       cgd 			return(0);
    142   1.1       cgd 		if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
    143   1.1       cgd 			return (0);
    144   1.1       cgd 		for (cg = 0; cg < proto.fs_ncg; cg++) {
    145   1.1       cgd 			bflag = fsbtodb(&proto, cgsblock(&proto, cg));
    146   1.1       cgd 			if (readsb(0) != 0)
    147   1.1       cgd 				break;
    148   1.1       cgd 		}
    149   1.1       cgd 		if (cg >= proto.fs_ncg) {
    150   1.1       cgd 			printf("%s %s\n%s %s\n%s %s\n",
    151   1.1       cgd 				"SEARCH FOR ALTERNATE SUPER-BLOCK",
    152   1.1       cgd 				"FAILED. YOU MUST USE THE",
    153  1.43   hubertf 				"-b OPTION TO fsck_ffs TO SPECIFY THE",
    154   1.1       cgd 				"LOCATION OF AN ALTERNATE",
    155   1.1       cgd 				"SUPER-BLOCK TO SUPPLY NEEDED",
    156  1.23       cgd 				"INFORMATION; SEE fsck_ffs(8).");
    157   1.1       cgd 			return(0);
    158   1.1       cgd 		}
    159  1.21   mycroft 		doskipclean = 0;
    160   1.1       cgd 		pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
    161   1.1       cgd 	}
    162  1.21   mycroft 	if (debug)
    163  1.33    bouyer 		printf("clean = %d\n", sblock->fs_clean);
    164  1.33    bouyer 	if (doswap)
    165  1.33    bouyer 		doskipclean = 0;
    166  1.59      fvdl 	if (sblock->fs_old_postblformat == FS_42POSTBLFMT) {
    167  1.59      fvdl 		pwarn("%sile system is in 4.2BSD format, check skipped\n",
    168  1.59      fvdl 		    preen ? "f" : "** F");
    169  1.59      fvdl 		return -1;
    170  1.59      fvdl 	}
    171  1.33    bouyer 	if (sblock->fs_clean & FS_ISCLEAN) {
    172  1.22       cgd 		if (doskipclean) {
    173  1.22       cgd 			pwarn("%sile system is clean; not checking\n",
    174  1.22       cgd 			    preen ? "f" : "** F");
    175  1.21   mycroft 			return (-1);
    176  1.21   mycroft 		}
    177  1.33    bouyer 		if (!preen && !doswap)
    178  1.21   mycroft 			pwarn("** File system is already clean\n");
    179  1.21   mycroft 	}
    180  1.33    bouyer 	maxfsblock = sblock->fs_size;
    181  1.33    bouyer 	maxino = sblock->fs_ncg * sblock->fs_ipg;
    182  1.33    bouyer 	sizepb = sblock->fs_bsize;
    183  1.33    bouyer 	maxfilesize = sblock->fs_bsize * NDADDR - 1;
    184  1.24   mycroft 	for (i = 0; i < NIADDR; i++) {
    185  1.33    bouyer 		sizepb *= NINDIR(sblock);
    186  1.24   mycroft 		maxfilesize += sizepb;
    187  1.24   mycroft 	}
    188   1.1       cgd 	/*
    189   1.1       cgd 	 * Check and potentially fix certain fields in the super block.
    190   1.1       cgd 	 */
    191  1.33    bouyer 	if (sblock->fs_optim != FS_OPTTIME && sblock->fs_optim != FS_OPTSPACE) {
    192   1.1       cgd 		pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
    193   1.1       cgd 		if (reply("SET TO DEFAULT") == 1) {
    194  1.33    bouyer 			sblock->fs_optim = FS_OPTTIME;
    195   1.1       cgd 			sbdirty();
    196   1.1       cgd 		}
    197   1.1       cgd 	}
    198  1.33    bouyer 	if ((sblock->fs_minfree < 0 || sblock->fs_minfree > 99)) {
    199   1.1       cgd 		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
    200  1.33    bouyer 			sblock->fs_minfree);
    201   1.1       cgd 		if (reply("SET TO DEFAULT") == 1) {
    202  1.33    bouyer 			sblock->fs_minfree = 10;
    203   1.1       cgd 			sbdirty();
    204   1.1       cgd 		}
    205   1.1       cgd 	}
    206  1.58      fvdl 	if (!is_ufs2 && sblock->fs_old_postblformat != FS_42POSTBLFMT &&
    207  1.58      fvdl 	    (sblock->fs_old_interleave < 1 ||
    208  1.58      fvdl 	    sblock->fs_old_interleave > sblock->fs_old_nsect)) {
    209   1.1       cgd 		pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
    210  1.58      fvdl 			sblock->fs_old_interleave);
    211  1.58      fvdl 		sblock->fs_old_interleave = 1;
    212   1.1       cgd 		if (preen)
    213   1.1       cgd 			printf(" (FIXED)\n");
    214   1.1       cgd 		if (preen || reply("SET TO DEFAULT") == 1) {
    215   1.1       cgd 			sbdirty();
    216   1.1       cgd 			dirty(&asblk);
    217   1.1       cgd 		}
    218   1.1       cgd 	}
    219  1.58      fvdl 	if (!is_ufs2 && sblock->fs_old_postblformat != FS_42POSTBLFMT &&
    220  1.58      fvdl 	    (sblock->fs_old_npsect < sblock->fs_old_nsect ||
    221  1.58      fvdl 	    sblock->fs_old_npsect > sblock->fs_old_nsect*2)) {
    222   1.1       cgd 		pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
    223  1.58      fvdl 			sblock->fs_old_npsect);
    224  1.58      fvdl 		sblock->fs_old_npsect = sblock->fs_old_nsect;
    225   1.1       cgd 		if (preen)
    226   1.1       cgd 			printf(" (FIXED)\n");
    227   1.1       cgd 		if (preen || reply("SET TO DEFAULT") == 1) {
    228   1.1       cgd 			sbdirty();
    229   1.1       cgd 			dirty(&asblk);
    230   1.1       cgd 		}
    231   1.1       cgd 	}
    232  1.33    bouyer 	if (sblock->fs_bmask != ~(sblock->fs_bsize - 1)) {
    233  1.28     lukem 		pwarn("INCORRECT BMASK=0x%x IN SUPERBLOCK",
    234  1.33    bouyer 			sblock->fs_bmask);
    235  1.33    bouyer 		sblock->fs_bmask = ~(sblock->fs_bsize - 1);
    236  1.24   mycroft 		if (preen)
    237  1.24   mycroft 			printf(" (FIXED)\n");
    238  1.24   mycroft 		if (preen || reply("FIX") == 1) {
    239  1.24   mycroft 			sbdirty();
    240  1.24   mycroft 			dirty(&asblk);
    241  1.24   mycroft 		}
    242  1.24   mycroft 	}
    243  1.33    bouyer 	if (sblock->fs_fmask != ~(sblock->fs_fsize - 1)) {
    244  1.28     lukem 		pwarn("INCORRECT FMASK=0x%x IN SUPERBLOCK",
    245  1.33    bouyer 			sblock->fs_fmask);
    246  1.33    bouyer 		sblock->fs_fmask = ~(sblock->fs_fsize - 1);
    247  1.24   mycroft 		if (preen)
    248  1.24   mycroft 			printf(" (FIXED)\n");
    249  1.24   mycroft 		if (preen || reply("FIX") == 1) {
    250  1.24   mycroft 			sbdirty();
    251  1.24   mycroft 			dirty(&asblk);
    252  1.24   mycroft 		}
    253  1.24   mycroft 	}
    254  1.58      fvdl 	if (sblock->fs_old_inodefmt >= FS_44INODEFMT) {
    255  1.33    bouyer 		if (sblock->fs_maxfilesize != maxfilesize) {
    256  1.38     lukem 			pwarn("INCORRECT MAXFILESIZE=%lld IN SUPERBLOCK",
    257  1.33    bouyer 			    (unsigned long long)sblock->fs_maxfilesize);
    258  1.33    bouyer 			sblock->fs_maxfilesize = maxfilesize;
    259  1.24   mycroft 			if (preen)
    260  1.24   mycroft 				printf(" (FIXED)\n");
    261  1.24   mycroft 			if (preen || reply("FIX") == 1) {
    262  1.24   mycroft 				sbdirty();
    263  1.24   mycroft 				dirty(&asblk);
    264  1.24   mycroft 			}
    265  1.24   mycroft 		}
    266  1.58      fvdl 		if ((is_ufs2 && sblock->fs_maxsymlinklen != MAXSYMLINKLEN_UFS2)
    267  1.58      fvdl 		    ||
    268  1.58      fvdl 		   (!is_ufs2 && sblock->fs_maxsymlinklen != MAXSYMLINKLEN_UFS1))
    269  1.58      fvdl 		    {
    270  1.24   mycroft 			pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK",
    271  1.33    bouyer 				sblock->fs_maxsymlinklen);
    272  1.58      fvdl 			sblock->fs_maxsymlinklen = is_ufs2 ?
    273  1.58      fvdl 			    MAXSYMLINKLEN_UFS2 : MAXSYMLINKLEN_UFS1;
    274  1.24   mycroft 			if (preen)
    275  1.24   mycroft 				printf(" (FIXED)\n");
    276  1.24   mycroft 			if (preen || reply("FIX") == 1) {
    277  1.24   mycroft 				sbdirty();
    278  1.24   mycroft 				dirty(&asblk);
    279  1.24   mycroft 			}
    280  1.24   mycroft 		}
    281  1.33    bouyer 		if (sblock->fs_qbmask != ~sblock->fs_bmask) {
    282  1.38     lukem 			pwarn("INCORRECT QBMASK=%llx IN SUPERBLOCK",
    283  1.33    bouyer 			    (unsigned long long)sblock->fs_qbmask);
    284  1.33    bouyer 			sblock->fs_qbmask = ~sblock->fs_bmask;
    285  1.24   mycroft 			if (preen)
    286  1.24   mycroft 				printf(" (FIXED)\n");
    287  1.24   mycroft 			if (preen || reply("FIX") == 1) {
    288  1.24   mycroft 				sbdirty();
    289  1.24   mycroft 				dirty(&asblk);
    290  1.24   mycroft 			}
    291  1.24   mycroft 		}
    292  1.33    bouyer 		if (sblock->fs_qfmask != ~sblock->fs_fmask) {
    293  1.38     lukem 			pwarn("INCORRECT QFMASK=%llx IN SUPERBLOCK",
    294  1.33    bouyer 			    (unsigned long long)sblock->fs_qfmask);
    295  1.33    bouyer 			sblock->fs_qfmask = ~sblock->fs_fmask;
    296  1.24   mycroft 			if (preen)
    297  1.24   mycroft 				printf(" (FIXED)\n");
    298  1.24   mycroft 			if (preen || reply("FIX") == 1) {
    299  1.24   mycroft 				sbdirty();
    300  1.24   mycroft 				dirty(&asblk);
    301  1.24   mycroft 			}
    302  1.24   mycroft 		}
    303  1.10   mycroft 		newinofmt = 1;
    304  1.10   mycroft 	} else {
    305  1.33    bouyer 		sblock->fs_qbmask = ~sblock->fs_bmask;
    306  1.33    bouyer 		sblock->fs_qfmask = ~sblock->fs_fmask;
    307  1.10   mycroft 		newinofmt = 0;
    308  1.10   mycroft 	}
    309  1.10   mycroft 	/*
    310  1.10   mycroft 	 * Convert to new inode format.
    311  1.10   mycroft 	 */
    312  1.58      fvdl 	if (!is_ufs2 && cvtlevel >= 2 &&
    313  1.58      fvdl 	    sblock->fs_old_inodefmt < FS_44INODEFMT) {
    314  1.10   mycroft 		if (preen)
    315  1.10   mycroft 			pwarn("CONVERTING TO NEW INODE FORMAT\n");
    316  1.10   mycroft 		else if (!reply("CONVERT TO NEW INODE FORMAT"))
    317  1.10   mycroft 			return(0);
    318  1.10   mycroft 		doinglevel2++;
    319  1.58      fvdl 		sblock->fs_old_inodefmt = FS_44INODEFMT;
    320  1.33    bouyer 		sblock->fs_maxfilesize = maxfilesize;
    321  1.58      fvdl 		sblock->fs_maxsymlinklen = MAXSYMLINKLEN_UFS1;
    322  1.33    bouyer 		sblock->fs_qbmask = ~sblock->fs_bmask;
    323  1.33    bouyer 		sblock->fs_qfmask = ~sblock->fs_fmask;
    324  1.10   mycroft 		sbdirty();
    325  1.10   mycroft 		dirty(&asblk);
    326  1.10   mycroft 	}
    327  1.10   mycroft 	/*
    328  1.10   mycroft 	 * Convert to new cylinder group format.
    329  1.10   mycroft 	 */
    330  1.58      fvdl 	if (!is_ufs2 && cvtlevel >= 1 &&
    331  1.58      fvdl 	    sblock->fs_old_postblformat == FS_42POSTBLFMT) {
    332  1.10   mycroft 		if (preen)
    333  1.10   mycroft 			pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n");
    334  1.10   mycroft 		else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT"))
    335  1.10   mycroft 			return(0);
    336  1.10   mycroft 		doinglevel1++;
    337  1.58      fvdl 		sblock->fs_old_postblformat = FS_DYNAMICPOSTBLFMT;
    338  1.58      fvdl 		sblock->fs_old_nrpos = 0;
    339  1.10   mycroft 		sbdirty();
    340  1.10   mycroft 		dirty(&asblk);
    341   1.1       cgd 	}
    342  1.11        ws 	if (asblk.b_dirty && !bflag) {
    343  1.58      fvdl 		memmove((struct fs*)sblk.b_un.b_fs, sblock, SBLOCKSIZE);
    344  1.33    bouyer 		if (needswap)
    345  1.45     lukem 			ffs_sb_swap(sblock, (struct fs*)sblk.b_un.b_fs);
    346  1.33    bouyer 		memmove(asblk.b_un.b_fs, sblk.b_un.b_fs, (size_t)sblock->fs_sbsize);
    347   1.1       cgd 		flush(fswritefd, &asblk);
    348   1.1       cgd 	}
    349   1.1       cgd 	/*
    350   1.1       cgd 	 * read in the summary info.
    351   1.1       cgd 	 */
    352   1.1       cgd 	asked = 0;
    353  1.46     lukem 	sblock->fs_csp = (struct csum *)calloc(1, sblock->fs_cssize);
    354  1.33    bouyer 	for (i = 0, j = 0; i < sblock->fs_cssize; i += sblock->fs_bsize, j++) {
    355  1.33    bouyer 		size = sblock->fs_cssize - i < sblock->fs_bsize ?
    356  1.33    bouyer 		    sblock->fs_cssize - i : sblock->fs_bsize;
    357  1.46     lukem 		ccsp = (struct csum *)((char *)sblock->fs_csp + i);
    358  1.46     lukem 		if (bread(fsreadfd, (char *)ccsp,
    359  1.33    bouyer 		    fsbtodb(sblock, sblock->fs_csaddr + j * sblock->fs_frag),
    360   1.1       cgd 		    size) != 0 && !asked) {
    361   1.1       cgd 			pfatal("BAD SUMMARY INFORMATION");
    362  1.37      fvdl 			if (reply("CONTINUE") == 0) {
    363  1.37      fvdl 				markclean = 0;
    364  1.30     lukem 				exit(EEXIT);
    365  1.37      fvdl 			}
    366   1.1       cgd 			asked++;
    367   1.1       cgd 		}
    368  1.33    bouyer 		if (doswap) {
    369  1.46     lukem 			ffs_csum_swap(ccsp, ccsp, size);
    370  1.46     lukem 			bwrite(fswritefd, (char *)ccsp,
    371  1.37      fvdl 			    fsbtodb(sblock,
    372  1.37      fvdl 				sblock->fs_csaddr + j * sblock->fs_frag),
    373  1.37      fvdl 			    size);
    374  1.33    bouyer 		}
    375  1.46     lukem 		if (needswap)
    376  1.46     lukem 			ffs_csum_swap(ccsp, ccsp, size);
    377   1.1       cgd 	}
    378   1.1       cgd 	/*
    379   1.1       cgd 	 * allocate and initialize the necessary maps
    380   1.1       cgd 	 */
    381  1.20       cgd 	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
    382   1.1       cgd 	blockmap = calloc((unsigned)bmapsize, sizeof (char));
    383   1.1       cgd 	if (blockmap == NULL) {
    384   1.1       cgd 		printf("cannot alloc %u bytes for blockmap\n",
    385   1.1       cgd 		    (unsigned)bmapsize);
    386  1.15       cgd 		goto badsblabel;
    387   1.1       cgd 	}
    388  1.58      fvdl 	inostathead = calloc((unsigned)(sblock->fs_ncg),
    389  1.58      fvdl 	    sizeof(struct inostatlist));
    390  1.58      fvdl 	if (inostathead == NULL) {
    391  1.58      fvdl 		printf("cannot alloc %u bytes for inostathead\n",
    392  1.58      fvdl 		    (unsigned)(sizeof(struct inostatlist) * (sblock->fs_ncg)));
    393  1.15       cgd 		goto badsblabel;
    394   1.1       cgd 	}
    395  1.39   mycroft 	/*
    396  1.39   mycroft 	 * cs_ndir may be inaccurate, particularly if we're using the -b
    397  1.39   mycroft 	 * option, so set a minimum to prevent bogus subdirectory reconnects
    398  1.39   mycroft 	 * and really inefficient directory scans.
    399  1.39   mycroft 	 * Also set a maximum in case the value is too large.
    400  1.39   mycroft 	 */
    401  1.33    bouyer 	numdirs = sblock->fs_cstotal.cs_ndir;
    402  1.39   mycroft 	if (numdirs < 1024)
    403  1.39   mycroft 		numdirs = 1024;
    404  1.39   mycroft 	if (numdirs > maxino + 1)
    405  1.39   mycroft 		numdirs = maxino + 1;
    406  1.58      fvdl 	dirhash = numdirs;
    407   1.1       cgd 	inplast = 0;
    408   1.1       cgd 	listmax = numdirs + 10;
    409   1.1       cgd 	inpsort = (struct inoinfo **)calloc((unsigned)listmax,
    410   1.1       cgd 	    sizeof(struct inoinfo *));
    411   1.1       cgd 	inphead = (struct inoinfo **)calloc((unsigned)numdirs,
    412   1.1       cgd 	    sizeof(struct inoinfo *));
    413   1.1       cgd 	if (inpsort == NULL || inphead == NULL) {
    414   1.1       cgd 		printf("cannot alloc %u bytes for inphead\n",
    415  1.29       mrg 		    (unsigned)(numdirs * sizeof(struct inoinfo *)));
    416  1.15       cgd 		goto badsblabel;
    417   1.1       cgd 	}
    418  1.37      fvdl 	cgrp = malloc(sblock->fs_cgsize);
    419  1.37      fvdl 	if (cgrp == NULL) {
    420  1.37      fvdl 		printf("cannot alloc %u bytes for cylinder group\n",
    421  1.37      fvdl 		    sblock->fs_cgsize);
    422  1.37      fvdl 		goto badsblabel;
    423  1.37      fvdl 	}
    424   1.1       cgd 	bufinit();
    425  1.37      fvdl 	if (sblock->fs_flags & FS_DOSOFTDEP)
    426  1.37      fvdl 		usedsoftdep = 1;
    427  1.37      fvdl 	else
    428  1.37      fvdl 		usedsoftdep = 0;
    429  1.54       dbj 
    430  1.54       dbj 	{
    431  1.54       dbj 		struct partition *pp = 0;
    432  1.55       dbj 		if (!forceimage && lp)
    433  1.54       dbj 			pp = getdisklabelpart(dev,lp);
    434  1.54       dbj 		if (pp && (pp->p_fstype == FS_APPLEUFS)) {
    435  1.54       dbj 			isappleufs = 1;
    436  1.54       dbj 		}
    437  1.54       dbj 	}
    438  1.54       dbj 	if (readappleufs()) {
    439  1.54       dbj 		isappleufs = 1;
    440  1.54       dbj 	}
    441  1.54       dbj 
    442  1.54       dbj 	dirblksiz = DIRBLKSIZ;
    443  1.54       dbj 	if (isappleufs)
    444  1.54       dbj 		dirblksiz = APPLEUFS_DIRBLKSIZ;
    445  1.54       dbj 
    446  1.54       dbj 	if (debug)
    447  1.54       dbj 		printf("isappleufs = %d, dirblksiz = %d\n", isappleufs, dirblksiz);
    448  1.54       dbj 
    449   1.1       cgd 	return (1);
    450   1.1       cgd 
    451  1.15       cgd badsblabel:
    452  1.33    bouyer 	markclean=0;
    453  1.33    bouyer 	ckfini();
    454   1.1       cgd 	return (0);
    455   1.1       cgd }
    456   1.1       cgd 
    457  1.54       dbj static int
    458  1.54       dbj readappleufs()
    459  1.54       dbj {
    460  1.56      fvdl 	daddr_t label = APPLEUFS_LABEL_OFFSET / dev_bsize;
    461  1.54       dbj 	struct appleufslabel *appleufs;
    462  1.54       dbj 	int i;
    463  1.54       dbj 
    464  1.54       dbj 	/* XXX do we have to deal with APPLEUFS_LABEL_OFFSET not
    465  1.54       dbj 	 * being block aligned (CD's?)
    466  1.54       dbj 	 */
    467  1.54       dbj 	if (bread(fsreadfd, (char *)appleufsblk.b_un.b_fs, label, (long)APPLEUFS_LABEL_SIZE) != 0)
    468  1.54       dbj 		return 0;
    469  1.54       dbj 	appleufsblk.b_bno = label;
    470  1.54       dbj 	appleufsblk.b_size = APPLEUFS_LABEL_SIZE;
    471  1.54       dbj 
    472  1.54       dbj 	appleufs = appleufsblk.b_un.b_appleufs;
    473  1.54       dbj 
    474  1.54       dbj 	if (ntohl(appleufs->ul_magic) != APPLEUFS_LABEL_MAGIC) {
    475  1.54       dbj 		if (!isappleufs) {
    476  1.54       dbj 			return 0;
    477  1.54       dbj 		} else {
    478  1.54       dbj 			pfatal("MISSING APPLEUFS VOLUME LABEL\n");
    479  1.54       dbj 			if (reply("FIX") == 0) {
    480  1.54       dbj 				return 1;
    481  1.54       dbj 			}
    482  1.54       dbj 			ffs_appleufs_set(appleufs,NULL,-1);
    483  1.54       dbj 			appleufsdirty();
    484  1.54       dbj 		}
    485  1.54       dbj 	}
    486  1.54       dbj 
    487  1.54       dbj 	if (ntohl(appleufs->ul_version) != APPLEUFS_LABEL_VERSION) {
    488  1.54       dbj 		pwarn("INCORRECT APPLE UFS VERSION NUMBER (%d should be %d)",
    489  1.54       dbj 			ntohl(appleufs->ul_version),APPLEUFS_LABEL_VERSION);
    490  1.54       dbj 		if (preen) {
    491  1.54       dbj 			printf(" (CORRECTED)\n");
    492  1.54       dbj 		}
    493  1.54       dbj 		if (preen || reply("CORRECT")) {
    494  1.54       dbj 			appleufs->ul_version = htonl(APPLEUFS_LABEL_VERSION);
    495  1.54       dbj 			appleufsdirty();
    496  1.54       dbj 		}
    497  1.54       dbj 	}
    498  1.54       dbj 
    499  1.54       dbj 	if (ntohs(appleufs->ul_namelen) > APPLEUFS_MAX_LABEL_NAME) {
    500  1.54       dbj 		pwarn("APPLE UFS LABEL NAME TOO LONG");
    501  1.54       dbj 		if (preen) {
    502  1.54       dbj 			printf(" (TRUNCATED)\n");
    503  1.54       dbj 		}
    504  1.54       dbj 		if (preen || reply("TRUNCATE")) {
    505  1.54       dbj 			appleufs->ul_namelen = htons(APPLEUFS_MAX_LABEL_NAME);
    506  1.54       dbj 			appleufsdirty();
    507  1.54       dbj 		}
    508  1.54       dbj 	}
    509  1.54       dbj 
    510  1.54       dbj 	if (ntohs(appleufs->ul_namelen) == 0) {
    511  1.54       dbj 		pwarn("MISSING APPLE UFS LABEL NAME");
    512  1.54       dbj 		if (preen) {
    513  1.54       dbj 			printf(" (FIXED)\n");
    514  1.54       dbj 		}
    515  1.54       dbj 		if (preen || reply("FIX")) {
    516  1.54       dbj 			ffs_appleufs_set(appleufs,NULL,-1);
    517  1.54       dbj 			appleufsdirty();
    518  1.54       dbj 		}
    519  1.54       dbj 	}
    520  1.54       dbj 
    521  1.54       dbj 	/* Scan name for first illegal character */
    522  1.54       dbj 	for (i=0;i<ntohs(appleufs->ul_namelen);i++) {
    523  1.54       dbj 		if ((appleufs->ul_name[i] == '\0') ||
    524  1.54       dbj 			(appleufs->ul_name[i] == ':') ||
    525  1.54       dbj 			(appleufs->ul_name[i] == '/')) {
    526  1.54       dbj 			pwarn("APPLE UFS LABEL NAME CONTAINS ILLEGAL CHARACTER");
    527  1.54       dbj 			if (preen) {
    528  1.54       dbj 				printf(" (TRUNCATED)\n");
    529  1.54       dbj 			}
    530  1.54       dbj 			if (preen || reply("TRUNCATE")) {
    531  1.54       dbj 				appleufs->ul_namelen = i+1;
    532  1.54       dbj 				appleufsdirty();
    533  1.54       dbj 			}
    534  1.54       dbj 			break;
    535  1.54       dbj 		}
    536  1.54       dbj 	}
    537  1.54       dbj 
    538  1.54       dbj 	/* Check the checksum last, because if anything else was wrong,
    539  1.54       dbj 	 * then the checksum gets reset anyway.
    540  1.54       dbj 	 */
    541  1.54       dbj 	appleufs->ul_checksum = 0;
    542  1.54       dbj 	appleufs->ul_checksum = ffs_appleufs_cksum(appleufs);
    543  1.54       dbj 	if (appleufsblk.b_un.b_appleufs->ul_checksum != appleufs->ul_checksum) {
    544  1.54       dbj 		pwarn("INVALID APPLE UFS CHECKSUM (%#04x should be %#04x)",
    545  1.54       dbj 			appleufsblk.b_un.b_appleufs->ul_checksum, appleufs->ul_checksum);
    546  1.54       dbj 		if (preen) {
    547  1.54       dbj 			printf(" (CORRECTED)\n");
    548  1.54       dbj 		}
    549  1.54       dbj 		if (preen || reply("CORRECT")) {
    550  1.54       dbj 			appleufsdirty();
    551  1.54       dbj 		} else {
    552  1.54       dbj 			/* put the incorrect checksum back in place */
    553  1.54       dbj 			appleufs->ul_checksum = appleufsblk.b_un.b_appleufs->ul_checksum;
    554  1.54       dbj 		}
    555  1.54       dbj 	}
    556  1.54       dbj 	return 1;
    557  1.54       dbj }
    558  1.54       dbj 
    559   1.1       cgd /*
    560  1.58      fvdl  * Detect byte order. Return 0 if valid magic found, -1 otherwise.
    561   1.1       cgd  */
    562  1.26  christos static int
    563  1.58      fvdl detect_byteorder(struct fs *fs)
    564   1.1       cgd {
    565  1.58      fvdl 	if (fs->fs_magic == FS_UFS1_MAGIC || fs->fs_magic == FS_UFS2_MAGIC) {
    566  1.58      fvdl 		if (endian == 0 || BYTE_ORDER == endian) {
    567  1.58      fvdl 			needswap = 0;
    568  1.58      fvdl 			doswap = do_blkswap = do_dirswap = 0;
    569  1.58      fvdl 		} else {
    570  1.58      fvdl 			needswap = 1;
    571  1.58      fvdl 			doswap = do_blkswap = do_dirswap = 1;
    572  1.58      fvdl 		}
    573  1.58      fvdl 		return 0;
    574  1.61     enami 	} else if (fs->fs_magic == bswap32(FS_UFS1_MAGIC) ||
    575  1.58      fvdl 		   fs->fs_magic == bswap32(FS_UFS2_MAGIC)) {
    576  1.33    bouyer 		if (endian == 0 || BYTE_ORDER != endian) {
    577  1.33    bouyer 			needswap = 1;
    578  1.33    bouyer 			doswap = do_blkswap = do_dirswap = 0;
    579  1.33    bouyer 		} else {
    580  1.33    bouyer 			needswap = 0;
    581  1.33    bouyer 			doswap = do_blkswap = do_dirswap = 1;
    582  1.33    bouyer 		}
    583  1.58      fvdl 		return 0;
    584  1.58      fvdl 	}
    585  1.58      fvdl 	return -1;
    586  1.58      fvdl }
    587  1.58      fvdl 
    588  1.58      fvdl /*
    589  1.58      fvdl  * Possible superblock locations ordered from most to least likely.
    590  1.58      fvdl  */
    591  1.58      fvdl static off_t sblock_try[] = SBLOCKSEARCH;
    592  1.58      fvdl 
    593  1.58      fvdl /*
    594  1.58      fvdl  * Read in the super block and its summary info.
    595  1.58      fvdl  */
    596  1.58      fvdl static int
    597  1.58      fvdl readsb(listerr)
    598  1.58      fvdl 	int listerr;
    599  1.58      fvdl {
    600  1.58      fvdl 	daddr_t super;
    601  1.58      fvdl 	struct fs *fs;
    602  1.58      fvdl 	int i;
    603  1.58      fvdl 
    604  1.58      fvdl 	if (bflag) {
    605  1.58      fvdl 		super = bflag;
    606  1.58      fvdl 		if (bread(fsreadfd, (char *)sblk.b_un.b_fs, super,
    607  1.58      fvdl 		    (long)SBLOCKSIZE) != 0)
    608  1.58      fvdl 			return (0);
    609  1.58      fvdl 		fs = sblk.b_un.b_fs;
    610  1.58      fvdl 		if (detect_byteorder(fs) < 0) {
    611  1.58      fvdl 			badsb(listerr, "MAGIC NUMBER WRONG");
    612  1.58      fvdl 			return (0);
    613  1.58      fvdl 		}
    614  1.33    bouyer 	} else {
    615  1.58      fvdl 		for (i = 0; sblock_try[i] != -1; i++) {
    616  1.58      fvdl 			super = sblock_try[i] / dev_bsize;
    617  1.58      fvdl 			if (bread(fsreadfd, (char *)sblk.b_un.b_fs,
    618  1.58      fvdl 			    super, (long)SBLOCKSIZE) != 0)
    619  1.58      fvdl 				continue;
    620  1.58      fvdl 			fs = sblk.b_un.b_fs;
    621  1.58      fvdl 			if (detect_byteorder(fs) == 0)
    622  1.58      fvdl 				break;
    623  1.58      fvdl 		}
    624  1.58      fvdl 		if (sblock_try[i] == -1) {
    625  1.58      fvdl 			badsb(listerr, "CAN'T FIND SUPERBLOCK");
    626  1.58      fvdl 			return (0);
    627  1.58      fvdl 		}
    628  1.33    bouyer 	}
    629  1.33    bouyer 	if (doswap) {
    630  1.33    bouyer 		if (preen)
    631  1.33    bouyer 			errx(EEXIT, "incompatible options -B and -p");
    632  1.33    bouyer 		if (nflag)
    633  1.33    bouyer 			errx(EEXIT, "incompatible options -B and -n");
    634  1.33    bouyer 		if (endian == LITTLE_ENDIAN) {
    635  1.36        is 			if (!reply("CONVERT TO LITTLE ENDIAN"))
    636  1.33    bouyer 				return 0;
    637  1.33    bouyer 		} else if (endian == BIG_ENDIAN) {
    638  1.36        is 			if (!reply("CONVERT TO BIG ENDIAN"))
    639  1.33    bouyer 				return 0;
    640  1.33    bouyer 		} else
    641  1.33    bouyer 			pfatal("INTERNAL ERROR: unknown endian");
    642  1.33    bouyer 	}
    643  1.33    bouyer 	if (needswap)
    644  1.33    bouyer 		printf("** Swapped byte order\n");
    645  1.33    bouyer 	/* swap SB byte order if asked */
    646  1.33    bouyer 	if (doswap)
    647  1.45     lukem 		ffs_sb_swap(sblk.b_un.b_fs, sblk.b_un.b_fs);
    648  1.33    bouyer 
    649  1.58      fvdl 	memmove(sblock, sblk.b_un.b_fs, SBLOCKSIZE);
    650  1.33    bouyer 	if (needswap)
    651  1.45     lukem 		ffs_sb_swap(sblk.b_un.b_fs, sblock);
    652  1.33    bouyer 
    653  1.58      fvdl 	is_ufs2 = sblock->fs_magic == FS_UFS2_MAGIC;
    654  1.58      fvdl 
    655   1.1       cgd 	/*
    656   1.1       cgd 	 * run a few consistency checks of the super block
    657   1.1       cgd 	 */
    658  1.58      fvdl 	if (sblock->fs_sbsize > SBLOCKSIZE)
    659   1.1       cgd 		{ badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
    660   1.1       cgd 	/*
    661   1.1       cgd 	 * Compute block size that the filesystem is based on,
    662   1.1       cgd 	 * according to fsbtodb, and adjust superblock block number
    663   1.1       cgd 	 * so we can tell if this is an alternate later.
    664   1.1       cgd 	 */
    665   1.1       cgd 	super *= dev_bsize;
    666  1.33    bouyer 	dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
    667   1.1       cgd 	sblk.b_bno = super / dev_bsize;
    668  1.58      fvdl 	sblk.b_size = SBLOCKSIZE;
    669  1.58      fvdl 	if (bflag)
    670  1.58      fvdl 		goto out;
    671   1.1       cgd 	/*
    672  1.58      fvdl 	 * Set all possible fields that could differ, then do check
    673  1.58      fvdl 	 * of whole super block against an alternate super block->
    674   1.1       cgd 	 * When an alternate super-block is specified this check is skipped.
    675   1.1       cgd 	 */
    676  1.33    bouyer 	getblk(&asblk, cgsblock(sblock, sblock->fs_ncg - 1), sblock->fs_sbsize);
    677   1.1       cgd 	if (asblk.b_errs)
    678   1.1       cgd 		return (0);
    679  1.33    bouyer 	/* swap SB byte order if asked */
    680  1.33    bouyer 	if (doswap)
    681  1.45     lukem 		ffs_sb_swap(asblk.b_un.b_fs, asblk.b_un.b_fs);
    682  1.33    bouyer 
    683  1.33    bouyer 	memmove(altsblock, asblk.b_un.b_fs, sblock->fs_sbsize);
    684  1.33    bouyer 	if (needswap)
    685  1.45     lukem 		ffs_sb_swap(asblk.b_un.b_fs, altsblock);
    686  1.41   thorpej 	if (cmpsblks(sblock, altsblock)) {
    687   1.1       cgd 		badsb(listerr,
    688   1.1       cgd 		"VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
    689   1.1       cgd 		return (0);
    690   1.1       cgd 	}
    691  1.58      fvdl out:
    692  1.58      fvdl         /*
    693  1.58      fvdl          * If not yet done, update UFS1 superblock with new wider fields.
    694  1.58      fvdl          */
    695  1.60      fvdl         if (sblock->fs_magic == FS_UFS1_MAGIC) {
    696  1.60      fvdl 		if (sblock->fs_maxbsize != sblock->fs_bsize ||
    697  1.60      fvdl 		    sblock->fs_time < sblock->fs_old_time) {
    698  1.60      fvdl 			sblock->fs_cstotal.cs_ndir =
    699  1.60      fvdl 			    sblock->fs_old_cstotal.cs_ndir;
    700  1.60      fvdl 			sblock->fs_cstotal.cs_nbfree =
    701  1.60      fvdl 			    sblock->fs_old_cstotal.cs_nbfree;
    702  1.60      fvdl 			sblock->fs_cstotal.cs_nifree =
    703  1.60      fvdl 			    sblock->fs_old_cstotal.cs_nifree;
    704  1.60      fvdl 			sblock->fs_cstotal.cs_nffree =
    705  1.60      fvdl 			    sblock->fs_old_cstotal.cs_nffree;
    706  1.60      fvdl 		}
    707  1.60      fvdl 		if (sblock->fs_maxbsize != sblock->fs_bsize) {
    708  1.60      fvdl 			sblock->fs_maxbsize = sblock->fs_bsize;
    709  1.60      fvdl 			sblock->fs_time = sblock->fs_old_time;
    710  1.60      fvdl 			sblock->fs_size = sblock->fs_old_size;
    711  1.60      fvdl 			sblock->fs_dsize = sblock->fs_old_dsize;
    712  1.60      fvdl 			sblock->fs_csaddr = sblock->fs_old_csaddr;
    713  1.60      fvdl 		}
    714  1.58      fvdl 	}
    715  1.58      fvdl 
    716  1.33    bouyer 	/* Now we know the SB is valid, we can write it back if needed */
    717  1.33    bouyer 	if (doswap) {
    718  1.33    bouyer 		sbdirty();
    719  1.33    bouyer 		dirty(&asblk);
    720  1.33    bouyer 	}
    721   1.1       cgd 	havesb = 1;
    722   1.1       cgd 	return (1);
    723  1.41   thorpej }
    724  1.41   thorpej 
    725  1.41   thorpej int
    726  1.41   thorpej cmpsblks(const struct fs *sb, struct fs *asb)
    727  1.41   thorpej {
    728  1.58      fvdl         if (asb->fs_sblkno != sb->fs_sblkno ||
    729  1.57      fvdl 	    asb->fs_cblkno != sb->fs_cblkno ||
    730  1.57      fvdl 	    asb->fs_iblkno != sb->fs_iblkno ||
    731  1.57      fvdl 	    asb->fs_dblkno != sb->fs_dblkno ||
    732  1.57      fvdl 	    asb->fs_ncg != sb->fs_ncg ||
    733  1.57      fvdl 	    asb->fs_bsize != sb->fs_bsize ||
    734  1.57      fvdl 	    asb->fs_fsize != sb->fs_fsize ||
    735  1.57      fvdl 	    asb->fs_frag != sb->fs_frag ||
    736  1.57      fvdl 	    asb->fs_bmask != sb->fs_bmask ||
    737  1.57      fvdl 	    asb->fs_fmask != sb->fs_fmask ||
    738  1.57      fvdl 	    asb->fs_bshift != sb->fs_bshift ||
    739  1.57      fvdl 	    asb->fs_fshift != sb->fs_fshift ||
    740  1.57      fvdl 	    asb->fs_fragshift != sb->fs_fragshift ||
    741  1.57      fvdl 	    asb->fs_fsbtodb != sb->fs_fsbtodb ||
    742  1.57      fvdl 	    asb->fs_sbsize != sb->fs_sbsize ||
    743  1.57      fvdl 	    asb->fs_nindir != sb->fs_nindir ||
    744  1.57      fvdl 	    asb->fs_inopb != sb->fs_inopb ||
    745  1.57      fvdl 	    asb->fs_cssize != sb->fs_cssize ||
    746  1.57      fvdl 	    asb->fs_ipg != sb->fs_ipg ||
    747  1.57      fvdl 	    asb->fs_fpg != sb->fs_fpg ||
    748  1.57      fvdl 	    asb->fs_magic != sb->fs_magic)
    749  1.58      fvdl 		return 1;
    750  1.58      fvdl 	return 0;
    751   1.1       cgd }
    752   1.1       cgd 
    753  1.30     lukem static void
    754   1.1       cgd badsb(listerr, s)
    755   1.1       cgd 	int listerr;
    756   1.1       cgd 	char *s;
    757   1.1       cgd {
    758   1.1       cgd 
    759   1.1       cgd 	if (!listerr)
    760   1.1       cgd 		return;
    761   1.1       cgd 	if (preen)
    762  1.26  christos 		printf("%s: ", cdevname());
    763   1.1       cgd 	pfatal("BAD SUPER BLOCK: %s\n", s);
    764   1.1       cgd }
    765   1.1       cgd 
    766   1.1       cgd /*
    767   1.1       cgd  * Calculate a prototype superblock based on information in the disk label.
    768   1.1       cgd  * When done the cgsblock macro can be calculated and the fs_ncg field
    769   1.1       cgd  * can be used. Do NOT attempt to use other macros without verifying that
    770   1.1       cgd  * their needed information is available!
    771   1.1       cgd  */
    772  1.30     lukem static int
    773   1.1       cgd calcsb(dev, devfd, fs)
    774  1.34   mycroft 	const char *dev;
    775   1.1       cgd 	int devfd;
    776  1.28     lukem 	struct fs *fs;
    777   1.1       cgd {
    778  1.28     lukem 	struct disklabel *lp;
    779  1.28     lukem 	struct partition *pp;
    780  1.58      fvdl 	int i, nspf;
    781   1.1       cgd 
    782  1.54       dbj 	lp = getdisklabel(dev, devfd);
    783  1.54       dbj 	pp = getdisklabelpart(dev,lp);
    784  1.54       dbj 	if (pp == 0) {
    785   1.1       cgd 		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
    786   1.1       cgd 		return (0);
    787   1.1       cgd 	}
    788  1.54       dbj 	if ((pp->p_fstype != FS_BSDFFS) && (pp->p_fstype != FS_APPLEUFS)) {
    789   1.1       cgd 		pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
    790   1.1       cgd 			dev, pp->p_fstype < FSMAXTYPES ?
    791   1.1       cgd 			fstypenames[pp->p_fstype] : "unknown");
    792   1.1       cgd 		return (0);
    793   1.1       cgd 	}
    794  1.33    bouyer 	/* avoid divide by 0 */
    795  1.33    bouyer 	if (pp->p_fsize == 0 || pp->p_frag == 0)
    796  1.33    bouyer 		return (0);
    797  1.12   mycroft 	memset(fs, 0, sizeof(struct fs));
    798   1.1       cgd 	fs->fs_fsize = pp->p_fsize;
    799   1.1       cgd 	fs->fs_frag = pp->p_frag;
    800   1.1       cgd 	fs->fs_size = pp->p_size;
    801   1.1       cgd 	fs->fs_sblkno = roundup(
    802   1.1       cgd 		howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize),
    803   1.1       cgd 		fs->fs_frag);
    804  1.58      fvdl 	nspf = fs->fs_fsize / lp->d_secsize;
    805  1.58      fvdl 	fs->fs_old_nspf = nspf;
    806  1.58      fvdl 	for (fs->fs_fsbtodb = 0, i = nspf; i > 1; i >>= 1)
    807   1.1       cgd 		fs->fs_fsbtodb++;
    808   1.1       cgd 	dev_bsize = lp->d_secsize;
    809  1.58      fvdl 	if (fs->fs_magic == FS_UFS2_MAGIC) {
    810  1.58      fvdl 		fs->fs_fpg = pp->p_cpg;
    811  1.58      fvdl 		fs->fs_ncg = howmany(fs->fs_size, fs->fs_fpg);
    812  1.58      fvdl 	} else /* if (fs->fs_magic == FS_UFS1_MAGIC) */ {
    813  1.58      fvdl 		fs->fs_old_cpg = pp->p_cpg;
    814  1.58      fvdl 		fs->fs_old_cgmask = 0xffffffff;
    815  1.58      fvdl 		for (i = lp->d_ntracks; i > 1; i >>= 1)
    816  1.58      fvdl 			fs->fs_old_cgmask <<= 1;
    817  1.58      fvdl 		if (!POWEROF2(lp->d_ntracks))
    818  1.58      fvdl 			fs->fs_old_cgmask <<= 1;
    819  1.58      fvdl 		fs->fs_old_cgoffset = roundup(
    820  1.58      fvdl 			howmany(lp->d_nsectors, nspf), fs->fs_frag);
    821  1.58      fvdl 		fs->fs_fpg = (fs->fs_old_cpg * lp->d_secpercyl) / nspf;
    822  1.58      fvdl 		fs->fs_ncg = howmany(fs->fs_size / lp->d_secpercyl,
    823  1.58      fvdl 		    fs->fs_old_cpg);
    824  1.58      fvdl 	}
    825   1.1       cgd 	return (1);
    826   1.1       cgd }
    827   1.1       cgd 
    828  1.26  christos static struct disklabel *
    829   1.1       cgd getdisklabel(s, fd)
    830  1.34   mycroft 	const char *s;
    831   1.1       cgd 	int	fd;
    832   1.1       cgd {
    833   1.1       cgd 	static struct disklabel lab;
    834   1.1       cgd 
    835   1.1       cgd 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
    836   1.1       cgd 		if (s == NULL)
    837   1.1       cgd 			return ((struct disklabel *)NULL);
    838   1.1       cgd 		pwarn("ioctl (GCINFO): %s\n", strerror(errno));
    839  1.30     lukem 		errx(EEXIT, "%s: can't read disk label", s);
    840   1.1       cgd 	}
    841   1.1       cgd 	return (&lab);
    842   1.1       cgd }
    843  1.54       dbj 
    844  1.54       dbj static struct partition *
    845  1.54       dbj getdisklabelpart(dev, lp)
    846  1.54       dbj 	const char *dev;
    847  1.54       dbj 	struct disklabel *lp;
    848  1.54       dbj {
    849  1.54       dbj 	char *cp;
    850  1.54       dbj 
    851  1.54       dbj 	cp = strchr(dev, '\0') - 1;
    852  1.54       dbj 	if ((cp == (char *)-1 || (*cp < 'a' || *cp > 'p')) && !isdigit(*cp)) {
    853  1.54       dbj 		return 0;
    854  1.54       dbj 	}
    855  1.54       dbj 	if (isdigit(*cp))
    856  1.54       dbj 		return &lp->d_partitions[0];
    857  1.54       dbj 	else
    858  1.54       dbj 		return &lp->d_partitions[*cp - 'a'];
    859  1.54       dbj }
    860  1.54       dbj 
    861