]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/dumpfs/dumpfs.c
This commit was generated by cvs2svn to compensate for changes in r99060,
[FreeBSD/FreeBSD.git] / sbin / dumpfs / dumpfs.c
1 /*
2  * Copyright (c) 2002 Networks Associates Technology, Inc.
3  * All rights reserved.
4  *
5  * This software was developed for the FreeBSD Project by Marshall
6  * Kirk McKusick and Network Associates Laboratories, the Security
7  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
8  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
9  * research program
10  *
11  * Copyright (c) 1982, 1989, 1993
12  *      The Regents of the University of California.  All rights reserved.
13  * (c) UNIX System Laboratories, Inc.
14  * Copyright (c) 1983, 1992, 1993
15  *      The Regents of the University of California.  All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  * 3. All advertising materials mentioning features or use of this software
26  *    must display the following acknowledgement:
27  *      This product includes software developed by the University of
28  *      California, Berkeley and its contributors.
29  * 4. Neither the name of the University nor the names of its contributors
30  *    may be used to endorse or promote products derived from this software
31  *    without specific prior written permission.
32  *
33  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43  * SUCH DAMAGE.
44  */
45
46 #ifndef lint
47 static const char copyright[] =
48 "@(#) Copyright (c) 1983, 1992, 1993\n\
49         The Regents of the University of California.  All rights reserved.\n";
50 #endif /* not lint */
51
52 #ifndef lint
53 #if 0
54 static char sccsid[] = "@(#)dumpfs.c    8.5 (Berkeley) 4/29/95";
55 #endif
56 static const char rcsid[] =
57   "$FreeBSD$";
58 #endif /* not lint */
59
60 #include <sys/param.h>
61 #include <sys/time.h>
62 #include <sys/disklabel.h>
63
64 #include <ufs/ufs/dinode.h>
65 #include <ufs/ffs/fs.h>
66
67 #include <err.h>
68 #include <fcntl.h>
69 #include <fstab.h>
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <unistd.h>
73
74 union {
75         struct fs fs;
76         char pad[MAXBSIZE];
77 } fsun;
78 #define afs     fsun.fs
79
80 union {
81         struct cg cg;
82         char pad[MAXBSIZE];
83 } cgun;
84 #define acg     cgun.cg
85
86 long    dev_bsize = 1;
87
88 /*
89  * Possible superblock locations ordered from most to least likely.
90  */
91 static int sblock_try[] = SBLOCKSEARCH;
92
93 int     dumpfs(const char *);
94 int     dumpcg(const char *, int, int);
95 void    pbits(void *, int);
96 void    usage(void) __dead2;
97
98 int
99 main(int argc, char *argv[])
100 {
101         struct fstab *fs;
102         int ch, eval;
103
104         while ((ch = getopt(argc, argv, "")) != -1)
105                 switch(ch) {
106                 case '?':
107                 default:
108                         usage();
109                 }
110         argc -= optind;
111         argv += optind;
112
113         if (argc < 1)
114                 usage();
115
116         for (eval = 0; *argv; ++argv)
117                 if ((fs = getfsfile(*argv)) == NULL)
118                         eval |= dumpfs(*argv);
119                 else
120                         eval |= dumpfs(fs->fs_spec);
121         exit(eval);
122 }
123
124 int
125 dumpfs(const char *name)
126 {
127         ssize_t n;
128         time_t time;
129         int64_t fssize;
130         int fd, c, i, j, k, size;
131
132         if ((fd = open(name, O_RDONLY, 0)) < 0)
133                 goto err;
134         for (i = 0; sblock_try[i] != -1; i++) {
135                 if (lseek(fd, (off_t)sblock_try[i], SEEK_SET) == (off_t)-1)
136                         goto err;
137                 if ((n = read(fd, &afs, SBLOCKSIZE)) == -1)
138                         goto err;
139                 if ((afs.fs_magic == FS_UFS1_MAGIC ||
140                      (afs.fs_magic == FS_UFS2_MAGIC &&
141                       afs.fs_sblockloc == numfrags(&afs, sblock_try[i]))) &&
142                     afs.fs_bsize <= MAXBSIZE &&
143                     afs.fs_bsize >= sizeof(struct fs))
144                         break;
145         }
146         if (sblock_try[i] == -1) {
147                 fprintf(stderr, "Cannot find filesystem superblock\n");
148                 return (1);
149         }
150
151         dev_bsize = afs.fs_fsize / fsbtodb(&afs, 1);
152         if (afs.fs_magic == FS_UFS2_MAGIC) {
153                 fssize = afs.fs_size;
154                 time = afs.fs_time;
155                 printf("magic\t%x (UFS2)\ttime\t%s",
156                     afs.fs_magic, ctime(&time));
157                 printf("offset\t%qd\tid\t[ %x %x ]\n",
158                     afs.fs_sblockloc, afs.fs_id[0], afs.fs_id[1]);
159                 printf("ncg\t%d\tsize\t%qd\tblocks\t%d\n",
160                     afs.fs_ncg, fssize, afs.fs_dsize);
161         } else {
162                 fssize = afs.fs_old_size;
163                 printf("magic\t%x (UFS1)\ttime\t%s",
164                     afs.fs_magic, ctime(&afs.fs_old_time));
165                 printf("id\t[ %x %x ]\n", afs.fs_id[0], afs.fs_id[1]);
166                 printf("ncg\t%d\tsize\t%qd\tblocks\t%d\n",
167                     afs.fs_ncg, fssize, afs.fs_dsize);
168         }
169         printf("bsize\t%d\tshift\t%d\tmask\t0x%08x\n",
170             afs.fs_bsize, afs.fs_bshift, afs.fs_bmask);
171         printf("fsize\t%d\tshift\t%d\tmask\t0x%08x\n",
172             afs.fs_fsize, afs.fs_fshift, afs.fs_fmask);
173         printf("frag\t%d\tshift\t%d\tfsbtodb\t%d\n",
174             afs.fs_frag, afs.fs_fragshift, afs.fs_fsbtodb);
175         printf("minfree\t%d%%\toptim\t%s\tsymlinklen %d\n",
176             afs.fs_minfree, afs.fs_optim == FS_OPTSPACE ? "space" : "time",
177             afs.fs_maxsymlinklen);
178         if (afs.fs_magic == FS_UFS2_MAGIC) {
179                 printf("%s %d\tmaxbpg\t%d\tmaxcontig %d\tcontigsumsize %d\n",
180                     "maxbsize", afs.fs_maxbsize, afs.fs_maxbpg,
181                     afs.fs_maxcontig, afs.fs_contigsumsize);
182                 printf("nbfree\t%qd\tndir\t%qd\tnifree\t%qd\tnffree\t%qd\n",
183                     afs.fs_cstotal.cs_nbfree, afs.fs_cstotal.cs_ndir,
184                     afs.fs_cstotal.cs_nifree, afs.fs_cstotal.cs_nffree);
185                 printf("bpg\t%d\tfpg\t%d\tipg\t%d\n",
186                     afs.fs_fpg / afs.fs_frag, afs.fs_fpg, afs.fs_ipg);
187                 printf("nindir\t%d\tinopb\t%d\tmaxfilesize\t%qu\n",
188                     afs.fs_nindir, afs.fs_inopb, afs.fs_maxfilesize);
189                 printf("sbsize\t%d\tcgsize\t%d\tcsaddr\t%d\tcssize\t%d\n",
190                     afs.fs_sbsize, afs.fs_cgsize, afs.fs_csaddr, afs.fs_cssize);
191         } else {
192                 printf("maxbpg\t%d\tmaxcontig %d\tcontigsumsize %d\n",
193                     afs.fs_maxbpg, afs.fs_maxcontig, afs.fs_contigsumsize);
194                 printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
195                     afs.fs_old_cstotal.cs_nbfree, afs.fs_old_cstotal.cs_ndir,
196                     afs.fs_old_cstotal.cs_nifree, afs.fs_old_cstotal.cs_nffree);
197                 printf("cpg\t%d\tbpg\t%d\tfpg\t%d\tipg\t%d\n",
198                     afs.fs_old_cpg, afs.fs_fpg / afs.fs_frag, afs.fs_fpg,
199                     afs.fs_ipg);
200                 printf("nindir\t%d\tinopb\t%d\tnspf\t%d\tmaxfilesize\t%qu\n",
201                     afs.fs_nindir, afs.fs_inopb, afs.fs_old_nspf,
202                     afs.fs_maxfilesize);
203                 printf("sbsize\t%d\tcgsize\t%d\tcgoffset %d\tcgmask\t0x%08x\n",
204                     afs.fs_sbsize, afs.fs_cgsize, afs.fs_old_cgoffset,
205                     afs.fs_old_cgmask);
206                 printf("csaddr\t%d\tcssize\t%d\n",
207                     afs.fs_old_csaddr, afs.fs_cssize);
208                 printf("rotdelay %dms\trps\t%d\ttrackskew %d\tinterleave %d\n",
209                     afs.fs_old_rotdelay, afs.fs_old_rps, afs.fs_old_trackskew,
210                     afs.fs_old_interleave);
211                 printf("nsect\t%d\tnpsect\t%d\tspc\t%d\n",
212                     afs.fs_old_nsect, afs.fs_old_npsect, afs.fs_old_spc);
213         }
214         printf("sblkno\t%d\tcblkno\t%d\tiblkno\t%d\tdblkno\t%d\n",
215             afs.fs_sblkno, afs.fs_cblkno, afs.fs_iblkno, afs.fs_dblkno);
216         printf("cgrotor\t%d\tfmod\t%d\tronly\t%d\tclean\t%d\n",
217             afs.fs_cgrotor, afs.fs_fmod, afs.fs_ronly, afs.fs_clean);
218         printf("flags\t");
219         if (afs.fs_flags == 0)
220                 printf("none");
221         if (afs.fs_flags & FS_UNCLEAN)
222                         printf("unclean ");
223         if (afs.fs_flags & FS_DOSOFTDEP)
224                         printf("soft-updates ");
225         if (afs.fs_flags & FS_NEEDSFSCK)
226                         printf("needs fsck run ");
227         if (afs.fs_flags & FS_INDEXDIRS)
228                         printf("indexed directories ");
229         if ((afs.fs_flags &
230             ~(FS_UNCLEAN | FS_DOSOFTDEP | FS_NEEDSFSCK | FS_INDEXDIRS)) != 0)
231                         printf("unknown flags (%#x)", afs.fs_flags &
232                             ~(FS_UNCLEAN | FS_DOSOFTDEP |
233                               FS_NEEDSFSCK | FS_INDEXDIRS));
234         putchar('\n');
235         printf("\ncs[].cs_(nbfree,ndir,nifree,nffree):\n\t");
236         afs.fs_csp = calloc(1, afs.fs_cssize);
237         if (lseek(fd,
238             (off_t)(fsbtodb(&afs, afs.fs_csaddr)) * (off_t)dev_bsize,
239             SEEK_SET) == (off_t)-1)
240                 goto err;
241         if (read(fd, (char *)afs.fs_csp, afs.fs_cssize) != afs.fs_cssize)
242                 goto err;
243         for (i = 0; i < afs.fs_ncg; i++) {
244                 struct csum *cs = &afs.fs_cs(&afs, i);
245                 if (i && i % 4 == 0)
246                         printf("\n\t");
247                 printf("(%d,%d,%d,%d) ",
248                     cs->cs_nbfree, cs->cs_ndir, cs->cs_nifree, cs->cs_nffree);
249         }
250         printf("\n");
251         if (fssize % afs.fs_fpg) {
252                 if (afs.fs_magic == FS_UFS1_MAGIC)
253                         printf("cylinders in last group %d\n",
254                             howmany(afs.fs_old_size % afs.fs_fpg,
255                             afs.fs_old_spc / afs.fs_old_nspf));
256                 printf("blocks in last group %d\n\n",
257                     (fssize % afs.fs_fpg) / afs.fs_frag);
258         }
259         for (i = 0; i < afs.fs_ncg; i++)
260                 if (dumpcg(name, fd, i))
261                         goto err;
262         (void)close(fd);
263         return (0);
264
265 err:    if (fd != -1)
266                 (void)close(fd);
267         warn("%s", name);
268         return (1);
269 };
270
271 int
272 dumpcg(const char *name, int fd, int c)
273 {
274         time_t time;
275         off_t cur;
276         int i, j;
277
278         printf("\ncg %d:\n", c);
279         if ((cur = lseek(fd, (off_t)(fsbtodb(&afs, cgtod(&afs, c))) *
280             (off_t)dev_bsize, SEEK_SET)) == (off_t)-1)
281                 return (1);
282         if (read(fd, &acg, afs.fs_bsize) != afs.fs_bsize) {
283                 warnx("%s: error reading cg", name);
284                 return (1);
285         }
286         if (afs.fs_magic == FS_UFS2_MAGIC) {
287                 time = acg.cg_time;
288                 printf("magic\t%x\ttell\t%qx\ttime\t%s",
289                     acg.cg_magic, cur, ctime(&time));
290                 printf("cgx\t%d\tndblk\t%d\tniblk\t%d\tinitiblk %d\n",
291                     acg.cg_cgx, acg.cg_ndblk, acg.cg_niblk, acg.cg_initediblk);
292         } else {
293                 printf("magic\t%x\ttell\t%qx\ttime\t%s",
294                     acg.cg_magic, cur, ctime(&acg.cg_old_time));
295                 printf("cgx\t%d\tncyl\t%d\tniblk\t%d\tndblk\t%d\n",
296                     acg.cg_cgx, acg.cg_old_ncyl, acg.cg_old_niblk,
297                     acg.cg_ndblk);
298         }
299         printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
300             acg.cg_cs.cs_nbfree, acg.cg_cs.cs_ndir,
301             acg.cg_cs.cs_nifree, acg.cg_cs.cs_nffree);
302         printf("rotor\t%d\tirotor\t%d\tfrotor\t%d\nfrsum",
303             acg.cg_rotor, acg.cg_irotor, acg.cg_frotor);
304         for (i = 1, j = 0; i < afs.fs_frag; i++) {
305                 printf("\t%d", acg.cg_frsum[i]);
306                 j += i * acg.cg_frsum[i];
307         }
308         printf("\nsum of frsum: %d", j);
309         if (afs.fs_contigsumsize > 0) {
310                 for (i = 1; i < afs.fs_contigsumsize; i++) {
311                         if ((i - 1) % 8 == 0)
312                                 printf("\nclusters %d-%d:", i,
313                                     afs.fs_contigsumsize - 1 < i + 7 ?
314                                     afs.fs_contigsumsize - 1 : i + 7);
315                         printf("\t%d", cg_clustersum(&acg)[i]);
316                 }
317                 printf("\nclusters size %d and over: %d\n",
318                     afs.fs_contigsumsize,
319                     cg_clustersum(&acg)[afs.fs_contigsumsize]);
320                 printf("clusters free:\t");
321                 pbits(cg_clustersfree(&acg), acg.cg_nclusterblks);
322         } else
323                 printf("\n");
324         printf("inodes used:\t");
325         pbits(cg_inosused(&acg), afs.fs_ipg);
326         printf("blks free:\t");
327         pbits(cg_blksfree(&acg), afs.fs_fpg);
328         return (0);
329 };
330
331 void
332 pbits(void *vp, int max)
333 {
334         int i;
335         char *p;
336         int count, j;
337
338         for (count = i = 0, p = vp; i < max; i++)
339                 if (isset(p, i)) {
340                         if (count)
341                                 printf(",%s", count % 6 ? " " : "\n\t");
342                         count++;
343                         printf("%d", i);
344                         j = i;
345                         while ((i+1)<max && isset(p, i+1))
346                                 i++;
347                         if (i != j)
348                                 printf("-%d", i);
349                 }
350         printf("\n");
351 }
352
353 void
354 usage(void)
355 {
356         (void)fprintf(stderr, "usage: dumpfs filesys | device\n");
357         exit(1);
358 }