setup.c revision 71884
11558Srgrimes/* 21558Srgrimes * Copyright (c) 1980, 1986, 1993 31558Srgrimes * The Regents of the University of California. All rights reserved. 41558Srgrimes * 51558Srgrimes * Redistribution and use in source and binary forms, with or without 61558Srgrimes * modification, are permitted provided that the following conditions 71558Srgrimes * are met: 81558Srgrimes * 1. Redistributions of source code must retain the above copyright 91558Srgrimes * notice, this list of conditions and the following disclaimer. 101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111558Srgrimes * notice, this list of conditions and the following disclaimer in the 121558Srgrimes * documentation and/or other materials provided with the distribution. 131558Srgrimes * 3. All advertising materials mentioning features or use of this software 141558Srgrimes * must display the following acknowledgement: 151558Srgrimes * This product includes software developed by the University of 161558Srgrimes * California, Berkeley and its contributors. 171558Srgrimes * 4. Neither the name of the University nor the names of its contributors 181558Srgrimes * may be used to endorse or promote products derived from this software 191558Srgrimes * without specific prior written permission. 201558Srgrimes * 211558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241558Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311558Srgrimes * SUCH DAMAGE. 321558Srgrimes */ 331558Srgrimes 341558Srgrimes#ifndef lint 3541477Sjulian#if 0 3623675Speterstatic const char sccsid[] = "@(#)setup.c 8.10 (Berkeley) 5/9/95"; 3741477Sjulian#endif 3841477Sjulianstatic const char rcsid[] = 3950476Speter "$FreeBSD: head/sbin/fsck_ffs/setup.c 71884 2001-01-31 15:16:56Z iedowse $"; 401558Srgrimes#endif /* not lint */ 411558Srgrimes 421558Srgrimes#define DKTYPENAMES 431558Srgrimes#include <sys/param.h> 441558Srgrimes#include <sys/stat.h> 451558Srgrimes#include <sys/disklabel.h> 461558Srgrimes#include <sys/file.h> 4723675Speter 4823675Speter#include <ufs/ufs/dinode.h> 4923675Speter#include <ufs/ffs/fs.h> 5023675Speter 5123675Speter#include <ctype.h> 5223675Speter#include <err.h> 531558Srgrimes#include <errno.h> 541558Srgrimes#include <string.h> 5523675Speter 561558Srgrimes#include "fsck.h" 571558Srgrimes 581558Srgrimesstruct bufarea asblk; 591558Srgrimes#define altsblock (*asblk.b_un.b_fs) 601558Srgrimes#define POWEROF2(num) (((num) & ((num) - 1)) == 0) 611558Srgrimes 6223675Speterstatic void badsb __P((int listerr, char *s)); 6323675Speterstatic int calcsb __P((char *dev, int devfd, struct fs *fs)); 6423675Speterstatic struct disklabel *getdisklabel __P((char *s, int fd)); 6523675Speterstatic int readsb __P((int listerr)); 661558Srgrimes 6723675Speter/* 6823675Speter * Read in a superblock finding an alternate if necessary. 6923675Speter * Return 1 if successful, 0 if unsuccessful, -1 if filesystem 7023675Speter * is already clean (preen mode only). 7123675Speter */ 727585Sbdeint 731558Srgrimessetup(dev) 741558Srgrimes char *dev; 751558Srgrimes{ 761558Srgrimes long cg, size, asked, i, j; 7723675Speter long skipclean, bmapsize; 781558Srgrimes struct disklabel *lp; 791558Srgrimes off_t sizepb; 801558Srgrimes struct stat statb; 811558Srgrimes struct fs proto; 821558Srgrimes 831558Srgrimes havesb = 0; 841558Srgrimes fswritefd = -1; 8562668Smckusick cursnapshot = 0; 8641474Sjulian skipclean = fflag ? 0 : preen; 871558Srgrimes if (stat(dev, &statb) < 0) { 881558Srgrimes printf("Can't stat %s: %s\n", dev, strerror(errno)); 891558Srgrimes return (0); 901558Srgrimes } 9153781Sphk if ((statb.st_mode & S_IFMT) != S_IFCHR && 9253781Sphk (statb.st_mode & S_IFMT) != S_IFBLK) { 9362668Smckusick if ((statb.st_flags & SF_SNAPSHOT) != 0) { 9462668Smckusick cursnapshot = statb.st_ino; 9562668Smckusick } else { 9662668Smckusick pfatal("%s is not a disk device", dev); 9762668Smckusick if (reply("CONTINUE") == 0) 9862668Smckusick return (0); 9962668Smckusick } 1001558Srgrimes } 1011558Srgrimes if ((fsreadfd = open(dev, O_RDONLY)) < 0) { 1021558Srgrimes printf("Can't open %s: %s\n", dev, strerror(errno)); 1031558Srgrimes return (0); 1041558Srgrimes } 1051558Srgrimes if (preen == 0) 1061558Srgrimes printf("** %s", dev); 1071558Srgrimes if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) { 1081558Srgrimes fswritefd = -1; 1091558Srgrimes if (preen) 1101558Srgrimes pfatal("NO WRITE ACCESS"); 1111558Srgrimes printf(" (NO WRITE)"); 1121558Srgrimes } 1131558Srgrimes if (preen == 0) 1141558Srgrimes printf("\n"); 1151558Srgrimes fsmodified = 0; 1161558Srgrimes lfdir = 0; 1171558Srgrimes initbarea(&sblk); 1181558Srgrimes initbarea(&asblk); 1191558Srgrimes sblk.b_un.b_buf = malloc(SBSIZE); 1201558Srgrimes asblk.b_un.b_buf = malloc(SBSIZE); 1211558Srgrimes if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL) 12223675Speter errx(EEXIT, "cannot allocate space for superblock"); 12341474Sjulian if ((lp = getdisklabel(NULL, fsreadfd))) 1241558Srgrimes dev_bsize = secsize = lp->d_secsize; 1251558Srgrimes else 1261558Srgrimes dev_bsize = secsize = DEV_BSIZE; 1271558Srgrimes /* 1281558Srgrimes * Read in the superblock, looking for alternates if necessary 1291558Srgrimes */ 1301558Srgrimes if (readsb(1) == 0) { 13123675Speter skipclean = 0; 1321558Srgrimes if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0) 1331558Srgrimes return(0); 1341558Srgrimes if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0) 1351558Srgrimes return (0); 1361558Srgrimes for (cg = 0; cg < proto.fs_ncg; cg++) { 1371558Srgrimes bflag = fsbtodb(&proto, cgsblock(&proto, cg)); 1381558Srgrimes if (readsb(0) != 0) 1391558Srgrimes break; 1401558Srgrimes } 1411558Srgrimes if (cg >= proto.fs_ncg) { 1421558Srgrimes printf("%s %s\n%s %s\n%s %s\n", 1431558Srgrimes "SEARCH FOR ALTERNATE SUPER-BLOCK", 1441558Srgrimes "FAILED. YOU MUST USE THE", 1451558Srgrimes "-b OPTION TO FSCK TO SPECIFY THE", 1461558Srgrimes "LOCATION OF AN ALTERNATE", 1471558Srgrimes "SUPER-BLOCK TO SUPPLY NEEDED", 1481558Srgrimes "INFORMATION; SEE fsck(8)."); 1496280Sbde bflag = 0; 1501558Srgrimes return(0); 1511558Srgrimes } 1521558Srgrimes pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag); 1536280Sbde bflag = 0; 1541558Srgrimes } 15541474Sjulian if (skipclean && sblock.fs_clean) { 15641474Sjulian pwarn("FILESYSTEM CLEAN; SKIPPING CHECKS\n"); 15741474Sjulian return (-1); 15841474Sjulian } 1591558Srgrimes maxfsblock = sblock.fs_size; 1601558Srgrimes maxino = sblock.fs_ncg * sblock.fs_ipg; 1611558Srgrimes /* 1621558Srgrimes * Check and potentially fix certain fields in the super block. 1631558Srgrimes */ 1641558Srgrimes if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) { 1651558Srgrimes pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK"); 1661558Srgrimes if (reply("SET TO DEFAULT") == 1) { 1671558Srgrimes sblock.fs_optim = FS_OPTTIME; 1681558Srgrimes sbdirty(); 1691558Srgrimes } 1701558Srgrimes } 1711558Srgrimes if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) { 1721558Srgrimes pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK", 1731558Srgrimes sblock.fs_minfree); 1741558Srgrimes if (reply("SET TO DEFAULT") == 1) { 1751558Srgrimes sblock.fs_minfree = 10; 1761558Srgrimes sbdirty(); 1771558Srgrimes } 1781558Srgrimes } 1798871Srgrimes if (sblock.fs_interleave < 1 || 1801558Srgrimes sblock.fs_interleave > sblock.fs_nsect) { 1811558Srgrimes pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK", 1821558Srgrimes sblock.fs_interleave); 1831558Srgrimes sblock.fs_interleave = 1; 1841558Srgrimes if (preen) 1851558Srgrimes printf(" (FIXED)\n"); 1861558Srgrimes if (preen || reply("SET TO DEFAULT") == 1) { 1871558Srgrimes sbdirty(); 1881558Srgrimes dirty(&asblk); 1891558Srgrimes } 1901558Srgrimes } 1918871Srgrimes if (sblock.fs_npsect < sblock.fs_nsect || 1921558Srgrimes sblock.fs_npsect > sblock.fs_nsect*2) { 1931558Srgrimes pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK", 1941558Srgrimes sblock.fs_npsect); 1951558Srgrimes sblock.fs_npsect = sblock.fs_nsect; 1961558Srgrimes if (preen) 1971558Srgrimes printf(" (FIXED)\n"); 1981558Srgrimes if (preen || reply("SET TO DEFAULT") == 1) { 1991558Srgrimes sbdirty(); 2001558Srgrimes dirty(&asblk); 2011558Srgrimes } 2021558Srgrimes } 2031558Srgrimes if (sblock.fs_inodefmt >= FS_44INODEFMT) { 2041558Srgrimes newinofmt = 1; 2051558Srgrimes } else { 2061558Srgrimes sblock.fs_qbmask = ~sblock.fs_bmask; 2071558Srgrimes sblock.fs_qfmask = ~sblock.fs_fmask; 20871884Siedowse /* This should match the kernel limit in ffs_oldfscompat(). */ 20971884Siedowse sblock.fs_maxfilesize = (u_int64_t)1 << 39; 2101558Srgrimes newinofmt = 0; 2111558Srgrimes } 2121558Srgrimes /* 2131558Srgrimes * Convert to new inode format. 2141558Srgrimes */ 2151558Srgrimes if (cvtlevel >= 2 && sblock.fs_inodefmt < FS_44INODEFMT) { 2161558Srgrimes if (preen) 2171558Srgrimes pwarn("CONVERTING TO NEW INODE FORMAT\n"); 2181558Srgrimes else if (!reply("CONVERT TO NEW INODE FORMAT")) 2191558Srgrimes return(0); 2201558Srgrimes doinglevel2++; 2211558Srgrimes sblock.fs_inodefmt = FS_44INODEFMT; 2221558Srgrimes sizepb = sblock.fs_bsize; 2231558Srgrimes sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1; 2241558Srgrimes for (i = 0; i < NIADDR; i++) { 2251558Srgrimes sizepb *= NINDIR(&sblock); 2261558Srgrimes sblock.fs_maxfilesize += sizepb; 2271558Srgrimes } 2281558Srgrimes sblock.fs_maxsymlinklen = MAXSYMLINKLEN; 2291558Srgrimes sblock.fs_qbmask = ~sblock.fs_bmask; 2301558Srgrimes sblock.fs_qfmask = ~sblock.fs_fmask; 2311558Srgrimes sbdirty(); 2321558Srgrimes dirty(&asblk); 2331558Srgrimes } 2341558Srgrimes /* 2351558Srgrimes * Convert to new cylinder group format. 2361558Srgrimes */ 2371558Srgrimes if (cvtlevel >= 1 && sblock.fs_postblformat == FS_42POSTBLFMT) { 2381558Srgrimes if (preen) 2391558Srgrimes pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n"); 2401558Srgrimes else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT")) 2411558Srgrimes return(0); 2421558Srgrimes doinglevel1++; 2431558Srgrimes sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT; 2441558Srgrimes sblock.fs_nrpos = 8; 2451558Srgrimes sblock.fs_postbloff = 2461558Srgrimes (char *)(&sblock.fs_opostbl[0][0]) - 24723675Speter (char *)(&sblock.fs_firstfield); 2481558Srgrimes sblock.fs_rotbloff = &sblock.fs_space[0] - 24923675Speter (u_char *)(&sblock.fs_firstfield); 2501558Srgrimes sblock.fs_cgsize = 2511558Srgrimes fragroundup(&sblock, CGSIZE(&sblock)); 2521558Srgrimes sbdirty(); 2531558Srgrimes dirty(&asblk); 2541558Srgrimes } 25523675Speter if (asblk.b_dirty && !bflag) { 25623675Speter memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize); 2571558Srgrimes flush(fswritefd, &asblk); 2581558Srgrimes } 2591558Srgrimes /* 2601558Srgrimes * read in the summary info. 2611558Srgrimes */ 2621558Srgrimes asked = 0; 26371073Siedowse sblock.fs_csp = calloc(1, sblock.fs_cssize); 2641558Srgrimes for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) { 2651558Srgrimes size = sblock.fs_cssize - i < sblock.fs_bsize ? 2661558Srgrimes sblock.fs_cssize - i : sblock.fs_bsize; 26771073Siedowse if (bread(fsreadfd, (char *)sblock.fs_csp + i, 2681558Srgrimes fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag), 2691558Srgrimes size) != 0 && !asked) { 2701558Srgrimes pfatal("BAD SUMMARY INFORMATION"); 27134266Sjulian if (reply("CONTINUE") == 0) { 27234266Sjulian ckfini(0); 27323675Speter exit(EEXIT); 27434266Sjulian } 2751558Srgrimes asked++; 2761558Srgrimes } 2771558Srgrimes } 2781558Srgrimes /* 2791558Srgrimes * allocate and initialize the necessary maps 2801558Srgrimes */ 2811558Srgrimes bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(short)); 2821558Srgrimes blockmap = calloc((unsigned)bmapsize, sizeof (char)); 2831558Srgrimes if (blockmap == NULL) { 2841558Srgrimes printf("cannot alloc %u bytes for blockmap\n", 2851558Srgrimes (unsigned)bmapsize); 2861558Srgrimes goto badsb; 2871558Srgrimes } 28841474Sjulian inostathead = calloc((unsigned)(sblock.fs_ncg), 28941474Sjulian sizeof(struct inostatlist)); 29041474Sjulian if (inostathead == NULL) { 29141474Sjulian printf("cannot alloc %u bytes for inostathead\n", 29241474Sjulian (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg))); 2931558Srgrimes goto badsb; 2941558Srgrimes } 2951558Srgrimes numdirs = sblock.fs_cstotal.cs_ndir; 29657573Smckusick dirhash = numdirs; 29734033Sphk if (numdirs == 0) { 29834033Sphk printf("numdirs is zero, try using an alternate superblock\n"); 29934033Sphk goto badsb; 30034033Sphk } 3011558Srgrimes inplast = 0; 3021558Srgrimes listmax = numdirs + 10; 3031558Srgrimes inpsort = (struct inoinfo **)calloc((unsigned)listmax, 3041558Srgrimes sizeof(struct inoinfo *)); 3051558Srgrimes inphead = (struct inoinfo **)calloc((unsigned)numdirs, 3061558Srgrimes sizeof(struct inoinfo *)); 3071558Srgrimes if (inpsort == NULL || inphead == NULL) { 3088871Srgrimes printf("cannot alloc %u bytes for inphead\n", 3091558Srgrimes (unsigned)numdirs * sizeof(struct inoinfo *)); 3101558Srgrimes goto badsb; 3111558Srgrimes } 3121558Srgrimes bufinit(); 31334266Sjulian if (sblock.fs_flags & FS_DOSOFTDEP) 31434266Sjulian usedsoftdep = 1; 31534266Sjulian else 31634266Sjulian usedsoftdep = 0; 3171558Srgrimes return (1); 3181558Srgrimes 3191558Srgrimesbadsb: 32023675Speter ckfini(0); 3211558Srgrimes return (0); 3221558Srgrimes} 3231558Srgrimes 3241558Srgrimes/* 3251558Srgrimes * Read in the super block and its summary info. 3261558Srgrimes */ 3277585Sbdestatic int 3281558Srgrimesreadsb(listerr) 3291558Srgrimes int listerr; 3301558Srgrimes{ 33123675Speter ufs_daddr_t super = bflag ? bflag : SBOFF / dev_bsize; 3321558Srgrimes 3331558Srgrimes if (bread(fsreadfd, (char *)&sblock, super, (long)SBSIZE) != 0) 3341558Srgrimes return (0); 3351558Srgrimes sblk.b_bno = super; 3361558Srgrimes sblk.b_size = SBSIZE; 3371558Srgrimes /* 3381558Srgrimes * run a few consistency checks of the super block 3391558Srgrimes */ 3401558Srgrimes if (sblock.fs_magic != FS_MAGIC) 3411558Srgrimes { badsb(listerr, "MAGIC NUMBER WRONG"); return (0); } 3421558Srgrimes if (sblock.fs_ncg < 1) 3431558Srgrimes { badsb(listerr, "NCG OUT OF RANGE"); return (0); } 3441558Srgrimes if (sblock.fs_cpg < 1) 3451558Srgrimes { badsb(listerr, "CPG OUT OF RANGE"); return (0); } 3461558Srgrimes if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl || 3471558Srgrimes (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl) 3481558Srgrimes { badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); } 3491558Srgrimes if (sblock.fs_sbsize > SBSIZE) 3501558Srgrimes { badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); } 3511558Srgrimes /* 3521558Srgrimes * Compute block size that the filesystem is based on, 3531558Srgrimes * according to fsbtodb, and adjust superblock block number 3541558Srgrimes * so we can tell if this is an alternate later. 3551558Srgrimes */ 3561558Srgrimes super *= dev_bsize; 3571558Srgrimes dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1); 3581558Srgrimes sblk.b_bno = super / dev_bsize; 3591558Srgrimes if (bflag) { 3601558Srgrimes havesb = 1; 3611558Srgrimes return (1); 3621558Srgrimes } 3631558Srgrimes /* 3641558Srgrimes * Set all possible fields that could differ, then do check 3651558Srgrimes * of whole super block against an alternate super block. 3661558Srgrimes * When an alternate super-block is specified this check is skipped. 3671558Srgrimes */ 3681558Srgrimes getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize); 3691558Srgrimes if (asblk.b_errs) 3701558Srgrimes return (0); 37123675Speter altsblock.fs_firstfield = sblock.fs_firstfield; 37223675Speter altsblock.fs_unused_1 = sblock.fs_unused_1; 3731558Srgrimes altsblock.fs_time = sblock.fs_time; 3741558Srgrimes altsblock.fs_cstotal = sblock.fs_cstotal; 3751558Srgrimes altsblock.fs_cgrotor = sblock.fs_cgrotor; 3761558Srgrimes altsblock.fs_fmod = sblock.fs_fmod; 3771558Srgrimes altsblock.fs_clean = sblock.fs_clean; 3781558Srgrimes altsblock.fs_ronly = sblock.fs_ronly; 3791558Srgrimes altsblock.fs_flags = sblock.fs_flags; 3801558Srgrimes altsblock.fs_maxcontig = sblock.fs_maxcontig; 3811558Srgrimes altsblock.fs_minfree = sblock.fs_minfree; 3821558Srgrimes altsblock.fs_optim = sblock.fs_optim; 3831558Srgrimes altsblock.fs_rotdelay = sblock.fs_rotdelay; 3841558Srgrimes altsblock.fs_maxbpg = sblock.fs_maxbpg; 38571073Siedowse memmove(altsblock.fs_ocsp, sblock.fs_ocsp, sizeof sblock.fs_ocsp); 38671073Siedowse altsblock.fs_csp = sblock.fs_csp; 38723675Speter altsblock.fs_maxcluster = sblock.fs_maxcluster; 38823675Speter memmove(altsblock.fs_fsmnt, sblock.fs_fsmnt, sizeof sblock.fs_fsmnt); 38962668Smckusick memmove(altsblock.fs_snapinum, sblock.fs_snapinum, 39062668Smckusick sizeof sblock.fs_snapinum); 39123675Speter memmove(altsblock.fs_sparecon, 39223675Speter sblock.fs_sparecon, sizeof sblock.fs_sparecon); 3931558Srgrimes /* 3941558Srgrimes * The following should not have to be copied. 3951558Srgrimes */ 3961558Srgrimes altsblock.fs_fsbtodb = sblock.fs_fsbtodb; 3971558Srgrimes altsblock.fs_interleave = sblock.fs_interleave; 3981558Srgrimes altsblock.fs_npsect = sblock.fs_npsect; 3991558Srgrimes altsblock.fs_nrpos = sblock.fs_nrpos; 40023675Speter altsblock.fs_state = sblock.fs_state; 4011558Srgrimes altsblock.fs_qbmask = sblock.fs_qbmask; 4021558Srgrimes altsblock.fs_qfmask = sblock.fs_qfmask; 4031558Srgrimes altsblock.fs_state = sblock.fs_state; 4041558Srgrimes altsblock.fs_maxfilesize = sblock.fs_maxfilesize; 40523675Speter if (memcmp(&sblock, &altsblock, (int)sblock.fs_sbsize)) { 40623675Speter if (debug) { 40723675Speter long *nlp, *olp, *endlp; 40823675Speter 40923675Speter printf("superblock mismatches\n"); 41023675Speter nlp = (long *)&altsblock; 41123675Speter olp = (long *)&sblock; 41223675Speter endlp = olp + (sblock.fs_sbsize / sizeof *olp); 41323675Speter for ( ; olp < endlp; olp++, nlp++) { 41423675Speter if (*olp == *nlp) 41523675Speter continue; 41637236Sbde printf( 41737236Sbde "offset %d, original %ld, alternate %ld\n", 41823675Speter olp - (long *)&sblock, *olp, *nlp); 41923675Speter } 42023675Speter } 4211558Srgrimes badsb(listerr, 4221558Srgrimes "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE"); 4231558Srgrimes return (0); 4241558Srgrimes } 4251558Srgrimes havesb = 1; 4261558Srgrimes return (1); 4271558Srgrimes} 4281558Srgrimes 4297585Sbdestatic void 4301558Srgrimesbadsb(listerr, s) 4311558Srgrimes int listerr; 4321558Srgrimes char *s; 4331558Srgrimes{ 4341558Srgrimes 4351558Srgrimes if (!listerr) 4361558Srgrimes return; 4371558Srgrimes if (preen) 4381558Srgrimes printf("%s: ", cdevname); 4391558Srgrimes pfatal("BAD SUPER BLOCK: %s\n", s); 4401558Srgrimes} 4411558Srgrimes 4421558Srgrimes/* 4431558Srgrimes * Calculate a prototype superblock based on information in the disk label. 4441558Srgrimes * When done the cgsblock macro can be calculated and the fs_ncg field 4451558Srgrimes * can be used. Do NOT attempt to use other macros without verifying that 4461558Srgrimes * their needed information is available! 4471558Srgrimes */ 44823675Speterstatic int 4491558Srgrimescalcsb(dev, devfd, fs) 4501558Srgrimes char *dev; 4511558Srgrimes int devfd; 4521558Srgrimes register struct fs *fs; 4531558Srgrimes{ 4541558Srgrimes register struct disklabel *lp; 4551558Srgrimes register struct partition *pp; 4561558Srgrimes register char *cp; 4571558Srgrimes int i; 4581558Srgrimes 45923675Speter cp = strchr(dev, '\0') - 1; 4607585Sbde if (cp == (char *)-1 || ((*cp < 'a' || *cp > 'h') && !isdigit(*cp))) { 4611558Srgrimes pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev); 4621558Srgrimes return (0); 4631558Srgrimes } 4641558Srgrimes lp = getdisklabel(dev, devfd); 4651558Srgrimes if (isdigit(*cp)) 4661558Srgrimes pp = &lp->d_partitions[0]; 4671558Srgrimes else 4681558Srgrimes pp = &lp->d_partitions[*cp - 'a']; 4691558Srgrimes if (pp->p_fstype != FS_BSDFFS) { 4701558Srgrimes pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n", 4711558Srgrimes dev, pp->p_fstype < FSMAXTYPES ? 4721558Srgrimes fstypenames[pp->p_fstype] : "unknown"); 4731558Srgrimes return (0); 4741558Srgrimes } 47541474Sjulian if (pp->p_fsize == 0 || pp->p_frag == 0 || 47641474Sjulian pp->p_cpg == 0 || pp->p_size == 0) { 47741474Sjulian pfatal("%s: %s: type %s fsize %d, frag %d, cpg %d, size %d\n", 47841474Sjulian dev, "INCOMPLETE LABEL", fstypenames[pp->p_fstype], 47941474Sjulian pp->p_fsize, pp->p_frag, pp->p_cpg, pp->p_size); 48032622Sbde return (0); 48132622Sbde } 48223675Speter memset(fs, 0, sizeof(struct fs)); 4831558Srgrimes fs->fs_fsize = pp->p_fsize; 4841558Srgrimes fs->fs_frag = pp->p_frag; 4851558Srgrimes fs->fs_cpg = pp->p_cpg; 4861558Srgrimes fs->fs_size = pp->p_size; 4871558Srgrimes fs->fs_ntrak = lp->d_ntracks; 4881558Srgrimes fs->fs_nsect = lp->d_nsectors; 4891558Srgrimes fs->fs_spc = lp->d_secpercyl; 4901558Srgrimes fs->fs_nspf = fs->fs_fsize / lp->d_secsize; 4911558Srgrimes fs->fs_sblkno = roundup( 4921558Srgrimes howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize), 4931558Srgrimes fs->fs_frag); 4941558Srgrimes fs->fs_cgmask = 0xffffffff; 4951558Srgrimes for (i = fs->fs_ntrak; i > 1; i >>= 1) 4961558Srgrimes fs->fs_cgmask <<= 1; 4971558Srgrimes if (!POWEROF2(fs->fs_ntrak)) 4981558Srgrimes fs->fs_cgmask <<= 1; 4991558Srgrimes fs->fs_cgoffset = roundup( 5001558Srgrimes howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag); 5011558Srgrimes fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs); 5021558Srgrimes fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg); 5031558Srgrimes for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1) 5041558Srgrimes fs->fs_fsbtodb++; 5051558Srgrimes dev_bsize = lp->d_secsize; 5061558Srgrimes return (1); 5071558Srgrimes} 5081558Srgrimes 50923675Speterstatic struct disklabel * 5101558Srgrimesgetdisklabel(s, fd) 5111558Srgrimes char *s; 5121558Srgrimes int fd; 5131558Srgrimes{ 5141558Srgrimes static struct disklabel lab; 5151558Srgrimes 5161558Srgrimes if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) { 5171558Srgrimes if (s == NULL) 5181558Srgrimes return ((struct disklabel *)NULL); 5191558Srgrimes pwarn("ioctl (GCINFO): %s\n", strerror(errno)); 52023675Speter errx(EEXIT, "%s: can't read disk label", s); 5211558Srgrimes } 5221558Srgrimes return (&lab); 5231558Srgrimes} 524