Linux codestyle format (tabs indenation)
[lwext4.git] / lwext4 / ext4_bcache.h
1 /*\r
2  * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)\r
3  * All rights reserved.\r
4  *\r
5  * Redistribution and use in source and binary forms, with or without\r
6  * modification, are permitted provided that the following conditions\r
7  * are met:\r
8  *\r
9  * - Redistributions of source code must retain the above copyright\r
10  *   notice, this list of conditions and the following disclaimer.\r
11  * - Redistributions in binary form must reproduce the above copyright\r
12  *   notice, this list of conditions and the following disclaimer in the\r
13  *   documentation and/or other materials provided with the distribution.\r
14  * - The name of the author may not be used to endorse or promote products\r
15  *   derived from this software without specific prior written permission.\r
16  *\r
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\r
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\r
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
27  */\r
28 \r
29 /** @addtogroup lwext4\r
30  * @{\r
31  */\r
32 /**\r
33  * @file  ext4_bcache.h\r
34  * @brief Block cache allocator.\r
35  */\r
36 \r
37 #ifndef EXT4_BCACHE_H_\r
38 #define EXT4_BCACHE_H_\r
39 \r
40 #include "ext4_config.h"\r
41 \r
42 #include <stdint.h>\r
43 #include <stdbool.h>\r
44 \r
45 /**@brief   Single block descriptor*/\r
46 struct ext4_block {\r
47         /**@brief   Dirty flag*/\r
48         bool dirty;\r
49 \r
50         /**@brief   Logical block ID*/\r
51         uint64_t lb_id;\r
52 \r
53         /**@brief   Cache id*/\r
54         uint32_t cache_id;\r
55 \r
56         /**@brief   Data buffer.*/\r
57         uint8_t *data;\r
58 };\r
59 \r
60 /**@brief   Block cache descriptor*/\r
61 struct ext4_bcache {\r
62 \r
63         /**@brief   Item count in block cache*/\r
64         uint32_t cnt;\r
65 \r
66         /**@brief   Item size in block cache*/\r
67         uint32_t itemsize;\r
68 \r
69         /**@brief   Last recently used counter*/\r
70         uint32_t lru_ctr;\r
71 \r
72         /**@brief   Reference count table*/\r
73         uint32_t refctr[CONFIG_BLOCK_DEV_CACHE_SIZE];\r
74 \r
75         /**@brief   Last recently used ID table*/\r
76         uint32_t lru_id[CONFIG_BLOCK_DEV_CACHE_SIZE];\r
77 \r
78         /**@brief   Writeback free delay mode table*/\r
79         uint8_t free_delay[CONFIG_BLOCK_DEV_CACHE_SIZE];\r
80 \r
81         /**@brief   Logical block table*/\r
82         uint64_t lba[CONFIG_BLOCK_DEV_CACHE_SIZE];\r
83 \r
84         /**@brief   Dirty mark*/\r
85         bool dirty[CONFIG_BLOCK_DEV_CACHE_SIZE];\r
86 \r
87         /**@brief   Cache data buffers*/\r
88         uint8_t *data;\r
89 \r
90         /**@brief   Currently referenced datablocks*/\r
91         uint32_t ref_blocks;\r
92 \r
93         /**@brief   Maximum referenced datablocks*/\r
94         uint32_t max_ref_blocks;\r
95 };\r
96 \r
97 /**@brief   Static initializer of block cache structure.*/\r
98 #define EXT4_BCACHE_STATIC_INSTANCE(__name, __cnt, __itemsize)                 \\r
99         static uint8_t __name##_data[(__cnt) * (__itemsize)];                  \\r
100         static struct ext4_bcache __name = {                                   \\r
101             .cnt = __cnt,                                                      \\r
102             .itemsize = __itemsize,                                            \\r
103             .lru_ctr = 0,                                                      \\r
104             .data = __name##_data,                                             \\r
105         }\r
106 \r
107 /**@brief   Dynamic initialization of block cache.\r
108  * @param   bc block cache descriptor\r
109  * @param   cnt items count in block cache\r
110  * @param   itemsize single item size (in bytes)\r
111  * @return  standard error code*/\r
112 int ext4_bcache_init_dynamic(struct ext4_bcache *bc, uint32_t cnt,\r
113                              uint32_t itemsize);\r
114 \r
115 /**@brief   Dynamic de-initialization of block cache.\r
116  * @param   bc block cache descriptor\r
117  * @return  standard error code*/\r
118 int ext4_bcache_fini_dynamic(struct ext4_bcache *bc);\r
119 \r
120 /**@brief   Allocate block from block cache memory.\r
121  *          Unreferenced block allocation is based on LRU\r
122  *          (Last Recently Used) algorithm.\r
123  * @param   bc block cache descriptor\r
124  * @param   b block to alloc\r
125  * @param   is_new block is new (needs to be read)\r
126  * @return  standard error code*/\r
127 int ext4_bcache_alloc(struct ext4_bcache *bc, struct ext4_block *b,\r
128                       bool *is_new);\r
129 \r
130 /**@brief   Free block from cache memory (decrement reference counter).\r
131  * @param   bc block cache descriptor\r
132  * @param   b block to free\r
133  * @param   cache writeback mode\r
134  * @return  standard error code*/\r
135 int ext4_bcache_free(struct ext4_bcache *bc, struct ext4_block *b,\r
136                      uint8_t free_delay);\r
137 \r
138 /**@brief   Return a full status of block cache.\r
139  * @param   bc block cache descriptor\r
140  * @return  full status*/\r
141 bool ext4_bcache_is_full(struct ext4_bcache *bc);\r
142 \r
143 #endif /* EXT4_BCACHE_H_ */\r
144 \r
145 /**\r
146  * @}\r
147  */\r