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_types.h>
39 #include <ext4_misc.h>
40 #include <ext4_errno.h>
41 #include <ext4_debug.h>
43 #include <ext4_blockdev.h>
45 #include <ext4_journal.h>
50 static void ext4_bdif_lock(struct ext4_blockdev *bdev)
52 if (!bdev->bdif->lock)
55 int r = bdev->bdif->lock(bdev);
56 ext4_assert(r == EOK);
59 static void ext4_bdif_unlock(struct ext4_blockdev *bdev)
61 if (!bdev->bdif->unlock)
64 int r = bdev->bdif->unlock(bdev);
65 ext4_assert(r == EOK);
68 static int ext4_bdif_bread(struct ext4_blockdev *bdev, void *buf,
69 uint64_t blk_id, uint32_t blk_cnt)
72 int r = bdev->bdif->bread(bdev, buf, blk_id, blk_cnt);
73 bdev->bdif->bread_ctr++;
74 ext4_bdif_unlock(bdev);
78 static int ext4_bdif_bwrite(struct ext4_blockdev *bdev, const void *buf,
79 uint64_t blk_id, uint32_t blk_cnt)
81 ext4_dbg(DEBUG_BLOCKDEV, DBG_NONE "Writing blk_id=%lu cnt=%d", blk_id, blk_cnt);
83 int r = bdev->bdif->bwrite(bdev, buf, blk_id, blk_cnt);
84 bdev->bdif->bwrite_ctr++;
85 ext4_bdif_unlock(bdev);
86 ext4_dbg(DEBUG_BLOCKDEV, DBG_NONE "Result was %d", r);
90 int ext4_block_init(struct ext4_blockdev *bdev)
94 ext4_assert(bdev->bdif);
95 ext4_assert(bdev->bdif->open &&
100 if (bdev->bdif->ph_refctr) {
101 bdev->bdif->ph_refctr++;
105 /*Low level block init*/
106 rc = bdev->bdif->open(bdev);
110 bdev->bdif->ph_refctr = 1;
114 int ext4_block_bind_bcache(struct ext4_blockdev *bdev, struct ext4_bcache *bc)
116 ext4_assert(bdev && bc);
122 void ext4_block_set_lb_size(struct ext4_blockdev *bdev, uint32_t lb_bsize)
124 /*Logical block size has to be multiply of physical */
125 ext4_assert(!(lb_bsize % bdev->bdif->ph_bsize));
127 bdev->lg_bsize = lb_bsize;
128 bdev->lg_bcnt = bdev->part_size / lb_bsize;
131 int ext4_block_fini(struct ext4_blockdev *bdev)
135 if (!bdev->bdif->ph_refctr)
138 bdev->bdif->ph_refctr--;
139 if (bdev->bdif->ph_refctr)
142 /*Low level block fini*/
143 return bdev->bdif->close(bdev);
146 int ext4_block_flush_buf(struct ext4_blockdev *bdev, struct ext4_buf *buf)
149 struct ext4_bcache *bc = bdev->bc;
151 if (ext4_bcache_test_flag(buf, BC_DIRTY) &&
152 ext4_bcache_test_flag(buf, BC_UPTODATE)) {
153 r = ext4_blocks_set_direct(bdev, buf->data, buf->lba, 1);
155 if (buf->end_write) {
156 bc->dont_shake = true;
157 buf->end_write(bc, buf, r, buf->end_write_arg);
158 bc->dont_shake = false;
164 ext4_bcache_remove_dirty_node(bc, buf);
165 ext4_bcache_clear_flag(buf, BC_DIRTY);
166 if (buf->end_write) {
167 bc->dont_shake = true;
168 buf->end_write(bc, buf, r, buf->end_write_arg);
169 bc->dont_shake = false;
175 int ext4_block_flush_lba(struct ext4_blockdev *bdev, uint64_t lba)
178 struct ext4_buf *buf;
180 buf = ext4_bcache_find_get(bdev->bc, &b, lba);
182 r = ext4_block_flush_buf(bdev, buf);
183 ext4_bcache_free(bdev->bc, &b);
188 int ext4_block_cache_shake(struct ext4_blockdev *bdev)
191 struct ext4_buf *buf;
192 if (bdev->bc->dont_shake)
195 bdev->bc->dont_shake = true;
197 while (!RB_EMPTY(&bdev->bc->lru_root) &&
198 ext4_bcache_is_full(bdev->bc)) {
200 buf = ext4_buf_lowest_lru(bdev->bc);
202 if (ext4_bcache_test_flag(buf, BC_DIRTY)) {
203 r = ext4_block_flush_buf(bdev, buf);
209 ext4_bcache_drop_buf(bdev->bc, buf);
211 bdev->bc->dont_shake = false;
215 int ext4_block_get_noread(struct ext4_blockdev *bdev, struct ext4_block *b,
221 ext4_assert(bdev && b);
223 if (!bdev->bdif->ph_refctr)
226 if (!(lba < bdev->lg_bcnt))
231 /*If cache is full we have to (flush and) drop it anyway :(*/
232 r = ext4_block_cache_shake(bdev);
236 r = ext4_bcache_alloc(bdev->bc, b, &is_new);
246 int ext4_block_get(struct ext4_blockdev *bdev, struct ext4_block *b,
249 int r = ext4_block_get_noread(bdev, b, lba);
253 if (ext4_bcache_test_flag(b->buf, BC_UPTODATE)) {
254 /* Data in the cache is up-to-date.
255 * Reading from physical device is not required */
259 r = ext4_blocks_get_direct(bdev, b->data, lba, 1);
261 ext4_bcache_free(bdev->bc, b);
266 /* Mark buffer up-to-date, since
267 * fresh data is read from physical device just now. */
268 ext4_bcache_set_flag(b->buf, BC_UPTODATE);
272 int ext4_block_set(struct ext4_blockdev *bdev, struct ext4_block *b)
274 ext4_assert(bdev && b);
277 if (!bdev->bdif->ph_refctr)
280 return ext4_bcache_free(bdev->bc, b);
283 int ext4_blocks_get_direct(struct ext4_blockdev *bdev, void *buf, uint64_t lba,
289 ext4_assert(bdev && buf);
291 pba = (lba * bdev->lg_bsize + bdev->part_offset) / bdev->bdif->ph_bsize;
292 pb_cnt = bdev->lg_bsize / bdev->bdif->ph_bsize;
294 return ext4_bdif_bread(bdev, buf, pba, pb_cnt * cnt);
297 int ext4_blocks_set_direct(struct ext4_blockdev *bdev, const void *buf,
298 uint64_t lba, uint32_t cnt)
303 ext4_assert(bdev && buf);
305 pba = (lba * bdev->lg_bsize + bdev->part_offset) / bdev->bdif->ph_bsize;
306 pb_cnt = bdev->lg_bsize / bdev->bdif->ph_bsize;
308 return ext4_bdif_bwrite(bdev, buf, pba, pb_cnt * cnt);
311 int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off,
312 const void *buf, uint32_t len)
319 const uint8_t *p = (void *)buf;
321 ext4_assert(bdev && buf);
323 if (!bdev->bdif->ph_refctr)
326 if (off + len > bdev->part_size)
327 return EINVAL; /*Ups. Out of range operation*/
329 block_idx = ((off + bdev->part_offset) / bdev->bdif->ph_bsize);
331 /*OK lets deal with the first possible unaligned block*/
332 unalg = (off & (bdev->bdif->ph_bsize - 1));
335 uint32_t wlen = (bdev->bdif->ph_bsize - unalg) > len
337 : (bdev->bdif->ph_bsize - unalg);
339 r = ext4_bdif_bread(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
343 memcpy(bdev->bdif->ph_bbuf + unalg, p, wlen);
344 r = ext4_bdif_bwrite(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
354 blen = len / bdev->bdif->ph_bsize;
356 r = ext4_bdif_bwrite(bdev, p, block_idx, blen);
360 p += bdev->bdif->ph_bsize * blen;
361 len -= bdev->bdif->ph_bsize * blen;
368 r = ext4_bdif_bread(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
372 memcpy(bdev->bdif->ph_bbuf, p, len);
373 r = ext4_bdif_bwrite(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
381 int ext4_block_readbytes(struct ext4_blockdev *bdev, uint64_t off, void *buf,
389 uint8_t *p = (void *)buf;
391 ext4_assert(bdev && buf);
393 if (!bdev->bdif->ph_refctr)
396 if (off + len > bdev->part_size)
397 return EINVAL; /*Ups. Out of range operation*/
399 block_idx = ((off + bdev->part_offset) / bdev->bdif->ph_bsize);
401 /*OK lets deal with the first possible unaligned block*/
402 unalg = (off & (bdev->bdif->ph_bsize - 1));
405 uint32_t rlen = (bdev->bdif->ph_bsize - unalg) > len
407 : (bdev->bdif->ph_bsize - unalg);
409 r = ext4_bdif_bread(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
413 memcpy(p, bdev->bdif->ph_bbuf + unalg, rlen);
421 blen = len / bdev->bdif->ph_bsize;
424 r = ext4_bdif_bread(bdev, p, block_idx, blen);
428 p += bdev->bdif->ph_bsize * blen;
429 len -= bdev->bdif->ph_bsize * blen;
436 r = ext4_bdif_bread(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
440 memcpy(p, bdev->bdif->ph_bbuf, len);
446 int ext4_block_cache_flush(struct ext4_blockdev *bdev)
448 while (!SLIST_EMPTY(&bdev->bc->dirty_list)) {
450 struct ext4_buf *buf = SLIST_FIRST(&bdev->bc->dirty_list);
452 r = ext4_block_flush_buf(bdev, buf);
460 int ext4_block_cache_write_back(struct ext4_blockdev *bdev, uint8_t on_off)
463 bdev->cache_write_back++;
465 if (!on_off && bdev->cache_write_back)
466 bdev->cache_write_back--;
468 if (bdev->cache_write_back)
471 /*Flush data in all delayed cache blocks*/
472 return ext4_block_cache_flush(bdev);