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