1 /* $OpenBSD: dumpfs.c,v 1.18 2003/06/25 21:23:53 deraadt Exp $ */ 2 /* $NetBSD: dumpfs.c,v 1.12 1997/04/26 05:41:33 lukem Exp $ */ 3 4 /* 5 * Copyright (c) 1983, 1992, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #ifndef lint 34 static char copyright[] = 35 "@(#) Copyright (c) 1983, 1992, 1993\n\ 36 The Regents of the University of California. All rights reserved.\n"; 37 #endif /* not lint */ 38 39 #ifndef lint 40 #if 0 41 static char sccsid[] = "@(#)dumpfs.c 8.2 (Berkeley) 2/2/94"; 42 #else 43 static char rcsid[] = "$OpenBSD: dumpfs.c,v 1.18 2003/06/25 21:23:53 deraadt Exp $"; 44 #endif 45 #endif /* not lint */ 46 47 #include <sys/param.h> 48 #include <sys/time.h> 49 50 #include <ufs/ufs/dinode.h> 51 #include <ufs/ffs/fs.h> 52 53 #include <err.h> 54 #include <errno.h> 55 #include <fcntl.h> 56 #include <fstab.h> 57 #include <stdio.h> 58 #include <stdlib.h> 59 #include <string.h> 60 #include <unistd.h> 61 62 union { 63 struct fs fs; 64 char pad[MAXBSIZE]; 65 } fsun; 66 #define afs fsun.fs 67 68 union { 69 struct cg cg; 70 char pad[MAXBSIZE]; 71 } cgun; 72 #define acg cgun.cg 73 74 long dev_bsize = 1; 75 76 int dumpfs(char *); 77 int dumpcg(char *, int, int); 78 void pbits(void *, int); 79 void usage(void); 80 81 int 82 main(int argc, char *argv[]) 83 { 84 struct fstab *fs; 85 int ch, eval; 86 87 while ((ch = getopt(argc, argv, "")) != -1) 88 switch(ch) { 89 case '?': 90 default: 91 usage(); 92 } 93 argc -= optind; 94 argv += optind; 95 96 if (argc < 1) 97 usage(); 98 99 for (eval = 0; *argv; ++argv) 100 if ((fs = getfsfile(*argv)) == NULL) 101 eval |= dumpfs(*argv); 102 else 103 eval |= dumpfs(fs->fs_spec); 104 exit(eval); 105 } 106 107 int 108 dumpfs(char *name) 109 { 110 ssize_t n; 111 int fd, c, i, j, k, size; 112 113 if ((fd = open(name, O_RDONLY, 0)) < 0) 114 goto err; 115 if (lseek(fd, (off_t)SBOFF, SEEK_SET) == (off_t)-1) 116 goto err; 117 if ((n = read(fd, &afs, SBSIZE)) == -1) 118 goto err; 119 120 if (n != SBSIZE) { 121 warnx("%s: non-existent or truncated superblock, skipped", 122 name); 123 (void)close(fd); 124 return (1); 125 } 126 if (afs.fs_magic != FS_MAGIC) { 127 warnx("%s: superblock has bad magic number, skipping.", name); 128 (void) close(fd); 129 return (1); 130 } 131 132 if (afs.fs_postblformat == FS_42POSTBLFMT) 133 afs.fs_nrpos = 8; 134 dev_bsize = afs.fs_fsize / fsbtodb(&afs, 1); 135 printf("magic\t%x\ttime\t%s", afs.fs_magic, 136 ctime(&afs.fs_time)); 137 i = 0; 138 if (afs.fs_postblformat != FS_42POSTBLFMT) { 139 i++; 140 if (afs.fs_inodefmt >= FS_44INODEFMT) { 141 int max; 142 143 i++; 144 max = afs.fs_maxcontig; 145 size = afs.fs_contigsumsize; 146 if ((max < 2 && size == 0) 147 || (max > 1 && size >= MIN(max, FS_MAXCONTIG))) 148 i++; 149 } 150 } 151 printf("cylgrp\t%s\tinodes\t%s\tfslevel %d%s\n", 152 i < 1 ? "static" : "dynamic", i < 2 ? "4.2/4.3BSD" : "4.4BSD", i, 153 (afs.fs_flags & FS_DOSOFTDEP) ? "\tsoft updates" : ""); 154 printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n", 155 afs.fs_cstotal.cs_nbfree, afs.fs_cstotal.cs_ndir, 156 afs.fs_cstotal.cs_nifree, afs.fs_cstotal.cs_nffree); 157 printf("ncg\t%d\tncyl\t%d\tsize\t%d\tblocks\t%d\n", 158 afs.fs_ncg, afs.fs_ncyl, afs.fs_size, afs.fs_dsize); 159 printf("bsize\t%d\tshift\t%d\tmask\t0x%08x\n", 160 afs.fs_bsize, afs.fs_bshift, afs.fs_bmask); 161 printf("fsize\t%d\tshift\t%d\tmask\t0x%08x\n", 162 afs.fs_fsize, afs.fs_fshift, afs.fs_fmask); 163 printf("frag\t%d\tshift\t%d\tfsbtodb\t%d\n", 164 afs.fs_frag, afs.fs_fragshift, afs.fs_fsbtodb); 165 printf("cpg\t%d\tbpg\t%d\tfpg\t%d\tipg\t%d\n", 166 afs.fs_cpg, afs.fs_fpg / afs.fs_frag, afs.fs_fpg, afs.fs_ipg); 167 printf("minfree\t%d%%\toptim\t%s\tmaxcontig %d\tmaxbpg\t%d\n", 168 afs.fs_minfree, afs.fs_optim == FS_OPTSPACE ? "space" : "time", 169 afs.fs_maxcontig, afs.fs_maxbpg); 170 printf("rotdelay %dms\trps\t%d\n", afs.fs_rotdelay, afs.fs_rps); 171 printf("ntrak\t%d\tnsect\t%d\tnpsect\t%d\tspc\t%d\n", 172 afs.fs_ntrak, afs.fs_nsect, afs.fs_npsect, afs.fs_spc); 173 printf("symlinklen %d\ttrackskew %d\tinterleave %d\tcontigsumsize %d\n", 174 afs.fs_maxsymlinklen, afs.fs_trackskew, afs.fs_interleave, 175 afs.fs_contigsumsize); 176 printf("nindir\t%d\tinopb\t%d\tnspf\t%d\tmaxfilesize\t%qu\n", 177 afs.fs_nindir, afs.fs_inopb, afs.fs_nspf, afs.fs_maxfilesize); 178 printf("sblkno\t%d\tcblkno\t%d\tiblkno\t%d\tdblkno\t%d\n", 179 afs.fs_sblkno, afs.fs_cblkno, afs.fs_iblkno, afs.fs_dblkno); 180 printf("sbsize\t%d\tcgsize\t%d\tcgoffset %d\tcgmask\t0x%08x\n", 181 afs.fs_sbsize, afs.fs_cgsize, afs.fs_cgoffset, afs.fs_cgmask); 182 printf("csaddr\t%d\tcssize\t%d\tshift\t%d\tmask\t0x%08x\n", 183 afs.fs_csaddr, afs.fs_cssize, afs.fs_csshift, afs.fs_csmask); 184 printf("cgrotor\t%d\tfmod\t%d\tronly\t%d\tclean\t0x%02x\n", 185 afs.fs_cgrotor, afs.fs_fmod, afs.fs_ronly, afs.fs_clean); 186 printf("flags\t"); 187 if (afs.fs_flags == 0) 188 printf("none"); 189 if (afs.fs_flags & FS_UNCLEAN) 190 printf("unclean "); 191 if (afs.fs_flags & FS_DOSOFTDEP) 192 printf("soft-updates "); 193 if ((afs.fs_flags & ~(FS_UNCLEAN | FS_DOSOFTDEP)) != 0) 194 printf("unknown flags (%#x)", 195 afs.fs_flags & ~(FS_UNCLEAN | FS_DOSOFTDEP)); 196 printf("\n"); 197 if (afs.fs_cpc != 0) 198 printf("blocks available in each of %d rotational positions", 199 afs.fs_nrpos); 200 else 201 printf("insufficient space to maintain rotational tables\n"); 202 for (c = 0; c < afs.fs_cpc; c++) { 203 printf("\ncylinder number %d:", c); 204 for (i = 0; i < afs.fs_nrpos; i++) { 205 if (fs_postbl(&afs, c)[i] == -1) 206 continue; 207 printf("\n position %d:\t", i); 208 for (j = fs_postbl(&afs, c)[i], k = 1; ; 209 j += fs_rotbl(&afs)[j], k++) { 210 printf("%5d", j); 211 if (k % 12 == 0) 212 printf("\n\t\t"); 213 if (fs_rotbl(&afs)[j] == 0) 214 break; 215 } 216 } 217 } 218 printf("\ncs[].cs_(nbfree,ndir,nifree,nffree):\n\t"); 219 afs.fs_csp = calloc(1, afs.fs_cssize); 220 for (i = 0, j = 0; i < afs.fs_cssize; i += afs.fs_bsize, j++) { 221 size = afs.fs_cssize - i < afs.fs_bsize ? 222 afs.fs_cssize - i : afs.fs_bsize; 223 if (lseek(fd, 224 (off_t)(fsbtodb(&afs, (afs.fs_csaddr + j * afs.fs_frag))) * 225 dev_bsize, SEEK_SET) == (off_t)-1) 226 goto err; 227 if (read(fd, (char *)afs.fs_csp + i, size) != size) 228 goto err; 229 } 230 for (i = 0; i < afs.fs_ncg; i++) { 231 struct csum *cs = &afs.fs_cs(&afs, i); 232 if (i && i % 4 == 0) 233 printf("\n\t"); 234 printf("(%d,%d,%d,%d) ", 235 cs->cs_nbfree, cs->cs_ndir, cs->cs_nifree, cs->cs_nffree); 236 } 237 printf("\n"); 238 if (afs.fs_ncyl % afs.fs_cpg) { 239 printf("cylinders in last group %d\n", 240 i = afs.fs_ncyl % afs.fs_cpg); 241 printf("blocks in last group %d\n", 242 i * afs.fs_spc / NSPB(&afs)); 243 } 244 printf("\n"); 245 for (i = 0; i < afs.fs_ncg; i++) 246 if (dumpcg(name, fd, i)) 247 goto err; 248 (void)close(fd); 249 return (0); 250 251 err: if (fd != -1) 252 (void)close(fd); 253 warn("%s", name); 254 return (1); 255 } 256 257 int 258 dumpcg(char *name, int fd, int c) 259 { 260 off_t cur; 261 int i, j; 262 263 printf("\ncg %d:\n", c); 264 if ((cur = lseek(fd, (off_t)(fsbtodb(&afs, cgtod(&afs, c))) * dev_bsize, 265 SEEK_SET)) == (off_t)-1) 266 return (1); 267 if (read(fd, &acg, afs.fs_bsize) != afs.fs_bsize) { 268 warnx("%s: error reading cg", name); 269 return (1); 270 } 271 printf("magic\t%x\ttell\t%qx\ttime\t%s", 272 afs.fs_postblformat == FS_42POSTBLFMT ? 273 ((struct ocg *)&acg)->cg_magic : acg.cg_magic, 274 cur, ctime(&acg.cg_time)); 275 printf("cgx\t%d\tncyl\t%d\tniblk\t%d\tndblk\t%d\n", 276 acg.cg_cgx, acg.cg_ncyl, acg.cg_niblk, acg.cg_ndblk); 277 printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n", 278 acg.cg_cs.cs_nbfree, acg.cg_cs.cs_ndir, 279 acg.cg_cs.cs_nifree, acg.cg_cs.cs_nffree); 280 printf("rotor\t%d\tirotor\t%d\tfrotor\t%d\nfrsum", 281 acg.cg_rotor, acg.cg_irotor, acg.cg_frotor); 282 for (i = 1, j = 0; i < afs.fs_frag; i++) { 283 printf("\t%d", acg.cg_frsum[i]); 284 j += i * acg.cg_frsum[i]; 285 } 286 printf("\nsum of frsum: %d", j); 287 if (afs.fs_contigsumsize > 0) { 288 for (i = 1; i < afs.fs_contigsumsize; i++) { 289 if ((i - 1) % 8 == 0) 290 printf("\nclusters %d-%d:", i, 291 afs.fs_contigsumsize - 1 < i + 7 ? 292 afs.fs_contigsumsize - 1 : i + 7); 293 printf("\t%d", cg_clustersum(&acg)[i]); 294 } 295 printf("\nclusters size %d and over: %d\n", 296 afs.fs_contigsumsize, 297 cg_clustersum(&acg)[afs.fs_contigsumsize]); 298 printf("clusters free:\t"); 299 pbits(cg_clustersfree(&acg), acg.cg_nclusterblks); 300 } else 301 printf("\n"); 302 printf("iused:\t"); 303 pbits(cg_inosused(&acg), afs.fs_ipg); 304 printf("free:\t"); 305 pbits(cg_blksfree(&acg), afs.fs_fpg); 306 printf("b:\n"); 307 for (i = 0; i < afs.fs_cpg; i++) { 308 if (cg_blktot(&acg)[i] == 0) 309 continue; 310 printf(" c%d:\t(%d)\t", i, cg_blktot(&acg)[i]); 311 for (j = 0; j < afs.fs_nrpos; j++) { 312 if (afs.fs_cpc > 0 && 313 fs_postbl(&afs, i % afs.fs_cpc)[j] == -1) 314 continue; 315 printf(" %d", cg_blks(&afs, &acg, i)[j]); 316 } 317 printf("\n"); 318 } 319 return (0); 320 } 321 322 void 323 pbits(void *vp, int max) 324 { 325 int i; 326 char *p; 327 int count, j; 328 329 for (count = i = 0, p = vp; i < max; i++) 330 if (isset(p, i)) { 331 if (count) 332 printf(",%s", count % 6 ? " " : "\n\t"); 333 count++; 334 printf("%d", i); 335 j = i; 336 while ((i+1)<max && isset(p, i+1)) 337 i++; 338 if (i != j) 339 printf("-%d", i); 340 } 341 printf("\n"); 342 } 343 344 void 345 usage(void) 346 { 347 348 (void)fprintf(stderr, "usage: dumpfs filesys | device\n"); 349 exit(1); 350 } 351