dumpfs.c revision 109510
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 47static 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 54static char sccsid[] = "@(#)dumpfs.c 8.5 (Berkeley) 4/29/95"; 55#endif 56static const char rcsid[] = 57 "$FreeBSD: head/sbin/dumpfs/dumpfs.c 109510 2003-01-19 01:31:49Z jmallett $"; 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 <stdio.h> 73#include <stdlib.h> 74#include <unistd.h> 75 76#define afs disk.d_fs 77#define acg disk.d_cg 78 79struct uufsd disk; 80 81int dumpfs(const char *); 82int dumpcg(int); 83void pbits(void *, int); 84void usage(void) __dead2; 85 86int 87main(int argc, char *argv[]) 88{ 89 int eval; 90 91 eval = 0; 92 93 while (getopt(argc, argv, "") != -1) 94 usage(); 95 argc -= optind; 96 argv += optind; 97 98 if (argc < 1) 99 usage(); 100 101 while (*argv != NULL) 102 eval |= dumpfs(*argv++); 103 exit(eval); 104} 105 106int 107dumpfs(const char *name) 108{ 109 time_t time; 110 int64_t fssize; 111 int i; 112 113 if (ufs_disk_fillout(&disk, name) == -1) 114 goto err; 115 116 switch (disk.d_ufs) { 117 case 2: 118 fssize = afs.fs_size; 119 time = afs.fs_time; 120 printf("magic\t%x (UFS2)\ttime\t%s", 121 afs.fs_magic, ctime(&time)); 122 printf("superblock location\t%qd\tid\t[ %x %x ]\n", 123 afs.fs_sblockloc, afs.fs_id[0], afs.fs_id[1]); 124 printf("ncg\t%d\tsize\t%qd\tblocks\t%d\n", 125 afs.fs_ncg, fssize, afs.fs_dsize); 126 break; 127 case 1: 128 fssize = afs.fs_old_size; 129 time = afs.fs_old_time; 130 printf("magic\t%x (UFS1)\ttime\t%s", 131 afs.fs_magic, ctime(&time)); 132 printf("id\t[ %x %x ]\n", afs.fs_id[0], afs.fs_id[1]); 133 printf("ncg\t%d\tsize\t%qd\tblocks\t%d\n", 134 afs.fs_ncg, fssize, afs.fs_dsize); 135 break; 136 default: 137 break; 138 } 139 printf("bsize\t%d\tshift\t%d\tmask\t0x%08x\n", 140 afs.fs_bsize, afs.fs_bshift, afs.fs_bmask); 141 printf("fsize\t%d\tshift\t%d\tmask\t0x%08x\n", 142 afs.fs_fsize, afs.fs_fshift, afs.fs_fmask); 143 printf("frag\t%d\tshift\t%d\tfsbtodb\t%d\n", 144 afs.fs_frag, afs.fs_fragshift, afs.fs_fsbtodb); 145 printf("minfree\t%d%%\toptim\t%s\tsymlinklen %d\n", 146 afs.fs_minfree, afs.fs_optim == FS_OPTSPACE ? "space" : "time", 147 afs.fs_maxsymlinklen); 148 switch (disk.d_ufs) { 149 case 2: 150 printf("%s %d\tmaxbpg\t%d\tmaxcontig %d\tcontigsumsize %d\n", 151 "maxbsize", afs.fs_maxbsize, afs.fs_maxbpg, 152 afs.fs_maxcontig, afs.fs_contigsumsize); 153 printf("nbfree\t%qd\tndir\t%qd\tnifree\t%qd\tnffree\t%qd\n", 154 afs.fs_cstotal.cs_nbfree, afs.fs_cstotal.cs_ndir, 155 afs.fs_cstotal.cs_nifree, afs.fs_cstotal.cs_nffree); 156 printf("bpg\t%d\tfpg\t%d\tipg\t%d\n", 157 afs.fs_fpg / afs.fs_frag, afs.fs_fpg, afs.fs_ipg); 158 printf("nindir\t%d\tinopb\t%d\tmaxfilesize\t%qu\n", 159 afs.fs_nindir, afs.fs_inopb, afs.fs_maxfilesize); 160 printf("sbsize\t%d\tcgsize\t%d\tcsaddr\t%d\tcssize\t%d\n", 161 afs.fs_sbsize, afs.fs_cgsize, afs.fs_csaddr, afs.fs_cssize); 162 break; 163 case 1: 164 printf("maxbpg\t%d\tmaxcontig %d\tcontigsumsize %d\n", 165 afs.fs_maxbpg, afs.fs_maxcontig, afs.fs_contigsumsize); 166 printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n", 167 afs.fs_old_cstotal.cs_nbfree, afs.fs_old_cstotal.cs_ndir, 168 afs.fs_old_cstotal.cs_nifree, afs.fs_old_cstotal.cs_nffree); 169 printf("cpg\t%d\tbpg\t%d\tfpg\t%d\tipg\t%d\n", 170 afs.fs_old_cpg, afs.fs_fpg / afs.fs_frag, afs.fs_fpg, 171 afs.fs_ipg); 172 printf("nindir\t%d\tinopb\t%d\tnspf\t%d\tmaxfilesize\t%qu\n", 173 afs.fs_nindir, afs.fs_inopb, afs.fs_old_nspf, 174 afs.fs_maxfilesize); 175 printf("sbsize\t%d\tcgsize\t%d\tcgoffset %d\tcgmask\t0x%08x\n", 176 afs.fs_sbsize, afs.fs_cgsize, afs.fs_old_cgoffset, 177 afs.fs_old_cgmask); 178 printf("csaddr\t%d\tcssize\t%d\n", 179 afs.fs_old_csaddr, afs.fs_cssize); 180 printf("rotdelay %dms\trps\t%d\ttrackskew %d\tinterleave %d\n", 181 afs.fs_old_rotdelay, afs.fs_old_rps, afs.fs_old_trackskew, 182 afs.fs_old_interleave); 183 printf("nsect\t%d\tnpsect\t%d\tspc\t%d\n", 184 afs.fs_old_nsect, afs.fs_old_npsect, afs.fs_old_spc); 185 break; 186 default: 187 break; 188 } 189 printf("sblkno\t%d\tcblkno\t%d\tiblkno\t%d\tdblkno\t%d\n", 190 afs.fs_sblkno, afs.fs_cblkno, afs.fs_iblkno, afs.fs_dblkno); 191 printf("cgrotor\t%d\tfmod\t%d\tronly\t%d\tclean\t%d\n", 192 afs.fs_cgrotor, afs.fs_fmod, afs.fs_ronly, afs.fs_clean); 193 printf("flags\t"); 194 if (afs.fs_flags == 0) 195 printf("none"); 196 if (afs.fs_flags & FS_UNCLEAN) 197 printf("unclean "); 198 if (afs.fs_flags & FS_DOSOFTDEP) 199 printf("soft-updates "); 200 if (afs.fs_flags & FS_NEEDSFSCK) 201 printf("needs fsck run "); 202 if (afs.fs_flags & FS_INDEXDIRS) 203 printf("indexed directories "); 204 if ((afs.fs_flags & 205 ~(FS_UNCLEAN | FS_DOSOFTDEP | FS_NEEDSFSCK | FS_INDEXDIRS)) != 0) 206 printf("unknown flags (%#x)", afs.fs_flags & 207 ~(FS_UNCLEAN | FS_DOSOFTDEP | 208 FS_NEEDSFSCK | FS_INDEXDIRS)); 209 putchar('\n'); 210 printf("\ncs[].cs_(nbfree,ndir,nifree,nffree):\n\t"); 211 afs.fs_csp = calloc(1, afs.fs_cssize); 212 if (bread(&disk, fsbtodb(&afs, afs.fs_csaddr), afs.fs_csp, afs.fs_cssize) == -1) 213 goto err; 214 for (i = 0; i < afs.fs_ncg; i++) { 215 struct csum *cs = &afs.fs_cs(&afs, i); 216 if (i && i % 4 == 0) 217 printf("\n\t"); 218 printf("(%d,%d,%d,%d) ", 219 cs->cs_nbfree, cs->cs_ndir, cs->cs_nifree, cs->cs_nffree); 220 } 221 printf("\n"); 222 if (fssize % afs.fs_fpg) { 223 if (disk.d_ufs == 1) 224 printf("cylinders in last group %d\n", 225 howmany(afs.fs_old_size % afs.fs_fpg, 226 afs.fs_old_spc / afs.fs_old_nspf)); 227 printf("blocks in last group %d\n\n", 228 (fssize % afs.fs_fpg) / afs.fs_frag); 229 } 230 for (i = 0; i < afs.fs_ncg; i++) 231 if (dumpcg(i)) 232 goto err; 233 ufs_disk_close(&disk); 234 return (0); 235 236err: if (disk.d_error != NULL) 237 warnx("%s: %s", name, disk.d_error); 238 else if (errno) 239 warn("%s", name); 240 ufs_disk_close(&disk); 241 return (1); 242} 243 244int 245dumpcg(int c) 246{ 247 time_t time; 248 off_t cur; 249 int i, j; 250 251 printf("\ncg %d:\n", c); 252 cur = fsbtodb(&afs, cgtod(&afs, c)) * disk.d_bsize; 253 if (cgread1(&disk, c) != 1) 254 return (1); 255 switch (disk.d_ufs) { 256 case 2: 257 time = acg.cg_time; 258 printf("magic\t%x\ttell\t%qx\ttime\t%s", 259 acg.cg_magic, cur, ctime(&time)); 260 printf("cgx\t%d\tndblk\t%d\tniblk\t%d\tinitiblk %d\n", 261 acg.cg_cgx, acg.cg_ndblk, acg.cg_niblk, acg.cg_initediblk); 262 break; 263 case 1: 264 time = acg.cg_old_time; 265 printf("magic\t%x\ttell\t%qx\ttime\t%s", 266 acg.cg_magic, cur, ctime(&time)); 267 printf("cgx\t%d\tncyl\t%d\tniblk\t%d\tndblk\t%d\n", 268 acg.cg_cgx, acg.cg_old_ncyl, acg.cg_old_niblk, 269 acg.cg_ndblk); 270 break; 271 default: 272 break; 273 } 274 printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n", 275 acg.cg_cs.cs_nbfree, acg.cg_cs.cs_ndir, 276 acg.cg_cs.cs_nifree, acg.cg_cs.cs_nffree); 277 printf("rotor\t%d\tirotor\t%d\tfrotor\t%d\nfrsum", 278 acg.cg_rotor, acg.cg_irotor, acg.cg_frotor); 279 for (i = 1, j = 0; i < afs.fs_frag; i++) { 280 printf("\t%d", acg.cg_frsum[i]); 281 j += i * acg.cg_frsum[i]; 282 } 283 printf("\nsum of frsum: %d", j); 284 if (afs.fs_contigsumsize > 0) { 285 for (i = 1; i < afs.fs_contigsumsize; i++) { 286 if ((i - 1) % 8 == 0) 287 printf("\nclusters %d-%d:", i, 288 afs.fs_contigsumsize - 1 < i + 7 ? 289 afs.fs_contigsumsize - 1 : i + 7); 290 printf("\t%d", cg_clustersum(&acg)[i]); 291 } 292 printf("\nclusters size %d and over: %d\n", 293 afs.fs_contigsumsize, 294 cg_clustersum(&acg)[afs.fs_contigsumsize]); 295 printf("clusters free:\t"); 296 pbits(cg_clustersfree(&acg), acg.cg_nclusterblks); 297 } else 298 printf("\n"); 299 printf("inodes used:\t"); 300 pbits(cg_inosused(&acg), afs.fs_ipg); 301 printf("blks free:\t"); 302 pbits(cg_blksfree(&acg), afs.fs_fpg); 303 return (0); 304} 305 306void 307pbits(void *vp, int max) 308{ 309 int i; 310 char *p; 311 int count, j; 312 313 for (count = i = 0, p = vp; i < max; i++) 314 if (isset(p, i)) { 315 if (count) 316 printf(",%s", count % 6 ? " " : "\n\t"); 317 count++; 318 printf("%d", i); 319 j = i; 320 while ((i+1)<max && isset(p, i+1)) 321 i++; 322 if (i != j) 323 printf("-%d", i); 324 } 325 printf("\n"); 326} 327 328void 329usage(void) 330{ 331 (void)fprintf(stderr, "usage: dumpfs filesys | device\n"); 332 exit(1); 333} 334