Home | History | Annotate | Line # | Download | only in fsck_ffs
setup.c revision 1.65
      1 /*	$NetBSD: setup.c,v 1.65 2004/01/09 19:12:31 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. 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.65 2004/01/09 19:12:31 dbj 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 __P((int, char *));
     69 static int calcsb __P((const char *, int, struct fs *));
     70 static struct disklabel *getdisklabel __P((const char *, int));
     71 static struct partition *getdisklabelpart __P((const char *, struct disklabel *));
     72 static int readsb __P((int));
     73 static int readappleufs __P((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 FFSv2 SUPERBLOCK\n");
    189 		else if (!reply("CONVERT TO FFSv2 SUPERBLOCK"))
    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, (long)APPLEUFS_LABEL_SIZE) != 0)
    490 		return 0;
    491 	appleufsblk.b_bno = label;
    492 	appleufsblk.b_size = APPLEUFS_LABEL_SIZE;
    493 
    494 	appleufs = appleufsblk.b_un.b_appleufs;
    495 
    496 	if (ntohl(appleufs->ul_magic) != APPLEUFS_LABEL_MAGIC) {
    497 		if (!isappleufs) {
    498 			return 0;
    499 		} else {
    500 			pfatal("MISSING APPLEUFS VOLUME LABEL\n");
    501 			if (reply("FIX") == 0) {
    502 				return 1;
    503 			}
    504 			ffs_appleufs_set(appleufs, NULL, -1, 0);
    505 			appleufsdirty();
    506 		}
    507 	}
    508 
    509 	if (ntohl(appleufs->ul_version) != APPLEUFS_LABEL_VERSION) {
    510 		pwarn("INCORRECT APPLE UFS VERSION NUMBER (%d should be %d)",
    511 			ntohl(appleufs->ul_version),APPLEUFS_LABEL_VERSION);
    512 		if (preen) {
    513 			printf(" (CORRECTED)\n");
    514 		}
    515 		if (preen || reply("CORRECT")) {
    516 			appleufs->ul_version = htonl(APPLEUFS_LABEL_VERSION);
    517 			appleufsdirty();
    518 		}
    519 	}
    520 
    521 	if (ntohs(appleufs->ul_namelen) > APPLEUFS_MAX_LABEL_NAME) {
    522 		pwarn("APPLE UFS LABEL NAME TOO LONG");
    523 		if (preen) {
    524 			printf(" (TRUNCATED)\n");
    525 		}
    526 		if (preen || reply("TRUNCATE")) {
    527 			appleufs->ul_namelen = htons(APPLEUFS_MAX_LABEL_NAME);
    528 			appleufsdirty();
    529 		}
    530 	}
    531 
    532 	if (ntohs(appleufs->ul_namelen) == 0) {
    533 		pwarn("MISSING APPLE UFS LABEL NAME");
    534 		if (preen) {
    535 			printf(" (FIXED)\n");
    536 		}
    537 		if (preen || reply("FIX")) {
    538 			ffs_appleufs_set(appleufs, NULL, -1, 0);
    539 			appleufsdirty();
    540 		}
    541 	}
    542 
    543 	/* Scan name for first illegal character */
    544 	for (i=0;i<ntohs(appleufs->ul_namelen);i++) {
    545 		if ((appleufs->ul_name[i] == '\0') ||
    546 			(appleufs->ul_name[i] == ':') ||
    547 			(appleufs->ul_name[i] == '/')) {
    548 			pwarn("APPLE UFS LABEL NAME CONTAINS ILLEGAL CHARACTER");
    549 			if (preen) {
    550 				printf(" (TRUNCATED)\n");
    551 			}
    552 			if (preen || reply("TRUNCATE")) {
    553 				appleufs->ul_namelen = i+1;
    554 				appleufsdirty();
    555 			}
    556 			break;
    557 		}
    558 	}
    559 
    560 	/* Check the checksum last, because if anything else was wrong,
    561 	 * then the checksum gets reset anyway.
    562 	 */
    563 	appleufs->ul_checksum = 0;
    564 	appleufs->ul_checksum = ffs_appleufs_cksum(appleufs);
    565 	if (appleufsblk.b_un.b_appleufs->ul_checksum != appleufs->ul_checksum) {
    566 		pwarn("INVALID APPLE UFS CHECKSUM (%#04x should be %#04x)",
    567 			appleufsblk.b_un.b_appleufs->ul_checksum, appleufs->ul_checksum);
    568 		if (preen) {
    569 			printf(" (CORRECTED)\n");
    570 		}
    571 		if (preen || reply("CORRECT")) {
    572 			appleufsdirty();
    573 		} else {
    574 			/* put the incorrect checksum back in place */
    575 			appleufs->ul_checksum = appleufsblk.b_un.b_appleufs->ul_checksum;
    576 		}
    577 	}
    578 	return 1;
    579 }
    580 
    581 /*
    582  * Detect byte order. Return 0 if valid magic found, -1 otherwise.
    583  */
    584 static int
    585 detect_byteorder(struct fs *fs)
    586 {
    587 	if (fs->fs_magic == FS_UFS1_MAGIC || fs->fs_magic == FS_UFS2_MAGIC) {
    588 		if (endian == 0 || BYTE_ORDER == endian) {
    589 			needswap = 0;
    590 			doswap = do_blkswap = do_dirswap = 0;
    591 		} else {
    592 			needswap = 1;
    593 			doswap = do_blkswap = do_dirswap = 1;
    594 		}
    595 		return 0;
    596 	} else if (fs->fs_magic == bswap32(FS_UFS1_MAGIC) ||
    597 		   fs->fs_magic == bswap32(FS_UFS2_MAGIC)) {
    598 		if (endian == 0 || BYTE_ORDER != endian) {
    599 			needswap = 1;
    600 			doswap = do_blkswap = do_dirswap = 0;
    601 		} else {
    602 			needswap = 0;
    603 			doswap = do_blkswap = do_dirswap = 1;
    604 		}
    605 		return 0;
    606 	}
    607 	return -1;
    608 }
    609 
    610 /*
    611  * Possible superblock locations ordered from most to least likely.
    612  */
    613 static off_t sblock_try[] = SBLOCKSEARCH;
    614 
    615 /*
    616  * Read in the super block and its summary info.
    617  */
    618 static int
    619 readsb(listerr)
    620 	int listerr;
    621 {
    622 	daddr_t super;
    623 	struct fs *fs;
    624 	int i;
    625 
    626 	if (bflag) {
    627 		super = bflag;
    628 		if (bread(fsreadfd, (char *)sblk.b_un.b_fs, super,
    629 		    (long)SBLOCKSIZE) != 0)
    630 			return (0);
    631 		fs = sblk.b_un.b_fs;
    632 		if (detect_byteorder(fs) < 0) {
    633 			badsb(listerr, "MAGIC NUMBER WRONG");
    634 			return (0);
    635 		}
    636 	} else {
    637 		for (i = 0; sblock_try[i] != -1; i++) {
    638 			super = sblock_try[i] / dev_bsize;
    639 			if (bread(fsreadfd, (char *)sblk.b_un.b_fs,
    640 			    super, (long)SBLOCKSIZE) != 0)
    641 				continue;
    642 			fs = sblk.b_un.b_fs;
    643 			if (detect_byteorder(fs) == 0)
    644 				break;
    645 		}
    646 		if (sblock_try[i] == -1) {
    647 			badsb(listerr, "CAN'T FIND SUPERBLOCK");
    648 			return (0);
    649 		}
    650 	}
    651 	if (doswap) {
    652 		if (preen)
    653 			errx(EEXIT, "incompatible options -B and -p");
    654 		if (nflag)
    655 			errx(EEXIT, "incompatible options -B and -n");
    656 		if (endian == LITTLE_ENDIAN) {
    657 			if (!reply("CONVERT TO LITTLE ENDIAN"))
    658 				return 0;
    659 		} else if (endian == BIG_ENDIAN) {
    660 			if (!reply("CONVERT TO BIG ENDIAN"))
    661 				return 0;
    662 		} else
    663 			pfatal("INTERNAL ERROR: unknown endian");
    664 	}
    665 	if (needswap)
    666 		pwarn("** Swapped byte order\n");
    667 	/* swap SB byte order if asked */
    668 	if (doswap)
    669 		ffs_sb_swap(sblk.b_un.b_fs, sblk.b_un.b_fs);
    670 
    671 	memmove(sblock, sblk.b_un.b_fs, SBLOCKSIZE);
    672 	if (needswap)
    673 		ffs_sb_swap(sblk.b_un.b_fs, sblock);
    674 
    675 	is_ufs2 = sblock->fs_magic == FS_UFS2_MAGIC;
    676 
    677 	/*
    678 	 * run a few consistency checks of the super block
    679 	 */
    680 	if (sblock->fs_sbsize > SBLOCKSIZE)
    681 		{ badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
    682 	/*
    683 	 * Compute block size that the filesystem is based on,
    684 	 * according to fsbtodb, and adjust superblock block number
    685 	 * so we can tell if this is an alternate later.
    686 	 */
    687 	super *= dev_bsize;
    688 	dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
    689 	sblk.b_bno = super / dev_bsize;
    690 	sblk.b_size = SBLOCKSIZE;
    691 	if (bflag)
    692 		goto out;
    693 	/*
    694 	 * Set all possible fields that could differ, then do check
    695 	 * of whole super block against an alternate super block->
    696 	 * When an alternate super-block is specified this check is skipped.
    697 	 */
    698 	getblk(&asblk, cgsblock(sblock, sblock->fs_ncg - 1), sblock->fs_sbsize);
    699 	if (asblk.b_errs)
    700 		return (0);
    701 	/* swap SB byte order if asked */
    702 	if (doswap)
    703 		ffs_sb_swap(asblk.b_un.b_fs, asblk.b_un.b_fs);
    704 
    705 	memmove(altsblock, asblk.b_un.b_fs, sblock->fs_sbsize);
    706 	if (needswap)
    707 		ffs_sb_swap(asblk.b_un.b_fs, altsblock);
    708 	if (cmpsblks(sblock, altsblock)) {
    709 		if (debug) {
    710 			uint32_t *nlp, *olp, *endlp;
    711 
    712 			printf("superblock mismatches\n");
    713 			nlp = (uint32_t *)altsblock;
    714 			olp = (uint32_t *)sblock;
    715 			endlp = olp + (sblock->fs_sbsize / sizeof *olp);
    716 			for ( ; olp < endlp; olp++, nlp++) {
    717 				if (*olp == *nlp)
    718 					continue;
    719 				printf("offset %#x, original 0x%08x, alternate 0x%08x\n",
    720 				    (uint8_t *)olp-(uint8_t *)sblock, *olp, *nlp);
    721 			}
    722 		}
    723 		badsb(listerr,
    724 		"VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
    725 		return (0);
    726 	}
    727 out:
    728 
    729 	sb_oldfscompat_read(sblock, &sblocksave);
    730 
    731 	/* Now we know the SB is valid, we can write it back if needed */
    732 	if (doswap) {
    733 		sbdirty();
    734 		dirty(&asblk);
    735 	}
    736 	havesb = 1;
    737 	return (1);
    738 }
    739 
    740 int
    741 cmpsblks(const struct fs *sb, struct fs *asb)
    742 {
    743 	if (!is_ufs2 && ((sb->fs_old_flags & FS_FLAGS_UPDATED) == 0)) {
    744 		if (sb->fs_old_postblformat < FS_DYNAMICPOSTBLFMT)
    745 			return cmpsblks42(sb, asb);
    746 		else
    747 			return cmpsblks44(sb, asb);
    748 	}
    749 	if (asb->fs_sblkno != sb->fs_sblkno ||
    750 	    asb->fs_cblkno != sb->fs_cblkno ||
    751 	    asb->fs_iblkno != sb->fs_iblkno ||
    752 	    asb->fs_dblkno != sb->fs_dblkno ||
    753 	    asb->fs_ncg != sb->fs_ncg ||
    754 	    asb->fs_bsize != sb->fs_bsize ||
    755 	    asb->fs_fsize != sb->fs_fsize ||
    756 	    asb->fs_frag != sb->fs_frag ||
    757 	    asb->fs_bmask != sb->fs_bmask ||
    758 	    asb->fs_fmask != sb->fs_fmask ||
    759 	    asb->fs_bshift != sb->fs_bshift ||
    760 	    asb->fs_fshift != sb->fs_fshift ||
    761 	    asb->fs_fragshift != sb->fs_fragshift ||
    762 	    asb->fs_fsbtodb != sb->fs_fsbtodb ||
    763 	    asb->fs_sbsize != sb->fs_sbsize ||
    764 	    asb->fs_nindir != sb->fs_nindir ||
    765 	    asb->fs_inopb != sb->fs_inopb ||
    766 	    asb->fs_cssize != sb->fs_cssize ||
    767 	    asb->fs_ipg != sb->fs_ipg ||
    768 	    asb->fs_fpg != sb->fs_fpg ||
    769 	    asb->fs_magic != sb->fs_magic)
    770 		return 1;
    771 	return 0;
    772 }
    773 
    774 /* BSD 4.2 performed the following superblock comparison
    775  * It should correspond to FS_42POSTBLFMT
    776  * (although note that in 4.2, the fs_old_postblformat
    777  * field didn't exist and the corresponding bits are
    778  * located near the end of the postbl itself, where they
    779  * are not likely to be used.)
    780  */
    781 int
    782 cmpsblks42(const struct fs *sb, struct fs *asb)
    783 {
    784 	asb->fs_firstfield = sb->fs_firstfield; /* fs_link */
    785 	asb->fs_unused_1 = sb->fs_unused_1; /* fs_rlink */
    786 	asb->fs_old_time = sb->fs_old_time; /* fs_time */
    787 	asb->fs_old_cstotal = sb->fs_old_cstotal; /* fs_cstotal */
    788 	asb->fs_cgrotor = sb->fs_cgrotor;
    789 	asb->fs_fmod = sb->fs_fmod;
    790 	asb->fs_clean = sb->fs_clean;
    791 	asb->fs_ronly = sb->fs_ronly;
    792 	asb->fs_old_flags = sb->fs_old_flags;
    793 	asb->fs_maxcontig = sb->fs_maxcontig;
    794 	asb->fs_minfree = sb->fs_minfree;
    795 	asb->fs_old_rotdelay = sb->fs_old_rotdelay;
    796 	asb->fs_maxbpg = sb->fs_maxbpg;
    797 
    798 	/* The former fs_csp, totaling 128 bytes  */
    799 	memmove(asb->fs_ocsp, sb->fs_ocsp, sizeof sb->fs_ocsp);
    800 	asb->fs_contigdirs = sb->fs_contigdirs;
    801 	asb->fs_csp = sb->fs_csp;
    802 	asb->fs_maxcluster = sb->fs_maxcluster;
    803 	asb->fs_active = sb->fs_active;
    804 
    805 	/* The former fs_fsmnt, totaling 512 bytes */
    806 	memmove(asb->fs_fsmnt, sb->fs_fsmnt, sizeof sb->fs_fsmnt);
    807 	memmove(asb->fs_volname, sb->fs_volname, sizeof sb->fs_volname);
    808 
    809 	return memcmp(sb, asb, sb->fs_sbsize);
    810 }
    811 
    812 /* BSD 4.4 performed the following superblock comparison
    813  * This was used in NetBSD through 1.6.1
    814  *
    815  * Note that this implementation is destructive to asb.
    816  */
    817 int
    818 cmpsblks44(const struct fs *sb, struct fs *asb)
    819 {
    820 	/*
    821 	 * "Copy fields which we don't care if they're different in the
    822 	 * alternate superblocks, as they're either likely to be
    823 	 * different because they're per-cylinder-group specific, or
    824 	 * because they're transient details which are only maintained
    825 	 * in the primary superblock."
    826 	 */
    827 	asb->fs_firstfield = sb->fs_firstfield;
    828 	asb->fs_unused_1 = sb->fs_unused_1;
    829 	asb->fs_old_time = sb->fs_old_time;
    830 	asb->fs_old_cstotal = sb->fs_old_cstotal;
    831 	asb->fs_cgrotor = sb->fs_cgrotor;
    832 	asb->fs_fmod = sb->fs_fmod;
    833 	asb->fs_clean = sb->fs_clean;
    834 	asb->fs_ronly = sb->fs_ronly;
    835 	asb->fs_old_flags = sb->fs_old_flags;
    836 	asb->fs_maxcontig = sb->fs_maxcontig;
    837 	asb->fs_minfree = sb->fs_minfree;
    838 	asb->fs_optim = sb->fs_optim;
    839 	asb->fs_old_rotdelay = sb->fs_old_rotdelay;
    840 	asb->fs_maxbpg = sb->fs_maxbpg;
    841 
    842 	/* The former fs_csp and fs_maxcluster, totaling 128 bytes */
    843 	memmove(asb->fs_ocsp, sb->fs_ocsp, sizeof sb->fs_ocsp);
    844 	asb->fs_contigdirs = sb->fs_contigdirs;
    845 	asb->fs_csp = sb->fs_csp;
    846 	asb->fs_maxcluster = sb->fs_maxcluster;
    847 	asb->fs_active = sb->fs_active;
    848 
    849 	/* The former fs_fsmnt, totaling 512 bytes */
    850 	memmove(asb->fs_fsmnt, sb->fs_fsmnt, sizeof sb->fs_fsmnt);
    851 	memmove(asb->fs_volname, sb->fs_volname, sizeof sb->fs_volname);
    852 
    853 	/* The former fs_sparecon, totaling 200 bytes */
    854 	memmove(asb->fs_snapinum,
    855 		sb->fs_snapinum, sizeof sb->fs_snapinum);
    856 	asb->fs_avgfilesize = sb->fs_avgfilesize;
    857 	asb->fs_avgfpdir = sb->fs_avgfpdir;
    858 	asb->fs_save_cgsize = sb->fs_save_cgsize;
    859 	memmove(asb->fs_sparecon32,
    860 		sb->fs_sparecon32, sizeof sb->fs_sparecon32);
    861 	asb->fs_flags = sb->fs_flags;
    862 
    863 	/* Original comment:
    864 	 * "The following should not have to be copied, but need to be."
    865 	 */
    866 	asb->fs_fsbtodb = sb->fs_fsbtodb;
    867 	asb->fs_old_interleave = sb->fs_old_interleave;
    868 	asb->fs_old_npsect = sb->fs_old_npsect;
    869 	asb->fs_old_nrpos = sb->fs_old_nrpos;
    870 	asb->fs_state = sb->fs_state;
    871 	asb->fs_qbmask = sb->fs_qbmask;
    872 	asb->fs_qfmask = sb->fs_qfmask;
    873 	asb->fs_state = sb->fs_state;
    874 	asb->fs_maxfilesize = sb->fs_maxfilesize;
    875 
    876 	/*
    877 	 * "Compare the superblocks, effectively checking every other
    878 	 * field to see if they differ."
    879 	 */
    880 	return memcmp(sb, asb, sb->fs_sbsize);
    881 }
    882 
    883 
    884 static void
    885 badsb(listerr, s)
    886 	int listerr;
    887 	char *s;
    888 {
    889 
    890 	if (!listerr)
    891 		return;
    892 	if (preen)
    893 		printf("%s: ", cdevname());
    894 	pfatal("BAD SUPER BLOCK: %s\n", s);
    895 }
    896 
    897 /*
    898  * Calculate a prototype superblock based on information in the disk label.
    899  * When done the cgsblock macro can be calculated and the fs_ncg field
    900  * can be used. Do NOT attempt to use other macros without verifying that
    901  * their needed information is available!
    902  */
    903 static int
    904 calcsb(dev, devfd, fs)
    905 	const char *dev;
    906 	int devfd;
    907 	struct fs *fs;
    908 {
    909 	struct disklabel *lp;
    910 	struct partition *pp;
    911 	int i, nspf;
    912 
    913 	lp = getdisklabel(dev, devfd);
    914 	pp = getdisklabelpart(dev,lp);
    915 	if (pp == 0) {
    916 		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
    917 		return (0);
    918 	}
    919 	if ((pp->p_fstype != FS_BSDFFS) && (pp->p_fstype != FS_APPLEUFS)) {
    920 		pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
    921 			dev, pp->p_fstype < FSMAXTYPES ?
    922 			fstypenames[pp->p_fstype] : "unknown");
    923 		return (0);
    924 	}
    925 	/* avoid divide by 0 */
    926 	if (pp->p_fsize == 0 || pp->p_frag == 0)
    927 		return (0);
    928 	memset(fs, 0, sizeof(struct fs));
    929 	fs->fs_fsize = pp->p_fsize;
    930 	fs->fs_frag = pp->p_frag;
    931 	fs->fs_size = pp->p_size;
    932 	fs->fs_sblkno = roundup(
    933 		howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize),
    934 		fs->fs_frag);
    935 	nspf = fs->fs_fsize / lp->d_secsize;
    936 	fs->fs_old_nspf = nspf;
    937 	for (fs->fs_fsbtodb = 0, i = nspf; i > 1; i >>= 1)
    938 		fs->fs_fsbtodb++;
    939 	dev_bsize = lp->d_secsize;
    940 	if (fs->fs_magic == FS_UFS2_MAGIC) {
    941 		fs->fs_fpg = pp->p_cpg;
    942 		fs->fs_ncg = howmany(fs->fs_size, fs->fs_fpg);
    943 	} else /* if (fs->fs_magic == FS_UFS1_MAGIC) */ {
    944 		fs->fs_old_cpg = pp->p_cpg;
    945 		fs->fs_old_cgmask = 0xffffffff;
    946 		for (i = lp->d_ntracks; i > 1; i >>= 1)
    947 			fs->fs_old_cgmask <<= 1;
    948 		if (!POWEROF2(lp->d_ntracks))
    949 			fs->fs_old_cgmask <<= 1;
    950 		fs->fs_old_cgoffset = roundup(
    951 			howmany(lp->d_nsectors, nspf), fs->fs_frag);
    952 		fs->fs_fpg = (fs->fs_old_cpg * lp->d_secpercyl) / nspf;
    953 		fs->fs_ncg = howmany(fs->fs_size / lp->d_secpercyl,
    954 		    fs->fs_old_cpg);
    955 	}
    956 	return (1);
    957 }
    958 
    959 static struct disklabel *
    960 getdisklabel(s, fd)
    961 	const char *s;
    962 	int	fd;
    963 {
    964 	static struct disklabel lab;
    965 
    966 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
    967 		if (s == NULL)
    968 			return ((struct disklabel *)NULL);
    969 		pwarn("ioctl (GCINFO): %s\n", strerror(errno));
    970 		errx(EEXIT, "%s: can't read disk label", s);
    971 	}
    972 	return (&lab);
    973 }
    974 
    975 static struct partition *
    976 getdisklabelpart(dev, lp)
    977 	const char *dev;
    978 	struct disklabel *lp;
    979 {
    980 	char *cp;
    981 
    982 	cp = strchr(dev, '\0') - 1;
    983 	if ((cp == (char *)-1 || (*cp < 'a' || *cp > 'p')) && !isdigit(*cp)) {
    984 		return 0;
    985 	}
    986 	if (isdigit(*cp))
    987 		return &lp->d_partitions[0];
    988 	else
    989 		return &lp->d_partitions[*cp - 'a'];
    990 }
    991 
    992