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