xref: /netbsd-src/usr.sbin/dumplfs/dumplfs.c (revision 100a3398b8d3c64e571cff36b46c23431b410e09)
1*100a3398Sandvar /*	$NetBSD: dumplfs.c,v 1.66 2024/02/09 22:08:38 andvar Exp $	*/
20114e805Scgd 
3ccfa3742Smycroft /*-
4ccfa3742Smycroft  * Copyright (c) 1991, 1993
5ccfa3742Smycroft  *	The Regents of the University of California.  All rights reserved.
6ccfa3742Smycroft  *
7ccfa3742Smycroft  * Redistribution and use in source and binary forms, with or without
8ccfa3742Smycroft  * modification, are permitted provided that the following conditions
9ccfa3742Smycroft  * are met:
10ccfa3742Smycroft  * 1. Redistributions of source code must retain the above copyright
11ccfa3742Smycroft  *    notice, this list of conditions and the following disclaimer.
12ccfa3742Smycroft  * 2. Redistributions in binary form must reproduce the above copyright
13ccfa3742Smycroft  *    notice, this list of conditions and the following disclaimer in the
14ccfa3742Smycroft  *    documentation and/or other materials provided with the distribution.
15326b2259Sagc  * 3. Neither the name of the University nor the names of its contributors
16ccfa3742Smycroft  *    may be used to endorse or promote products derived from this software
17ccfa3742Smycroft  *    without specific prior written permission.
18ccfa3742Smycroft  *
19ccfa3742Smycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20ccfa3742Smycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21ccfa3742Smycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22ccfa3742Smycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23ccfa3742Smycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24ccfa3742Smycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25ccfa3742Smycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26ccfa3742Smycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27ccfa3742Smycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28ccfa3742Smycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29ccfa3742Smycroft  * SUCH DAMAGE.
30ccfa3742Smycroft  */
31ccfa3742Smycroft 
32e5bc90f4Sfvdl #include <sys/cdefs.h>
33e5bc90f4Sfvdl 
34ccfa3742Smycroft #ifndef lint
359c194566Slukem __COPYRIGHT("@(#) Copyright (c) 1991, 1993\
369c194566Slukem  The Regents of the University of California.  All rights reserved.");
37ccfa3742Smycroft #endif /* not lint */
38ccfa3742Smycroft 
39ccfa3742Smycroft #ifndef lint
400114e805Scgd #if 0
41e5bc90f4Sfvdl static char sccsid[] = "@(#)dumplfs.c	8.5 (Berkeley) 5/24/95";
420114e805Scgd #else
43*100a3398Sandvar __RCSID("$NetBSD: dumplfs.c,v 1.66 2024/02/09 22:08:38 andvar Exp $");
440114e805Scgd #endif
45ccfa3742Smycroft #endif /* not lint */
46ccfa3742Smycroft 
47ccfa3742Smycroft #include <sys/param.h>
48ccfa3742Smycroft #include <sys/ucred.h>
49ccfa3742Smycroft #include <sys/mount.h>
50ccfa3742Smycroft #include <sys/time.h>
51ccfa3742Smycroft 
52ccfa3742Smycroft #include <ufs/lfs/lfs.h>
5334f0d74cSdholland #include <ufs/lfs/lfs_accessors.h>
54ccfa3742Smycroft 
558ffd59e2Smycroft #include <err.h>
568ffd59e2Smycroft #include <errno.h>
57ccfa3742Smycroft #include <fcntl.h>
58ccfa3742Smycroft #include <fstab.h>
5917e0569bSriastradh #include <stdbool.h>
60ccfa3742Smycroft #include <stdlib.h>
61ccfa3742Smycroft #include <stdio.h>
62ccfa3742Smycroft #include <string.h>
638ffd59e2Smycroft #include <unistd.h>
64a583d893Schristos #include <util.h>
65ccfa3742Smycroft #include "extern.h"
66ccfa3742Smycroft 
674e3fced9Sperseant static void	addseg(char *);
684e3fced9Sperseant static void	dump_cleaner_info(struct lfs *, void *);
69b1828e0bSdholland static void	dump_dinode(struct lfs *, union lfs_dinode *);
70a3ff3a30Sfvdl static void	dump_ifile(int, struct lfs *, int, int, daddr_t);
714e3fced9Sperseant static int	dump_ipage_ifile(struct lfs *, int, char *, int);
724e3fced9Sperseant static int	dump_ipage_segusage(struct lfs *, int, char *, int);
734e3fced9Sperseant static void	dump_segment(int, int, daddr_t, struct lfs *, int);
744e3fced9Sperseant static int	dump_sum(int, struct lfs *, SEGSUM *, int, daddr_t);
754e3fced9Sperseant static void	dump_super(struct lfs *);
764e3fced9Sperseant static void	usage(void);
77ccfa3742Smycroft 
7833ffa226Smatt extern uint32_t	cksum(void *, size_t);
79e5bc90f4Sfvdl 
80ccfa3742Smycroft typedef struct seglist SEGLIST;
81ccfa3742Smycroft struct seglist {
82ccfa3742Smycroft         SEGLIST *next;
83ccfa3742Smycroft 	int num;
84ccfa3742Smycroft };
85ccfa3742Smycroft SEGLIST	*seglist;
86ccfa3742Smycroft 
87ccfa3742Smycroft char *special;
88ccfa3742Smycroft 
89ccfa3742Smycroft /* Segment Usage formats */
90ccfa3742Smycroft #define print_suheader \
91ccfa3742Smycroft 	(void)printf("segnum\tflags\tnbytes\tninos\tnsums\tlastmod\n")
92ccfa3742Smycroft 
93b1c92f5aSperseant static inline void
print_suentry(int i,SEGUSE * sp,struct lfs * fs)94b1c92f5aSperseant print_suentry(int i, SEGUSE *sp, struct lfs *fs)
95b1c92f5aSperseant {
96b1c92f5aSperseant 	time_t t;
97b1c92f5aSperseant 	char flags[4] = "   ";
98b1c92f5aSperseant 
99b1c92f5aSperseant 	if (sp->su_flags & SEGUSE_ACTIVE)
100b1c92f5aSperseant 		flags[0] = 'A';
101b1c92f5aSperseant 	if (sp->su_flags & SEGUSE_DIRTY)
102b1c92f5aSperseant 		flags[1] = 'D';
103b1c92f5aSperseant 	else
104b1c92f5aSperseant 		flags[1] = 'C';
105b1c92f5aSperseant 	if (sp->su_flags & SEGUSE_SUPERBLOCK)
106b1c92f5aSperseant 		flags[2] = 'S';
107b1c92f5aSperseant 
108992b9a23Sdholland 	t = (lfs_sb_getversion(fs) == 1 ? sp->su_olastmod : sp->su_lastmod);
109b1c92f5aSperseant 
110b1c92f5aSperseant 	printf("%d\t%s\t%d\t%d\t%d\t%s", i, flags,
111b1c92f5aSperseant 		sp->su_nbytes, sp->su_ninos, sp->su_nsums,
112b1c92f5aSperseant 		ctime(&t));
113b1c92f5aSperseant }
114ccfa3742Smycroft 
115ccfa3742Smycroft /* Ifile formats */
116ccfa3742Smycroft #define print_iheader \
117ccfa3742Smycroft 	(void)printf("inum\tstatus\tversion\tdaddr\t\tfreeptr\n")
118b1c92f5aSperseant 
119b1c92f5aSperseant static inline void
print_ientry(int i,struct lfs * lfsp,IFILE * ip)12032577c4fSdholland print_ientry(int i, struct lfs *lfsp, IFILE *ip)
121b1c92f5aSperseant {
12232577c4fSdholland 	uint32_t version;
12332577c4fSdholland 	daddr_t daddr;
12432577c4fSdholland 	ino_t nextfree;
12532577c4fSdholland 
12632577c4fSdholland 	version = lfs_if_getversion(lfsp, ip);
12732577c4fSdholland 	daddr = lfs_if_getdaddr(lfsp, ip);
12832577c4fSdholland 	nextfree = lfs_if_getnextfree(lfsp, ip);
12932577c4fSdholland 
13032577c4fSdholland 	if (daddr == LFS_UNUSED_DADDR)
13132577c4fSdholland 		printf("%d\tFREE\t%u\t \t\t%ju\n", i, version,
13232577c4fSdholland 		    (uintmax_t)nextfree);
133b1c92f5aSperseant 	else
13432577c4fSdholland 		printf("%d\tINUSE\t%u\t%8jX\t%s\n",
13532577c4fSdholland 		    i, version, (intmax_t)daddr,
1362d468aafSriastradh 		    nextfree == LFS_ORPHAN_NEXTFREE(lfsp) ? "orphan" : "-");
137b1c92f5aSperseant }
138b1c92f5aSperseant 
1395191580cSdholland /*
1405191580cSdholland  * Set the is64 and dobyteswap fields of struct lfs. Note that the
1415191580cSdholland  * magic number (and version) fields are necessarily at the same place
1425191580cSdholland  * in all superblock versions, so we can read it via u_32 without
1435191580cSdholland  * getting confused.
1445191580cSdholland  */
1455191580cSdholland static void
identify(struct lfs * fs)1465191580cSdholland identify(struct lfs *fs)
1475191580cSdholland {
1485191580cSdholland 	unsigned magic;
1495191580cSdholland 
1505191580cSdholland 	magic = fs->lfs_dlfs_u.u_32.dlfs_magic;
1515191580cSdholland 	switch (magic) {
1525191580cSdholland 	    case LFS_MAGIC:
1535191580cSdholland 		fs->lfs_is64 = false;
1545191580cSdholland 		fs->lfs_dobyteswap = false;
1555191580cSdholland 		break;
1565191580cSdholland 	    case LFS_MAGIC_SWAPPED:
1575191580cSdholland 		fs->lfs_is64 = false;
1585191580cSdholland 		fs->lfs_dobyteswap = true;
1595191580cSdholland 		break;
1605191580cSdholland 	    case LFS64_MAGIC:
1615191580cSdholland 		fs->lfs_is64 = true;
1625191580cSdholland 		fs->lfs_dobyteswap = false;
1635191580cSdholland 		break;
1645191580cSdholland 	    case LFS64_MAGIC_SWAPPED:
1655191580cSdholland 		fs->lfs_is64 = true;
1665191580cSdholland 		fs->lfs_dobyteswap = true;
1675191580cSdholland 		break;
1685191580cSdholland 	    default:
1695191580cSdholland 		warnx("Superblock magic number 0x%x not known; "
1705191580cSdholland 		      "assuming 32-bit, native-endian", magic);
1715191580cSdholland 		fs->lfs_is64 = false;
1725191580cSdholland 		fs->lfs_dobyteswap = false;
1735191580cSdholland 		break;
1745191580cSdholland 	}
1755191580cSdholland }
1765191580cSdholland 
177855bb66eSchristos #define fsbtobyte(fs, b)	lfs_fsbtob((fs), (off_t)((b)))
1783f9bbd97Stoshii 
179af18198dSperseant int datasum_check = 0;
180af18198dSperseant 
181ccfa3742Smycroft int
main(int argc,char ** argv)1824e3fced9Sperseant main(int argc, char **argv)
183ccfa3742Smycroft {
184ccfa3742Smycroft 	struct lfs lfs_sb1, lfs_sb2, *lfs_master;
18537318e5aSperseant 	daddr_t seg_addr, idaddr, sbdaddr;
1864e3fced9Sperseant 	int ch, do_allsb, do_ientries, do_segentries, fd, segnum;
18703a893b3Smlelstv 	void *sbuf;
188ccfa3742Smycroft 
189ccfa3742Smycroft 	do_allsb = 0;
190ccfa3742Smycroft 	do_ientries = 0;
1914e3fced9Sperseant 	do_segentries = 0;
19237318e5aSperseant 	idaddr = 0x0;
19337318e5aSperseant 	sbdaddr = 0x0;
194af18198dSperseant 	while ((ch = getopt(argc, argv, "ab:diI:Ss:")) != -1)
195ccfa3742Smycroft 		switch(ch) {
196ccfa3742Smycroft 		case 'a':		/* Dump all superblocks */
197ccfa3742Smycroft 			do_allsb = 1;
198ccfa3742Smycroft 			break;
19937318e5aSperseant 		case 'b':		/* Use this superblock */
20037318e5aSperseant 			sbdaddr = strtol(optarg, NULL, 0);
20137318e5aSperseant 			break;
202af18198dSperseant 		case 'd':
203af18198dSperseant 			datasum_check = 1;
204af18198dSperseant 			break;
205ccfa3742Smycroft 		case 'i':		/* Dump ifile entries */
2064e3fced9Sperseant 			do_ientries = !do_ientries;
207ccfa3742Smycroft 			break;
20837318e5aSperseant 		case 'I':		/* Use this ifile inode */
20937318e5aSperseant 			idaddr = strtol(optarg, NULL, 0);
21037318e5aSperseant 			break;
2114e3fced9Sperseant 		case 'S':
2124e3fced9Sperseant 			do_segentries = !do_segentries;
2134e3fced9Sperseant 			break;
214ccfa3742Smycroft 		case 's':		/* Dump out these segments */
215ccfa3742Smycroft 			addseg(optarg);
216ccfa3742Smycroft 			break;
217ccfa3742Smycroft 		default:
218ccfa3742Smycroft 			usage();
219ccfa3742Smycroft 		}
220ccfa3742Smycroft 	argc -= optind;
221ccfa3742Smycroft 	argv += optind;
222ccfa3742Smycroft 
223ccfa3742Smycroft 	if (argc != 1)
224ccfa3742Smycroft 		usage();
225ccfa3742Smycroft 
226ccfa3742Smycroft 	special = argv[0];
227ccfa3742Smycroft 	if ((fd = open(special, O_RDONLY, 0)) < 0)
2288ffd59e2Smycroft 		err(1, "%s", special);
229ccfa3742Smycroft 
230a583d893Schristos 	sbuf = emalloc(LFS_SBPAD);
23137318e5aSperseant 	if (sbdaddr == 0x0) {
2324e3fced9Sperseant 		/* Read the proto-superblock */
2339e5184b8Sdholland 		__CTASSERT(sizeof(struct dlfs) == sizeof(struct dlfs64));
23403a893b3Smlelstv 		get(fd, LFS_LABELPAD, sbuf, LFS_SBPAD);
2359e5184b8Sdholland 		memcpy(&lfs_sb1.lfs_dlfs_u, sbuf, sizeof(struct dlfs));
2365191580cSdholland 		identify(&lfs_sb1);
237ccfa3742Smycroft 
2384e3fced9Sperseant 		/* If that wasn't the real first sb, get the real first sb */
239992b9a23Sdholland 		if (lfs_sb_getversion(&lfs_sb1) > 1 &&
2405191580cSdholland 		    lfs_sb_getsboff(&lfs_sb1, 0) > lfs_btofsb(&lfs_sb1, LFS_LABELPAD)) {
241adca8af5Sdholland 			get(fd, lfs_fsbtob(&lfs_sb1, lfs_sb_getsboff(&lfs_sb1, 0)),
2429e5184b8Sdholland 			    &lfs_sb1.lfs_dlfs_u, sizeof(struct dlfs));
2435191580cSdholland 			identify(&lfs_sb1);
2445191580cSdholland 		}
2454e3fced9Sperseant 
246ccfa3742Smycroft 		/*
247ccfa3742Smycroft 	 	* Read the second superblock and figure out which check point is
248ccfa3742Smycroft 	 	* most up to date.
249ccfa3742Smycroft 	 	*/
250ccfa3742Smycroft 		get(fd,
251adca8af5Sdholland 		    fsbtobyte(&lfs_sb1, lfs_sb_getsboff(&lfs_sb1, 1)),
25203a893b3Smlelstv 		    sbuf, LFS_SBPAD);
2539e5184b8Sdholland 		memcpy(&lfs_sb2.lfs_dlfs_u, sbuf, sizeof(struct dlfs));
2545191580cSdholland 		identify(&lfs_sb2);
255ccfa3742Smycroft 
256ccfa3742Smycroft 		lfs_master = &lfs_sb1;
257992b9a23Sdholland 		if (lfs_sb_getversion(&lfs_sb1) > 1) {
258f59b8f4bSdholland 			if (lfs_sb_getserial(&lfs_sb1) > lfs_sb_getserial(&lfs_sb2)) {
259ccfa3742Smycroft 				lfs_master = &lfs_sb2;
260adca8af5Sdholland 				sbdaddr = lfs_sb_getsboff(&lfs_sb1, 1);
26137318e5aSperseant 			} else
262adca8af5Sdholland 				sbdaddr = lfs_sb_getsboff(&lfs_sb1, 0);
2634e3fced9Sperseant 		} else {
264f59b8f4bSdholland 			if (lfs_sb_getotstamp(&lfs_sb1) > lfs_sb_getotstamp(&lfs_sb2)) {
2654e3fced9Sperseant 				lfs_master = &lfs_sb2;
266adca8af5Sdholland 				sbdaddr = lfs_sb_getsboff(&lfs_sb1, 1);
2674e3fced9Sperseant 			} else
268adca8af5Sdholland 				sbdaddr = lfs_sb_getsboff(&lfs_sb1, 0);
2694e3fced9Sperseant 		}
27037318e5aSperseant 	} else {
27137318e5aSperseant 		/* Read the first superblock */
27203a893b3Smlelstv 		get(fd, dbtob((off_t)sbdaddr), sbuf, LFS_SBPAD);
2739e5184b8Sdholland 		memcpy(&lfs_sb1.lfs_dlfs_u, sbuf, sizeof(struct dlfs));
2745191580cSdholland 		identify(&lfs_sb1);
27537318e5aSperseant 		lfs_master = &lfs_sb1;
2765415bc30Sperseant 	}
277ccfa3742Smycroft 
27803a893b3Smlelstv 	free(sbuf);
27903a893b3Smlelstv 
2804e3fced9Sperseant 	/* Compatibility */
281992b9a23Sdholland 	if (lfs_sb_getversion(lfs_master) == 1) {
282adca8af5Sdholland 		lfs_sb_setsumsize(lfs_master, LFS_V1_SUMMARY_SIZE);
283adca8af5Sdholland 		lfs_sb_setibsize(lfs_master, lfs_sb_getbsize(lfs_master));
284adca8af5Sdholland 		lfs_sb_sets0addr(lfs_master, lfs_sb_getsboff(lfs_master, 0));
285f59b8f4bSdholland 		lfs_sb_settstamp(lfs_master, lfs_sb_getotstamp(lfs_master));
286adca8af5Sdholland 		lfs_sb_setfsbtodb(lfs_master, 0);
2874e3fced9Sperseant 	}
2884e3fced9Sperseant 
2895191580cSdholland 	(void)printf("Master LFS%d superblock at 0x%llx:\n",
2905191580cSdholland 	    lfs_master->lfs_is64 ? 64 : 32, (long long)sbdaddr);
291ccfa3742Smycroft 	dump_super(lfs_master);
292ccfa3742Smycroft 
2934e3fced9Sperseant 	dump_ifile(fd, lfs_master, do_ientries, do_segentries, idaddr);
294ccfa3742Smycroft 
295ccfa3742Smycroft 	if (seglist != NULL)
296ccfa3742Smycroft 		for (; seglist != NULL; seglist = seglist->next) {
297855bb66eSchristos 			seg_addr = lfs_sntod(lfs_master, seglist->num);
2984e3fced9Sperseant 			dump_segment(fd, seglist->num, seg_addr, lfs_master,
2994e3fced9Sperseant 				     do_allsb);
300ccfa3742Smycroft 		}
301ccfa3742Smycroft 	else
302855bb66eSchristos 		for (segnum = 0, seg_addr = lfs_sntod(lfs_master, 0);
303adca8af5Sdholland 		     segnum < lfs_sb_getnseg(lfs_master);
304855bb66eSchristos 		     segnum++, seg_addr = lfs_sntod(lfs_master, segnum))
3054e3fced9Sperseant 			dump_segment(fd, segnum, seg_addr, lfs_master,
3064e3fced9Sperseant 				     do_allsb);
307ccfa3742Smycroft 
308ccfa3742Smycroft 	(void)close(fd);
309ccfa3742Smycroft 	exit(0);
310ccfa3742Smycroft }
311ccfa3742Smycroft 
312ccfa3742Smycroft /*
313ccfa3742Smycroft  * We are reading all the blocks of an inode and dumping out the ifile table.
314ccfa3742Smycroft  * This code could be tighter, but this is a first pass at getting the stuff
315ccfa3742Smycroft  * printed out rather than making this code incredibly efficient.
316ccfa3742Smycroft  */
317ccfa3742Smycroft static void
dump_ifile(int fd,struct lfs * lfsp,int do_ientries,int do_segentries,daddr_t addr)3184e3fced9Sperseant dump_ifile(int fd, struct lfs *lfsp, int do_ientries, int do_segentries, daddr_t addr)
319ccfa3742Smycroft {
3204e3fced9Sperseant 	char *ipage;
321b1828e0bSdholland 	char *dpage;
322b1828e0bSdholland 	union lfs_dinode *dip = NULL;
3234cb9c64aSdholland 	void *dindir, *indir;
3244cb9c64aSdholland 	unsigned offset;
3254cb9c64aSdholland 	daddr_t thisblock;
326b1828e0bSdholland 	daddr_t pdb;
3275eeab43eScgd 	int block_limit, i, inum, j, nblocks, psize;
328ccfa3742Smycroft 
329f59b8f4bSdholland 	psize = lfs_sb_getbsize(lfsp);
33037318e5aSperseant 	if (!addr)
331f59b8f4bSdholland 		addr = lfs_sb_getidaddr(lfsp);
332ccfa3742Smycroft 
333a583d893Schristos 	dpage = emalloc(psize);
3344e3fced9Sperseant 	get(fd, fsbtobyte(lfsp, addr), dpage, psize);
335ccfa3742Smycroft 
336a8308178Sdholland 	dip = NULL;
337b1828e0bSdholland 	for (i = LFS_INOPB(lfsp); i-- > 0; ) {
338b1828e0bSdholland 		dip = DINO_IN_BLOCK(lfsp, dpage, i);
339b1828e0bSdholland 		if (lfs_dino_getinumber(lfsp, dip) == LFS_IFILE_INUM)
340ccfa3742Smycroft 			break;
341b1828e0bSdholland 	}
342ccfa3742Smycroft 
343a8308178Sdholland 	/* just in case */
344a8308178Sdholland 	if (dip == NULL) {
345a8308178Sdholland 		warnx("this volume apparently has zero inodes per block");
346a8308178Sdholland 		return;
347a8308178Sdholland 	}
348a8308178Sdholland 
349b1828e0bSdholland 	if (lfs_dino_getinumber(lfsp, dip) != LFS_IFILE_INUM) {
350078ffcb8Sdholland 		warnx("unable to locate ifile inode at disk address 0x%jx",
351078ffcb8Sdholland 		     (uintmax_t)addr);
352b7872b2dSjoerg 		return;
353b7872b2dSjoerg 	}
354ccfa3742Smycroft 
355ccfa3742Smycroft 	(void)printf("\nIFILE inode\n");
356b1828e0bSdholland 	dump_dinode(lfsp, dip);
357ccfa3742Smycroft 
358ccfa3742Smycroft 	(void)printf("\nIFILE contents\n");
359b1828e0bSdholland 	nblocks = lfs_dino_getsize(lfsp, dip) >> lfs_sb_getbshift(lfsp);
3606dc91806Sdholland 	block_limit = MIN(nblocks, ULFS_NDADDR);
361ccfa3742Smycroft 
362ccfa3742Smycroft 	/* Get the direct block */
363a583d893Schristos 	ipage = emalloc(psize);
364b1828e0bSdholland 	for (inum = 0, i = 0; i < block_limit; i++) {
365b1828e0bSdholland 		pdb = lfs_dino_getdb(lfsp, dip, i);
366b1828e0bSdholland 		get(fd, fsbtobyte(lfsp, pdb), ipage, psize);
367f59b8f4bSdholland 		if (i < lfs_sb_getcleansz(lfsp)) {
368ccfa3742Smycroft 			dump_cleaner_info(lfsp, ipage);
3694e3fced9Sperseant 			if (do_segentries)
370ccfa3742Smycroft 				print_suheader;
371ccfa3742Smycroft 			continue;
372ccfa3742Smycroft 		}
373ccfa3742Smycroft 
374f59b8f4bSdholland 		if (i < (lfs_sb_getsegtabsz(lfsp) + lfs_sb_getcleansz(lfsp))) {
3754e3fced9Sperseant 			if (do_segentries)
376ccfa3742Smycroft 				inum = dump_ipage_segusage(lfsp, inum, ipage,
377f59b8f4bSdholland 							   lfs_sb_getsepb(lfsp));
3784e3fced9Sperseant 			else
379f59b8f4bSdholland 				inum = (i < lfs_sb_getsegtabsz(lfsp) + lfs_sb_getcleansz(lfsp) - 1);
3805b1f2d4cSross 			if (!inum) {
381ccfa3742Smycroft 				if(!do_ientries)
382ccfa3742Smycroft 					goto e0;
383ccfa3742Smycroft 				else
384ccfa3742Smycroft 					print_iheader;
3855b1f2d4cSross 			}
386ccfa3742Smycroft 		} else
387f59b8f4bSdholland 			inum = dump_ipage_ifile(lfsp, inum, ipage, lfs_sb_getifpb(lfsp));
388ccfa3742Smycroft 	}
389ccfa3742Smycroft 
3906dc91806Sdholland 	if (nblocks <= ULFS_NDADDR)
391ccfa3742Smycroft 		goto e0;
392ccfa3742Smycroft 
393ccfa3742Smycroft 	/* Dump out blocks off of single indirect block */
394a583d893Schristos 	indir = emalloc(psize);
395b1828e0bSdholland 	get(fd, fsbtobyte(lfsp, lfs_dino_getib(lfsp, dip, 0)), indir, psize);
396f59b8f4bSdholland 	block_limit = MIN(i + lfs_sb_getnindir(lfsp), nblocks);
3974cb9c64aSdholland 	for (offset = 0; i < block_limit; i++, offset++) {
3984cb9c64aSdholland 		thisblock = lfs_iblock_get(lfsp, indir, offset);
3994cb9c64aSdholland 		if (thisblock == LFS_UNUSED_DADDR)
400ccfa3742Smycroft 			break;
4014cb9c64aSdholland 		get(fd, fsbtobyte(lfsp, thisblock), ipage, psize);
402f59b8f4bSdholland 		if (i < lfs_sb_getcleansz(lfsp)) {
403ccfa3742Smycroft 			dump_cleaner_info(lfsp, ipage);
404ccfa3742Smycroft 			continue;
4054e3fced9Sperseant 		}
406ccfa3742Smycroft 
407f59b8f4bSdholland 		if (i < lfs_sb_getsegtabsz(lfsp) + lfs_sb_getcleansz(lfsp)) {
4084e3fced9Sperseant 			if (do_segentries)
409ccfa3742Smycroft 				inum = dump_ipage_segusage(lfsp, inum, ipage,
410f59b8f4bSdholland 							   lfs_sb_getsepb(lfsp));
4114e3fced9Sperseant 			else
412f59b8f4bSdholland 				inum = (i < lfs_sb_getsegtabsz(lfsp) + lfs_sb_getcleansz(lfsp) - 1);
413a542b8e8Snathanw 			if (!inum) {
414ccfa3742Smycroft 				if(!do_ientries)
415ccfa3742Smycroft 					goto e1;
416ccfa3742Smycroft 				else
417ccfa3742Smycroft 					print_iheader;
418a542b8e8Snathanw 			}
419ccfa3742Smycroft 		} else
420f59b8f4bSdholland 			inum = dump_ipage_ifile(lfsp, inum, ipage, lfs_sb_getifpb(lfsp));
421ccfa3742Smycroft 	}
422ccfa3742Smycroft 
423d0cadc37Sdholland 	if (nblocks <= ULFS_NDADDR + lfs_sb_getnindir(lfsp))
424ccfa3742Smycroft 		goto e1;
425ccfa3742Smycroft 
426ccfa3742Smycroft 	/* Get the double indirect block */
427a583d893Schristos 	dindir = emalloc(psize);
428b1828e0bSdholland 	get(fd, fsbtobyte(lfsp, lfs_dino_getib(lfsp, dip, 1)), dindir, psize);
4294cb9c64aSdholland 	for (j = 0; j < lfs_sb_getnindir(lfsp); j++) {
4304cb9c64aSdholland 		thisblock = lfs_iblock_get(lfsp, dindir, j);
4314cb9c64aSdholland 		if (thisblock == LFS_UNUSED_DADDR)
432ccfa3742Smycroft 			break;
4334cb9c64aSdholland 		get(fd, fsbtobyte(lfsp, thisblock), indir, psize);
434f59b8f4bSdholland 		block_limit = MIN(i + lfs_sb_getnindir(lfsp), nblocks);
4354cb9c64aSdholland 		for (offset = 0; i < block_limit; i++, offset++) {
4364cb9c64aSdholland 			thisblock = lfs_iblock_get(lfsp, indir, offset);
4374cb9c64aSdholland 			if (thisblock == LFS_UNUSED_DADDR)
438ccfa3742Smycroft 				break;
4394cb9c64aSdholland 			get(fd, fsbtobyte(lfsp, thisblock), ipage, psize);
440f59b8f4bSdholland 			if (i < lfs_sb_getcleansz(lfsp)) {
441ccfa3742Smycroft 				dump_cleaner_info(lfsp, ipage);
442ccfa3742Smycroft 				continue;
4434e3fced9Sperseant 			}
444ccfa3742Smycroft 
445f59b8f4bSdholland 			if (i < lfs_sb_getsegtabsz(lfsp) + lfs_sb_getcleansz(lfsp)) {
4464e3fced9Sperseant 				if (do_segentries)
447ccfa3742Smycroft 					inum = dump_ipage_segusage(lfsp,
448f59b8f4bSdholland 						 inum, ipage, lfs_sb_getsepb(lfsp));
4494e3fced9Sperseant 				else
450f59b8f4bSdholland 					inum = (i < lfs_sb_getsegtabsz(lfsp) +
451f59b8f4bSdholland 						lfs_sb_getcleansz(lfsp) - 1);
4525b1f2d4cSross 				if (!inum) {
453ccfa3742Smycroft 					if(!do_ientries)
454ccfa3742Smycroft 						goto e2;
455ccfa3742Smycroft 					else
456ccfa3742Smycroft 						print_iheader;
4575b1f2d4cSross 				}
458ccfa3742Smycroft 			} else
4594e3fced9Sperseant 				inum = dump_ipage_ifile(lfsp, inum,
460f59b8f4bSdholland 				    ipage, lfs_sb_getifpb(lfsp));
461ccfa3742Smycroft 		}
462ccfa3742Smycroft 	}
463ccfa3742Smycroft e2:	free(dindir);
464ccfa3742Smycroft e1:	free(indir);
465ccfa3742Smycroft e0:	free(dpage);
466ccfa3742Smycroft 	free(ipage);
467ccfa3742Smycroft }
468ccfa3742Smycroft 
469ccfa3742Smycroft static int
dump_ipage_ifile(struct lfs * lfsp,int i,char * pp,int tot)4704e3fced9Sperseant dump_ipage_ifile(struct lfs *lfsp, int i, char *pp, int tot)
471ccfa3742Smycroft {
4724e3fced9Sperseant 	char *ip;
4734e3fced9Sperseant 	int cnt, max, entsize;
474ccfa3742Smycroft 
47532577c4fSdholland 	if (lfsp->lfs_is64)
47632577c4fSdholland 		entsize = sizeof(IFILE64);
47732577c4fSdholland 	if (lfs_sb_getversion(lfsp) > 1)
47832577c4fSdholland 		entsize = sizeof(IFILE32);
4794e3fced9Sperseant 	else
48032577c4fSdholland 		entsize = sizeof(IFILE_V1);
481ccfa3742Smycroft 	max = i + tot;
482ccfa3742Smycroft 
4834e3fced9Sperseant 	for (ip = pp, cnt = i; cnt < max; cnt++, ip += entsize)
48432577c4fSdholland 		print_ientry(cnt, lfsp, (IFILE *)ip);
485ccfa3742Smycroft 	return (max);
486ccfa3742Smycroft }
487ccfa3742Smycroft 
488ccfa3742Smycroft static int
dump_ipage_segusage(struct lfs * lfsp,int i,char * pp,int tot)4894e3fced9Sperseant dump_ipage_segusage(struct lfs *lfsp, int i, char *pp, int tot)
490ccfa3742Smycroft {
491ccfa3742Smycroft 	SEGUSE *sp;
492ccfa3742Smycroft 	int cnt, max;
4934e02de35Sperseant 	struct seglist *slp;
494ccfa3742Smycroft 
495ccfa3742Smycroft 	max = i + tot;
496ccfa3742Smycroft 	for (sp = (SEGUSE *)pp, cnt = i;
497f59b8f4bSdholland 	     cnt < lfs_sb_getnseg(lfsp) && cnt < max; cnt++) {
4984e02de35Sperseant 		if (seglist == NULL)
4994e3fced9Sperseant 			print_suentry(cnt, sp, lfsp);
5004e02de35Sperseant 		else {
5014e02de35Sperseant 			for (slp = seglist; slp != NULL; slp = slp->next)
5024e02de35Sperseant 				if (cnt == slp->num) {
5034e3fced9Sperseant 					print_suentry(cnt, sp, lfsp);
5044e02de35Sperseant 					break;
5054e02de35Sperseant 				}
5064e02de35Sperseant 		}
507992b9a23Sdholland 		if (lfs_sb_getversion(lfsp) > 1)
5084e3fced9Sperseant 			++sp;
5094e3fced9Sperseant 		else
5104e3fced9Sperseant 			sp = (SEGUSE *)((SEGUSE_V1 *)sp + 1);
5114e02de35Sperseant 	}
512f59b8f4bSdholland 	if (max >= lfs_sb_getnseg(lfsp))
513ccfa3742Smycroft 		return (0);
514ccfa3742Smycroft 	else
515ccfa3742Smycroft 		return (max);
516ccfa3742Smycroft }
517ccfa3742Smycroft 
518ccfa3742Smycroft static void
dump_dinode(struct lfs * fs,union lfs_dinode * dip)519b1828e0bSdholland dump_dinode(struct lfs *fs, union lfs_dinode *dip)
520ccfa3742Smycroft {
521ccfa3742Smycroft 	int i;
52247efd9f3Sthorpej 	time_t at, mt, ct;
52347efd9f3Sthorpej 
524b1828e0bSdholland 	at = lfs_dino_getatime(fs, dip);
525b1828e0bSdholland 	mt = lfs_dino_getmtime(fs, dip);
526b1828e0bSdholland 	ct = lfs_dino_getctime(fs, dip);
527ccfa3742Smycroft 
528b1828e0bSdholland 	(void)printf("    %so%o\t%s%d\t%s%d\t%s%d\t%s%ju\n",
529b1828e0bSdholland 		"mode  ", lfs_dino_getmode(fs, dip),
530b1828e0bSdholland 		"nlink ", lfs_dino_getnlink(fs, dip),
531b1828e0bSdholland 		"uid   ", lfs_dino_getuid(fs, dip),
532b1828e0bSdholland 		"gid   ", lfs_dino_getgid(fs, dip),
533b1828e0bSdholland 		"size  ", (uintmax_t)lfs_dino_getsize(fs, dip));
534b1828e0bSdholland 	(void)printf("    %s%s", "atime ", ctime(&at));
535b1828e0bSdholland 	(void)printf("    %s%s", "mtime ", ctime(&mt));
536b1828e0bSdholland 	(void)printf("    %s%s", "ctime ", ctime(&ct));
537b1828e0bSdholland 	(void)printf("    inum  %ju\n",
538b1828e0bSdholland 		(uintmax_t)lfs_dino_getinumber(fs, dip));
539ccfa3742Smycroft 	(void)printf("    Direct Addresses\n");
5406dc91806Sdholland 	for (i = 0; i < ULFS_NDADDR; i++) {
541b1828e0bSdholland 		(void)printf("\t0x%jx", (intmax_t)lfs_dino_getdb(fs, dip, i));
542ccfa3742Smycroft 		if ((i % 6) == 5)
543ccfa3742Smycroft 			(void)printf("\n");
544ccfa3742Smycroft 	}
545b1828e0bSdholland 	(void)printf("    Indirect Addresses\n");
5466dc91806Sdholland 	for (i = 0; i < ULFS_NIADDR; i++)
547b1828e0bSdholland 		(void)printf("\t0x%jx", (intmax_t)lfs_dino_getib(fs, dip, i));
548ccfa3742Smycroft 	(void)printf("\n");
549ccfa3742Smycroft }
550ccfa3742Smycroft 
551ccfa3742Smycroft static int
dump_sum(int fd,struct lfs * lfsp,SEGSUM * sp,int segnum,daddr_t addr)5524e3fced9Sperseant dump_sum(int fd, struct lfs *lfsp, SEGSUM *sp, int segnum, daddr_t addr)
553ccfa3742Smycroft {
554ccfa3742Smycroft 	FINFO *fp;
5557f7a5fa1Sdholland 	IINFO *iip, *iip2;
556e54b457cSdholland 	union lfs_blocks fipblocks;
557af18198dSperseant 	int i, j, acc;
558ccfa3742Smycroft 	int ck;
559af18198dSperseant 	int numbytes, numblocks;
560af18198dSperseant 	char *datap;
561b1828e0bSdholland 	char *diblock;
562b1828e0bSdholland 	union lfs_dinode *dip;
563af18198dSperseant 	size_t el_size;
564af18198dSperseant 	u_int32_t datasum;
5652e090556Sdholland 	u_int32_t ssflags;
566b1c92f5aSperseant 	time_t t;
567af18198dSperseant 	char *buf;
5682e090556Sdholland 	size_t sumstart;
569ccfa3742Smycroft 
5702e090556Sdholland 	sumstart = lfs_ss_getsumstart(lfsp);
5712e090556Sdholland 	if (lfs_ss_getmagic(lfsp, sp) != SS_MAGIC ||
5722e090556Sdholland 	    lfs_ss_getsumsum(lfsp, sp) != (ck = cksum((char *)sp + sumstart,
5732e090556Sdholland 	    lfs_sb_getsumsize(lfsp) - sumstart))) {
57437318e5aSperseant 		/* Don't print "corrupt" if we're just too close to the edge */
575855bb66eSchristos 		if (lfs_dtosn(lfsp, addr + LFS_FSBTODB(lfsp, 1)) ==
576855bb66eSchristos 		    lfs_dtosn(lfsp, addr))
577a3ff3a30Sfvdl 			(void)printf("dumplfs: %s %d address 0x%llx\n",
57837318e5aSperseant 		                     "corrupt summary block; segment", segnum,
579a3ff3a30Sfvdl 				     (long long)addr);
58016c0dce8Sperseant 		return -1;
581ccfa3742Smycroft 	}
5822e090556Sdholland 	if (lfs_sb_getversion(lfsp) > 1 && lfs_ss_getident(lfsp, sp) != lfs_sb_getident(lfsp)) {
583a3ff3a30Sfvdl 		(void)printf("dumplfs: %s %d address 0x%llx\n",
5844e3fced9Sperseant 	                     "summary from a former life; segment", segnum,
585a1f69dd5Smrg 			     (long long)addr);
58616c0dce8Sperseant 		return -1;
5874e3fced9Sperseant 	}
588ccfa3742Smycroft 
589a3ff3a30Sfvdl 	(void)printf("Segment Summary Info at 0x%llx\n", (long long)addr);
5902e090556Sdholland 	ssflags = lfs_ss_getflags(lfsp, sp);
5912e090556Sdholland 	(void)printf("    %s0x%jx\t%s%d\t%s%d\t%s%c%c%c%c\n    %s0x%x\t%s0x%x",
5922e090556Sdholland 		"next     ", (intmax_t)lfs_ss_getnext(lfsp, sp),
5932e090556Sdholland 		"nfinfo   ", lfs_ss_getnfinfo(lfsp, sp),
5942e090556Sdholland 		"ninos    ", lfs_ss_getninos(lfsp, sp),
5952e090556Sdholland 		"flags    ", (ssflags & SS_DIROP) ? 'D' : '-',
5962e090556Sdholland 			     (ssflags & SS_CONT)  ? 'C' : '-',
5972e090556Sdholland 			     (ssflags & SS_CLEAN)  ? 'L' : '-',
5982e090556Sdholland 			     (ssflags & SS_RFW)  ? 'R' : '-',
5992e090556Sdholland 		"sumsum   ", lfs_ss_getsumsum(lfsp, sp),
6002e090556Sdholland 		"datasum  ", lfs_ss_getdatasum(lfsp, sp));
601992b9a23Sdholland 	if (lfs_sb_getversion(lfsp) == 1) {
6022e090556Sdholland 		t = lfs_ss_getocreate(lfsp, sp);
603b1c92f5aSperseant 		(void)printf("\tcreate   %s\n", ctime(&t));
604b1c92f5aSperseant 	} else {
6052e090556Sdholland 		t = lfs_ss_getcreate(lfsp, sp);
606b1c92f5aSperseant 		(void)printf("\tcreate   %s", ctime(&t));
6072e090556Sdholland 		(void)printf("    roll_id  %-8x", lfs_ss_getident(lfsp, sp));
6082e090556Sdholland 		(void)printf("   serial   %lld\n",
6092e090556Sdholland 			     (long long)lfs_ss_getserial(lfsp, sp));
6104e3fced9Sperseant 	}
611ccfa3742Smycroft 
612ccfa3742Smycroft 	/* Dump out inode disk addresses */
6137f7a5fa1Sdholland 	iip = SEGSUM_IINFOSTART(lfsp, sp);
614a583d893Schristos 	diblock = emalloc(lfs_sb_getbsize(lfsp));
615ccfa3742Smycroft 	printf("    Inode addresses:");
616e5bc90f4Sfvdl 	numbytes = 0;
617af18198dSperseant 	numblocks = 0;
6187f7a5fa1Sdholland 	for (i = 0; i < lfs_ss_getninos(lfsp, sp); iip = NEXTLOWER_IINFO(lfsp, iip)) {
619af18198dSperseant 		++numblocks;
620f59b8f4bSdholland 		numbytes += lfs_sb_getibsize(lfsp);	/* add bytes for inode block */
6217f7a5fa1Sdholland 		printf("\t0x%jx {", (intmax_t)lfs_ii_getblock(lfsp, iip));
6227f7a5fa1Sdholland 		get(fd, fsbtobyte(lfsp, lfs_ii_getblock(lfsp, iip)), diblock, lfs_sb_getibsize(lfsp));
6232e090556Sdholland 		for (j = 0; i < lfs_ss_getninos(lfsp, sp) && j < LFS_INOPB(lfsp); j++, i++) {
624ccfa3742Smycroft 			if (j > 0)
625ccfa3742Smycroft 				(void)printf(", ");
626b1828e0bSdholland 			dip = DINO_IN_BLOCK(lfsp, diblock, j);
627b1828e0bSdholland 			(void)printf("%juv%d", lfs_dino_getinumber(lfsp, dip),
628b1828e0bSdholland 				     lfs_dino_getgen(lfsp, dip));
629ccfa3742Smycroft 		}
630ccfa3742Smycroft 		(void)printf("}");
631855bb66eSchristos 		if (((i/LFS_INOPB(lfsp)) % 4) == 3)
632ccfa3742Smycroft 			(void)printf("\n");
633ccfa3742Smycroft 	}
634b1828e0bSdholland 	free(diblock);
635ccfa3742Smycroft 
636ccfa3742Smycroft 	printf("\n");
637af18198dSperseant 
6382e090556Sdholland 	fp = SEGSUM_FINFOBASE(lfsp, sp);
6392e090556Sdholland 	for (i = 0; i < lfs_ss_getnfinfo(lfsp, sp); i++) {
640e54b457cSdholland 		(void)printf("    FINFO for inode: %ju version %u nblocks %u lastlength %u\n",
641e54b457cSdholland 		    (uintmax_t)lfs_fi_getino(lfsp, fp),
642e54b457cSdholland 		    lfs_fi_getversion(lfsp, fp),
643e54b457cSdholland 		    lfs_fi_getnblocks(lfsp, fp),
644e54b457cSdholland 		    lfs_fi_getlastlength(lfsp, fp));
645e54b457cSdholland 		lfs_blocks_fromfinfo(lfsp, &fipblocks, fp);
646e54b457cSdholland 		numblocks += lfs_fi_getnblocks(lfsp, fp);
647e54b457cSdholland 		for (j = 0; j < lfs_fi_getnblocks(lfsp, fp); j++) {
648e54b457cSdholland 			(void)printf("\t%jd",
649e54b457cSdholland 			    (intmax_t)lfs_blocks_get(lfsp, &fipblocks, j));
650ccfa3742Smycroft 			if ((j % 8) == 7)
651ccfa3742Smycroft 				(void)printf("\n");
652e54b457cSdholland 			if (j == lfs_fi_getnblocks(lfsp, fp) - 1)
653e54b457cSdholland 				numbytes += lfs_fi_getlastlength(lfsp, fp);
654e5bc90f4Sfvdl 			else
655f59b8f4bSdholland 				numbytes += lfs_sb_getbsize(lfsp);
656ccfa3742Smycroft 		}
657ccfa3742Smycroft 		if ((j % 8) != 0)
658ccfa3742Smycroft 			(void)printf("\n");
659e54b457cSdholland 		fp = NEXT_FINFO(lfsp, fp);
660ccfa3742Smycroft 	}
661af18198dSperseant 
662af18198dSperseant 	if (datasum_check == 0)
663af18198dSperseant 		return (numbytes);
664af18198dSperseant 
665af18198dSperseant 	/*
666af18198dSperseant 	 * Now that we know the number of blocks, run back through and
667af18198dSperseant 	 * compute the data checksum.  (A bad data checksum is not enough
668*100a3398Sandvar 	 * to prevent us from continuing, but it does merit a warning.)
669af18198dSperseant 	 */
6707f7a5fa1Sdholland 	iip2 = SEGSUM_IINFOSTART(lfsp, sp);
671f3729261Sdholland 	fp = SEGSUM_FINFOBASE(lfsp, sp);
672992b9a23Sdholland 	if (lfs_sb_getversion(lfsp) == 1) {
673af18198dSperseant 		el_size = sizeof(unsigned long);
674af18198dSperseant 	} else {
675af18198dSperseant 		el_size = sizeof(u_int32_t);
676af18198dSperseant 	}
677a583d893Schristos 	datap = ecalloc(numblocks, el_size);
678a583d893Schristos 
679af18198dSperseant 	acc = 0;
680f59b8f4bSdholland 	addr += lfs_btofsb(lfsp, lfs_sb_getsumsize(lfsp));
681a583d893Schristos 	buf = emalloc(lfs_sb_getbsize(lfsp));
6822e090556Sdholland 	for (i = 0; i < lfs_ss_getnfinfo(lfsp, sp); i++) {
6837f7a5fa1Sdholland 		while (addr == lfs_ii_getblock(lfsp, iip2)) {
684f59b8f4bSdholland 			get(fd, fsbtobyte(lfsp, addr), buf, lfs_sb_getibsize(lfsp));
685af18198dSperseant 			memcpy(datap + acc * el_size, buf, el_size);
686f59b8f4bSdholland 			addr += lfs_btofsb(lfsp, lfs_sb_getibsize(lfsp));
6877f7a5fa1Sdholland 			iip2 = NEXTLOWER_IINFO(lfsp, iip2);
688af18198dSperseant 			++acc;
689af18198dSperseant 		}
690e54b457cSdholland 		for (j = 0; j < lfs_fi_getnblocks(lfsp, fp); j++) {
691f59b8f4bSdholland 			get(fd, fsbtobyte(lfsp, addr), buf, lfs_sb_getfsize(lfsp));
692af18198dSperseant 			memcpy(datap + acc * el_size, buf, el_size);
693e54b457cSdholland 			if (j == lfs_fi_getnblocks(lfsp, fp) - 1)
694e54b457cSdholland 				addr += lfs_btofsb(lfsp, lfs_fi_getlastlength(lfsp, fp));
695af18198dSperseant 			else
696f59b8f4bSdholland 				addr += lfs_btofsb(lfsp, lfs_sb_getbsize(lfsp));
697af18198dSperseant 			++acc;
698af18198dSperseant 		}
699e54b457cSdholland 		fp = NEXT_FINFO(lfsp, fp);
700af18198dSperseant 	}
7017f7a5fa1Sdholland 	while (addr == lfs_ii_getblock(lfsp, iip2)) {
702f59b8f4bSdholland 		get(fd, fsbtobyte(lfsp, addr), buf, lfs_sb_getibsize(lfsp));
703af18198dSperseant 		memcpy(datap + acc * el_size, buf, el_size);
704f59b8f4bSdholland 		addr += lfs_btofsb(lfsp, lfs_sb_getibsize(lfsp));
7057f7a5fa1Sdholland 		iip2 = NEXTLOWER_IINFO(lfsp, iip2);
706af18198dSperseant 		++acc;
707af18198dSperseant 	}
708af18198dSperseant 	free(buf);
709af18198dSperseant 	if (acc != numblocks)
710af18198dSperseant 		printf("** counted %d blocks but should have been %d\n",
711af18198dSperseant 		     acc, numblocks);
712af18198dSperseant 	datasum = cksum(datap, numblocks * el_size);
7132e090556Sdholland 	if (datasum != lfs_ss_getdatasum(lfsp, sp))
7142e090556Sdholland 		printf("** computed datasum 0x%lx does not match given datasum 0x%lx\n", (unsigned long)datasum, (unsigned long)lfs_ss_getdatasum(lfsp, sp));
715af18198dSperseant 	free(datap);
716af18198dSperseant 
717e5bc90f4Sfvdl 	return (numbytes);
718ccfa3742Smycroft }
719ccfa3742Smycroft 
720ccfa3742Smycroft static void
dump_segment(int fd,int segnum,daddr_t addr,struct lfs * lfsp,int dump_sb)7214e3fced9Sperseant dump_segment(int fd, int segnum, daddr_t addr, struct lfs *lfsp, int dump_sb)
722ccfa3742Smycroft {
723ccfa3742Smycroft 	struct lfs lfs_sb, *sbp;
724ccfa3742Smycroft 	SEGSUM *sump;
7252e090556Sdholland 	size_t sumstart;
7264e3fced9Sperseant 	char *sumblock;
727e5bc90f4Sfvdl 	int did_one, nbytes, sb;
72837318e5aSperseant 	off_t sum_offset;
72937318e5aSperseant 	daddr_t new_addr;
730ccfa3742Smycroft 
731a3ff3a30Sfvdl 	(void)printf("\nSEGMENT %lld (Disk Address 0x%llx)\n",
732855bb66eSchristos 		     (long long)lfs_dtosn(lfsp, addr), (long long)addr);
7334e3fced9Sperseant 	sum_offset = fsbtobyte(lfsp, addr);
734a583d893Schristos 	sumblock = emalloc(lfs_sb_getsumsize(lfsp));
7354e3fced9Sperseant 
736992b9a23Sdholland 	if (lfs_sb_getversion(lfsp) > 1 && segnum == 0) {
737adca8af5Sdholland 		if (lfs_fsbtob(lfsp, lfs_sb_gets0addr(lfsp)) < LFS_LABELPAD) {
7388685c52dSperseant 			/* First segment eats the disklabel */
739855bb66eSchristos 			sum_offset += lfs_fragroundup(lfsp, LFS_LABELPAD) -
740adca8af5Sdholland 				      lfs_fsbtob(lfsp, lfs_sb_gets0addr(lfsp));
741855bb66eSchristos 			addr += lfs_btofsb(lfsp, lfs_fragroundup(lfsp, LFS_LABELPAD)) -
742adca8af5Sdholland 				lfs_sb_gets0addr(lfsp);
7434e3fced9Sperseant 			printf("Disklabel at 0x0\n");
7444e3fced9Sperseant 		}
7458685c52dSperseant 	}
746ccfa3742Smycroft 
747ccfa3742Smycroft 	sb = 0;
748ccfa3742Smycroft 	did_one = 0;
749ccfa3742Smycroft 	do {
750f59b8f4bSdholland 		get(fd, sum_offset, sumblock, lfs_sb_getsumsize(lfsp));
751ccfa3742Smycroft 		sump = (SEGSUM *)sumblock;
7522e090556Sdholland 		sumstart = lfs_ss_getsumstart(lfsp);
753992b9a23Sdholland 		if ((lfs_sb_getversion(lfsp) > 1 &&
7542e090556Sdholland 		     lfs_ss_getident(lfsp, sump) != lfs_sb_getident(lfsp)) ||
7552e090556Sdholland 		    lfs_ss_getsumsum(lfsp, sump) !=
7562e090556Sdholland 		      cksum((char *)sump + sumstart,
7572e090556Sdholland 			    lfs_sb_getsumsize(lfsp) - sumstart)) {
758ccfa3742Smycroft 			sbp = (struct lfs *)sump;
7599e5184b8Sdholland 			if ((sb = (sbp->lfs_dlfs_u.u_32.dlfs_magic == LFS_MAGIC))) {
7604e3fced9Sperseant 				printf("Superblock at 0x%x\n",
761855bb66eSchristos 				       (unsigned)lfs_btofsb(lfsp, sum_offset));
76237318e5aSperseant 				if (dump_sb)  {
7639e5184b8Sdholland 					__CTASSERT(sizeof(struct dlfs) ==
7649e5184b8Sdholland 						   sizeof(struct dlfs64));
7659e5184b8Sdholland 					get(fd, sum_offset, &(lfs_sb.lfs_dlfs_u),
76637318e5aSperseant 					    sizeof(struct dlfs));
76737318e5aSperseant 					dump_super(&lfs_sb);
76837318e5aSperseant 				}
769992b9a23Sdholland 				if (lfs_sb_getversion(lfsp) > 1)
770855bb66eSchristos 					sum_offset += lfs_fragroundup(lfsp, LFS_SBPAD);
7714e3fced9Sperseant 				else
772ccfa3742Smycroft 					sum_offset += LFS_SBPAD;
773ccfa3742Smycroft 			} else if (did_one)
774ccfa3742Smycroft 				break;
775ccfa3742Smycroft 			else {
776a3ff3a30Sfvdl 				printf("Segment at 0x%llx empty or corrupt\n",
777a3ff3a30Sfvdl                                        (long long)addr);
778ccfa3742Smycroft 				break;
779ccfa3742Smycroft 			}
780ccfa3742Smycroft 		} else {
7814e3fced9Sperseant 			nbytes = dump_sum(fd, lfsp, sump, segnum,
782855bb66eSchristos 				lfs_btofsb(lfsp, sum_offset));
78316c0dce8Sperseant 			if (nbytes >= 0)
784f59b8f4bSdholland 				sum_offset += lfs_sb_getsumsize(lfsp) + nbytes;
785ccfa3742Smycroft 			else
786ccfa3742Smycroft 				sum_offset = 0;
787ccfa3742Smycroft 			did_one = 1;
788ccfa3742Smycroft 		}
78937318e5aSperseant 		/* If the segment ends right on a boundary, it still ends */
790855bb66eSchristos 		new_addr = lfs_btofsb(lfsp, sum_offset);
7914e3fced9Sperseant 		/* printf("end daddr = 0x%lx\n", (long)new_addr); */
792855bb66eSchristos 		if (lfs_dtosn(lfsp, new_addr) != lfs_dtosn(lfsp, addr))
79337318e5aSperseant 			break;
794ccfa3742Smycroft 	} while (sum_offset);
795ccfa3742Smycroft 
796666b712cSdsl 	free(sumblock);
797ccfa3742Smycroft }
798ccfa3742Smycroft 
799ccfa3742Smycroft static void
dump_super(struct lfs * lfsp)8004e3fced9Sperseant dump_super(struct lfs *lfsp)
801ccfa3742Smycroft {
802f59b8f4bSdholland 	time_t stamp;
803ccfa3742Smycroft 	int i;
804ccfa3742Smycroft 
80595a8d28cSdholland  	(void)printf("    %s0x%-8x  %s0x%-8x  %s%-10ju\n",
8069e5184b8Sdholland  		     "magic    ", lfsp->lfs_dlfs_u.u_32.dlfs_magic,
807992b9a23Sdholland  		     "version  ", lfs_sb_getversion(lfsp),
80895a8d28cSdholland  		     "size     ", (uintmax_t)lfs_sb_getsize(lfsp));
80995a8d28cSdholland  	(void)printf("    %s%-10d  %s%-10ju  %s%-10d\n",
810f59b8f4bSdholland  		     "ssize    ", lfs_sb_getssize(lfsp),
81195a8d28cSdholland  		     "dsize    ", (uintmax_t)lfs_sb_getdsize(lfsp),
812f59b8f4bSdholland  		     "bsize    ", lfs_sb_getbsize(lfsp));
8134e3fced9Sperseant  	(void)printf("    %s%-10d  %s%-10d  %s%-10d\n",
814f59b8f4bSdholland  		     "fsize    ", lfs_sb_getfsize(lfsp),
815f59b8f4bSdholland  		     "frag     ", lfs_sb_getfrag(lfsp),
816f59b8f4bSdholland  		     "minfree  ", lfs_sb_getminfree(lfsp));
8174e3fced9Sperseant  	(void)printf("    %s%-10d  %s%-10d  %s%-10d\n",
818f59b8f4bSdholland  		     "inopb    ", lfs_sb_getinopb(lfsp),
819f59b8f4bSdholland  		     "ifpb     ", lfs_sb_getifpb(lfsp),
820f59b8f4bSdholland  		     "nindir   ", lfs_sb_getnindir(lfsp));
8214e3fced9Sperseant  	(void)printf("    %s%-10d  %s%-10d  %s%-10d\n",
822f59b8f4bSdholland  		     "nseg     ", lfs_sb_getnseg(lfsp),
823f59b8f4bSdholland  		     "sepb     ", lfs_sb_getsepb(lfsp),
824f59b8f4bSdholland  		     "cleansz  ", lfs_sb_getcleansz(lfsp));
8254e3fced9Sperseant  	(void)printf("    %s%-10d  %s0x%-8x  %s%-10d\n",
826f59b8f4bSdholland  		     "segtabsz ", lfs_sb_getsegtabsz(lfsp),
827f59b8f4bSdholland  		     "segmask  ", lfs_sb_getsegmask(lfsp),
828f59b8f4bSdholland  		     "segshift ", lfs_sb_getsegshift(lfsp));
829f59b8f4bSdholland  	(void)printf("    %s0x%-8jx  %s%-10d  %s0x%-8jX\n",
830f59b8f4bSdholland  		     "bmask    ", (uintmax_t)lfs_sb_getbmask(lfsp),
831f59b8f4bSdholland  		     "bshift   ", lfs_sb_getbshift(lfsp),
832f59b8f4bSdholland  		     "ffmask   ", (uintmax_t)lfs_sb_getffmask(lfsp));
833f59b8f4bSdholland  	(void)printf("    %s%-10d  %s0x%-8jx  %s%u\n",
834f59b8f4bSdholland  		     "ffshift  ", lfs_sb_getffshift(lfsp),
835f59b8f4bSdholland  		     "fbmask   ", (uintmax_t)lfs_sb_getfbmask(lfsp),
836f59b8f4bSdholland  		     "fbshift  ", lfs_sb_getfbshift(lfsp));
837ccfa3742Smycroft 
8384e3fced9Sperseant  	(void)printf("    %s%-10d  %s%-10d  %s0x%-8x\n",
839f59b8f4bSdholland  		     "sushift  ", lfs_sb_getsushift(lfsp),
840f59b8f4bSdholland  		     "fsbtodb  ", lfs_sb_getfsbtodb(lfsp),
841f59b8f4bSdholland  		     "cksum    ", lfs_sb_getcksum(lfsp));
8424e3fced9Sperseant  	(void)printf("    %s%-10d  %s%-10d  %s%-10d\n",
843f59b8f4bSdholland  		     "nclean   ", lfs_sb_getnclean(lfsp),
844f59b8f4bSdholland  		     "dmeta    ", lfs_sb_getdmeta(lfsp),
845f59b8f4bSdholland  		     "minfreeseg ", lfs_sb_getminfreeseg(lfsp));
8464e3fced9Sperseant  	(void)printf("    %s0x%-8x  %s%-9d %s%-10d\n",
847f59b8f4bSdholland  		     "roll_id  ", lfs_sb_getident(lfsp),
848f59b8f4bSdholland  		     "interleave ", lfs_sb_getinterleave(lfsp),
849f59b8f4bSdholland  		     "sumsize  ", lfs_sb_getsumsize(lfsp));
850078ffcb8Sdholland  	(void)printf("    %s%-10jd  %s0x%-8jx\n",
851078ffcb8Sdholland 		     "seg0addr ", (intmax_t)lfs_sb_gets0addr(lfsp),
852f59b8f4bSdholland  		     "maxfilesize  ", (uintmax_t)lfs_sb_getmaxfilesize(lfsp));
853a3bb9e16Sperseant 
8544e3fced9Sperseant 
8554e3fced9Sperseant  	(void)printf("  Superblock disk addresses:\n    ");
856ccfa3742Smycroft   	for (i = 0; i < LFS_MAXNUMSB; i++) {
857078ffcb8Sdholland  		(void)printf(" 0x%-8jx", (intmax_t)lfs_sb_getsboff(lfsp, i));
858ccfa3742Smycroft  		if (i == (LFS_MAXNUMSB >> 1))
8594e3fced9Sperseant  			(void)printf("\n    ");
860ccfa3742Smycroft   	}
861ccfa3742Smycroft   	(void)printf("\n");
862ccfa3742Smycroft 
863ccfa3742Smycroft  	(void)printf("  Checkpoint Info\n");
864eb2560adSdholland  	(void)printf("    %s%-10ju  %s0x%-8jx\n",
8654b344787Sdholland  		     "freehd   ", (uintmax_t)lfs_sb_getfreehd(lfsp),
866eb2560adSdholland  		     "idaddr   ", (intmax_t)lfs_sb_getidaddr(lfsp));
86795a8d28cSdholland  	(void)printf("    %s%-10d  %s%-10jd  %s%-10jd\n",
868f59b8f4bSdholland  		     "uinodes  ", lfs_sb_getuinodes(lfsp),
86995a8d28cSdholland  		     "bfree    ", (intmax_t)lfs_sb_getbfree(lfsp),
87095a8d28cSdholland  		     "avail    ", (intmax_t)lfs_sb_getavail(lfsp));
8714b344787Sdholland  	(void)printf("    %s%-10ju  %s0x%-8jx  %s0x%-8jx\n",
8724b344787Sdholland  		     "nfiles   ", (uintmax_t)lfs_sb_getnfiles(lfsp),
873078ffcb8Sdholland  		     "lastseg  ", (uintmax_t)lfs_sb_getlastseg(lfsp),
874078ffcb8Sdholland  		     "nextseg  ", (uintmax_t)lfs_sb_getnextseg(lfsp));
875078ffcb8Sdholland  	(void)printf("    %s0x%-8jx  %s0x%-8jx  %s%-10ju\n",
876078ffcb8Sdholland  		     "curseg   ", (uintmax_t)lfs_sb_getcurseg(lfsp),
877078ffcb8Sdholland  		     "offset   ", (uintmax_t)lfs_sb_getoffset(lfsp),
878f59b8f4bSdholland 		     "serial   ", (uintmax_t)lfs_sb_getserial(lfsp));
879f59b8f4bSdholland 	stamp = lfs_sb_gettstamp(lfsp);
880f59b8f4bSdholland  	(void)printf("    tstamp   %s", ctime(&stamp));
881eb2560adSdholland 
882eb2560adSdholland 	if (!lfsp->lfs_is64) {
883eb2560adSdholland 		(void)printf("  32-bit only derived or constant fields\n");
884eb2560adSdholland 		(void)printf("    %s%-10u\n",
885eb2560adSdholland  		     "ifile    ", lfs_sb_getifile(lfsp));
886eb2560adSdholland 	}
887ccfa3742Smycroft }
888ccfa3742Smycroft 
889ccfa3742Smycroft static void
addseg(char * arg)8904e3fced9Sperseant addseg(char *arg)
891ccfa3742Smycroft {
892ccfa3742Smycroft 	SEGLIST *p;
893ccfa3742Smycroft 
894a583d893Schristos 	p = emalloc(sizeof(*p));
895ccfa3742Smycroft 	p->next = seglist;
896ccfa3742Smycroft 	p->num = atoi(arg);
897ccfa3742Smycroft 	seglist = p;
898ccfa3742Smycroft }
899ccfa3742Smycroft 
900ccfa3742Smycroft static void
dump_cleaner_info(struct lfs * lfsp,void * ipage)9014e3fced9Sperseant dump_cleaner_info(struct lfs *lfsp, void *ipage)
902ccfa3742Smycroft {
903ccfa3742Smycroft 	CLEANERINFO *cip;
904ccfa3742Smycroft 
905ccfa3742Smycroft 	cip = (CLEANERINFO *)ipage;
906992b9a23Sdholland 	if (lfs_sb_getversion(lfsp) > 1) {
907f11e4edeSdholland 		(void)printf("free_head %ju\n",
908f11e4edeSdholland 			     (uintmax_t)lfs_ci_getfree_head(lfsp, cip));
909f11e4edeSdholland 		(void)printf("free_tail %ju\n",
910f11e4edeSdholland 			     (uintmax_t)lfs_ci_getfree_tail(lfsp, cip));
9114e3fced9Sperseant 	}
912f11e4edeSdholland 	(void)printf("clean\t%u\tdirty\t%u\n",
913f11e4edeSdholland 		     lfs_ci_getclean(lfsp, cip), lfs_ci_getdirty(lfsp, cip));
914f11e4edeSdholland 	(void)printf("bfree\t%jd\tavail\t%jd\n\n",
915f11e4edeSdholland 		     (intmax_t)lfs_ci_getbfree(lfsp, cip),
916f11e4edeSdholland 		     (intmax_t)lfs_ci_getavail(lfsp, cip));
917ccfa3742Smycroft }
918ccfa3742Smycroft 
919ccfa3742Smycroft static void
usage(void)9204e3fced9Sperseant usage(void)
921ccfa3742Smycroft {
922a0d5a7bbSwiz 	(void)fprintf(stderr, "usage: dumplfs [-adiS] [-b blkno] [-I blkno] [-s segno] filesys|device\n");
923ccfa3742Smycroft 	exit(1);
924ccfa3742Smycroft }
925