1 /* $NetBSD: dumpfs.c,v 1.59 2012/04/07 05:07:32 christos 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.59 2012/04/07 05:07:32 christos 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 / 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_INDEXDIRS) 418 printf("indexed directories "); 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_FLAGS_UPDATED) 424 printf("fs_flags expanded "); 425 if (fs->fs_flags & FS_DOWAPBL) 426 printf("wapbl "); 427 fsflags = fs->fs_flags & ~(FS_UNCLEAN | FS_DOSOFTDEP | FS_NEEDSFSCK | 428 FS_INDEXDIRS | FS_ACLS | FS_MULTILABEL | 429 FS_FLAGS_UPDATED | FS_DOWAPBL); 430 if (fsflags != 0) 431 printf("unknown flags (%#x)", fsflags); 432 printf("\nfsmnt\t%s\n", fs->fs_fsmnt); 433 if (!printold) 434 printf("volname\t%s\tswuid\t%ju\n", 435 fs->fs_volname, (uintmax_t)fs->fs_swuid); 436 if (printold) { 437 if (fs->fs_old_cpc != 0) 438 printf("blocks available in each of %d rotational " 439 "positions\n", fs->fs_old_nrpos); 440 else 441 printf("(no rotational position table)\n\n"); 442 if (ISOPT(opt_verbose)) { 443 int c, j, k; 444 for (c = 0; c < fs->fs_old_cpc; c++) { 445 printf("cylinder number %d:", c); 446 for (i = 0; i < fs->fs_old_nrpos; i++) { 447 if (old_fs_postbl(&afs, c, opostbl)[i] == -1) 448 continue; 449 printf("\n position %d:\t", i); 450 for (j = old_fs_postbl(&afs, c, opostbl)[i], k = 1; ; 451 j += old_fs_rotbl(&afs)[j], k++) { 452 printf("%5d", j); 453 if (k % 12 == 0) 454 printf("\n\t\t"); 455 if (old_fs_rotbl(&afs)[j] == 0) 456 break; 457 } 458 } 459 printf("\n"); 460 } 461 } 462 } 463 464 return 0; 465 } 466 467 static int 468 print_cgsum(const char *name, int fd) 469 { 470 struct csum *ccsp; 471 int i, j, size; 472 473 afs.fs_csp = calloc(1, afs.fs_cssize); 474 for (i = 0, j = 0; i < afs.fs_cssize; i += afs.fs_bsize, j++) { 475 size = afs.fs_cssize - i < afs.fs_bsize ? 476 afs.fs_cssize - i : afs.fs_bsize; 477 ccsp = (struct csum *)((char *)afs.fs_csp + i); 478 if (lseek(fd, 479 (off_t)(fsbtodb(&afs, (afs.fs_csaddr + j * afs.fs_frag))) * 480 dev_bsize, SEEK_SET) == (off_t)-1) 481 return 1; 482 if (read(fd, ccsp, size) != size) 483 return 1; 484 if (needswap) 485 ffs_csum_swap(ccsp, ccsp, size); 486 } 487 488 printf("cs[].cs_(nbfree,ndir,nifree,nffree):\n\t"); 489 for (i = 0; i < afs.fs_ncg; i++) { 490 struct csum *cs = &afs.fs_cs(&afs, i); 491 if (i && i % 4 == 0) 492 printf("\n\t"); 493 printf("(%d,%d,%d,%d) ", 494 cs->cs_nbfree, cs->cs_ndir, cs->cs_nifree, cs->cs_nffree); 495 } 496 printf("\n"); 497 if (printold && (afs.fs_old_ncyl % afs.fs_old_cpg)) { 498 printf("cylinders in last group %d\n", 499 i = afs.fs_old_ncyl % afs.fs_old_cpg); 500 printf("blocks in last group %d\n", 501 i * afs.fs_old_spc / (afs.fs_old_nspf << afs.fs_fragshift)); 502 } 503 free(afs.fs_csp); 504 afs.fs_csp = NULL; 505 506 return 0; 507 } 508 509 static int 510 print_alt_super(const char *name, int fd) 511 { 512 union fsun alt; 513 int i; 514 off_t loc; 515 uint16_t alt_opostblsave[32*8]; 516 int save_printold; 517 518 for (i = 0; i < afs.fs_ncg; i++) { 519 loc = (off_t)(fsbtodb(&afs, cgsblock(&afs, i))) * dev_bsize; 520 printf("\nalternate %d\n", i); 521 if (pread(fd, &alt, sizeof alt, loc) != sizeof alt) { 522 warnx("%s: error reading alt %d", name, i); 523 return (1); 524 } 525 save_printold = printold; 526 fix_superblock(&alt.fs, alt_opostblsave); 527 if (print_superblock(&alt.fs, alt_opostblsave, name, fd, loc)) { 528 printold = save_printold; 529 return 1; 530 } 531 printold = save_printold; 532 } 533 return 0; 534 } 535 536 static int 537 print_cginfo(const char *name, int fd) 538 { 539 int i; 540 541 printf("\n"); 542 for (i = 0; i < afs.fs_ncg; i++) { 543 printf("\n"); 544 if (dumpcg(name, fd, i)) 545 return 1; 546 if (ISOPT(opt_inodes) && print_inodes(name, fd, i, 1)) 547 return 1; 548 } 549 return 0; 550 } 551 552 static int 553 print_inodes(const char *name, int fd, int c, int n) 554 { 555 void *ino_buf = malloc(afs.fs_bsize); 556 void (*print_inode)(int, int, void *); 557 int i, inum; 558 559 if (ino_buf == 0) 560 return 1; 561 562 print_inode = is_ufs2 ? print_ufs2_inode : print_ufs1_inode; 563 564 for (inum = c * afs.fs_ipg ; inum < (c+n) * afs.fs_ipg; inum += afs.fs_inopb) { 565 if (pread(fd, ino_buf, afs.fs_bsize, 566 ino_to_fsba(&afs, inum) * afs.fs_fsize) != afs.fs_bsize) { 567 free(ino_buf); 568 return 1; 569 } 570 for (i = 0; i < afs.fs_inopb; i++) 571 print_inode(inum + i, i, ino_buf); 572 } 573 574 free(ino_buf); 575 return 0; 576 } 577 578 static int 579 dumpcg(const char *name, int fd, int c) 580 { 581 off_t cur; 582 int i, j; 583 time_t t; 584 585 printf("cg %d:\n", c); 586 if ((cur = lseek(fd, (off_t)(fsbtodb(&afs, cgtod(&afs, c))) * dev_bsize, 587 SEEK_SET)) == (off_t)-1) 588 return (1); 589 if (read(fd, &acg, afs.fs_bsize) != afs.fs_bsize) { 590 warnx("%s: error reading cg", name); 591 return (1); 592 } 593 if (needswap) 594 ffs_cg_swap(&acg, &acg, &afs); 595 if (printold) 596 t = acg.cg_old_time; 597 else 598 t = acg.cg_time; 599 printf("magic\t%x\ttell\t%llx\ttime\t%s", 600 afs.fs_old_postblformat == FS_42POSTBLFMT ? 601 ((struct ocg *)&acg)->cg_magic : acg.cg_magic, 602 (long long)cur, ctime(&t)); 603 if (printold) 604 printf("cgx\t%d\tncyl\t%d\tniblk\t%d\tndblk\t%d\n", 605 acg.cg_cgx, acg.cg_old_ncyl, acg.cg_old_niblk, 606 acg.cg_ndblk); 607 else 608 printf("cgx\t%d\tniblk\t%d\tndblk\t%d\n", 609 acg.cg_cgx, acg.cg_niblk, acg.cg_ndblk); 610 printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n", 611 acg.cg_cs.cs_nbfree, acg.cg_cs.cs_ndir, 612 acg.cg_cs.cs_nifree, acg.cg_cs.cs_nffree); 613 printf("rotor\t%d\tirotor\t%d\tfrotor\t%d\nfrsum", 614 acg.cg_rotor, acg.cg_irotor, acg.cg_frotor); 615 for (i = 1, j = 0; i < afs.fs_frag; i++) { 616 printf("\t%d", acg.cg_frsum[i]); 617 j += i * acg.cg_frsum[i]; 618 } 619 printf("\nsum of frsum: %d", j); 620 if (afs.fs_contigsumsize > 0) { 621 for (i = 1; i < afs.fs_contigsumsize; i++) { 622 if ((i - 1) % 8 == 0) 623 printf("\nclusters %d-%d:", i, 624 afs.fs_contigsumsize - 1 < i + 7 ? 625 afs.fs_contigsumsize - 1 : i + 7); 626 printf("\t%d", cg_clustersum(&acg, 0)[i]); 627 } 628 printf("\nclusters size %d and over: %d\n", 629 afs.fs_contigsumsize, 630 cg_clustersum(&acg, 0)[afs.fs_contigsumsize]); 631 printf("clusters free:\t"); 632 pbits(0, cg_clustersfree(&acg, 0), acg.cg_nclusterblks); 633 } else 634 printf("\n"); 635 printf("iused:\t"); 636 pbits(0 * c * afs.fs_ipg, cg_inosused(&acg, 0), afs.fs_ipg); 637 printf("free:\t"); 638 pbits(0, cg_blksfree(&acg, 0), afs.fs_fpg); 639 if (printold && ISOPT(opt_verbose)) { 640 printf("b:\n"); 641 for (i = 0; i < afs.fs_old_cpg; i++) { 642 if (old_cg_blktot(&acg, 0)[i] == 0) 643 continue; 644 printf(" c%d:\t(%d)\t", i, old_cg_blktot(&acg, 0)[i]); 645 for (j = 0; j < afs.fs_old_nrpos; j++) { 646 if (afs.fs_old_cpc > 0 && 647 old_fs_postbl(&afs, i % afs.fs_old_cpc, opostblsave)[j] == -1) 648 continue; 649 printf(" %d", old_cg_blks(&afs, &acg, i, 0)[j]); 650 } 651 printf("\n"); 652 } 653 } 654 return (0); 655 } 656 657 static void 658 print_ufs1_inode(int inum, int i_off, void *ibuf) 659 { 660 struct ufs1_dinode *i = ibuf; 661 662 i += i_off; 663 664 if (needswap) 665 ffs_dinode1_swap(i,i); 666 667 if (afs.fs_old_inodefmt < FS_44INODEFMT) { 668 i->di_uid = i->di_ouid; 669 i->di_gid = i->di_ogid; 670 } 671 672 if (inum % afs.fs_ipg == 0) 673 printf(" inode: mode nlink size" 674 " ctime.nsec flags blocks" 675 " generation uid gid\n"); 676 if (i->di_mode == 0 && i->di_nlink == 0 && !ISOPT(opt_verbose)) 677 return; 678 printf("%8u: %6o %6d %20" PRIu64 " %10u.%09u %8x %10u %8x %10u %10u\n", 679 inum, i->di_mode, i->di_nlink, i->di_size, 680 i->di_ctime, i->di_ctimensec, i->di_flags, i->di_blocks, 681 i->di_gen, i->di_uid, i->di_gid); 682 } 683 684 static void 685 print_ufs2_inode(int inum, int i_off, void *ibuf) 686 { 687 struct ufs2_dinode *i = ibuf; 688 689 i += i_off; 690 691 if (needswap) 692 ffs_dinode2_swap(i,i); 693 694 if (inum % afs.fs_ipg == 0) 695 printf(" inode: mode nlink size" 696 " ctime.nsec flags blocks" 697 " generation uid gid\n"); 698 699 if (i->di_mode == 0 && i->di_nlink == 0 && !ISOPT(opt_verbose)) 700 return; 701 702 printf("%8u: %6o %6d %20" PRIu64 " %10" PRIu64 ".%09u %8x %10" PRIu64 " %8x %10u %10u\n", 703 inum, i->di_mode, i->di_nlink, i->di_size, 704 i->di_ctime, i->di_ctimensec, i->di_flags, i->di_blocks, 705 i->di_gen, i->di_uid, i->di_gid); 706 } 707 708 static void 709 pbits(int offset, void *vp, int max) 710 { 711 int i; 712 char *p; 713 int count, j; 714 715 for (count = i = 0, p = vp; i < max; i++) 716 if (isset(p, i)) { 717 if (count) 718 printf(",%s", count % 6 ? " " : "\n\t"); 719 count++; 720 printf("%d", offset + i); 721 j = i; 722 while ((i+1)<max && isset(p, i+1)) 723 i++; 724 if (i != j) 725 printf("-%d", offset + i); 726 } 727 printf("\n"); 728 } 729 730 static int 731 print_journal(const char *name, int fd) 732 { 733 daddr_t off; 734 size_t count, blklen, bno, skip; 735 off_t boff, head, tail, len; 736 uint32_t generation; 737 738 if (afs.fs_journal_version != UFS_WAPBL_VERSION) 739 return 0; 740 741 generation = 0; 742 head = tail = 0; 743 744 switch (afs.fs_journal_location) { 745 case UFS_WAPBL_JOURNALLOC_END_PARTITION: 746 case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM: 747 748 off = afs.fs_journallocs[0]; 749 count = afs.fs_journallocs[1]; 750 blklen = afs.fs_journallocs[2]; 751 752 for (bno=0; bno<count; bno += skip / blklen) { 753 754 skip = blklen; 755 756 boff = bno * blklen; 757 if (bno * blklen >= 2 * blklen && 758 ((head >= tail && (boff < tail || boff >= head)) || 759 (head < tail && (boff >= head && boff < tail)))) 760 continue; 761 762 printf("journal block %lu offset %lld\n", 763 (unsigned long)bno, (long long) boff); 764 765 if (lseek(fd, (off_t)(off*blklen) + boff, SEEK_SET) 766 == (off_t)-1) 767 return (1); 768 if (read(fd, &jbuf, blklen) != (ssize_t)blklen) { 769 warnx("%s: error reading journal", name); 770 return 1; 771 } 772 773 switch (awh.wc_type) { 774 case 0: 775 break; 776 case WAPBL_WC_HEADER: 777 print_journal_header(name); 778 if (awh.wc_generation > generation) { 779 head = awh.wc_head; 780 tail = awh.wc_tail; 781 } 782 generation = awh.wc_generation; 783 skip = awh.wc_len; 784 break; 785 default: 786 len = print_journal_entries(name, blklen); 787 skip = awh.wc_len; 788 if (len != (off_t)skip) 789 printf(" CORRUPTED RECORD\n"); 790 break; 791 } 792 793 if (blklen == 0) 794 break; 795 796 skip = (skip + blklen - 1) / blklen * blklen; 797 if (skip == 0) 798 break; 799 800 } 801 break; 802 } 803 804 return 0; 805 } 806 807 static const char * 808 wapbl_type_string(unsigned t) 809 { 810 static char buf[12]; 811 812 switch (t) { 813 case WAPBL_WC_BLOCKS: 814 return "blocks"; 815 case WAPBL_WC_REVOCATIONS: 816 return "revocations"; 817 case WAPBL_WC_INODES: 818 return "inodes"; 819 case WAPBL_WC_HEADER: 820 return "header"; 821 } 822 823 snprintf(buf,sizeof(buf),"%08x",t); 824 return buf; 825 } 826 827 static void 828 print_journal_header(const char *name) 829 { 830 printf(" type %s len %d version %u\n", 831 wapbl_type_string(awh.wc_type), awh.wc_len, 832 awh.wc_version); 833 printf(" checksum %08x generation %9u\n", 834 awh.wc_checksum, awh.wc_generation); 835 printf(" fsid %08x.%08x time %llu nsec %u\n", 836 awh.wc_fsid[0], awh.wc_fsid[1], 837 (unsigned long long)awh.wc_time, awh.wc_timensec); 838 printf(" log_bshift %10u fs_bshift %10u\n", 839 awh.wc_log_dev_bshift, awh.wc_fs_dev_bshift); 840 printf(" head %10lld tail %10lld\n", 841 (long long)awh.wc_head, (long long)awh.wc_tail); 842 printf(" circ_off %10lld circ_size %10lld\n", 843 (long long)awh.wc_circ_off, (long long)awh.wc_circ_size); 844 } 845 846 static off_t 847 print_journal_entries(const char *name, size_t blklen) 848 { 849 int i, n; 850 struct wapbl_wc_blocklist *wcb; 851 struct wapbl_wc_inodelist *wci; 852 off_t len = 0; 853 int ph; 854 855 printf(" type %s len %d", 856 wapbl_type_string(awn.wc_type), awn.wc_len); 857 858 switch (awn.wc_type) { 859 case WAPBL_WC_BLOCKS: 860 case WAPBL_WC_REVOCATIONS: 861 wcb = (struct wapbl_wc_blocklist *)&awn; 862 printf(" blkcount %u\n", wcb->wc_blkcount); 863 ph = (blklen - offsetof(struct wapbl_wc_blocklist, wc_blocks)) 864 / sizeof(wcb->wc_blocks[0]); 865 n = MIN(wcb->wc_blkcount, ph); 866 for (i=0; i<n; i++) { 867 if (ISOPT(opt_verbose)) { 868 printf(" %3d: daddr %14llu dlen %d\n", i, 869 (unsigned long long)wcb->wc_blocks[i].wc_daddr, 870 wcb->wc_blocks[i].wc_dlen); 871 } 872 len += wcb->wc_blocks[i].wc_dlen; 873 } 874 if (awn.wc_type == WAPBL_WC_BLOCKS) { 875 if (len % blklen) 876 len += blklen - len % blklen; 877 } else 878 len = 0; 879 break; 880 case WAPBL_WC_INODES: 881 wci = (struct wapbl_wc_inodelist *)&awn; 882 printf(" count %u clear %u\n", 883 wci->wc_inocnt, wci->wc_clear); 884 ph = (blklen - offsetof(struct wapbl_wc_inodelist, wc_inodes)) 885 / sizeof(wci->wc_inodes[0]); 886 n = MIN(wci->wc_inocnt, ph); 887 for (i=0; i<n; ++i) { 888 if (ISOPT(opt_verbose)) { 889 printf(" %3d: inumber %10u imode %08x\n", i, 890 wci->wc_inodes[i].wc_inumber, 891 wci->wc_inodes[i].wc_imode); 892 } 893 } 894 break; 895 default: 896 printf("\n"); 897 break; 898 } 899 900 return len + blklen; 901 } 902 903 static void 904 usage(void) 905 { 906 907 (void)fprintf(stderr, "usage: dumpfs [-acFijmsv] filesys | device [...]\n"); 908 exit(1); 909 } 910 911 static int 912 openpartition(const char *name, int flags, char *device, size_t devicelen) 913 { 914 char rawspec[MAXPATHLEN], xbuf[MAXPATHLEN], *p; 915 struct fstab *fs; 916 int fd, oerrno; 917 918 fs = getfsfile(name); 919 if (fs) { 920 const char *fsspec; 921 fsspec = getfsspecname(xbuf, sizeof(xbuf), fs->fs_spec); 922 if (fsspec == NULL) { 923 warn("%s", xbuf); 924 return -1; 925 } 926 if ((p = strrchr(fsspec, '/')) != NULL) { 927 snprintf(rawspec, sizeof(rawspec), "%.*s/r%s", 928 (int)(p - fsspec), fsspec, p + 1); 929 name = rawspec; 930 } else 931 name = fsspec; 932 } 933 fd = opendisk(name, flags, device, devicelen, 0); 934 if (fd == -1 && errno == ENOENT) { 935 oerrno = errno; 936 strlcpy(device, name, devicelen); 937 errno = oerrno; 938 } 939 return (fd); 940 } 941