Home | History | Annotate | Line # | Download | only in fsck_lfs
setup.c revision 1.1
      1  1.1  perseant /*	$Id: setup.c,v 1.1 1999/03/18 02:02:19 perseant Exp $	*/
      2  1.1  perseant 
      3  1.1  perseant /*
      4  1.1  perseant  * Copyright (c) 1980, 1986, 1993
      5  1.1  perseant  *	The Regents of the University of California.  All rights reserved.
      6  1.1  perseant  *
      7  1.1  perseant  * Redistribution and use in source and binary forms, with or without
      8  1.1  perseant  * modification, are permitted provided that the following conditions
      9  1.1  perseant  * are met:
     10  1.1  perseant  * 1. Redistributions of source code must retain the above copyright
     11  1.1  perseant  *    notice, this list of conditions and the following disclaimer.
     12  1.1  perseant  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  perseant  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  perseant  *    documentation and/or other materials provided with the distribution.
     15  1.1  perseant  * 3. All advertising materials mentioning features or use of this software
     16  1.1  perseant  *    must display the following acknowledgement:
     17  1.1  perseant  *	This product includes software developed by the University of
     18  1.1  perseant  *	California, Berkeley and its contributors.
     19  1.1  perseant  * 4. Neither the name of the University nor the names of its contributors
     20  1.1  perseant  *    may be used to endorse or promote products derived from this software
     21  1.1  perseant  *    without specific prior written permission.
     22  1.1  perseant  *
     23  1.1  perseant  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1  perseant  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1  perseant  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1  perseant  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1  perseant  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1  perseant  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1  perseant  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1  perseant  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1  perseant  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1  perseant  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1  perseant  * SUCH DAMAGE.
     34  1.1  perseant  */
     35  1.1  perseant 
     36  1.1  perseant /* #define DKTYPENAMES */
     37  1.1  perseant #define FSTYPENAMES
     38  1.1  perseant #include <sys/param.h>
     39  1.1  perseant #include <sys/time.h>
     40  1.1  perseant #include <sys/stat.h>
     41  1.1  perseant #include <sys/ioctl.h>
     42  1.1  perseant #include <sys/disklabel.h>
     43  1.1  perseant #include <sys/file.h>
     44  1.1  perseant 
     45  1.1  perseant #include <ufs/ufs/dinode.h>
     46  1.1  perseant #include <sys/mount.h> /* XXX ufs/lfs/lfs.h should include this for us */
     47  1.1  perseant #include <ufs/lfs/lfs.h>
     48  1.1  perseant #include <ufs/lfs/lfs_extern.h>
     49  1.1  perseant 
     50  1.1  perseant #include <ctype.h>
     51  1.1  perseant #include <err.h>
     52  1.1  perseant #include <errno.h>
     53  1.1  perseant #include <stdio.h>
     54  1.1  perseant #include <stdlib.h>
     55  1.1  perseant #include <string.h>
     56  1.1  perseant 
     57  1.1  perseant #include "fsck.h"
     58  1.1  perseant #include "extern.h"
     59  1.1  perseant #include "fsutil.h"
     60  1.1  perseant 
     61  1.1  perseant struct bufarea asblk;
     62  1.1  perseant extern struct dinode **din_table;
     63  1.1  perseant extern SEGUSE *seg_table;
     64  1.1  perseant #define altsblock (*asblk.b_un.b_fs)
     65  1.1  perseant #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
     66  1.1  perseant 
     67  1.1  perseant void badsb __P((int, char *));
     68  1.1  perseant int calcsb __P((const char *, int, struct lfs *));
     69  1.1  perseant static struct disklabel *getdisklabel __P((const char *, int));
     70  1.1  perseant static int readsb __P((int));
     71  1.1  perseant int lfs_maxino(void);
     72  1.1  perseant 
     73  1.1  perseant #ifdef DKTYPENAMES
     74  1.1  perseant int useless __P((void));
     75  1.1  perseant 
     76  1.1  perseant int
     77  1.1  perseant useless(void)
     78  1.1  perseant {
     79  1.1  perseant 	char **foo = (char **)dktypenames;
     80  1.1  perseant 	char **bar = (char **)fscknames;
     81  1.1  perseant 
     82  1.1  perseant 	return foo-bar;
     83  1.1  perseant }
     84  1.1  perseant #endif
     85  1.1  perseant 
     86  1.1  perseant int
     87  1.1  perseant setup(dev)
     88  1.1  perseant 	const char *dev;
     89  1.1  perseant {
     90  1.1  perseant 	long bmapsize;
     91  1.1  perseant 	struct disklabel *lp;
     92  1.1  perseant #if 0
     93  1.1  perseant 	long i;
     94  1.1  perseant 	off_t sizepb;
     95  1.1  perseant #endif
     96  1.1  perseant 	struct stat statb;
     97  1.1  perseant 	struct lfs proto;
     98  1.1  perseant 	int doskipclean;
     99  1.1  perseant 	u_int64_t maxfilesize;
    100  1.1  perseant         struct lfs *sb0;
    101  1.1  perseant 
    102  1.1  perseant 	havesb = 0;
    103  1.1  perseant 	fswritefd = -1;
    104  1.1  perseant 	doskipclean = skipclean;
    105  1.1  perseant 	if (stat(dev, &statb) < 0) {
    106  1.1  perseant 		printf("Can't stat %s: %s\n", dev, strerror(errno));
    107  1.1  perseant 		return (0);
    108  1.1  perseant 	}
    109  1.1  perseant 	if (!S_ISCHR(statb.st_mode)) {
    110  1.1  perseant 		pfatal("%s is not a character device", dev);
    111  1.1  perseant 		if (reply("CONTINUE") == 0)
    112  1.1  perseant 			return (0);
    113  1.1  perseant 	}
    114  1.1  perseant 	if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
    115  1.1  perseant 		printf("Can't open %s: %s\n", dev, strerror(errno));
    116  1.1  perseant 		return (0);
    117  1.1  perseant 	}
    118  1.1  perseant 	if (preen == 0)
    119  1.1  perseant 		printf("** %s", dev);
    120  1.1  perseant 	if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
    121  1.1  perseant 		fswritefd = -1;
    122  1.1  perseant 		if (preen)
    123  1.1  perseant 			pfatal("NO WRITE ACCESS");
    124  1.1  perseant 		printf(" (NO WRITE)");
    125  1.1  perseant 	}
    126  1.1  perseant 	if (preen == 0)
    127  1.1  perseant 		printf("\n");
    128  1.1  perseant 	fsmodified = 0;
    129  1.1  perseant 	lfdir = 0;
    130  1.1  perseant 	initbarea(&sblk);
    131  1.1  perseant 	initbarea(&asblk);
    132  1.1  perseant 	sblk.b_un.b_buf = malloc(LFS_SBPAD);
    133  1.1  perseant 	asblk.b_un.b_buf = malloc(LFS_SBPAD);
    134  1.1  perseant 	if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
    135  1.1  perseant 		errexit("cannot allocate space for superblock\n");
    136  1.1  perseant 	if ((lp = getdisklabel((char *)NULL, fsreadfd)) != NULL)
    137  1.1  perseant 		dev_bsize = secsize = lp->d_secsize;
    138  1.1  perseant 	else
    139  1.1  perseant 		dev_bsize = secsize = DEV_BSIZE;
    140  1.1  perseant 
    141  1.1  perseant 	/*
    142  1.1  perseant 	 * Read in the superblock, looking for alternates if necessary
    143  1.1  perseant 	 */
    144  1.1  perseant 	if (readsb(1) == 0) {
    145  1.1  perseant 		if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
    146  1.1  perseant 			return(0);
    147  1.1  perseant 		if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
    148  1.1  perseant 			return (0);
    149  1.1  perseant #if 0 /* XXX find the LFS way to do this */
    150  1.1  perseant 		for (cg = 0; cg < proto.lfs_ncg; cg++) {
    151  1.1  perseant 			bflag = fsbtodb(&proto, cgsblock(&proto, cg));
    152  1.1  perseant 			if (readsb(0) != 0)
    153  1.1  perseant 				break;
    154  1.1  perseant 		}
    155  1.1  perseant 		if (cg >= proto.lfs_ncg) {
    156  1.1  perseant 			printf("%s %s\n%s %s\n%s %s\n",
    157  1.1  perseant 				"SEARCH FOR ALTERNATE SUPER-BLOCK",
    158  1.1  perseant 				"FAILED. YOU MUST USE THE",
    159  1.1  perseant 				"-b OPTION TO FSCK_FFS TO SPECIFY THE",
    160  1.1  perseant 				"LOCATION OF AN ALTERNATE",
    161  1.1  perseant 				"SUPER-BLOCK TO SUPPLY NEEDED",
    162  1.1  perseant 				"INFORMATION; SEE fsck_ffs(8).");
    163  1.1  perseant 			return(0);
    164  1.1  perseant 		}
    165  1.1  perseant #else
    166  1.1  perseant 		pwarn("XXX Can't look for alternate superblocks yet\n");
    167  1.1  perseant 		return(0);
    168  1.1  perseant #endif
    169  1.1  perseant 		doskipclean = 0;
    170  1.1  perseant 		pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
    171  1.1  perseant 	}
    172  1.1  perseant 	if(bflag==0) {
    173  1.1  perseant 		/*
    174  1.1  perseant 		 * Even if that superblock read in properly, it may not
    175  1.1  perseant 		 * be guaranteed to point to a complete checkpoint.
    176  1.1  perseant 		 * Read in the second superblock too, and take whichever
    177  1.1  perseant 		 * of the two is *less* recent. --ks
    178  1.1  perseant 		 */
    179  1.1  perseant 		sb0 = malloc(sizeof(*sb0));
    180  1.1  perseant 		memcpy(sb0,&sblock,sizeof(*sb0));
    181  1.1  perseant 			bflag = sblock.lfs_sboffs[1];
    182  1.1  perseant 			if(readsb(1)==0) {
    183  1.1  perseant 				pwarn("COULDN'T READ ALT SUPERBLOCK AT BLK %d",bflag);
    184  1.1  perseant 				if (reply("ASSUME PRIMARY SUPERBLOCK IS GOOD") == 0) {
    185  1.1  perseant 					return (0);
    186  1.1  perseant 				} else { /* use primary as good */
    187  1.1  perseant 					memcpy(&sblock,sb0,sizeof(*sb0)); /* XXX cheating? */
    188  1.1  perseant 				}
    189  1.1  perseant 			} else {
    190  1.1  perseant 				if(debug)
    191  1.1  perseant 					pwarn("sb0 %d, sb1 %d\n",sb0->lfs_tstamp,sblock.lfs_tstamp);
    192  1.1  perseant 				if(sblock.lfs_tstamp >= sb0->lfs_tstamp) {
    193  1.1  perseant 					memcpy(&sblock,sb0,sizeof(*sb0)); /* XXX cheating? */
    194  1.1  perseant 				} else {
    195  1.1  perseant 					pwarn("Using alt superblock, disk addr 0x%x\n",
    196  1.1  perseant 					      bflag);
    197  1.1  perseant 				}
    198  1.1  perseant 			}
    199  1.1  perseant 		free(sb0);
    200  1.1  perseant 	}
    201  1.1  perseant 	if(debug) {
    202  1.1  perseant         	printf("dev_bsize = %lu\n",dev_bsize);
    203  1.1  perseant                 printf("lfs_bsize = %lu\n",(unsigned long)sblock.lfs_bsize);
    204  1.1  perseant                 printf("lfs_fsize = %lu\n",(unsigned long)sblock.lfs_fsize);
    205  1.1  perseant                 printf("lfs_frag  = %lu\n",(unsigned long)sblock.lfs_frag);
    206  1.1  perseant                 printf("INOPB(fs) = %lu\n",(unsigned long)INOPB(&sblock));
    207  1.1  perseant         	/* printf("fsbtodb(fs,1) = %lu\n",fsbtodb(&sblock,1)); */
    208  1.1  perseant 	}
    209  1.1  perseant #if 0 /* FFS-specific fs-clean check */
    210  1.1  perseant 	if (debug)
    211  1.1  perseant 		printf("clean = %d\n", sblock.lfs_clean);
    212  1.1  perseant 	if (sblock.lfs_clean & FS_ISCLEAN) {
    213  1.1  perseant 		if (doskipclean) {
    214  1.1  perseant 			pwarn("%sile system is clean; not checking\n",
    215  1.1  perseant 			    preen ? "f" : "** F");
    216  1.1  perseant 			return (-1);
    217  1.1  perseant 		}
    218  1.1  perseant 		if (!preen)
    219  1.1  perseant 			pwarn("** File system is already clean\n");
    220  1.1  perseant 	}
    221  1.1  perseant 	maxino = sblock.lfs_ncg * sblock.lfs_ipg;
    222  1.1  perseant #else
    223  1.1  perseant #if 0
    224  1.1  perseant         /* XXX - count the number of inodes here */
    225  1.1  perseant         maxino = sblock.lfs_nfiles+2;
    226  1.1  perseant #else
    227  1.1  perseant         initbarea(&iblk);
    228  1.1  perseant         iblk.b_un.b_buf = malloc(sblock.lfs_bsize);
    229  1.1  perseant         if(bread(fsreadfd, (char *)iblk.b_un.b_buf,
    230  1.1  perseant                  sblock.lfs_idaddr,
    231  1.1  perseant                  (long)sblock.lfs_bsize) != 0)
    232  1.1  perseant         {
    233  1.1  perseant             printf("Couldn't read disk block %d\n",sblock.lfs_idaddr);
    234  1.1  perseant             exit(1);
    235  1.1  perseant         }
    236  1.1  perseant         maxino = lfs_maxino();
    237  1.1  perseant #endif
    238  1.1  perseant 	if (debug)
    239  1.1  perseant         	printf("maxino=%d\n",maxino);
    240  1.1  perseant         din_table = (struct dinode **)malloc(maxino*sizeof(*din_table));
    241  1.1  perseant         memset(din_table,0,maxino*sizeof(*din_table));
    242  1.1  perseant         seg_table = (SEGUSE *)malloc(sblock.lfs_nseg * sizeof(SEGUSE));
    243  1.1  perseant         memset(seg_table,0,sblock.lfs_nseg * sizeof(SEGUSE));
    244  1.1  perseant #endif
    245  1.1  perseant 	maxfsblock = sblock.lfs_size * (sblock.lfs_bsize / dev_bsize);
    246  1.1  perseant #if 0
    247  1.1  perseant 	sizepb = sblock.lfs_bsize;
    248  1.1  perseant 	maxfilesize = sblock.lfs_bsize * NDADDR - 1;
    249  1.1  perseant 	for (i = 0; i < NIADDR; i++) {
    250  1.1  perseant 		sizepb *= NINDIR(&sblock);
    251  1.1  perseant 		maxfilesize += sizepb;
    252  1.1  perseant 	}
    253  1.1  perseant         maxfilesize++; /* XXX */
    254  1.1  perseant #else /* LFS way */
    255  1.1  perseant {
    256  1.1  perseant u_quad_t maxtable[] = {
    257  1.1  perseant         /*    1 */ -1,
    258  1.1  perseant         /*    2 */ -1,
    259  1.1  perseant         /*    4 */ -1,
    260  1.1  perseant         /*    8 */ -1,
    261  1.1  perseant         /*   16 */ -1,
    262  1.1  perseant         /*   32 */ -1,
    263  1.1  perseant         /*   64 */ -1,
    264  1.1  perseant         /*  128 */ -1,
    265  1.1  perseant         /*  256 */ -1,
    266  1.1  perseant         /*  512 */ NDADDR + 128 + 128 * 128 + 128 * 128 * 128,
    267  1.1  perseant         /* 1024 */ NDADDR + 256 + 256 * 256 + 256 * 256 * 256,
    268  1.1  perseant         /* 2048 */ NDADDR + 512 + 512 * 512 + 512 * 512 * 512,
    269  1.1  perseant         /* 4096 */ NDADDR + 1024 + 1024 * 1024 + 1024 * 1024 * 1024,
    270  1.1  perseant         /* 8192 */ 1 << 31,
    271  1.1  perseant         /* 16 K */ 1 << 31,
    272  1.1  perseant         /* 32 K */ 1 << 31,
    273  1.1  perseant };
    274  1.1  perseant 	maxfilesize = maxtable[sblock.lfs_bshift] << sblock.lfs_bshift;
    275  1.1  perseant }
    276  1.1  perseant #endif
    277  1.1  perseant 	if ((sblock.lfs_minfree < 0 || sblock.lfs_minfree > 99)) {
    278  1.1  perseant 		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
    279  1.1  perseant 			sblock.lfs_minfree);
    280  1.1  perseant 		if (reply("SET TO DEFAULT") == 1) {
    281  1.1  perseant 			sblock.lfs_minfree = 10;
    282  1.1  perseant 			sbdirty();
    283  1.1  perseant 		}
    284  1.1  perseant 	}
    285  1.1  perseant         /* XXX used to be ~(sblock.lfs_bsize - 1) */
    286  1.1  perseant 	if (sblock.lfs_bmask != sblock.lfs_bsize - 1) {
    287  1.1  perseant 		pwarn("INCORRECT BMASK=%x IN SUPERBLOCK (should be %x)",
    288  1.1  perseant 			(unsigned int)sblock.lfs_bmask,
    289  1.1  perseant 			(unsigned int)sblock.lfs_bsize - 1);
    290  1.1  perseant 		sblock.lfs_bmask = sblock.lfs_bsize - 1;
    291  1.1  perseant 		if (preen)
    292  1.1  perseant 			printf(" (FIXED)\n");
    293  1.1  perseant 		if (preen || reply("FIX") == 1) {
    294  1.1  perseant 			sbdirty();
    295  1.1  perseant 			dirty(&asblk);
    296  1.1  perseant 		}
    297  1.1  perseant 	}
    298  1.1  perseant #if 0 /* FFS-specific checks */
    299  1.1  perseant 	if (sblock.lfs_fmask != ~(sblock.lfs_fsize - 1)) {
    300  1.1  perseant 		pwarn("INCORRECT FMASK=%x IN SUPERBLOCK",
    301  1.1  perseant 			sblock.lfs_fmask);
    302  1.1  perseant 		sblock.lfs_fmask = ~(sblock.lfs_fsize - 1);
    303  1.1  perseant 		if (preen)
    304  1.1  perseant 			printf(" (FIXED)\n");
    305  1.1  perseant 		if (preen || reply("FIX") == 1) {
    306  1.1  perseant 			sbdirty();
    307  1.1  perseant 			dirty(&asblk);
    308  1.1  perseant 		}
    309  1.1  perseant 	}
    310  1.1  perseant #endif
    311  1.1  perseant 		if (sblock.lfs_maxfilesize != maxfilesize) {
    312  1.1  perseant 			pwarn("INCORRECT MAXFILESIZE=%qu IN SUPERBLOCK (should be %qu)",
    313  1.1  perseant 				sblock.lfs_maxfilesize, maxfilesize);
    314  1.1  perseant 			sblock.lfs_maxfilesize = maxfilesize;
    315  1.1  perseant 			if (preen)
    316  1.1  perseant 				printf(" (FIXED)\n");
    317  1.1  perseant 			if (preen || reply("FIX") == 1) {
    318  1.1  perseant 				sbdirty();
    319  1.1  perseant 				dirty(&asblk);
    320  1.1  perseant 			}
    321  1.1  perseant 		}
    322  1.1  perseant 		if (sblock.lfs_maxsymlinklen != MAXSYMLINKLEN) {
    323  1.1  perseant 			pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK",
    324  1.1  perseant 				sblock.lfs_maxsymlinklen);
    325  1.1  perseant 			sblock.lfs_maxsymlinklen = MAXSYMLINKLEN;
    326  1.1  perseant 			if (preen)
    327  1.1  perseant 				printf(" (FIXED)\n");
    328  1.1  perseant 			if (preen || reply("FIX") == 1) {
    329  1.1  perseant 				sbdirty();
    330  1.1  perseant 				dirty(&asblk);
    331  1.1  perseant 			}
    332  1.1  perseant 		}
    333  1.1  perseant 		newinofmt = 1;
    334  1.1  perseant 	/*
    335  1.1  perseant 	 * allocate and initialize the necessary maps
    336  1.1  perseant 	 */
    337  1.1  perseant #ifndef VERBOSE_BLOCKMAP
    338  1.1  perseant 	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
    339  1.1  perseant         blockmap = malloc((unsigned)bmapsize * sizeof (char));
    340  1.1  perseant         bzero(blockmap, bmapsize * sizeof(char));
    341  1.1  perseant #else
    342  1.1  perseant 	bmapsize = maxfsblock*sizeof(ino_t);
    343  1.1  perseant         blockmap = (ino_t *)malloc(maxfsblock * sizeof (ino_t));
    344  1.1  perseant         bzero(blockmap, maxfsblock * sizeof(ino_t));
    345  1.1  perseant #endif
    346  1.1  perseant 	if (blockmap == NULL) {
    347  1.1  perseant 		printf("cannot alloc %u bytes for blockmap\n",
    348  1.1  perseant 		    (unsigned)bmapsize);
    349  1.1  perseant 		goto badsblabel;
    350  1.1  perseant 	}
    351  1.1  perseant 	statemap = calloc((unsigned)(maxino + 1), sizeof(char));
    352  1.1  perseant 	if (statemap == NULL) {
    353  1.1  perseant 		printf("cannot alloc %u bytes for statemap\n",
    354  1.1  perseant 		    (unsigned)(maxino + 1));
    355  1.1  perseant 		goto badsblabel;
    356  1.1  perseant 	}
    357  1.1  perseant 	typemap = calloc((unsigned)(maxino + 1), sizeof(char));
    358  1.1  perseant 	if (typemap == NULL) {
    359  1.1  perseant 		printf("cannot alloc %u bytes for typemap\n",
    360  1.1  perseant 		    (unsigned)(maxino + 1));
    361  1.1  perseant 		goto badsblabel;
    362  1.1  perseant 	}
    363  1.1  perseant 	lncntp = (int16_t *)calloc((unsigned)(maxino + 1), sizeof(int16_t));
    364  1.1  perseant 	if (lncntp == NULL) {
    365  1.1  perseant 		printf("cannot alloc %u bytes for lncntp\n",
    366  1.1  perseant 		    (unsigned)(maxino + 1) * sizeof(int16_t));
    367  1.1  perseant 		goto badsblabel;
    368  1.1  perseant 	}
    369  1.1  perseant 	bufinit();
    370  1.1  perseant 	return (1);
    371  1.1  perseant 
    372  1.1  perseant badsblabel:
    373  1.1  perseant 	ckfini(0);
    374  1.1  perseant 	return (0);
    375  1.1  perseant }
    376  1.1  perseant 
    377  1.1  perseant /*
    378  1.1  perseant  * Read in the LFS super block and its summary info.
    379  1.1  perseant  */
    380  1.1  perseant static int
    381  1.1  perseant readsb(listerr)
    382  1.1  perseant 	int listerr;
    383  1.1  perseant {
    384  1.1  perseant 	daddr_t super = bflag ? bflag : LFS_LABELPAD / dev_bsize;
    385  1.1  perseant         u_int32_t checksum;
    386  1.1  perseant 
    387  1.1  perseant 	if (bread(fsreadfd, (char *)&sblock, super, (long)LFS_SBPAD) != 0)
    388  1.1  perseant 		return (0);
    389  1.1  perseant 
    390  1.1  perseant 	sblk.b_bno = super;
    391  1.1  perseant 	sblk.b_size = LFS_SBPAD;
    392  1.1  perseant 	/*
    393  1.1  perseant 	 * run a few consistency checks of the super block
    394  1.1  perseant 	 */
    395  1.1  perseant 	if (sblock.lfs_magic != LFS_MAGIC)
    396  1.1  perseant 		{ badsb(listerr, "MAGIC NUMBER WRONG"); return (0); }
    397  1.1  perseant 
    398  1.1  perseant         /* checksum */
    399  1.1  perseant         checksum = lfs_sb_cksum(&(sblock.lfs_dlfs));
    400  1.1  perseant         if(sblock.lfs_cksum != checksum)
    401  1.1  perseant         {
    402  1.1  perseant             printf("Superblock checksum (%lu) does not match computed checksum %lu\n",
    403  1.1  perseant                    (unsigned long)sblock.lfs_cksum, (unsigned long)checksum);
    404  1.1  perseant         }
    405  1.1  perseant 
    406  1.1  perseant #if 0 /* XXX - replace these checks with appropriate LFS sanity checks */
    407  1.1  perseant 	if (sblock.lfs_ncg < 1)
    408  1.1  perseant 		{ badsb(listerr, "NCG OUT OF RANGE"); return (0); }
    409  1.1  perseant 	if (sblock.lfs_cpg < 1)
    410  1.1  perseant 		{ badsb(listerr, "CPG OUT OF RANGE"); return (0); }
    411  1.1  perseant 	if (sblock.lfs_ncg * sblock.lfs_cpg < sblock.lfs_ncyl ||
    412  1.1  perseant 	    (sblock.lfs_ncg - 1) * sblock.lfs_cpg >= sblock.lfs_ncyl)
    413  1.1  perseant 		{ badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); }
    414  1.1  perseant 	if (sblock.lfs_sbsize > SBSIZE)
    415  1.1  perseant 		{ badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
    416  1.1  perseant #endif
    417  1.1  perseant 	/*
    418  1.1  perseant 	 * Compute block size that the filesystem is based on,
    419  1.1  perseant 	 * according to fsbtodb, and adjust superblock block number
    420  1.1  perseant 	 * so we can tell if this is an alternate later.
    421  1.1  perseant 	 */
    422  1.1  perseant 	super *= dev_bsize;
    423  1.1  perseant #if 0
    424  1.1  perseant 	dev_bsize = sblock.lfs_bsize / fsbtodb(&sblock, 1);
    425  1.1  perseant #endif
    426  1.1  perseant 	sblk.b_bno = super / dev_bsize;
    427  1.1  perseant 	if (bflag) {
    428  1.1  perseant 		havesb = 1;
    429  1.1  perseant 		return (1);
    430  1.1  perseant 	}
    431  1.1  perseant #if 0 /* XXX - for now skip the alt. superblock test as well */
    432  1.1  perseant 	/*
    433  1.1  perseant 	 * Set all possible fields that could differ, then do check
    434  1.1  perseant 	 * of whole super block against an alternate super block.
    435  1.1  perseant 	 * When an alternate super-block is specified this check is skipped.
    436  1.1  perseant 	 */
    437  1.1  perseant 	getblk(&asblk, cgsblock(&sblock, sblock.lfs_ncg - 1), sblock.lfs_sbsize);
    438  1.1  perseant 	if (asblk.b_errs)
    439  1.1  perseant 		return (0);
    440  1.1  perseant 	altsblock.lfs_firstfield = sblock.lfs_firstfield;
    441  1.1  perseant 	altsblock.lfs_fscktime = sblock.lfs_fscktime;
    442  1.1  perseant 	altsblock.lfs_time = sblock.lfs_time;
    443  1.1  perseant 	altsblock.lfs_cstotal = sblock.lfs_cstotal;
    444  1.1  perseant 	altsblock.lfs_cgrotor = sblock.lfs_cgrotor;
    445  1.1  perseant 	altsblock.lfs_fmod = sblock.lfs_fmod;
    446  1.1  perseant 	altsblock.lfs_clean = sblock.lfs_clean;
    447  1.1  perseant 	altsblock.lfs_ronly = sblock.lfs_ronly;
    448  1.1  perseant 	altsblock.lfs_flags = sblock.lfs_flags;
    449  1.1  perseant 	altsblock.lfs_maxcontig = sblock.lfs_maxcontig;
    450  1.1  perseant 	altsblock.lfs_minfree = sblock.lfs_minfree;
    451  1.1  perseant 	altsblock.lfs_optim = sblock.lfs_optim;
    452  1.1  perseant 	altsblock.lfs_rotdelay = sblock.lfs_rotdelay;
    453  1.1  perseant 	altsblock.lfs_maxbpg = sblock.lfs_maxbpg;
    454  1.1  perseant 	memcpy(altsblock.lfs_csp, sblock.lfs_csp,
    455  1.1  perseant 		sizeof sblock.lfs_csp);
    456  1.1  perseant 	altsblock.lfs_maxcluster = sblock.lfs_maxcluster;
    457  1.1  perseant 	memcpy(altsblock.lfs_fsmnt, sblock.lfs_fsmnt,
    458  1.1  perseant 		sizeof sblock.lfs_fsmnt);
    459  1.1  perseant 	memcpy(altsblock.lfs_sparecon, sblock.lfs_sparecon,
    460  1.1  perseant 		sizeof sblock.lfs_sparecon);
    461  1.1  perseant 	/*
    462  1.1  perseant 	 * The following should not have to be copied.
    463  1.1  perseant 	 */
    464  1.1  perseant 	altsblock.lfs_fsbtodb = sblock.lfs_fsbtodb;
    465  1.1  perseant 	altsblock.lfs_interleave = sblock.lfs_interleave;
    466  1.1  perseant 	altsblock.lfs_npsect = sblock.lfs_npsect;
    467  1.1  perseant 	altsblock.lfs_nrpos = sblock.lfs_nrpos;
    468  1.1  perseant 	altsblock.lfs_state = sblock.lfs_state;
    469  1.1  perseant 	altsblock.lfs_qbmask = sblock.lfs_qbmask;
    470  1.1  perseant 	altsblock.lfs_qfmask = sblock.lfs_qfmask;
    471  1.1  perseant 	altsblock.lfs_state = sblock.lfs_state;
    472  1.1  perseant 	altsblock.lfs_maxfilesize = sblock.lfs_maxfilesize;
    473  1.1  perseant 	if (memcmp(&sblock, &altsblock, (int)sblock.lfs_sbsize)) {
    474  1.1  perseant 		if (debug) {
    475  1.1  perseant 			long *nlp, *olp, *endlp;
    476  1.1  perseant 
    477  1.1  perseant 			printf("superblock mismatches\n");
    478  1.1  perseant 			nlp = (long *)&altsblock;
    479  1.1  perseant 			olp = (long *)&sblock;
    480  1.1  perseant 			endlp = olp + (sblock.lfs_sbsize / sizeof *olp);
    481  1.1  perseant 			for ( ; olp < endlp; olp++, nlp++) {
    482  1.1  perseant 				if (*olp == *nlp)
    483  1.1  perseant 					continue;
    484  1.1  perseant 				printf("offset %d, original %ld, alternate %ld\n",
    485  1.1  perseant 				    olp - (long *)&sblock, *olp, *nlp);
    486  1.1  perseant 			}
    487  1.1  perseant 		}
    488  1.1  perseant 		badsb(listerr,
    489  1.1  perseant 		"VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
    490  1.1  perseant 		return (0);
    491  1.1  perseant 	}
    492  1.1  perseant #endif
    493  1.1  perseant 	havesb = 1;
    494  1.1  perseant 	return (1);
    495  1.1  perseant }
    496  1.1  perseant 
    497  1.1  perseant void
    498  1.1  perseant badsb(listerr, s)
    499  1.1  perseant 	int listerr;
    500  1.1  perseant 	char *s;
    501  1.1  perseant {
    502  1.1  perseant 
    503  1.1  perseant 	if (!listerr)
    504  1.1  perseant 		return;
    505  1.1  perseant 	if (preen)
    506  1.1  perseant 		printf("%s: ", cdevname());
    507  1.1  perseant 	pfatal("BAD SUPER BLOCK: %s\n", s);
    508  1.1  perseant }
    509  1.1  perseant 
    510  1.1  perseant /*
    511  1.1  perseant  * Calculate a prototype superblock based on information in the disk label.
    512  1.1  perseant  * When done the cgsblock macro can be calculated and the fs_ncg field
    513  1.1  perseant  * can be used. Do NOT attempt to use other macros without verifying that
    514  1.1  perseant  * their needed information is available!
    515  1.1  perseant  */
    516  1.1  perseant int
    517  1.1  perseant calcsb(dev, devfd, fs)
    518  1.1  perseant 	const char *dev;
    519  1.1  perseant 	int devfd;
    520  1.1  perseant 	register struct lfs *fs;
    521  1.1  perseant {
    522  1.1  perseant 	register struct disklabel *lp;
    523  1.1  perseant 	register struct partition *pp;
    524  1.1  perseant 	register char *cp;
    525  1.1  perseant 	int i;
    526  1.1  perseant 
    527  1.1  perseant 	cp = strchr(dev, '\0') - 1;
    528  1.1  perseant 	if ((cp == (char *)-1 || (*cp < 'a' || *cp > 'h')) && !isdigit(*cp)) {
    529  1.1  perseant 		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
    530  1.1  perseant 		return (0);
    531  1.1  perseant 	}
    532  1.1  perseant 	lp = getdisklabel(dev, devfd);
    533  1.1  perseant 	if (isdigit(*cp))
    534  1.1  perseant 		pp = &lp->d_partitions[0];
    535  1.1  perseant 	else
    536  1.1  perseant 		pp = &lp->d_partitions[*cp - 'a'];
    537  1.1  perseant 	if (pp->p_fstype != FS_BSDLFS) {
    538  1.1  perseant 		pfatal("%s: NOT LABELED AS AN LFS FILE SYSTEM (%s)\n",
    539  1.1  perseant 			dev, pp->p_fstype < FSMAXTYPES ?
    540  1.1  perseant 			fstypenames[pp->p_fstype] : "unknown");
    541  1.1  perseant 		return (0);
    542  1.1  perseant 	}
    543  1.1  perseant 	memset(fs, 0, sizeof(struct lfs));
    544  1.1  perseant 	fs->lfs_fsize = pp->p_fsize;
    545  1.1  perseant 	fs->lfs_frag = pp->p_frag;
    546  1.1  perseant 	fs->lfs_size = pp->p_size;
    547  1.1  perseant 	fs->lfs_nspf = fs->lfs_fsize / lp->d_secsize;
    548  1.1  perseant 	dev_bsize = lp->d_secsize;
    549  1.1  perseant 	for (fs->lfs_fsbtodb = 0, i = fs->lfs_nspf; i > 1; i >>= 1)
    550  1.1  perseant 		fs->lfs_fsbtodb++;
    551  1.1  perseant 	return (1);
    552  1.1  perseant }
    553  1.1  perseant 
    554  1.1  perseant static struct disklabel *
    555  1.1  perseant getdisklabel(s, fd)
    556  1.1  perseant 	const char *s;
    557  1.1  perseant 	int	fd;
    558  1.1  perseant {
    559  1.1  perseant 	static struct disklabel lab;
    560  1.1  perseant 
    561  1.1  perseant 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
    562  1.1  perseant 		if (s == NULL)
    563  1.1  perseant 			return ((struct disklabel *)NULL);
    564  1.1  perseant 		pwarn("ioctl (GCINFO): %s\n", strerror(errno));
    565  1.1  perseant 		errexit("%s: can't read disk label\n", s);
    566  1.1  perseant 	}
    567  1.1  perseant 	return (&lab);
    568  1.1  perseant }
    569