caaa8606b9406eae96ab644ab608e7938f4d6ce6
[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 /**@brief   Single block descriptor*/
73 struct ext4_buf {
74         /**@brief   Flags*/
75         int flags;
76
77         /**@brief   Logical block address*/
78         uint64_t lba;
79
80         /**@brief   Data buffer.*/
81         uint8_t *data;
82
83         /**@brief   LRU priority. (unused) */
84         uint32_t lru_prio;
85
86         /**@brief   LRU id.*/
87         uint32_t lru_id;
88
89         /**@brief   Reference count table*/
90         uint32_t refctr;
91
92         /**@brief   LBA tree node*/
93         RB_ENTRY(ext4_buf) lba_node;
94
95         /**@brief   LRU tree node*/
96         RB_ENTRY(ext4_buf) lru_node;
97
98         /**@brief   Dirty list node*/
99         SLIST_ENTRY(ext4_buf) dirty_node;
100 };
101
102 /**@brief   Block cache descriptor*/
103 struct ext4_bcache {
104
105         /**@brief   Item count in block cache*/
106         uint32_t cnt;
107
108         /**@brief   Item size in block cache*/
109         uint32_t itemsize;
110
111         /**@brief   Last recently used counter*/
112         uint32_t lru_ctr;
113
114         /**@brief   Writeback free delay mode*/
115         uint8_t free_delay;
116
117         /**@brief   Currently referenced datablocks*/
118         uint32_t ref_blocks;
119
120         /**@brief   Maximum referenced datablocks*/
121         uint32_t max_ref_blocks;
122
123         /**@brief   A tree holding all bufs*/
124         RB_HEAD(ext4_buf_lba, ext4_buf) lba_root;
125
126         /**@brief   A tree holding unreferenced bufs*/
127         RB_HEAD(ext4_buf_lru, ext4_buf) lru_root;
128
129         /**@brief   A singly-linked list holding dirty buffers*/
130         SLIST_HEAD(ext4_buf_dirty, ext4_buf) dirty_list;
131 };
132
133 enum bcache_state_bits {
134         BC_UPTODATE,
135         BC_DIRTY
136 };
137
138 #define ext4_bcache_set_flag(buf, b)    \
139         (buf)->flags |= 1 << (b)
140
141 #define ext4_bcache_clear_flag(buf, b)    \
142         (buf)->flags &= ~(1 << (b))
143
144 #define ext4_bcache_test_flag(buf, b)    \
145         (((buf)->flags & (1 << (b))) >> (b))
146
147 /**@brief   Static initializer of block cache structure.*/
148 #define EXT4_BCACHE_STATIC_INSTANCE(__name, __cnt, __itemsize)                 \
149         static struct ext4_bcache __name = {                                   \
150             .cnt = __cnt,                                                      \
151             .itemsize = __itemsize,                                            \
152             .lru_ctr = 0,                                                      \
153         }
154
155 /**@brief   Dynamic initialization of block cache.
156  * @param   bc block cache descriptor
157  * @param   cnt items count in block cache
158  * @param   itemsize single item size (in bytes)
159  * @return  standard error code*/
160 int ext4_bcache_init_dynamic(struct ext4_bcache *bc, uint32_t cnt,
161                              uint32_t itemsize);
162
163 /**@brief   Dynamic de-initialization of block cache.
164  * @param   bc block cache descriptor
165  * @return  standard error code*/
166 int ext4_bcache_fini_dynamic(struct ext4_bcache *bc);
167
168 /**@brief   Get a buffer with the lowest LRU counter in bcache.
169  * @param   bc block cache descriptor
170  * @return  buffer with the lowest LRU counter*/
171 struct ext4_buf *ext4_buf_lowest_lru(struct ext4_bcache *bc);
172
173 /**@brief   Drop unreferenced buffer from bcache.
174  * @param   bc block cache descriptor
175  * @param   buf buffer*/
176 void ext4_bcache_drop_buf(struct ext4_bcache *bc, struct ext4_buf *buf);
177
178 /**@brief   Allocate block from block cache memory.
179  *          Unreferenced block allocation is based on LRU
180  *          (Last Recently Used) algorithm.
181  * @param   bc block cache descriptor
182  * @param   b block to alloc
183  * @param   is_new block is new (needs to be read)
184  * @return  standard error code*/
185 int ext4_bcache_alloc(struct ext4_bcache *bc, struct ext4_block *b,
186                       bool *is_new);
187
188 /**@brief   Free block from cache memory (decrement reference counter).
189  * @param   bc block cache descriptor
190  * @param   b block to free
191  * @param   cache writeback mode
192  * @return  standard error code*/
193 int ext4_bcache_free(struct ext4_bcache *bc, struct ext4_block *b,
194                      uint8_t free_delay);
195
196 /**@brief   Return a full status of block cache.
197  * @param   bc block cache descriptor
198  * @return  full status*/
199 bool ext4_bcache_is_full(struct ext4_bcache *bc);
200
201 #ifdef __cplusplus
202 }
203 #endif
204
205 #endif /* EXT4_BCACHE_H_ */
206
207 /**
208  * @}
209  */