2 * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /** @addtogroup lwext4
34 * @brief Block cache allocator.
37 #include "ext4_config.h"
38 #include "ext4_bcache.h"
39 #include "ext4_debug.h"
40 #include "ext4_errno.h"
45 int ext4_bcache_init_dynamic(struct ext4_bcache *bc, uint32_t cnt,
48 ext4_assert(bc && cnt && itemsize);
50 memset(bc, 0, sizeof(struct ext4_bcache));
52 bc->data = malloc(cnt * itemsize);
57 bc->itemsize = itemsize;
59 bc->max_ref_blocks = 0;
68 memset(bc, 0, sizeof(struct ext4_bcache));
73 int ext4_bcache_fini_dynamic(struct ext4_bcache *bc)
78 memset(bc, 0, sizeof(struct ext4_bcache));
83 int ext4_bcache_alloc(struct ext4_bcache *bc, struct ext4_block *b,
87 ext4_assert(bc && b && is_new);
90 ext4_assert(b->lb_id);
92 ext4_assert(b->lb_id);
95 uint32_t cache_id = bc->cnt;
96 uint32_t alloc_id = 0;
100 /*Find in free blocks (Last Recently Used).*/
101 for (i = 0; i < bc->cnt; ++i) {
103 /*Check if block is already in cache*/
104 if (b->lb_id == bc->lba[i]) {
106 if (!bc->refctr[i] && !bc->free_delay[i])
109 /*Update reference counter*/
112 /*Update usage marker*/
113 bc->lru_id[i] = ++bc->lru_ctr;
115 /*Set valid cache data and id*/
116 b->data = bc->data + i * bc->itemsize;
119 /* If data in the caxhe is up-to-date */
120 b->uptodate = ext4_bcache_test_flag(bc, i, BC_UPTODATE);
125 /*Best fit calculations.*/
129 if (bc->free_delay[i])
132 /*Block is unreferenced, but it may exist block with
133 * lower usage marker*/
136 if (cache_id == bc->cnt) {
138 alloc_id = bc->lru_id[i];
143 if (alloc_id <= bc->lru_id[i])
146 /*This block has lower alloc id marker*/
148 alloc_id = bc->lru_id[i];
151 if (cache_id != bc->cnt) {
152 /*There was unreferenced block*/
153 bc->lba[cache_id] = b->lb_id;
154 bc->refctr[cache_id] = 1;
155 bc->lru_id[cache_id] = ++bc->lru_ctr;
157 /*Set valid cache data and id*/
158 b->data = bc->data + cache_id * bc->itemsize;
159 b->cache_id = cache_id;
161 /* Data in the cache is not up-to-date anymore. */
162 ext4_bcache_clear_flag(bc, cache_id, BC_UPTODATE);
167 if (bc->ref_blocks > bc->max_ref_blocks)
168 bc->max_ref_blocks = bc->ref_blocks;
170 /*Block needs to be read.*/
176 ext4_dbg(DEBUG_BCACHE, DBG_ERROR
177 "unable to alloc block cache!\n");
181 int ext4_bcache_free(struct ext4_bcache *bc, struct ext4_block *b,
184 ext4_assert(bc && b);
187 ext4_assert(b->lb_id);
189 /*Block should be in cache.*/
190 ext4_assert(b->cache_id < bc->cnt);
192 /*Check if someone don't try free unreferenced block cache.*/
193 ext4_assert(bc->refctr[b->cache_id]);
195 /*Just decrease reference counter*/
196 if (bc->refctr[b->cache_id])
197 bc->refctr[b->cache_id]--;
200 bc->free_delay[b->cache_id] = free_delay;
202 /*Update statistics*/
203 if (!bc->refctr[b->cache_id] && !bc->free_delay[b->cache_id])
214 bool ext4_bcache_is_full(struct ext4_bcache *bc)
216 return (bc->cnt == bc->ref_blocks);