Home | History | Annotate | Line # | Download | only in dumplfs
dumplfs.c revision 1.62
      1  1.62  riastrad /*	$NetBSD: dumplfs.c,v 1.62 2016/06/15 14:07:54 riastradh Exp $	*/
      2   1.5       cgd 
      3   1.1   mycroft /*-
      4   1.1   mycroft  * Copyright (c) 1991, 1993
      5   1.1   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1   mycroft  *
      7   1.1   mycroft  * Redistribution and use in source and binary forms, with or without
      8   1.1   mycroft  * modification, are permitted provided that the following conditions
      9   1.1   mycroft  * are met:
     10   1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     11   1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     12   1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     14   1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     15  1.28       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1   mycroft  *    may be used to endorse or promote products derived from this software
     17   1.1   mycroft  *    without specific prior written permission.
     18   1.1   mycroft  *
     19   1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1   mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1   mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1   mycroft  * SUCH DAMAGE.
     30   1.1   mycroft  */
     31   1.1   mycroft 
     32   1.8      fvdl #include <sys/cdefs.h>
     33   1.8      fvdl 
     34   1.1   mycroft #ifndef lint
     35  1.37     lukem __COPYRIGHT("@(#) Copyright (c) 1991, 1993\
     36  1.37     lukem  The Regents of the University of California.  All rights reserved.");
     37   1.1   mycroft #endif /* not lint */
     38   1.1   mycroft 
     39   1.1   mycroft #ifndef lint
     40   1.5       cgd #if 0
     41   1.8      fvdl static char sccsid[] = "@(#)dumplfs.c	8.5 (Berkeley) 5/24/95";
     42   1.5       cgd #else
     43  1.62  riastrad __RCSID("$NetBSD: dumplfs.c,v 1.62 2016/06/15 14:07:54 riastradh Exp $");
     44   1.5       cgd #endif
     45   1.1   mycroft #endif /* not lint */
     46   1.1   mycroft 
     47   1.1   mycroft #include <sys/param.h>
     48   1.1   mycroft #include <sys/ucred.h>
     49   1.1   mycroft #include <sys/mount.h>
     50   1.1   mycroft #include <sys/time.h>
     51   1.1   mycroft 
     52   1.1   mycroft #include <ufs/lfs/lfs.h>
     53  1.45  dholland #include <ufs/lfs/lfs_accessors.h>
     54   1.1   mycroft 
     55   1.2   mycroft #include <err.h>
     56   1.2   mycroft #include <errno.h>
     57   1.1   mycroft #include <fcntl.h>
     58   1.1   mycroft #include <fstab.h>
     59  1.62  riastrad #include <stdbool.h>
     60   1.1   mycroft #include <stdlib.h>
     61   1.1   mycroft #include <stdio.h>
     62   1.1   mycroft #include <string.h>
     63   1.2   mycroft #include <unistd.h>
     64   1.1   mycroft #include "extern.h"
     65   1.1   mycroft 
     66  1.19  perseant static void	addseg(char *);
     67  1.19  perseant static void	dump_cleaner_info(struct lfs *, void *);
     68  1.54  dholland static void	dump_dinode(struct lfs *, union lfs_dinode *);
     69  1.20      fvdl static void	dump_ifile(int, struct lfs *, int, int, daddr_t);
     70  1.19  perseant static int	dump_ipage_ifile(struct lfs *, int, char *, int);
     71  1.19  perseant static int	dump_ipage_segusage(struct lfs *, int, char *, int);
     72  1.19  perseant static void	dump_segment(int, int, daddr_t, struct lfs *, int);
     73  1.19  perseant static int	dump_sum(int, struct lfs *, SEGSUM *, int, daddr_t);
     74  1.19  perseant static void	dump_super(struct lfs *);
     75  1.19  perseant static void	usage(void);
     76   1.1   mycroft 
     77  1.36      matt extern uint32_t	cksum(void *, size_t);
     78   1.8      fvdl 
     79   1.1   mycroft typedef struct seglist SEGLIST;
     80   1.1   mycroft struct seglist {
     81   1.1   mycroft         SEGLIST *next;
     82   1.1   mycroft 	int num;
     83   1.1   mycroft };
     84   1.1   mycroft SEGLIST	*seglist;
     85   1.1   mycroft 
     86   1.1   mycroft char *special;
     87   1.1   mycroft 
     88   1.1   mycroft /* Segment Usage formats */
     89   1.1   mycroft #define print_suheader \
     90   1.1   mycroft 	(void)printf("segnum\tflags\tnbytes\tninos\tnsums\tlastmod\n")
     91   1.1   mycroft 
     92  1.31  perseant static inline void
     93  1.31  perseant print_suentry(int i, SEGUSE *sp, struct lfs *fs)
     94  1.31  perseant {
     95  1.31  perseant 	time_t t;
     96  1.31  perseant 	char flags[4] = "   ";
     97  1.31  perseant 
     98  1.31  perseant 	if (sp->su_flags & SEGUSE_ACTIVE)
     99  1.31  perseant 		flags[0] = 'A';
    100  1.31  perseant 	if (sp->su_flags & SEGUSE_DIRTY)
    101  1.31  perseant 		flags[1] = 'D';
    102  1.31  perseant 	else
    103  1.31  perseant 		flags[1] = 'C';
    104  1.31  perseant 	if (sp->su_flags & SEGUSE_SUPERBLOCK)
    105  1.31  perseant 		flags[2] = 'S';
    106  1.31  perseant 
    107  1.48  dholland 	t = (lfs_sb_getversion(fs) == 1 ? sp->su_olastmod : sp->su_lastmod);
    108  1.31  perseant 
    109  1.31  perseant 	printf("%d\t%s\t%d\t%d\t%d\t%s", i, flags,
    110  1.31  perseant 		sp->su_nbytes, sp->su_ninos, sp->su_nsums,
    111  1.31  perseant 		ctime(&t));
    112  1.31  perseant }
    113   1.1   mycroft 
    114   1.1   mycroft /* Ifile formats */
    115   1.1   mycroft #define print_iheader \
    116   1.1   mycroft 	(void)printf("inum\tstatus\tversion\tdaddr\t\tfreeptr\n")
    117  1.31  perseant 
    118  1.31  perseant static inline void
    119  1.51  dholland print_ientry(int i, struct lfs *lfsp, IFILE *ip)
    120  1.31  perseant {
    121  1.51  dholland 	uint32_t version;
    122  1.51  dholland 	daddr_t daddr;
    123  1.51  dholland 	ino_t nextfree;
    124  1.51  dholland 
    125  1.51  dholland 	version = lfs_if_getversion(lfsp, ip);
    126  1.51  dholland 	daddr = lfs_if_getdaddr(lfsp, ip);
    127  1.51  dholland 	nextfree = lfs_if_getnextfree(lfsp, ip);
    128  1.51  dholland 
    129  1.51  dholland 	if (daddr == LFS_UNUSED_DADDR)
    130  1.51  dholland 		printf("%d\tFREE\t%u\t \t\t%ju\n", i, version,
    131  1.51  dholland 		    (uintmax_t)nextfree);
    132  1.31  perseant 	else
    133  1.51  dholland 		printf("%d\tINUSE\t%u\t%8jX\t%s\n",
    134  1.51  dholland 		    i, version, (intmax_t)daddr,
    135  1.51  dholland 		    nextfree == LFS_ORPHAN_NEXTFREE ? "FFFFFFFF" : "-");
    136  1.31  perseant }
    137  1.31  perseant 
    138  1.61  dholland /*
    139  1.61  dholland  * Set the is64 and dobyteswap fields of struct lfs. Note that the
    140  1.61  dholland  * magic number (and version) fields are necessarily at the same place
    141  1.61  dholland  * in all superblock versions, so we can read it via u_32 without
    142  1.61  dholland  * getting confused.
    143  1.61  dholland  */
    144  1.61  dholland static void
    145  1.61  dholland identify(struct lfs *fs)
    146  1.61  dholland {
    147  1.61  dholland 	unsigned magic;
    148  1.61  dholland 
    149  1.61  dholland 	magic = fs->lfs_dlfs_u.u_32.dlfs_magic;
    150  1.61  dholland 	switch (magic) {
    151  1.61  dholland 	    case LFS_MAGIC:
    152  1.61  dholland 		fs->lfs_is64 = false;
    153  1.61  dholland 		fs->lfs_dobyteswap = false;
    154  1.61  dholland 		break;
    155  1.61  dholland 	    case LFS_MAGIC_SWAPPED:
    156  1.61  dholland 		fs->lfs_is64 = false;
    157  1.61  dholland 		fs->lfs_dobyteswap = true;
    158  1.61  dholland 		break;
    159  1.61  dholland 	    case LFS64_MAGIC:
    160  1.61  dholland 		fs->lfs_is64 = true;
    161  1.61  dholland 		fs->lfs_dobyteswap = false;
    162  1.61  dholland 		break;
    163  1.61  dholland 	    case LFS64_MAGIC_SWAPPED:
    164  1.61  dholland 		fs->lfs_is64 = true;
    165  1.61  dholland 		fs->lfs_dobyteswap = true;
    166  1.61  dholland 		break;
    167  1.61  dholland 	    default:
    168  1.61  dholland 		warnx("Superblock magic number 0x%x not known; "
    169  1.61  dholland 		      "assuming 32-bit, native-endian", magic);
    170  1.61  dholland 		fs->lfs_is64 = false;
    171  1.61  dholland 		fs->lfs_dobyteswap = false;
    172  1.61  dholland 		break;
    173  1.61  dholland 	}
    174  1.61  dholland }
    175  1.61  dholland 
    176  1.41  christos #define fsbtobyte(fs, b)	lfs_fsbtob((fs), (off_t)((b)))
    177  1.16    toshii 
    178  1.25  perseant int datasum_check = 0;
    179  1.25  perseant 
    180   1.1   mycroft int
    181  1.19  perseant main(int argc, char **argv)
    182   1.1   mycroft {
    183   1.1   mycroft 	struct lfs lfs_sb1, lfs_sb2, *lfs_master;
    184  1.14  perseant 	daddr_t seg_addr, idaddr, sbdaddr;
    185  1.19  perseant 	int ch, do_allsb, do_ientries, do_segentries, fd, segnum;
    186  1.38   mlelstv 	void *sbuf;
    187   1.1   mycroft 
    188   1.1   mycroft 	do_allsb = 0;
    189   1.1   mycroft 	do_ientries = 0;
    190  1.19  perseant 	do_segentries = 0;
    191  1.14  perseant 	idaddr = 0x0;
    192  1.14  perseant 	sbdaddr = 0x0;
    193  1.25  perseant 	while ((ch = getopt(argc, argv, "ab:diI:Ss:")) != -1)
    194   1.1   mycroft 		switch(ch) {
    195   1.1   mycroft 		case 'a':		/* Dump all superblocks */
    196   1.1   mycroft 			do_allsb = 1;
    197   1.1   mycroft 			break;
    198  1.14  perseant 		case 'b':		/* Use this superblock */
    199  1.14  perseant 			sbdaddr = strtol(optarg, NULL, 0);
    200  1.14  perseant 			break;
    201  1.25  perseant 		case 'd':
    202  1.25  perseant 			datasum_check = 1;
    203  1.25  perseant 			break;
    204   1.1   mycroft 		case 'i':		/* Dump ifile entries */
    205  1.19  perseant 			do_ientries = !do_ientries;
    206   1.1   mycroft 			break;
    207  1.14  perseant 		case 'I':		/* Use this ifile inode */
    208  1.14  perseant 			idaddr = strtol(optarg, NULL, 0);
    209  1.14  perseant 			break;
    210  1.19  perseant 		case 'S':
    211  1.19  perseant 			do_segentries = !do_segentries;
    212  1.19  perseant 			break;
    213   1.1   mycroft 		case 's':		/* Dump out these segments */
    214   1.1   mycroft 			addseg(optarg);
    215   1.1   mycroft 			break;
    216   1.1   mycroft 		default:
    217   1.1   mycroft 			usage();
    218   1.1   mycroft 		}
    219   1.1   mycroft 	argc -= optind;
    220   1.1   mycroft 	argv += optind;
    221   1.1   mycroft 
    222   1.1   mycroft 	if (argc != 1)
    223   1.1   mycroft 		usage();
    224   1.1   mycroft 
    225   1.1   mycroft 	special = argv[0];
    226   1.1   mycroft 	if ((fd = open(special, O_RDONLY, 0)) < 0)
    227   1.2   mycroft 		err(1, "%s", special);
    228   1.1   mycroft 
    229  1.38   mlelstv 	sbuf = malloc(LFS_SBPAD);
    230  1.38   mlelstv 	if (sbuf == NULL)
    231  1.38   mlelstv 		err(1, "malloc");
    232  1.38   mlelstv 
    233  1.14  perseant 	if (sbdaddr == 0x0) {
    234  1.19  perseant 		/* Read the proto-superblock */
    235  1.49  dholland 		__CTASSERT(sizeof(struct dlfs) == sizeof(struct dlfs64));
    236  1.38   mlelstv 		get(fd, LFS_LABELPAD, sbuf, LFS_SBPAD);
    237  1.49  dholland 		memcpy(&lfs_sb1.lfs_dlfs_u, sbuf, sizeof(struct dlfs));
    238  1.61  dholland 		identify(&lfs_sb1);
    239  1.19  perseant 
    240  1.19  perseant 		/* If that wasn't the real first sb, get the real first sb */
    241  1.48  dholland 		if (lfs_sb_getversion(&lfs_sb1) > 1 &&
    242  1.61  dholland 		    lfs_sb_getsboff(&lfs_sb1, 0) > lfs_btofsb(&lfs_sb1, LFS_LABELPAD)) {
    243  1.44  dholland 			get(fd, lfs_fsbtob(&lfs_sb1, lfs_sb_getsboff(&lfs_sb1, 0)),
    244  1.49  dholland 			    &lfs_sb1.lfs_dlfs_u, sizeof(struct dlfs));
    245  1.61  dholland 			identify(&lfs_sb1);
    246  1.61  dholland 		}
    247  1.14  perseant 
    248  1.14  perseant 		/*
    249  1.14  perseant 	 	* Read the second superblock and figure out which check point is
    250  1.14  perseant 	 	* most up to date.
    251  1.14  perseant 	 	*/
    252  1.14  perseant 		get(fd,
    253  1.44  dholland 		    fsbtobyte(&lfs_sb1, lfs_sb_getsboff(&lfs_sb1, 1)),
    254  1.38   mlelstv 		    sbuf, LFS_SBPAD);
    255  1.49  dholland 		memcpy(&lfs_sb2.lfs_dlfs_u, sbuf, sizeof(struct dlfs));
    256  1.61  dholland 		identify(&lfs_sb2);
    257  1.14  perseant 
    258  1.14  perseant 		lfs_master = &lfs_sb1;
    259  1.48  dholland 		if (lfs_sb_getversion(&lfs_sb1) > 1) {
    260  1.43  dholland 			if (lfs_sb_getserial(&lfs_sb1) > lfs_sb_getserial(&lfs_sb2)) {
    261  1.19  perseant 				lfs_master = &lfs_sb2;
    262  1.44  dholland 				sbdaddr = lfs_sb_getsboff(&lfs_sb1, 1);
    263  1.19  perseant 			} else
    264  1.44  dholland 				sbdaddr = lfs_sb_getsboff(&lfs_sb1, 0);
    265  1.19  perseant 		} else {
    266  1.43  dholland 			if (lfs_sb_getotstamp(&lfs_sb1) > lfs_sb_getotstamp(&lfs_sb2)) {
    267  1.19  perseant 				lfs_master = &lfs_sb2;
    268  1.44  dholland 				sbdaddr = lfs_sb_getsboff(&lfs_sb1, 1);
    269  1.19  perseant 			} else
    270  1.44  dholland 				sbdaddr = lfs_sb_getsboff(&lfs_sb1, 0);
    271  1.19  perseant 		}
    272  1.14  perseant 	} else {
    273  1.14  perseant 		/* Read the first superblock */
    274  1.38   mlelstv 		get(fd, dbtob((off_t)sbdaddr), sbuf, LFS_SBPAD);
    275  1.49  dholland 		memcpy(&lfs_sb1.lfs_dlfs_u, sbuf, sizeof(struct dlfs));
    276  1.61  dholland 		identify(&lfs_sb1);
    277  1.14  perseant 		lfs_master = &lfs_sb1;
    278  1.13  perseant 	}
    279   1.1   mycroft 
    280  1.38   mlelstv 	free(sbuf);
    281  1.38   mlelstv 
    282  1.19  perseant 	/* Compatibility */
    283  1.48  dholland 	if (lfs_sb_getversion(lfs_master) == 1) {
    284  1.44  dholland 		lfs_sb_setsumsize(lfs_master, LFS_V1_SUMMARY_SIZE);
    285  1.44  dholland 		lfs_sb_setibsize(lfs_master, lfs_sb_getbsize(lfs_master));
    286  1.44  dholland 		lfs_sb_sets0addr(lfs_master, lfs_sb_getsboff(lfs_master, 0));
    287  1.43  dholland 		lfs_sb_settstamp(lfs_master, lfs_sb_getotstamp(lfs_master));
    288  1.44  dholland 		lfs_sb_setfsbtodb(lfs_master, 0);
    289  1.19  perseant 	}
    290  1.19  perseant 
    291  1.61  dholland 	(void)printf("Master LFS%d superblock at 0x%llx:\n",
    292  1.61  dholland 	    lfs_master->lfs_is64 ? 64 : 32, (long long)sbdaddr);
    293   1.1   mycroft 	dump_super(lfs_master);
    294   1.1   mycroft 
    295  1.19  perseant 	dump_ifile(fd, lfs_master, do_ientries, do_segentries, idaddr);
    296   1.1   mycroft 
    297   1.1   mycroft 	if (seglist != NULL)
    298   1.1   mycroft 		for (; seglist != NULL; seglist = seglist->next) {
    299  1.41  christos 			seg_addr = lfs_sntod(lfs_master, seglist->num);
    300  1.19  perseant 			dump_segment(fd, seglist->num, seg_addr, lfs_master,
    301  1.19  perseant 				     do_allsb);
    302   1.1   mycroft 		}
    303   1.1   mycroft 	else
    304  1.41  christos 		for (segnum = 0, seg_addr = lfs_sntod(lfs_master, 0);
    305  1.44  dholland 		     segnum < lfs_sb_getnseg(lfs_master);
    306  1.41  christos 		     segnum++, seg_addr = lfs_sntod(lfs_master, segnum))
    307  1.19  perseant 			dump_segment(fd, segnum, seg_addr, lfs_master,
    308  1.19  perseant 				     do_allsb);
    309   1.1   mycroft 
    310   1.1   mycroft 	(void)close(fd);
    311   1.1   mycroft 	exit(0);
    312   1.1   mycroft }
    313   1.1   mycroft 
    314   1.1   mycroft /*
    315   1.1   mycroft  * We are reading all the blocks of an inode and dumping out the ifile table.
    316   1.1   mycroft  * This code could be tighter, but this is a first pass at getting the stuff
    317   1.1   mycroft  * printed out rather than making this code incredibly efficient.
    318   1.1   mycroft  */
    319   1.1   mycroft static void
    320  1.19  perseant dump_ifile(int fd, struct lfs *lfsp, int do_ientries, int do_segentries, daddr_t addr)
    321   1.1   mycroft {
    322  1.19  perseant 	char *ipage;
    323  1.54  dholland 	char *dpage;
    324  1.54  dholland 	union lfs_dinode *dip = NULL;
    325  1.59  dholland 	void *dindir, *indir;
    326  1.59  dholland 	unsigned offset;
    327  1.59  dholland 	daddr_t thisblock;
    328  1.54  dholland 	daddr_t pdb;
    329   1.4       cgd 	int block_limit, i, inum, j, nblocks, psize;
    330   1.1   mycroft 
    331  1.43  dholland 	psize = lfs_sb_getbsize(lfsp);
    332  1.14  perseant 	if (!addr)
    333  1.43  dholland 		addr = lfs_sb_getidaddr(lfsp);
    334   1.1   mycroft 
    335   1.1   mycroft 	if (!(dpage = malloc(psize)))
    336   1.2   mycroft 		err(1, "malloc");
    337  1.19  perseant 	get(fd, fsbtobyte(lfsp, addr), dpage, psize);
    338   1.1   mycroft 
    339  1.54  dholland 	for (i = LFS_INOPB(lfsp); i-- > 0; ) {
    340  1.54  dholland 		dip = DINO_IN_BLOCK(lfsp, dpage, i);
    341  1.54  dholland 		if (lfs_dino_getinumber(lfsp, dip) == LFS_IFILE_INUM)
    342   1.1   mycroft 			break;
    343  1.54  dholland 	}
    344   1.1   mycroft 
    345  1.54  dholland 	if (lfs_dino_getinumber(lfsp, dip) != LFS_IFILE_INUM) {
    346  1.47  dholland 		warnx("unable to locate ifile inode at disk address 0x%jx",
    347  1.47  dholland 		     (uintmax_t)addr);
    348  1.35     joerg 		return;
    349  1.35     joerg 	}
    350   1.1   mycroft 
    351   1.1   mycroft 	(void)printf("\nIFILE inode\n");
    352  1.54  dholland 	dump_dinode(lfsp, dip);
    353   1.1   mycroft 
    354   1.1   mycroft 	(void)printf("\nIFILE contents\n");
    355  1.54  dholland 	nblocks = lfs_dino_getsize(lfsp, dip) >> lfs_sb_getbshift(lfsp);
    356  1.40  dholland 	block_limit = MIN(nblocks, ULFS_NDADDR);
    357   1.1   mycroft 
    358   1.1   mycroft 	/* Get the direct block */
    359   1.1   mycroft 	if ((ipage = malloc(psize)) == NULL)
    360   1.2   mycroft 		err(1, "malloc");
    361  1.54  dholland 	for (inum = 0, i = 0; i < block_limit; i++) {
    362  1.54  dholland 		pdb = lfs_dino_getdb(lfsp, dip, i);
    363  1.54  dholland 		get(fd, fsbtobyte(lfsp, pdb), ipage, psize);
    364  1.43  dholland 		if (i < lfs_sb_getcleansz(lfsp)) {
    365   1.1   mycroft 			dump_cleaner_info(lfsp, ipage);
    366  1.19  perseant 			if (do_segentries)
    367  1.19  perseant 				print_suheader;
    368   1.1   mycroft 			continue;
    369   1.1   mycroft 		}
    370   1.1   mycroft 
    371  1.43  dholland 		if (i < (lfs_sb_getsegtabsz(lfsp) + lfs_sb_getcleansz(lfsp))) {
    372  1.19  perseant 			if (do_segentries)
    373  1.19  perseant 				inum = dump_ipage_segusage(lfsp, inum, ipage,
    374  1.43  dholland 							   lfs_sb_getsepb(lfsp));
    375  1.19  perseant 			else
    376  1.43  dholland 				inum = (i < lfs_sb_getsegtabsz(lfsp) + lfs_sb_getcleansz(lfsp) - 1);
    377  1.10      ross 			if (!inum) {
    378   1.1   mycroft 				if(!do_ientries)
    379   1.1   mycroft 					goto e0;
    380   1.1   mycroft 				else
    381   1.1   mycroft 					print_iheader;
    382  1.10      ross 			}
    383   1.1   mycroft 		} else
    384  1.43  dholland 			inum = dump_ipage_ifile(lfsp, inum, ipage, lfs_sb_getifpb(lfsp));
    385   1.1   mycroft 	}
    386   1.1   mycroft 
    387  1.40  dholland 	if (nblocks <= ULFS_NDADDR)
    388   1.1   mycroft 		goto e0;
    389   1.1   mycroft 
    390   1.1   mycroft 	/* Dump out blocks off of single indirect block */
    391   1.1   mycroft 	if (!(indir = malloc(psize)))
    392   1.2   mycroft 		err(1, "malloc");
    393  1.54  dholland 	get(fd, fsbtobyte(lfsp, lfs_dino_getib(lfsp, dip, 0)), indir, psize);
    394  1.43  dholland 	block_limit = MIN(i + lfs_sb_getnindir(lfsp), nblocks);
    395  1.59  dholland 	for (offset = 0; i < block_limit; i++, offset++) {
    396  1.59  dholland 		thisblock = lfs_iblock_get(lfsp, indir, offset);
    397  1.59  dholland 		if (thisblock == LFS_UNUSED_DADDR)
    398   1.1   mycroft 			break;
    399  1.59  dholland 		get(fd, fsbtobyte(lfsp, thisblock), ipage, psize);
    400  1.43  dholland 		if (i < lfs_sb_getcleansz(lfsp)) {
    401   1.1   mycroft 			dump_cleaner_info(lfsp, ipage);
    402   1.1   mycroft 			continue;
    403  1.19  perseant 		}
    404   1.1   mycroft 
    405  1.43  dholland 		if (i < lfs_sb_getsegtabsz(lfsp) + lfs_sb_getcleansz(lfsp)) {
    406  1.19  perseant 			if (do_segentries)
    407  1.19  perseant 				inum = dump_ipage_segusage(lfsp, inum, ipage,
    408  1.43  dholland 							   lfs_sb_getsepb(lfsp));
    409  1.19  perseant 			else
    410  1.43  dholland 				inum = (i < lfs_sb_getsegtabsz(lfsp) + lfs_sb_getcleansz(lfsp) - 1);
    411  1.11   nathanw 			if (!inum) {
    412   1.1   mycroft 				if(!do_ientries)
    413   1.1   mycroft 					goto e1;
    414   1.1   mycroft 				else
    415   1.1   mycroft 					print_iheader;
    416  1.11   nathanw 			}
    417   1.1   mycroft 		} else
    418  1.43  dholland 			inum = dump_ipage_ifile(lfsp, inum, ipage, lfs_sb_getifpb(lfsp));
    419   1.1   mycroft 	}
    420   1.1   mycroft 
    421  1.55  dholland 	if (nblocks <= ULFS_NDADDR + lfs_sb_getnindir(lfsp))
    422   1.1   mycroft 		goto e1;
    423   1.1   mycroft 
    424   1.1   mycroft 	/* Get the double indirect block */
    425   1.1   mycroft 	if (!(dindir = malloc(psize)))
    426   1.2   mycroft 		err(1, "malloc");
    427  1.54  dholland 	get(fd, fsbtobyte(lfsp, lfs_dino_getib(lfsp, dip, 1)), dindir, psize);
    428  1.59  dholland 	for (j = 0; j < lfs_sb_getnindir(lfsp); j++) {
    429  1.59  dholland 		thisblock = lfs_iblock_get(lfsp, dindir, j);
    430  1.59  dholland 		if (thisblock == LFS_UNUSED_DADDR)
    431   1.1   mycroft 			break;
    432  1.59  dholland 		get(fd, fsbtobyte(lfsp, thisblock), indir, psize);
    433  1.43  dholland 		block_limit = MIN(i + lfs_sb_getnindir(lfsp), nblocks);
    434  1.59  dholland 		for (offset = 0; i < block_limit; i++, offset++) {
    435  1.59  dholland 			thisblock = lfs_iblock_get(lfsp, indir, offset);
    436  1.59  dholland 			if (thisblock == LFS_UNUSED_DADDR)
    437   1.1   mycroft 				break;
    438  1.59  dholland 			get(fd, fsbtobyte(lfsp, thisblock), ipage, psize);
    439  1.43  dholland 			if (i < lfs_sb_getcleansz(lfsp)) {
    440   1.1   mycroft 				dump_cleaner_info(lfsp, ipage);
    441   1.1   mycroft 				continue;
    442  1.19  perseant 			}
    443   1.1   mycroft 
    444  1.43  dholland 			if (i < lfs_sb_getsegtabsz(lfsp) + lfs_sb_getcleansz(lfsp)) {
    445  1.19  perseant 				if (do_segentries)
    446  1.19  perseant 					inum = dump_ipage_segusage(lfsp,
    447  1.43  dholland 						 inum, ipage, lfs_sb_getsepb(lfsp));
    448  1.19  perseant 				else
    449  1.43  dholland 					inum = (i < lfs_sb_getsegtabsz(lfsp) +
    450  1.43  dholland 						lfs_sb_getcleansz(lfsp) - 1);
    451  1.10      ross 				if (!inum) {
    452   1.1   mycroft 					if(!do_ientries)
    453   1.1   mycroft 						goto e2;
    454   1.1   mycroft 					else
    455   1.1   mycroft 						print_iheader;
    456  1.10      ross 				}
    457   1.1   mycroft 			} else
    458  1.19  perseant 				inum = dump_ipage_ifile(lfsp, inum,
    459  1.43  dholland 				    ipage, lfs_sb_getifpb(lfsp));
    460   1.1   mycroft 		}
    461   1.1   mycroft 	}
    462   1.1   mycroft e2:	free(dindir);
    463   1.1   mycroft e1:	free(indir);
    464   1.1   mycroft e0:	free(dpage);
    465   1.1   mycroft 	free(ipage);
    466   1.1   mycroft }
    467   1.1   mycroft 
    468   1.1   mycroft static int
    469  1.19  perseant dump_ipage_ifile(struct lfs *lfsp, int i, char *pp, int tot)
    470   1.1   mycroft {
    471  1.19  perseant 	char *ip;
    472  1.19  perseant 	int cnt, max, entsize;
    473   1.1   mycroft 
    474  1.51  dholland 	if (lfsp->lfs_is64)
    475  1.51  dholland 		entsize = sizeof(IFILE64);
    476  1.51  dholland 	if (lfs_sb_getversion(lfsp) > 1)
    477  1.51  dholland 		entsize = sizeof(IFILE32);
    478  1.51  dholland 	else
    479  1.19  perseant 		entsize = sizeof(IFILE_V1);
    480   1.1   mycroft 	max = i + tot;
    481   1.1   mycroft 
    482  1.19  perseant 	for (ip = pp, cnt = i; cnt < max; cnt++, ip += entsize)
    483  1.51  dholland 		print_ientry(cnt, lfsp, (IFILE *)ip);
    484   1.1   mycroft 	return (max);
    485   1.1   mycroft }
    486   1.1   mycroft 
    487   1.1   mycroft static int
    488  1.19  perseant dump_ipage_segusage(struct lfs *lfsp, int i, char *pp, int tot)
    489   1.1   mycroft {
    490   1.1   mycroft 	SEGUSE *sp;
    491   1.1   mycroft 	int cnt, max;
    492  1.15  perseant 	struct seglist *slp;
    493   1.1   mycroft 
    494   1.1   mycroft 	max = i + tot;
    495   1.1   mycroft 	for (sp = (SEGUSE *)pp, cnt = i;
    496  1.43  dholland 	     cnt < lfs_sb_getnseg(lfsp) && cnt < max; cnt++) {
    497  1.15  perseant 		if (seglist == NULL)
    498  1.19  perseant 			print_suentry(cnt, sp, lfsp);
    499  1.15  perseant 		else {
    500  1.15  perseant 			for (slp = seglist; slp != NULL; slp = slp->next)
    501  1.15  perseant 				if (cnt == slp->num) {
    502  1.19  perseant 					print_suentry(cnt, sp, lfsp);
    503  1.15  perseant 					break;
    504  1.15  perseant 				}
    505  1.15  perseant 		}
    506  1.48  dholland 		if (lfs_sb_getversion(lfsp) > 1)
    507  1.19  perseant 			++sp;
    508  1.19  perseant 		else
    509  1.19  perseant 			sp = (SEGUSE *)((SEGUSE_V1 *)sp + 1);
    510  1.15  perseant 	}
    511  1.43  dholland 	if (max >= lfs_sb_getnseg(lfsp))
    512   1.1   mycroft 		return (0);
    513   1.1   mycroft 	else
    514   1.1   mycroft 		return (max);
    515   1.1   mycroft }
    516   1.1   mycroft 
    517   1.1   mycroft static void
    518  1.54  dholland dump_dinode(struct lfs *fs, union lfs_dinode *dip)
    519   1.1   mycroft {
    520   1.1   mycroft 	int i;
    521   1.7   thorpej 	time_t at, mt, ct;
    522   1.7   thorpej 
    523  1.54  dholland 	at = lfs_dino_getatime(fs, dip);
    524  1.54  dholland 	mt = lfs_dino_getmtime(fs, dip);
    525  1.54  dholland 	ct = lfs_dino_getctime(fs, dip);
    526  1.54  dholland 
    527  1.54  dholland 	(void)printf("    %so%o\t%s%d\t%s%d\t%s%d\t%s%ju\n",
    528  1.54  dholland 		"mode  ", lfs_dino_getmode(fs, dip),
    529  1.54  dholland 		"nlink ", lfs_dino_getnlink(fs, dip),
    530  1.54  dholland 		"uid   ", lfs_dino_getuid(fs, dip),
    531  1.54  dholland 		"gid   ", lfs_dino_getgid(fs, dip),
    532  1.54  dholland 		"size  ", (uintmax_t)lfs_dino_getsize(fs, dip));
    533  1.54  dholland 	(void)printf("    %s%s", "atime ", ctime(&at));
    534  1.54  dholland 	(void)printf("    %s%s", "mtime ", ctime(&mt));
    535  1.54  dholland 	(void)printf("    %s%s", "ctime ", ctime(&ct));
    536  1.54  dholland 	(void)printf("    inum  %ju\n",
    537  1.54  dholland 		(uintmax_t)lfs_dino_getinumber(fs, dip));
    538  1.19  perseant 	(void)printf("    Direct Addresses\n");
    539  1.40  dholland 	for (i = 0; i < ULFS_NDADDR; i++) {
    540  1.54  dholland 		(void)printf("\t0x%jx", (intmax_t)lfs_dino_getdb(fs, dip, i));
    541   1.1   mycroft 		if ((i % 6) == 5)
    542   1.1   mycroft 			(void)printf("\n");
    543   1.1   mycroft 	}
    544  1.54  dholland 	(void)printf("    Indirect Addresses\n");
    545  1.40  dholland 	for (i = 0; i < ULFS_NIADDR; i++)
    546  1.54  dholland 		(void)printf("\t0x%jx", (intmax_t)lfs_dino_getib(fs, dip, i));
    547   1.1   mycroft 	(void)printf("\n");
    548   1.1   mycroft }
    549   1.1   mycroft 
    550   1.1   mycroft static int
    551  1.19  perseant dump_sum(int fd, struct lfs *lfsp, SEGSUM *sp, int segnum, daddr_t addr)
    552   1.1   mycroft {
    553   1.1   mycroft 	FINFO *fp;
    554  1.58  dholland 	IINFO *iip, *iip2;
    555  1.53  dholland 	union lfs_blocks fipblocks;
    556  1.25  perseant 	int i, j, acc;
    557   1.1   mycroft 	int ck;
    558  1.25  perseant 	int numbytes, numblocks;
    559  1.25  perseant 	char *datap;
    560  1.54  dholland 	char *diblock;
    561  1.54  dholland 	union lfs_dinode *dip;
    562  1.25  perseant 	size_t el_size;
    563  1.25  perseant 	u_int32_t datasum;
    564  1.52  dholland 	u_int32_t ssflags;
    565  1.31  perseant 	time_t t;
    566  1.25  perseant 	char *buf;
    567  1.52  dholland 	size_t sumstart;
    568   1.1   mycroft 
    569  1.52  dholland 	sumstart = lfs_ss_getsumstart(lfsp);
    570  1.52  dholland 	if (lfs_ss_getmagic(lfsp, sp) != SS_MAGIC ||
    571  1.52  dholland 	    lfs_ss_getsumsum(lfsp, sp) != (ck = cksum((char *)sp + sumstart,
    572  1.52  dholland 	    lfs_sb_getsumsize(lfsp) - sumstart))) {
    573  1.14  perseant 		/* Don't print "corrupt" if we're just too close to the edge */
    574  1.41  christos 		if (lfs_dtosn(lfsp, addr + LFS_FSBTODB(lfsp, 1)) ==
    575  1.41  christos 		    lfs_dtosn(lfsp, addr))
    576  1.20      fvdl 			(void)printf("dumplfs: %s %d address 0x%llx\n",
    577  1.14  perseant 		                     "corrupt summary block; segment", segnum,
    578  1.20      fvdl 				     (long long)addr);
    579  1.34  perseant 		return -1;
    580  1.19  perseant 	}
    581  1.52  dholland 	if (lfs_sb_getversion(lfsp) > 1 && lfs_ss_getident(lfsp, sp) != lfs_sb_getident(lfsp)) {
    582  1.20      fvdl 		(void)printf("dumplfs: %s %d address 0x%llx\n",
    583  1.19  perseant 	                     "summary from a former life; segment", segnum,
    584  1.21       mrg 			     (long long)addr);
    585  1.34  perseant 		return -1;
    586   1.1   mycroft 	}
    587   1.1   mycroft 
    588  1.20      fvdl 	(void)printf("Segment Summary Info at 0x%llx\n", (long long)addr);
    589  1.52  dholland 	ssflags = lfs_ss_getflags(lfsp, sp);
    590  1.52  dholland 	(void)printf("    %s0x%jx\t%s%d\t%s%d\t%s%c%c%c%c\n    %s0x%x\t%s0x%x",
    591  1.52  dholland 		"next     ", (intmax_t)lfs_ss_getnext(lfsp, sp),
    592  1.52  dholland 		"nfinfo   ", lfs_ss_getnfinfo(lfsp, sp),
    593  1.52  dholland 		"ninos    ", lfs_ss_getninos(lfsp, sp),
    594  1.52  dholland 		"flags    ", (ssflags & SS_DIROP) ? 'D' : '-',
    595  1.52  dholland 			     (ssflags & SS_CONT)  ? 'C' : '-',
    596  1.52  dholland 			     (ssflags & SS_CLEAN)  ? 'L' : '-',
    597  1.52  dholland 			     (ssflags & SS_RFW)  ? 'R' : '-',
    598  1.52  dholland 		"sumsum   ", lfs_ss_getsumsum(lfsp, sp),
    599  1.52  dholland 		"datasum  ", lfs_ss_getdatasum(lfsp, sp));
    600  1.48  dholland 	if (lfs_sb_getversion(lfsp) == 1) {
    601  1.52  dholland 		t = lfs_ss_getocreate(lfsp, sp);
    602  1.31  perseant 		(void)printf("\tcreate   %s\n", ctime(&t));
    603  1.31  perseant 	} else {
    604  1.52  dholland 		t = lfs_ss_getcreate(lfsp, sp);
    605  1.31  perseant 		(void)printf("\tcreate   %s", ctime(&t));
    606  1.52  dholland 		(void)printf("    roll_id  %-8x", lfs_ss_getident(lfsp, sp));
    607  1.52  dholland 		(void)printf("   serial   %lld\n",
    608  1.52  dholland 			     (long long)lfs_ss_getserial(lfsp, sp));
    609  1.19  perseant 	}
    610   1.1   mycroft 
    611   1.1   mycroft 	/* Dump out inode disk addresses */
    612  1.58  dholland 	iip = SEGSUM_IINFOSTART(lfsp, sp);
    613  1.54  dholland 	diblock = malloc(lfs_sb_getbsize(lfsp));
    614   1.1   mycroft 	printf("    Inode addresses:");
    615   1.8      fvdl 	numbytes = 0;
    616  1.25  perseant 	numblocks = 0;
    617  1.58  dholland 	for (i = 0; i < lfs_ss_getninos(lfsp, sp); iip = NEXTLOWER_IINFO(lfsp, iip)) {
    618  1.25  perseant 		++numblocks;
    619  1.43  dholland 		numbytes += lfs_sb_getibsize(lfsp);	/* add bytes for inode block */
    620  1.58  dholland 		printf("\t0x%jx {", (intmax_t)lfs_ii_getblock(lfsp, iip));
    621  1.58  dholland 		get(fd, fsbtobyte(lfsp, lfs_ii_getblock(lfsp, iip)), diblock, lfs_sb_getibsize(lfsp));
    622  1.52  dholland 		for (j = 0; i < lfs_ss_getninos(lfsp, sp) && j < LFS_INOPB(lfsp); j++, i++) {
    623   1.1   mycroft 			if (j > 0)
    624   1.1   mycroft 				(void)printf(", ");
    625  1.54  dholland 			dip = DINO_IN_BLOCK(lfsp, diblock, j);
    626  1.54  dholland 			(void)printf("%juv%d", lfs_dino_getinumber(lfsp, dip),
    627  1.54  dholland 				     lfs_dino_getgen(lfsp, dip));
    628   1.1   mycroft 		}
    629   1.1   mycroft 		(void)printf("}");
    630  1.41  christos 		if (((i/LFS_INOPB(lfsp)) % 4) == 3)
    631   1.1   mycroft 			(void)printf("\n");
    632   1.1   mycroft 	}
    633  1.54  dholland 	free(diblock);
    634   1.1   mycroft 
    635   1.1   mycroft 	printf("\n");
    636  1.25  perseant 
    637  1.52  dholland 	fp = SEGSUM_FINFOBASE(lfsp, sp);
    638  1.52  dholland 	for (i = 0; i < lfs_ss_getnfinfo(lfsp, sp); i++) {
    639  1.53  dholland 		(void)printf("    FINFO for inode: %ju version %u nblocks %u lastlength %u\n",
    640  1.53  dholland 		    (uintmax_t)lfs_fi_getino(lfsp, fp),
    641  1.53  dholland 		    lfs_fi_getversion(lfsp, fp),
    642  1.53  dholland 		    lfs_fi_getnblocks(lfsp, fp),
    643  1.53  dholland 		    lfs_fi_getlastlength(lfsp, fp));
    644  1.53  dholland 		lfs_blocks_fromfinfo(lfsp, &fipblocks, fp);
    645  1.53  dholland 		numblocks += lfs_fi_getnblocks(lfsp, fp);
    646  1.53  dholland 		for (j = 0; j < lfs_fi_getnblocks(lfsp, fp); j++) {
    647  1.53  dholland 			(void)printf("\t%jd",
    648  1.53  dholland 			    (intmax_t)lfs_blocks_get(lfsp, &fipblocks, j));
    649   1.1   mycroft 			if ((j % 8) == 7)
    650   1.1   mycroft 				(void)printf("\n");
    651  1.53  dholland 			if (j == lfs_fi_getnblocks(lfsp, fp) - 1)
    652  1.53  dholland 				numbytes += lfs_fi_getlastlength(lfsp, fp);
    653   1.8      fvdl 			else
    654  1.43  dholland 				numbytes += lfs_sb_getbsize(lfsp);
    655   1.1   mycroft 		}
    656   1.1   mycroft 		if ((j % 8) != 0)
    657   1.1   mycroft 			(void)printf("\n");
    658  1.53  dholland 		fp = NEXT_FINFO(lfsp, fp);
    659   1.1   mycroft 	}
    660  1.25  perseant 
    661  1.25  perseant 	if (datasum_check == 0)
    662  1.25  perseant 		return (numbytes);
    663  1.25  perseant 
    664  1.25  perseant 	/*
    665  1.25  perseant 	 * Now that we know the number of blocks, run back through and
    666  1.25  perseant 	 * compute the data checksum.  (A bad data checksum is not enough
    667  1.25  perseant 	 * to prevent us from continuing, but it odes merit a warning.)
    668  1.25  perseant 	 */
    669  1.58  dholland 	iip2 = SEGSUM_IINFOSTART(lfsp, sp);
    670  1.60  dholland 	fp = SEGSUM_FINFOBASE(lfsp, sp);
    671  1.48  dholland 	if (lfs_sb_getversion(lfsp) == 1) {
    672  1.25  perseant 		el_size = sizeof(unsigned long);
    673  1.25  perseant 	} else {
    674  1.25  perseant 		el_size = sizeof(u_int32_t);
    675  1.25  perseant 	}
    676  1.25  perseant 	datap = (char *)malloc(el_size * numblocks);
    677  1.25  perseant 	memset(datap, 0, el_size * numblocks);
    678  1.25  perseant 	acc = 0;
    679  1.43  dholland 	addr += lfs_btofsb(lfsp, lfs_sb_getsumsize(lfsp));
    680  1.43  dholland 	buf = malloc(lfs_sb_getbsize(lfsp));
    681  1.52  dholland 	for (i = 0; i < lfs_ss_getnfinfo(lfsp, sp); i++) {
    682  1.58  dholland 		while (addr == lfs_ii_getblock(lfsp, iip2)) {
    683  1.43  dholland 			get(fd, fsbtobyte(lfsp, addr), buf, lfs_sb_getibsize(lfsp));
    684  1.25  perseant 			memcpy(datap + acc * el_size, buf, el_size);
    685  1.43  dholland 			addr += lfs_btofsb(lfsp, lfs_sb_getibsize(lfsp));
    686  1.58  dholland 			iip2 = NEXTLOWER_IINFO(lfsp, iip2);
    687  1.25  perseant 			++acc;
    688  1.25  perseant 		}
    689  1.53  dholland 		for (j = 0; j < lfs_fi_getnblocks(lfsp, fp); j++) {
    690  1.43  dholland 			get(fd, fsbtobyte(lfsp, addr), buf, lfs_sb_getfsize(lfsp));
    691  1.25  perseant 			memcpy(datap + acc * el_size, buf, el_size);
    692  1.53  dholland 			if (j == lfs_fi_getnblocks(lfsp, fp) - 1)
    693  1.53  dholland 				addr += lfs_btofsb(lfsp, lfs_fi_getlastlength(lfsp, fp));
    694  1.25  perseant 			else
    695  1.43  dholland 				addr += lfs_btofsb(lfsp, lfs_sb_getbsize(lfsp));
    696  1.25  perseant 			++acc;
    697  1.25  perseant 		}
    698  1.53  dholland 		fp = NEXT_FINFO(lfsp, fp);
    699  1.25  perseant 	}
    700  1.58  dholland 	while (addr == lfs_ii_getblock(lfsp, iip2)) {
    701  1.43  dholland 		get(fd, fsbtobyte(lfsp, addr), buf, lfs_sb_getibsize(lfsp));
    702  1.25  perseant 		memcpy(datap + acc * el_size, buf, el_size);
    703  1.43  dholland 		addr += lfs_btofsb(lfsp, lfs_sb_getibsize(lfsp));
    704  1.58  dholland 		iip2 = NEXTLOWER_IINFO(lfsp, iip2);
    705  1.25  perseant 		++acc;
    706  1.25  perseant 	}
    707  1.25  perseant 	free(buf);
    708  1.25  perseant 	if (acc != numblocks)
    709  1.25  perseant 		printf("** counted %d blocks but should have been %d\n",
    710  1.25  perseant 		     acc, numblocks);
    711  1.25  perseant 	datasum = cksum(datap, numblocks * el_size);
    712  1.52  dholland 	if (datasum != lfs_ss_getdatasum(lfsp, sp))
    713  1.52  dholland 		printf("** computed datasum 0x%lx does not match given datasum 0x%lx\n", (unsigned long)datasum, (unsigned long)lfs_ss_getdatasum(lfsp, sp));
    714  1.25  perseant 	free(datap);
    715  1.25  perseant 
    716   1.8      fvdl 	return (numbytes);
    717   1.1   mycroft }
    718   1.1   mycroft 
    719   1.1   mycroft static void
    720  1.19  perseant dump_segment(int fd, int segnum, daddr_t addr, struct lfs *lfsp, int dump_sb)
    721   1.1   mycroft {
    722   1.1   mycroft 	struct lfs lfs_sb, *sbp;
    723   1.1   mycroft 	SEGSUM *sump;
    724  1.52  dholland 	size_t sumstart;
    725  1.19  perseant 	char *sumblock;
    726   1.8      fvdl 	int did_one, nbytes, sb;
    727  1.14  perseant 	off_t sum_offset;
    728  1.14  perseant 	daddr_t new_addr;
    729   1.1   mycroft 
    730  1.20      fvdl 	(void)printf("\nSEGMENT %lld (Disk Address 0x%llx)\n",
    731  1.41  christos 		     (long long)lfs_dtosn(lfsp, addr), (long long)addr);
    732  1.19  perseant 	sum_offset = fsbtobyte(lfsp, addr);
    733  1.43  dholland 	sumblock = malloc(lfs_sb_getsumsize(lfsp));
    734  1.19  perseant 
    735  1.48  dholland 	if (lfs_sb_getversion(lfsp) > 1 && segnum == 0) {
    736  1.44  dholland 		if (lfs_fsbtob(lfsp, lfs_sb_gets0addr(lfsp)) < LFS_LABELPAD) {
    737  1.24  perseant 			/* First segment eats the disklabel */
    738  1.41  christos 			sum_offset += lfs_fragroundup(lfsp, LFS_LABELPAD) -
    739  1.44  dholland 				      lfs_fsbtob(lfsp, lfs_sb_gets0addr(lfsp));
    740  1.41  christos 			addr += lfs_btofsb(lfsp, lfs_fragroundup(lfsp, LFS_LABELPAD)) -
    741  1.44  dholland 				lfs_sb_gets0addr(lfsp);
    742  1.24  perseant 			printf("Disklabel at 0x0\n");
    743  1.24  perseant 		}
    744  1.19  perseant 	}
    745   1.1   mycroft 
    746   1.1   mycroft 	sb = 0;
    747   1.1   mycroft 	did_one = 0;
    748   1.1   mycroft 	do {
    749  1.43  dholland 		get(fd, sum_offset, sumblock, lfs_sb_getsumsize(lfsp));
    750   1.1   mycroft 		sump = (SEGSUM *)sumblock;
    751  1.52  dholland 		sumstart = lfs_ss_getsumstart(lfsp);
    752  1.48  dholland 		if ((lfs_sb_getversion(lfsp) > 1 &&
    753  1.52  dholland 		     lfs_ss_getident(lfsp, sump) != lfs_sb_getident(lfsp)) ||
    754  1.52  dholland 		    lfs_ss_getsumsum(lfsp, sump) !=
    755  1.52  dholland 		      cksum((char *)sump + sumstart,
    756  1.52  dholland 			    lfs_sb_getsumsize(lfsp) - sumstart)) {
    757   1.1   mycroft 			sbp = (struct lfs *)sump;
    758  1.49  dholland 			if ((sb = (sbp->lfs_dlfs_u.u_32.dlfs_magic == LFS_MAGIC))) {
    759  1.19  perseant 				printf("Superblock at 0x%x\n",
    760  1.41  christos 				       (unsigned)lfs_btofsb(lfsp, sum_offset));
    761  1.14  perseant 				if (dump_sb)  {
    762  1.49  dholland 					__CTASSERT(sizeof(struct dlfs) ==
    763  1.49  dholland 						   sizeof(struct dlfs64));
    764  1.49  dholland 					get(fd, sum_offset, &(lfs_sb.lfs_dlfs_u),
    765  1.14  perseant 					    sizeof(struct dlfs));
    766  1.14  perseant 					dump_super(&lfs_sb);
    767  1.14  perseant 				}
    768  1.48  dholland 				if (lfs_sb_getversion(lfsp) > 1)
    769  1.41  christos 					sum_offset += lfs_fragroundup(lfsp, LFS_SBPAD);
    770  1.19  perseant 				else
    771  1.19  perseant 					sum_offset += LFS_SBPAD;
    772   1.1   mycroft 			} else if (did_one)
    773   1.1   mycroft 				break;
    774   1.1   mycroft 			else {
    775  1.20      fvdl 				printf("Segment at 0x%llx empty or corrupt\n",
    776  1.20      fvdl                                        (long long)addr);
    777   1.1   mycroft 				break;
    778   1.1   mycroft 			}
    779   1.1   mycroft 		} else {
    780  1.19  perseant 			nbytes = dump_sum(fd, lfsp, sump, segnum,
    781  1.41  christos 				lfs_btofsb(lfsp, sum_offset));
    782  1.34  perseant 			if (nbytes >= 0)
    783  1.43  dholland 				sum_offset += lfs_sb_getsumsize(lfsp) + nbytes;
    784   1.1   mycroft 			else
    785   1.1   mycroft 				sum_offset = 0;
    786   1.1   mycroft 			did_one = 1;
    787   1.1   mycroft 		}
    788  1.14  perseant 		/* If the segment ends right on a boundary, it still ends */
    789  1.41  christos 		new_addr = lfs_btofsb(lfsp, sum_offset);
    790  1.19  perseant 		/* printf("end daddr = 0x%lx\n", (long)new_addr); */
    791  1.41  christos 		if (lfs_dtosn(lfsp, new_addr) != lfs_dtosn(lfsp, addr))
    792  1.14  perseant 			break;
    793   1.1   mycroft 	} while (sum_offset);
    794   1.1   mycroft 
    795  1.30       dsl 	free(sumblock);
    796   1.1   mycroft }
    797   1.1   mycroft 
    798   1.1   mycroft static void
    799  1.19  perseant dump_super(struct lfs *lfsp)
    800   1.1   mycroft {
    801  1.43  dholland 	time_t stamp;
    802   1.1   mycroft 	int i;
    803   1.1   mycroft 
    804  1.46  dholland  	(void)printf("    %s0x%-8x  %s0x%-8x  %s%-10ju\n",
    805  1.49  dholland  		     "magic    ", lfsp->lfs_dlfs_u.u_32.dlfs_magic,
    806  1.48  dholland  		     "version  ", lfs_sb_getversion(lfsp),
    807  1.46  dholland  		     "size     ", (uintmax_t)lfs_sb_getsize(lfsp));
    808  1.46  dholland  	(void)printf("    %s%-10d  %s%-10ju  %s%-10d\n",
    809  1.43  dholland  		     "ssize    ", lfs_sb_getssize(lfsp),
    810  1.46  dholland  		     "dsize    ", (uintmax_t)lfs_sb_getdsize(lfsp),
    811  1.43  dholland  		     "bsize    ", lfs_sb_getbsize(lfsp));
    812  1.19  perseant  	(void)printf("    %s%-10d  %s%-10d  %s%-10d\n",
    813  1.43  dholland  		     "fsize    ", lfs_sb_getfsize(lfsp),
    814  1.43  dholland  		     "frag     ", lfs_sb_getfrag(lfsp),
    815  1.43  dholland  		     "minfree  ", lfs_sb_getminfree(lfsp));
    816  1.19  perseant  	(void)printf("    %s%-10d  %s%-10d  %s%-10d\n",
    817  1.43  dholland  		     "inopb    ", lfs_sb_getinopb(lfsp),
    818  1.43  dholland  		     "ifpb     ", lfs_sb_getifpb(lfsp),
    819  1.43  dholland  		     "nindir   ", lfs_sb_getnindir(lfsp));
    820  1.19  perseant  	(void)printf("    %s%-10d  %s%-10d  %s%-10d\n",
    821  1.43  dholland  		     "nseg     ", lfs_sb_getnseg(lfsp),
    822  1.43  dholland  		     "sepb     ", lfs_sb_getsepb(lfsp),
    823  1.43  dholland  		     "cleansz  ", lfs_sb_getcleansz(lfsp));
    824  1.19  perseant  	(void)printf("    %s%-10d  %s0x%-8x  %s%-10d\n",
    825  1.43  dholland  		     "segtabsz ", lfs_sb_getsegtabsz(lfsp),
    826  1.43  dholland  		     "segmask  ", lfs_sb_getsegmask(lfsp),
    827  1.43  dholland  		     "segshift ", lfs_sb_getsegshift(lfsp));
    828  1.43  dholland  	(void)printf("    %s0x%-8jx  %s%-10d  %s0x%-8jX\n",
    829  1.43  dholland  		     "bmask    ", (uintmax_t)lfs_sb_getbmask(lfsp),
    830  1.43  dholland  		     "bshift   ", lfs_sb_getbshift(lfsp),
    831  1.43  dholland  		     "ffmask   ", (uintmax_t)lfs_sb_getffmask(lfsp));
    832  1.43  dholland  	(void)printf("    %s%-10d  %s0x%-8jx  %s%u\n",
    833  1.43  dholland  		     "ffshift  ", lfs_sb_getffshift(lfsp),
    834  1.43  dholland  		     "fbmask   ", (uintmax_t)lfs_sb_getfbmask(lfsp),
    835  1.43  dholland  		     "fbshift  ", lfs_sb_getfbshift(lfsp));
    836  1.19  perseant 
    837  1.19  perseant  	(void)printf("    %s%-10d  %s%-10d  %s0x%-8x\n",
    838  1.43  dholland  		     "sushift  ", lfs_sb_getsushift(lfsp),
    839  1.43  dholland  		     "fsbtodb  ", lfs_sb_getfsbtodb(lfsp),
    840  1.43  dholland  		     "cksum    ", lfs_sb_getcksum(lfsp));
    841  1.19  perseant  	(void)printf("    %s%-10d  %s%-10d  %s%-10d\n",
    842  1.43  dholland  		     "nclean   ", lfs_sb_getnclean(lfsp),
    843  1.43  dholland  		     "dmeta    ", lfs_sb_getdmeta(lfsp),
    844  1.43  dholland  		     "minfreeseg ", lfs_sb_getminfreeseg(lfsp));
    845  1.19  perseant  	(void)printf("    %s0x%-8x  %s%-9d %s%-10d\n",
    846  1.43  dholland  		     "roll_id  ", lfs_sb_getident(lfsp),
    847  1.43  dholland  		     "interleave ", lfs_sb_getinterleave(lfsp),
    848  1.43  dholland  		     "sumsize  ", lfs_sb_getsumsize(lfsp));
    849  1.47  dholland  	(void)printf("    %s%-10jd  %s0x%-8jx\n",
    850  1.47  dholland 		     "seg0addr ", (intmax_t)lfs_sb_gets0addr(lfsp),
    851  1.43  dholland  		     "maxfilesize  ", (uintmax_t)lfs_sb_getmaxfilesize(lfsp));
    852  1.19  perseant 
    853  1.19  perseant 
    854  1.19  perseant  	(void)printf("  Superblock disk addresses:\n    ");
    855  1.19  perseant   	for (i = 0; i < LFS_MAXNUMSB; i++) {
    856  1.47  dholland  		(void)printf(" 0x%-8jx", (intmax_t)lfs_sb_getsboff(lfsp, i));
    857  1.19  perseant  		if (i == (LFS_MAXNUMSB >> 1))
    858  1.19  perseant  			(void)printf("\n    ");
    859  1.19  perseant   	}
    860  1.19  perseant   	(void)printf("\n");
    861  1.19  perseant 
    862  1.19  perseant  	(void)printf("  Checkpoint Info\n");
    863  1.57  dholland  	(void)printf("    %s%-10ju  %s0x%-8jx\n",
    864  1.56  dholland  		     "freehd   ", (uintmax_t)lfs_sb_getfreehd(lfsp),
    865  1.57  dholland  		     "idaddr   ", (intmax_t)lfs_sb_getidaddr(lfsp));
    866  1.46  dholland  	(void)printf("    %s%-10d  %s%-10jd  %s%-10jd\n",
    867  1.43  dholland  		     "uinodes  ", lfs_sb_getuinodes(lfsp),
    868  1.46  dholland  		     "bfree    ", (intmax_t)lfs_sb_getbfree(lfsp),
    869  1.46  dholland  		     "avail    ", (intmax_t)lfs_sb_getavail(lfsp));
    870  1.56  dholland  	(void)printf("    %s%-10ju  %s0x%-8jx  %s0x%-8jx\n",
    871  1.56  dholland  		     "nfiles   ", (uintmax_t)lfs_sb_getnfiles(lfsp),
    872  1.47  dholland  		     "lastseg  ", (uintmax_t)lfs_sb_getlastseg(lfsp),
    873  1.47  dholland  		     "nextseg  ", (uintmax_t)lfs_sb_getnextseg(lfsp));
    874  1.47  dholland  	(void)printf("    %s0x%-8jx  %s0x%-8jx  %s%-10ju\n",
    875  1.47  dholland  		     "curseg   ", (uintmax_t)lfs_sb_getcurseg(lfsp),
    876  1.47  dholland  		     "offset   ", (uintmax_t)lfs_sb_getoffset(lfsp),
    877  1.43  dholland 		     "serial   ", (uintmax_t)lfs_sb_getserial(lfsp));
    878  1.43  dholland 	stamp = lfs_sb_gettstamp(lfsp);
    879  1.43  dholland  	(void)printf("    tstamp   %s", ctime(&stamp));
    880  1.57  dholland 
    881  1.57  dholland 	if (!lfsp->lfs_is64) {
    882  1.57  dholland 		(void)printf("  32-bit only derived or constant fields\n");
    883  1.57  dholland 		(void)printf("    %s%-10u\n",
    884  1.57  dholland  		     "ifile    ", lfs_sb_getifile(lfsp));
    885  1.57  dholland 	}
    886   1.1   mycroft }
    887   1.1   mycroft 
    888   1.1   mycroft static void
    889  1.19  perseant addseg(char *arg)
    890   1.1   mycroft {
    891   1.1   mycroft 	SEGLIST *p;
    892   1.1   mycroft 
    893   1.1   mycroft 	if ((p = malloc(sizeof(SEGLIST))) == NULL)
    894   1.2   mycroft 		err(1, "malloc");
    895   1.1   mycroft 	p->next = seglist;
    896   1.1   mycroft 	p->num = atoi(arg);
    897   1.1   mycroft 	seglist = p;
    898   1.1   mycroft }
    899   1.1   mycroft 
    900   1.1   mycroft static void
    901  1.19  perseant dump_cleaner_info(struct lfs *lfsp, void *ipage)
    902   1.1   mycroft {
    903   1.1   mycroft 	CLEANERINFO *cip;
    904   1.1   mycroft 
    905   1.1   mycroft 	cip = (CLEANERINFO *)ipage;
    906  1.48  dholland 	if (lfs_sb_getversion(lfsp) > 1) {
    907  1.50  dholland 		(void)printf("free_head %ju\n",
    908  1.50  dholland 			     (uintmax_t)lfs_ci_getfree_head(lfsp, cip));
    909  1.50  dholland 		(void)printf("free_tail %ju\n",
    910  1.50  dholland 			     (uintmax_t)lfs_ci_getfree_tail(lfsp, cip));
    911  1.50  dholland 	}
    912  1.50  dholland 	(void)printf("clean\t%u\tdirty\t%u\n",
    913  1.50  dholland 		     lfs_ci_getclean(lfsp, cip), lfs_ci_getdirty(lfsp, cip));
    914  1.50  dholland 	(void)printf("bfree\t%jd\tavail\t%jd\n\n",
    915  1.50  dholland 		     (intmax_t)lfs_ci_getbfree(lfsp, cip),
    916  1.50  dholland 		     (intmax_t)lfs_ci_getavail(lfsp, cip));
    917   1.1   mycroft }
    918   1.1   mycroft 
    919   1.1   mycroft static void
    920  1.19  perseant usage(void)
    921   1.1   mycroft {
    922  1.27       wiz 	(void)fprintf(stderr, "usage: dumplfs [-adiS] [-b blkno] [-I blkno] [-s segno] filesys|device\n");
    923   1.1   mycroft 	exit(1);
    924   1.1   mycroft }
    925