summaryrefslogtreecommitdiff
path: root/blockdev
diff options
context:
space:
mode:
authorgkostka <kostka.grzegorz@gmail.com>2015-09-21 22:58:46 +0200
committergkostka <kostka.grzegorz@gmail.com>2015-09-22 00:19:51 +0200
commit107a9ee9b40fc55a65e84c755b5433e7ad465332 (patch)
tree65cc931a30c2de6cdb5c5855dae252d9c8099376 /blockdev
parentfd69372547ede31874302ac2921abb7e3b34a60c (diff)
Demo apps refactoring
Diffstat (limited to 'blockdev')
-rw-r--r--blockdev/CMakeLists.txt14
-rw-r--r--blockdev/chibios/sdc_lwext4.c184
-rw-r--r--blockdev/chibios/sdc_lwext4.h45
-rw-r--r--blockdev/chibios/spi_lwext4.c211
-rw-r--r--blockdev/chibios/spi_lwext4.h24
-rw-r--r--blockdev/chibios/timings.c49
-rw-r--r--blockdev/linux/ext4_filedev.c (renamed from blockdev/filedev/ext4_filedev.c)0
-rw-r--r--blockdev/linux/ext4_filedev.h (renamed from blockdev/filedev/ext4_filedev.h)0
-rw-r--r--blockdev/test_lwext4.c375
-rw-r--r--blockdev/test_lwext4.h59
-rw-r--r--blockdev/windows/io_raw.c (renamed from blockdev/filedev_win/io_raw.c)0
-rw-r--r--blockdev/windows/io_raw.h (renamed from blockdev/filedev_win/io_raw.h)1
12 files changed, 960 insertions, 2 deletions
diff --git a/blockdev/CMakeLists.txt b/blockdev/CMakeLists.txt
index 0f1f20b..2099d50 100644
--- a/blockdev/CMakeLists.txt
+++ b/blockdev/CMakeLists.txt
@@ -1,5 +1,15 @@
#Blockdev library
-aux_source_directory(filedev BLOCKDEV_SRC)
-aux_source_directory(filedev_win BLOCKDEV_SRC)
+
+if (BLOCKDEV_TYPE STREQUAL linux)
+aux_source_directory(linux BLOCKDEV_SRC)
+elseif (BLOCKDEV_TYPE STREQUAL windows)
+aux_source_directory(windows BLOCKDEV_SRC)
+elseif (BLOCKDEV_TYPE STREQUAL chibios)
+aux_source_directory(chibios BLOCKDEV_SRC)
+else()
+endif()
+
+aux_source_directory(. BLOCKDEV_SRC)
+
add_library(blockdev ${BLOCKDEV_SRC})
diff --git a/blockdev/chibios/sdc_lwext4.c b/blockdev/chibios/sdc_lwext4.c
new file mode 100644
index 0000000..de2f5ae
--- /dev/null
+++ b/blockdev/chibios/sdc_lwext4.c
@@ -0,0 +1,184 @@
+/*
+ * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "../chibios/sdc_lwext4.h"
+
+#include <config.h>
+#include <ext4_config.h>
+#include <ext4_blockdev.h>
+#include <ext4_errno.h>
+#include <stdio.h>
+#include <stdbool.h>
+#include <string.h>
+#include <fcntl.h>
+
+#include <hal.h>
+#include <sdc.h>
+
+
+/**@brief Block size.*/
+#define SDC_BLOCK_SIZE 512
+
+/**@brief MBR_block ID*/
+#define MBR_BLOCK_ID 0
+#define MBR_PART_TABLE_OFF 446
+
+struct part_tab_entry {
+ uint8_t status;
+ uint8_t chs1[3];
+ uint8_t type;
+ uint8_t chs2[3];
+ uint32_t first_lba;
+ uint32_t sectors;
+} __attribute__((packed));
+
+/**@brief Partition block offset*/
+static uint32_t part_offset;
+
+/**@brief IO timings*/
+struct sdc_io_timings {
+ uint64_t acc_bread;
+ uint64_t acc_bwrite;
+
+ uint32_t cnt_bread;
+ uint32_t cnt_bwrite;
+
+ uint32_t av_bread;
+ uint32_t av_bwrite;
+};
+
+static struct sdc_io_timings io_timings;
+
+void io_timings_clear(void)
+{
+ memset(&io_timings, 0, sizeof(struct sdc_io_timings));
+}
+
+const struct ext4_io_stats *io_timings_get(uint32_t time_sum_ms)
+{
+ static struct ext4_io_stats s;
+
+ s.io_read = (((float)io_timings.acc_bread * 100.0) / time_sum_ms);
+ s.io_read /= 1000.0;
+
+ s.io_write = (((float)io_timings.acc_bwrite * 100.0) / time_sum_ms);
+ s.io_write /= 1000.0;
+
+ s.cpu = 100.0 - s.io_read - s.io_write;
+
+ return &s;
+}
+
+/**********************BLOCKDEV INTERFACE**************************************/
+static int sdc_open(struct ext4_blockdev *bdev);
+static int sdc_bread(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id,
+ uint32_t blk_cnt);
+static int sdc_bwrite(struct ext4_blockdev *bdev, const void *buf,
+ uint64_t blk_id, uint32_t blk_cnt);
+static int sdc_close(struct ext4_blockdev *bdev);
+
+/******************************************************************************/
+EXT4_BLOCKDEV_STATIC_INSTANCE(_sdc, SDC_BLOCK_SIZE, 0, sdc_open, sdc_bread,
+ sdc_bwrite, sdc_close);
+
+/******************************************************************************/
+EXT4_BCACHE_STATIC_INSTANCE(_sdc_cache, CONFIG_BLOCK_DEV_CACHE_SIZE,
+ EXT_LOGICAL_BLOCK_SIZE);
+
+/******************************************************************************/
+
+static int sdc_open(struct ext4_blockdev *bdev)
+{
+ (void)bdev;
+
+ static uint8_t mbr[512];
+ struct part_tab_entry *part0;
+
+ sdcStart(&SDCD1, NULL);
+
+ if (sdcConnect(&SDCD1) != HAL_SUCCESS)
+ return EIO;
+
+ if (sdcRead(&SDCD1, 0, mbr, 1) != HAL_SUCCESS)
+ return EIO;
+
+ part0 = (struct part_tab_entry *)(mbr + MBR_PART_TABLE_OFF);
+
+ part_offset = part0->first_lba;
+ _sdc.ph_bcnt = SDCD1.capacity * SDC_BLOCK_SIZE;
+
+ return EOK;
+}
+
+static int sdc_bread(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id,
+ uint32_t blk_cnt)
+{
+ (void)bdev;
+ bool status;
+ uint64_t v = tim_get_us();
+
+ status = sdcRead(&SDCD1, blk_id, buf, blk_cnt);
+ if (status != HAL_SUCCESS)
+ return EIO;
+
+ io_timings.acc_bread += tim_get_us() - v;
+ io_timings.cnt_bread++;
+ io_timings.av_bread = io_timings.acc_bread / io_timings.cnt_bread;
+
+ return EOK;
+}
+
+static int sdc_bwrite(struct ext4_blockdev *bdev, const void *buf,
+ uint64_t blk_id, uint32_t blk_cnt)
+{
+ (void)bdev;
+ bool status;
+ uint64_t v = tim_get_us();
+
+ status = sdcWrite(&SDCD1, blk_id, buf, blk_cnt);
+ if (status != HAL_SUCCESS)
+ return EIO;
+
+ io_timings.acc_bwrite += tim_get_us() - v;
+ io_timings.cnt_bwrite++;
+ io_timings.av_bwrite = io_timings.acc_bwrite / io_timings.cnt_bwrite;
+
+ return EOK;
+}
+
+static int sdc_close(struct ext4_blockdev *bdev)
+{
+ (void)bdev;
+ return EOK;
+}
+
+/******************************************************************************/
+
+struct ext4_bcache *sdc_cache_get(void) { return &_sdc_cache; }
+
+struct ext4_blockdev *sdc_bdev_get(void) { return &_sdc; }
diff --git a/blockdev/chibios/sdc_lwext4.h b/blockdev/chibios/sdc_lwext4.h
new file mode 100644
index 0000000..f70194a
--- /dev/null
+++ b/blockdev/chibios/sdc_lwext4.h
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2014 Grzegorz Kostka (kostka.grzegorz@gmail.com)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef SDC_LWEXT4_H_
+#define SDC_LWEXT4_H_
+
+#include <config.h>
+#include <ext4_config.h>
+#include <ext4_blockdev.h>
+
+#include <stdint.h>
+#include <stdbool.h>
+
+/**@brief SDC cache get.*/
+struct ext4_bcache *sdc_cache_get(void);
+
+/**@brief SDC blockdev get.*/
+struct ext4_blockdev *sdc_bdev_get(void);
+
+
+#endif /* CHIBI_SDC_LWEXT4_H_ */
diff --git a/blockdev/chibios/spi_lwext4.c b/blockdev/chibios/spi_lwext4.c
new file mode 100644
index 0000000..a67ca79
--- /dev/null
+++ b/blockdev/chibios/spi_lwext4.c
@@ -0,0 +1,211 @@
+/*
+ * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "../chibios/spi_lwext4.h"
+
+#include <config.h>
+#include <ext4_config.h>
+#include <ext4_blockdev.h>
+#include <ext4_errno.h>
+#include <stdio.h>
+#include <stdbool.h>
+#include <string.h>
+#include <fcntl.h>
+
+#include <hal.h>
+#include <mmc_spi.h>
+
+
+#if HAL_USE_MMC_SPI
+extern MMCDriver MMCD1;
+#endif
+
+/**@brief Block size.*/
+#define SPI_BLOCK_SIZE 512
+
+/**@brief MBR_block ID*/
+#define MBR_BLOCK_ID 0
+#define MBR_PART_TABLE_OFF 446
+
+struct part_tab_entry {
+ uint8_t status;
+ uint8_t chs1[3];
+ uint8_t type;
+ uint8_t chs2[3];
+ uint32_t first_lba;
+ uint32_t sectors;
+} __attribute__((packed));
+
+/**@brief Partition block offset*/
+static uint32_t part_offset;
+
+/**@brief IO timings*/
+struct spi_io_timings {
+ uint64_t acc_bread;
+ uint64_t acc_bwrite;
+
+ uint32_t cnt_bread;
+ uint32_t cnt_bwrite;
+
+ uint32_t av_bread;
+ uint32_t av_bwrite;
+};
+
+static struct spi_io_timings io_timings;
+
+void io_timings_clear(void)
+{
+ memset(&io_timings, 0, sizeof(struct spi_io_timings));
+}
+
+const struct ext4_io_stats *io_timings_get(uint32_t time_sum_ms)
+{
+ static struct ext4_io_stats s;
+
+ s.io_read = (((float)io_timings.acc_bread * 100.0) / time_sum_ms);
+ s.io_read /= 1000.0;
+
+ s.io_write = (((float)io_timings.acc_bwrite * 100.0) / time_sum_ms);
+ s.io_write /= 1000.0;
+
+ s.cpu = 100.0 - s.io_read - s.io_write;
+
+ return &s;
+}
+
+/**********************BLOCKDEV INTERFACE**************************************/
+static int spi_open(struct ext4_blockdev *bdev);
+static int spi_bread(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id,
+ uint32_t blk_cnt);
+static int spi_bwrite(struct ext4_blockdev *bdev, const void *buf,
+ uint64_t blk_id, uint32_t blk_cnt);
+static int spi_close(struct ext4_blockdev *bdev);
+
+/******************************************************************************/
+EXT4_BLOCKDEV_STATIC_INSTANCE(_spi, SPI_BLOCK_SIZE, 0, spi_open, spi_bread,
+ spi_bwrite, spi_close);
+
+/******************************************************************************/
+EXT4_BCACHE_STATIC_INSTANCE(_spi_cache, CONFIG_BLOCK_DEV_CACHE_SIZE,
+ EXT_LOGICAL_BLOCK_SIZE);
+
+/******************************************************************************/
+
+static int spi_open(struct ext4_blockdev *bdev)
+{
+
+ (void)bdev;
+
+ static uint8_t mbr[512];
+ struct part_tab_entry *part0;
+
+ if (mmcConnect(&MMCD1) != HAL_SUCCESS)
+ return EIO;
+
+ if (mmcStartSequentialRead(&MMCD1, 0) != HAL_SUCCESS)
+ return EIO;
+
+ if (mmcSequentialRead(&MMCD1, mbr) != HAL_SUCCESS)
+ return EIO;
+
+ if (mmcStopSequentialRead(&MMCD1) != HAL_SUCCESS)
+ return EIO;
+
+ part0 = (struct part_tab_entry *)(mbr + MBR_PART_TABLE_OFF);
+
+ part_offset = part0->first_lba;
+ _spi.ph_bcnt = MMCD1.capacity * SPI_BLOCK_SIZE;
+
+ return EOK;
+}
+
+static int spi_bread(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id,
+ uint32_t blk_cnt)
+{
+ (void)bdev;
+ uint64_t v = tim_get_us();
+
+ if (mmcStartSequentialRead(&MMCD1, blk_id) != HAL_SUCCESS)
+ return EIO;
+
+ while (blk_cnt) {
+ if (mmcSequentialRead(&MMCD1, buf) != HAL_SUCCESS)
+ return EIO;
+
+ buf += SPI_BLOCK_SIZE;
+ blk_cnt--;
+ }
+
+ if (mmcStopSequentialRead(&MMCD1) != HAL_SUCCESS)
+ return EIO;
+
+ io_timings.acc_bread += tim_get_us() - v;
+ io_timings.cnt_bread++;
+ io_timings.av_bread = io_timings.acc_bread / io_timings.cnt_bread;
+
+ return EOK;
+}
+
+static int spi_bwrite(struct ext4_blockdev *bdev, const void *buf,
+ uint64_t blk_id, uint32_t blk_cnt)
+{
+ (void)bdev;
+ uint64_t v = tim_get_us();
+
+ if (mmcStartSequentialWrite(&MMCD1, blk_id) != HAL_SUCCESS)
+ return EIO;
+
+ while (blk_cnt) {
+ if (mmcSequentialWrite(&MMCD1, buf) != HAL_SUCCESS)
+ return EIO;
+
+ buf += SPI_BLOCK_SIZE;
+ blk_cnt--;
+ }
+
+ if (mmcStopSequentialWrite(&MMCD1) != HAL_SUCCESS)
+ return EIO;
+
+ io_timings.acc_bwrite += tim_get_us() - v;
+ io_timings.cnt_bwrite++;
+ io_timings.av_bwrite = io_timings.acc_bwrite / io_timings.cnt_bwrite;
+
+ return EOK;
+}
+
+static int spi_close(struct ext4_blockdev *bdev)
+{
+ (void)bdev;
+ return EOK;
+}
+
+/******************************************************************************/
+
+struct ext4_bcache *spi_cache_get(void) { return &_spi_cache; }
+
+struct ext4_blockdev *spi_bdev_get(void) { return &_spi; }
diff --git a/blockdev/chibios/spi_lwext4.h b/blockdev/chibios/spi_lwext4.h
new file mode 100644
index 0000000..292c4f0
--- /dev/null
+++ b/blockdev/chibios/spi_lwext4.h
@@ -0,0 +1,24 @@
+/*
+ * spi_lwext4.h
+ *
+ * Created on: 21 gru 2014
+ * Author: gko
+ */
+
+#ifndef SPI_LWEXT4_H_
+#define SPI_LWEXT4_H_
+
+#include <config.h>
+#include <ext4_config.h>
+#include <ext4_blockdev.h>
+
+#include <stdint.h>
+#include <stdbool.h>
+
+/**@brief SPI cache get.*/
+struct ext4_bcache *spi_cache_get(void);
+
+/**@brief SPI blockdev get.*/
+struct ext4_blockdev *spi_bdev_get(void);
+
+#endif /* SPI_LWEXT4_H_ */
diff --git a/blockdev/chibios/timings.c b/blockdev/chibios/timings.c
new file mode 100644
index 0000000..8f05d58
--- /dev/null
+++ b/blockdev/chibios/timings.c
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2014 Grzegorz Kostka (kostka.grzegorz@gmail.com)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "../chibios/timings.h"
+
+#include <config.h>
+
+#include <ch.h>
+#include <chvt.h>
+
+
+void tim_wait_ms(uint32_t v) { chThdSleepMilliseconds(v); }
+
+uint64_t tim_get_us(void)
+{
+ uint64_t v = chVTGetSystemTimeX();
+ return ST2US(v);
+}
+
+uint32_t tim_get_ms(void)
+{
+ uint32_t v = chVTGetSystemTimeX();
+ return ST2MS(v);
+}
diff --git a/blockdev/filedev/ext4_filedev.c b/blockdev/linux/ext4_filedev.c
index 14fae75..14fae75 100644
--- a/blockdev/filedev/ext4_filedev.c
+++ b/blockdev/linux/ext4_filedev.c
diff --git a/blockdev/filedev/ext4_filedev.h b/blockdev/linux/ext4_filedev.h
index 8f13d9a..8f13d9a 100644
--- a/blockdev/filedev/ext4_filedev.h
+++ b/blockdev/linux/ext4_filedev.h
diff --git a/blockdev/test_lwext4.c b/blockdev/test_lwext4.c
new file mode 100644
index 0000000..061ee8e
--- /dev/null
+++ b/blockdev/test_lwext4.c
@@ -0,0 +1,375 @@
+/*
+ * Copyright (c) 2014 Grzegorz Kostka (kostka.grzegorz@gmail.com)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <ext4.h>
+
+#include <stdio.h>
+#include <time.h>
+#include <inttypes.h>
+#include <string.h>
+
+#include "test_lwext4.h"
+
+/**@brief Read-write size*/
+#define READ_MAX_WRITE_SZIZE 1024 * 8
+
+/**@brief File read/write buffer*/
+static uint8_t rw_buff[READ_MAX_WRITE_SZIZE];
+
+/**@brief Block device handle.*/
+static struct ext4_blockdev *bd;
+
+/**@brief Block cache handle.*/
+static struct ext4_bcache *bc;
+
+static char *entry_to_str(uint8_t type)
+{
+ switch (type) {
+ case EXT4_DIRENTRY_UNKNOWN:
+ return "[unk] ";
+ case EXT4_DIRENTRY_REG_FILE:
+ return "[fil] ";
+ case EXT4_DIRENTRY_DIR:
+ return "[dir] ";
+ case EXT4_DIRENTRY_CHRDEV:
+ return "[cha] ";
+ case EXT4_DIRENTRY_BLKDEV:
+ return "[blk] ";
+ case EXT4_DIRENTRY_FIFO:
+ return "[fif] ";
+ case EXT4_DIRENTRY_SOCK:
+ return "[soc] ";
+ case EXT4_DIRENTRY_SYMLINK:
+ return "[sym] ";
+ default:
+ break;
+ }
+ return "[???]";
+}
+
+static clock_t get_ms(void) { return tim_get_ms(); }
+
+static void printf_io_timings(clock_t diff)
+{
+ const struct ext4_io_stats *stats = io_timings_get(diff);
+ printf("io_timings:\n");
+ printf(" io_read: %.3f%%\n", stats->io_read);
+ printf(" io_write: %.3f%%\n", stats->io_write);
+ printf(" io_cpu: %.3f%%\n", stats->cpu);
+}
+
+void test_lwext4_dir_ls(const char *path)
+{
+ char sss[255];
+ ext4_dir d;
+ const ext4_direntry *de;
+
+ printf("ls %s\n", path);
+
+ ext4_dir_open(&d, path);
+ de = ext4_dir_entry_next(&d);
+
+ while (de) {
+ memcpy(sss, de->name, de->name_length);
+ sss[de->name_length] = 0;
+ printf(" %s%s\n", entry_to_str(de->inode_type), sss);
+ de = ext4_dir_entry_next(&d);
+ }
+ ext4_dir_close(&d);
+}
+
+void test_lwext4_mp_stats(void)
+{
+ struct ext4_mount_stats stats;
+ ext4_mount_point_stats("/mp/", &stats);
+
+ printf("********************\n");
+ printf("ext4_mount_point_stats\n");
+ printf("inodes_count = %" PRIu32 "\n", stats.inodes_count);
+ printf("free_inodes_count = %" PRIu32 "\n", stats.free_inodes_count);
+ printf("blocks_count = %" PRIu32 "\n", (uint32_t)stats.blocks_count);
+ printf("free_blocks_count = %" PRIu32 "\n",
+ (uint32_t)stats.free_blocks_count);
+ printf("block_size = %" PRIu32 "\n", stats.block_size);
+ printf("block_group_count = %" PRIu32 "\n", stats.block_group_count);
+ printf("blocks_per_group= %" PRIu32 "\n", stats.blocks_per_group);
+ printf("inodes_per_group = %" PRIu32 "\n", stats.inodes_per_group);
+ printf("volume_name = %s\n", stats.volume_name);
+ printf("********************\n");
+}
+
+void test_lwext4_block_stats(void)
+{
+ if (!bd)
+ return;
+
+ printf("********************\n");
+ printf("ext4 blockdev stats\n");
+ printf("bdev->bread_ctr = %" PRIu32 "\n", bd->bread_ctr);
+ printf("bdev->bwrite_ctr = %" PRIu32 "\n", bd->bwrite_ctr);
+
+ printf("bcache->ref_blocks = %" PRIu32 "\n", bc->ref_blocks);
+ printf("bcache->max_ref_blocks = %" PRIu32 "\n", bc->max_ref_blocks);
+ printf("bcache->lru_ctr = %" PRIu32 "\n", bc->lru_ctr);
+
+ printf("\n");
+
+ uint32_t i;
+ for (i = 0; i < bc->cnt; ++i) {
+ printf("bcache->refctr[%" PRIu32 "]= %" PRIu32 "\n", i,
+ bc->refctr[i]);
+ }
+
+ printf("\n");
+ for (i = 0; i < bc->cnt; ++i) {
+ printf("bcache->lru_id[%" PRIu32 "] = %" PRIu32 "\n", i,
+ bc->lru_id[i]);
+ }
+
+ printf("\n");
+ for (i = 0; i < bc->cnt; ++i) {
+ printf("bcache->free_delay[%" PRIu32 "] = %d\n", i,
+ bc->free_delay[i]);
+ }
+
+ printf("\n");
+ for (i = 0; i < bc->cnt; ++i) {
+ printf("bcache->lba[%" PRIu32 "] = %" PRIu32 "\n", i,
+ (uint32_t)bc->lba[i]);
+ }
+
+ printf("********************\n");
+}
+
+bool test_lwext4_dir_test(int len)
+{
+ ext4_file f;
+ int r;
+ int i;
+ char path[64];
+ clock_t diff;
+ clock_t stop;
+ clock_t start;
+
+ printf("test_lwext4_dir_test: %d\n", len);
+ io_timings_clear();
+ start = get_ms();
+
+ printf("directory create: /mp/dir1\n");
+ r = ext4_dir_mk("/mp/dir1");
+ if (r != EOK) {
+ printf("ext4_dir_mk: rc = %d\n", r);
+ return false;
+ }
+
+ printf("add files to: /mp/dir1\n");
+ for (i = 0; i < len; ++i) {
+ sprintf(path, "/mp/dir1/f%d", i);
+ r = ext4_fopen(&f, path, "wb");
+ if (r != EOK) {
+ printf("ext4_fopen: rc = %d\n", r);
+ return false;
+ }
+ }
+
+ stop = get_ms();
+ diff = stop - start;
+ test_lwext4_dir_ls("/mp/dir1");
+ printf("test_lwext4_dir_test: time: %d ms\n", (int)diff);
+ printf("test_lwext4_dir_test: av: %d ms/entry\n", (int)diff / len);
+ printf_io_timings(diff);
+ return true;
+}
+
+static int verify_buf(const unsigned char *b, size_t len, unsigned char c)
+{
+ size_t i;
+ for (i = 0; i < len; ++i) {
+ if (b[i] != c)
+ return c - b[i];
+ }
+
+ return 0;
+}
+
+bool test_lwext4_file_test(uint32_t rw_szie, uint32_t rw_count)
+{
+ int r;
+ size_t size;
+ uint32_t i;
+ clock_t start;
+ clock_t stop;
+ clock_t diff;
+ uint32_t kbps;
+ uint64_t size_bytes;
+
+ ext4_file f;
+
+ printf("file_test:\n");
+ printf(" rw size: %" PRIu32 "\n", rw_szie);
+ printf(" rw count: %" PRIu32 "\n", rw_count);
+
+ /*Add hello world file.*/
+ r = ext4_fopen(&f, "/mp/hello.txt", "wb");
+ r = ext4_fwrite(&f, "Hello World !\n", strlen("Hello World !\n"), 0);
+ r = ext4_fclose(&f);
+
+ io_timings_clear();
+ start = get_ms();
+ r = ext4_fopen(&f, "/mp/test1", "wb");
+ if (r != EOK) {
+ printf("ext4_fopen ERROR = %d\n", r);
+ return false;
+ }
+
+ printf("ext4_write: %" PRIu32 " * %" PRIu32 " ...\n", rw_szie,
+ rw_count);
+ for (i = 0; i < rw_count; ++i) {
+
+ memset(rw_buff, i % 10 + '0', rw_szie);
+
+ r = ext4_fwrite(&f, rw_buff, rw_szie, &size);
+
+ if ((r != EOK) || (size != rw_szie))
+ break;
+ }
+
+ if (i != rw_count) {
+ printf(" file_test: rw_count = %" PRIu32 "\n", i);
+ return false;
+ }
+
+ stop = get_ms();
+ diff = stop - start;
+ size_bytes = rw_szie * rw_count;
+ size_bytes = (size_bytes * 1000) / 1024;
+ kbps = (size_bytes) / (diff + 1);
+ printf(" write time: %d ms\n", (int)diff);
+ printf(" write speed: %" PRIu32 " KB/s\n", kbps);
+ printf_io_timings(diff);
+ r = ext4_fclose(&f);
+
+ io_timings_clear();
+ start = get_ms();
+ r = ext4_fopen(&f, "/mp/test1", "r+");
+ if (r != EOK) {
+ printf("ext4_fopen ERROR = %d\n", r);
+ return false;
+ }
+
+ printf("ext4_read: %" PRIu32 " * %" PRIu32 " ...\n", rw_szie, rw_count);
+
+ for (i = 0; i < rw_count; ++i) {
+ r = ext4_fread(&f, rw_buff, rw_szie, &size);
+
+ if ((r != EOK) || (size != rw_szie))
+ break;
+
+ if (verify_buf(rw_buff, rw_szie, i % 10 + '0'))
+ break;
+ }
+
+ if (i != rw_count) {
+ printf(" file_test: rw_count = %" PRIu32 "\n", i);
+ return false;
+ }
+
+ stop = get_ms();
+ diff = stop - start;
+ size_bytes = rw_szie * rw_count;
+ size_bytes = (size_bytes * 1000) / 1024;
+ kbps = (size_bytes) / (diff + 1);
+ printf(" read time: %d ms\n", (int)diff);
+ printf(" read speed: %d KB/s\n", (int)kbps);
+ printf_io_timings(diff);
+
+ r = ext4_fclose(&f);
+ return true;
+}
+void test_lwext4_cleanup(void)
+{
+ clock_t start;
+ clock_t stop;
+ clock_t diff;
+
+ printf("\ncleanup:\n");
+ ext4_fremove("/mp/hello.txt");
+
+ printf("remove /mp/test1\n");
+ ext4_fremove("/mp/test1");
+
+ printf("remove /mp/dir1\n");
+ io_timings_clear();
+ start = get_ms();
+ ext4_dir_rm("/mp/dir1");
+ stop = get_ms();
+ diff = stop - start;
+ printf("cleanup: time: %d ms\n", (int)diff);
+ printf_io_timings(diff);
+}
+
+bool test_lwext4_mount(struct ext4_blockdev *bdev, struct ext4_bcache *bcache)
+{
+ int r;
+
+ bc = bcache;
+ bd = bdev;
+
+ if (!bd) {
+ printf("test_lwext4_mount: no block device\n");
+ return false;
+ }
+
+ ext4_dmask_set(EXT4_DEBUG_ALL);
+
+ r = ext4_device_register(bd, bc ? bc : 0, "ext4_fs");
+ if (r != EOK) {
+ printf("ext4_device_register: rc = %d\n", r);
+ return false;
+ }
+
+ r = ext4_mount("ext4_fs", "/mp/");
+ if (r != EOK) {
+ printf("ext4_mount: rc = %d\n", r);
+ return false;
+ }
+
+ ext4_cache_write_back("/mp/", 1);
+ return true;
+}
+
+bool test_lwext4_umount(void)
+{
+ ext4_cache_write_back("/mp/", 0);
+ int r = ext4_umount("/mp/");
+ if (r != EOK) {
+ printf("ext4_umount: fail %d", r);
+ return false;
+ }
+ return true;
+}
diff --git a/blockdev/test_lwext4.h b/blockdev/test_lwext4.h
new file mode 100644
index 0000000..0ed04ac
--- /dev/null
+++ b/blockdev/test_lwext4.h
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2014 Grzegorz Kostka (kostka.grzegorz@gmail.com)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef TEST_LWEXT4_H_
+#define TEST_LWEXT4_H_
+
+#include <stdint.h>
+#include <stdbool.h>
+
+void test_lwext4_dir_ls(const char *path);
+void test_lwext4_mp_stats(void);
+void test_lwext4_block_stats(void);
+bool test_lwext4_dir_test(int len);
+bool test_lwext4_file_test(uint32_t rw_szie, uint32_t rw_count);
+void test_lwext4_cleanup(void);
+
+bool test_lwext4_mount(struct ext4_blockdev *bdev, struct ext4_bcache *bcache);
+bool test_lwext4_umount(void);
+
+void tim_wait_ms(uint32_t v);
+
+uint32_t tim_get_ms(void);
+uint64_t tim_get_us(void);
+
+struct ext4_io_stats {
+ float io_read;
+ float io_write;
+ float cpu;
+};
+
+void io_timings_clear(void);
+const struct ext4_io_stats *io_timings_get(uint32_t time_sum_ms);
+
+#endif /* TEST_LWEXT4_H_ */
diff --git a/blockdev/filedev_win/io_raw.c b/blockdev/windows/io_raw.c
index 902cd4f..902cd4f 100644
--- a/blockdev/filedev_win/io_raw.c
+++ b/blockdev/windows/io_raw.c
diff --git a/blockdev/filedev_win/io_raw.h b/blockdev/windows/io_raw.h
index cfd6561..275fe22 100644
--- a/blockdev/filedev_win/io_raw.h
+++ b/blockdev/windows/io_raw.h
@@ -40,4 +40,5 @@ struct ext4_blockdev *ext4_io_raw_dev_get(void);
/**@brief Set filrname to open.*/
void ext4_io_raw_filename(const char *n);
+
#endif /* IO_RAW_H_ */