]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/dumpfs/dumpfs.c
This commit was generated by cvs2svn to compensate for changes in r96263,
[FreeBSD/FreeBSD.git] / sbin / dumpfs / dumpfs.c
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.
32  */
33
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1983, 1992, 1993\n\
37         The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)dumpfs.c    8.5 (Berkeley) 4/29/95";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD$";
46 #endif /* not lint */
47
48 #include <sys/param.h>
49 #include <sys/time.h>
50
51 #include <ufs/ffs/fs.h>
52
53 #include <err.h>
54 #include <fcntl.h>
55 #include <fstab.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <unistd.h>
59
60 union {
61         struct fs fs;
62         char pad[MAXBSIZE];
63 } fsun;
64 #define afs     fsun.fs
65
66 union {
67         struct cg cg;
68         char pad[MAXBSIZE];
69 } cgun;
70 #define acg     cgun.cg
71
72 long    dev_bsize = 1;
73
74 int     dumpfs(const char *);
75 int     dumpcg(const char *, int, int);
76 void    pbits(void *, int);
77 void    usage(void) __dead2;
78
79 int
80 main(int argc, char *argv[])
81 {
82         struct fstab *fs;
83         int ch, eval;
84
85         while ((ch = getopt(argc, argv, "")) != -1)
86                 switch(ch) {
87                 case '?':
88                 default:
89                         usage();
90                 }
91         argc -= optind;
92         argv += optind;
93
94         if (argc < 1)
95                 usage();
96
97         for (eval = 0; *argv; ++argv)
98                 if ((fs = getfsfile(*argv)) == NULL)
99                         eval |= dumpfs(*argv);
100                 else
101                         eval |= dumpfs(fs->fs_spec);
102         exit(eval);
103 }
104
105 int
106 dumpfs(const char *name)
107 {
108         ssize_t n;
109         int fd, c, i, j, k, size;
110
111         if ((fd = open(name, O_RDONLY, 0)) < 0)
112                 goto err;
113         if (lseek(fd, (off_t)SBOFF, SEEK_SET) == (off_t)-1)
114                 goto err;
115         if ((n = read(fd, &afs, SBSIZE)) == -1)
116                 goto err;
117
118         if (n != SBSIZE) {
119                 warnx("%s: non-existent or truncated superblock, skipped",
120                     name);
121                 (void)close(fd);
122                 return (1);
123         }
124         if (afs.fs_magic != FS_MAGIC) {
125                 warnx("%s: superblock has bad magic number, skipped", name);
126                 (void)close(fd);
127                 return (1);
128         }
129
130         if (afs.fs_postblformat == FS_42POSTBLFMT)
131                 afs.fs_nrpos = 8;
132         dev_bsize = afs.fs_fsize / fsbtodb(&afs, 1);
133         printf("magic\t%x\ttime\t%s", afs.fs_magic,
134             ctime(&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%qu\n",
163             afs.fs_nindir, afs.fs_inopb, afs.fs_nspf, afs.fs_maxfilesize);
164         printf("sblkno\t%d\tcblkno\t%d\tiblkno\t%d\tdblkno\t%d\n",
165             afs.fs_sblkno, afs.fs_cblkno, afs.fs_iblkno, afs.fs_dblkno);
166         printf("sbsize\t%d\tcgsize\t%d\tcgoffset %d\tcgmask\t0x%08x\n",
167             afs.fs_sbsize, afs.fs_cgsize, afs.fs_cgoffset, afs.fs_cgmask);
168         printf("csaddr\t%d\tcssize\t%d\tshift\t%d\tmask\t0x%08x\n",
169             afs.fs_csaddr, afs.fs_cssize, afs.fs_csshift, afs.fs_csmask);
170         printf("cgrotor\t%d\tfmod\t%d\tronly\t%d\tclean\t%d\n",
171             afs.fs_cgrotor, afs.fs_fmod, afs.fs_ronly, afs.fs_clean);
172         printf("flags\t");
173         if (afs.fs_flags == 0)
174                 printf("none");
175         if (afs.fs_flags & FS_UNCLEAN)
176                         printf("unclean ");
177         if (afs.fs_flags & FS_DOSOFTDEP)
178                         printf("soft-updates ");
179         if ((afs.fs_flags & ~(FS_UNCLEAN | FS_DOSOFTDEP)) != 0)
180                         printf("unknown flags (%#x)", 
181                             afs.fs_flags & ~(FS_UNCLEAN | FS_DOSOFTDEP));
182         putchar('\n');
183         if (afs.fs_cpc != 0)
184                 printf("blocks available in each of %d rotational positions",
185                      afs.fs_nrpos);
186         else
187                 printf("(no rotational position table)\n");
188         for (c = 0; c < afs.fs_cpc; c++) {
189                 printf("\ncylinder number %d:", c);
190                 for (i = 0; i < afs.fs_nrpos; i++) {
191                         if (fs_postbl(&afs, c)[i] == -1)
192                                 continue;
193                         printf("\n   position %d:\t", i);
194                         for (j = fs_postbl(&afs, c)[i], k = 1; ;
195                              j += fs_rotbl(&afs)[j], k++) {
196                                 printf("%5d", j);
197                                 if (k % 12 == 0)
198                                         printf("\n\t\t");
199                                 if (fs_rotbl(&afs)[j] == 0)
200                                         break;
201                         }
202                 }
203         }
204         printf("\ncs[].cs_(nbfree,ndir,nifree,nffree):\n\t");
205         afs.fs_csp = calloc(1, afs.fs_cssize);
206         for (i = 0, j = 0; i < afs.fs_cssize; i += afs.fs_bsize, j++) {
207                 size = afs.fs_cssize - i < afs.fs_bsize ?
208                     afs.fs_cssize - i : afs.fs_bsize;
209                 if (lseek(fd,
210                     (off_t)(fsbtodb(&afs, (afs.fs_csaddr + j * afs.fs_frag))) *
211                     (off_t)dev_bsize, SEEK_SET) == (off_t)-1)
212                         goto err;
213                 if (read(fd, (char *)afs.fs_csp + i, size) != size)
214                         goto err;
215         }
216         for (i = 0; i < afs.fs_ncg; i++) {
217                 struct csum *cs = &afs.fs_cs(&afs, i);
218                 if (i && i % 4 == 0)
219                         printf("\n\t");
220                 printf("(%d,%d,%d,%d) ",
221                     cs->cs_nbfree, cs->cs_ndir, cs->cs_nifree, cs->cs_nffree);
222         }
223         printf("\n");
224         if (afs.fs_ncyl % afs.fs_cpg) {
225                 printf("cylinders in last group %d\n",
226                     i = afs.fs_ncyl % afs.fs_cpg);
227                 printf("blocks in last group %d\n",
228                     i * afs.fs_spc / NSPB(&afs));
229         }
230         printf("\n");
231         for (i = 0; i < afs.fs_ncg; i++)
232                 if (dumpcg(name, fd, i))
233                         goto err;
234         (void)close(fd);
235         return (0);
236
237 err:    if (fd != -1)
238                 (void)close(fd);
239         warn("%s", name);
240         return (1);
241 };
242
243 int
244 dumpcg(const char *name, int fd, int c)
245 {
246         off_t cur;
247         int i, j;
248
249         printf("\ncg %d:\n", c);
250         if ((cur = lseek(fd, (off_t)(fsbtodb(&afs, cgtod(&afs, c))) *
251             (off_t)dev_bsize, SEEK_SET)) == (off_t)-1)
252                 return (1);
253         if (read(fd, &acg, afs.fs_bsize) != afs.fs_bsize) {
254                 warnx("%s: error reading cg", name);
255                 return (1);
256         }
257         printf("magic\t%x\ttell\t%qx\ttime\t%s",
258             afs.fs_postblformat == FS_42POSTBLFMT ?
259             ((struct ocg *)&acg)->cg_magic : acg.cg_magic,
260             cur, ctime(&acg.cg_time));
261         printf("cgx\t%d\tncyl\t%d\tniblk\t%d\tndblk\t%d\n",
262             acg.cg_cgx, acg.cg_ncyl, acg.cg_niblk, acg.cg_ndblk);
263         printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
264             acg.cg_cs.cs_nbfree, acg.cg_cs.cs_ndir,
265             acg.cg_cs.cs_nifree, acg.cg_cs.cs_nffree);
266         printf("rotor\t%d\tirotor\t%d\tfrotor\t%d\nfrsum",
267             acg.cg_rotor, acg.cg_irotor, acg.cg_frotor);
268         for (i = 1, j = 0; i < afs.fs_frag; i++) {
269                 printf("\t%d", acg.cg_frsum[i]);
270                 j += i * acg.cg_frsum[i];
271         }
272         printf("\nsum of frsum: %d", j);
273         if (afs.fs_contigsumsize > 0) {
274                 for (i = 1; i < afs.fs_contigsumsize; i++) {
275                         if ((i - 1) % 8 == 0)
276                                 printf("\nclusters %d-%d:", i,
277                                     afs.fs_contigsumsize - 1 < i + 7 ?
278                                     afs.fs_contigsumsize - 1 : i + 7);
279                         printf("\t%d", cg_clustersum(&acg)[i]);
280                 }
281                 printf("\nclusters size %d and over: %d\n",
282                     afs.fs_contigsumsize,
283                     cg_clustersum(&acg)[afs.fs_contigsumsize]);
284                 printf("clusters free:\t");
285                 pbits(cg_clustersfree(&acg), acg.cg_nclusterblks);
286         } else
287                 printf("\n");
288         printf("iused:\t");
289         pbits(cg_inosused(&acg), afs.fs_ipg);
290         printf("free:\t");
291         pbits(cg_blksfree(&acg), afs.fs_fpg);
292         printf("b:\n");
293         for (i = 0; i < afs.fs_cpg; i++) {
294                 if (cg_blktot(&acg)[i] == 0)
295                         continue;
296                 printf("   c%d:\t(%d)\t", i, cg_blktot(&acg)[i]);
297                 for (j = 0; j < afs.fs_nrpos; j++) {
298                         if (afs.fs_cpc > 0 &&
299                             fs_postbl(&afs, i % afs.fs_cpc)[j] == -1)
300                                 continue;
301                         printf(" %d", cg_blks(&afs, &acg, i)[j]);
302                 }
303                 printf("\n");
304         }
305         return (0);
306 };
307
308 void
309 pbits(void *vp, int max)
310 {
311         int i;
312         char *p;
313         int count, j;
314
315         for (count = i = 0, p = vp; i < max; i++)
316                 if (isset(p, i)) {
317                         if (count)
318                                 printf(",%s", count % 6 ? " " : "\n\t");
319                         count++;
320                         printf("%d", i);
321                         j = i;
322                         while ((i+1)<max && isset(p, i+1))
323                                 i++;
324                         if (i != j)
325                                 printf("-%d", i);
326                 }
327         printf("\n");
328 }
329
330 void
331 usage(void)
332 {
333         (void)fprintf(stderr, "usage: dumpfs filesys | device\n");
334         exit(1);
335 }