2001-01-05 05:02:57 +03:00
|
|
|
/* $NetBSD: setup.c,v 1.8 2001/01/05 02:02:58 lukem Exp $ */
|
1999-03-18 05:02:18 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1980, 1986, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* #define DKTYPENAMES */
|
|
|
|
#define FSTYPENAMES
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/disklabel.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
|
|
|
|
#include <ufs/ufs/dinode.h>
|
2000-05-23 05:48:52 +04:00
|
|
|
#include <sys/mount.h> /* XXX ufs/lfs/lfs.h should include this for
|
|
|
|
* us */
|
1999-03-18 05:02:18 +03:00
|
|
|
#include <ufs/lfs/lfs.h>
|
|
|
|
#include <ufs/lfs/lfs_extern.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "fsck.h"
|
|
|
|
#include "extern.h"
|
|
|
|
#include "fsutil.h"
|
|
|
|
|
2000-05-23 05:48:52 +04:00
|
|
|
struct bufarea asblk;
|
|
|
|
daddr_t *din_table;
|
|
|
|
SEGUSE *seg_table;
|
1999-03-18 05:02:18 +03:00
|
|
|
#define altsblock (*asblk.b_un.b_fs)
|
|
|
|
#define POWEROF2(num) (((num) & ((num) - 1)) == 0)
|
|
|
|
|
2000-05-23 05:48:52 +04:00
|
|
|
void badsb(int, char *);
|
|
|
|
int calcsb(const char *, int, struct lfs *);
|
|
|
|
static struct disklabel *getdisklabel(const char *, int);
|
|
|
|
static int readsb(int);
|
|
|
|
int lfs_maxino(void);
|
1999-03-18 05:02:18 +03:00
|
|
|
|
2000-05-23 05:48:52 +04:00
|
|
|
static daddr_t try_verify(struct lfs *, struct lfs *);
|
2000-05-16 08:55:58 +04:00
|
|
|
|
1999-03-18 05:02:18 +03:00
|
|
|
#ifdef DKTYPENAMES
|
2000-05-23 05:48:52 +04:00
|
|
|
int useless(void);
|
1999-03-18 05:02:18 +03:00
|
|
|
|
|
|
|
int
|
|
|
|
useless(void)
|
|
|
|
{
|
2000-05-23 05:48:52 +04:00
|
|
|
char **foo = (char **)dktypenames;
|
|
|
|
char **bar = (char **)fscknames;
|
1999-03-18 05:02:18 +03:00
|
|
|
|
2000-05-23 05:48:52 +04:00
|
|
|
return foo - bar;
|
1999-03-18 05:02:18 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-06-14 22:43:57 +04:00
|
|
|
static daddr_t
|
2000-05-23 05:48:52 +04:00
|
|
|
try_verify(struct lfs * osb, struct lfs * nsb)
|
2000-05-16 08:55:58 +04:00
|
|
|
{
|
2000-05-23 05:48:52 +04:00
|
|
|
daddr_t daddr;
|
|
|
|
SEGSUM *sp;
|
|
|
|
char summary[LFS_SUMMARY_SIZE];
|
|
|
|
int bc, flag;
|
|
|
|
|
2000-05-16 08:55:58 +04:00
|
|
|
daddr = osb->lfs_offset;
|
2000-05-23 05:48:52 +04:00
|
|
|
while (daddr != nsb->lfs_offset) {
|
2000-05-16 08:55:58 +04:00
|
|
|
flag = 0;
|
2000-05-23 05:48:52 +04:00
|
|
|
oncemore:
|
2000-05-16 08:55:58 +04:00
|
|
|
/* Read in summary block */
|
|
|
|
bread(fsreadfd, summary, daddr, LFS_SUMMARY_SIZE);
|
|
|
|
sp = (SEGSUM *)summary;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Could be a superblock instead of a segment summary.
|
|
|
|
* XXX should use gseguse, but right now we need to do more
|
|
|
|
* setup before we can...fix this
|
|
|
|
*/
|
2000-05-23 05:48:52 +04:00
|
|
|
if (sp->ss_magic != SS_MAGIC ||
|
|
|
|
sp->ss_sumsum != cksum(&sp->ss_datasum, LFS_SUMMARY_SIZE -
|
|
|
|
sizeof(sp->ss_sumsum))) {
|
|
|
|
if (flag == 0) {
|
|
|
|
daddr += LFS_SBPAD / dev_bsize;
|
2000-05-16 08:55:58 +04:00
|
|
|
goto oncemore;
|
|
|
|
}
|
|
|
|
return 0x0;
|
|
|
|
}
|
|
|
|
bc = check_summary(osb, sp, daddr);
|
2000-05-23 05:48:52 +04:00
|
|
|
if (bc == 0)
|
2000-05-16 08:55:58 +04:00
|
|
|
break;
|
|
|
|
daddr += (LFS_SUMMARY_SIZE + bc) / dev_bsize;
|
2000-05-23 05:48:52 +04:00
|
|
|
if (datosn(osb, daddr) != datosn(osb, daddr + (LFS_SUMMARY_SIZE + (1 << osb->lfs_bshift)) / dev_bsize)) {
|
2000-05-16 08:55:58 +04:00
|
|
|
daddr = ((SEGSUM *)summary)->ss_next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return daddr;
|
|
|
|
}
|
|
|
|
|
1999-03-18 05:02:18 +03:00
|
|
|
int
|
2000-05-23 05:48:52 +04:00
|
|
|
setup(const char *dev)
|
1999-03-18 05:02:18 +03:00
|
|
|
{
|
2000-05-23 05:48:52 +04:00
|
|
|
long bmapsize;
|
1999-03-18 05:02:18 +03:00
|
|
|
struct disklabel *lp;
|
|
|
|
#if 0
|
2000-05-23 05:48:52 +04:00
|
|
|
long i;
|
|
|
|
off_t sizepb;
|
1999-03-18 05:02:18 +03:00
|
|
|
#endif
|
2000-05-23 05:48:52 +04:00
|
|
|
struct stat statb;
|
|
|
|
daddr_t daddr;
|
|
|
|
struct lfs proto;
|
|
|
|
int doskipclean;
|
|
|
|
u_int64_t maxfilesize;
|
|
|
|
struct lfs *sb0, *sb1, *osb, *nsb;
|
|
|
|
struct dinode *idinode;
|
1999-03-18 05:02:18 +03:00
|
|
|
|
|
|
|
havesb = 0;
|
|
|
|
fswritefd = -1;
|
|
|
|
doskipclean = skipclean;
|
|
|
|
if (stat(dev, &statb) < 0) {
|
|
|
|
printf("Can't stat %s: %s\n", dev, strerror(errno));
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (!S_ISCHR(statb.st_mode)) {
|
|
|
|
pfatal("%s is not a character device", dev);
|
|
|
|
if (reply("CONTINUE") == 0)
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
|
|
|
|
printf("Can't open %s: %s\n", dev, strerror(errno));
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (preen == 0)
|
|
|
|
printf("** %s", dev);
|
|
|
|
if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
|
|
|
|
fswritefd = -1;
|
|
|
|
if (preen)
|
|
|
|
pfatal("NO WRITE ACCESS");
|
|
|
|
printf(" (NO WRITE)");
|
|
|
|
}
|
|
|
|
if (preen == 0)
|
|
|
|
printf("\n");
|
|
|
|
fsmodified = 0;
|
|
|
|
lfdir = 0;
|
|
|
|
initbarea(&sblk);
|
|
|
|
initbarea(&asblk);
|
|
|
|
sblk.b_un.b_buf = malloc(LFS_SBPAD);
|
|
|
|
asblk.b_un.b_buf = malloc(LFS_SBPAD);
|
|
|
|
if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
|
|
|
|
errexit("cannot allocate space for superblock\n");
|
2000-05-23 05:48:52 +04:00
|
|
|
if ((lp = getdisklabel((char *) NULL, fsreadfd)) != NULL)
|
1999-03-18 05:02:18 +03:00
|
|
|
dev_bsize = secsize = lp->d_secsize;
|
|
|
|
else
|
|
|
|
dev_bsize = secsize = DEV_BSIZE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read in the superblock, looking for alternates if necessary
|
|
|
|
*/
|
|
|
|
if (readsb(1) == 0) {
|
|
|
|
if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
|
2000-05-23 05:48:52 +04:00
|
|
|
return (0);
|
1999-03-18 05:02:18 +03:00
|
|
|
if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
|
|
|
|
return (0);
|
2000-05-23 05:48:52 +04:00
|
|
|
#if 0 /* XXX find the LFS way to do this */
|
1999-03-18 05:02:18 +03:00
|
|
|
for (cg = 0; cg < proto.lfs_ncg; cg++) {
|
|
|
|
bflag = fsbtodb(&proto, cgsblock(&proto, cg));
|
|
|
|
if (readsb(0) != 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (cg >= proto.lfs_ncg) {
|
|
|
|
printf("%s %s\n%s %s\n%s %s\n",
|
2000-05-23 05:48:52 +04:00
|
|
|
"SEARCH FOR ALTERNATE SUPER-BLOCK",
|
|
|
|
"FAILED. YOU MUST USE THE",
|
|
|
|
"-b OPTION TO FSCK_FFS TO SPECIFY THE",
|
|
|
|
"LOCATION OF AN ALTERNATE",
|
|
|
|
"SUPER-BLOCK TO SUPPLY NEEDED",
|
|
|
|
"INFORMATION; SEE fsck_ffs(8).");
|
|
|
|
return (0);
|
1999-03-18 05:02:18 +03:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
pwarn("XXX Can't look for alternate superblocks yet\n");
|
2000-05-23 05:48:52 +04:00
|
|
|
return (0);
|
1999-03-18 05:02:18 +03:00
|
|
|
#endif
|
|
|
|
doskipclean = 0;
|
|
|
|
pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
|
|
|
|
}
|
2000-05-16 08:55:58 +04:00
|
|
|
bufinit();
|
|
|
|
|
2000-06-14 22:43:57 +04:00
|
|
|
if (bflag == 0x0 && idaddr == 0x0) {
|
1999-03-18 05:02:18 +03:00
|
|
|
/*
|
|
|
|
* Even if that superblock read in properly, it may not
|
|
|
|
* be guaranteed to point to a complete checkpoint.
|
|
|
|
* Read in the second superblock too, and take whichever
|
|
|
|
* of the two is *less* recent. --ks
|
|
|
|
*/
|
|
|
|
sb0 = malloc(sizeof(*sb0));
|
2000-05-16 08:55:58 +04:00
|
|
|
sb1 = malloc(sizeof(*sb1));
|
2000-05-23 05:48:52 +04:00
|
|
|
memcpy(sb0, &sblock, sizeof(*sb0));
|
2000-05-16 08:55:58 +04:00
|
|
|
bflag = sblock.lfs_sboffs[1];
|
2000-05-23 05:48:52 +04:00
|
|
|
if (readsb(1) == 0) {
|
|
|
|
pwarn("COULDN'T READ ALT SUPERBLOCK AT BLK %d", bflag);
|
2000-05-16 08:55:58 +04:00
|
|
|
if (reply("ASSUME PRIMARY SUPERBLOCK IS GOOD") == 0) {
|
|
|
|
return (0);
|
2000-05-23 05:48:52 +04:00
|
|
|
} else {/* use primary as good */
|
|
|
|
memcpy(&sblock, sb0, sizeof(*sb0)); /* XXX cheating? */
|
2000-05-16 08:55:58 +04:00
|
|
|
}
|
|
|
|
} else {
|
2000-05-23 05:48:52 +04:00
|
|
|
memcpy(sb1, &sblock, sizeof(*sb1));
|
|
|
|
if (debug)
|
|
|
|
pwarn("sb0 %d, sb1 %d\n", sb0->lfs_tstamp, sblock.lfs_tstamp);
|
2000-05-16 08:55:58 +04:00
|
|
|
/*
|
|
|
|
* Verify the checkpoint of the newer superblock,
|
|
|
|
* if the timestamp of the two superblocks is
|
|
|
|
* different. XXX use lfs_offset instead, discover
|
|
|
|
* how to quickly discover "newness" based on that.
|
|
|
|
*/
|
2000-05-23 05:48:52 +04:00
|
|
|
if (sb0->lfs_tstamp != sb1->lfs_tstamp) {
|
|
|
|
if (sb0->lfs_tstamp > sb1->lfs_tstamp) {
|
2000-05-16 08:55:58 +04:00
|
|
|
osb = sb1;
|
|
|
|
nsb = sb0;
|
|
|
|
} else {
|
|
|
|
osb = sb0;
|
|
|
|
nsb = sb1;
|
1999-03-18 05:02:18 +03:00
|
|
|
}
|
2000-05-16 08:55:58 +04:00
|
|
|
daddr = try_verify(osb, nsb);
|
|
|
|
|
|
|
|
if (debug)
|
|
|
|
printf("done.\n");
|
|
|
|
if (daddr == nsb->lfs_offset) {
|
|
|
|
pwarn("Checkpoint verified, recovered %d seconds of data\n", nsb->lfs_tstamp - osb->lfs_tstamp);
|
2000-05-23 05:48:52 +04:00
|
|
|
memcpy(&sblock, nsb, sizeof(*nsb));
|
2000-05-16 08:55:58 +04:00
|
|
|
sbdirty();
|
1999-03-18 05:02:18 +03:00
|
|
|
} else {
|
2000-05-16 08:55:58 +04:00
|
|
|
pwarn("Checkpoint invalid, lost %d seconds of data\n", nsb->lfs_tstamp - osb->lfs_tstamp);
|
2000-05-23 05:48:52 +04:00
|
|
|
memcpy(&sblock, osb, sizeof(*osb));
|
1999-03-18 05:02:18 +03:00
|
|
|
}
|
|
|
|
}
|
2000-05-16 08:55:58 +04:00
|
|
|
}
|
1999-03-18 05:02:18 +03:00
|
|
|
free(sb0);
|
2000-05-16 08:55:58 +04:00
|
|
|
free(sb1);
|
1999-03-18 05:02:18 +03:00
|
|
|
}
|
2000-06-14 22:43:57 +04:00
|
|
|
if (idaddr == 0x0)
|
|
|
|
idaddr = sblock.lfs_idaddr;
|
2000-05-23 05:48:52 +04:00
|
|
|
if (debug) {
|
|
|
|
printf("dev_bsize = %lu\n", dev_bsize);
|
|
|
|
printf("lfs_bsize = %lu\n", (unsigned long)sblock.lfs_bsize);
|
|
|
|
printf("lfs_fsize = %lu\n", (unsigned long)sblock.lfs_fsize);
|
|
|
|
printf("lfs_frag = %lu\n", (unsigned long)sblock.lfs_frag);
|
|
|
|
printf("INOPB(fs) = %lu\n", (unsigned long) INOPB(&sblock));
|
|
|
|
/* printf("fsbtodb(fs,1) = %lu\n",fsbtodb(&sblock,1)); */
|
1999-03-18 05:02:18 +03:00
|
|
|
}
|
2000-05-23 05:48:52 +04:00
|
|
|
#if 0 /* FFS-specific fs-clean check */
|
1999-03-18 05:02:18 +03:00
|
|
|
if (debug)
|
|
|
|
printf("clean = %d\n", sblock.lfs_clean);
|
|
|
|
if (sblock.lfs_clean & FS_ISCLEAN) {
|
|
|
|
if (doskipclean) {
|
|
|
|
pwarn("%sile system is clean; not checking\n",
|
2000-05-23 05:48:52 +04:00
|
|
|
preen ? "f" : "** F");
|
1999-03-18 05:02:18 +03:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
if (!preen)
|
|
|
|
pwarn("** File system is already clean\n");
|
|
|
|
}
|
|
|
|
maxino = sblock.lfs_ncg * sblock.lfs_ipg;
|
|
|
|
#else
|
2000-05-23 05:48:52 +04:00
|
|
|
initbarea(&iblk);
|
|
|
|
iblk.b_un.b_buf = malloc(sblock.lfs_bsize);
|
2000-06-14 22:43:57 +04:00
|
|
|
if (bread(fsreadfd, (char *)iblk.b_un.b_buf, idaddr,
|
2000-05-23 05:48:52 +04:00
|
|
|
(long)sblock.lfs_bsize) != 0) {
|
2000-06-14 22:43:57 +04:00
|
|
|
printf("Couldn't read disk block %d\n", idaddr);
|
2000-05-23 05:48:52 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
idinode = lfs_difind(&sblock, sblock.lfs_ifile, &ifblock);
|
2000-05-16 08:55:58 +04:00
|
|
|
maxino = ((idinode->di_size
|
2000-05-23 05:48:52 +04:00
|
|
|
- (sblock.lfs_cleansz + sblock.lfs_segtabsz) * sblock.lfs_bsize)
|
2000-06-14 22:43:57 +04:00
|
|
|
/ sblock.lfs_bsize) * sblock.lfs_ifpb;
|
1999-03-18 05:02:18 +03:00
|
|
|
if (debug)
|
2000-05-23 05:48:52 +04:00
|
|
|
printf("maxino=%d\n", maxino);
|
|
|
|
din_table = (daddr_t *)malloc(maxino * sizeof(*din_table));
|
|
|
|
memset(din_table, 0, maxino * sizeof(*din_table));
|
|
|
|
seg_table = (SEGUSE *)malloc(sblock.lfs_nseg * sizeof(SEGUSE));
|
|
|
|
memset(seg_table, 0, sblock.lfs_nseg * sizeof(SEGUSE));
|
1999-03-18 05:02:18 +03:00
|
|
|
#endif
|
|
|
|
maxfsblock = sblock.lfs_size * (sblock.lfs_bsize / dev_bsize);
|
|
|
|
#if 0
|
|
|
|
sizepb = sblock.lfs_bsize;
|
|
|
|
maxfilesize = sblock.lfs_bsize * NDADDR - 1;
|
|
|
|
for (i = 0; i < NIADDR; i++) {
|
|
|
|
sizepb *= NINDIR(&sblock);
|
|
|
|
maxfilesize += sizepb;
|
|
|
|
}
|
2000-05-23 05:48:52 +04:00
|
|
|
maxfilesize++; /* XXX */
|
|
|
|
#else /* LFS way */
|
|
|
|
{
|
|
|
|
u_quad_t maxtable[] = {
|
|
|
|
/* 1 */ -1,
|
|
|
|
/* 2 */ -1,
|
|
|
|
/* 4 */ -1,
|
|
|
|
/* 8 */ -1,
|
|
|
|
/* 16 */ -1,
|
|
|
|
/* 32 */ -1,
|
|
|
|
/* 64 */ -1,
|
|
|
|
/* 128 */ -1,
|
|
|
|
/* 256 */ -1,
|
|
|
|
/* 512 */ NDADDR + 128 + 128 * 128 + 128 * 128 * 128,
|
|
|
|
/* 1024 */ NDADDR + 256 + 256 * 256 + 256 * 256 * 256,
|
|
|
|
/* 2048 */ NDADDR + 512 + 512 * 512 + 512 * 512 * 512,
|
|
|
|
/* 4096 */ NDADDR + 1024 + 1024 * 1024 + 1024 * 1024 * 1024,
|
|
|
|
/* 8192 */ 1 << 31,
|
|
|
|
/* 16 K */ 1 << 31,
|
|
|
|
/* 32 K */ 1 << 31,
|
|
|
|
};
|
|
|
|
maxfilesize = maxtable[sblock.lfs_bshift] << sblock.lfs_bshift;
|
|
|
|
}
|
1999-03-18 05:02:18 +03:00
|
|
|
#endif
|
|
|
|
if ((sblock.lfs_minfree < 0 || sblock.lfs_minfree > 99)) {
|
|
|
|
pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
|
2000-05-23 05:48:52 +04:00
|
|
|
sblock.lfs_minfree);
|
1999-03-18 05:02:18 +03:00
|
|
|
if (reply("SET TO DEFAULT") == 1) {
|
|
|
|
sblock.lfs_minfree = 10;
|
|
|
|
sbdirty();
|
|
|
|
}
|
|
|
|
}
|
2000-05-23 05:48:52 +04:00
|
|
|
/* XXX used to be ~(sblock.lfs_bsize - 1) */
|
1999-03-18 05:02:18 +03:00
|
|
|
if (sblock.lfs_bmask != sblock.lfs_bsize - 1) {
|
|
|
|
pwarn("INCORRECT BMASK=%x IN SUPERBLOCK (should be %x)",
|
2000-05-23 05:48:52 +04:00
|
|
|
(unsigned int)sblock.lfs_bmask,
|
|
|
|
(unsigned int)sblock.lfs_bsize - 1);
|
1999-03-18 05:02:18 +03:00
|
|
|
sblock.lfs_bmask = sblock.lfs_bsize - 1;
|
|
|
|
if (preen)
|
|
|
|
printf(" (FIXED)\n");
|
|
|
|
if (preen || reply("FIX") == 1) {
|
|
|
|
sbdirty();
|
|
|
|
dirty(&asblk);
|
|
|
|
}
|
|
|
|
}
|
2000-05-23 05:48:52 +04:00
|
|
|
#if 0 /* FFS-specific checks */
|
1999-03-18 05:02:18 +03:00
|
|
|
if (sblock.lfs_fmask != ~(sblock.lfs_fsize - 1)) {
|
|
|
|
pwarn("INCORRECT FMASK=%x IN SUPERBLOCK",
|
2000-05-23 05:48:52 +04:00
|
|
|
sblock.lfs_fmask);
|
1999-03-18 05:02:18 +03:00
|
|
|
sblock.lfs_fmask = ~(sblock.lfs_fsize - 1);
|
|
|
|
if (preen)
|
|
|
|
printf(" (FIXED)\n");
|
|
|
|
if (preen || reply("FIX") == 1) {
|
|
|
|
sbdirty();
|
|
|
|
dirty(&asblk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2000-05-23 05:48:52 +04:00
|
|
|
if (sblock.lfs_maxfilesize != maxfilesize) {
|
2001-01-05 05:02:57 +03:00
|
|
|
pwarn(
|
|
|
|
"INCORRECT MAXFILESIZE=%llu IN SUPERBLOCK (should be %llu)",
|
|
|
|
(unsigned long long)sblock.lfs_maxfilesize,
|
|
|
|
(unsigned long long)maxfilesize);
|
2000-05-23 05:48:52 +04:00
|
|
|
sblock.lfs_maxfilesize = maxfilesize;
|
|
|
|
if (preen)
|
|
|
|
printf(" (FIXED)\n");
|
|
|
|
if (preen || reply("FIX") == 1) {
|
|
|
|
sbdirty();
|
|
|
|
dirty(&asblk);
|
1999-03-18 05:02:18 +03:00
|
|
|
}
|
2000-05-23 05:48:52 +04:00
|
|
|
}
|
|
|
|
if (sblock.lfs_maxsymlinklen != MAXSYMLINKLEN) {
|
|
|
|
pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK",
|
|
|
|
sblock.lfs_maxsymlinklen);
|
|
|
|
sblock.lfs_maxsymlinklen = MAXSYMLINKLEN;
|
|
|
|
if (preen)
|
|
|
|
printf(" (FIXED)\n");
|
|
|
|
if (preen || reply("FIX") == 1) {
|
|
|
|
sbdirty();
|
|
|
|
dirty(&asblk);
|
1999-03-18 05:02:18 +03:00
|
|
|
}
|
2000-05-23 05:48:52 +04:00
|
|
|
}
|
|
|
|
newinofmt = 1;
|
1999-03-18 05:02:18 +03:00
|
|
|
/*
|
|
|
|
* allocate and initialize the necessary maps
|
|
|
|
*/
|
|
|
|
#ifndef VERBOSE_BLOCKMAP
|
|
|
|
bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
|
2000-10-04 15:37:54 +04:00
|
|
|
blockmap = calloc((unsigned)bmapsize, sizeof(char));
|
1999-03-18 05:02:18 +03:00
|
|
|
#else
|
2000-05-23 05:48:52 +04:00
|
|
|
bmapsize = maxfsblock * sizeof(ino_t);
|
2000-10-04 15:37:54 +04:00
|
|
|
blockmap = (ino_t *)calloc(maxfsblock, sizeof(ino_t));
|
1999-03-18 05:02:18 +03:00
|
|
|
#endif
|
|
|
|
if (blockmap == NULL) {
|
|
|
|
printf("cannot alloc %u bytes for blockmap\n",
|
2000-05-23 05:48:52 +04:00
|
|
|
(unsigned)bmapsize);
|
1999-03-18 05:02:18 +03:00
|
|
|
goto badsblabel;
|
|
|
|
}
|
2000-10-04 15:37:54 +04:00
|
|
|
|
2000-06-14 22:43:57 +04:00
|
|
|
statemap = calloc((unsigned)maxino, sizeof(char));
|
1999-03-18 05:02:18 +03:00
|
|
|
if (statemap == NULL) {
|
|
|
|
printf("cannot alloc %u bytes for statemap\n",
|
2000-06-14 22:43:57 +04:00
|
|
|
(unsigned)maxino);
|
1999-03-18 05:02:18 +03:00
|
|
|
goto badsblabel;
|
|
|
|
}
|
2000-06-14 22:43:57 +04:00
|
|
|
typemap = calloc((unsigned)maxino, sizeof(char));
|
1999-03-18 05:02:18 +03:00
|
|
|
if (typemap == NULL) {
|
|
|
|
printf("cannot alloc %u bytes for typemap\n",
|
2000-06-14 22:43:57 +04:00
|
|
|
(unsigned)maxino);
|
1999-03-18 05:02:18 +03:00
|
|
|
goto badsblabel;
|
|
|
|
}
|
2000-06-14 22:43:57 +04:00
|
|
|
lncntp = (int16_t *)calloc((unsigned)maxino, sizeof(int16_t));
|
1999-03-18 05:02:18 +03:00
|
|
|
if (lncntp == NULL) {
|
2000-05-23 05:48:52 +04:00
|
|
|
printf("cannot alloc %lu bytes for lncntp\n",
|
2000-06-14 22:43:57 +04:00
|
|
|
(unsigned long)maxino * sizeof(int16_t));
|
1999-03-18 05:02:18 +03:00
|
|
|
goto badsblabel;
|
|
|
|
}
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
badsblabel:
|
|
|
|
ckfini(0);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read in the LFS super block and its summary info.
|
|
|
|
*/
|
|
|
|
static int
|
2000-05-23 05:48:52 +04:00
|
|
|
readsb(int listerr)
|
1999-03-18 05:02:18 +03:00
|
|
|
{
|
2000-05-23 05:48:52 +04:00
|
|
|
daddr_t super = bflag ? bflag : LFS_LABELPAD / dev_bsize;
|
|
|
|
u_int32_t checksum;
|
1999-03-18 05:02:18 +03:00
|
|
|
|
2000-05-23 05:48:52 +04:00
|
|
|
if (bread(fsreadfd, (char *) &sblock, super, (long) LFS_SBPAD) != 0)
|
1999-03-18 05:02:18 +03:00
|
|
|
return (0);
|
|
|
|
|
|
|
|
sblk.b_bno = super;
|
|
|
|
sblk.b_size = LFS_SBPAD;
|
|
|
|
/*
|
|
|
|
* run a few consistency checks of the super block
|
|
|
|
*/
|
2000-05-23 05:48:52 +04:00
|
|
|
if (sblock.lfs_magic != LFS_MAGIC) {
|
|
|
|
badsb(listerr, "MAGIC NUMBER WRONG");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
/* checksum */
|
|
|
|
checksum = lfs_sb_cksum(&(sblock.lfs_dlfs));
|
|
|
|
if (sblock.lfs_cksum != checksum) {
|
|
|
|
printf("Superblock checksum (%lu)does not match computed checksum %lu\n",
|
|
|
|
(unsigned long)sblock.lfs_cksum, (unsigned long) checksum);
|
|
|
|
}
|
|
|
|
#if 0 /* XXX - replace these checks with
|
|
|
|
* appropriate LFS sanity checks */
|
|
|
|
if (sblock.lfs_ncg < 1) {
|
|
|
|
badsb(listerr, "NCG OUT OF RANGE");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (sblock.lfs_cpg < 1) {
|
|
|
|
badsb(listerr, "CPG OUT OF RANGE");
|
|
|
|
return (0);
|
|
|
|
}
|
1999-03-18 05:02:18 +03:00
|
|
|
if (sblock.lfs_ncg * sblock.lfs_cpg < sblock.lfs_ncyl ||
|
2000-05-23 05:48:52 +04:00
|
|
|
(sblock.lfs_ncg - 1) * sblock.lfs_cpg >= sblock.lfs_ncyl) {
|
|
|
|
badsb(listerr, "NCYL LESS THAN NCG*CPG");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (sblock.lfs_sbsize > SBSIZE) {
|
|
|
|
badsb(listerr, "SIZE PREPOSTEROUSLY LARGE");
|
|
|
|
return (0);
|
|
|
|
}
|
1999-03-18 05:02:18 +03:00
|
|
|
#endif
|
|
|
|
/*
|
|
|
|
* Compute block size that the filesystem is based on,
|
|
|
|
* according to fsbtodb, and adjust superblock block number
|
|
|
|
* so we can tell if this is an alternate later.
|
|
|
|
*/
|
|
|
|
super *= dev_bsize;
|
|
|
|
#if 0
|
|
|
|
dev_bsize = sblock.lfs_bsize / fsbtodb(&sblock, 1);
|
|
|
|
#endif
|
|
|
|
sblk.b_bno = super / dev_bsize;
|
|
|
|
if (bflag) {
|
|
|
|
havesb = 1;
|
|
|
|
return (1);
|
|
|
|
}
|
2000-05-23 05:48:52 +04:00
|
|
|
#if 0 /* XXX - for now skip the alt. superblock
|
|
|
|
* test as well */
|
1999-03-18 05:02:18 +03:00
|
|
|
/*
|
|
|
|
* Set all possible fields that could differ, then do check
|
|
|
|
* of whole super block against an alternate super block.
|
|
|
|
* When an alternate super-block is specified this check is skipped.
|
|
|
|
*/
|
|
|
|
getblk(&asblk, cgsblock(&sblock, sblock.lfs_ncg - 1), sblock.lfs_sbsize);
|
|
|
|
if (asblk.b_errs)
|
|
|
|
return (0);
|
|
|
|
altsblock.lfs_firstfield = sblock.lfs_firstfield;
|
|
|
|
altsblock.lfs_fscktime = sblock.lfs_fscktime;
|
|
|
|
altsblock.lfs_time = sblock.lfs_time;
|
|
|
|
altsblock.lfs_cstotal = sblock.lfs_cstotal;
|
|
|
|
altsblock.lfs_cgrotor = sblock.lfs_cgrotor;
|
|
|
|
altsblock.lfs_fmod = sblock.lfs_fmod;
|
|
|
|
altsblock.lfs_clean = sblock.lfs_clean;
|
|
|
|
altsblock.lfs_ronly = sblock.lfs_ronly;
|
|
|
|
altsblock.lfs_flags = sblock.lfs_flags;
|
|
|
|
altsblock.lfs_maxcontig = sblock.lfs_maxcontig;
|
|
|
|
altsblock.lfs_minfree = sblock.lfs_minfree;
|
|
|
|
altsblock.lfs_optim = sblock.lfs_optim;
|
|
|
|
altsblock.lfs_rotdelay = sblock.lfs_rotdelay;
|
|
|
|
altsblock.lfs_maxbpg = sblock.lfs_maxbpg;
|
|
|
|
memcpy(altsblock.lfs_csp, sblock.lfs_csp,
|
2000-05-23 05:48:52 +04:00
|
|
|
sizeof sblock.lfs_csp);
|
1999-03-18 05:02:18 +03:00
|
|
|
altsblock.lfs_maxcluster = sblock.lfs_maxcluster;
|
|
|
|
memcpy(altsblock.lfs_fsmnt, sblock.lfs_fsmnt,
|
2000-05-23 05:48:52 +04:00
|
|
|
sizeof sblock.lfs_fsmnt);
|
1999-03-18 05:02:18 +03:00
|
|
|
memcpy(altsblock.lfs_sparecon, sblock.lfs_sparecon,
|
2000-05-23 05:48:52 +04:00
|
|
|
sizeof sblock.lfs_sparecon);
|
1999-03-18 05:02:18 +03:00
|
|
|
/*
|
|
|
|
* The following should not have to be copied.
|
|
|
|
*/
|
|
|
|
altsblock.lfs_fsbtodb = sblock.lfs_fsbtodb;
|
|
|
|
altsblock.lfs_interleave = sblock.lfs_interleave;
|
|
|
|
altsblock.lfs_npsect = sblock.lfs_npsect;
|
|
|
|
altsblock.lfs_nrpos = sblock.lfs_nrpos;
|
|
|
|
altsblock.lfs_state = sblock.lfs_state;
|
|
|
|
altsblock.lfs_qbmask = sblock.lfs_qbmask;
|
|
|
|
altsblock.lfs_qfmask = sblock.lfs_qfmask;
|
|
|
|
altsblock.lfs_state = sblock.lfs_state;
|
|
|
|
altsblock.lfs_maxfilesize = sblock.lfs_maxfilesize;
|
|
|
|
if (memcmp(&sblock, &altsblock, (int)sblock.lfs_sbsize)) {
|
|
|
|
if (debug) {
|
2000-05-23 05:48:52 +04:00
|
|
|
long *nlp, *olp, *endlp;
|
1999-03-18 05:02:18 +03:00
|
|
|
|
|
|
|
printf("superblock mismatches\n");
|
2000-05-23 05:48:52 +04:00
|
|
|
nlp = (long *) &altsblock;
|
|
|
|
olp = (long *) &sblock;
|
1999-03-18 05:02:18 +03:00
|
|
|
endlp = olp + (sblock.lfs_sbsize / sizeof *olp);
|
2000-05-23 05:48:52 +04:00
|
|
|
for (; olp < endlp; olp++, nlp++) {
|
1999-03-18 05:02:18 +03:00
|
|
|
if (*olp == *nlp)
|
|
|
|
continue;
|
|
|
|
printf("offset %d, original %ld, alternate %ld\n",
|
2000-05-23 05:48:52 +04:00
|
|
|
olp - (long *) &sblock, *olp, *nlp);
|
1999-03-18 05:02:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
badsb(listerr,
|
2000-05-23 05:48:52 +04:00
|
|
|
"VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
|
1999-03-18 05:02:18 +03:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
havesb = 1;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-05-23 05:48:52 +04:00
|
|
|
badsb(int listerr, char *s)
|
1999-03-18 05:02:18 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
if (!listerr)
|
|
|
|
return;
|
|
|
|
if (preen)
|
|
|
|
printf("%s: ", cdevname());
|
|
|
|
pfatal("BAD SUPER BLOCK: %s\n", s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate a prototype superblock based on information in the disk label.
|
|
|
|
* When done the cgsblock macro can be calculated and the fs_ncg field
|
|
|
|
* can be used. Do NOT attempt to use other macros without verifying that
|
|
|
|
* their needed information is available!
|
|
|
|
*/
|
|
|
|
int
|
2000-05-23 05:48:52 +04:00
|
|
|
calcsb(const char *dev, int devfd, struct lfs * fs)
|
1999-03-18 05:02:18 +03:00
|
|
|
{
|
|
|
|
register struct disklabel *lp;
|
|
|
|
register struct partition *pp;
|
2000-05-23 05:48:52 +04:00
|
|
|
register char *cp;
|
|
|
|
int i;
|
1999-03-18 05:02:18 +03:00
|
|
|
|
|
|
|
cp = strchr(dev, '\0') - 1;
|
2000-05-23 05:48:52 +04:00
|
|
|
if ((cp == (char *) -1 || (*cp < 'a' || *cp > 'h')) && !isdigit(*cp)) {
|
1999-03-18 05:02:18 +03:00
|
|
|
pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
lp = getdisklabel(dev, devfd);
|
2000-05-23 05:48:52 +04:00
|
|
|
if (lp == NULL) {
|
2000-05-16 08:55:58 +04:00
|
|
|
dev_bsize = DEV_BSIZE;
|
|
|
|
} else {
|
|
|
|
if (isdigit(*cp))
|
|
|
|
pp = &lp->d_partitions[0];
|
|
|
|
else
|
|
|
|
pp = &lp->d_partitions[*cp - 'a'];
|
|
|
|
if (pp->p_fstype != FS_BSDLFS) {
|
|
|
|
pfatal("%s: NOT LABELED AS AN LFS FILE SYSTEM (%s)\n",
|
2000-05-23 05:48:52 +04:00
|
|
|
dev, pp->p_fstype < FSMAXTYPES ?
|
|
|
|
fstypenames[pp->p_fstype] : "unknown");
|
2000-05-16 08:55:58 +04:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
memset(fs, 0, sizeof(struct lfs));
|
|
|
|
fs->lfs_fsize = pp->p_fsize;
|
|
|
|
fs->lfs_frag = pp->p_frag;
|
|
|
|
fs->lfs_size = pp->p_size;
|
|
|
|
fs->lfs_nspf = fs->lfs_fsize / lp->d_secsize;
|
|
|
|
dev_bsize = lp->d_secsize;
|
|
|
|
for (fs->lfs_fsbtodb = 0, i = fs->lfs_nspf; i > 1; i >>= 1)
|
|
|
|
fs->lfs_fsbtodb++;
|
1999-03-18 05:02:18 +03:00
|
|
|
}
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct disklabel *
|
2000-05-23 05:48:52 +04:00
|
|
|
getdisklabel(const char *s, int fd)
|
1999-03-18 05:02:18 +03:00
|
|
|
{
|
|
|
|
static struct disklabel lab;
|
|
|
|
|
2000-05-23 05:48:52 +04:00
|
|
|
if (ioctl(fd, DIOCGDINFO, (char *) &lab) < 0) {
|
1999-03-18 05:02:18 +03:00
|
|
|
if (s == NULL)
|
2000-05-23 05:48:52 +04:00
|
|
|
return ((struct disklabel *) NULL);
|
1999-03-18 05:02:18 +03:00
|
|
|
pwarn("ioctl (GCINFO): %s\n", strerror(errno));
|
2000-05-16 08:55:58 +04:00
|
|
|
#if 0
|
1999-03-18 05:02:18 +03:00
|
|
|
errexit("%s: can't read disk label\n", s);
|
2000-05-16 08:55:58 +04:00
|
|
|
#else
|
|
|
|
return NULL;
|
|
|
|
#endif
|
1999-03-18 05:02:18 +03:00
|
|
|
}
|
|
|
|
return (&lab);
|
|
|
|
}
|