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