xref: /dragonfly/sbin/dumpfs/dumpfs.c (revision 719497f4)
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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#) Copyright (c) 1983, 1992, 1993 The Regents of the University of California.  All rights reserved.
30  * @(#)dumpfs.c	8.5 (Berkeley) 4/29/95
31  * $FreeBSD: src/sbin/dumpfs/dumpfs.c,v 1.13.2.1 2001/01/22 18:10:11 iedowse Exp $
32  */
33 
34 #include <sys/param.h>
35 #include <sys/time.h>
36 
37 #include <vfs/ufs/dinode.h>
38 #include <vfs/ufs/fs.h>
39 
40 #include <err.h>
41 #include <fcntl.h>
42 #include <fstab.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 
48 static union {
49 	struct fs fs;
50 	char pad[MAXBSIZE];
51 } fsun;
52 #define	afs	fsun.fs
53 
54 static union {
55 	struct cg cg;
56 	char pad[MAXBSIZE];
57 } cgun;
58 #define	acg	cgun.cg
59 
60 static long	dev_bsize = 1;
61 
62 static int	dumpfs(char *);
63 static int	dumpcg(char *, int, int);
64 static int	marshal(const char*);
65 static void	pbits(void *, int);
66 static void	usage(void) __dead2;
67 
68 int
69 main(int argc, char **argv)
70 {
71 	struct fstab *fs;
72 	int ch, domarshal, eval;
73 
74 	domarshal = 0;
75 
76 	while ((ch = getopt(argc, argv, "m")) != -1)
77 		switch(ch) {
78 		case 'm':
79 			domarshal = 1;
80 			break;
81 		case '?':
82 		default:
83 			usage();
84 		}
85 	argc -= optind;
86 	argv += optind;
87 
88 	if (argc < 1)
89 		usage();
90 
91 	for (eval = 0; *argv; ++argv)
92 		if ((fs = getfsfile(*argv)) == NULL)
93 			if (domarshal)
94 				eval |= marshal(*argv);
95 			else
96 				eval |= dumpfs(*argv);
97 		else
98 			if (domarshal)
99 				eval |= marshal(fs->fs_spec);
100 			else
101 				eval |= dumpfs(fs->fs_spec);
102 	exit(eval);
103 }
104 
105 static int
106 dumpfs(char *name)
107 {
108 	ssize_t n;
109 	int fd, c, i, j, k, size;
110 
111 	if ((fd = open(name, O_RDONLY, 0)) < 0)
112 		goto err;
113 	if (lseek(fd, (off_t)SBOFF, SEEK_SET) == (off_t)-1)
114 		goto err;
115 	if ((n = read(fd, &afs, SBSIZE)) == -1)
116 		goto err;
117 
118 	if (n != SBSIZE) {
119 		warnx("%s: non-existent or truncated superblock, skipped",
120 		    name);
121 		close(fd);
122  		return (1);
123 	}
124  	if (afs.fs_magic != FS_MAGIC) {
125 		warnx("%s: superblock has bad magic number, skipped", name);
126 		close(fd);
127  		return (1);
128  	}
129 
130 	if (afs.fs_postblformat == FS_42POSTBLFMT)
131 		afs.fs_nrpos = 8;
132 	dev_bsize = afs.fs_fsize / fsbtodb(&afs, 1);
133 	printf("magic\t%x\ttime\t%s", afs.fs_magic,
134 	    ctime((time_t *)&afs.fs_time));
135 	printf("id\t[ %x %x ]\n", afs.fs_id[0], afs.fs_id[1]);
136 	printf("cylgrp\t%s\tinodes\t%s\n",
137 	    afs.fs_postblformat == FS_42POSTBLFMT ? "static" : "dynamic",
138 	    afs.fs_inodefmt < FS_44INODEFMT ? "4.2/4.3BSD" : "4.4BSD");
139 	printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
140 	    afs.fs_cstotal.cs_nbfree, afs.fs_cstotal.cs_ndir,
141 	    afs.fs_cstotal.cs_nifree, afs.fs_cstotal.cs_nffree);
142 	printf("ncg\t%d\tncyl\t%d\tsize\t%d\tblocks\t%d\n",
143 	    afs.fs_ncg, afs.fs_ncyl, afs.fs_size, afs.fs_dsize);
144 	printf("bsize\t%d\tshift\t%d\tmask\t0x%08x\n",
145 	    afs.fs_bsize, afs.fs_bshift, afs.fs_bmask);
146 	printf("fsize\t%d\tshift\t%d\tmask\t0x%08x\n",
147 	    afs.fs_fsize, afs.fs_fshift, afs.fs_fmask);
148 	printf("frag\t%d\tshift\t%d\tfsbtodb\t%d\n",
149 	    afs.fs_frag, afs.fs_fragshift, afs.fs_fsbtodb);
150 	printf("cpg\t%d\tbpg\t%d\tfpg\t%d\tipg\t%d\n",
151 	    afs.fs_cpg, afs.fs_fpg / afs.fs_frag, afs.fs_fpg, afs.fs_ipg);
152 	printf("minfree\t%d%%\toptim\t%s\tmaxcontig %d\tmaxbpg\t%d\n",
153 	    afs.fs_minfree, afs.fs_optim == FS_OPTSPACE ? "space" : "time",
154 	    afs.fs_maxcontig, afs.fs_maxbpg);
155 	printf("rotdelay %dms\trps\t%d\n",
156 	    afs.fs_rotdelay, afs.fs_rps);
157 	printf("ntrak\t%d\tnsect\t%d\tnpsect\t%d\tspc\t%d\n",
158 	    afs.fs_ntrak, afs.fs_nsect, afs.fs_npsect, afs.fs_spc);
159 	printf("symlinklen %d\ttrackskew %d\tinterleave %d\tcontigsumsize %d\n",
160 	    afs.fs_maxsymlinklen, afs.fs_trackskew, afs.fs_interleave,
161 	    afs.fs_contigsumsize);
162 	printf("nindir\t%d\tinopb\t%d\tnspf\t%d\tmaxfilesize\t%ju\n",
163 	    afs.fs_nindir, afs.fs_inopb, afs.fs_nspf,
164 	    (intmax_t)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 	close(fd);
236 	return (0);
237 
238 err:	if (fd != -1)
239 		close(fd);
240 	warn("%s", name);
241 	return (1);
242 };
243 
244 static int
245 dumpcg(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%jx\ttime\t%s",
259 	    afs.fs_postblformat == FS_42POSTBLFMT ?
260 	    ((struct ocg *)&acg)->cg_magic : acg.cg_magic,
261 	    (intmax_t)cur, ctime((time_t *)&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 static int
310 marshal(const char *name)
311 {
312 	ssize_t n;
313 	int fd;
314 	static char realname[PATH_MAX];
315 
316 	if ((fd = open(name, O_RDONLY, 0)) < 0)
317 		goto err;
318 	if (lseek(fd, (off_t)SBOFF, SEEK_SET) == (off_t)-1)
319 		goto err;
320 	if ((n = read(fd, &afs, SBSIZE)) == -1)
321 		goto err;
322 
323 	if (n != SBSIZE) {
324 		warnx("%s: non-existent or truncated superblock, skipped",
325 		    name);
326 		close(fd);
327  		return (1);
328 	}
329  	if (afs.fs_magic != FS_MAGIC) {
330 		warnx("%s: superblock has bad magic number, skipped", name);
331 		close(fd);
332  		return (1);
333  	}
334 
335 	if(strncmp(name, "/dev", 4) == 0) {
336 		snprintf(realname, PATH_MAX, "%s", name);
337 	} else {
338 		snprintf(realname, PATH_MAX, "/dev/%s", name);
339 	}
340 
341 	printf("# newfs command for %s (%s)\n", name, realname);
342 	printf("newfs ");
343 	if (afs.fs_flags & FS_DOSOFTDEP)
344 		printf("-U ");
345 	printf("-a %d ", afs.fs_maxcontig);
346 	printf("-b %d ", afs.fs_bsize);
347 	/* -c calculated */
348 	/* -d should be 0 per manpage */
349 	printf("-e %d ", afs.fs_maxbpg);
350 	printf("-f %d ", afs.fs_fsize);
351 	printf("-g %d ", afs.fs_avgfilesize);
352 	printf("-h %d ", afs.fs_avgfpdir);
353 	/* -i calculated */
354 	/* -j..l not implemented */
355 	printf("-m %d ", afs.fs_minfree);
356 	/* -n not implemented */
357 	printf(" -o ");
358 	switch (afs.fs_optim) {
359 	case FS_OPTSPACE:
360 		printf("space ");
361 		break;
362 	case FS_OPTTIME:
363 		printf("time ");
364 		break;
365 	default:
366 		printf("unknown ");
367 		break;
368 	}
369 	/* -p..r not implemented */
370 	printf("-s %jd ", (intmax_t)afs.fs_size);
371 	printf("%s ", realname);
372 	printf("\n");
373 
374 	return (0);
375 
376 err:    if (fd != -1)
377                 close(fd);
378         warn("%s", name);
379         return (1);
380 
381 }
382 
383 static void
384 pbits(void *vp, int max)
385 {
386 	int i;
387 	char *p;
388 	int count, j;
389 
390 	for (count = i = 0, p = vp; i < max; i++)
391 		if (isset(p, i)) {
392 			if (count)
393 				printf(",%s", count % 6 ? " " : "\n\t");
394 			count++;
395 			printf("%d", i);
396 			j = i;
397 			while ((i+1)<max && isset(p, i+1))
398 				i++;
399 			if (i != j)
400 				printf("-%d", i);
401 		}
402 	printf("\n");
403 }
404 
405 static void
406 usage(void)
407 {
408 	fprintf(stderr, "usage: dumpfs filesys | device\n");
409 	exit(1);
410 }
411