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