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"
48 static void ext4_bdif_lock(struct ext4_blockdev *bdev)
50 if (!bdev->bdif->lock)
53 int r = bdev->bdif->lock(bdev);
54 ext4_assert(r == EOK);
57 static void ext4_bdif_unlock(struct ext4_blockdev *bdev)
59 if (!bdev->bdif->unlock)
62 int r = bdev->bdif->unlock(bdev);
63 ext4_assert(r == EOK);
66 static int ext4_bdif_bread(struct ext4_blockdev *bdev, void *buf,
67 uint64_t blk_id, uint32_t blk_cnt)
70 int r = bdev->bdif->bread(bdev, buf, blk_id, blk_cnt);
71 bdev->bdif->bread_ctr++;
72 ext4_bdif_unlock(bdev);
76 static int ext4_bdif_bwrite(struct ext4_blockdev *bdev, const void *buf,
77 uint64_t blk_id, uint32_t blk_cnt)
80 int r = bdev->bdif->bwrite(bdev, buf, blk_id, blk_cnt);
81 bdev->bdif->bwrite_ctr++;
82 ext4_bdif_unlock(bdev);
86 int ext4_block_init(struct ext4_blockdev *bdev)
90 ext4_assert(bdev->bdif);
91 ext4_assert(bdev->bdif->open &&
96 if (bdev->bdif->ph_refctr) {
97 bdev->bdif->ph_refctr++;
101 /*Low level block init*/
102 rc = bdev->bdif->open(bdev);
106 bdev->bdif->ph_refctr = 1;
110 int ext4_block_bind_bcache(struct ext4_blockdev *bdev, struct ext4_bcache *bc)
112 ext4_assert(bdev && bc);
118 void ext4_block_set_lb_size(struct ext4_blockdev *bdev, uint32_t lb_bsize)
120 /*Logical block size has to be multiply of physical */
121 ext4_assert(!(lb_bsize % bdev->bdif->ph_bsize));
123 bdev->lg_bsize = lb_bsize;
124 bdev->lg_bcnt = bdev->part_size / lb_bsize;
127 int ext4_block_fini(struct ext4_blockdev *bdev)
131 if (!bdev->bdif->ph_refctr)
134 bdev->bdif->ph_refctr--;
135 if (bdev->bdif->ph_refctr)
138 /*Low level block fini*/
139 return bdev->bdif->close(bdev);
142 int ext4_block_flush_buf(struct ext4_blockdev *bdev, struct ext4_buf *buf)
145 struct ext4_bcache *bc = bdev->bc;
147 if (ext4_bcache_test_flag(buf, BC_DIRTY) &&
148 ext4_bcache_test_flag(buf, BC_UPTODATE)) {
149 r = ext4_blocks_set_direct(bdev, buf->data, buf->lba, 1);
151 if (buf->end_write) {
152 bc->dont_shake = true;
153 buf->end_write(bc, buf, r, buf->end_write_arg);
154 bc->dont_shake = false;
160 ext4_bcache_remove_dirty_node(bc, buf);
161 ext4_bcache_clear_flag(buf, BC_DIRTY);
162 if (buf->end_write) {
163 bc->dont_shake = true;
164 buf->end_write(bc, buf, r, buf->end_write_arg);
165 bc->dont_shake = false;
171 int ext4_block_flush_lba(struct ext4_blockdev *bdev, uint64_t lba)
174 struct ext4_buf *buf;
176 buf = ext4_bcache_find_get(bdev->bc, &b, lba);
178 r = ext4_block_flush_buf(bdev, buf);
179 ext4_bcache_free(bdev->bc, &b);
184 int ext4_block_cache_shake(struct ext4_blockdev *bdev)
187 struct ext4_buf *buf;
188 if (bdev->bc->dont_shake)
191 while (!RB_EMPTY(&bdev->bc->lru_root) &&
192 ext4_bcache_is_full(bdev->bc)) {
194 buf = ext4_buf_lowest_lru(bdev->bc);
196 if (ext4_bcache_test_flag(buf, BC_DIRTY)) {
197 r = ext4_block_flush_buf(bdev, buf);
203 ext4_bcache_drop_buf(bdev->bc, buf);
208 int ext4_block_get_noread(struct ext4_blockdev *bdev, struct ext4_block *b,
214 ext4_assert(bdev && b);
216 if (!bdev->bdif->ph_refctr)
219 if (!(lba < bdev->lg_bcnt))
224 /*If cache is full we have to (flush and) drop it anyway :(*/
225 r = ext4_block_cache_shake(bdev);
229 r = ext4_bcache_alloc(bdev->bc, b, &is_new);
239 int ext4_block_get(struct ext4_blockdev *bdev, struct ext4_block *b,
242 int r = ext4_block_get_noread(bdev, b, lba);
246 if (ext4_bcache_test_flag(b->buf, BC_UPTODATE)) {
247 /* Data in the cache is up-to-date.
248 * Reading from physical device is not required */
252 r = ext4_blocks_get_direct(bdev, b->data, lba, 1);
254 ext4_bcache_free(bdev->bc, b);
259 /* Mark buffer up-to-date, since
260 * fresh data is read from physical device just now. */
261 ext4_bcache_set_flag(b->buf, BC_UPTODATE);
265 int ext4_block_set(struct ext4_blockdev *bdev, struct ext4_block *b)
267 ext4_assert(bdev && b);
270 if (!bdev->bdif->ph_refctr)
273 return ext4_bcache_free(bdev->bc, b);
276 int ext4_blocks_get_direct(struct ext4_blockdev *bdev, void *buf, uint64_t lba,
282 ext4_assert(bdev && buf);
284 pba = (lba * bdev->lg_bsize + bdev->part_offset) / bdev->bdif->ph_bsize;
285 pb_cnt = bdev->lg_bsize / bdev->bdif->ph_bsize;
287 return ext4_bdif_bread(bdev, buf, pba, pb_cnt * cnt);
290 int ext4_blocks_set_direct(struct ext4_blockdev *bdev, const void *buf,
291 uint64_t lba, uint32_t cnt)
296 ext4_assert(bdev && buf);
298 pba = (lba * bdev->lg_bsize + bdev->part_offset) / bdev->bdif->ph_bsize;
299 pb_cnt = bdev->lg_bsize / bdev->bdif->ph_bsize;
301 return ext4_bdif_bwrite(bdev, buf, pba, pb_cnt * cnt);
304 int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off,
305 const void *buf, uint32_t len)
312 const uint8_t *p = (void *)buf;
314 ext4_assert(bdev && buf);
316 if (!bdev->bdif->ph_refctr)
319 if (off + len > bdev->part_size)
320 return EINVAL; /*Ups. Out of range operation*/
322 block_idx = ((off + bdev->part_offset) / bdev->bdif->ph_bsize);
324 /*OK lets deal with the first possible unaligned block*/
325 unalg = (off & (bdev->bdif->ph_bsize - 1));
328 uint32_t wlen = (bdev->bdif->ph_bsize - unalg) > len
330 : (bdev->bdif->ph_bsize - unalg);
332 r = ext4_bdif_bread(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
336 memcpy(bdev->bdif->ph_bbuf + unalg, p, wlen);
337 r = ext4_bdif_bwrite(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
347 blen = len / bdev->bdif->ph_bsize;
348 r = ext4_bdif_bwrite(bdev, p, block_idx, blen);
352 p += bdev->bdif->ph_bsize * blen;
353 len -= bdev->bdif->ph_bsize * blen;
359 r = ext4_bdif_bread(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
363 memcpy(bdev->bdif->ph_bbuf, p, len);
364 r = ext4_bdif_bwrite(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
372 int ext4_block_readbytes(struct ext4_blockdev *bdev, uint64_t off, void *buf,
380 uint8_t *p = (void *)buf;
382 ext4_assert(bdev && buf);
384 if (!bdev->bdif->ph_refctr)
387 if (off + len > bdev->part_size)
388 return EINVAL; /*Ups. Out of range operation*/
390 block_idx = ((off + bdev->part_offset) / bdev->bdif->ph_bsize);
392 /*OK lets deal with the first possible unaligned block*/
393 unalg = (off & (bdev->bdif->ph_bsize - 1));
396 uint32_t rlen = (bdev->bdif->ph_bsize - unalg) > len
398 : (bdev->bdif->ph_bsize - unalg);
400 r = ext4_bdif_bread(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
404 memcpy(p, bdev->bdif->ph_bbuf + unalg, rlen);
412 blen = len / bdev->bdif->ph_bsize;
414 r = ext4_bdif_bread(bdev, p, block_idx, blen);
418 p += bdev->bdif->ph_bsize * blen;
419 len -= bdev->bdif->ph_bsize * blen;
425 r = ext4_bdif_bread(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
429 memcpy(p, bdev->bdif->ph_bbuf, len);
435 int ext4_block_cache_flush(struct ext4_blockdev *bdev)
437 while (!SLIST_EMPTY(&bdev->bc->dirty_list)) {
439 struct ext4_buf *buf = SLIST_FIRST(&bdev->bc->dirty_list);
441 r = ext4_block_flush_buf(bdev, buf);
449 int ext4_block_cache_write_back(struct ext4_blockdev *bdev, uint8_t on_off)
452 bdev->cache_write_back++;
454 if (!on_off && bdev->cache_write_back)
455 bdev->cache_write_back--;
457 if (bdev->cache_write_back)
460 /*Flush data in all delayed cache blocks*/
461 return ext4_block_cache_flush(bdev);