Cleanup:
[dragonfly.git] / sbin / dumpfs / dumpfs.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1983, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
1de703da
MD
32 *
33 * @(#) Copyright (c) 1983, 1992, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)dumpfs.c 8.5 (Berkeley) 4/29/95
35 * $FreeBSD: src/sbin/dumpfs/dumpfs.c,v 1.13.2.1 2001/01/22 18:10:11 iedowse Exp $
d2e67f89 36 * $DragonFly: src/sbin/dumpfs/dumpfs.c,v 1.7 2005/11/06 12:09:06 swildner Exp $
984263bc
MD
37 */
38
984263bc
MD
39#include <sys/param.h>
40#include <sys/time.h>
41
38a690d7 42#include <vfs/ufs/fs.h>
984263bc
MD
43
44#include <err.h>
45#include <fcntl.h>
46#include <fstab.h>
47#include <stdio.h>
48#include <stdlib.h>
49#include <unistd.h>
50
51union {
52 struct fs fs;
53 char pad[MAXBSIZE];
54} fsun;
55#define afs fsun.fs
56
57union {
58 struct cg cg;
59 char pad[MAXBSIZE];
60} cgun;
61#define acg cgun.cg
62
63long dev_bsize = 1;
64
9a76cd75
DR
65int dumpfs(char *);
66int dumpcg(char *, int, int);
67void pbits(void *, int);
68void usage(void);
984263bc
MD
69
70int
b5744197 71main(int argc, char **argv)
984263bc 72{
d2e67f89 73 struct fstab *fs;
984263bc
MD
74 int ch, eval;
75
76 while ((ch = getopt(argc, argv, "")) != -1)
77 switch(ch) {
78 case '?':
79 default:
80 usage();
81 }
82 argc -= optind;
83 argv += optind;
84
85 if (argc < 1)
86 usage();
87
88 for (eval = 0; *argv; ++argv)
89 if ((fs = getfsfile(*argv)) == NULL)
90 eval |= dumpfs(*argv);
91 else
92 eval |= dumpfs(fs->fs_spec);
93 exit(eval);
94}
95
96int
b5744197 97dumpfs(char *name)
984263bc
MD
98{
99 ssize_t n;
100 int fd, c, i, j, k, size;
101
102 if ((fd = open(name, O_RDONLY, 0)) < 0)
103 goto err;
104 if (lseek(fd, (off_t)SBOFF, SEEK_SET) == (off_t)-1)
105 goto err;
106 if ((n = read(fd, &afs, SBSIZE)) == -1)
107 goto err;
108
109 if (n != SBSIZE) {
110 warnx("%s: non-existent or truncated superblock, skipped",
111 name);
7cee7052 112 close(fd);
984263bc
MD
113 return (1);
114 }
115 if (afs.fs_magic != FS_MAGIC) {
116 warnx("%s: superblock has bad magic number, skipped", name);
7cee7052 117 close(fd);
984263bc
MD
118 return (1);
119 }
120
121 if (afs.fs_postblformat == FS_42POSTBLFMT)
122 afs.fs_nrpos = 8;
123 dev_bsize = afs.fs_fsize / fsbtodb(&afs, 1);
124 printf("magic\t%x\ttime\t%s", afs.fs_magic,
125 ctime(&afs.fs_time));
126 printf("id\t[ %x %x ]\n", afs.fs_id[0], afs.fs_id[1]);
127 printf("cylgrp\t%s\tinodes\t%s\n",
128 afs.fs_postblformat == FS_42POSTBLFMT ? "static" : "dynamic",
129 afs.fs_inodefmt < FS_44INODEFMT ? "4.2/4.3BSD" : "4.4BSD");
130 printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
131 afs.fs_cstotal.cs_nbfree, afs.fs_cstotal.cs_ndir,
132 afs.fs_cstotal.cs_nifree, afs.fs_cstotal.cs_nffree);
133 printf("ncg\t%d\tncyl\t%d\tsize\t%d\tblocks\t%d\n",
134 afs.fs_ncg, afs.fs_ncyl, afs.fs_size, afs.fs_dsize);
135 printf("bsize\t%d\tshift\t%d\tmask\t0x%08x\n",
136 afs.fs_bsize, afs.fs_bshift, afs.fs_bmask);
137 printf("fsize\t%d\tshift\t%d\tmask\t0x%08x\n",
138 afs.fs_fsize, afs.fs_fshift, afs.fs_fmask);
139 printf("frag\t%d\tshift\t%d\tfsbtodb\t%d\n",
140 afs.fs_frag, afs.fs_fragshift, afs.fs_fsbtodb);
141 printf("cpg\t%d\tbpg\t%d\tfpg\t%d\tipg\t%d\n",
142 afs.fs_cpg, afs.fs_fpg / afs.fs_frag, afs.fs_fpg, afs.fs_ipg);
143 printf("minfree\t%d%%\toptim\t%s\tmaxcontig %d\tmaxbpg\t%d\n",
144 afs.fs_minfree, afs.fs_optim == FS_OPTSPACE ? "space" : "time",
145 afs.fs_maxcontig, afs.fs_maxbpg);
146 printf("rotdelay %dms\trps\t%d\n",
147 afs.fs_rotdelay, afs.fs_rps);
148 printf("ntrak\t%d\tnsect\t%d\tnpsect\t%d\tspc\t%d\n",
149 afs.fs_ntrak, afs.fs_nsect, afs.fs_npsect, afs.fs_spc);
150 printf("symlinklen %d\ttrackskew %d\tinterleave %d\tcontigsumsize %d\n",
151 afs.fs_maxsymlinklen, afs.fs_trackskew, afs.fs_interleave,
152 afs.fs_contigsumsize);
153 printf("nindir\t%d\tinopb\t%d\tnspf\t%d\tmaxfilesize\t%qu\n",
154 afs.fs_nindir, afs.fs_inopb, afs.fs_nspf, afs.fs_maxfilesize);
155 printf("sblkno\t%d\tcblkno\t%d\tiblkno\t%d\tdblkno\t%d\n",
156 afs.fs_sblkno, afs.fs_cblkno, afs.fs_iblkno, afs.fs_dblkno);
157 printf("sbsize\t%d\tcgsize\t%d\tcgoffset %d\tcgmask\t0x%08x\n",
158 afs.fs_sbsize, afs.fs_cgsize, afs.fs_cgoffset, afs.fs_cgmask);
159 printf("csaddr\t%d\tcssize\t%d\tshift\t%d\tmask\t0x%08x\n",
160 afs.fs_csaddr, afs.fs_cssize, afs.fs_csshift, afs.fs_csmask);
161 printf("cgrotor\t%d\tfmod\t%d\tronly\t%d\tclean\t%d\n",
162 afs.fs_cgrotor, afs.fs_fmod, afs.fs_ronly, afs.fs_clean);
163 printf("flags\t");
164 if (afs.fs_flags == 0)
165 printf("none");
166 if (afs.fs_flags & FS_UNCLEAN)
167 printf("unclean ");
168 if (afs.fs_flags & FS_DOSOFTDEP)
169 printf("soft-updates ");
170 if ((afs.fs_flags & ~(FS_UNCLEAN | FS_DOSOFTDEP)) != 0)
171 printf("unknown flags (%#x)",
172 afs.fs_flags & ~(FS_UNCLEAN | FS_DOSOFTDEP));
173 putchar('\n');
174 if (afs.fs_cpc != 0)
175 printf("blocks available in each of %d rotational positions",
176 afs.fs_nrpos);
177 else
178 printf("(no rotational position table)\n");
179 for (c = 0; c < afs.fs_cpc; c++) {
180 printf("\ncylinder number %d:", c);
181 for (i = 0; i < afs.fs_nrpos; i++) {
182 if (fs_postbl(&afs, c)[i] == -1)
183 continue;
184 printf("\n position %d:\t", i);
185 for (j = fs_postbl(&afs, c)[i], k = 1; ;
186 j += fs_rotbl(&afs)[j], k++) {
187 printf("%5d", j);
188 if (k % 12 == 0)
189 printf("\n\t\t");
190 if (fs_rotbl(&afs)[j] == 0)
191 break;
192 }
193 }
194 }
195 printf("\ncs[].cs_(nbfree,ndir,nifree,nffree):\n\t");
196 afs.fs_csp = calloc(1, afs.fs_cssize);
197 for (i = 0, j = 0; i < afs.fs_cssize; i += afs.fs_bsize, j++) {
198 size = afs.fs_cssize - i < afs.fs_bsize ?
199 afs.fs_cssize - i : afs.fs_bsize;
200 if (lseek(fd,
201 (off_t)(fsbtodb(&afs, (afs.fs_csaddr + j * afs.fs_frag))) *
202 (off_t)dev_bsize, SEEK_SET) == (off_t)-1)
203 goto err;
204 if (read(fd, (char *)afs.fs_csp + i, size) != size)
205 goto err;
206 }
207 for (i = 0; i < afs.fs_ncg; i++) {
208 struct csum *cs = &afs.fs_cs(&afs, i);
209 if (i && i % 4 == 0)
210 printf("\n\t");
211 printf("(%d,%d,%d,%d) ",
212 cs->cs_nbfree, cs->cs_ndir, cs->cs_nifree, cs->cs_nffree);
213 }
214 printf("\n");
215 if (afs.fs_ncyl % afs.fs_cpg) {
216 printf("cylinders in last group %d\n",
217 i = afs.fs_ncyl % afs.fs_cpg);
218 printf("blocks in last group %d\n",
219 i * afs.fs_spc / NSPB(&afs));
220 }
221 printf("\n");
222 for (i = 0; i < afs.fs_ncg; i++)
223 if (dumpcg(name, fd, i))
224 goto err;
7cee7052 225 close(fd);
984263bc
MD
226 return (0);
227
228err: if (fd != -1)
7cee7052 229 close(fd);
984263bc
MD
230 warn("%s", name);
231 return (1);
232};
233
234int
b5744197 235dumpcg(char *name, int fd, int c)
984263bc
MD
236{
237 off_t cur;
238 int i, j;
239
240 printf("\ncg %d:\n", c);
241 if ((cur = lseek(fd, (off_t)(fsbtodb(&afs, cgtod(&afs, c))) *
242 (off_t)dev_bsize, SEEK_SET)) == (off_t)-1)
243 return (1);
244 if (read(fd, &acg, afs.fs_bsize) != afs.fs_bsize) {
245 warnx("%s: error reading cg", name);
246 return (1);
247 }
248 printf("magic\t%x\ttell\t%qx\ttime\t%s",
249 afs.fs_postblformat == FS_42POSTBLFMT ?
250 ((struct ocg *)&acg)->cg_magic : acg.cg_magic,
251 cur, ctime(&acg.cg_time));
252 printf("cgx\t%d\tncyl\t%d\tniblk\t%d\tndblk\t%d\n",
253 acg.cg_cgx, acg.cg_ncyl, acg.cg_niblk, acg.cg_ndblk);
254 printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
255 acg.cg_cs.cs_nbfree, acg.cg_cs.cs_ndir,
256 acg.cg_cs.cs_nifree, acg.cg_cs.cs_nffree);
257 printf("rotor\t%d\tirotor\t%d\tfrotor\t%d\nfrsum",
258 acg.cg_rotor, acg.cg_irotor, acg.cg_frotor);
259 for (i = 1, j = 0; i < afs.fs_frag; i++) {
260 printf("\t%d", acg.cg_frsum[i]);
261 j += i * acg.cg_frsum[i];
262 }
263 printf("\nsum of frsum: %d", j);
264 if (afs.fs_contigsumsize > 0) {
265 for (i = 1; i < afs.fs_contigsumsize; i++) {
266 if ((i - 1) % 8 == 0)
267 printf("\nclusters %d-%d:", i,
268 afs.fs_contigsumsize - 1 < i + 7 ?
269 afs.fs_contigsumsize - 1 : i + 7);
270 printf("\t%d", cg_clustersum(&acg)[i]);
271 }
272 printf("\nclusters size %d and over: %d\n",
273 afs.fs_contigsumsize,
274 cg_clustersum(&acg)[afs.fs_contigsumsize]);
275 printf("clusters free:\t");
276 pbits(cg_clustersfree(&acg), acg.cg_nclusterblks);
277 } else
278 printf("\n");
279 printf("iused:\t");
280 pbits(cg_inosused(&acg), afs.fs_ipg);
281 printf("free:\t");
282 pbits(cg_blksfree(&acg), afs.fs_fpg);
283 printf("b:\n");
284 for (i = 0; i < afs.fs_cpg; i++) {
285 if (cg_blktot(&acg)[i] == 0)
286 continue;
287 printf(" c%d:\t(%d)\t", i, cg_blktot(&acg)[i]);
288 for (j = 0; j < afs.fs_nrpos; j++) {
289 if (afs.fs_cpc > 0 &&
290 fs_postbl(&afs, i % afs.fs_cpc)[j] == -1)
291 continue;
292 printf(" %d", cg_blks(&afs, &acg, i)[j]);
293 }
294 printf("\n");
295 }
296 return (0);
297};
298
299void
d2e67f89 300pbits(void *vp, int max)
984263bc 301{
d2e67f89
SW
302 int i;
303 char *p;
984263bc
MD
304 int count, j;
305
306 for (count = i = 0, p = vp; i < max; i++)
307 if (isset(p, i)) {
308 if (count)
309 printf(",%s", count % 6 ? " " : "\n\t");
310 count++;
311 printf("%d", i);
312 j = i;
313 while ((i+1)<max && isset(p, i+1))
314 i++;
315 if (i != j)
316 printf("-%d", i);
317 }
318 printf("\n");
319}
320
321void
b5744197 322usage(void)
984263bc 323{
7cee7052 324 fprintf(stderr, "usage: dumpfs filesys | device\n");
984263bc
MD
325 exit(1);
326}