dumpfs.c revision 99826
11558Srgrimes/*
298542Smckusick * Copyright (c) 2002 Networks Associates Technology, Inc.
398542Smckusick * All rights reserved.
498542Smckusick *
598542Smckusick * This software was developed for the FreeBSD Project by Marshall
698542Smckusick * Kirk McKusick and Network Associates Laboratories, the Security
798542Smckusick * Research Division of Network Associates, Inc. under DARPA/SPAWAR
898542Smckusick * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
998542Smckusick * research program
1098542Smckusick *
1198542Smckusick * Copyright (c) 1982, 1989, 1993
1298542Smckusick *	The Regents of the University of California.  All rights reserved.
1398542Smckusick * (c) UNIX System Laboratories, Inc.
141558Srgrimes * Copyright (c) 1983, 1992, 1993
151558Srgrimes *	The Regents of the University of California.  All rights reserved.
161558Srgrimes *
171558Srgrimes * Redistribution and use in source and binary forms, with or without
181558Srgrimes * modification, are permitted provided that the following conditions
191558Srgrimes * are met:
201558Srgrimes * 1. Redistributions of source code must retain the above copyright
211558Srgrimes *    notice, this list of conditions and the following disclaimer.
221558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
231558Srgrimes *    notice, this list of conditions and the following disclaimer in the
241558Srgrimes *    documentation and/or other materials provided with the distribution.
251558Srgrimes * 3. All advertising materials mentioning features or use of this software
261558Srgrimes *    must display the following acknowledgement:
271558Srgrimes *	This product includes software developed by the University of
281558Srgrimes *	California, Berkeley and its contributors.
291558Srgrimes * 4. Neither the name of the University nor the names of its contributors
301558Srgrimes *    may be used to endorse or promote products derived from this software
311558Srgrimes *    without specific prior written permission.
321558Srgrimes *
331558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
341558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
351558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
361558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
371558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
381558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
391558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
401558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
411558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
421558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
431558Srgrimes * SUCH DAMAGE.
441558Srgrimes */
451558Srgrimes
461558Srgrimes#ifndef lint
4736998Scharnierstatic const char copyright[] =
481558Srgrimes"@(#) Copyright (c) 1983, 1992, 1993\n\
491558Srgrimes	The Regents of the University of California.  All rights reserved.\n";
501558Srgrimes#endif /* not lint */
511558Srgrimes
521558Srgrimes#ifndef lint
5336998Scharnier#if 0
5423673Speterstatic char sccsid[] = "@(#)dumpfs.c	8.5 (Berkeley) 4/29/95";
5536998Scharnier#endif
5636998Scharnierstatic const char rcsid[] =
5750476Speter  "$FreeBSD: head/sbin/dumpfs/dumpfs.c 99826 2002-07-11 21:44:03Z jmallett $";
581558Srgrimes#endif /* not lint */
591558Srgrimes
601558Srgrimes#include <sys/param.h>
611558Srgrimes#include <sys/time.h>
6296478Sphk#include <sys/disklabel.h>
631558Srgrimes
6498542Smckusick#include <ufs/ufs/dinode.h>
651558Srgrimes#include <ufs/ffs/fs.h>
661558Srgrimes
6723673Speter#include <err.h>
681558Srgrimes#include <fcntl.h>
691558Srgrimes#include <fstab.h>
7099826Sjmallett#include <libufs.h>
711558Srgrimes#include <stdio.h>
721558Srgrimes#include <stdlib.h>
7323673Speter#include <unistd.h>
741558Srgrimes
7599826Sjmallett#define	afs	disk.d_fs
761558Srgrimes
771558Srgrimesunion {
781558Srgrimes	struct cg cg;
791558Srgrimes	char pad[MAXBSIZE];
801558Srgrimes} cgun;
811558Srgrimes#define	acg	cgun.cg
821558Srgrimes
8399826Sjmallettstruct uufsd disk;
841558Srgrimes
8592839Simpint	dumpfs(const char *);
8692839Simpint	dumpcg(const char *, int, int);
8792839Simpvoid	pbits(void *, int);
8892839Simpvoid	usage(void) __dead2;
891558Srgrimes
901558Srgrimesint
9192839Simpmain(int argc, char *argv[])
921558Srgrimes{
9392806Sobrien	struct fstab *fs;
941558Srgrimes	int ch, eval;
951558Srgrimes
9623673Speter	while ((ch = getopt(argc, argv, "")) != -1)
971558Srgrimes		switch(ch) {
981558Srgrimes		case '?':
991558Srgrimes		default:
1001558Srgrimes			usage();
1011558Srgrimes		}
1021558Srgrimes	argc -= optind;
1031558Srgrimes	argv += optind;
1041558Srgrimes
1051558Srgrimes	if (argc < 1)
1061558Srgrimes		usage();
1071558Srgrimes
1081558Srgrimes	for (eval = 0; *argv; ++argv)
1091558Srgrimes		if ((fs = getfsfile(*argv)) == NULL)
1101558Srgrimes			eval |= dumpfs(*argv);
1111558Srgrimes		else
1121558Srgrimes			eval |= dumpfs(fs->fs_spec);
1131558Srgrimes	exit(eval);
1141558Srgrimes}
1151558Srgrimes
1161558Srgrimesint
11792839Simpdumpfs(const char *name)
1181558Srgrimes{
11935341Srnordier	ssize_t n;
12098542Smckusick	time_t time;
12198542Smckusick	int64_t fssize;
12299826Sjmallett	int c, i, j, k, size;
1231558Srgrimes
12499826Sjmallett	if (ufs_disk_fillout(&disk, name) == -1)
12598542Smckusick			goto err;
12623673Speter
12798542Smckusick	if (afs.fs_magic == FS_UFS2_MAGIC) {
12898542Smckusick		fssize = afs.fs_size;
12998542Smckusick		time = afs.fs_time;
13098542Smckusick		printf("magic\t%x (UFS2)\ttime\t%s",
13198542Smckusick		    afs.fs_magic, ctime(&time));
13298542Smckusick		printf("offset\t%qd\tid\t[ %x %x ]\n",
13398542Smckusick		    afs.fs_sblockloc, afs.fs_id[0], afs.fs_id[1]);
13498542Smckusick		printf("ncg\t%d\tsize\t%qd\tblocks\t%d\n",
13598542Smckusick		    afs.fs_ncg, fssize, afs.fs_dsize);
13698542Smckusick	} else {
13798542Smckusick		fssize = afs.fs_old_size;
13898542Smckusick		printf("magic\t%x (UFS1)\ttime\t%s",
13998542Smckusick		    afs.fs_magic, ctime(&afs.fs_old_time));
14098542Smckusick		printf("id\t[ %x %x ]\n", afs.fs_id[0], afs.fs_id[1]);
14198542Smckusick		printf("ncg\t%d\tsize\t%qd\tblocks\t%d\n",
14298542Smckusick		    afs.fs_ncg, fssize, afs.fs_dsize);
14398542Smckusick	}
1441558Srgrimes	printf("bsize\t%d\tshift\t%d\tmask\t0x%08x\n",
1451558Srgrimes	    afs.fs_bsize, afs.fs_bshift, afs.fs_bmask);
1461558Srgrimes	printf("fsize\t%d\tshift\t%d\tmask\t0x%08x\n",
1471558Srgrimes	    afs.fs_fsize, afs.fs_fshift, afs.fs_fmask);
1481558Srgrimes	printf("frag\t%d\tshift\t%d\tfsbtodb\t%d\n",
1491558Srgrimes	    afs.fs_frag, afs.fs_fragshift, afs.fs_fsbtodb);
15098542Smckusick	printf("minfree\t%d%%\toptim\t%s\tsymlinklen %d\n",
1511558Srgrimes	    afs.fs_minfree, afs.fs_optim == FS_OPTSPACE ? "space" : "time",
15298542Smckusick	    afs.fs_maxsymlinklen);
15398542Smckusick	if (afs.fs_magic == FS_UFS2_MAGIC) {
15498542Smckusick		printf("%s %d\tmaxbpg\t%d\tmaxcontig %d\tcontigsumsize %d\n",
15598542Smckusick		    "maxbsize", afs.fs_maxbsize, afs.fs_maxbpg,
15698542Smckusick		    afs.fs_maxcontig, afs.fs_contigsumsize);
15798542Smckusick		printf("nbfree\t%qd\tndir\t%qd\tnifree\t%qd\tnffree\t%qd\n",
15898542Smckusick		    afs.fs_cstotal.cs_nbfree, afs.fs_cstotal.cs_ndir,
15998542Smckusick		    afs.fs_cstotal.cs_nifree, afs.fs_cstotal.cs_nffree);
16098542Smckusick		printf("bpg\t%d\tfpg\t%d\tipg\t%d\n",
16198542Smckusick		    afs.fs_fpg / afs.fs_frag, afs.fs_fpg, afs.fs_ipg);
16298542Smckusick		printf("nindir\t%d\tinopb\t%d\tmaxfilesize\t%qu\n",
16398542Smckusick		    afs.fs_nindir, afs.fs_inopb, afs.fs_maxfilesize);
16498542Smckusick		printf("sbsize\t%d\tcgsize\t%d\tcsaddr\t%d\tcssize\t%d\n",
16598542Smckusick		    afs.fs_sbsize, afs.fs_cgsize, afs.fs_csaddr, afs.fs_cssize);
16698542Smckusick	} else {
16798542Smckusick		printf("maxbpg\t%d\tmaxcontig %d\tcontigsumsize %d\n",
16898542Smckusick		    afs.fs_maxbpg, afs.fs_maxcontig, afs.fs_contigsumsize);
16998542Smckusick		printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
17098542Smckusick		    afs.fs_old_cstotal.cs_nbfree, afs.fs_old_cstotal.cs_ndir,
17198542Smckusick		    afs.fs_old_cstotal.cs_nifree, afs.fs_old_cstotal.cs_nffree);
17298542Smckusick		printf("cpg\t%d\tbpg\t%d\tfpg\t%d\tipg\t%d\n",
17398542Smckusick		    afs.fs_old_cpg, afs.fs_fpg / afs.fs_frag, afs.fs_fpg,
17498542Smckusick		    afs.fs_ipg);
17598542Smckusick		printf("nindir\t%d\tinopb\t%d\tnspf\t%d\tmaxfilesize\t%qu\n",
17698542Smckusick		    afs.fs_nindir, afs.fs_inopb, afs.fs_old_nspf,
17798542Smckusick		    afs.fs_maxfilesize);
17898542Smckusick		printf("sbsize\t%d\tcgsize\t%d\tcgoffset %d\tcgmask\t0x%08x\n",
17998542Smckusick		    afs.fs_sbsize, afs.fs_cgsize, afs.fs_old_cgoffset,
18098542Smckusick		    afs.fs_old_cgmask);
18198542Smckusick		printf("csaddr\t%d\tcssize\t%d\n",
18298542Smckusick		    afs.fs_old_csaddr, afs.fs_cssize);
18398542Smckusick		printf("rotdelay %dms\trps\t%d\ttrackskew %d\tinterleave %d\n",
18498542Smckusick		    afs.fs_old_rotdelay, afs.fs_old_rps, afs.fs_old_trackskew,
18598542Smckusick		    afs.fs_old_interleave);
18698542Smckusick		printf("nsect\t%d\tnpsect\t%d\tspc\t%d\n",
18798542Smckusick		    afs.fs_old_nsect, afs.fs_old_npsect, afs.fs_old_spc);
18898542Smckusick	}
1891558Srgrimes	printf("sblkno\t%d\tcblkno\t%d\tiblkno\t%d\tdblkno\t%d\n",
1901558Srgrimes	    afs.fs_sblkno, afs.fs_cblkno, afs.fs_iblkno, afs.fs_dblkno);
1912154Sdg	printf("cgrotor\t%d\tfmod\t%d\tronly\t%d\tclean\t%d\n",
1922154Sdg	    afs.fs_cgrotor, afs.fs_fmod, afs.fs_ronly, afs.fs_clean);
19348875Smpp	printf("flags\t");
19448875Smpp	if (afs.fs_flags == 0)
19548875Smpp		printf("none");
19648875Smpp	if (afs.fs_flags & FS_UNCLEAN)
19748875Smpp			printf("unclean ");
19848875Smpp	if (afs.fs_flags & FS_DOSOFTDEP)
19948875Smpp			printf("soft-updates ");
20098542Smckusick	if (afs.fs_flags & FS_NEEDSFSCK)
20198542Smckusick			printf("needs fsck run ");
20298542Smckusick	if (afs.fs_flags & FS_INDEXDIRS)
20398542Smckusick			printf("indexed directories ");
20498542Smckusick	if ((afs.fs_flags &
20598542Smckusick	    ~(FS_UNCLEAN | FS_DOSOFTDEP | FS_NEEDSFSCK | FS_INDEXDIRS)) != 0)
20698542Smckusick			printf("unknown flags (%#x)", afs.fs_flags &
20798542Smckusick			    ~(FS_UNCLEAN | FS_DOSOFTDEP |
20898542Smckusick			      FS_NEEDSFSCK | FS_INDEXDIRS));
20948875Smpp	putchar('\n');
2101558Srgrimes	printf("\ncs[].cs_(nbfree,ndir,nifree,nffree):\n\t");
21171073Siedowse	afs.fs_csp = calloc(1, afs.fs_cssize);
21299826Sjmallett	if (lseek(disk.d_fd,
21399826Sjmallett	    (off_t)(fsbtodb(&afs, afs.fs_csaddr)) * (off_t)disk.d_bsize,
21498542Smckusick	    SEEK_SET) == (off_t)-1)
21598542Smckusick		goto err;
21699826Sjmallett	if (read(disk.d_fd, (char *)afs.fs_csp, afs.fs_cssize) != afs.fs_cssize)
21798542Smckusick		goto err;
2181558Srgrimes	for (i = 0; i < afs.fs_ncg; i++) {
2191558Srgrimes		struct csum *cs = &afs.fs_cs(&afs, i);
2201558Srgrimes		if (i && i % 4 == 0)
2211558Srgrimes			printf("\n\t");
2221558Srgrimes		printf("(%d,%d,%d,%d) ",
2231558Srgrimes		    cs->cs_nbfree, cs->cs_ndir, cs->cs_nifree, cs->cs_nffree);
2241558Srgrimes	}
2251558Srgrimes	printf("\n");
22698542Smckusick	if (fssize % afs.fs_fpg) {
22798542Smckusick		if (afs.fs_magic == FS_UFS1_MAGIC)
22898542Smckusick			printf("cylinders in last group %d\n",
22998542Smckusick			    howmany(afs.fs_old_size % afs.fs_fpg,
23098542Smckusick			    afs.fs_old_spc / afs.fs_old_nspf));
23198542Smckusick		printf("blocks in last group %d\n\n",
23298542Smckusick		    (fssize % afs.fs_fpg) / afs.fs_frag);
2331558Srgrimes	}
2341558Srgrimes	for (i = 0; i < afs.fs_ncg; i++)
23599826Sjmallett		if (dumpcg(name, disk.d_fd, i))
2361558Srgrimes			goto err;
23799826Sjmallett	ufs_disk_close(&disk);
2381558Srgrimes	return (0);
2391558Srgrimes
24099826Sjmalletterr:	ufs_disk_close(&disk);
24123673Speter	warn("%s", name);
2421558Srgrimes	return (1);
2431558Srgrimes};
2441558Srgrimes
2451558Srgrimesint
24692839Simpdumpcg(const char *name, int fd, int c)
2471558Srgrimes{
24898542Smckusick	time_t time;
2491558Srgrimes	off_t cur;
2501558Srgrimes	int i, j;
2511558Srgrimes
2521558Srgrimes	printf("\ncg %d:\n", c);
25316603Speter	if ((cur = lseek(fd, (off_t)(fsbtodb(&afs, cgtod(&afs, c))) *
25499826Sjmallett	    (off_t)disk.d_bsize, SEEK_SET)) == (off_t)-1)
2551558Srgrimes		return (1);
25699826Sjmallett	if (read(disk.d_fd, &acg, afs.fs_bsize) != afs.fs_bsize) {
25723673Speter		warnx("%s: error reading cg", name);
2581558Srgrimes		return (1);
2591558Srgrimes	}
26098542Smckusick	if (afs.fs_magic == FS_UFS2_MAGIC) {
26198542Smckusick		time = acg.cg_time;
26298542Smckusick		printf("magic\t%x\ttell\t%qx\ttime\t%s",
26398542Smckusick		    acg.cg_magic, cur, ctime(&time));
26498542Smckusick		printf("cgx\t%d\tndblk\t%d\tniblk\t%d\tinitiblk %d\n",
26598542Smckusick		    acg.cg_cgx, acg.cg_ndblk, acg.cg_niblk, acg.cg_initediblk);
26698542Smckusick	} else {
26798542Smckusick		printf("magic\t%x\ttell\t%qx\ttime\t%s",
26898542Smckusick		    acg.cg_magic, cur, ctime(&acg.cg_old_time));
26998542Smckusick		printf("cgx\t%d\tncyl\t%d\tniblk\t%d\tndblk\t%d\n",
27098542Smckusick		    acg.cg_cgx, acg.cg_old_ncyl, acg.cg_old_niblk,
27198542Smckusick		    acg.cg_ndblk);
27298542Smckusick	}
2731558Srgrimes	printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
2741558Srgrimes	    acg.cg_cs.cs_nbfree, acg.cg_cs.cs_ndir,
2751558Srgrimes	    acg.cg_cs.cs_nifree, acg.cg_cs.cs_nffree);
2761558Srgrimes	printf("rotor\t%d\tirotor\t%d\tfrotor\t%d\nfrsum",
2771558Srgrimes	    acg.cg_rotor, acg.cg_irotor, acg.cg_frotor);
2781558Srgrimes	for (i = 1, j = 0; i < afs.fs_frag; i++) {
2791558Srgrimes		printf("\t%d", acg.cg_frsum[i]);
2801558Srgrimes		j += i * acg.cg_frsum[i];
2811558Srgrimes	}
2821558Srgrimes	printf("\nsum of frsum: %d", j);
2831558Srgrimes	if (afs.fs_contigsumsize > 0) {
2841558Srgrimes		for (i = 1; i < afs.fs_contigsumsize; i++) {
2851558Srgrimes			if ((i - 1) % 8 == 0)
2861558Srgrimes				printf("\nclusters %d-%d:", i,
2871558Srgrimes				    afs.fs_contigsumsize - 1 < i + 7 ?
2881558Srgrimes				    afs.fs_contigsumsize - 1 : i + 7);
2891558Srgrimes			printf("\t%d", cg_clustersum(&acg)[i]);
2901558Srgrimes		}
2911558Srgrimes		printf("\nclusters size %d and over: %d\n",
2921558Srgrimes		    afs.fs_contigsumsize,
2931558Srgrimes		    cg_clustersum(&acg)[afs.fs_contigsumsize]);
2941558Srgrimes		printf("clusters free:\t");
2951558Srgrimes		pbits(cg_clustersfree(&acg), acg.cg_nclusterblks);
2961558Srgrimes	} else
2971558Srgrimes		printf("\n");
29898542Smckusick	printf("inodes used:\t");
2991558Srgrimes	pbits(cg_inosused(&acg), afs.fs_ipg);
30098542Smckusick	printf("blks free:\t");
3011558Srgrimes	pbits(cg_blksfree(&acg), afs.fs_fpg);
3021558Srgrimes	return (0);
3031558Srgrimes};
3041558Srgrimes
3051558Srgrimesvoid
30692839Simppbits(void *vp, int max)
3071558Srgrimes{
30892806Sobrien	int i;
30992806Sobrien	char *p;
3101558Srgrimes	int count, j;
3111558Srgrimes
3121558Srgrimes	for (count = i = 0, p = vp; i < max; i++)
3131558Srgrimes		if (isset(p, i)) {
3141558Srgrimes			if (count)
3151558Srgrimes				printf(",%s", count % 6 ? " " : "\n\t");
3161558Srgrimes			count++;
3171558Srgrimes			printf("%d", i);
3181558Srgrimes			j = i;
3191558Srgrimes			while ((i+1)<max && isset(p, i+1))
3201558Srgrimes				i++;
3211558Srgrimes			if (i != j)
3221558Srgrimes				printf("-%d", i);
3231558Srgrimes		}
3241558Srgrimes	printf("\n");
3251558Srgrimes}
3261558Srgrimes
3271558Srgrimesvoid
32892839Simpusage(void)
3291558Srgrimes{
3301558Srgrimes	(void)fprintf(stderr, "usage: dumpfs filesys | device\n");
3311558Srgrimes	exit(1);
3321558Srgrimes}
333