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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1983, 1992, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)dumpfs.c 8.5 (Berkeley) 4/29/95
31 * $FreeBSD: src/sbin/dumpfs/dumpfs.c,v 1.13.2.1 2001/01/22 18:10:11 iedowse Exp $
34 #include <sys/param.h>
37 #include <vfs/ufs/dinode.h>
38 #include <vfs/ufs/fs.h>
60 static long dev_bsize
= 1;
62 static int dumpfs(char *);
63 static int dumpcg(char *, int, int);
64 static int marshal(const char*);
65 static void pbits(void *, int);
66 static void usage(void) __dead2
;
69 main(int argc
, char **argv
)
72 int ch
, domarshal
, eval
;
76 while ((ch
= getopt(argc
, argv
, "m")) != -1)
91 for (eval
= 0; *argv
; ++argv
)
92 if ((fs
= getfsfile(*argv
)) == NULL
)
94 eval
|= marshal(*argv
);
96 eval
|= dumpfs(*argv
);
99 eval
|= marshal(fs
->fs_spec
);
101 eval
|= dumpfs(fs
->fs_spec
);
109 int fd
, c
, i
, j
, k
, size
;
111 if ((fd
= open(name
, O_RDONLY
, 0)) < 0)
113 if (lseek(fd
, (off_t
)SBOFF
, SEEK_SET
) == (off_t
)-1)
115 if ((n
= read(fd
, &afs
, SBSIZE
)) == -1)
119 warnx("%s: non-existent or truncated superblock, skipped",
124 if (afs
.fs_magic
!= FS_MAGIC
) {
125 warnx("%s: superblock has bad magic number, skipped", name
);
130 if (afs
.fs_postblformat
== FS_42POSTBLFMT
)
132 dev_bsize
= afs
.fs_fsize
/ fsbtodb(&afs
, 1);
133 printf("magic\t%x\ttime\t%s", afs
.fs_magic
,
134 ctime((time_t *)&afs
.fs_time
));
135 printf("id\t[ %x %x ]\n", afs
.fs_id
[0], afs
.fs_id
[1]);
136 printf("cylgrp\t%s\tinodes\t%s\n",
137 afs
.fs_postblformat
== FS_42POSTBLFMT
? "static" : "dynamic",
138 afs
.fs_inodefmt
< FS_44INODEFMT
? "4.2/4.3BSD" : "4.4BSD");
139 printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
140 afs
.fs_cstotal
.cs_nbfree
, afs
.fs_cstotal
.cs_ndir
,
141 afs
.fs_cstotal
.cs_nifree
, afs
.fs_cstotal
.cs_nffree
);
142 printf("ncg\t%d\tncyl\t%d\tsize\t%d\tblocks\t%d\n",
143 afs
.fs_ncg
, afs
.fs_ncyl
, afs
.fs_size
, afs
.fs_dsize
);
144 printf("bsize\t%d\tshift\t%d\tmask\t0x%08x\n",
145 afs
.fs_bsize
, afs
.fs_bshift
, afs
.fs_bmask
);
146 printf("fsize\t%d\tshift\t%d\tmask\t0x%08x\n",
147 afs
.fs_fsize
, afs
.fs_fshift
, afs
.fs_fmask
);
148 printf("frag\t%d\tshift\t%d\tfsbtodb\t%d\n",
149 afs
.fs_frag
, afs
.fs_fragshift
, afs
.fs_fsbtodb
);
150 printf("cpg\t%d\tbpg\t%d\tfpg\t%d\tipg\t%d\n",
151 afs
.fs_cpg
, afs
.fs_fpg
/ afs
.fs_frag
, afs
.fs_fpg
, afs
.fs_ipg
);
152 printf("minfree\t%d%%\toptim\t%s\tmaxcontig %d\tmaxbpg\t%d\n",
153 afs
.fs_minfree
, afs
.fs_optim
== FS_OPTSPACE
? "space" : "time",
154 afs
.fs_maxcontig
, afs
.fs_maxbpg
);
155 printf("rotdelay %dms\trps\t%d\n",
156 afs
.fs_rotdelay
, afs
.fs_rps
);
157 printf("ntrak\t%d\tnsect\t%d\tnpsect\t%d\tspc\t%d\n",
158 afs
.fs_ntrak
, afs
.fs_nsect
, afs
.fs_npsect
, afs
.fs_spc
);
159 printf("symlinklen %d\ttrackskew %d\tinterleave %d\tcontigsumsize %d\n",
160 afs
.fs_maxsymlinklen
, afs
.fs_trackskew
, afs
.fs_interleave
,
161 afs
.fs_contigsumsize
);
162 printf("nindir\t%d\tinopb\t%d\tnspf\t%d\tmaxfilesize\t%ju\n",
163 afs
.fs_nindir
, afs
.fs_inopb
, afs
.fs_nspf
,
164 (intmax_t)afs
.fs_maxfilesize
);
165 printf("sblkno\t%d\tcblkno\t%d\tiblkno\t%d\tdblkno\t%d\n",
166 afs
.fs_sblkno
, afs
.fs_cblkno
, afs
.fs_iblkno
, afs
.fs_dblkno
);
167 printf("sbsize\t%d\tcgsize\t%d\tcgoffset %d\tcgmask\t0x%08x\n",
168 afs
.fs_sbsize
, afs
.fs_cgsize
, afs
.fs_cgoffset
, afs
.fs_cgmask
);
169 printf("csaddr\t%d\tcssize\t%d\tshift\t%d\tmask\t0x%08x\n",
170 afs
.fs_csaddr
, afs
.fs_cssize
, afs
.fs_csshift
, afs
.fs_csmask
);
171 printf("cgrotor\t%d\tfmod\t%d\tronly\t%d\tclean\t%d\n",
172 afs
.fs_cgrotor
, afs
.fs_fmod
, afs
.fs_ronly
, afs
.fs_clean
);
174 if (afs
.fs_flags
== 0)
176 if (afs
.fs_flags
& FS_UNCLEAN
)
178 if (afs
.fs_flags
& FS_DOSOFTDEP
)
179 printf("soft-updates ");
180 if ((afs
.fs_flags
& ~(FS_UNCLEAN
| FS_DOSOFTDEP
)) != 0)
181 printf("unknown flags (%#x)",
182 afs
.fs_flags
& ~(FS_UNCLEAN
| FS_DOSOFTDEP
));
185 printf("blocks available in each of %d rotational positions",
188 printf("(no rotational position table)\n");
189 for (c
= 0; c
< afs
.fs_cpc
; c
++) {
190 printf("\ncylinder number %d:", c
);
191 for (i
= 0; i
< afs
.fs_nrpos
; i
++) {
192 if (fs_postbl(&afs
, c
)[i
] == -1)
194 printf("\n position %d:\t", i
);
195 for (j
= fs_postbl(&afs
, c
)[i
], k
= 1; ;
196 j
+= fs_rotbl(&afs
)[j
], k
++) {
200 if (fs_rotbl(&afs
)[j
] == 0)
205 printf("\ncs[].cs_(nbfree,ndir,nifree,nffree):\n\t");
206 afs
.fs_csp
= calloc(1, afs
.fs_cssize
);
207 for (i
= 0, j
= 0; i
< afs
.fs_cssize
; i
+= afs
.fs_bsize
, j
++) {
208 size
= afs
.fs_cssize
- i
< afs
.fs_bsize
?
209 afs
.fs_cssize
- i
: afs
.fs_bsize
;
211 (off_t
)(fsbtodb(&afs
, (afs
.fs_csaddr
+ j
* afs
.fs_frag
))) *
212 (off_t
)dev_bsize
, SEEK_SET
) == (off_t
)-1)
214 if (read(fd
, (char *)afs
.fs_csp
+ i
, size
) != size
)
217 for (i
= 0; i
< afs
.fs_ncg
; i
++) {
218 struct csum
*cs
= &afs
.fs_cs(&afs
, i
);
221 printf("(%d,%d,%d,%d) ",
222 cs
->cs_nbfree
, cs
->cs_ndir
, cs
->cs_nifree
, cs
->cs_nffree
);
225 if (afs
.fs_ncyl
% afs
.fs_cpg
) {
226 printf("cylinders in last group %d\n",
227 i
= afs
.fs_ncyl
% afs
.fs_cpg
);
228 printf("blocks in last group %d\n",
229 i
* afs
.fs_spc
/ NSPB(&afs
));
232 for (i
= 0; i
< afs
.fs_ncg
; i
++)
233 if (dumpcg(name
, fd
, i
))
245 dumpcg(char *name
, int fd
, int c
)
250 printf("\ncg %d:\n", c
);
251 if ((cur
= lseek(fd
, (off_t
)(fsbtodb(&afs
, cgtod(&afs
, c
))) *
252 (off_t
)dev_bsize
, SEEK_SET
)) == (off_t
)-1)
254 if (read(fd
, &acg
, afs
.fs_bsize
) != afs
.fs_bsize
) {
255 warnx("%s: error reading cg", name
);
258 printf("magic\t%x\ttell\t%jx\ttime\t%s",
259 afs
.fs_postblformat
== FS_42POSTBLFMT
?
260 ((struct ocg
*)&acg
)->cg_magic
: acg
.cg_magic
,
261 (intmax_t)cur
, ctime((time_t *)&acg
.cg_time
));
262 printf("cgx\t%d\tncyl\t%d\tniblk\t%d\tndblk\t%d\n",
263 acg
.cg_cgx
, acg
.cg_ncyl
, acg
.cg_niblk
, acg
.cg_ndblk
);
264 printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
265 acg
.cg_cs
.cs_nbfree
, acg
.cg_cs
.cs_ndir
,
266 acg
.cg_cs
.cs_nifree
, acg
.cg_cs
.cs_nffree
);
267 printf("rotor\t%d\tirotor\t%d\tfrotor\t%d\nfrsum",
268 acg
.cg_rotor
, acg
.cg_irotor
, acg
.cg_frotor
);
269 for (i
= 1, j
= 0; i
< afs
.fs_frag
; i
++) {
270 printf("\t%d", acg
.cg_frsum
[i
]);
271 j
+= i
* acg
.cg_frsum
[i
];
273 printf("\nsum of frsum: %d", j
);
274 if (afs
.fs_contigsumsize
> 0) {
275 for (i
= 1; i
< afs
.fs_contigsumsize
; i
++) {
276 if ((i
- 1) % 8 == 0)
277 printf("\nclusters %d-%d:", i
,
278 afs
.fs_contigsumsize
- 1 < i
+ 7 ?
279 afs
.fs_contigsumsize
- 1 : i
+ 7);
280 printf("\t%d", cg_clustersum(&acg
)[i
]);
282 printf("\nclusters size %d and over: %d\n",
283 afs
.fs_contigsumsize
,
284 cg_clustersum(&acg
)[afs
.fs_contigsumsize
]);
285 printf("clusters free:\t");
286 pbits(cg_clustersfree(&acg
), acg
.cg_nclusterblks
);
290 pbits(cg_inosused(&acg
), afs
.fs_ipg
);
292 pbits(cg_blksfree(&acg
), afs
.fs_fpg
);
294 for (i
= 0; i
< afs
.fs_cpg
; i
++) {
295 if (cg_blktot(&acg
)[i
] == 0)
297 printf(" c%d:\t(%d)\t", i
, cg_blktot(&acg
)[i
]);
298 for (j
= 0; j
< afs
.fs_nrpos
; j
++) {
299 if (afs
.fs_cpc
> 0 &&
300 fs_postbl(&afs
, i
% afs
.fs_cpc
)[j
] == -1)
302 printf(" %d", cg_blks(&afs
, &acg
, i
)[j
]);
310 marshal(const char *name
)
314 static char realname
[PATH_MAX
];
316 if ((fd
= open(name
, O_RDONLY
, 0)) < 0)
318 if (lseek(fd
, (off_t
)SBOFF
, SEEK_SET
) == (off_t
)-1)
320 if ((n
= read(fd
, &afs
, SBSIZE
)) == -1)
324 warnx("%s: non-existent or truncated superblock, skipped",
329 if (afs
.fs_magic
!= FS_MAGIC
) {
330 warnx("%s: superblock has bad magic number, skipped", name
);
335 if(strncmp(name
, "/dev", 4) == 0) {
336 snprintf(realname
, PATH_MAX
, "%s", name
);
338 snprintf(realname
, PATH_MAX
, "/dev/%s", name
);
341 printf("# newfs command for %s (%s)\n", name
, realname
);
343 if (afs
.fs_flags
& FS_DOSOFTDEP
)
345 printf("-a %d ", afs
.fs_maxcontig
);
346 printf("-b %d ", afs
.fs_bsize
);
348 /* -d should be 0 per manpage */
349 printf("-e %d ", afs
.fs_maxbpg
);
350 printf("-f %d ", afs
.fs_fsize
);
351 printf("-g %d ", afs
.fs_avgfilesize
);
352 printf("-h %d ", afs
.fs_avgfpdir
);
354 /* -j..l not implemented */
355 printf("-m %d ", afs
.fs_minfree
);
356 /* -n not implemented */
358 switch (afs
.fs_optim
) {
369 /* -p..r not implemented */
370 printf("-s %jd ", (intmax_t)afs
.fs_size
);
371 printf("%s ", realname
);
384 pbits(void *vp
, int max
)
390 for (count
= i
= 0, p
= vp
; i
< max
; i
++)
393 printf(",%s", count
% 6 ? " " : "\n\t");
397 while ((i
+1)<max
&& isset(p
, i
+1))
408 fprintf(stderr
, "usage: dumpfs filesys | device\n");