ref: 2e155673ce33cec9821fb4e933a701329608109f
parent: c6a76b0c566f6cc4fd0d7a956cdf9739912944d7
author: gkostka <kostka.grzegorz@gmail.com>
date: Tue Jun 16 06:02:37 EDT 2015
clang-format: ext4_blockdev
--- a/lwext4/ext4_blockdev.c
+++ b/lwext4/ext4_blockdev.c
@@ -42,18 +42,16 @@
#include <string.h>
#include <stdlib.h>
-
-
int ext4_block_init(struct ext4_blockdev *bdev)
{
int rc;
ext4_assert(bdev);
- ext4_assert(bdev->open && bdev->close && bdev->bread && bdev->bwrite);
+ ext4_assert(bdev->open && bdev->close && bdev->bread && bdev->bwrite);
/*Low level block init*/
rc = bdev->open(bdev);
- if(rc != EOK)
+ if (rc != EOK)
return rc;
bdev->flags |= EXT4_BDEV_INITIALIZED;
@@ -75,7 +73,6 @@
bdev->lg_bsize = lb_bsize;
bdev->lg_bcnt = (bdev->ph_bcnt * bdev->ph_bsize) / lb_bsize;
-
}
int ext4_block_fini(struct ext4_blockdev *bdev)
@@ -88,9 +85,8 @@
return bdev->close(bdev);
}
-
int ext4_block_get(struct ext4_blockdev *bdev, struct ext4_block *b,
- uint64_t lba)
+ uint64_t lba)
{
uint64_t pba;
uint32_t pb_cnt;
@@ -100,10 +96,10 @@
ext4_assert(bdev && b);
- if(!(bdev->flags & EXT4_BDEV_INITIALIZED))
+ if (!(bdev->flags & EXT4_BDEV_INITIALIZED))
return EIO;
- if(!(lba < bdev->lg_bcnt))
+ if (!(lba < bdev->lg_bcnt))
return ERANGE;
b->dirty = 0;
@@ -110,7 +106,7 @@
b->lb_id = lba;
/*If cache is full we have to flush it anyway :(*/
- if(ext4_bcache_is_full(bdev->bc) && bdev->cache_write_back){
+ if (ext4_bcache_is_full(bdev->bc) && bdev->cache_write_back) {
uint32_t free_candidate = bdev->bc->cnt;
uint32_t min_lru = 0xFFFFFFFF;
@@ -117,14 +113,14 @@
for (i = 0; i < bdev->bc->cnt; ++i) {
/*Check if buffer free was delayed.*/
- if(!bdev->bc->free_delay[i])
+ if (!bdev->bc->free_delay[i])
continue;
/*Check reference counter.*/
- if(bdev->bc->refctr[i])
+ if (bdev->bc->refctr[i])
continue;
- if(bdev->bc->lru_id[i] < min_lru){
+ if (bdev->bc->lru_id[i] < min_lru) {
min_lru = bdev->bc->lru_id[i];
free_candidate = i;
continue;
@@ -131,12 +127,12 @@
}
}
- if(free_candidate < bdev->bc->cnt){
+ if (free_candidate < bdev->bc->cnt) {
/*Buffer free was delayed and have no reference. Flush it.*/
- r = ext4_blocks_set_direct(bdev,
- bdev->bc->data + bdev->bc->itemsize * free_candidate,
- bdev->bc->lba[free_candidate], 1);
- if(r != EOK)
+ r = ext4_blocks_set_direct(
+ bdev, bdev->bc->data + bdev->bc->itemsize * free_candidate,
+ bdev->bc->lba[free_candidate], 1);
+ if (r != EOK)
return r;
/*No delayed anymore*/
@@ -147,18 +143,16 @@
}
}
-
r = ext4_bcache_alloc(bdev->bc, b, &is_new);
- if(r != EOK)
+ if (r != EOK)
return r;
-
- if(!is_new){
+ if (!is_new) {
/*Block is in cache. Read from physical device is not required*/
return EOK;
}
- if(!b->data)
+ if (!b->data)
return ENOMEM;
pba = (lba * bdev->lg_bsize) / bdev->ph_bsize;
@@ -166,7 +160,7 @@
r = bdev->bread(bdev, b->data, pba, pb_cnt);
- if(r != EOK){
+ if (r != EOK) {
ext4_bcache_free(bdev->bc, b, 0);
b->lb_id = 0;
return r;
@@ -184,34 +178,33 @@
ext4_assert(bdev && b);
- if(!(bdev->flags & EXT4_BDEV_INITIALIZED))
+ if (!(bdev->flags & EXT4_BDEV_INITIALIZED))
return EIO;
/*Doesn,t need to write.*/
- if(!b->dirty && !bdev->bc->dirty[b->cache_id]){
+ if (!b->dirty && !bdev->bc->dirty[b->cache_id]) {
ext4_bcache_free(bdev->bc, b, 0);
return EOK;
}
/*Free cache delay mode*/
- if(bdev->cache_write_back){
+ if (bdev->cache_write_back) {
/*Free cahe block and mark as free delayed*/
return ext4_bcache_free(bdev->bc, b, bdev->cache_write_back);
}
- if(bdev->bc->refctr[b->cache_id] > 1){
+ if (bdev->bc->refctr[b->cache_id] > 1) {
bdev->bc->dirty[b->cache_id] = true;
return ext4_bcache_free(bdev->bc, b, 0);
}
-
pba = (b->lb_id * bdev->lg_bsize) / bdev->ph_bsize;
pb_cnt = bdev->lg_bsize / bdev->ph_bsize;
r = bdev->bwrite(bdev, b->data, pba, pb_cnt);
bdev->bc->dirty[b->cache_id] = false;
- if(r != EOK){
+ if (r != EOK) {
b->dirty = false;
ext4_bcache_free(bdev->bc, b, 0);
return r;
@@ -223,8 +216,8 @@
return EOK;
}
-int ext4_blocks_get_direct(struct ext4_blockdev *bdev, void *buf,
- uint64_t lba, uint32_t cnt)
+int ext4_blocks_get_direct(struct ext4_blockdev *bdev, void *buf, uint64_t lba,
+ uint32_t cnt)
{
uint64_t pba;
uint32_t pb_cnt;
@@ -239,7 +232,7 @@
}
int ext4_blocks_set_direct(struct ext4_blockdev *bdev, const void *buf,
- uint64_t lba, uint32_t cnt)
+ uint64_t lba, uint32_t cnt)
{
uint64_t pba;
uint32_t pb_cnt;
@@ -254,9 +247,8 @@
return bdev->bwrite(bdev, buf, pba, pb_cnt * cnt);
}
-
int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off,
- const void *buf, uint32_t len)
+ const void *buf, uint32_t len)
{
uint64_t block_idx;
uint64_t block_end;
@@ -268,44 +260,43 @@
ext4_assert(bdev && buf);
- if(!(bdev->flags & EXT4_BDEV_INITIALIZED))
+ if (!(bdev->flags & EXT4_BDEV_INITIALIZED))
return EIO;
- block_idx = off / bdev->ph_bsize;
- block_end = block_idx + len / bdev->ph_bsize;
+ block_idx = off / bdev->ph_bsize;
+ block_end = block_idx + len / bdev->ph_bsize;
- if(!(block_end < bdev->ph_bcnt))
- return EINVAL; /*Ups. Out of range operation*/
+ if (!(block_end < bdev->ph_bcnt))
+ return EINVAL; /*Ups. Out of range operation*/
/*OK lets deal with the first possible unaligned block*/
unalg = (off & (bdev->ph_bsize - 1));
- if(unalg){
+ if (unalg) {
- uint32_t wlen = (bdev->ph_bsize - unalg) > len ?
- len : (bdev->ph_bsize - unalg);
+ uint32_t wlen =
+ (bdev->ph_bsize - unalg) > len ? len : (bdev->ph_bsize - unalg);
r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1);
- if(r != EOK)
+ if (r != EOK)
return r;
memcpy(bdev->ph_bbuf + unalg, p, wlen);
r = bdev->bwrite(bdev, bdev->ph_bbuf, block_idx, 1);
- if(r != EOK)
+ if (r != EOK)
return r;
- p += wlen;
+ p += wlen;
len -= wlen;
block_idx++;
}
-
/*Aligned data*/
blen = len / bdev->ph_bsize;
r = bdev->bwrite(bdev, p, block_idx, blen);
- if(r != EOK)
+ if (r != EOK)
return r;
p += bdev->ph_bsize * blen;
@@ -313,11 +304,10 @@
block_idx += blen;
-
/*Rest of the data*/
- if(len){
+ if (len) {
r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1);
- if(r != EOK)
+ if (r != EOK)
return r;
memcpy(bdev->ph_bbuf, p, len);
@@ -324,7 +314,7 @@
r = bdev->bwrite(bdev, bdev->ph_bbuf, block_idx, 1);
- if(r != EOK)
+ if (r != EOK)
return r;
}
@@ -331,9 +321,8 @@
return r;
}
-
int ext4_block_readbytes(struct ext4_blockdev *bdev, uint64_t off, void *buf,
- uint32_t len)
+ uint32_t len)
{
uint64_t block_idx;
uint64_t block_end;
@@ -345,29 +334,29 @@
ext4_assert(bdev && buf);
- if(!(bdev->flags & EXT4_BDEV_INITIALIZED))
+ if (!(bdev->flags & EXT4_BDEV_INITIALIZED))
return EIO;
block_idx = off / bdev->ph_bsize;
block_end = block_idx + len / bdev->ph_bsize;
- if(!(block_end < bdev->ph_bcnt))
- return EINVAL; /*Ups. Out of range operation*/
+ if (!(block_end < bdev->ph_bcnt))
+ return EINVAL; /*Ups. Out of range operation*/
/*OK lets deal with the first possible unaligned block*/
unalg = (off & (bdev->ph_bsize - 1));
- if(unalg){
+ if (unalg) {
- uint32_t rlen = (bdev->ph_bsize - unalg) > len ?
- len : (bdev->ph_bsize - unalg);
+ uint32_t rlen =
+ (bdev->ph_bsize - unalg) > len ? len : (bdev->ph_bsize - unalg);
r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1);
- if(r != EOK)
+ if (r != EOK)
return r;
memcpy(p, bdev->ph_bbuf + unalg, rlen);
- p += rlen;
+ p += rlen;
len -= rlen;
block_idx++;
}
@@ -377,7 +366,7 @@
r = bdev->bread(bdev, p, block_idx, blen);
- if(r != EOK)
+ if (r != EOK)
return r;
p += bdev->ph_bsize * blen;
@@ -385,11 +374,10 @@
block_idx += blen;
-
/*Rest of the data*/
- if(len){
+ if (len) {
r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1);
- if(r != EOK)
+ if (r != EOK)
return r;
memcpy(p, bdev->ph_bbuf, len);
@@ -398,35 +386,34 @@
return r;
}
-int ext4_block_cache_write_back(struct ext4_blockdev *bdev,
- uint8_t on_off)
+int ext4_block_cache_write_back(struct ext4_blockdev *bdev, uint8_t on_off)
{
int r;
uint32_t i;
- if(on_off)
+ if (on_off)
bdev->cache_write_back++;
- if(!on_off && bdev->cache_write_back)
+ if (!on_off && bdev->cache_write_back)
bdev->cache_write_back--;
/*Flush all delayed cache blocks*/
- if(!bdev->cache_write_back){
+ if (!bdev->cache_write_back) {
for (i = 0; i < bdev->bc->cnt; ++i) {
/*Check if buffer free was delayed.*/
- if(!bdev->bc->free_delay[i])
+ if (!bdev->bc->free_delay[i])
continue;
/*Check reference counter.*/
- if(bdev->bc->refctr[i])
+ if (bdev->bc->refctr[i])
continue;
/*Buffer free was delayed and have no reference. Flush it.*/
r = ext4_blocks_set_direct(bdev,
- bdev->bc->data + bdev->bc->itemsize * i,
- bdev->bc->lba[i], 1);
- if(r != EOK)
+ bdev->bc->data + bdev->bc->itemsize * i,
+ bdev->bc->lba[i], 1);
+ if (r != EOK)
return r;
/*No delayed anymore*/
@@ -442,4 +429,3 @@
/**
* @}
*/
-
--- a/lwext4/ext4_blockdev.h
+++ b/lwext4/ext4_blockdev.h
@@ -40,20 +40,19 @@
#include <ext4_bcache.h>
#include <ext4_debug.h>
-
#include <stdbool.h>
#include <stdint.h>
/**@brief Initialization status flag*/
-#define EXT4_BDEV_INITIALIZED (1 << 0)
+#define EXT4_BDEV_INITIALIZED (1 << 0)
-
/**@brief Definiton of the simple block device.*/
-struct ext4_blockdev {
+struct ext4_blockdev
+{
/**@brief Open device function
* @param bdev block device.*/
- int (*open)(struct ext4_blockdev *bdev);
+ int (*open)(struct ext4_blockdev *bdev);
/**@brief Block read function.
* @param bdev block device
@@ -60,65 +59,64 @@
* @param buf output buffer
* @param blk_id block id
* @param blk_cnt block count*/
- int (*bread)(struct ext4_blockdev *bdev, void *buf,
- uint64_t blk_id, uint32_t blk_cnt);
+ int (*bread)(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id,
+ uint32_t blk_cnt);
/**@brief Block write function.
* @param buf input buffer
* @param blk_id block id
* @param blk_cnt block count*/
- int (*bwrite)(struct ext4_blockdev *bdev, const void *buf,
- uint64_t blk_id, uint32_t blk_cnt);
+ int (*bwrite)(struct ext4_blockdev *bdev, const void *buf, uint64_t blk_id,
+ uint32_t blk_cnt);
/**@brief Close device function.
* @param bdev block device.*/
- int (*close)(struct ext4_blockdev *bdev);
+ int (*close)(struct ext4_blockdev *bdev);
/**@brief Block cache.*/
- struct ext4_bcache *bc;
+ struct ext4_bcache *bc;
/**@brief Block size (bytes): physical*/
- uint32_t ph_bsize;
+ uint32_t ph_bsize;
/**@brief Block count: physical.*/
- uint64_t ph_bcnt;
+ uint64_t ph_bcnt;
/**@brief Block size buffer: physical.*/
- uint8_t *ph_bbuf;
+ uint8_t *ph_bbuf;
/**@brief Block size (bytes) logical*/
- uint32_t lg_bsize;
+ uint32_t lg_bsize;
/**@brief Block count: phisical.*/
- uint64_t lg_bcnt;
+ uint64_t lg_bcnt;
/**@brief Flags of te block device.*/
- uint32_t flags;
+ uint32_t flags;
/**@brief Cache write back mode refference counter.*/
- uint32_t cache_write_back;
+ uint32_t cache_write_back;
/**@brief Physical read counter*/
- uint32_t bread_ctr;
+ uint32_t bread_ctr;
/**@brief Physical write counter*/
- uint32_t bwrite_ctr;
+ uint32_t bwrite_ctr;
};
-
/**@brief Static initialization fo the block device.*/
-#define EXT4_BLOCKDEV_STATIC_INSTANCE(__name, __bsize, __bcnt, __open, \
- __bread, __bwrite, __close) \
- static uint8_t __name##_ph_bbuf[(__bsize)]; \
- static struct ext4_blockdev __name = { \
- .open = __open, \
- .bread = __bread, \
- .bwrite = __bwrite, \
- .close = __close, \
- .ph_bsize = __bsize, \
- .ph_bcnt = __bcnt, \
- .ph_bbuf = __name##_ph_bbuf, \
- }
+#define EXT4_BLOCKDEV_STATIC_INSTANCE(__name, __bsize, __bcnt, __open, \
+ __bread, __bwrite, __close) \
+ static uint8_t __name##_ph_bbuf[(__bsize)]; \
+ static struct ext4_blockdev __name = { \
+ .open = __open, \
+ .bread = __bread, \
+ .bwrite = __bwrite, \
+ .close = __close, \
+ .ph_bsize = __bsize, \
+ .ph_bcnt = __bcnt, \
+ .ph_bbuf = __name##_ph_bbuf, \
+ }
/**@brief Block device initialization.
* @param bdev block device descriptor
@@ -127,13 +125,11 @@
* @return standard error code*/
int ext4_block_init(struct ext4_blockdev *bdev);
-
/**@brief Binds a bcache to block device.
* @param bdev block device descriptor
* @param bc block cache descriptor
* @return standard error code*/
-int ext4_block_bind_bcache(struct ext4_blockdev *bdev,
- struct ext4_bcache *bc);
+int ext4_block_bind_bcache(struct ext4_blockdev *bdev, struct ext4_bcache *bc);
/**@brief Close block device
* @param bdev block device descriptor
@@ -140,13 +136,11 @@
* @return standard error code*/
int ext4_block_fini(struct ext4_blockdev *bdev);
-
/**@brief Set logical block size in block device.
* @param bdev block device descriptor
* @param lb_size ligical block size (in bytes)
* @return standard error code*/
-void ext4_block_set_lb_size(struct ext4_blockdev *bdev,
- uint64_t lb_bsize);
+void ext4_block_set_lb_size(struct ext4_blockdev *bdev, uint64_t lb_bsize);
/**@brief Block get function (through cache).
* @param bdev block device descriptor
@@ -154,7 +148,7 @@
* @param lba logical block address
* @return standard error code*/
int ext4_block_get(struct ext4_blockdev *bdev, struct ext4_block *b,
- uint64_t lba);
+ uint64_t lba);
/**@brief Block set procedure (through cache).
* @param bdev block device descriptor
@@ -162,16 +156,14 @@
* @return standard error code*/
int ext4_block_set(struct ext4_blockdev *bdev, struct ext4_block *b);
-
/**@brief Block read procedure (without cache)
* @param bdev block device descriptor
* @param buf output buffer
* @param lba logical block adderss
* @return standard error code*/
-int ext4_blocks_get_direct(struct ext4_blockdev *bdev, void *buf,
- uint64_t lba, uint32_t cnt);
+int ext4_blocks_get_direct(struct ext4_blockdev *bdev, void *buf, uint64_t lba,
+ uint32_t cnt);
-
/**@brief Block write procedure (without cache)
* @param bdev block device descriptor
* @param buf output buffer
@@ -178,7 +170,7 @@
* @param lba logical block address
* @return standard error code*/
int ext4_blocks_set_direct(struct ext4_blockdev *bdev, const void *buf,
- uint64_t lba, uint32_t cnt);
+ uint64_t lba, uint32_t cnt);
/**@brief Write to block device (by direct adress).
* @param bdev block device descriptor
@@ -187,10 +179,8 @@
* @param len length of the write nuffer
* @return EOK when sucess*/
int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off,
- const void *buf, uint32_t len);
+ const void *buf, uint32_t len);
-
-
/**@brief Read freom block device (by direct adress).
* @param bdev block device descriptor
* @param off byte offset in block device
@@ -197,8 +187,8 @@
* @param buf input buffer
* @param len length of the write nuffer
* @return EOK when sucess*/
-int ext4_block_readbytes(struct ext4_blockdev *bdev, uint64_t off,
- void *buf, uint32_t len);
+int ext4_block_readbytes(struct ext4_blockdev *bdev, uint64_t off, void *buf,
+ uint32_t len);
/**@brief Enable/disable write back cache mode
* @param bdev block device descriptor