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