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