dumpfs.c revision 2154
11558Srgrimes/* 21558Srgrimes * Copyright (c) 1983, 1992, 1993 31558Srgrimes * The Regents of the University of California. All rights reserved. 41558Srgrimes * 51558Srgrimes * Redistribution and use in source and binary forms, with or without 61558Srgrimes * modification, are permitted provided that the following conditions 71558Srgrimes * are met: 81558Srgrimes * 1. Redistributions of source code must retain the above copyright 91558Srgrimes * notice, this list of conditions and the following disclaimer. 101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111558Srgrimes * notice, this list of conditions and the following disclaimer in the 121558Srgrimes * documentation and/or other materials provided with the distribution. 131558Srgrimes * 3. All advertising materials mentioning features or use of this software 141558Srgrimes * must display the following acknowledgement: 151558Srgrimes * This product includes software developed by the University of 161558Srgrimes * California, Berkeley and its contributors. 171558Srgrimes * 4. Neither the name of the University nor the names of its contributors 181558Srgrimes * may be used to endorse or promote products derived from this software 191558Srgrimes * without specific prior written permission. 201558Srgrimes * 211558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241558Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311558Srgrimes * SUCH DAMAGE. 321558Srgrimes */ 331558Srgrimes 341558Srgrimes#ifndef lint 351558Srgrimesstatic char copyright[] = 361558Srgrimes"@(#) Copyright (c) 1983, 1992, 1993\n\ 371558Srgrimes The Regents of the University of California. All rights reserved.\n"; 381558Srgrimes#endif /* not lint */ 391558Srgrimes 401558Srgrimes#ifndef lint 411558Srgrimesstatic char sccsid[] = "@(#)dumpfs.c 8.2 (Berkeley) 2/2/94"; 421558Srgrimes#endif /* not lint */ 431558Srgrimes 441558Srgrimes#include <sys/param.h> 451558Srgrimes#include <sys/time.h> 461558Srgrimes 471558Srgrimes#include <ufs/ufs/dinode.h> 481558Srgrimes#include <ufs/ffs/fs.h> 491558Srgrimes 501558Srgrimes#include <unistd.h> 511558Srgrimes#include <fcntl.h> 521558Srgrimes#include <errno.h> 531558Srgrimes#include <fstab.h> 541558Srgrimes#include <stdio.h> 551558Srgrimes#include <stdlib.h> 561558Srgrimes#include <string.h> 571558Srgrimes 581558Srgrimesunion { 591558Srgrimes struct fs fs; 601558Srgrimes char pad[MAXBSIZE]; 611558Srgrimes} fsun; 621558Srgrimes#define afs fsun.fs 631558Srgrimes 641558Srgrimesunion { 651558Srgrimes struct cg cg; 661558Srgrimes char pad[MAXBSIZE]; 671558Srgrimes} cgun; 681558Srgrimes#define acg cgun.cg 691558Srgrimes 701558Srgrimeslong dev_bsize = 1; 711558Srgrimes 721558Srgrimesint dumpfs __P((char *)); 731558Srgrimesint dumpcg __P((char *, int, int)); 741558Srgrimesvoid pbits __P((void *, int)); 751558Srgrimesvoid usage __P((void)); 761558Srgrimes 771558Srgrimesint 781558Srgrimesmain(argc, argv) 791558Srgrimes int argc; 801558Srgrimes char *argv[]; 811558Srgrimes{ 821558Srgrimes register struct fstab *fs; 831558Srgrimes int ch, eval; 841558Srgrimes 851558Srgrimes while ((ch = getopt(argc, argv, "")) != EOF) 861558Srgrimes switch(ch) { 871558Srgrimes case '?': 881558Srgrimes default: 891558Srgrimes usage(); 901558Srgrimes } 911558Srgrimes argc -= optind; 921558Srgrimes argv += optind; 931558Srgrimes 941558Srgrimes if (argc < 1) 951558Srgrimes usage(); 961558Srgrimes 971558Srgrimes for (eval = 0; *argv; ++argv) 981558Srgrimes if ((fs = getfsfile(*argv)) == NULL) 991558Srgrimes eval |= dumpfs(*argv); 1001558Srgrimes else 1011558Srgrimes eval |= dumpfs(fs->fs_spec); 1021558Srgrimes exit(eval); 1031558Srgrimes} 1041558Srgrimes 1051558Srgrimesint 1061558Srgrimesdumpfs(name) 1071558Srgrimes char *name; 1081558Srgrimes{ 1091558Srgrimes int fd, c, i, j, k, size; 1101558Srgrimes 1111558Srgrimes if ((fd = open(name, O_RDONLY, 0)) < 0) 1121558Srgrimes goto err; 1131558Srgrimes if (lseek(fd, (off_t)SBOFF, SEEK_SET) == (off_t)-1) 1141558Srgrimes goto err; 1151558Srgrimes if (read(fd, &afs, SBSIZE) != SBSIZE) 1161558Srgrimes goto err; 1171558Srgrimes 1181558Srgrimes if (afs.fs_postblformat == FS_42POSTBLFMT) 1191558Srgrimes afs.fs_nrpos = 8; 1201558Srgrimes dev_bsize = afs.fs_fsize / fsbtodb(&afs, 1); 1211558Srgrimes printf("magic\t%x\ttime\t%s", afs.fs_magic, 1221558Srgrimes ctime(&afs.fs_time)); 1231558Srgrimes printf("cylgrp\t%s\tinodes\t%s\n", 1241558Srgrimes afs.fs_postblformat == FS_42POSTBLFMT ? "static" : "dynamic", 1251558Srgrimes afs.fs_inodefmt < FS_44INODEFMT ? "4.2/4.3BSD" : "4.4BSD"); 1261558Srgrimes printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n", 1271558Srgrimes afs.fs_cstotal.cs_nbfree, afs.fs_cstotal.cs_ndir, 1281558Srgrimes afs.fs_cstotal.cs_nifree, afs.fs_cstotal.cs_nffree); 1291558Srgrimes printf("ncg\t%d\tncyl\t%d\tsize\t%d\tblocks\t%d\n", 1301558Srgrimes afs.fs_ncg, afs.fs_ncyl, afs.fs_size, afs.fs_dsize); 1311558Srgrimes printf("bsize\t%d\tshift\t%d\tmask\t0x%08x\n", 1321558Srgrimes afs.fs_bsize, afs.fs_bshift, afs.fs_bmask); 1331558Srgrimes printf("fsize\t%d\tshift\t%d\tmask\t0x%08x\n", 1341558Srgrimes afs.fs_fsize, afs.fs_fshift, afs.fs_fmask); 1351558Srgrimes printf("frag\t%d\tshift\t%d\tfsbtodb\t%d\n", 1361558Srgrimes afs.fs_frag, afs.fs_fragshift, afs.fs_fsbtodb); 1371558Srgrimes printf("cpg\t%d\tbpg\t%d\tfpg\t%d\tipg\t%d\n", 1381558Srgrimes afs.fs_cpg, afs.fs_fpg / afs.fs_frag, afs.fs_fpg, afs.fs_ipg); 1391558Srgrimes printf("minfree\t%d%%\toptim\t%s\tmaxcontig %d\tmaxbpg\t%d\n", 1401558Srgrimes afs.fs_minfree, afs.fs_optim == FS_OPTSPACE ? "space" : "time", 1411558Srgrimes afs.fs_maxcontig, afs.fs_maxbpg); 1421558Srgrimes printf("rotdelay %dms\theadswitch %dus\ttrackseek %dus\trps\t%d\n", 1431558Srgrimes afs.fs_rotdelay, afs.fs_headswitch, afs.fs_trkseek, afs.fs_rps); 1441558Srgrimes printf("ntrak\t%d\tnsect\t%d\tnpsect\t%d\tspc\t%d\n", 1451558Srgrimes afs.fs_ntrak, afs.fs_nsect, afs.fs_npsect, afs.fs_spc); 1461558Srgrimes printf("symlinklen %d\ttrackskew %d\tinterleave %d\tcontigsumsize %d\n", 1471558Srgrimes afs.fs_maxsymlinklen, afs.fs_trackskew, afs.fs_interleave, 1481558Srgrimes afs.fs_contigsumsize); 1491558Srgrimes printf("nindir\t%d\tinopb\t%d\tnspf\t%d\n", 1501558Srgrimes afs.fs_nindir, afs.fs_inopb, afs.fs_nspf); 1511558Srgrimes printf("sblkno\t%d\tcblkno\t%d\tiblkno\t%d\tdblkno\t%d\n", 1521558Srgrimes afs.fs_sblkno, afs.fs_cblkno, afs.fs_iblkno, afs.fs_dblkno); 1531558Srgrimes printf("sbsize\t%d\tcgsize\t%d\tcgoffset %d\tcgmask\t0x%08x\n", 1541558Srgrimes afs.fs_sbsize, afs.fs_cgsize, afs.fs_cgoffset, afs.fs_cgmask); 1551558Srgrimes printf("csaddr\t%d\tcssize\t%d\tshift\t%d\tmask\t0x%08x\n", 1561558Srgrimes afs.fs_csaddr, afs.fs_cssize, afs.fs_csshift, afs.fs_csmask); 1572154Sdg printf("cgrotor\t%d\tfmod\t%d\tronly\t%d\tclean\t%d\n", 1582154Sdg afs.fs_cgrotor, afs.fs_fmod, afs.fs_ronly, afs.fs_clean); 1591558Srgrimes if (afs.fs_cpc != 0) 1601558Srgrimes printf("blocks available in each of %d rotational positions", 1611558Srgrimes afs.fs_nrpos); 1621558Srgrimes else 1631558Srgrimes printf("insufficient space to maintain rotational tables\n"); 1641558Srgrimes for (c = 0; c < afs.fs_cpc; c++) { 1651558Srgrimes printf("\ncylinder number %d:", c); 1661558Srgrimes for (i = 0; i < afs.fs_nrpos; i++) { 1671558Srgrimes if (fs_postbl(&afs, c)[i] == -1) 1681558Srgrimes continue; 1691558Srgrimes printf("\n position %d:\t", i); 1701558Srgrimes for (j = fs_postbl(&afs, c)[i], k = 1; ; 1711558Srgrimes j += fs_rotbl(&afs)[j], k++) { 1721558Srgrimes printf("%5d", j); 1731558Srgrimes if (k % 12 == 0) 1741558Srgrimes printf("\n\t\t"); 1751558Srgrimes if (fs_rotbl(&afs)[j] == 0) 1761558Srgrimes break; 1771558Srgrimes } 1781558Srgrimes } 1791558Srgrimes } 1801558Srgrimes printf("\ncs[].cs_(nbfree,ndir,nifree,nffree):\n\t"); 1811558Srgrimes for (i = 0, j = 0; i < afs.fs_cssize; i += afs.fs_bsize, j++) { 1821558Srgrimes size = afs.fs_cssize - i < afs.fs_bsize ? 1831558Srgrimes afs.fs_cssize - i : afs.fs_bsize; 1841558Srgrimes afs.fs_csp[j] = calloc(1, size); 1851558Srgrimes if (lseek(fd, 1861558Srgrimes (off_t)(fsbtodb(&afs, (afs.fs_csaddr + j * afs.fs_frag)) * 1871558Srgrimes dev_bsize), SEEK_SET) == (off_t)-1) 1881558Srgrimes goto err; 1891558Srgrimes if (read(fd, afs.fs_csp[j], size) != size) 1901558Srgrimes goto err; 1911558Srgrimes } 1921558Srgrimes for (i = 0; i < afs.fs_ncg; i++) { 1931558Srgrimes struct csum *cs = &afs.fs_cs(&afs, i); 1941558Srgrimes if (i && i % 4 == 0) 1951558Srgrimes printf("\n\t"); 1961558Srgrimes printf("(%d,%d,%d,%d) ", 1971558Srgrimes cs->cs_nbfree, cs->cs_ndir, cs->cs_nifree, cs->cs_nffree); 1981558Srgrimes } 1991558Srgrimes printf("\n"); 2001558Srgrimes if (afs.fs_ncyl % afs.fs_cpg) { 2011558Srgrimes printf("cylinders in last group %d\n", 2021558Srgrimes i = afs.fs_ncyl % afs.fs_cpg); 2031558Srgrimes printf("blocks in last group %d\n", 2041558Srgrimes i * afs.fs_spc / NSPB(&afs)); 2051558Srgrimes } 2061558Srgrimes printf("\n"); 2071558Srgrimes for (i = 0; i < afs.fs_ncg; i++) 2081558Srgrimes if (dumpcg(name, fd, i)) 2091558Srgrimes goto err; 2101558Srgrimes (void)close(fd); 2111558Srgrimes return (0); 2121558Srgrimes 2131558Srgrimeserr: if (fd != -1) 2141558Srgrimes (void)close(fd); 2151558Srgrimes (void)fprintf(stderr, "dumpfs: %s: %s\n", name, strerror(errno)); 2161558Srgrimes return (1); 2171558Srgrimes}; 2181558Srgrimes 2191558Srgrimesint 2201558Srgrimesdumpcg(name, fd, c) 2211558Srgrimes char *name; 2221558Srgrimes int fd, c; 2231558Srgrimes{ 2241558Srgrimes off_t cur; 2251558Srgrimes int i, j; 2261558Srgrimes 2271558Srgrimes printf("\ncg %d:\n", c); 2281558Srgrimes if ((cur = lseek(fd, (off_t)(fsbtodb(&afs, cgtod(&afs, c)) * dev_bsize), 2291558Srgrimes SEEK_SET)) == (off_t)-1) 2301558Srgrimes return (1); 2311558Srgrimes if (read(fd, &acg, afs.fs_bsize) != afs.fs_bsize) { 2321558Srgrimes (void)fprintf(stderr, "dumpfs: %s: error reading cg\n", name); 2331558Srgrimes return (1); 2341558Srgrimes } 2351558Srgrimes printf("magic\t%x\ttell\t%qx\ttime\t%s", 2361558Srgrimes afs.fs_postblformat == FS_42POSTBLFMT ? 2371558Srgrimes ((struct ocg *)&acg)->cg_magic : acg.cg_magic, 2381558Srgrimes cur, ctime(&acg.cg_time)); 2391558Srgrimes printf("cgx\t%d\tncyl\t%d\tniblk\t%d\tndblk\t%d\n", 2401558Srgrimes acg.cg_cgx, acg.cg_ncyl, acg.cg_niblk, acg.cg_ndblk); 2411558Srgrimes printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n", 2421558Srgrimes acg.cg_cs.cs_nbfree, acg.cg_cs.cs_ndir, 2431558Srgrimes acg.cg_cs.cs_nifree, acg.cg_cs.cs_nffree); 2441558Srgrimes printf("rotor\t%d\tirotor\t%d\tfrotor\t%d\nfrsum", 2451558Srgrimes acg.cg_rotor, acg.cg_irotor, acg.cg_frotor); 2461558Srgrimes for (i = 1, j = 0; i < afs.fs_frag; i++) { 2471558Srgrimes printf("\t%d", acg.cg_frsum[i]); 2481558Srgrimes j += i * acg.cg_frsum[i]; 2491558Srgrimes } 2501558Srgrimes printf("\nsum of frsum: %d", j); 2511558Srgrimes if (afs.fs_contigsumsize > 0) { 2521558Srgrimes for (i = 1; i < afs.fs_contigsumsize; i++) { 2531558Srgrimes if ((i - 1) % 8 == 0) 2541558Srgrimes printf("\nclusters %d-%d:", i, 2551558Srgrimes afs.fs_contigsumsize - 1 < i + 7 ? 2561558Srgrimes afs.fs_contigsumsize - 1 : i + 7); 2571558Srgrimes printf("\t%d", cg_clustersum(&acg)[i]); 2581558Srgrimes } 2591558Srgrimes printf("\nclusters size %d and over: %d\n", 2601558Srgrimes afs.fs_contigsumsize, 2611558Srgrimes cg_clustersum(&acg)[afs.fs_contigsumsize]); 2621558Srgrimes printf("clusters free:\t"); 2631558Srgrimes pbits(cg_clustersfree(&acg), acg.cg_nclusterblks); 2641558Srgrimes } else 2651558Srgrimes printf("\n"); 2661558Srgrimes printf("iused:\t"); 2671558Srgrimes pbits(cg_inosused(&acg), afs.fs_ipg); 2681558Srgrimes printf("free:\t"); 2691558Srgrimes pbits(cg_blksfree(&acg), afs.fs_fpg); 2701558Srgrimes printf("b:\n"); 2711558Srgrimes for (i = 0; i < afs.fs_cpg; i++) { 2721558Srgrimes if (cg_blktot(&acg)[i] == 0) 2731558Srgrimes continue; 2741558Srgrimes printf(" c%d:\t(%d)\t", i, cg_blktot(&acg)[i]); 2751558Srgrimes for (j = 0; j < afs.fs_nrpos; j++) { 2761558Srgrimes if (afs.fs_cpc > 0 && 2771558Srgrimes fs_postbl(&afs, i % afs.fs_cpc)[j] == -1) 2781558Srgrimes continue; 2791558Srgrimes printf(" %d", cg_blks(&afs, &acg, i)[j]); 2801558Srgrimes } 2811558Srgrimes printf("\n"); 2821558Srgrimes } 2831558Srgrimes return (0); 2841558Srgrimes}; 2851558Srgrimes 2861558Srgrimesvoid 2871558Srgrimespbits(vp, max) 2881558Srgrimes register void *vp; 2891558Srgrimes int max; 2901558Srgrimes{ 2911558Srgrimes register int i; 2921558Srgrimes register char *p; 2931558Srgrimes int count, j; 2941558Srgrimes 2951558Srgrimes for (count = i = 0, p = vp; i < max; i++) 2961558Srgrimes if (isset(p, i)) { 2971558Srgrimes if (count) 2981558Srgrimes printf(",%s", count % 6 ? " " : "\n\t"); 2991558Srgrimes count++; 3001558Srgrimes printf("%d", i); 3011558Srgrimes j = i; 3021558Srgrimes while ((i+1)<max && isset(p, i+1)) 3031558Srgrimes i++; 3041558Srgrimes if (i != j) 3051558Srgrimes printf("-%d", i); 3061558Srgrimes } 3071558Srgrimes printf("\n"); 3081558Srgrimes} 3091558Srgrimes 3101558Srgrimesvoid 3111558Srgrimesusage() 3121558Srgrimes{ 3131558Srgrimes (void)fprintf(stderr, "usage: dumpfs filesys | device\n"); 3141558Srgrimes exit(1); 3151558Srgrimes} 316