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(struct ext4_blockdev *bdev, struct ext4_block *b,
97 ext4_assert(bdev && b);
99 if (!(bdev->flags & EXT4_BDEV_INITIALIZED))
102 if (!(lba < bdev->lg_bcnt))
108 /*If cache is full we have to flush it anyway :(*/
109 if (ext4_bcache_is_full(bdev->bc) && bdev->cache_write_back) {
111 uint32_t free_candidate = bdev->bc->cnt;
112 uint32_t min_lru = 0xFFFFFFFF;
114 for (i = 0; i < bdev->bc->cnt; ++i) {
115 /*Check if buffer free was delayed.*/
116 if (!bdev->bc->free_delay[i])
119 /*Check reference counter.*/
120 if (bdev->bc->refctr[i])
123 if (bdev->bc->lru_id[i] < min_lru) {
124 min_lru = bdev->bc->lru_id[i];
130 if (free_candidate < bdev->bc->cnt) {
131 /*Buffer free was delayed and have no reference. Flush it.*/
132 r = ext4_blocks_set_direct(
133 bdev, bdev->bc->data + 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 refered block count*/
142 bdev->bc->ref_blocks--;
146 r = ext4_bcache_alloc(bdev->bc, b, &is_new);
151 /*Block is in cache. Read from physical device is not required*/
158 pba = (lba * bdev->lg_bsize) / bdev->ph_bsize;
159 pb_cnt = bdev->lg_bsize / bdev->ph_bsize;
161 r = bdev->bread(bdev, b->data, pba, pb_cnt);
164 ext4_bcache_free(bdev->bc, b, 0);
173 int ext4_block_set(struct ext4_blockdev *bdev, struct ext4_block *b)
179 ext4_assert(bdev && b);
181 if (!(bdev->flags & EXT4_BDEV_INITIALIZED))
184 /*Doesn,t need to write.*/
185 if (!b->dirty && !bdev->bc->dirty[b->cache_id]) {
186 ext4_bcache_free(bdev->bc, b, 0);
190 /*Free cache delay mode*/
191 if (bdev->cache_write_back) {
193 /*Free cahe block and mark as free delayed*/
194 return ext4_bcache_free(bdev->bc, b, bdev->cache_write_back);
197 if (bdev->bc->refctr[b->cache_id] > 1) {
198 bdev->bc->dirty[b->cache_id] = true;
199 return ext4_bcache_free(bdev->bc, b, 0);
202 pba = (b->lb_id * bdev->lg_bsize) / bdev->ph_bsize;
203 pb_cnt = bdev->lg_bsize / bdev->ph_bsize;
205 r = bdev->bwrite(bdev, b->data, pba, pb_cnt);
206 bdev->bc->dirty[b->cache_id] = false;
209 ext4_bcache_free(bdev->bc, b, 0);
215 ext4_bcache_free(bdev->bc, b, 0);
219 int ext4_blocks_get_direct(struct ext4_blockdev *bdev, void *buf, uint64_t lba,
225 ext4_assert(bdev && buf);
227 pba = (lba * bdev->lg_bsize) / bdev->ph_bsize;
228 pb_cnt = bdev->lg_bsize / bdev->ph_bsize;
231 return bdev->bread(bdev, buf, pba, pb_cnt * cnt);
234 int ext4_blocks_set_direct(struct ext4_blockdev *bdev, const void *buf,
235 uint64_t lba, uint32_t cnt)
240 ext4_assert(bdev && buf);
242 pba = (lba * bdev->lg_bsize) / bdev->ph_bsize;
243 pb_cnt = bdev->lg_bsize / bdev->ph_bsize;
247 return bdev->bwrite(bdev, buf, pba, pb_cnt * cnt);
250 int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off,
251 const void *buf, uint32_t len)
259 const uint8_t *p = (void *)buf;
261 ext4_assert(bdev && buf);
263 if (!(bdev->flags & EXT4_BDEV_INITIALIZED))
266 block_idx = off / bdev->ph_bsize;
267 block_end = block_idx + len / bdev->ph_bsize;
269 if (!(block_end < bdev->ph_bcnt))
270 return EINVAL; /*Ups. Out of range operation*/
272 /*OK lets deal with the first possible unaligned block*/
273 unalg = (off & (bdev->ph_bsize - 1));
277 (bdev->ph_bsize - unalg) > len ? len : (bdev->ph_bsize - unalg);
279 r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1);
284 memcpy(bdev->ph_bbuf + unalg, p, wlen);
286 r = bdev->bwrite(bdev, bdev->ph_bbuf, block_idx, 1);
296 blen = len / bdev->ph_bsize;
297 r = bdev->bwrite(bdev, p, block_idx, blen);
302 p += bdev->ph_bsize * blen;
303 len -= bdev->ph_bsize * blen;
309 r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1);
313 memcpy(bdev->ph_bbuf, p, len);
315 r = bdev->bwrite(bdev, bdev->ph_bbuf, block_idx, 1);
324 int ext4_block_readbytes(struct ext4_blockdev *bdev, uint64_t off, void *buf,
333 uint8_t *p = (void *)buf;
335 ext4_assert(bdev && buf);
337 if (!(bdev->flags & EXT4_BDEV_INITIALIZED))
340 block_idx = off / bdev->ph_bsize;
341 block_end = block_idx + len / bdev->ph_bsize;
343 if (!(block_end < bdev->ph_bcnt))
344 return EINVAL; /*Ups. Out of range operation*/
346 /*OK lets deal with the first possible unaligned block*/
347 unalg = (off & (bdev->ph_bsize - 1));
351 (bdev->ph_bsize - unalg) > len ? len : (bdev->ph_bsize - unalg);
353 r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1);
357 memcpy(p, bdev->ph_bbuf + unalg, rlen);
365 blen = len / bdev->ph_bsize;
367 r = bdev->bread(bdev, p, block_idx, blen);
372 p += bdev->ph_bsize * blen;
373 len -= bdev->ph_bsize * blen;
379 r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1);
383 memcpy(p, bdev->ph_bbuf, len);
389 int ext4_block_cache_write_back(struct ext4_blockdev *bdev, uint8_t on_off)
395 bdev->cache_write_back++;
397 if (!on_off && bdev->cache_write_back)
398 bdev->cache_write_back--;
400 /*Flush all delayed cache blocks*/
401 if (!bdev->cache_write_back) {
402 for (i = 0; i < bdev->bc->cnt; ++i) {
404 /*Check if buffer free was delayed.*/
405 if (!bdev->bc->free_delay[i])
408 /*Check reference counter.*/
409 if (bdev->bc->refctr[i])
412 /*Buffer free was delayed and have no reference. Flush it.*/
413 r = ext4_blocks_set_direct(bdev,
414 bdev->bc->data + bdev->bc->itemsize * i,
415 bdev->bc->lba[i], 1);
419 /*No delayed anymore*/
420 bdev->bc->free_delay[i] = 0;
422 /*Reduce refered block count*/
423 bdev->bc->ref_blocks--;