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_
40 #include "ext4_config.h"
45 #define EXT4_BLOCK_ZERO() {.dirty = 0, .lb_id = 0, .cache_id = 0, .data = 0}
47 /**@brief Single block descriptor*/
49 /**@brief Dirty flag*/
52 /**@brief Logical block ID*/
58 /**@brief Data buffer.*/
62 /**@brief Block cache descriptor*/
65 /**@brief Item count in block cache*/
68 /**@brief Item size in block cache*/
71 /**@brief Last recently used counter*/
74 /**@brief Reference count table*/
75 uint32_t refctr[CONFIG_BLOCK_DEV_CACHE_SIZE];
77 /**@brief Last recently used ID table*/
78 uint32_t lru_id[CONFIG_BLOCK_DEV_CACHE_SIZE];
80 /**@brief Writeback free delay mode table*/
81 uint8_t free_delay[CONFIG_BLOCK_DEV_CACHE_SIZE];
83 /**@brief Logical block table*/
84 uint64_t lba[CONFIG_BLOCK_DEV_CACHE_SIZE];
86 /**@brief Dirty mark*/
87 bool dirty[CONFIG_BLOCK_DEV_CACHE_SIZE];
89 /**@brief Cache data buffers*/
92 /**@brief Currently referenced datablocks*/
95 /**@brief Maximum referenced datablocks*/
96 uint32_t max_ref_blocks;
99 /**@brief Static initializer of block cache structure.*/
100 #define EXT4_BCACHE_STATIC_INSTANCE(__name, __cnt, __itemsize) \
101 static uint8_t __name##_data[(__cnt) * (__itemsize)]; \
102 static struct ext4_bcache __name = { \
104 .itemsize = __itemsize, \
106 .data = __name##_data, \
109 /**@brief Dynamic initialization of block cache.
110 * @param bc block cache descriptor
111 * @param cnt items count in block cache
112 * @param itemsize single item size (in bytes)
113 * @return standard error code*/
114 int ext4_bcache_init_dynamic(struct ext4_bcache *bc, uint32_t cnt,
117 /**@brief Dynamic de-initialization of block cache.
118 * @param bc block cache descriptor
119 * @return standard error code*/
120 int ext4_bcache_fini_dynamic(struct ext4_bcache *bc);
122 /**@brief Allocate block from block cache memory.
123 * Unreferenced block allocation is based on LRU
124 * (Last Recently Used) algorithm.
125 * @param bc block cache descriptor
126 * @param b block to alloc
127 * @param is_new block is new (needs to be read)
128 * @return standard error code*/
129 int ext4_bcache_alloc(struct ext4_bcache *bc, struct ext4_block *b,
132 /**@brief Free block from cache memory (decrement reference counter).
133 * @param bc block cache descriptor
134 * @param b block to free
135 * @param cache writeback mode
136 * @return standard error code*/
137 int ext4_bcache_free(struct ext4_bcache *bc, struct ext4_block *b,
140 /**@brief Return a full status of block cache.
141 * @param bc block cache descriptor
142 * @return full status*/
143 bool ext4_bcache_is_full(struct ext4_bcache *bc);
145 #endif /* EXT4_BCACHE_H_ */