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