155860Sbostic /*-
255860Sbostic  * Copyright (c) 1992 The Regents of the University of California.
355860Sbostic  * All rights reserved.
455860Sbostic  *
555860Sbostic  * %sccs.include.redist.c%
655860Sbostic  */
755860Sbostic 
855860Sbostic #ifndef lint
9*56036Sbostic static char sccsid[] = "@(#)library.c	5.4 (Berkeley) 08/26/92";
1055860Sbostic #endif /* not lint */
1155860Sbostic 
1255491Sbostic #include <sys/param.h>
1355491Sbostic #include <sys/time.h>
1455491Sbostic #include <sys/stat.h>
1555491Sbostic #include <sys/mount.h>
1655491Sbostic 
1755491Sbostic #include <ufs/ufs/dinode.h>
1855491Sbostic #include <ufs/lfs/lfs.h>
1955491Sbostic 
2055491Sbostic #include <fcntl.h>
2155491Sbostic #include <stdio.h>
2255491Sbostic #include <unistd.h>
2355856Sbostic #include <stdlib.h>
2455856Sbostic #include <string.h>
2555491Sbostic #include "clean.h"
2655491Sbostic 
2755856Sbostic void	 add_blocks __P((FS_INFO *, BLOCK_INFO *, int *, SEGSUM *, caddr_t,
2855856Sbostic 	     daddr_t, daddr_t));
2955930Sbostic void	 add_inodes __P((FS_INFO *, BLOCK_INFO *, int *, SEGSUM *, caddr_t,
3055856Sbostic 	     daddr_t));
3155856Sbostic int	 bi_compare __P((const void *, const void *));
3255856Sbostic int	 bi_toss __P((const void *, const void *, const void *));
3355856Sbostic void	 get_ifile __P((FS_INFO *));
3455856Sbostic int	 get_superblock __P((FS_INFO *, struct lfs *));
3555856Sbostic int	 pseg_valid __P((FS_INFO *, SEGSUM *));
3655856Sbostic 
3755491Sbostic /*
3855856Sbostic  * This function will get information on all mounted file systems
3955491Sbostic  * of a given type.
4055491Sbostic  */
4155491Sbostic int
4255491Sbostic fs_getmntinfo(buf, type)
4355491Sbostic 	struct	statfs	**buf;
4455491Sbostic 	int	type;
4555491Sbostic {
4655856Sbostic 	struct statfs *tstatfsp;
4755856Sbostic 	struct statfs *sbp;
4855856Sbostic 	int count, i, tcount;
4955491Sbostic 
5055863Sbostic 	tcount = getmntinfo(&tstatfsp, MNT_NOWAIT);
5155491Sbostic 
5255491Sbostic 	if (tcount < 0) {
5355856Sbostic 		err(0, "getmntinfo failed");
5455856Sbostic 		return (-1);
5555491Sbostic 	}
5655491Sbostic 
5755856Sbostic 	for (count = 0, i = 0; i < tcount ; ++i)
5855856Sbostic 		if (tstatfsp[i].f_type == type)
5955856Sbostic 			++count;
6055491Sbostic 
6155856Sbostic 	if (count) {
6255856Sbostic 		if (!(*buf = (struct statfs *)
6355856Sbostic 			malloc(count * sizeof(struct statfs))))
6455856Sbostic 			err(1, "fs_getmntinfo: out of space");
6555856Sbostic 		for (i = 0, sbp = *buf; i < tcount ; ++i) {
6655856Sbostic 			if (tstatfsp[i].f_type == type) {
6755856Sbostic 				*sbp = tstatfsp[i];
6855856Sbostic 				++sbp;
6955491Sbostic 			}
7055491Sbostic 		}
7155491Sbostic 	}
7255856Sbostic 	return (count);
7355491Sbostic }
7455491Sbostic 
7555491Sbostic /*
7655856Sbostic  * Get all the information available on an LFS file system.
7755856Sbostic  * Returns an array of FS_INFO structures, NULL on error.
7855491Sbostic  */
7955856Sbostic FS_INFO *
8055856Sbostic get_fs_info (lstatfsp, count)
8155856Sbostic 	struct statfs *lstatfsp;	/* IN: array of statfs structs */
8255856Sbostic 	int count;			/* IN: number of file systems */
8355491Sbostic {
8455856Sbostic 	FS_INFO	*fp, *fsp;
8555491Sbostic 	int	i;
8655491Sbostic 
8755856Sbostic 	fsp = (FS_INFO *)malloc(count * sizeof(FS_INFO));
8855491Sbostic 
8955856Sbostic 	for (fp = fsp, i = 0; i < count; ++i, ++fp) {
9055856Sbostic 		fp->fi_statfsp = lstatfsp++;
9155856Sbostic 		if (get_superblock (fp, &fp->fi_lfs))
9255856Sbostic 			err(1, "get_fs_info: get_superblock failed");
9355856Sbostic 		fp->fi_daddr_shift =
9455856Sbostic 		     fp->fi_lfs.lfs_bshift - fp->fi_lfs.lfs_fsbtodb;
9555856Sbostic 		get_ifile (fp);
9655491Sbostic 	}
9755856Sbostic 	return (fsp);
9855491Sbostic }
9955491Sbostic 
10055856Sbostic /*
10155856Sbostic  * If we are reading the ifile then we need to refresh it.  Even if
10255856Sbostic  * we are mmapping it, it might have grown.  Finally, we need to
10355856Sbostic  * refresh the file system information (statfs) info.
10455856Sbostic  */
10555491Sbostic void
10655856Sbostic reread_fs_info(fsp, count)
10755856Sbostic 	FS_INFO *fsp;	/* IN: array of fs_infos to free */
10855856Sbostic 	int count;	/* IN: number of file systems */
10955491Sbostic {
11055856Sbostic 	int i;
11155491Sbostic 
11255856Sbostic 	for (i = 0; i < count; ++i, ++fsp) {
11355856Sbostic 		if (statfs(fsp->fi_statfsp->f_mntonname, fsp->fi_statfsp))
11455856Sbostic 			err(0, "reread_fs_info: statfs failed");
11555856Sbostic #ifdef MMAP_WORKS
11655856Sbostic 		if (munmap(fsp->fi_cip, fsp->fi_ifile_length) < 0)
11755856Sbostic 			err(0, "reread_fs_info: munmap failed");
11855491Sbostic #else
11955856Sbostic 		free (fsp->fi_cip);
12055491Sbostic #endif /* MMAP_WORKS */
12155856Sbostic 		get_ifile (fsp);
12255491Sbostic 	}
12355491Sbostic }
12455491Sbostic 
12555491Sbostic /*
12655856Sbostic  * Gets the superblock from disk (possibly in face of errors)
12755491Sbostic  */
12855491Sbostic int
12955491Sbostic get_superblock (fsp, sbp)
13055856Sbostic 	FS_INFO *fsp;		/* local file system info structure */
13155856Sbostic 	struct lfs *sbp;
13255491Sbostic {
13355856Sbostic 	char mntfromname[MNAMELEN+1];
13455856Sbostic         int fid;
13555491Sbostic 
13655491Sbostic 	strcpy(mntfromname, "/dev/r");
13755856Sbostic 	strcat(mntfromname, fsp->fi_statfsp->f_mntfromname+5);
13855491Sbostic 
13955491Sbostic 	if ((fid = open(mntfromname, O_RDONLY, (mode_t)0)) < 0) {
14055856Sbostic 		err(0, "get_superblock: bad open");
14155856Sbostic 		return (-1);
14255491Sbostic 	}
14355491Sbostic 
14455856Sbostic 	get(fid, LFS_LABELPAD, sbp, sizeof(struct lfs));
14555491Sbostic 	close (fid);
14655491Sbostic 
14755856Sbostic 	return (0);
14855491Sbostic }
14955491Sbostic 
15055491Sbostic /*
15155856Sbostic  * This function will map the ifile into memory.  It causes a
15255856Sbostic  * fatal error on failure.
15355491Sbostic  */
15455856Sbostic void
15555491Sbostic get_ifile (fsp)
15655491Sbostic 	FS_INFO	*fsp;
15755491Sbostic {
15855856Sbostic 	struct stat file_stat;
15955856Sbostic 	caddr_t ifp;
16055856Sbostic 	char *ifile_name;
16155856Sbostic 	int count, fid;
16255491Sbostic 
16355856Sbostic 	ifp = NULL;
16455856Sbostic 	ifile_name = malloc(strlen(fsp->fi_statfsp->f_mntonname) +
16555856Sbostic 	    strlen(IFILE_NAME)+2);
16655856Sbostic 	strcat(strcat(strcpy(ifile_name, fsp->fi_statfsp->f_mntonname), "/"),
16755856Sbostic 	    IFILE_NAME);
16855491Sbostic 
16955856Sbostic 	if ((fid = open(ifile_name, O_RDWR, (mode_t)0)) < 0)
17055856Sbostic 		err(1, "get_ifile: bad open");
17155491Sbostic 
17255856Sbostic 	if (fstat (fid, &file_stat))
17355856Sbostic 		err(1, "get_ifile: fstat failed");
17455491Sbostic 
17555856Sbostic 	fsp->fi_ifile_length = file_stat.st_size;
17655856Sbostic 
17755491Sbostic 	/* get the ifile */
17855491Sbostic #ifndef MMAP_WORKS
17955856Sbostic 	if (!(ifp = malloc ((size_t)fsp->fi_ifile_length)))
18055856Sbostic 		err (1, "get_ifile: malloc failed");
18155856Sbostic redo_read:
18255856Sbostic 	count = read (fid, ifp, (size_t) fsp->fi_ifile_length);
18355491Sbostic 
18455856Sbostic 	if (count < 0)
18555856Sbostic 		err(1, "get_ifile: bad ifile read");
18655856Sbostic 	else if (count < (int)fsp->fi_ifile_length) {
18755856Sbostic 		err(0, "get_ifile");
18855856Sbostic 		if (lseek(fid, 0, SEEK_SET) < 0)
18955856Sbostic 			err(1, "get_ifile: bad ifile lseek");
19055856Sbostic 		goto redo_read;
19155491Sbostic 	}
19255491Sbostic #else	/* MMAP_WORKS */
19355856Sbostic 	ifp = mmap ((caddr_t)0, (size_t) fsp->fi_ifile_length, PROT_READ|PROT_WRITE,
19455491Sbostic 		MAP_FILE|MAP_SHARED, fid, (off_t)0);
19555856Sbostic 	if (ifp < 0)
19655856Sbostic 		err(1, "get_ifile: mmap failed");
19755491Sbostic #endif	/* MMAP_WORKS */
19855491Sbostic 
19955491Sbostic 	close (fid);
20055491Sbostic 
20155856Sbostic 	fsp->fi_cip = (CLEANERINFO *)ifp;
20255856Sbostic 	fsp->fi_segusep = (SEGUSE *)(ifp + CLEANSIZE(fsp));
20355856Sbostic 	fsp->fi_ifilep  = (IFILE *)((caddr_t)fsp->fi_segusep + SEGTABSIZE(fsp));
20455856Sbostic 
20555856Sbostic 	/*
20655856Sbostic 	 * The number of ifile entries is equal to the number of blocks
20755856Sbostic 	 * blocks in the ifile minus the ones allocated to cleaner info
20855856Sbostic 	 * and segment usage table multiplied by the number of ifile
20955856Sbostic 	 * entries per page.
21055856Sbostic 	 */
21155856Sbostic 	fsp->fi_ifile_count = (fsp->fi_ifile_length >> fsp->fi_lfs.lfs_bshift -
21255856Sbostic 	    fsp->fi_lfs.lfs_cleansz - fsp->fi_lfs.lfs_segtabsz) *
21355856Sbostic 	    fsp->fi_lfs.lfs_ifpb;
21455856Sbostic 
21555491Sbostic 	free (ifile_name);
21655491Sbostic }
21755491Sbostic 
21855491Sbostic /*
21955856Sbostic  * This function will scan a segment and return a list of
22055491Sbostic  * <inode, blocknum> pairs which indicate which blocks were
22155856Sbostic  * contained as live data within the segment when the segment
22255856Sbostic  * summary was read (it may have "died" since then).  Any given
22355856Sbostic  * pair will be listed at most once.
22455491Sbostic  */
22555491Sbostic int
22655930Sbostic lfs_segmapv(fsp, seg, seg_buf, blocks, bcount)
22755856Sbostic 	FS_INFO *fsp;		/* pointer to local file system information */
22855856Sbostic 	int seg;		/* the segment number */
22955856Sbostic 	caddr_t seg_buf;	/* the buffer containing the segment's data */
23055856Sbostic 	BLOCK_INFO **blocks;	/* OUT: array of block_info for live blocks */
23155856Sbostic 	int *bcount;		/* OUT: number of active blocks in segment */
23255491Sbostic {
23355856Sbostic 	BLOCK_INFO *bip;
23455856Sbostic 	SEGSUM *sp;
23555856Sbostic 	SEGUSE *sup;
23655856Sbostic 	struct lfs *lfsp;
23755856Sbostic 	caddr_t s, segend;
23855856Sbostic 	daddr_t pseg_addr, seg_addr;
23955930Sbostic 	int nelem, nblocks;
24055856Sbostic 	time_t timestamp;
24155491Sbostic 
24255856Sbostic 	lfsp = &fsp->fi_lfs;
24355930Sbostic 	nelem = 2 * lfsp->lfs_ssize;
24455930Sbostic 	if (!(bip = malloc(nelem * sizeof(BLOCK_INFO))))
24555856Sbostic 		goto err0;
24655491Sbostic 
24755856Sbostic 	sup = SEGUSE_ENTRY(lfsp, fsp->fi_segusep, seg);
24855856Sbostic 	s = seg_buf + (sup->su_flags & SEGUSE_SUPERBLOCK ? LFS_SBPAD : 0);
24955856Sbostic 	seg_addr = sntoda(lfsp, seg);
25055856Sbostic 	pseg_addr = seg_addr + (sup->su_flags & SEGUSE_SUPERBLOCK ? btodb(LFS_SBPAD) : 0);
25155856Sbostic #ifdef VERBOSE
25255856Sbostic 		printf("\tsegment buffer at: 0x%x\tseg_addr 0x%x\n", s, seg_addr);
25355856Sbostic #endif /* VERBOSE */
25455491Sbostic 
25555856Sbostic 	*bcount = 0;
25655856Sbostic 	for (segend = seg_buf + seg_size(lfsp), timestamp = 0; s < segend; ) {
25755856Sbostic 		sp = (SEGSUM *)s;
25855491Sbostic #ifdef VERBOSE
25955856Sbostic 		printf("\tpartial at: 0x%x\n", pseg_addr);
26055856Sbostic 		print_SEGSUM(lfsp, sp);
26155491Sbostic 		fflush(stdout);
26255491Sbostic #endif /* VERBOSE */
26355491Sbostic 
26455856Sbostic 		nblocks = pseg_valid(fsp, sp);
26555856Sbostic 		if (nblocks <= 0)
26655856Sbostic 			break;
26755491Sbostic 
26855856Sbostic 		/* Check if we have hit old data */
26955856Sbostic 		if (timestamp > ((SEGSUM*)s)->ss_create)
27055856Sbostic 			break;
27155491Sbostic 		timestamp = ((SEGSUM*)s)->ss_create;
27255491Sbostic 
27355930Sbostic 		if (*bcount + nblocks + sp->ss_ninos > nelem) {
27455930Sbostic 			nelem = *bcount + nblocks + sp->ss_ninos;
27555930Sbostic 			bip = realloc (bip, nelem * sizeof(BLOCK_INFO));
27655930Sbostic 			if (!bip)
27755930Sbostic 				goto err0;
27855856Sbostic 		}
27955856Sbostic 		add_blocks(fsp, bip, bcount, sp, seg_buf, seg_addr, pseg_addr);
28055930Sbostic 		add_inodes(fsp, bip, bcount, sp, seg_buf, seg_addr);
28155856Sbostic 		pseg_addr += fsbtodb(lfsp, nblocks) +
28255856Sbostic 		    bytetoda(fsp, LFS_SUMMARY_SIZE);
28355856Sbostic 		s += (nblocks << lfsp->lfs_bshift) + LFS_SUMMARY_SIZE;
28455856Sbostic 	}
28555856Sbostic 	qsort(bip, *bcount, sizeof(BLOCK_INFO), bi_compare);
28655856Sbostic 	toss(bip, bcount, sizeof(BLOCK_INFO), bi_toss, NULL);
28755856Sbostic #ifdef VERBOSE
28855856Sbostic 	{
28955856Sbostic 		BLOCK_INFO *_bip;
29055856Sbostic 		int i;
29155491Sbostic 
29255856Sbostic 		printf("BLOCK INFOS\n");
29355856Sbostic 		for (_bip = bip, i=0; i < *bcount; ++_bip, ++i)
29455856Sbostic 			PRINT_BINFO(_bip);
29555491Sbostic 	}
29655856Sbostic #endif
29755856Sbostic 	*blocks = bip;
29855856Sbostic 	return (0);
29955856Sbostic 
30055856Sbostic err0:	*bcount = 0;
30155856Sbostic 	return (-1);
30255491Sbostic 
30355491Sbostic }
30455491Sbostic 
30555491Sbostic /*
30655856Sbostic  * This will parse a partial segment and fill in BLOCK_INFO structures
30755856Sbostic  * for each block described in the segment summary.  It will not include
30855856Sbostic  * blocks or inodes from files with new version numbers.
30955491Sbostic  */
31055491Sbostic void
31155856Sbostic add_blocks (fsp, bip, countp, sp, seg_buf, segaddr, psegaddr)
31255491Sbostic 	FS_INFO *fsp;		/* pointer to super block */
31355856Sbostic 	BLOCK_INFO *bip;	/* Block info array */
31455856Sbostic 	int *countp;		/* IN/OUT: number of blocks in array */
31555856Sbostic 	SEGSUM	*sp;		/* segment summmary pointer */
31655856Sbostic 	caddr_t seg_buf;	/* buffer containing segment */
31755856Sbostic 	daddr_t segaddr;	/* address of this segment */
31855856Sbostic 	daddr_t psegaddr;	/* address of this partial segment */
31955491Sbostic {
32055856Sbostic 	IFILE	*ifp;
32155856Sbostic 	FINFO	*fip;
32255856Sbostic 	caddr_t	bp;
32355930Sbostic 	daddr_t	*dp, *iaddrp;
32455856Sbostic 	int db_per_block, i, j;
32555856Sbostic 	u_long page_size;
32655491Sbostic 
32755491Sbostic #ifdef VERBOSE
32855856Sbostic 	printf("FILE INFOS\n");
32955491Sbostic #endif
33055856Sbostic 	db_per_block = fsbtodb(&fsp->fi_lfs, 1);
33155856Sbostic 	page_size = fsp->fi_lfs.lfs_bsize;
33255856Sbostic 	bp = seg_buf + datobyte(fsp, psegaddr - segaddr) + LFS_SUMMARY_SIZE;
33355856Sbostic 	bip += *countp;
33455856Sbostic 	psegaddr += bytetoda(fsp, LFS_SUMMARY_SIZE);
33555856Sbostic 	iaddrp = (daddr_t *)((caddr_t)sp + LFS_SUMMARY_SIZE);
33655856Sbostic 	--iaddrp;
33755856Sbostic 	for (fip = (FINFO *)(sp + 1), i = 0; i < sp->ss_nfinfo;
33855856Sbostic 	    ++i, fip = (FINFO *)(&fip->fi_blocks[fip->fi_nblocks])) {
33955491Sbostic 
34055856Sbostic 		ifp = IFILE_ENTRY(&fsp->fi_lfs, fsp->fi_ifilep, fip->fi_ino);
34155856Sbostic 		PRINT_FINFO(fip, ifp);
34255856Sbostic 		if (ifp->if_version > fip->fi_version)
34355491Sbostic 			continue;
34455856Sbostic 		dp = &(fip->fi_blocks[0]);
34555856Sbostic 		for (j = 0; j < fip->fi_nblocks; j++, dp++) {
34655856Sbostic 			while (psegaddr == *iaddrp) {
34755856Sbostic 				psegaddr += db_per_block;
34855856Sbostic 				bp += page_size;
34955856Sbostic 				--iaddrp;
35055491Sbostic 			}
35155856Sbostic 			bip->bi_inode = fip->fi_ino;
35255856Sbostic 			bip->bi_lbn = *dp;
35355856Sbostic 			bip->bi_daddr = psegaddr;
35455856Sbostic 			bip->bi_segcreate = (time_t)(sp->ss_create);
35555856Sbostic 			bip->bi_bp = bp;
35655856Sbostic 			psegaddr += db_per_block;
35755856Sbostic 			bp += page_size;
35855856Sbostic 			++bip;
35955856Sbostic 			++(*countp);
36055491Sbostic 		}
36155491Sbostic 	}
36255491Sbostic }
36355491Sbostic 
36455856Sbostic /*
36555856Sbostic  * For a particular segment summary, reads the inode blocks and adds
36655856Sbostic  * INODE_INFO structures to the array.  Returns the number of inodes
36755856Sbostic  * actually added.
36855856Sbostic  */
36955491Sbostic void
37055930Sbostic add_inodes (fsp, bip, countp, sp, seg_buf, seg_addr)
37155491Sbostic 	FS_INFO *fsp;		/* pointer to super block */
37255930Sbostic 	BLOCK_INFO *bip;	/* block info array */
37355856Sbostic 	int *countp;		/* pointer to current number of inodes */
37455856Sbostic 	SEGSUM *sp;		/* segsum pointer */
37555491Sbostic 	caddr_t	seg_buf;	/* the buffer containing the segment's data */
37655856Sbostic 	daddr_t	seg_addr;	/* disk address of seg_buf */
37755491Sbostic {
37855856Sbostic 	struct dinode *di;
37955856Sbostic 	struct lfs *lfsp;
38055856Sbostic 	IFILE *ifp;
38155930Sbostic 	BLOCK_INFO *bp;
38255856Sbostic 	daddr_t	*daddrp;
38355856Sbostic 	ino_t inum;
38455856Sbostic 	int i;
38555491Sbostic 
38655856Sbostic 	if (sp->ss_ninos <= 0)
38755856Sbostic 		return;
38855491Sbostic 
38955930Sbostic 	bp = bip + *countp;
39055856Sbostic 	lfsp = &fsp->fi_lfs;
39155491Sbostic #ifdef VERBOSE
39255930Sbostic 	(void) printf("INODES:\n");
39355491Sbostic #endif
39455856Sbostic 	daddrp = (daddr_t *)((caddr_t)sp + LFS_SUMMARY_SIZE);
39555856Sbostic 	for (i = 0; i < sp->ss_ninos; ++i) {
39655491Sbostic 		if (i % INOPB(lfsp) == 0) {
39755856Sbostic 			--daddrp;
39855856Sbostic 			di = (struct dinode *)(seg_buf +
39955856Sbostic 			    ((*daddrp - seg_addr) << fsp->fi_daddr_shift));
40055491Sbostic 		} else
40155491Sbostic 			++di;
40255491Sbostic 
40355491Sbostic 		inum = di->di_inum;
40455930Sbostic 		bp->bi_lbn = LFS_UNUSED_LBN;
40555930Sbostic 		bp->bi_inode = inum;
40655930Sbostic 		bp->bi_daddr = *daddrp;
40755930Sbostic 		bp->bi_bp = di;
40855930Sbostic 		bp->bi_segcreate = sp->ss_create;
40955491Sbostic 
410*56036Sbostic 		if (inum == LFS_IFILE_INUM) {
41155930Sbostic 			bp++;
41255856Sbostic 			++(*countp);
413*56036Sbostic 			PRINT_INODE(1, bp);
414*56036Sbostic 		} else {
415*56036Sbostic 			ifp = IFILE_ENTRY(lfsp, fsp->fi_ifilep, inum);
416*56036Sbostic 			PRINT_INODE(ifp->if_daddr == *daddrp, bp);
417*56036Sbostic 			if (ifp->if_daddr == *daddrp) {
418*56036Sbostic 				bp++;
419*56036Sbostic 				++(*countp);
420*56036Sbostic 			}
421*56036Sbostic 		}
42255491Sbostic 	}
42355491Sbostic }
42455491Sbostic 
42555491Sbostic /*
42655856Sbostic  * Checks the summary checksum and the data checksum to determine if the
42755856Sbostic  * segment is valid or not.  Returns the size of the partial segment if it
42855856Sbostic  * is valid, * and 0 otherwise.  Use dump_summary to figure out size of the
42955856Sbostic  * the partial as well as whether or not the checksum is valid.
43055491Sbostic  */
43155491Sbostic int
43255491Sbostic pseg_valid (fsp, ssp)
43355491Sbostic 	FS_INFO *fsp;   /* pointer to file system info */
43455856Sbostic 	SEGSUM *ssp;	/* pointer to segment summary block */
43555491Sbostic {
43655491Sbostic 	caddr_t	p;
43755856Sbostic 	int i, nblocks;
43855856Sbostic 	u_long *datap;
43955491Sbostic 
44055856Sbostic 	if ((nblocks = dump_summary(&fsp->fi_lfs, ssp, 0, NULL)) <= 0)
44155856Sbostic 		return(0);
44255491Sbostic 
44355856Sbostic 	/* check data/inode block(s) checksum too */
44455856Sbostic 	datap = (u_long *)malloc(nblocks * sizeof(u_long));
44555491Sbostic 	p = (caddr_t)ssp + LFS_SUMMARY_SIZE;
44655856Sbostic 	for (i = 0; i < nblocks; ++i) {
44755491Sbostic 		datap[i] = *((u_long *)p);
44855856Sbostic 		p += fsp->fi_lfs.lfs_bsize;
44955491Sbostic 	}
45055856Sbostic 	if (cksum ((void *)datap, nblocks * sizeof(u_long)) != ssp->ss_datasum)
45155856Sbostic 		return (0);
45255491Sbostic 
45355856Sbostic 	return (nblocks);
45455491Sbostic }
45555491Sbostic 
45655491Sbostic 
45755491Sbostic /* #define MMAP_SEGMENT */
45855491Sbostic /*
45955491Sbostic  * read a segment into a memory buffer
46055491Sbostic  */
46155491Sbostic int
46255856Sbostic mmap_segment (fsp, segment, segbuf)
46355856Sbostic 	FS_INFO *fsp;		/* file system information */
46455856Sbostic 	int segment;		/* segment number */
46555856Sbostic 	caddr_t *segbuf;	/* pointer to buffer area */
46655491Sbostic {
46755856Sbostic 	struct lfs *lfsp;
46855856Sbostic 	int fid;		/* fildes for file system device */
46955856Sbostic 	daddr_t seg_daddr;	/* base disk address of segment */
47055856Sbostic 	off_t seg_byte;
47155856Sbostic 	size_t ssize;
47255856Sbostic 	char mntfromname[MNAMELEN+2];
47355491Sbostic 
47455856Sbostic 	lfsp = &fsp->fi_lfs;
47555856Sbostic 
47655491Sbostic 	/* get the disk address of the beginning of the segment */
47755491Sbostic 	seg_daddr = sntoda(lfsp, segment);
47855856Sbostic 	seg_byte = datobyte(fsp, seg_daddr);
47955856Sbostic 	ssize = seg_size(lfsp);
48055491Sbostic 
48155491Sbostic 	strcpy(mntfromname, "/dev/r");
48255856Sbostic 	strcat(mntfromname, fsp->fi_statfsp->f_mntfromname+5);
48355491Sbostic 
48455491Sbostic 	if ((fid = open(mntfromname, O_RDONLY, (mode_t)0)) < 0) {
48555856Sbostic 		err(0, "mmap_segment: bad open");
48655856Sbostic 		return (-1);
48755491Sbostic 	}
48855491Sbostic 
48955491Sbostic #ifdef MMAP_SEGMENT
49055856Sbostic 	*segbuf = mmap ((caddr_t)0, seg_size(lfsp), PROT_READ,
49155856Sbostic 	    MAP_FILE, fid, seg_byte);
49255856Sbostic 	if (*(long *)segbuf < 0) {
49355856Sbostic 		err(0, "mmap_segment: mmap failed");
49455856Sbostic 		return (NULL);
49555491Sbostic 	}
49655491Sbostic #else /* MMAP_SEGMENT */
49755856Sbostic #ifdef VERBOSE
49855856Sbostic 	printf("mmap_segment\tseg_daddr: %lu\tseg_size: %lu\tseg_offset: %qu\n",
49955856Sbostic 	    seg_daddr, ssize, seg_byte);
50055856Sbostic #endif
50155491Sbostic 	/* malloc the space for the buffer */
50255856Sbostic 	*segbuf = malloc(ssize);
50355856Sbostic 	if (!*segbuf) {
50455856Sbostic 		err(0, "mmap_segment: malloc failed");
50555856Sbostic 		return(NULL);
50655856Sbostic 	}
50755491Sbostic 
50855491Sbostic 	/* read the segment data into the buffer */
50955856Sbostic 	if (lseek (fid, seg_byte, SEEK_SET) != seg_byte) {
51055856Sbostic 		err (0, "mmap_segment: bad lseek");
51155856Sbostic 		free(*segbuf);
51255856Sbostic 		return (-1);
51355491Sbostic 	}
51455491Sbostic 
51555856Sbostic 	if (read (fid, *segbuf, ssize) != ssize) {
51655856Sbostic 		err (0, "mmap_segment: bad read");
51755856Sbostic 		free(*segbuf);
51855856Sbostic 		return (-1);
51955491Sbostic 	}
52055491Sbostic #endif /* MMAP_SEGMENT */
52155491Sbostic 	close (fid);
52255491Sbostic 
52355856Sbostic 	return (0);
52455491Sbostic }
52555491Sbostic 
52655491Sbostic void
52755491Sbostic munmap_segment (fsp, seg_buf)
52855856Sbostic 	FS_INFO *fsp;		/* file system information */
52955856Sbostic 	caddr_t seg_buf;	/* pointer to buffer area */
53055491Sbostic {
53155491Sbostic #ifdef MMAP_SEGMENT
53255856Sbostic 	munmap (seg_buf, seg_size(&fsp->fi_lfs));
53355491Sbostic #else /* MMAP_SEGMENT */
53455491Sbostic 	free (seg_buf);
53555491Sbostic #endif /* MMAP_SEGMENT */
53655491Sbostic }
53755491Sbostic 
53855491Sbostic 
53955491Sbostic /*
54055491Sbostic  * USEFUL DEBUGGING TOOLS:
54155491Sbostic  */
54255491Sbostic void
54355856Sbostic print_SEGSUM (lfsp, p)
54455856Sbostic 	struct lfs *lfsp;
54555856Sbostic 	SEGSUM	*p;
54655491Sbostic {
54755856Sbostic 	if (p)
54855856Sbostic 		(void) dump_summary(lfsp, p, DUMP_ALL, NULL);
54955491Sbostic 	else printf("0x0");
55055491Sbostic 	fflush(stdout);
55155491Sbostic }
55255491Sbostic 
55355856Sbostic int
55455856Sbostic bi_compare(a, b)
55555856Sbostic 	const void *a;
55655856Sbostic 	const void *b;
55755491Sbostic {
55855856Sbostic 	const BLOCK_INFO *ba, *bb;
55955856Sbostic 	int diff;
56055491Sbostic 
56155856Sbostic 	ba = a;
56255856Sbostic 	bb = b;
56355491Sbostic 
56455856Sbostic 	if (diff = (int)(ba->bi_inode - bb->bi_inode))
56555856Sbostic 		return (diff);
56655930Sbostic 	if (diff = (int)(ba->bi_lbn - bb->bi_lbn)) {
56755930Sbostic 		if (ba->bi_lbn == LFS_UNUSED_LBN)
56855930Sbostic 			return(-1);
56955930Sbostic 		else if (bb->bi_lbn == LFS_UNUSED_LBN)
57055930Sbostic 			return(1);
57155930Sbostic 		else if (ba->bi_lbn < 0)
57255930Sbostic 			return(1);
57355930Sbostic 		else
57455930Sbostic 			return (diff);
57555930Sbostic 	}
57655856Sbostic 	if (diff = (int)(ba->bi_segcreate - bb->bi_segcreate))
57755856Sbostic 		return (diff);
57855856Sbostic 	diff = (int)(ba->bi_daddr - bb->bi_daddr);
57955856Sbostic 	return (diff);
58055856Sbostic }
58155491Sbostic 
58255856Sbostic int
58355856Sbostic bi_toss(dummy, a, b)
58455856Sbostic 	const void *dummy;
58555856Sbostic 	const void *a;
58655856Sbostic 	const void *b;
58755491Sbostic {
58855856Sbostic 	const BLOCK_INFO *ba, *bb;
58955491Sbostic 
59055856Sbostic 	ba = a;
59155856Sbostic 	bb = b;
59255856Sbostic 
59355856Sbostic 	return(ba->bi_inode == bb->bi_inode && ba->bi_lbn == bb->bi_lbn);
59455491Sbostic }
59555491Sbostic 
59655491Sbostic void
59755856Sbostic toss(p, nump, size, dotoss, client)
59855856Sbostic 	void *p;
59955856Sbostic 	int *nump;
60055856Sbostic 	size_t size;
60155856Sbostic 	int (*dotoss) __P((const void *, const void *, const void *));
60255856Sbostic 	void *client;
60355491Sbostic {
60455856Sbostic 	int i;
60555856Sbostic 	void *p1;
60655491Sbostic 
60755856Sbostic 	if (*nump == 0)
60855856Sbostic 		return;
60955491Sbostic 
61055856Sbostic 	for (i = *nump; --i > 0;) {
61155856Sbostic 		p1 = p + size;
61255856Sbostic 		if (dotoss(client, p, p1)) {
61355856Sbostic 			bcopy(p1, p, i * size);
61455856Sbostic 			--(*nump);
61555856Sbostic 		} else
61655856Sbostic 			p += size;
61755491Sbostic 	}
61855491Sbostic }
619