Move kfile interface to the io/ directory.
[bertos.git] / bertos / io / kblock.c
index 32c36cf356b5a10cc7188f774da589b7e2abe296..65f427903a11ba018e92acbd1d2a2600bf57e8e1 100644 (file)
 
 #include "kblock.h"
 
+#define LOG_LEVEL LOG_LVL_ERR
+#define LOG_FORMAT LOG_FMT_VERBOSE
 
-static void *kblock_swMap(struct KBlock *b, size_t offset, size_t size)
+#include <cfg/log.h>
+
+INLINE size_t kblock_readDirect(struct KBlock *b, block_idx_t index, void *buf, size_t offset, size_t size)
+{
+       KB_ASSERT_METHOD(b, readDirect);
+       return b->priv.vt->readDirect(b, b->priv.blk_start + index, buf, offset, size);
+}
+
+INLINE size_t kblock_readBuf(struct KBlock *b, void *buf, size_t offset, size_t size)
+{
+       KB_ASSERT_METHOD(b, readBuf);
+       ASSERT(offset + size <= b->blk_size);
+
+       return b->priv.vt->readBuf(b, buf, offset, size);
+}
+
+INLINE size_t kblock_writeBuf(struct KBlock *b, const void *buf, size_t offset, size_t size)
+{
+       KB_ASSERT_METHOD(b, writeBuf);
+       ASSERT(offset + size <= b->blk_size);
+       return b->priv.vt->writeBuf(b, buf, offset, size);
+}
+
+INLINE int kblock_load(struct KBlock *b, block_idx_t index)
+{
+       KB_ASSERT_METHOD(b, load);
+       ASSERT(index < b->blk_cnt);
+
+       LOG_INFO("index %d\n", index);
+       return b->priv.vt->load(b, b->priv.blk_start + index);
+}
+
+INLINE int kblock_store(struct KBlock *b, block_idx_t index)
+{
+       KB_ASSERT_METHOD(b, store);
+       ASSERT(index < b->blk_cnt);
+
+       LOG_INFO("index %d\n", index);
+       return b->priv.vt->store(b, b->priv.blk_start + index);
+}
+
+
+
+size_t kblock_read(struct KBlock *b, block_idx_t idx, void *buf, size_t offset, size_t size)
 {
-       return (kblock_readBuf(b, b->priv.pagebuf, offset, size) == size) ? b->priv.pagebuf : NULL;
+       ASSERT(b);
+       ASSERT(buf);
+       ASSERT(offset + size <= b->blk_size);
+       LOG_INFO("blk_idx %d, offset %d, size %d\n", idx, offset, size);
+
+       if (idx == b->priv.curr_blk)
+               return kblock_readBuf(b, buf, offset, size);
+       else
+               return kblock_readDirect(b, idx, buf, offset, size);
 }
 
 
-static int kblock_swUnmap(struct KBlock *b, size_t offset, size_t size)
+int kblock_flush(struct KBlock *b)
 {
-       return (kblock_writeBuf(b, b->priv.pagebuf, offset, size) == size) ? 0 : EOF;
+       ASSERT(b);
+
+       if (b->priv.cache_dirty)
+       {
+               LOG_INFO("flushing block %d\n", b->priv.curr_blk);
+               if (kblock_store(b, b->priv.curr_blk) == 0)
+                       b->priv.cache_dirty = false;
+               else
+                       return EOF;
+       }
+       return 0;
 }
 
 
-void *kblock_unsupportedMap(struct KBlock *b, UNUSED_ARG(size_t, offset), UNUSED_ARG(size_t, size))
+static bool kblock_loadPage(struct KBlock *b, block_idx_t idx)
 {
-       LOG_WARN("This driver does not support block mapping: use kblock_addMapping() to add generic mapping functionality.\n");
-       b->priv.flags |= BV(KBS_ERR_MAP_NOT_AVAILABLE);
-       return NULL;
+       ASSERT(b);
+
+       if (idx != b->priv.curr_blk)
+       {
+               LOG_INFO("loading block %d\n", idx);
+               if (kblock_flush(b) != 0 || kblock_load(b, idx) != 0)
+                               return false;
+
+               b->priv.curr_blk = idx;
+       }
+       return true;
 }
 
-void kblock_addMapping(struct KBlock *dev, void *buf, size_t size)
+
+size_t kblock_write(struct KBlock *b, block_idx_t idx, const void *buf, size_t offset, size_t size)
 {
+       ASSERT(b);
        ASSERT(buf);
-       ASSERT(size);
-       ASSERT(dev);
-       
-       dev->vt->map = kblock_swMap;
-       dev->vt->unmap = kblock_swUnmap;
-
-       dev->priv.pagebuf = buf;
-       dev->priv.pagebuf_size = size;
+       ASSERT(offset + size <= b->blk_size);
+
+       LOG_INFO("blk_idx %d, offset %d, size %d\n", idx, offset, size);
+
+       if (!kblock_loadPage(b, idx))
+               return 0;
+
+       b->priv.cache_dirty = true;
+       return kblock_writeBuf(b, buf, offset, size);
 }
+
+int kblock_copy(struct KBlock *b, block_idx_t idx1, block_idx_t idx2)
+{
+       ASSERT(b);
+
+       if (!kblock_loadPage(b, idx1))
+               return EOF;
+
+       b->priv.curr_blk = idx2;
+       b->priv.cache_dirty = true;
+       return 0;
+}
+