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