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