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"
49 #define EXT4_BLOCK_ZERO() \
50 {.uptodate = 0, .dirty = 0, .lb_id = 0, .cache_id = 0, .data = 0}
52 /**@brief Single block descriptor*/
54 /**@brief Uptodate flag*/
57 /**@brief Dirty flag*/
60 /**@brief Logical block ID*/
66 /**@brief Data buffer.*/
70 /**@brief Block cache descriptor*/
73 /**@brief Item count in block cache*/
76 /**@brief Item size in block cache*/
79 /**@brief Last recently used counter*/
82 /**@brief Reference count table*/
83 uint32_t refctr[CONFIG_BLOCK_DEV_CACHE_SIZE];
85 /**@brief Last recently used ID table*/
86 uint32_t lru_id[CONFIG_BLOCK_DEV_CACHE_SIZE];
88 /**@brief Writeback free delay mode table*/
89 uint8_t free_delay[CONFIG_BLOCK_DEV_CACHE_SIZE];
91 /**@brief Logical block table*/
92 uint64_t lba[CONFIG_BLOCK_DEV_CACHE_SIZE];
95 int flags[CONFIG_BLOCK_DEV_CACHE_SIZE];
97 /**@brief Cache data buffers*/
100 /**@brief Currently referenced datablocks*/
103 /**@brief Maximum referenced datablocks*/
104 uint32_t max_ref_blocks;
107 enum bcache_state_bits {
112 #define ext4_bcache_set_flag(bc, id, b) \
113 (bc)->flags[id] |= 1 << (b)
115 #define ext4_bcache_clear_flag(bc, id, b) \
116 (bc)->flags[id] &= ~(1 << (b))
118 #define ext4_bcache_test_flag(bc, id, b) \
119 (((bc)->flags[id] & (1 << (b))) >> (b))
121 /**@brief Static initializer of block cache structure.*/
122 #define EXT4_BCACHE_STATIC_INSTANCE(__name, __cnt, __itemsize) \
123 static uint8_t __name##_data[(__cnt) * (__itemsize)]; \
124 static struct ext4_bcache __name = { \
126 .itemsize = __itemsize, \
128 .data = __name##_data, \
131 /**@brief Dynamic initialization of block cache.
132 * @param bc block cache descriptor
133 * @param cnt items count in block cache
134 * @param itemsize single item size (in bytes)
135 * @return standard error code*/
136 int ext4_bcache_init_dynamic(struct ext4_bcache *bc, uint32_t cnt,
139 /**@brief Dynamic de-initialization of block cache.
140 * @param bc block cache descriptor
141 * @return standard error code*/
142 int ext4_bcache_fini_dynamic(struct ext4_bcache *bc);
144 /**@brief Allocate block from block cache memory.
145 * Unreferenced block allocation is based on LRU
146 * (Last Recently Used) algorithm.
147 * @param bc block cache descriptor
148 * @param b block to alloc
149 * @param is_new block is new (needs to be read)
150 * @return standard error code*/
151 int ext4_bcache_alloc(struct ext4_bcache *bc, struct ext4_block *b,
154 /**@brief Free block from cache memory (decrement reference counter).
155 * @param bc block cache descriptor
156 * @param b block to free
157 * @param cache writeback mode
158 * @return standard error code*/
159 int ext4_bcache_free(struct ext4_bcache *bc, struct ext4_block *b,
162 /**@brief Return a full status of block cache.
163 * @param bc block cache descriptor
164 * @return full status*/
165 bool ext4_bcache_is_full(struct ext4_bcache *bc);
171 #endif /* EXT4_BCACHE_H_ */