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;
122 /*Best fit calculations.*/
126 if (bc->free_delay[i])
129 /*Block is unreferenced, but it may exist block with
130 * lower usage marker*/
133 if (cache_id == bc->cnt) {
135 alloc_id = bc->lru_id[i];
140 if (alloc_id <= bc->lru_id[i])
143 /*This block has lower alloc id marker*/
145 alloc_id = bc->lru_id[i];
148 if (cache_id != bc->cnt) {
149 /*There was unreferenced block*/
150 bc->lba[cache_id] = b->lb_id;
151 bc->refctr[cache_id] = 1;
152 bc->lru_id[cache_id] = ++bc->lru_ctr;
154 /*Set valid cache data and id*/
155 b->data = bc->data + cache_id * bc->itemsize;
156 b->cache_id = cache_id;
160 if (bc->ref_blocks > bc->max_ref_blocks)
161 bc->max_ref_blocks = bc->ref_blocks;
163 /*Block needs to be read.*/
169 ext4_dprintf(EXT4_DEBUG_BCACHE,
170 "ext4_bcache_alloc: FAIL, unable to alloc block cache!\n");
174 int ext4_bcache_free(struct ext4_bcache *bc, struct ext4_block *b,
177 ext4_assert(bc && b);
180 ext4_assert(b->lb_id);
182 /*Block should be in cache.*/
183 ext4_assert(b->cache_id < bc->cnt);
185 /*Check if someone don't try free unreferenced block cache.*/
186 ext4_assert(bc->refctr[b->cache_id]);
188 /*Just decrease reference counter*/
189 if (bc->refctr[b->cache_id])
190 bc->refctr[b->cache_id]--;
193 bc->free_delay[b->cache_id] = free_delay;
195 /*Update statistics*/
196 if (!bc->refctr[b->cache_id] && !bc->free_delay[b->cache_id])
206 bool ext4_bcache_is_full(struct ext4_bcache *bc)
208 return (bc->cnt == bc->ref_blocks);