1*0a6a1f1dSLionel Sambuc /* $NetBSD: setup.c,v 1.32 2014/12/04 01:41:37 christos Exp $ */
294715d8eSBen Gras
394715d8eSBen Gras /*
494715d8eSBen Gras * Copyright (c) 1980, 1986, 1993
594715d8eSBen Gras * The Regents of the University of California. All rights reserved.
694715d8eSBen Gras *
794715d8eSBen Gras * Redistribution and use in source and binary forms, with or without
894715d8eSBen Gras * modification, are permitted provided that the following conditions
994715d8eSBen Gras * are met:
1094715d8eSBen Gras * 1. Redistributions of source code must retain the above copyright
1194715d8eSBen Gras * notice, this list of conditions and the following disclaimer.
1294715d8eSBen Gras * 2. Redistributions in binary form must reproduce the above copyright
1394715d8eSBen Gras * notice, this list of conditions and the following disclaimer in the
1494715d8eSBen Gras * documentation and/or other materials provided with the distribution.
1594715d8eSBen Gras * 3. Neither the name of the University nor the names of its contributors
1694715d8eSBen Gras * may be used to endorse or promote products derived from this software
1794715d8eSBen Gras * without specific prior written permission.
1894715d8eSBen Gras *
1994715d8eSBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2094715d8eSBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2194715d8eSBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2294715d8eSBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2394715d8eSBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2494715d8eSBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2594715d8eSBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2694715d8eSBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2794715d8eSBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2894715d8eSBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2994715d8eSBen Gras * SUCH DAMAGE.
3094715d8eSBen Gras */
3194715d8eSBen Gras
3294715d8eSBen Gras /*
3394715d8eSBen Gras * Copyright (c) 1997 Manuel Bouyer.
3494715d8eSBen Gras *
3594715d8eSBen Gras * Redistribution and use in source and binary forms, with or without
3694715d8eSBen Gras * modification, are permitted provided that the following conditions
3794715d8eSBen Gras * are met:
3894715d8eSBen Gras * 1. Redistributions of source code must retain the above copyright
3994715d8eSBen Gras * notice, this list of conditions and the following disclaimer.
4094715d8eSBen Gras * 2. Redistributions in binary form must reproduce the above copyright
4194715d8eSBen Gras * notice, this list of conditions and the following disclaimer in the
4294715d8eSBen Gras * documentation and/or other materials provided with the distribution.
4394715d8eSBen Gras *
4494715d8eSBen Gras * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
4594715d8eSBen Gras * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
4694715d8eSBen Gras * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
4794715d8eSBen Gras * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
4894715d8eSBen Gras * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4994715d8eSBen Gras * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
5094715d8eSBen Gras * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
5194715d8eSBen Gras * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
5294715d8eSBen Gras * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
5394715d8eSBen Gras * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5494715d8eSBen Gras */
5594715d8eSBen Gras
5694715d8eSBen Gras #include <sys/cdefs.h>
5794715d8eSBen Gras #ifndef lint
5894715d8eSBen Gras #if 0
5994715d8eSBen Gras static char sccsid[] = "@(#)setup.c 8.5 (Berkeley) 11/23/94";
6094715d8eSBen Gras #else
61*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: setup.c,v 1.32 2014/12/04 01:41:37 christos Exp $");
6294715d8eSBen Gras #endif
6394715d8eSBen Gras #endif /* not lint */
6494715d8eSBen Gras
6594715d8eSBen Gras #define FSTYPENAMES
6694715d8eSBen Gras #include <sys/param.h>
6794715d8eSBen Gras #include <sys/time.h>
6894715d8eSBen Gras #include <ufs/ext2fs/ext2fs_dinode.h>
6994715d8eSBen Gras #include <ufs/ext2fs/ext2fs.h>
7094715d8eSBen Gras #include <sys/stat.h>
7194715d8eSBen Gras #include <sys/ioctl.h>
7294715d8eSBen Gras #include <sys/disklabel.h>
7394715d8eSBen Gras #include <sys/file.h>
7494715d8eSBen Gras
7594715d8eSBen Gras #include <errno.h>
7694715d8eSBen Gras #include <stdio.h>
7794715d8eSBen Gras #include <stdlib.h>
7894715d8eSBen Gras #include <string.h>
7994715d8eSBen Gras #include <ctype.h>
8094715d8eSBen Gras
8194715d8eSBen Gras #include "fsck.h"
8294715d8eSBen Gras #include "extern.h"
8394715d8eSBen Gras #include "fsutil.h"
8494715d8eSBen Gras #include "exitvalues.h"
8594715d8eSBen Gras
8694715d8eSBen Gras void badsb(int, const char *);
8794715d8eSBen Gras int calcsb(const char *, int, struct m_ext2fs *);
8894715d8eSBen Gras static struct disklabel *getdisklabel(const char *, int);
8994715d8eSBen Gras static int readsb(int);
9094715d8eSBen Gras
9194715d8eSBen Gras int
setup(const char * dev)9294715d8eSBen Gras setup(const char *dev)
9394715d8eSBen Gras {
9494715d8eSBen Gras long cg, asked, i;
9594715d8eSBen Gras long bmapsize;
9694715d8eSBen Gras struct disklabel *lp;
9794715d8eSBen Gras off_t sizepb;
9894715d8eSBen Gras struct stat statb;
9994715d8eSBen Gras struct m_ext2fs proto;
10094715d8eSBen Gras int doskipclean;
10194715d8eSBen Gras u_int64_t maxfilesize;
10294715d8eSBen Gras
10394715d8eSBen Gras havesb = 0;
10494715d8eSBen Gras fswritefd = -1;
10594715d8eSBen Gras doskipclean = skipclean;
10694715d8eSBen Gras if (stat(dev, &statb) < 0) {
10794715d8eSBen Gras printf("Can't stat %s: %s\n", dev, strerror(errno));
10894715d8eSBen Gras return 0;
10994715d8eSBen Gras }
11084d9c625SLionel Sambuc #if !defined(__minix)
11194715d8eSBen Gras if (!S_ISCHR(statb.st_mode)) {
11294715d8eSBen Gras pfatal("%s is not a character device", dev);
11394715d8eSBen Gras if (reply("CONTINUE") == 0)
11494715d8eSBen Gras return 0;
11594715d8eSBen Gras }
11684d9c625SLionel Sambuc #endif /* !defined(__minix) */
11794715d8eSBen Gras if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
11894715d8eSBen Gras printf("Can't open %s: %s\n", dev, strerror(errno));
11994715d8eSBen Gras return 0;
12094715d8eSBen Gras }
12194715d8eSBen Gras if (preen == 0)
12294715d8eSBen Gras printf("** %s", dev);
12394715d8eSBen Gras if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
12494715d8eSBen Gras fswritefd = -1;
12594715d8eSBen Gras if (preen)
12694715d8eSBen Gras pfatal("NO WRITE ACCESS");
12794715d8eSBen Gras printf(" (NO WRITE)");
12894715d8eSBen Gras }
12994715d8eSBen Gras if (preen == 0)
13094715d8eSBen Gras printf("\n");
13194715d8eSBen Gras fsmodified = 0;
13294715d8eSBen Gras lfdir = 0;
13394715d8eSBen Gras initbarea(&sblk);
13494715d8eSBen Gras initbarea(&asblk);
13594715d8eSBen Gras sblk.b_un.b_buf = malloc(SBSIZE);
13694715d8eSBen Gras asblk.b_un.b_buf = malloc(SBSIZE);
13794715d8eSBen Gras if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
13894715d8eSBen Gras errexit("cannot allocate space for superblock");
13994715d8eSBen Gras if ((lp = getdisklabel(NULL, fsreadfd)) != NULL)
14094715d8eSBen Gras dev_bsize = secsize = lp->d_secsize;
14194715d8eSBen Gras else
14294715d8eSBen Gras dev_bsize = secsize = DEV_BSIZE;
14394715d8eSBen Gras /*
14494715d8eSBen Gras * Read in the superblock, looking for alternates if necessary
14594715d8eSBen Gras */
14694715d8eSBen Gras if (readsb(1) == 0) {
14794715d8eSBen Gras if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
14894715d8eSBen Gras return 0;
14994715d8eSBen Gras if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
15094715d8eSBen Gras return 0;
15194715d8eSBen Gras for (cg = 1; cg < proto.e2fs_ncg; cg++) {
15284d9c625SLionel Sambuc bflag = EXT2_FSBTODB(&proto,
15394715d8eSBen Gras cg * proto.e2fs.e2fs_bpg +
15494715d8eSBen Gras proto.e2fs.e2fs_first_dblock);
15594715d8eSBen Gras if (readsb(0) != 0)
15694715d8eSBen Gras break;
15794715d8eSBen Gras }
15894715d8eSBen Gras if (cg >= proto.e2fs_ncg) {
15994715d8eSBen Gras printf("%s %s\n%s %s\n%s %s\n",
16094715d8eSBen Gras "SEARCH FOR ALTERNATE SUPER-BLOCK",
16194715d8eSBen Gras "FAILED. YOU MUST USE THE",
16294715d8eSBen Gras "-b OPTION TO FSCK_FFS TO SPECIFY THE",
16394715d8eSBen Gras "LOCATION OF AN ALTERNATE",
16494715d8eSBen Gras "SUPER-BLOCK TO SUPPLY NEEDED",
16594715d8eSBen Gras "INFORMATION; SEE fsck_ext2fs(8).");
16694715d8eSBen Gras return 0;
16794715d8eSBen Gras }
16894715d8eSBen Gras doskipclean = 0;
16994715d8eSBen Gras pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
17094715d8eSBen Gras }
17194715d8eSBen Gras if (debug)
17294715d8eSBen Gras printf("state = %d\n", sblock.e2fs.e2fs_state);
17394715d8eSBen Gras if (sblock.e2fs.e2fs_state == E2FS_ISCLEAN) {
17494715d8eSBen Gras if (doskipclean) {
17594715d8eSBen Gras pwarn("%sile system is clean; not checking\n",
17694715d8eSBen Gras preen ? "f" : "** F");
17794715d8eSBen Gras return -1;
17894715d8eSBen Gras }
17994715d8eSBen Gras if (!preen)
18094715d8eSBen Gras pwarn("** File system is already clean\n");
18194715d8eSBen Gras }
18294715d8eSBen Gras maxfsblock = sblock.e2fs.e2fs_bcount;
18394715d8eSBen Gras maxino = sblock.e2fs_ncg * sblock.e2fs.e2fs_ipg;
18494715d8eSBen Gras sizepb = sblock.e2fs_bsize;
18584d9c625SLionel Sambuc maxfilesize = sblock.e2fs_bsize * EXT2FS_NDADDR - 1;
18684d9c625SLionel Sambuc for (i = 0; i < EXT2FS_NIADDR; i++) {
18784d9c625SLionel Sambuc sizepb *= EXT2_NINDIR(&sblock);
18894715d8eSBen Gras maxfilesize += sizepb;
18994715d8eSBen Gras }
19094715d8eSBen Gras /*
19194715d8eSBen Gras * Check and potentially fix certain fields in the super block.
19294715d8eSBen Gras */
19394715d8eSBen Gras if (/* (sblock.e2fs.e2fs_rbcount < 0) || */
19494715d8eSBen Gras (sblock.e2fs.e2fs_rbcount > sblock.e2fs.e2fs_bcount)) {
19594715d8eSBen Gras pfatal("IMPOSSIBLE RESERVED BLOCK COUNT=%d IN SUPERBLOCK",
19694715d8eSBen Gras sblock.e2fs.e2fs_rbcount);
19794715d8eSBen Gras if (reply("SET TO DEFAULT") == 1) {
19894715d8eSBen Gras sblock.e2fs.e2fs_rbcount =
19994715d8eSBen Gras sblock.e2fs.e2fs_bcount * MINFREE / 100;
20094715d8eSBen Gras sbdirty();
20194715d8eSBen Gras dirty(&asblk);
20294715d8eSBen Gras }
20394715d8eSBen Gras }
20494715d8eSBen Gras if (sblock.e2fs.e2fs_bpg != sblock.e2fs.e2fs_fpg) {
20594715d8eSBen Gras pfatal("WRONG FPG=%d (BPG=%d) IN SUPERBLOCK",
20694715d8eSBen Gras sblock.e2fs.e2fs_fpg, sblock.e2fs.e2fs_bpg);
20794715d8eSBen Gras return 0;
20894715d8eSBen Gras }
20994715d8eSBen Gras if (asblk.b_dirty && !bflag) {
21094715d8eSBen Gras copyback_sb(&asblk);
21194715d8eSBen Gras flush(fswritefd, &asblk);
21294715d8eSBen Gras }
21394715d8eSBen Gras /*
21494715d8eSBen Gras * read in the summary info.
21594715d8eSBen Gras */
21694715d8eSBen Gras
21794715d8eSBen Gras sblock.e2fs_gd = malloc(sblock.e2fs_ngdb * sblock.e2fs_bsize);
21894715d8eSBen Gras if (sblock.e2fs_gd == NULL)
21994715d8eSBen Gras errexit("out of memory");
22094715d8eSBen Gras asked = 0;
22194715d8eSBen Gras for (i = 0; i < sblock.e2fs_ngdb; i++) {
22294715d8eSBen Gras if (bread(fsreadfd,
22394715d8eSBen Gras (char *)&sblock.e2fs_gd[i * sblock.e2fs_bsize /
22494715d8eSBen Gras sizeof(struct ext2_gd)],
22584d9c625SLionel Sambuc EXT2_FSBTODB(&sblock, ((sblock.e2fs_bsize > 1024) ? 0 : 1) +
22694715d8eSBen Gras i + 1),
22794715d8eSBen Gras sblock.e2fs_bsize) != 0 && !asked) {
22894715d8eSBen Gras pfatal("BAD SUMMARY INFORMATION");
22994715d8eSBen Gras if (reply("CONTINUE") == 0)
23094715d8eSBen Gras exit(FSCK_EXIT_CHECK_FAILED);
23194715d8eSBen Gras asked++;
23294715d8eSBen Gras }
23394715d8eSBen Gras }
23494715d8eSBen Gras /*
23594715d8eSBen Gras * allocate and initialize the necessary maps
23694715d8eSBen Gras */
23794715d8eSBen Gras bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
23894715d8eSBen Gras blockmap = calloc((unsigned int)bmapsize, sizeof(char));
23994715d8eSBen Gras if (blockmap == NULL) {
24094715d8eSBen Gras printf("cannot alloc %u bytes for blockmap\n",
24194715d8eSBen Gras (unsigned int)bmapsize);
24294715d8eSBen Gras goto badsblabel;
24394715d8eSBen Gras }
24494715d8eSBen Gras statemap = calloc((unsigned int)(maxino + 2), sizeof(char));
24594715d8eSBen Gras if (statemap == NULL) {
24694715d8eSBen Gras printf("cannot alloc %u bytes for statemap\n",
24794715d8eSBen Gras (unsigned int)(maxino + 1));
24894715d8eSBen Gras goto badsblabel;
24994715d8eSBen Gras }
25094715d8eSBen Gras typemap = calloc((unsigned int)(maxino + 1), sizeof(char));
25194715d8eSBen Gras if (typemap == NULL) {
25294715d8eSBen Gras printf("cannot alloc %u bytes for typemap\n",
25394715d8eSBen Gras (unsigned int)(maxino + 1));
25494715d8eSBen Gras goto badsblabel;
25594715d8eSBen Gras }
25694715d8eSBen Gras lncntp = calloc((unsigned)(maxino + 1), sizeof(int16_t));
25794715d8eSBen Gras if (lncntp == NULL) {
25894715d8eSBen Gras printf("cannot alloc %u bytes for lncntp\n",
25994715d8eSBen Gras (unsigned int)((maxino + 1) * sizeof(int16_t)));
26094715d8eSBen Gras goto badsblabel;
26194715d8eSBen Gras }
26294715d8eSBen Gras for (numdirs = 0, cg = 0; cg < sblock.e2fs_ncg; cg++) {
26394715d8eSBen Gras numdirs += fs2h16(sblock.e2fs_gd[cg].ext2bgd_ndirs);
26494715d8eSBen Gras }
26594715d8eSBen Gras inplast = 0;
26694715d8eSBen Gras listmax = numdirs + 10;
26794715d8eSBen Gras inpsort = calloc((unsigned int)listmax, sizeof(struct inoinfo *));
26894715d8eSBen Gras inphead = calloc((unsigned int)numdirs, sizeof(struct inoinfo *));
26994715d8eSBen Gras if (inpsort == NULL || inphead == NULL) {
27094715d8eSBen Gras printf("cannot alloc %u bytes for inphead\n",
27194715d8eSBen Gras (unsigned int)(numdirs * sizeof(struct inoinfo *)));
27294715d8eSBen Gras goto badsblabel;
27394715d8eSBen Gras }
27494715d8eSBen Gras bufinit();
27594715d8eSBen Gras return 1;
27694715d8eSBen Gras
27794715d8eSBen Gras badsblabel:
27894715d8eSBen Gras ckfini(0);
27994715d8eSBen Gras return 0;
28094715d8eSBen Gras }
28194715d8eSBen Gras
28294715d8eSBen Gras /*
28394715d8eSBen Gras * Read in the super block and its summary info, convert to host byte order.
28494715d8eSBen Gras */
28594715d8eSBen Gras static int
readsb(int listerr)28694715d8eSBen Gras readsb(int listerr)
28794715d8eSBen Gras {
28894715d8eSBen Gras daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
28994715d8eSBen Gras
29094715d8eSBen Gras if (bread(fsreadfd, (char *)sblk.b_un.b_fs, super, (long)SBSIZE) != 0)
29194715d8eSBen Gras return 0;
29294715d8eSBen Gras sblk.b_bno = super;
29394715d8eSBen Gras sblk.b_size = SBSIZE;
29494715d8eSBen Gras
29594715d8eSBen Gras /* Copy the superblock in memory */
29694715d8eSBen Gras e2fs_sbload(sblk.b_un.b_fs, &sblock.e2fs);
29794715d8eSBen Gras
29894715d8eSBen Gras /*
29994715d8eSBen Gras * run a few consistency checks of the super block
30094715d8eSBen Gras */
30194715d8eSBen Gras if (sblock.e2fs.e2fs_magic != E2FS_MAGIC) {
30294715d8eSBen Gras badsb(listerr, "MAGIC NUMBER WRONG");
30394715d8eSBen Gras return 0;
30494715d8eSBen Gras }
30594715d8eSBen Gras if (sblock.e2fs.e2fs_log_bsize > 2) {
30694715d8eSBen Gras badsb(listerr, "BAD LOG_BSIZE");
30794715d8eSBen Gras return 0;
30894715d8eSBen Gras }
30994715d8eSBen Gras if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
31094715d8eSBen Gras (!powerof2(sblock.e2fs.e2fs_inode_size) ||
31194715d8eSBen Gras sblock.e2fs.e2fs_inode_size < sizeof(struct ext2fs_dinode) ||
31294715d8eSBen Gras sblock.e2fs.e2fs_inode_size >
31394715d8eSBen Gras (1024 << sblock.e2fs.e2fs_log_bsize))) {
31494715d8eSBen Gras badsb(listerr, "BAD INODE_SIZE");
31594715d8eSBen Gras return 0;
31694715d8eSBen Gras }
31794715d8eSBen Gras
31894715d8eSBen Gras /* compute the dynamic fields of the in-memory sb */
31994715d8eSBen Gras /* compute dynamic sb infos */
32094715d8eSBen Gras sblock.e2fs_ncg =
32194715d8eSBen Gras howmany(sblock.e2fs.e2fs_bcount - sblock.e2fs.e2fs_first_dblock,
32294715d8eSBen Gras sblock.e2fs.e2fs_bpg);
32394715d8eSBen Gras /* XXX assume hw bsize = 512 */
32494715d8eSBen Gras sblock.e2fs_fsbtodb = sblock.e2fs.e2fs_log_bsize + 1;
32594715d8eSBen Gras sblock.e2fs_bsize = 1024 << sblock.e2fs.e2fs_log_bsize;
32694715d8eSBen Gras sblock.e2fs_bshift = LOG_MINBSIZE + sblock.e2fs.e2fs_log_bsize;
32794715d8eSBen Gras sblock.e2fs_qbmask = sblock.e2fs_bsize - 1;
32894715d8eSBen Gras sblock.e2fs_bmask = ~sblock.e2fs_qbmask;
32994715d8eSBen Gras sblock.e2fs_ngdb = howmany(sblock.e2fs_ncg,
33094715d8eSBen Gras sblock.e2fs_bsize / sizeof(struct ext2_gd));
33194715d8eSBen Gras sblock.e2fs_ipb = sblock.e2fs_bsize / EXT2_DINODE_SIZE(&sblock);
33294715d8eSBen Gras sblock.e2fs_itpg = howmany(sblock.e2fs.e2fs_ipg, sblock.e2fs_ipb);
33394715d8eSBen Gras
33494715d8eSBen Gras /*
33594715d8eSBen Gras * Compute block size that the filesystem is based on,
33694715d8eSBen Gras * according to fsbtodb, and adjust superblock block number
33794715d8eSBen Gras * so we can tell if this is an alternate later.
33894715d8eSBen Gras */
33994715d8eSBen Gras super *= dev_bsize;
34084d9c625SLionel Sambuc dev_bsize = sblock.e2fs_bsize / EXT2_FSBTODB(&sblock, 1);
34194715d8eSBen Gras sblk.b_bno = super / dev_bsize;
34294715d8eSBen Gras
34394715d8eSBen Gras if (sblock.e2fs_ncg == 1) {
34494715d8eSBen Gras /* no alternate superblock; assume it's okay */
34594715d8eSBen Gras havesb = 1;
34694715d8eSBen Gras return 1;
34794715d8eSBen Gras }
34894715d8eSBen Gras getblk(&asblk, 1 * sblock.e2fs.e2fs_bpg + sblock.e2fs.e2fs_first_dblock,
34994715d8eSBen Gras (long)SBSIZE);
35094715d8eSBen Gras if (asblk.b_errs)
35194715d8eSBen Gras return 0;
35294715d8eSBen Gras if (bflag) {
35394715d8eSBen Gras havesb = 1;
35494715d8eSBen Gras return 1;
35594715d8eSBen Gras }
35694715d8eSBen Gras
35794715d8eSBen Gras /*
35894715d8eSBen Gras * Set all possible fields that could differ, then do check
35994715d8eSBen Gras * of whole super block against an alternate super block.
36094715d8eSBen Gras * When an alternate super-block is specified this check is skipped.
36194715d8eSBen Gras */
36294715d8eSBen Gras asblk.b_un.b_fs->e2fs_rbcount = sblk.b_un.b_fs->e2fs_rbcount;
36394715d8eSBen Gras asblk.b_un.b_fs->e2fs_fbcount = sblk.b_un.b_fs->e2fs_fbcount;
36494715d8eSBen Gras asblk.b_un.b_fs->e2fs_ficount = sblk.b_un.b_fs->e2fs_ficount;
36594715d8eSBen Gras asblk.b_un.b_fs->e2fs_mtime = sblk.b_un.b_fs->e2fs_mtime;
36694715d8eSBen Gras asblk.b_un.b_fs->e2fs_wtime = sblk.b_un.b_fs->e2fs_wtime;
36794715d8eSBen Gras asblk.b_un.b_fs->e2fs_mnt_count = sblk.b_un.b_fs->e2fs_mnt_count;
36894715d8eSBen Gras asblk.b_un.b_fs->e2fs_max_mnt_count =
36994715d8eSBen Gras sblk.b_un.b_fs->e2fs_max_mnt_count;
37094715d8eSBen Gras asblk.b_un.b_fs->e2fs_state = sblk.b_un.b_fs->e2fs_state;
37194715d8eSBen Gras asblk.b_un.b_fs->e2fs_beh = sblk.b_un.b_fs->e2fs_beh;
37294715d8eSBen Gras asblk.b_un.b_fs->e2fs_lastfsck = sblk.b_un.b_fs->e2fs_lastfsck;
37394715d8eSBen Gras asblk.b_un.b_fs->e2fs_fsckintv = sblk.b_un.b_fs->e2fs_fsckintv;
37494715d8eSBen Gras asblk.b_un.b_fs->e2fs_ruid = sblk.b_un.b_fs->e2fs_ruid;
37594715d8eSBen Gras asblk.b_un.b_fs->e2fs_rgid = sblk.b_un.b_fs->e2fs_rgid;
37694715d8eSBen Gras asblk.b_un.b_fs->e2fs_block_group_nr =
37794715d8eSBen Gras sblk.b_un.b_fs->e2fs_block_group_nr;
37894715d8eSBen Gras asblk.b_un.b_fs->e2fs_features_rocompat &= ~EXT2F_ROCOMPAT_LARGEFILE;
37994715d8eSBen Gras asblk.b_un.b_fs->e2fs_features_rocompat |=
38094715d8eSBen Gras sblk.b_un.b_fs->e2fs_features_rocompat & EXT2F_ROCOMPAT_LARGEFILE;
38194715d8eSBen Gras if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
38294715d8eSBen Gras ((sblock.e2fs.e2fs_features_incompat & ~EXT2F_INCOMPAT_SUPP) ||
38394715d8eSBen Gras (sblock.e2fs.e2fs_features_rocompat & ~EXT2F_ROCOMPAT_SUPP))) {
38494715d8eSBen Gras if (debug) {
38594715d8eSBen Gras printf("compat 0x%08x, incompat 0x%08x, compat_ro "
38694715d8eSBen Gras "0x%08x\n",
38794715d8eSBen Gras sblock.e2fs.e2fs_features_compat,
38894715d8eSBen Gras sblock.e2fs.e2fs_features_incompat,
38994715d8eSBen Gras sblock.e2fs.e2fs_features_rocompat);
39094715d8eSBen Gras }
39194715d8eSBen Gras badsb(listerr, "INCOMPATIBLE FEATURE BITS IN SUPER BLOCK");
39294715d8eSBen Gras return 0;
39394715d8eSBen Gras }
39494715d8eSBen Gras if (memcmp(sblk.b_un.b_fs, asblk.b_un.b_fs, SBSIZE)) {
39594715d8eSBen Gras if (debug) {
39694715d8eSBen Gras u_int32_t *nlp, *olp, *endlp;
39794715d8eSBen Gras
39894715d8eSBen Gras printf("superblock mismatches\n");
39994715d8eSBen Gras nlp = (u_int32_t *)asblk.b_un.b_fs;
40094715d8eSBen Gras olp = (u_int32_t *)sblk.b_un.b_fs;
40194715d8eSBen Gras endlp = olp + (SBSIZE / sizeof(*olp));
40294715d8eSBen Gras for ( ; olp < endlp; olp++, nlp++) {
40394715d8eSBen Gras if (*olp == *nlp)
40494715d8eSBen Gras continue;
40594715d8eSBen Gras printf("offset %ld, original %ld, "
40694715d8eSBen Gras "alternate %ld\n",
40794715d8eSBen Gras (long)(olp - (u_int32_t *)sblk.b_un.b_fs),
40894715d8eSBen Gras (long)fs2h32(*olp),
40994715d8eSBen Gras (long)fs2h32(*nlp));
41094715d8eSBen Gras }
41194715d8eSBen Gras }
41294715d8eSBen Gras badsb(listerr,
41394715d8eSBen Gras "VALUES IN SUPER BLOCK DISAGREE WITH "
41494715d8eSBen Gras "THOSE IN FIRST ALTERNATE");
41594715d8eSBen Gras return 0;
41694715d8eSBen Gras }
41794715d8eSBen Gras havesb = 1;
41894715d8eSBen Gras return 1;
41994715d8eSBen Gras }
42094715d8eSBen Gras
42194715d8eSBen Gras void
copyback_sb(struct bufarea * bp)42294715d8eSBen Gras copyback_sb(struct bufarea *bp)
42394715d8eSBen Gras {
42494715d8eSBen Gras /* Copy the in-memory superblock back to buffer */
42594715d8eSBen Gras bp->b_un.b_fs->e2fs_icount = h2fs32(sblock.e2fs.e2fs_icount);
42694715d8eSBen Gras bp->b_un.b_fs->e2fs_bcount = h2fs32(sblock.e2fs.e2fs_bcount);
42794715d8eSBen Gras bp->b_un.b_fs->e2fs_rbcount = h2fs32(sblock.e2fs.e2fs_rbcount);
42894715d8eSBen Gras bp->b_un.b_fs->e2fs_fbcount = h2fs32(sblock.e2fs.e2fs_fbcount);
42994715d8eSBen Gras bp->b_un.b_fs->e2fs_ficount = h2fs32(sblock.e2fs.e2fs_ficount);
43094715d8eSBen Gras bp->b_un.b_fs->e2fs_first_dblock =
43194715d8eSBen Gras h2fs32(sblock.e2fs.e2fs_first_dblock);
43294715d8eSBen Gras bp->b_un.b_fs->e2fs_log_bsize = h2fs32(sblock.e2fs.e2fs_log_bsize);
43394715d8eSBen Gras bp->b_un.b_fs->e2fs_fsize = h2fs32(sblock.e2fs.e2fs_fsize);
43494715d8eSBen Gras bp->b_un.b_fs->e2fs_bpg = h2fs32(sblock.e2fs.e2fs_bpg);
43594715d8eSBen Gras bp->b_un.b_fs->e2fs_fpg = h2fs32(sblock.e2fs.e2fs_fpg);
43694715d8eSBen Gras bp->b_un.b_fs->e2fs_ipg = h2fs32(sblock.e2fs.e2fs_ipg);
43794715d8eSBen Gras bp->b_un.b_fs->e2fs_mtime = h2fs32(sblock.e2fs.e2fs_mtime);
43894715d8eSBen Gras bp->b_un.b_fs->e2fs_wtime = h2fs32(sblock.e2fs.e2fs_wtime);
43994715d8eSBen Gras bp->b_un.b_fs->e2fs_lastfsck = h2fs32(sblock.e2fs.e2fs_lastfsck);
44094715d8eSBen Gras bp->b_un.b_fs->e2fs_fsckintv = h2fs32(sblock.e2fs.e2fs_fsckintv);
44194715d8eSBen Gras bp->b_un.b_fs->e2fs_creator = h2fs32(sblock.e2fs.e2fs_creator);
44294715d8eSBen Gras bp->b_un.b_fs->e2fs_rev = h2fs32(sblock.e2fs.e2fs_rev);
44394715d8eSBen Gras bp->b_un.b_fs->e2fs_mnt_count = h2fs16(sblock.e2fs.e2fs_mnt_count);
44494715d8eSBen Gras bp->b_un.b_fs->e2fs_max_mnt_count =
44594715d8eSBen Gras h2fs16(sblock.e2fs.e2fs_max_mnt_count);
44694715d8eSBen Gras bp->b_un.b_fs->e2fs_magic = h2fs16(sblock.e2fs.e2fs_magic);
44794715d8eSBen Gras bp->b_un.b_fs->e2fs_state = h2fs16(sblock.e2fs.e2fs_state);
44894715d8eSBen Gras bp->b_un.b_fs->e2fs_beh = h2fs16(sblock.e2fs.e2fs_beh);
44994715d8eSBen Gras bp->b_un.b_fs->e2fs_ruid = h2fs16(sblock.e2fs.e2fs_ruid);
45094715d8eSBen Gras bp->b_un.b_fs->e2fs_rgid = h2fs16(sblock.e2fs.e2fs_rgid);
45194715d8eSBen Gras }
45294715d8eSBen Gras
45394715d8eSBen Gras void
badsb(int listerr,const char * s)45494715d8eSBen Gras badsb(int listerr, const char *s)
45594715d8eSBen Gras {
45694715d8eSBen Gras
45794715d8eSBen Gras if (!listerr)
45894715d8eSBen Gras return;
45994715d8eSBen Gras if (preen)
46094715d8eSBen Gras printf("%s: ", cdevname());
46194715d8eSBen Gras pfatal("BAD SUPER BLOCK: %s\n", s);
46294715d8eSBen Gras }
46394715d8eSBen Gras
46494715d8eSBen Gras /*
46594715d8eSBen Gras * Calculate a prototype superblock based on information in the disk label.
46694715d8eSBen Gras * When done the cgsblock macro can be calculated and the fs_ncg field
46794715d8eSBen Gras * can be used. Do NOT attempt to use other macros without verifying that
46894715d8eSBen Gras * their needed information is available!
46994715d8eSBen Gras */
47094715d8eSBen Gras
47194715d8eSBen Gras int
calcsb(const char * dev,int devfd,struct m_ext2fs * fs)47294715d8eSBen Gras calcsb(const char *dev, int devfd, struct m_ext2fs *fs)
47394715d8eSBen Gras {
47484d9c625SLionel Sambuc #if defined(__minix)
47594715d8eSBen Gras errexit("%s: calcsb: can't read disk label under minix", dev);
47694715d8eSBen Gras #else
47794715d8eSBen Gras struct disklabel *lp;
47894715d8eSBen Gras struct partition *pp;
47994715d8eSBen Gras char *cp;
48094715d8eSBen Gras
48194715d8eSBen Gras cp = strchr(dev, '\0');
48294715d8eSBen Gras if (cp-- == dev ||
48394715d8eSBen Gras ((*cp < 'a' || *cp > 'h') && !isdigit((unsigned char)*cp))) {
48494715d8eSBen Gras pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
48594715d8eSBen Gras return 0;
48694715d8eSBen Gras }
48794715d8eSBen Gras lp = getdisklabel(dev, devfd);
48894715d8eSBen Gras if (isdigit((unsigned char)*cp))
48994715d8eSBen Gras pp = &lp->d_partitions[0];
49094715d8eSBen Gras else
49194715d8eSBen Gras pp = &lp->d_partitions[*cp - 'a'];
49294715d8eSBen Gras if (pp->p_fstype != FS_EX2FS) {
49394715d8eSBen Gras pfatal("%s: NOT LABELED AS A EXT2 FILE SYSTEM (%s)\n",
49494715d8eSBen Gras dev, pp->p_fstype < FSMAXTYPES ?
49594715d8eSBen Gras fstypenames[pp->p_fstype] : "unknown");
49694715d8eSBen Gras return 0;
49794715d8eSBen Gras }
498*0a6a1f1dSLionel Sambuc if (pp->p_fsize == 0) {
499*0a6a1f1dSLionel Sambuc pfatal("%s: PARTITION SIZE IS 0\n", dev);
500*0a6a1f1dSLionel Sambuc return 0;
501*0a6a1f1dSLionel Sambuc }
50294715d8eSBen Gras memset(fs, 0, sizeof(struct m_ext2fs));
50394715d8eSBen Gras fs->e2fs_bsize = pp->p_fsize;
50494715d8eSBen Gras fs->e2fs.e2fs_log_bsize = pp->p_fsize / 1024;
50594715d8eSBen Gras fs->e2fs.e2fs_bcount = (pp->p_size * DEV_BSIZE) / fs->e2fs_bsize;
50694715d8eSBen Gras fs->e2fs.e2fs_first_dblock = (fs->e2fs.e2fs_log_bsize == 0) ? 1 : 0;
50794715d8eSBen Gras fs->e2fs.e2fs_bpg = fs->e2fs_bsize * NBBY;
50894715d8eSBen Gras fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs.e2fs_log_bsize;
50994715d8eSBen Gras fs->e2fs_qbmask = fs->e2fs_bsize - 1;
51094715d8eSBen Gras fs->e2fs_bmask = ~fs->e2fs_qbmask;
51194715d8eSBen Gras fs->e2fs_ncg =
51294715d8eSBen Gras howmany(fs->e2fs.e2fs_bcount - fs->e2fs.e2fs_first_dblock,
51394715d8eSBen Gras fs->e2fs.e2fs_bpg);
51494715d8eSBen Gras fs->e2fs_fsbtodb = fs->e2fs.e2fs_log_bsize + 1;
51594715d8eSBen Gras fs->e2fs_ngdb = howmany(fs->e2fs_ncg,
51694715d8eSBen Gras fs->e2fs_bsize / sizeof(struct ext2_gd));
51794715d8eSBen Gras
51894715d8eSBen Gras return 1;
51984d9c625SLionel Sambuc #endif /* defined(__minix) */
52094715d8eSBen Gras }
52194715d8eSBen Gras
52294715d8eSBen Gras static struct disklabel *
getdisklabel(const char * s,int fd)52394715d8eSBen Gras getdisklabel(const char *s, int fd)
52494715d8eSBen Gras {
52594715d8eSBen Gras static struct disklabel lab;
52694715d8eSBen Gras
52784d9c625SLionel Sambuc #if defined(__minix)
52894715d8eSBen Gras if (s == NULL)
52994715d8eSBen Gras return NULL;
53094715d8eSBen Gras errexit("%s: can't read disk label under minix", s);
53194715d8eSBen Gras #else
53294715d8eSBen Gras if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
53394715d8eSBen Gras if (s == NULL)
53494715d8eSBen Gras return NULL;
53594715d8eSBen Gras pwarn("ioctl (GCINFO): %s\n", strerror(errno));
53694715d8eSBen Gras errexit("%s: can't read disk label", s);
53794715d8eSBen Gras }
53884d9c625SLionel Sambuc #endif /* defined(__minix) */
53994715d8eSBen Gras return &lab;
54094715d8eSBen Gras }
54194715d8eSBen Gras
54294715d8eSBen Gras daddr_t
cgoverhead(int c)54394715d8eSBen Gras cgoverhead(int c)
54494715d8eSBen Gras {
54594715d8eSBen Gras int overh;
54694715d8eSBen Gras overh =
54794715d8eSBen Gras 1 /* block bitmap */ +
54894715d8eSBen Gras 1 /* inode bitmap */ +
54994715d8eSBen Gras sblock.e2fs_itpg;
55094715d8eSBen Gras if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
55194715d8eSBen Gras sblock.e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) {
55294715d8eSBen Gras if (cg_has_sb(c) == 0)
55394715d8eSBen Gras return overh;
55494715d8eSBen Gras }
55594715d8eSBen Gras overh += 1 /* superblock */ + sblock.e2fs_ngdb;
55694715d8eSBen Gras return overh;
55794715d8eSBen Gras }
558