2 * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
29 /** @addtogroup lwext4
33 * @file ext4_blockdev.c
34 * @brief Block device module.
37 #include "ext4_config.h"
38 #include "ext4_blockdev.h"
39 #include "ext4_errno.h"
40 #include "ext4_debug.h"
45 int ext4_block_init(struct ext4_blockdev *bdev)
50 ext4_assert(bdev->open && bdev->close && bdev->bread && bdev->bwrite);
52 /*Low level block init*/
53 rc = bdev->open(bdev);
57 bdev->flags |= EXT4_BDEV_INITIALIZED;
62 int ext4_block_bind_bcache(struct ext4_blockdev *bdev, struct ext4_bcache *bc)
64 ext4_assert(bdev && bc);
69 void ext4_block_set_lb_size(struct ext4_blockdev *bdev, uint64_t lb_bsize)
71 /*Logical block size has to be multiply of physical */
72 ext4_assert(!(lb_bsize % bdev->ph_bsize));
74 bdev->lg_bsize = lb_bsize;
75 bdev->lg_bcnt = (bdev->ph_bcnt * bdev->ph_bsize) / lb_bsize;
78 int ext4_block_fini(struct ext4_blockdev *bdev)
82 bdev->flags &= ~(EXT4_BDEV_INITIALIZED);
84 /*Low level block fini*/
85 return bdev->close(bdev);
88 int ext4_block_get(struct ext4_blockdev *bdev, struct ext4_block *b,
97 ext4_assert(bdev && b);
99 if (!(bdev->flags & EXT4_BDEV_INITIALIZED))
102 if (!(lba < bdev->lg_bcnt))
108 /*If cache is full we have to flush it anyway :(*/
109 if (ext4_bcache_is_full(bdev->bc) && bdev->cache_write_back) {
111 uint32_t free_candidate = bdev->bc->cnt;
112 uint32_t min_lru = 0xFFFFFFFF;
114 for (i = 0; i < bdev->bc->cnt; ++i) {
115 /*Check if buffer free was delayed.*/
116 if (!bdev->bc->free_delay[i])
119 /*Check reference counter.*/
120 if (bdev->bc->refctr[i])
123 if (bdev->bc->lru_id[i] < min_lru) {
124 min_lru = bdev->bc->lru_id[i];
130 if (free_candidate < bdev->bc->cnt) {
131 /*Buffer free was delayed and have no reference. Flush
133 r = ext4_blocks_set_direct(
134 bdev, bdev->bc->data +
135 bdev->bc->itemsize * free_candidate,
136 bdev->bc->lba[free_candidate], 1);
140 /*No delayed anymore*/
141 bdev->bc->free_delay[free_candidate] = 0;
143 /*Reduce reference counter*/
144 bdev->bc->ref_blocks--;
148 r = ext4_bcache_alloc(bdev->bc, b, &is_new);
153 /*Block is in cache. Read from physical device is not required*/
160 pba = (lba * bdev->lg_bsize) / bdev->ph_bsize;
161 pb_cnt = bdev->lg_bsize / bdev->ph_bsize;
163 r = bdev->bread(bdev, b->data, pba, pb_cnt);
166 ext4_bcache_free(bdev->bc, b, 0);
175 int ext4_block_set(struct ext4_blockdev *bdev, struct ext4_block *b)
181 ext4_assert(bdev && b);
183 if (!(bdev->flags & EXT4_BDEV_INITIALIZED))
186 /*No need to write.*/
187 if (!b->dirty && !bdev->bc->dirty[b->cache_id]) {
188 ext4_bcache_free(bdev->bc, b, 0);
192 /*Free cache delay mode*/
193 if (bdev->cache_write_back) {
195 /*Free cache block and mark as free delayed*/
196 return ext4_bcache_free(bdev->bc, b, bdev->cache_write_back);
199 if (bdev->bc->refctr[b->cache_id] > 1) {
200 bdev->bc->dirty[b->cache_id] = true;
201 return ext4_bcache_free(bdev->bc, b, 0);
204 pba = (b->lb_id * bdev->lg_bsize) / bdev->ph_bsize;
205 pb_cnt = bdev->lg_bsize / bdev->ph_bsize;
207 r = bdev->bwrite(bdev, b->data, pba, pb_cnt);
208 bdev->bc->dirty[b->cache_id] = false;
211 ext4_bcache_free(bdev->bc, b, 0);
217 ext4_bcache_free(bdev->bc, b, 0);
221 int ext4_blocks_get_direct(struct ext4_blockdev *bdev, void *buf, uint64_t lba,
227 ext4_assert(bdev && buf);
229 pba = (lba * bdev->lg_bsize) / bdev->ph_bsize;
230 pb_cnt = bdev->lg_bsize / bdev->ph_bsize;
233 return bdev->bread(bdev, buf, pba, pb_cnt * cnt);
236 int ext4_blocks_set_direct(struct ext4_blockdev *bdev, const void *buf,
237 uint64_t lba, uint32_t cnt)
242 ext4_assert(bdev && buf);
244 pba = (lba * bdev->lg_bsize) / bdev->ph_bsize;
245 pb_cnt = bdev->lg_bsize / bdev->ph_bsize;
249 return bdev->bwrite(bdev, buf, pba, pb_cnt * cnt);
252 int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off,
253 const void *buf, uint32_t len)
261 const uint8_t *p = (void *)buf;
263 ext4_assert(bdev && buf);
265 if (!(bdev->flags & EXT4_BDEV_INITIALIZED))
268 block_idx = off / bdev->ph_bsize;
269 block_end = block_idx + len / bdev->ph_bsize;
271 if (!(block_end < bdev->ph_bcnt))
272 return EINVAL; /*Ups. Out of range operation*/
274 /*OK lets deal with the first possible unaligned block*/
275 unalg = (off & (bdev->ph_bsize - 1));
278 uint32_t wlen = (bdev->ph_bsize - unalg) > len
280 : (bdev->ph_bsize - unalg);
282 r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1);
287 memcpy(bdev->ph_bbuf + unalg, p, wlen);
289 r = bdev->bwrite(bdev, bdev->ph_bbuf, block_idx, 1);
299 blen = len / bdev->ph_bsize;
300 r = bdev->bwrite(bdev, p, block_idx, blen);
305 p += bdev->ph_bsize * blen;
306 len -= bdev->ph_bsize * blen;
312 r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1);
316 memcpy(bdev->ph_bbuf, p, len);
318 r = bdev->bwrite(bdev, bdev->ph_bbuf, block_idx, 1);
327 int ext4_block_readbytes(struct ext4_blockdev *bdev, uint64_t off, void *buf,
336 uint8_t *p = (void *)buf;
338 ext4_assert(bdev && buf);
340 if (!(bdev->flags & EXT4_BDEV_INITIALIZED))
343 block_idx = off / bdev->ph_bsize;
344 block_end = block_idx + len / bdev->ph_bsize;
346 if (!(block_end < bdev->ph_bcnt))
347 return EINVAL; /*Ups. Out of range operation*/
349 /*OK lets deal with the first possible unaligned block*/
350 unalg = (off & (bdev->ph_bsize - 1));
353 uint32_t rlen = (bdev->ph_bsize - unalg) > len
355 : (bdev->ph_bsize - unalg);
357 r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1);
361 memcpy(p, bdev->ph_bbuf + unalg, rlen);
369 blen = len / bdev->ph_bsize;
371 r = bdev->bread(bdev, p, block_idx, blen);
376 p += bdev->ph_bsize * blen;
377 len -= bdev->ph_bsize * blen;
383 r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1);
387 memcpy(p, bdev->ph_bbuf, len);
393 int ext4_block_cache_write_back(struct ext4_blockdev *bdev, uint8_t on_off)
399 bdev->cache_write_back++;
401 if (!on_off && bdev->cache_write_back)
402 bdev->cache_write_back--;
404 /*Flush all delayed cache blocks*/
405 if (!bdev->cache_write_back) {
406 for (i = 0; i < bdev->bc->cnt; ++i) {
408 /*Check if buffer free was delayed.*/
409 if (!bdev->bc->free_delay[i])
412 /*Check reference counter.*/
413 if (bdev->bc->refctr[i])
416 /*Buffer free was delayed and have no reference. Flush
418 r = ext4_blocks_set_direct(
419 bdev, bdev->bc->data + bdev->bc->itemsize * i,
420 bdev->bc->lba[i], 1);
424 /*No delayed anymore*/
425 bdev->bc->free_delay[i] = 0;
427 /*Reduce reference counter*/
428 bdev->bc->ref_blocks--;