dumpfs.c revision 109519
1/*
2 * Copyright (c) 2002 Networks Associates Technology, Inc.
3 * All rights reserved.
4 *
5 * This software was developed for the FreeBSD Project by Marshall
6 * Kirk McKusick and Network Associates Laboratories, the Security
7 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
8 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
9 * research program
10 *
11 * Copyright (c) 1982, 1989, 1993
12 *	The Regents of the University of California.  All rights reserved.
13 * (c) UNIX System Laboratories, Inc.
14 * Copyright (c) 1983, 1992, 1993
15 *	The Regents of the University of California.  All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 *    notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 *    notice, this list of conditions and the following disclaimer in the
24 *    documentation and/or other materials provided with the distribution.
25 * 3. All advertising materials mentioning features or use of this software
26 *    must display the following acknowledgement:
27 *	This product includes software developed by the University of
28 *	California, Berkeley and its contributors.
29 * 4. Neither the name of the University nor the names of its contributors
30 *    may be used to endorse or promote products derived from this software
31 *    without specific prior written permission.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * SUCH DAMAGE.
44 */
45
46#ifndef lint
47static const char copyright[] =
48"@(#) Copyright (c) 1983, 1992, 1993\n\
49	The Regents of the University of California.  All rights reserved.\n";
50#endif /* not lint */
51
52#ifndef lint
53#if 0
54static char sccsid[] = "@(#)dumpfs.c	8.5 (Berkeley) 4/29/95";
55#endif
56static const char rcsid[] =
57  "$FreeBSD: head/sbin/dumpfs/dumpfs.c 109519 2003-01-19 05:51:36Z jmallett $";
58#endif /* not lint */
59
60#include <sys/param.h>
61#include <sys/time.h>
62#include <sys/disklabel.h>
63
64#include <ufs/ufs/dinode.h>
65#include <ufs/ffs/fs.h>
66
67#include <err.h>
68#include <errno.h>
69#include <fcntl.h>
70#include <fstab.h>
71#include <libufs.h>
72#include <stdio.h>
73#include <stdlib.h>
74#include <unistd.h>
75
76#define	afs	disk.d_fs
77#define	acg	disk.d_cg
78
79struct uufsd disk;
80
81int	dumpfs(const char *);
82int	dumpcg(void);
83void	pbits(void *, int);
84void	usage(void) __dead2;
85
86int
87main(int argc, char *argv[])
88{
89	int eval;
90
91	eval = 0;
92
93	while (getopt(argc, argv, "") != -1)
94		usage();
95	argc -= optind;
96	argv += optind;
97
98	if (argc < 1)
99		usage();
100
101	while (*argv != NULL)
102		eval |= dumpfs(*argv++);
103	exit(eval);
104}
105
106int
107dumpfs(const char *name)
108{
109	time_t time;
110	int64_t fssize;
111	int i;
112
113	if (ufs_disk_fillout(&disk, name) == -1)
114			goto err;
115
116	switch (disk.d_ufs) {
117	case 2:
118		fssize = afs.fs_size;
119		time = afs.fs_time;
120		printf("magic\t%x (UFS2)\ttime\t%s",
121		    afs.fs_magic, ctime(&time));
122		printf("superblock location\t%qd\tid\t[ %x %x ]\n",
123		    afs.fs_sblockloc, afs.fs_id[0], afs.fs_id[1]);
124		printf("ncg\t%d\tsize\t%qd\tblocks\t%d\n",
125		    afs.fs_ncg, fssize, afs.fs_dsize);
126		break;
127	case 1:
128		fssize = afs.fs_old_size;
129		time = afs.fs_old_time;
130		printf("magic\t%x (UFS1)\ttime\t%s",
131		    afs.fs_magic, ctime(&time));
132		printf("id\t[ %x %x ]\n", afs.fs_id[0], afs.fs_id[1]);
133		printf("ncg\t%d\tsize\t%qd\tblocks\t%d\n",
134		    afs.fs_ncg, fssize, afs.fs_dsize);
135		break;
136	default:
137		break;
138	}
139	printf("bsize\t%d\tshift\t%d\tmask\t0x%08x\n",
140	    afs.fs_bsize, afs.fs_bshift, afs.fs_bmask);
141	printf("fsize\t%d\tshift\t%d\tmask\t0x%08x\n",
142	    afs.fs_fsize, afs.fs_fshift, afs.fs_fmask);
143	printf("frag\t%d\tshift\t%d\tfsbtodb\t%d\n",
144	    afs.fs_frag, afs.fs_fragshift, afs.fs_fsbtodb);
145	printf("minfree\t%d%%\toptim\t%s\tsymlinklen %d\n",
146	    afs.fs_minfree, afs.fs_optim == FS_OPTSPACE ? "space" : "time",
147	    afs.fs_maxsymlinklen);
148	switch (disk.d_ufs) {
149	case 2:
150		printf("%s %d\tmaxbpg\t%d\tmaxcontig %d\tcontigsumsize %d\n",
151		    "maxbsize", afs.fs_maxbsize, afs.fs_maxbpg,
152		    afs.fs_maxcontig, afs.fs_contigsumsize);
153		printf("nbfree\t%qd\tndir\t%qd\tnifree\t%qd\tnffree\t%qd\n",
154		    afs.fs_cstotal.cs_nbfree, afs.fs_cstotal.cs_ndir,
155		    afs.fs_cstotal.cs_nifree, afs.fs_cstotal.cs_nffree);
156		printf("bpg\t%d\tfpg\t%d\tipg\t%d\n",
157		    afs.fs_fpg / afs.fs_frag, afs.fs_fpg, afs.fs_ipg);
158		printf("nindir\t%d\tinopb\t%d\tmaxfilesize\t%qu\n",
159		    afs.fs_nindir, afs.fs_inopb, afs.fs_maxfilesize);
160		printf("sbsize\t%d\tcgsize\t%d\tcsaddr\t%d\tcssize\t%d\n",
161		    afs.fs_sbsize, afs.fs_cgsize, afs.fs_csaddr, afs.fs_cssize);
162		break;
163	case 1:
164		printf("maxbpg\t%d\tmaxcontig %d\tcontigsumsize %d\n",
165		    afs.fs_maxbpg, afs.fs_maxcontig, afs.fs_contigsumsize);
166		printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
167		    afs.fs_old_cstotal.cs_nbfree, afs.fs_old_cstotal.cs_ndir,
168		    afs.fs_old_cstotal.cs_nifree, afs.fs_old_cstotal.cs_nffree);
169		printf("cpg\t%d\tbpg\t%d\tfpg\t%d\tipg\t%d\n",
170		    afs.fs_old_cpg, afs.fs_fpg / afs.fs_frag, afs.fs_fpg,
171		    afs.fs_ipg);
172		printf("nindir\t%d\tinopb\t%d\tnspf\t%d\tmaxfilesize\t%qu\n",
173		    afs.fs_nindir, afs.fs_inopb, afs.fs_old_nspf,
174		    afs.fs_maxfilesize);
175		printf("sbsize\t%d\tcgsize\t%d\tcgoffset %d\tcgmask\t0x%08x\n",
176		    afs.fs_sbsize, afs.fs_cgsize, afs.fs_old_cgoffset,
177		    afs.fs_old_cgmask);
178		printf("csaddr\t%d\tcssize\t%d\n",
179		    afs.fs_old_csaddr, afs.fs_cssize);
180		printf("rotdelay %dms\trps\t%d\ttrackskew %d\tinterleave %d\n",
181		    afs.fs_old_rotdelay, afs.fs_old_rps, afs.fs_old_trackskew,
182		    afs.fs_old_interleave);
183		printf("nsect\t%d\tnpsect\t%d\tspc\t%d\n",
184		    afs.fs_old_nsect, afs.fs_old_npsect, afs.fs_old_spc);
185		break;
186	default:
187		break;
188	}
189	printf("sblkno\t%d\tcblkno\t%d\tiblkno\t%d\tdblkno\t%d\n",
190	    afs.fs_sblkno, afs.fs_cblkno, afs.fs_iblkno, afs.fs_dblkno);
191	printf("cgrotor\t%d\tfmod\t%d\tronly\t%d\tclean\t%d\n",
192	    afs.fs_cgrotor, afs.fs_fmod, afs.fs_ronly, afs.fs_clean);
193	printf("flags\t");
194	if (afs.fs_flags == 0)
195		printf("none");
196	if (afs.fs_flags & FS_UNCLEAN)
197			printf("unclean ");
198	if (afs.fs_flags & FS_DOSOFTDEP)
199			printf("soft-updates ");
200	if (afs.fs_flags & FS_NEEDSFSCK)
201			printf("needs fsck run ");
202	if (afs.fs_flags & FS_INDEXDIRS)
203			printf("indexed directories ");
204	if ((afs.fs_flags &
205	    ~(FS_UNCLEAN | FS_DOSOFTDEP | FS_NEEDSFSCK | FS_INDEXDIRS)) != 0)
206			printf("unknown flags (%#x)", afs.fs_flags &
207			    ~(FS_UNCLEAN | FS_DOSOFTDEP |
208			      FS_NEEDSFSCK | FS_INDEXDIRS));
209	putchar('\n');
210	printf("\ncs[].cs_(nbfree,ndir,nifree,nffree):\n\t");
211	afs.fs_csp = calloc(1, afs.fs_cssize);
212	if (bread(&disk, fsbtodb(&afs, afs.fs_csaddr), afs.fs_csp, afs.fs_cssize) == -1)
213		goto err;
214	for (i = 0; i < afs.fs_ncg; i++) {
215		struct csum *cs = &afs.fs_cs(&afs, i);
216		if (i && i % 4 == 0)
217			printf("\n\t");
218		printf("(%d,%d,%d,%d) ",
219		    cs->cs_nbfree, cs->cs_ndir, cs->cs_nifree, cs->cs_nffree);
220	}
221	printf("\n");
222	if (fssize % afs.fs_fpg) {
223		if (disk.d_ufs == 1)
224			printf("cylinders in last group %d\n",
225			    howmany(afs.fs_old_size % afs.fs_fpg,
226			    afs.fs_old_spc / afs.fs_old_nspf));
227		printf("blocks in last group %d\n\n",
228		    (fssize % afs.fs_fpg) / afs.fs_frag);
229	}
230	while ((i = cgread(&disk)) != 0) {
231		if (i == -1 || dumpcg())
232			goto err;
233	}
234	ufs_disk_close(&disk);
235	return (0);
236
237err:	if (disk.d_error != NULL)
238		warnx("%s: %s", name, disk.d_error);
239	else if (errno)
240		warn("%s", name);
241	ufs_disk_close(&disk);
242	return (1);
243}
244
245int
246dumpcg(void)
247{
248	time_t time;
249	off_t cur;
250	int i, j;
251
252	printf("\ncg %d:\n", disk.d_lcg);
253	cur = fsbtodb(&afs, cgtod(&afs, disk.d_lcg)) * disk.d_bsize;
254	switch (disk.d_ufs) {
255	case 2:
256		time = acg.cg_time;
257		printf("magic\t%x\ttell\t%qx\ttime\t%s",
258		    acg.cg_magic, cur, ctime(&time));
259		printf("cgx\t%d\tndblk\t%d\tniblk\t%d\tinitiblk %d\n",
260		    acg.cg_cgx, acg.cg_ndblk, acg.cg_niblk, acg.cg_initediblk);
261		break;
262	case 1:
263		time = acg.cg_old_time;
264		printf("magic\t%x\ttell\t%qx\ttime\t%s",
265		    acg.cg_magic, cur, ctime(&time));
266		printf("cgx\t%d\tncyl\t%d\tniblk\t%d\tndblk\t%d\n",
267		    acg.cg_cgx, acg.cg_old_ncyl, acg.cg_old_niblk,
268		    acg.cg_ndblk);
269		break;
270	default:
271		break;
272	}
273	printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
274	    acg.cg_cs.cs_nbfree, acg.cg_cs.cs_ndir,
275	    acg.cg_cs.cs_nifree, acg.cg_cs.cs_nffree);
276	printf("rotor\t%d\tirotor\t%d\tfrotor\t%d\nfrsum",
277	    acg.cg_rotor, acg.cg_irotor, acg.cg_frotor);
278	for (i = 1, j = 0; i < afs.fs_frag; i++) {
279		printf("\t%d", acg.cg_frsum[i]);
280		j += i * acg.cg_frsum[i];
281	}
282	printf("\nsum of frsum: %d", j);
283	if (afs.fs_contigsumsize > 0) {
284		for (i = 1; i < afs.fs_contigsumsize; i++) {
285			if ((i - 1) % 8 == 0)
286				printf("\nclusters %d-%d:", i,
287				    afs.fs_contigsumsize - 1 < i + 7 ?
288				    afs.fs_contigsumsize - 1 : i + 7);
289			printf("\t%d", cg_clustersum(&acg)[i]);
290		}
291		printf("\nclusters size %d and over: %d\n",
292		    afs.fs_contigsumsize,
293		    cg_clustersum(&acg)[afs.fs_contigsumsize]);
294		printf("clusters free:\t");
295		pbits(cg_clustersfree(&acg), acg.cg_nclusterblks);
296	} else
297		printf("\n");
298	printf("inodes used:\t");
299	pbits(cg_inosused(&acg), afs.fs_ipg);
300	printf("blks free:\t");
301	pbits(cg_blksfree(&acg), afs.fs_fpg);
302	return (0);
303}
304
305void
306pbits(void *vp, int max)
307{
308	int i;
309	char *p;
310	int count, j;
311
312	for (count = i = 0, p = vp; i < max; i++)
313		if (isset(p, i)) {
314			if (count)
315				printf(",%s", count % 6 ? " " : "\n\t");
316			count++;
317			printf("%d", i);
318			j = i;
319			while ((i+1)<max && isset(p, i+1))
320				i++;
321			if (i != j)
322				printf("-%d", i);
323		}
324	printf("\n");
325}
326
327void
328usage(void)
329{
330	(void)fprintf(stderr, "usage: dumpfs filesys | device\n");
331	exit(1);
332}
333