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