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