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