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 static void ext4_bdif_lock(struct ext4_blockdev *bdev)
47 if (!bdev->bdif->lock)
50 int r = bdev->bdif->lock(bdev);
51 ext4_assert(r == EOK);
54 static void ext4_bdif_unlock(struct ext4_blockdev *bdev)
56 if (!bdev->bdif->unlock)
59 int r = bdev->bdif->unlock(bdev);
60 ext4_assert(r == EOK);
63 static int ext4_bdif_bread(struct ext4_blockdev *bdev, void *buf,
64 uint64_t blk_id, uint32_t blk_cnt)
67 int r = bdev->bdif->bread(bdev, buf, blk_id, blk_cnt);
68 bdev->bdif->bread_ctr++;
69 ext4_bdif_unlock(bdev);
73 static int ext4_bdif_bwrite(struct ext4_blockdev *bdev, const void *buf,
74 uint64_t blk_id, uint32_t blk_cnt)
77 int r = bdev->bdif->bwrite(bdev, buf, blk_id, blk_cnt);
78 bdev->bdif->bwrite_ctr++;
79 ext4_bdif_unlock(bdev);
83 int ext4_block_init(struct ext4_blockdev *bdev)
87 ext4_assert(bdev->bdif);
88 ext4_assert(bdev->bdif->open &&
93 if (bdev->bdif->ph_refctr) {
94 bdev->bdif->ph_refctr++;
98 /*Low level block init*/
99 rc = bdev->bdif->open(bdev);
103 bdev->bdif->ph_refctr = 1;
107 int ext4_block_bind_bcache(struct ext4_blockdev *bdev, struct ext4_bcache *bc)
109 ext4_assert(bdev && bc);
115 void ext4_block_set_lb_size(struct ext4_blockdev *bdev, uint64_t lb_bsize)
117 /*Logical block size has to be multiply of physical */
118 ext4_assert(!(lb_bsize % bdev->bdif->ph_bsize));
120 bdev->lg_bsize = lb_bsize;
121 bdev->lg_bcnt = bdev->part_size / lb_bsize;
124 int ext4_block_fini(struct ext4_blockdev *bdev)
128 if (!bdev->bdif->ph_refctr)
131 bdev->bdif->ph_refctr--;
132 if (bdev->bdif->ph_refctr)
135 /*Low level block fini*/
136 return bdev->bdif->close(bdev);
139 int ext4_block_flush_buf(struct ext4_blockdev *bdev, struct ext4_buf *buf)
142 struct ext4_bcache *bc = bdev->bc;
144 if (ext4_bcache_test_flag(buf, BC_DIRTY) &&
145 ext4_bcache_test_flag(buf, BC_UPTODATE)) {
146 r = ext4_blocks_set_direct(bdev, buf->data, buf->lba, 1);
148 if (buf->end_write) {
149 bc->dont_shake = true;
150 buf->end_write(bc, buf, r, buf->end_write_arg);
151 bc->dont_shake = false;
157 ext4_bcache_remove_dirty_node(bc, buf);
158 ext4_bcache_clear_flag(buf, BC_DIRTY);
159 if (buf->end_write) {
160 bc->dont_shake = true;
161 buf->end_write(bc, buf, r, buf->end_write_arg);
162 bc->dont_shake = false;
168 int ext4_block_flush_lba(struct ext4_blockdev *bdev, uint64_t lba)
171 struct ext4_buf *buf;
173 buf = ext4_bcache_find_get(bdev->bc, &b, lba);
175 r = ext4_block_flush_buf(bdev, buf);
176 ext4_bcache_free(bdev->bc, &b);
181 int ext4_block_cache_shake(struct ext4_blockdev *bdev)
184 struct ext4_buf *buf;
185 if (bdev->bc->dont_shake)
188 while (!RB_EMPTY(&bdev->bc->lru_root) &&
189 ext4_bcache_is_full(bdev->bc)) {
191 buf = ext4_buf_lowest_lru(bdev->bc);
193 if (ext4_bcache_test_flag(buf, BC_DIRTY)) {
194 r = ext4_block_flush_buf(bdev, buf);
200 ext4_bcache_drop_buf(bdev->bc, buf);
205 int ext4_block_get_noread(struct ext4_blockdev *bdev, struct ext4_block *b,
211 ext4_assert(bdev && b);
213 if (!bdev->bdif->ph_refctr)
216 if (!(lba < bdev->lg_bcnt))
221 /*If cache is full we have to (flush and) drop it anyway :(*/
222 r = ext4_block_cache_shake(bdev);
226 r = ext4_bcache_alloc(bdev->bc, b, &is_new);
236 int ext4_block_get(struct ext4_blockdev *bdev, struct ext4_block *b,
239 int r = ext4_block_get_noread(bdev, b, lba);
243 if (ext4_bcache_test_flag(b->buf, BC_UPTODATE)) {
244 /* Data in the cache is up-to-date.
245 * Reading from physical device is not required */
249 r = ext4_blocks_get_direct(bdev, b->data, lba, 1);
251 ext4_bcache_free(bdev->bc, b);
256 /* Mark buffer up-to-date, since
257 * fresh data is read from physical device just now. */
258 ext4_bcache_set_flag(b->buf, BC_UPTODATE);
262 int ext4_block_set(struct ext4_blockdev *bdev, struct ext4_block *b)
264 ext4_assert(bdev && b);
267 if (!bdev->bdif->ph_refctr)
270 return ext4_bcache_free(bdev->bc, b);
273 int ext4_blocks_get_direct(struct ext4_blockdev *bdev, void *buf, uint64_t lba,
279 ext4_assert(bdev && buf);
281 pba = (lba * bdev->lg_bsize + bdev->part_offset) / bdev->bdif->ph_bsize;
282 pb_cnt = bdev->lg_bsize / bdev->bdif->ph_bsize;
284 return ext4_bdif_bread(bdev, buf, pba, pb_cnt * cnt);
287 int ext4_blocks_set_direct(struct ext4_blockdev *bdev, const void *buf,
288 uint64_t lba, uint32_t cnt)
293 ext4_assert(bdev && buf);
295 pba = (lba * bdev->lg_bsize + bdev->part_offset) / bdev->bdif->ph_bsize;
296 pb_cnt = bdev->lg_bsize / bdev->bdif->ph_bsize;
298 return ext4_bdif_bwrite(bdev, buf, pba, pb_cnt * cnt);
301 int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off,
302 const void *buf, uint32_t len)
309 const uint8_t *p = (void *)buf;
311 ext4_assert(bdev && buf);
313 if (!bdev->bdif->ph_refctr)
316 if (off + len > bdev->part_size)
317 return EINVAL; /*Ups. Out of range operation*/
319 block_idx = ((off + bdev->part_offset) / bdev->bdif->ph_bsize);
321 /*OK lets deal with the first possible unaligned block*/
322 unalg = (off & (bdev->bdif->ph_bsize - 1));
325 uint32_t wlen = (bdev->bdif->ph_bsize - unalg) > len
327 : (bdev->bdif->ph_bsize - unalg);
329 r = ext4_bdif_bread(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
333 memcpy(bdev->bdif->ph_bbuf + unalg, p, wlen);
334 r = ext4_bdif_bwrite(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
344 blen = len / bdev->bdif->ph_bsize;
345 r = ext4_bdif_bwrite(bdev, p, block_idx, blen);
349 p += bdev->bdif->ph_bsize * blen;
350 len -= bdev->bdif->ph_bsize * blen;
356 r = ext4_bdif_bread(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
360 memcpy(bdev->bdif->ph_bbuf, p, len);
361 r = ext4_bdif_bwrite(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
369 int ext4_block_readbytes(struct ext4_blockdev *bdev, uint64_t off, void *buf,
377 uint8_t *p = (void *)buf;
379 ext4_assert(bdev && buf);
381 if (!bdev->bdif->ph_refctr)
384 if (off + len > bdev->part_size)
385 return EINVAL; /*Ups. Out of range operation*/
387 block_idx = ((off + bdev->part_offset) / bdev->bdif->ph_bsize);
389 /*OK lets deal with the first possible unaligned block*/
390 unalg = (off & (bdev->bdif->ph_bsize - 1));
393 uint32_t rlen = (bdev->bdif->ph_bsize - unalg) > len
395 : (bdev->bdif->ph_bsize - unalg);
397 r = ext4_bdif_bread(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
401 memcpy(p, bdev->bdif->ph_bbuf + unalg, rlen);
409 blen = len / bdev->bdif->ph_bsize;
411 r = ext4_bdif_bread(bdev, p, block_idx, blen);
415 p += bdev->bdif->ph_bsize * blen;
416 len -= bdev->bdif->ph_bsize * blen;
422 r = ext4_bdif_bread(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
426 memcpy(p, bdev->bdif->ph_bbuf, len);
432 int ext4_block_cache_flush(struct ext4_blockdev *bdev)
434 while (!SLIST_EMPTY(&bdev->bc->dirty_list)) {
436 struct ext4_buf *buf = SLIST_FIRST(&bdev->bc->dirty_list);
438 r = ext4_block_flush_buf(bdev, buf);
446 int ext4_block_cache_write_back(struct ext4_blockdev *bdev, uint8_t on_off)
449 bdev->cache_write_back++;
451 if (!on_off && bdev->cache_write_back)
452 bdev->cache_write_back--;
454 if (bdev->cache_write_back)
457 /*Flush data in all delayed cache blocks*/
458 return ext4_block_cache_flush(bdev);