3fc722a1bccb76c6f2610df83c2d58d0f46ce5ca
[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
49 #define EXT4_BLOCK_ZERO()       \
50         {.uptodate = 0, .dirty = 0, .lb_id = 0, .cache_id = 0, .data = 0}
51
52 /**@brief   Single block descriptor*/
53 struct ext4_block {
54         /**@brief   Uptodate flag*/
55         bool uptodate;
56
57         /**@brief   Dirty flag*/
58         bool dirty;
59
60         /**@brief   Logical block ID*/
61         uint64_t lb_id;
62
63         /**@brief   Cache id*/
64         uint32_t cache_id;
65
66         /**@brief   Data buffer.*/
67         uint8_t *data;
68 };
69
70 /**@brief   Block cache descriptor*/
71 struct ext4_bcache {
72
73         /**@brief   Item count in block cache*/
74         uint32_t cnt;
75
76         /**@brief   Item size in block cache*/
77         uint32_t itemsize;
78
79         /**@brief   Last recently used counter*/
80         uint32_t lru_ctr;
81
82         /**@brief   Reference count table*/
83         uint32_t refctr[CONFIG_BLOCK_DEV_CACHE_SIZE];
84
85         /**@brief   Last recently used ID table*/
86         uint32_t lru_id[CONFIG_BLOCK_DEV_CACHE_SIZE];
87
88         /**@brief   Writeback free delay mode table*/
89         uint8_t free_delay[CONFIG_BLOCK_DEV_CACHE_SIZE];
90
91         /**@brief   Logical block table*/
92         uint64_t lba[CONFIG_BLOCK_DEV_CACHE_SIZE];
93
94         /**@brief   Flags*/
95         int flags[CONFIG_BLOCK_DEV_CACHE_SIZE];
96
97         /**@brief   Cache data buffers*/
98         uint8_t *data;
99
100         /**@brief   Currently referenced datablocks*/
101         uint32_t ref_blocks;
102
103         /**@brief   Maximum referenced datablocks*/
104         uint32_t max_ref_blocks;
105 };
106
107 enum bcache_state_bits {
108         BC_UPTODATE,
109         BC_DIRTY
110 };
111
112 #define ext4_bcache_set_flag(bc, id, b)    \
113         (bc)->flags[id] |= 1 << (b)
114
115 #define ext4_bcache_clear_flag(bc, id, b)    \
116         (bc)->flags[id] &= ~(1 << (b))
117
118 #define ext4_bcache_test_flag(bc, id, b)    \
119         (((bc)->flags[id] & (1 << (b))) >> (b))
120
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 = {                                   \
125             .cnt = __cnt,                                                      \
126             .itemsize = __itemsize,                                            \
127             .lru_ctr = 0,                                                      \
128             .data = __name##_data,                                             \
129         }
130
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,
137                              uint32_t itemsize);
138
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);
143
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,
152                       bool *is_new);
153
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,
160                      uint8_t free_delay);
161
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);
166
167 #ifdef __cplusplus
168 }
169 #endif
170
171 #endif /* EXT4_BCACHE_H_ */
172
173 /**
174  * @}
175  */