]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sbin/dumpfs/dumpfs.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sbin / dumpfs / dumpfs.c
1 /*
2  * Copyright (c) 2009 Robert N. M. Watson
3  * All rights reserved.
4  *
5  * This software was developed at the University of Cambridge Computer
6  * Laboratory with support from a grant from Google, Inc.
7  *
8  * Copyright (c) 2002 Networks Associates Technology, Inc.
9  * All rights reserved.
10  *
11  * This software was developed for the FreeBSD Project by Marshall
12  * Kirk McKusick and Network Associates Laboratories, the Security
13  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
14  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
15  * research program.
16  *
17  * Copyright (c) 1983, 1992, 1993
18  *      The Regents of the University of California.  All rights reserved.
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted provided that the following conditions
22  * are met:
23  * 1. Redistributions of source code must retain the above copyright
24  *    notice, this list of conditions and the following disclaimer.
25  * 2. Redistributions in binary form must reproduce the above copyright
26  *    notice, this list of conditions and the following disclaimer in the
27  *    documentation and/or other materials provided with the distribution.
28  * 4. Neither the name of the University nor the names of its contributors
29  *    may be used to endorse or promote products derived from this software
30  *    without specific prior written permission.
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42  * SUCH DAMAGE.
43  */
44
45 #ifndef lint
46 static const char copyright[] =
47 "@(#) Copyright (c) 1983, 1992, 1993\n\
48         The Regents of the University of California.  All rights reserved.\n";
49 #endif /* not lint */
50
51 #ifndef lint
52 #if 0
53 static char sccsid[] = "@(#)dumpfs.c    8.5 (Berkeley) 4/29/95";
54 #endif
55 static const char rcsid[] =
56   "$FreeBSD$";
57 #endif /* not lint */
58
59 #include <sys/param.h>
60 #include <sys/time.h>
61 #include <sys/disklabel.h>
62
63 #include <ufs/ufs/dinode.h>
64 #include <ufs/ffs/fs.h>
65
66 #include <err.h>
67 #include <errno.h>
68 #include <fcntl.h>
69 #include <fstab.h>
70 #include <libufs.h>
71 #include <stdint.h>
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <unistd.h>
75
76 #define afs     disk.d_fs
77 #define acg     disk.d_cg
78
79 struct uufsd disk;
80
81 int     dumpfs(const char *);
82 int     dumpcg(void);
83 int     dumpfreespace(const char *, int);
84 void    dumpfreespacecg(int);
85 int     marshal(const char *);
86 void    pbits(void *, int);
87 void    pblklist(void *, int, off_t, int);
88 void    ufserr(const char *);
89 void    usage(void) __dead2;
90
91 int
92 main(int argc, char *argv[])
93 {
94         const char *name;
95         int ch, dofreespace, domarshal, eval;
96
97         dofreespace = domarshal = eval = 0;
98
99         while ((ch = getopt(argc, argv, "fm")) != -1) {
100                 switch (ch) {
101                 case 'f':
102                         dofreespace++;
103                         break;
104                 case 'm':
105                         domarshal = 1;
106                         break;
107                 case '?':
108                 default:
109                         usage();
110                 }
111         }
112         argc -= optind;
113         argv += optind;
114
115         if (argc < 1)
116                 usage();
117         if (dofreespace && domarshal)
118                 usage();
119         if (dofreespace > 2)
120                 usage();
121
122         while ((name = *argv++) != NULL) {
123                 if (ufs_disk_fillout(&disk, name) == -1) {
124                         ufserr(name);
125                         eval |= 1;
126                         continue;
127                 }
128                 if (dofreespace)
129                         eval |= dumpfreespace(name, dofreespace);
130                 else if (domarshal)
131                         eval |= marshal(name);
132                 else
133                         eval |= dumpfs(name);
134                 ufs_disk_close(&disk);
135         }
136         exit(eval);
137 }
138
139 int
140 dumpfs(const char *name)
141 {
142         time_t fstime;
143         int64_t fssize;
144         int32_t fsflags;
145         int i;
146
147         switch (disk.d_ufs) {
148         case 2:
149                 fssize = afs.fs_size;
150                 fstime = afs.fs_time;
151                 printf("magic\t%x (UFS2)\ttime\t%s",
152                     afs.fs_magic, ctime(&fstime));
153                 printf("superblock location\t%jd\tid\t[ %x %x ]\n",
154                     (intmax_t)afs.fs_sblockloc, afs.fs_id[0], afs.fs_id[1]);
155                 printf("ncg\t%d\tsize\t%jd\tblocks\t%jd\n",
156                     afs.fs_ncg, (intmax_t)fssize, (intmax_t)afs.fs_dsize);
157                 break;
158         case 1:
159                 fssize = afs.fs_old_size;
160                 fstime = afs.fs_old_time;
161                 printf("magic\t%x (UFS1)\ttime\t%s",
162                     afs.fs_magic, ctime(&fstime));
163                 printf("id\t[ %08x %08x ]\n", afs.fs_id[0], afs.fs_id[1]);
164                 printf("ncg\t%d\tsize\t%jd\tblocks\t%jd\n",
165                     afs.fs_ncg, (intmax_t)fssize, (intmax_t)afs.fs_dsize);
166                 break;
167         default:
168                 goto err;
169         }
170         printf("bsize\t%d\tshift\t%d\tmask\t0x%08x\n",
171             afs.fs_bsize, afs.fs_bshift, afs.fs_bmask);
172         printf("fsize\t%d\tshift\t%d\tmask\t0x%08x\n",
173             afs.fs_fsize, afs.fs_fshift, afs.fs_fmask);
174         printf("frag\t%d\tshift\t%d\tfsbtodb\t%d\n",
175             afs.fs_frag, afs.fs_fragshift, afs.fs_fsbtodb);
176         printf("minfree\t%d%%\toptim\t%s\tsymlinklen %d\n",
177             afs.fs_minfree, afs.fs_optim == FS_OPTSPACE ? "space" : "time",
178             afs.fs_maxsymlinklen);
179         switch (disk.d_ufs) {
180         case 2:
181                 printf("%s %d\tmaxbpg\t%d\tmaxcontig %d\tcontigsumsize %d\n",
182                     "maxbsize", afs.fs_maxbsize, afs.fs_maxbpg,
183                     afs.fs_maxcontig, afs.fs_contigsumsize);
184                 printf("nbfree\t%jd\tndir\t%jd\tnifree\t%jd\tnffree\t%jd\n",
185                     (intmax_t)afs.fs_cstotal.cs_nbfree, 
186                     (intmax_t)afs.fs_cstotal.cs_ndir,
187                     (intmax_t)afs.fs_cstotal.cs_nifree, 
188                     (intmax_t)afs.fs_cstotal.cs_nffree);
189                 printf("bpg\t%d\tfpg\t%d\tipg\t%d\tunrefs\t%jd\n",
190                     afs.fs_fpg / afs.fs_frag, afs.fs_fpg, afs.fs_ipg,
191                     (intmax_t)afs.fs_unrefs);
192                 printf("nindir\t%d\tinopb\t%d\tmaxfilesize\t%ju\n",
193                     afs.fs_nindir, afs.fs_inopb, 
194                     (uintmax_t)afs.fs_maxfilesize);
195                 printf("sbsize\t%d\tcgsize\t%d\tcsaddr\t%jd\tcssize\t%d\n",
196                     afs.fs_sbsize, afs.fs_cgsize, (intmax_t)afs.fs_csaddr,
197                     afs.fs_cssize);
198                 break;
199         case 1:
200                 printf("maxbpg\t%d\tmaxcontig %d\tcontigsumsize %d\n",
201                     afs.fs_maxbpg, afs.fs_maxcontig, afs.fs_contigsumsize);
202                 printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
203                     afs.fs_old_cstotal.cs_nbfree, afs.fs_old_cstotal.cs_ndir,
204                     afs.fs_old_cstotal.cs_nifree, afs.fs_old_cstotal.cs_nffree);
205                 printf("cpg\t%d\tbpg\t%d\tfpg\t%d\tipg\t%d\n",
206                     afs.fs_old_cpg, afs.fs_fpg / afs.fs_frag, afs.fs_fpg,
207                     afs.fs_ipg);
208                 printf("nindir\t%d\tinopb\t%d\tnspf\t%d\tmaxfilesize\t%ju\n",
209                     afs.fs_nindir, afs.fs_inopb, afs.fs_old_nspf,
210                     (uintmax_t)afs.fs_maxfilesize);
211                 printf("sbsize\t%d\tcgsize\t%d\tcgoffset %d\tcgmask\t0x%08x\n",
212                     afs.fs_sbsize, afs.fs_cgsize, afs.fs_old_cgoffset,
213                     afs.fs_old_cgmask);
214                 printf("csaddr\t%d\tcssize\t%d\n",
215                     afs.fs_old_csaddr, afs.fs_cssize);
216                 printf("rotdelay %dms\trps\t%d\ttrackskew %d\tinterleave %d\n",
217                     afs.fs_old_rotdelay, afs.fs_old_rps, afs.fs_old_trackskew,
218                     afs.fs_old_interleave);
219                 printf("nsect\t%d\tnpsect\t%d\tspc\t%d\n",
220                     afs.fs_old_nsect, afs.fs_old_npsect, afs.fs_old_spc);
221                 break;
222         default:
223                 goto err;
224         }
225         printf("sblkno\t%d\tcblkno\t%d\tiblkno\t%d\tdblkno\t%d\n",
226             afs.fs_sblkno, afs.fs_cblkno, afs.fs_iblkno, afs.fs_dblkno);
227         printf("cgrotor\t%d\tfmod\t%d\tronly\t%d\tclean\t%d\n",
228             afs.fs_cgrotor, afs.fs_fmod, afs.fs_ronly, afs.fs_clean);
229         printf("avgfpdir %d\tavgfilesize %d\n",
230             afs.fs_avgfpdir, afs.fs_avgfilesize);
231         printf("flags\t");
232         if (afs.fs_old_flags & FS_FLAGS_UPDATED)
233                 fsflags = afs.fs_flags;
234         else
235                 fsflags = afs.fs_old_flags;
236         if (fsflags == 0)
237                 printf("none");
238         if (fsflags & FS_UNCLEAN)
239                 printf("unclean ");
240         if (fsflags & FS_DOSOFTDEP)
241                 printf("soft-updates ");
242         if (fsflags & FS_NEEDSFSCK)
243                 printf("needs fsck run ");
244         if (fsflags & FS_INDEXDIRS)
245                 printf("indexed directories ");
246         if (fsflags & FS_ACLS)
247                 printf("acls ");
248         if (fsflags & FS_MULTILABEL)
249                 printf("multilabel ");
250         if (fsflags & FS_GJOURNAL)
251                 printf("gjournal ");
252         if (fsflags & FS_FLAGS_UPDATED)
253                 printf("fs_flags expanded ");
254         if (fsflags & FS_NFS4ACLS)
255                 printf("nfsv4acls ");
256         fsflags &= ~(FS_UNCLEAN | FS_DOSOFTDEP | FS_NEEDSFSCK | FS_INDEXDIRS |
257                      FS_ACLS | FS_MULTILABEL | FS_GJOURNAL | FS_FLAGS_UPDATED |
258                      FS_NFS4ACLS);
259         if (fsflags != 0)
260                 printf("unknown flags (%#x)", fsflags);
261         putchar('\n');
262         printf("fsmnt\t%s\n", afs.fs_fsmnt);
263         printf("volname\t%s\tswuid\t%ju\n",
264                 afs.fs_volname, (uintmax_t)afs.fs_swuid);
265         printf("\ncs[].cs_(nbfree,ndir,nifree,nffree):\n\t");
266         afs.fs_csp = calloc(1, afs.fs_cssize);
267         if (bread(&disk, fsbtodb(&afs, afs.fs_csaddr), afs.fs_csp, afs.fs_cssize) == -1)
268                 goto err;
269         for (i = 0; i < afs.fs_ncg; i++) {
270                 struct csum *cs = &afs.fs_cs(&afs, i);
271                 if (i && i % 4 == 0)
272                         printf("\n\t");
273                 printf("(%d,%d,%d,%d) ",
274                     cs->cs_nbfree, cs->cs_ndir, cs->cs_nifree, cs->cs_nffree);
275         }
276         printf("\n");
277         if (fssize % afs.fs_fpg) {
278                 if (disk.d_ufs == 1)
279                         printf("cylinders in last group %d\n",
280                             howmany(afs.fs_old_size % afs.fs_fpg,
281                             afs.fs_old_spc / afs.fs_old_nspf));
282                 printf("blocks in last group %ld\n\n",
283                     (long)((fssize % afs.fs_fpg) / afs.fs_frag));
284         }
285         while ((i = cgread(&disk)) != 0) {
286                 if (i == -1 || dumpcg())
287                         goto err;
288         }
289         return (0);
290
291 err:    ufserr(name);
292         return (1);
293 }
294
295 int
296 dumpcg(void)
297 {
298         time_t cgtime;
299         off_t cur;
300         int i, j;
301
302         printf("\ncg %d:\n", disk.d_lcg);
303         cur = fsbtodb(&afs, cgtod(&afs, disk.d_lcg)) * disk.d_bsize;
304         switch (disk.d_ufs) {
305         case 2:
306                 cgtime = acg.cg_time;
307                 printf("magic\t%x\ttell\t%jx\ttime\t%s",
308                     acg.cg_magic, (intmax_t)cur, ctime(&cgtime));
309                 printf("cgx\t%d\tndblk\t%d\tniblk\t%d\tinitiblk %d\tunrefs %d\n",
310                     acg.cg_cgx, acg.cg_ndblk, acg.cg_niblk, acg.cg_initediblk,
311                     acg.cg_unrefs);
312                 break;
313         case 1:
314                 cgtime = acg.cg_old_time;
315                 printf("magic\t%x\ttell\t%jx\ttime\t%s",
316                     acg.cg_magic, (intmax_t)cur, ctime(&cgtime));
317                 printf("cgx\t%d\tncyl\t%d\tniblk\t%d\tndblk\t%d\n",
318                     acg.cg_cgx, acg.cg_old_ncyl, acg.cg_old_niblk,
319                     acg.cg_ndblk);
320                 break;
321         default:
322                 break;
323         }
324         printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
325             acg.cg_cs.cs_nbfree, acg.cg_cs.cs_ndir,
326             acg.cg_cs.cs_nifree, acg.cg_cs.cs_nffree);
327         printf("rotor\t%d\tirotor\t%d\tfrotor\t%d\nfrsum",
328             acg.cg_rotor, acg.cg_irotor, acg.cg_frotor);
329         for (i = 1, j = 0; i < afs.fs_frag; i++) {
330                 printf("\t%d", acg.cg_frsum[i]);
331                 j += i * acg.cg_frsum[i];
332         }
333         printf("\nsum of frsum: %d", j);
334         if (afs.fs_contigsumsize > 0) {
335                 for (i = 1; i < afs.fs_contigsumsize; i++) {
336                         if ((i - 1) % 8 == 0)
337                                 printf("\nclusters %d-%d:", i,
338                                     afs.fs_contigsumsize - 1 < i + 7 ?
339                                     afs.fs_contigsumsize - 1 : i + 7);
340                         printf("\t%d", cg_clustersum(&acg)[i]);
341                 }
342                 printf("\nclusters size %d and over: %d\n",
343                     afs.fs_contigsumsize,
344                     cg_clustersum(&acg)[afs.fs_contigsumsize]);
345                 printf("clusters free:\t");
346                 pbits(cg_clustersfree(&acg), acg.cg_nclusterblks);
347         } else
348                 printf("\n");
349         printf("inodes used:\t");
350         pbits(cg_inosused(&acg), afs.fs_ipg);
351         printf("blks free:\t");
352         pbits(cg_blksfree(&acg), afs.fs_fpg);
353         return (0);
354 }
355
356 int
357 dumpfreespace(const char *name, int fflag)
358 {
359         int i;
360
361         while ((i = cgread(&disk)) != 0) {
362                 if (i == -1)
363                         goto err;
364                 dumpfreespacecg(fflag);
365         }
366         return (0);
367 err:
368         ufserr(name);
369         return (1);
370 }
371
372 void
373 dumpfreespacecg(int fflag)
374 {
375
376         pblklist(cg_blksfree(&acg), afs.fs_fpg, disk.d_lcg * afs.fs_fpg,
377             fflag);
378 }
379
380 int
381 marshal(const char *name)
382 {
383         struct fs *fs;
384
385         fs = &disk.d_fs;
386
387         printf("# newfs command for %s (%s)\n", name, disk.d_name);
388         printf("newfs ");
389         if (fs->fs_volname[0] != '\0')
390                 printf("-L %s ", fs->fs_volname);
391         printf("-O %d ", disk.d_ufs);
392         if (fs->fs_flags & FS_DOSOFTDEP)
393                 printf("-U ");
394         printf("-a %d ", fs->fs_maxcontig);
395         printf("-b %d ", fs->fs_bsize);
396         /* -c is dumb */
397         printf("-d %d ", fs->fs_maxbsize);
398         printf("-e %d ", fs->fs_maxbpg);
399         printf("-f %d ", fs->fs_fsize);
400         printf("-g %d ", fs->fs_avgfilesize);
401         printf("-h %d ", fs->fs_avgfpdir);
402         /* -i is dumb */
403         /* -j..l unimplemented */
404         printf("-m %d ", fs->fs_minfree);
405         /* -n unimplemented */
406         printf("-o ");
407         switch (fs->fs_optim) {
408         case FS_OPTSPACE:
409                 printf("space ");
410                 break;
411         case FS_OPTTIME:
412                 printf("time ");
413                 break;
414         default:
415                 printf("unknown ");
416                 break;
417         }
418         /* -p..r unimplemented */
419         printf("-s %jd ", (intmax_t)fsbtodb(fs, fs->fs_size));
420         printf("%s ", disk.d_name);
421         printf("\n");
422
423         return 0;
424 }
425
426 void
427 pbits(void *vp, int max)
428 {
429         int i;
430         char *p;
431         int count, j;
432
433         for (count = i = 0, p = vp; i < max; i++)
434                 if (isset(p, i)) {
435                         if (count)
436                                 printf(",%s", count % 6 ? " " : "\n\t");
437                         count++;
438                         printf("%d", i);
439                         j = i;
440                         while ((i+1)<max && isset(p, i+1))
441                                 i++;
442                         if (i != j)
443                                 printf("-%d", i);
444                 }
445         printf("\n");
446 }
447
448 void
449 pblklist(void *vp, int max, off_t offset, int fflag)
450 {
451         int i, j;
452         char *p;
453
454         for (i = 0, p = vp; i < max; i++) {
455                 if (isset(p, i)) {
456                         printf("%jd", (intmax_t)(i + offset));
457                         if (fflag < 2) {
458                                 j = i;
459                                 while ((i+1)<max && isset(p, i+1))
460                                         i++;
461                                 if (i != j)
462                                         printf("-%jd", (intmax_t)(i + offset));
463                         }
464                         printf("\n");
465                 }
466         }
467 }
468
469 void
470 ufserr(const char *name)
471 {
472         if (disk.d_error != NULL)
473                 warnx("%s: %s", name, disk.d_error);
474         else if (errno)
475                 warn("%s", name);
476 }
477
478 void
479 usage(void)
480 {
481         (void)fprintf(stderr, "usage: dumpfs [-fm] filesys | device\n");
482         exit(1);
483 }