18fae3551SRodney W. Grimes /*
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
4fd15d586SRobert Watson * Copyright (c) 2009 Robert N. M. Watson
5fd15d586SRobert Watson * All rights reserved.
6fd15d586SRobert Watson *
7fd15d586SRobert Watson * This software was developed at the University of Cambridge Computer
8fd15d586SRobert Watson * Laboratory with support from a grant from Google, Inc.
9fd15d586SRobert Watson *
101c85e6a3SKirk McKusick * Copyright (c) 2002 Networks Associates Technology, Inc.
111c85e6a3SKirk McKusick * All rights reserved.
121c85e6a3SKirk McKusick *
131c85e6a3SKirk McKusick * This software was developed for the FreeBSD Project by Marshall
141c85e6a3SKirk McKusick * Kirk McKusick and Network Associates Laboratories, the Security
151c85e6a3SKirk McKusick * Research Division of Network Associates, Inc. under DARPA/SPAWAR
161c85e6a3SKirk McKusick * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
17363c1852SKirk McKusick * research program.
181c85e6a3SKirk McKusick *
198fae3551SRodney W. Grimes * Copyright (c) 1983, 1992, 1993
208fae3551SRodney W. Grimes * The Regents of the University of California. All rights reserved.
218fae3551SRodney W. Grimes *
228fae3551SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
238fae3551SRodney W. Grimes * modification, are permitted provided that the following conditions
248fae3551SRodney W. Grimes * are met:
258fae3551SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
268fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
278fae3551SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
288fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
298fae3551SRodney W. Grimes * documentation and/or other materials provided with the distribution.
30fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
318fae3551SRodney W. Grimes * may be used to endorse or promote products derived from this software
328fae3551SRodney W. Grimes * without specific prior written permission.
338fae3551SRodney W. Grimes *
348fae3551SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
358fae3551SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
368fae3551SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
378fae3551SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
388fae3551SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
398fae3551SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
408fae3551SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
418fae3551SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
428fae3551SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
438fae3551SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
448fae3551SRodney W. Grimes * SUCH DAMAGE.
458fae3551SRodney W. Grimes */
468fae3551SRodney W. Grimes
478fae3551SRodney W. Grimes #include <sys/param.h>
488fae3551SRodney W. Grimes #include <sys/time.h>
4975766e17SPoul-Henning Kamp #include <sys/disklabel.h>
508fae3551SRodney W. Grimes
511c85e6a3SKirk McKusick #include <ufs/ufs/dinode.h>
528fae3551SRodney W. Grimes #include <ufs/ffs/fs.h>
538fae3551SRodney W. Grimes
549d1741b9SPeter Wemm #include <err.h>
557d8939c5SJuli Mallett #include <errno.h>
569d1741b9SPeter Wemm #include <fcntl.h>
578fae3551SRodney W. Grimes #include <fstab.h>
58d67caa29SJuli Mallett #include <libufs.h>
59ef84bb4cSXin LI #include <paths.h>
6087d35aadSJuli Mallett #include <stdint.h>
618fae3551SRodney W. Grimes #include <stdio.h>
628fae3551SRodney W. Grimes #include <stdlib.h>
63*02b7bf07SKirk McKusick #include <string.h>
649d1741b9SPeter Wemm #include <unistd.h>
658fae3551SRodney W. Grimes
66d67caa29SJuli Mallett #define afs disk.d_fs
67a778dc9cSJuli Mallett #define acg disk.d_cg
688fae3551SRodney W. Grimes
691efe3c6bSEd Schouten static struct uufsd disk;
701c85e6a3SKirk McKusick
71dc354845SRobert Wing static int dumpfs(const char *, int);
722162cff1SEd Schouten static int dumpfsid(void);
732162cff1SEd Schouten static int dumpcg(void);
742162cff1SEd Schouten static int dumpfreespace(const char *, int);
752162cff1SEd Schouten static void dumpfreespacecg(int);
762162cff1SEd Schouten static int marshal(const char *);
772162cff1SEd Schouten static void pbits(void *, int);
782162cff1SEd Schouten static void pblklist(void *, int, off_t, int);
79*02b7bf07SKirk McKusick static const char *ufserr(void);
802162cff1SEd Schouten static void usage(void) __dead2;
818fae3551SRodney W. Grimes
828fae3551SRodney W. Grimes int
main(int argc,char * argv[])83b70cd7eeSWarner Losh main(int argc, char *argv[])
848fae3551SRodney W. Grimes {
8587d35aadSJuli Mallett const char *name;
86dc354845SRobert Wing int ch, dofreespace, domarshal, dolabel, dosb, eval;
878fae3551SRodney W. Grimes
88dc354845SRobert Wing dofreespace = domarshal = dolabel = dosb = eval = 0;
89684d26eaSJuli Mallett
90dc354845SRobert Wing while ((ch = getopt(argc, argv, "lfms")) != -1) {
9187d35aadSJuli Mallett switch (ch) {
92fd15d586SRobert Watson case 'f':
93fd15d586SRobert Watson dofreespace++;
94fd15d586SRobert Watson break;
9587d35aadSJuli Mallett case 'm':
9687d35aadSJuli Mallett domarshal = 1;
9787d35aadSJuli Mallett break;
98e293d087SXin LI case 'l':
99e293d087SXin LI dolabel = 1;
100e293d087SXin LI break;
101dc354845SRobert Wing case 's':
102dc354845SRobert Wing dosb = 1;
103dc354845SRobert Wing break;
10487d35aadSJuli Mallett case '?':
10587d35aadSJuli Mallett default:
1068fae3551SRodney W. Grimes usage();
10787d35aadSJuli Mallett }
10887d35aadSJuli Mallett }
1098fae3551SRodney W. Grimes argc -= optind;
1108fae3551SRodney W. Grimes argv += optind;
1118fae3551SRodney W. Grimes
1128fae3551SRodney W. Grimes if (argc < 1)
1138fae3551SRodney W. Grimes usage();
114fd15d586SRobert Watson if (dofreespace && domarshal)
115fd15d586SRobert Watson usage();
116fd15d586SRobert Watson if (dofreespace > 2)
117fd15d586SRobert Watson usage();
1188fae3551SRodney W. Grimes
11987d35aadSJuli Mallett while ((name = *argv++) != NULL) {
120e6886616SKirk McKusick if (ufs_disk_fillout_blank(&disk, name) == -1 ||
121e6886616SKirk McKusick sbfind(&disk, 0) == -1) {
122*02b7bf07SKirk McKusick printf("\n%s: %s\n", name, ufserr());
12387d35aadSJuli Mallett eval |= 1;
12487d35aadSJuli Mallett continue;
12587d35aadSJuli Mallett }
126fd15d586SRobert Watson if (dofreespace)
127fd15d586SRobert Watson eval |= dumpfreespace(name, dofreespace);
128fd15d586SRobert Watson else if (domarshal)
12987d35aadSJuli Mallett eval |= marshal(name);
130e293d087SXin LI else if (dolabel)
131e293d087SXin LI eval |= dumpfsid();
13287d35aadSJuli Mallett else
133dc354845SRobert Wing eval |= dumpfs(name, dosb);
13487d35aadSJuli Mallett ufs_disk_close(&disk);
13587d35aadSJuli Mallett }
1368fae3551SRodney W. Grimes exit(eval);
1378fae3551SRodney W. Grimes }
1388fae3551SRodney W. Grimes
1392162cff1SEd Schouten static int
dumpfsid(void)140e293d087SXin LI dumpfsid(void)
141e293d087SXin LI {
142e293d087SXin LI
143ef84bb4cSXin LI printf("%sufsid/%08x%08x\n", _PATH_DEV, afs.fs_id[0], afs.fs_id[1]);
144e293d087SXin LI return 0;
145e293d087SXin LI }
146e293d087SXin LI
1472162cff1SEd Schouten static int
dumpfs(const char * name,int dosb)148dc354845SRobert Wing dumpfs(const char *name, int dosb)
1498fae3551SRodney W. Grimes {
150a9d9cfffSChuck Silvers time_t fstime, fsmtime;
1511c85e6a3SKirk McKusick int64_t fssize;
152bbef019cSNate Lawson int32_t fsflags;
153*02b7bf07SKirk McKusick int i, ret;
1548fae3551SRodney W. Grimes
15560b9ade0SJuli Mallett switch (disk.d_ufs) {
15660b9ade0SJuli Mallett case 2:
1571c85e6a3SKirk McKusick fssize = afs.fs_size;
1582a6b42f1SJuli Mallett fstime = afs.fs_time;
159a9d9cfffSChuck Silvers fsmtime = afs.fs_mtime;
160a9d9cfffSChuck Silvers printf("magic\t%x (UFS2)\n", afs.fs_magic);
161a9d9cfffSChuck Silvers printf("last mounted time\t%s", ctime(&fsmtime));
162a9d9cfffSChuck Silvers printf("last modified time\t%s", ctime(&fstime));
16324211a0aSAndrey V. Elsukov printf("superblock location\t%jd\tid\t[ %08x %08x ]\n",
164657c605cSJohan Karlsson (intmax_t)afs.fs_sblockloc, afs.fs_id[0], afs.fs_id[1]);
165657c605cSJohan Karlsson printf("ncg\t%d\tsize\t%jd\tblocks\t%jd\n",
166657c605cSJohan Karlsson afs.fs_ncg, (intmax_t)fssize, (intmax_t)afs.fs_dsize);
16760b9ade0SJuli Mallett break;
16860b9ade0SJuli Mallett case 1:
1691c85e6a3SKirk McKusick fssize = afs.fs_old_size;
1702a6b42f1SJuli Mallett fstime = afs.fs_old_time;
1711c85e6a3SKirk McKusick printf("magic\t%x (UFS1)\ttime\t%s",
1722a6b42f1SJuli Mallett afs.fs_magic, ctime(&fstime));
1738f701116SGavin Atkinson printf("id\t[ %08x %08x ]\n", afs.fs_id[0], afs.fs_id[1]);
174657c605cSJohan Karlsson printf("ncg\t%d\tsize\t%jd\tblocks\t%jd\n",
175657c605cSJohan Karlsson afs.fs_ncg, (intmax_t)fssize, (intmax_t)afs.fs_dsize);
17660b9ade0SJuli Mallett break;
17760b9ade0SJuli Mallett default:
178*02b7bf07SKirk McKusick printf("Unknown filesystem type %d\n", disk.d_ufs);
179*02b7bf07SKirk McKusick return (1);
1801c85e6a3SKirk McKusick }
1818fae3551SRodney W. Grimes printf("bsize\t%d\tshift\t%d\tmask\t0x%08x\n",
1828fae3551SRodney W. Grimes afs.fs_bsize, afs.fs_bshift, afs.fs_bmask);
1838fae3551SRodney W. Grimes printf("fsize\t%d\tshift\t%d\tmask\t0x%08x\n",
1848fae3551SRodney W. Grimes afs.fs_fsize, afs.fs_fshift, afs.fs_fmask);
1858fae3551SRodney W. Grimes printf("frag\t%d\tshift\t%d\tfsbtodb\t%d\n",
1868fae3551SRodney W. Grimes afs.fs_frag, afs.fs_fragshift, afs.fs_fsbtodb);
1871c85e6a3SKirk McKusick printf("minfree\t%d%%\toptim\t%s\tsymlinklen %d\n",
1888fae3551SRodney W. Grimes afs.fs_minfree, afs.fs_optim == FS_OPTSPACE ? "space" : "time",
1891c85e6a3SKirk McKusick afs.fs_maxsymlinklen);
19060b9ade0SJuli Mallett switch (disk.d_ufs) {
19160b9ade0SJuli Mallett case 2:
1921c85e6a3SKirk McKusick printf("%s %d\tmaxbpg\t%d\tmaxcontig %d\tcontigsumsize %d\n",
1931c85e6a3SKirk McKusick "maxbsize", afs.fs_maxbsize, afs.fs_maxbpg,
1941c85e6a3SKirk McKusick afs.fs_maxcontig, afs.fs_contigsumsize);
195657c605cSJohan Karlsson printf("nbfree\t%jd\tndir\t%jd\tnifree\t%jd\tnffree\t%jd\n",
196657c605cSJohan Karlsson (intmax_t)afs.fs_cstotal.cs_nbfree,
197657c605cSJohan Karlsson (intmax_t)afs.fs_cstotal.cs_ndir,
198657c605cSJohan Karlsson (intmax_t)afs.fs_cstotal.cs_nifree,
199657c605cSJohan Karlsson (intmax_t)afs.fs_cstotal.cs_nffree);
2005a9dc069SPawel Jakub Dawidek printf("bpg\t%d\tfpg\t%d\tipg\t%d\tunrefs\t%jd\n",
2015a9dc069SPawel Jakub Dawidek afs.fs_fpg / afs.fs_frag, afs.fs_fpg, afs.fs_ipg,
2025a9dc069SPawel Jakub Dawidek (intmax_t)afs.fs_unrefs);
203657c605cSJohan Karlsson printf("nindir\t%d\tinopb\t%d\tmaxfilesize\t%ju\n",
204657c605cSJohan Karlsson afs.fs_nindir, afs.fs_inopb,
205657c605cSJohan Karlsson (uintmax_t)afs.fs_maxfilesize);
2062a6b42f1SJuli Mallett printf("sbsize\t%d\tcgsize\t%d\tcsaddr\t%jd\tcssize\t%d\n",
2072a6b42f1SJuli Mallett afs.fs_sbsize, afs.fs_cgsize, (intmax_t)afs.fs_csaddr,
2082a6b42f1SJuli Mallett afs.fs_cssize);
20960b9ade0SJuli Mallett break;
21060b9ade0SJuli Mallett case 1:
2111c85e6a3SKirk McKusick printf("maxbpg\t%d\tmaxcontig %d\tcontigsumsize %d\n",
2121c85e6a3SKirk McKusick afs.fs_maxbpg, afs.fs_maxcontig, afs.fs_contigsumsize);
2131c85e6a3SKirk McKusick printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
2141c85e6a3SKirk McKusick afs.fs_old_cstotal.cs_nbfree, afs.fs_old_cstotal.cs_ndir,
2151c85e6a3SKirk McKusick afs.fs_old_cstotal.cs_nifree, afs.fs_old_cstotal.cs_nffree);
2161c85e6a3SKirk McKusick printf("cpg\t%d\tbpg\t%d\tfpg\t%d\tipg\t%d\n",
2171c85e6a3SKirk McKusick afs.fs_old_cpg, afs.fs_fpg / afs.fs_frag, afs.fs_fpg,
2181c85e6a3SKirk McKusick afs.fs_ipg);
219657c605cSJohan Karlsson printf("nindir\t%d\tinopb\t%d\tnspf\t%d\tmaxfilesize\t%ju\n",
2201c85e6a3SKirk McKusick afs.fs_nindir, afs.fs_inopb, afs.fs_old_nspf,
221657c605cSJohan Karlsson (uintmax_t)afs.fs_maxfilesize);
2221c85e6a3SKirk McKusick printf("sbsize\t%d\tcgsize\t%d\tcgoffset %d\tcgmask\t0x%08x\n",
2231c85e6a3SKirk McKusick afs.fs_sbsize, afs.fs_cgsize, afs.fs_old_cgoffset,
2241c85e6a3SKirk McKusick afs.fs_old_cgmask);
2251297a704SKirk McKusick printf("csaddr\t%jd\tcssize\t%d\n",
2261297a704SKirk McKusick (intmax_t)afs.fs_csaddr, afs.fs_cssize);
2271c85e6a3SKirk McKusick printf("rotdelay %dms\trps\t%d\ttrackskew %d\tinterleave %d\n",
2281c85e6a3SKirk McKusick afs.fs_old_rotdelay, afs.fs_old_rps, afs.fs_old_trackskew,
2291c85e6a3SKirk McKusick afs.fs_old_interleave);
2301c85e6a3SKirk McKusick printf("nsect\t%d\tnpsect\t%d\tspc\t%d\n",
2311c85e6a3SKirk McKusick afs.fs_old_nsect, afs.fs_old_npsect, afs.fs_old_spc);
23260b9ade0SJuli Mallett break;
23360b9ade0SJuli Mallett default:
234*02b7bf07SKirk McKusick printf("Unknown filesystem type %d\n", disk.d_ufs);
235*02b7bf07SKirk McKusick return (1);
2361c85e6a3SKirk McKusick }
2372e7797cdSKirk McKusick printf("old_cpg\t%d\tsize_cg\t%zu\tCGSIZE\t%zu\n",
23804997e19SKirk McKusick afs.fs_old_cpg, sizeof(struct cg), CGSIZE(&afs));
2398fae3551SRodney W. Grimes printf("sblkno\t%d\tcblkno\t%d\tiblkno\t%d\tdblkno\t%d\n",
2408fae3551SRodney W. Grimes afs.fs_sblkno, afs.fs_cblkno, afs.fs_iblkno, afs.fs_dblkno);
24131fdf44cSDavid Greenman printf("cgrotor\t%d\tfmod\t%d\tronly\t%d\tclean\t%d\n",
24231fdf44cSDavid Greenman afs.fs_cgrotor, afs.fs_fmod, afs.fs_ronly, afs.fs_clean);
243baa12a84SKirk McKusick printf("metaspace %jd\tavgfpdir %d\tavgfilesize %d\n",
244baa12a84SKirk McKusick afs.fs_metaspace, afs.fs_avgfpdir, afs.fs_avgfilesize);
24556d253f6SMike Pritchard printf("flags\t");
246bbef019cSNate Lawson if (afs.fs_old_flags & FS_FLAGS_UPDATED)
247bbef019cSNate Lawson fsflags = afs.fs_flags;
248bbef019cSNate Lawson else
249bbef019cSNate Lawson fsflags = afs.fs_old_flags;
250bbef019cSNate Lawson if (fsflags == 0)
25156d253f6SMike Pritchard printf("none");
252bbef019cSNate Lawson if (fsflags & FS_UNCLEAN)
25356d253f6SMike Pritchard printf("unclean ");
254bbef019cSNate Lawson if (fsflags & FS_DOSOFTDEP)
255113db2ddSJeff Roberson printf("soft-updates%s ", (fsflags & FS_SUJ) ? "+journal" : "");
256bbef019cSNate Lawson if (fsflags & FS_NEEDSFSCK)
257068beacfSKirk McKusick printf("needs-fsck-run ");
258bbef019cSNate Lawson if (fsflags & FS_INDEXDIRS)
259068beacfSKirk McKusick printf("indexed-directories ");
260bbef019cSNate Lawson if (fsflags & FS_ACLS)
261bbef019cSNate Lawson printf("acls ");
262bbef019cSNate Lawson if (fsflags & FS_MULTILABEL)
263bbef019cSNate Lawson printf("multilabel ");
2645a9dc069SPawel Jakub Dawidek if (fsflags & FS_GJOURNAL)
2655a9dc069SPawel Jakub Dawidek printf("gjournal ");
266bbef019cSNate Lawson if (fsflags & FS_FLAGS_UPDATED)
267068beacfSKirk McKusick printf("fs_flags-expanded ");
2689340fc72SEdward Tomasz Napierala if (fsflags & FS_NFS4ACLS)
2699340fc72SEdward Tomasz Napierala printf("nfsv4acls ");
270a738d4cfSKonstantin Belousov if (fsflags & FS_TRIM)
271a738d4cfSKonstantin Belousov printf("trim ");
272068beacfSKirk McKusick fsflags &= ~(FS_UNCLEAN | FS_DOSOFTDEP | FS_NEEDSFSCK | FS_METACKHASH |
2739340fc72SEdward Tomasz Napierala FS_ACLS | FS_MULTILABEL | FS_GJOURNAL | FS_FLAGS_UPDATED |
274068beacfSKirk McKusick FS_NFS4ACLS | FS_SUJ | FS_TRIM | FS_INDEXDIRS);
275bbef019cSNate Lawson if (fsflags != 0)
276068beacfSKirk McKusick printf("unknown-flags (%#x)", fsflags);
27756d253f6SMike Pritchard putchar('\n');
278068beacfSKirk McKusick if (afs.fs_flags & FS_METACKHASH) {
27975e3597aSKirk McKusick printf("check hashes\t");
28075e3597aSKirk McKusick fsflags = afs.fs_metackhash;
28175e3597aSKirk McKusick if (fsflags == 0)
28275e3597aSKirk McKusick printf("none");
28375e3597aSKirk McKusick if (fsflags & CK_SUPERBLOCK)
28475e3597aSKirk McKusick printf("superblock ");
28575e3597aSKirk McKusick if (fsflags & CK_CYLGRP)
28675e3597aSKirk McKusick printf("cylinder-groups ");
28775e3597aSKirk McKusick if (fsflags & CK_INODE)
28875e3597aSKirk McKusick printf("inodes ");
28975e3597aSKirk McKusick if (fsflags & CK_INDIR)
29075e3597aSKirk McKusick printf("indirect-blocks ");
29175e3597aSKirk McKusick if (fsflags & CK_DIR)
29275e3597aSKirk McKusick printf("directories ");
293068beacfSKirk McKusick }
29475e3597aSKirk McKusick fsflags &= ~(CK_SUPERBLOCK | CK_CYLGRP | CK_INODE | CK_INDIR | CK_DIR);
29575e3597aSKirk McKusick if (fsflags != 0)
29675e3597aSKirk McKusick printf("unknown flags (%#x)", fsflags);
29775e3597aSKirk McKusick putchar('\n');
298c715b047SGordon Tetlow printf("fsmnt\t%s\n", afs.fs_fsmnt);
299549f62faSEdward Tomasz Napierala printf("volname\t%s\tswuid\t%ju\tprovidersize\t%ju\n",
300549f62faSEdward Tomasz Napierala afs.fs_volname, (uintmax_t)afs.fs_swuid,
301549f62faSEdward Tomasz Napierala (uintmax_t)afs.fs_providersize);
3028fae3551SRodney W. Grimes printf("\ncs[].cs_(nbfree,ndir,nifree,nffree):\n\t");
3038fae3551SRodney W. Grimes for (i = 0; i < afs.fs_ncg; i++) {
3048fae3551SRodney W. Grimes struct csum *cs = &afs.fs_cs(&afs, i);
3058fae3551SRodney W. Grimes if (i && i % 4 == 0)
3068fae3551SRodney W. Grimes printf("\n\t");
3078fae3551SRodney W. Grimes printf("(%d,%d,%d,%d) ",
3088fae3551SRodney W. Grimes cs->cs_nbfree, cs->cs_ndir, cs->cs_nifree, cs->cs_nffree);
3098fae3551SRodney W. Grimes }
3108fae3551SRodney W. Grimes printf("\n");
3111c85e6a3SKirk McKusick if (fssize % afs.fs_fpg) {
31260b9ade0SJuli Mallett if (disk.d_ufs == 1)
3138fae3551SRodney W. Grimes printf("cylinders in last group %d\n",
3141c85e6a3SKirk McKusick howmany(afs.fs_old_size % afs.fs_fpg,
3151c85e6a3SKirk McKusick afs.fs_old_spc / afs.fs_old_nspf));
3162a6b42f1SJuli Mallett printf("blocks in last group %ld\n\n",
3172a6b42f1SJuli Mallett (long)((fssize % afs.fs_fpg) / afs.fs_frag));
3188fae3551SRodney W. Grimes }
319dc354845SRobert Wing if (dosb)
320dc354845SRobert Wing return (0);
321*02b7bf07SKirk McKusick ret = 0;
322bc82f903SJuli Mallett while ((i = cgread(&disk)) != 0) {
323*02b7bf07SKirk McKusick if (i == -1) {
324*02b7bf07SKirk McKusick ret = 1;
325*02b7bf07SKirk McKusick printf("\ncg %d: %s\n", disk.d_lcg, ufserr());
326*02b7bf07SKirk McKusick } else if (dumpcg())
327*02b7bf07SKirk McKusick ret = 1;
328bc82f903SJuli Mallett }
329*02b7bf07SKirk McKusick return (ret);
330a1baa513SJuli Mallett }
3318fae3551SRodney W. Grimes
3322162cff1SEd Schouten static int
dumpcg(void)333bc82f903SJuli Mallett dumpcg(void)
3348fae3551SRodney W. Grimes {
3352a6b42f1SJuli Mallett time_t cgtime;
3368fae3551SRodney W. Grimes off_t cur;
3378fae3551SRodney W. Grimes int i, j;
3388fae3551SRodney W. Grimes
339bc82f903SJuli Mallett printf("\ncg %d:\n", disk.d_lcg);
340bc82f903SJuli Mallett cur = fsbtodb(&afs, cgtod(&afs, disk.d_lcg)) * disk.d_bsize;
34160b9ade0SJuli Mallett switch (disk.d_ufs) {
34260b9ade0SJuli Mallett case 2:
3432a6b42f1SJuli Mallett cgtime = acg.cg_time;
344657c605cSJohan Karlsson printf("magic\t%x\ttell\t%jx\ttime\t%s",
345657c605cSJohan Karlsson acg.cg_magic, (intmax_t)cur, ctime(&cgtime));
3465a9dc069SPawel Jakub Dawidek printf("cgx\t%d\tndblk\t%d\tniblk\t%d\tinitiblk %d\tunrefs %d\n",
3475a9dc069SPawel Jakub Dawidek acg.cg_cgx, acg.cg_ndblk, acg.cg_niblk, acg.cg_initediblk,
3485a9dc069SPawel Jakub Dawidek acg.cg_unrefs);
34960b9ade0SJuli Mallett break;
35060b9ade0SJuli Mallett case 1:
3512a6b42f1SJuli Mallett cgtime = acg.cg_old_time;
352657c605cSJohan Karlsson printf("magic\t%x\ttell\t%jx\ttime\t%s",
353657c605cSJohan Karlsson acg.cg_magic, (intmax_t)cur, ctime(&cgtime));
3548fae3551SRodney W. Grimes printf("cgx\t%d\tncyl\t%d\tniblk\t%d\tndblk\t%d\n",
3551c85e6a3SKirk McKusick acg.cg_cgx, acg.cg_old_ncyl, acg.cg_old_niblk,
3561c85e6a3SKirk McKusick acg.cg_ndblk);
35760b9ade0SJuli Mallett break;
35860b9ade0SJuli Mallett default:
35960b9ade0SJuli Mallett break;
3601c85e6a3SKirk McKusick }
3618fae3551SRodney W. Grimes printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
3628fae3551SRodney W. Grimes acg.cg_cs.cs_nbfree, acg.cg_cs.cs_ndir,
3638fae3551SRodney W. Grimes acg.cg_cs.cs_nifree, acg.cg_cs.cs_nffree);
3648fae3551SRodney W. Grimes printf("rotor\t%d\tirotor\t%d\tfrotor\t%d\nfrsum",
3658fae3551SRodney W. Grimes acg.cg_rotor, acg.cg_irotor, acg.cg_frotor);
3668fae3551SRodney W. Grimes for (i = 1, j = 0; i < afs.fs_frag; i++) {
3678fae3551SRodney W. Grimes printf("\t%d", acg.cg_frsum[i]);
3688fae3551SRodney W. Grimes j += i * acg.cg_frsum[i];
3698fae3551SRodney W. Grimes }
3708fae3551SRodney W. Grimes printf("\nsum of frsum: %d", j);
3718fae3551SRodney W. Grimes if (afs.fs_contigsumsize > 0) {
3728fae3551SRodney W. Grimes for (i = 1; i < afs.fs_contigsumsize; i++) {
3738fae3551SRodney W. Grimes if ((i - 1) % 8 == 0)
3748fae3551SRodney W. Grimes printf("\nclusters %d-%d:", i,
3752c953857SMarcelo Araujo MIN(afs.fs_contigsumsize - 1, i + 7));
3768fae3551SRodney W. Grimes printf("\t%d", cg_clustersum(&acg)[i]);
3778fae3551SRodney W. Grimes }
3788fae3551SRodney W. Grimes printf("\nclusters size %d and over: %d\n",
3798fae3551SRodney W. Grimes afs.fs_contigsumsize,
3808fae3551SRodney W. Grimes cg_clustersum(&acg)[afs.fs_contigsumsize]);
3818fae3551SRodney W. Grimes printf("clusters free:\t");
3828fae3551SRodney W. Grimes pbits(cg_clustersfree(&acg), acg.cg_nclusterblks);
3838fae3551SRodney W. Grimes } else
3848fae3551SRodney W. Grimes printf("\n");
3851c85e6a3SKirk McKusick printf("inodes used:\t");
3868fae3551SRodney W. Grimes pbits(cg_inosused(&acg), afs.fs_ipg);
3871c85e6a3SKirk McKusick printf("blks free:\t");
3888fae3551SRodney W. Grimes pbits(cg_blksfree(&acg), afs.fs_fpg);
3898fae3551SRodney W. Grimes return (0);
390a1baa513SJuli Mallett }
3918fae3551SRodney W. Grimes
3922162cff1SEd Schouten static int
dumpfreespace(const char * name,int fflag)393fd15d586SRobert Watson dumpfreespace(const char *name, int fflag)
394fd15d586SRobert Watson {
395*02b7bf07SKirk McKusick intmax_t startblkno;
396*02b7bf07SKirk McKusick int i, ret;
397fd15d586SRobert Watson
398*02b7bf07SKirk McKusick ret = 0;
399fd15d586SRobert Watson while ((i = cgread(&disk)) != 0) {
400*02b7bf07SKirk McKusick if (i != -1) {
401fd15d586SRobert Watson dumpfreespacecg(fflag);
402*02b7bf07SKirk McKusick } else {
403*02b7bf07SKirk McKusick startblkno = disk.d_lcg * afs.fs_fpg;
404*02b7bf07SKirk McKusick printf("\nBlocks %jd-%jd of cg %d skipped: %s\n",
405*02b7bf07SKirk McKusick startblkno, startblkno + afs.fs_fpg - 1,
406*02b7bf07SKirk McKusick disk.d_lcg, ufserr());
407*02b7bf07SKirk McKusick ret = 1;
408fd15d586SRobert Watson }
409*02b7bf07SKirk McKusick }
410*02b7bf07SKirk McKusick return (ret);
411fd15d586SRobert Watson }
412fd15d586SRobert Watson
4132162cff1SEd Schouten static void
dumpfreespacecg(int fflag)414fd15d586SRobert Watson dumpfreespacecg(int fflag)
415fd15d586SRobert Watson {
416fd15d586SRobert Watson
417fd15d586SRobert Watson pblklist(cg_blksfree(&acg), afs.fs_fpg, disk.d_lcg * afs.fs_fpg,
418fd15d586SRobert Watson fflag);
419fd15d586SRobert Watson }
420fd15d586SRobert Watson
4212162cff1SEd Schouten static int
marshal(const char * name)42287d35aadSJuli Mallett marshal(const char *name)
42387d35aadSJuli Mallett {
42487d35aadSJuli Mallett struct fs *fs;
42587d35aadSJuli Mallett
42687d35aadSJuli Mallett fs = &disk.d_fs;
42787d35aadSJuli Mallett
42887d35aadSJuli Mallett printf("# newfs command for %s (%s)\n", name, disk.d_name);
42987d35aadSJuli Mallett printf("newfs ");
430feff4d85SJuli Mallett if (fs->fs_volname[0] != '\0')
431feff4d85SJuli Mallett printf("-L %s ", fs->fs_volname);
43287d35aadSJuli Mallett printf("-O %d ", disk.d_ufs);
43387d35aadSJuli Mallett if (fs->fs_flags & FS_DOSOFTDEP)
43487d35aadSJuli Mallett printf("-U ");
43587d35aadSJuli Mallett printf("-a %d ", fs->fs_maxcontig);
43687d35aadSJuli Mallett printf("-b %d ", fs->fs_bsize);
43787d35aadSJuli Mallett /* -c is dumb */
43887d35aadSJuli Mallett printf("-d %d ", fs->fs_maxbsize);
43987d35aadSJuli Mallett printf("-e %d ", fs->fs_maxbpg);
44087d35aadSJuli Mallett printf("-f %d ", fs->fs_fsize);
44187d35aadSJuli Mallett printf("-g %d ", fs->fs_avgfilesize);
44287d35aadSJuli Mallett printf("-h %d ", fs->fs_avgfpdir);
4439d384d4dSKirk McKusick printf("-i %jd ", fragroundup(fs, lblktosize(fs, fragstoblks(fs,
4449d384d4dSKirk McKusick fs->fs_fpg)) / fs->fs_ipg));
4459775d5ebSJuli Mallett if (fs->fs_flags & FS_SUJ)
4469775d5ebSJuli Mallett printf("-j ");
44773ea379eSEitan Adler if (fs->fs_flags & FS_GJOURNAL)
44873ea379eSEitan Adler printf("-J ");
4499d384d4dSKirk McKusick printf("-k %jd ", fs->fs_metaspace);
4509d384d4dSKirk McKusick if (fs->fs_flags & FS_MULTILABEL)
4519d384d4dSKirk McKusick printf("-l ");
45287d35aadSJuli Mallett printf("-m %d ", fs->fs_minfree);
45387d35aadSJuli Mallett /* -n unimplemented */
45487d35aadSJuli Mallett printf("-o ");
45587d35aadSJuli Mallett switch (fs->fs_optim) {
45687d35aadSJuli Mallett case FS_OPTSPACE:
45787d35aadSJuli Mallett printf("space ");
45887d35aadSJuli Mallett break;
45987d35aadSJuli Mallett case FS_OPTTIME:
46087d35aadSJuli Mallett printf("time ");
46187d35aadSJuli Mallett break;
46287d35aadSJuli Mallett default:
46387d35aadSJuli Mallett printf("unknown ");
46487d35aadSJuli Mallett break;
46587d35aadSJuli Mallett }
46687d35aadSJuli Mallett /* -p..r unimplemented */
467f807cca1SRuslan Ermilov printf("-s %jd ", (intmax_t)fsbtodb(fs, fs->fs_size));
4689775d5ebSJuli Mallett if (fs->fs_flags & FS_TRIM)
4699775d5ebSJuli Mallett printf("-t ");
47087d35aadSJuli Mallett printf("%s ", disk.d_name);
47187d35aadSJuli Mallett printf("\n");
47287d35aadSJuli Mallett
47387d35aadSJuli Mallett return 0;
47487d35aadSJuli Mallett }
47587d35aadSJuli Mallett
4762162cff1SEd Schouten static void
pbits(void * vp,int max)477b70cd7eeSWarner Losh pbits(void *vp, int max)
4788fae3551SRodney W. Grimes {
4793d438ad6SDavid E. O'Brien int i;
4803d438ad6SDavid E. O'Brien char *p;
4818fae3551SRodney W. Grimes int count, j;
4828fae3551SRodney W. Grimes
4838fae3551SRodney W. Grimes for (count = i = 0, p = vp; i < max; i++)
4848fae3551SRodney W. Grimes if (isset(p, i)) {
4858fae3551SRodney W. Grimes if (count)
4868fae3551SRodney W. Grimes printf(",%s", count % 6 ? " " : "\n\t");
4878fae3551SRodney W. Grimes count++;
4888fae3551SRodney W. Grimes printf("%d", i);
4898fae3551SRodney W. Grimes j = i;
4908fae3551SRodney W. Grimes while ((i+1)<max && isset(p, i+1))
4918fae3551SRodney W. Grimes i++;
4928fae3551SRodney W. Grimes if (i != j)
4938fae3551SRodney W. Grimes printf("-%d", i);
4948fae3551SRodney W. Grimes }
4958fae3551SRodney W. Grimes printf("\n");
4968fae3551SRodney W. Grimes }
4978fae3551SRodney W. Grimes
4982162cff1SEd Schouten static void
pblklist(void * vp,int max,off_t offset,int fflag)499fd15d586SRobert Watson pblklist(void *vp, int max, off_t offset, int fflag)
500fd15d586SRobert Watson {
501fd15d586SRobert Watson int i, j;
502fd15d586SRobert Watson char *p;
503fd15d586SRobert Watson
504fd15d586SRobert Watson for (i = 0, p = vp; i < max; i++) {
505fd15d586SRobert Watson if (isset(p, i)) {
506fd15d586SRobert Watson printf("%jd", (intmax_t)(i + offset));
507fd15d586SRobert Watson if (fflag < 2) {
508fd15d586SRobert Watson j = i;
509fd15d586SRobert Watson while ((i+1)<max && isset(p, i+1))
510fd15d586SRobert Watson i++;
511fd15d586SRobert Watson if (i != j)
5126458691cSRobert Watson printf("-%jd", (intmax_t)(i + offset));
513fd15d586SRobert Watson }
514fd15d586SRobert Watson printf("\n");
515fd15d586SRobert Watson }
516fd15d586SRobert Watson }
517fd15d586SRobert Watson }
518fd15d586SRobert Watson
519*02b7bf07SKirk McKusick static const char *
ufserr(void)520*02b7bf07SKirk McKusick ufserr(void)
52187d35aadSJuli Mallett {
52287d35aadSJuli Mallett if (disk.d_error != NULL)
523*02b7bf07SKirk McKusick return(disk.d_error);
524*02b7bf07SKirk McKusick if (errno)
525*02b7bf07SKirk McKusick return (strerror(errno));
526*02b7bf07SKirk McKusick return ("unknown error");
52787d35aadSJuli Mallett }
52887d35aadSJuli Mallett
5292162cff1SEd Schouten static void
usage(void)530b70cd7eeSWarner Losh usage(void)
5318fae3551SRodney W. Grimes {
5324ff79fdfSChristian Brueffer (void)fprintf(stderr, "usage: dumpfs [-flm] filesys | device\n");
5338fae3551SRodney W. Grimes exit(1);
5348fae3551SRodney W. Grimes }
535