1 /* 2 * Copyright (c) 2005 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Matthew Dillon <dillon@backplane.com> 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 3. Neither the name of The DragonFly Project nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific, prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * $FreeBSD: src/sbin/dump/cache.c,v 1.1.2.1 2003/01/25 18:54:59 dillon Exp $ 35 */ 36 /* 37 * Block cache for dump 38 */ 39 40 #include <sys/param.h> 41 #include <sys/stat.h> 42 #include <sys/mman.h> 43 44 #ifdef sunos 45 #include <sys/vnode.h> 46 47 #include <ufs/fs.h> 48 #include <ufs/fsdir.h> 49 #include <ufs/inode.h> 50 #else 51 #include <vfs/ufs/dir.h> 52 #include <vfs/ufs/dinode.h> 53 #include <vfs/ufs/fs.h> 54 #endif 55 56 #include <protocols/dumprestore.h> 57 58 #include <ctype.h> 59 #include <stdio.h> 60 #include <errno.h> 61 #include <string.h> 62 #include <stdlib.h> 63 #include <unistd.h> 64 65 #include "dump.h" 66 67 typedef struct Block { 68 struct Block *b_HNext; /* must be first field */ 69 off_t b_Offset; 70 char *b_Data; 71 } Block; 72 73 #define HFACTOR 4 74 #define BLKFACTOR 4 75 76 static char *DataBase; 77 static Block **BlockHash; 78 static int BlockSize; 79 static int HSize; 80 static int NBlocks; 81 82 static void 83 cinit(void) 84 { 85 int i; 86 int hi; 87 Block *base; 88 89 if ((BlockSize = sblock->fs_bsize * BLKFACTOR) > MAXBSIZE) 90 BlockSize = MAXBSIZE; 91 NBlocks = cachesize / BlockSize; 92 HSize = NBlocks / HFACTOR; 93 94 msg("Cache %d MB, blocksize = %d\n", 95 NBlocks * BlockSize / (1024 * 1024), BlockSize); 96 97 base = calloc(sizeof(Block), NBlocks); 98 BlockHash = calloc(sizeof(Block *), HSize); 99 DataBase = mmap(NULL, NBlocks * BlockSize, 100 PROT_READ|PROT_WRITE, MAP_ANON, -1, 0); 101 for (i = 0; i < NBlocks; ++i) { 102 base[i].b_Data = DataBase + i * BlockSize; 103 base[i].b_Offset = (off_t)-1; 104 hi = i / HFACTOR; 105 base[i].b_HNext = BlockHash[hi]; 106 BlockHash[hi] = &base[i]; 107 } 108 } 109 110 ssize_t 111 cread(int fd, void *buf, size_t nbytes, off_t offset) 112 { 113 Block *blk; 114 Block **pblk; 115 Block **ppblk; 116 int hi; 117 int n; 118 off_t mask; 119 120 /* 121 * If the cache is disabled, or we do not yet know the filesystem 122 * block size, then revert to pread. Otherwise initialize the 123 * cache as necessary and continue. 124 */ 125 if (cachesize <= 0 || sblock->fs_bsize == 0) 126 return(pread(fd, buf, nbytes, offset)); 127 if (DataBase == NULL) 128 cinit(); 129 130 /* 131 * If the request crosses a cache block boundary, or the 132 * request is larger or equal to the cache block size, 133 * revert to pread(). Full-block-reads are typically 134 * one-time calls and caching would be detrimental. 135 */ 136 mask = ~(off_t)(BlockSize - 1); 137 if (nbytes >= (unsigned)BlockSize || 138 ((offset ^ (offset + nbytes - 1)) & mask) != 0) { 139 return(pread(fd, buf, nbytes, offset)); 140 } 141 142 /* 143 * Obtain and access the cache block. Cache a successful 144 * result. If an error occurs, revert to pread() (this might 145 * occur near the end of the media). 146 */ 147 hi = (offset / BlockSize) % HSize; 148 pblk = &BlockHash[hi]; 149 ppblk = NULL; 150 while ((blk = *pblk) != NULL) { 151 if (((blk->b_Offset ^ offset) & mask) == 0) 152 break; 153 ppblk = pblk; 154 pblk = &blk->b_HNext; 155 } 156 if (blk == NULL) { 157 blk = *ppblk; 158 pblk = ppblk; 159 blk->b_Offset = offset & mask; 160 n = pread(fd, blk->b_Data, BlockSize, blk->b_Offset); 161 if (n != BlockSize) { 162 blk->b_Offset = (off_t)-1; 163 blk = NULL; 164 } 165 } 166 if (blk) { 167 bcopy(blk->b_Data + (offset - blk->b_Offset), buf, nbytes); 168 *pblk = blk->b_HNext; 169 blk->b_HNext = BlockHash[hi]; 170 BlockHash[hi] = blk; 171 return(nbytes); 172 } else { 173 return(pread(fd, buf, nbytes, offset)); 174 } 175 } 176 177