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->open && bdev->close && bdev->bread && bdev->bwrite);
52 /*Low level block init*/
53 rc = bdev->open(bdev);
57 bdev->flags |= EXT4_BDEV_INITIALIZED;
62 int ext4_block_bind_bcache(struct ext4_blockdev *bdev, struct ext4_bcache *bc)
64 ext4_assert(bdev && bc);
69 void ext4_block_set_lb_size(struct ext4_blockdev *bdev, uint64_t lb_bsize)
71 /*Logical block size has to be multiply of physical */
72 ext4_assert(!(lb_bsize % bdev->ph_bsize));
74 bdev->lg_bsize = lb_bsize;
75 bdev->lg_bcnt = (bdev->ph_bcnt * bdev->ph_bsize) / lb_bsize;
78 int ext4_block_fini(struct ext4_blockdev *bdev)
82 bdev->flags &= ~(EXT4_BDEV_INITIALIZED);
84 /*Low level block fini*/
85 return bdev->close(bdev);
88 int ext4_block_get_noread(struct ext4_blockdev *bdev, struct ext4_block *b,
95 ext4_assert(bdev && b);
97 if (!(bdev->flags & EXT4_BDEV_INITIALIZED))
100 if (!(lba < bdev->lg_bcnt))
106 /*If cache is full we have to flush it anyway :(*/
107 if (ext4_bcache_is_full(bdev->bc) && bdev->cache_write_back) {
109 uint32_t free_candidate = bdev->bc->cnt;
110 uint32_t min_lru = 0xFFFFFFFF;
112 for (i = 0; i < bdev->bc->cnt; ++i) {
113 /*Check if buffer free was delayed.*/
114 if (!bdev->bc->free_delay[i])
117 /*Check reference counter.*/
118 if (bdev->bc->refctr[i])
121 if (bdev->bc->lru_id[i] < min_lru) {
122 min_lru = bdev->bc->lru_id[i];
128 if (free_candidate < bdev->bc->cnt) {
129 /*Buffer free was delayed and have no reference. Flush
131 r = ext4_blocks_set_direct(
132 bdev, bdev->bc->data +
133 bdev->bc->itemsize * free_candidate,
134 bdev->bc->lba[free_candidate], 1);
138 /*No delayed anymore*/
139 bdev->bc->free_delay[free_candidate] = 0;
141 /*Reduce reference counter*/
142 bdev->bc->ref_blocks--;
146 r = ext4_bcache_alloc(bdev->bc, b, &is_new);
156 int ext4_block_get(struct ext4_blockdev *bdev, struct ext4_block *b,
161 int r = ext4_block_get_noread(bdev, b, lba);
166 /* Data in the cache is up-to-date.
167 * Reading from physical device is not required */
171 pba = (lba * bdev->lg_bsize) / bdev->ph_bsize;
172 pb_cnt = bdev->lg_bsize / bdev->ph_bsize;
174 r = bdev->bread(bdev, b->data, pba, pb_cnt);
177 ext4_bcache_free(bdev->bc, b, 0);
182 /* Mark buffer up-to-date, since
183 * fresh data is read from physical device just now. */
184 ext4_bcache_set_flag(bdev->bc, b->cache_id, BC_UPTODATE);
190 int ext4_block_set(struct ext4_blockdev *bdev, struct ext4_block *b)
196 ext4_assert(bdev && b);
198 if (!(bdev->flags & EXT4_BDEV_INITIALIZED))
201 /*Buffer is not marked dirty and is stale*/
202 if (!b->uptodate && !b->dirty)
203 ext4_bcache_clear_flag(bdev->bc, b->cache_id, BC_UPTODATE);
205 /*No need to write.*/
207 !ext4_bcache_test_flag(bdev->bc, b->cache_id, BC_DIRTY)) {
208 ext4_bcache_free(bdev->bc, b, 0);
211 /* Data is valid, so mark buffer up-to-date. */
212 ext4_bcache_set_flag(bdev->bc, b->cache_id, BC_UPTODATE);
214 /*Free cache delay mode*/
215 if (bdev->cache_write_back) {
217 /*Free cache block and mark as free delayed*/
218 return ext4_bcache_free(bdev->bc, b, bdev->cache_write_back);
221 if (bdev->bc->refctr[b->cache_id] > 1) {
222 ext4_bcache_set_flag(bdev->bc, b->cache_id, BC_DIRTY);
223 return ext4_bcache_free(bdev->bc, b, 0);
226 pba = (b->lb_id * bdev->lg_bsize) / bdev->ph_bsize;
227 pb_cnt = bdev->lg_bsize / bdev->ph_bsize;
229 r = bdev->bwrite(bdev, b->data, pba, pb_cnt);
230 ext4_bcache_clear_flag(bdev->bc, b->cache_id, BC_DIRTY);
233 ext4_bcache_clear_flag(bdev->bc, b->cache_id, BC_UPTODATE);
234 ext4_bcache_free(bdev->bc, b, 0);
240 ext4_bcache_free(bdev->bc, b, 0);
244 int ext4_blocks_get_direct(struct ext4_blockdev *bdev, void *buf, uint64_t lba,
250 ext4_assert(bdev && buf);
252 pba = (lba * bdev->lg_bsize) / bdev->ph_bsize;
253 pb_cnt = bdev->lg_bsize / bdev->ph_bsize;
256 return bdev->bread(bdev, buf, pba, pb_cnt * cnt);
259 int ext4_blocks_set_direct(struct ext4_blockdev *bdev, const void *buf,
260 uint64_t lba, uint32_t cnt)
265 ext4_assert(bdev && buf);
267 pba = (lba * bdev->lg_bsize) / bdev->ph_bsize;
268 pb_cnt = bdev->lg_bsize / bdev->ph_bsize;
272 return bdev->bwrite(bdev, buf, pba, pb_cnt * cnt);
275 int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off,
276 const void *buf, uint32_t len)
284 const uint8_t *p = (void *)buf;
286 ext4_assert(bdev && buf);
288 if (!(bdev->flags & EXT4_BDEV_INITIALIZED))
291 block_idx = off / bdev->ph_bsize;
292 block_end = block_idx + len / bdev->ph_bsize;
294 if (!(block_end < bdev->ph_bcnt))
295 return EINVAL; /*Ups. Out of range operation*/
297 /*OK lets deal with the first possible unaligned block*/
298 unalg = (off & (bdev->ph_bsize - 1));
301 uint32_t wlen = (bdev->ph_bsize - unalg) > len
303 : (bdev->ph_bsize - unalg);
305 r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1);
310 memcpy(bdev->ph_bbuf + unalg, p, wlen);
312 r = bdev->bwrite(bdev, bdev->ph_bbuf, block_idx, 1);
322 blen = len / bdev->ph_bsize;
323 r = bdev->bwrite(bdev, p, block_idx, blen);
328 p += bdev->ph_bsize * blen;
329 len -= bdev->ph_bsize * blen;
335 r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1);
339 memcpy(bdev->ph_bbuf, p, len);
341 r = bdev->bwrite(bdev, bdev->ph_bbuf, block_idx, 1);
350 int ext4_block_readbytes(struct ext4_blockdev *bdev, uint64_t off, void *buf,
359 uint8_t *p = (void *)buf;
361 ext4_assert(bdev && buf);
363 if (!(bdev->flags & EXT4_BDEV_INITIALIZED))
366 block_idx = off / bdev->ph_bsize;
367 block_end = block_idx + len / bdev->ph_bsize;
369 if (!(block_end < bdev->ph_bcnt))
370 return EINVAL; /*Ups. Out of range operation*/
372 /*OK lets deal with the first possible unaligned block*/
373 unalg = (off & (bdev->ph_bsize - 1));
376 uint32_t rlen = (bdev->ph_bsize - unalg) > len
378 : (bdev->ph_bsize - unalg);
380 r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1);
384 memcpy(p, bdev->ph_bbuf + unalg, rlen);
392 blen = len / bdev->ph_bsize;
394 r = bdev->bread(bdev, p, block_idx, blen);
399 p += bdev->ph_bsize * blen;
400 len -= bdev->ph_bsize * blen;
406 r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1);
410 memcpy(p, bdev->ph_bbuf, len);
416 int ext4_block_cache_write_back(struct ext4_blockdev *bdev, uint8_t on_off)
422 bdev->cache_write_back++;
424 if (!on_off && bdev->cache_write_back)
425 bdev->cache_write_back--;
427 /*Flush all delayed cache blocks*/
428 if (!bdev->cache_write_back) {
429 for (i = 0; i < bdev->bc->cnt; ++i) {
431 /*Check if buffer free was delayed.*/
432 if (!bdev->bc->free_delay[i])
435 /*Check reference counter.*/
436 if (bdev->bc->refctr[i])
439 /*Buffer free was delayed and have no reference. Flush
441 r = ext4_blocks_set_direct(
442 bdev, bdev->bc->data + bdev->bc->itemsize * i,
443 bdev->bc->lba[i], 1);
447 /*No delayed anymore*/
448 bdev->bc->free_delay[i] = 0;
450 /*Reduce reference counter*/
451 bdev->bc->ref_blocks--;