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