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*55930Sbostic static char sccsid[] = "@(#)library.c 5.3 (Berkeley) 08/21/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)); 29*55930Sbostic 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 226*55930Sbostic 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; 239*55930Sbostic int nelem, nblocks; 24055856Sbostic time_t timestamp; 24155491Sbostic 24255856Sbostic lfsp = &fsp->fi_lfs; 243*55930Sbostic nelem = 2 * lfsp->lfs_ssize; 244*55930Sbostic 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 273*55930Sbostic if (*bcount + nblocks + sp->ss_ninos > nelem) { 274*55930Sbostic nelem = *bcount + nblocks + sp->ss_ninos; 275*55930Sbostic bip = realloc (bip, nelem * sizeof(BLOCK_INFO)); 276*55930Sbostic if (!bip) 277*55930Sbostic goto err0; 27855856Sbostic } 27955856Sbostic add_blocks(fsp, bip, bcount, sp, seg_buf, seg_addr, pseg_addr); 280*55930Sbostic 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; 323*55930Sbostic 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 370*55930Sbostic add_inodes (fsp, bip, countp, sp, seg_buf, seg_addr) 37155491Sbostic FS_INFO *fsp; /* pointer to super block */ 372*55930Sbostic 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; 381*55930Sbostic BLOCK_INFO *bp; 38255856Sbostic daddr_t *daddrp; 38355856Sbostic ino_t inum; 38455856Sbostic int i; 38555491Sbostic 38655856Sbostic if (sp->ss_ninos <= 0) 38755856Sbostic return; 38855491Sbostic 389*55930Sbostic bp = bip + *countp; 39055856Sbostic lfsp = &fsp->fi_lfs; 39155491Sbostic #ifdef VERBOSE 392*55930Sbostic (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; 404*55930Sbostic bp->bi_lbn = LFS_UNUSED_LBN; 405*55930Sbostic bp->bi_inode = inum; 406*55930Sbostic bp->bi_daddr = *daddrp; 407*55930Sbostic bp->bi_bp = di; 408*55930Sbostic bp->bi_segcreate = sp->ss_create; 40955491Sbostic 41055856Sbostic ifp = IFILE_ENTRY(lfsp, fsp->fi_ifilep, inum); 411*55930Sbostic PRINT_INODE(ifp->if_daddr == *daddrp, ip); 41255856Sbostic if (ifp->if_daddr == *daddrp) { 413*55930Sbostic bp++; 41455856Sbostic ++(*countp); 41555491Sbostic } 41655491Sbostic } 41755491Sbostic } 41855491Sbostic 41955491Sbostic /* 42055856Sbostic * Checks the summary checksum and the data checksum to determine if the 42155856Sbostic * segment is valid or not. Returns the size of the partial segment if it 42255856Sbostic * is valid, * and 0 otherwise. Use dump_summary to figure out size of the 42355856Sbostic * the partial as well as whether or not the checksum is valid. 42455491Sbostic */ 42555491Sbostic int 42655491Sbostic pseg_valid (fsp, ssp) 42755491Sbostic FS_INFO *fsp; /* pointer to file system info */ 42855856Sbostic SEGSUM *ssp; /* pointer to segment summary block */ 42955491Sbostic { 43055491Sbostic caddr_t p; 43155856Sbostic int i, nblocks; 43255856Sbostic u_long *datap; 43355491Sbostic 43455856Sbostic if ((nblocks = dump_summary(&fsp->fi_lfs, ssp, 0, NULL)) <= 0) 43555856Sbostic return(0); 43655491Sbostic 43755856Sbostic /* check data/inode block(s) checksum too */ 43855856Sbostic datap = (u_long *)malloc(nblocks * sizeof(u_long)); 43955491Sbostic p = (caddr_t)ssp + LFS_SUMMARY_SIZE; 44055856Sbostic for (i = 0; i < nblocks; ++i) { 44155491Sbostic datap[i] = *((u_long *)p); 44255856Sbostic p += fsp->fi_lfs.lfs_bsize; 44355491Sbostic } 44455856Sbostic if (cksum ((void *)datap, nblocks * sizeof(u_long)) != ssp->ss_datasum) 44555856Sbostic return (0); 44655491Sbostic 44755856Sbostic return (nblocks); 44855491Sbostic } 44955491Sbostic 45055491Sbostic 45155491Sbostic /* #define MMAP_SEGMENT */ 45255491Sbostic /* 45355491Sbostic * read a segment into a memory buffer 45455491Sbostic */ 45555491Sbostic int 45655856Sbostic mmap_segment (fsp, segment, segbuf) 45755856Sbostic FS_INFO *fsp; /* file system information */ 45855856Sbostic int segment; /* segment number */ 45955856Sbostic caddr_t *segbuf; /* pointer to buffer area */ 46055491Sbostic { 46155856Sbostic struct lfs *lfsp; 46255856Sbostic int fid; /* fildes for file system device */ 46355856Sbostic daddr_t seg_daddr; /* base disk address of segment */ 46455856Sbostic off_t seg_byte; 46555856Sbostic size_t ssize; 46655856Sbostic char mntfromname[MNAMELEN+2]; 46755491Sbostic 46855856Sbostic lfsp = &fsp->fi_lfs; 46955856Sbostic 47055491Sbostic /* get the disk address of the beginning of the segment */ 47155491Sbostic seg_daddr = sntoda(lfsp, segment); 47255856Sbostic seg_byte = datobyte(fsp, seg_daddr); 47355856Sbostic ssize = seg_size(lfsp); 47455491Sbostic 47555491Sbostic strcpy(mntfromname, "/dev/r"); 47655856Sbostic strcat(mntfromname, fsp->fi_statfsp->f_mntfromname+5); 47755491Sbostic 47855491Sbostic if ((fid = open(mntfromname, O_RDONLY, (mode_t)0)) < 0) { 47955856Sbostic err(0, "mmap_segment: bad open"); 48055856Sbostic return (-1); 48155491Sbostic } 48255491Sbostic 48355491Sbostic #ifdef MMAP_SEGMENT 48455856Sbostic *segbuf = mmap ((caddr_t)0, seg_size(lfsp), PROT_READ, 48555856Sbostic MAP_FILE, fid, seg_byte); 48655856Sbostic if (*(long *)segbuf < 0) { 48755856Sbostic err(0, "mmap_segment: mmap failed"); 48855856Sbostic return (NULL); 48955491Sbostic } 49055491Sbostic #else /* MMAP_SEGMENT */ 49155856Sbostic #ifdef VERBOSE 49255856Sbostic printf("mmap_segment\tseg_daddr: %lu\tseg_size: %lu\tseg_offset: %qu\n", 49355856Sbostic seg_daddr, ssize, seg_byte); 49455856Sbostic #endif 49555491Sbostic /* malloc the space for the buffer */ 49655856Sbostic *segbuf = malloc(ssize); 49755856Sbostic if (!*segbuf) { 49855856Sbostic err(0, "mmap_segment: malloc failed"); 49955856Sbostic return(NULL); 50055856Sbostic } 50155491Sbostic 50255491Sbostic /* read the segment data into the buffer */ 50355856Sbostic if (lseek (fid, seg_byte, SEEK_SET) != seg_byte) { 50455856Sbostic err (0, "mmap_segment: bad lseek"); 50555856Sbostic free(*segbuf); 50655856Sbostic return (-1); 50755491Sbostic } 50855491Sbostic 50955856Sbostic if (read (fid, *segbuf, ssize) != ssize) { 51055856Sbostic err (0, "mmap_segment: bad read"); 51155856Sbostic free(*segbuf); 51255856Sbostic return (-1); 51355491Sbostic } 51455491Sbostic #endif /* MMAP_SEGMENT */ 51555491Sbostic close (fid); 51655491Sbostic 51755856Sbostic return (0); 51855491Sbostic } 51955491Sbostic 52055491Sbostic void 52155491Sbostic munmap_segment (fsp, seg_buf) 52255856Sbostic FS_INFO *fsp; /* file system information */ 52355856Sbostic caddr_t seg_buf; /* pointer to buffer area */ 52455491Sbostic { 52555491Sbostic #ifdef MMAP_SEGMENT 52655856Sbostic munmap (seg_buf, seg_size(&fsp->fi_lfs)); 52755491Sbostic #else /* MMAP_SEGMENT */ 52855491Sbostic free (seg_buf); 52955491Sbostic #endif /* MMAP_SEGMENT */ 53055491Sbostic } 53155491Sbostic 53255491Sbostic 53355491Sbostic /* 53455491Sbostic * USEFUL DEBUGGING TOOLS: 53555491Sbostic */ 53655491Sbostic void 53755856Sbostic print_SEGSUM (lfsp, p) 53855856Sbostic struct lfs *lfsp; 53955856Sbostic SEGSUM *p; 54055491Sbostic { 54155856Sbostic if (p) 54255856Sbostic (void) dump_summary(lfsp, p, DUMP_ALL, NULL); 54355491Sbostic else printf("0x0"); 54455491Sbostic fflush(stdout); 54555491Sbostic } 54655491Sbostic 54755856Sbostic int 54855856Sbostic bi_compare(a, b) 54955856Sbostic const void *a; 55055856Sbostic const void *b; 55155491Sbostic { 55255856Sbostic const BLOCK_INFO *ba, *bb; 55355856Sbostic int diff; 55455491Sbostic 55555856Sbostic ba = a; 55655856Sbostic bb = b; 55755491Sbostic 55855856Sbostic if (diff = (int)(ba->bi_inode - bb->bi_inode)) 55955856Sbostic return (diff); 560*55930Sbostic if (diff = (int)(ba->bi_lbn - bb->bi_lbn)) { 561*55930Sbostic if (ba->bi_lbn == LFS_UNUSED_LBN) 562*55930Sbostic return(-1); 563*55930Sbostic else if (bb->bi_lbn == LFS_UNUSED_LBN) 564*55930Sbostic return(1); 565*55930Sbostic else if (ba->bi_lbn < 0) 566*55930Sbostic return(1); 567*55930Sbostic else 568*55930Sbostic return (diff); 569*55930Sbostic } 57055856Sbostic if (diff = (int)(ba->bi_segcreate - bb->bi_segcreate)) 57155856Sbostic return (diff); 57255856Sbostic diff = (int)(ba->bi_daddr - bb->bi_daddr); 57355856Sbostic return (diff); 57455856Sbostic } 57555491Sbostic 57655856Sbostic int 57755856Sbostic bi_toss(dummy, a, b) 57855856Sbostic const void *dummy; 57955856Sbostic const void *a; 58055856Sbostic const void *b; 58155491Sbostic { 58255856Sbostic const BLOCK_INFO *ba, *bb; 58355491Sbostic 58455856Sbostic ba = a; 58555856Sbostic bb = b; 58655856Sbostic 58755856Sbostic return(ba->bi_inode == bb->bi_inode && ba->bi_lbn == bb->bi_lbn); 58855491Sbostic } 58955491Sbostic 59055491Sbostic void 59155856Sbostic toss(p, nump, size, dotoss, client) 59255856Sbostic void *p; 59355856Sbostic int *nump; 59455856Sbostic size_t size; 59555856Sbostic int (*dotoss) __P((const void *, const void *, const void *)); 59655856Sbostic void *client; 59755491Sbostic { 59855856Sbostic int i; 59955856Sbostic void *p1; 60055491Sbostic 60155856Sbostic if (*nump == 0) 60255856Sbostic return; 60355491Sbostic 60455856Sbostic for (i = *nump; --i > 0;) { 60555856Sbostic p1 = p + size; 60655856Sbostic if (dotoss(client, p, p1)) { 60755856Sbostic bcopy(p1, p, i * size); 60855856Sbostic --(*nump); 60955856Sbostic } else 61055856Sbostic p += size; 61155491Sbostic } 61255491Sbostic } 613