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