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