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