xref: /freebsd/sbin/dumpfs/dumpfs.c (revision 2a63683b5d5c480d0f652f0ea9cf6b9621d55920)
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 *, int);
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, dosb, eval;
100 
101 	dofreespace = domarshal = dolabel = dosb = eval = 0;
102 
103 	while ((ch = getopt(argc, argv, "lfms")) != -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 's':
115 			dosb = 1;
116 			break;
117 		case '?':
118 		default:
119 			usage();
120 		}
121 	}
122 	argc -= optind;
123 	argv += optind;
124 
125 	if (argc < 1)
126 		usage();
127 	if (dofreespace && domarshal)
128 		usage();
129 	if (dofreespace > 2)
130 		usage();
131 
132 	while ((name = *argv++) != NULL) {
133 		if (ufs_disk_fillout_blank(&disk, name) == -1 ||
134 		    sbfind(&disk, 0) == -1) {
135 			ufserr(name);
136 			eval |= 1;
137 			continue;
138 		}
139 		if (dofreespace)
140 			eval |= dumpfreespace(name, dofreespace);
141 		else if (domarshal)
142 			eval |= marshal(name);
143 		else if (dolabel)
144 			eval |= dumpfsid();
145 		else
146 			eval |= dumpfs(name, dosb);
147 		ufs_disk_close(&disk);
148 	}
149 	exit(eval);
150 }
151 
152 static int
153 dumpfsid(void)
154 {
155 
156 	printf("%sufsid/%08x%08x\n", _PATH_DEV, afs.fs_id[0], afs.fs_id[1]);
157 	return 0;
158 }
159 
160 static int
161 dumpfs(const char *name, int dosb)
162 {
163 	time_t fstime, fsmtime;
164 	int64_t fssize;
165 	int32_t fsflags;
166 	int i;
167 
168 	switch (disk.d_ufs) {
169 	case 2:
170 		fssize = afs.fs_size;
171 		fstime = afs.fs_time;
172 		fsmtime = afs.fs_mtime;
173 		printf("magic\t%x (UFS2)\n", afs.fs_magic);
174 		printf("last mounted time\t%s", ctime(&fsmtime));
175 		printf("last modified time\t%s", ctime(&fstime));
176 		printf("superblock location\t%jd\tid\t[ %08x %08x ]\n",
177 		    (intmax_t)afs.fs_sblockloc, afs.fs_id[0], afs.fs_id[1]);
178 		printf("ncg\t%d\tsize\t%jd\tblocks\t%jd\n",
179 		    afs.fs_ncg, (intmax_t)fssize, (intmax_t)afs.fs_dsize);
180 		break;
181 	case 1:
182 		fssize = afs.fs_old_size;
183 		fstime = afs.fs_old_time;
184 		printf("magic\t%x (UFS1)\ttime\t%s",
185 		    afs.fs_magic, ctime(&fstime));
186 		printf("id\t[ %08x %08x ]\n", afs.fs_id[0], afs.fs_id[1]);
187 		printf("ncg\t%d\tsize\t%jd\tblocks\t%jd\n",
188 		    afs.fs_ncg, (intmax_t)fssize, (intmax_t)afs.fs_dsize);
189 		break;
190 	default:
191 		goto err;
192 	}
193 	printf("bsize\t%d\tshift\t%d\tmask\t0x%08x\n",
194 	    afs.fs_bsize, afs.fs_bshift, afs.fs_bmask);
195 	printf("fsize\t%d\tshift\t%d\tmask\t0x%08x\n",
196 	    afs.fs_fsize, afs.fs_fshift, afs.fs_fmask);
197 	printf("frag\t%d\tshift\t%d\tfsbtodb\t%d\n",
198 	    afs.fs_frag, afs.fs_fragshift, afs.fs_fsbtodb);
199 	printf("minfree\t%d%%\toptim\t%s\tsymlinklen %d\n",
200 	    afs.fs_minfree, afs.fs_optim == FS_OPTSPACE ? "space" : "time",
201 	    afs.fs_maxsymlinklen);
202 	switch (disk.d_ufs) {
203 	case 2:
204 		printf("%s %d\tmaxbpg\t%d\tmaxcontig %d\tcontigsumsize %d\n",
205 		    "maxbsize", afs.fs_maxbsize, afs.fs_maxbpg,
206 		    afs.fs_maxcontig, afs.fs_contigsumsize);
207 		printf("nbfree\t%jd\tndir\t%jd\tnifree\t%jd\tnffree\t%jd\n",
208 		    (intmax_t)afs.fs_cstotal.cs_nbfree,
209 		    (intmax_t)afs.fs_cstotal.cs_ndir,
210 		    (intmax_t)afs.fs_cstotal.cs_nifree,
211 		    (intmax_t)afs.fs_cstotal.cs_nffree);
212 		printf("bpg\t%d\tfpg\t%d\tipg\t%d\tunrefs\t%jd\n",
213 		    afs.fs_fpg / afs.fs_frag, afs.fs_fpg, afs.fs_ipg,
214 		    (intmax_t)afs.fs_unrefs);
215 		printf("nindir\t%d\tinopb\t%d\tmaxfilesize\t%ju\n",
216 		    afs.fs_nindir, afs.fs_inopb,
217 		    (uintmax_t)afs.fs_maxfilesize);
218 		printf("sbsize\t%d\tcgsize\t%d\tcsaddr\t%jd\tcssize\t%d\n",
219 		    afs.fs_sbsize, afs.fs_cgsize, (intmax_t)afs.fs_csaddr,
220 		    afs.fs_cssize);
221 		break;
222 	case 1:
223 		printf("maxbpg\t%d\tmaxcontig %d\tcontigsumsize %d\n",
224 		    afs.fs_maxbpg, afs.fs_maxcontig, afs.fs_contigsumsize);
225 		printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
226 		    afs.fs_old_cstotal.cs_nbfree, afs.fs_old_cstotal.cs_ndir,
227 		    afs.fs_old_cstotal.cs_nifree, afs.fs_old_cstotal.cs_nffree);
228 		printf("cpg\t%d\tbpg\t%d\tfpg\t%d\tipg\t%d\n",
229 		    afs.fs_old_cpg, afs.fs_fpg / afs.fs_frag, afs.fs_fpg,
230 		    afs.fs_ipg);
231 		printf("nindir\t%d\tinopb\t%d\tnspf\t%d\tmaxfilesize\t%ju\n",
232 		    afs.fs_nindir, afs.fs_inopb, afs.fs_old_nspf,
233 		    (uintmax_t)afs.fs_maxfilesize);
234 		printf("sbsize\t%d\tcgsize\t%d\tcgoffset %d\tcgmask\t0x%08x\n",
235 		    afs.fs_sbsize, afs.fs_cgsize, afs.fs_old_cgoffset,
236 		    afs.fs_old_cgmask);
237 		printf("csaddr\t%jd\tcssize\t%d\n",
238 		    (intmax_t)afs.fs_csaddr, afs.fs_cssize);
239 		printf("rotdelay %dms\trps\t%d\ttrackskew %d\tinterleave %d\n",
240 		    afs.fs_old_rotdelay, afs.fs_old_rps, afs.fs_old_trackskew,
241 		    afs.fs_old_interleave);
242 		printf("nsect\t%d\tnpsect\t%d\tspc\t%d\n",
243 		    afs.fs_old_nsect, afs.fs_old_npsect, afs.fs_old_spc);
244 		break;
245 	default:
246 		goto err;
247 	}
248 	printf("sblkno\t%d\tcblkno\t%d\tiblkno\t%d\tdblkno\t%d\n",
249 	    afs.fs_sblkno, afs.fs_cblkno, afs.fs_iblkno, afs.fs_dblkno);
250 	printf("cgrotor\t%d\tfmod\t%d\tronly\t%d\tclean\t%d\n",
251 	    afs.fs_cgrotor, afs.fs_fmod, afs.fs_ronly, afs.fs_clean);
252 	printf("metaspace %jd\tavgfpdir %d\tavgfilesize %d\n",
253 	    afs.fs_metaspace, afs.fs_avgfpdir, afs.fs_avgfilesize);
254 	printf("flags\t");
255 	if (afs.fs_old_flags & FS_FLAGS_UPDATED)
256 		fsflags = afs.fs_flags;
257 	else
258 		fsflags = afs.fs_old_flags;
259 	if (fsflags == 0)
260 		printf("none");
261 	if (fsflags & FS_UNCLEAN)
262 		printf("unclean ");
263 	if (fsflags & FS_DOSOFTDEP)
264 		printf("soft-updates%s ", (fsflags & FS_SUJ) ? "+journal" : "");
265 	if (fsflags & FS_NEEDSFSCK)
266 		printf("needs-fsck-run ");
267 	if (fsflags & FS_INDEXDIRS)
268 		printf("indexed-directories ");
269 	if (fsflags & FS_ACLS)
270 		printf("acls ");
271 	if (fsflags & FS_MULTILABEL)
272 		printf("multilabel ");
273 	if (fsflags & FS_GJOURNAL)
274 		printf("gjournal ");
275 	if (fsflags & FS_FLAGS_UPDATED)
276 		printf("fs_flags-expanded ");
277 	if (fsflags & FS_NFS4ACLS)
278 		printf("nfsv4acls ");
279 	if (fsflags & FS_TRIM)
280 		printf("trim ");
281 	fsflags &= ~(FS_UNCLEAN | FS_DOSOFTDEP | FS_NEEDSFSCK | FS_METACKHASH |
282 		     FS_ACLS | FS_MULTILABEL | FS_GJOURNAL | FS_FLAGS_UPDATED |
283 		     FS_NFS4ACLS | FS_SUJ | FS_TRIM | FS_INDEXDIRS);
284 	if (fsflags != 0)
285 		printf("unknown-flags (%#x)", fsflags);
286 	putchar('\n');
287 	if (afs.fs_flags & FS_METACKHASH) {
288 		printf("check hashes\t");
289 		fsflags = afs.fs_metackhash;
290 		if (fsflags == 0)
291 			printf("none");
292 		if (fsflags & CK_SUPERBLOCK)
293 			printf("superblock ");
294 		if (fsflags & CK_CYLGRP)
295 			printf("cylinder-groups ");
296 		if (fsflags & CK_INODE)
297 			printf("inodes ");
298 		if (fsflags & CK_INDIR)
299 			printf("indirect-blocks ");
300 		if (fsflags & CK_DIR)
301 			printf("directories ");
302 	}
303 	fsflags &= ~(CK_SUPERBLOCK | CK_CYLGRP | CK_INODE | CK_INDIR | CK_DIR);
304 	if (fsflags != 0)
305 		printf("unknown flags (%#x)", fsflags);
306 	putchar('\n');
307 	printf("fsmnt\t%s\n", afs.fs_fsmnt);
308 	printf("volname\t%s\tswuid\t%ju\tprovidersize\t%ju\n",
309 		afs.fs_volname, (uintmax_t)afs.fs_swuid,
310 		(uintmax_t)afs.fs_providersize);
311 	printf("\ncs[].cs_(nbfree,ndir,nifree,nffree):\n\t");
312 	for (i = 0; i < afs.fs_ncg; i++) {
313 		struct csum *cs = &afs.fs_cs(&afs, i);
314 		if (i && i % 4 == 0)
315 			printf("\n\t");
316 		printf("(%d,%d,%d,%d) ",
317 		    cs->cs_nbfree, cs->cs_ndir, cs->cs_nifree, cs->cs_nffree);
318 	}
319 	printf("\n");
320 	if (fssize % afs.fs_fpg) {
321 		if (disk.d_ufs == 1)
322 			printf("cylinders in last group %d\n",
323 			    howmany(afs.fs_old_size % afs.fs_fpg,
324 			    afs.fs_old_spc / afs.fs_old_nspf));
325 		printf("blocks in last group %ld\n\n",
326 		    (long)((fssize % afs.fs_fpg) / afs.fs_frag));
327 	}
328 	if (dosb)
329 		return (0);
330 	while ((i = cgread(&disk)) != 0) {
331 		if (i == -1 || dumpcg())
332 			goto err;
333 	}
334 	return (0);
335 
336 err:	ufserr(name);
337 	return (1);
338 }
339 
340 static int
341 dumpcg(void)
342 {
343 	time_t cgtime;
344 	off_t cur;
345 	int i, j;
346 
347 	printf("\ncg %d:\n", disk.d_lcg);
348 	cur = fsbtodb(&afs, cgtod(&afs, disk.d_lcg)) * disk.d_bsize;
349 	switch (disk.d_ufs) {
350 	case 2:
351 		cgtime = acg.cg_time;
352 		printf("magic\t%x\ttell\t%jx\ttime\t%s",
353 		    acg.cg_magic, (intmax_t)cur, ctime(&cgtime));
354 		printf("cgx\t%d\tndblk\t%d\tniblk\t%d\tinitiblk %d\tunrefs %d\n",
355 		    acg.cg_cgx, acg.cg_ndblk, acg.cg_niblk, acg.cg_initediblk,
356 		    acg.cg_unrefs);
357 		break;
358 	case 1:
359 		cgtime = acg.cg_old_time;
360 		printf("magic\t%x\ttell\t%jx\ttime\t%s",
361 		    acg.cg_magic, (intmax_t)cur, ctime(&cgtime));
362 		printf("cgx\t%d\tncyl\t%d\tniblk\t%d\tndblk\t%d\n",
363 		    acg.cg_cgx, acg.cg_old_ncyl, acg.cg_old_niblk,
364 		    acg.cg_ndblk);
365 		break;
366 	default:
367 		break;
368 	}
369 	printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
370 	    acg.cg_cs.cs_nbfree, acg.cg_cs.cs_ndir,
371 	    acg.cg_cs.cs_nifree, acg.cg_cs.cs_nffree);
372 	printf("rotor\t%d\tirotor\t%d\tfrotor\t%d\nfrsum",
373 	    acg.cg_rotor, acg.cg_irotor, acg.cg_frotor);
374 	for (i = 1, j = 0; i < afs.fs_frag; i++) {
375 		printf("\t%d", acg.cg_frsum[i]);
376 		j += i * acg.cg_frsum[i];
377 	}
378 	printf("\nsum of frsum: %d", j);
379 	if (afs.fs_contigsumsize > 0) {
380 		for (i = 1; i < afs.fs_contigsumsize; i++) {
381 			if ((i - 1) % 8 == 0)
382 				printf("\nclusters %d-%d:", i,
383 				    MIN(afs.fs_contigsumsize - 1, i + 7));
384 			printf("\t%d", cg_clustersum(&acg)[i]);
385 		}
386 		printf("\nclusters size %d and over: %d\n",
387 		    afs.fs_contigsumsize,
388 		    cg_clustersum(&acg)[afs.fs_contigsumsize]);
389 		printf("clusters free:\t");
390 		pbits(cg_clustersfree(&acg), acg.cg_nclusterblks);
391 	} else
392 		printf("\n");
393 	printf("inodes used:\t");
394 	pbits(cg_inosused(&acg), afs.fs_ipg);
395 	printf("blks free:\t");
396 	pbits(cg_blksfree(&acg), afs.fs_fpg);
397 	return (0);
398 }
399 
400 static int
401 dumpfreespace(const char *name, int fflag)
402 {
403 	int i;
404 
405 	while ((i = cgread(&disk)) != 0) {
406 		if (i == -1)
407 			goto err;
408 		dumpfreespacecg(fflag);
409 	}
410 	return (0);
411 err:
412 	ufserr(name);
413 	return (1);
414 }
415 
416 static void
417 dumpfreespacecg(int fflag)
418 {
419 
420 	pblklist(cg_blksfree(&acg), afs.fs_fpg, disk.d_lcg * afs.fs_fpg,
421 	    fflag);
422 }
423 
424 static int
425 marshal(const char *name)
426 {
427 	struct fs *fs;
428 
429 	fs = &disk.d_fs;
430 
431 	printf("# newfs command for %s (%s)\n", name, disk.d_name);
432 	printf("newfs ");
433 	if (fs->fs_volname[0] != '\0')
434 		printf("-L %s ", fs->fs_volname);
435 	printf("-O %d ", disk.d_ufs);
436 	if (fs->fs_flags & FS_DOSOFTDEP)
437 		printf("-U ");
438 	printf("-a %d ", fs->fs_maxcontig);
439 	printf("-b %d ", fs->fs_bsize);
440 	/* -c is dumb */
441 	printf("-d %d ", fs->fs_maxbsize);
442 	printf("-e %d ", fs->fs_maxbpg);
443 	printf("-f %d ", fs->fs_fsize);
444 	printf("-g %d ", fs->fs_avgfilesize);
445 	printf("-h %d ", fs->fs_avgfpdir);
446 	printf("-i %jd ", fragroundup(fs, lblktosize(fs, fragstoblks(fs,
447 	    fs->fs_fpg)) / fs->fs_ipg));
448 	if (fs->fs_flags & FS_SUJ)
449 		printf("-j ");
450 	if (fs->fs_flags & FS_GJOURNAL)
451 		printf("-J ");
452 	printf("-k %jd ", fs->fs_metaspace);
453 	if (fs->fs_flags & FS_MULTILABEL)
454 		printf("-l ");
455 	printf("-m %d ", fs->fs_minfree);
456 	/* -n unimplemented */
457 	printf("-o ");
458 	switch (fs->fs_optim) {
459 	case FS_OPTSPACE:
460 		printf("space ");
461 		break;
462 	case FS_OPTTIME:
463 		printf("time ");
464 		break;
465 	default:
466 		printf("unknown ");
467 		break;
468 	}
469 	/* -p..r unimplemented */
470 	printf("-s %jd ", (intmax_t)fsbtodb(fs, fs->fs_size));
471 	if (fs->fs_flags & FS_TRIM)
472 		printf("-t ");
473 	printf("%s ", disk.d_name);
474 	printf("\n");
475 
476 	return 0;
477 }
478 
479 static void
480 pbits(void *vp, int max)
481 {
482 	int i;
483 	char *p;
484 	int count, j;
485 
486 	for (count = i = 0, p = vp; i < max; i++)
487 		if (isset(p, i)) {
488 			if (count)
489 				printf(",%s", count % 6 ? " " : "\n\t");
490 			count++;
491 			printf("%d", i);
492 			j = i;
493 			while ((i+1)<max && isset(p, i+1))
494 				i++;
495 			if (i != j)
496 				printf("-%d", i);
497 		}
498 	printf("\n");
499 }
500 
501 static void
502 pblklist(void *vp, int max, off_t offset, int fflag)
503 {
504 	int i, j;
505 	char *p;
506 
507 	for (i = 0, p = vp; i < max; i++) {
508 		if (isset(p, i)) {
509 			printf("%jd", (intmax_t)(i + offset));
510 			if (fflag < 2) {
511 				j = i;
512 				while ((i+1)<max && isset(p, i+1))
513 					i++;
514 				if (i != j)
515 					printf("-%jd", (intmax_t)(i + offset));
516 			}
517 			printf("\n");
518 		}
519 	}
520 }
521 
522 static void
523 ufserr(const char *name)
524 {
525 	if (disk.d_error != NULL)
526 		warnx("%s: %s", name, disk.d_error);
527 	else if (errno)
528 		warn("%s", name);
529 }
530 
531 static void
532 usage(void)
533 {
534 	(void)fprintf(stderr, "usage: dumpfs [-flm] filesys | device\n");
535 	exit(1);
536 }
537