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>
47 int ext4_block_init(struct ext4_blockdev *bdev)
52 ext4_assert(bdev->open && bdev->close && bdev->bread && bdev->bwrite);
54 /*Low level block init*/
55 rc = bdev->open(bdev);
59 bdev->flags |= EXT4_BDEV_INITIALIZED;
64 int ext4_block_bind_bcache(struct ext4_blockdev *bdev, struct ext4_bcache *bc)
66 ext4_assert(bdev && bc);
71 void ext4_block_set_lb_size(struct ext4_blockdev *bdev, uint64_t lb_bsize)
73 /*Logical block size has to be multiply of physical */
74 ext4_assert(!(lb_bsize % bdev->ph_bsize));
76 bdev->lg_bsize = lb_bsize;
77 bdev->lg_bcnt = (bdev->ph_bcnt * bdev->ph_bsize) / lb_bsize;
81 int ext4_block_fini(struct ext4_blockdev *bdev)
85 bdev->flags &= ~(EXT4_BDEV_INITIALIZED);
87 /*Low level block fini*/
88 return bdev->close(bdev);
92 int ext4_block_get(struct ext4_blockdev *bdev, struct ext4_block *b,
99 ext4_assert(bdev && b);
101 if(!(bdev->flags & EXT4_BDEV_INITIALIZED))
104 if(!(lba < bdev->lg_bcnt))
110 r = ext4_bcache_alloc(bdev->bc, b, &is_new);
116 /*Block is in cache. Read from physical device is not required*/
123 pba = (lba * bdev->lg_bsize) / bdev->ph_bsize;
124 pb_cnt = bdev->lg_bsize / bdev->ph_bsize;
127 r = bdev->bread(bdev, b->data, pba, pb_cnt);
130 ext4_bcache_free(bdev->bc, b, 0);
140 int ext4_block_set(struct ext4_blockdev *bdev, struct ext4_block *b)
147 ext4_assert(bdev && b);
149 if(!(bdev->flags & EXT4_BDEV_INITIALIZED))
152 /*Doesn,t need to write.*/
153 if(b->dirty == false){
154 ext4_bcache_free(bdev->bc, b, 0);
158 /*Free cache delay mode*/
159 if(bdev->cache_flush_delay){
161 /*Free cahe block and mark as free delayed*/
162 ext4_bcache_free(bdev->bc, b, bdev->cache_flush_delay);
164 /*If cache is full we have to flush it anyway :(*/
165 if(ext4_bcache_is_full(bdev->bc)){
166 for (i = 0; i < bdev->bc->cnt; ++i) {
167 /*Check if buffer free was delayed.*/
168 if(!bdev->bc->free_delay[i])
171 /*Check reference counter.*/
172 if(bdev->bc->refctr[i])
175 /*Buffer free was delayed and have no reference. Flush it.*/
176 r = ext4_block_set_direct(bdev,
177 bdev->bc->data + bdev->bc->itemsize * i,
182 /*No delayed anymore*/
183 bdev->bc->free_delay[i] = 0;
185 /*Reduce refered block count*/
186 bdev->bc->ref_blocks--;
193 pba = (b->lb_id * bdev->lg_bsize) / bdev->ph_bsize;
194 pb_cnt = bdev->lg_bsize / bdev->ph_bsize;
197 r = bdev->bwrite(bdev, b->data, pba, pb_cnt);
201 ext4_bcache_free(bdev->bc, b, 0);
207 ext4_bcache_free(bdev->bc, b, 0);
211 int ext4_block_get_direct(struct ext4_blockdev *bdev, void *buf,
217 ext4_assert(bdev && buf);
219 pba = (lba * bdev->lg_bsize) / bdev->ph_bsize;
220 pb_cnt = bdev->lg_bsize / bdev->ph_bsize;
224 return bdev->bread(bdev, buf, pba, pb_cnt);
229 int ext4_block_set_direct(struct ext4_blockdev *bdev, const void *buf,
235 ext4_assert(bdev && buf);
237 pba = (lba * bdev->lg_bsize) / bdev->ph_bsize;
238 pb_cnt = bdev->lg_bsize / bdev->ph_bsize;
242 return bdev->bwrite(bdev, buf, pba, pb_cnt);
246 int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off,
247 const void *buf, uint32_t len)
257 const uint8_t *p = (void *)buf;
259 ext4_assert(bdev && buf);
261 if(!(bdev->flags & EXT4_BDEV_INITIALIZED))
265 block_idx = off / bdev->ph_bsize;
266 block_end = block_idx + len / bdev->ph_bsize;
268 if(!(block_end < bdev->ph_bcnt))
269 return EINVAL; /*Ups. Out of range operation*/
272 /*OK lets deal with the first possible unaligned block*/
273 unalg = (off & (bdev->ph_bsize - 1));
276 uint32_t wlen = (bdev->ph_bsize - unalg) > len ?
277 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);
298 blen = len / bdev->ph_bsize;
300 r = bdev->bwrite(bdev, p, block_idx, blen);
305 p += bdev->ph_bsize * blen;
306 len -= bdev->ph_bsize * blen;
314 r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1);
319 memcpy(bdev->ph_bbuf, p, len);
321 r = bdev->bwrite(bdev, bdev->ph_bbuf, block_idx, 1);
333 int ext4_block_readbytes(struct ext4_blockdev *bdev, uint64_t off, void *buf,
344 uint8_t *p = (void *)buf;
346 ext4_assert(bdev && buf);
348 if(!(bdev->flags & EXT4_BDEV_INITIALIZED))
352 block_idx = off / bdev->ph_bsize;
353 block_end = block_idx + len / bdev->ph_bsize;
355 if(!(block_end < bdev->ph_bcnt))
356 return EINVAL; /*Ups. Out of range operation*/
359 /*OK lets deal with the first possible unaligned block*/
360 unalg = (off & (bdev->ph_bsize - 1));
363 uint32_t rlen = (bdev->ph_bsize - unalg) > len ?
364 len : (bdev->ph_bsize - unalg);
366 r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1);
371 memcpy(p, bdev->ph_bbuf + unalg, rlen);
380 blen = len / bdev->ph_bsize;
382 r = bdev->bread(bdev, p, block_idx, blen);
387 p += bdev->ph_bsize * blen;
388 len -= bdev->ph_bsize * blen;
396 r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1);
401 memcpy(p, bdev->ph_bbuf, len);
408 int ext4_block_delay_cache_flush(struct ext4_blockdev *bdev,
413 bdev->cache_flush_delay = on_off;
415 /*Flush all delayed cache blocks*/
417 for (i = 0; i < bdev->bc->cnt; ++i) {
419 /*Check if buffer free was delayed.*/
420 if(!bdev->bc->free_delay[i])
423 /*Check reference counter.*/
424 if(bdev->bc->refctr[i])
427 /*Buffer free was delayed and have no reference. Flush it.*/
428 r = ext4_block_set_direct(bdev,
429 bdev->bc->data + bdev->bc->itemsize * i,
434 /*No delayed anymore*/
435 bdev->bc->free_delay[i] = 0;
437 /*Reduce refered block count*/
438 bdev->bc->ref_blocks--;