1 /* $NetBSD: dumpfs.c,v 1.35 2003/08/07 11:25:19 agc Exp $ */ 2 3 /* 4 * Copyright (c) 1983, 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #ifndef lint 34 __COPYRIGHT("@(#) Copyright (c) 1983, 1992, 1993\n\ 35 The Regents of the University of California. All rights reserved.\n"); 36 #endif /* not lint */ 37 38 #ifndef lint 39 #if 0 40 static char sccsid[] = "@(#)dumpfs.c 8.5 (Berkeley) 4/29/95"; 41 #else 42 __RCSID("$NetBSD: dumpfs.c,v 1.35 2003/08/07 11:25:19 agc Exp $"); 43 #endif 44 #endif /* not lint */ 45 46 #include <sys/param.h> 47 #include <sys/time.h> 48 49 #include <ufs/ufs/dinode.h> 50 #include <ufs/ufs/ufs_bswap.h> 51 #include <ufs/ffs/fs.h> 52 #include <ufs/ffs/ffs_extern.h> 53 54 #include <err.h> 55 #include <errno.h> 56 #include <fcntl.h> 57 #include <fstab.h> 58 #include <stdio.h> 59 #include <stdlib.h> 60 #include <string.h> 61 #include <unistd.h> 62 #include <util.h> 63 64 union { 65 struct fs fs; 66 char pad[MAXBSIZE]; 67 } fsun; 68 #define afs fsun.fs 69 70 union { 71 struct cg cg; 72 char pad[MAXBSIZE]; 73 } cgun; 74 #define acg cgun.cg 75 76 long dev_bsize = 1; 77 int needswap, Fflag; 78 int is_ufs2; 79 80 int dumpfs(const char *); 81 int dumpcg(const char *, int, int); 82 int main(int, char **); 83 int openpartition(const char *, int, char *, size_t); 84 void pbits(void *, int); 85 void usage(void); 86 void swap_cg(struct cg *); 87 88 int 89 main(int argc, char *argv[]) 90 { 91 int ch, eval; 92 93 while ((ch = getopt(argc, argv, "F")) != -1) 94 switch(ch) { 95 case 'F': 96 Fflag = 1; 97 break; 98 case '?': 99 default: 100 usage(); 101 } 102 argc -= optind; 103 argv += optind; 104 105 if (argc < 1) 106 usage(); 107 108 for (eval = 0; *argv; ++argv) { 109 eval |= dumpfs(*argv); 110 printf("\n"); 111 } 112 113 exit(eval); 114 } 115 116 off_t sblock_try[] = SBLOCKSEARCH; 117 118 int 119 dumpfs(const char *name) 120 { 121 int fd, i, j, size, printold; 122 time_t t; 123 char device[MAXPATHLEN]; 124 struct csum *ccsp; 125 126 if (Fflag) 127 fd = open(name, O_RDONLY); 128 else { 129 fd = openpartition(name, O_RDONLY, device, sizeof(device)); 130 name = device; 131 } 132 if (fd == -1) 133 goto err; 134 135 for (i = 0; sblock_try[i] != -1; i++) { 136 if (lseek(fd, sblock_try[i], SEEK_SET) == (off_t)-1) 137 continue; 138 if (read(fd, &afs, SBLOCKSIZE) != SBLOCKSIZE) 139 continue; 140 switch(afs.fs_magic) { 141 case FS_UFS2_MAGIC: 142 is_ufs2 = 1; 143 /*FALLTHROUGH*/ 144 case FS_UFS1_MAGIC: 145 goto found; 146 case FS_UFS2_MAGIC_SWAPPED: 147 /*FALLTHROUGH*/ 148 case FS_UFS1_MAGIC_SWAPPED: 149 needswap = 1; 150 goto found; 151 default: 152 continue; 153 } 154 } 155 warnx("%s: could not find superblock, skipped", name); 156 return 1; 157 found: 158 159 if (needswap) 160 ffs_sb_swap(&afs, &afs); 161 162 printold = (afs.fs_magic == FS_UFS1_MAGIC && 163 (afs.fs_old_flags & FS_FLAGS_UPDATED) == 0); 164 if (printold) { 165 afs.fs_flags = afs.fs_old_flags; 166 afs.fs_maxbsize = afs.fs_bsize; 167 afs.fs_time = afs.fs_old_time; 168 afs.fs_size = afs.fs_old_size; 169 afs.fs_dsize = afs.fs_old_dsize; 170 afs.fs_csaddr = afs.fs_old_csaddr; 171 afs.fs_cstotal.cs_ndir = afs.fs_old_cstotal.cs_ndir; 172 afs.fs_cstotal.cs_nbfree = afs.fs_old_cstotal.cs_nbfree; 173 afs.fs_cstotal.cs_nifree = afs.fs_old_cstotal.cs_nifree; 174 afs.fs_cstotal.cs_nffree = afs.fs_old_cstotal.cs_nffree; 175 } 176 177 printf("file system: %s\n", name); 178 #if BYTE_ORDER == LITTLE_ENDIAN 179 if (needswap) 180 #else 181 if (!needswap) 182 #endif 183 printf("endian\tbig-endian\n"); 184 else 185 printf("endian\tlittle-endian\n"); 186 if (printold && afs.fs_old_postblformat == FS_42POSTBLFMT) 187 afs.fs_old_nrpos = 8; 188 dev_bsize = afs.fs_fsize / fsbtodb(&afs, 1); 189 t = afs.fs_time; 190 printf("location%lld\tmagic\t%x\ttime\t%s", (long long)sblock_try[i], 191 afs.fs_magic, ctime(&t)); 192 i = 0; 193 if (printold && afs.fs_old_postblformat != FS_42POSTBLFMT) { 194 i++; 195 if (afs.fs_old_inodefmt >= FS_44INODEFMT) { 196 int max; 197 198 i++; 199 max = afs.fs_maxcontig; 200 size = afs.fs_contigsumsize; 201 if ((max < 2 && size == 0) 202 || (max > 1 && size >= MIN(max, FS_MAXCONTIG))) 203 i++; 204 } 205 } 206 printf("id\t[ %x %x ]\n", afs.fs_id[0], afs.fs_id[1]); 207 printf("cylgrp\t%s\tinodes\t%s\tfslevel %d\tsoftdep %sabled\n", 208 i < 1 ? "static" : "dynamic", i < 2 ? "4.2/4.3BSD" : "4.4BSD", i, 209 (afs.fs_flags & FS_DOSOFTDEP) ? "en" : "dis"); 210 printf("nbfree\t%lld\tndir\t%lld\tnifree\t%lld\tnffree\t%lld\n", 211 (long long)afs.fs_cstotal.cs_nbfree, 212 (long long)afs.fs_cstotal.cs_ndir, 213 (long long)afs.fs_cstotal.cs_nifree, 214 (long long)afs.fs_cstotal.cs_nffree); 215 printf("ncg\t%d\tsize\t%lld\tblocks\t%lld\n", 216 afs.fs_ncg, (long long)afs.fs_size, (long long)afs.fs_dsize); 217 if (printold) 218 printf("ncyl\t%d\n", afs.fs_old_ncyl); 219 printf("bsize\t%d\tshift\t%d\tmask\t0x%08x\n", 220 afs.fs_bsize, afs.fs_bshift, afs.fs_bmask); 221 printf("fsize\t%d\tshift\t%d\tmask\t0x%08x\n", 222 afs.fs_fsize, afs.fs_fshift, afs.fs_fmask); 223 printf("frag\t%d\tshift\t%d\tfsbtodb\t%d\n", 224 afs.fs_frag, afs.fs_fragshift, afs.fs_fsbtodb); 225 printf("bpg\t%d\tfpg\t%d\tipg\t%d\n", 226 afs.fs_fpg / afs.fs_frag, afs.fs_fpg, afs.fs_ipg); 227 printf("minfree\t%d%%\toptim\t%s\tmaxcontig %d\tmaxbpg\t%d\n", 228 afs.fs_minfree, afs.fs_optim == FS_OPTSPACE ? "space" : "time", 229 afs.fs_maxcontig, afs.fs_maxbpg); 230 if (printold) { 231 printf("cpg\t%d\trotdelay %dms\trps\t%d\n", 232 afs.fs_old_cpg, afs.fs_old_rotdelay, afs.fs_old_rps); 233 printf("ntrak\t%d\tnsect\t%d\tnpsect\t%d\tspc\t%d\n", 234 afs.fs_spare2, afs.fs_old_nsect, afs.fs_old_npsect, 235 afs.fs_old_spc); 236 printf("trackskew %d\tinterleave %d\n", 237 afs.fs_old_trackskew, afs.fs_old_interleave); 238 } 239 printf("symlinklen %d\tcontigsumsize %d\n", 240 afs.fs_maxsymlinklen, afs.fs_contigsumsize); 241 printf("maxfilesize 0x%016llx\n", 242 (unsigned long long)afs.fs_maxfilesize); 243 printf("nindir\t%d\tinopb\t%d\n", afs.fs_nindir, afs.fs_inopb); 244 if (printold) 245 printf("nspf\t%d\n", afs.fs_old_nspf); 246 printf("avgfilesize %d\tavgfpdir %d\n", 247 afs.fs_avgfilesize, afs.fs_avgfpdir); 248 printf("sblkno\t%d\tcblkno\t%d\tiblkno\t%d\tdblkno\t%d\n", 249 afs.fs_sblkno, afs.fs_cblkno, afs.fs_iblkno, afs.fs_dblkno); 250 printf("sbsize\t%d\tcgsize\t%d\n", afs.fs_sbsize, afs.fs_cgsize); 251 if (printold) 252 printf("offset\t%d\tmask\t0x%08x\n", 253 afs.fs_old_cgoffset, afs.fs_old_cgmask); 254 printf("csaddr\t%lld\tcssize %d\n", 255 (long long)afs.fs_csaddr, afs.fs_cssize); 256 if (printold) 257 printf("shift\t%d\tmask\t0x%08x\n", 258 afs.fs_spare1[0], afs.fs_spare1[1]); 259 printf("cgrotor\t%d\tfmod\t%d\tronly\t%d\tclean\t0x%02x\n", 260 afs.fs_cgrotor, afs.fs_fmod, afs.fs_ronly, afs.fs_clean); 261 if (printold) { 262 if (afs.fs_old_cpc != 0) 263 printf("blocks available in each of %d rotational " 264 "positions", afs.fs_old_nrpos); 265 else 266 printf("(no rotational position table)\n"); 267 } 268 printf("\ncs[].cs_(nbfree,ndir,nifree,nffree):\n\t"); 269 afs.fs_csp = calloc(1, afs.fs_cssize); 270 for (i = 0, j = 0; i < afs.fs_cssize; i += afs.fs_bsize, j++) { 271 size = afs.fs_cssize - i < afs.fs_bsize ? 272 afs.fs_cssize - i : afs.fs_bsize; 273 ccsp = (struct csum *)((char *)afs.fs_csp + i); 274 if (lseek(fd, 275 (off_t)(fsbtodb(&afs, (afs.fs_csaddr + j * afs.fs_frag))) * 276 dev_bsize, SEEK_SET) == (off_t)-1) 277 goto err; 278 if (read(fd, ccsp, size) != size) 279 goto err; 280 if (needswap) 281 ffs_csum_swap(ccsp, ccsp, size); 282 } 283 for (i = 0; i < afs.fs_ncg; i++) { 284 struct csum *cs = &afs.fs_cs(&afs, i); 285 if (i && i % 4 == 0) 286 printf("\n\t"); 287 printf("(%d,%d,%d,%d) ", 288 cs->cs_nbfree, cs->cs_ndir, cs->cs_nifree, cs->cs_nffree); 289 } 290 printf("\n"); 291 if (printold && (afs.fs_old_ncyl % afs.fs_old_cpg)) { 292 printf("cylinders in last group %d\n", 293 i = afs.fs_old_ncyl % afs.fs_old_cpg); 294 printf("blocks in last group %d\n", 295 i * afs.fs_old_spc / (afs.fs_old_nspf << afs.fs_fragshift)); 296 } 297 printf("\n"); 298 for (i = 0; i < afs.fs_ncg; i++) 299 if (dumpcg(name, fd, i)) 300 goto err; 301 (void)close(fd); 302 return 0; 303 304 err: if (fd != -1) 305 (void)close(fd); 306 warn("%s", name); 307 return 1; 308 }; 309 310 int 311 dumpcg(const char *name, int fd, int c) 312 { 313 off_t cur; 314 int i, j; 315 time_t t; 316 317 printf("\ncg %d:\n", c); 318 if ((cur = lseek(fd, (off_t)(fsbtodb(&afs, cgtod(&afs, c))) * dev_bsize, 319 SEEK_SET)) == (off_t)-1) 320 return (1); 321 if (read(fd, &acg, afs.fs_bsize) != afs.fs_bsize) { 322 warnx("%s: error reading cg", name); 323 return (1); 324 } 325 if (needswap) 326 ffs_cg_swap(&acg, &acg, &afs); 327 t = acg.cg_time; 328 printf("magic\t%x\ttell\t%llx\ttime\t%s", 329 afs.fs_old_postblformat == FS_42POSTBLFMT ? 330 ((struct ocg *)&acg)->cg_magic : acg.cg_magic, 331 (long long)cur, ctime(&t)); 332 printf("cgx\t%d\tniblk\t%d\tndblk\t%d\n", 333 acg.cg_cgx, acg.cg_niblk, acg.cg_ndblk); 334 printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n", 335 acg.cg_cs.cs_nbfree, acg.cg_cs.cs_ndir, 336 acg.cg_cs.cs_nifree, acg.cg_cs.cs_nffree); 337 printf("rotor\t%d\tirotor\t%d\tfrotor\t%d\nfrsum", 338 acg.cg_rotor, acg.cg_irotor, acg.cg_frotor); 339 for (i = 1, j = 0; i < afs.fs_frag; i++) { 340 printf("\t%d", acg.cg_frsum[i]); 341 j += i * acg.cg_frsum[i]; 342 } 343 printf("\nsum of frsum: %d", j); 344 if (afs.fs_contigsumsize > 0) { 345 for (i = 1; i < afs.fs_contigsumsize; i++) { 346 if ((i - 1) % 8 == 0) 347 printf("\nclusters %d-%d:", i, 348 afs.fs_contigsumsize - 1 < i + 7 ? 349 afs.fs_contigsumsize - 1 : i + 7); 350 printf("\t%d", cg_clustersum(&acg, 0)[i]); 351 } 352 printf("\nclusters size %d and over: %d\n", 353 afs.fs_contigsumsize, 354 cg_clustersum(&acg, 0)[afs.fs_contigsumsize]); 355 printf("clusters free:\t"); 356 pbits(cg_clustersfree(&acg, 0), acg.cg_nclusterblks); 357 } else 358 printf("\n"); 359 printf("iused:\t"); 360 pbits(cg_inosused(&acg, 0), afs.fs_ipg); 361 printf("free:\t"); 362 pbits(cg_blksfree(&acg, 0), afs.fs_fpg); 363 return (0); 364 }; 365 366 void 367 pbits(void *vp, int max) 368 { 369 int i; 370 char *p; 371 int count, j; 372 373 for (count = i = 0, p = vp; i < max; i++) 374 if (isset(p, i)) { 375 if (count) 376 printf(",%s", count % 6 ? " " : "\n\t"); 377 count++; 378 printf("%d", i); 379 j = i; 380 while ((i+1)<max && isset(p, i+1)) 381 i++; 382 if (i != j) 383 printf("-%d", i); 384 } 385 printf("\n"); 386 } 387 388 void 389 usage(void) 390 { 391 392 (void)fprintf(stderr, "usage: dumpfs [-F] filesys | device [...]\n"); 393 exit(1); 394 } 395 396 int 397 openpartition(const char *name, int flags, char *device, size_t devicelen) 398 { 399 char rawspec[MAXPATHLEN], *p; 400 struct fstab *fs; 401 int fd, oerrno; 402 403 fs = getfsfile(name); 404 if (fs) { 405 if ((p = strrchr(fs->fs_spec, '/')) != NULL) { 406 snprintf(rawspec, sizeof(rawspec), "%.*s/r%s", 407 (int)(p - fs->fs_spec), fs->fs_spec, p + 1); 408 name = rawspec; 409 } else 410 name = fs->fs_spec; 411 } 412 fd = opendisk(name, flags, device, devicelen, 0); 413 if (fd == -1 && errno == ENOENT) { 414 oerrno = errno; 415 strlcpy(device, name, devicelen); 416 errno = oerrno; 417 } 418 return (fd); 419 } 420