2 * Copyright (c) 1983, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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 $
36 * $DragonFly: src/sbin/dumpfs/dumpfs.c,v 1.9 2008/01/07 12:07:06 matthias Exp $
39 #include <sys/param.h>
42 #include <vfs/ufs/dinode.h>
43 #include <vfs/ufs/fs.h>
68 int dumpcg(char *, int, int);
69 int marshal(const char*);
70 void pbits(void *, int);
74 main(int argc
, char **argv
)
77 int ch
, domarshal
, eval
;
81 while ((ch
= getopt(argc
, argv
, "m")) != -1)
96 for (eval
= 0; *argv
; ++argv
)
97 if ((fs
= getfsfile(*argv
)) == NULL
)
99 eval
|= marshal(*argv
);
101 eval
|= dumpfs(*argv
);
104 eval
|= marshal(fs
->fs_spec
);
106 eval
|= dumpfs(fs
->fs_spec
);
114 int fd
, c
, i
, j
, k
, size
;
116 if ((fd
= open(name
, O_RDONLY
, 0)) < 0)
118 if (lseek(fd
, (off_t
)SBOFF
, SEEK_SET
) == (off_t
)-1)
120 if ((n
= read(fd
, &afs
, SBSIZE
)) == -1)
124 warnx("%s: non-existent or truncated superblock, skipped",
129 if (afs
.fs_magic
!= FS_MAGIC
) {
130 warnx("%s: superblock has bad magic number, skipped", name
);
135 if (afs
.fs_postblformat
== FS_42POSTBLFMT
)
137 dev_bsize
= afs
.fs_fsize
/ fsbtodb(&afs
, 1);
138 printf("magic\t%x\ttime\t%s", afs
.fs_magic
,
139 ctime(&afs
.fs_time
));
140 printf("id\t[ %x %x ]\n", afs
.fs_id
[0], afs
.fs_id
[1]);
141 printf("cylgrp\t%s\tinodes\t%s\n",
142 afs
.fs_postblformat
== FS_42POSTBLFMT
? "static" : "dynamic",
143 afs
.fs_inodefmt
< FS_44INODEFMT
? "4.2/4.3BSD" : "4.4BSD");
144 printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
145 afs
.fs_cstotal
.cs_nbfree
, afs
.fs_cstotal
.cs_ndir
,
146 afs
.fs_cstotal
.cs_nifree
, afs
.fs_cstotal
.cs_nffree
);
147 printf("ncg\t%d\tncyl\t%d\tsize\t%d\tblocks\t%d\n",
148 afs
.fs_ncg
, afs
.fs_ncyl
, afs
.fs_size
, afs
.fs_dsize
);
149 printf("bsize\t%d\tshift\t%d\tmask\t0x%08x\n",
150 afs
.fs_bsize
, afs
.fs_bshift
, afs
.fs_bmask
);
151 printf("fsize\t%d\tshift\t%d\tmask\t0x%08x\n",
152 afs
.fs_fsize
, afs
.fs_fshift
, afs
.fs_fmask
);
153 printf("frag\t%d\tshift\t%d\tfsbtodb\t%d\n",
154 afs
.fs_frag
, afs
.fs_fragshift
, afs
.fs_fsbtodb
);
155 printf("cpg\t%d\tbpg\t%d\tfpg\t%d\tipg\t%d\n",
156 afs
.fs_cpg
, afs
.fs_fpg
/ afs
.fs_frag
, afs
.fs_fpg
, afs
.fs_ipg
);
157 printf("minfree\t%d%%\toptim\t%s\tmaxcontig %d\tmaxbpg\t%d\n",
158 afs
.fs_minfree
, afs
.fs_optim
== FS_OPTSPACE
? "space" : "time",
159 afs
.fs_maxcontig
, afs
.fs_maxbpg
);
160 printf("rotdelay %dms\trps\t%d\n",
161 afs
.fs_rotdelay
, afs
.fs_rps
);
162 printf("ntrak\t%d\tnsect\t%d\tnpsect\t%d\tspc\t%d\n",
163 afs
.fs_ntrak
, afs
.fs_nsect
, afs
.fs_npsect
, afs
.fs_spc
);
164 printf("symlinklen %d\ttrackskew %d\tinterleave %d\tcontigsumsize %d\n",
165 afs
.fs_maxsymlinklen
, afs
.fs_trackskew
, afs
.fs_interleave
,
166 afs
.fs_contigsumsize
);
167 printf("nindir\t%d\tinopb\t%d\tnspf\t%d\tmaxfilesize\t%qu\n",
168 afs
.fs_nindir
, afs
.fs_inopb
, afs
.fs_nspf
, afs
.fs_maxfilesize
);
169 printf("sblkno\t%d\tcblkno\t%d\tiblkno\t%d\tdblkno\t%d\n",
170 afs
.fs_sblkno
, afs
.fs_cblkno
, afs
.fs_iblkno
, afs
.fs_dblkno
);
171 printf("sbsize\t%d\tcgsize\t%d\tcgoffset %d\tcgmask\t0x%08x\n",
172 afs
.fs_sbsize
, afs
.fs_cgsize
, afs
.fs_cgoffset
, afs
.fs_cgmask
);
173 printf("csaddr\t%d\tcssize\t%d\tshift\t%d\tmask\t0x%08x\n",
174 afs
.fs_csaddr
, afs
.fs_cssize
, afs
.fs_csshift
, afs
.fs_csmask
);
175 printf("cgrotor\t%d\tfmod\t%d\tronly\t%d\tclean\t%d\n",
176 afs
.fs_cgrotor
, afs
.fs_fmod
, afs
.fs_ronly
, afs
.fs_clean
);
178 if (afs
.fs_flags
== 0)
180 if (afs
.fs_flags
& FS_UNCLEAN
)
182 if (afs
.fs_flags
& FS_DOSOFTDEP
)
183 printf("soft-updates ");
184 if ((afs
.fs_flags
& ~(FS_UNCLEAN
| FS_DOSOFTDEP
)) != 0)
185 printf("unknown flags (%#x)",
186 afs
.fs_flags
& ~(FS_UNCLEAN
| FS_DOSOFTDEP
));
189 printf("blocks available in each of %d rotational positions",
192 printf("(no rotational position table)\n");
193 for (c
= 0; c
< afs
.fs_cpc
; c
++) {
194 printf("\ncylinder number %d:", c
);
195 for (i
= 0; i
< afs
.fs_nrpos
; i
++) {
196 if (fs_postbl(&afs
, c
)[i
] == -1)
198 printf("\n position %d:\t", i
);
199 for (j
= fs_postbl(&afs
, c
)[i
], k
= 1; ;
200 j
+= fs_rotbl(&afs
)[j
], k
++) {
204 if (fs_rotbl(&afs
)[j
] == 0)
209 printf("\ncs[].cs_(nbfree,ndir,nifree,nffree):\n\t");
210 afs
.fs_csp
= calloc(1, afs
.fs_cssize
);
211 for (i
= 0, j
= 0; i
< afs
.fs_cssize
; i
+= afs
.fs_bsize
, j
++) {
212 size
= afs
.fs_cssize
- i
< afs
.fs_bsize
?
213 afs
.fs_cssize
- i
: afs
.fs_bsize
;
215 (off_t
)(fsbtodb(&afs
, (afs
.fs_csaddr
+ j
* afs
.fs_frag
))) *
216 (off_t
)dev_bsize
, SEEK_SET
) == (off_t
)-1)
218 if (read(fd
, (char *)afs
.fs_csp
+ i
, size
) != size
)
221 for (i
= 0; i
< afs
.fs_ncg
; i
++) {
222 struct csum
*cs
= &afs
.fs_cs(&afs
, i
);
225 printf("(%d,%d,%d,%d) ",
226 cs
->cs_nbfree
, cs
->cs_ndir
, cs
->cs_nifree
, cs
->cs_nffree
);
229 if (afs
.fs_ncyl
% afs
.fs_cpg
) {
230 printf("cylinders in last group %d\n",
231 i
= afs
.fs_ncyl
% afs
.fs_cpg
);
232 printf("blocks in last group %d\n",
233 i
* afs
.fs_spc
/ NSPB(&afs
));
236 for (i
= 0; i
< afs
.fs_ncg
; i
++)
237 if (dumpcg(name
, fd
, i
))
249 dumpcg(char *name
, int fd
, int c
)
254 printf("\ncg %d:\n", c
);
255 if ((cur
= lseek(fd
, (off_t
)(fsbtodb(&afs
, cgtod(&afs
, c
))) *
256 (off_t
)dev_bsize
, SEEK_SET
)) == (off_t
)-1)
258 if (read(fd
, &acg
, afs
.fs_bsize
) != afs
.fs_bsize
) {
259 warnx("%s: error reading cg", name
);
262 printf("magic\t%x\ttell\t%qx\ttime\t%s",
263 afs
.fs_postblformat
== FS_42POSTBLFMT
?
264 ((struct ocg
*)&acg
)->cg_magic
: acg
.cg_magic
,
265 cur
, ctime(&acg
.cg_time
));
266 printf("cgx\t%d\tncyl\t%d\tniblk\t%d\tndblk\t%d\n",
267 acg
.cg_cgx
, acg
.cg_ncyl
, acg
.cg_niblk
, acg
.cg_ndblk
);
268 printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
269 acg
.cg_cs
.cs_nbfree
, acg
.cg_cs
.cs_ndir
,
270 acg
.cg_cs
.cs_nifree
, acg
.cg_cs
.cs_nffree
);
271 printf("rotor\t%d\tirotor\t%d\tfrotor\t%d\nfrsum",
272 acg
.cg_rotor
, acg
.cg_irotor
, acg
.cg_frotor
);
273 for (i
= 1, j
= 0; i
< afs
.fs_frag
; i
++) {
274 printf("\t%d", acg
.cg_frsum
[i
]);
275 j
+= i
* acg
.cg_frsum
[i
];
277 printf("\nsum of frsum: %d", j
);
278 if (afs
.fs_contigsumsize
> 0) {
279 for (i
= 1; i
< afs
.fs_contigsumsize
; i
++) {
280 if ((i
- 1) % 8 == 0)
281 printf("\nclusters %d-%d:", i
,
282 afs
.fs_contigsumsize
- 1 < i
+ 7 ?
283 afs
.fs_contigsumsize
- 1 : i
+ 7);
284 printf("\t%d", cg_clustersum(&acg
)[i
]);
286 printf("\nclusters size %d and over: %d\n",
287 afs
.fs_contigsumsize
,
288 cg_clustersum(&acg
)[afs
.fs_contigsumsize
]);
289 printf("clusters free:\t");
290 pbits(cg_clustersfree(&acg
), acg
.cg_nclusterblks
);
294 pbits(cg_inosused(&acg
), afs
.fs_ipg
);
296 pbits(cg_blksfree(&acg
), afs
.fs_fpg
);
298 for (i
= 0; i
< afs
.fs_cpg
; i
++) {
299 if (cg_blktot(&acg
)[i
] == 0)
301 printf(" c%d:\t(%d)\t", i
, cg_blktot(&acg
)[i
]);
302 for (j
= 0; j
< afs
.fs_nrpos
; j
++) {
303 if (afs
.fs_cpc
> 0 &&
304 fs_postbl(&afs
, i
% afs
.fs_cpc
)[j
] == -1)
306 printf(" %d", cg_blks(&afs
, &acg
, i
)[j
]);
314 marshal(const char *name
)
318 static char realname
[12];
320 if ((fd
= open(name
, O_RDONLY
, 0)) < 0)
322 if (lseek(fd
, (off_t
)SBOFF
, SEEK_SET
) == (off_t
)-1)
324 if ((n
= read(fd
, &afs
, SBSIZE
)) == -1)
328 warnx("%s: non-existent or truncated superblock, skipped",
333 if (afs
.fs_magic
!= FS_MAGIC
) {
334 warnx("%s: superblock has bad magic number, skipped", name
);
339 if(strncmp(name
, "/dev", 4) == 0) {
340 snprintf(realname
, 12, "%s", name
);
342 snprintf(realname
, 12, "/dev/%s", name
);
345 printf("# newfs command for %s (%s)\n", name
, realname
);
347 if (afs
.fs_flags
& FS_DOSOFTDEP
)
349 printf("-a %d ", afs
.fs_maxcontig
);
350 printf("-b %d ", afs
.fs_bsize
);
352 /* -d should be 0 per manpage */
353 printf("-e %d ", afs
.fs_maxbpg
);
354 printf("-f %d ", afs
.fs_fsize
);
355 printf("-g %d ", afs
.fs_avgfilesize
);
356 printf("-h %d ", afs
.fs_avgfpdir
);
358 /* -j..l not implemented */
359 printf("-m %d ", afs
.fs_minfree
);
360 /* -n not implemented */
362 switch (afs
.fs_optim
) {
373 /* -p..r not implemented */
374 printf("-s %jd ", (intmax_t)afs
.fs_size
);
375 printf("%s ", realname
);
388 pbits(void *vp
, int max
)
394 for (count
= i
= 0, p
= vp
; i
< max
; i
++)
397 printf(",%s", count
% 6 ? " " : "\n\t");
401 while ((i
+1)<max
&& isset(p
, i
+1))
412 fprintf(stderr
, "usage: dumpfs filesys | device\n");