Improve include policy
[lwext4.git] / lwext4 / ext4_blockdev.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_blockdev.c
34  * @brief Block device module.
35  */
36
37 #include "ext4_config.h"
38 #include "ext4_blockdev.h"
39 #include "ext4_errno.h"
40 #include "ext4_debug.h"
41
42 #include <string.h>
43 #include <stdlib.h>
44
45 int ext4_block_init(struct ext4_blockdev *bdev)
46 {
47     int rc;
48     ext4_assert(bdev);
49
50     ext4_assert(bdev->open && bdev->close && bdev->bread && bdev->bwrite);
51
52     /*Low level block init*/
53     rc = bdev->open(bdev);
54     if (rc != EOK)
55         return rc;
56
57     bdev->flags |= EXT4_BDEV_INITIALIZED;
58
59     return EOK;
60 }
61
62 int ext4_block_bind_bcache(struct ext4_blockdev *bdev, struct ext4_bcache *bc)
63 {
64     ext4_assert(bdev && bc);
65     bdev->bc = bc;
66     return EOK;
67 }
68
69 void ext4_block_set_lb_size(struct ext4_blockdev *bdev, uint64_t lb_bsize)
70 {
71     /*Logical block size has to be multiply of physical */
72     ext4_assert(!(lb_bsize % bdev->ph_bsize));
73
74     bdev->lg_bsize = lb_bsize;
75     bdev->lg_bcnt = (bdev->ph_bcnt * bdev->ph_bsize) / lb_bsize;
76 }
77
78 int ext4_block_fini(struct ext4_blockdev *bdev)
79 {
80     ext4_assert(bdev);
81
82     bdev->flags &= ~(EXT4_BDEV_INITIALIZED);
83
84     /*Low level block fini*/
85     return bdev->close(bdev);
86 }
87
88 int ext4_block_get(struct ext4_blockdev *bdev, struct ext4_block *b,
89                    uint64_t lba)
90 {
91     uint64_t pba;
92     uint32_t pb_cnt;
93     uint32_t i;
94     bool is_new;
95     int r;
96
97     ext4_assert(bdev && b);
98
99     if (!(bdev->flags & EXT4_BDEV_INITIALIZED))
100         return EIO;
101
102     if (!(lba < bdev->lg_bcnt))
103         return ERANGE;
104
105     b->dirty = 0;
106     b->lb_id = lba;
107
108     /*If cache is full we have to flush it anyway :(*/
109     if (ext4_bcache_is_full(bdev->bc) && bdev->cache_write_back) {
110
111         uint32_t free_candidate = bdev->bc->cnt;
112         uint32_t min_lru = 0xFFFFFFFF;
113
114         for (i = 0; i < bdev->bc->cnt; ++i) {
115             /*Check if buffer free was delayed.*/
116             if (!bdev->bc->free_delay[i])
117                 continue;
118
119             /*Check reference counter.*/
120             if (bdev->bc->refctr[i])
121                 continue;
122
123             if (bdev->bc->lru_id[i] < min_lru) {
124                 min_lru = bdev->bc->lru_id[i];
125                 free_candidate = i;
126                 continue;
127             }
128         }
129
130         if (free_candidate < bdev->bc->cnt) {
131             /*Buffer free was delayed and have no reference. Flush it.*/
132             r = ext4_blocks_set_direct(
133                 bdev, bdev->bc->data + bdev->bc->itemsize * free_candidate,
134                 bdev->bc->lba[free_candidate], 1);
135             if (r != EOK)
136                 return r;
137
138             /*No delayed anymore*/
139             bdev->bc->free_delay[free_candidate] = 0;
140
141             /*Reduce refered block count*/
142             bdev->bc->ref_blocks--;
143         }
144     }
145
146     r = ext4_bcache_alloc(bdev->bc, b, &is_new);
147     if (r != EOK)
148         return r;
149
150     if (!is_new) {
151         /*Block is in cache. Read from physical device is not required*/
152         return EOK;
153     }
154
155     if (!b->data)
156         return ENOMEM;
157
158     pba = (lba * bdev->lg_bsize) / bdev->ph_bsize;
159     pb_cnt = bdev->lg_bsize / bdev->ph_bsize;
160
161     r = bdev->bread(bdev, b->data, pba, pb_cnt);
162
163     if (r != EOK) {
164         ext4_bcache_free(bdev->bc, b, 0);
165         b->lb_id = 0;
166         return r;
167     }
168
169     bdev->bread_ctr++;
170     return EOK;
171 }
172
173 int ext4_block_set(struct ext4_blockdev *bdev, struct ext4_block *b)
174 {
175     uint64_t pba;
176     uint32_t pb_cnt;
177     int r;
178
179     ext4_assert(bdev && b);
180
181     if (!(bdev->flags & EXT4_BDEV_INITIALIZED))
182         return EIO;
183
184     /*Doesn,t need to write.*/
185     if (!b->dirty && !bdev->bc->dirty[b->cache_id]) {
186         ext4_bcache_free(bdev->bc, b, 0);
187         return EOK;
188     }
189
190     /*Free cache delay mode*/
191     if (bdev->cache_write_back) {
192
193         /*Free cahe block and mark as free delayed*/
194         return ext4_bcache_free(bdev->bc, b, bdev->cache_write_back);
195     }
196
197     if (bdev->bc->refctr[b->cache_id] > 1) {
198         bdev->bc->dirty[b->cache_id] = true;
199         return ext4_bcache_free(bdev->bc, b, 0);
200     }
201
202     pba = (b->lb_id * bdev->lg_bsize) / bdev->ph_bsize;
203     pb_cnt = bdev->lg_bsize / bdev->ph_bsize;
204
205     r = bdev->bwrite(bdev, b->data, pba, pb_cnt);
206     bdev->bc->dirty[b->cache_id] = false;
207     if (r != EOK) {
208         b->dirty = false;
209         ext4_bcache_free(bdev->bc, b, 0);
210         return r;
211     }
212
213     bdev->bwrite_ctr++;
214     b->dirty = false;
215     ext4_bcache_free(bdev->bc, b, 0);
216     return EOK;
217 }
218
219 int ext4_blocks_get_direct(struct ext4_blockdev *bdev, void *buf, uint64_t lba,
220                            uint32_t cnt)
221 {
222     uint64_t pba;
223     uint32_t pb_cnt;
224
225     ext4_assert(bdev && buf);
226
227     pba = (lba * bdev->lg_bsize) / bdev->ph_bsize;
228     pb_cnt = bdev->lg_bsize / bdev->ph_bsize;
229
230     bdev->bread_ctr++;
231     return bdev->bread(bdev, buf, pba, pb_cnt * cnt);
232 }
233
234 int ext4_blocks_set_direct(struct ext4_blockdev *bdev, const void *buf,
235                            uint64_t lba, uint32_t cnt)
236 {
237     uint64_t pba;
238     uint32_t pb_cnt;
239
240     ext4_assert(bdev && buf);
241
242     pba = (lba * bdev->lg_bsize) / bdev->ph_bsize;
243     pb_cnt = bdev->lg_bsize / bdev->ph_bsize;
244
245     bdev->bwrite_ctr++;
246
247     return bdev->bwrite(bdev, buf, pba, pb_cnt * cnt);
248 }
249
250 int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off,
251                           const void *buf, uint32_t len)
252 {
253     uint64_t block_idx;
254     uint64_t block_end;
255     uint32_t blen;
256     uint32_t unalg;
257     int r = EOK;
258
259     const uint8_t *p = (void *)buf;
260
261     ext4_assert(bdev && buf);
262
263     if (!(bdev->flags & EXT4_BDEV_INITIALIZED))
264         return EIO;
265
266     block_idx = off / bdev->ph_bsize;
267     block_end = block_idx + len / bdev->ph_bsize;
268
269     if (!(block_end < bdev->ph_bcnt))
270         return EINVAL; /*Ups. Out of range operation*/
271
272     /*OK lets deal with the first possible unaligned block*/
273     unalg = (off & (bdev->ph_bsize - 1));
274     if (unalg) {
275
276         uint32_t wlen =
277             (bdev->ph_bsize - unalg) > len ? len : (bdev->ph_bsize - unalg);
278
279         r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1);
280
281         if (r != EOK)
282             return r;
283
284         memcpy(bdev->ph_bbuf + unalg, p, wlen);
285
286         r = bdev->bwrite(bdev, bdev->ph_bbuf, block_idx, 1);
287         if (r != EOK)
288             return r;
289
290         p += wlen;
291         len -= wlen;
292         block_idx++;
293     }
294
295     /*Aligned data*/
296     blen = len / bdev->ph_bsize;
297     r = bdev->bwrite(bdev, p, block_idx, blen);
298
299     if (r != EOK)
300         return r;
301
302     p += bdev->ph_bsize * blen;
303     len -= bdev->ph_bsize * blen;
304
305     block_idx += blen;
306
307     /*Rest of the data*/
308     if (len) {
309         r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1);
310         if (r != EOK)
311             return r;
312
313         memcpy(bdev->ph_bbuf, p, len);
314
315         r = bdev->bwrite(bdev, bdev->ph_bbuf, block_idx, 1);
316
317         if (r != EOK)
318             return r;
319     }
320
321     return r;
322 }
323
324 int ext4_block_readbytes(struct ext4_blockdev *bdev, uint64_t off, void *buf,
325                          uint32_t len)
326 {
327     uint64_t block_idx;
328     uint64_t block_end;
329     uint32_t blen;
330     uint32_t unalg;
331     int r = EOK;
332
333     uint8_t *p = (void *)buf;
334
335     ext4_assert(bdev && buf);
336
337     if (!(bdev->flags & EXT4_BDEV_INITIALIZED))
338         return EIO;
339
340     block_idx = off / bdev->ph_bsize;
341     block_end = block_idx + len / bdev->ph_bsize;
342
343     if (!(block_end < bdev->ph_bcnt))
344         return EINVAL; /*Ups. Out of range operation*/
345
346     /*OK lets deal with the first possible unaligned block*/
347     unalg = (off & (bdev->ph_bsize - 1));
348     if (unalg) {
349
350         uint32_t rlen =
351             (bdev->ph_bsize - unalg) > len ? len : (bdev->ph_bsize - unalg);
352
353         r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1);
354         if (r != EOK)
355             return r;
356
357         memcpy(p, bdev->ph_bbuf + unalg, rlen);
358
359         p += rlen;
360         len -= rlen;
361         block_idx++;
362     }
363
364     /*Aligned data*/
365     blen = len / bdev->ph_bsize;
366
367     r = bdev->bread(bdev, p, block_idx, blen);
368
369     if (r != EOK)
370         return r;
371
372     p += bdev->ph_bsize * blen;
373     len -= bdev->ph_bsize * blen;
374
375     block_idx += blen;
376
377     /*Rest of the data*/
378     if (len) {
379         r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1);
380         if (r != EOK)
381             return r;
382
383         memcpy(p, bdev->ph_bbuf, len);
384     }
385
386     return r;
387 }
388
389 int ext4_block_cache_write_back(struct ext4_blockdev *bdev, uint8_t on_off)
390 {
391     int r;
392     uint32_t i;
393
394     if (on_off)
395         bdev->cache_write_back++;
396
397     if (!on_off && bdev->cache_write_back)
398         bdev->cache_write_back--;
399
400     /*Flush all delayed cache blocks*/
401     if (!bdev->cache_write_back) {
402         for (i = 0; i < bdev->bc->cnt; ++i) {
403
404             /*Check if buffer free was delayed.*/
405             if (!bdev->bc->free_delay[i])
406                 continue;
407
408             /*Check reference counter.*/
409             if (bdev->bc->refctr[i])
410                 continue;
411
412             /*Buffer free was delayed and have no reference. Flush it.*/
413             r = ext4_blocks_set_direct(bdev,
414                                        bdev->bc->data + bdev->bc->itemsize * i,
415                                        bdev->bc->lba[i], 1);
416             if (r != EOK)
417                 return r;
418
419             /*No delayed anymore*/
420             bdev->bc->free_delay[i] = 0;
421
422             /*Reduce refered block count*/
423             bdev->bc->ref_blocks--;
424         }
425     }
426     return EOK;
427 }
428
429 /**
430  * @}
431  */