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)
82 int r = bdev->bdif->bwrite(bdev, buf, blk_id, blk_cnt);
83 bdev->bdif->bwrite_ctr++;
84 ext4_bdif_unlock(bdev);
88 int ext4_block_init(struct ext4_blockdev *bdev)
92 ext4_assert(bdev->bdif);
93 ext4_assert(bdev->bdif->open &&
98 if (bdev->bdif->ph_refctr) {
99 bdev->bdif->ph_refctr++;
103 /*Low level block init*/
104 rc = bdev->bdif->open(bdev);
108 bdev->bdif->ph_refctr = 1;
112 int ext4_block_bind_bcache(struct ext4_blockdev *bdev, struct ext4_bcache *bc)
114 ext4_assert(bdev && bc);
120 void ext4_block_set_lb_size(struct ext4_blockdev *bdev, uint32_t lb_bsize)
122 /*Logical block size has to be multiply of physical */
123 ext4_assert(!(lb_bsize % bdev->bdif->ph_bsize));
125 bdev->lg_bsize = lb_bsize;
126 bdev->lg_bcnt = bdev->part_size / lb_bsize;
129 int ext4_block_fini(struct ext4_blockdev *bdev)
133 if (!bdev->bdif->ph_refctr)
136 bdev->bdif->ph_refctr--;
137 if (bdev->bdif->ph_refctr)
140 /*Low level block fini*/
141 return bdev->bdif->close(bdev);
144 int ext4_block_flush_buf(struct ext4_blockdev *bdev, struct ext4_buf *buf)
147 struct ext4_bcache *bc = bdev->bc;
149 if (ext4_bcache_test_flag(buf, BC_DIRTY) &&
150 ext4_bcache_test_flag(buf, BC_UPTODATE)) {
151 r = ext4_blocks_set_direct(bdev, buf->data, buf->lba, 1);
153 if (buf->end_write) {
154 bc->dont_shake = true;
155 buf->end_write(bc, buf, r, buf->end_write_arg);
156 bc->dont_shake = false;
162 ext4_bcache_remove_dirty_node(bc, buf);
163 ext4_bcache_clear_flag(buf, BC_DIRTY);
164 if (buf->end_write) {
165 bc->dont_shake = true;
166 buf->end_write(bc, buf, r, buf->end_write_arg);
167 bc->dont_shake = false;
173 int ext4_block_flush_lba(struct ext4_blockdev *bdev, uint64_t lba)
176 struct ext4_buf *buf;
178 buf = ext4_bcache_find_get(bdev->bc, &b, lba);
180 r = ext4_block_flush_buf(bdev, buf);
181 ext4_bcache_free(bdev->bc, &b);
186 int ext4_block_cache_shake(struct ext4_blockdev *bdev)
189 struct ext4_buf *buf;
190 if (bdev->bc->dont_shake)
193 bdev->bc->dont_shake = true;
195 while (!RB_EMPTY(&bdev->bc->lru_root) &&
196 ext4_bcache_is_full(bdev->bc)) {
198 buf = ext4_buf_lowest_lru(bdev->bc);
200 if (ext4_bcache_test_flag(buf, BC_DIRTY)) {
201 r = ext4_block_flush_buf(bdev, buf);
207 ext4_bcache_drop_buf(bdev->bc, buf);
209 bdev->bc->dont_shake = false;
213 int ext4_block_get_noread(struct ext4_blockdev *bdev, struct ext4_block *b,
219 ext4_assert(bdev && b);
221 if (!bdev->bdif->ph_refctr)
224 if (!(lba < bdev->lg_bcnt))
229 /*If cache is full we have to (flush and) drop it anyway :(*/
230 r = ext4_block_cache_shake(bdev);
234 r = ext4_bcache_alloc(bdev->bc, b, &is_new);
244 int ext4_block_get(struct ext4_blockdev *bdev, struct ext4_block *b,
247 int r = ext4_block_get_noread(bdev, b, lba);
251 if (ext4_bcache_test_flag(b->buf, BC_UPTODATE)) {
252 /* Data in the cache is up-to-date.
253 * Reading from physical device is not required */
257 r = ext4_blocks_get_direct(bdev, b->data, lba, 1);
259 ext4_bcache_free(bdev->bc, b);
264 /* Mark buffer up-to-date, since
265 * fresh data is read from physical device just now. */
266 ext4_bcache_set_flag(b->buf, BC_UPTODATE);
270 int ext4_block_set(struct ext4_blockdev *bdev, struct ext4_block *b)
272 ext4_assert(bdev && b);
275 if (!bdev->bdif->ph_refctr)
278 return ext4_bcache_free(bdev->bc, b);
281 int ext4_blocks_get_direct(struct ext4_blockdev *bdev, void *buf, uint64_t lba,
287 ext4_assert(bdev && buf);
289 pba = (lba * bdev->lg_bsize + bdev->part_offset) / bdev->bdif->ph_bsize;
290 pb_cnt = bdev->lg_bsize / bdev->bdif->ph_bsize;
292 return ext4_bdif_bread(bdev, buf, pba, pb_cnt * cnt);
295 int ext4_blocks_set_direct(struct ext4_blockdev *bdev, const void *buf,
296 uint64_t lba, uint32_t cnt)
301 ext4_assert(bdev && buf);
303 pba = (lba * bdev->lg_bsize + bdev->part_offset) / bdev->bdif->ph_bsize;
304 pb_cnt = bdev->lg_bsize / bdev->bdif->ph_bsize;
306 return ext4_bdif_bwrite(bdev, buf, pba, pb_cnt * cnt);
309 int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off,
310 const void *buf, uint32_t len)
317 const uint8_t *p = (void *)buf;
319 ext4_assert(bdev && buf);
321 if (!bdev->bdif->ph_refctr)
324 if (off + len > bdev->part_size)
325 return EINVAL; /*Ups. Out of range operation*/
327 block_idx = ((off + bdev->part_offset) / bdev->bdif->ph_bsize);
329 /*OK lets deal with the first possible unaligned block*/
330 unalg = (off & (bdev->bdif->ph_bsize - 1));
333 uint32_t wlen = (bdev->bdif->ph_bsize - unalg) > len
335 : (bdev->bdif->ph_bsize - unalg);
337 r = ext4_bdif_bread(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
341 memcpy(bdev->bdif->ph_bbuf + unalg, p, wlen);
342 r = ext4_bdif_bwrite(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
352 blen = len / bdev->bdif->ph_bsize;
353 r = ext4_bdif_bwrite(bdev, p, block_idx, blen);
357 p += bdev->bdif->ph_bsize * blen;
358 len -= bdev->bdif->ph_bsize * blen;
364 r = ext4_bdif_bread(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
368 memcpy(bdev->bdif->ph_bbuf, p, len);
369 r = ext4_bdif_bwrite(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
377 int ext4_block_readbytes(struct ext4_blockdev *bdev, uint64_t off, void *buf,
385 uint8_t *p = (void *)buf;
387 ext4_assert(bdev && buf);
389 if (!bdev->bdif->ph_refctr)
392 if (off + len > bdev->part_size)
393 return EINVAL; /*Ups. Out of range operation*/
395 block_idx = ((off + bdev->part_offset) / bdev->bdif->ph_bsize);
397 /*OK lets deal with the first possible unaligned block*/
398 unalg = (off & (bdev->bdif->ph_bsize - 1));
401 uint32_t rlen = (bdev->bdif->ph_bsize - unalg) > len
403 : (bdev->bdif->ph_bsize - unalg);
405 r = ext4_bdif_bread(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
409 memcpy(p, bdev->bdif->ph_bbuf + unalg, rlen);
417 blen = len / bdev->bdif->ph_bsize;
419 r = ext4_bdif_bread(bdev, p, block_idx, blen);
423 p += bdev->bdif->ph_bsize * blen;
424 len -= bdev->bdif->ph_bsize * blen;
430 r = ext4_bdif_bread(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
434 memcpy(p, bdev->bdif->ph_bbuf, len);
440 int ext4_block_cache_flush(struct ext4_blockdev *bdev)
442 while (!SLIST_EMPTY(&bdev->bc->dirty_list)) {
444 struct ext4_buf *buf = SLIST_FIRST(&bdev->bc->dirty_list);
446 r = ext4_block_flush_buf(bdev, buf);
454 int ext4_block_cache_write_back(struct ext4_blockdev *bdev, uint8_t on_off)
457 bdev->cache_write_back++;
459 if (!on_off && bdev->cache_write_back)
460 bdev->cache_write_back--;
462 if (bdev->cache_write_back)
465 /*Flush data in all delayed cache blocks*/
466 return ext4_block_cache_flush(bdev);