Add to mkfs configurable descriptor size & hash seed init
[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 #include "ext4_config.h"
41
42 #include <stdint.h>
43 #include <stdbool.h>
44
45 #define EXT4_BLOCK_ZERO() {.uptodate = 0, .dirty = 0, .lb_id = 0, .cache_id = 0, .data = 0}
46
47 /**@brief   Single block descriptor*/
48 struct ext4_block {
49         /**@brief   Uptodate flag*/
50         bool uptodate;
51
52         /**@brief   Dirty flag*/
53         bool dirty;
54
55         /**@brief   Logical block ID*/
56         uint64_t lb_id;
57
58         /**@brief   Cache id*/
59         uint32_t cache_id;
60
61         /**@brief   Data buffer.*/
62         uint8_t *data;
63 };
64
65 /**@brief   Block cache descriptor*/
66 struct ext4_bcache {
67
68         /**@brief   Item count in block cache*/
69         uint32_t cnt;
70
71         /**@brief   Item size in block cache*/
72         uint32_t itemsize;
73
74         /**@brief   Last recently used counter*/
75         uint32_t lru_ctr;
76
77         /**@brief   Reference count table*/
78         uint32_t refctr[CONFIG_BLOCK_DEV_CACHE_SIZE];
79
80         /**@brief   Last recently used ID table*/
81         uint32_t lru_id[CONFIG_BLOCK_DEV_CACHE_SIZE];
82
83         /**@brief   Writeback free delay mode table*/
84         uint8_t free_delay[CONFIG_BLOCK_DEV_CACHE_SIZE];
85
86         /**@brief   Logical block table*/
87         uint64_t lba[CONFIG_BLOCK_DEV_CACHE_SIZE];
88
89         /**@brief   Flags*/
90         int flags[CONFIG_BLOCK_DEV_CACHE_SIZE];
91
92         /**@brief   Cache data buffers*/
93         uint8_t *data;
94
95         /**@brief   Currently referenced datablocks*/
96         uint32_t ref_blocks;
97
98         /**@brief   Maximum referenced datablocks*/
99         uint32_t max_ref_blocks;
100 };
101
102 enum bcache_state_bits {
103         BC_UPTODATE,
104         BC_DIRTY
105 };
106
107 #define ext4_bcache_set_flag(bc, id, b)    \
108         (bc)->flags[id] |= 1 << (b)
109
110 #define ext4_bcache_clear_flag(bc, id, b)    \
111         (bc)->flags[id] &= ~(1 << (b))
112
113 #define ext4_bcache_test_flag(bc, id, b)    \
114         (((bc)->flags[id] & (1 << (b))) >> (b))
115
116 /**@brief   Static initializer of block cache structure.*/
117 #define EXT4_BCACHE_STATIC_INSTANCE(__name, __cnt, __itemsize)                 \
118         static uint8_t __name##_data[(__cnt) * (__itemsize)];                  \
119         static struct ext4_bcache __name = {                                   \
120             .cnt = __cnt,                                                      \
121             .itemsize = __itemsize,                                            \
122             .lru_ctr = 0,                                                      \
123             .data = __name##_data,                                             \
124         }
125
126 /**@brief   Dynamic initialization of block cache.
127  * @param   bc block cache descriptor
128  * @param   cnt items count in block cache
129  * @param   itemsize single item size (in bytes)
130  * @return  standard error code*/
131 int ext4_bcache_init_dynamic(struct ext4_bcache *bc, uint32_t cnt,
132                              uint32_t itemsize);
133
134 /**@brief   Dynamic de-initialization of block cache.
135  * @param   bc block cache descriptor
136  * @return  standard error code*/
137 int ext4_bcache_fini_dynamic(struct ext4_bcache *bc);
138
139 /**@brief   Allocate block from block cache memory.
140  *          Unreferenced block allocation is based on LRU
141  *          (Last Recently Used) algorithm.
142  * @param   bc block cache descriptor
143  * @param   b block to alloc
144  * @param   is_new block is new (needs to be read)
145  * @return  standard error code*/
146 int ext4_bcache_alloc(struct ext4_bcache *bc, struct ext4_block *b,
147                       bool *is_new);
148
149 /**@brief   Free block from cache memory (decrement reference counter).
150  * @param   bc block cache descriptor
151  * @param   b block to free
152  * @param   cache writeback mode
153  * @return  standard error code*/
154 int ext4_bcache_free(struct ext4_bcache *bc, struct ext4_block *b,
155                      uint8_t free_delay);
156
157 /**@brief   Return a full status of block cache.
158  * @param   bc block cache descriptor
159  * @return  full status*/
160 bool ext4_bcache_is_full(struct ext4_bcache *bc);
161
162 #endif /* EXT4_BCACHE_H_ */
163
164 /**
165  * @}
166  */