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