ext4_bcache & ext4_blockdev: unify buffer flushing code.
[lwext4.git] / lwext4 / ext4_bcache.c
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.c
34  * @brief Block cache allocator.
35  */
36
37 #include "ext4_config.h"
38 #include "ext4_bcache.h"
39 #include "ext4_blockdev.h"
40 #include "ext4_debug.h"
41 #include "ext4_errno.h"
42
43 #include <string.h>
44 #include <stdlib.h>
45
46 static int ext4_bcache_lba_compare(struct ext4_buf *a, struct ext4_buf *b)
47 {
48          if (a->lba > b->lba)
49                  return 1;
50          else if (a->lba < b->lba)
51                  return -1;
52          return 0;
53 }
54
55 static int ext4_bcache_lru_compare(struct ext4_buf *a, struct ext4_buf *b)
56 {
57         if (a->lru_id > b->lru_id)
58                 return 1;
59         else if (a->lru_id < b->lru_id)
60                 return -1;
61         return 0;
62 }
63
64 RB_GENERATE_INTERNAL(ext4_buf_lba, ext4_buf, lba_node,
65                      ext4_bcache_lba_compare, static inline)
66 RB_GENERATE_INTERNAL(ext4_buf_lru, ext4_buf, lru_node,
67                      ext4_bcache_lru_compare, static inline)
68
69 int ext4_bcache_init_dynamic(struct ext4_bcache *bc, uint32_t cnt,
70                              uint32_t itemsize)
71 {
72         ext4_assert(bc && cnt && itemsize);
73
74         memset(bc, 0, sizeof(struct ext4_bcache));
75
76         bc->cnt = cnt;
77         bc->itemsize = itemsize;
78         bc->ref_blocks = 0;
79         bc->max_ref_blocks = 0;
80
81         return EOK;
82 }
83
84 int ext4_bcache_fini_dynamic(struct ext4_bcache *bc)
85 {
86         memset(bc, 0, sizeof(struct ext4_bcache));
87         return EOK;
88 }
89
90 /**@brief:
91  *
92  *  This is ext4_bcache, the module handling basic buffer-cache stuff.
93  *
94  *  Buffers in a bcache are sorted by their LBA and stored in a
95  *  RB-Tree(lba_root).
96  *
97  *  Bcache also maintains another RB-Tree(lru_root) right now, where
98  *  buffers are sorted by their LRU id.
99  *
100  *  A singly-linked list is used to track those dirty buffers which are
101  *  ready to be flushed. (Those buffers which are dirty but also referenced
102  *  are not considered ready to be flushed.)
103  *
104  *  When a buffer is not referenced, it will be stored in both lba_root
105  *  and lru_root, while it will only be stored in lba_root when it is
106  *  referenced.
107  */
108
109 static struct ext4_buf *
110 ext4_buf_alloc(struct ext4_bcache *bc, uint64_t lba)
111 {
112         void *data;
113         struct ext4_buf *buf;
114         data = malloc(bc->itemsize);
115         if (!data)
116                 return NULL;
117
118         buf = calloc(1, sizeof(struct ext4_buf));
119         if (!buf) {
120                 free(data);
121                 return NULL;
122         }
123
124         buf->lba = lba;
125         buf->data = data;
126         return buf;
127 }
128
129 static void ext4_buf_free(struct ext4_buf *buf)
130 {
131         free(buf->data);
132         free(buf);
133 }
134
135 static struct ext4_buf *
136 ext4_buf_lookup(struct ext4_bcache *bc, uint64_t lba)
137 {
138         struct ext4_buf tmp = {
139                 .lba = lba
140         };
141
142         return RB_FIND(ext4_buf_lba, &bc->lba_root, &tmp);
143 }
144
145 struct ext4_buf *ext4_buf_lowest_lru(struct ext4_bcache *bc)
146 {
147         return RB_MIN(ext4_buf_lru, &bc->lru_root);
148 }
149
150 void ext4_bcache_drop_buf(struct ext4_bcache *bc, struct ext4_buf *buf)
151 {
152         /*Cannot drop any referenced buffers.*/
153         ext4_assert(!buf->refctr);
154
155         RB_REMOVE(ext4_buf_lba, &bc->lba_root, buf);
156         RB_REMOVE(ext4_buf_lru, &bc->lru_root, buf);
157
158         /*Forcibly drop dirty buffer.*/
159         if (ext4_bcache_test_flag(buf, BC_DIRTY))
160                 ext4_bcache_remove_dirty_node(bc, buf);
161
162         ext4_buf_free(buf);
163         bc->ref_blocks--;
164 }
165
166 int ext4_bcache_alloc(struct ext4_bcache *bc, struct ext4_block *b,
167                       bool *is_new)
168 {
169         /* Try to search the buffer with exaxt LBA. */
170         struct ext4_buf *buf = ext4_buf_lookup(bc, b->lb_id);
171         if (buf) {
172                 /* If buffer is not referenced. */
173                 if (!buf->refctr) {
174                         /* Assign new value to LRU id and increment LRU counter
175                          * by 1*/
176                         buf->lru_id = ++bc->lru_ctr;
177                         RB_REMOVE(ext4_buf_lru, &bc->lru_root, buf);
178                         if (ext4_bcache_test_flag(buf, BC_DIRTY))
179                                 ext4_bcache_remove_dirty_node(bc, buf);
180
181                 }
182
183                 buf->refctr++;
184
185                 b->uptodate = ext4_bcache_test_flag(buf, BC_UPTODATE);
186                 /* Right now we don't propagate the dirty flag from ext4_buf to
187                  * ext4_block. */
188                 b->dirty = false;
189                 b->buf = buf;
190                 b->data = buf->data;
191
192                 *is_new = false;
193                 return EOK;
194         }
195
196         /* We need to allocate one buffer.*/
197         buf = ext4_buf_alloc(bc, b->lb_id);
198         if (!buf)
199                 return ENOMEM;
200
201         RB_INSERT(ext4_buf_lba, &bc->lba_root, buf);
202         /* One more buffer in bcache now. :-) */
203         bc->ref_blocks++;
204
205         buf->refctr = 1;
206         /* Assign new value to LRU id and increment LRU counter
207          * by 1*/
208         buf->lru_id = ++bc->lru_ctr;
209
210         b->uptodate = false;
211         b->dirty = false;
212         b->buf = buf;
213         b->data = buf->data;
214
215         *is_new = true;
216         return EOK;
217 }
218
219 int ext4_bcache_free(struct ext4_bcache *bc, struct ext4_block *b)
220 {
221         struct ext4_buf *buf = b->buf;
222
223         ext4_assert(bc && b);
224
225         /*Check if valid.*/
226         ext4_assert(b->lb_id);
227
228         /*Block should have a valid pointer to ext4_buf.*/
229         ext4_assert(buf);
230
231         /*Check if someone don't try free unreferenced block cache.*/
232         ext4_assert(buf->refctr);
233
234         /*Just decrease reference counter*/
235         buf->refctr--;
236
237         /* If buffer is modified, buf will be mark up-to-date and dirty. */
238         if (b->dirty) {
239                 ext4_bcache_set_flag(buf, BC_DIRTY);
240                 ext4_bcache_set_flag(buf, BC_UPTODATE);
241                 b->uptodate = true;
242         }
243         /* Someone might want to drop this buffer from bcache. */
244         if (!b->uptodate) {
245                 ext4_bcache_clear_flag(buf, BC_DIRTY);
246                 ext4_bcache_clear_flag(buf, BC_UPTODATE);
247         }
248
249         /* We are the last one touching this buffer, do the cleanups. */
250         if (!buf->refctr) {
251                 RB_INSERT(ext4_buf_lru, &bc->lru_root, buf);
252                 /* This buffer is ready to be flushed. */
253                 if (ext4_bcache_test_flag(buf, BC_DIRTY)) {
254                         if (bc->bdev->cache_write_back)
255                                 ext4_bcache_insert_dirty_node(bc, buf);
256                         else
257                                 ext4_block_flush_buf(bc->bdev, buf);
258                 }
259
260                 /* The buffer is invalidated...drop it. */
261                 if (!ext4_bcache_test_flag(buf, BC_UPTODATE))
262                         ext4_bcache_drop_buf(bc, buf);
263
264         }
265
266         b->lb_id = 0;
267         b->data = 0;
268         b->uptodate = false;
269         b->dirty = false;
270
271         return EOK;
272 }
273
274 bool ext4_bcache_is_full(struct ext4_bcache *bc)
275 {
276         return (bc->cnt <= bc->ref_blocks);
277 }
278
279 /**
280  * @}
281  */