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