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 #ifndef EXT4_BCACHE_H_
38 #define EXT4_BCACHE_H_
44 #include "ext4_config.h"
51 #define EXT4_BLOCK_ZERO() \
52 {.uptodate = 0, .dirty = 0, .lb_id = 0, .data = 0}
54 /**@brief Single block descriptor*/
56 /**@brief Uptodate flag*/
59 /**@brief Dirty flag*/
62 /**@brief Logical block ID*/
68 /**@brief Data buffer.*/
72 /**@brief Single block descriptor*/
77 /**@brief Logical block address*/
80 /**@brief Data buffer.*/
83 /**@brief LRU priority. (unused) */
89 /**@brief Reference count table*/
92 /**@brief Whether or not buffer is on dirty list.*/
95 /**@brief LBA tree node*/
96 RB_ENTRY(ext4_buf) lba_node;
98 /**@brief LRU tree node*/
99 RB_ENTRY(ext4_buf) lru_node;
101 /**@brief Dirty list node*/
102 SLIST_ENTRY(ext4_buf) dirty_node;
105 /**@brief Block cache descriptor*/
108 /**@brief Item count in block cache*/
111 /**@brief Item size in block cache*/
114 /**@brief Last recently used counter*/
117 /**@brief Currently referenced datablocks*/
120 /**@brief Maximum referenced datablocks*/
121 uint32_t max_ref_blocks;
123 /**@brief The blockdev binded to this block cache*/
124 struct ext4_blockdev *bdev;
126 /**@brief A tree holding all bufs*/
127 RB_HEAD(ext4_buf_lba, ext4_buf) lba_root;
129 /**@brief A tree holding unreferenced bufs*/
130 RB_HEAD(ext4_buf_lru, ext4_buf) lru_root;
132 /**@brief A singly-linked list holding dirty buffers*/
133 SLIST_HEAD(ext4_buf_dirty, ext4_buf) dirty_list;
136 enum bcache_state_bits {
141 #define ext4_bcache_set_flag(buf, b) \
142 (buf)->flags |= 1 << (b)
144 #define ext4_bcache_clear_flag(buf, b) \
145 (buf)->flags &= ~(1 << (b))
147 #define ext4_bcache_test_flag(buf, b) \
148 (((buf)->flags & (1 << (b))) >> (b))
150 /**@brief Static initializer of block cache structure.*/
151 #define EXT4_BCACHE_STATIC_INSTANCE(__name, __cnt, __itemsize) \
152 static struct ext4_bcache __name = { \
154 .itemsize = __itemsize, \
158 /**@brief Insert buffer to dirty cache list
159 * @param bc block cache descriptor
160 * @param buf buffer descriptor */
162 ext4_bcache_insert_dirty_node(struct ext4_bcache *bc, struct ext4_buf *buf) {
163 if (!buf->on_dirty_list) {
164 SLIST_INSERT_HEAD(&bc->dirty_list, buf, dirty_node);
165 buf->on_dirty_list = true;
169 /**@brief Remove buffer to dirty cache list
170 * @param bc block cache descriptor
171 * @param buf buffer descriptor */
173 ext4_bcache_remove_dirty_node(struct ext4_bcache *bc, struct ext4_buf *buf) {
174 if (buf->on_dirty_list) {
175 SLIST_REMOVE(&bc->dirty_list, buf, ext4_buf, dirty_node);
176 buf->on_dirty_list = false;
181 /**@brief Dynamic initialization of block cache.
182 * @param bc block cache descriptor
183 * @param cnt items count in block cache
184 * @param itemsize single item size (in bytes)
185 * @return standard error code*/
186 int ext4_bcache_init_dynamic(struct ext4_bcache *bc, uint32_t cnt,
189 /**@brief Dynamic de-initialization of block cache.
190 * @param bc block cache descriptor
191 * @return standard error code*/
192 int ext4_bcache_fini_dynamic(struct ext4_bcache *bc);
194 /**@brief Get a buffer with the lowest LRU counter in bcache.
195 * @param bc block cache descriptor
196 * @return buffer with the lowest LRU counter*/
197 struct ext4_buf *ext4_buf_lowest_lru(struct ext4_bcache *bc);
199 /**@brief Drop unreferenced buffer from bcache.
200 * @param bc block cache descriptor
201 * @param buf buffer*/
202 void ext4_bcache_drop_buf(struct ext4_bcache *bc, struct ext4_buf *buf);
204 /**@brief Allocate block from block cache memory.
205 * Unreferenced block allocation is based on LRU
206 * (Last Recently Used) algorithm.
207 * @param bc block cache descriptor
208 * @param b block to alloc
209 * @param is_new block is new (needs to be read)
210 * @return standard error code*/
211 int ext4_bcache_alloc(struct ext4_bcache *bc, struct ext4_block *b,
214 /**@brief Free block from cache memory (decrement reference counter).
215 * @param bc block cache descriptor
216 * @param b block to free
217 * @return standard error code*/
218 int ext4_bcache_free(struct ext4_bcache *bc, struct ext4_block *b);
220 /**@brief Return a full status of block cache.
221 * @param bc block cache descriptor
222 * @return full status*/
223 bool ext4_bcache_is_full(struct ext4_bcache *bc);
229 #endif /* EXT4_BCACHE_H_ */