]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sbin/dumpfs/dumpfs.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.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 <paths.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     dumpfsid(void);
84 int     dumpcg(void);
85 int     dumpfreespace(const char *, int);
86 void    dumpfreespacecg(int);
87 int     marshal(const char *);
88 void    pbits(void *, int);
89 void    pblklist(void *, int, off_t, int);
90 void    ufserr(const char *);
91 void    usage(void) __dead2;
92
93 int
94 main(int argc, char *argv[])
95 {
96         const char *name;
97         int ch, dofreespace, domarshal, dolabel, eval;
98
99         dofreespace = domarshal = dolabel = eval = 0;
100
101         while ((ch = getopt(argc, argv, "lfm")) != -1) {
102                 switch (ch) {
103                 case 'f':
104                         dofreespace++;
105                         break;
106                 case 'm':
107                         domarshal = 1;
108                         break;
109                 case 'l':
110                         dolabel = 1;
111                         break;
112                 case '?':
113                 default:
114                         usage();
115                 }
116         }
117         argc -= optind;
118         argv += optind;
119
120         if (argc < 1)
121                 usage();
122         if (dofreespace && domarshal)
123                 usage();
124         if (dofreespace > 2)
125                 usage();
126
127         while ((name = *argv++) != NULL) {
128                 if (ufs_disk_fillout(&disk, name) == -1) {
129                         ufserr(name);
130                         eval |= 1;
131                         continue;
132                 }
133                 if (dofreespace)
134                         eval |= dumpfreespace(name, dofreespace);
135                 else if (domarshal)
136                         eval |= marshal(name);
137                 else if (dolabel)
138                         eval |= dumpfsid();
139                 else
140                         eval |= dumpfs(name);
141                 ufs_disk_close(&disk);
142         }
143         exit(eval);
144 }
145
146 int
147 dumpfsid(void)
148 {
149
150         printf("%sufsid/%08x%08x\n", _PATH_DEV, afs.fs_id[0], afs.fs_id[1]);
151         return 0;
152 }
153
154 int
155 dumpfs(const char *name)
156 {
157         time_t fstime;
158         int64_t fssize;
159         int32_t fsflags;
160         int i;
161
162         switch (disk.d_ufs) {
163         case 2:
164                 fssize = afs.fs_size;
165                 fstime = afs.fs_time;
166                 printf("magic\t%x (UFS2)\ttime\t%s",
167                     afs.fs_magic, ctime(&fstime));
168                 printf("superblock location\t%jd\tid\t[ %x %x ]\n",
169                     (intmax_t)afs.fs_sblockloc, afs.fs_id[0], afs.fs_id[1]);
170                 printf("ncg\t%d\tsize\t%jd\tblocks\t%jd\n",
171                     afs.fs_ncg, (intmax_t)fssize, (intmax_t)afs.fs_dsize);
172                 break;
173         case 1:
174                 fssize = afs.fs_old_size;
175                 fstime = afs.fs_old_time;
176                 printf("magic\t%x (UFS1)\ttime\t%s",
177                     afs.fs_magic, ctime(&fstime));
178                 printf("id\t[ %08x %08x ]\n", afs.fs_id[0], afs.fs_id[1]);
179                 printf("ncg\t%d\tsize\t%jd\tblocks\t%jd\n",
180                     afs.fs_ncg, (intmax_t)fssize, (intmax_t)afs.fs_dsize);
181                 break;
182         default:
183                 goto err;
184         }
185         printf("bsize\t%d\tshift\t%d\tmask\t0x%08x\n",
186             afs.fs_bsize, afs.fs_bshift, afs.fs_bmask);
187         printf("fsize\t%d\tshift\t%d\tmask\t0x%08x\n",
188             afs.fs_fsize, afs.fs_fshift, afs.fs_fmask);
189         printf("frag\t%d\tshift\t%d\tfsbtodb\t%d\n",
190             afs.fs_frag, afs.fs_fragshift, afs.fs_fsbtodb);
191         printf("minfree\t%d%%\toptim\t%s\tsymlinklen %d\n",
192             afs.fs_minfree, afs.fs_optim == FS_OPTSPACE ? "space" : "time",
193             afs.fs_maxsymlinklen);
194         switch (disk.d_ufs) {
195         case 2:
196                 printf("%s %d\tmaxbpg\t%d\tmaxcontig %d\tcontigsumsize %d\n",
197                     "maxbsize", afs.fs_maxbsize, afs.fs_maxbpg,
198                     afs.fs_maxcontig, afs.fs_contigsumsize);
199                 printf("nbfree\t%jd\tndir\t%jd\tnifree\t%jd\tnffree\t%jd\n",
200                     (intmax_t)afs.fs_cstotal.cs_nbfree, 
201                     (intmax_t)afs.fs_cstotal.cs_ndir,
202                     (intmax_t)afs.fs_cstotal.cs_nifree, 
203                     (intmax_t)afs.fs_cstotal.cs_nffree);
204                 printf("bpg\t%d\tfpg\t%d\tipg\t%d\tunrefs\t%jd\n",
205                     afs.fs_fpg / afs.fs_frag, afs.fs_fpg, afs.fs_ipg,
206                     (intmax_t)afs.fs_unrefs);
207                 printf("nindir\t%d\tinopb\t%d\tmaxfilesize\t%ju\n",
208                     afs.fs_nindir, afs.fs_inopb, 
209                     (uintmax_t)afs.fs_maxfilesize);
210                 printf("sbsize\t%d\tcgsize\t%d\tcsaddr\t%jd\tcssize\t%d\n",
211                     afs.fs_sbsize, afs.fs_cgsize, (intmax_t)afs.fs_csaddr,
212                     afs.fs_cssize);
213                 break;
214         case 1:
215                 printf("maxbpg\t%d\tmaxcontig %d\tcontigsumsize %d\n",
216                     afs.fs_maxbpg, afs.fs_maxcontig, afs.fs_contigsumsize);
217                 printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
218                     afs.fs_old_cstotal.cs_nbfree, afs.fs_old_cstotal.cs_ndir,
219                     afs.fs_old_cstotal.cs_nifree, afs.fs_old_cstotal.cs_nffree);
220                 printf("cpg\t%d\tbpg\t%d\tfpg\t%d\tipg\t%d\n",
221                     afs.fs_old_cpg, afs.fs_fpg / afs.fs_frag, afs.fs_fpg,
222                     afs.fs_ipg);
223                 printf("nindir\t%d\tinopb\t%d\tnspf\t%d\tmaxfilesize\t%ju\n",
224                     afs.fs_nindir, afs.fs_inopb, afs.fs_old_nspf,
225                     (uintmax_t)afs.fs_maxfilesize);
226                 printf("sbsize\t%d\tcgsize\t%d\tcgoffset %d\tcgmask\t0x%08x\n",
227                     afs.fs_sbsize, afs.fs_cgsize, afs.fs_old_cgoffset,
228                     afs.fs_old_cgmask);
229                 printf("csaddr\t%d\tcssize\t%d\n",
230                     afs.fs_old_csaddr, afs.fs_cssize);
231                 printf("rotdelay %dms\trps\t%d\ttrackskew %d\tinterleave %d\n",
232                     afs.fs_old_rotdelay, afs.fs_old_rps, afs.fs_old_trackskew,
233                     afs.fs_old_interleave);
234                 printf("nsect\t%d\tnpsect\t%d\tspc\t%d\n",
235                     afs.fs_old_nsect, afs.fs_old_npsect, afs.fs_old_spc);
236                 break;
237         default:
238                 goto err;
239         }
240         printf("sblkno\t%d\tcblkno\t%d\tiblkno\t%d\tdblkno\t%d\n",
241             afs.fs_sblkno, afs.fs_cblkno, afs.fs_iblkno, afs.fs_dblkno);
242         printf("cgrotor\t%d\tfmod\t%d\tronly\t%d\tclean\t%d\n",
243             afs.fs_cgrotor, afs.fs_fmod, afs.fs_ronly, afs.fs_clean);
244         printf("avgfpdir %d\tavgfilesize %d\n",
245             afs.fs_avgfpdir, afs.fs_avgfilesize);
246         printf("flags\t");
247         if (afs.fs_old_flags & FS_FLAGS_UPDATED)
248                 fsflags = afs.fs_flags;
249         else
250                 fsflags = afs.fs_old_flags;
251         if (fsflags == 0)
252                 printf("none");
253         if (fsflags & FS_UNCLEAN)
254                 printf("unclean ");
255         if (fsflags & FS_DOSOFTDEP)
256                 printf("soft-updates%s ", (fsflags & FS_SUJ) ? "+journal" : "");
257         if (fsflags & FS_NEEDSFSCK)
258                 printf("needs fsck run ");
259         if (fsflags & FS_INDEXDIRS)
260                 printf("indexed directories ");
261         if (fsflags & FS_ACLS)
262                 printf("acls ");
263         if (fsflags & FS_MULTILABEL)
264                 printf("multilabel ");
265         if (fsflags & FS_GJOURNAL)
266                 printf("gjournal ");
267         if (fsflags & FS_FLAGS_UPDATED)
268                 printf("fs_flags expanded ");
269         if (fsflags & FS_NFS4ACLS)
270                 printf("nfsv4acls ");
271         if (fsflags & FS_TRIM)
272                 printf("trim ");
273         fsflags &= ~(FS_UNCLEAN | FS_DOSOFTDEP | FS_NEEDSFSCK | FS_INDEXDIRS |
274                      FS_ACLS | FS_MULTILABEL | FS_GJOURNAL | FS_FLAGS_UPDATED |
275                      FS_NFS4ACLS | FS_SUJ | FS_TRIM);
276         if (fsflags != 0)
277                 printf("unknown flags (%#x)", fsflags);
278         putchar('\n');
279         printf("fsmnt\t%s\n", afs.fs_fsmnt);
280         printf("volname\t%s\tswuid\t%ju\n",
281                 afs.fs_volname, (uintmax_t)afs.fs_swuid);
282         printf("\ncs[].cs_(nbfree,ndir,nifree,nffree):\n\t");
283         afs.fs_csp = calloc(1, afs.fs_cssize);
284         if (bread(&disk, fsbtodb(&afs, afs.fs_csaddr), afs.fs_csp, afs.fs_cssize) == -1)
285                 goto err;
286         for (i = 0; i < afs.fs_ncg; i++) {
287                 struct csum *cs = &afs.fs_cs(&afs, i);
288                 if (i && i % 4 == 0)
289                         printf("\n\t");
290                 printf("(%d,%d,%d,%d) ",
291                     cs->cs_nbfree, cs->cs_ndir, cs->cs_nifree, cs->cs_nffree);
292         }
293         printf("\n");
294         if (fssize % afs.fs_fpg) {
295                 if (disk.d_ufs == 1)
296                         printf("cylinders in last group %d\n",
297                             howmany(afs.fs_old_size % afs.fs_fpg,
298                             afs.fs_old_spc / afs.fs_old_nspf));
299                 printf("blocks in last group %ld\n\n",
300                     (long)((fssize % afs.fs_fpg) / afs.fs_frag));
301         }
302         while ((i = cgread(&disk)) != 0) {
303                 if (i == -1 || dumpcg())
304                         goto err;
305         }
306         return (0);
307
308 err:    ufserr(name);
309         return (1);
310 }
311
312 int
313 dumpcg(void)
314 {
315         time_t cgtime;
316         off_t cur;
317         int i, j;
318
319         printf("\ncg %d:\n", disk.d_lcg);
320         cur = fsbtodb(&afs, cgtod(&afs, disk.d_lcg)) * disk.d_bsize;
321         switch (disk.d_ufs) {
322         case 2:
323                 cgtime = acg.cg_time;
324                 printf("magic\t%x\ttell\t%jx\ttime\t%s",
325                     acg.cg_magic, (intmax_t)cur, ctime(&cgtime));
326                 printf("cgx\t%d\tndblk\t%d\tniblk\t%d\tinitiblk %d\tunrefs %d\n",
327                     acg.cg_cgx, acg.cg_ndblk, acg.cg_niblk, acg.cg_initediblk,
328                     acg.cg_unrefs);
329                 break;
330         case 1:
331                 cgtime = acg.cg_old_time;
332                 printf("magic\t%x\ttell\t%jx\ttime\t%s",
333                     acg.cg_magic, (intmax_t)cur, ctime(&cgtime));
334                 printf("cgx\t%d\tncyl\t%d\tniblk\t%d\tndblk\t%d\n",
335                     acg.cg_cgx, acg.cg_old_ncyl, acg.cg_old_niblk,
336                     acg.cg_ndblk);
337                 break;
338         default:
339                 break;
340         }
341         printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
342             acg.cg_cs.cs_nbfree, acg.cg_cs.cs_ndir,
343             acg.cg_cs.cs_nifree, acg.cg_cs.cs_nffree);
344         printf("rotor\t%d\tirotor\t%d\tfrotor\t%d\nfrsum",
345             acg.cg_rotor, acg.cg_irotor, acg.cg_frotor);
346         for (i = 1, j = 0; i < afs.fs_frag; i++) {
347                 printf("\t%d", acg.cg_frsum[i]);
348                 j += i * acg.cg_frsum[i];
349         }
350         printf("\nsum of frsum: %d", j);
351         if (afs.fs_contigsumsize > 0) {
352                 for (i = 1; i < afs.fs_contigsumsize; i++) {
353                         if ((i - 1) % 8 == 0)
354                                 printf("\nclusters %d-%d:", i,
355                                     afs.fs_contigsumsize - 1 < i + 7 ?
356                                     afs.fs_contigsumsize - 1 : i + 7);
357                         printf("\t%d", cg_clustersum(&acg)[i]);
358                 }
359                 printf("\nclusters size %d and over: %d\n",
360                     afs.fs_contigsumsize,
361                     cg_clustersum(&acg)[afs.fs_contigsumsize]);
362                 printf("clusters free:\t");
363                 pbits(cg_clustersfree(&acg), acg.cg_nclusterblks);
364         } else
365                 printf("\n");
366         printf("inodes used:\t");
367         pbits(cg_inosused(&acg), afs.fs_ipg);
368         printf("blks free:\t");
369         pbits(cg_blksfree(&acg), afs.fs_fpg);
370         return (0);
371 }
372
373 int
374 dumpfreespace(const char *name, int fflag)
375 {
376         int i;
377
378         while ((i = cgread(&disk)) != 0) {
379                 if (i == -1)
380                         goto err;
381                 dumpfreespacecg(fflag);
382         }
383         return (0);
384 err:
385         ufserr(name);
386         return (1);
387 }
388
389 void
390 dumpfreespacecg(int fflag)
391 {
392
393         pblklist(cg_blksfree(&acg), afs.fs_fpg, disk.d_lcg * afs.fs_fpg,
394             fflag);
395 }
396
397 int
398 marshal(const char *name)
399 {
400         struct fs *fs;
401
402         fs = &disk.d_fs;
403
404         printf("# newfs command for %s (%s)\n", name, disk.d_name);
405         printf("newfs ");
406         if (fs->fs_volname[0] != '\0')
407                 printf("-L %s ", fs->fs_volname);
408         printf("-O %d ", disk.d_ufs);
409         if (fs->fs_flags & FS_DOSOFTDEP)
410                 printf("-U ");
411         printf("-a %d ", fs->fs_maxcontig);
412         printf("-b %d ", fs->fs_bsize);
413         /* -c is dumb */
414         printf("-d %d ", fs->fs_maxbsize);
415         printf("-e %d ", fs->fs_maxbpg);
416         printf("-f %d ", fs->fs_fsize);
417         printf("-g %d ", fs->fs_avgfilesize);
418         printf("-h %d ", fs->fs_avgfpdir);
419         /* -i is dumb */
420         if (fs->fs_flags & FS_SUJ)
421                 printf("-j ");
422         /* -k..l unimplemented */
423         printf("-m %d ", fs->fs_minfree);
424         /* -n unimplemented */
425         printf("-o ");
426         switch (fs->fs_optim) {
427         case FS_OPTSPACE:
428                 printf("space ");
429                 break;
430         case FS_OPTTIME:
431                 printf("time ");
432                 break;
433         default:
434                 printf("unknown ");
435                 break;
436         }
437         /* -p..r unimplemented */
438         printf("-s %jd ", (intmax_t)fsbtodb(fs, fs->fs_size));
439         if (fs->fs_flags & FS_TRIM)
440                 printf("-t ");
441         printf("%s ", disk.d_name);
442         printf("\n");
443
444         return 0;
445 }
446
447 void
448 pbits(void *vp, int max)
449 {
450         int i;
451         char *p;
452         int count, j;
453
454         for (count = i = 0, p = vp; i < max; i++)
455                 if (isset(p, i)) {
456                         if (count)
457                                 printf(",%s", count % 6 ? " " : "\n\t");
458                         count++;
459                         printf("%d", i);
460                         j = i;
461                         while ((i+1)<max && isset(p, i+1))
462                                 i++;
463                         if (i != j)
464                                 printf("-%d", i);
465                 }
466         printf("\n");
467 }
468
469 void
470 pblklist(void *vp, int max, off_t offset, int fflag)
471 {
472         int i, j;
473         char *p;
474
475         for (i = 0, p = vp; i < max; i++) {
476                 if (isset(p, i)) {
477                         printf("%jd", (intmax_t)(i + offset));
478                         if (fflag < 2) {
479                                 j = i;
480                                 while ((i+1)<max && isset(p, i+1))
481                                         i++;
482                                 if (i != j)
483                                         printf("-%jd", (intmax_t)(i + offset));
484                         }
485                         printf("\n");
486                 }
487         }
488 }
489
490 void
491 ufserr(const char *name)
492 {
493         if (disk.d_error != NULL)
494                 warnx("%s: %s", name, disk.d_error);
495         else if (errno)
496                 warn("%s", name);
497 }
498
499 void
500 usage(void)
501 {
502         (void)fprintf(stderr, "usage: dumpfs [-fm] filesys | device\n");
503         exit(1);
504 }