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