xref: /freebsd/sbin/dumpfs/dumpfs.c (revision 5773cccf19ef7b97e56c1101aa481c43149224da)
1 /*
2  * Copyright (c) 2002 Networks Associates Technology, Inc.
3  * All rights reserved.
4  *
5  * This software was developed for the FreeBSD Project by Marshall
6  * Kirk McKusick and Network Associates Laboratories, the Security
7  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
8  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
9  * research program
10  *
11  * Copyright (c) 1982, 1989, 1993
12  *	The Regents of the University of California.  All rights reserved.
13  * (c) UNIX System Laboratories, Inc.
14  * Copyright (c) 1983, 1992, 1993
15  *	The Regents of the University of California.  All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  * 3. All advertising materials mentioning features or use of this software
26  *    must display the following acknowledgement:
27  *	This product includes software developed by the University of
28  *	California, Berkeley and its contributors.
29  * 4. Neither the name of the University nor the names of its contributors
30  *    may be used to endorse or promote products derived from this software
31  *    without specific prior written permission.
32  *
33  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43  * SUCH DAMAGE.
44  */
45 
46 #ifndef lint
47 static const char copyright[] =
48 "@(#) Copyright (c) 1983, 1992, 1993\n\
49 	The Regents of the University of California.  All rights reserved.\n";
50 #endif /* not lint */
51 
52 #ifndef lint
53 #if 0
54 static char sccsid[] = "@(#)dumpfs.c	8.5 (Berkeley) 4/29/95";
55 #endif
56 static const char rcsid[] =
57   "$FreeBSD$";
58 #endif /* not lint */
59 
60 #include <sys/param.h>
61 #include <sys/time.h>
62 #include <sys/disklabel.h>
63 
64 #include <ufs/ufs/dinode.h>
65 #include <ufs/ffs/fs.h>
66 
67 #include <err.h>
68 #include <errno.h>
69 #include <fcntl.h>
70 #include <fstab.h>
71 #include <libufs.h>
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <unistd.h>
75 
76 #define	afs	disk.d_fs
77 
78 union {
79 	struct cg cg;
80 	char pad[MAXBSIZE];
81 } cgun;
82 #define	acg	cgun.cg
83 
84 struct uufsd disk;
85 
86 int	dumpfs(const char *);
87 int	dumpcg(int);
88 void	pbits(void *, int);
89 void	usage(void) __dead2;
90 
91 int
92 main(int argc, char *argv[])
93 {
94 	struct fstab *fs;
95 	int ch, eval;
96 
97 	while ((ch = getopt(argc, argv, "")) != -1)
98 		switch(ch) {
99 		case '?':
100 		default:
101 			usage();
102 		}
103 	argc -= optind;
104 	argv += optind;
105 
106 	if (argc < 1)
107 		usage();
108 
109 	for (eval = 0; *argv; ++argv)
110 		if ((fs = getfsfile(*argv)) == NULL)
111 			eval |= dumpfs(*argv);
112 		else
113 			eval |= dumpfs(fs->fs_spec);
114 	exit(eval);
115 }
116 
117 int
118 dumpfs(const char *name)
119 {
120 	time_t time;
121 	int64_t fssize;
122 	int i;
123 
124 	if (ufs_disk_fillout(&disk, name) == -1)
125 			goto err;
126 
127 	switch (disk.d_ufs) {
128 	case 2:
129 		fssize = afs.fs_size;
130 		time = afs.fs_time;
131 		printf("magic\t%x (UFS2)\ttime\t%s",
132 		    afs.fs_magic, ctime(&time));
133 		printf("offset\t%qd\tid\t[ %x %x ]\n",
134 		    afs.fs_sblockloc, afs.fs_id[0], afs.fs_id[1]);
135 		printf("ncg\t%d\tsize\t%qd\tblocks\t%d\n",
136 		    afs.fs_ncg, fssize, afs.fs_dsize);
137 		break;
138 	case 1:
139 		fssize = afs.fs_old_size;
140 		time = afs.fs_old_time;
141 		printf("magic\t%x (UFS1)\ttime\t%s",
142 		    afs.fs_magic, ctime(&time));
143 		printf("id\t[ %x %x ]\n", afs.fs_id[0], afs.fs_id[1]);
144 		printf("ncg\t%d\tsize\t%qd\tblocks\t%d\n",
145 		    afs.fs_ncg, fssize, afs.fs_dsize);
146 		break;
147 	default:
148 		break;
149 	}
150 	printf("bsize\t%d\tshift\t%d\tmask\t0x%08x\n",
151 	    afs.fs_bsize, afs.fs_bshift, afs.fs_bmask);
152 	printf("fsize\t%d\tshift\t%d\tmask\t0x%08x\n",
153 	    afs.fs_fsize, afs.fs_fshift, afs.fs_fmask);
154 	printf("frag\t%d\tshift\t%d\tfsbtodb\t%d\n",
155 	    afs.fs_frag, afs.fs_fragshift, afs.fs_fsbtodb);
156 	printf("minfree\t%d%%\toptim\t%s\tsymlinklen %d\n",
157 	    afs.fs_minfree, afs.fs_optim == FS_OPTSPACE ? "space" : "time",
158 	    afs.fs_maxsymlinklen);
159 	switch (disk.d_ufs) {
160 	case 2:
161 		printf("%s %d\tmaxbpg\t%d\tmaxcontig %d\tcontigsumsize %d\n",
162 		    "maxbsize", afs.fs_maxbsize, afs.fs_maxbpg,
163 		    afs.fs_maxcontig, afs.fs_contigsumsize);
164 		printf("nbfree\t%qd\tndir\t%qd\tnifree\t%qd\tnffree\t%qd\n",
165 		    afs.fs_cstotal.cs_nbfree, afs.fs_cstotal.cs_ndir,
166 		    afs.fs_cstotal.cs_nifree, afs.fs_cstotal.cs_nffree);
167 		printf("bpg\t%d\tfpg\t%d\tipg\t%d\n",
168 		    afs.fs_fpg / afs.fs_frag, afs.fs_fpg, afs.fs_ipg);
169 		printf("nindir\t%d\tinopb\t%d\tmaxfilesize\t%qu\n",
170 		    afs.fs_nindir, afs.fs_inopb, afs.fs_maxfilesize);
171 		printf("sbsize\t%d\tcgsize\t%d\tcsaddr\t%d\tcssize\t%d\n",
172 		    afs.fs_sbsize, afs.fs_cgsize, afs.fs_csaddr, afs.fs_cssize);
173 		break;
174 	case 1:
175 		printf("maxbpg\t%d\tmaxcontig %d\tcontigsumsize %d\n",
176 		    afs.fs_maxbpg, afs.fs_maxcontig, afs.fs_contigsumsize);
177 		printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
178 		    afs.fs_old_cstotal.cs_nbfree, afs.fs_old_cstotal.cs_ndir,
179 		    afs.fs_old_cstotal.cs_nifree, afs.fs_old_cstotal.cs_nffree);
180 		printf("cpg\t%d\tbpg\t%d\tfpg\t%d\tipg\t%d\n",
181 		    afs.fs_old_cpg, afs.fs_fpg / afs.fs_frag, afs.fs_fpg,
182 		    afs.fs_ipg);
183 		printf("nindir\t%d\tinopb\t%d\tnspf\t%d\tmaxfilesize\t%qu\n",
184 		    afs.fs_nindir, afs.fs_inopb, afs.fs_old_nspf,
185 		    afs.fs_maxfilesize);
186 		printf("sbsize\t%d\tcgsize\t%d\tcgoffset %d\tcgmask\t0x%08x\n",
187 		    afs.fs_sbsize, afs.fs_cgsize, afs.fs_old_cgoffset,
188 		    afs.fs_old_cgmask);
189 		printf("csaddr\t%d\tcssize\t%d\n",
190 		    afs.fs_old_csaddr, afs.fs_cssize);
191 		printf("rotdelay %dms\trps\t%d\ttrackskew %d\tinterleave %d\n",
192 		    afs.fs_old_rotdelay, afs.fs_old_rps, afs.fs_old_trackskew,
193 		    afs.fs_old_interleave);
194 		printf("nsect\t%d\tnpsect\t%d\tspc\t%d\n",
195 		    afs.fs_old_nsect, afs.fs_old_npsect, afs.fs_old_spc);
196 		break;
197 	default:
198 		break;
199 	}
200 	printf("sblkno\t%d\tcblkno\t%d\tiblkno\t%d\tdblkno\t%d\n",
201 	    afs.fs_sblkno, afs.fs_cblkno, afs.fs_iblkno, afs.fs_dblkno);
202 	printf("cgrotor\t%d\tfmod\t%d\tronly\t%d\tclean\t%d\n",
203 	    afs.fs_cgrotor, afs.fs_fmod, afs.fs_ronly, afs.fs_clean);
204 	printf("flags\t");
205 	if (afs.fs_flags == 0)
206 		printf("none");
207 	if (afs.fs_flags & FS_UNCLEAN)
208 			printf("unclean ");
209 	if (afs.fs_flags & FS_DOSOFTDEP)
210 			printf("soft-updates ");
211 	if (afs.fs_flags & FS_NEEDSFSCK)
212 			printf("needs fsck run ");
213 	if (afs.fs_flags & FS_INDEXDIRS)
214 			printf("indexed directories ");
215 	if ((afs.fs_flags &
216 	    ~(FS_UNCLEAN | FS_DOSOFTDEP | FS_NEEDSFSCK | FS_INDEXDIRS)) != 0)
217 			printf("unknown flags (%#x)", afs.fs_flags &
218 			    ~(FS_UNCLEAN | FS_DOSOFTDEP |
219 			      FS_NEEDSFSCK | FS_INDEXDIRS));
220 	putchar('\n');
221 	printf("\ncs[].cs_(nbfree,ndir,nifree,nffree):\n\t");
222 	afs.fs_csp = calloc(1, afs.fs_cssize);
223 	if (bread(&disk, fsbtodb(&afs, afs.fs_csaddr), afs.fs_csp, afs.fs_cssize) == -1)
224 		goto err;
225 	for (i = 0; i < afs.fs_ncg; i++) {
226 		struct csum *cs = &afs.fs_cs(&afs, i);
227 		if (i && i % 4 == 0)
228 			printf("\n\t");
229 		printf("(%d,%d,%d,%d) ",
230 		    cs->cs_nbfree, cs->cs_ndir, cs->cs_nifree, cs->cs_nffree);
231 	}
232 	printf("\n");
233 	if (fssize % afs.fs_fpg) {
234 		if (disk.d_ufs == 1)
235 			printf("cylinders in last group %d\n",
236 			    howmany(afs.fs_old_size % afs.fs_fpg,
237 			    afs.fs_old_spc / afs.fs_old_nspf));
238 		printf("blocks in last group %d\n\n",
239 		    (fssize % afs.fs_fpg) / afs.fs_frag);
240 	}
241 	for (i = 0; i < afs.fs_ncg; i++)
242 		if (dumpcg(i))
243 			goto err;
244 	ufs_disk_close(&disk);
245 	return (0);
246 
247 err:	if (errno || disk.d_error != NULL) {
248 		if (disk.d_error != NULL)
249 			warnx("%s: %s", name, disk.d_error);
250 		else
251 			warn("%s", name);
252 	}
253 	ufs_disk_close(&disk);
254 	return (1);
255 }
256 
257 int
258 dumpcg(int c)
259 {
260 	time_t time;
261 	off_t cur;
262 	int i, j;
263 
264 	printf("\ncg %d:\n", c);
265 	cur = fsbtodb(&afs, cgtod(&afs, c)) * disk.d_bsize;
266 	if (bread(&disk, fsbtodb(&afs, cgtod(&afs, c)), &acg, afs.fs_bsize) == -1)
267 		return (1);
268 	switch (disk.d_ufs) {
269 	case 2:
270 		time = acg.cg_time;
271 		printf("magic\t%x\ttell\t%qx\ttime\t%s",
272 		    acg.cg_magic, cur, ctime(&time));
273 		printf("cgx\t%d\tndblk\t%d\tniblk\t%d\tinitiblk %d\n",
274 		    acg.cg_cgx, acg.cg_ndblk, acg.cg_niblk, acg.cg_initediblk);
275 		break;
276 	case 1:
277 		time = acg.cg_old_time;
278 		printf("magic\t%x\ttell\t%qx\ttime\t%s",
279 		    acg.cg_magic, cur, ctime(&time));
280 		printf("cgx\t%d\tncyl\t%d\tniblk\t%d\tndblk\t%d\n",
281 		    acg.cg_cgx, acg.cg_old_ncyl, acg.cg_old_niblk,
282 		    acg.cg_ndblk);
283 		break;
284 	default:
285 		break;
286 	}
287 	printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
288 	    acg.cg_cs.cs_nbfree, acg.cg_cs.cs_ndir,
289 	    acg.cg_cs.cs_nifree, acg.cg_cs.cs_nffree);
290 	printf("rotor\t%d\tirotor\t%d\tfrotor\t%d\nfrsum",
291 	    acg.cg_rotor, acg.cg_irotor, acg.cg_frotor);
292 	for (i = 1, j = 0; i < afs.fs_frag; i++) {
293 		printf("\t%d", acg.cg_frsum[i]);
294 		j += i * acg.cg_frsum[i];
295 	}
296 	printf("\nsum of frsum: %d", j);
297 	if (afs.fs_contigsumsize > 0) {
298 		for (i = 1; i < afs.fs_contigsumsize; i++) {
299 			if ((i - 1) % 8 == 0)
300 				printf("\nclusters %d-%d:", i,
301 				    afs.fs_contigsumsize - 1 < i + 7 ?
302 				    afs.fs_contigsumsize - 1 : i + 7);
303 			printf("\t%d", cg_clustersum(&acg)[i]);
304 		}
305 		printf("\nclusters size %d and over: %d\n",
306 		    afs.fs_contigsumsize,
307 		    cg_clustersum(&acg)[afs.fs_contigsumsize]);
308 		printf("clusters free:\t");
309 		pbits(cg_clustersfree(&acg), acg.cg_nclusterblks);
310 	} else
311 		printf("\n");
312 	printf("inodes used:\t");
313 	pbits(cg_inosused(&acg), afs.fs_ipg);
314 	printf("blks free:\t");
315 	pbits(cg_blksfree(&acg), afs.fs_fpg);
316 	return (0);
317 }
318 
319 void
320 pbits(void *vp, int max)
321 {
322 	int i;
323 	char *p;
324 	int count, j;
325 
326 	for (count = i = 0, p = vp; i < max; i++)
327 		if (isset(p, i)) {
328 			if (count)
329 				printf(",%s", count % 6 ? " " : "\n\t");
330 			count++;
331 			printf("%d", i);
332 			j = i;
333 			while ((i+1)<max && isset(p, i+1))
334 				i++;
335 			if (i != j)
336 				printf("-%d", i);
337 		}
338 	printf("\n");
339 }
340 
341 void
342 usage(void)
343 {
344 	(void)fprintf(stderr, "usage: dumpfs filesys | device\n");
345 	exit(1);
346 }
347