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