Home | History | Annotate | Line # | Download | only in dumplfs
dumplfs.c revision 1.41
      1 /*	$NetBSD: dumplfs.c,v 1.41 2013/06/18 18:18:58 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1991, 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 
     34 #ifndef lint
     35 __COPYRIGHT("@(#) Copyright (c) 1991, 1993\
     36  The Regents of the University of California.  All rights reserved.");
     37 #endif /* not lint */
     38 
     39 #ifndef lint
     40 #if 0
     41 static char sccsid[] = "@(#)dumplfs.c	8.5 (Berkeley) 5/24/95";
     42 #else
     43 __RCSID("$NetBSD: dumplfs.c,v 1.41 2013/06/18 18:18:58 christos Exp $");
     44 #endif
     45 #endif /* not lint */
     46 
     47 #include <sys/param.h>
     48 #include <sys/ucred.h>
     49 #include <sys/mount.h>
     50 #include <sys/time.h>
     51 
     52 #include <ufs/lfs/lfs.h>
     53 
     54 #include <err.h>
     55 #include <errno.h>
     56 #include <fcntl.h>
     57 #include <fstab.h>
     58 #include <stdlib.h>
     59 #include <stdio.h>
     60 #include <string.h>
     61 #include <unistd.h>
     62 #include "extern.h"
     63 
     64 static void	addseg(char *);
     65 static void	dump_cleaner_info(struct lfs *, void *);
     66 static void	dump_dinode(struct ulfs1_dinode *);
     67 static void	dump_ifile(int, struct lfs *, int, int, daddr_t);
     68 static int	dump_ipage_ifile(struct lfs *, int, char *, int);
     69 static int	dump_ipage_segusage(struct lfs *, int, char *, int);
     70 static void	dump_segment(int, int, daddr_t, struct lfs *, int);
     71 static int	dump_sum(int, struct lfs *, SEGSUM *, int, daddr_t);
     72 static void	dump_super(struct lfs *);
     73 static void	usage(void);
     74 
     75 extern uint32_t	cksum(void *, size_t);
     76 
     77 typedef struct seglist SEGLIST;
     78 struct seglist {
     79         SEGLIST *next;
     80 	int num;
     81 };
     82 SEGLIST	*seglist;
     83 
     84 char *special;
     85 
     86 /* Segment Usage formats */
     87 #define print_suheader \
     88 	(void)printf("segnum\tflags\tnbytes\tninos\tnsums\tlastmod\n")
     89 
     90 static inline void
     91 print_suentry(int i, SEGUSE *sp, struct lfs *fs)
     92 {
     93 	time_t t;
     94 	char flags[4] = "   ";
     95 
     96 	if (sp->su_flags & SEGUSE_ACTIVE)
     97 		flags[0] = 'A';
     98 	if (sp->su_flags & SEGUSE_DIRTY)
     99 		flags[1] = 'D';
    100 	else
    101 		flags[1] = 'C';
    102 	if (sp->su_flags & SEGUSE_SUPERBLOCK)
    103 		flags[2] = 'S';
    104 
    105 	t = (fs->lfs_version == 1 ? sp->su_olastmod : sp->su_lastmod);
    106 
    107 	printf("%d\t%s\t%d\t%d\t%d\t%s", i, flags,
    108 		sp->su_nbytes, sp->su_ninos, sp->su_nsums,
    109 		ctime(&t));
    110 }
    111 
    112 /* Ifile formats */
    113 #define print_iheader \
    114 	(void)printf("inum\tstatus\tversion\tdaddr\t\tfreeptr\n")
    115 
    116 static inline void
    117 print_ientry(int i, IFILE *ip)
    118 {
    119 	if (ip->if_daddr == LFS_UNUSED_DADDR)
    120 		printf("%d\tFREE\t%d\t \t\t%llu\n", i, ip->if_version,
    121 		    (unsigned long long)ip->if_nextfree);
    122 	else
    123 		printf("%d\tINUSE\t%d\t%8X\t%s\n",
    124 		    i, ip->if_version, ip->if_daddr,
    125 		    (ip->if_nextfree == LFS_ORPHAN_NEXTFREE ? "FFFFFFFF" : "-"));
    126 }
    127 
    128 #define fsbtobyte(fs, b)	lfs_fsbtob((fs), (off_t)((b)))
    129 
    130 int datasum_check = 0;
    131 
    132 int
    133 main(int argc, char **argv)
    134 {
    135 	struct lfs lfs_sb1, lfs_sb2, *lfs_master;
    136 	daddr_t seg_addr, idaddr, sbdaddr;
    137 	int ch, do_allsb, do_ientries, do_segentries, fd, segnum;
    138 	void *sbuf;
    139 
    140 	do_allsb = 0;
    141 	do_ientries = 0;
    142 	do_segentries = 0;
    143 	idaddr = 0x0;
    144 	sbdaddr = 0x0;
    145 	while ((ch = getopt(argc, argv, "ab:diI:Ss:")) != -1)
    146 		switch(ch) {
    147 		case 'a':		/* Dump all superblocks */
    148 			do_allsb = 1;
    149 			break;
    150 		case 'b':		/* Use this superblock */
    151 			sbdaddr = strtol(optarg, NULL, 0);
    152 			break;
    153 		case 'd':
    154 			datasum_check = 1;
    155 			break;
    156 		case 'i':		/* Dump ifile entries */
    157 			do_ientries = !do_ientries;
    158 			break;
    159 		case 'I':		/* Use this ifile inode */
    160 			idaddr = strtol(optarg, NULL, 0);
    161 			break;
    162 		case 'S':
    163 			do_segentries = !do_segentries;
    164 			break;
    165 		case 's':		/* Dump out these segments */
    166 			addseg(optarg);
    167 			break;
    168 		default:
    169 			usage();
    170 		}
    171 	argc -= optind;
    172 	argv += optind;
    173 
    174 	if (argc != 1)
    175 		usage();
    176 
    177 	special = argv[0];
    178 	if ((fd = open(special, O_RDONLY, 0)) < 0)
    179 		err(1, "%s", special);
    180 
    181 	sbuf = malloc(LFS_SBPAD);
    182 	if (sbuf == NULL)
    183 		err(1, "malloc");
    184 
    185 	if (sbdaddr == 0x0) {
    186 		/* Read the proto-superblock */
    187 		get(fd, LFS_LABELPAD, sbuf, LFS_SBPAD);
    188 		memcpy(&(lfs_sb1.lfs_dlfs), sbuf, sizeof(struct dlfs));
    189 
    190 		/* If that wasn't the real first sb, get the real first sb */
    191 		if (lfs_sb1.lfs_version > 1 &&
    192 		    lfs_sb1.lfs_sboffs[0] > lfs_btofsb(&lfs_sb1, LFS_LABELPAD))
    193 			get(fd, lfs_fsbtob(&lfs_sb1, lfs_sb1.lfs_sboffs[0]),
    194 			    &(lfs_sb1.lfs_dlfs), sizeof(struct dlfs));
    195 
    196 		/*
    197 	 	* Read the second superblock and figure out which check point is
    198 	 	* most up to date.
    199 	 	*/
    200 		get(fd,
    201 		    fsbtobyte(&lfs_sb1, lfs_sb1.lfs_sboffs[1]),
    202 		    sbuf, LFS_SBPAD);
    203 		memcpy(&(lfs_sb2.lfs_dlfs), sbuf, sizeof(struct dlfs));
    204 
    205 		lfs_master = &lfs_sb1;
    206 		if (lfs_sb1.lfs_version > 1) {
    207 			if (lfs_sb1.lfs_serial > lfs_sb2.lfs_serial) {
    208 				lfs_master = &lfs_sb2;
    209 				sbdaddr = lfs_sb1.lfs_sboffs[1];
    210 			} else
    211 				sbdaddr = lfs_sb1.lfs_sboffs[0];
    212 		} else {
    213 			if (lfs_sb1.lfs_otstamp > lfs_sb2.lfs_otstamp) {
    214 				lfs_master = &lfs_sb2;
    215 				sbdaddr = lfs_sb1.lfs_sboffs[1];
    216 			} else
    217 				sbdaddr = lfs_sb1.lfs_sboffs[0];
    218 		}
    219 	} else {
    220 		/* Read the first superblock */
    221 		get(fd, dbtob((off_t)sbdaddr), sbuf, LFS_SBPAD);
    222 		memcpy(&(lfs_sb1.lfs_dlfs), sbuf, sizeof(struct dlfs));
    223 		lfs_master = &lfs_sb1;
    224 	}
    225 
    226 	free(sbuf);
    227 
    228 	/* Compatibility */
    229 	if (lfs_master->lfs_version == 1) {
    230 		lfs_master->lfs_sumsize = LFS_V1_SUMMARY_SIZE;
    231 		lfs_master->lfs_ibsize = lfs_master->lfs_bsize;
    232 		lfs_master->lfs_start = lfs_master->lfs_sboffs[0];
    233 		lfs_master->lfs_tstamp = lfs_master->lfs_otstamp;
    234 		lfs_master->lfs_fsbtodb = 0;
    235 	}
    236 
    237 	(void)printf("Master Superblock at 0x%llx:\n", (long long)sbdaddr);
    238 	dump_super(lfs_master);
    239 
    240 	dump_ifile(fd, lfs_master, do_ientries, do_segentries, idaddr);
    241 
    242 	if (seglist != NULL)
    243 		for (; seglist != NULL; seglist = seglist->next) {
    244 			seg_addr = lfs_sntod(lfs_master, seglist->num);
    245 			dump_segment(fd, seglist->num, seg_addr, lfs_master,
    246 				     do_allsb);
    247 		}
    248 	else
    249 		for (segnum = 0, seg_addr = lfs_sntod(lfs_master, 0);
    250 		     segnum < lfs_master->lfs_nseg;
    251 		     segnum++, seg_addr = lfs_sntod(lfs_master, segnum))
    252 			dump_segment(fd, segnum, seg_addr, lfs_master,
    253 				     do_allsb);
    254 
    255 	(void)close(fd);
    256 	exit(0);
    257 }
    258 
    259 /*
    260  * We are reading all the blocks of an inode and dumping out the ifile table.
    261  * This code could be tighter, but this is a first pass at getting the stuff
    262  * printed out rather than making this code incredibly efficient.
    263  */
    264 static void
    265 dump_ifile(int fd, struct lfs *lfsp, int do_ientries, int do_segentries, daddr_t addr)
    266 {
    267 	char *ipage;
    268 	struct ulfs1_dinode *dip, *dpage;
    269 	/* XXX ondisk32 */
    270 	int32_t *addrp, *dindir, *iaddrp, *indir;
    271 	int block_limit, i, inum, j, nblocks, psize;
    272 
    273 	psize = lfsp->lfs_bsize;
    274 	if (!addr)
    275 		addr = lfsp->lfs_idaddr;
    276 
    277 	if (!(dpage = malloc(psize)))
    278 		err(1, "malloc");
    279 	get(fd, fsbtobyte(lfsp, addr), dpage, psize);
    280 
    281 	for (dip = dpage + LFS_INOPB(lfsp) - 1; dip >= dpage; --dip)
    282 		if (dip->di_inumber == LFS_IFILE_INUM)
    283 			break;
    284 
    285 	if (dip < dpage) {
    286 		warnx("unable to locate ifile inode at disk address 0x%llx",
    287 		     (long long)addr);
    288 		return;
    289 	}
    290 
    291 	(void)printf("\nIFILE inode\n");
    292 	dump_dinode(dip);
    293 
    294 	(void)printf("\nIFILE contents\n");
    295 	nblocks = dip->di_size >> lfsp->lfs_bshift;
    296 	block_limit = MIN(nblocks, ULFS_NDADDR);
    297 
    298 	/* Get the direct block */
    299 	if ((ipage = malloc(psize)) == NULL)
    300 		err(1, "malloc");
    301 	for (inum = 0, addrp = dip->di_db, i = 0; i < block_limit;
    302 	    i++, addrp++) {
    303 		get(fd, fsbtobyte(lfsp, *addrp), ipage, psize);
    304 		if (i < lfsp->lfs_cleansz) {
    305 			dump_cleaner_info(lfsp, ipage);
    306 			if (do_segentries)
    307 				print_suheader;
    308 			continue;
    309 		}
    310 
    311 		if (i < (lfsp->lfs_segtabsz + lfsp->lfs_cleansz)) {
    312 			if (do_segentries)
    313 				inum = dump_ipage_segusage(lfsp, inum, ipage,
    314 							   lfsp->lfs_sepb);
    315 			else
    316 				inum = (i < lfsp->lfs_segtabsz + lfsp->lfs_cleansz - 1);
    317 			if (!inum) {
    318 				if(!do_ientries)
    319 					goto e0;
    320 				else
    321 					print_iheader;
    322 			}
    323 		} else
    324 			inum = dump_ipage_ifile(lfsp, inum, ipage, lfsp->lfs_ifpb);
    325 	}
    326 
    327 	if (nblocks <= ULFS_NDADDR)
    328 		goto e0;
    329 
    330 	/* Dump out blocks off of single indirect block */
    331 	if (!(indir = malloc(psize)))
    332 		err(1, "malloc");
    333 	get(fd, fsbtobyte(lfsp, dip->di_ib[0]), indir, psize);
    334 	block_limit = MIN(i + lfsp->lfs_nindir, nblocks);
    335 	for (addrp = indir; i < block_limit; i++, addrp++) {
    336 		if (*addrp == LFS_UNUSED_DADDR)
    337 			break;
    338 		get(fd, fsbtobyte(lfsp, *addrp), ipage, psize);
    339 		if (i < lfsp->lfs_cleansz) {
    340 			dump_cleaner_info(lfsp, ipage);
    341 			continue;
    342 		}
    343 
    344 		if (i < lfsp->lfs_segtabsz + lfsp->lfs_cleansz) {
    345 			if (do_segentries)
    346 				inum = dump_ipage_segusage(lfsp, inum, ipage,
    347 							   lfsp->lfs_sepb);
    348 			else
    349 				inum = (i < lfsp->lfs_segtabsz + lfsp->lfs_cleansz - 1);
    350 			if (!inum) {
    351 				if(!do_ientries)
    352 					goto e1;
    353 				else
    354 					print_iheader;
    355 			}
    356 		} else
    357 			inum = dump_ipage_ifile(lfsp, inum, ipage, lfsp->lfs_ifpb);
    358 	}
    359 
    360 	if (nblocks <= lfsp->lfs_nindir * lfsp->lfs_ifpb)
    361 		goto e1;
    362 
    363 	/* Get the double indirect block */
    364 	if (!(dindir = malloc(psize)))
    365 		err(1, "malloc");
    366 	get(fd, fsbtobyte(lfsp, dip->di_ib[1]), dindir, psize);
    367 	for (iaddrp = dindir, j = 0; j < lfsp->lfs_nindir; j++, iaddrp++) {
    368 		if (*iaddrp == LFS_UNUSED_DADDR)
    369 			break;
    370 		get(fd, fsbtobyte(lfsp, *iaddrp), indir, psize);
    371 		block_limit = MIN(i + lfsp->lfs_nindir, nblocks);
    372 		for (addrp = indir; i < block_limit; i++, addrp++) {
    373 			if (*addrp == LFS_UNUSED_DADDR)
    374 				break;
    375 			get(fd, fsbtobyte(lfsp, *addrp), ipage, psize);
    376 			if (i < lfsp->lfs_cleansz) {
    377 				dump_cleaner_info(lfsp, ipage);
    378 				continue;
    379 			}
    380 
    381 			if (i < lfsp->lfs_segtabsz + lfsp->lfs_cleansz) {
    382 				if (do_segentries)
    383 					inum = dump_ipage_segusage(lfsp,
    384 						 inum, ipage, lfsp->lfs_sepb);
    385 				else
    386 					inum = (i < lfsp->lfs_segtabsz +
    387 						lfsp->lfs_cleansz - 1);
    388 				if (!inum) {
    389 					if(!do_ientries)
    390 						goto e2;
    391 					else
    392 						print_iheader;
    393 				}
    394 			} else
    395 				inum = dump_ipage_ifile(lfsp, inum,
    396 				    ipage, lfsp->lfs_ifpb);
    397 		}
    398 	}
    399 e2:	free(dindir);
    400 e1:	free(indir);
    401 e0:	free(dpage);
    402 	free(ipage);
    403 }
    404 
    405 static int
    406 dump_ipage_ifile(struct lfs *lfsp, int i, char *pp, int tot)
    407 {
    408 	char *ip;
    409 	int cnt, max, entsize;
    410 
    411 	if (lfsp->lfs_version == 1)
    412 		entsize = sizeof(IFILE_V1);
    413 	else
    414 		entsize = sizeof(IFILE);
    415 	max = i + tot;
    416 
    417 	for (ip = pp, cnt = i; cnt < max; cnt++, ip += entsize)
    418 		print_ientry(cnt, (IFILE *)ip);
    419 	return (max);
    420 }
    421 
    422 static int
    423 dump_ipage_segusage(struct lfs *lfsp, int i, char *pp, int tot)
    424 {
    425 	SEGUSE *sp;
    426 	int cnt, max;
    427 	struct seglist *slp;
    428 
    429 	max = i + tot;
    430 	for (sp = (SEGUSE *)pp, cnt = i;
    431 	     cnt < lfsp->lfs_nseg && cnt < max; cnt++) {
    432 		if (seglist == NULL)
    433 			print_suentry(cnt, sp, lfsp);
    434 		else {
    435 			for (slp = seglist; slp != NULL; slp = slp->next)
    436 				if (cnt == slp->num) {
    437 					print_suentry(cnt, sp, lfsp);
    438 					break;
    439 				}
    440 		}
    441 		if (lfsp->lfs_version > 1)
    442 			++sp;
    443 		else
    444 			sp = (SEGUSE *)((SEGUSE_V1 *)sp + 1);
    445 	}
    446 	if (max >= lfsp->lfs_nseg)
    447 		return (0);
    448 	else
    449 		return (max);
    450 }
    451 
    452 static void
    453 dump_dinode(struct ulfs1_dinode *dip)
    454 {
    455 	int i;
    456 	time_t at, mt, ct;
    457 
    458 	at = dip->di_atime;
    459 	mt = dip->di_mtime;
    460 	ct = dip->di_ctime;
    461 
    462 	(void)printf("    %so%o\t%s%d\t%s%d\t%s%d\t%s%llu\n",
    463 		"mode  ", dip->di_mode,
    464 		"nlink ", dip->di_nlink,
    465 		"uid   ", dip->di_uid,
    466 		"gid   ", dip->di_gid,
    467 		"size  ", (long long)dip->di_size);
    468 	(void)printf("    %s%s    %s%s    %s%s",
    469 		"atime ", ctime(&at),
    470 		"mtime ", ctime(&mt),
    471 		"ctime ", ctime(&ct));
    472 	(void)printf("    inum  %d\n", dip->di_inumber);
    473 	(void)printf("    Direct Addresses\n");
    474 	for (i = 0; i < ULFS_NDADDR; i++) {
    475 		(void)printf("\t0x%x", dip->di_db[i]);
    476 		if ((i % 6) == 5)
    477 			(void)printf("\n");
    478 	}
    479 	for (i = 0; i < ULFS_NIADDR; i++)
    480 		(void)printf("\t0x%x", dip->di_ib[i]);
    481 	(void)printf("\n");
    482 }
    483 
    484 static int
    485 dump_sum(int fd, struct lfs *lfsp, SEGSUM *sp, int segnum, daddr_t addr)
    486 {
    487 	FINFO *fp;
    488 	int32_t *dp, *idp;
    489 	int i, j, acc;
    490 	int ck;
    491 	int numbytes, numblocks;
    492 	char *datap;
    493 	struct ulfs1_dinode *inop;
    494 	size_t el_size;
    495 	u_int32_t datasum;
    496 	time_t t;
    497 	char *buf;
    498 
    499 	if (sp->ss_magic != SS_MAGIC ||
    500 	    sp->ss_sumsum != (ck = cksum(&sp->ss_datasum,
    501 	    lfsp->lfs_sumsize - sizeof(sp->ss_sumsum)))) {
    502 		/* Don't print "corrupt" if we're just too close to the edge */
    503 		if (lfs_dtosn(lfsp, addr + LFS_FSBTODB(lfsp, 1)) ==
    504 		    lfs_dtosn(lfsp, addr))
    505 			(void)printf("dumplfs: %s %d address 0x%llx\n",
    506 		                     "corrupt summary block; segment", segnum,
    507 				     (long long)addr);
    508 		return -1;
    509 	}
    510 	if (lfsp->lfs_version > 1 && sp->ss_ident != lfsp->lfs_ident) {
    511 		(void)printf("dumplfs: %s %d address 0x%llx\n",
    512 	                     "summary from a former life; segment", segnum,
    513 			     (long long)addr);
    514 		return -1;
    515 	}
    516 
    517 	(void)printf("Segment Summary Info at 0x%llx\n", (long long)addr);
    518 	(void)printf("    %s0x%x\t%s%d\t%s%d\t%s%c%c%c%c\n    %s0x%x\t%s0x%x",
    519 		"next     ", sp->ss_next,
    520 		"nfinfo   ", sp->ss_nfinfo,
    521 		"ninos    ", sp->ss_ninos,
    522 		"flags    ", (sp->ss_flags & SS_DIROP) ? 'D' : '-',
    523 			     (sp->ss_flags & SS_CONT)  ? 'C' : '-',
    524 			     (sp->ss_flags & SS_CLEAN)  ? 'L' : '-',
    525 			     (sp->ss_flags & SS_RFW)  ? 'R' : '-',
    526 		"sumsum   ", sp->ss_sumsum,
    527 		"datasum  ", sp->ss_datasum );
    528 	if (lfsp->lfs_version == 1) {
    529 		t = sp->ss_ocreate;
    530 		(void)printf("\tcreate   %s\n", ctime(&t));
    531 	} else {
    532 		t = sp->ss_create;
    533 		(void)printf("\tcreate   %s", ctime(&t));
    534 		(void)printf("    roll_id  %-8x", sp->ss_ident);
    535 		(void)printf("   serial   %lld\n", (long long)sp->ss_serial);
    536 	}
    537 
    538 	/* Dump out inode disk addresses */
    539 	dp = (int32_t *)sp;
    540 	dp += lfsp->lfs_sumsize / sizeof(int32_t);
    541 	inop = malloc(lfsp->lfs_bsize);
    542 	printf("    Inode addresses:");
    543 	numbytes = 0;
    544 	numblocks = 0;
    545 	for (dp--, i = 0; i < sp->ss_ninos; dp--) {
    546 		++numblocks;
    547 		numbytes += lfsp->lfs_ibsize;	/* add bytes for inode block */
    548 		printf("\t0x%x {", *dp);
    549 		get(fd, fsbtobyte(lfsp, *dp), inop, lfsp->lfs_ibsize);
    550 		for (j = 0; i < sp->ss_ninos && j < LFS_INOPB(lfsp); j++, i++) {
    551 			if (j > 0)
    552 				(void)printf(", ");
    553 			(void)printf("%dv%d", inop[j].di_inumber, inop[j].di_gen);
    554 		}
    555 		(void)printf("}");
    556 		if (((i/LFS_INOPB(lfsp)) % 4) == 3)
    557 			(void)printf("\n");
    558 	}
    559 	free(inop);
    560 
    561 	printf("\n");
    562 
    563 	if (lfsp->lfs_version == 1)
    564 		fp = (FINFO *)((SEGSUM_V1 *)sp + 1);
    565 	else
    566 		fp = (FINFO *)(sp + 1);
    567 	for (i = 0; i < sp->ss_nfinfo; i++) {
    568 		(void)printf("    FINFO for inode: %d version %d nblocks %d lastlength %d\n",
    569 		    fp->fi_ino, fp->fi_version, fp->fi_nblocks,
    570 		    fp->fi_lastlength);
    571 		dp = &(fp->fi_blocks[0]);
    572 		numblocks += fp->fi_nblocks;
    573 		for (j = 0; j < fp->fi_nblocks; j++, dp++) {
    574 			(void)printf("\t%d", *dp);
    575 			if ((j % 8) == 7)
    576 				(void)printf("\n");
    577 			if (j == fp->fi_nblocks - 1)
    578 				numbytes += fp->fi_lastlength;
    579 			else
    580 				numbytes += lfsp->lfs_bsize;
    581 		}
    582 		if ((j % 8) != 0)
    583 			(void)printf("\n");
    584 		fp = (FINFO *)dp;
    585 	}
    586 
    587 	if (datasum_check == 0)
    588 		return (numbytes);
    589 
    590 	/*
    591 	 * Now that we know the number of blocks, run back through and
    592 	 * compute the data checksum.  (A bad data checksum is not enough
    593 	 * to prevent us from continuing, but it odes merit a warning.)
    594 	 */
    595 	idp = (int32_t *)sp;
    596 	idp += lfsp->lfs_sumsize / sizeof(int32_t);
    597 	--idp;
    598 	if (lfsp->lfs_version == 1) {
    599 		fp = (FINFO *)((SEGSUM_V1 *)sp + 1);
    600 		el_size = sizeof(unsigned long);
    601 	} else {
    602 		fp = (FINFO *)(sp + 1);
    603 		el_size = sizeof(u_int32_t);
    604 	}
    605 	datap = (char *)malloc(el_size * numblocks);
    606 	memset(datap, 0, el_size * numblocks);
    607 	acc = 0;
    608 	addr += lfs_btofsb(lfsp, lfsp->lfs_sumsize);
    609 	buf = malloc(lfsp->lfs_bsize);
    610 	for (i = 0; i < sp->ss_nfinfo; i++) {
    611 		while (addr == *idp) {
    612 			get(fd, fsbtobyte(lfsp, addr), buf, lfsp->lfs_ibsize);
    613 			memcpy(datap + acc * el_size, buf, el_size);
    614 			addr += lfs_btofsb(lfsp, lfsp->lfs_ibsize);
    615 			--idp;
    616 			++acc;
    617 		}
    618 		for (j = 0; j < fp->fi_nblocks; j++) {
    619 			get(fd, fsbtobyte(lfsp, addr), buf, lfsp->lfs_fsize);
    620 			memcpy(datap + acc * el_size, buf, el_size);
    621 			if (j == fp->fi_nblocks - 1)
    622 				addr += lfs_btofsb(lfsp, fp->fi_lastlength);
    623 			else
    624 				addr += lfs_btofsb(lfsp, lfsp->lfs_bsize);
    625 			++acc;
    626 		}
    627 		fp = (FINFO *)&(fp->fi_blocks[fp->fi_nblocks]);
    628 	}
    629 	while (addr == *idp) {
    630 		get(fd, fsbtobyte(lfsp, addr), buf, lfsp->lfs_ibsize);
    631 		memcpy(datap + acc * el_size, buf, el_size);
    632 		addr += lfs_btofsb(lfsp, lfsp->lfs_ibsize);
    633 		--idp;
    634 		++acc;
    635 	}
    636 	free(buf);
    637 	if (acc != numblocks)
    638 		printf("** counted %d blocks but should have been %d\n",
    639 		     acc, numblocks);
    640 	datasum = cksum(datap, numblocks * el_size);
    641 	if (datasum != sp->ss_datasum)
    642 		printf("** computed datasum 0x%lx does not match given datasum 0x%lx\n", (unsigned long)datasum, (unsigned long)sp->ss_datasum);
    643 	free(datap);
    644 
    645 	return (numbytes);
    646 }
    647 
    648 static void
    649 dump_segment(int fd, int segnum, daddr_t addr, struct lfs *lfsp, int dump_sb)
    650 {
    651 	struct lfs lfs_sb, *sbp;
    652 	SEGSUM *sump;
    653 	char *sumblock;
    654 	int did_one, nbytes, sb;
    655 	off_t sum_offset;
    656 	daddr_t new_addr;
    657 
    658 	(void)printf("\nSEGMENT %lld (Disk Address 0x%llx)\n",
    659 		     (long long)lfs_dtosn(lfsp, addr), (long long)addr);
    660 	sum_offset = fsbtobyte(lfsp, addr);
    661 	sumblock = malloc(lfsp->lfs_sumsize);
    662 
    663 	if (lfsp->lfs_version > 1 && segnum == 0) {
    664 		if (lfs_fsbtob(lfsp, lfsp->lfs_start) < LFS_LABELPAD) {
    665 			/* First segment eats the disklabel */
    666 			sum_offset += lfs_fragroundup(lfsp, LFS_LABELPAD) -
    667 				      lfs_fsbtob(lfsp, lfsp->lfs_start);
    668 			addr += lfs_btofsb(lfsp, lfs_fragroundup(lfsp, LFS_LABELPAD)) -
    669 				lfsp->lfs_start;
    670 			printf("Disklabel at 0x0\n");
    671 		}
    672 	}
    673 
    674 	sb = 0;
    675 	did_one = 0;
    676 	do {
    677 		get(fd, sum_offset, sumblock, lfsp->lfs_sumsize);
    678 		sump = (SEGSUM *)sumblock;
    679 		if ((lfsp->lfs_version > 1 &&
    680 		     sump->ss_ident != lfsp->lfs_ident) ||
    681 		    sump->ss_sumsum != cksum (&sump->ss_datasum,
    682 			      lfsp->lfs_sumsize - sizeof(sump->ss_sumsum))) {
    683 			sbp = (struct lfs *)sump;
    684 			if ((sb = (sbp->lfs_magic == LFS_MAGIC))) {
    685 				printf("Superblock at 0x%x\n",
    686 				       (unsigned)lfs_btofsb(lfsp, sum_offset));
    687 				if (dump_sb)  {
    688 					get(fd, sum_offset, &(lfs_sb.lfs_dlfs),
    689 					    sizeof(struct dlfs));
    690 					dump_super(&lfs_sb);
    691 				}
    692 				if (lfsp->lfs_version > 1)
    693 					sum_offset += lfs_fragroundup(lfsp, LFS_SBPAD);
    694 				else
    695 					sum_offset += LFS_SBPAD;
    696 			} else if (did_one)
    697 				break;
    698 			else {
    699 				printf("Segment at 0x%llx empty or corrupt\n",
    700                                        (long long)addr);
    701 				break;
    702 			}
    703 		} else {
    704 			nbytes = dump_sum(fd, lfsp, sump, segnum,
    705 				lfs_btofsb(lfsp, sum_offset));
    706 			if (nbytes >= 0)
    707 				sum_offset += lfsp->lfs_sumsize + nbytes;
    708 			else
    709 				sum_offset = 0;
    710 			did_one = 1;
    711 		}
    712 		/* If the segment ends right on a boundary, it still ends */
    713 		new_addr = lfs_btofsb(lfsp, sum_offset);
    714 		/* printf("end daddr = 0x%lx\n", (long)new_addr); */
    715 		if (lfs_dtosn(lfsp, new_addr) != lfs_dtosn(lfsp, addr))
    716 			break;
    717 	} while (sum_offset);
    718 
    719 	free(sumblock);
    720 }
    721 
    722 static void
    723 dump_super(struct lfs *lfsp)
    724 {
    725 	int i;
    726 
    727  	(void)printf("    %s0x%-8x  %s0x%-8x  %s%-10d\n",
    728  		     "magic    ", lfsp->lfs_magic,
    729  		     "version  ", lfsp->lfs_version,
    730  		     "size     ", lfsp->lfs_size);
    731  	(void)printf("    %s%-10d  %s%-10d  %s%-10d\n",
    732  		     "ssize    ", lfsp->lfs_ssize,
    733  		     "dsize    ", lfsp->lfs_dsize,
    734  		     "bsize    ", lfsp->lfs_bsize);
    735  	(void)printf("    %s%-10d  %s%-10d  %s%-10d\n",
    736  		     "fsize    ", lfsp->lfs_fsize,
    737  		     "frag     ", lfsp->lfs_frag,
    738  		     "minfree  ", lfsp->lfs_minfree);
    739  	(void)printf("    %s%-10d  %s%-10d  %s%-10d\n",
    740  		     "inopb    ", lfsp->lfs_inopb,
    741  		     "ifpb     ", lfsp->lfs_ifpb,
    742  		     "nindir   ", lfsp->lfs_nindir);
    743  	(void)printf("    %s%-10d  %s%-10d  %s%-10d\n",
    744  		     "nseg     ", lfsp->lfs_nseg,
    745  		     "sepb     ", lfsp->lfs_sepb,
    746  		     "cleansz  ", lfsp->lfs_cleansz);
    747  	(void)printf("    %s%-10d  %s0x%-8x  %s%-10d\n",
    748  		     "segtabsz ", lfsp->lfs_segtabsz,
    749  		     "segmask  ", lfsp->lfs_segmask,
    750  		     "segshift ", lfsp->lfs_segshift);
    751  	(void)printf("    %s0x%-8qx  %s%-10d  %s0x%-8qX\n",
    752  		     "bmask    ", (long long)lfsp->lfs_bmask,
    753  		     "bshift   ", lfsp->lfs_bshift,
    754  		     "ffmask   ", (long long)lfsp->lfs_ffmask);
    755  	(void)printf("    %s%-10d  %s0x%-8qx  %s%u\n",
    756  		     "ffshift  ", lfsp->lfs_ffshift,
    757  		     "fbmask   ", (long long)lfsp->lfs_fbmask,
    758  		     "fbshift  ", lfsp->lfs_fbshift);
    759 
    760  	(void)printf("    %s%-10d  %s%-10d  %s0x%-8x\n",
    761  		     "sushift  ", lfsp->lfs_sushift,
    762  		     "fsbtodb  ", lfsp->lfs_fsbtodb,
    763  		     "cksum    ", lfsp->lfs_cksum);
    764  	(void)printf("    %s%-10d  %s%-10d  %s%-10d\n",
    765  		     "nclean   ", lfsp->lfs_nclean,
    766  		     "dmeta    ", lfsp->lfs_dmeta,
    767  		     "minfreeseg ", lfsp->lfs_minfreeseg);
    768  	(void)printf("    %s0x%-8x  %s%-9d %s%-10d\n",
    769  		     "roll_id  ", lfsp->lfs_ident,
    770  		     "interleave ", lfsp->lfs_interleave,
    771  		     "sumsize  ", lfsp->lfs_sumsize);
    772  	(void)printf("    %s%-10d  %s0x%-8qx\n",
    773 		     "seg0addr ", lfsp->lfs_start,
    774  		     "maxfilesize  ", (long long)lfsp->lfs_maxfilesize);
    775 
    776 
    777  	(void)printf("  Superblock disk addresses:\n    ");
    778   	for (i = 0; i < LFS_MAXNUMSB; i++) {
    779  		(void)printf(" 0x%-8x", lfsp->lfs_sboffs[i]);
    780  		if (i == (LFS_MAXNUMSB >> 1))
    781  			(void)printf("\n    ");
    782   	}
    783   	(void)printf("\n");
    784 
    785  	(void)printf("  Checkpoint Info\n");
    786  	(void)printf("    %s%-10d  %s0x%-8x  %s%-10d\n",
    787  		     "freehd   ", lfsp->lfs_freehd,
    788  		     "idaddr   ", lfsp->lfs_idaddr,
    789  		     "ifile    ", lfsp->lfs_ifile);
    790  	(void)printf("    %s%-10d  %s%-10d  %s%-10d\n",
    791  		     "uinodes  ", lfsp->lfs_uinodes,
    792  		     "bfree    ", lfsp->lfs_bfree,
    793  		     "avail    ", lfsp->lfs_avail);
    794  	(void)printf("    %s%-10d  %s0x%-8x  %s0x%-8x\n",
    795  		     "nfiles   ", lfsp->lfs_nfiles,
    796  		     "lastseg  ", lfsp->lfs_lastseg,
    797  		     "nextseg  ", lfsp->lfs_nextseg);
    798  	(void)printf("    %s0x%-8x  %s0x%-8x  %s%-10lld\n",
    799  		     "curseg   ", lfsp->lfs_curseg,
    800  		     "offset   ", lfsp->lfs_offset,
    801 		     "serial   ", (long long)lfsp->lfs_serial);
    802  	(void)printf("    tstamp   %s", ctime((time_t *)&lfsp->lfs_tstamp));
    803 }
    804 
    805 static void
    806 addseg(char *arg)
    807 {
    808 	SEGLIST *p;
    809 
    810 	if ((p = malloc(sizeof(SEGLIST))) == NULL)
    811 		err(1, "malloc");
    812 	p->next = seglist;
    813 	p->num = atoi(arg);
    814 	seglist = p;
    815 }
    816 
    817 static void
    818 dump_cleaner_info(struct lfs *lfsp, void *ipage)
    819 {
    820 	CLEANERINFO *cip;
    821 
    822 	cip = (CLEANERINFO *)ipage;
    823 	if (lfsp->lfs_version > 1) {
    824 		(void)printf("free_head %d\n", cip->free_head);
    825 		(void)printf("free_tail %d\n", cip->free_tail);
    826 	}
    827 	(void)printf("clean\t%d\tdirty\t%d\n",
    828 		     cip->clean, cip->dirty);
    829 	(void)printf("bfree\t%d\tavail\t%d\n\n",
    830 		     cip->bfree, cip->avail);
    831 }
    832 
    833 static void
    834 usage(void)
    835 {
    836 	(void)fprintf(stderr, "usage: dumplfs [-adiS] [-b blkno] [-I blkno] [-s segno] filesys|device\n");
    837 	exit(1);
    838 }
    839