setup.c revision 23675
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 3523675Speterstatic const char sccsid[] = "@(#)setup.c 8.10 (Berkeley) 5/9/95"; 361558Srgrimes#endif /* not lint */ 371558Srgrimes 381558Srgrimes#define DKTYPENAMES 391558Srgrimes#include <sys/param.h> 401558Srgrimes#include <sys/time.h> 411558Srgrimes#include <sys/stat.h> 421558Srgrimes#include <sys/ioctl.h> 431558Srgrimes#include <sys/disklabel.h> 441558Srgrimes#include <sys/file.h> 4523675Speter 4623675Speter#include <ufs/ufs/dinode.h> 4723675Speter#include <ufs/ffs/fs.h> 4823675Speter 4923675Speter#include <ctype.h> 5023675Speter#include <err.h> 511558Srgrimes#include <errno.h> 527585Sbde#include <stdio.h> 531558Srgrimes#include <stdlib.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; 8523675Speter skipclean = preen; 861558Srgrimes if (stat(dev, &statb) < 0) { 871558Srgrimes printf("Can't stat %s: %s\n", dev, strerror(errno)); 881558Srgrimes return (0); 891558Srgrimes } 901558Srgrimes if ((statb.st_mode & S_IFMT) != S_IFCHR) { 911558Srgrimes pfatal("%s is not a character device", dev); 921558Srgrimes if (reply("CONTINUE") == 0) 931558Srgrimes return (0); 941558Srgrimes } 951558Srgrimes if ((fsreadfd = open(dev, O_RDONLY)) < 0) { 961558Srgrimes printf("Can't open %s: %s\n", dev, strerror(errno)); 971558Srgrimes return (0); 981558Srgrimes } 991558Srgrimes if (preen == 0) 1001558Srgrimes printf("** %s", dev); 1011558Srgrimes if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) { 1021558Srgrimes fswritefd = -1; 1031558Srgrimes if (preen) 1041558Srgrimes pfatal("NO WRITE ACCESS"); 1051558Srgrimes printf(" (NO WRITE)"); 1061558Srgrimes } 1071558Srgrimes if (preen == 0) 1081558Srgrimes printf("\n"); 1091558Srgrimes fsmodified = 0; 1101558Srgrimes lfdir = 0; 1111558Srgrimes initbarea(&sblk); 1121558Srgrimes initbarea(&asblk); 1131558Srgrimes sblk.b_un.b_buf = malloc(SBSIZE); 1141558Srgrimes asblk.b_un.b_buf = malloc(SBSIZE); 1151558Srgrimes if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL) 11623675Speter errx(EEXIT, "cannot allocate space for superblock"); 1177585Sbde lp = getdisklabel((char *)NULL, fsreadfd); 1187585Sbde if (lp) 1191558Srgrimes dev_bsize = secsize = lp->d_secsize; 1201558Srgrimes else 1211558Srgrimes dev_bsize = secsize = DEV_BSIZE; 1221558Srgrimes /* 1231558Srgrimes * Read in the superblock, looking for alternates if necessary 1241558Srgrimes */ 1251558Srgrimes if (readsb(1) == 0) { 12623675Speter skipclean = 0; 1271558Srgrimes if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0) 1281558Srgrimes return(0); 1291558Srgrimes if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0) 1301558Srgrimes return (0); 1311558Srgrimes for (cg = 0; cg < proto.fs_ncg; cg++) { 1321558Srgrimes bflag = fsbtodb(&proto, cgsblock(&proto, cg)); 1331558Srgrimes if (readsb(0) != 0) 1341558Srgrimes break; 1351558Srgrimes } 1361558Srgrimes if (cg >= proto.fs_ncg) { 1371558Srgrimes printf("%s %s\n%s %s\n%s %s\n", 1381558Srgrimes "SEARCH FOR ALTERNATE SUPER-BLOCK", 1391558Srgrimes "FAILED. YOU MUST USE THE", 1401558Srgrimes "-b OPTION TO FSCK TO SPECIFY THE", 1411558Srgrimes "LOCATION OF AN ALTERNATE", 1421558Srgrimes "SUPER-BLOCK TO SUPPLY NEEDED", 1431558Srgrimes "INFORMATION; SEE fsck(8)."); 1446280Sbde bflag = 0; 1451558Srgrimes return(0); 1461558Srgrimes } 1471558Srgrimes pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag); 1486280Sbde bflag = 0; 1491558Srgrimes } 15023675Speter if (skipclean && sblock.fs_clean) { 15123675Speter pwarn("FILESYSTEM CLEAN; SKIPPING CHECKS\n"); 15223675Speter return (-1); 15323675Speter } 1541558Srgrimes maxfsblock = sblock.fs_size; 1551558Srgrimes maxino = sblock.fs_ncg * sblock.fs_ipg; 1561558Srgrimes /* 1571558Srgrimes * Check and potentially fix certain fields in the super block. 1581558Srgrimes */ 1591558Srgrimes if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) { 1601558Srgrimes pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK"); 1611558Srgrimes if (reply("SET TO DEFAULT") == 1) { 1621558Srgrimes sblock.fs_optim = FS_OPTTIME; 1631558Srgrimes sbdirty(); 1641558Srgrimes } 1651558Srgrimes } 1661558Srgrimes if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) { 1671558Srgrimes pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK", 1681558Srgrimes sblock.fs_minfree); 1691558Srgrimes if (reply("SET TO DEFAULT") == 1) { 1701558Srgrimes sblock.fs_minfree = 10; 1711558Srgrimes sbdirty(); 1721558Srgrimes } 1731558Srgrimes } 1748871Srgrimes if (sblock.fs_interleave < 1 || 1751558Srgrimes sblock.fs_interleave > sblock.fs_nsect) { 1761558Srgrimes pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK", 1771558Srgrimes sblock.fs_interleave); 1781558Srgrimes sblock.fs_interleave = 1; 1791558Srgrimes if (preen) 1801558Srgrimes printf(" (FIXED)\n"); 1811558Srgrimes if (preen || reply("SET TO DEFAULT") == 1) { 1821558Srgrimes sbdirty(); 1831558Srgrimes dirty(&asblk); 1841558Srgrimes } 1851558Srgrimes } 1868871Srgrimes if (sblock.fs_npsect < sblock.fs_nsect || 1871558Srgrimes sblock.fs_npsect > sblock.fs_nsect*2) { 1881558Srgrimes pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK", 1891558Srgrimes sblock.fs_npsect); 1901558Srgrimes sblock.fs_npsect = sblock.fs_nsect; 1911558Srgrimes if (preen) 1921558Srgrimes printf(" (FIXED)\n"); 1931558Srgrimes if (preen || reply("SET TO DEFAULT") == 1) { 1941558Srgrimes sbdirty(); 1951558Srgrimes dirty(&asblk); 1961558Srgrimes } 1971558Srgrimes } 1981558Srgrimes if (sblock.fs_inodefmt >= FS_44INODEFMT) { 1991558Srgrimes newinofmt = 1; 2001558Srgrimes } else { 2011558Srgrimes sblock.fs_qbmask = ~sblock.fs_bmask; 2021558Srgrimes sblock.fs_qfmask = ~sblock.fs_fmask; 2031558Srgrimes newinofmt = 0; 2041558Srgrimes } 2051558Srgrimes /* 2061558Srgrimes * Convert to new inode format. 2071558Srgrimes */ 2081558Srgrimes if (cvtlevel >= 2 && sblock.fs_inodefmt < FS_44INODEFMT) { 2091558Srgrimes if (preen) 2101558Srgrimes pwarn("CONVERTING TO NEW INODE FORMAT\n"); 2111558Srgrimes else if (!reply("CONVERT TO NEW INODE FORMAT")) 2121558Srgrimes return(0); 2131558Srgrimes doinglevel2++; 2141558Srgrimes sblock.fs_inodefmt = FS_44INODEFMT; 2151558Srgrimes sizepb = sblock.fs_bsize; 2161558Srgrimes sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1; 2171558Srgrimes for (i = 0; i < NIADDR; i++) { 2181558Srgrimes sizepb *= NINDIR(&sblock); 2191558Srgrimes sblock.fs_maxfilesize += sizepb; 2201558Srgrimes } 2211558Srgrimes sblock.fs_maxsymlinklen = MAXSYMLINKLEN; 2221558Srgrimes sblock.fs_qbmask = ~sblock.fs_bmask; 2231558Srgrimes sblock.fs_qfmask = ~sblock.fs_fmask; 2241558Srgrimes sbdirty(); 2251558Srgrimes dirty(&asblk); 2261558Srgrimes } 2271558Srgrimes /* 2281558Srgrimes * Convert to new cylinder group format. 2291558Srgrimes */ 2301558Srgrimes if (cvtlevel >= 1 && sblock.fs_postblformat == FS_42POSTBLFMT) { 2311558Srgrimes if (preen) 2321558Srgrimes pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n"); 2331558Srgrimes else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT")) 2341558Srgrimes return(0); 2351558Srgrimes doinglevel1++; 2361558Srgrimes sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT; 2371558Srgrimes sblock.fs_nrpos = 8; 2381558Srgrimes sblock.fs_postbloff = 2391558Srgrimes (char *)(&sblock.fs_opostbl[0][0]) - 24023675Speter (char *)(&sblock.fs_firstfield); 2411558Srgrimes sblock.fs_rotbloff = &sblock.fs_space[0] - 24223675Speter (u_char *)(&sblock.fs_firstfield); 2431558Srgrimes sblock.fs_cgsize = 2441558Srgrimes fragroundup(&sblock, CGSIZE(&sblock)); 2451558Srgrimes sbdirty(); 2461558Srgrimes dirty(&asblk); 2471558Srgrimes } 24823675Speter if (asblk.b_dirty && !bflag) { 24923675Speter memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize); 2501558Srgrimes flush(fswritefd, &asblk); 2511558Srgrimes } 2521558Srgrimes /* 2531558Srgrimes * read in the summary info. 2541558Srgrimes */ 2551558Srgrimes asked = 0; 2561558Srgrimes for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) { 2571558Srgrimes size = sblock.fs_cssize - i < sblock.fs_bsize ? 2581558Srgrimes sblock.fs_cssize - i : sblock.fs_bsize; 2591558Srgrimes sblock.fs_csp[j] = (struct csum *)calloc(1, (unsigned)size); 2601558Srgrimes if (bread(fsreadfd, (char *)sblock.fs_csp[j], 2611558Srgrimes fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag), 2621558Srgrimes size) != 0 && !asked) { 2631558Srgrimes pfatal("BAD SUMMARY INFORMATION"); 2641558Srgrimes if (reply("CONTINUE") == 0) 26523675Speter exit(EEXIT); 2661558Srgrimes asked++; 2671558Srgrimes } 2681558Srgrimes } 2691558Srgrimes /* 2701558Srgrimes * allocate and initialize the necessary maps 2711558Srgrimes */ 2721558Srgrimes bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(short)); 2731558Srgrimes blockmap = calloc((unsigned)bmapsize, sizeof (char)); 2741558Srgrimes if (blockmap == NULL) { 2751558Srgrimes printf("cannot alloc %u bytes for blockmap\n", 2761558Srgrimes (unsigned)bmapsize); 2771558Srgrimes goto badsb; 2781558Srgrimes } 2791558Srgrimes statemap = calloc((unsigned)(maxino + 1), sizeof(char)); 2801558Srgrimes if (statemap == NULL) { 2811558Srgrimes printf("cannot alloc %u bytes for statemap\n", 2821558Srgrimes (unsigned)(maxino + 1)); 2831558Srgrimes goto badsb; 2841558Srgrimes } 2851558Srgrimes typemap = calloc((unsigned)(maxino + 1), sizeof(char)); 2861558Srgrimes if (typemap == NULL) { 2871558Srgrimes printf("cannot alloc %u bytes for typemap\n", 2881558Srgrimes (unsigned)(maxino + 1)); 2891558Srgrimes goto badsb; 2901558Srgrimes } 2911558Srgrimes lncntp = (short *)calloc((unsigned)(maxino + 1), sizeof(short)); 2921558Srgrimes if (lncntp == NULL) { 2938871Srgrimes printf("cannot alloc %u bytes for lncntp\n", 2941558Srgrimes (unsigned)(maxino + 1) * sizeof(short)); 2951558Srgrimes goto badsb; 2961558Srgrimes } 2971558Srgrimes numdirs = sblock.fs_cstotal.cs_ndir; 2981558Srgrimes inplast = 0; 2991558Srgrimes listmax = numdirs + 10; 3001558Srgrimes inpsort = (struct inoinfo **)calloc((unsigned)listmax, 3011558Srgrimes sizeof(struct inoinfo *)); 3021558Srgrimes inphead = (struct inoinfo **)calloc((unsigned)numdirs, 3031558Srgrimes sizeof(struct inoinfo *)); 3041558Srgrimes if (inpsort == NULL || inphead == NULL) { 3058871Srgrimes printf("cannot alloc %u bytes for inphead\n", 3061558Srgrimes (unsigned)numdirs * sizeof(struct inoinfo *)); 3071558Srgrimes goto badsb; 3081558Srgrimes } 3091558Srgrimes bufinit(); 3101558Srgrimes return (1); 3111558Srgrimes 3121558Srgrimesbadsb: 31323675Speter ckfini(0); 3141558Srgrimes return (0); 3151558Srgrimes} 3161558Srgrimes 3171558Srgrimes/* 3181558Srgrimes * Read in the super block and its summary info. 3191558Srgrimes */ 3207585Sbdestatic int 3211558Srgrimesreadsb(listerr) 3221558Srgrimes int listerr; 3231558Srgrimes{ 32423675Speter ufs_daddr_t super = bflag ? bflag : SBOFF / dev_bsize; 3251558Srgrimes 3261558Srgrimes if (bread(fsreadfd, (char *)&sblock, super, (long)SBSIZE) != 0) 3271558Srgrimes return (0); 3281558Srgrimes sblk.b_bno = super; 3291558Srgrimes sblk.b_size = SBSIZE; 3301558Srgrimes /* 3311558Srgrimes * run a few consistency checks of the super block 3321558Srgrimes */ 3331558Srgrimes if (sblock.fs_magic != FS_MAGIC) 3341558Srgrimes { badsb(listerr, "MAGIC NUMBER WRONG"); return (0); } 3351558Srgrimes if (sblock.fs_ncg < 1) 3361558Srgrimes { badsb(listerr, "NCG OUT OF RANGE"); return (0); } 3371558Srgrimes if (sblock.fs_cpg < 1) 3381558Srgrimes { badsb(listerr, "CPG OUT OF RANGE"); return (0); } 3391558Srgrimes if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl || 3401558Srgrimes (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl) 3411558Srgrimes { badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); } 3421558Srgrimes if (sblock.fs_sbsize > SBSIZE) 3431558Srgrimes { badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); } 3441558Srgrimes /* 3451558Srgrimes * Compute block size that the filesystem is based on, 3461558Srgrimes * according to fsbtodb, and adjust superblock block number 3471558Srgrimes * so we can tell if this is an alternate later. 3481558Srgrimes */ 3491558Srgrimes super *= dev_bsize; 3501558Srgrimes dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1); 3511558Srgrimes sblk.b_bno = super / dev_bsize; 3521558Srgrimes if (bflag) { 3531558Srgrimes havesb = 1; 3541558Srgrimes return (1); 3551558Srgrimes } 3561558Srgrimes /* 3571558Srgrimes * Set all possible fields that could differ, then do check 3581558Srgrimes * of whole super block against an alternate super block. 3591558Srgrimes * When an alternate super-block is specified this check is skipped. 3601558Srgrimes */ 3611558Srgrimes getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize); 3621558Srgrimes if (asblk.b_errs) 3631558Srgrimes return (0); 36423675Speter altsblock.fs_firstfield = sblock.fs_firstfield; 36523675Speter altsblock.fs_unused_1 = sblock.fs_unused_1; 3661558Srgrimes altsblock.fs_time = sblock.fs_time; 3671558Srgrimes altsblock.fs_cstotal = sblock.fs_cstotal; 3681558Srgrimes altsblock.fs_cgrotor = sblock.fs_cgrotor; 3691558Srgrimes altsblock.fs_fmod = sblock.fs_fmod; 3701558Srgrimes altsblock.fs_clean = sblock.fs_clean; 3711558Srgrimes altsblock.fs_ronly = sblock.fs_ronly; 3721558Srgrimes altsblock.fs_flags = sblock.fs_flags; 3731558Srgrimes altsblock.fs_maxcontig = sblock.fs_maxcontig; 3741558Srgrimes altsblock.fs_minfree = sblock.fs_minfree; 3751558Srgrimes altsblock.fs_optim = sblock.fs_optim; 3761558Srgrimes altsblock.fs_rotdelay = sblock.fs_rotdelay; 3771558Srgrimes altsblock.fs_maxbpg = sblock.fs_maxbpg; 37823675Speter memmove(altsblock.fs_csp, sblock.fs_csp, sizeof sblock.fs_csp); 37923675Speter altsblock.fs_maxcluster = sblock.fs_maxcluster; 38023675Speter memmove(altsblock.fs_fsmnt, sblock.fs_fsmnt, sizeof sblock.fs_fsmnt); 38123675Speter memmove(altsblock.fs_sparecon, 38223675Speter sblock.fs_sparecon, sizeof sblock.fs_sparecon); 3831558Srgrimes /* 3841558Srgrimes * The following should not have to be copied. 3851558Srgrimes */ 3861558Srgrimes altsblock.fs_fsbtodb = sblock.fs_fsbtodb; 3871558Srgrimes altsblock.fs_interleave = sblock.fs_interleave; 3881558Srgrimes altsblock.fs_npsect = sblock.fs_npsect; 3891558Srgrimes altsblock.fs_nrpos = sblock.fs_nrpos; 39023675Speter altsblock.fs_state = sblock.fs_state; 3911558Srgrimes altsblock.fs_qbmask = sblock.fs_qbmask; 3921558Srgrimes altsblock.fs_qfmask = sblock.fs_qfmask; 3931558Srgrimes altsblock.fs_state = sblock.fs_state; 3941558Srgrimes altsblock.fs_maxfilesize = sblock.fs_maxfilesize; 39523675Speter if (memcmp(&sblock, &altsblock, (int)sblock.fs_sbsize)) { 39623675Speter if (debug) { 39723675Speter long *nlp, *olp, *endlp; 39823675Speter 39923675Speter printf("superblock mismatches\n"); 40023675Speter nlp = (long *)&altsblock; 40123675Speter olp = (long *)&sblock; 40223675Speter endlp = olp + (sblock.fs_sbsize / sizeof *olp); 40323675Speter for ( ; olp < endlp; olp++, nlp++) { 40423675Speter if (*olp == *nlp) 40523675Speter continue; 40623675Speter printf("offset %d, original %d, alternate %d\n", 40723675Speter olp - (long *)&sblock, *olp, *nlp); 40823675Speter } 40923675Speter } 4101558Srgrimes badsb(listerr, 4111558Srgrimes "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE"); 4121558Srgrimes return (0); 4131558Srgrimes } 4141558Srgrimes havesb = 1; 4151558Srgrimes return (1); 4161558Srgrimes} 4171558Srgrimes 4187585Sbdestatic void 4191558Srgrimesbadsb(listerr, s) 4201558Srgrimes int listerr; 4211558Srgrimes char *s; 4221558Srgrimes{ 4231558Srgrimes 4241558Srgrimes if (!listerr) 4251558Srgrimes return; 4261558Srgrimes if (preen) 4271558Srgrimes printf("%s: ", cdevname); 4281558Srgrimes pfatal("BAD SUPER BLOCK: %s\n", s); 4291558Srgrimes} 4301558Srgrimes 4311558Srgrimes/* 4321558Srgrimes * Calculate a prototype superblock based on information in the disk label. 4331558Srgrimes * When done the cgsblock macro can be calculated and the fs_ncg field 4341558Srgrimes * can be used. Do NOT attempt to use other macros without verifying that 4351558Srgrimes * their needed information is available! 4361558Srgrimes */ 43723675Speterstatic int 4381558Srgrimescalcsb(dev, devfd, fs) 4391558Srgrimes char *dev; 4401558Srgrimes int devfd; 4411558Srgrimes register struct fs *fs; 4421558Srgrimes{ 4431558Srgrimes register struct disklabel *lp; 4441558Srgrimes register struct partition *pp; 4451558Srgrimes register char *cp; 4461558Srgrimes int i; 4471558Srgrimes 44823675Speter cp = strchr(dev, '\0') - 1; 4497585Sbde if (cp == (char *)-1 || ((*cp < 'a' || *cp > 'h') && !isdigit(*cp))) { 4501558Srgrimes pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev); 4511558Srgrimes return (0); 4521558Srgrimes } 4531558Srgrimes lp = getdisklabel(dev, devfd); 4541558Srgrimes if (isdigit(*cp)) 4551558Srgrimes pp = &lp->d_partitions[0]; 4561558Srgrimes else 4571558Srgrimes pp = &lp->d_partitions[*cp - 'a']; 4581558Srgrimes if (pp->p_fstype != FS_BSDFFS) { 4591558Srgrimes pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n", 4601558Srgrimes dev, pp->p_fstype < FSMAXTYPES ? 4611558Srgrimes fstypenames[pp->p_fstype] : "unknown"); 4621558Srgrimes return (0); 4631558Srgrimes } 46423675Speter memset(fs, 0, sizeof(struct fs)); 4651558Srgrimes fs->fs_fsize = pp->p_fsize; 4661558Srgrimes fs->fs_frag = pp->p_frag; 4671558Srgrimes fs->fs_cpg = pp->p_cpg; 4681558Srgrimes fs->fs_size = pp->p_size; 4691558Srgrimes fs->fs_ntrak = lp->d_ntracks; 4701558Srgrimes fs->fs_nsect = lp->d_nsectors; 4711558Srgrimes fs->fs_spc = lp->d_secpercyl; 4721558Srgrimes fs->fs_nspf = fs->fs_fsize / lp->d_secsize; 4731558Srgrimes fs->fs_sblkno = roundup( 4741558Srgrimes howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize), 4751558Srgrimes fs->fs_frag); 4761558Srgrimes fs->fs_cgmask = 0xffffffff; 4771558Srgrimes for (i = fs->fs_ntrak; i > 1; i >>= 1) 4781558Srgrimes fs->fs_cgmask <<= 1; 4791558Srgrimes if (!POWEROF2(fs->fs_ntrak)) 4801558Srgrimes fs->fs_cgmask <<= 1; 4811558Srgrimes fs->fs_cgoffset = roundup( 4821558Srgrimes howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag); 4831558Srgrimes fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs); 4841558Srgrimes fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg); 4851558Srgrimes for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1) 4861558Srgrimes fs->fs_fsbtodb++; 4871558Srgrimes dev_bsize = lp->d_secsize; 4881558Srgrimes return (1); 4891558Srgrimes} 4901558Srgrimes 49123675Speterstatic struct disklabel * 4921558Srgrimesgetdisklabel(s, fd) 4931558Srgrimes char *s; 4941558Srgrimes int fd; 4951558Srgrimes{ 4961558Srgrimes static struct disklabel lab; 4971558Srgrimes 4981558Srgrimes if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) { 4991558Srgrimes if (s == NULL) 5001558Srgrimes return ((struct disklabel *)NULL); 5011558Srgrimes pwarn("ioctl (GCINFO): %s\n", strerror(errno)); 50223675Speter errx(EEXIT, "%s: can't read disk label", s); 5031558Srgrimes } 5041558Srgrimes return (&lab); 5051558Srgrimes} 506