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