ext4_blockdev: a number of changes below
[lwext4.git] / lwext4 / ext4_bcache.h
1 /*
2  * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
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.
16  *
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.
27  */
28
29 /** @addtogroup lwext4
30  * @{
31  */
32 /**
33  * @file  ext4_bcache.h
34  * @brief Block cache allocator.
35  */
36
37 #ifndef EXT4_BCACHE_H_
38 #define EXT4_BCACHE_H_
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43
44 #include "ext4_config.h"
45
46 #include <stdint.h>
47 #include <stdbool.h>
48 #include "tree.h"
49 #include "queue.h"
50
51 #define EXT4_BLOCK_ZERO()       \
52         {.uptodate = 0, .dirty = 0, .lb_id = 0, .data = 0}
53
54 /**@brief   Single block descriptor*/
55 struct ext4_block {
56         /**@brief   Uptodate flag*/
57         bool uptodate;
58
59         /**@brief   Dirty flag*/
60         bool dirty;
61
62         /**@brief   Logical block ID*/
63         uint64_t lb_id;
64
65         /**@brief   Buffer */
66         struct ext4_buf *buf;
67
68         /**@brief   Data buffer.*/
69         uint8_t *data;
70 };
71
72 struct ext4_bcache;
73
74 /**@brief   Single block descriptor*/
75 struct ext4_buf {
76         /**@brief   Flags*/
77         int flags;
78
79         /**@brief   Logical block address*/
80         uint64_t lba;
81
82         /**@brief   Data buffer.*/
83         uint8_t *data;
84
85         /**@brief   LRU priority. (unused) */
86         uint32_t lru_prio;
87
88         /**@brief   LRU id.*/
89         uint32_t lru_id;
90
91         /**@brief   Reference count table*/
92         uint32_t refctr;
93
94         /**@brief   Whether or not buffer is on dirty list.*/
95         bool on_dirty_list;
96
97         /**@brief   LBA tree node*/
98         RB_ENTRY(ext4_buf) lba_node;
99
100         /**@brief   LRU tree node*/
101         RB_ENTRY(ext4_buf) lru_node;
102
103         /**@brief   Dirty list node*/
104         SLIST_ENTRY(ext4_buf) dirty_node;
105
106         /**@brief   Callback routine after a disk-write operation.
107          * @param   bc block cache descriptor
108          * @param   buf buffer descriptor
109          * @param   standard error code returned by bdev->bwrite()
110          * @param   arg argument passed to this routine*/
111         void (*end_write)(struct ext4_bcache *bc,
112                           struct ext4_buf *buf,
113                           int res,
114                           void *arg);
115
116         /**@brief   argument passed to end_write() callback.*/
117         void *end_write_arg;
118 };
119
120 /**@brief   Block cache descriptor*/
121 struct ext4_bcache {
122
123         /**@brief   Item count in block cache*/
124         uint32_t cnt;
125
126         /**@brief   Item size in block cache*/
127         uint32_t itemsize;
128
129         /**@brief   Last recently used counter*/
130         uint32_t lru_ctr;
131
132         /**@brief   Currently referenced datablocks*/
133         uint32_t ref_blocks;
134
135         /**@brief   Maximum referenced datablocks*/
136         uint32_t max_ref_blocks;
137
138         /**@brief   The blockdev binded to this block cache*/
139         struct ext4_blockdev *bdev;
140
141         /**@brief   A tree holding all bufs*/
142         RB_HEAD(ext4_buf_lba, ext4_buf) lba_root;
143
144         /**@brief   A tree holding unreferenced bufs*/
145         RB_HEAD(ext4_buf_lru, ext4_buf) lru_root;
146
147         /**@brief   A singly-linked list holding dirty buffers*/
148         SLIST_HEAD(ext4_buf_dirty, ext4_buf) dirty_list;
149 };
150
151 enum bcache_state_bits {
152         BC_UPTODATE,
153         BC_DIRTY
154 };
155
156 #define ext4_bcache_set_flag(buf, b)    \
157         (buf)->flags |= 1 << (b)
158
159 #define ext4_bcache_clear_flag(buf, b)    \
160         (buf)->flags &= ~(1 << (b))
161
162 #define ext4_bcache_test_flag(buf, b)    \
163         (((buf)->flags & (1 << (b))) >> (b))
164
165 /**@brief   Static initializer of block cache structure.*/
166 #define EXT4_BCACHE_STATIC_INSTANCE(__name, __cnt, __itemsize)                 \
167         static struct ext4_bcache __name = {                                   \
168             .cnt = __cnt,                                                      \
169             .itemsize = __itemsize,                                            \
170             .lru_ctr = 0,                                                      \
171         }
172
173 /**@brief   Insert buffer to dirty cache list
174  * @param   bc block cache descriptor
175  * @param   buf buffer descriptor */
176 static inline void
177 ext4_bcache_insert_dirty_node(struct ext4_bcache *bc, struct ext4_buf *buf) {
178         if (!buf->on_dirty_list) {
179                 SLIST_INSERT_HEAD(&bc->dirty_list, buf, dirty_node);
180                 buf->on_dirty_list = true;
181         }
182 }
183
184 /**@brief   Remove buffer to dirty cache list
185  * @param   bc block cache descriptor
186  * @param   buf buffer descriptor */
187 static inline void
188 ext4_bcache_remove_dirty_node(struct ext4_bcache *bc, struct ext4_buf *buf) {
189         if (buf->on_dirty_list) {
190                 SLIST_REMOVE(&bc->dirty_list, buf, ext4_buf, dirty_node);
191                 buf->on_dirty_list = false;
192         }
193 }
194
195
196 /**@brief   Dynamic initialization of block cache.
197  * @param   bc block cache descriptor
198  * @param   cnt items count in block cache
199  * @param   itemsize single item size (in bytes)
200  * @return  standard error code*/
201 int ext4_bcache_init_dynamic(struct ext4_bcache *bc, uint32_t cnt,
202                              uint32_t itemsize);
203
204 /**@brief   Dynamic de-initialization of block cache.
205  * @param   bc block cache descriptor
206  * @return  standard error code*/
207 int ext4_bcache_fini_dynamic(struct ext4_bcache *bc);
208
209 /**@brief   Get a buffer with the lowest LRU counter in bcache.
210  * @param   bc block cache descriptor
211  * @return  buffer with the lowest LRU counter*/
212 struct ext4_buf *ext4_buf_lowest_lru(struct ext4_bcache *bc);
213
214 /**@brief   Drop unreferenced buffer from bcache.
215  * @param   bc block cache descriptor
216  * @param   buf buffer*/
217 void ext4_bcache_drop_buf(struct ext4_bcache *bc, struct ext4_buf *buf);
218
219 /**@brief   Allocate block from block cache memory.
220  *          Unreferenced block allocation is based on LRU
221  *          (Last Recently Used) algorithm.
222  * @param   bc block cache descriptor
223  * @param   b block to alloc
224  * @param   is_new block is new (needs to be read)
225  * @return  standard error code*/
226 int ext4_bcache_alloc(struct ext4_bcache *bc, struct ext4_block *b,
227                       bool *is_new);
228
229 /**@brief   Free block from cache memory (decrement reference counter).
230  * @param   bc block cache descriptor
231  * @param   b block to free
232  * @return  standard error code*/
233 int ext4_bcache_free(struct ext4_bcache *bc, struct ext4_block *b);
234
235 /**@brief   Return a full status of block cache.
236  * @param   bc block cache descriptor
237  * @return  full status*/
238 bool ext4_bcache_is_full(struct ext4_bcache *bc);
239
240 #ifdef __cplusplus
241 }
242 #endif
243
244 #endif /* EXT4_BCACHE_H_ */
245
246 /**
247  * @}
248  */