xref: /freebsd/sbin/dumpfs/dumpfs.c (revision df7f5d4de4592a8948a25ce01e5bddfbb7ce39dc)
1 /*
2  * Copyright (c) 1983, 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static char copyright[] =
36 "@(#) Copyright (c) 1983, 1992, 1993\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 static char sccsid[] = "@(#)dumpfs.c	8.5 (Berkeley) 4/29/95";
42 #endif /* not lint */
43 
44 #include <sys/param.h>
45 #include <sys/time.h>
46 
47 #include <ufs/ufs/dinode.h>
48 #include <ufs/ffs/fs.h>
49 
50 #include <err.h>
51 #include <errno.h>
52 #include <fcntl.h>
53 #include <fstab.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 
59 union {
60 	struct fs fs;
61 	char pad[MAXBSIZE];
62 } fsun;
63 #define	afs	fsun.fs
64 
65 union {
66 	struct cg cg;
67 	char pad[MAXBSIZE];
68 } cgun;
69 #define	acg	cgun.cg
70 
71 long	dev_bsize = 1;
72 
73 int	dumpfs __P((char *));
74 int	dumpcg __P((char *, int, int));
75 void	pbits __P((void *, int));
76 void	usage __P((void));
77 
78 int
79 main(argc, argv)
80 	int argc;
81 	char *argv[];
82 {
83 	register struct fstab *fs;
84 	int ch, eval;
85 
86 	while ((ch = getopt(argc, argv, "")) != -1)
87 		switch(ch) {
88 		case '?':
89 		default:
90 			usage();
91 		}
92 	argc -= optind;
93 	argv += optind;
94 
95 	if (argc < 1)
96 		usage();
97 
98 	for (eval = 0; *argv; ++argv)
99 		if ((fs = getfsfile(*argv)) == NULL)
100 			eval |= dumpfs(*argv);
101 		else
102 			eval |= dumpfs(fs->fs_spec);
103 	exit(eval);
104 }
105 
106 int
107 dumpfs(name)
108 	char *name;
109 {
110 	int fd, c, i, j, k, size;
111 
112 	if ((fd = open(name, O_RDONLY, 0)) < 0)
113 		goto err;
114 	if (lseek(fd, (off_t)SBOFF, SEEK_SET) == (off_t)-1)
115 		goto err;
116 	if (read(fd, &afs, SBSIZE) != SBSIZE)
117 		goto err;
118 
119  	if (afs.fs_magic != FS_MAGIC) {
120 		warnx("%s: superblock has bad magic number, skipped", name);
121 		(void)close(fd);
122  		return (1);
123  	}
124 
125 	if (afs.fs_postblformat == FS_42POSTBLFMT)
126 		afs.fs_nrpos = 8;
127 	dev_bsize = afs.fs_fsize / fsbtodb(&afs, 1);
128 	printf("magic\t%x\ttime\t%s", afs.fs_magic,
129 	    ctime(&afs.fs_time));
130 	printf("cylgrp\t%s\tinodes\t%s\n",
131 	    afs.fs_postblformat == FS_42POSTBLFMT ? "static" : "dynamic",
132 	    afs.fs_inodefmt < FS_44INODEFMT ? "4.2/4.3BSD" : "4.4BSD");
133 	printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
134 	    afs.fs_cstotal.cs_nbfree, afs.fs_cstotal.cs_ndir,
135 	    afs.fs_cstotal.cs_nifree, afs.fs_cstotal.cs_nffree);
136 	printf("ncg\t%d\tncyl\t%d\tsize\t%d\tblocks\t%d\n",
137 	    afs.fs_ncg, afs.fs_ncyl, afs.fs_size, afs.fs_dsize);
138 	printf("bsize\t%d\tshift\t%d\tmask\t0x%08x\n",
139 	    afs.fs_bsize, afs.fs_bshift, afs.fs_bmask);
140 	printf("fsize\t%d\tshift\t%d\tmask\t0x%08x\n",
141 	    afs.fs_fsize, afs.fs_fshift, afs.fs_fmask);
142 	printf("frag\t%d\tshift\t%d\tfsbtodb\t%d\n",
143 	    afs.fs_frag, afs.fs_fragshift, afs.fs_fsbtodb);
144 	printf("cpg\t%d\tbpg\t%d\tfpg\t%d\tipg\t%d\n",
145 	    afs.fs_cpg, afs.fs_fpg / afs.fs_frag, afs.fs_fpg, afs.fs_ipg);
146 	printf("minfree\t%d%%\toptim\t%s\tmaxcontig %d\tmaxbpg\t%d\n",
147 	    afs.fs_minfree, afs.fs_optim == FS_OPTSPACE ? "space" : "time",
148 	    afs.fs_maxcontig, afs.fs_maxbpg);
149 	printf("rotdelay %dms\theadswitch %dus\ttrackseek %dus\trps\t%d\n",
150 	    afs.fs_rotdelay, afs.fs_headswitch, afs.fs_trkseek, afs.fs_rps);
151 	printf("ntrak\t%d\tnsect\t%d\tnpsect\t%d\tspc\t%d\n",
152 	    afs.fs_ntrak, afs.fs_nsect, afs.fs_npsect, afs.fs_spc);
153 	printf("symlinklen %d\ttrackskew %d\tinterleave %d\tcontigsumsize %d\n",
154 	    afs.fs_maxsymlinklen, afs.fs_trackskew, afs.fs_interleave,
155 	    afs.fs_contigsumsize);
156 	printf("nindir\t%d\tinopb\t%d\tnspf\t%d\n",
157 	    afs.fs_nindir, afs.fs_inopb, afs.fs_nspf);
158 	printf("sblkno\t%d\tcblkno\t%d\tiblkno\t%d\tdblkno\t%d\n",
159 	    afs.fs_sblkno, afs.fs_cblkno, afs.fs_iblkno, afs.fs_dblkno);
160 	printf("sbsize\t%d\tcgsize\t%d\tcgoffset %d\tcgmask\t0x%08x\n",
161 	    afs.fs_sbsize, afs.fs_cgsize, afs.fs_cgoffset, afs.fs_cgmask);
162 	printf("csaddr\t%d\tcssize\t%d\tshift\t%d\tmask\t0x%08x\n",
163 	    afs.fs_csaddr, afs.fs_cssize, afs.fs_csshift, afs.fs_csmask);
164 	printf("cgrotor\t%d\tfmod\t%d\tronly\t%d\tclean\t%d\n",
165 	    afs.fs_cgrotor, afs.fs_fmod, afs.fs_ronly, afs.fs_clean);
166 	if (afs.fs_cpc != 0)
167 		printf("blocks available in each of %d rotational positions",
168 		     afs.fs_nrpos);
169 	else
170 		printf("(no rotational position table)\n");
171 	for (c = 0; c < afs.fs_cpc; c++) {
172 		printf("\ncylinder number %d:", c);
173 		for (i = 0; i < afs.fs_nrpos; i++) {
174 			if (fs_postbl(&afs, c)[i] == -1)
175 				continue;
176 			printf("\n   position %d:\t", i);
177 			for (j = fs_postbl(&afs, c)[i], k = 1; ;
178 			     j += fs_rotbl(&afs)[j], k++) {
179 				printf("%5d", j);
180 				if (k % 12 == 0)
181 					printf("\n\t\t");
182 				if (fs_rotbl(&afs)[j] == 0)
183 					break;
184 			}
185 		}
186 	}
187 	printf("\ncs[].cs_(nbfree,ndir,nifree,nffree):\n\t");
188 	for (i = 0, j = 0; i < afs.fs_cssize; i += afs.fs_bsize, j++) {
189 		size = afs.fs_cssize - i < afs.fs_bsize ?
190 		    afs.fs_cssize - i : afs.fs_bsize;
191 		afs.fs_csp[j] = calloc(1, size);
192 		if (lseek(fd,
193 		    (off_t)(fsbtodb(&afs, (afs.fs_csaddr + j * afs.fs_frag))) *
194 		    (off_t)dev_bsize, SEEK_SET) == (off_t)-1)
195 			goto err;
196 		if (read(fd, afs.fs_csp[j], size) != size)
197 			goto err;
198 	}
199 	for (i = 0; i < afs.fs_ncg; i++) {
200 		struct csum *cs = &afs.fs_cs(&afs, i);
201 		if (i && i % 4 == 0)
202 			printf("\n\t");
203 		printf("(%d,%d,%d,%d) ",
204 		    cs->cs_nbfree, cs->cs_ndir, cs->cs_nifree, cs->cs_nffree);
205 	}
206 	printf("\n");
207 	if (afs.fs_ncyl % afs.fs_cpg) {
208 		printf("cylinders in last group %d\n",
209 		    i = afs.fs_ncyl % afs.fs_cpg);
210 		printf("blocks in last group %d\n",
211 		    i * afs.fs_spc / NSPB(&afs));
212 	}
213 	printf("\n");
214 	for (i = 0; i < afs.fs_ncg; i++)
215 		if (dumpcg(name, fd, i))
216 			goto err;
217 	(void)close(fd);
218 	return (0);
219 
220 err:	if (fd != -1)
221 		(void)close(fd);
222 	warn("%s", name);
223 	return (1);
224 };
225 
226 int
227 dumpcg(name, fd, c)
228 	char *name;
229 	int fd, c;
230 {
231 	off_t cur;
232 	int i, j;
233 
234 	printf("\ncg %d:\n", c);
235 	if ((cur = lseek(fd, (off_t)(fsbtodb(&afs, cgtod(&afs, c))) *
236 	    (off_t)dev_bsize, SEEK_SET)) == (off_t)-1)
237 		return (1);
238 	if (read(fd, &acg, afs.fs_bsize) != afs.fs_bsize) {
239 		warnx("%s: error reading cg", name);
240 		return (1);
241 	}
242 	printf("magic\t%x\ttell\t%qx\ttime\t%s",
243 	    afs.fs_postblformat == FS_42POSTBLFMT ?
244 	    ((struct ocg *)&acg)->cg_magic : acg.cg_magic,
245 	    cur, ctime(&acg.cg_time));
246 	printf("cgx\t%d\tncyl\t%d\tniblk\t%d\tndblk\t%d\n",
247 	    acg.cg_cgx, acg.cg_ncyl, acg.cg_niblk, acg.cg_ndblk);
248 	printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
249 	    acg.cg_cs.cs_nbfree, acg.cg_cs.cs_ndir,
250 	    acg.cg_cs.cs_nifree, acg.cg_cs.cs_nffree);
251 	printf("rotor\t%d\tirotor\t%d\tfrotor\t%d\nfrsum",
252 	    acg.cg_rotor, acg.cg_irotor, acg.cg_frotor);
253 	for (i = 1, j = 0; i < afs.fs_frag; i++) {
254 		printf("\t%d", acg.cg_frsum[i]);
255 		j += i * acg.cg_frsum[i];
256 	}
257 	printf("\nsum of frsum: %d", j);
258 	if (afs.fs_contigsumsize > 0) {
259 		for (i = 1; i < afs.fs_contigsumsize; i++) {
260 			if ((i - 1) % 8 == 0)
261 				printf("\nclusters %d-%d:", i,
262 				    afs.fs_contigsumsize - 1 < i + 7 ?
263 				    afs.fs_contigsumsize - 1 : i + 7);
264 			printf("\t%d", cg_clustersum(&acg)[i]);
265 		}
266 		printf("\nclusters size %d and over: %d\n",
267 		    afs.fs_contigsumsize,
268 		    cg_clustersum(&acg)[afs.fs_contigsumsize]);
269 		printf("clusters free:\t");
270 		pbits(cg_clustersfree(&acg), acg.cg_nclusterblks);
271 	} else
272 		printf("\n");
273 	printf("iused:\t");
274 	pbits(cg_inosused(&acg), afs.fs_ipg);
275 	printf("free:\t");
276 	pbits(cg_blksfree(&acg), afs.fs_fpg);
277 	printf("b:\n");
278 	for (i = 0; i < afs.fs_cpg; i++) {
279 		if (cg_blktot(&acg)[i] == 0)
280 			continue;
281 		printf("   c%d:\t(%d)\t", i, cg_blktot(&acg)[i]);
282 		for (j = 0; j < afs.fs_nrpos; j++) {
283 			if (afs.fs_cpc > 0 &&
284 			    fs_postbl(&afs, i % afs.fs_cpc)[j] == -1)
285 				continue;
286 			printf(" %d", cg_blks(&afs, &acg, i)[j]);
287 		}
288 		printf("\n");
289 	}
290 	return (0);
291 };
292 
293 void
294 pbits(vp, max)
295 	register void *vp;
296 	int max;
297 {
298 	register int i;
299 	register char *p;
300 	int count, j;
301 
302 	for (count = i = 0, p = vp; i < max; i++)
303 		if (isset(p, i)) {
304 			if (count)
305 				printf(",%s", count % 6 ? " " : "\n\t");
306 			count++;
307 			printf("%d", i);
308 			j = i;
309 			while ((i+1)<max && isset(p, i+1))
310 				i++;
311 			if (i != j)
312 				printf("-%d", i);
313 		}
314 	printf("\n");
315 }
316 
317 void
318 usage()
319 {
320 	(void)fprintf(stderr, "usage: dumpfs filesys | device\n");
321 	exit(1);
322 }
323