xref: /freebsd/sbin/dumpfs/dumpfs.c (revision e627b39baccd1ec9129690167cf5e6d860509655)
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.2 (Berkeley) 2/2/94";
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 <unistd.h>
51 #include <fcntl.h>
52 #include <errno.h>
53 #include <fstab.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 
58 union {
59 	struct fs fs;
60 	char pad[MAXBSIZE];
61 } fsun;
62 #define	afs	fsun.fs
63 
64 union {
65 	struct cg cg;
66 	char pad[MAXBSIZE];
67 } cgun;
68 #define	acg	cgun.cg
69 
70 long	dev_bsize = 1;
71 
72 int	dumpfs __P((char *));
73 int	dumpcg __P((char *, int, int));
74 void	pbits __P((void *, int));
75 void	usage __P((void));
76 
77 int
78 main(argc, argv)
79 	int argc;
80 	char *argv[];
81 {
82 	register struct fstab *fs;
83 	int ch, eval;
84 
85 	while ((ch = getopt(argc, argv, "")) != EOF)
86 		switch(ch) {
87 		case '?':
88 		default:
89 			usage();
90 		}
91 	argc -= optind;
92 	argv += optind;
93 
94 	if (argc < 1)
95 		usage();
96 
97 	for (eval = 0; *argv; ++argv)
98 		if ((fs = getfsfile(*argv)) == NULL)
99 			eval |= dumpfs(*argv);
100 		else
101 			eval |= dumpfs(fs->fs_spec);
102 	exit(eval);
103 }
104 
105 int
106 dumpfs(name)
107 	char *name;
108 {
109 	int fd, c, i, j, k, size;
110 
111 	if ((fd = open(name, O_RDONLY, 0)) < 0)
112 		goto err;
113 	if (lseek(fd, (off_t)SBOFF, SEEK_SET) == (off_t)-1)
114 		goto err;
115 	if (read(fd, &afs, SBSIZE) != SBSIZE)
116 		goto err;
117 
118 	if (afs.fs_postblformat == FS_42POSTBLFMT)
119 		afs.fs_nrpos = 8;
120 	dev_bsize = afs.fs_fsize / fsbtodb(&afs, 1);
121 	printf("magic\t%x\ttime\t%s", afs.fs_magic,
122 	    ctime(&afs.fs_time));
123 	printf("cylgrp\t%s\tinodes\t%s\n",
124 	    afs.fs_postblformat == FS_42POSTBLFMT ? "static" : "dynamic",
125 	    afs.fs_inodefmt < FS_44INODEFMT ? "4.2/4.3BSD" : "4.4BSD");
126 	printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
127 	    afs.fs_cstotal.cs_nbfree, afs.fs_cstotal.cs_ndir,
128 	    afs.fs_cstotal.cs_nifree, afs.fs_cstotal.cs_nffree);
129 	printf("ncg\t%d\tncyl\t%d\tsize\t%d\tblocks\t%d\n",
130 	    afs.fs_ncg, afs.fs_ncyl, afs.fs_size, afs.fs_dsize);
131 	printf("bsize\t%d\tshift\t%d\tmask\t0x%08x\n",
132 	    afs.fs_bsize, afs.fs_bshift, afs.fs_bmask);
133 	printf("fsize\t%d\tshift\t%d\tmask\t0x%08x\n",
134 	    afs.fs_fsize, afs.fs_fshift, afs.fs_fmask);
135 	printf("frag\t%d\tshift\t%d\tfsbtodb\t%d\n",
136 	    afs.fs_frag, afs.fs_fragshift, afs.fs_fsbtodb);
137 	printf("cpg\t%d\tbpg\t%d\tfpg\t%d\tipg\t%d\n",
138 	    afs.fs_cpg, afs.fs_fpg / afs.fs_frag, afs.fs_fpg, afs.fs_ipg);
139 	printf("minfree\t%d%%\toptim\t%s\tmaxcontig %d\tmaxbpg\t%d\n",
140 	    afs.fs_minfree, afs.fs_optim == FS_OPTSPACE ? "space" : "time",
141 	    afs.fs_maxcontig, afs.fs_maxbpg);
142 	printf("rotdelay %dms\theadswitch %dus\ttrackseek %dus\trps\t%d\n",
143 	    afs.fs_rotdelay, afs.fs_headswitch, afs.fs_trkseek, afs.fs_rps);
144 	printf("ntrak\t%d\tnsect\t%d\tnpsect\t%d\tspc\t%d\n",
145 	    afs.fs_ntrak, afs.fs_nsect, afs.fs_npsect, afs.fs_spc);
146 	printf("symlinklen %d\ttrackskew %d\tinterleave %d\tcontigsumsize %d\n",
147 	    afs.fs_maxsymlinklen, afs.fs_trackskew, afs.fs_interleave,
148 	    afs.fs_contigsumsize);
149 	printf("nindir\t%d\tinopb\t%d\tnspf\t%d\n",
150 	    afs.fs_nindir, afs.fs_inopb, afs.fs_nspf);
151 	printf("sblkno\t%d\tcblkno\t%d\tiblkno\t%d\tdblkno\t%d\n",
152 	    afs.fs_sblkno, afs.fs_cblkno, afs.fs_iblkno, afs.fs_dblkno);
153 	printf("sbsize\t%d\tcgsize\t%d\tcgoffset %d\tcgmask\t0x%08x\n",
154 	    afs.fs_sbsize, afs.fs_cgsize, afs.fs_cgoffset, afs.fs_cgmask);
155 	printf("csaddr\t%d\tcssize\t%d\tshift\t%d\tmask\t0x%08x\n",
156 	    afs.fs_csaddr, afs.fs_cssize, afs.fs_csshift, afs.fs_csmask);
157 	printf("cgrotor\t%d\tfmod\t%d\tronly\t%d\tclean\t%d\n",
158 	    afs.fs_cgrotor, afs.fs_fmod, afs.fs_ronly, afs.fs_clean);
159 	if (afs.fs_cpc != 0)
160 		printf("blocks available in each of %d rotational positions",
161 		     afs.fs_nrpos);
162 	else
163 		printf("(no rotational position table)\n");
164 	for (c = 0; c < afs.fs_cpc; c++) {
165 		printf("\ncylinder number %d:", c);
166 		for (i = 0; i < afs.fs_nrpos; i++) {
167 			if (fs_postbl(&afs, c)[i] == -1)
168 				continue;
169 			printf("\n   position %d:\t", i);
170 			for (j = fs_postbl(&afs, c)[i], k = 1; ;
171 			     j += fs_rotbl(&afs)[j], k++) {
172 				printf("%5d", j);
173 				if (k % 12 == 0)
174 					printf("\n\t\t");
175 				if (fs_rotbl(&afs)[j] == 0)
176 					break;
177 			}
178 		}
179 	}
180 	printf("\ncs[].cs_(nbfree,ndir,nifree,nffree):\n\t");
181 	for (i = 0, j = 0; i < afs.fs_cssize; i += afs.fs_bsize, j++) {
182 		size = afs.fs_cssize - i < afs.fs_bsize ?
183 		    afs.fs_cssize - i : afs.fs_bsize;
184 		afs.fs_csp[j] = calloc(1, size);
185 		if (lseek(fd,
186 		    (off_t)(fsbtodb(&afs, (afs.fs_csaddr + j * afs.fs_frag))) *
187 		    (off_t)dev_bsize, SEEK_SET) == (off_t)-1)
188 			goto err;
189 		if (read(fd, afs.fs_csp[j], size) != size)
190 			goto err;
191 	}
192 	for (i = 0; i < afs.fs_ncg; i++) {
193 		struct csum *cs = &afs.fs_cs(&afs, i);
194 		if (i && i % 4 == 0)
195 			printf("\n\t");
196 		printf("(%d,%d,%d,%d) ",
197 		    cs->cs_nbfree, cs->cs_ndir, cs->cs_nifree, cs->cs_nffree);
198 	}
199 	printf("\n");
200 	if (afs.fs_ncyl % afs.fs_cpg) {
201 		printf("cylinders in last group %d\n",
202 		    i = afs.fs_ncyl % afs.fs_cpg);
203 		printf("blocks in last group %d\n",
204 		    i * afs.fs_spc / NSPB(&afs));
205 	}
206 	printf("\n");
207 	for (i = 0; i < afs.fs_ncg; i++)
208 		if (dumpcg(name, fd, i))
209 			goto err;
210 	(void)close(fd);
211 	return (0);
212 
213 err:	if (fd != -1)
214 		(void)close(fd);
215 	(void)fprintf(stderr, "dumpfs: %s: %s\n", name, strerror(errno));
216 	return (1);
217 };
218 
219 int
220 dumpcg(name, fd, c)
221 	char *name;
222 	int fd, c;
223 {
224 	off_t cur;
225 	int i, j;
226 
227 	printf("\ncg %d:\n", c);
228 	if ((cur = lseek(fd, (off_t)(fsbtodb(&afs, cgtod(&afs, c))) *
229 	    (off_t)dev_bsize, SEEK_SET)) == (off_t)-1)
230 		return (1);
231 	if (read(fd, &acg, afs.fs_bsize) != afs.fs_bsize) {
232 		(void)fprintf(stderr, "dumpfs: %s: error reading cg\n", name);
233 		return (1);
234 	}
235 	printf("magic\t%x\ttell\t%qx\ttime\t%s",
236 	    afs.fs_postblformat == FS_42POSTBLFMT ?
237 	    ((struct ocg *)&acg)->cg_magic : acg.cg_magic,
238 	    cur, ctime(&acg.cg_time));
239 	printf("cgx\t%d\tncyl\t%d\tniblk\t%d\tndblk\t%d\n",
240 	    acg.cg_cgx, acg.cg_ncyl, acg.cg_niblk, acg.cg_ndblk);
241 	printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
242 	    acg.cg_cs.cs_nbfree, acg.cg_cs.cs_ndir,
243 	    acg.cg_cs.cs_nifree, acg.cg_cs.cs_nffree);
244 	printf("rotor\t%d\tirotor\t%d\tfrotor\t%d\nfrsum",
245 	    acg.cg_rotor, acg.cg_irotor, acg.cg_frotor);
246 	for (i = 1, j = 0; i < afs.fs_frag; i++) {
247 		printf("\t%d", acg.cg_frsum[i]);
248 		j += i * acg.cg_frsum[i];
249 	}
250 	printf("\nsum of frsum: %d", j);
251 	if (afs.fs_contigsumsize > 0) {
252 		for (i = 1; i < afs.fs_contigsumsize; i++) {
253 			if ((i - 1) % 8 == 0)
254 				printf("\nclusters %d-%d:", i,
255 				    afs.fs_contigsumsize - 1 < i + 7 ?
256 				    afs.fs_contigsumsize - 1 : i + 7);
257 			printf("\t%d", cg_clustersum(&acg)[i]);
258 		}
259 		printf("\nclusters size %d and over: %d\n",
260 		    afs.fs_contigsumsize,
261 		    cg_clustersum(&acg)[afs.fs_contigsumsize]);
262 		printf("clusters free:\t");
263 		pbits(cg_clustersfree(&acg), acg.cg_nclusterblks);
264 	} else
265 		printf("\n");
266 	printf("iused:\t");
267 	pbits(cg_inosused(&acg), afs.fs_ipg);
268 	printf("free:\t");
269 	pbits(cg_blksfree(&acg), afs.fs_fpg);
270 	printf("b:\n");
271 	for (i = 0; i < afs.fs_cpg; i++) {
272 		if (cg_blktot(&acg)[i] == 0)
273 			continue;
274 		printf("   c%d:\t(%d)\t", i, cg_blktot(&acg)[i]);
275 		for (j = 0; j < afs.fs_nrpos; j++) {
276 			if (afs.fs_cpc > 0 &&
277 			    fs_postbl(&afs, i % afs.fs_cpc)[j] == -1)
278 				continue;
279 			printf(" %d", cg_blks(&afs, &acg, i)[j]);
280 		}
281 		printf("\n");
282 	}
283 	return (0);
284 };
285 
286 void
287 pbits(vp, max)
288 	register void *vp;
289 	int max;
290 {
291 	register int i;
292 	register char *p;
293 	int count, j;
294 
295 	for (count = i = 0, p = vp; i < max; i++)
296 		if (isset(p, i)) {
297 			if (count)
298 				printf(",%s", count % 6 ? " " : "\n\t");
299 			count++;
300 			printf("%d", i);
301 			j = i;
302 			while ((i+1)<max && isset(p, i+1))
303 				i++;
304 			if (i != j)
305 				printf("-%d", i);
306 		}
307 	printf("\n");
308 }
309 
310 void
311 usage()
312 {
313 	(void)fprintf(stderr, "usage: dumpfs filesys | device\n");
314 	exit(1);
315 }
316