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->bdif->open &&
55 /*Low level block init*/
56 rc = bdev->bdif->open(bdev);
60 bdev->bdif->ph_flags |= EXT4_BDEV_INITIALIZED;
65 int ext4_block_bind_bcache(struct ext4_blockdev *bdev, struct ext4_bcache *bc)
67 ext4_assert(bdev && bc);
73 void ext4_block_set_lb_size(struct ext4_blockdev *bdev, uint64_t lb_bsize)
75 /*Logical block size has to be multiply of physical */
76 ext4_assert(!(lb_bsize % bdev->bdif->ph_bsize));
78 bdev->lg_bsize = lb_bsize;
79 bdev->lg_bcnt = (bdev->bdif->ph_bcnt * bdev->bdif->ph_bsize) / lb_bsize;
82 int ext4_block_fini(struct ext4_blockdev *bdev)
86 bdev->bdif->ph_flags &= ~(EXT4_BDEV_INITIALIZED);
88 /*Low level block fini*/
89 return bdev->bdif->close(bdev);
92 int ext4_block_flush_buf(struct ext4_blockdev *bdev, struct ext4_buf *buf)
95 struct ext4_bcache *bc = bdev->bc;
96 /*Only flushing unreferenced buffer is allowed.*/
97 ext4_assert(!buf->refctr);
99 if (ext4_bcache_test_flag(buf, BC_DIRTY)) {
100 r = ext4_blocks_set_direct(bdev, buf->data, buf->lba, 1);
104 buf->end_write(bc, buf, r, buf->end_write_arg);
109 ext4_bcache_remove_dirty_node(bc, buf);
110 ext4_bcache_clear_flag(buf, BC_DIRTY);
112 buf->end_write(bc, buf, r, buf->end_write_arg);
118 int ext4_block_cache_shake(struct ext4_blockdev *bdev)
120 struct ext4_buf *buf;
121 while (!RB_EMPTY(&bdev->bc->lru_root) &&
122 ext4_bcache_is_full(bdev->bc)) {
124 buf = ext4_buf_lowest_lru(bdev->bc);
126 if (ext4_bcache_test_flag(buf, BC_DIRTY)) {
127 int r = ext4_block_flush_buf(bdev, buf);
133 ext4_bcache_drop_buf(bdev->bc, buf);
138 int ext4_block_get_noread(struct ext4_blockdev *bdev, struct ext4_block *b,
144 ext4_assert(bdev && b);
146 if (!(bdev->bdif->ph_flags & EXT4_BDEV_INITIALIZED))
149 if (!(lba < bdev->lg_bcnt))
154 /*If cache is full we have to (flush and) drop it anyway :(*/
155 r = ext4_block_cache_shake(bdev);
159 r = ext4_bcache_alloc(bdev->bc, b, &is_new);
169 int ext4_block_get(struct ext4_blockdev *bdev, struct ext4_block *b,
172 int r = ext4_block_get_noread(bdev, b, lba);
176 if (ext4_bcache_test_flag(b->buf, BC_UPTODATE)) {
177 /* Data in the cache is up-to-date.
178 * Reading from physical device is not required */
182 r = ext4_blocks_get_direct(bdev, b->data, lba, 1);
184 ext4_bcache_free(bdev->bc, b);
189 /* Mark buffer up-to-date, since
190 * fresh data is read from physical device just now. */
191 ext4_bcache_set_flag(b->buf, BC_UPTODATE);
195 int ext4_block_set(struct ext4_blockdev *bdev, struct ext4_block *b)
197 ext4_assert(bdev && b);
200 if (!(bdev->bdif->ph_flags & EXT4_BDEV_INITIALIZED))
203 return ext4_bcache_free(bdev->bc, b);
206 int ext4_blocks_get_direct(struct ext4_blockdev *bdev, void *buf, uint64_t lba,
212 ext4_assert(bdev && buf);
214 pba = (lba * bdev->lg_bsize) / bdev->bdif->ph_bsize;
215 pba += bdev->ph_blk_offset;
216 pb_cnt = bdev->lg_bsize / bdev->bdif->ph_bsize;
219 return bdev->bdif->bread(bdev, buf, pba, pb_cnt * cnt);
222 int ext4_blocks_set_direct(struct ext4_blockdev *bdev, const void *buf,
223 uint64_t lba, uint32_t cnt)
228 ext4_assert(bdev && buf);
230 pba = (lba * bdev->lg_bsize) / bdev->bdif->ph_bsize;
231 pba += bdev->ph_blk_offset;
232 pb_cnt = bdev->lg_bsize / bdev->bdif->ph_bsize;
236 return bdev->bdif->bwrite(bdev, buf, pba, pb_cnt * cnt);
239 int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off,
240 const void *buf, uint32_t len)
248 const uint8_t *p = (void *)buf;
250 ext4_assert(bdev && buf);
252 if (!(bdev->bdif->ph_flags & EXT4_BDEV_INITIALIZED))
255 block_idx = (off / bdev->bdif->ph_bsize) + bdev->ph_blk_offset;
256 block_end = block_idx + len / bdev->bdif->ph_bsize;
258 if (!(block_end < bdev->bdif->ph_bcnt))
259 return EINVAL; /*Ups. Out of range operation*/
261 /*OK lets deal with the first possible unaligned block*/
262 unalg = (off & (bdev->bdif->ph_bsize - 1));
265 uint32_t wlen = (bdev->bdif->ph_bsize - unalg) > len
267 : (bdev->bdif->ph_bsize - unalg);
269 r = bdev->bdif->bread(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
273 memcpy(bdev->bdif->ph_bbuf + unalg, p, wlen);
275 r = bdev->bdif->bwrite(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
285 blen = len / bdev->bdif->ph_bsize;
286 r = bdev->bdif->bwrite(bdev, p, block_idx, blen);
290 p += bdev->bdif->ph_bsize * blen;
291 len -= bdev->bdif->ph_bsize * blen;
297 r = bdev->bdif->bread(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
301 memcpy(bdev->bdif->ph_bbuf, p, len);
303 r = bdev->bdif->bwrite(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
311 int ext4_block_readbytes(struct ext4_blockdev *bdev, uint64_t off, void *buf,
320 uint8_t *p = (void *)buf;
322 ext4_assert(bdev && buf);
324 if (!(bdev->bdif->ph_flags & EXT4_BDEV_INITIALIZED))
327 block_idx = (off / bdev->bdif->ph_bsize) + bdev->ph_blk_offset;
328 block_end = block_idx + len / bdev->bdif->ph_bsize;
330 if (!(block_end < bdev->bdif->ph_bcnt))
331 return EINVAL; /*Ups. Out of range operation*/
333 /*OK lets deal with the first possible unaligned block*/
334 unalg = (off & (bdev->bdif->ph_bsize - 1));
337 uint32_t rlen = (bdev->bdif->ph_bsize - unalg) > len
339 : (bdev->bdif->ph_bsize - unalg);
341 r = bdev->bdif->bread(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
345 memcpy(p, bdev->bdif->ph_bbuf + unalg, rlen);
353 blen = len / bdev->bdif->ph_bsize;
355 r = bdev->bdif->bread(bdev, p, block_idx, blen);
359 p += bdev->bdif->ph_bsize * blen;
360 len -= bdev->bdif->ph_bsize * blen;
366 r = bdev->bdif->bread(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
370 memcpy(p, bdev->bdif->ph_bbuf, len);
376 int ext4_block_cache_write_back(struct ext4_blockdev *bdev, uint8_t on_off)
379 struct ext4_buf *buf;
382 bdev->cache_write_back++;
384 if (!on_off && bdev->cache_write_back)
385 bdev->cache_write_back--;
387 if (bdev->cache_write_back)
390 /*Flush all delayed cache blocks*/
391 while (!SLIST_EMPTY(&bdev->bc->dirty_list)) {
393 buf = SLIST_FIRST(&bdev->bc->dirty_list);
395 r = ext4_block_flush_buf(bdev, buf);