Home | History | Annotate | Line # | Download | only in dumpfs
dumpfs.c revision 1.50
      1 /*	$NetBSD: dumpfs.c,v 1.50 2008/07/31 05:38:07 simonb 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.50 2008/07/31 05:38:07 simonb 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 	const static 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 	    ((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 #if BYTE_ORDER == LITTLE_ENDIAN
    279 	if (needswap)
    280 #else
    281 	if (!needswap)
    282 #endif
    283 		printf("endian\tbig-endian\n");
    284 	else
    285 		printf("endian\tlittle-endian\n");
    286 	t = fs->fs_time;
    287 	if ((sblock != SBLOCK_UFS1) || ISOPT(opt_alt_super))
    288 		printf("location %lld\t(-b %lld)\n",
    289 		    (long long)sblock, (long long)(sblock/dev_bsize));
    290 	printf("magic\t%x (UFS%d)\ttime\t%s",
    291 	    fs->fs_magic, is_ufs2+1, ctime(&t));
    292 
    293 	if (is_ufs2)
    294 		i = 5;
    295 	else {
    296 		i = 0;
    297 		if (fs->fs_old_postblformat != FS_42POSTBLFMT) {
    298 			i++;
    299 			if (fs->fs_old_inodefmt >= FS_44INODEFMT) {
    300 				int max;
    301 
    302 				i++;
    303 				max = fs->fs_maxcontig;
    304 				size = fs->fs_contigsumsize;
    305 				if ((max < 2 && size == 0)
    306 				    || (max > 1 && size >= MIN(max, FS_MAXCONTIG)))
    307 					i++;
    308 			}
    309 			if (fs->fs_old_flags & FS_FLAGS_UPDATED) {
    310 				i = 4;
    311 			}
    312 		}
    313 	}
    314 	if (!printold || fs->fs_sblockloc != SBLOCK_UFS1 ||
    315 	    fs->fs_id[0] || fs->fs_id[1])
    316 		printf("superblock location\t%jd\tid\t[ %x %x ]\n",
    317 		    (intmax_t)fs->fs_sblockloc, fs->fs_id[0], fs->fs_id[1]);
    318 	printf("cylgrp\t%s\tinodes\t%s\tsblock\t%s\tfslevel %d\n",
    319 	    i < 1 ? "static" : "dynamic",
    320 	    i < 2 ? "4.2/4.3BSD" : i < 5 ? "4.4BSD" : "FFSv2",
    321 			i < 4 ? "FFSv1" : "FFSv2", i);
    322 	printf("nbfree\t%lld\tndir\t%lld\tnifree\t%lld\tnffree\t%lld\n",
    323 	    (long long)fs->fs_cstotal.cs_nbfree,
    324 	    (long long)fs->fs_cstotal.cs_ndir,
    325 	    (long long)fs->fs_cstotal.cs_nifree,
    326 	    (long long)fs->fs_cstotal.cs_nffree);
    327 	if (printold)
    328 		printf("ncg\t%d\tncyl\t%d\tsize\t%lld\tblocks\t%lld\n",
    329 		    fs->fs_ncg, fs->fs_old_ncyl, (long long)fs->fs_size, (long long)fs->fs_dsize);
    330 	else
    331 		printf("ncg\t%d\tsize\t%lld\tblocks\t%lld\n",
    332 		    fs->fs_ncg, (long long)fs->fs_size, (long long)fs->fs_dsize);
    333 	printf("bsize\t%d\tshift\t%d\tmask\t0x%08x\n",
    334 	    fs->fs_bsize, fs->fs_bshift, fs->fs_bmask);
    335 	printf("fsize\t%d\tshift\t%d\tmask\t0x%08x\n",
    336 	    fs->fs_fsize, fs->fs_fshift, fs->fs_fmask);
    337 	printf("frag\t%d\tshift\t%d\tfsbtodb\t%d\n",
    338 	    fs->fs_frag, fs->fs_fragshift, fs->fs_fsbtodb);
    339 	if (printold)
    340 		printf("cpg\t%d\t", fs->fs_old_cpg);
    341 	printf("bpg\t%d\tfpg\t%d\tipg\t%d\n",
    342 	    fs->fs_fpg / fs->fs_frag, fs->fs_fpg, fs->fs_ipg);
    343 	printf("minfree\t%d%%\toptim\t%s\tmaxcontig %d\tmaxbpg\t%d\n",
    344 	    fs->fs_minfree, fs->fs_optim == FS_OPTSPACE ? "space" : "time",
    345 	    fs->fs_maxcontig, fs->fs_maxbpg);
    346 	if (printold) {
    347 		printf("rotdelay %dms\theadswitch %dus\ttrackseek %dus\trps\t%d\n",
    348 		    fs->fs_old_rotdelay, fs->fs_old_headswitch,
    349 		    fs->fs_old_trkseek, fs->fs_old_rps);
    350 		printf("ntrak\t%d\tnsect\t%d\tnpsect\t%d\tspc\t%d\n",
    351 		    fs->fs_spare2, fs->fs_old_nsect, fs->fs_old_npsect,
    352 		    fs->fs_old_spc);
    353 	}
    354 	printf("symlinklen %d\t", fs->fs_maxsymlinklen);
    355 	if (printold)
    356 		printf("trackskew %d\tinterleave %d\t",
    357 		    fs->fs_old_trackskew, fs->fs_old_interleave);
    358 	printf("contigsumsize %d\n", fs->fs_contigsumsize);
    359 	printf("maxfilesize 0x%016llx\n",
    360 	    (unsigned long long)fs->fs_maxfilesize);
    361 	if (printold)
    362 		printf("nindir\t%d\tinopb\t%d\tnspf\t%d\n", fs->fs_nindir,
    363 		    fs->fs_inopb, fs->fs_old_nspf);
    364 	else
    365 		printf("nindir\t%d\tinopb\t%d\n", fs->fs_nindir, fs->fs_inopb);
    366 	if (!printold || (fs->fs_avgfilesize > 0) || (fs->fs_avgfpdir > 0))
    367 		printf("avgfilesize %d\tavgfpdir %d\n",
    368 		    fs->fs_avgfilesize, fs->fs_avgfpdir);
    369 	printf("sblkno\t%d\tcblkno\t%d\tiblkno\t%d\tdblkno\t%d\n",
    370 	    fs->fs_sblkno, fs->fs_cblkno, fs->fs_iblkno, fs->fs_dblkno);
    371 	printf("sbsize\t%d\tcgsize\t%d", fs->fs_sbsize, fs->fs_cgsize);
    372 	if (printold)
    373 		printf("\toffset\t%d\tmask\t0x%08x",
    374 		    fs->fs_old_cgoffset, fs->fs_old_cgmask);
    375 	printf("\ncsaddr\t%lld\tcssize\t%d",
    376 	    (long long)fs->fs_csaddr, fs->fs_cssize);
    377 	if (printold)
    378 		printf("\tshift\t%d\tmask\t0x%08x",
    379 		    fs->fs_old_csshift, fs->fs_old_csmask);
    380 	printf("\ncgrotor\t%d\tfmod\t%d\tronly\t%d\tclean\t0x%02x\n",
    381 	    fs->fs_cgrotor, fs->fs_fmod, fs->fs_ronly, fs->fs_clean);
    382 	printf("wapbl version 0x%x\tlocation %u\tflags 0x%x\n",
    383 	    fs->fs_journal_version, fs->fs_journal_location,
    384 	    fs->fs_journal_flags);
    385 	printf("wapbl loc0 %" PRIu64 "\tloc1 %" PRIu64,
    386 	    fs->fs_journallocs[0], fs->fs_journallocs[1]);
    387 	printf("\tloc1 %" PRIu64 "\tloc2 %" PRIu64 "\n",
    388 	    fs->fs_journallocs[2], fs->fs_journallocs[3]);
    389 	printf("flags\t");
    390 	if (fs->fs_flags == 0)
    391 		printf("none");
    392 	if (fs->fs_flags & FS_UNCLEAN)
    393 		printf("unclean ");
    394 	if (fs->fs_flags & FS_DOSOFTDEP)
    395 		printf("soft-updates ");
    396 	if (fs->fs_flags & FS_NEEDSFSCK)
    397 		printf("needs fsck run ");
    398 	if (fs->fs_flags & FS_INDEXDIRS)
    399 		printf("indexed directories ");
    400 	if (fs->fs_flags & FS_ACLS)
    401 		printf("acls ");
    402 	if (fs->fs_flags & FS_MULTILABEL)
    403 		printf("multilabel ");
    404 	if (fs->fs_flags & FS_FLAGS_UPDATED)
    405 		printf("fs_flags expanded ");
    406 	if (fs->fs_flags & FS_DOWAPBL)
    407 		printf("wapbl ");
    408 	fsflags = fs->fs_flags & ~(FS_UNCLEAN | FS_DOSOFTDEP | FS_NEEDSFSCK |
    409 			FS_INDEXDIRS | FS_ACLS | FS_MULTILABEL |
    410 			FS_FLAGS_UPDATED | FS_DOWAPBL);
    411 	if (fsflags != 0)
    412 		printf("unknown flags (%#x)", fsflags);
    413 	printf("\nfsmnt\t%s\n", fs->fs_fsmnt);
    414 	if (!printold)
    415 		printf("volname\t%s\tswuid\t%ju\n",
    416 		    fs->fs_volname, (uintmax_t)fs->fs_swuid);
    417 	if (printold) {
    418 		if (fs->fs_old_cpc != 0)
    419 			printf("blocks available in each of %d rotational "
    420 			       "positions\n", fs->fs_old_nrpos);
    421 		else
    422 			printf("(no rotational position table)\n\n");
    423 		if (ISOPT(opt_verbose)) {
    424 			int c, j, k;
    425 			for (c = 0; c < fs->fs_old_cpc; c++) {
    426 				printf("cylinder number %d:", c);
    427 				for (i = 0; i < fs->fs_old_nrpos; i++) {
    428 					if (old_fs_postbl(&afs, c, opostbl)[i] == -1)
    429 						continue;
    430 					printf("\n   position %d:\t", i);
    431 					for (j = old_fs_postbl(&afs, c, opostbl)[i], k = 1; ;
    432 							 j += old_fs_rotbl(&afs)[j], k++) {
    433 						printf("%5d", j);
    434 						if (k % 12 == 0)
    435 							printf("\n\t\t");
    436 						if (old_fs_rotbl(&afs)[j] == 0)
    437 							break;
    438 					}
    439 				}
    440 				printf("\n");
    441 			}
    442 		}
    443 	}
    444 
    445 	return 0;
    446 }
    447 
    448 int
    449 print_cgsum(const char *name, int fd)
    450 {
    451 	struct csum *ccsp;
    452 	int i, j, size;
    453 
    454 	afs.fs_csp = calloc(1, afs.fs_cssize);
    455 	for (i = 0, j = 0; i < afs.fs_cssize; i += afs.fs_bsize, j++) {
    456 		size = afs.fs_cssize - i < afs.fs_bsize ?
    457 		    afs.fs_cssize - i : afs.fs_bsize;
    458 		ccsp = (struct csum *)((char *)afs.fs_csp + i);
    459 		if (lseek(fd,
    460 		    (off_t)(fsbtodb(&afs, (afs.fs_csaddr + j * afs.fs_frag))) *
    461 		    dev_bsize, SEEK_SET) == (off_t)-1)
    462 			return 1;
    463 		if (read(fd, ccsp, size) != size)
    464 			return 1;
    465 		if (needswap)
    466 			ffs_csum_swap(ccsp, ccsp, size);
    467 	}
    468 
    469 	printf("cs[].cs_(nbfree,ndir,nifree,nffree):\n\t");
    470 	for (i = 0; i < afs.fs_ncg; i++) {
    471 		struct csum *cs = &afs.fs_cs(&afs, i);
    472 		if (i && i % 4 == 0)
    473 			printf("\n\t");
    474 		printf("(%d,%d,%d,%d) ",
    475 		    cs->cs_nbfree, cs->cs_ndir, cs->cs_nifree, cs->cs_nffree);
    476 	}
    477 	printf("\n");
    478 	if (printold && (afs.fs_old_ncyl % afs.fs_old_cpg)) {
    479 		printf("cylinders in last group %d\n",
    480 		    i = afs.fs_old_ncyl % afs.fs_old_cpg);
    481 		printf("blocks in last group %d\n",
    482 		    i * afs.fs_old_spc / (afs.fs_old_nspf << afs.fs_fragshift));
    483 	}
    484 	free(afs.fs_csp);
    485 	afs.fs_csp = NULL;
    486 
    487 	return 0;
    488 }
    489 
    490 int
    491 print_alt_super(const char *name, int fd)
    492 {
    493 	union fsun alt;
    494 	int i;
    495 	off_t loc;
    496 	uint16_t alt_opostblsave[32*8];
    497 	int save_printold;
    498 
    499 	for (i = 0; i < afs.fs_ncg; i++) {
    500 		loc = (off_t)(fsbtodb(&afs, cgsblock(&afs, i))) * dev_bsize;
    501 		printf("\nalternate %d\n", i);
    502 		if (pread(fd, &alt, sizeof alt, loc) != sizeof alt) {
    503 			warnx("%s: error reading alt %d", name, i);
    504 			return (1);
    505 		}
    506 		save_printold = printold;
    507 		fix_superblock(&alt.fs, alt_opostblsave);
    508 		if (print_superblock(&alt.fs, alt_opostblsave, name, fd, loc)) {
    509 			printold = save_printold;
    510 			return 1;
    511 		}
    512 		printold = save_printold;
    513 	}
    514 	return 0;
    515 }
    516 
    517 int
    518 print_cginfo(const char *name, int fd)
    519 {
    520 	int i;
    521 
    522 	printf("\n");
    523 	for (i = 0; i < afs.fs_ncg; i++) {
    524 		printf("\n");
    525 		if (dumpcg(name, fd, i))
    526 			return 1;
    527 		if (ISOPT(opt_inodes) && print_inodes(name, fd, i, 1))
    528 			return 1;
    529 	}
    530 	return 0;
    531 }
    532 
    533 int
    534 print_inodes(const char *name, int fd, int c, int n)
    535 {
    536 	void *ino_buf = malloc(afs.fs_bsize);
    537 	void (*print_inode)(int, int, void *);
    538 	int i, inum;
    539 
    540 	if (ino_buf == 0)
    541 		return 1;
    542 
    543 	print_inode = is_ufs2 ? print_ufs2_inode : print_ufs1_inode;
    544 
    545 	for (inum = c * afs.fs_ipg ; inum < (c+n) * afs.fs_ipg; inum += afs.fs_inopb) {
    546 		if (pread(fd, ino_buf, afs.fs_bsize,
    547 		    ino_to_fsba(&afs, inum) * afs.fs_fsize) != afs.fs_bsize) {
    548 			free(ino_buf);
    549 			return 1;
    550 		}
    551 		for (i = 0; i < afs.fs_inopb; i++)
    552 			print_inode(inum + i, i, ino_buf);
    553 	}
    554 
    555 	free(ino_buf);
    556 	return 0;
    557 }
    558 
    559 int
    560 dumpcg(const char *name, int fd, int c)
    561 {
    562 	off_t cur;
    563 	int i, j;
    564 	time_t t;
    565 
    566 	printf("cg %d:\n", c);
    567 	if ((cur = lseek(fd, (off_t)(fsbtodb(&afs, cgtod(&afs, c))) * dev_bsize,
    568 	    SEEK_SET)) == (off_t)-1)
    569 		return (1);
    570 	if (read(fd, &acg, afs.fs_bsize) != afs.fs_bsize) {
    571 		warnx("%s: error reading cg", name);
    572 		return (1);
    573 	}
    574 	if (needswap)
    575 		ffs_cg_swap(&acg, &acg, &afs);
    576 	if (printold)
    577 		t = acg.cg_old_time;
    578 	else
    579 		t = acg.cg_time;
    580 	printf("magic\t%x\ttell\t%llx\ttime\t%s",
    581 	    afs.fs_old_postblformat == FS_42POSTBLFMT ?
    582 	    ((struct ocg *)&acg)->cg_magic : acg.cg_magic,
    583 	    (long long)cur, ctime(&t));
    584 	if (printold)
    585 		printf("cgx\t%d\tncyl\t%d\tniblk\t%d\tndblk\t%d\n",
    586 		    acg.cg_cgx, acg.cg_old_ncyl, acg.cg_old_niblk,
    587 		    acg.cg_ndblk);
    588 	else
    589 		printf("cgx\t%d\tniblk\t%d\tndblk\t%d\n",
    590 		    acg.cg_cgx, acg.cg_niblk, acg.cg_ndblk);
    591 	printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
    592 	    acg.cg_cs.cs_nbfree, acg.cg_cs.cs_ndir,
    593 	    acg.cg_cs.cs_nifree, acg.cg_cs.cs_nffree);
    594 	printf("rotor\t%d\tirotor\t%d\tfrotor\t%d\nfrsum",
    595 	    acg.cg_rotor, acg.cg_irotor, acg.cg_frotor);
    596 	for (i = 1, j = 0; i < afs.fs_frag; i++) {
    597 		printf("\t%d", acg.cg_frsum[i]);
    598 		j += i * acg.cg_frsum[i];
    599 	}
    600 	printf("\nsum of frsum: %d", j);
    601 	if (afs.fs_contigsumsize > 0) {
    602 		for (i = 1; i < afs.fs_contigsumsize; i++) {
    603 			if ((i - 1) % 8 == 0)
    604 				printf("\nclusters %d-%d:", i,
    605 				    afs.fs_contigsumsize - 1 < i + 7 ?
    606 				    afs.fs_contigsumsize - 1 : i + 7);
    607 			printf("\t%d", cg_clustersum(&acg, 0)[i]);
    608 		}
    609 		printf("\nclusters size %d and over: %d\n",
    610 		    afs.fs_contigsumsize,
    611 		    cg_clustersum(&acg, 0)[afs.fs_contigsumsize]);
    612 		printf("clusters free:\t");
    613 		pbits(0, cg_clustersfree(&acg, 0), acg.cg_nclusterblks);
    614 	} else
    615 		printf("\n");
    616 	printf("iused:\t");
    617 	pbits(0 * c * afs.fs_ipg, cg_inosused(&acg, 0), afs.fs_ipg);
    618 	printf("free:\t");
    619 	pbits(0, cg_blksfree(&acg, 0), afs.fs_fpg);
    620 	if (printold && ISOPT(opt_verbose)) {
    621 		printf("b:\n");
    622 		for (i = 0; i < afs.fs_old_cpg; i++) {
    623 			if (old_cg_blktot(&acg, 0)[i] == 0)
    624 				continue;
    625 			printf("   c%d:\t(%d)\t", i, old_cg_blktot(&acg, 0)[i]);
    626 			for (j = 0; j < afs.fs_old_nrpos; j++) {
    627 				if (afs.fs_old_cpc > 0 &&
    628 						old_fs_postbl(&afs, i % afs.fs_old_cpc, opostblsave)[j] == -1)
    629 					continue;
    630 				printf(" %d", old_cg_blks(&afs, &acg, i, 0)[j]);
    631 			}
    632 			printf("\n");
    633 		}
    634 	}
    635 	return (0);
    636 }
    637 
    638 void
    639 print_ufs1_inode(int inum, int i_off, void *ibuf)
    640 {
    641 	struct ufs1_dinode *i = ibuf;
    642 
    643 	i += i_off;
    644 
    645 	if (needswap)
    646 		ffs_dinode1_swap(i,i);
    647 
    648 	if (afs.fs_old_inodefmt < FS_44INODEFMT) {
    649 		i->di_uid = i->di_ouid;
    650 		i->di_gid = i->di_ogid;
    651 	}
    652 
    653 	if (inum % afs.fs_ipg == 0)
    654 		printf("   inode:   mode  nlink                 size"
    655 		    "      ctime.nsec         flags     blocks"
    656 		    " generation      uid        gid\n");
    657 	if (i->di_mode == 0 && i->di_nlink == 0 && !ISOPT(opt_verbose))
    658 		return;
    659 	printf("%8u: %6o %6d %20" PRIu64 " %10u.%09u %8x %10u %8x %10u %10u\n",
    660 		inum, i->di_mode, i->di_nlink, i->di_size,
    661 		i->di_ctime, i->di_ctimensec, i->di_flags, i->di_blocks,
    662 		i->di_gen, i->di_uid, i->di_gid);
    663 }
    664 
    665 void
    666 print_ufs2_inode(int inum, int i_off, void *ibuf)
    667 {
    668 	struct ufs2_dinode *i = ibuf;
    669 
    670 	i += i_off;
    671 
    672 	if (needswap)
    673 		ffs_dinode2_swap(i,i);
    674 
    675 	if (inum % afs.fs_ipg == 0)
    676 		printf("   inode:   mode  nlink                 size"
    677 		    "      ctime.nsec         flags     blocks"
    678 		    " generation      uid        gid\n");
    679 
    680 	if (i->di_mode == 0 && i->di_nlink == 0 && !ISOPT(opt_verbose))
    681 		return;
    682 
    683 	printf("%8u: %6o %6d %20" PRIu64 " %10" PRIu64 ".%09u %8x %10" PRIu64 " %8x %10u %10u\n",
    684 		inum, i->di_mode, i->di_nlink, i->di_size,
    685 		i->di_ctime, i->di_ctimensec, i->di_flags, i->di_blocks,
    686 		i->di_gen, i->di_uid, i->di_gid);
    687 }
    688 
    689 
    690 void
    691 pbits(int offset, void *vp, int max)
    692 {
    693 	int i;
    694 	char *p;
    695 	int count, j;
    696 
    697 	for (count = i = 0, p = vp; i < max; i++)
    698 		if (isset(p, i)) {
    699 			if (count)
    700 				printf(",%s", count % 6 ? " " : "\n\t");
    701 			count++;
    702 			printf("%d", offset + i);
    703 			j = i;
    704 			while ((i+1)<max && isset(p, i+1))
    705 				i++;
    706 			if (i != j)
    707 				printf("-%d", offset + i);
    708 		}
    709 	printf("\n");
    710 }
    711 
    712 void
    713 usage(void)
    714 {
    715 
    716 	(void)fprintf(stderr, "usage: dumpfs [-acFimsv] filesys | device [...]\n");
    717 	exit(1);
    718 }
    719 
    720 int
    721 openpartition(const char *name, int flags, char *device, size_t devicelen)
    722 {
    723 	char		rawspec[MAXPATHLEN], *p;
    724 	struct fstab	*fs;
    725 	int		fd, oerrno;
    726 
    727 	fs = getfsfile(name);
    728 	if (fs) {
    729 		if ((p = strrchr(fs->fs_spec, '/')) != NULL) {
    730 			snprintf(rawspec, sizeof(rawspec), "%.*s/r%s",
    731 			    (int)(p - fs->fs_spec), fs->fs_spec, p + 1);
    732 			name = rawspec;
    733 		} else
    734 			name = fs->fs_spec;
    735 	}
    736 	fd = opendisk(name, flags, device, devicelen, 0);
    737 	if (fd == -1 && errno == ENOENT) {
    738 		oerrno = errno;
    739 		strlcpy(device, name, devicelen);
    740 		errno = oerrno;
    741 	}
    742 	return (fd);
    743 }
    744