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);
89 ext4_block_flush_buf(struct ext4_blockdev *bdev, struct ext4_buf *buf)
92 struct ext4_bcache *bc = bdev->bc;
93 /*Only flushing unreferenced buffer is allowed.*/
94 ext4_assert(!buf->refctr);
96 if (ext4_bcache_test_flag(buf, BC_DIRTY)) {
97 r = ext4_blocks_set_direct(bdev, buf->data, buf->lba, 1);
101 SLIST_REMOVE(&bc->dirty_list,
105 ext4_bcache_clear_flag(buf, BC_DIRTY);
110 int ext4_block_cache_shake(struct ext4_blockdev *bdev)
112 struct ext4_buf *buf;
113 while (!RB_EMPTY(&bdev->bc->lru_root) &&
114 ext4_bcache_is_full(bdev->bc)) {
116 buf = ext4_buf_lowest_lru(bdev->bc);
118 if (ext4_bcache_test_flag(buf, BC_DIRTY)) {
119 int r = ext4_block_flush_buf(bdev, buf);
125 ext4_bcache_drop_buf(bdev->bc, buf);
130 int ext4_block_get_noread(struct ext4_blockdev *bdev, struct ext4_block *b,
136 ext4_assert(bdev && b);
138 if (!(bdev->flags & EXT4_BDEV_INITIALIZED))
141 if (!(lba < bdev->lg_bcnt))
147 /*If cache is full we have to (flush and) drop it anyway :(*/
148 r = ext4_block_cache_shake(bdev);
152 r = ext4_bcache_alloc(bdev->bc, b, &is_new);
162 int ext4_block_get(struct ext4_blockdev *bdev, struct ext4_block *b,
167 int r = ext4_block_get_noread(bdev, b, lba);
172 /* Data in the cache is up-to-date.
173 * Reading from physical device is not required */
177 pba = (lba * bdev->lg_bsize) / bdev->ph_bsize;
178 pb_cnt = bdev->lg_bsize / bdev->ph_bsize;
180 r = bdev->bread(bdev, b->data, pba, pb_cnt);
183 ext4_bcache_free(bdev->bc, b);
188 /* Mark buffer up-to-date, since
189 * fresh data is read from physical device just now. */
190 ext4_bcache_set_flag(b->buf, BC_UPTODATE);
196 int ext4_block_set(struct ext4_blockdev *bdev, struct ext4_block *b)
202 ext4_assert(bdev && b);
205 if (!(bdev->flags & EXT4_BDEV_INITIALIZED))
208 /*Free cache delay mode*/
209 if (bdev->cache_write_back) {
211 /*Free cache block and mark as free delayed*/
212 return ext4_bcache_free(bdev->bc, b);
215 if (b->buf->refctr > 1)
216 return ext4_bcache_free(bdev->bc, b);
218 /*We handle the dirty flag ourselves.*/
219 if (ext4_bcache_test_flag(b->buf, BC_DIRTY) || b->dirty) {
221 ext4_bcache_set_flag(b->buf, BC_UPTODATE);
223 pba = (b->lb_id * bdev->lg_bsize) / bdev->ph_bsize;
224 pb_cnt = bdev->lg_bsize / bdev->ph_bsize;
226 r = bdev->bwrite(bdev, b->data, pba, pb_cnt);
227 ext4_bcache_clear_flag(b->buf, BC_DIRTY);
230 ext4_bcache_free(bdev->bc, b);
237 ext4_bcache_free(bdev->bc, b);
241 int ext4_blocks_get_direct(struct ext4_blockdev *bdev, void *buf, uint64_t lba,
247 ext4_assert(bdev && buf);
249 pba = (lba * bdev->lg_bsize) / bdev->ph_bsize;
250 pb_cnt = bdev->lg_bsize / bdev->ph_bsize;
253 return bdev->bread(bdev, buf, pba, pb_cnt * cnt);
256 int ext4_blocks_set_direct(struct ext4_blockdev *bdev, const void *buf,
257 uint64_t lba, uint32_t cnt)
262 ext4_assert(bdev && buf);
264 pba = (lba * bdev->lg_bsize) / bdev->ph_bsize;
265 pb_cnt = bdev->lg_bsize / bdev->ph_bsize;
269 return bdev->bwrite(bdev, buf, pba, pb_cnt * cnt);
272 int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off,
273 const void *buf, uint32_t len)
281 const uint8_t *p = (void *)buf;
283 ext4_assert(bdev && buf);
285 if (!(bdev->flags & EXT4_BDEV_INITIALIZED))
288 block_idx = off / bdev->ph_bsize;
289 block_end = block_idx + len / bdev->ph_bsize;
291 if (!(block_end < bdev->ph_bcnt))
292 return EINVAL; /*Ups. Out of range operation*/
294 /*OK lets deal with the first possible unaligned block*/
295 unalg = (off & (bdev->ph_bsize - 1));
298 uint32_t wlen = (bdev->ph_bsize - unalg) > len
300 : (bdev->ph_bsize - unalg);
302 r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1);
306 memcpy(bdev->ph_bbuf + unalg, p, wlen);
308 r = bdev->bwrite(bdev, bdev->ph_bbuf, block_idx, 1);
318 blen = len / bdev->ph_bsize;
319 r = bdev->bwrite(bdev, p, block_idx, blen);
323 p += bdev->ph_bsize * blen;
324 len -= bdev->ph_bsize * blen;
330 r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1);
334 memcpy(bdev->ph_bbuf, p, len);
336 r = bdev->bwrite(bdev, bdev->ph_bbuf, block_idx, 1);
344 int ext4_block_readbytes(struct ext4_blockdev *bdev, uint64_t off, void *buf,
353 uint8_t *p = (void *)buf;
355 ext4_assert(bdev && buf);
357 if (!(bdev->flags & EXT4_BDEV_INITIALIZED))
360 block_idx = off / bdev->ph_bsize;
361 block_end = block_idx + len / bdev->ph_bsize;
363 if (!(block_end < bdev->ph_bcnt))
364 return EINVAL; /*Ups. Out of range operation*/
366 /*OK lets deal with the first possible unaligned block*/
367 unalg = (off & (bdev->ph_bsize - 1));
370 uint32_t rlen = (bdev->ph_bsize - unalg) > len
372 : (bdev->ph_bsize - unalg);
374 r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1);
378 memcpy(p, bdev->ph_bbuf + unalg, rlen);
386 blen = len / bdev->ph_bsize;
388 r = bdev->bread(bdev, p, block_idx, blen);
392 p += bdev->ph_bsize * blen;
393 len -= bdev->ph_bsize * blen;
399 r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1);
403 memcpy(p, bdev->ph_bbuf, len);
409 int ext4_block_cache_write_back(struct ext4_blockdev *bdev, uint8_t on_off)
412 struct ext4_buf *buf;
415 bdev->cache_write_back++;
417 if (!on_off && bdev->cache_write_back)
418 bdev->cache_write_back--;
420 if (bdev->cache_write_back)
423 /*Flush all delayed cache blocks*/
424 while (!SLIST_EMPTY(&bdev->bc->dirty_list)) {
426 buf = SLIST_FIRST(&bdev->bc->dirty_list);
428 r = ext4_block_flush_buf(bdev, buf);