ext4_journal: code logic optimization. See below.
[lwext4.git] / lwext4 / ext4_blockdev.h
index e0d6620d8acfdd9d1500682ec61dbab42db13d19..f5329ec8b34484a3e1d26d4deeb9cb9fc3dadbdc 100644 (file)
@@ -42,17 +42,13 @@ extern "C" {
 
 #include "ext4_config.h"
 #include "ext4_bcache.h"
+#include "ext4_trans.h"
 #include "ext4_debug.h"
 
 #include <stdbool.h>
 #include <stdint.h>
 
-/**@brief   Initialization status flag*/
-#define EXT4_BDEV_INITIALIZED (1 << 0)
-
-/**@brief   Definition of the simple block device.*/
-struct ext4_blockdev {
-
+struct ext4_blockdev_iface {
        /**@brief   Open device function
         * @param   bdev block device.*/
        int (*open)(struct ext4_blockdev *bdev);
@@ -76,8 +72,15 @@ struct ext4_blockdev {
         * @param   bdev block device.*/
        int (*close)(struct ext4_blockdev *bdev);
 
-       /**@brief   Block cache.*/
-       struct ext4_bcache *bc;
+       /**@brief   Lock block device. Required in multi partition mode
+        *          operations. Not mandatory field.
+        * @param   bdev block device.*/
+       int (*lock)(struct ext4_blockdev *bdev);
+
+       /**@brief   Unlock block device. Required in multi partition mode
+        *          operations. Not mandatory field.
+        * @param   bdev block device.*/
+       int (*unlock)(struct ext4_blockdev *bdev);
 
        /**@brief   Block size (bytes): physical*/
        uint32_t ph_bsize;
@@ -88,37 +91,62 @@ struct ext4_blockdev {
        /**@brief   Block size buffer: physical*/
        uint8_t *ph_bbuf;
 
+       /**@brief   Reference counter to block device interface*/
+       uint32_t ph_refctr;
+
+       /**@brief   Physical read counter*/
+       uint32_t bread_ctr;
+
+       /**@brief   Physical write counter*/
+       uint32_t bwrite_ctr;
+};
+
+/**@brief   Definition of the simple block device.*/
+struct ext4_blockdev {
+       /**@brief Block device interface*/
+       struct ext4_blockdev_iface *bdif;
+
+       /**@brief Offset in bdif. For multi partition mode.*/
+       uint64_t part_offset;
+
+       /**@brief Part size in bdif. For multi partition mode.*/
+       uint64_t part_size;
+
+       /**@brief   Block cache.*/
+       struct ext4_bcache *bc;
+
        /**@brief   Block size (bytes) logical*/
        uint32_t lg_bsize;
 
-       /**@brief   Block count: physical*/
+       /**@brief   Block count: logical*/
        uint64_t lg_bcnt;
 
-       /**@brief   Flags of block device*/
-       uint32_t flags;
-
        /**@brief   Cache write back mode reference counter*/
        uint32_t cache_write_back;
 
-       /**@brief   Physical read counter*/
-       uint32_t bread_ctr;
-
-       /**@brief   Physical write counter*/
-       uint32_t bwrite_ctr;
+       /**@brief   The filesystem this block device belongs to. */
+       struct ext4_fs *fs;
 };
 
 /**@brief   Static initialization of the block device.*/
-#define EXT4_BLOCKDEV_STATIC_INSTANCE(__name, __bsize, __bcnt, __open,         \
-                                     __bread, __bwrite, __close)              \
+#define EXT4_BLOCKDEV_STATIC_INSTANCE(__name, __bsize, __bcnt, __open, __bread,\
+                                     __bwrite, __close, __lock, __unlock)     \
        static uint8_t __name##_ph_bbuf[(__bsize)];                            \
+       static struct ext4_blockdev_iface __name##_iface = {                   \
+               .open = __open,                                                \
+               .bread = __bread,                                              \
+               .bwrite = __bwrite,                                            \
+               .close = __close,                                              \
+               .lock = __lock,                                                \
+               .unlock = __unlock,                                            \
+               .ph_bsize = __bsize,                                           \
+               .ph_bcnt = __bcnt,                                             \
+               .ph_bbuf = __name##_ph_bbuf,                                   \
+       };                                                                     \
        static struct ext4_blockdev __name = {                                 \
-           .open = __open,                                                    \
-           .bread = __bread,                                                  \
-           .bwrite = __bwrite,                                                \
-           .close = __close,                                                  \
-           .ph_bsize = __bsize,                                               \
-           .ph_bcnt = __bcnt,                                                 \
-           .ph_bbuf = __name##_ph_bbuf,                                       \
+               .bdif = &__name##_iface,                                       \
+               .part_offset = 0,                                              \
+               .part_size =  (__bcnt) * (__bsize),                            \
        }
 
 /**@brief   Block device initialization.
@@ -145,6 +173,13 @@ int ext4_block_fini(struct ext4_blockdev *bdev);
  * @return  standard error code*/
 int ext4_block_flush_buf(struct ext4_blockdev *bdev, struct ext4_buf *buf);
 
+/**@brief   Flush data in buffer of given lba to disk,
+ *          if that buffer exists in block cache.
+ * @param   bdev block device descriptor
+ * @param   lba logical block address
+ * @return  standard error code*/
+int ext4_block_flush_lba(struct ext4_blockdev *bdev, uint64_t lba);
+
 /**@brief   Set logical block size in block device.
  * @param   bdev block device descriptor
  * @param   lb_size logical block size (in bytes)
@@ -207,6 +242,11 @@ int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off,
 int ext4_block_readbytes(struct ext4_blockdev *bdev, uint64_t off, void *buf,
                         uint32_t len);
 
+/**@brief   Flush all dirty buffers to disk
+ * @param   bdev block device descriptor
+ * @return  standard error code*/
+int ext4_block_cache_flush(struct ext4_blockdev *bdev);
+
 /**@brief   Enable/disable write back cache mode
  * @param   bdev block device descriptor
  * @param   on_off