1331722Seadler/* 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 * 4. Neither the name of the University nor the names of its contributors 141558Srgrimes * may be used to endorse or promote products derived from this software 151558Srgrimes * without specific prior written permission. 161558Srgrimes * 171558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201558Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271558Srgrimes * SUCH DAMAGE. 281558Srgrimes */ 291558Srgrimes 30114589Sobrien#if 0 311558Srgrimes#ifndef lint 3223675Speterstatic const char sccsid[] = "@(#)setup.c 8.10 (Berkeley) 5/9/95"; 33114589Sobrien#endif /* not lint */ 3441477Sjulian#endif 35114589Sobrien#include <sys/cdefs.h> 36114589Sobrien__FBSDID("$FreeBSD: stable/11/sbin/fsck_ffs/setup.c 359754 2020-04-09 20:38:36Z kevans $"); 371558Srgrimes 381558Srgrimes#include <sys/param.h> 39356905Seugen#include <sys/disk.h> 401558Srgrimes#include <sys/stat.h> 41101994Sbmilekic#define FSTYPENAMES 421558Srgrimes#include <sys/disklabel.h> 431558Srgrimes#include <sys/file.h> 4486514Siedowse#include <sys/sysctl.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> 52103949Smike#include <limits.h> 53101037Smux#include <stdint.h> 541558Srgrimes#include <string.h> 5523675Speter 561558Srgrimes#include "fsck.h" 571558Srgrimes 58359754Skevansstruct inoinfo **inphead, **inpsort; 59359754Skevans 601558Srgrimesstruct bufarea asblk; 611558Srgrimes#define altsblock (*asblk.b_un.b_fs) 621558Srgrimes#define POWEROF2(num) (((num) & ((num) - 1)) == 0) 631558Srgrimes 64322806Smckusickstatic int calcsb(char *dev, int devfd, struct fs *fs); 65322806Smckusickstatic void saverecovery(int readfd, int writefd); 66322806Smckusickstatic int chkrecovery(int devfd); 671558Srgrimes 6823675Speter/* 6923675Speter * Read in a superblock finding an alternate if necessary. 70102231Strhodes * Return 1 if successful, 0 if unsuccessful, -1 if file system 71187931Sobrien * is already clean (ckclean and preen mode only). 7223675Speter */ 737585Sbdeint 7492839Simpsetup(char *dev) 751558Srgrimes{ 7686514Siedowse long cg, asked, i, j; 7775557Smckusick long bmapsize; 781558Srgrimes struct stat statb; 791558Srgrimes struct fs proto; 8086514Siedowse size_t size; 811558Srgrimes 821558Srgrimes havesb = 0; 831558Srgrimes fswritefd = -1; 8462668Smckusick cursnapshot = 0; 851558Srgrimes if (stat(dev, &statb) < 0) { 861558Srgrimes printf("Can't stat %s: %s\n", dev, strerror(errno)); 8774556Smckusick if (bkgrdflag) { 8874556Smckusick unlink(snapname); 8974556Smckusick bkgrdflag = 0; 9074556Smckusick } 911558Srgrimes return (0); 921558Srgrimes } 9353781Sphk if ((statb.st_mode & S_IFMT) != S_IFCHR && 9453781Sphk (statb.st_mode & S_IFMT) != S_IFBLK) { 9574556Smckusick if (bkgrdflag != 0 && (statb.st_flags & SF_SNAPSHOT) == 0) { 9674556Smckusick unlink(snapname); 9774556Smckusick printf("background fsck lacks a snapshot\n"); 9874556Smckusick exit(EEXIT); 9974556Smckusick } 10074556Smckusick if ((statb.st_flags & SF_SNAPSHOT) != 0 && cvtlevel == 0) { 10162668Smckusick cursnapshot = statb.st_ino; 10262668Smckusick } else { 10374556Smckusick if (cvtlevel == 0 || 10474556Smckusick (statb.st_flags & SF_SNAPSHOT) == 0) { 10574556Smckusick if (preen && bkgrdflag) { 10674556Smckusick unlink(snapname); 10774556Smckusick bkgrdflag = 0; 10874556Smckusick } 10974556Smckusick pfatal("%s is not a disk device", dev); 11074556Smckusick if (reply("CONTINUE") == 0) { 11174556Smckusick if (bkgrdflag) { 11274556Smckusick unlink(snapname); 11374556Smckusick bkgrdflag = 0; 11474556Smckusick } 11574556Smckusick return (0); 11674556Smckusick } 11774556Smckusick } else { 11874556Smckusick if (bkgrdflag) { 11974556Smckusick unlink(snapname); 12074556Smckusick bkgrdflag = 0; 12174556Smckusick } 12274556Smckusick pfatal("cannot convert a snapshot"); 12374556Smckusick exit(EEXIT); 12474556Smckusick } 12562668Smckusick } 1261558Srgrimes } 1271558Srgrimes if ((fsreadfd = open(dev, O_RDONLY)) < 0) { 12874556Smckusick if (bkgrdflag) { 12974556Smckusick unlink(snapname); 13074556Smckusick bkgrdflag = 0; 13174556Smckusick } 1321558Srgrimes printf("Can't open %s: %s\n", dev, strerror(errno)); 1331558Srgrimes return (0); 1341558Srgrimes } 13574556Smckusick if (bkgrdflag) { 13674556Smckusick unlink(snapname); 13774556Smckusick size = MIBSIZE; 13874556Smckusick if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0|| 13974556Smckusick sysctlnametomib("vfs.ffs.adjblkcnt", adjblkcnt, &size) < 0|| 140344861Smckusick sysctlnametomib("vfs.ffs.setsize", setsize, &size) < 0 || 14174556Smckusick sysctlnametomib("vfs.ffs.freefiles", freefiles, &size) < 0|| 14274556Smckusick sysctlnametomib("vfs.ffs.freedirs", freedirs, &size) < 0 || 14374556Smckusick sysctlnametomib("vfs.ffs.freeblks", freeblks, &size) < 0) { 14474556Smckusick pfatal("kernel lacks background fsck support\n"); 14574556Smckusick exit(EEXIT); 14674556Smckusick } 147143235Sdelphij /* 148143235Sdelphij * When kernel is lack of runtime bgfsck superblock summary 149143235Sdelphij * adjustment functionality, it does not mean we can not 150143235Sdelphij * continue, as old kernels will recompute the summary at 151143235Sdelphij * mount time. However, it will be an unexpected softupdates 152143235Sdelphij * inconsistency if it turns out that the summary is still 153143235Sdelphij * incorrect. Set a flag so subsequent operation can know 154143235Sdelphij * this. 155143235Sdelphij */ 156143235Sdelphij bkgrdsumadj = 1; 157143235Sdelphij if (sysctlnametomib("vfs.ffs.adjndir", adjndir, &size) < 0 || 158143235Sdelphij sysctlnametomib("vfs.ffs.adjnbfree", adjnbfree, &size) < 0 || 159143235Sdelphij sysctlnametomib("vfs.ffs.adjnifree", adjnifree, &size) < 0 || 160143235Sdelphij sysctlnametomib("vfs.ffs.adjnffree", adjnffree, &size) < 0 || 161143235Sdelphij sysctlnametomib("vfs.ffs.adjnumclusters", adjnumclusters, &size) < 0) { 162143235Sdelphij bkgrdsumadj = 0; 163143235Sdelphij pwarn("kernel lacks runtime superblock summary adjustment support"); 164143235Sdelphij } 16574556Smckusick cmd.version = FFS_CMD_VERSION; 16674556Smckusick cmd.handle = fsreadfd; 16774556Smckusick fswritefd = -1; 16874556Smckusick } 1691558Srgrimes if (preen == 0) 1701558Srgrimes printf("** %s", dev); 17174556Smckusick if (bkgrdflag == 0 && 17274556Smckusick (nflag || (fswritefd = open(dev, O_WRONLY)) < 0)) { 1731558Srgrimes fswritefd = -1; 1741558Srgrimes if (preen) 1751558Srgrimes pfatal("NO WRITE ACCESS"); 1761558Srgrimes printf(" (NO WRITE)"); 1771558Srgrimes } 1781558Srgrimes if (preen == 0) 1791558Srgrimes printf("\n"); 1801558Srgrimes /* 1811558Srgrimes * Read in the superblock, looking for alternates if necessary 1821558Srgrimes */ 1831558Srgrimes if (readsb(1) == 0) { 18423675Speter skipclean = 0; 185322806Smckusick if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0) 1861558Srgrimes return(0); 1871558Srgrimes if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0) 1881558Srgrimes return (0); 1891558Srgrimes for (cg = 0; cg < proto.fs_ncg; cg++) { 1901558Srgrimes bflag = fsbtodb(&proto, cgsblock(&proto, cg)); 1911558Srgrimes if (readsb(0) != 0) 1921558Srgrimes break; 1931558Srgrimes } 1941558Srgrimes if (cg >= proto.fs_ncg) { 1951558Srgrimes printf("%s %s\n%s %s\n%s %s\n", 1961558Srgrimes "SEARCH FOR ALTERNATE SUPER-BLOCK", 1971558Srgrimes "FAILED. YOU MUST USE THE", 1981558Srgrimes "-b OPTION TO FSCK TO SPECIFY THE", 1991558Srgrimes "LOCATION OF AN ALTERNATE", 2001558Srgrimes "SUPER-BLOCK TO SUPPLY NEEDED", 201231377Strasz "INFORMATION; SEE fsck_ffs(8)."); 2026280Sbde bflag = 0; 2031558Srgrimes return(0); 2041558Srgrimes } 205307535Smckusick pwarn("USING ALTERNATE SUPERBLOCK AT %jd\n", bflag); 2066280Sbde bflag = 0; 2071558Srgrimes } 208187931Sobrien if (skipclean && ckclean && sblock.fs_clean) { 209118302Sru pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n"); 21041474Sjulian return (-1); 21141474Sjulian } 2121558Srgrimes maxfsblock = sblock.fs_size; 2131558Srgrimes maxino = sblock.fs_ncg * sblock.fs_ipg; 2141558Srgrimes /* 2151558Srgrimes * Check and potentially fix certain fields in the super block. 2161558Srgrimes */ 2171558Srgrimes if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) { 2181558Srgrimes pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK"); 2191558Srgrimes if (reply("SET TO DEFAULT") == 1) { 2201558Srgrimes sblock.fs_optim = FS_OPTTIME; 2211558Srgrimes sbdirty(); 2221558Srgrimes } 2231558Srgrimes } 2241558Srgrimes if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) { 2251558Srgrimes pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK", 2261558Srgrimes sblock.fs_minfree); 2271558Srgrimes if (reply("SET TO DEFAULT") == 1) { 2281558Srgrimes sblock.fs_minfree = 10; 2291558Srgrimes sbdirty(); 2301558Srgrimes } 2311558Srgrimes } 23298542Smckusick if (sblock.fs_magic == FS_UFS1_MAGIC && 23398542Smckusick sblock.fs_old_inodefmt < FS_44INODEFMT) { 234102231Strhodes pwarn("Format of file system is too old.\n"); 23596483Sphk pwarn("Must update to modern format using a version of fsck\n"); 23696483Sphk pfatal("from before 2002 with the command ``fsck -c 2''\n"); 23796483Sphk exit(EEXIT); 2381558Srgrimes } 23923675Speter if (asblk.b_dirty && !bflag) { 24023675Speter memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize); 2411558Srgrimes flush(fswritefd, &asblk); 2421558Srgrimes } 243322806Smckusick if (preen == 0 && yflag == 0 && sblock.fs_magic == FS_UFS2_MAGIC && 244322806Smckusick fswritefd != -1 && chkrecovery(fsreadfd) == 0 && 245322806Smckusick reply("SAVE DATA TO FIND ALTERNATE SUPERBLOCKS") != 0) 246322806Smckusick saverecovery(fsreadfd, fswritefd); 2471558Srgrimes /* 2481558Srgrimes * read in the summary info. 2491558Srgrimes */ 2501558Srgrimes asked = 0; 251248658Smckusick sblock.fs_csp = Calloc(1, sblock.fs_cssize); 252125036Scperciva if (sblock.fs_csp == NULL) { 253125036Scperciva printf("cannot alloc %u bytes for cg summary info\n", 254125036Scperciva (unsigned)sblock.fs_cssize); 255125036Scperciva goto badsb; 256125036Scperciva } 2571558Srgrimes for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) { 258298907Saraujo size = MIN(sblock.fs_cssize - i, sblock.fs_bsize); 259247212Smckusick readcnt[sblk.b_type]++; 260163845Spjd if (blread(fsreadfd, (char *)sblock.fs_csp + i, 2611558Srgrimes fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag), 2621558Srgrimes size) != 0 && !asked) { 2631558Srgrimes pfatal("BAD SUMMARY INFORMATION"); 26434266Sjulian if (reply("CONTINUE") == 0) { 26534266Sjulian ckfini(0); 26623675Speter exit(EEXIT); 26734266Sjulian } 2681558Srgrimes asked++; 2691558Srgrimes } 2701558Srgrimes } 2711558Srgrimes /* 2721558Srgrimes * allocate and initialize the necessary maps 2731558Srgrimes */ 274103949Smike bmapsize = roundup(howmany(maxfsblock, CHAR_BIT), sizeof(short)); 275248658Smckusick blockmap = Calloc((unsigned)bmapsize, sizeof (char)); 2761558Srgrimes if (blockmap == NULL) { 2771558Srgrimes printf("cannot alloc %u bytes for blockmap\n", 2781558Srgrimes (unsigned)bmapsize); 2791558Srgrimes goto badsb; 2801558Srgrimes } 281317860Spfg inostathead = Calloc(sblock.fs_ncg, sizeof(struct inostatlist)); 28241474Sjulian if (inostathead == NULL) { 28341474Sjulian printf("cannot alloc %u bytes for inostathead\n", 28441474Sjulian (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg))); 2851558Srgrimes goto badsb; 2861558Srgrimes } 287107518Smckusick numdirs = MAX(sblock.fs_cstotal.cs_ndir, 128); 28857573Smckusick dirhash = numdirs; 2891558Srgrimes inplast = 0; 2901558Srgrimes listmax = numdirs + 10; 291317860Spfg inpsort = (struct inoinfo **)Calloc(listmax, sizeof(struct inoinfo *)); 292317860Spfg inphead = (struct inoinfo **)Calloc(numdirs, sizeof(struct inoinfo *)); 2931558Srgrimes if (inpsort == NULL || inphead == NULL) { 294101037Smux printf("cannot alloc %ju bytes for inphead\n", 295101037Smux (uintmax_t)numdirs * sizeof(struct inoinfo *)); 2961558Srgrimes goto badsb; 2971558Srgrimes } 2981558Srgrimes bufinit(); 29934266Sjulian if (sblock.fs_flags & FS_DOSOFTDEP) 30034266Sjulian usedsoftdep = 1; 30134266Sjulian else 30234266Sjulian usedsoftdep = 0; 3031558Srgrimes return (1); 3041558Srgrimes 3051558Srgrimesbadsb: 30623675Speter ckfini(0); 3071558Srgrimes return (0); 3081558Srgrimes} 3091558Srgrimes 3101558Srgrimes/* 31198542Smckusick * Possible superblock locations ordered from most to least likely. 31298542Smckusick */ 31398542Smckusickstatic int sblock_try[] = SBLOCKSEARCH; 31498542Smckusick 315122784Swes#define BAD_MAGIC_MSG \ 316122784Swes"The previous newfs operation on this volume did not complete.\n" \ 317122784Swes"You must complete newfs before mounting this volume.\n" 318122784Swes 31998542Smckusick/* 3201558Srgrimes * Read in the super block and its summary info. 3211558Srgrimes */ 32275557Smckusickint 32392839Simpreadsb(int listerr) 3241558Srgrimes{ 32598542Smckusick ufs2_daddr_t super; 326322806Smckusick int i, bad; 3271558Srgrimes 32898542Smckusick if (bflag) { 32998542Smckusick super = bflag; 330247212Smckusick readcnt[sblk.b_type]++; 331163845Spjd if ((blread(fsreadfd, (char *)&sblock, super, (long)SBLOCKSIZE))) 33298542Smckusick return (0); 333134011Sjhb if (sblock.fs_magic == FS_BAD_MAGIC) { 334122784Swes fprintf(stderr, BAD_MAGIC_MSG); 335122784Swes exit(11); 336122784Swes } 337107518Smckusick if (sblock.fs_magic != FS_UFS1_MAGIC && 338107518Smckusick sblock.fs_magic != FS_UFS2_MAGIC) { 339307535Smckusick fprintf(stderr, "%jd is not a file system superblock\n", 340107518Smckusick bflag); 341107518Smckusick return (0); 342107518Smckusick } 34398542Smckusick } else { 34498542Smckusick for (i = 0; sblock_try[i] != -1; i++) { 34598542Smckusick super = sblock_try[i] / dev_bsize; 346247212Smckusick readcnt[sblk.b_type]++; 347163845Spjd if ((blread(fsreadfd, (char *)&sblock, super, 34898542Smckusick (long)SBLOCKSIZE))) 34998542Smckusick return (0); 350134011Sjhb if (sblock.fs_magic == FS_BAD_MAGIC) { 351122784Swes fprintf(stderr, BAD_MAGIC_MSG); 352122784Swes exit(11); 353122784Swes } 35498542Smckusick if ((sblock.fs_magic == FS_UFS1_MAGIC || 35598542Smckusick (sblock.fs_magic == FS_UFS2_MAGIC && 356107294Smckusick sblock.fs_sblockloc == sblock_try[i])) && 35798542Smckusick sblock.fs_ncg >= 1 && 358104745Smckusick sblock.fs_bsize >= MINBSIZE && 359176573Sdelphij sblock.fs_sbsize >= roundup(sizeof(struct fs), dev_bsize)) 36098542Smckusick break; 36198542Smckusick } 36298542Smckusick if (sblock_try[i] == -1) { 363102231Strhodes fprintf(stderr, "Cannot find file system superblock\n"); 36498542Smckusick return (0); 36598542Smckusick } 36698542Smckusick } 3671558Srgrimes /* 368102231Strhodes * Compute block size that the file system is based on, 3691558Srgrimes * according to fsbtodb, and adjust superblock block number 3701558Srgrimes * so we can tell if this is an alternate later. 3711558Srgrimes */ 3721558Srgrimes super *= dev_bsize; 3731558Srgrimes dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1); 3741558Srgrimes sblk.b_bno = super / dev_bsize; 37598542Smckusick sblk.b_size = SBLOCKSIZE; 3761558Srgrimes /* 37794031Smckusick * Compare all fields that should not differ in alternate super block. 3781558Srgrimes * When an alternate super-block is specified this check is skipped. 3791558Srgrimes */ 380322806Smckusick if (bflag) 381322806Smckusick goto out; 3821558Srgrimes getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize); 3831558Srgrimes if (asblk.b_errs) 3841558Srgrimes return (0); 385322806Smckusick bad = 0; 386322806Smckusick#define CHK(x, y) \ 387322806Smckusick if (altsblock.x != sblock.x) { \ 388322806Smckusick bad++; \ 389322806Smckusick if (listerr && debug) \ 390322806Smckusick printf("SUPER BLOCK VS ALTERNATE MISMATCH %s: " y " vs " y "\n", \ 391322806Smckusick #x, (intmax_t)sblock.x, (intmax_t)altsblock.x); \ 3921558Srgrimes } 393322806Smckusick CHK(fs_sblkno, "%jd"); 394322806Smckusick CHK(fs_cblkno, "%jd"); 395322806Smckusick CHK(fs_iblkno, "%jd"); 396322806Smckusick CHK(fs_dblkno, "%jd"); 397322806Smckusick CHK(fs_ncg, "%jd"); 398322806Smckusick CHK(fs_bsize, "%jd"); 399322806Smckusick CHK(fs_fsize, "%jd"); 400322806Smckusick CHK(fs_frag, "%jd"); 401322806Smckusick CHK(fs_bmask, "%#jx"); 402322806Smckusick CHK(fs_fmask, "%#jx"); 403322806Smckusick CHK(fs_bshift, "%jd"); 404322806Smckusick CHK(fs_fshift, "%jd"); 405322806Smckusick CHK(fs_fragshift, "%jd"); 406322806Smckusick CHK(fs_fsbtodb, "%jd"); 407322806Smckusick CHK(fs_sbsize, "%jd"); 408322806Smckusick CHK(fs_nindir, "%jd"); 409322806Smckusick CHK(fs_inopb, "%jd"); 410322806Smckusick CHK(fs_cssize, "%jd"); 411322806Smckusick CHK(fs_ipg, "%jd"); 412322806Smckusick CHK(fs_fpg, "%jd"); 413322806Smckusick CHK(fs_magic, "%#jx"); 414322806Smckusick#undef CHK 415322806Smckusick if (bad) { 416322806Smckusick if (listerr == 0) 417322806Smckusick return (0); 418322806Smckusick if (preen) 419322806Smckusick printf("%s: ", cdevname); 420322806Smckusick printf( 421322806Smckusick "VALUES IN SUPER BLOCK LSB=%jd DISAGREE WITH THOSE IN\n" 422322806Smckusick "LAST ALTERNATE LSB=%jd\n", 423322806Smckusick sblk.b_bno, asblk.b_bno); 424322806Smckusick if (reply("IGNORE ALTERNATE SUPER BLOCK") == 0) 425322806Smckusick return (0); 426322806Smckusick } 427107518Smckusickout: 42898542Smckusick /* 42998542Smckusick * If not yet done, update UFS1 superblock with new wider fields. 43098542Smckusick */ 43198542Smckusick if (sblock.fs_magic == FS_UFS1_MAGIC && 43298542Smckusick sblock.fs_maxbsize != sblock.fs_bsize) { 43398542Smckusick sblock.fs_maxbsize = sblock.fs_bsize; 43498542Smckusick sblock.fs_time = sblock.fs_old_time; 43598542Smckusick sblock.fs_size = sblock.fs_old_size; 43698542Smckusick sblock.fs_dsize = sblock.fs_old_dsize; 43798542Smckusick sblock.fs_csaddr = sblock.fs_old_csaddr; 43898542Smckusick sblock.fs_cstotal.cs_ndir = sblock.fs_old_cstotal.cs_ndir; 43998542Smckusick sblock.fs_cstotal.cs_nbfree = sblock.fs_old_cstotal.cs_nbfree; 44098542Smckusick sblock.fs_cstotal.cs_nifree = sblock.fs_old_cstotal.cs_nifree; 44198542Smckusick sblock.fs_cstotal.cs_nffree = sblock.fs_old_cstotal.cs_nffree; 44298542Smckusick } 4431558Srgrimes havesb = 1; 4441558Srgrimes return (1); 4451558Srgrimes} 4461558Srgrimes 44775884Siedowsevoid 44892839Simpsblock_init(void) 44975557Smckusick{ 45075557Smckusick 45175557Smckusick fswritefd = -1; 45275557Smckusick fsmodified = 0; 45375557Smckusick lfdir = 0; 454247212Smckusick initbarea(&sblk, BT_SUPERBLK); 455247212Smckusick initbarea(&asblk, BT_SUPERBLK); 456248658Smckusick sblk.b_un.b_buf = Malloc(SBLOCKSIZE); 457248658Smckusick asblk.b_un.b_buf = Malloc(SBLOCKSIZE); 45875557Smckusick if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL) 45975557Smckusick errx(EEXIT, "cannot allocate space for superblock"); 460276737Simp dev_bsize = secsize = DEV_BSIZE; 46175557Smckusick} 462322806Smckusick 463322806Smckusick/* 464322806Smckusick * Calculate a prototype superblock based on information in the boot area. 465322806Smckusick * When done the cgsblock macro can be calculated and the fs_ncg field 466322806Smckusick * can be used. Do NOT attempt to use other macros without verifying that 467322806Smckusick * their needed information is available! 468322806Smckusick */ 469322806Smckusickstatic int 470322806Smckusickcalcsb(char *dev, int devfd, struct fs *fs) 471322806Smckusick{ 472356905Seugen struct fsrecovery *fsr; 473356905Seugen char *fsrbuf; 474356905Seugen u_int secsize; 475322806Smckusick 476322806Smckusick /* 477322806Smckusick * We need fragments-per-group and the partition-size. 478322806Smckusick * 479322806Smckusick * Newfs stores these details at the end of the boot block area 480322806Smckusick * at the start of the filesystem partition. If they have been 481322806Smckusick * overwritten by a boot block, we fail. But usually they are 482322806Smckusick * there and we can use them. 483322806Smckusick */ 484356905Seugen if (ioctl(devfd, DIOCGSECTORSIZE, &secsize) == -1) 485322806Smckusick return (0); 486356905Seugen fsrbuf = Malloc(secsize); 487356905Seugen if (fsrbuf == NULL) 488356905Seugen errx(EEXIT, "calcsb: cannot allocate recovery buffer"); 489356905Seugen if (blread(devfd, fsrbuf, 490356905Seugen (SBLOCK_UFS2 - secsize) / dev_bsize, secsize) != 0) 491356905Seugen return (0); 492356905Seugen fsr = (struct fsrecovery *)&fsrbuf[secsize - sizeof *fsr]; 493356905Seugen if (fsr->fsr_magic != FS_UFS2_MAGIC) 494356905Seugen return (0); 495322806Smckusick memset(fs, 0, sizeof(struct fs)); 496356905Seugen fs->fs_fpg = fsr->fsr_fpg; 497356905Seugen fs->fs_fsbtodb = fsr->fsr_fsbtodb; 498356905Seugen fs->fs_sblkno = fsr->fsr_sblkno; 499356905Seugen fs->fs_magic = fsr->fsr_magic; 500356905Seugen fs->fs_ncg = fsr->fsr_ncg; 501356905Seugen free(fsrbuf); 502322806Smckusick return (1); 503322806Smckusick} 504322806Smckusick 505322806Smckusick/* 506322806Smckusick * Check to see if recovery information exists. 507356905Seugen * Return 1 if it exists or cannot be created. 508356905Seugen * Return 0 if it does not exist and can be created. 509322806Smckusick */ 510322806Smckusickstatic int 511322806Smckusickchkrecovery(int devfd) 512322806Smckusick{ 513356905Seugen struct fsrecovery *fsr; 514356905Seugen char *fsrbuf; 515356905Seugen u_int secsize; 516322806Smckusick 517356905Seugen /* 518356905Seugen * Could not determine if backup material exists, so do not 519356905Seugen * offer to create it. 520356905Seugen */ 521356905Seugen if (ioctl(devfd, DIOCGSECTORSIZE, &secsize) == -1 || 522356905Seugen (fsrbuf = Malloc(secsize)) == NULL || 523356905Seugen blread(devfd, fsrbuf, (SBLOCK_UFS2 - secsize) / dev_bsize, 524356905Seugen secsize) != 0) 525356905Seugen return (1); 526356905Seugen /* 527356905Seugen * Recovery material has already been created, so do not 528356905Seugen * need to create it again. 529356905Seugen */ 530356905Seugen fsr = (struct fsrecovery *)&fsrbuf[secsize - sizeof *fsr]; 531356905Seugen if (fsr->fsr_magic == FS_UFS2_MAGIC) { 532356905Seugen free(fsrbuf); 533356905Seugen return (1); 534356905Seugen } 535356905Seugen /* 536356905Seugen * Recovery material has not been created and can be if desired. 537356905Seugen */ 538356905Seugen free(fsrbuf); 539356905Seugen return (0); 540322806Smckusick} 541322806Smckusick 542322806Smckusick/* 543322806Smckusick * Read the last sector of the boot block, replace the last 544322806Smckusick * 20 bytes with the recovery information, then write it back. 545322806Smckusick * The recovery information only works for UFS2 filesystems. 546322806Smckusick */ 547322806Smckusickstatic void 548322806Smckusicksaverecovery(int readfd, int writefd) 549322806Smckusick{ 550356905Seugen struct fsrecovery *fsr; 551356905Seugen char *fsrbuf; 552356905Seugen u_int secsize; 553322806Smckusick 554322806Smckusick if (sblock.fs_magic != FS_UFS2_MAGIC || 555356905Seugen ioctl(readfd, DIOCGSECTORSIZE, &secsize) == -1 || 556356905Seugen (fsrbuf = Malloc(secsize)) == NULL || 557356905Seugen blread(readfd, fsrbuf, (SBLOCK_UFS2 - secsize) / dev_bsize, 558356905Seugen secsize) != 0) { 559356905Seugen printf("RECOVERY DATA COULD NOT BE CREATED\n"); 560322806Smckusick return; 561356905Seugen } 562356905Seugen fsr = (struct fsrecovery *)&fsrbuf[secsize - sizeof *fsr]; 563356905Seugen fsr->fsr_magic = sblock.fs_magic; 564356905Seugen fsr->fsr_fpg = sblock.fs_fpg; 565356905Seugen fsr->fsr_fsbtodb = sblock.fs_fsbtodb; 566356905Seugen fsr->fsr_sblkno = sblock.fs_sblkno; 567356905Seugen fsr->fsr_ncg = sblock.fs_ncg; 568356905Seugen blwrite(writefd, fsrbuf, (SBLOCK_UFS2 - secsize) / secsize, secsize); 569356905Seugen free(fsrbuf); 570322806Smckusick} 571