From 3f5d618c89fa6c4323d6cb83e8e5dca745171c0c Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Wed, 24 Jan 2018 17:58:20 -0600 Subject: [PATCH 1/3] bd: Add sync function to the block device API /** Ensure data on storage is in sync with the driver * * @return 0 on success or a negative error code on failure */ virtual int sync() --- features/filesystem/bd/BlockDevice.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/features/filesystem/bd/BlockDevice.h b/features/filesystem/bd/BlockDevice.h index da46c7b727..b7dbbcce15 100644 --- a/features/filesystem/bd/BlockDevice.h +++ b/features/filesystem/bd/BlockDevice.h @@ -59,6 +59,15 @@ public: */ virtual int deinit() = 0; + /** Ensure data on storage is in sync with the driver + * + * @return 0 on success or a negative error code on failure + */ + virtual int sync() + { + return 0; + } + /** Read blocks from a block device * * If a failure occurs, it is not possible to determine how many bytes succeeded From a4f8af9d5bd5c7c762f1776719d6c6910c6bb15d Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Wed, 24 Jan 2018 18:07:35 -0600 Subject: [PATCH 2/3] bd: Adopted the sync function in the util block devices --- features/filesystem/bd/ChainingBlockDevice.cpp | 12 ++++++++++++ features/filesystem/bd/ChainingBlockDevice.h | 6 ++++++ features/filesystem/bd/ExhaustibleBlockDevice.cpp | 5 +++++ features/filesystem/bd/ExhaustibleBlockDevice.h | 6 ++++++ features/filesystem/bd/MBRBlockDevice.cpp | 5 +++++ features/filesystem/bd/MBRBlockDevice.h | 6 ++++++ features/filesystem/bd/ObservingBlockDevice.cpp | 5 +++++ features/filesystem/bd/ObservingBlockDevice.h | 6 ++++++ features/filesystem/bd/ProfilingBlockDevice.cpp | 5 +++++ features/filesystem/bd/ProfilingBlockDevice.h | 6 ++++++ features/filesystem/bd/ReadOnlyBlockDevice.cpp | 5 +++++ features/filesystem/bd/ReadOnlyBlockDevice.h | 6 ++++++ features/filesystem/bd/SlicingBlockDevice.cpp | 5 +++++ features/filesystem/bd/SlicingBlockDevice.h | 6 ++++++ 14 files changed, 84 insertions(+) diff --git a/features/filesystem/bd/ChainingBlockDevice.cpp b/features/filesystem/bd/ChainingBlockDevice.cpp index f9f5c9a29f..b28ad9493e 100644 --- a/features/filesystem/bd/ChainingBlockDevice.cpp +++ b/features/filesystem/bd/ChainingBlockDevice.cpp @@ -84,6 +84,18 @@ int ChainingBlockDevice::deinit() return 0; } +int ChainingBlockDevice::sync() +{ + for (size_t i = 0; i < _bd_count; i++) { + int err = _bds[i]->sync(); + if (err) { + return err; + } + } + + return 0; +} + int ChainingBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size) { MBED_ASSERT(is_valid_read(addr, size)); diff --git a/features/filesystem/bd/ChainingBlockDevice.h b/features/filesystem/bd/ChainingBlockDevice.h index 63b4a5d020..d3cb737b99 100644 --- a/features/filesystem/bd/ChainingBlockDevice.h +++ b/features/filesystem/bd/ChainingBlockDevice.h @@ -85,6 +85,12 @@ public: */ virtual int deinit(); + /** Ensure data on storage is in sync with the driver + * + * @return 0 on success or a negative error code on failure + */ + virtual int sync(); + /** Read blocks from a block device * * @param buffer Buffer to write blocks to diff --git a/features/filesystem/bd/ExhaustibleBlockDevice.cpp b/features/filesystem/bd/ExhaustibleBlockDevice.cpp index 0b16a9217f..fb264e51fe 100644 --- a/features/filesystem/bd/ExhaustibleBlockDevice.cpp +++ b/features/filesystem/bd/ExhaustibleBlockDevice.cpp @@ -53,6 +53,11 @@ int ExhaustibleBlockDevice::deinit() return _bd->deinit(); } +int ExhaustibleBlockDevice::sync() +{ + return _bd->sync(); +} + int ExhaustibleBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size) { return _bd->read(buffer, addr, size); diff --git a/features/filesystem/bd/ExhaustibleBlockDevice.h b/features/filesystem/bd/ExhaustibleBlockDevice.h index 8fc3918317..98f3120178 100644 --- a/features/filesystem/bd/ExhaustibleBlockDevice.h +++ b/features/filesystem/bd/ExhaustibleBlockDevice.h @@ -70,6 +70,12 @@ public: */ virtual int deinit(); + /** Ensure data on storage is in sync with the driver + * + * @return 0 on success or a negative error code on failure + */ + virtual int sync(); + /** Read blocks from a block device * * @param buffer Buffer to read blocks into diff --git a/features/filesystem/bd/MBRBlockDevice.cpp b/features/filesystem/bd/MBRBlockDevice.cpp index 134ce9848b..145d233ccb 100644 --- a/features/filesystem/bd/MBRBlockDevice.cpp +++ b/features/filesystem/bd/MBRBlockDevice.cpp @@ -234,6 +234,11 @@ int MBRBlockDevice::deinit() return _bd->deinit(); } +int MBRBlockDevice::sync() +{ + return _bd->sync(); +} + int MBRBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size) { MBED_ASSERT(is_valid_read(addr, size)); diff --git a/features/filesystem/bd/MBRBlockDevice.h b/features/filesystem/bd/MBRBlockDevice.h index 48964fba1c..2eb7d91cfd 100644 --- a/features/filesystem/bd/MBRBlockDevice.h +++ b/features/filesystem/bd/MBRBlockDevice.h @@ -139,6 +139,12 @@ public: */ virtual int deinit(); + /** Ensure data on storage is in sync with the driver + * + * @return 0 on success or a negative error code on failure + */ + virtual int sync(); + /** Read blocks from a block device * * @param buffer Buffer to read blocks into diff --git a/features/filesystem/bd/ObservingBlockDevice.cpp b/features/filesystem/bd/ObservingBlockDevice.cpp index ea6dc5da15..2196fd8f4d 100644 --- a/features/filesystem/bd/ObservingBlockDevice.cpp +++ b/features/filesystem/bd/ObservingBlockDevice.cpp @@ -51,6 +51,11 @@ int ObservingBlockDevice::deinit() return _bd->deinit(); } +int ObservingBlockDevice::sync() +{ + return _bd->sync(); +} + int ObservingBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size) { return _bd->read(buffer, addr, size); diff --git a/features/filesystem/bd/ObservingBlockDevice.h b/features/filesystem/bd/ObservingBlockDevice.h index 1e8c1942cb..d6048d53a6 100644 --- a/features/filesystem/bd/ObservingBlockDevice.h +++ b/features/filesystem/bd/ObservingBlockDevice.h @@ -56,6 +56,12 @@ public: */ virtual int deinit(); + /** Ensure data on storage is in sync with the driver + * + * @return 0 on success or a negative error code on failure + */ + virtual int sync(); + /** Read blocks from a block device * * @param buffer Buffer to read blocks into diff --git a/features/filesystem/bd/ProfilingBlockDevice.cpp b/features/filesystem/bd/ProfilingBlockDevice.cpp index 6baa24cb90..ff344b058f 100644 --- a/features/filesystem/bd/ProfilingBlockDevice.cpp +++ b/features/filesystem/bd/ProfilingBlockDevice.cpp @@ -35,6 +35,11 @@ int ProfilingBlockDevice::deinit() return _bd->deinit(); } +int ProfilingBlockDevice::sync() +{ + return _bd->sync(); +} + int ProfilingBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size) { int err = _bd->read(b, addr, size); diff --git a/features/filesystem/bd/ProfilingBlockDevice.h b/features/filesystem/bd/ProfilingBlockDevice.h index c019fa7300..6be8655267 100644 --- a/features/filesystem/bd/ProfilingBlockDevice.h +++ b/features/filesystem/bd/ProfilingBlockDevice.h @@ -71,6 +71,12 @@ public: */ virtual int deinit(); + /** Ensure data on storage is in sync with the driver + * + * @return 0 on success or a negative error code on failure + */ + virtual int sync(); + /** Read blocks from a block device * * @param buffer Buffer to read blocks into diff --git a/features/filesystem/bd/ReadOnlyBlockDevice.cpp b/features/filesystem/bd/ReadOnlyBlockDevice.cpp index 84ab36ff2d..0a727fb9b7 100644 --- a/features/filesystem/bd/ReadOnlyBlockDevice.cpp +++ b/features/filesystem/bd/ReadOnlyBlockDevice.cpp @@ -45,6 +45,11 @@ int ReadOnlyBlockDevice::deinit() return _bd->deinit(); } +int ReadOnlyBlockDevice::sync() +{ + return _bd->sync(); +} + int ReadOnlyBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size) { return _bd->read(buffer, addr, size); diff --git a/features/filesystem/bd/ReadOnlyBlockDevice.h b/features/filesystem/bd/ReadOnlyBlockDevice.h index 23c8f40e8d..a9c32b7fcd 100644 --- a/features/filesystem/bd/ReadOnlyBlockDevice.h +++ b/features/filesystem/bd/ReadOnlyBlockDevice.h @@ -49,6 +49,12 @@ public: */ virtual int deinit(); + /** Ensure data on storage is in sync with the driver + * + * @return 0 on success or a negative error code on failure + */ + virtual int sync(); + /** Read blocks from a block device * * @param buffer Buffer to read blocks into diff --git a/features/filesystem/bd/SlicingBlockDevice.cpp b/features/filesystem/bd/SlicingBlockDevice.cpp index 249acf81b6..5da23873e2 100644 --- a/features/filesystem/bd/SlicingBlockDevice.cpp +++ b/features/filesystem/bd/SlicingBlockDevice.cpp @@ -64,6 +64,11 @@ int SlicingBlockDevice::deinit() return _bd->deinit(); } +int SlicingBlockDevice::sync() +{ + return _bd->sync(); +} + int SlicingBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size) { MBED_ASSERT(is_valid_read(addr, size)); diff --git a/features/filesystem/bd/SlicingBlockDevice.h b/features/filesystem/bd/SlicingBlockDevice.h index e219f77480..f591699a51 100644 --- a/features/filesystem/bd/SlicingBlockDevice.h +++ b/features/filesystem/bd/SlicingBlockDevice.h @@ -77,6 +77,12 @@ public: */ virtual int deinit(); + /** Ensure data on storage is in sync with the driver + * + * @return 0 on success or a negative error code on failure + */ + virtual int sync(); + /** Read blocks from a block device * * @param buffer Buffer to read blocks into From 6e5f2439a30b6cac6fa7b5ca8175684ff0039803 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Wed, 24 Jan 2018 18:07:47 -0600 Subject: [PATCH 3/3] littlefs: Adopted the block device sync function Required to garuntee that data is flushed all the way down to the disk level when a file is synced or closed. --- features/filesystem/littlefs/LittleFileSystem.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/features/filesystem/littlefs/LittleFileSystem.cpp b/features/filesystem/littlefs/LittleFileSystem.cpp index 9278ca4f09..d60d972f7c 100644 --- a/features/filesystem/littlefs/LittleFileSystem.cpp +++ b/features/filesystem/littlefs/LittleFileSystem.cpp @@ -102,7 +102,8 @@ static int lfs_bd_erase(const struct lfs_config *c, lfs_block_t block) static int lfs_bd_sync(const struct lfs_config *c) { - return 0; + BlockDevice *bd = (BlockDevice *)c->context; + return bd->sync(); }