1*bf2afe82Skevlo /* $OpenBSD: setup.c,v 1.33 2019/07/01 07:13:44 kevlo Exp $ */
25ff4e0c8Sdownsj /* $NetBSD: setup.c,v 1.1 1997/06/11 11:22:01 bouyer Exp $ */
38c424e8eSdownsj
48c424e8eSdownsj /*
55ff4e0c8Sdownsj * Copyright (c) 1997 Manuel Bouyer.
68c424e8eSdownsj * Copyright (c) 1980, 1986, 1993
78c424e8eSdownsj * The Regents of the University of California. All rights reserved.
88c424e8eSdownsj *
98c424e8eSdownsj * Redistribution and use in source and binary forms, with or without
108c424e8eSdownsj * modification, are permitted provided that the following conditions
118c424e8eSdownsj * are met:
128c424e8eSdownsj * 1. Redistributions of source code must retain the above copyright
138c424e8eSdownsj * notice, this list of conditions and the following disclaimer.
148c424e8eSdownsj * 2. Redistributions in binary form must reproduce the above copyright
158c424e8eSdownsj * notice, this list of conditions and the following disclaimer in the
168c424e8eSdownsj * documentation and/or other materials provided with the distribution.
171ef0d710Smillert * 3. Neither the name of the University nor the names of its contributors
188c424e8eSdownsj * may be used to endorse or promote products derived from this software
198c424e8eSdownsj * without specific prior written permission.
208c424e8eSdownsj *
218c424e8eSdownsj * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
228c424e8eSdownsj * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
238c424e8eSdownsj * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
248c424e8eSdownsj * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
258c424e8eSdownsj * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
268c424e8eSdownsj * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
278c424e8eSdownsj * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
288c424e8eSdownsj * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
298c424e8eSdownsj * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
308c424e8eSdownsj * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
318c424e8eSdownsj * SUCH DAMAGE.
328c424e8eSdownsj */
338c424e8eSdownsj
348c424e8eSdownsj #define DKTYPENAMES
35b9fc9a72Sderaadt #include <sys/param.h> /* DEV_BSIZE roundup */
368c424e8eSdownsj #include <sys/time.h>
378c424e8eSdownsj #include <ufs/ext2fs/ext2fs_dinode.h>
388c424e8eSdownsj #include <ufs/ext2fs/ext2fs.h>
398c424e8eSdownsj #include <sys/stat.h>
408c424e8eSdownsj #include <sys/ioctl.h>
4191f4f7d8Sdlg #include <sys/dkio.h>
428c424e8eSdownsj #include <sys/disklabel.h>
438c424e8eSdownsj
448c424e8eSdownsj #include <errno.h>
4501a83688Smillert #include <fcntl.h>
468c424e8eSdownsj #include <stdio.h>
478c424e8eSdownsj #include <stdlib.h>
48c0c611b7Sderaadt #include <unistd.h>
498c424e8eSdownsj #include <string.h>
508c424e8eSdownsj #include <ctype.h>
51c0c611b7Sderaadt #include <err.h>
528c424e8eSdownsj
538c424e8eSdownsj #include "fsck.h"
548c424e8eSdownsj #include "extern.h"
558c424e8eSdownsj #include "fsutil.h"
568c424e8eSdownsj
578c424e8eSdownsj #define POWEROF2(num) (((num) & ((num) - 1)) == 0)
588c424e8eSdownsj
59c72b5b24Smillert void badsb(int, char *);
60c0c611b7Sderaadt int calcsb(char *, int, struct m_ext2fs *, struct disklabel *);
61c72b5b24Smillert static struct disklabel *getdisklabel(char *, int);
62c72b5b24Smillert static int readsb(int);
638c424e8eSdownsj
648c424e8eSdownsj int
setup(char * dev)658809fabbSderaadt setup(char *dev)
668c424e8eSdownsj {
6765348f21Sjasoni long cg, asked, i;
688c424e8eSdownsj long bmapsize;
698c424e8eSdownsj struct disklabel *lp;
708c424e8eSdownsj off_t sizepb;
718c424e8eSdownsj struct stat statb;
728c424e8eSdownsj struct m_ext2fs proto;
738c424e8eSdownsj int doskipclean;
748c424e8eSdownsj u_int64_t maxfilesize;
758c424e8eSdownsj
768c424e8eSdownsj havesb = 0;
778c424e8eSdownsj fswritefd = -1;
788c424e8eSdownsj doskipclean = skipclean;
79df69c215Sderaadt if (stat(dev, &statb) == -1) {
808c424e8eSdownsj printf("Can't stat %s: %s\n", dev, strerror(errno));
818c424e8eSdownsj return (0);
828c424e8eSdownsj }
838c424e8eSdownsj if (!S_ISCHR(statb.st_mode)) {
848c424e8eSdownsj pfatal("%s is not a character device", dev);
858c424e8eSdownsj if (reply("CONTINUE") == 0)
868c424e8eSdownsj return (0);
878c424e8eSdownsj }
88df69c215Sderaadt if ((fsreadfd = open(dev, O_RDONLY)) == -1) {
898c424e8eSdownsj printf("Can't open %s: %s\n", dev, strerror(errno));
908c424e8eSdownsj return (0);
918c424e8eSdownsj }
928c424e8eSdownsj if (preen == 0)
938c424e8eSdownsj printf("** %s", dev);
94df69c215Sderaadt if (nflag || (fswritefd = open(dev, O_WRONLY)) == -1) {
958c424e8eSdownsj fswritefd = -1;
968c424e8eSdownsj if (preen)
978c424e8eSdownsj pfatal("NO WRITE ACCESS");
988c424e8eSdownsj printf(" (NO WRITE)");
998c424e8eSdownsj }
1008c424e8eSdownsj if (preen == 0)
1018c424e8eSdownsj printf("\n");
1028c424e8eSdownsj fsmodified = 0;
1038c424e8eSdownsj lfdir = 0;
1048c424e8eSdownsj initbarea(&sblk);
1058c424e8eSdownsj initbarea(&asblk);
10665348f21Sjasoni sblk.b_un.b_buf = malloc(SBSIZE);
10765348f21Sjasoni asblk.b_un.b_buf = malloc(SBSIZE);
1088c424e8eSdownsj if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
1098c424e8eSdownsj errexit("cannot allocate space for superblock\n");
110c0c611b7Sderaadt if ((lp = getdisklabel(NULL, fsreadfd)) != NULL)
1119e7daf04Skrw secsize = lp->d_secsize;
1128c424e8eSdownsj else
1139e7daf04Skrw secsize = DEV_BSIZE;
114c0c611b7Sderaadt
115c0c611b7Sderaadt if (!hotroot()) {
116f0426f3aSsemarie #ifndef SMALL
117f0426f3aSsemarie if (pledge("stdio getpw", NULL) == -1)
118f0426f3aSsemarie err(1, "pledge");
119f0426f3aSsemarie #else
120c0c611b7Sderaadt if (pledge("stdio", NULL) == -1)
121c0c611b7Sderaadt err(1, "pledge");
122f0426f3aSsemarie #endif
123c0c611b7Sderaadt }
124c0c611b7Sderaadt
1258c424e8eSdownsj /*
1268c424e8eSdownsj * Read in the superblock, looking for alternates if necessary
1278c424e8eSdownsj */
1288c424e8eSdownsj if (readsb(1) == 0) {
129c0c611b7Sderaadt if (bflag || preen || calcsb(dev, fsreadfd, &proto, lp) == 0)
1308c424e8eSdownsj return(0);
1318c424e8eSdownsj if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
1328c424e8eSdownsj return (0);
1338c424e8eSdownsj for (cg = 1; cg < proto.e2fs_ncg; cg++) {
1348c424e8eSdownsj bflag = fsbtodb(&proto,
1358c424e8eSdownsj cg * proto.e2fs.e2fs_bpg + proto.e2fs.e2fs_first_dblock);
1368c424e8eSdownsj if (readsb(0) != 0)
1378c424e8eSdownsj break;
1388c424e8eSdownsj }
1398c424e8eSdownsj if (cg >= proto.e2fs_ncg) {
1408c424e8eSdownsj printf("%s %s\n%s %s\n%s %s\n",
1418c424e8eSdownsj "SEARCH FOR ALTERNATE SUPER-BLOCK",
1428c424e8eSdownsj "FAILED. YOU MUST USE THE",
1438c424e8eSdownsj "-b OPTION TO FSCK_FFS TO SPECIFY THE",
1448c424e8eSdownsj "LOCATION OF AN ALTERNATE",
1458c424e8eSdownsj "SUPER-BLOCK TO SUPPLY NEEDED",
1468c424e8eSdownsj "INFORMATION; SEE fsck_ext2fs(8).");
1478c424e8eSdownsj return(0);
1488c424e8eSdownsj }
1498c424e8eSdownsj doskipclean = 0;
1508c424e8eSdownsj pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
1518c424e8eSdownsj }
1528c424e8eSdownsj if (debug)
1538c424e8eSdownsj printf("state = %d\n", sblock.e2fs.e2fs_state);
1548c424e8eSdownsj if (sblock.e2fs.e2fs_state == E2FS_ISCLEAN) {
1558c424e8eSdownsj if (doskipclean) {
1568c424e8eSdownsj pwarn("%sile system is clean; not checking\n",
1578c424e8eSdownsj preen ? "f" : "** F");
1588c424e8eSdownsj return (-1);
1598c424e8eSdownsj }
1608c424e8eSdownsj if (!preen)
1618c424e8eSdownsj pwarn("** File system is already clean\n");
1628c424e8eSdownsj }
1638c424e8eSdownsj maxfsblock = sblock.e2fs.e2fs_bcount;
1648c424e8eSdownsj maxino = sblock.e2fs_ncg * sblock.e2fs.e2fs_ipg;
1658c424e8eSdownsj sizepb = sblock.e2fs_bsize;
1668c424e8eSdownsj maxfilesize = sblock.e2fs_bsize * NDADDR - 1;
1678c424e8eSdownsj for (i = 0; i < NIADDR; i++) {
1688c424e8eSdownsj sizepb *= NINDIR(&sblock);
1698c424e8eSdownsj maxfilesize += sizepb;
1708c424e8eSdownsj }
1718c424e8eSdownsj /*
1728c424e8eSdownsj * Check and potentially fix certain fields in the super block.
1738c424e8eSdownsj */
1745814667eSotto if (/* (sblock.e2fs.e2fs_rbcount < 0) || */
1758c424e8eSdownsj (sblock.e2fs.e2fs_rbcount > sblock.e2fs.e2fs_bcount)) {
1768c424e8eSdownsj pfatal("IMPOSSIBLE RESERVED BLOCK COUNT=%d IN SUPERBLOCK",
1778c424e8eSdownsj sblock.e2fs.e2fs_rbcount);
1788c424e8eSdownsj if (reply("SET TO DEFAULT") == 1) {
1798c424e8eSdownsj sblock.e2fs.e2fs_rbcount = sblock.e2fs.e2fs_bcount * 0.1;
1808c424e8eSdownsj sbdirty();
18165348f21Sjasoni dirty(&asblk);
1828c424e8eSdownsj }
1838c424e8eSdownsj }
1848c424e8eSdownsj if (sblock.e2fs.e2fs_bpg != sblock.e2fs.e2fs_fpg) {
1858c424e8eSdownsj pfatal("WRONG FPG=%d (BPG=%d) IN SUPERBLOCK",
1868c424e8eSdownsj sblock.e2fs.e2fs_fpg, sblock.e2fs.e2fs_bpg);
1878c424e8eSdownsj return 0;
1888c424e8eSdownsj }
1898c424e8eSdownsj if (asblk.b_dirty && !bflag) {
19065348f21Sjasoni copyback_sb(&asblk);
1918c424e8eSdownsj flush(fswritefd, &asblk);
1928c424e8eSdownsj }
1938c424e8eSdownsj /*
1948c424e8eSdownsj * read in the summary info.
1958c424e8eSdownsj */
1968c424e8eSdownsj
1971ed98fdfSderaadt sblock.e2fs_gd = calloc(sblock.e2fs_ngdb, sblock.e2fs_bsize);
19865348f21Sjasoni if (sblock.e2fs_gd == NULL)
19965348f21Sjasoni errexit("out of memory\n");
2008c424e8eSdownsj asked = 0;
2018c424e8eSdownsj for (i=0; i < sblock.e2fs_ngdb; i++) {
2028c424e8eSdownsj if (bread(fsreadfd,(char *)
2038c424e8eSdownsj &sblock.e2fs_gd[i* sblock.e2fs_bsize / sizeof(struct ext2_gd)],
2048c424e8eSdownsj fsbtodb(&sblock, ((sblock.e2fs_bsize>1024)?0:1)+i+1),
2058c424e8eSdownsj sblock.e2fs_bsize) != 0 && !asked) {
2068c424e8eSdownsj pfatal("BAD SUMMARY INFORMATION");
2078c424e8eSdownsj if (reply("CONTINUE") == 0)
2088c424e8eSdownsj errexit("%s\n", "");
2098c424e8eSdownsj asked++;
2108c424e8eSdownsj }
2118c424e8eSdownsj }
2128c424e8eSdownsj /*
2138c424e8eSdownsj * allocate and initialize the necessary maps
2148c424e8eSdownsj */
2158c424e8eSdownsj bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
2168c424e8eSdownsj blockmap = calloc((unsigned)bmapsize, sizeof (char));
2178c424e8eSdownsj if (blockmap == NULL) {
2188c424e8eSdownsj printf("cannot alloc %u bytes for blockmap\n",
2198c424e8eSdownsj (unsigned)bmapsize);
2208c424e8eSdownsj goto badsblabel;
2218c424e8eSdownsj }
2228c424e8eSdownsj statemap = calloc((unsigned)(maxino + 2), sizeof(char));
2238c424e8eSdownsj if (statemap == NULL) {
2248c424e8eSdownsj printf("cannot alloc %u bytes for statemap\n",
2258c424e8eSdownsj (unsigned)(maxino + 1));
2268c424e8eSdownsj goto badsblabel;
2278c424e8eSdownsj }
22828704fc2Sderaadt typemap = calloc((unsigned)(maxino + 1), sizeof(u_char));
22965348f21Sjasoni if (typemap == NULL) {
23065348f21Sjasoni printf("cannot alloc %u bytes for typemap\n",
23165348f21Sjasoni (unsigned)(maxino + 1));
23265348f21Sjasoni goto badsblabel;
23365348f21Sjasoni }
2345ae94ef8Sderaadt lncntp = calloc((unsigned)(maxino + 1), sizeof(int16_t));
2358c424e8eSdownsj if (lncntp == NULL) {
2368c424e8eSdownsj printf("cannot alloc %u bytes for lncntp\n",
23765348f21Sjasoni (unsigned)((maxino + 1) * sizeof(int16_t)));
2388c424e8eSdownsj goto badsblabel;
2398c424e8eSdownsj }
2408c424e8eSdownsj for (numdirs = 0, cg = 0; cg < sblock.e2fs_ncg; cg++) {
24160a51e06Spelikan numdirs += letoh16(sblock.e2fs_gd[cg].ext2bgd_ndirs);
2428c424e8eSdownsj }
2438c424e8eSdownsj inplast = 0;
2448c424e8eSdownsj listmax = numdirs + 10;
2455ae94ef8Sderaadt inpsort = calloc((unsigned)listmax, sizeof(struct inoinfo *));
2465ae94ef8Sderaadt inphead = calloc((unsigned)numdirs, sizeof(struct inoinfo *));
2478c424e8eSdownsj if (inpsort == NULL || inphead == NULL) {
2488c424e8eSdownsj printf("cannot alloc %u bytes for inphead\n",
24965348f21Sjasoni (unsigned)(numdirs * sizeof(struct inoinfo *)));
2508c424e8eSdownsj goto badsblabel;
2518c424e8eSdownsj }
2528c424e8eSdownsj bufinit();
2538c424e8eSdownsj return (1);
2548c424e8eSdownsj
2558c424e8eSdownsj badsblabel:
2568c424e8eSdownsj ckfini(0);
2578c424e8eSdownsj return (0);
2588c424e8eSdownsj }
2598c424e8eSdownsj
2608c424e8eSdownsj /*
2618c424e8eSdownsj * Read in the super block and its summary info.
2628c424e8eSdownsj */
2638c424e8eSdownsj static int
readsb(int listerr)2648809fabbSderaadt readsb(int listerr)
2658c424e8eSdownsj {
2669e7daf04Skrw daddr32_t super = bflag ? bflag : SBOFF / DEV_BSIZE;
2678c424e8eSdownsj
26865348f21Sjasoni if (bread(fsreadfd, (char *)sblk.b_un.b_fs, super, (long)SBSIZE) != 0)
2698c424e8eSdownsj return (0);
2708c424e8eSdownsj sblk.b_bno = super;
2718c424e8eSdownsj sblk.b_size = SBSIZE;
27265348f21Sjasoni
27365348f21Sjasoni /* Copy the superblock in memory */
27465348f21Sjasoni e2fs_sbload(sblk.b_un.b_fs, &sblock.e2fs);
27565348f21Sjasoni
2768c424e8eSdownsj /*
2778c424e8eSdownsj * run a few consistency checks of the super block
2788c424e8eSdownsj */
2798c424e8eSdownsj if (sblock.e2fs.e2fs_magic != E2FS_MAGIC) {
2808c424e8eSdownsj badsb(listerr, "MAGIC NUMBER WRONG"); return (0);
2818c424e8eSdownsj }
2828c424e8eSdownsj if (sblock.e2fs.e2fs_log_bsize > 2) {
2838c424e8eSdownsj badsb(listerr, "BAD LOG_BSIZE"); return (0);
2848c424e8eSdownsj }
2853917ca2dStobias if (sblock.e2fs.e2fs_bpg == 0) {
2863917ca2dStobias badsb(listerr, "BAD BLOCKS PER GROUP"); return (0);
2873917ca2dStobias }
2888c424e8eSdownsj
2898c424e8eSdownsj /* compute the dynamic fields of the in-memory sb */
2908c424e8eSdownsj /* compute dynamic sb infos */
2918c424e8eSdownsj sblock.e2fs_ncg =
2928c424e8eSdownsj howmany(sblock.e2fs.e2fs_bcount - sblock.e2fs.e2fs_first_dblock,
2938c424e8eSdownsj sblock.e2fs.e2fs_bpg);
2948c424e8eSdownsj /* XXX assume hw bsize = 512 */
2958c424e8eSdownsj sblock.e2fs_fsbtodb = sblock.e2fs.e2fs_log_bsize + 1;
2968c424e8eSdownsj sblock.e2fs_bsize = 1024 << sblock.e2fs.e2fs_log_bsize;
2978c424e8eSdownsj sblock.e2fs_bshift = LOG_MINBSIZE + sblock.e2fs.e2fs_log_bsize;
2988c424e8eSdownsj sblock.e2fs_qbmask = sblock.e2fs_bsize - 1;
2998c424e8eSdownsj sblock.e2fs_bmask = ~sblock.e2fs_qbmask;
3008c424e8eSdownsj sblock.e2fs_ngdb = howmany(sblock.e2fs_ncg,
3018c424e8eSdownsj sblock.e2fs_bsize / sizeof(struct ext2_gd));
30250e48d67Skrw sblock.e2fs_ipb = sblock.e2fs_bsize / EXT2_DINODE_SIZE(&sblock);
3038c424e8eSdownsj sblock.e2fs_itpg = sblock.e2fs.e2fs_ipg/sblock.e2fs_ipb;
3048c424e8eSdownsj
3058c424e8eSdownsj /*
3068c424e8eSdownsj * Compute block size that the filesystem is based on,
3078c424e8eSdownsj * according to fsbtodb, and adjust superblock block number
3088c424e8eSdownsj * so we can tell if this is an alternate later.
3098c424e8eSdownsj */
3109e7daf04Skrw sblk.b_bno = super / DEV_BSIZE;
31165348f21Sjasoni
3123c4b0c78Sotto if (sblock.e2fs_ncg == 1) {
3133c4b0c78Sotto /* no alternate superblock; assume it's okey */
3143c4b0c78Sotto havesb = 1;
3153c4b0c78Sotto return 1;
3163c4b0c78Sotto }
3173c4b0c78Sotto
31865348f21Sjasoni getblk(&asblk, 1 * sblock.e2fs.e2fs_bpg + sblock.e2fs.e2fs_first_dblock,
31965348f21Sjasoni (long)SBSIZE);
32065348f21Sjasoni if (asblk.b_errs)
32165348f21Sjasoni return (0);
3228c424e8eSdownsj if (bflag) {
3238c424e8eSdownsj havesb = 1;
3248c424e8eSdownsj return (1);
3258c424e8eSdownsj }
3268c424e8eSdownsj
3278c424e8eSdownsj /*
3288c424e8eSdownsj * Set all possible fields that could differ, then do check
3298c424e8eSdownsj * of whole super block against an alternate super block.
3308c424e8eSdownsj * When an alternate super-block is specified this check is skipped.
3318c424e8eSdownsj */
33265348f21Sjasoni asblk.b_un.b_fs->e2fs_rbcount = sblk.b_un.b_fs->e2fs_rbcount;
33365348f21Sjasoni asblk.b_un.b_fs->e2fs_fbcount = sblk.b_un.b_fs->e2fs_fbcount;
33465348f21Sjasoni asblk.b_un.b_fs->e2fs_ficount = sblk.b_un.b_fs->e2fs_ficount;
33565348f21Sjasoni asblk.b_un.b_fs->e2fs_mtime = sblk.b_un.b_fs->e2fs_mtime;
33665348f21Sjasoni asblk.b_un.b_fs->e2fs_wtime = sblk.b_un.b_fs->e2fs_wtime;
33765348f21Sjasoni asblk.b_un.b_fs->e2fs_mnt_count = sblk.b_un.b_fs->e2fs_mnt_count;
33865348f21Sjasoni asblk.b_un.b_fs->e2fs_max_mnt_count = sblk.b_un.b_fs->e2fs_max_mnt_count;
33965348f21Sjasoni asblk.b_un.b_fs->e2fs_state = sblk.b_un.b_fs->e2fs_state;
34065348f21Sjasoni asblk.b_un.b_fs->e2fs_beh = sblk.b_un.b_fs->e2fs_beh;
34165348f21Sjasoni asblk.b_un.b_fs->e2fs_lastfsck = sblk.b_un.b_fs->e2fs_lastfsck;
34265348f21Sjasoni asblk.b_un.b_fs->e2fs_fsckintv = sblk.b_un.b_fs->e2fs_fsckintv;
34365348f21Sjasoni asblk.b_un.b_fs->e2fs_ruid = sblk.b_un.b_fs->e2fs_ruid;
34465348f21Sjasoni asblk.b_un.b_fs->e2fs_rgid = sblk.b_un.b_fs->e2fs_rgid;
34565348f21Sjasoni asblk.b_un.b_fs->e2fs_block_group_nr =
34665348f21Sjasoni sblk.b_un.b_fs->e2fs_block_group_nr;
347*bf2afe82Skevlo asblk.b_un.b_fs->e2fs_features_rocompat &= ~EXT2F_ROCOMPAT_LARGE_FILE;
348935730fbSniallo asblk.b_un.b_fs->e2fs_features_rocompat |=
349*bf2afe82Skevlo sblk.b_un.b_fs->e2fs_features_rocompat & EXT2F_ROCOMPAT_LARGE_FILE;
35065348f21Sjasoni if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
35165348f21Sjasoni ((sblock.e2fs.e2fs_features_incompat & ~EXT2F_INCOMPAT_SUPP) ||
35265348f21Sjasoni (sblock.e2fs.e2fs_features_rocompat & ~EXT2F_ROCOMPAT_SUPP))) {
3538c424e8eSdownsj if (debug) {
35465348f21Sjasoni printf("compat 0x%08x, incompat 0x%08x, compat_ro "
35565348f21Sjasoni "0x%08x\n",
35665348f21Sjasoni sblock.e2fs.e2fs_features_compat,
35765348f21Sjasoni sblock.e2fs.e2fs_features_incompat,
35865348f21Sjasoni sblock.e2fs.e2fs_features_rocompat);
35965348f21Sjasoni }
36065348f21Sjasoni badsb(listerr,"INCOMPATIBLE FEATURE BITS IN SUPER BLOCK");
36165348f21Sjasoni return 0;
36265348f21Sjasoni }
36365348f21Sjasoni if (memcmp(sblk.b_un.b_fs, asblk.b_un.b_fs, SBSIZE)) {
36465348f21Sjasoni if (debug) {
36565348f21Sjasoni u_int32_t *nlp, *olp, *endlp;
3668c424e8eSdownsj
3678c424e8eSdownsj printf("superblock mismatches\n");
36865348f21Sjasoni nlp = (u_int32_t *)asblk.b_un.b_fs;
36965348f21Sjasoni olp = (u_int32_t *)sblk.b_un.b_fs;
3708c424e8eSdownsj endlp = olp + (SBSIZE / sizeof *olp);
3718c424e8eSdownsj for ( ; olp < endlp; olp++, nlp++) {
3728c424e8eSdownsj if (*olp == *nlp)
3738c424e8eSdownsj continue;
37465348f21Sjasoni printf("offset %ld, original %ld, alternate %ld\n",
37565348f21Sjasoni (long)(olp - (u_int32_t *)sblk.b_un.b_fs),
37660a51e06Spelikan (long)letoh32(*olp),
37760a51e06Spelikan (long)letoh32(*nlp));
3788c424e8eSdownsj }
3798c424e8eSdownsj }
3808c424e8eSdownsj badsb(listerr,
3818c424e8eSdownsj "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
3828c424e8eSdownsj return (0);
3838c424e8eSdownsj }
3848c424e8eSdownsj havesb = 1;
3858c424e8eSdownsj return (1);
3868c424e8eSdownsj }
3878c424e8eSdownsj
3888c424e8eSdownsj void
copyback_sb(struct bufarea * bp)3898809fabbSderaadt copyback_sb(struct bufarea *bp)
39065348f21Sjasoni {
39165348f21Sjasoni /* Copy the in-memory superblock back to buffer */
39260a51e06Spelikan bp->b_un.b_fs->e2fs_icount = letoh32(sblock.e2fs.e2fs_icount);
39360a51e06Spelikan bp->b_un.b_fs->e2fs_bcount = letoh32(sblock.e2fs.e2fs_bcount);
39460a51e06Spelikan bp->b_un.b_fs->e2fs_rbcount = letoh32(sblock.e2fs.e2fs_rbcount);
39560a51e06Spelikan bp->b_un.b_fs->e2fs_fbcount = letoh32(sblock.e2fs.e2fs_fbcount);
39660a51e06Spelikan bp->b_un.b_fs->e2fs_ficount = letoh32(sblock.e2fs.e2fs_ficount);
39765348f21Sjasoni bp->b_un.b_fs->e2fs_first_dblock =
39860a51e06Spelikan letoh32(sblock.e2fs.e2fs_first_dblock);
39960a51e06Spelikan bp->b_un.b_fs->e2fs_log_bsize = letoh32(sblock.e2fs.e2fs_log_bsize);
40060a51e06Spelikan bp->b_un.b_fs->e2fs_log_fsize = letoh32(sblock.e2fs.e2fs_log_fsize);
40160a51e06Spelikan bp->b_un.b_fs->e2fs_bpg = letoh32(sblock.e2fs.e2fs_bpg);
40260a51e06Spelikan bp->b_un.b_fs->e2fs_fpg = letoh32(sblock.e2fs.e2fs_fpg);
40360a51e06Spelikan bp->b_un.b_fs->e2fs_ipg = letoh32(sblock.e2fs.e2fs_ipg);
40460a51e06Spelikan bp->b_un.b_fs->e2fs_mtime = letoh32(sblock.e2fs.e2fs_mtime);
40560a51e06Spelikan bp->b_un.b_fs->e2fs_wtime = letoh32(sblock.e2fs.e2fs_wtime);
40660a51e06Spelikan bp->b_un.b_fs->e2fs_lastfsck = letoh32(sblock.e2fs.e2fs_lastfsck);
40760a51e06Spelikan bp->b_un.b_fs->e2fs_fsckintv = letoh32(sblock.e2fs.e2fs_fsckintv);
40860a51e06Spelikan bp->b_un.b_fs->e2fs_creator = letoh32(sblock.e2fs.e2fs_creator);
40960a51e06Spelikan bp->b_un.b_fs->e2fs_rev = letoh32(sblock.e2fs.e2fs_rev);
41060a51e06Spelikan bp->b_un.b_fs->e2fs_mnt_count = letoh16(sblock.e2fs.e2fs_mnt_count);
41165348f21Sjasoni bp->b_un.b_fs->e2fs_max_mnt_count =
41260a51e06Spelikan letoh16(sblock.e2fs.e2fs_max_mnt_count);
41360a51e06Spelikan bp->b_un.b_fs->e2fs_magic = letoh16(sblock.e2fs.e2fs_magic);
41460a51e06Spelikan bp->b_un.b_fs->e2fs_state = letoh16(sblock.e2fs.e2fs_state);
41560a51e06Spelikan bp->b_un.b_fs->e2fs_beh = letoh16(sblock.e2fs.e2fs_beh);
41660a51e06Spelikan bp->b_un.b_fs->e2fs_ruid = letoh16(sblock.e2fs.e2fs_ruid);
41760a51e06Spelikan bp->b_un.b_fs->e2fs_rgid = letoh16(sblock.e2fs.e2fs_rgid);
41865348f21Sjasoni }
41965348f21Sjasoni
42065348f21Sjasoni void
badsb(int listerr,char * s)4218809fabbSderaadt badsb(int listerr, char *s)
4228c424e8eSdownsj {
4238c424e8eSdownsj
4248c424e8eSdownsj if (!listerr)
4258c424e8eSdownsj return;
4268c424e8eSdownsj if (preen)
4278c424e8eSdownsj printf("%s: ", cdevname());
4288c424e8eSdownsj pfatal("BAD SUPER BLOCK: %s\n", s);
4298c424e8eSdownsj }
4308c424e8eSdownsj
4318c424e8eSdownsj /*
4328c424e8eSdownsj * Calculate a prototype superblock based on information in the disk label.
4338c424e8eSdownsj * When done the cgsblock macro can be calculated and the fs_ncg field
4348c424e8eSdownsj * can be used. Do NOT attempt to use other macros without verifying that
4358c424e8eSdownsj * their needed information is available!
4368c424e8eSdownsj */
4378c424e8eSdownsj int
calcsb(char * dev,int devfd,struct m_ext2fs * fs,struct disklabel * lp)438c0c611b7Sderaadt calcsb(char *dev, int devfd, struct m_ext2fs *fs, struct disklabel *lp)
4398c424e8eSdownsj {
4400190393fSart struct partition *pp;
4410190393fSart char *cp;
4428c424e8eSdownsj
443e45111b0Sguenther cp = strchr(dev, '\0');
444e45111b0Sguenther if ((cp == NULL || (cp[-1] < 'a' || cp[-1] >= 'a' + MAXPARTITIONS)) &&
445e45111b0Sguenther !isdigit((unsigned char)cp[-1])) {
4468c424e8eSdownsj pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
4478c424e8eSdownsj return (0);
4488c424e8eSdownsj }
449e45111b0Sguenther cp--;
450c0c611b7Sderaadt if (lp == NULL)
451c0c611b7Sderaadt pfatal("%s: CANNOT READ DISKLABEL\n", dev);
452e45111b0Sguenther if (isdigit((unsigned char)*cp))
4538c424e8eSdownsj pp = &lp->d_partitions[0];
4548c424e8eSdownsj else
4558c424e8eSdownsj pp = &lp->d_partitions[*cp - 'a'];
45665348f21Sjasoni if (pp->p_fstype != FS_EXT2FS) {
45765348f21Sjasoni pfatal("%s: NOT LABELED AS A EXT2 FILE SYSTEM (%s)\n",
4588c424e8eSdownsj dev, pp->p_fstype < FSMAXTYPES ?
4598c424e8eSdownsj fstypenames[pp->p_fstype] : "unknown");
4608c424e8eSdownsj return (0);
4618c424e8eSdownsj }
4628c424e8eSdownsj memset(fs, 0, sizeof(struct m_ext2fs));
463ddfcbf38Sotto fs->e2fs_bsize = DISKLABELV1_FFS_FSIZE(pp->p_fragblock); /* XXX */
46428704fc2Sderaadt if (fs->e2fs_bsize == 0) {
46528704fc2Sderaadt pfatal("%s: BLOCK SIZE DETERMINED TO BE ZERO\n", dev);
46628704fc2Sderaadt return (0);
46728704fc2Sderaadt }
468ddfcbf38Sotto fs->e2fs.e2fs_log_bsize = fs->e2fs_bsize / 1024;
4698c424e8eSdownsj fs->e2fs.e2fs_bcount = (pp->p_size * DEV_BSIZE) / fs->e2fs_bsize;
47065348f21Sjasoni fs->e2fs.e2fs_first_dblock = (fs->e2fs.e2fs_log_bsize == 0) ? 1 : 0;
4718c424e8eSdownsj fs->e2fs.e2fs_bpg = fs->e2fs_bsize * NBBY;
4728c424e8eSdownsj fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs.e2fs_log_bsize;
4738c424e8eSdownsj fs->e2fs_qbmask = fs->e2fs_bsize - 1;
4748c424e8eSdownsj fs->e2fs_bmask = ~fs->e2fs_qbmask;
4758c424e8eSdownsj fs->e2fs_ncg =
4768c424e8eSdownsj howmany(fs->e2fs.e2fs_bcount - fs->e2fs.e2fs_first_dblock,
4778c424e8eSdownsj fs->e2fs.e2fs_bpg);
4788c424e8eSdownsj fs->e2fs_fsbtodb = fs->e2fs.e2fs_log_bsize + 1;
4798c424e8eSdownsj fs->e2fs_ngdb = howmany(fs->e2fs_ncg,
4808c424e8eSdownsj fs->e2fs_bsize / sizeof(struct ext2_gd));
4818c424e8eSdownsj
4828c424e8eSdownsj return (1);
4838c424e8eSdownsj }
4848c424e8eSdownsj
4858c424e8eSdownsj static struct disklabel *
getdisklabel(char * s,int fd)4868809fabbSderaadt getdisklabel(char *s, int fd)
4878c424e8eSdownsj {
4888c424e8eSdownsj static struct disklabel lab;
4898c424e8eSdownsj
490df69c215Sderaadt if (ioctl(fd, DIOCGDINFO, (char *)&lab) == -1) {
4918c424e8eSdownsj if (s == NULL)
492c0c611b7Sderaadt return (NULL);
4938c424e8eSdownsj pwarn("ioctl (GCINFO): %s\n", strerror(errno));
4948c424e8eSdownsj errexit("%s: can't read disk label\n", s);
4958c424e8eSdownsj }
4968c424e8eSdownsj return (&lab);
4978c424e8eSdownsj }
49865348f21Sjasoni
499b6d2e2d5Sderaadt daddr32_t
cgoverhead(int c)5008809fabbSderaadt cgoverhead(int c)
50165348f21Sjasoni {
50265348f21Sjasoni int overh;
50365348f21Sjasoni overh = 1 /* block bitmap */ +
50465348f21Sjasoni 1 /* inode bitmap */ +
50565348f21Sjasoni sblock.e2fs_itpg;
50665348f21Sjasoni if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
507*bf2afe82Skevlo sblock.e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSE_SUPER) {
50865348f21Sjasoni if (cg_has_sb(c) == 0)
50965348f21Sjasoni return overh;
51065348f21Sjasoni }
51165348f21Sjasoni overh += 1 + sblock.e2fs_ngdb;
51265348f21Sjasoni return overh;
51365348f21Sjasoni }
514