Home | History | Annotate | Line # | Download | only in fsck_ext2fs
setup.c revision 1.12
      1  1.12    bouyer /*	$NetBSD: setup.c,v 1.12 2000/01/28 16:01:46 bouyer Exp $	*/
      2   1.1    bouyer 
      3   1.1    bouyer /*
      4   1.1    bouyer  * Copyright (c) 1997 Manuel Bouyer.
      5   1.1    bouyer  * Copyright (c) 1980, 1986, 1993
      6   1.1    bouyer  *	The Regents of the University of California.  All rights reserved.
      7   1.1    bouyer  *
      8   1.1    bouyer  * Redistribution and use in source and binary forms, with or without
      9   1.1    bouyer  * modification, are permitted provided that the following conditions
     10   1.1    bouyer  * are met:
     11   1.1    bouyer  * 1. Redistributions of source code must retain the above copyright
     12   1.1    bouyer  *	notice, this list of conditions and the following disclaimer.
     13   1.1    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1    bouyer  *	notice, this list of conditions and the following disclaimer in the
     15   1.1    bouyer  *	documentation and/or other materials provided with the distribution.
     16   1.1    bouyer  * 3. All advertising materials mentioning features or use of this software
     17   1.1    bouyer  *	must display the following acknowledgement:
     18   1.1    bouyer  *	This product includes software developed by the University of
     19   1.1    bouyer  *	California, Berkeley and its contributors.
     20   1.1    bouyer  * 4. Neither the name of the University nor the names of its contributors
     21   1.1    bouyer  *	may be used to endorse or promote products derived from this software
     22   1.1    bouyer  *	without specific prior written permission.
     23   1.1    bouyer  *
     24   1.1    bouyer  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25   1.1    bouyer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26   1.1    bouyer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27   1.1    bouyer  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28   1.1    bouyer  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29   1.1    bouyer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30   1.1    bouyer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31   1.1    bouyer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32   1.1    bouyer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33   1.1    bouyer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34   1.1    bouyer  * SUCH DAMAGE.
     35   1.1    bouyer  */
     36   1.1    bouyer 
     37   1.4     lukem #include <sys/cdefs.h>
     38   1.1    bouyer #ifndef lint
     39   1.1    bouyer #if 0
     40   1.1    bouyer static char sccsid[] = "@(#)setup.c	8.5 (Berkeley) 11/23/94";
     41   1.1    bouyer #else
     42  1.12    bouyer __RCSID("$NetBSD: setup.c,v 1.12 2000/01/28 16:01:46 bouyer Exp $");
     43   1.1    bouyer #endif
     44   1.1    bouyer #endif /* not lint */
     45   1.1    bouyer 
     46   1.8  christos #define FSTYPENAMES
     47   1.1    bouyer #include <sys/param.h>
     48   1.1    bouyer #include <sys/time.h>
     49   1.1    bouyer #include <ufs/ext2fs/ext2fs_dinode.h>
     50   1.1    bouyer #include <ufs/ext2fs/ext2fs.h>
     51   1.1    bouyer #include <sys/stat.h>
     52   1.1    bouyer #include <sys/ioctl.h>
     53   1.1    bouyer #include <sys/disklabel.h>
     54   1.1    bouyer #include <sys/file.h>
     55   1.1    bouyer 
     56   1.1    bouyer #include <errno.h>
     57   1.1    bouyer #include <stdio.h>
     58   1.1    bouyer #include <stdlib.h>
     59   1.1    bouyer #include <string.h>
     60   1.1    bouyer #include <ctype.h>
     61   1.1    bouyer 
     62   1.1    bouyer #include "fsck.h"
     63   1.1    bouyer #include "extern.h"
     64   1.1    bouyer #include "fsutil.h"
     65   1.1    bouyer 
     66   1.1    bouyer #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
     67   1.1    bouyer 
     68   1.1    bouyer void badsb __P((int, char *));
     69   1.7   mycroft int calcsb __P((const char *, int, struct m_ext2fs *));
     70   1.7   mycroft static struct disklabel *getdisklabel __P((const char *, int));
     71   1.1    bouyer static int readsb __P((int));
     72   1.1    bouyer 
     73   1.1    bouyer int
     74   1.1    bouyer setup(dev)
     75   1.7   mycroft 	const char *dev;
     76   1.1    bouyer {
     77   1.4     lukem 	long cg, asked, i;
     78   1.1    bouyer 	long bmapsize;
     79   1.1    bouyer 	struct disklabel *lp;
     80   1.1    bouyer 	off_t sizepb;
     81   1.1    bouyer 	struct stat statb;
     82   1.1    bouyer 	struct m_ext2fs proto;
     83   1.1    bouyer 	int doskipclean;
     84   1.1    bouyer 	u_int64_t maxfilesize;
     85   1.1    bouyer 
     86   1.1    bouyer 	havesb = 0;
     87   1.1    bouyer 	fswritefd = -1;
     88   1.1    bouyer 	doskipclean = skipclean;
     89   1.1    bouyer 	if (stat(dev, &statb) < 0) {
     90   1.1    bouyer 		printf("Can't stat %s: %s\n", dev, strerror(errno));
     91   1.1    bouyer 		return (0);
     92   1.1    bouyer 	}
     93   1.1    bouyer 	if (!S_ISCHR(statb.st_mode)) {
     94   1.1    bouyer 		pfatal("%s is not a character device", dev);
     95   1.1    bouyer 		if (reply("CONTINUE") == 0)
     96   1.1    bouyer 			return (0);
     97   1.1    bouyer 	}
     98   1.1    bouyer 	if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
     99   1.1    bouyer 		printf("Can't open %s: %s\n", dev, strerror(errno));
    100   1.1    bouyer 		return (0);
    101   1.1    bouyer 	}
    102   1.1    bouyer 	if (preen == 0)
    103   1.1    bouyer 		printf("** %s", dev);
    104   1.1    bouyer 	if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
    105   1.1    bouyer 		fswritefd = -1;
    106   1.1    bouyer 		if (preen)
    107   1.1    bouyer 			pfatal("NO WRITE ACCESS");
    108   1.1    bouyer 		printf(" (NO WRITE)");
    109   1.1    bouyer 	}
    110   1.1    bouyer 	if (preen == 0)
    111   1.1    bouyer 		printf("\n");
    112   1.1    bouyer 	fsmodified = 0;
    113   1.1    bouyer 	lfdir = 0;
    114   1.1    bouyer 	initbarea(&sblk);
    115   1.1    bouyer 	initbarea(&asblk);
    116   1.6    bouyer 	sblk.b_un.b_buf = malloc(SBSIZE);
    117   1.6    bouyer 	asblk.b_un.b_buf = malloc(SBSIZE);
    118   1.1    bouyer 	if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
    119   1.1    bouyer 		errexit("cannot allocate space for superblock\n");
    120   1.1    bouyer 	if ((lp = getdisklabel((char *)NULL, fsreadfd)) != NULL)
    121   1.1    bouyer 		dev_bsize = secsize = lp->d_secsize;
    122   1.1    bouyer 	else
    123   1.1    bouyer 		dev_bsize = secsize = DEV_BSIZE;
    124   1.1    bouyer 	/*
    125   1.1    bouyer 	 * Read in the superblock, looking for alternates if necessary
    126   1.1    bouyer 	 */
    127   1.1    bouyer 	if (readsb(1) == 0) {
    128   1.1    bouyer 		if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
    129   1.1    bouyer 			return(0);
    130   1.1    bouyer 		if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
    131   1.1    bouyer 			return (0);
    132   1.1    bouyer 		for (cg = 1; cg < proto.e2fs_ncg; cg++) {
    133   1.1    bouyer 			bflag = fsbtodb(&proto,
    134   1.1    bouyer 				cg * proto.e2fs.e2fs_bpg + proto.e2fs.e2fs_first_dblock);
    135   1.1    bouyer 			if (readsb(0) != 0)
    136   1.1    bouyer 				break;
    137   1.1    bouyer 		}
    138   1.1    bouyer 		if (cg >= proto.e2fs_ncg) {
    139   1.1    bouyer 			printf("%s %s\n%s %s\n%s %s\n",
    140   1.1    bouyer 				"SEARCH FOR ALTERNATE SUPER-BLOCK",
    141   1.1    bouyer 				"FAILED. YOU MUST USE THE",
    142   1.1    bouyer 				"-b OPTION TO FSCK_FFS TO SPECIFY THE",
    143   1.1    bouyer 				"LOCATION OF AN ALTERNATE",
    144   1.1    bouyer 				"SUPER-BLOCK TO SUPPLY NEEDED",
    145   1.1    bouyer 				"INFORMATION; SEE fsck_ext2fs(8).");
    146   1.1    bouyer 			return(0);
    147   1.1    bouyer 		}
    148   1.1    bouyer 		doskipclean = 0;
    149   1.1    bouyer 		pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
    150   1.1    bouyer 	}
    151   1.1    bouyer 	if (debug)
    152   1.1    bouyer 		printf("state = %d\n", sblock.e2fs.e2fs_state);
    153   1.1    bouyer 	if (sblock.e2fs.e2fs_state == E2FS_ISCLEAN) {
    154   1.1    bouyer 		if (doskipclean) {
    155   1.1    bouyer 			pwarn("%sile system is clean; not checking\n",
    156   1.1    bouyer 				preen ? "f" : "** F");
    157   1.1    bouyer 			return (-1);
    158   1.1    bouyer 		}
    159   1.1    bouyer 		if (!preen)
    160   1.1    bouyer 			pwarn("** File system is already clean\n");
    161   1.1    bouyer 	}
    162   1.1    bouyer 	maxfsblock = sblock.e2fs.e2fs_bcount;
    163   1.1    bouyer 	maxino = sblock.e2fs_ncg * sblock.e2fs.e2fs_ipg;
    164   1.1    bouyer 	sizepb = sblock.e2fs_bsize;
    165   1.1    bouyer 	maxfilesize = sblock.e2fs_bsize * NDADDR - 1;
    166   1.1    bouyer 	for (i = 0; i < NIADDR; i++) {
    167   1.1    bouyer 		sizepb *= NINDIR(&sblock);
    168   1.1    bouyer 		maxfilesize += sizepb;
    169   1.1    bouyer 	}
    170   1.1    bouyer 	/*
    171   1.1    bouyer 	 * Check and potentially fix certain fields in the super block.
    172   1.1    bouyer 	 */
    173   1.1    bouyer 	if ((sblock.e2fs.e2fs_rbcount < 0) ||
    174   1.1    bouyer 			(sblock.e2fs.e2fs_rbcount > sblock.e2fs.e2fs_bcount)) {
    175   1.1    bouyer 		pfatal("IMPOSSIBLE RESERVED BLOCK COUNT=%d IN SUPERBLOCK",
    176   1.1    bouyer 			sblock.e2fs.e2fs_rbcount);
    177   1.1    bouyer 		if (reply("SET TO DEFAULT") == 1) {
    178   1.1    bouyer 			sblock.e2fs.e2fs_rbcount = sblock.e2fs.e2fs_bcount * 0.1;
    179   1.1    bouyer 			sbdirty();
    180   1.6    bouyer 			dirty(&asblk);
    181   1.1    bouyer 		}
    182   1.1    bouyer 	}
    183   1.1    bouyer 	if (sblock.e2fs.e2fs_bpg != sblock.e2fs.e2fs_fpg) {
    184   1.1    bouyer 		pfatal("WRONG FPG=%d (BPG=%d) IN SUPERBLOCK",
    185   1.1    bouyer 			sblock.e2fs.e2fs_fpg, sblock.e2fs.e2fs_bpg);
    186   1.1    bouyer 		return 0;
    187   1.1    bouyer 	}
    188   1.1    bouyer 	if (asblk.b_dirty && !bflag) {
    189   1.6    bouyer 		copyback_sb(&asblk);
    190   1.1    bouyer 		flush(fswritefd, &asblk);
    191   1.1    bouyer 	}
    192   1.1    bouyer 	/*
    193   1.1    bouyer 	 * read in the summary info.
    194   1.1    bouyer 	 */
    195   1.1    bouyer 
    196   1.1    bouyer 	sblock.e2fs_gd = malloc(sblock.e2fs_ngdb * sblock.e2fs_bsize);
    197   1.9    bouyer 	if (sblock.e2fs_gd == NULL)
    198   1.9    bouyer 		errexit("out of memory\n");
    199   1.1    bouyer 	asked = 0;
    200   1.1    bouyer 	for (i=0; i < sblock.e2fs_ngdb; i++) {
    201   1.1    bouyer 		if (bread(fsreadfd,(char *)
    202   1.1    bouyer 			&sblock.e2fs_gd[i* sblock.e2fs_bsize / sizeof(struct ext2_gd)],
    203   1.1    bouyer 			fsbtodb(&sblock, ((sblock.e2fs_bsize>1024)?0:1)+i+1),
    204   1.1    bouyer 			sblock.e2fs_bsize) != 0 && !asked) {
    205   1.1    bouyer 			pfatal("BAD SUMMARY INFORMATION");
    206   1.1    bouyer 			if (reply("CONTINUE") == 0)
    207   1.1    bouyer 				errexit("%s\n", "");
    208   1.1    bouyer 			asked++;
    209   1.1    bouyer 		}
    210   1.1    bouyer 	}
    211   1.1    bouyer 	/*
    212   1.1    bouyer 	 * allocate and initialize the necessary maps
    213   1.1    bouyer 	 */
    214   1.1    bouyer 	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
    215   1.1    bouyer 	blockmap = calloc((unsigned)bmapsize, sizeof (char));
    216   1.1    bouyer 	if (blockmap == NULL) {
    217   1.1    bouyer 		printf("cannot alloc %u bytes for blockmap\n",
    218   1.1    bouyer 			(unsigned)bmapsize);
    219   1.1    bouyer 		goto badsblabel;
    220   1.1    bouyer 	}
    221   1.1    bouyer 	statemap = calloc((unsigned)(maxino + 2), sizeof(char));
    222   1.1    bouyer 	if (statemap == NULL) {
    223   1.1    bouyer 		printf("cannot alloc %u bytes for statemap\n",
    224   1.1    bouyer 			(unsigned)(maxino + 1));
    225   1.1    bouyer 		goto badsblabel;
    226   1.1    bouyer 	}
    227  1.11    bouyer 	typemap = calloc((unsigned)(maxino + 1), sizeof(char));
    228  1.11    bouyer 	if (typemap == NULL) {
    229  1.11    bouyer 		printf("cannot alloc %u bytes for typemap\n",
    230  1.11    bouyer 		    (unsigned)(maxino + 1));
    231  1.11    bouyer 		goto badsblabel;
    232  1.11    bouyer 	}
    233   1.1    bouyer 	lncntp = (int16_t *)calloc((unsigned)(maxino + 1), sizeof(int16_t));
    234   1.1    bouyer 	if (lncntp == NULL) {
    235   1.1    bouyer 		printf("cannot alloc %u bytes for lncntp\n",
    236   1.5       mrg 			(unsigned)((maxino + 1) * sizeof(int16_t)));
    237   1.1    bouyer 		goto badsblabel;
    238   1.1    bouyer 	}
    239   1.1    bouyer 	for (numdirs = 0, cg = 0; cg < sblock.e2fs_ncg; cg++) {
    240   1.6    bouyer 		numdirs += fs2h16(sblock.e2fs_gd[cg].ext2bgd_ndirs);
    241   1.1    bouyer 	}
    242   1.1    bouyer 	inplast = 0;
    243   1.1    bouyer 	listmax = numdirs + 10;
    244   1.1    bouyer 	inpsort = (struct inoinfo **)calloc((unsigned)listmax,
    245   1.1    bouyer 		sizeof(struct inoinfo *));
    246   1.1    bouyer 	inphead = (struct inoinfo **)calloc((unsigned)numdirs,
    247   1.1    bouyer 		sizeof(struct inoinfo *));
    248   1.1    bouyer 	if (inpsort == NULL || inphead == NULL) {
    249   1.1    bouyer 		printf("cannot alloc %u bytes for inphead\n",
    250   1.5       mrg 			(unsigned)(numdirs * sizeof(struct inoinfo *)));
    251   1.1    bouyer 		goto badsblabel;
    252   1.1    bouyer 	}
    253   1.1    bouyer 	bufinit();
    254   1.1    bouyer 	return (1);
    255   1.1    bouyer 
    256   1.1    bouyer badsblabel:
    257   1.1    bouyer 	ckfini(0);
    258   1.1    bouyer 	return (0);
    259   1.1    bouyer }
    260   1.1    bouyer 
    261   1.1    bouyer /*
    262   1.6    bouyer  * Read in the super block and its summary info, convert to host byte order.
    263   1.1    bouyer  */
    264   1.1    bouyer static int
    265   1.1    bouyer readsb(listerr)
    266   1.1    bouyer 	int listerr;
    267   1.1    bouyer {
    268   1.1    bouyer 	daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
    269   1.1    bouyer 
    270   1.6    bouyer 	if (bread(fsreadfd, (char *)sblk.b_un.b_fs, super, (long)SBSIZE) != 0)
    271   1.1    bouyer 		return (0);
    272   1.1    bouyer 	sblk.b_bno = super;
    273   1.1    bouyer 	sblk.b_size = SBSIZE;
    274   1.6    bouyer 
    275   1.6    bouyer 	/* Copy the superblock in memory */
    276  1.12    bouyer 	e2fs_sbload(sblk.b_un.b_fs, &sblock.e2fs);
    277   1.6    bouyer 
    278   1.1    bouyer 	/*
    279   1.1    bouyer 	 * run a few consistency checks of the super block
    280   1.1    bouyer 	 */
    281   1.1    bouyer 	if (sblock.e2fs.e2fs_magic != E2FS_MAGIC) {
    282   1.1    bouyer 		badsb(listerr, "MAGIC NUMBER WRONG"); return (0);
    283   1.1    bouyer 	}
    284   1.1    bouyer 	if (sblock.e2fs.e2fs_log_bsize > 2) {
    285   1.1    bouyer 		badsb(listerr, "BAD LOG_BSIZE"); return (0);
    286   1.1    bouyer 	}
    287   1.1    bouyer 
    288   1.1    bouyer 	/* compute the dynamic fields of the in-memory sb */
    289   1.1    bouyer 	/* compute dynamic sb infos */
    290   1.1    bouyer 	sblock.e2fs_ncg =
    291   1.1    bouyer 		howmany(sblock.e2fs.e2fs_bcount - sblock.e2fs.e2fs_first_dblock,
    292   1.1    bouyer 		sblock.e2fs.e2fs_bpg);
    293   1.1    bouyer 	/* XXX assume hw bsize = 512 */
    294   1.1    bouyer 	sblock.e2fs_fsbtodb = sblock.e2fs.e2fs_log_bsize + 1;
    295   1.1    bouyer 	sblock.e2fs_bsize = 1024 << sblock.e2fs.e2fs_log_bsize;
    296   1.1    bouyer 	sblock.e2fs_bshift = LOG_MINBSIZE + sblock.e2fs.e2fs_log_bsize;
    297   1.1    bouyer 	sblock.e2fs_qbmask = sblock.e2fs_bsize - 1;
    298   1.1    bouyer 	sblock.e2fs_bmask = ~sblock.e2fs_qbmask;
    299   1.1    bouyer 	sblock.e2fs_ngdb = howmany(sblock.e2fs_ncg,
    300   1.1    bouyer 		sblock.e2fs_bsize / sizeof(struct ext2_gd));
    301   1.1    bouyer 	sblock.e2fs_ipb = sblock.e2fs_bsize / sizeof(struct ext2fs_dinode);
    302   1.1    bouyer 	sblock.e2fs_itpg = sblock.e2fs.e2fs_ipg/sblock.e2fs_ipb;
    303   1.1    bouyer 
    304   1.1    bouyer 	/*
    305   1.1    bouyer 	 * Compute block size that the filesystem is based on,
    306   1.1    bouyer 	 * according to fsbtodb, and adjust superblock block number
    307   1.1    bouyer 	 * so we can tell if this is an alternate later.
    308   1.1    bouyer 	 */
    309   1.1    bouyer 	super *= dev_bsize;
    310   1.1    bouyer 	dev_bsize = sblock.e2fs_bsize / fsbtodb(&sblock, 1);
    311   1.1    bouyer 	sblk.b_bno = super / dev_bsize;
    312   1.6    bouyer 
    313   1.6    bouyer 	getblk(&asblk, 1 * sblock.e2fs.e2fs_bpg + sblock.e2fs.e2fs_first_dblock,
    314   1.6    bouyer 		(long)SBSIZE);
    315   1.6    bouyer 	if (asblk.b_errs)
    316   1.6    bouyer 		return (0);
    317   1.1    bouyer 	if (bflag) {
    318   1.1    bouyer 		havesb = 1;
    319   1.1    bouyer 		return (1);
    320   1.1    bouyer 	}
    321   1.1    bouyer 
    322   1.1    bouyer 	/*
    323   1.1    bouyer 	 * Set all possible fields that could differ, then do check
    324   1.1    bouyer 	 * of whole super block against an alternate super block.
    325   1.1    bouyer 	 * When an alternate super-block is specified this check is skipped.
    326   1.1    bouyer 	 */
    327   1.6    bouyer 	asblk.b_un.b_fs->e2fs_rbcount = sblk.b_un.b_fs->e2fs_rbcount;
    328   1.6    bouyer 	asblk.b_un.b_fs->e2fs_fbcount = sblk.b_un.b_fs->e2fs_fbcount;
    329   1.6    bouyer 	asblk.b_un.b_fs->e2fs_ficount = sblk.b_un.b_fs->e2fs_ficount;
    330   1.6    bouyer 	asblk.b_un.b_fs->e2fs_mtime = sblk.b_un.b_fs->e2fs_mtime;
    331   1.6    bouyer 	asblk.b_un.b_fs->e2fs_wtime = sblk.b_un.b_fs->e2fs_wtime;
    332   1.6    bouyer 	asblk.b_un.b_fs->e2fs_mnt_count = sblk.b_un.b_fs->e2fs_mnt_count;
    333   1.6    bouyer 	asblk.b_un.b_fs->e2fs_max_mnt_count = sblk.b_un.b_fs->e2fs_max_mnt_count;
    334   1.6    bouyer 	asblk.b_un.b_fs->e2fs_state = sblk.b_un.b_fs->e2fs_state;
    335   1.6    bouyer 	asblk.b_un.b_fs->e2fs_beh = sblk.b_un.b_fs->e2fs_beh;
    336   1.6    bouyer 	asblk.b_un.b_fs->e2fs_lastfsck = sblk.b_un.b_fs->e2fs_lastfsck;
    337   1.6    bouyer 	asblk.b_un.b_fs->e2fs_fsckintv = sblk.b_un.b_fs->e2fs_fsckintv;
    338   1.6    bouyer 	asblk.b_un.b_fs->e2fs_ruid = sblk.b_un.b_fs->e2fs_ruid;
    339   1.6    bouyer 	asblk.b_un.b_fs->e2fs_rgid = sblk.b_un.b_fs->e2fs_rgid;
    340  1.10    bouyer 	asblk.b_un.b_fs->e2fs_block_group_nr =
    341  1.10    bouyer 	    sblk.b_un.b_fs->e2fs_block_group_nr;
    342  1.12    bouyer 	if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
    343  1.12    bouyer 	    ((sblock.e2fs.e2fs_features_incompat & ~EXT2F_INCOMPAT_SUPP) ||
    344  1.12    bouyer 	    (sblock.e2fs.e2fs_features_rocompat & ~EXT2F_ROCOMPAT_SUPP))) {
    345  1.10    bouyer 		if (debug) {
    346  1.10    bouyer 			printf("compat 0x%08x, incompat 0x%08x, compat_ro "
    347  1.10    bouyer 			    "0x%08x\n",
    348  1.12    bouyer 			    sblock.e2fs.e2fs_features_compat,
    349  1.12    bouyer 			    sblock.e2fs.e2fs_features_incompat,
    350  1.12    bouyer 			    sblock.e2fs.e2fs_features_rocompat);
    351  1.10    bouyer 		}
    352  1.11    bouyer 		badsb(listerr,"INCOMPATIBLE FEATURE BITS IN SUPER BLOCK");
    353  1.10    bouyer 		return 0;
    354  1.10    bouyer 	}
    355   1.6    bouyer 	if (memcmp(sblk.b_un.b_fs, asblk.b_un.b_fs, SBSIZE)) {
    356   1.1    bouyer 		if (debug) {
    357   1.6    bouyer 			u_int32_t *nlp, *olp, *endlp;
    358   1.1    bouyer 
    359   1.1    bouyer 			printf("superblock mismatches\n");
    360   1.6    bouyer 			nlp = (u_int32_t *)asblk.b_un.b_fs;
    361   1.6    bouyer 			olp = (u_int32_t *)sblk.b_un.b_fs;
    362   1.1    bouyer 			endlp = olp + (SBSIZE / sizeof *olp);
    363   1.1    bouyer 			for ( ; olp < endlp; olp++, nlp++) {
    364   1.1    bouyer 				if (*olp == *nlp)
    365   1.1    bouyer 					continue;
    366   1.5       mrg 				printf("offset %ld, original %ld, alternate %ld\n",
    367   1.6    bouyer 					(long)(olp - (u_int32_t *)sblk.b_un.b_fs),
    368   1.6    bouyer 					(long)fs2h32(*olp),
    369   1.6    bouyer 					(long)fs2h32(*nlp));
    370   1.1    bouyer 			}
    371   1.1    bouyer 		}
    372   1.1    bouyer 		badsb(listerr,
    373   1.1    bouyer 		"VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
    374   1.1    bouyer 		return (0);
    375   1.1    bouyer 	}
    376   1.1    bouyer 	havesb = 1;
    377   1.1    bouyer 	return (1);
    378   1.1    bouyer }
    379   1.1    bouyer 
    380   1.1    bouyer void
    381   1.6    bouyer copyback_sb(bp)
    382   1.6    bouyer 	struct bufarea *bp;
    383   1.6    bouyer {
    384   1.6    bouyer 	/* Copy the in-memory superblock back to buffer */
    385   1.6    bouyer 	bp->b_un.b_fs->e2fs_icount = fs2h32(sblock.e2fs.e2fs_icount);
    386   1.6    bouyer 	bp->b_un.b_fs->e2fs_bcount = fs2h32(sblock.e2fs.e2fs_bcount);
    387   1.6    bouyer 	bp->b_un.b_fs->e2fs_rbcount = fs2h32(sblock.e2fs.e2fs_rbcount);
    388   1.6    bouyer 	bp->b_un.b_fs->e2fs_fbcount = fs2h32(sblock.e2fs.e2fs_fbcount);
    389   1.6    bouyer 	bp->b_un.b_fs->e2fs_ficount = fs2h32(sblock.e2fs.e2fs_ficount);
    390   1.6    bouyer 	bp->b_un.b_fs->e2fs_first_dblock =
    391  1.12    bouyer 					fs2h32(sblock.e2fs.e2fs_first_dblock);
    392   1.6    bouyer 	bp->b_un.b_fs->e2fs_log_bsize = fs2h32(sblock.e2fs.e2fs_log_bsize);
    393   1.6    bouyer 	bp->b_un.b_fs->e2fs_fsize = fs2h32(sblock.e2fs.e2fs_fsize);
    394   1.6    bouyer 	bp->b_un.b_fs->e2fs_bpg = fs2h32(sblock.e2fs.e2fs_bpg);
    395   1.6    bouyer 	bp->b_un.b_fs->e2fs_fpg = fs2h32(sblock.e2fs.e2fs_fpg);
    396   1.6    bouyer 	bp->b_un.b_fs->e2fs_ipg = fs2h32(sblock.e2fs.e2fs_ipg);
    397   1.6    bouyer 	bp->b_un.b_fs->e2fs_mtime = fs2h32(sblock.e2fs.e2fs_mtime);
    398   1.6    bouyer 	bp->b_un.b_fs->e2fs_wtime = fs2h32(sblock.e2fs.e2fs_wtime);
    399   1.6    bouyer 	bp->b_un.b_fs->e2fs_lastfsck = fs2h32(sblock.e2fs.e2fs_lastfsck);
    400   1.6    bouyer 	bp->b_un.b_fs->e2fs_fsckintv = fs2h32(sblock.e2fs.e2fs_fsckintv);
    401   1.6    bouyer 	bp->b_un.b_fs->e2fs_creator = fs2h32(sblock.e2fs.e2fs_creator);
    402   1.6    bouyer 	bp->b_un.b_fs->e2fs_rev = fs2h32(sblock.e2fs.e2fs_rev);
    403   1.6    bouyer 	bp->b_un.b_fs->e2fs_mnt_count = fs2h16(sblock.e2fs.e2fs_mnt_count);
    404   1.6    bouyer 	bp->b_un.b_fs->e2fs_max_mnt_count =
    405  1.12    bouyer 					fs2h16(sblock.e2fs.e2fs_max_mnt_count);
    406   1.6    bouyer 	bp->b_un.b_fs->e2fs_magic = fs2h16(sblock.e2fs.e2fs_magic);
    407   1.6    bouyer 	bp->b_un.b_fs->e2fs_state = fs2h16(sblock.e2fs.e2fs_state);
    408   1.6    bouyer 	bp->b_un.b_fs->e2fs_beh = fs2h16(sblock.e2fs.e2fs_beh);
    409   1.6    bouyer 	bp->b_un.b_fs->e2fs_ruid = fs2h16(sblock.e2fs.e2fs_ruid);
    410   1.6    bouyer 	bp->b_un.b_fs->e2fs_rgid = fs2h16(sblock.e2fs.e2fs_rgid);
    411   1.6    bouyer }
    412   1.6    bouyer 
    413   1.6    bouyer void
    414   1.1    bouyer badsb(listerr, s)
    415   1.1    bouyer 	int listerr;
    416   1.1    bouyer 	char *s;
    417   1.1    bouyer {
    418   1.1    bouyer 
    419   1.1    bouyer 	if (!listerr)
    420   1.1    bouyer 		return;
    421   1.1    bouyer 	if (preen)
    422   1.1    bouyer 		printf("%s: ", cdevname());
    423   1.1    bouyer 	pfatal("BAD SUPER BLOCK: %s\n", s);
    424   1.1    bouyer }
    425   1.1    bouyer 
    426   1.1    bouyer /*
    427   1.1    bouyer  * Calculate a prototype superblock based on information in the disk label.
    428   1.1    bouyer  * When done the cgsblock macro can be calculated and the fs_ncg field
    429   1.1    bouyer  * can be used. Do NOT attempt to use other macros without verifying that
    430   1.1    bouyer  * their needed information is available!
    431   1.1    bouyer  */
    432   1.1    bouyer 
    433   1.1    bouyer int
    434   1.1    bouyer calcsb(dev, devfd, fs)
    435   1.7   mycroft 	const char *dev;
    436   1.1    bouyer 	int devfd;
    437   1.4     lukem 	struct m_ext2fs *fs;
    438   1.1    bouyer {
    439   1.4     lukem 	struct disklabel *lp;
    440   1.4     lukem 	struct partition *pp;
    441   1.4     lukem 	char *cp;
    442   1.1    bouyer 
    443   1.1    bouyer 	cp = strchr(dev, '\0') - 1;
    444   1.1    bouyer 	if ((cp == (char *)-1 || (*cp < 'a' || *cp > 'h')) && !isdigit(*cp)) {
    445   1.1    bouyer 		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
    446   1.1    bouyer 		return (0);
    447   1.1    bouyer 	}
    448   1.1    bouyer 	lp = getdisklabel(dev, devfd);
    449   1.1    bouyer 	if (isdigit(*cp))
    450   1.1    bouyer 		pp = &lp->d_partitions[0];
    451   1.1    bouyer 	else
    452   1.1    bouyer 		pp = &lp->d_partitions[*cp - 'a'];
    453   1.6    bouyer 	if (pp->p_fstype != FS_EX2FS) {
    454   1.6    bouyer 		pfatal("%s: NOT LABELED AS A EXT2 FILE SYSTEM (%s)\n",
    455   1.1    bouyer 			dev, pp->p_fstype < FSMAXTYPES ?
    456   1.1    bouyer 			fstypenames[pp->p_fstype] : "unknown");
    457   1.1    bouyer 		return (0);
    458   1.1    bouyer 	}
    459   1.1    bouyer 	memset(fs, 0, sizeof(struct m_ext2fs));
    460   1.6    bouyer 	fs->e2fs_bsize = pp->p_fsize;
    461   1.6    bouyer 	fs->e2fs.e2fs_log_bsize = pp->p_fsize / 1024;
    462   1.1    bouyer 	fs->e2fs.e2fs_bcount = (pp->p_size * DEV_BSIZE) / fs->e2fs_bsize;
    463   1.6    bouyer 	fs->e2fs.e2fs_first_dblock = (fs->e2fs.e2fs_log_bsize == 0) ? 1 : 0;
    464   1.1    bouyer 	fs->e2fs.e2fs_bpg = fs->e2fs_bsize * NBBY;
    465   1.1    bouyer 	fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs.e2fs_log_bsize;
    466   1.1    bouyer 	fs->e2fs_qbmask = fs->e2fs_bsize - 1;
    467   1.1    bouyer 	fs->e2fs_bmask = ~fs->e2fs_qbmask;
    468   1.1    bouyer 	fs->e2fs_ncg =
    469   1.1    bouyer 		howmany(fs->e2fs.e2fs_bcount - fs->e2fs.e2fs_first_dblock,
    470   1.1    bouyer 		fs->e2fs.e2fs_bpg);
    471   1.1    bouyer 	fs->e2fs_fsbtodb = fs->e2fs.e2fs_log_bsize + 1;
    472   1.1    bouyer 	fs->e2fs_ngdb = howmany(fs->e2fs_ncg,
    473   1.1    bouyer 		fs->e2fs_bsize / sizeof(struct ext2_gd));
    474   1.1    bouyer 
    475   1.1    bouyer 	return (1);
    476   1.1    bouyer }
    477   1.1    bouyer 
    478   1.1    bouyer static struct disklabel *
    479   1.1    bouyer getdisklabel(s, fd)
    480   1.7   mycroft 	const char *s;
    481   1.1    bouyer 	int	fd;
    482   1.1    bouyer {
    483   1.1    bouyer 	static struct disklabel lab;
    484   1.1    bouyer 
    485   1.1    bouyer 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
    486   1.1    bouyer 		if (s == NULL)
    487   1.1    bouyer 			return ((struct disklabel *)NULL);
    488   1.1    bouyer 		pwarn("ioctl (GCINFO): %s\n", strerror(errno));
    489   1.1    bouyer 		errexit("%s: can't read disk label\n", s);
    490   1.1    bouyer 	}
    491   1.1    bouyer 	return (&lab);
    492  1.12    bouyer }
    493  1.12    bouyer 
    494  1.12    bouyer daddr_t
    495  1.12    bouyer cgoverhead(c)
    496  1.12    bouyer 	int c;
    497  1.12    bouyer {
    498  1.12    bouyer 	int overh;
    499  1.12    bouyer 	overh =	1 /* block bitmap */ +
    500  1.12    bouyer 		1 /* inode bitmap */ +
    501  1.12    bouyer 		sblock.e2fs_itpg;
    502  1.12    bouyer 	if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
    503  1.12    bouyer 	    sblock.e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) {
    504  1.12    bouyer 		if (cg_has_sb(c) == 0)
    505  1.12    bouyer 			return overh;
    506  1.12    bouyer 	}
    507  1.12    bouyer 	overh += 1 + sblock.e2fs_ngdb;
    508  1.12    bouyer 	return overh;
    509   1.1    bouyer }
    510