1 /* $NetBSD: dumpfs.c,v 1.71 2026/05/07 20:32:11 kre 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\ 35 The Regents of the University of California. All rights reserved."); 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.71 2026/05/07 20:32:11 kre Exp $"); 43 #endif 44 #endif /* not lint */ 45 46 #include <sys/param.h> 47 #include <sys/time.h> 48 49 #include <sys/wapbl.h> 50 #include <sys/wapbl_replay.h> 51 52 #include <ufs/ufs/dinode.h> 53 #include <ufs/ufs/ufs_bswap.h> 54 #include <ufs/ufs/ufs_wapbl.h> 55 #include <ufs/ufs/quota.h> 56 #include <ufs/ffs/fs.h> 57 #include <ufs/ffs/ffs_extern.h> 58 59 #include <err.h> 60 #include <errno.h> 61 #include <fcntl.h> 62 #include <fstab.h> 63 #include <stdio.h> 64 #include <stdlib.h> 65 #include <string.h> 66 #include <stddef.h> 67 #include <unistd.h> 68 #include <util.h> 69 70 #include "partutil.h" 71 72 static union fsun { 73 struct fs fs; 74 char pad[MAXBSIZE]; 75 } fsun; 76 #define afs fsun.fs 77 78 static uint16_t opostblsave[32*8]; 79 80 static union { 81 struct cg cg; 82 char pad[MAXBSIZE]; 83 } cgun; 84 #define acg cgun.cg 85 86 static union { 87 struct wapbl_wc_header wh; 88 struct wapbl_wc_null wn; 89 char pad[MAXBSIZE]; 90 } jbuf; 91 #define awh jbuf.wh 92 #define awn jbuf.wn 93 94 #define OPT_FLAG(ch) (1 << ((ch) & 31)) 95 #define ISOPT(opt) (opt_flags & (opt)) 96 #define opt_alt_super OPT_FLAG('a') 97 #define opt_superblock OPT_FLAG('s') 98 #define opt_cg_summary OPT_FLAG('m') 99 #define opt_cg_info OPT_FLAG('c') 100 #define opt_inodes OPT_FLAG('i') 101 #define opt_journal OPT_FLAG('j') 102 #define opt_verbose OPT_FLAG('v') 103 #define DFLT_CHECK (opt_alt_super | opt_cg_info | opt_inodes | \ 104 opt_cg_summary | opt_superblock | opt_journal ) 105 #define DFLT_OPTS (opt_superblock | opt_cg_summary | opt_cg_info | opt_verbose) 106 107 static long dev_bsize = 512; 108 static int needswap, printold, is_ufs2; 109 static int Fflag; 110 111 static uint opt_flags; 112 113 static int dumpfs(const char *); 114 static int print_superblock(struct fs *, uint16_t *, const char *, int, off_t); 115 static int print_cgsum(const char *, int); 116 static int print_cginfo(const char *, int); 117 static int print_inodes(const char *, int, int, int); 118 static int print_alt_super(const char *, int); 119 static int print_journal(const char *, int); 120 static void print_journal_header(const char *); 121 static off_t print_journal_entries(const char *, size_t); 122 static int dumpcg(const char *, int, int); 123 static int openpartition(const char *, int, char *, size_t); 124 static void pbits(int, void *, int); 125 __dead static void usage(void); 126 static void print_ufs1_inode(int, int, void *); 127 static void print_ufs2_inode(int, int, void *); 128 static void fix_superblock(struct fs *, uint16_t *); 129 130 int 131 main(int argc, char *argv[]) 132 { 133 int ch, eval; 134 135 while ((ch = getopt(argc, argv, "acijmsvF")) != -1) 136 switch(ch) { 137 case 'a': /* alternate superblocks */ 138 case 'c': /* cylinder group info */ 139 case 'i': /* actual inodes */ 140 case 'j': /* journal */ 141 case 'm': /* cylinder group summary */ 142 case 's': /* superblock */ 143 case 'v': /* more verbose */ 144 opt_flags |= OPT_FLAG(ch); 145 break; 146 case 'F': /* File (not device) */ 147 Fflag = 1; 148 break; 149 case '?': 150 default: 151 usage(); 152 } 153 argc -= optind; 154 argv += optind; 155 156 if ((opt_flags & DFLT_CHECK) == 0) 157 opt_flags |= DFLT_OPTS; 158 159 if (argc < 1) 160 usage(); 161 162 for (eval = 0; *argv; ++argv) { 163 eval |= dumpfs(*argv); 164 } 165 166 exit(eval); 167 } 168 169 170 static int 171 dumpfs(const char *name) 172 { 173 static const off_t sblock_try[] = SBLOCKSEARCH; 174 char device[MAXPATHLEN]; 175 int fd, i; 176 int rval = 1; 177 178 if (Fflag) 179 fd = open(name, O_RDONLY); 180 else { 181 fd = openpartition(name, O_RDONLY, device, sizeof(device)); 182 name = device; 183 } 184 if (fd == -1) 185 goto err; 186 187 for (i = 0; ; i++) { 188 if (sblock_try[i] == -1) { 189 warnx("%s: could not find superblock, skipped", name); 190 return 1; 191 } 192 if (lseek(fd, sblock_try[i], SEEK_SET) == (off_t)-1) 193 continue; 194 if (read(fd, &afs, SBLOCKSIZE) != SBLOCKSIZE) 195 continue; 196 switch(afs.fs_magic) { 197 case FS_UFS2EA_MAGIC: 198 case FS_UFS2_MAGIC: 199 is_ufs2 = 1; 200 break; 201 case FS_UFS1_MAGIC: 202 break; 203 case FS_UFS2EA_MAGIC_SWAPPED: 204 case FS_UFS2_MAGIC_SWAPPED: 205 is_ufs2 = 1; 206 needswap = 1; 207 break; 208 case FS_UFS1_MAGIC_SWAPPED: 209 needswap = 1; 210 break; 211 default: 212 continue; 213 } 214 fix_superblock(&afs, opostblsave); 215 if (printold) { 216 if (sblock_try[i] == SBLOCK_UFS2) 217 /* This might be an alternate superblock */ 218 continue; 219 } else { 220 if (sblock_try[i] != afs.fs_sblockloc) 221 /* This must be an alternate superblock */ 222 continue; 223 } 224 break; 225 } 226 227 228 dev_bsize = afs.fs_fsize / FFS_FSBTODB(&afs, 1); 229 230 rval = 0; 231 printf("file system: %s\n", name); 232 233 if (ISOPT(opt_superblock)) 234 rval = print_superblock(&afs, opostblsave, name, fd, sblock_try[i]); 235 if (rval == 0 && ISOPT(opt_cg_summary)) 236 rval = print_cgsum(name, fd); 237 if (rval == 0 && ISOPT(opt_alt_super)) 238 rval = print_alt_super(name, fd); 239 if (rval == 0 && ISOPT(opt_journal)) 240 rval = print_journal(name, fd); 241 if (rval == 0 && ISOPT(opt_cg_info)) 242 rval = print_cginfo(name, fd); 243 else if (rval == 0 && ISOPT(opt_inodes)) 244 rval = print_inodes(name, fd, 0, afs.fs_ncg); 245 246 err: 247 if (fd != -1) 248 (void)close(fd); 249 if (rval) 250 warn("%s", name); 251 return rval; 252 } 253 254 static void 255 fix_superblock(struct fs *fs, uint16_t *opostbl) 256 { 257 if (needswap && 258 (((int32_t)bswap32(fs->fs_old_postblformat) == FS_42POSTBLFMT) || 259 (bswap32(fs->fs_old_postbloff) == offsetof(struct fs, fs_old_postbl_start)))) { 260 int i; 261 memcpy(opostbl, &fs->fs_old_postbl_start, 512); 262 for(i=0;i<256;i++) 263 opostbl[i] = bswap16(opostbl[i]); 264 } else if (!needswap && 265 ((fs->fs_old_postblformat == FS_42POSTBLFMT) || 266 (fs->fs_old_postbloff == offsetof(struct fs, fs_old_postbl_start)))) { 267 memcpy(opostbl, &fs->fs_old_postbl_start, 512); 268 } 269 270 if (needswap) 271 ffs_sb_swap(fs, fs); 272 273 printold = (fs->fs_magic == FS_UFS1_MAGIC && 274 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0); 275 if (printold) { 276 fs->fs_sblockloc = SBLOCK_UFS1; 277 fs->fs_flags = fs->fs_old_flags; 278 fs->fs_maxbsize = fs->fs_bsize; 279 fs->fs_time = fs->fs_old_time; 280 fs->fs_size = fs->fs_old_size; 281 fs->fs_dsize = fs->fs_old_dsize; 282 fs->fs_csaddr = fs->fs_old_csaddr; 283 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir; 284 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree; 285 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree; 286 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree; 287 } 288 289 if (printold && fs->fs_old_postblformat == FS_42POSTBLFMT) 290 fs->fs_old_nrpos = 8; 291 } 292 293 static int 294 print_superblock(struct fs *fs, uint16_t *opostbl, 295 const char *name, int fd, off_t sblock) 296 { 297 int i, size; 298 time_t t; 299 int32_t fsflags; 300 301 printf("format\tFFSv%d%s\n", is_ufs2+1, 302 fs->fs_magic == FS_UFS2EA_MAGIC ? "ea" : ""); 303 #if BYTE_ORDER == LITTLE_ENDIAN 304 if (needswap) 305 #else 306 if (!needswap) 307 #endif 308 printf("endian\tbig-endian\n"); 309 else 310 printf("endian\tlittle-endian\n"); 311 t = fs->fs_time; 312 if ((sblock != SBLOCK_UFS1) || ISOPT(opt_alt_super)) 313 printf("location %lld\t(-b %lld)\n", 314 (long long)sblock, (long long)(sblock/dev_bsize)); 315 printf("magic\t%-8x\ttime\t%s", 316 fs->fs_magic, ctime(&t)); 317 318 if (is_ufs2) 319 i = -1; 320 else { 321 i = 0; 322 if (fs->fs_old_postblformat != FS_42POSTBLFMT) { 323 i++; 324 if (fs->fs_old_inodefmt >= FS_44INODEFMT) { 325 int max; 326 327 i++; 328 max = fs->fs_maxcontig; 329 size = fs->fs_contigsumsize; 330 if ((max < 2 && size == 0) 331 || (max > 1 && size >= MIN(max, FS_MAXCONTIG))) 332 i++; 333 } 334 if (fs->fs_old_flags & FS_FLAGS_UPDATED) { 335 i = 4; 336 } 337 } 338 } 339 if (!printold || fs->fs_sblockloc != SBLOCK_UFS1 || 340 fs->fs_id[0] || fs->fs_id[1]) 341 printf("superblock location\t%jd\tid\t[ %x %x ]\n", 342 (intmax_t)fs->fs_sblockloc, fs->fs_id[0], fs->fs_id[1]); 343 if (!is_ufs2) 344 printf("cylgrp\t%s\tinodes\t%s\tsblock\t%s\tfslevel %d\n", 345 i < 1 ? "static" : "dynamic", 346 i < 2 ? "4.2/4.3BSD" : i < 5 ? "4.4BSD" : "FFSv2", 347 i < 4 ? "FFSv1" : "FFSv2", i); 348 printf("nbfree\t%lld\tndir\t%lld\tnifree\t%lld\tnffree\t%lld\n", 349 (long long)fs->fs_cstotal.cs_nbfree, 350 (long long)fs->fs_cstotal.cs_ndir, 351 (long long)fs->fs_cstotal.cs_nifree, 352 (long long)fs->fs_cstotal.cs_nffree); 353 if (printold) 354 printf("ncg\t%d\tncyl\t%d\tsize\t%lld\tblocks\t%lld\n", 355 fs->fs_ncg, fs->fs_old_ncyl, (long long)fs->fs_size, (long long)fs->fs_dsize); 356 else 357 printf("ncg\t%d\tsize\t%lld\tblocks\t%lld\n", 358 fs->fs_ncg, (long long)fs->fs_size, (long long)fs->fs_dsize); 359 printf("bsize\t%d\tshift\t%d\tmask\t0x%08x\n", 360 fs->fs_bsize, fs->fs_bshift, fs->fs_bmask); 361 printf("fsize\t%d\tshift\t%d\tmask\t0x%08x\n", 362 fs->fs_fsize, fs->fs_fshift, fs->fs_fmask); 363 printf("frag\t%d\tshift\t%d\tfsbtodb\t%d\n", 364 fs->fs_frag, fs->fs_fragshift, fs->fs_fsbtodb); 365 if (printold) 366 printf("cpg\t%d\t", fs->fs_old_cpg); 367 printf("bpg\t%d\tfpg\t%d\tipg\t%d\n", 368 fs->fs_fpg / fs->fs_frag, fs->fs_fpg, fs->fs_ipg); 369 printf("minfree\t%d%%\toptim\t%s\tmaxcontig %d\tmaxbpg\t%d\n", 370 fs->fs_minfree, fs->fs_optim == FS_OPTSPACE ? "space" : "time", 371 fs->fs_maxcontig, fs->fs_maxbpg); 372 if (printold) { 373 printf("rotdelay %dms\theadswitch %dus\ttrackseek %dus\trps\t%d\n", 374 fs->fs_old_rotdelay, fs->fs_old_headswitch, 375 fs->fs_old_trkseek, fs->fs_old_rps); 376 printf("ntrak\t%d\tnsect\t%d\tnpsect\t%d\tspc\t%d\n", 377 fs->fs_spare2, fs->fs_old_nsect, fs->fs_old_npsect, 378 fs->fs_old_spc); 379 } 380 printf("symlinklen %d\t", fs->fs_maxsymlinklen); 381 if (printold) 382 printf("trackskew %d\tinterleave %d\t", 383 fs->fs_old_trackskew, fs->fs_old_interleave); 384 printf("contigsumsize %d\n", fs->fs_contigsumsize); 385 printf("maxfilesize 0x%016llx\n", 386 (unsigned long long)fs->fs_maxfilesize); 387 if (printold) 388 printf("nindir\t%d\tinopb\t%d\tnspf\t%d\n", fs->fs_nindir, 389 fs->fs_inopb, fs->fs_old_nspf); 390 else 391 printf("nindir\t%d\tinopb\t%d\n", fs->fs_nindir, fs->fs_inopb); 392 if (!printold || (fs->fs_avgfilesize > 0) || (fs->fs_avgfpdir > 0)) 393 printf("avgfilesize %d\tavgfpdir %d\n", 394 fs->fs_avgfilesize, fs->fs_avgfpdir); 395 printf("sblkno\t%d\tcblkno\t%d\tiblkno\t%d\tdblkno\t%d\n", 396 fs->fs_sblkno, fs->fs_cblkno, fs->fs_iblkno, fs->fs_dblkno); 397 printf("sbsize\t%d\tcgsize\t%d", fs->fs_sbsize, fs->fs_cgsize); 398 if (printold) 399 printf("\toffset\t%d\tmask\t0x%08x", 400 fs->fs_old_cgoffset, fs->fs_old_cgmask); 401 printf("\ncsaddr\t%lld\tcssize\t%d", 402 (long long)fs->fs_csaddr, fs->fs_cssize); 403 if (printold) 404 printf("\tshift\t%d\tmask\t0x%08x", 405 fs->fs_old_csshift, fs->fs_old_csmask); 406 printf("\ncgrotor\t%d\tfmod\t%d\tronly\t%d\tclean\t0x%02x\n", 407 fs->fs_cgrotor, fs->fs_fmod, fs->fs_ronly, fs->fs_clean); 408 printf("wapbl version 0x%x\tlocation %u\tflags 0x%x\n", 409 fs->fs_journal_version, fs->fs_journal_location, 410 fs->fs_journal_flags); 411 printf("wapbl loc0 %" PRIu64 "\tloc1 %" PRIu64, 412 fs->fs_journallocs[0], fs->fs_journallocs[1]); 413 printf("\tloc2 %" PRIu64 "\tloc3 %" PRIu64 "\n", 414 fs->fs_journallocs[2], fs->fs_journallocs[3]); 415 printf("snapshot inodes"); 416 for (int s = 0; s < FSMAXSNAP; s++) { 417 printf(" %u", fs->fs_snapinum[s]); 418 } 419 printf("\n"); 420 printf("usrquota %" PRIu64 "\tgrpquota %" PRIu64 "\n", 421 fs->fs_quotafile[USRQUOTA], fs->fs_quotafile[GRPQUOTA]); 422 printf("flags\t"); 423 if (fs->fs_flags == 0) 424 printf("none"); 425 if (fs->fs_flags & FS_UNCLEAN) 426 printf("unclean "); 427 if (fs->fs_flags & FS_DOSOFTDEP) 428 printf("soft-updates "); 429 if (fs->fs_flags & FS_NEEDSFSCK) 430 printf("needs fsck run "); 431 if (fs->fs_flags & FS_SUJ) 432 printf("journaled soft-updates "); 433 if (fs->fs_flags & FS_POSIX1EACLS) 434 printf("posix1e acls "); 435 if (fs->fs_flags & FS_NFS4ACLS) 436 printf("nfs4 acls "); 437 if (fs->fs_flags & FS_MULTILABEL) 438 printf("multilabel "); 439 if (fs->fs_flags & FS_GJOURNAL) 440 printf("gjournal "); 441 if (fs->fs_flags & FS_FLAGS_UPDATED) 442 printf("fs_flags expanded "); 443 if (fs->fs_flags & FS_DOWAPBL) 444 printf("wapbl "); 445 if (fs->fs_flags & FS_DOQUOTA2) 446 printf("quotas "); 447 if (fs->fs_flags & FS_TRIM) 448 printf("trim "); 449 fsflags = fs->fs_flags & ~(FS_UNCLEAN | FS_DOSOFTDEP | FS_NEEDSFSCK | 450 FS_SUJ | FS_POSIX1EACLS | FS_MULTILABEL | FS_GJOURNAL | 451 FS_NFS4ACLS | FS_FLAGS_UPDATED | FS_DOWAPBL | 452 FS_DOQUOTA2 | FS_TRIM); 453 #ifdef FS_INDEXDIRS 454 if (fs->fs_flags & FS_INDEXDIRS) 455 printf("indexed directories "); 456 fsflags &= ~FS_INDEXDIRS 457 #endif 458 if (fsflags != 0) 459 printf("unknown flags (%#x)", fsflags); 460 printf("\nfsmnt\t%s\n", fs->fs_fsmnt); 461 if (!printold) { 462 printf("volname\t%s\tswuid\t%ju\n", 463 fs->fs_volname, (uintmax_t)fs->fs_swuid); 464 return 0; 465 } 466 467 if (fs->fs_old_cpc != 0) 468 printf("blocks available in each of %d rotational " 469 "positions\n", fs->fs_old_nrpos); 470 else 471 printf("(no rotational position table)\n\n"); 472 473 if (!ISOPT(opt_verbose)) { 474 return 0; 475 } 476 477 for (int c = 0; c < fs->fs_old_cpc; c++) { 478 printf("cylinder number %d:", c); 479 for (i = 0; i < fs->fs_old_nrpos; i++) { 480 if (old_fs_postbl(&afs, c, opostbl)[i] == -1) 481 continue; 482 printf("\n position %d:\t", i); 483 for (int j = old_fs_postbl(&afs, c, opostbl)[i], k = 1; 484 ; j += old_fs_rotbl(&afs)[j], k++) { 485 printf("%5d", j); 486 if (k % 12 == 0) 487 printf("\n\t\t"); 488 if (old_fs_rotbl(&afs)[j] == 0) 489 break; 490 } 491 } 492 printf("\n"); 493 } 494 495 return 0; 496 } 497 498 static int 499 print_cgsum(const char *name, int fd) 500 { 501 struct csum *ccsp; 502 int i, j, size; 503 uint32_t cgnum; 504 505 afs.fs_csp = calloc(1, afs.fs_cssize); 506 for (i = 0, j = 0; i < afs.fs_cssize; i += afs.fs_bsize, j++) { 507 size = afs.fs_cssize - i < afs.fs_bsize ? 508 afs.fs_cssize - i : afs.fs_bsize; 509 ccsp = (struct csum *)((char *)afs.fs_csp + i); 510 if (lseek(fd, 511 (off_t)(FFS_FSBTODB(&afs, (afs.fs_csaddr + j * afs.fs_frag))) * 512 dev_bsize, SEEK_SET) == (off_t)-1) 513 return 1; 514 if (read(fd, ccsp, size) != size) 515 return 1; 516 if (needswap) 517 ffs_csum_swap(ccsp, ccsp, size); 518 } 519 520 printf("cs[].cs_(nbfree,ndir,nifree,nffree):\n\t"); 521 for (cgnum = 0; cgnum < afs.fs_ncg; cgnum++) { 522 struct csum *cs = &afs.fs_cs(&afs, cgnum); 523 if (i && i % 4 == 0) 524 printf("\n\t"); 525 printf("(%d,%d,%d,%d) ", 526 cs->cs_nbfree, cs->cs_ndir, cs->cs_nifree, cs->cs_nffree); 527 } 528 printf("\n"); 529 if (printold && (afs.fs_old_ncyl % afs.fs_old_cpg)) { 530 printf("cylinders in last group %d\n", 531 i = afs.fs_old_ncyl % afs.fs_old_cpg); 532 printf("blocks in last group %d\n", 533 i * afs.fs_old_spc / (afs.fs_old_nspf << afs.fs_fragshift)); 534 } 535 free(afs.fs_csp); 536 afs.fs_csp = NULL; 537 538 return 0; 539 } 540 541 static int 542 print_alt_super(const char *name, int fd) 543 { 544 union fsun alt; 545 uint32_t i; 546 off_t loc; 547 uint16_t alt_opostblsave[32*8]; 548 int save_printold; 549 550 for (i = 0; i < afs.fs_ncg; i++) { 551 loc = (off_t)(FFS_FSBTODB(&afs, cgsblock(&afs, i))) * dev_bsize; 552 printf("\nalternate %d\n", i); 553 if (pread(fd, &alt, sizeof alt, loc) != sizeof alt) { 554 warnx("%s: error reading alt %d", name, i); 555 return (1); 556 } 557 save_printold = printold; 558 fix_superblock(&alt.fs, alt_opostblsave); 559 if (print_superblock(&alt.fs, alt_opostblsave, name, fd, loc)) { 560 printold = save_printold; 561 return 1; 562 } 563 printold = save_printold; 564 } 565 return 0; 566 } 567 568 static int 569 print_cginfo(const char *name, int fd) 570 { 571 uint32_t i; 572 573 printf("\n"); 574 for (i = 0; i < afs.fs_ncg; i++) { 575 printf("\n"); 576 if (dumpcg(name, fd, i)) 577 return 1; 578 if (ISOPT(opt_inodes) && print_inodes(name, fd, i, 1)) 579 return 1; 580 } 581 return 0; 582 } 583 584 static int 585 print_inodes(const char *name, int fd, int c, int n) 586 { 587 void *ino_buf = malloc(afs.fs_bsize); 588 void (*print_inode)(int, int, void *); 589 ino_t inum; 590 uint32_t i; 591 592 if (ino_buf == 0) 593 return 1; 594 595 print_inode = is_ufs2 ? print_ufs2_inode : print_ufs1_inode; 596 597 for (inum = c * afs.fs_ipg ; inum < (c+n) * afs.fs_ipg; inum += afs.fs_inopb) { 598 if (pread(fd, ino_buf, afs.fs_bsize, 599 ino_to_fsba(&afs, inum) * afs.fs_fsize) != afs.fs_bsize) { 600 free(ino_buf); 601 return 1; 602 } 603 for (i = 0; i < afs.fs_inopb; i++) 604 print_inode(inum + i, i, ino_buf); 605 } 606 607 free(ino_buf); 608 return 0; 609 } 610 611 static int 612 dumpcg(const char *name, int fd, int c) 613 { 614 off_t cur; 615 int i, j; 616 time_t t; 617 618 printf("cg %d:\n", c); 619 if ((cur = lseek(fd, (off_t)(FFS_FSBTODB(&afs, cgtod(&afs, c))) * dev_bsize, 620 SEEK_SET)) == (off_t)-1) 621 return (1); 622 if (read(fd, &acg, afs.fs_bsize) != afs.fs_bsize) { 623 warnx("%s: error reading cg", name); 624 return (1); 625 } 626 if (needswap) 627 ffs_cg_swap(&acg, &acg, &afs); 628 if (printold) 629 t = acg.cg_old_time; 630 else 631 t = acg.cg_time; 632 printf("magic\t%x\ttell\t%llx\ttime\t%s", 633 afs.fs_old_postblformat == FS_42POSTBLFMT ? 634 ((struct ocg *)&acg)->cg_magic : acg.cg_magic, 635 (long long)cur, ctime(&t)); 636 if (printold) 637 printf("cgx\t%d\tncyl\t%d\tniblk\t%d\tndblk\t%d\n", 638 acg.cg_cgx, acg.cg_old_ncyl, acg.cg_old_niblk, 639 acg.cg_ndblk); 640 else 641 printf("cgx\t%d\tniblk\t%d\tndblk\t%d\n", 642 acg.cg_cgx, acg.cg_niblk, acg.cg_ndblk); 643 printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n", 644 acg.cg_cs.cs_nbfree, acg.cg_cs.cs_ndir, 645 acg.cg_cs.cs_nifree, acg.cg_cs.cs_nffree); 646 printf("rotor\t%d\tirotor\t%d\tfrotor\t%d\nfrsum", 647 acg.cg_rotor, acg.cg_irotor, acg.cg_frotor); 648 for (i = 1, j = 0; i < afs.fs_frag; i++) { 649 printf("\t%d", acg.cg_frsum[i]); 650 j += i * acg.cg_frsum[i]; 651 } 652 printf("\nsum of frsum: %d", j); 653 if (afs.fs_contigsumsize > 0) { 654 for (i = 1; i < afs.fs_contigsumsize; i++) { 655 if ((i - 1) % 8 == 0) 656 printf("\nclusters %d-%d:", i, 657 afs.fs_contigsumsize - 1 < i + 7 ? 658 afs.fs_contigsumsize - 1 : i + 7); 659 printf("\t%d", cg_clustersum(&acg, 0)[i]); 660 } 661 printf("\nclusters size %d and over: %d\n", 662 afs.fs_contigsumsize, 663 cg_clustersum(&acg, 0)[afs.fs_contigsumsize]); 664 printf("clusters free:\t"); 665 pbits(0, cg_clustersfree(&acg, 0), acg.cg_nclusterblks); 666 } else 667 printf("\n"); 668 printf("iused:\t"); 669 pbits(0 * c * afs.fs_ipg, cg_inosused(&acg, 0), afs.fs_ipg); 670 printf("free:\t"); 671 pbits(0, cg_blksfree(&acg, 0), afs.fs_fpg); 672 if (printold && ISOPT(opt_verbose)) { 673 printf("b:\n"); 674 for (i = 0; i < afs.fs_old_cpg; i++) { 675 if (old_cg_blktot(&acg, 0)[i] == 0) 676 continue; 677 printf(" c%d:\t(%d)\t", i, old_cg_blktot(&acg, 0)[i]); 678 for (j = 0; j < afs.fs_old_nrpos; j++) { 679 if (afs.fs_old_cpc > 0 && 680 old_fs_postbl(&afs, i % afs.fs_old_cpc, opostblsave)[j] == -1) 681 continue; 682 printf(" %d", old_cg_blks(&afs, &acg, i, 0)[j]); 683 } 684 printf("\n"); 685 } 686 } 687 return (0); 688 } 689 690 static void 691 print_ufs1_inode(int inum, int i_off, void *ibuf) 692 { 693 struct ufs1_dinode *i = ibuf; 694 695 i += i_off; 696 697 if (needswap) 698 ffs_dinode1_swap(i,i); 699 700 if (afs.fs_old_inodefmt < FS_44INODEFMT) { 701 i->di_uid = i->di_ouid; 702 i->di_gid = i->di_ogid; 703 } 704 705 if (inum % afs.fs_ipg == 0) 706 printf(" inode: mode nlink size" 707 " ctime.nsec flags blocks" 708 " generation uid gid\n"); 709 if (i->di_mode == 0 && i->di_nlink == 0 && !ISOPT(opt_verbose)) 710 return; 711 printf("%8u: %6o %6d %20" PRIu64 " %10u.%09u %8x %10u %8x %10u %10u\n", 712 inum, i->di_mode, i->di_nlink, i->di_size, 713 i->di_ctime, i->di_ctimensec, i->di_flags, i->di_blocks, 714 i->di_gen, i->di_uid, i->di_gid); 715 } 716 717 static void 718 print_ufs2_inode(int inum, int i_off, void *ibuf) 719 { 720 struct ufs2_dinode *i = ibuf; 721 722 i += i_off; 723 724 if (needswap) 725 ffs_dinode2_swap(i,i); 726 727 if (inum % afs.fs_ipg == 0) 728 printf(" inode: mode nlink size" 729 " ctime.nsec flags blocks" 730 " generation uid gid\n"); 731 732 if (i->di_mode == 0 && i->di_nlink == 0 && !ISOPT(opt_verbose)) 733 return; 734 735 printf("%8u: %6o %6d %20" PRIu64 " %10" PRIu64 ".%09u %8x %10" PRIu64 " %8x %10u %10u\n", 736 inum, i->di_mode, i->di_nlink, i->di_size, 737 i->di_ctime, i->di_ctimensec, i->di_flags, i->di_blocks, 738 i->di_gen, i->di_uid, i->di_gid); 739 } 740 741 static void 742 pbits(int offset, void *vp, int max) 743 { 744 int i; 745 char *p; 746 int count, j; 747 748 for (count = i = 0, p = vp; i < max; i++) 749 if (isset(p, i)) { 750 if (count) 751 printf(",%s", count % 6 ? " " : "\n\t"); 752 count++; 753 printf("%d", offset + i); 754 j = i; 755 while ((i+1)<max && isset(p, i+1)) 756 i++; 757 if (i != j) 758 printf("-%d", offset + i); 759 } 760 printf("\n"); 761 } 762 763 static int 764 print_journal(const char *name, int fd) 765 { 766 daddr_t off; 767 size_t count, blklen, bno, skip, seq; 768 off_t boff, head, tail, len; 769 uint32_t generation; 770 771 if (afs.fs_journal_version != UFS_WAPBL_VERSION) 772 return 0; 773 774 generation = 0; 775 head = tail = 0; 776 777 switch (afs.fs_journal_location) { 778 case UFS_WAPBL_JOURNALLOC_END_PARTITION: 779 case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM: 780 781 off = afs.fs_journallocs[0]; 782 count = afs.fs_journallocs[1]; 783 blklen = afs.fs_journallocs[2]; 784 785 if (blklen == 0) { 786 warnx("%s: zero block length", name); 787 return 1; 788 } 789 790 for (seq=0; seq<count; seq += skip / blklen) { 791 792 if (seq < 2) { 793 bno = seq; 794 } else { 795 bno = tail/blklen + (seq - 2); 796 while (bno >= count) 797 bno -= (count - 2); 798 } 799 800 skip = blklen; 801 802 boff = bno * blklen; 803 if (bno * blklen >= 2 * blklen && 804 ((head >= tail && (boff < tail || boff >= head)) || 805 (head < tail && (boff >= head && boff < tail)))) 806 continue; 807 808 printf("journal block %lu offset %lld\n", 809 (unsigned long)bno, (long long) boff); 810 811 if (lseek(fd, (off_t)(off*blklen) + boff, SEEK_SET) 812 == (off_t)-1) 813 return (1); 814 if (read(fd, &jbuf, blklen) != (ssize_t)blklen) { 815 warnx("%s: error reading journal", name); 816 return 1; 817 } 818 819 switch (awh.wc_type) { 820 case 0: 821 break; 822 case WAPBL_WC_HEADER: 823 print_journal_header(name); 824 if (awh.wc_generation > generation) { 825 head = awh.wc_head; 826 tail = awh.wc_tail; 827 } 828 generation = awh.wc_generation; 829 skip = awh.wc_len; 830 break; 831 default: 832 len = print_journal_entries(name, blklen); 833 if (len != (off_t)awh.wc_len) { 834 printf(" CORRUPTED RECORD\n"); 835 break; 836 } 837 skip = awh.wc_len; 838 break; 839 } 840 841 skip = (skip + blklen - 1) / blklen * blklen; 842 if (skip == 0) 843 break; 844 } 845 break; 846 } 847 848 return 0; 849 } 850 851 static const char * 852 wapbl_type_string(unsigned t) 853 { 854 static char buf[12]; 855 856 switch (t) { 857 case WAPBL_WC_BLOCKS: 858 return "blocks"; 859 case WAPBL_WC_REVOCATIONS: 860 return "revocations"; 861 case WAPBL_WC_INODES: 862 return "inodes"; 863 case WAPBL_WC_HEADER: 864 return "header"; 865 } 866 867 snprintf(buf,sizeof(buf),"%08x",t); 868 return buf; 869 } 870 871 static void 872 print_journal_header(const char *name) 873 { 874 printf(" type %s len %d version %u\n", 875 wapbl_type_string(awh.wc_type), awh.wc_len, 876 awh.wc_version); 877 printf(" checksum %08x generation %9u\n", 878 awh.wc_checksum, awh.wc_generation); 879 printf(" fsid %08x.%08x time %llu nsec %u\n", 880 awh.wc_fsid[0], awh.wc_fsid[1], 881 (unsigned long long)awh.wc_time, awh.wc_timensec); 882 printf(" log_bshift %10u fs_bshift %10u\n", 883 awh.wc_log_dev_bshift, awh.wc_fs_dev_bshift); 884 printf(" head %10lld tail %10lld\n", 885 (long long)awh.wc_head, (long long)awh.wc_tail); 886 printf(" circ_off %10lld circ_size %10lld\n", 887 (long long)awh.wc_circ_off, (long long)awh.wc_circ_size); 888 } 889 890 static off_t 891 print_journal_entries(const char *name, size_t blklen) 892 { 893 int i, n; 894 struct wapbl_wc_blocklist *wcb; 895 struct wapbl_wc_inodelist *wci; 896 off_t len = 0; 897 int ph; 898 899 printf(" type %s len %d", 900 wapbl_type_string(awn.wc_type), awn.wc_len); 901 902 switch (awn.wc_type) { 903 case WAPBL_WC_BLOCKS: 904 case WAPBL_WC_REVOCATIONS: 905 wcb = (struct wapbl_wc_blocklist *)&awn; 906 printf(" blkcount %u\n", wcb->wc_blkcount); 907 ph = (blklen - offsetof(struct wapbl_wc_blocklist, wc_blocks)) 908 / sizeof(wcb->wc_blocks[0]); 909 n = MIN(wcb->wc_blkcount, ph); 910 for (i=0; i<n; i++) { 911 if (ISOPT(opt_verbose)) { 912 printf(" %3d: daddr %14llu dlen %d\n", i, 913 (unsigned long long)wcb->wc_blocks[i].wc_daddr, 914 wcb->wc_blocks[i].wc_dlen); 915 } 916 len += wcb->wc_blocks[i].wc_dlen; 917 } 918 if (awn.wc_type == WAPBL_WC_BLOCKS) { 919 if (len % blklen) 920 len += blklen - len % blklen; 921 } else 922 len = 0; 923 break; 924 case WAPBL_WC_INODES: 925 wci = (struct wapbl_wc_inodelist *)&awn; 926 printf(" count %u clear %u\n", 927 wci->wc_inocnt, wci->wc_clear); 928 ph = (blklen - offsetof(struct wapbl_wc_inodelist, wc_inodes)) 929 / sizeof(wci->wc_inodes[0]); 930 n = MIN(wci->wc_inocnt, ph); 931 for (i=0; i<n; ++i) { 932 if (ISOPT(opt_verbose)) { 933 printf(" %3d: inumber %10u imode %08x\n", i, 934 wci->wc_inodes[i].wc_inumber, 935 wci->wc_inodes[i].wc_imode); 936 } 937 } 938 break; 939 default: 940 printf("\n"); 941 break; 942 } 943 944 return len + blklen; 945 } 946 947 static void 948 usage(void) 949 { 950 951 (void)fprintf(stderr, "usage: dumpfs [-acFijmsv] filesys | device [...]\n"); 952 exit(1); 953 } 954 955 static int 956 openpartition(const char *name, int flags, char *device, size_t devicelen) 957 { 958 int fd, oerrno; 959 960 name = findspecial(name, 1); 961 fd = opendisk(name, flags, device, devicelen, 0); 962 if (fd == -1 && errno == ENOENT) { 963 oerrno = errno; 964 strlcpy(device, name, devicelen); 965 errno = oerrno; 966 } 967 return (fd); 968 } 969