]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/dumpfs/dumpfs.c
Implement SIOCGIFMEDIA for vlan devices by passing the request to the
[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 <errno.h>
69 #include <fcntl.h>
70 #include <fstab.h>
71 #include <libufs.h>
72 #include <stdint.h>
73 #include <stdio.h>
74 #include <stdlib.h>
75 #include <unistd.h>
76
77 #define afs     disk.d_fs
78 #define acg     disk.d_cg
79
80 struct uufsd disk;
81
82 int     dumpfs(const char *);
83 int     dumpcg(void);
84 int     marshal(const char *);
85 void    pbits(void *, int);
86 void    ufserr(const char *);
87 void    usage(void) __dead2;
88
89 int
90 main(int argc, char *argv[])
91 {
92         const char *name;
93         int ch, domarshal, eval;
94
95         domarshal = eval = 0;
96
97         while ((ch = getopt(argc, argv, "m")) != -1) {
98                 switch (ch) {
99                 case 'm':
100                         domarshal = 1;
101                         break;
102                 case '?':
103                 default:
104                         usage();
105                 }
106         }
107         argc -= optind;
108         argv += optind;
109
110         if (argc < 1)
111                 usage();
112
113         while ((name = *argv++) != NULL) {
114                 if (ufs_disk_fillout(&disk, name) == -1) {
115                         ufserr(name);
116                         eval |= 1;
117                         continue;
118                 }
119                 if (domarshal)
120                         eval |= marshal(name);
121                 else
122                         eval |= dumpfs(name);
123                 ufs_disk_close(&disk);
124         }
125         exit(eval);
126 }
127
128 int
129 dumpfs(const char *name)
130 {
131         time_t fstime;
132         int64_t fssize;
133         int i;
134
135         switch (disk.d_ufs) {
136         case 2:
137                 fssize = afs.fs_size;
138                 fstime = afs.fs_time;
139                 printf("magic\t%x (UFS2)\ttime\t%s",
140                     afs.fs_magic, ctime(&fstime));
141                 printf("superblock location\t%qd\tid\t[ %x %x ]\n",
142                     afs.fs_sblockloc, afs.fs_id[0], afs.fs_id[1]);
143                 printf("ncg\t%d\tsize\t%qd\tblocks\t%jd\n",
144                     afs.fs_ncg, fssize, (intmax_t)afs.fs_dsize);
145                 break;
146         case 1:
147                 fssize = afs.fs_old_size;
148                 fstime = afs.fs_old_time;
149                 printf("magic\t%x (UFS1)\ttime\t%s",
150                     afs.fs_magic, ctime(&fstime));
151                 printf("id\t[ %x %x ]\n", afs.fs_id[0], afs.fs_id[1]);
152                 printf("ncg\t%d\tsize\t%qd\tblocks\t%jd\n",
153                     afs.fs_ncg, fssize, (intmax_t)afs.fs_dsize);
154                 break;
155         default:
156                 goto err;
157         }
158         printf("bsize\t%d\tshift\t%d\tmask\t0x%08x\n",
159             afs.fs_bsize, afs.fs_bshift, afs.fs_bmask);
160         printf("fsize\t%d\tshift\t%d\tmask\t0x%08x\n",
161             afs.fs_fsize, afs.fs_fshift, afs.fs_fmask);
162         printf("frag\t%d\tshift\t%d\tfsbtodb\t%d\n",
163             afs.fs_frag, afs.fs_fragshift, afs.fs_fsbtodb);
164         printf("minfree\t%d%%\toptim\t%s\tsymlinklen %d\n",
165             afs.fs_minfree, afs.fs_optim == FS_OPTSPACE ? "space" : "time",
166             afs.fs_maxsymlinklen);
167         switch (disk.d_ufs) {
168         case 2:
169                 printf("%s %d\tmaxbpg\t%d\tmaxcontig %d\tcontigsumsize %d\n",
170                     "maxbsize", afs.fs_maxbsize, afs.fs_maxbpg,
171                     afs.fs_maxcontig, afs.fs_contigsumsize);
172                 printf("nbfree\t%qd\tndir\t%qd\tnifree\t%qd\tnffree\t%qd\n",
173                     afs.fs_cstotal.cs_nbfree, afs.fs_cstotal.cs_ndir,
174                     afs.fs_cstotal.cs_nifree, afs.fs_cstotal.cs_nffree);
175                 printf("bpg\t%d\tfpg\t%d\tipg\t%d\n",
176                     afs.fs_fpg / afs.fs_frag, afs.fs_fpg, afs.fs_ipg);
177                 printf("nindir\t%d\tinopb\t%d\tmaxfilesize\t%qu\n",
178                     afs.fs_nindir, afs.fs_inopb, afs.fs_maxfilesize);
179                 printf("sbsize\t%d\tcgsize\t%d\tcsaddr\t%jd\tcssize\t%d\n",
180                     afs.fs_sbsize, afs.fs_cgsize, (intmax_t)afs.fs_csaddr,
181                     afs.fs_cssize);
182                 break;
183         case 1:
184                 printf("maxbpg\t%d\tmaxcontig %d\tcontigsumsize %d\n",
185                     afs.fs_maxbpg, afs.fs_maxcontig, afs.fs_contigsumsize);
186                 printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
187                     afs.fs_old_cstotal.cs_nbfree, afs.fs_old_cstotal.cs_ndir,
188                     afs.fs_old_cstotal.cs_nifree, afs.fs_old_cstotal.cs_nffree);
189                 printf("cpg\t%d\tbpg\t%d\tfpg\t%d\tipg\t%d\n",
190                     afs.fs_old_cpg, afs.fs_fpg / afs.fs_frag, afs.fs_fpg,
191                     afs.fs_ipg);
192                 printf("nindir\t%d\tinopb\t%d\tnspf\t%d\tmaxfilesize\t%qu\n",
193                     afs.fs_nindir, afs.fs_inopb, afs.fs_old_nspf,
194                     afs.fs_maxfilesize);
195                 printf("sbsize\t%d\tcgsize\t%d\tcgoffset %d\tcgmask\t0x%08x\n",
196                     afs.fs_sbsize, afs.fs_cgsize, afs.fs_old_cgoffset,
197                     afs.fs_old_cgmask);
198                 printf("csaddr\t%d\tcssize\t%d\n",
199                     afs.fs_old_csaddr, afs.fs_cssize);
200                 printf("rotdelay %dms\trps\t%d\ttrackskew %d\tinterleave %d\n",
201                     afs.fs_old_rotdelay, afs.fs_old_rps, afs.fs_old_trackskew,
202                     afs.fs_old_interleave);
203                 printf("nsect\t%d\tnpsect\t%d\tspc\t%d\n",
204                     afs.fs_old_nsect, afs.fs_old_npsect, afs.fs_old_spc);
205                 break;
206         default:
207                 goto err;
208         }
209         printf("sblkno\t%d\tcblkno\t%d\tiblkno\t%d\tdblkno\t%d\n",
210             afs.fs_sblkno, afs.fs_cblkno, afs.fs_iblkno, afs.fs_dblkno);
211         printf("cgrotor\t%d\tfmod\t%d\tronly\t%d\tclean\t%d\n",
212             afs.fs_cgrotor, afs.fs_fmod, afs.fs_ronly, afs.fs_clean);
213         printf("flags\t");
214         if (afs.fs_flags == 0)
215                 printf("none");
216         if (afs.fs_flags & FS_UNCLEAN)
217                         printf("unclean ");
218         if (afs.fs_flags & FS_DOSOFTDEP)
219                         printf("soft-updates ");
220         if (afs.fs_flags & FS_NEEDSFSCK)
221                         printf("needs fsck run ");
222         if (afs.fs_flags & FS_INDEXDIRS)
223                         printf("indexed directories ");
224         if ((afs.fs_flags &
225             ~(FS_UNCLEAN | FS_DOSOFTDEP | FS_NEEDSFSCK | FS_INDEXDIRS)) != 0)
226                         printf("unknown flags (%#x)", afs.fs_flags &
227                             ~(FS_UNCLEAN | FS_DOSOFTDEP |
228                               FS_NEEDSFSCK | FS_INDEXDIRS));
229         putchar('\n');
230         printf("\ncs[].cs_(nbfree,ndir,nifree,nffree):\n\t");
231         afs.fs_csp = calloc(1, afs.fs_cssize);
232         if (bread(&disk, fsbtodb(&afs, afs.fs_csaddr), afs.fs_csp, afs.fs_cssize) == -1)
233                 goto err;
234         for (i = 0; i < afs.fs_ncg; i++) {
235                 struct csum *cs = &afs.fs_cs(&afs, i);
236                 if (i && i % 4 == 0)
237                         printf("\n\t");
238                 printf("(%d,%d,%d,%d) ",
239                     cs->cs_nbfree, cs->cs_ndir, cs->cs_nifree, cs->cs_nffree);
240         }
241         printf("\n");
242         if (fssize % afs.fs_fpg) {
243                 if (disk.d_ufs == 1)
244                         printf("cylinders in last group %d\n",
245                             howmany(afs.fs_old_size % afs.fs_fpg,
246                             afs.fs_old_spc / afs.fs_old_nspf));
247                 printf("blocks in last group %ld\n\n",
248                     (long)((fssize % afs.fs_fpg) / afs.fs_frag));
249         }
250         while ((i = cgread(&disk)) != 0) {
251                 if (i == -1 || dumpcg())
252                         goto err;
253         }
254         return (0);
255
256 err:    ufserr(name);
257         return (1);
258 }
259
260 int
261 dumpcg(void)
262 {
263         time_t cgtime;
264         off_t cur;
265         int i, j;
266
267         printf("\ncg %d:\n", disk.d_lcg);
268         cur = fsbtodb(&afs, cgtod(&afs, disk.d_lcg)) * disk.d_bsize;
269         switch (disk.d_ufs) {
270         case 2:
271                 cgtime = acg.cg_time;
272                 printf("magic\t%x\ttell\t%qx\ttime\t%s",
273                     acg.cg_magic, cur, ctime(&cgtime));
274                 printf("cgx\t%d\tndblk\t%d\tniblk\t%d\tinitiblk %d\n",
275                     acg.cg_cgx, acg.cg_ndblk, acg.cg_niblk, acg.cg_initediblk);
276                 break;
277         case 1:
278                 cgtime = acg.cg_old_time;
279                 printf("magic\t%x\ttell\t%qx\ttime\t%s",
280                     acg.cg_magic, cur, ctime(&cgtime));
281                 printf("cgx\t%d\tncyl\t%d\tniblk\t%d\tndblk\t%d\n",
282                     acg.cg_cgx, acg.cg_old_ncyl, acg.cg_old_niblk,
283                     acg.cg_ndblk);
284                 break;
285         default:
286                 break;
287         }
288         printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
289             acg.cg_cs.cs_nbfree, acg.cg_cs.cs_ndir,
290             acg.cg_cs.cs_nifree, acg.cg_cs.cs_nffree);
291         printf("rotor\t%d\tirotor\t%d\tfrotor\t%d\nfrsum",
292             acg.cg_rotor, acg.cg_irotor, acg.cg_frotor);
293         for (i = 1, j = 0; i < afs.fs_frag; i++) {
294                 printf("\t%d", acg.cg_frsum[i]);
295                 j += i * acg.cg_frsum[i];
296         }
297         printf("\nsum of frsum: %d", j);
298         if (afs.fs_contigsumsize > 0) {
299                 for (i = 1; i < afs.fs_contigsumsize; i++) {
300                         if ((i - 1) % 8 == 0)
301                                 printf("\nclusters %d-%d:", i,
302                                     afs.fs_contigsumsize - 1 < i + 7 ?
303                                     afs.fs_contigsumsize - 1 : i + 7);
304                         printf("\t%d", cg_clustersum(&acg)[i]);
305                 }
306                 printf("\nclusters size %d and over: %d\n",
307                     afs.fs_contigsumsize,
308                     cg_clustersum(&acg)[afs.fs_contigsumsize]);
309                 printf("clusters free:\t");
310                 pbits(cg_clustersfree(&acg), acg.cg_nclusterblks);
311         } else
312                 printf("\n");
313         printf("inodes used:\t");
314         pbits(cg_inosused(&acg), afs.fs_ipg);
315         printf("blks free:\t");
316         pbits(cg_blksfree(&acg), afs.fs_fpg);
317         return (0);
318 }
319
320 int
321 marshal(const char *name)
322 {
323         struct fs *fs;
324
325         fs = &disk.d_fs;
326
327         printf("# newfs command for %s (%s)\n", name, disk.d_name);
328         printf("newfs ");
329         printf("-O %d ", disk.d_ufs);
330         if (fs->fs_flags & FS_DOSOFTDEP)
331                 printf("-U ");
332         printf("-a %d ", fs->fs_maxcontig);
333         printf("-b %d ", fs->fs_bsize);
334         /* -c is dumb */
335         printf("-d %d ", fs->fs_maxbsize);
336         printf("-e %d ", fs->fs_maxbpg);
337         printf("-f %d ", fs->fs_fsize);
338         printf("-g %d ", fs->fs_avgfilesize);
339         printf("-h %d ", fs->fs_avgfpdir);
340         /* -i is dumb */
341         /* -j..l unimplemented */
342         printf("-m %d ", fs->fs_minfree);
343         /* -n unimplemented */
344         printf("-o ");
345         switch (fs->fs_optim) {
346         case FS_OPTSPACE:
347                 printf("space ");
348                 break;
349         case FS_OPTTIME:
350                 printf("time ");
351                 break;
352         default:
353                 printf("unknown ");
354                 break;
355         }
356         /* -p..r unimplemented */
357         printf("-s %jd ", (intmax_t)fs->fs_size);
358         printf("%s ", disk.d_name);
359         printf("\n");
360
361         return 0;
362 }
363
364 void
365 pbits(void *vp, int max)
366 {
367         int i;
368         char *p;
369         int count, j;
370
371         for (count = i = 0, p = vp; i < max; i++)
372                 if (isset(p, i)) {
373                         if (count)
374                                 printf(",%s", count % 6 ? " " : "\n\t");
375                         count++;
376                         printf("%d", i);
377                         j = i;
378                         while ((i+1)<max && isset(p, i+1))
379                                 i++;
380                         if (i != j)
381                                 printf("-%d", i);
382                 }
383         printf("\n");
384 }
385
386 void
387 ufserr(const char *name)
388 {
389         if (disk.d_error != NULL)
390                 warnx("%s: %s", name, disk.d_error);
391         else if (errno)
392                 warn("%s", name);
393 }
394
395 void
396 usage(void)
397 {
398         (void)fprintf(stderr, "usage: dumpfs [-m] filesys | device\n");
399         exit(1);
400 }