Home | History | Annotate | Line # | Download | only in fsck_ffs
setup.c revision 1.37.4.2
      1  1.37.4.2        he /*	$NetBSD: setup.c,v 1.37.4.2 2001/11/25 19:27:02 he Exp $	*/
      2      1.19       cgd 
      3       1.1       cgd /*
      4      1.10   mycroft  * Copyright (c) 1980, 1986, 1993
      5      1.10   mycroft  *	The Regents of the University of California.  All rights reserved.
      6       1.1       cgd  *
      7       1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8       1.1       cgd  * modification, are permitted provided that the following conditions
      9       1.1       cgd  * are met:
     10       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15       1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16       1.1       cgd  *    must display the following acknowledgement:
     17       1.1       cgd  *	This product includes software developed by the University of
     18       1.1       cgd  *	California, Berkeley and its contributors.
     19       1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20       1.1       cgd  *    may be used to endorse or promote products derived from this software
     21       1.1       cgd  *    without specific prior written permission.
     22       1.1       cgd  *
     23       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33       1.1       cgd  * SUCH DAMAGE.
     34       1.1       cgd  */
     35       1.1       cgd 
     36      1.28     lukem #include <sys/cdefs.h>
     37       1.1       cgd #ifndef lint
     38      1.19       cgd #if 0
     39      1.30     lukem static char sccsid[] = "@(#)setup.c	8.10 (Berkeley) 5/9/95";
     40      1.19       cgd #else
     41  1.37.4.2        he __RCSID("$NetBSD: setup.c,v 1.37.4.2 2001/11/25 19:27:02 he Exp $");
     42      1.19       cgd #endif
     43       1.1       cgd #endif /* not lint */
     44       1.1       cgd 
     45       1.1       cgd #include <sys/param.h>
     46       1.8       cgd #include <sys/time.h>
     47       1.1       cgd #include <sys/stat.h>
     48       1.1       cgd #include <sys/ioctl.h>
     49      1.35  christos #define FSTYPENAMES
     50       1.1       cgd #include <sys/disklabel.h>
     51       1.1       cgd #include <sys/file.h>
     52      1.15       cgd 
     53      1.30     lukem #include <ufs/ufs/dinode.h>
     54      1.33    bouyer #include <ufs/ufs/ufs_bswap.h>
     55      1.30     lukem #include <ufs/ffs/fs.h>
     56      1.33    bouyer #include <ufs/ffs/ffs_extern.h>
     57      1.30     lukem 
     58      1.30     lukem #include <ctype.h>
     59      1.30     lukem #include <err.h>
     60       1.1       cgd #include <errno.h>
     61      1.31     lukem #include <stdio.h>
     62      1.31     lukem #include <stdlib.h>
     63       1.1       cgd #include <string.h>
     64      1.26  christos 
     65       1.1       cgd #include "fsck.h"
     66      1.15       cgd #include "extern.h"
     67      1.27  christos #include "fsutil.h"
     68       1.1       cgd 
     69       1.1       cgd #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
     70       1.1       cgd 
     71      1.30     lukem static void badsb __P((int, char *));
     72      1.34   mycroft static int calcsb __P((const char *, int, struct fs *));
     73      1.34   mycroft static struct disklabel *getdisklabel __P((const char *, int));
     74      1.26  christos static int readsb __P((int));
     75       1.1       cgd 
     76      1.30     lukem /*
     77      1.30     lukem  * Read in a superblock finding an alternate if necessary.
     78      1.30     lukem  * Return 1 if successful, 0 if unsuccessful, -1 if filesystem
     79      1.30     lukem  * is already clean (preen mode only).
     80      1.30     lukem  */
     81      1.15       cgd int
     82       1.1       cgd setup(dev)
     83      1.34   mycroft 	const char *dev;
     84       1.1       cgd {
     85       1.1       cgd 	long cg, size, asked, i, j;
     86       1.1       cgd 	long bmapsize;
     87       1.1       cgd 	struct disklabel *lp;
     88      1.10   mycroft 	off_t sizepb;
     89      1.10   mycroft 	struct stat statb;
     90       1.1       cgd 	struct fs proto;
     91      1.21   mycroft 	int doskipclean;
     92      1.24   mycroft 	u_int64_t maxfilesize;
     93       1.1       cgd 
     94       1.1       cgd 	havesb = 0;
     95      1.10   mycroft 	fswritefd = -1;
     96      1.21   mycroft 	doskipclean = skipclean;
     97       1.1       cgd 	if (stat(dev, &statb) < 0) {
     98       1.1       cgd 		printf("Can't stat %s: %s\n", dev, strerror(errno));
     99       1.1       cgd 		return (0);
    100       1.1       cgd 	}
    101      1.14   mycroft 	if (!S_ISCHR(statb.st_mode)) {
    102       1.1       cgd 		pfatal("%s is not a character device", dev);
    103       1.1       cgd 		if (reply("CONTINUE") == 0)
    104       1.1       cgd 			return (0);
    105       1.1       cgd 	}
    106       1.1       cgd 	if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
    107       1.1       cgd 		printf("Can't open %s: %s\n", dev, strerror(errno));
    108       1.1       cgd 		return (0);
    109       1.1       cgd 	}
    110       1.1       cgd 	if (preen == 0)
    111       1.1       cgd 		printf("** %s", dev);
    112       1.1       cgd 	if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
    113       1.1       cgd 		fswritefd = -1;
    114       1.1       cgd 		if (preen)
    115       1.1       cgd 			pfatal("NO WRITE ACCESS");
    116       1.1       cgd 		printf(" (NO WRITE)");
    117       1.1       cgd 	}
    118       1.1       cgd 	if (preen == 0)
    119       1.1       cgd 		printf("\n");
    120       1.1       cgd 	fsmodified = 0;
    121       1.1       cgd 	lfdir = 0;
    122       1.1       cgd 	initbarea(&sblk);
    123       1.1       cgd 	initbarea(&asblk);
    124       1.1       cgd 	sblk.b_un.b_buf = malloc(SBSIZE);
    125      1.33    bouyer 	sblock = malloc(SBSIZE);
    126       1.1       cgd 	asblk.b_un.b_buf = malloc(SBSIZE);
    127      1.33    bouyer 	altsblock = malloc(SBSIZE);
    128      1.33    bouyer 	if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL ||
    129      1.33    bouyer 		sblock == NULL || altsblock == NULL)
    130      1.30     lukem 		errx(EEXIT, "cannot allocate space for superblock");
    131      1.30     lukem 	if ((lp = getdisklabel(NULL, fsreadfd)) != NULL)
    132       1.1       cgd 		dev_bsize = secsize = lp->d_secsize;
    133       1.1       cgd 	else
    134       1.1       cgd 		dev_bsize = secsize = DEV_BSIZE;
    135       1.1       cgd 	/*
    136       1.1       cgd 	 * Read in the superblock, looking for alternates if necessary
    137       1.1       cgd 	 */
    138       1.1       cgd 	if (readsb(1) == 0) {
    139       1.1       cgd 		if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
    140       1.1       cgd 			return(0);
    141       1.1       cgd 		if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
    142       1.1       cgd 			return (0);
    143       1.1       cgd 		for (cg = 0; cg < proto.fs_ncg; cg++) {
    144       1.1       cgd 			bflag = fsbtodb(&proto, cgsblock(&proto, cg));
    145       1.1       cgd 			if (readsb(0) != 0)
    146       1.1       cgd 				break;
    147       1.1       cgd 		}
    148       1.1       cgd 		if (cg >= proto.fs_ncg) {
    149       1.1       cgd 			printf("%s %s\n%s %s\n%s %s\n",
    150       1.1       cgd 				"SEARCH FOR ALTERNATE SUPER-BLOCK",
    151       1.1       cgd 				"FAILED. YOU MUST USE THE",
    152  1.37.4.1        he 				"-b OPTION TO fsck_ffs TO SPECIFY THE",
    153       1.1       cgd 				"LOCATION OF AN ALTERNATE",
    154       1.1       cgd 				"SUPER-BLOCK TO SUPPLY NEEDED",
    155      1.23       cgd 				"INFORMATION; SEE fsck_ffs(8).");
    156       1.1       cgd 			return(0);
    157       1.1       cgd 		}
    158      1.21   mycroft 		doskipclean = 0;
    159       1.1       cgd 		pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
    160       1.1       cgd 	}
    161      1.21   mycroft 	if (debug)
    162      1.33    bouyer 		printf("clean = %d\n", sblock->fs_clean);
    163      1.33    bouyer 	if (doswap)
    164      1.33    bouyer 		doskipclean = 0;
    165      1.33    bouyer 	if (sblock->fs_clean & FS_ISCLEAN) {
    166      1.22       cgd 		if (doskipclean) {
    167      1.22       cgd 			pwarn("%sile system is clean; not checking\n",
    168      1.22       cgd 			    preen ? "f" : "** F");
    169      1.21   mycroft 			return (-1);
    170      1.21   mycroft 		}
    171      1.33    bouyer 		if (!preen && !doswap)
    172      1.21   mycroft 			pwarn("** File system is already clean\n");
    173      1.21   mycroft 	}
    174      1.33    bouyer 	maxfsblock = sblock->fs_size;
    175      1.33    bouyer 	maxino = sblock->fs_ncg * sblock->fs_ipg;
    176      1.33    bouyer 	sizepb = sblock->fs_bsize;
    177      1.33    bouyer 	maxfilesize = sblock->fs_bsize * NDADDR - 1;
    178      1.24   mycroft 	for (i = 0; i < NIADDR; i++) {
    179      1.33    bouyer 		sizepb *= NINDIR(sblock);
    180      1.24   mycroft 		maxfilesize += sizepb;
    181      1.24   mycroft 	}
    182       1.1       cgd 	/*
    183       1.1       cgd 	 * Check and potentially fix certain fields in the super block.
    184       1.1       cgd 	 */
    185      1.33    bouyer 	if (sblock->fs_optim != FS_OPTTIME && sblock->fs_optim != FS_OPTSPACE) {
    186       1.1       cgd 		pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
    187       1.1       cgd 		if (reply("SET TO DEFAULT") == 1) {
    188      1.33    bouyer 			sblock->fs_optim = FS_OPTTIME;
    189       1.1       cgd 			sbdirty();
    190       1.1       cgd 		}
    191       1.1       cgd 	}
    192      1.33    bouyer 	if ((sblock->fs_minfree < 0 || sblock->fs_minfree > 99)) {
    193       1.1       cgd 		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
    194      1.33    bouyer 			sblock->fs_minfree);
    195       1.1       cgd 		if (reply("SET TO DEFAULT") == 1) {
    196      1.33    bouyer 			sblock->fs_minfree = 10;
    197       1.1       cgd 			sbdirty();
    198       1.1       cgd 		}
    199       1.1       cgd 	}
    200      1.33    bouyer 	if (sblock->fs_interleave < 1 ||
    201      1.33    bouyer 	    sblock->fs_interleave > sblock->fs_nsect) {
    202       1.1       cgd 		pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
    203      1.33    bouyer 			sblock->fs_interleave);
    204      1.33    bouyer 		sblock->fs_interleave = 1;
    205       1.1       cgd 		if (preen)
    206       1.1       cgd 			printf(" (FIXED)\n");
    207       1.1       cgd 		if (preen || reply("SET TO DEFAULT") == 1) {
    208       1.1       cgd 			sbdirty();
    209       1.1       cgd 			dirty(&asblk);
    210       1.1       cgd 		}
    211       1.1       cgd 	}
    212      1.33    bouyer 	if (sblock->fs_npsect < sblock->fs_nsect ||
    213      1.33    bouyer 	    sblock->fs_npsect > sblock->fs_nsect*2) {
    214       1.1       cgd 		pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
    215      1.33    bouyer 			sblock->fs_npsect);
    216      1.33    bouyer 		sblock->fs_npsect = sblock->fs_nsect;
    217       1.1       cgd 		if (preen)
    218       1.1       cgd 			printf(" (FIXED)\n");
    219       1.1       cgd 		if (preen || reply("SET TO DEFAULT") == 1) {
    220       1.1       cgd 			sbdirty();
    221       1.1       cgd 			dirty(&asblk);
    222       1.1       cgd 		}
    223       1.1       cgd 	}
    224      1.33    bouyer 	if (sblock->fs_bmask != ~(sblock->fs_bsize - 1)) {
    225      1.28     lukem 		pwarn("INCORRECT BMASK=0x%x IN SUPERBLOCK",
    226      1.33    bouyer 			sblock->fs_bmask);
    227      1.33    bouyer 		sblock->fs_bmask = ~(sblock->fs_bsize - 1);
    228      1.24   mycroft 		if (preen)
    229      1.24   mycroft 			printf(" (FIXED)\n");
    230      1.24   mycroft 		if (preen || reply("FIX") == 1) {
    231      1.24   mycroft 			sbdirty();
    232      1.24   mycroft 			dirty(&asblk);
    233      1.24   mycroft 		}
    234      1.24   mycroft 	}
    235      1.33    bouyer 	if (sblock->fs_fmask != ~(sblock->fs_fsize - 1)) {
    236      1.28     lukem 		pwarn("INCORRECT FMASK=0x%x IN SUPERBLOCK",
    237      1.33    bouyer 			sblock->fs_fmask);
    238      1.33    bouyer 		sblock->fs_fmask = ~(sblock->fs_fsize - 1);
    239      1.24   mycroft 		if (preen)
    240      1.24   mycroft 			printf(" (FIXED)\n");
    241      1.24   mycroft 		if (preen || reply("FIX") == 1) {
    242      1.24   mycroft 			sbdirty();
    243      1.24   mycroft 			dirty(&asblk);
    244      1.24   mycroft 		}
    245      1.24   mycroft 	}
    246      1.33    bouyer 	if (sblock->fs_inodefmt >= FS_44INODEFMT) {
    247      1.33    bouyer 		if (sblock->fs_maxfilesize != maxfilesize) {
    248      1.24   mycroft 			pwarn("INCORRECT MAXFILESIZE=%qd IN SUPERBLOCK",
    249      1.33    bouyer 			    (unsigned long long)sblock->fs_maxfilesize);
    250      1.33    bouyer 			sblock->fs_maxfilesize = maxfilesize;
    251      1.24   mycroft 			if (preen)
    252      1.24   mycroft 				printf(" (FIXED)\n");
    253      1.24   mycroft 			if (preen || reply("FIX") == 1) {
    254      1.24   mycroft 				sbdirty();
    255      1.24   mycroft 				dirty(&asblk);
    256      1.24   mycroft 			}
    257      1.24   mycroft 		}
    258      1.33    bouyer 		if (sblock->fs_maxsymlinklen != MAXSYMLINKLEN) {
    259      1.24   mycroft 			pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK",
    260      1.33    bouyer 				sblock->fs_maxsymlinklen);
    261      1.33    bouyer 			sblock->fs_maxsymlinklen = MAXSYMLINKLEN;
    262      1.24   mycroft 			if (preen)
    263      1.24   mycroft 				printf(" (FIXED)\n");
    264      1.24   mycroft 			if (preen || reply("FIX") == 1) {
    265      1.24   mycroft 				sbdirty();
    266      1.24   mycroft 				dirty(&asblk);
    267      1.24   mycroft 			}
    268      1.24   mycroft 		}
    269      1.33    bouyer 		if (sblock->fs_qbmask != ~sblock->fs_bmask) {
    270      1.25   mycroft 			pwarn("INCORRECT QBMASK=%qx IN SUPERBLOCK",
    271      1.33    bouyer 			    (unsigned long long)sblock->fs_qbmask);
    272      1.33    bouyer 			sblock->fs_qbmask = ~sblock->fs_bmask;
    273      1.24   mycroft 			if (preen)
    274      1.24   mycroft 				printf(" (FIXED)\n");
    275      1.24   mycroft 			if (preen || reply("FIX") == 1) {
    276      1.24   mycroft 				sbdirty();
    277      1.24   mycroft 				dirty(&asblk);
    278      1.24   mycroft 			}
    279      1.24   mycroft 		}
    280      1.33    bouyer 		if (sblock->fs_qfmask != ~sblock->fs_fmask) {
    281      1.25   mycroft 			pwarn("INCORRECT QFMASK=%qx IN SUPERBLOCK",
    282      1.33    bouyer 			    (unsigned long long)sblock->fs_qfmask);
    283      1.33    bouyer 			sblock->fs_qfmask = ~sblock->fs_fmask;
    284      1.24   mycroft 			if (preen)
    285      1.24   mycroft 				printf(" (FIXED)\n");
    286      1.24   mycroft 			if (preen || reply("FIX") == 1) {
    287      1.24   mycroft 				sbdirty();
    288      1.24   mycroft 				dirty(&asblk);
    289      1.24   mycroft 			}
    290      1.24   mycroft 		}
    291      1.10   mycroft 		newinofmt = 1;
    292      1.10   mycroft 	} else {
    293      1.33    bouyer 		sblock->fs_qbmask = ~sblock->fs_bmask;
    294      1.33    bouyer 		sblock->fs_qfmask = ~sblock->fs_fmask;
    295      1.10   mycroft 		newinofmt = 0;
    296      1.10   mycroft 	}
    297      1.10   mycroft 	/*
    298      1.10   mycroft 	 * Convert to new inode format.
    299      1.10   mycroft 	 */
    300      1.33    bouyer 	if (cvtlevel >= 2 && sblock->fs_inodefmt < FS_44INODEFMT) {
    301      1.10   mycroft 		if (preen)
    302      1.10   mycroft 			pwarn("CONVERTING TO NEW INODE FORMAT\n");
    303      1.10   mycroft 		else if (!reply("CONVERT TO NEW INODE FORMAT"))
    304      1.10   mycroft 			return(0);
    305      1.10   mycroft 		doinglevel2++;
    306      1.33    bouyer 		sblock->fs_inodefmt = FS_44INODEFMT;
    307      1.33    bouyer 		sblock->fs_maxfilesize = maxfilesize;
    308      1.33    bouyer 		sblock->fs_maxsymlinklen = MAXSYMLINKLEN;
    309      1.33    bouyer 		sblock->fs_qbmask = ~sblock->fs_bmask;
    310      1.33    bouyer 		sblock->fs_qfmask = ~sblock->fs_fmask;
    311      1.10   mycroft 		sbdirty();
    312      1.10   mycroft 		dirty(&asblk);
    313      1.10   mycroft 	}
    314      1.10   mycroft 	/*
    315      1.10   mycroft 	 * Convert to new cylinder group format.
    316      1.10   mycroft 	 */
    317      1.33    bouyer 	if (cvtlevel >= 1 && sblock->fs_postblformat == FS_42POSTBLFMT) {
    318      1.10   mycroft 		if (preen)
    319      1.10   mycroft 			pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n");
    320      1.10   mycroft 		else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT"))
    321      1.10   mycroft 			return(0);
    322      1.10   mycroft 		doinglevel1++;
    323      1.33    bouyer 		sblock->fs_postblformat = FS_DYNAMICPOSTBLFMT;
    324      1.33    bouyer 		sblock->fs_nrpos = 8;
    325      1.33    bouyer 		sblock->fs_postbloff =
    326      1.33    bouyer 		    (char *)(&sblock->fs_opostbl[0][0]) -
    327      1.33    bouyer 		    (char *)(&sblock->fs_firstfield);
    328      1.33    bouyer 		sblock->fs_rotbloff = &sblock->fs_space[0] -
    329      1.33    bouyer 		    (u_char *)(&sblock->fs_firstfield);
    330      1.33    bouyer 		sblock->fs_cgsize =
    331      1.33    bouyer 			fragroundup(sblock, CGSIZE(sblock));
    332      1.10   mycroft 		sbdirty();
    333      1.10   mycroft 		dirty(&asblk);
    334       1.1       cgd 	}
    335      1.11        ws 	if (asblk.b_dirty && !bflag) {
    336      1.33    bouyer 		memmove((struct fs*)sblk.b_un.b_fs, sblock, SBSIZE);
    337      1.33    bouyer 		if (needswap)
    338  1.37.4.2        he 			ffs_sb_swap(sblock, (struct fs*)sblk.b_un.b_fs);
    339      1.33    bouyer 		memmove(asblk.b_un.b_fs, sblk.b_un.b_fs, (size_t)sblock->fs_sbsize);
    340       1.1       cgd 		flush(fswritefd, &asblk);
    341       1.1       cgd 	}
    342       1.1       cgd 	/*
    343       1.1       cgd 	 * read in the summary info.
    344       1.1       cgd 	 */
    345       1.1       cgd 	asked = 0;
    346      1.33    bouyer 	for (i = 0, j = 0; i < sblock->fs_cssize; i += sblock->fs_bsize, j++) {
    347      1.33    bouyer 		size = sblock->fs_cssize - i < sblock->fs_bsize ?
    348      1.33    bouyer 		    sblock->fs_cssize - i : sblock->fs_bsize;
    349      1.33    bouyer 		sblock->fs_csp[j] = (struct csum *)calloc(1, (unsigned)size);
    350      1.33    bouyer 		if (bread(fsreadfd, (char *)sblock->fs_csp[j],
    351      1.33    bouyer 		    fsbtodb(sblock, sblock->fs_csaddr + j * sblock->fs_frag),
    352       1.1       cgd 		    size) != 0 && !asked) {
    353       1.1       cgd 			pfatal("BAD SUMMARY INFORMATION");
    354      1.37      fvdl 			if (reply("CONTINUE") == 0) {
    355      1.37      fvdl 				markclean = 0;
    356      1.30     lukem 				exit(EEXIT);
    357      1.37      fvdl 			}
    358       1.1       cgd 			asked++;
    359       1.1       cgd 		}
    360      1.33    bouyer 		/*
    361      1.37      fvdl 		 * The following assumes that struct csum is made of
    362      1.37      fvdl 		 * u_int32_t
    363      1.33    bouyer 		 */
    364      1.33    bouyer 		if (doswap) {
    365      1.33    bouyer 			int k;
    366      1.33    bouyer 			u_int32_t *cd = (u_int32_t *)sblock->fs_csp[j];
    367      1.37      fvdl 
    368      1.33    bouyer 			for (k = 0; k < size / sizeof(u_int32_t); k++)
    369      1.33    bouyer 				cd[k] = bswap32(cd[k]);
    370      1.33    bouyer 			bwrite(fswritefd, (char *)sblock->fs_csp[j],
    371      1.37      fvdl 			    fsbtodb(sblock,
    372      1.37      fvdl 				sblock->fs_csaddr + j * sblock->fs_frag),
    373      1.37      fvdl 			    size);
    374      1.33    bouyer 		}
    375      1.33    bouyer 		if (needswap) {
    376      1.33    bouyer 			int k;
    377      1.33    bouyer 			u_int32_t *cd = (u_int32_t *)sblock->fs_csp[j];
    378      1.37      fvdl 
    379      1.33    bouyer 			for (k = 0; k < size / sizeof(u_int32_t); k++)
    380      1.33    bouyer 				cd[k] = bswap32(cd[k]);
    381      1.33    bouyer 		}
    382       1.1       cgd 	}
    383       1.1       cgd 	/*
    384       1.1       cgd 	 * allocate and initialize the necessary maps
    385       1.1       cgd 	 */
    386      1.20       cgd 	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
    387       1.1       cgd 	blockmap = calloc((unsigned)bmapsize, sizeof (char));
    388       1.1       cgd 	if (blockmap == NULL) {
    389       1.1       cgd 		printf("cannot alloc %u bytes for blockmap\n",
    390       1.1       cgd 		    (unsigned)bmapsize);
    391      1.15       cgd 		goto badsblabel;
    392       1.1       cgd 	}
    393       1.1       cgd 	statemap = calloc((unsigned)(maxino + 1), sizeof(char));
    394       1.1       cgd 	if (statemap == NULL) {
    395       1.1       cgd 		printf("cannot alloc %u bytes for statemap\n",
    396       1.1       cgd 		    (unsigned)(maxino + 1));
    397      1.15       cgd 		goto badsblabel;
    398       1.1       cgd 	}
    399      1.10   mycroft 	typemap = calloc((unsigned)(maxino + 1), sizeof(char));
    400      1.10   mycroft 	if (typemap == NULL) {
    401      1.10   mycroft 		printf("cannot alloc %u bytes for typemap\n",
    402      1.10   mycroft 		    (unsigned)(maxino + 1));
    403      1.15       cgd 		goto badsblabel;
    404      1.10   mycroft 	}
    405      1.20       cgd 	lncntp = (int16_t *)calloc((unsigned)(maxino + 1), sizeof(int16_t));
    406       1.1       cgd 	if (lncntp == NULL) {
    407       1.1       cgd 		printf("cannot alloc %u bytes for lncntp\n",
    408      1.29       mrg 		    (unsigned)((maxino + 1) * sizeof(int16_t)));
    409      1.15       cgd 		goto badsblabel;
    410       1.1       cgd 	}
    411  1.37.4.1        he 	/*
    412  1.37.4.1        he 	 * cs_ndir may be inaccurate, particularly if we're using the -b
    413  1.37.4.1        he 	 * option, so set a minimum to prevent bogus subdirectory reconnects
    414  1.37.4.1        he 	 * and really inefficient directory scans.
    415  1.37.4.1        he 	 * Also set a maximum in case the value is too large.
    416  1.37.4.1        he 	 */
    417      1.33    bouyer 	numdirs = sblock->fs_cstotal.cs_ndir;
    418  1.37.4.1        he 	if (numdirs < 1024)
    419  1.37.4.1        he 		numdirs = 1024;
    420  1.37.4.1        he 	if (numdirs > maxino + 1)
    421  1.37.4.1        he 		numdirs = maxino + 1;
    422       1.1       cgd 	inplast = 0;
    423       1.1       cgd 	listmax = numdirs + 10;
    424       1.1       cgd 	inpsort = (struct inoinfo **)calloc((unsigned)listmax,
    425       1.1       cgd 	    sizeof(struct inoinfo *));
    426       1.1       cgd 	inphead = (struct inoinfo **)calloc((unsigned)numdirs,
    427       1.1       cgd 	    sizeof(struct inoinfo *));
    428       1.1       cgd 	if (inpsort == NULL || inphead == NULL) {
    429       1.1       cgd 		printf("cannot alloc %u bytes for inphead\n",
    430      1.29       mrg 		    (unsigned)(numdirs * sizeof(struct inoinfo *)));
    431      1.15       cgd 		goto badsblabel;
    432       1.1       cgd 	}
    433      1.37      fvdl 	cgrp = malloc(sblock->fs_cgsize);
    434      1.37      fvdl 	if (cgrp == NULL) {
    435      1.37      fvdl 		printf("cannot alloc %u bytes for cylinder group\n",
    436      1.37      fvdl 		    sblock->fs_cgsize);
    437      1.37      fvdl 		goto badsblabel;
    438      1.37      fvdl 	}
    439       1.1       cgd 	bufinit();
    440      1.37      fvdl 	if (sblock->fs_flags & FS_DOSOFTDEP)
    441      1.37      fvdl 		usedsoftdep = 1;
    442      1.37      fvdl 	else
    443      1.37      fvdl 		usedsoftdep = 0;
    444       1.1       cgd 	return (1);
    445       1.1       cgd 
    446      1.15       cgd badsblabel:
    447      1.33    bouyer 	markclean=0;
    448      1.33    bouyer 	ckfini();
    449       1.1       cgd 	return (0);
    450       1.1       cgd }
    451       1.1       cgd 
    452       1.1       cgd /*
    453       1.1       cgd  * Read in the super block and its summary info.
    454       1.1       cgd  */
    455      1.26  christos static int
    456       1.1       cgd readsb(listerr)
    457       1.1       cgd 	int listerr;
    458       1.1       cgd {
    459      1.30     lukem 	ufs_daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
    460      1.33    bouyer 	struct fs *fs;
    461       1.1       cgd 
    462      1.33    bouyer 	if (bread(fsreadfd, (char *)sblk.b_un.b_fs, super, (long)SBSIZE) != 0)
    463       1.1       cgd 		return (0);
    464       1.1       cgd 	sblk.b_bno = super;
    465       1.1       cgd 	sblk.b_size = SBSIZE;
    466      1.33    bouyer 
    467      1.33    bouyer 	fs = sblk.b_un.b_fs;
    468      1.33    bouyer 	/* auto detect byte order */
    469      1.33    bouyer 	if( fs->fs_magic == FS_MAGIC) {
    470      1.33    bouyer 			if (endian == 0 || BYTE_ORDER == endian) {
    471      1.33    bouyer 				needswap = 0;
    472      1.33    bouyer 				doswap = do_blkswap = do_dirswap = 0;
    473      1.33    bouyer 			} else {
    474      1.33    bouyer 				needswap = 1;
    475      1.33    bouyer 				doswap = do_blkswap = do_dirswap = 1;
    476      1.33    bouyer 			}
    477      1.33    bouyer 	} else if (fs->fs_magic == bswap32(FS_MAGIC)) {
    478      1.33    bouyer 		if (endian == 0 || BYTE_ORDER != endian) {
    479      1.33    bouyer 			needswap = 1;
    480      1.33    bouyer 			doswap = do_blkswap = do_dirswap = 0;
    481      1.33    bouyer 		} else {
    482      1.33    bouyer 			needswap = 0;
    483      1.33    bouyer 			doswap = do_blkswap = do_dirswap = 1;
    484      1.33    bouyer 		}
    485      1.33    bouyer 	} else {
    486      1.33    bouyer 		badsb(listerr, "MAGIC NUMBER WRONG");
    487      1.33    bouyer 		return (0);
    488      1.33    bouyer 	}
    489      1.33    bouyer 	if (doswap) {
    490      1.33    bouyer 		if (preen)
    491      1.33    bouyer 			errx(EEXIT, "incompatible options -B and -p");
    492      1.33    bouyer 		if (nflag)
    493      1.33    bouyer 			errx(EEXIT, "incompatible options -B and -n");
    494      1.33    bouyer 		if (endian == LITTLE_ENDIAN) {
    495      1.36        is 			if (!reply("CONVERT TO LITTLE ENDIAN"))
    496      1.33    bouyer 				return 0;
    497      1.33    bouyer 		} else if (endian == BIG_ENDIAN) {
    498      1.36        is 			if (!reply("CONVERT TO BIG ENDIAN"))
    499      1.33    bouyer 				return 0;
    500      1.33    bouyer 		} else
    501      1.33    bouyer 			pfatal("INTERNAL ERROR: unknown endian");
    502      1.33    bouyer 	}
    503      1.33    bouyer 	if (needswap)
    504      1.33    bouyer 		printf("** Swapped byte order\n");
    505      1.33    bouyer 	/* swap SB byte order if asked */
    506      1.33    bouyer 	if (doswap)
    507  1.37.4.2        he 		ffs_sb_swap(sblk.b_un.b_fs, sblk.b_un.b_fs);
    508      1.33    bouyer 
    509      1.33    bouyer 	memmove(sblock, sblk.b_un.b_fs, SBSIZE);
    510      1.33    bouyer 	if (needswap)
    511  1.37.4.2        he 		ffs_sb_swap(sblk.b_un.b_fs, sblock);
    512      1.33    bouyer 
    513       1.1       cgd 	/*
    514       1.1       cgd 	 * run a few consistency checks of the super block
    515       1.1       cgd 	 */
    516      1.33    bouyer 	if (sblock->fs_ncg < 1)
    517       1.1       cgd 		{ badsb(listerr, "NCG OUT OF RANGE"); return (0); }
    518      1.33    bouyer 	if (sblock->fs_cpg < 1)
    519       1.1       cgd 		{ badsb(listerr, "CPG OUT OF RANGE"); return (0); }
    520      1.33    bouyer 	if (sblock->fs_ncg * sblock->fs_cpg < sblock->fs_ncyl ||
    521      1.33    bouyer 	    (sblock->fs_ncg - 1) * sblock->fs_cpg >= sblock->fs_ncyl)
    522       1.1       cgd 		{ badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); }
    523      1.33    bouyer 	if (sblock->fs_sbsize > SBSIZE)
    524       1.1       cgd 		{ badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
    525       1.1       cgd 	/*
    526       1.1       cgd 	 * Compute block size that the filesystem is based on,
    527       1.1       cgd 	 * according to fsbtodb, and adjust superblock block number
    528       1.1       cgd 	 * so we can tell if this is an alternate later.
    529       1.1       cgd 	 */
    530       1.1       cgd 	super *= dev_bsize;
    531      1.33    bouyer 	dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
    532       1.1       cgd 	sblk.b_bno = super / dev_bsize;
    533      1.33    bouyer 
    534      1.10   mycroft 	if (bflag) {
    535      1.10   mycroft 		havesb = 1;
    536      1.10   mycroft 		return (1);
    537      1.10   mycroft 	}
    538       1.1       cgd 	/*
    539       1.1       cgd 	 * Set all possible fields that could differ, then do check
    540       1.1       cgd 	 * of whole super block against an alternate super block.
    541       1.1       cgd 	 * When an alternate super-block is specified this check is skipped.
    542       1.1       cgd 	 */
    543      1.33    bouyer 	getblk(&asblk, cgsblock(sblock, sblock->fs_ncg - 1), sblock->fs_sbsize);
    544       1.1       cgd 	if (asblk.b_errs)
    545       1.1       cgd 		return (0);
    546      1.33    bouyer 	/* swap SB byte order if asked */
    547      1.33    bouyer 	if (doswap)
    548  1.37.4.2        he 		ffs_sb_swap(asblk.b_un.b_fs, asblk.b_un.b_fs);
    549      1.33    bouyer 
    550      1.33    bouyer 	memmove(altsblock, asblk.b_un.b_fs, sblock->fs_sbsize);
    551      1.33    bouyer 	if (needswap)
    552  1.37.4.2        he 		ffs_sb_swap(asblk.b_un.b_fs, altsblock);
    553  1.37.4.1        he 	if (cmpsblks(sblock, altsblock)) {
    554      1.18   mycroft 		if (debug) {
    555      1.18   mycroft 			long *nlp, *olp, *endlp;
    556      1.18   mycroft 
    557      1.18   mycroft 			printf("superblock mismatches\n");
    558      1.33    bouyer 			nlp = (long *)altsblock;
    559      1.33    bouyer 			olp = (long *)sblock;
    560      1.33    bouyer 			endlp = olp + (sblock->fs_sbsize / sizeof *olp);
    561      1.18   mycroft 			for ( ; olp < endlp; olp++, nlp++) {
    562      1.18   mycroft 				if (*olp == *nlp)
    563      1.18   mycroft 					continue;
    564      1.33    bouyer 				printf("offset %ld, original %lx, alternate %lx\n",
    565      1.33    bouyer 				    (long)(olp - (long *)sblock), *olp, *nlp);
    566      1.18   mycroft 			}
    567      1.18   mycroft 		}
    568       1.1       cgd 		badsb(listerr,
    569       1.1       cgd 		"VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
    570       1.1       cgd 		return (0);
    571       1.1       cgd 	}
    572      1.33    bouyer 	/* Now we know the SB is valid, we can write it back if needed */
    573      1.33    bouyer 	if (doswap) {
    574      1.33    bouyer 		sbdirty();
    575      1.33    bouyer 		dirty(&asblk);
    576      1.33    bouyer 	}
    577       1.1       cgd 	havesb = 1;
    578       1.1       cgd 	return (1);
    579  1.37.4.1        he }
    580  1.37.4.1        he 
    581  1.37.4.1        he int
    582  1.37.4.1        he cmpsblks(const struct fs *sb, struct fs *asb)
    583  1.37.4.1        he {
    584  1.37.4.1        he 
    585  1.37.4.1        he 	asb->fs_firstfield = sb->fs_firstfield;
    586  1.37.4.1        he 	asb->fs_fscktime = sb->fs_fscktime;
    587  1.37.4.1        he 	asb->fs_unused_1 = sb->fs_unused_1;
    588  1.37.4.1        he 	asb->fs_time = sb->fs_time;
    589  1.37.4.1        he 	asb->fs_cstotal = sb->fs_cstotal;
    590  1.37.4.1        he 	asb->fs_cgrotor = sb->fs_cgrotor;
    591  1.37.4.1        he 	asb->fs_fmod = sb->fs_fmod;
    592  1.37.4.1        he 	asb->fs_clean = sb->fs_clean;
    593  1.37.4.1        he 	asb->fs_ronly = sb->fs_ronly;
    594  1.37.4.1        he 	asb->fs_flags = sb->fs_flags;
    595  1.37.4.1        he 	asb->fs_maxcontig = sb->fs_maxcontig;
    596  1.37.4.1        he 	asb->fs_minfree = sb->fs_minfree;
    597  1.37.4.1        he 	asb->fs_optim = sb->fs_optim;
    598  1.37.4.1        he 	asb->fs_rotdelay = sb->fs_rotdelay;
    599  1.37.4.1        he 	asb->fs_maxbpg = sb->fs_maxbpg;
    600  1.37.4.1        he 	memmove(asb->fs_csp, sb->fs_csp, sizeof sb->fs_csp);
    601  1.37.4.1        he 	asb->fs_maxcluster = sb->fs_maxcluster;
    602  1.37.4.1        he 	memmove(asb->fs_fsmnt, sb->fs_fsmnt, sizeof sb->fs_fsmnt);
    603  1.37.4.1        he 	memmove(asb->fs_sparecon,
    604  1.37.4.1        he 		sb->fs_sparecon, sizeof sb->fs_sparecon);
    605  1.37.4.1        he 	/*
    606  1.37.4.1        he 	 * The following should not have to be copied.
    607  1.37.4.1        he 	 */
    608  1.37.4.1        he 	asb->fs_fsbtodb = sb->fs_fsbtodb;
    609  1.37.4.1        he 	asb->fs_interleave = sb->fs_interleave;
    610  1.37.4.1        he 	asb->fs_npsect = sb->fs_npsect;
    611  1.37.4.1        he 	asb->fs_nrpos = sb->fs_nrpos;
    612  1.37.4.1        he 	asb->fs_state = sb->fs_state;
    613  1.37.4.1        he 	asb->fs_qbmask = sb->fs_qbmask;
    614  1.37.4.1        he 	asb->fs_qfmask = sb->fs_qfmask;
    615  1.37.4.1        he 	asb->fs_state = sb->fs_state;
    616  1.37.4.1        he 	asb->fs_maxfilesize = sb->fs_maxfilesize;
    617  1.37.4.1        he 
    618  1.37.4.1        he 	return (memcmp(sb, asb, (int)sb->fs_sbsize));
    619       1.1       cgd }
    620       1.1       cgd 
    621      1.30     lukem static void
    622       1.1       cgd badsb(listerr, s)
    623       1.1       cgd 	int listerr;
    624       1.1       cgd 	char *s;
    625       1.1       cgd {
    626       1.1       cgd 
    627       1.1       cgd 	if (!listerr)
    628       1.1       cgd 		return;
    629       1.1       cgd 	if (preen)
    630      1.26  christos 		printf("%s: ", cdevname());
    631       1.1       cgd 	pfatal("BAD SUPER BLOCK: %s\n", s);
    632       1.1       cgd }
    633       1.1       cgd 
    634       1.1       cgd /*
    635       1.1       cgd  * Calculate a prototype superblock based on information in the disk label.
    636       1.1       cgd  * When done the cgsblock macro can be calculated and the fs_ncg field
    637       1.1       cgd  * can be used. Do NOT attempt to use other macros without verifying that
    638       1.1       cgd  * their needed information is available!
    639       1.1       cgd  */
    640      1.30     lukem static int
    641       1.1       cgd calcsb(dev, devfd, fs)
    642      1.34   mycroft 	const char *dev;
    643       1.1       cgd 	int devfd;
    644      1.28     lukem 	struct fs *fs;
    645       1.1       cgd {
    646      1.28     lukem 	struct disklabel *lp;
    647      1.28     lukem 	struct partition *pp;
    648      1.28     lukem 	char *cp;
    649       1.1       cgd 	int i;
    650       1.1       cgd 
    651      1.12   mycroft 	cp = strchr(dev, '\0') - 1;
    652      1.26  christos 	if ((cp == (char *)-1 || (*cp < 'a' || *cp > 'h')) && !isdigit(*cp)) {
    653       1.1       cgd 		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
    654       1.1       cgd 		return (0);
    655       1.1       cgd 	}
    656       1.1       cgd 	lp = getdisklabel(dev, devfd);
    657       1.1       cgd 	if (isdigit(*cp))
    658       1.1       cgd 		pp = &lp->d_partitions[0];
    659       1.1       cgd 	else
    660       1.1       cgd 		pp = &lp->d_partitions[*cp - 'a'];
    661       1.1       cgd 	if (pp->p_fstype != FS_BSDFFS) {
    662       1.1       cgd 		pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
    663       1.1       cgd 			dev, pp->p_fstype < FSMAXTYPES ?
    664       1.1       cgd 			fstypenames[pp->p_fstype] : "unknown");
    665       1.1       cgd 		return (0);
    666       1.1       cgd 	}
    667      1.33    bouyer 	/* avoid divide by 0 */
    668      1.33    bouyer 	if (pp->p_fsize == 0 || pp->p_frag == 0)
    669      1.33    bouyer 		return (0);
    670      1.12   mycroft 	memset(fs, 0, sizeof(struct fs));
    671       1.1       cgd 	fs->fs_fsize = pp->p_fsize;
    672       1.1       cgd 	fs->fs_frag = pp->p_frag;
    673       1.1       cgd 	fs->fs_cpg = pp->p_cpg;
    674       1.1       cgd 	fs->fs_size = pp->p_size;
    675       1.1       cgd 	fs->fs_ntrak = lp->d_ntracks;
    676       1.1       cgd 	fs->fs_nsect = lp->d_nsectors;
    677       1.1       cgd 	fs->fs_spc = lp->d_secpercyl;
    678       1.1       cgd 	fs->fs_nspf = fs->fs_fsize / lp->d_secsize;
    679       1.1       cgd 	fs->fs_sblkno = roundup(
    680       1.1       cgd 		howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize),
    681       1.1       cgd 		fs->fs_frag);
    682       1.1       cgd 	fs->fs_cgmask = 0xffffffff;
    683       1.1       cgd 	for (i = fs->fs_ntrak; i > 1; i >>= 1)
    684       1.1       cgd 		fs->fs_cgmask <<= 1;
    685       1.1       cgd 	if (!POWEROF2(fs->fs_ntrak))
    686       1.1       cgd 		fs->fs_cgmask <<= 1;
    687       1.1       cgd 	fs->fs_cgoffset = roundup(
    688       1.1       cgd 		howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag);
    689       1.1       cgd 	fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs);
    690       1.1       cgd 	fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg);
    691       1.1       cgd 	for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1)
    692       1.1       cgd 		fs->fs_fsbtodb++;
    693       1.1       cgd 	dev_bsize = lp->d_secsize;
    694       1.1       cgd 	return (1);
    695       1.1       cgd }
    696       1.1       cgd 
    697      1.26  christos static struct disklabel *
    698       1.1       cgd getdisklabel(s, fd)
    699      1.34   mycroft 	const char *s;
    700       1.1       cgd 	int	fd;
    701       1.1       cgd {
    702       1.1       cgd 	static struct disklabel lab;
    703       1.1       cgd 
    704       1.1       cgd 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
    705       1.1       cgd 		if (s == NULL)
    706       1.1       cgd 			return ((struct disklabel *)NULL);
    707       1.1       cgd 		pwarn("ioctl (GCINFO): %s\n", strerror(errno));
    708      1.30     lukem 		errx(EEXIT, "%s: can't read disk label", s);
    709       1.1       cgd 	}
    710       1.1       cgd 	return (&lab);
    711       1.1       cgd }
    712