blob: 8ac5a49170cfc2691176d9e6601addd604bc5794 [file] [log] [blame]
Travis Geiselbrechtb29c3ba2010-05-06 14:08:46 -07001/*
2 * Copyright (c) 2007 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#include <list.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/types.h>
27#include <debug.h>
28#include <lib/bcache.h>
29#include <lib/bio.h>
30
31#define LOCAL_TRACE 0
32
33struct bcache_block {
34 struct list_node node;
35 bnum_t blocknum;
36 int ref_count;
37 bool is_dirty;
38 void *ptr;
39};
40
41struct bcache_stats {
42 uint32_t hits;
43 uint32_t depth;
44 uint32_t misses;
45 uint32_t reads;
46 uint32_t writes;
47};
48
49struct bcache {
50 bdev_t *dev;
51 size_t block_size;
52 int count;
53 struct bcache_stats stats;
54
55 struct list_node free_list;
56 struct list_node lru_list;
57
58 struct bcache_block *blocks;
59};
60
61bcache_t bcache_create(bdev_t *dev, size_t block_size, int block_count)
62{
63 struct bcache *cache;
64
65 cache = malloc(sizeof(struct bcache));
66
67 cache->dev = dev;
68 cache->block_size = block_size;
69 cache->count = block_count;
70 memset(&cache->stats, 0, sizeof(cache->stats));
71
72 list_initialize(&cache->free_list);
73 list_initialize(&cache->lru_list);
74
75 cache->blocks = malloc(sizeof(struct bcache_block) * block_count);
76 int i;
77 for (i=0; i < block_count; i++) {
78 cache->blocks[i].ref_count = 0;
79 cache->blocks[i].is_dirty = false;
80 cache->blocks[i].ptr = malloc(block_size);
81 // add to the free list
82 list_add_head(&cache->free_list, &cache->blocks[i].node);
83 }
84
85 return (bcache_t)cache;
86}
87
88static int flush_block(struct bcache *cache, struct bcache_block *block)
89{
90 int rc;
91
92 rc = bio_write(cache->dev, block->ptr,
93 (off_t)block->blocknum * cache->block_size,
94 cache->block_size);
95 if (rc < 0)
96 goto exit;
97
98 block->is_dirty = false;
99 cache->stats.writes++;
100 rc = 0;
101exit:
102 return (rc);
103}
104
105void bcache_destroy(bcache_t _cache)
106{
107 struct bcache *cache = _cache;
108 int i;
109
110 for (i=0; i < cache->count; i++) {
111 DEBUG_ASSERT(cache->blocks[i].ref_count == 0);
112
113 if (cache->blocks[i].is_dirty)
114 printf("warning: freeing dirty block %u\n",
115 cache->blocks[i].blocknum);
116
117 free(cache->blocks[i].ptr);
118 }
119
120 free(cache);
121}
122
123/* find a block if it's already present */
124static struct bcache_block *find_block(struct bcache *cache, uint blocknum)
125{
126 uint32_t depth = 0;
127 struct bcache_block *block;
128
129 LTRACEF("num %u\n", blocknum);
130
131 block = NULL;
132 list_for_every_entry(&cache->lru_list, block, struct bcache_block, node) {
133 LTRACEF("looking at entry %p, num %u\n", block, block->blocknum);
134 depth++;
135
136 if (block->blocknum == blocknum) {
137 list_delete(&block->node);
138 list_add_tail(&cache->lru_list, &block->node);
139 cache->stats.hits++;
140 cache->stats.depth += depth;
141 return block;
142 }
143 }
144
145 cache->stats.misses++;
146 return NULL;
147}
148
149/* allocate a new block */
150static struct bcache_block *alloc_block(struct bcache *cache)
151{
152 int err;
153 struct bcache_block *block;
154
155 /* pop one off the free list if it's present */
156 block = list_remove_head_type(&cache->free_list, struct bcache_block, node);
157 if (block) {
158 block->ref_count = 0;
159 list_add_tail(&cache->lru_list, &block->node);
160 LTRACEF("found block %p on free list\n", block);
161 return block;
162 }
163
164 /* walk the lru, looking for a free block */
165 list_for_every_entry(&cache->lru_list, block, struct bcache_block, node) {
166 LTRACEF("looking at %p, num %u\n", block, block->blocknum);
167 if (block->ref_count == 0) {
168 if (block->is_dirty) {
169 err = flush_block(cache, block);
170 if (err)
171 return NULL;
172 }
173
174 // add it to the tail of the lru
175 list_delete(&block->node);
176 list_add_tail(&cache->lru_list, &block->node);
177 return block;
178 }
179 }
180
181 return NULL;
182}
183
184static struct bcache_block *find_or_fill_block(struct bcache *cache, uint blocknum)
185{
186 int err;
187
188 LTRACEF("block %u\n", blocknum);
189
190 /* see if it's already in the cache */
191 struct bcache_block *block = find_block(cache, blocknum);
192 if (block == NULL) {
193 LTRACEF("wasn't allocated\n");
194
195 /* allocate a new block and fill it */
196 block = alloc_block(cache);
197 DEBUG_ASSERT(block);
198
199 LTRACEF("wasn't allocated, new block %p\n", block);
200
201 block->blocknum = blocknum;
202 err = bio_read(cache->dev, block->ptr, (off_t)blocknum * cache->block_size, cache->block_size);
203 if (err < 0) {
204 /* free the block, return an error */
205 list_add_tail(&cache->free_list, &block->node);
206 return NULL;
207 }
208
209 cache->stats.reads++;
210 }
211
212 DEBUG_ASSERT(block->blocknum == blocknum);
213
214 return block;
215}
216
217int bcache_read_block(bcache_t _cache, void *buf, uint blocknum)
218{
219 struct bcache *cache = _cache;
220
221 LTRACEF("buf %p, blocknum %u\n", buf, blocknum);
222
223 struct bcache_block *block = find_or_fill_block(cache, blocknum);
224 if (block == NULL) {
225 /* error */
226 return -1;
227 }
228
229 memcpy(buf, block->ptr, cache->block_size);
230 return 0;
231}
232
233int bcache_get_block(bcache_t _cache, void **ptr, uint blocknum)
234{
235 struct bcache *cache = _cache;
236
237 LTRACEF("ptr %p, blocknum %u\n", ptr, blocknum);
238
239 DEBUG_ASSERT(ptr);
240
241 struct bcache_block *block = find_or_fill_block(cache, blocknum);
242 if (block == NULL) {
243 /* error */
244 return -1;
245 }
246
247 /* increment the ref count to keep it from being freed */
248 block->ref_count++;
249 *ptr = block->ptr;
250
251 return 0;
252}
253
254int bcache_put_block(bcache_t _cache, uint blocknum)
255{
256 struct bcache *cache = _cache;
257
258 LTRACEF("blocknum %u\n", blocknum);
259
260 struct bcache_block *block = find_block(cache, blocknum);
261
262 /* be pretty hard on the caller for now */
263 DEBUG_ASSERT(block);
264 DEBUG_ASSERT(block->ref_count > 0);
265
266 block->ref_count--;
267
268 return 0;
269}
270
271int bcache_mark_block_dirty(bcache_t priv, uint blocknum)
272{
273 int err;
274 struct bcache *cache = priv;
275 struct bcache_block *block;
276
277 block = find_block(cache, blocknum);
278 if (!block) {
279 err = -1;
280 goto exit;
281 }
282
283 block->is_dirty = true;
284 err = 0;
285exit:
286 return (err);
287}
288
289int bcache_zero_block(bcache_t priv, uint blocknum)
290{
291 int err;
292 struct bcache *cache = priv;
293 struct bcache_block *block;
294
295 block = find_block(cache, blocknum);
296 if (!block) {
297 block = alloc_block(cache);
298 if (!block) {
299 err = -1;
300 goto exit;
301 }
302
303 block->blocknum = blocknum;
304 }
305
306 memset(block->ptr, 0, cache->block_size);
307 block->is_dirty = true;
308 err = 0;
309exit:
310 return (err);
311}
312
313int bcache_flush(bcache_t priv)
314{
315 int err;
316 struct bcache *cache = priv;
317 struct bcache_block *block;
318
319 list_for_every_entry(&cache->lru_list, block, struct bcache_block, node) {
320 if (block->is_dirty) {
321 err = flush_block(cache, block);
322 if (err)
323 goto exit;
324 }
325 }
326
327 err = 0;
328exit:
329 return (err);
330}
331
332void bcache_dump(bcache_t priv, const char *name)
333{
334 uint32_t finds;
335 struct bcache *cache = priv;
336
337 finds = cache->stats.hits + cache->stats.misses;
338
339 printf("%s: hits=%u(%u%%) depth=%u misses=%u(%u%%) reads=%u writes=%u\n",
340 name,
341 cache->stats.hits,
342 finds ? (cache->stats.hits * 100) / finds : 0,
343 cache->stats.hits ? cache->stats.depth / cache->stats.hits : 0,
344 cache->stats.misses,
345 finds ? (cache->stats.misses * 100) / finds : 0,
346 cache->stats.reads,
347 cache->stats.writes);
348}