bcache.c revision 1.1.4.2 1 1.1.4.2 elad /* $NetBSD: bcache.c,v 1.1.4.2 2006/04/19 02:33:05 elad Exp $ */
2 1.1.4.2 elad
3 1.1.4.2 elad /*-
4 1.1.4.2 elad * Copyright (c) 1998 Michael Smith <msmith (at) freebsd.org>
5 1.1.4.2 elad * All rights reserved.
6 1.1.4.2 elad *
7 1.1.4.2 elad * Redistribution and use in source and binary forms, with or without
8 1.1.4.2 elad * modification, are permitted provided that the following conditions
9 1.1.4.2 elad * are met:
10 1.1.4.2 elad * 1. Redistributions of source code must retain the above copyright
11 1.1.4.2 elad * notice, this list of conditions and the following disclaimer.
12 1.1.4.2 elad * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.4.2 elad * notice, this list of conditions and the following disclaimer in the
14 1.1.4.2 elad * documentation and/or other materials provided with the distribution.
15 1.1.4.2 elad *
16 1.1.4.2 elad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1.4.2 elad * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1.4.2 elad * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1.4.2 elad * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1.4.2 elad * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1.4.2 elad * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1.4.2 elad * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1.4.2 elad * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1.4.2 elad * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1.4.2 elad * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1.4.2 elad * SUCH DAMAGE.
27 1.1.4.2 elad */
28 1.1.4.2 elad
29 1.1.4.2 elad #include <sys/cdefs.h>
30 1.1.4.2 elad
31 1.1.4.2 elad /*
32 1.1.4.2 elad * Simple LRU block cache
33 1.1.4.2 elad */
34 1.1.4.2 elad
35 1.1.4.2 elad #include <sys/stdint.h>
36 1.1.4.2 elad
37 1.1.4.2 elad #include <lib/libsa/stand.h>
38 1.1.4.2 elad
39 1.1.4.2 elad #include "bitstring.h" /* XXX: Brought this in from FreeBSD:sys/sys/. Do we have an equivalent in NetBSD ? */
40 1.1.4.2 elad #include "bootstrap.h"
41 1.1.4.2 elad
42 1.1.4.2 elad /* #define BCACHE_DEBUG */
43 1.1.4.2 elad
44 1.1.4.2 elad #ifdef BCACHE_DEBUG
45 1.1.4.2 elad #define BCACHE_TIMEOUT 10
46 1.1.4.2 elad # define DEBUG(fmt, args...) printf("%s: " fmt "\n" , __func__ , ## args)
47 1.1.4.2 elad #else
48 1.1.4.2 elad #define BCACHE_TIMEOUT 2
49 1.1.4.2 elad # define DEBUG(fmt, args...)
50 1.1.4.2 elad #endif
51 1.1.4.2 elad
52 1.1.4.2 elad
53 1.1.4.2 elad struct bcachectl
54 1.1.4.2 elad {
55 1.1.4.2 elad daddr_t bc_blkno;
56 1.1.4.2 elad time_t bc_stamp;
57 1.1.4.2 elad int bc_count;
58 1.1.4.2 elad };
59 1.1.4.2 elad
60 1.1.4.2 elad static struct bcachectl *bcache_ctl;
61 1.1.4.2 elad static caddr_t bcache_data;
62 1.1.4.2 elad static bitstr_t *bcache_miss;
63 1.1.4.2 elad static u_int bcache_nblks;
64 1.1.4.2 elad static u_int bcache_blksize;
65 1.1.4.2 elad static u_int bcache_hits, bcache_misses, bcache_ops, bcache_bypasses;
66 1.1.4.2 elad static u_int bcache_flushes;
67 1.1.4.2 elad static u_int bcache_bcount;
68 1.1.4.2 elad
69 1.1.4.2 elad static void bcache_invalidate(daddr_t blkno);
70 1.1.4.2 elad static void bcache_insert(caddr_t buf, daddr_t blkno);
71 1.1.4.2 elad static int bcache_lookup(caddr_t buf, daddr_t blkno);
72 1.1.4.2 elad
73 1.1.4.2 elad /*
74 1.1.4.2 elad * Initialise the cache for (nblks) of (bsize).
75 1.1.4.2 elad */
76 1.1.4.2 elad int
77 1.1.4.2 elad bcache_init(u_int nblks, size_t bsize)
78 1.1.4.2 elad {
79 1.1.4.2 elad /* discard any old contents */
80 1.1.4.2 elad if (bcache_data != NULL) {
81 1.1.4.2 elad free(bcache_data);
82 1.1.4.2 elad bcache_data = NULL;
83 1.1.4.2 elad free(bcache_ctl);
84 1.1.4.2 elad }
85 1.1.4.2 elad
86 1.1.4.2 elad /* Allocate control structures */
87 1.1.4.2 elad bcache_nblks = nblks;
88 1.1.4.2 elad bcache_blksize = bsize;
89 1.1.4.2 elad bcache_data = alloc(bcache_nblks * bcache_blksize);
90 1.1.4.2 elad bcache_ctl = (struct bcachectl *)alloc(bcache_nblks * sizeof(struct bcachectl));
91 1.1.4.2 elad bcache_miss = bit_alloc((bcache_nblks + 1) / 2);
92 1.1.4.2 elad if ((bcache_data == NULL) || (bcache_ctl == NULL) || (bcache_miss == NULL)) {
93 1.1.4.2 elad if (bcache_miss)
94 1.1.4.2 elad free(bcache_miss);
95 1.1.4.2 elad if (bcache_ctl)
96 1.1.4.2 elad free(bcache_ctl);
97 1.1.4.2 elad if (bcache_data)
98 1.1.4.2 elad free(bcache_data);
99 1.1.4.2 elad bcache_data = NULL;
100 1.1.4.2 elad return(ENOMEM);
101 1.1.4.2 elad }
102 1.1.4.2 elad
103 1.1.4.2 elad return(0);
104 1.1.4.2 elad }
105 1.1.4.2 elad
106 1.1.4.2 elad /*
107 1.1.4.2 elad * Flush the cache
108 1.1.4.2 elad */
109 1.1.4.2 elad void
110 1.1.4.2 elad bcache_flush(void)
111 1.1.4.2 elad {
112 1.1.4.2 elad u_int i;
113 1.1.4.2 elad
114 1.1.4.2 elad bcache_flushes++;
115 1.1.4.2 elad
116 1.1.4.2 elad /* Flush the cache */
117 1.1.4.2 elad for (i = 0; i < bcache_nblks; i++) {
118 1.1.4.2 elad bcache_ctl[i].bc_count = -1;
119 1.1.4.2 elad bcache_ctl[i].bc_blkno = -1;
120 1.1.4.2 elad }
121 1.1.4.2 elad }
122 1.1.4.2 elad
123 1.1.4.2 elad /*
124 1.1.4.2 elad * Handle a write request; write directly to the disk, and populate the
125 1.1.4.2 elad * cache with the new values.
126 1.1.4.2 elad */
127 1.1.4.2 elad static int
128 1.1.4.2 elad write_strategy(void *devdata, int unit, int rw, daddr_t blk, size_t size,
129 1.1.4.2 elad char *buf, size_t *rsize)
130 1.1.4.2 elad {
131 1.1.4.2 elad struct bcache_devdata *dd = (struct bcache_devdata *)devdata;
132 1.1.4.2 elad daddr_t i, nblk;
133 1.1.4.2 elad int err;
134 1.1.4.2 elad
135 1.1.4.2 elad nblk = size / bcache_blksize;
136 1.1.4.2 elad
137 1.1.4.2 elad /* Invalidate the blocks being written */
138 1.1.4.2 elad for (i = 0; i < nblk; i++) {
139 1.1.4.2 elad bcache_invalidate(blk + i);
140 1.1.4.2 elad }
141 1.1.4.2 elad
142 1.1.4.2 elad /* Write the blocks */
143 1.1.4.2 elad err = dd->dv_strategy(dd->dv_devdata, rw, blk, size, buf, rsize);
144 1.1.4.2 elad
145 1.1.4.2 elad /* Populate the block cache with the new data */
146 1.1.4.2 elad if (err == 0) {
147 1.1.4.2 elad for (i = 0; i < nblk; i++) {
148 1.1.4.2 elad bcache_insert(buf + (i * bcache_blksize),blk + i);
149 1.1.4.2 elad }
150 1.1.4.2 elad }
151 1.1.4.2 elad
152 1.1.4.2 elad return err;
153 1.1.4.2 elad }
154 1.1.4.2 elad
155 1.1.4.2 elad /*
156 1.1.4.2 elad * Handle a read request; fill in parts of the request that can
157 1.1.4.2 elad * be satisfied by the cache, use the supplied strategy routine to do
158 1.1.4.2 elad * device I/O and then use the I/O results to populate the cache.
159 1.1.4.2 elad */
160 1.1.4.2 elad static int
161 1.1.4.2 elad read_strategy(void *devdata, int unit, int rw, daddr_t blk, size_t size,
162 1.1.4.2 elad char *buf, size_t *rsize)
163 1.1.4.2 elad {
164 1.1.4.2 elad struct bcache_devdata *dd = (struct bcache_devdata *)devdata;
165 1.1.4.2 elad int p_size, result;
166 1.1.4.2 elad daddr_t p_blk, i, j, nblk;
167 1.1.4.2 elad caddr_t p_buf;
168 1.1.4.2 elad
169 1.1.4.2 elad nblk = size / bcache_blksize;
170 1.1.4.2 elad result = 0;
171 1.1.4.2 elad
172 1.1.4.2 elad /* Satisfy any cache hits up front */
173 1.1.4.2 elad for (i = 0; i < nblk; i++) {
174 1.1.4.2 elad if (bcache_lookup(buf + (bcache_blksize * i), blk + i)) {
175 1.1.4.2 elad bit_set(bcache_miss, i); /* cache miss */
176 1.1.4.2 elad bcache_misses++;
177 1.1.4.2 elad } else {
178 1.1.4.2 elad bit_clear(bcache_miss, i); /* cache hit */
179 1.1.4.2 elad bcache_hits++;
180 1.1.4.2 elad }
181 1.1.4.2 elad }
182 1.1.4.2 elad
183 1.1.4.2 elad /* Go back and fill in any misses XXX optimise */
184 1.1.4.2 elad p_blk = -1;
185 1.1.4.2 elad p_buf = NULL;
186 1.1.4.2 elad p_size = 0;
187 1.1.4.2 elad for (i = 0; i < nblk; i++) {
188 1.1.4.2 elad if (bit_test(bcache_miss, i)) {
189 1.1.4.2 elad /* miss, add to pending transfer */
190 1.1.4.2 elad if (p_blk == -1) {
191 1.1.4.2 elad p_blk = blk + i;
192 1.1.4.2 elad p_buf = buf + (bcache_blksize * i);
193 1.1.4.2 elad p_size = 1;
194 1.1.4.2 elad } else {
195 1.1.4.2 elad p_size++;
196 1.1.4.2 elad }
197 1.1.4.2 elad } else if (p_blk != -1) {
198 1.1.4.2 elad /* hit, complete pending transfer */
199 1.1.4.2 elad result = dd->dv_strategy(dd->dv_devdata, rw, p_blk, p_size * bcache_blksize, p_buf, NULL);
200 1.1.4.2 elad if (result != 0)
201 1.1.4.2 elad goto done;
202 1.1.4.2 elad for (j = 0; j < p_size; j++)
203 1.1.4.2 elad bcache_insert(p_buf + (j * bcache_blksize), p_blk + j);
204 1.1.4.2 elad p_blk = -1;
205 1.1.4.2 elad }
206 1.1.4.2 elad }
207 1.1.4.2 elad if (p_blk != -1) {
208 1.1.4.2 elad /* pending transfer left */
209 1.1.4.2 elad result = dd->dv_strategy(dd->dv_devdata, rw, p_blk, p_size * bcache_blksize, p_buf, NULL);
210 1.1.4.2 elad if (result != 0)
211 1.1.4.2 elad goto done;
212 1.1.4.2 elad for (j = 0; j < p_size; j++)
213 1.1.4.2 elad bcache_insert(p_buf + (j * bcache_blksize), p_blk + j);
214 1.1.4.2 elad }
215 1.1.4.2 elad
216 1.1.4.2 elad done:
217 1.1.4.2 elad if ((result == 0) && (rsize != NULL))
218 1.1.4.2 elad *rsize = size;
219 1.1.4.2 elad return(result);
220 1.1.4.2 elad }
221 1.1.4.2 elad
222 1.1.4.2 elad /*
223 1.1.4.2 elad * Requests larger than 1/2 the cache size will be bypassed and go
224 1.1.4.2 elad * directly to the disk. XXX tune this.
225 1.1.4.2 elad */
226 1.1.4.2 elad int
227 1.1.4.2 elad bcache_strategy(void *devdata, int unit, int rw, daddr_t blk, size_t size,
228 1.1.4.2 elad char *buf, size_t *rsize)
229 1.1.4.2 elad {
230 1.1.4.2 elad static int bcache_unit = -1;
231 1.1.4.2 elad struct bcache_devdata *dd = (struct bcache_devdata *)devdata;
232 1.1.4.2 elad
233 1.1.4.2 elad bcache_ops++;
234 1.1.4.2 elad
235 1.1.4.2 elad if(bcache_unit != unit) {
236 1.1.4.2 elad bcache_flush();
237 1.1.4.2 elad bcache_unit = unit;
238 1.1.4.2 elad }
239 1.1.4.2 elad
240 1.1.4.2 elad /* bypass large requests, or when the cache is inactive */
241 1.1.4.2 elad if ((bcache_data == NULL) || ((size * 2 / bcache_blksize) > bcache_nblks)) {
242 1.1.4.2 elad DEBUG("bypass %d from %d", size / bcache_blksize, blk);
243 1.1.4.2 elad bcache_bypasses++;
244 1.1.4.2 elad return(dd->dv_strategy(dd->dv_devdata, rw, blk, size, buf, rsize));
245 1.1.4.2 elad }
246 1.1.4.2 elad
247 1.1.4.2 elad switch (rw) {
248 1.1.4.2 elad case F_READ:
249 1.1.4.2 elad return read_strategy(devdata, unit, rw, blk, size, buf, rsize);
250 1.1.4.2 elad case F_WRITE:
251 1.1.4.2 elad return write_strategy(devdata, unit, rw, blk, size, buf, rsize);
252 1.1.4.2 elad }
253 1.1.4.2 elad return -1;
254 1.1.4.2 elad }
255 1.1.4.2 elad
256 1.1.4.2 elad
257 1.1.4.2 elad /*
258 1.1.4.2 elad * Insert a block into the cache. Retire the oldest block to do so, if required.
259 1.1.4.2 elad *
260 1.1.4.2 elad * XXX the LRU algorithm will fail after 2^31 blocks have been transferred.
261 1.1.4.2 elad */
262 1.1.4.2 elad static void
263 1.1.4.2 elad bcache_insert(caddr_t buf, daddr_t blkno)
264 1.1.4.2 elad {
265 1.1.4.2 elad time_t now;
266 1.1.4.2 elad int cand, ocount;
267 1.1.4.2 elad u_int i;
268 1.1.4.2 elad
269 1.1.4.2 elad time(&now);
270 1.1.4.2 elad cand = 0; /* assume the first block */
271 1.1.4.2 elad ocount = bcache_ctl[0].bc_count;
272 1.1.4.2 elad
273 1.1.4.2 elad /* find the oldest block */
274 1.1.4.2 elad for (i = 1; i < bcache_nblks; i++) {
275 1.1.4.2 elad if (bcache_ctl[i].bc_blkno == blkno) {
276 1.1.4.2 elad /* reuse old entry */
277 1.1.4.2 elad cand = i;
278 1.1.4.2 elad break;
279 1.1.4.2 elad }
280 1.1.4.2 elad if (bcache_ctl[i].bc_count < ocount) {
281 1.1.4.2 elad ocount = bcache_ctl[i].bc_count;
282 1.1.4.2 elad cand = i;
283 1.1.4.2 elad }
284 1.1.4.2 elad }
285 1.1.4.2 elad
286 1.1.4.2 elad DEBUG("insert blk %d -> %d @ %d # %d", blkno, cand, now, bcache_bcount);
287 1.1.4.2 elad bcopy(buf, bcache_data + (bcache_blksize * cand), bcache_blksize);
288 1.1.4.2 elad bcache_ctl[cand].bc_blkno = blkno;
289 1.1.4.2 elad bcache_ctl[cand].bc_stamp = now;
290 1.1.4.2 elad bcache_ctl[cand].bc_count = bcache_bcount++;
291 1.1.4.2 elad }
292 1.1.4.2 elad
293 1.1.4.2 elad /*
294 1.1.4.2 elad * Look for a block in the cache. Blocks more than BCACHE_TIMEOUT seconds old
295 1.1.4.2 elad * may be stale (removable media) and thus are discarded. Copy the block out
296 1.1.4.2 elad * if successful and return zero, or return nonzero on failure.
297 1.1.4.2 elad */
298 1.1.4.2 elad static int
299 1.1.4.2 elad bcache_lookup(caddr_t buf, daddr_t blkno)
300 1.1.4.2 elad {
301 1.1.4.2 elad time_t now;
302 1.1.4.2 elad u_int i;
303 1.1.4.2 elad
304 1.1.4.2 elad time(&now);
305 1.1.4.2 elad
306 1.1.4.2 elad for (i = 0; i < bcache_nblks; i++)
307 1.1.4.2 elad /* cache hit? */
308 1.1.4.2 elad if ((bcache_ctl[i].bc_blkno == blkno) && ((bcache_ctl[i].bc_stamp + BCACHE_TIMEOUT) >= now)) {
309 1.1.4.2 elad bcopy(bcache_data + (bcache_blksize * i), buf, bcache_blksize);
310 1.1.4.2 elad DEBUG("hit blk %d <- %d (now %d then %d)", blkno, i, now, bcache_ctl[i].bc_stamp);
311 1.1.4.2 elad return(0);
312 1.1.4.2 elad }
313 1.1.4.2 elad return(ENOENT);
314 1.1.4.2 elad }
315 1.1.4.2 elad
316 1.1.4.2 elad /*
317 1.1.4.2 elad * Invalidate a block from the cache.
318 1.1.4.2 elad */
319 1.1.4.2 elad static void
320 1.1.4.2 elad bcache_invalidate(daddr_t blkno)
321 1.1.4.2 elad {
322 1.1.4.2 elad u_int i;
323 1.1.4.2 elad
324 1.1.4.2 elad for (i = 0; i < bcache_nblks; i++) {
325 1.1.4.2 elad if (bcache_ctl[i].bc_blkno == blkno) {
326 1.1.4.2 elad bcache_ctl[i].bc_count = -1;
327 1.1.4.2 elad bcache_ctl[i].bc_blkno = -1;
328 1.1.4.2 elad DEBUG("invalidate blk %d", blkno);
329 1.1.4.2 elad break;
330 1.1.4.2 elad }
331 1.1.4.2 elad }
332 1.1.4.2 elad }
333 1.1.4.2 elad
334 1.1.4.2 elad int
335 1.1.4.2 elad command_bcache(int argc, char *argv[])
336 1.1.4.2 elad {
337 1.1.4.2 elad u_int i;
338 1.1.4.2 elad
339 1.1.4.2 elad for (i = 0; i < bcache_nblks; i++) {
340 1.1.4.2 elad printf("%lx %x %x|", (uintmax_t)bcache_ctl[i].bc_blkno, (unsigned int)bcache_ctl[i].bc_stamp & 0xffff, bcache_ctl[i].bc_count & 0xffff);
341 1.1.4.2 elad if (((i + 1) % 4) == 0)
342 1.1.4.2 elad printf("\n");
343 1.1.4.2 elad }
344 1.1.4.2 elad printf("\n%d ops %d bypasses %d hits %d misses %d flushes\n", bcache_ops, bcache_bypasses, bcache_hits, bcache_misses, bcache_flushes);
345 1.1.4.2 elad return(CMD_OK);
346 1.1.4.2 elad }
347 1.1.4.2 elad
348