2 * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
\r
3 * All rights reserved.
\r
5 * Redistribution and use in source and binary forms, with or without
\r
6 * modification, are permitted provided that the following conditions
\r
9 * - Redistributions of source code must retain the above copyright
\r
10 * notice, this list of conditions and the following disclaimer.
\r
11 * - Redistributions in binary form must reproduce the above copyright
\r
12 * notice, this list of conditions and the following disclaimer in the
\r
13 * documentation and/or other materials provided with the distribution.
\r
14 * - The name of the author may not be used to endorse or promote products
\r
15 * derived from this software without specific prior written permission.
\r
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
\r
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
\r
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
\r
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
\r
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
\r
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
\r
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
\r
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
\r
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
\r
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
29 /** @addtogroup lwext4
\r
33 * @file ext4_bcache.h
\r
34 * @brief Block cache allocator.
\r
37 #ifndef EXT4_BCACHE_H_
\r
38 #define EXT4_BCACHE_H_
\r
40 #include <ext4_config.h>
\r
43 #include <stdbool.h>
\r
45 /**@brief Single block descriptor.*/
\r
47 /**@brief Dirty flag.*/
\r
50 /**@brief Logical block ID*/
\r
53 /**@brief Cache id*/
\r
56 /**@brief Data buffer.*/
\r
61 /**@brief Block cache descriptor.*/
\r
62 struct ext4_bcache {
\r
64 /**@brief Item count in block cache*/
\r
67 /**@brief Item size in block cache*/
\r
70 /**@brief Last recently used counter.*/
\r
73 /**@brief Reference count table (cnt).*/
\r
76 /**@brief Last recently used ID table (cnt)*/
\r
79 /**@brief Writeback free delay mode table (cnt)*/
\r
80 uint8_t *free_delay;
\r
82 /**@brief Logical block table (cnt).*/
\r
85 /**@brief Dirty mark (cnt).*/
\r
88 /**@brief Cache data buffers (cnt * itemsize)*/
\r
91 /**@brief Currently referenced datablocks*/
\r
92 uint32_t ref_blocks;
\r
94 /**@brief Maximum referenced datablocks*/
\r
95 uint32_t max_ref_blocks;
\r
99 /**@brief Static initializer of block cache structure.*/
\r
100 #define EXT4_BCACHE_STATIC_INSTANCE(__name, __cnt, __itemsize) \
\r
101 static uint32_t __name##_refctr[(__cnt)]; \
\r
102 static uint32_t __name##_lru_id[(__cnt)]; \
\r
103 static uint8_t __name##_free_delay[(__cnt)]; \
\r
104 static uint64_t __name##_lba[(__cnt)]; \
\r
105 static bool __name##_dirty[(__cnt)]; \
\r
106 static uint8_t __name##_data[(__cnt) * (__itemsize)]; \
\r
107 static struct ext4_bcache __name = { \
\r
109 .itemsize = __itemsize, \
\r
111 .refctr = __name##_refctr, \
\r
112 .lru_id = __name##_lru_id, \
\r
113 .lba = __name##_lba, \
\r
114 .dirty = __name##_dirty, \
\r
115 .free_delay= __name##_free_delay, \
\r
116 .data = __name##_data, \
\r
120 /**@brief Dynamic initialization of block cache.
\r
121 * @param bc block cache descriptor
\r
122 * @param cnt items count in block cache
\r
123 * @param itemsize single item size (in bytes)
\r
124 * @return standard error code*/
\r
125 int ext4_bcache_init_dynamic(struct ext4_bcache *bc, uint32_t cnt,
\r
126 uint32_t itemsize);
\r
128 /**@brief Dynamic de-initialization of block cache.
\r
129 * @param bc block cache descriptor
\r
130 * @return standard error code*/
\r
131 int ext4_bcache_fini_dynamic(struct ext4_bcache *bc);
\r
133 /**@brief Allocate block from block cache memory.
\r
134 * Unreferenced block allocation is based on LRU
\r
135 * (Last Recently Used) algorithm.
\r
136 * @param bc block cache descriptor
\r
137 * @param b block to alloc
\r
138 * @param is_new block is new (needs to be read)
\r
139 * @return standard error code*/
\r
140 int ext4_bcache_alloc(struct ext4_bcache *bc, struct ext4_block *b,
\r
143 /**@brief Free block from cache memory (decrement reference counter).
\r
144 * @param bc block cache descriptor
\r
145 * @param b block to free
\r
146 * @param cache writeback mode
\r
147 * @return standard error code*/
\r
148 int ext4_bcache_free (struct ext4_bcache *bc, struct ext4_block *b,
\r
149 uint8_t free_delay);
\r
152 /**@brief Return a full status of block cache.
\r
153 * @param bc block cache descriptor
\r
154 * @return full status*/
\r
155 bool ext4_bcache_is_full(struct ext4_bcache *bc);
\r
157 #endif /* EXT4_BCACHE_H_ */
\r