Home | History | Annotate | Line # | Download | only in dumpfs
dumpfs.c revision 1.53
      1 /*	$NetBSD: dumpfs.c,v 1.53 2009/05/07 06:40:38 lukem Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1992, 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 __COPYRIGHT("@(#) Copyright (c) 1983, 1992, 1993\
     35  The Regents of the University of California.  All rights reserved.");
     36 #endif /* not lint */
     37 
     38 #ifndef lint
     39 #if 0
     40 static char sccsid[] = "@(#)dumpfs.c	8.5 (Berkeley) 4/29/95";
     41 #else
     42 __RCSID("$NetBSD: dumpfs.c,v 1.53 2009/05/07 06:40:38 lukem Exp $");
     43 #endif
     44 #endif /* not lint */
     45 
     46 #include <sys/param.h>
     47 #include <sys/time.h>
     48 
     49 #include <ufs/ufs/dinode.h>
     50 #include <ufs/ufs/ufs_bswap.h>
     51 #include <ufs/ffs/fs.h>
     52 #include <ufs/ffs/ffs_extern.h>
     53 
     54 #include <err.h>
     55 #include <errno.h>
     56 #include <fcntl.h>
     57 #include <fstab.h>
     58 #include <stdio.h>
     59 #include <stdlib.h>
     60 #include <string.h>
     61 #include <stddef.h>
     62 #include <unistd.h>
     63 #include <util.h>
     64 
     65 union fsun {
     66 	struct fs fs;
     67 	char pad[MAXBSIZE];
     68 } fsun;
     69 #define	afs	fsun.fs
     70 
     71 uint16_t opostblsave[32*8];
     72 
     73 union {
     74 	struct cg cg;
     75 	char pad[MAXBSIZE];
     76 } cgun;
     77 #define	acg	cgun.cg
     78 
     79 #define OPT_FLAG(ch)	(1 << ((ch) & 31))
     80 #define ISOPT(opt)	(opt_flags & (opt))
     81 #define opt_alt_super	OPT_FLAG('a')
     82 #define opt_superblock	OPT_FLAG('s')
     83 #define opt_cg_summary	OPT_FLAG('m')
     84 #define opt_cg_info	OPT_FLAG('c')
     85 #define opt_inodes	OPT_FLAG('i')
     86 #define opt_verbose	OPT_FLAG('v')
     87 #define DFLT_CHECK (opt_alt_super | opt_cg_info | opt_inodes | \
     88     opt_cg_summary | opt_superblock)
     89 #define DFLT_OPTS	(opt_superblock | opt_cg_summary | opt_cg_info | opt_verbose)
     90 
     91 long	dev_bsize = 512;
     92 int	needswap, printold, is_ufs2;
     93 int	Fflag;
     94 
     95 uint	opt_flags;
     96 
     97 int	dumpfs(const char *);
     98 int	print_superblock(struct fs *, uint16_t *, const char *, int, off_t);
     99 int	print_cgsum(const char *, int);
    100 int	print_cginfo(const char *, int);
    101 int	print_inodes(const char *, int, int, int);
    102 int	print_alt_super(const char *, int);
    103 int	dumpcg(const char *, int, int);
    104 int	main(int, char **);
    105 int	openpartition(const char *, int, char *, size_t);
    106 void	pbits(int, void *, int);
    107 void	usage(void);
    108 void	print_ufs1_inode(int, int, void *);
    109 void	print_ufs2_inode(int, int, void *);
    110 void	fix_superblock(struct fs *, uint16_t *);
    111 
    112 int
    113 main(int argc, char *argv[])
    114 {
    115 	int ch, eval;
    116 
    117 	while ((ch = getopt(argc, argv, "acimsvF")) != -1)
    118 		switch(ch) {
    119 		case 'a':	/* alternate superblocks */
    120 		case 'c':	/* cylinder group info */
    121 		case 'i':	/* actual inodes */
    122 		case 'm':	/* cylinder group summary */
    123 		case 's':	/* superblock */
    124 		case 'v':	/* more verbose */
    125 			opt_flags |= OPT_FLAG(ch);
    126 			break;
    127 		case 'F':	/* File (not device) */
    128 			Fflag = 1;
    129 			break;
    130 		case '?':
    131 		default:
    132 			usage();
    133 		}
    134 	argc -= optind;
    135 	argv += optind;
    136 
    137 	if ((opt_flags & DFLT_CHECK) == 0)
    138 		opt_flags |= DFLT_OPTS;
    139 
    140 	if (argc < 1)
    141 		usage();
    142 
    143 	for (eval = 0; *argv; ++argv) {
    144 		eval |= dumpfs(*argv);
    145 	}
    146 
    147 	exit(eval);
    148 }
    149 
    150 
    151 int
    152 dumpfs(const char *name)
    153 {
    154 	static const off_t sblock_try[] = SBLOCKSEARCH;
    155 	char device[MAXPATHLEN];
    156 	int fd, i;
    157 	int rval = 1;
    158 
    159 	if (Fflag)
    160 		fd = open(name, O_RDONLY);
    161 	else {
    162 		fd = openpartition(name, O_RDONLY, device, sizeof(device));
    163 		name = device;
    164 	}
    165 	if (fd == -1)
    166 		goto err;
    167 
    168 	for (i = 0; ; i++) {
    169 		if (sblock_try[i] == -1) {
    170 			warnx("%s: could not find superblock, skipped", name);
    171 			return 1;
    172 		}
    173 		if (lseek(fd, sblock_try[i], SEEK_SET) == (off_t)-1)
    174 			continue;
    175 		if (read(fd, &afs, SBLOCKSIZE) != SBLOCKSIZE)
    176 			continue;
    177 		switch(afs.fs_magic) {
    178 		case FS_UFS2_MAGIC:
    179 			is_ufs2 = 1;
    180 			break;
    181 		case FS_UFS1_MAGIC:
    182 			break;
    183 		case FS_UFS2_MAGIC_SWAPPED:
    184 			is_ufs2 = 1;
    185 			needswap = 1;
    186 			break;
    187 		case FS_UFS1_MAGIC_SWAPPED:
    188 			needswap = 1;
    189 			break;
    190 		default:
    191 			continue;
    192 		}
    193 		fix_superblock(&afs, opostblsave);
    194 		if (printold) {
    195 			if (sblock_try[i] == SBLOCK_UFS2)
    196 				/* This might be an alternate superblock */
    197 				continue;
    198 		} else {
    199 			if (sblock_try[i] != afs.fs_sblockloc)
    200 				/* This must be an alternate superblock */
    201 				continue;
    202 		}
    203 		break;
    204 	}
    205 
    206 
    207 	dev_bsize = afs.fs_fsize / fsbtodb(&afs, 1);
    208 
    209 	rval = 0;
    210 	printf("file system: %s\n", name);
    211 
    212 	if (ISOPT(opt_superblock))
    213 		rval = print_superblock(&afs, opostblsave, name, fd, sblock_try[i]);
    214 	if (rval == 0 && ISOPT(opt_cg_summary))
    215 		rval = print_cgsum(name, fd);
    216 	if (rval == 0 && ISOPT(opt_alt_super))
    217 		rval = print_alt_super(name, fd);
    218 	if (rval == 0 && ISOPT(opt_cg_info))
    219 		rval = print_cginfo(name, fd);
    220 	else if (rval == 0 && ISOPT(opt_inodes))
    221 		rval = print_inodes(name, fd, 0, afs.fs_ncg);
    222 
    223     err:
    224 	if (fd != -1)
    225 		(void)close(fd);
    226 	if (rval)
    227 		warn("%s", name);
    228 	return rval;
    229 }
    230 
    231 void
    232 fix_superblock(struct fs *fs, uint16_t *opostbl)
    233 {
    234 	if (needswap &&
    235 	    (((int32_t)bswap32(fs->fs_old_postblformat) == FS_42POSTBLFMT) ||
    236 	     (bswap32(fs->fs_old_postbloff) == offsetof(struct fs, fs_old_postbl_start)))) {
    237 		int i;
    238 		memcpy(opostbl, &fs->fs_old_postbl_start, 512);
    239 		for(i=0;i<256;i++)
    240 			opostbl[i] = bswap16(opostbl[i]);
    241 	} else if (!needswap &&
    242 	   ((fs->fs_old_postblformat == FS_42POSTBLFMT) ||
    243 	    (fs->fs_old_postbloff == offsetof(struct fs, fs_old_postbl_start)))) {
    244 		memcpy(opostbl, &fs->fs_old_postbl_start, 512);
    245 	}
    246 
    247 	if (needswap)
    248 		ffs_sb_swap(fs, fs);
    249 
    250 	printold = (fs->fs_magic == FS_UFS1_MAGIC &&
    251 		    (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0);
    252 	if (printold) {
    253 		fs->fs_sblockloc = SBLOCK_UFS1;
    254 		fs->fs_flags = fs->fs_old_flags;
    255 		fs->fs_maxbsize = fs->fs_bsize;
    256 		fs->fs_time = fs->fs_old_time;
    257 		fs->fs_size = fs->fs_old_size;
    258 		fs->fs_dsize = fs->fs_old_dsize;
    259 		fs->fs_csaddr = fs->fs_old_csaddr;
    260 		fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
    261 		fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
    262 		fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
    263 		fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
    264 	}
    265 
    266 	if (printold && fs->fs_old_postblformat == FS_42POSTBLFMT)
    267 		fs->fs_old_nrpos = 8;
    268 }
    269 
    270 int
    271 print_superblock(struct fs *fs, uint16_t *opostbl,
    272     const char *name, int fd, off_t sblock)
    273 {
    274 	int i, size;
    275 	time_t t;
    276 	int32_t fsflags;
    277 
    278 	printf("format\tFFSv%d\n", is_ufs2+1);
    279 #if BYTE_ORDER == LITTLE_ENDIAN
    280 	if (needswap)
    281 #else
    282 	if (!needswap)
    283 #endif
    284 		printf("endian\tbig-endian\n");
    285 	else
    286 		printf("endian\tlittle-endian\n");
    287 	t = fs->fs_time;
    288 	if ((sblock != SBLOCK_UFS1) || ISOPT(opt_alt_super))
    289 		printf("location %lld\t(-b %lld)\n",
    290 		    (long long)sblock, (long long)(sblock/dev_bsize));
    291 	printf("magic\t%-8x\ttime\t%s",
    292 	    fs->fs_magic, ctime(&t));
    293 
    294 	if (is_ufs2)
    295 		i = 5;
    296 	else {
    297 		i = 0;
    298 		if (fs->fs_old_postblformat != FS_42POSTBLFMT) {
    299 			i++;
    300 			if (fs->fs_old_inodefmt >= FS_44INODEFMT) {
    301 				int max;
    302 
    303 				i++;
    304 				max = fs->fs_maxcontig;
    305 				size = fs->fs_contigsumsize;
    306 				if ((max < 2 && size == 0)
    307 				    || (max > 1 && size >= MIN(max, FS_MAXCONTIG)))
    308 					i++;
    309 			}
    310 			if (fs->fs_old_flags & FS_FLAGS_UPDATED) {
    311 				i = 4;
    312 			}
    313 		}
    314 	}
    315 	if (!printold || fs->fs_sblockloc != SBLOCK_UFS1 ||
    316 	    fs->fs_id[0] || fs->fs_id[1])
    317 		printf("superblock location\t%jd\tid\t[ %x %x ]\n",
    318 		    (intmax_t)fs->fs_sblockloc, fs->fs_id[0], fs->fs_id[1]);
    319 	printf("cylgrp\t%s\tinodes\t%s\tsblock\t%s\tfslevel %d\n",
    320 	    i < 1 ? "static" : "dynamic",
    321 	    i < 2 ? "4.2/4.3BSD" : i < 5 ? "4.4BSD" : "FFSv2",
    322 	    i < 4 ? "FFSv1" : "FFSv2", i);
    323 	printf("nbfree\t%lld\tndir\t%lld\tnifree\t%lld\tnffree\t%lld\n",
    324 	    (long long)fs->fs_cstotal.cs_nbfree,
    325 	    (long long)fs->fs_cstotal.cs_ndir,
    326 	    (long long)fs->fs_cstotal.cs_nifree,
    327 	    (long long)fs->fs_cstotal.cs_nffree);
    328 	if (printold)
    329 		printf("ncg\t%d\tncyl\t%d\tsize\t%lld\tblocks\t%lld\n",
    330 		    fs->fs_ncg, fs->fs_old_ncyl, (long long)fs->fs_size, (long long)fs->fs_dsize);
    331 	else
    332 		printf("ncg\t%d\tsize\t%lld\tblocks\t%lld\n",
    333 		    fs->fs_ncg, (long long)fs->fs_size, (long long)fs->fs_dsize);
    334 	printf("bsize\t%d\tshift\t%d\tmask\t0x%08x\n",
    335 	    fs->fs_bsize, fs->fs_bshift, fs->fs_bmask);
    336 	printf("fsize\t%d\tshift\t%d\tmask\t0x%08x\n",
    337 	    fs->fs_fsize, fs->fs_fshift, fs->fs_fmask);
    338 	printf("frag\t%d\tshift\t%d\tfsbtodb\t%d\n",
    339 	    fs->fs_frag, fs->fs_fragshift, fs->fs_fsbtodb);
    340 	if (printold)
    341 		printf("cpg\t%d\t", fs->fs_old_cpg);
    342 	printf("bpg\t%d\tfpg\t%d\tipg\t%d\n",
    343 	    fs->fs_fpg / fs->fs_frag, fs->fs_fpg, fs->fs_ipg);
    344 	printf("minfree\t%d%%\toptim\t%s\tmaxcontig %d\tmaxbpg\t%d\n",
    345 	    fs->fs_minfree, fs->fs_optim == FS_OPTSPACE ? "space" : "time",
    346 	    fs->fs_maxcontig, fs->fs_maxbpg);
    347 	if (printold) {
    348 		printf("rotdelay %dms\theadswitch %dus\ttrackseek %dus\trps\t%d\n",
    349 		    fs->fs_old_rotdelay, fs->fs_old_headswitch,
    350 		    fs->fs_old_trkseek, fs->fs_old_rps);
    351 		printf("ntrak\t%d\tnsect\t%d\tnpsect\t%d\tspc\t%d\n",
    352 		    fs->fs_spare2, fs->fs_old_nsect, fs->fs_old_npsect,
    353 		    fs->fs_old_spc);
    354 	}
    355 	printf("symlinklen %d\t", fs->fs_maxsymlinklen);
    356 	if (printold)
    357 		printf("trackskew %d\tinterleave %d\t",
    358 		    fs->fs_old_trackskew, fs->fs_old_interleave);
    359 	printf("contigsumsize %d\n", fs->fs_contigsumsize);
    360 	printf("maxfilesize 0x%016llx\n",
    361 	    (unsigned long long)fs->fs_maxfilesize);
    362 	if (printold)
    363 		printf("nindir\t%d\tinopb\t%d\tnspf\t%d\n", fs->fs_nindir,
    364 		    fs->fs_inopb, fs->fs_old_nspf);
    365 	else
    366 		printf("nindir\t%d\tinopb\t%d\n", fs->fs_nindir, fs->fs_inopb);
    367 	if (!printold || (fs->fs_avgfilesize > 0) || (fs->fs_avgfpdir > 0))
    368 		printf("avgfilesize %d\tavgfpdir %d\n",
    369 		    fs->fs_avgfilesize, fs->fs_avgfpdir);
    370 	printf("sblkno\t%d\tcblkno\t%d\tiblkno\t%d\tdblkno\t%d\n",
    371 	    fs->fs_sblkno, fs->fs_cblkno, fs->fs_iblkno, fs->fs_dblkno);
    372 	printf("sbsize\t%d\tcgsize\t%d", fs->fs_sbsize, fs->fs_cgsize);
    373 	if (printold)
    374 		printf("\toffset\t%d\tmask\t0x%08x",
    375 		    fs->fs_old_cgoffset, fs->fs_old_cgmask);
    376 	printf("\ncsaddr\t%lld\tcssize\t%d",
    377 	    (long long)fs->fs_csaddr, fs->fs_cssize);
    378 	if (printold)
    379 		printf("\tshift\t%d\tmask\t0x%08x",
    380 		    fs->fs_old_csshift, fs->fs_old_csmask);
    381 	printf("\ncgrotor\t%d\tfmod\t%d\tronly\t%d\tclean\t0x%02x\n",
    382 	    fs->fs_cgrotor, fs->fs_fmod, fs->fs_ronly, fs->fs_clean);
    383 	printf("wapbl version 0x%x\tlocation %u\tflags 0x%x\n",
    384 	    fs->fs_journal_version, fs->fs_journal_location,
    385 	    fs->fs_journal_flags);
    386 	printf("wapbl loc0 %" PRIu64 "\tloc1 %" PRIu64,
    387 	    fs->fs_journallocs[0], fs->fs_journallocs[1]);
    388 	printf("\tloc2 %" PRIu64 "\tloc3 %" PRIu64 "\n",
    389 	    fs->fs_journallocs[2], fs->fs_journallocs[3]);
    390 	printf("flags\t");
    391 	if (fs->fs_flags == 0)
    392 		printf("none");
    393 	if (fs->fs_flags & FS_UNCLEAN)
    394 		printf("unclean ");
    395 	if (fs->fs_flags & FS_DOSOFTDEP)
    396 		printf("soft-updates ");
    397 	if (fs->fs_flags & FS_NEEDSFSCK)
    398 		printf("needs fsck run ");
    399 	if (fs->fs_flags & FS_INDEXDIRS)
    400 		printf("indexed directories ");
    401 	if (fs->fs_flags & FS_ACLS)
    402 		printf("acls ");
    403 	if (fs->fs_flags & FS_MULTILABEL)
    404 		printf("multilabel ");
    405 	if (fs->fs_flags & FS_FLAGS_UPDATED)
    406 		printf("fs_flags expanded ");
    407 	if (fs->fs_flags & FS_DOWAPBL)
    408 		printf("wapbl ");
    409 	fsflags = fs->fs_flags & ~(FS_UNCLEAN | FS_DOSOFTDEP | FS_NEEDSFSCK |
    410 			FS_INDEXDIRS | FS_ACLS | FS_MULTILABEL |
    411 			FS_FLAGS_UPDATED | FS_DOWAPBL);
    412 	if (fsflags != 0)
    413 		printf("unknown flags (%#x)", fsflags);
    414 	printf("\nfsmnt\t%s\n", fs->fs_fsmnt);
    415 	if (!printold)
    416 		printf("volname\t%s\tswuid\t%ju\n",
    417 		    fs->fs_volname, (uintmax_t)fs->fs_swuid);
    418 	if (printold) {
    419 		if (fs->fs_old_cpc != 0)
    420 			printf("blocks available in each of %d rotational "
    421 			       "positions\n", fs->fs_old_nrpos);
    422 		else
    423 			printf("(no rotational position table)\n\n");
    424 		if (ISOPT(opt_verbose)) {
    425 			int c, j, k;
    426 			for (c = 0; c < fs->fs_old_cpc; c++) {
    427 				printf("cylinder number %d:", c);
    428 				for (i = 0; i < fs->fs_old_nrpos; i++) {
    429 					if (old_fs_postbl(&afs, c, opostbl)[i] == -1)
    430 						continue;
    431 					printf("\n   position %d:\t", i);
    432 					for (j = old_fs_postbl(&afs, c, opostbl)[i], k = 1; ;
    433 							 j += old_fs_rotbl(&afs)[j], k++) {
    434 						printf("%5d", j);
    435 						if (k % 12 == 0)
    436 							printf("\n\t\t");
    437 						if (old_fs_rotbl(&afs)[j] == 0)
    438 							break;
    439 					}
    440 				}
    441 				printf("\n");
    442 			}
    443 		}
    444 	}
    445 
    446 	return 0;
    447 }
    448 
    449 int
    450 print_cgsum(const char *name, int fd)
    451 {
    452 	struct csum *ccsp;
    453 	int i, j, size;
    454 
    455 	afs.fs_csp = calloc(1, afs.fs_cssize);
    456 	for (i = 0, j = 0; i < afs.fs_cssize; i += afs.fs_bsize, j++) {
    457 		size = afs.fs_cssize - i < afs.fs_bsize ?
    458 		    afs.fs_cssize - i : afs.fs_bsize;
    459 		ccsp = (struct csum *)((char *)afs.fs_csp + i);
    460 		if (lseek(fd,
    461 		    (off_t)(fsbtodb(&afs, (afs.fs_csaddr + j * afs.fs_frag))) *
    462 		    dev_bsize, SEEK_SET) == (off_t)-1)
    463 			return 1;
    464 		if (read(fd, ccsp, size) != size)
    465 			return 1;
    466 		if (needswap)
    467 			ffs_csum_swap(ccsp, ccsp, size);
    468 	}
    469 
    470 	printf("cs[].cs_(nbfree,ndir,nifree,nffree):\n\t");
    471 	for (i = 0; i < afs.fs_ncg; i++) {
    472 		struct csum *cs = &afs.fs_cs(&afs, i);
    473 		if (i && i % 4 == 0)
    474 			printf("\n\t");
    475 		printf("(%d,%d,%d,%d) ",
    476 		    cs->cs_nbfree, cs->cs_ndir, cs->cs_nifree, cs->cs_nffree);
    477 	}
    478 	printf("\n");
    479 	if (printold && (afs.fs_old_ncyl % afs.fs_old_cpg)) {
    480 		printf("cylinders in last group %d\n",
    481 		    i = afs.fs_old_ncyl % afs.fs_old_cpg);
    482 		printf("blocks in last group %d\n",
    483 		    i * afs.fs_old_spc / (afs.fs_old_nspf << afs.fs_fragshift));
    484 	}
    485 	free(afs.fs_csp);
    486 	afs.fs_csp = NULL;
    487 
    488 	return 0;
    489 }
    490 
    491 int
    492 print_alt_super(const char *name, int fd)
    493 {
    494 	union fsun alt;
    495 	int i;
    496 	off_t loc;
    497 	uint16_t alt_opostblsave[32*8];
    498 	int save_printold;
    499 
    500 	for (i = 0; i < afs.fs_ncg; i++) {
    501 		loc = (off_t)(fsbtodb(&afs, cgsblock(&afs, i))) * dev_bsize;
    502 		printf("\nalternate %d\n", i);
    503 		if (pread(fd, &alt, sizeof alt, loc) != sizeof alt) {
    504 			warnx("%s: error reading alt %d", name, i);
    505 			return (1);
    506 		}
    507 		save_printold = printold;
    508 		fix_superblock(&alt.fs, alt_opostblsave);
    509 		if (print_superblock(&alt.fs, alt_opostblsave, name, fd, loc)) {
    510 			printold = save_printold;
    511 			return 1;
    512 		}
    513 		printold = save_printold;
    514 	}
    515 	return 0;
    516 }
    517 
    518 int
    519 print_cginfo(const char *name, int fd)
    520 {
    521 	int i;
    522 
    523 	printf("\n");
    524 	for (i = 0; i < afs.fs_ncg; i++) {
    525 		printf("\n");
    526 		if (dumpcg(name, fd, i))
    527 			return 1;
    528 		if (ISOPT(opt_inodes) && print_inodes(name, fd, i, 1))
    529 			return 1;
    530 	}
    531 	return 0;
    532 }
    533 
    534 int
    535 print_inodes(const char *name, int fd, int c, int n)
    536 {
    537 	void *ino_buf = malloc(afs.fs_bsize);
    538 	void (*print_inode)(int, int, void *);
    539 	int i, inum;
    540 
    541 	if (ino_buf == 0)
    542 		return 1;
    543 
    544 	print_inode = is_ufs2 ? print_ufs2_inode : print_ufs1_inode;
    545 
    546 	for (inum = c * afs.fs_ipg ; inum < (c+n) * afs.fs_ipg; inum += afs.fs_inopb) {
    547 		if (pread(fd, ino_buf, afs.fs_bsize,
    548 		    ino_to_fsba(&afs, inum) * afs.fs_fsize) != afs.fs_bsize) {
    549 			free(ino_buf);
    550 			return 1;
    551 		}
    552 		for (i = 0; i < afs.fs_inopb; i++)
    553 			print_inode(inum + i, i, ino_buf);
    554 	}
    555 
    556 	free(ino_buf);
    557 	return 0;
    558 }
    559 
    560 int
    561 dumpcg(const char *name, int fd, int c)
    562 {
    563 	off_t cur;
    564 	int i, j;
    565 	time_t t;
    566 
    567 	printf("cg %d:\n", c);
    568 	if ((cur = lseek(fd, (off_t)(fsbtodb(&afs, cgtod(&afs, c))) * dev_bsize,
    569 	    SEEK_SET)) == (off_t)-1)
    570 		return (1);
    571 	if (read(fd, &acg, afs.fs_bsize) != afs.fs_bsize) {
    572 		warnx("%s: error reading cg", name);
    573 		return (1);
    574 	}
    575 	if (needswap)
    576 		ffs_cg_swap(&acg, &acg, &afs);
    577 	if (printold)
    578 		t = acg.cg_old_time;
    579 	else
    580 		t = acg.cg_time;
    581 	printf("magic\t%x\ttell\t%llx\ttime\t%s",
    582 	    afs.fs_old_postblformat == FS_42POSTBLFMT ?
    583 	    ((struct ocg *)&acg)->cg_magic : acg.cg_magic,
    584 	    (long long)cur, ctime(&t));
    585 	if (printold)
    586 		printf("cgx\t%d\tncyl\t%d\tniblk\t%d\tndblk\t%d\n",
    587 		    acg.cg_cgx, acg.cg_old_ncyl, acg.cg_old_niblk,
    588 		    acg.cg_ndblk);
    589 	else
    590 		printf("cgx\t%d\tniblk\t%d\tndblk\t%d\n",
    591 		    acg.cg_cgx, acg.cg_niblk, acg.cg_ndblk);
    592 	printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
    593 	    acg.cg_cs.cs_nbfree, acg.cg_cs.cs_ndir,
    594 	    acg.cg_cs.cs_nifree, acg.cg_cs.cs_nffree);
    595 	printf("rotor\t%d\tirotor\t%d\tfrotor\t%d\nfrsum",
    596 	    acg.cg_rotor, acg.cg_irotor, acg.cg_frotor);
    597 	for (i = 1, j = 0; i < afs.fs_frag; i++) {
    598 		printf("\t%d", acg.cg_frsum[i]);
    599 		j += i * acg.cg_frsum[i];
    600 	}
    601 	printf("\nsum of frsum: %d", j);
    602 	if (afs.fs_contigsumsize > 0) {
    603 		for (i = 1; i < afs.fs_contigsumsize; i++) {
    604 			if ((i - 1) % 8 == 0)
    605 				printf("\nclusters %d-%d:", i,
    606 				    afs.fs_contigsumsize - 1 < i + 7 ?
    607 				    afs.fs_contigsumsize - 1 : i + 7);
    608 			printf("\t%d", cg_clustersum(&acg, 0)[i]);
    609 		}
    610 		printf("\nclusters size %d and over: %d\n",
    611 		    afs.fs_contigsumsize,
    612 		    cg_clustersum(&acg, 0)[afs.fs_contigsumsize]);
    613 		printf("clusters free:\t");
    614 		pbits(0, cg_clustersfree(&acg, 0), acg.cg_nclusterblks);
    615 	} else
    616 		printf("\n");
    617 	printf("iused:\t");
    618 	pbits(0 * c * afs.fs_ipg, cg_inosused(&acg, 0), afs.fs_ipg);
    619 	printf("free:\t");
    620 	pbits(0, cg_blksfree(&acg, 0), afs.fs_fpg);
    621 	if (printold && ISOPT(opt_verbose)) {
    622 		printf("b:\n");
    623 		for (i = 0; i < afs.fs_old_cpg; i++) {
    624 			if (old_cg_blktot(&acg, 0)[i] == 0)
    625 				continue;
    626 			printf("   c%d:\t(%d)\t", i, old_cg_blktot(&acg, 0)[i]);
    627 			for (j = 0; j < afs.fs_old_nrpos; j++) {
    628 				if (afs.fs_old_cpc > 0 &&
    629 						old_fs_postbl(&afs, i % afs.fs_old_cpc, opostblsave)[j] == -1)
    630 					continue;
    631 				printf(" %d", old_cg_blks(&afs, &acg, i, 0)[j]);
    632 			}
    633 			printf("\n");
    634 		}
    635 	}
    636 	return (0);
    637 }
    638 
    639 void
    640 print_ufs1_inode(int inum, int i_off, void *ibuf)
    641 {
    642 	struct ufs1_dinode *i = ibuf;
    643 
    644 	i += i_off;
    645 
    646 	if (needswap)
    647 		ffs_dinode1_swap(i,i);
    648 
    649 	if (afs.fs_old_inodefmt < FS_44INODEFMT) {
    650 		i->di_uid = i->di_ouid;
    651 		i->di_gid = i->di_ogid;
    652 	}
    653 
    654 	if (inum % afs.fs_ipg == 0)
    655 		printf("   inode:   mode  nlink                 size"
    656 		    "      ctime.nsec         flags     blocks"
    657 		    " generation      uid        gid\n");
    658 	if (i->di_mode == 0 && i->di_nlink == 0 && !ISOPT(opt_verbose))
    659 		return;
    660 	printf("%8u: %6o %6d %20" PRIu64 " %10u.%09u %8x %10u %8x %10u %10u\n",
    661 		inum, i->di_mode, i->di_nlink, i->di_size,
    662 		i->di_ctime, i->di_ctimensec, i->di_flags, i->di_blocks,
    663 		i->di_gen, i->di_uid, i->di_gid);
    664 }
    665 
    666 void
    667 print_ufs2_inode(int inum, int i_off, void *ibuf)
    668 {
    669 	struct ufs2_dinode *i = ibuf;
    670 
    671 	i += i_off;
    672 
    673 	if (needswap)
    674 		ffs_dinode2_swap(i,i);
    675 
    676 	if (inum % afs.fs_ipg == 0)
    677 		printf("   inode:   mode  nlink                 size"
    678 		    "      ctime.nsec         flags     blocks"
    679 		    " generation      uid        gid\n");
    680 
    681 	if (i->di_mode == 0 && i->di_nlink == 0 && !ISOPT(opt_verbose))
    682 		return;
    683 
    684 	printf("%8u: %6o %6d %20" PRIu64 " %10" PRIu64 ".%09u %8x %10" PRIu64 " %8x %10u %10u\n",
    685 		inum, i->di_mode, i->di_nlink, i->di_size,
    686 		i->di_ctime, i->di_ctimensec, i->di_flags, i->di_blocks,
    687 		i->di_gen, i->di_uid, i->di_gid);
    688 }
    689 
    690 
    691 void
    692 pbits(int offset, void *vp, int max)
    693 {
    694 	int i;
    695 	char *p;
    696 	int count, j;
    697 
    698 	for (count = i = 0, p = vp; i < max; i++)
    699 		if (isset(p, i)) {
    700 			if (count)
    701 				printf(",%s", count % 6 ? " " : "\n\t");
    702 			count++;
    703 			printf("%d", offset + i);
    704 			j = i;
    705 			while ((i+1)<max && isset(p, i+1))
    706 				i++;
    707 			if (i != j)
    708 				printf("-%d", offset + i);
    709 		}
    710 	printf("\n");
    711 }
    712 
    713 void
    714 usage(void)
    715 {
    716 
    717 	(void)fprintf(stderr, "usage: dumpfs [-acFimsv] filesys | device [...]\n");
    718 	exit(1);
    719 }
    720 
    721 int
    722 openpartition(const char *name, int flags, char *device, size_t devicelen)
    723 {
    724 	char		rawspec[MAXPATHLEN], *p;
    725 	struct fstab	*fs;
    726 	int		fd, oerrno;
    727 
    728 	fs = getfsfile(name);
    729 	if (fs) {
    730 		if ((p = strrchr(fs->fs_spec, '/')) != NULL) {
    731 			snprintf(rawspec, sizeof(rawspec), "%.*s/r%s",
    732 			    (int)(p - fs->fs_spec), fs->fs_spec, p + 1);
    733 			name = rawspec;
    734 		} else
    735 			name = fs->fs_spec;
    736 	}
    737 	fd = opendisk(name, flags, device, devicelen, 0);
    738 	if (fd == -1 && errno == ENOENT) {
    739 		oerrno = errno;
    740 		strlcpy(device, name, devicelen);
    741 		errno = oerrno;
    742 	}
    743 	return (fd);
    744 }
    745