Home | History | Annotate | Line # | Download | only in dumpfs
dumpfs.c revision 1.34
      1 /*	$NetBSD: dumpfs.c,v 1.34 2003/04/02 22:50:52 he 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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT("@(#) Copyright (c) 1983, 1992, 1993\n\
     39 	The Regents of the University of California.  All rights reserved.\n");
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)dumpfs.c	8.5 (Berkeley) 4/29/95";
     45 #else
     46 __RCSID("$NetBSD: dumpfs.c,v 1.34 2003/04/02 22:50:52 he Exp $");
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <sys/param.h>
     51 #include <sys/time.h>
     52 
     53 #include <ufs/ufs/dinode.h>
     54 #include <ufs/ufs/ufs_bswap.h>
     55 #include <ufs/ffs/fs.h>
     56 #include <ufs/ffs/ffs_extern.h>
     57 
     58 #include <err.h>
     59 #include <errno.h>
     60 #include <fcntl.h>
     61 #include <fstab.h>
     62 #include <stdio.h>
     63 #include <stdlib.h>
     64 #include <string.h>
     65 #include <unistd.h>
     66 #include <util.h>
     67 
     68 union {
     69 	struct fs fs;
     70 	char pad[MAXBSIZE];
     71 } fsun;
     72 #define	afs	fsun.fs
     73 
     74 union {
     75 	struct cg cg;
     76 	char pad[MAXBSIZE];
     77 } cgun;
     78 #define	acg	cgun.cg
     79 
     80 long	dev_bsize = 1;
     81 int	needswap, Fflag;
     82 int	is_ufs2;
     83 
     84 int	dumpfs(const char *);
     85 int	dumpcg(const char *, int, int);
     86 int	main(int, char **);
     87 int	openpartition(const char *, int, char *, size_t);
     88 void	pbits(void *, int);
     89 void	usage(void);
     90 void	swap_cg(struct cg *);
     91 
     92 int
     93 main(int argc, char *argv[])
     94 {
     95 	int ch, eval;
     96 
     97 	while ((ch = getopt(argc, argv, "F")) != -1)
     98 		switch(ch) {
     99 		case 'F':
    100 			Fflag = 1;
    101 			break;
    102 		case '?':
    103 		default:
    104 			usage();
    105 		}
    106 	argc -= optind;
    107 	argv += optind;
    108 
    109 	if (argc < 1)
    110 		usage();
    111 
    112 	for (eval = 0; *argv; ++argv) {
    113 		eval |= dumpfs(*argv);
    114 		printf("\n");
    115 	}
    116 
    117 	exit(eval);
    118 }
    119 
    120 off_t sblock_try[] = SBLOCKSEARCH;
    121 
    122 int
    123 dumpfs(const char *name)
    124 {
    125 	int fd, i, j, size, printold;
    126 	time_t t;
    127 	char device[MAXPATHLEN];
    128 	struct csum *ccsp;
    129 
    130 	if (Fflag)
    131 		fd = open(name, O_RDONLY);
    132 	else {
    133 		fd = openpartition(name, O_RDONLY, device, sizeof(device));
    134 		name = device;
    135 	}
    136 	if (fd == -1)
    137 		goto err;
    138 
    139 	for (i = 0; sblock_try[i] != -1; i++) {
    140 		if (lseek(fd, sblock_try[i], SEEK_SET) == (off_t)-1)
    141 			continue;
    142 		if (read(fd, &afs, SBLOCKSIZE) != SBLOCKSIZE)
    143 			continue;
    144 		switch(afs.fs_magic) {
    145 		case FS_UFS2_MAGIC:
    146 			is_ufs2 = 1;
    147 			/*FALLTHROUGH*/
    148 		case FS_UFS1_MAGIC:
    149 			goto found;
    150 		case FS_UFS2_MAGIC_SWAPPED:
    151 			/*FALLTHROUGH*/
    152 		case FS_UFS1_MAGIC_SWAPPED:
    153 			needswap = 1;
    154 			goto found;
    155 		default:
    156 			continue;
    157 		}
    158 	}
    159 	warnx("%s: could not find superblock, skipped", name);
    160 	return 1;
    161 found:
    162 
    163 	if (needswap)
    164 		ffs_sb_swap(&afs, &afs);
    165 
    166 	printold = (afs.fs_magic == FS_UFS1_MAGIC &&
    167 		    (afs.fs_old_flags & FS_FLAGS_UPDATED) == 0);
    168 	if (printold) {
    169 		afs.fs_flags = afs.fs_old_flags;
    170 		afs.fs_maxbsize = afs.fs_bsize;
    171 		afs.fs_time = afs.fs_old_time;
    172 		afs.fs_size = afs.fs_old_size;
    173 		afs.fs_dsize = afs.fs_old_dsize;
    174 		afs.fs_csaddr = afs.fs_old_csaddr;
    175 		afs.fs_cstotal.cs_ndir = afs.fs_old_cstotal.cs_ndir;
    176 		afs.fs_cstotal.cs_nbfree = afs.fs_old_cstotal.cs_nbfree;
    177 		afs.fs_cstotal.cs_nifree = afs.fs_old_cstotal.cs_nifree;
    178 		afs.fs_cstotal.cs_nffree = afs.fs_old_cstotal.cs_nffree;
    179 	}
    180 
    181 	printf("file system: %s\n", name);
    182 #if BYTE_ORDER == LITTLE_ENDIAN
    183 	if (needswap)
    184 #else
    185 	if (!needswap)
    186 #endif
    187 		printf("endian\tbig-endian\n");
    188 	else
    189 		printf("endian\tlittle-endian\n");
    190 	if (printold && afs.fs_old_postblformat == FS_42POSTBLFMT)
    191 		afs.fs_old_nrpos = 8;
    192 	dev_bsize = afs.fs_fsize / fsbtodb(&afs, 1);
    193 	t = afs.fs_time;
    194 	printf("location%lld\tmagic\t%x\ttime\t%s", (long long)sblock_try[i],
    195 	    afs.fs_magic, ctime(&t));
    196 	i = 0;
    197 	if (printold && afs.fs_old_postblformat != FS_42POSTBLFMT) {
    198 		i++;
    199 		if (afs.fs_old_inodefmt >= FS_44INODEFMT) {
    200 			int max;
    201 
    202 			i++;
    203 			max = afs.fs_maxcontig;
    204 			size = afs.fs_contigsumsize;
    205 			if ((max < 2 && size == 0)
    206 			    || (max > 1 && size >= MIN(max, FS_MAXCONTIG)))
    207 				i++;
    208 		}
    209 	}
    210 	printf("id\t[ %x %x ]\n", afs.fs_id[0], afs.fs_id[1]);
    211 	printf("cylgrp\t%s\tinodes\t%s\tfslevel %d\tsoftdep %sabled\n",
    212 	    i < 1 ? "static" : "dynamic", i < 2 ? "4.2/4.3BSD" : "4.4BSD", i,
    213 	    (afs.fs_flags & FS_DOSOFTDEP) ? "en" : "dis");
    214 	printf("nbfree\t%lld\tndir\t%lld\tnifree\t%lld\tnffree\t%lld\n",
    215 	    (long long)afs.fs_cstotal.cs_nbfree,
    216 	    (long long)afs.fs_cstotal.cs_ndir,
    217 	    (long long)afs.fs_cstotal.cs_nifree,
    218 	    (long long)afs.fs_cstotal.cs_nffree);
    219 	printf("ncg\t%d\tsize\t%lld\tblocks\t%lld\n",
    220 	    afs.fs_ncg, (long long)afs.fs_size, (long long)afs.fs_dsize);
    221 	if (printold)
    222 		printf("ncyl\t%d\n", afs.fs_old_ncyl);
    223 	printf("bsize\t%d\tshift\t%d\tmask\t0x%08x\n",
    224 	    afs.fs_bsize, afs.fs_bshift, afs.fs_bmask);
    225 	printf("fsize\t%d\tshift\t%d\tmask\t0x%08x\n",
    226 	    afs.fs_fsize, afs.fs_fshift, afs.fs_fmask);
    227 	printf("frag\t%d\tshift\t%d\tfsbtodb\t%d\n",
    228 	    afs.fs_frag, afs.fs_fragshift, afs.fs_fsbtodb);
    229 	printf("bpg\t%d\tfpg\t%d\tipg\t%d\n",
    230 	    afs.fs_fpg / afs.fs_frag, afs.fs_fpg, afs.fs_ipg);
    231 	printf("minfree\t%d%%\toptim\t%s\tmaxcontig %d\tmaxbpg\t%d\n",
    232 	    afs.fs_minfree, afs.fs_optim == FS_OPTSPACE ? "space" : "time",
    233 	    afs.fs_maxcontig, afs.fs_maxbpg);
    234 	if (printold) {
    235 		printf("cpg\t%d\trotdelay %dms\trps\t%d\n",
    236 		    afs.fs_old_cpg, afs.fs_old_rotdelay, afs.fs_old_rps);
    237 		printf("ntrak\t%d\tnsect\t%d\tnpsect\t%d\tspc\t%d\n",
    238 		    afs.fs_spare2, afs.fs_old_nsect, afs.fs_old_npsect,
    239 		    afs.fs_old_spc);
    240 		printf("trackskew %d\tinterleave %d\n",
    241 		    afs.fs_old_trackskew, afs.fs_old_interleave);
    242 	}
    243 	printf("symlinklen %d\tcontigsumsize %d\n",
    244 	    afs.fs_maxsymlinklen, afs.fs_contigsumsize);
    245 	printf("maxfilesize 0x%016llx\n",
    246 	    (unsigned long long)afs.fs_maxfilesize);
    247 	printf("nindir\t%d\tinopb\t%d\n", afs.fs_nindir, afs.fs_inopb);
    248 	if (printold)
    249 		printf("nspf\t%d\n", afs.fs_old_nspf);
    250 	printf("avgfilesize %d\tavgfpdir %d\n",
    251 	    afs.fs_avgfilesize, afs.fs_avgfpdir);
    252 	printf("sblkno\t%d\tcblkno\t%d\tiblkno\t%d\tdblkno\t%d\n",
    253 	    afs.fs_sblkno, afs.fs_cblkno, afs.fs_iblkno, afs.fs_dblkno);
    254 	printf("sbsize\t%d\tcgsize\t%d\n", afs.fs_sbsize, afs.fs_cgsize);
    255 	if (printold)
    256 		printf("offset\t%d\tmask\t0x%08x\n",
    257 		    afs.fs_old_cgoffset, afs.fs_old_cgmask);
    258 	printf("csaddr\t%lld\tcssize %d\n",
    259 	    (long long)afs.fs_csaddr, afs.fs_cssize);
    260 	if (printold)
    261 		printf("shift\t%d\tmask\t0x%08x\n",
    262 		    afs.fs_spare1[0], afs.fs_spare1[1]);
    263 	printf("cgrotor\t%d\tfmod\t%d\tronly\t%d\tclean\t0x%02x\n",
    264 	    afs.fs_cgrotor, afs.fs_fmod, afs.fs_ronly, afs.fs_clean);
    265 	if (printold) {
    266 		if (afs.fs_old_cpc != 0)
    267 			printf("blocks available in each of %d rotational "
    268 			       "positions", afs.fs_old_nrpos);
    269 		else
    270 			printf("(no rotational position table)\n");
    271 	}
    272 	printf("\ncs[].cs_(nbfree,ndir,nifree,nffree):\n\t");
    273 	afs.fs_csp = calloc(1, afs.fs_cssize);
    274 	for (i = 0, j = 0; i < afs.fs_cssize; i += afs.fs_bsize, j++) {
    275 		size = afs.fs_cssize - i < afs.fs_bsize ?
    276 		    afs.fs_cssize - i : afs.fs_bsize;
    277 		ccsp = (struct csum *)((char *)afs.fs_csp + i);
    278 		if (lseek(fd,
    279 		    (off_t)(fsbtodb(&afs, (afs.fs_csaddr + j * afs.fs_frag))) *
    280 		    dev_bsize, SEEK_SET) == (off_t)-1)
    281 			goto err;
    282 		if (read(fd, ccsp, size) != size)
    283 			goto err;
    284 		if (needswap)
    285 			ffs_csum_swap(ccsp, ccsp, size);
    286 	}
    287 	for (i = 0; i < afs.fs_ncg; i++) {
    288 		struct csum *cs = &afs.fs_cs(&afs, i);
    289 		if (i && i % 4 == 0)
    290 			printf("\n\t");
    291 		printf("(%d,%d,%d,%d) ",
    292 		    cs->cs_nbfree, cs->cs_ndir, cs->cs_nifree, cs->cs_nffree);
    293 	}
    294 	printf("\n");
    295 	if (printold && (afs.fs_old_ncyl % afs.fs_old_cpg)) {
    296 		printf("cylinders in last group %d\n",
    297 		    i = afs.fs_old_ncyl % afs.fs_old_cpg);
    298 		printf("blocks in last group %d\n",
    299 		    i * afs.fs_old_spc / (afs.fs_old_nspf << afs.fs_fragshift));
    300 	}
    301 	printf("\n");
    302 	for (i = 0; i < afs.fs_ncg; i++)
    303 		if (dumpcg(name, fd, i))
    304 			goto err;
    305 	(void)close(fd);
    306 	return 0;
    307 
    308 err:	if (fd != -1)
    309 		(void)close(fd);
    310 	warn("%s", name);
    311 	return 1;
    312 };
    313 
    314 int
    315 dumpcg(const char *name, int fd, int c)
    316 {
    317 	off_t cur;
    318 	int i, j;
    319 	time_t t;
    320 
    321 	printf("\ncg %d:\n", c);
    322 	if ((cur = lseek(fd, (off_t)(fsbtodb(&afs, cgtod(&afs, c))) * dev_bsize,
    323 	    SEEK_SET)) == (off_t)-1)
    324 		return (1);
    325 	if (read(fd, &acg, afs.fs_bsize) != afs.fs_bsize) {
    326 		warnx("%s: error reading cg", name);
    327 		return (1);
    328 	}
    329 	if (needswap)
    330 		ffs_cg_swap(&acg, &acg, &afs);
    331 	t = acg.cg_time;
    332 	printf("magic\t%x\ttell\t%llx\ttime\t%s",
    333 	    afs.fs_old_postblformat == FS_42POSTBLFMT ?
    334 	    ((struct ocg *)&acg)->cg_magic : acg.cg_magic,
    335 	    (long long)cur, ctime(&t));
    336 	printf("cgx\t%d\tniblk\t%d\tndblk\t%d\n",
    337 	    acg.cg_cgx, acg.cg_niblk, acg.cg_ndblk);
    338 	printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
    339 	    acg.cg_cs.cs_nbfree, acg.cg_cs.cs_ndir,
    340 	    acg.cg_cs.cs_nifree, acg.cg_cs.cs_nffree);
    341 	printf("rotor\t%d\tirotor\t%d\tfrotor\t%d\nfrsum",
    342 	    acg.cg_rotor, acg.cg_irotor, acg.cg_frotor);
    343 	for (i = 1, j = 0; i < afs.fs_frag; i++) {
    344 		printf("\t%d", acg.cg_frsum[i]);
    345 		j += i * acg.cg_frsum[i];
    346 	}
    347 	printf("\nsum of frsum: %d", j);
    348 	if (afs.fs_contigsumsize > 0) {
    349 		for (i = 1; i < afs.fs_contigsumsize; i++) {
    350 			if ((i - 1) % 8 == 0)
    351 				printf("\nclusters %d-%d:", i,
    352 				    afs.fs_contigsumsize - 1 < i + 7 ?
    353 				    afs.fs_contigsumsize - 1 : i + 7);
    354 			printf("\t%d", cg_clustersum(&acg, 0)[i]);
    355 		}
    356 		printf("\nclusters size %d and over: %d\n",
    357 		    afs.fs_contigsumsize,
    358 		    cg_clustersum(&acg, 0)[afs.fs_contigsumsize]);
    359 		printf("clusters free:\t");
    360 		pbits(cg_clustersfree(&acg, 0), acg.cg_nclusterblks);
    361 	} else
    362 		printf("\n");
    363 	printf("iused:\t");
    364 	pbits(cg_inosused(&acg, 0), afs.fs_ipg);
    365 	printf("free:\t");
    366 	pbits(cg_blksfree(&acg, 0), afs.fs_fpg);
    367 	return (0);
    368 };
    369 
    370 void
    371 pbits(void *vp, int max)
    372 {
    373 	int i;
    374 	char *p;
    375 	int count, j;
    376 
    377 	for (count = i = 0, p = vp; i < max; i++)
    378 		if (isset(p, i)) {
    379 			if (count)
    380 				printf(",%s", count % 6 ? " " : "\n\t");
    381 			count++;
    382 			printf("%d", i);
    383 			j = i;
    384 			while ((i+1)<max && isset(p, i+1))
    385 				i++;
    386 			if (i != j)
    387 				printf("-%d", i);
    388 		}
    389 	printf("\n");
    390 }
    391 
    392 void
    393 usage(void)
    394 {
    395 
    396 	(void)fprintf(stderr, "usage: dumpfs [-F] filesys | device [...]\n");
    397 	exit(1);
    398 }
    399 
    400 int
    401 openpartition(const char *name, int flags, char *device, size_t devicelen)
    402 {
    403 	char		rawspec[MAXPATHLEN], *p;
    404 	struct fstab	*fs;
    405 	int		fd, oerrno;
    406 
    407 	fs = getfsfile(name);
    408 	if (fs) {
    409 		if ((p = strrchr(fs->fs_spec, '/')) != NULL) {
    410 			snprintf(rawspec, sizeof(rawspec), "%.*s/r%s",
    411 			    (int)(p - fs->fs_spec), fs->fs_spec, p + 1);
    412 			name = rawspec;
    413 		} else
    414 			name = fs->fs_spec;
    415 	}
    416 	fd = opendisk(name, flags, device, devicelen, 0);
    417 	if (fd == -1 && errno == ENOENT) {
    418 		oerrno = errno;
    419 		strlcpy(device, name, devicelen);
    420 		errno = oerrno;
    421 	}
    422 	return (fd);
    423 }
    424