xref: /dragonfly/sbin/dump/cache.c (revision 6ca88057)
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 #include <vfs/ufs/dir.h>
45 #include <vfs/ufs/dinode.h>
46 #include <vfs/ufs/fs.h>
47 
48 #include <protocols/dumprestore.h>
49 
50 #include <ctype.h>
51 #include <stdio.h>
52 #include <errno.h>
53 #include <string.h>
54 #include <stdlib.h>
55 #include <unistd.h>
56 
57 #include "dump.h"
58 
59 typedef struct Block {
60 	struct Block	*b_HNext;	/* must be first field */
61 	off_t		b_Offset;
62 	char		*b_Data;
63 } Block;
64 
65 #define HFACTOR		4
66 #define BLKFACTOR	4
67 
68 static char  *DataBase;
69 static Block **BlockHash;
70 static int   BlockSize;
71 static int   HSize;
72 static int   NBlocks;
73 
74 static void
75 cinit(void)
76 {
77 	int i;
78 	int hi;
79 	Block *base;
80 
81 	if ((BlockSize = sblock->fs_bsize * BLKFACTOR) > MAXBSIZE)
82 		BlockSize = MAXBSIZE;
83 	NBlocks = cachesize / BlockSize;
84 	HSize = NBlocks / HFACTOR;
85 
86 	msg("Cache %d MB, blocksize = %d\n",
87 	    NBlocks * BlockSize / (1024 * 1024), BlockSize);
88 
89 	base = calloc(sizeof(Block), NBlocks);
90 	BlockHash = calloc(sizeof(Block *), HSize);
91 	DataBase = mmap(NULL, NBlocks * BlockSize,
92 			PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
93 	for (i = 0; i < NBlocks; ++i) {
94 		base[i].b_Data = DataBase + i * BlockSize;
95 		base[i].b_Offset = (off_t)-1;
96 		hi = i / HFACTOR;
97 		base[i].b_HNext = BlockHash[hi];
98 		BlockHash[hi] = &base[i];
99 	}
100 }
101 
102 ssize_t
103 cread(int fd, void *buf, size_t nbytes, off_t offset)
104 {
105 	Block *blk;
106 	Block **pblk;
107 	Block **ppblk;
108 	int hi;
109 	int n;
110 	off_t mask;
111 
112 	/*
113 	 * If the cache is disabled, or we do not yet know the filesystem
114 	 * block size, then revert to pread.  Otherwise initialize the
115 	 * cache as necessary and continue.
116 	 */
117 	if (cachesize <= 0 || sblock->fs_bsize == 0)
118 		return(pread(fd, buf, nbytes, offset));
119 	if (DataBase == NULL)
120 		cinit();
121 
122 	/*
123 	 * If the request crosses a cache block boundary, or the
124 	 * request is larger or equal to the cache block size,
125 	 * revert to pread().  Full-block-reads are typically
126 	 * one-time calls and caching would be detrimental.
127 	 */
128 	mask = ~(off_t)(BlockSize - 1);
129 	if (nbytes >= (unsigned)BlockSize ||
130 	    ((offset ^ (offset + nbytes - 1)) & mask) != 0) {
131 		return(pread(fd, buf, nbytes, offset));
132 	}
133 
134 	/*
135 	 * Obtain and access the cache block.  Cache a successful
136 	 * result.  If an error occurs, revert to pread() (this might
137 	 * occur near the end of the media).
138 	 */
139 	hi = (offset / BlockSize) % HSize;
140 	pblk = &BlockHash[hi];
141 	ppblk = NULL;
142 	while ((blk = *pblk) != NULL) {
143 		if (((blk->b_Offset ^ offset) & mask) == 0)
144 			break;
145 		ppblk = pblk;
146 		pblk = &blk->b_HNext;
147 	}
148 	if (blk == NULL) {
149 		blk = *ppblk;
150 		pblk = ppblk;
151 		blk->b_Offset = offset & mask;
152 		n = pread(fd, blk->b_Data, BlockSize, blk->b_Offset);
153 		if (n != BlockSize) {
154 			blk->b_Offset = (off_t)-1;
155 			blk = NULL;
156 		}
157 	}
158 	if (blk) {
159 		bcopy(blk->b_Data + (offset - blk->b_Offset), buf, nbytes);
160 		*pblk = blk->b_HNext;
161 		blk->b_HNext = BlockHash[hi];
162 		BlockHash[hi] = blk;
163 		return(nbytes);
164 	} else {
165 		return(pread(fd, buf, nbytes, offset));
166 	}
167 }
168 
169