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