Compare commits

...

32 Commits

Author SHA1 Message Date
Christopher Haster
bad02b6003 Changed release script to generate drafts 2018-10-20 12:32:41 -05:00
Christopher Haster
0d7ae0fef4 Added a handful of links to related projects
Interesting open-source projects that I've ran into around embedded
storage. May be interesting to others in the embedded space.

Added mklfs, SPIFFS, and Dhara.

Also a thanks to jolivepetrus for posting the mklfs tool he put
together.
2018-10-14 11:45:38 -05:00
Christopher Haster
0bb1f7af17 Modified release script to create notes only on minor releases
Before, release notes with a list of changes were created every
patch release. Unfortunately, it looks like this will create a lot of
noise on github, with a notification every patch release, which may be
as often as every time a PR is merged.

Rather than creating all of this noise for relatively uninteresting
changes, the script will now stick to simple tags, and create the
release notes only on minor releases.

I think this is what several of you were originally suggesting,
sorry about the journey, at least I learned a lot.
2018-09-29 12:31:27 -05:00
Christopher Haster
447d89cbd8 Merge pull request #109 from OTAkeys/pr/fix-sign-compare
Fix -Wsign-compare error
2018-09-29 12:29:54 -05:00
Vincent Dupont
28d2d96a83 Fix -Wsign-compare error 2018-09-29 11:33:19 -05:00
Christopher Haster
cb62bf2188 Fixed release script issue with fetching recent tags
Fetching all tags was triggering the pagination system inside the github
API. This prevent version tags from being found.

Modified to use the version tag prefix in the ref lookup, however this
still may cause an issue if there are still enough patch releases to trigger
pagination.

Simpleish solution is to grab the link header to jump to the last page,
since pagination results appear to be in sorted order.
2018-09-27 14:46:12 -05:00
Christopher Haster
646b1b5a6c Added -Wjump-misses-init and fixed uninitialized warnings 2018-09-26 18:58:54 -05:00
Christopher Haster
1b7a15599e Merge pull request #106 from conkerkh/master
If stats file doesn't exist lfs_emubd_create will fail.
2018-09-26 18:58:34 -05:00
Christopher Haster
e5a6938faf Fixed possible infinite loop in deorphan step
Normally, the linked-list of directory pairs should terminate at a null
pointer. However, it is possible if the filesystem is corrupted, that
that this linked-list forms a cycle.

This should never happen with littlefs's power resilience, but if it does
we should recover appropriately.

Modified lfs_deorphan to notice if we have a cycle and return
LFS_ERR_CORRUPT in that situation.

Found by kneko715
2018-09-26 18:58:11 -05:00
Chris
6ad544f3f3 If stats file doesn't exist lfs_emubd_create will fail.
This will create default stats file if it doesn't exist.
2018-09-26 18:24:58 -05:00
Christopher Haster
3419284689 Fixed issue with corruption due to different cache sizes
The lfs_cache_zero function that was recently added assumed a single cache
size, which is incorrect. This would cause a buffer overflow if
read_size != prog_size.

Since lfs_cache_zero is only used for scrubbing prog caches, the fix
here is to use lfs_cache_drop instead on read caches. Info in read
caches should never make its way to disk.

Found by nstcl
2018-09-04 13:57:22 -05:00
Christopher Haster
510cd13df9 Bumped minor version to v1.6 2018-07-27 15:59:18 -05:00
Christopher Haster
f5e0539951 Fixed issue with release script non-standard version tags 2018-07-27 15:20:00 -05:00
Christopher Haster
066448055c Moved SPDX and license info into README
This makes is a bit easier to find the description about the SPDX tags,
and fixes the issue where GitHub doesn't detect the license text.
2018-07-27 14:02:38 -05:00
Christopher Haster
d66723ccfd Merge pull request #81 from ARMmbed/simple-versioning
Simplified release process based on feedback
2018-07-27 14:02:23 -05:00
Christopher Haster
0234c77102 Simplified release process based on feedback
Previously, littlefs had mutable versions. That is, anytime a new commit
landed on master, the bot would update the most recent version to
contain the patch. The idea was that this would make sure users always
had the most recent bug fixes. Immutable snapshots could be accessed
through the git hashes.

However, at this point multiple developers have pointed out that this is
confusing, with mutable versions being non-standard and surprising.

This new release process adopts SemVer in its entirety, with
incrementing patch numbers and immutable versions.

When a new commit lands on master:
1. The major/minor version is taken from lfs.h
2. The most recent patch version is looked up on GitHub and incremented
3. A changelog is built out of the commits to the previous version
4. A new release is created on GitHub

Additionally, any commits that land while CI is still running are
coalesced together. Which means multiple PRs can land in a single
release.
2018-07-25 14:21:58 -05:00
Christopher Haster
84adead98b Merge pull request #80 from FreddieChopin/fix-memory-leaks
Fix memory leaks
2018-07-19 17:30:48 -05:00
Freddie Chopin
0422c55b81 Fix memory leaks in lfs_mount and lfs_format
Squashed:
- Change lfs_deinit() return to void to simplify error handling
- Move lfs_deinit() before lfs_init()
- Fix memory leaks in lfs_init()
- Fix memory leaks in lfs_format()
- Fix memory leaks in lfs_mount()
2018-07-19 16:54:38 -05:00
Christopher Haster
11ad3a2414 Merge pull request #76 from ARMmbed/fix-corrupt-read
Add handling for corrupt as initial state of blocks
2018-07-17 20:32:33 -05:00
Christopher Haster
16318d003f Merge pull request #58 from dpgeorge/file-open-no-malloc
Added possibility to open multiple files with LFS_NO_MALLOC enabled
2018-07-17 20:31:20 -05:00
Damien George
961fab70c3 Added file config structure and lfs_file_opencfg
The optional config structure options up the possibility of adding
file-level configuration in a backwards compatible manner.

Also adds possibility to open multiple files with LFS_NO_MALLOC
enabled thanks to dpgeorge

Also bumped minor version to v1.5
2018-07-17 18:32:18 -05:00
Christopher Haster
041e90a1ca Added handling for corrupt as initial state of blocks
Before this, littlefs incorrectly assumed corrupt blocks were only the result
of our own modification. This would be fine for most cases of freshly
erased storage, but for storage with block-level ECC this wasn't always
true.

Fortunately, it's quite easy for littlefs to handle this case correctly,
as long as corrupt storage always reports that it is corrupt, which for
most forms of ECC is the case unless we perform a write on the storage.

found by rojer
2018-07-16 15:33:52 -05:00
Christopher Haster
f94d233deb Merge pull request #74 from FreddieChopin/cxx-guards
Add C++ guards to public headers
2018-07-13 10:55:16 -05:00
Freddie Chopin
577d777c20 Add C++ guards to public headers
Fixes #53
Fixes #32
2018-07-13 09:34:49 +02:00
Christopher Haster
c72d25203c Merge pull request #73 from FreddieChopin/fix-format-specifiers
Use PRIu32 and PRIx32 format specifiers to fix warnings
2018-07-12 16:54:13 -05:00
Freddie Chopin
7e67f9324e Use PRIu32 and PRIx32 format specifiers to fix warnings
When using "%d" or "%x" with uint32_t types, arm-none-eabi-gcc reports
warnings like below:

-- >8 -- >8 -- >8 -- >8 -- >8 -- >8 --

In file included from lfs.c:8:
lfs_util.h:45:12: warning: format '%d' expects argument of type 'int', but argument 4 has type 'lfs_block_t' {aka 'long unsigned int'} [-Wformat=]
     printf("lfs debug:%d: " fmt "\n", __LINE__, __VA_ARGS__)
            ^~~~~~~~~~~~~~~~
lfs.c:2512:21: note: in expansion of macro 'LFS_DEBUG'
                     LFS_DEBUG("Found partial move %d %d",
                     ^~~~~~~~~
lfs.c:2512:55: note: format string is defined here
                     LFS_DEBUG("Found partial move %d %d",
                                                      ~^
                                                      %ld

-- >8 -- >8 -- >8 -- >8 -- >8 -- >8 --

Fix this by replacing "%d" and "%x" with `"%" PRIu32` and `"%" PRIx32`.
2018-07-11 12:32:21 +02:00
Christopher Haster
5a17fa42e4 Fixed script issue with bash expansion inside makefile parameter
This was causing code sizes to be reported with several of the logging
functions still built in. A useful number, but not the minimum
achievable code size.
2018-07-10 17:18:45 -05:00
Christopher Haster
eed1eec5fd Fixed information leaks through reused caches
As a shortcut, littlefs never bother to zero any of the buffers is used.
It didn't need to because it would always write out the entirety of the
data it needed.

Unfortunately, this, combined with the extra padding used to align
buffers to the nearest prog size, would lead to uninitialized data
getting written out to disk.

This means unrelated file data could be written to different parts of
storage, or worse, information leaked from the malloc calls could be
written out to disk unnecessarily.

found by rojer
2018-07-10 11:18:46 -05:00
Christopher Haster
4a86370327 Added quality of life improvements for main.c/test.c issues
1. Added check for main.c and test.c to decide compilation target
2. Added step to remove test.c after successful test completion

The test.c file, which contains the expanded test main, is useful when
debugging why tests are failing. However, keeping the test.c file around
causes problems when a later attempt is made to compile a larger project
containing the littlefs directory.

Under (hopefully) normal operation, tests always pass. So it should be ok
to remove the test.c file after a successful test run. Hopefully this
behaviour doesn't cause too much confusion for contributors using the
tests.

On the other side of things, compiling the library with no main ends
(successfully) with the "main not found" error message. By defaulting
to lfs.a if neither test.c/main.c is avoid this in the common cases

found by armijnhemel and Sim4n6
2018-07-10 11:17:50 -05:00
Christopher Haster
ba4f17173f Merge pull request #57 from dpgeorge/fix-warnings
Fix some compiler warnings: shadowed variable and unused parameters
2018-07-02 12:01:34 -05:00
Damien George
51346b8bf4 Fixed shadowed variable warnings
- Fixed shadowed variable warnings in lfs_dir_find.
- Fixed unused parameter warnings when LFS_NO_MALLOC is enabled.
- Added extra warning flags to CFLAGS.
- Updated tests so they don't shadow the "size" variable for -Wshadow
2018-07-02 10:29:19 -05:00
Christopher Haster
93a2e0bbe5 Merge pull request #62 from ARMmbed/license-bsd-3
v1.4 - Change license to BSD-3-Clause
2018-06-21 13:10:57 -05:00
13 changed files with 415 additions and 261 deletions

View File

@@ -27,7 +27,7 @@ script:
# compile and find the code size with the smallest configuration # compile and find the code size with the smallest configuration
- make clean size - make clean size
OBJ="$(ls lfs*.o | tr '\n' ' ')" OBJ="$(ls lfs*.o | tr '\n' ' ')"
CFLAGS+="-DLFS_NO{ASSERT,DEBUG,WARN,ERROR}" CFLAGS+="-DLFS_NO_ASSERT -DLFS_NO_DEBUG -DLFS_NO_WARN -DLFS_NO_ERROR"
| tee sizes | tee sizes
# update status if we succeeded, compare with master if possible # update status if we succeeded, compare with master if possible
@@ -134,53 +134,58 @@ jobs:
- STAGE=deploy - STAGE=deploy
- NAME=deploy - NAME=deploy
script: script:
# Update tag for version defined in lfs.h # Find version defined in lfs.h
- LFS_VERSION=$(grep -ox '#define LFS_VERSION .*' lfs.h | cut -d ' ' -f3) - LFS_VERSION=$(grep -ox '#define LFS_VERSION .*' lfs.h | cut -d ' ' -f3)
- LFS_VERSION_MAJOR=$((0xffff & ($LFS_VERSION >> 16))) - LFS_VERSION_MAJOR=$((0xffff & ($LFS_VERSION >> 16)))
- LFS_VERSION_MINOR=$((0xffff & ($LFS_VERSION >> 0))) - LFS_VERSION_MINOR=$((0xffff & ($LFS_VERSION >> 0)))
- LFS_VERSION="v$LFS_VERSION_MAJOR.$LFS_VERSION_MINOR" # Grab latests patch from repo tags, default to 0, needs finagling to get past github's pagination api
- echo "littlefs version $LFS_VERSION" - PREV_URL=https://api.github.com/repos/$TRAVIS_REPO_SLUG/git/refs/tags/v$LFS_VERSION_MAJOR.$LFS_VERSION_MINOR.
- PREV_URL=$(curl -u "$GEKY_BOT_RELEASES" "$PREV_URL" -I
| sed -n '/^Link/{s/.*<\(.*\)>; rel="last"/\1/;p;q0};$q1'
|| echo $PREV_URL)
- LFS_VERSION_PATCH=$(curl -u "$GEKY_BOT_RELEASES" "$PREV_URL"
| jq 'map(.ref | match("\\bv.*\\..*\\.(.*)$";"g")
.captures[].string | tonumber) | max + 1'
|| echo 0)
# We have our new version
- LFS_VERSION="v$LFS_VERSION_MAJOR.$LFS_VERSION_MINOR.$LFS_VERSION_PATCH"
- echo "VERSION $LFS_VERSION"
- | - |
curl -u $GEKY_BOT_RELEASES -X POST \ # Check that we're the most recent commit
https://api.github.com/repos/$TRAVIS_REPO_SLUG/git/refs \ CURRENT_COMMIT=$(curl -f -u "$GEKY_BOT_RELEASES" \
-d "{ https://api.github.com/repos/$TRAVIS_REPO_SLUG/commits/master \
\"ref\": \"refs/tags/$LFS_VERSION\", | jq -re '.sha')
\"sha\": \"$TRAVIS_COMMIT\" if [ "$TRAVIS_COMMIT" == "$CURRENT_COMMIT" ]
}"
- |
curl -f -u $GEKY_BOT_RELEASES -X PATCH \
https://api.github.com/repos/$TRAVIS_REPO_SLUG/git/refs/tags/$LFS_VERSION \
-d "{
\"sha\": \"$TRAVIS_COMMIT\"
}"
# Create release notes from commits
- LFS_PREV_VERSION="v$LFS_VERSION_MAJOR.$(($LFS_VERSION_MINOR-1))"
- |
if [ $(git tag -l "$LFS_PREV_VERSION") ]
then then
curl -u $GEKY_BOT_RELEASES -X POST \ # Create a simple tag
https://api.github.com/repos/$TRAVIS_REPO_SLUG/releases \ curl -f -u "$GEKY_BOT_RELEASES" -X POST \
https://api.github.com/repos/$TRAVIS_REPO_SLUG/git/refs \
-d "{ -d "{
\"tag_name\": \"$LFS_VERSION\", \"ref\": \"refs/tags/$LFS_VERSION\",
\"name\": \"$LFS_VERSION\" \"sha\": \"$TRAVIS_COMMIT\"
}" }"
RELEASE=$( # Minor release?
curl -f -u $GEKY_BOT_RELEASES \ if [[ "$LFS_VERSION" == *.0 ]]
https://api.github.com/repos/$TRAVIS_REPO_SLUG/releases/tags/$LFS_VERSION then
) # Build release notes
CHANGES=$( PREV=$(git tag --sort=-v:refname -l "v*.0" | head -1)
git log --oneline $LFS_PREV_VERSION.. --grep='^Merge' --invert-grep if [ ! -z "$PREV" ]
) then
curl -f -u $GEKY_BOT_RELEASES -X PATCH \ echo "PREV $PREV"
https://api.github.com/repos/$TRAVIS_REPO_SLUG/releases/$( CHANGES=$'### Changes\n\n'$( \
jq -r '.id' <<< "$RELEASE" git log --oneline $PREV.. --grep='^Merge' --invert-grep)
) \ printf "CHANGES\n%s\n\n" "$CHANGES"
-d "$( fi
jq -s '{ # Create the release
"body": ((.[0] // "" | sub("(?<=\n)#+ Changes.*"; ""; "mi")) curl -f -u "$GEKY_BOT_RELEASES" -X POST \
+ "### Changes\n\n" + .[1]) https://api.github.com/repos/$TRAVIS_REPO_SLUG/releases \
}' <(jq '.body' <<< "$RELEASE") <(jq -sR '.' <<< "$CHANGES") -d "{
)" \"tag_name\": \"$LFS_VERSION\",
\"name\": \"${LFS_VERSION%.0}\",
\"draft\": true,
\"body\": $(jq -sR '.' <<< "$CHANGES")
}"
fi
fi fi
# Manage statuses # Manage statuses

View File

@@ -22,15 +22,3 @@ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 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 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---
*Note*:
Individual files contain the following tag instead of the full license text.
SPDX-License-Identifier: BSD-3-Clause
This enables machine processing of license information based on the SPDX
License Identifiers that are here available: http://spdx.org/licenses/

View File

@@ -1,4 +1,7 @@
TARGET = lfs TARGET = lfs.a
ifneq ($(wildcard test.c main.c),)
override TARGET = lfs
endif
CC ?= gcc CC ?= gcc
AR ?= ar AR ?= ar
@@ -23,6 +26,7 @@ override CFLAGS += -m$(WORD)
endif endif
override CFLAGS += -I. override CFLAGS += -I.
override CFLAGS += -std=c99 -Wall -pedantic override CFLAGS += -std=c99 -Wall -pedantic
override CFLAGS += -Wshadow -Wunused-parameter -Wjump-misses-init -Wsign-compare
all: $(TARGET) all: $(TARGET)
@@ -35,7 +39,9 @@ size: $(OBJ)
.SUFFIXES: .SUFFIXES:
test: test_format test_dirs test_files test_seek test_truncate \ test: test_format test_dirs test_files test_seek test_truncate \
test_interspersed test_alloc test_paths test_orphan test_move test_corrupt test_interspersed test_alloc test_paths test_orphan test_move test_corrupt
@rm test.c
test_%: tests/test_%.sh test_%: tests/test_%.sh
ifdef QUIET ifdef QUIET
@./$< | sed -n '/^[-=]/p' @./$< | sed -n '/^[-=]/p'
else else
@@ -44,7 +50,7 @@ endif
-include $(DEP) -include $(DEP)
$(TARGET): $(OBJ) lfs: $(OBJ)
$(CC) $(CFLAGS) $^ $(LFLAGS) -o $@ $(CC) $(CFLAGS) $^ $(LFLAGS) -o $@
%.a: $(OBJ) %.a: $(OBJ)

View File

@@ -146,6 +146,19 @@ The tests assume a Linux environment and can be started with make:
make test make test
``` ```
## License
The littlefs is provided under the [BSD-3-Clause](https://spdx.org/licenses/BSD-3-Clause.html)
license. See [LICENSE.md](LICENSE.md) for more information. Contributions to
this project are accepted under the same license.
Individual files contain the following tag instead of the full license text.
SPDX-License-Identifier: BSD-3-Clause
This enables machine processing of license information based on the SPDX
License Identifiers that are here available: http://spdx.org/licenses/
## Related projects ## Related projects
[Mbed OS](https://github.com/ARMmbed/mbed-os/tree/master/features/filesystem/littlefs) - [Mbed OS](https://github.com/ARMmbed/mbed-os/tree/master/features/filesystem/littlefs) -
@@ -162,3 +175,18 @@ handy.
[littlefs-js](https://github.com/geky/littlefs-js) - A javascript wrapper for [littlefs-js](https://github.com/geky/littlefs-js) - A javascript wrapper for
littlefs. I'm not sure why you would want this, but it is handy for demos. littlefs. I'm not sure why you would want this, but it is handy for demos.
You can see it in action [here](http://littlefs.geky.net/demo.html). You can see it in action [here](http://littlefs.geky.net/demo.html).
[mklfs](https://github.com/whitecatboard/Lua-RTOS-ESP32/tree/master/components/mklfs/src) -
A command line tool built by the [Lua RTOS](https://github.com/whitecatboard/Lua-RTOS-ESP32)
guys for making littlefs images from a host PC. Supports Windows, Mac OS,
and Linux.
[SPIFFS](https://github.com/pellepl/spiffs) - Another excellent embedded
filesystem for NOR flash. As a more traditional logging filesystem with full
static wear-leveling, SPIFFS will likely outperform littlefs on small
memories such as the internal flash on microcontrollers.
[Dhara](https://github.com/dlbeer/dhara) - An interesting NAND flash
translation layer designed for small MCUs. It offers static wear-leveling and
power-resilience with only a fixed O(|address|) pointer structure stored on
each block and in RAM.

View File

@@ -16,6 +16,7 @@
#include <unistd.h> #include <unistd.h>
#include <assert.h> #include <assert.h>
#include <stdbool.h> #include <stdbool.h>
#include <inttypes.h>
// Block device emulated on existing filesystem // Block device emulated on existing filesystem
@@ -46,19 +47,24 @@ int lfs_emubd_create(const struct lfs_config *cfg, const char *path) {
// Load stats to continue incrementing // Load stats to continue incrementing
snprintf(emu->child, LFS_NAME_MAX, "stats"); snprintf(emu->child, LFS_NAME_MAX, "stats");
FILE *f = fopen(emu->path, "r"); FILE *f = fopen(emu->path, "r");
if (!f) { if (!f && errno != ENOENT) {
return -errno; return -errno;
} }
size_t res = fread(&emu->stats, sizeof(emu->stats), 1, f); if (errno == ENOENT) {
if (res < 1) { memset(&emu->stats, 0x0, sizeof(emu->stats));
return -errno; } else {
} size_t res = fread(&emu->stats, sizeof(emu->stats), 1, f);
if (res < 1) {
return -errno;
}
err = fclose(f); err = fclose(f);
if (err) { if (err) {
return -errno; return -errno;
}
} }
return 0; return 0;
@@ -85,7 +91,7 @@ int lfs_emubd_read(const struct lfs_config *cfg, lfs_block_t block,
memset(data, 0, size); memset(data, 0, size);
// Read data // Read data
snprintf(emu->child, LFS_NAME_MAX, "%x", block); snprintf(emu->child, LFS_NAME_MAX, "%" PRIx32, block);
FILE *f = fopen(emu->path, "rb"); FILE *f = fopen(emu->path, "rb");
if (!f && errno != ENOENT) { if (!f && errno != ENOENT) {
@@ -124,7 +130,7 @@ int lfs_emubd_prog(const struct lfs_config *cfg, lfs_block_t block,
assert(block < cfg->block_count); assert(block < cfg->block_count);
// Program data // Program data
snprintf(emu->child, LFS_NAME_MAX, "%x", block); snprintf(emu->child, LFS_NAME_MAX, "%" PRIx32, block);
FILE *f = fopen(emu->path, "r+b"); FILE *f = fopen(emu->path, "r+b");
if (!f) { if (!f) {
@@ -171,7 +177,7 @@ int lfs_emubd_erase(const struct lfs_config *cfg, lfs_block_t block) {
assert(block < cfg->block_count); assert(block < cfg->block_count);
// Erase the block // Erase the block
snprintf(emu->child, LFS_NAME_MAX, "%x", block); snprintf(emu->child, LFS_NAME_MAX, "%" PRIx32, block);
struct stat st; struct stat st;
int err = stat(emu->path, &st); int err = stat(emu->path, &st);
if (err && errno != ENOENT) { if (err && errno != ENOENT) {
@@ -239,4 +245,3 @@ int lfs_emubd_sync(const struct lfs_config *cfg) {
return 0; return 0;
} }

View File

@@ -10,6 +10,11 @@
#include "lfs.h" #include "lfs.h"
#include "lfs_util.h" #include "lfs_util.h"
#ifdef __cplusplus
extern "C"
{
#endif
// Config options // Config options
#ifndef LFS_EMUBD_READ_SIZE #ifndef LFS_EMUBD_READ_SIZE
@@ -75,4 +80,8 @@ int lfs_emubd_erase(const struct lfs_config *cfg, lfs_block_t block);
int lfs_emubd_sync(const struct lfs_config *cfg); int lfs_emubd_sync(const struct lfs_config *cfg);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif #endif

414
lfs.c
View File

@@ -7,6 +7,8 @@
#include "lfs.h" #include "lfs.h"
#include "lfs_util.h" #include "lfs_util.h"
#include <inttypes.h>
/// Caching block device operations /// /// Caching block device operations ///
static int lfs_cache_read(lfs_t *lfs, lfs_cache_t *rcache, static int lfs_cache_read(lfs_t *lfs, lfs_cache_t *rcache,
@@ -107,6 +109,19 @@ static int lfs_cache_crc(lfs_t *lfs, lfs_cache_t *rcache,
return 0; return 0;
} }
static inline void lfs_cache_drop(lfs_t *lfs, lfs_cache_t *rcache) {
// do not zero, cheaper if cache is readonly or only going to be
// written with identical data (during relocates)
(void)lfs;
rcache->block = 0xffffffff;
}
static inline void lfs_cache_zero(lfs_t *lfs, lfs_cache_t *pcache) {
// zero to avoid information leak
memset(pcache->buffer, 0xff, lfs->cfg->prog_size);
pcache->block = 0xffffffff;
}
static int lfs_cache_flush(lfs_t *lfs, static int lfs_cache_flush(lfs_t *lfs,
lfs_cache_t *pcache, lfs_cache_t *rcache) { lfs_cache_t *pcache, lfs_cache_t *rcache) {
if (pcache->block != 0xffffffff) { if (pcache->block != 0xffffffff) {
@@ -128,7 +143,7 @@ static int lfs_cache_flush(lfs_t *lfs,
} }
} }
pcache->block = 0xffffffff; lfs_cache_zero(lfs, pcache);
} }
return 0; return 0;
@@ -233,7 +248,7 @@ static int lfs_bd_erase(lfs_t *lfs, lfs_block_t block) {
} }
static int lfs_bd_sync(lfs_t *lfs) { static int lfs_bd_sync(lfs_t *lfs) {
lfs->rcache.block = 0xffffffff; lfs_cache_drop(lfs, &lfs->rcache);
int err = lfs_cache_flush(lfs, &lfs->pcache, NULL); int err = lfs_cache_flush(lfs, &lfs->pcache, NULL);
if (err) { if (err) {
@@ -295,7 +310,8 @@ static int lfs_alloc(lfs_t *lfs, lfs_block_t *block) {
// check if we have looked at all blocks since last ack // check if we have looked at all blocks since last ack
if (lfs->free.ack == 0) { if (lfs->free.ack == 0) {
LFS_WARN("No more free space %d", lfs->free.i + lfs->free.off); LFS_WARN("No more free space %" PRIu32,
lfs->free.i + lfs->free.off);
return LFS_ERR_NOSPC; return LFS_ERR_NOSPC;
} }
@@ -401,11 +417,14 @@ static int lfs_dir_alloc(lfs_t *lfs, lfs_dir_t *dir) {
// rather than clobbering one of the blocks we just pretend // rather than clobbering one of the blocks we just pretend
// the revision may be valid // the revision may be valid
int err = lfs_bd_read(lfs, dir->pair[0], 0, &dir->d.rev, 4); int err = lfs_bd_read(lfs, dir->pair[0], 0, &dir->d.rev, 4);
dir->d.rev = lfs_fromle32(dir->d.rev); if (err && err != LFS_ERR_CORRUPT) {
if (err) {
return err; return err;
} }
if (err != LFS_ERR_CORRUPT) {
dir->d.rev = lfs_fromle32(dir->d.rev);
}
// set defaults // set defaults
dir->d.rev += 1; dir->d.rev += 1;
dir->d.size = sizeof(dir->d)+4; dir->d.size = sizeof(dir->d)+4;
@@ -429,6 +448,9 @@ static int lfs_dir_fetch(lfs_t *lfs,
int err = lfs_bd_read(lfs, tpair[i], 0, &test, sizeof(test)); int err = lfs_bd_read(lfs, tpair[i], 0, &test, sizeof(test));
lfs_dir_fromle32(&test); lfs_dir_fromle32(&test);
if (err) { if (err) {
if (err == LFS_ERR_CORRUPT) {
continue;
}
return err; return err;
} }
@@ -448,6 +470,9 @@ static int lfs_dir_fetch(lfs_t *lfs,
err = lfs_bd_crc(lfs, tpair[i], sizeof(test), err = lfs_bd_crc(lfs, tpair[i], sizeof(test),
(0x7fffffff & test.size) - sizeof(test), &crc); (0x7fffffff & test.size) - sizeof(test), &crc);
if (err) { if (err) {
if (err == LFS_ERR_CORRUPT) {
continue;
}
return err; return err;
} }
@@ -465,7 +490,8 @@ static int lfs_dir_fetch(lfs_t *lfs,
} }
if (!valid) { if (!valid) {
LFS_ERROR("Corrupted dir pair at %d %d", tpair[0], tpair[1]); LFS_ERROR("Corrupted dir pair at %" PRIu32 " %" PRIu32 ,
tpair[0], tpair[1]);
return LFS_ERR_CORRUPT; return LFS_ERR_CORRUPT;
} }
@@ -588,15 +614,16 @@ static int lfs_dir_commit(lfs_t *lfs, lfs_dir_t *dir,
break; break;
relocate: relocate:
//commit was corrupted //commit was corrupted
LFS_DEBUG("Bad block at %d", dir->pair[0]); LFS_DEBUG("Bad block at %" PRIu32, dir->pair[0]);
// drop caches and prepare to relocate block // drop caches and prepare to relocate block
relocated = true; relocated = true;
lfs->pcache.block = 0xffffffff; lfs_cache_drop(lfs, &lfs->pcache);
// can't relocate superblock, filesystem is now frozen // can't relocate superblock, filesystem is now frozen
if (lfs_paircmp(oldpair, (const lfs_block_t[2]){0, 1}) == 0) { if (lfs_paircmp(oldpair, (const lfs_block_t[2]){0, 1}) == 0) {
LFS_WARN("Superblock %d has become unwritable", oldpair[0]); LFS_WARN("Superblock %" PRIu32 " has become unwritable",
oldpair[0]);
return LFS_ERR_CORRUPT; return LFS_ERR_CORRUPT;
} }
@@ -609,7 +636,7 @@ relocate:
if (relocated) { if (relocated) {
// update references if we relocated // update references if we relocated
LFS_DEBUG("Relocating %d %d to %d %d", LFS_DEBUG("Relocating %" PRIu32 " %" PRIu32 " to %" PRIu32 " %" PRIu32,
oldpair[0], oldpair[1], dir->pair[0], dir->pair[1]); oldpair[0], oldpair[1], dir->pair[0], dir->pair[1]);
int err = lfs_relocate(lfs, oldpair, dir->pair); int err = lfs_relocate(lfs, oldpair, dir->pair);
if (err) { if (err) {
@@ -836,7 +863,7 @@ nextname:
// find entry matching name // find entry matching name
while (true) { while (true) {
int err = lfs_dir_next(lfs, dir, entry); err = lfs_dir_next(lfs, dir, entry);
if (err) { if (err) {
return err; return err;
} }
@@ -1214,10 +1241,10 @@ static int lfs_ctz_extend(lfs_t *lfs,
} }
relocate: relocate:
LFS_DEBUG("Bad block at %d", nblock); LFS_DEBUG("Bad block at %" PRIu32, nblock);
// just clear cache and try a new block // just clear cache and try a new block
pcache->block = 0xffffffff; lfs_cache_drop(lfs, &lfs->pcache);
} }
} }
@@ -1264,8 +1291,9 @@ static int lfs_ctz_traverse(lfs_t *lfs,
/// Top level file operations /// /// Top level file operations ///
int lfs_file_open(lfs_t *lfs, lfs_file_t *file, int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file,
const char *path, int flags) { const char *path, int flags,
const struct lfs_file_config *cfg) {
// deorphan if we haven't yet, needed at most once after poweron // deorphan if we haven't yet, needed at most once after poweron
if ((flags & 3) != LFS_O_RDONLY && !lfs->deorphaned) { if ((flags & 3) != LFS_O_RDONLY && !lfs->deorphaned) {
int err = lfs_deorphan(lfs); int err = lfs_deorphan(lfs);
@@ -1305,6 +1333,7 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
} }
// setup file struct // setup file struct
file->cfg = cfg;
file->pair[0] = cwd.pair[0]; file->pair[0] = cwd.pair[0];
file->pair[1] = cwd.pair[1]; file->pair[1] = cwd.pair[1];
file->poff = entry.off; file->poff = entry.off;
@@ -1323,7 +1352,9 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
// allocate buffer if needed // allocate buffer if needed
file->cache.block = 0xffffffff; file->cache.block = 0xffffffff;
if (lfs->cfg->file_buffer) { if (file->cfg && file->cfg->buffer) {
file->cache.buffer = file->cfg->buffer;
} else if (lfs->cfg->file_buffer) {
if (lfs->files) { if (lfs->files) {
// already in use // already in use
return LFS_ERR_NOMEM; return LFS_ERR_NOMEM;
@@ -1341,6 +1372,12 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
} }
} }
// zero to avoid information leak
lfs_cache_drop(lfs, &file->cache);
if ((file->flags & 3) != LFS_O_RDONLY) {
lfs_cache_zero(lfs, &file->cache);
}
// add to list of files // add to list of files
file->next = lfs->files; file->next = lfs->files;
lfs->files = file; lfs->files = file;
@@ -1348,6 +1385,11 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
return 0; return 0;
} }
int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
const char *path, int flags) {
return lfs_file_opencfg(lfs, file, path, flags, NULL);
}
int lfs_file_close(lfs_t *lfs, lfs_file_t *file) { int lfs_file_close(lfs_t *lfs, lfs_file_t *file) {
int err = lfs_file_sync(lfs, file); int err = lfs_file_sync(lfs, file);
@@ -1360,7 +1402,7 @@ int lfs_file_close(lfs_t *lfs, lfs_file_t *file) {
} }
// clean up memory // clean up memory
if (!lfs->cfg->file_buffer) { if (!(file->cfg && file->cfg->buffer) && !lfs->cfg->file_buffer) {
lfs_free(file->cache.buffer); lfs_free(file->cache.buffer);
} }
@@ -1369,7 +1411,7 @@ int lfs_file_close(lfs_t *lfs, lfs_file_t *file) {
static int lfs_file_relocate(lfs_t *lfs, lfs_file_t *file) { static int lfs_file_relocate(lfs_t *lfs, lfs_file_t *file) {
relocate: relocate:
LFS_DEBUG("Bad block at %d", file->block); LFS_DEBUG("Bad block at %" PRIu32, file->block);
// just relocate what exists into new block // just relocate what exists into new block
lfs_block_t nblock; lfs_block_t nblock;
@@ -1409,7 +1451,7 @@ relocate:
memcpy(file->cache.buffer, lfs->pcache.buffer, lfs->cfg->prog_size); memcpy(file->cache.buffer, lfs->pcache.buffer, lfs->cfg->prog_size);
file->cache.block = lfs->pcache.block; file->cache.block = lfs->pcache.block;
file->cache.off = lfs->pcache.off; file->cache.off = lfs->pcache.off;
lfs->pcache.block = 0xffffffff; lfs_cache_zero(lfs, &lfs->pcache);
file->block = nblock; file->block = nblock;
return 0; return 0;
@@ -1418,7 +1460,7 @@ relocate:
static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) { static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) {
if (file->flags & LFS_F_READING) { if (file->flags & LFS_F_READING) {
// just drop read cache // just drop read cache
file->cache.block = 0xffffffff; lfs_cache_drop(lfs, &file->cache);
file->flags &= ~LFS_F_READING; file->flags &= ~LFS_F_READING;
} }
@@ -1433,7 +1475,7 @@ static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) {
.pos = file->pos, .pos = file->pos,
.cache = lfs->rcache, .cache = lfs->rcache,
}; };
lfs->rcache.block = 0xffffffff; lfs_cache_drop(lfs, &lfs->rcache);
while (file->pos < file->size) { while (file->pos < file->size) {
// copy over a byte at a time, leave it up to caching // copy over a byte at a time, leave it up to caching
@@ -1451,8 +1493,8 @@ static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) {
// keep our reference to the rcache in sync // keep our reference to the rcache in sync
if (lfs->rcache.block != 0xffffffff) { if (lfs->rcache.block != 0xffffffff) {
orig.cache.block = 0xffffffff; lfs_cache_drop(lfs, &orig.cache);
lfs->rcache.block = 0xffffffff; lfs_cache_drop(lfs, &lfs->rcache);
} }
} }
@@ -1630,7 +1672,7 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
} }
// mark cache as dirty since we may have read data into it // mark cache as dirty since we may have read data into it
file->cache.block = 0xffffffff; lfs_cache_zero(lfs, &file->cache);
} }
// extend file with new blocks // extend file with new blocks
@@ -1977,31 +2019,48 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) {
/// Filesystem operations /// /// Filesystem operations ///
static void lfs_deinit(lfs_t *lfs) {
// free allocated memory
if (!lfs->cfg->read_buffer) {
lfs_free(lfs->rcache.buffer);
}
if (!lfs->cfg->prog_buffer) {
lfs_free(lfs->pcache.buffer);
}
if (!lfs->cfg->lookahead_buffer) {
lfs_free(lfs->free.buffer);
}
}
static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) { static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
lfs->cfg = cfg; lfs->cfg = cfg;
// setup read cache // setup read cache
lfs->rcache.block = 0xffffffff;
if (lfs->cfg->read_buffer) { if (lfs->cfg->read_buffer) {
lfs->rcache.buffer = lfs->cfg->read_buffer; lfs->rcache.buffer = lfs->cfg->read_buffer;
} else { } else {
lfs->rcache.buffer = lfs_malloc(lfs->cfg->read_size); lfs->rcache.buffer = lfs_malloc(lfs->cfg->read_size);
if (!lfs->rcache.buffer) { if (!lfs->rcache.buffer) {
return LFS_ERR_NOMEM; goto cleanup;
} }
} }
// setup program cache // setup program cache
lfs->pcache.block = 0xffffffff;
if (lfs->cfg->prog_buffer) { if (lfs->cfg->prog_buffer) {
lfs->pcache.buffer = lfs->cfg->prog_buffer; lfs->pcache.buffer = lfs->cfg->prog_buffer;
} else { } else {
lfs->pcache.buffer = lfs_malloc(lfs->cfg->prog_size); lfs->pcache.buffer = lfs_malloc(lfs->cfg->prog_size);
if (!lfs->pcache.buffer) { if (!lfs->pcache.buffer) {
return LFS_ERR_NOMEM; goto cleanup;
} }
} }
// zero to avoid information leaks
lfs_cache_zero(lfs, &lfs->pcache);
lfs_cache_drop(lfs, &lfs->rcache);
// setup lookahead, round down to nearest 32-bits // setup lookahead, round down to nearest 32-bits
LFS_ASSERT(lfs->cfg->lookahead % 32 == 0); LFS_ASSERT(lfs->cfg->lookahead % 32 == 0);
LFS_ASSERT(lfs->cfg->lookahead > 0); LFS_ASSERT(lfs->cfg->lookahead > 0);
@@ -2010,7 +2069,7 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
} else { } else {
lfs->free.buffer = lfs_malloc(lfs->cfg->lookahead/8); lfs->free.buffer = lfs_malloc(lfs->cfg->lookahead/8);
if (!lfs->free.buffer) { if (!lfs->free.buffer) {
return LFS_ERR_NOMEM; goto cleanup;
} }
} }
@@ -2030,155 +2089,160 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
lfs->deorphaned = false; lfs->deorphaned = false;
return 0; return 0;
}
static int lfs_deinit(lfs_t *lfs) { cleanup:
// free allocated memory lfs_deinit(lfs);
if (!lfs->cfg->read_buffer) { return LFS_ERR_NOMEM;
lfs_free(lfs->rcache.buffer);
}
if (!lfs->cfg->prog_buffer) {
lfs_free(lfs->pcache.buffer);
}
if (!lfs->cfg->lookahead_buffer) {
lfs_free(lfs->free.buffer);
}
return 0;
} }
int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) { int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) {
int err = lfs_init(lfs, cfg); int err = 0;
if (err) { if (true) {
return err; err = lfs_init(lfs, cfg);
}
// create free lookahead
memset(lfs->free.buffer, 0, lfs->cfg->lookahead/8);
lfs->free.off = 0;
lfs->free.size = lfs_min(lfs->cfg->lookahead, lfs->cfg->block_count);
lfs->free.i = 0;
lfs_alloc_ack(lfs);
// create superblock dir
lfs_dir_t superdir;
err = lfs_dir_alloc(lfs, &superdir);
if (err) {
return err;
}
// write root directory
lfs_dir_t root;
err = lfs_dir_alloc(lfs, &root);
if (err) {
return err;
}
err = lfs_dir_commit(lfs, &root, NULL, 0);
if (err) {
return err;
}
lfs->root[0] = root.pair[0];
lfs->root[1] = root.pair[1];
// write superblocks
lfs_superblock_t superblock = {
.off = sizeof(superdir.d),
.d.type = LFS_TYPE_SUPERBLOCK,
.d.elen = sizeof(superblock.d) - sizeof(superblock.d.magic) - 4,
.d.nlen = sizeof(superblock.d.magic),
.d.version = LFS_DISK_VERSION,
.d.magic = {"littlefs"},
.d.block_size = lfs->cfg->block_size,
.d.block_count = lfs->cfg->block_count,
.d.root = {lfs->root[0], lfs->root[1]},
};
superdir.d.tail[0] = root.pair[0];
superdir.d.tail[1] = root.pair[1];
superdir.d.size = sizeof(superdir.d) + sizeof(superblock.d) + 4;
// write both pairs to be safe
lfs_superblock_tole32(&superblock.d);
bool valid = false;
for (int i = 0; i < 2; i++) {
err = lfs_dir_commit(lfs, &superdir, (struct lfs_region[]){
{sizeof(superdir.d), sizeof(superblock.d),
&superblock.d, sizeof(superblock.d)}
}, 1);
if (err && err != LFS_ERR_CORRUPT) {
return err;
}
valid = valid || !err;
}
if (!valid) {
return LFS_ERR_CORRUPT;
}
// sanity check that fetch works
err = lfs_dir_fetch(lfs, &superdir, (const lfs_block_t[2]){0, 1});
if (err) {
return err;
}
lfs_alloc_ack(lfs);
return lfs_deinit(lfs);
}
int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
int err = lfs_init(lfs, cfg);
if (err) {
return err;
}
// setup free lookahead
lfs->free.off = 0;
lfs->free.size = 0;
lfs->free.i = 0;
lfs_alloc_ack(lfs);
// load superblock
lfs_dir_t dir;
lfs_superblock_t superblock;
err = lfs_dir_fetch(lfs, &dir, (const lfs_block_t[2]){0, 1});
if (err && err != LFS_ERR_CORRUPT) {
return err;
}
if (!err) {
err = lfs_bd_read(lfs, dir.pair[0], sizeof(dir.d),
&superblock.d, sizeof(superblock.d));
lfs_superblock_fromle32(&superblock.d);
if (err) { if (err) {
return err; return err;
} }
lfs->root[0] = superblock.d.root[0]; // create free lookahead
lfs->root[1] = superblock.d.root[1]; memset(lfs->free.buffer, 0, lfs->cfg->lookahead/8);
lfs->free.off = 0;
lfs->free.size = lfs_min(lfs->cfg->lookahead, lfs->cfg->block_count);
lfs->free.i = 0;
lfs_alloc_ack(lfs);
// create superblock dir
lfs_dir_t superdir;
err = lfs_dir_alloc(lfs, &superdir);
if (err) {
goto cleanup;
}
// write root directory
lfs_dir_t root;
err = lfs_dir_alloc(lfs, &root);
if (err) {
goto cleanup;
}
err = lfs_dir_commit(lfs, &root, NULL, 0);
if (err) {
goto cleanup;
}
lfs->root[0] = root.pair[0];
lfs->root[1] = root.pair[1];
// write superblocks
lfs_superblock_t superblock = {
.off = sizeof(superdir.d),
.d.type = LFS_TYPE_SUPERBLOCK,
.d.elen = sizeof(superblock.d) - sizeof(superblock.d.magic) - 4,
.d.nlen = sizeof(superblock.d.magic),
.d.version = LFS_DISK_VERSION,
.d.magic = {"littlefs"},
.d.block_size = lfs->cfg->block_size,
.d.block_count = lfs->cfg->block_count,
.d.root = {lfs->root[0], lfs->root[1]},
};
superdir.d.tail[0] = root.pair[0];
superdir.d.tail[1] = root.pair[1];
superdir.d.size = sizeof(superdir.d) + sizeof(superblock.d) + 4;
// write both pairs to be safe
lfs_superblock_tole32(&superblock.d);
bool valid = false;
for (int i = 0; i < 2; i++) {
err = lfs_dir_commit(lfs, &superdir, (struct lfs_region[]){
{sizeof(superdir.d), sizeof(superblock.d),
&superblock.d, sizeof(superblock.d)}
}, 1);
if (err && err != LFS_ERR_CORRUPT) {
goto cleanup;
}
valid = valid || !err;
}
if (!valid) {
err = LFS_ERR_CORRUPT;
goto cleanup;
}
// sanity check that fetch works
err = lfs_dir_fetch(lfs, &superdir, (const lfs_block_t[2]){0, 1});
if (err) {
goto cleanup;
}
lfs_alloc_ack(lfs);
} }
if (err || memcmp(superblock.d.magic, "littlefs", 8) != 0) { cleanup:
LFS_ERROR("Invalid superblock at %d %d", 0, 1); lfs_deinit(lfs);
return LFS_ERR_CORRUPT; return err;
}
int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
int err = 0;
if (true) {
err = lfs_init(lfs, cfg);
if (err) {
return err;
}
// setup free lookahead
lfs->free.off = 0;
lfs->free.size = 0;
lfs->free.i = 0;
lfs_alloc_ack(lfs);
// load superblock
lfs_dir_t dir;
lfs_superblock_t superblock;
err = lfs_dir_fetch(lfs, &dir, (const lfs_block_t[2]){0, 1});
if (err && err != LFS_ERR_CORRUPT) {
goto cleanup;
}
if (!err) {
err = lfs_bd_read(lfs, dir.pair[0], sizeof(dir.d),
&superblock.d, sizeof(superblock.d));
lfs_superblock_fromle32(&superblock.d);
if (err) {
goto cleanup;
}
lfs->root[0] = superblock.d.root[0];
lfs->root[1] = superblock.d.root[1];
}
if (err || memcmp(superblock.d.magic, "littlefs", 8) != 0) {
LFS_ERROR("Invalid superblock at %d %d", 0, 1);
err = LFS_ERR_CORRUPT;
goto cleanup;
}
uint16_t major_version = (0xffff & (superblock.d.version >> 16));
uint16_t minor_version = (0xffff & (superblock.d.version >> 0));
if ((major_version != LFS_DISK_VERSION_MAJOR ||
minor_version > LFS_DISK_VERSION_MINOR)) {
LFS_ERROR("Invalid version %d.%d", major_version, minor_version);
err = LFS_ERR_INVAL;
goto cleanup;
}
return 0;
} }
uint16_t major_version = (0xffff & (superblock.d.version >> 16)); cleanup:
uint16_t minor_version = (0xffff & (superblock.d.version >> 0));
if ((major_version != LFS_DISK_VERSION_MAJOR ||
minor_version > LFS_DISK_VERSION_MINOR)) {
LFS_ERROR("Invalid version %d.%d", major_version, minor_version);
return LFS_ERR_INVAL;
}
return 0; lfs_deinit(lfs);
return err;
} }
int lfs_unmount(lfs_t *lfs) { int lfs_unmount(lfs_t *lfs) {
return lfs_deinit(lfs); lfs_deinit(lfs);
return 0;
} }
@@ -2378,7 +2442,8 @@ static int lfs_relocate(lfs_t *lfs,
// update internal root // update internal root
if (lfs_paircmp(oldpair, lfs->root) == 0) { if (lfs_paircmp(oldpair, lfs->root) == 0) {
LFS_DEBUG("Relocating root %d %d", newpair[0], newpair[1]); LFS_DEBUG("Relocating root %" PRIu32 " %" PRIu32,
newpair[0], newpair[1]);
lfs->root[0] = newpair[0]; lfs->root[0] = newpair[0];
lfs->root[1] = newpair[1]; lfs->root[1] = newpair[1];
} }
@@ -2416,7 +2481,11 @@ int lfs_deorphan(lfs_t *lfs) {
lfs_dir_t cwd = {.d.tail[0] = 0, .d.tail[1] = 1}; lfs_dir_t cwd = {.d.tail[0] = 0, .d.tail[1] = 1};
// iterate over all directory directory entries // iterate over all directory directory entries
while (!lfs_pairisnull(cwd.d.tail)) { for (lfs_size_t i = 0; i < lfs->cfg->block_count; i++) {
if (lfs_pairisnull(cwd.d.tail)) {
return 0;
}
int err = lfs_dir_fetch(lfs, &cwd, cwd.d.tail); int err = lfs_dir_fetch(lfs, &cwd, cwd.d.tail);
if (err) { if (err) {
return err; return err;
@@ -2434,7 +2503,7 @@ int lfs_deorphan(lfs_t *lfs) {
if (!res) { if (!res) {
// we are an orphan // we are an orphan
LFS_DEBUG("Found orphan %d %d", LFS_DEBUG("Found orphan %" PRIu32 " %" PRIu32,
pdir.d.tail[0], pdir.d.tail[1]); pdir.d.tail[0], pdir.d.tail[1]);
pdir.d.tail[0] = cwd.d.tail[0]; pdir.d.tail[0] = cwd.d.tail[0];
@@ -2445,12 +2514,12 @@ int lfs_deorphan(lfs_t *lfs) {
return err; return err;
} }
break; return 0;
} }
if (!lfs_pairsync(entry.d.u.dir, pdir.d.tail)) { if (!lfs_pairsync(entry.d.u.dir, pdir.d.tail)) {
// we have desynced // we have desynced
LFS_DEBUG("Found desync %d %d", LFS_DEBUG("Found desync %" PRIu32 " %" PRIu32,
entry.d.u.dir[0], entry.d.u.dir[1]); entry.d.u.dir[0], entry.d.u.dir[1]);
pdir.d.tail[0] = entry.d.u.dir[0]; pdir.d.tail[0] = entry.d.u.dir[0];
@@ -2461,7 +2530,7 @@ int lfs_deorphan(lfs_t *lfs) {
return err; return err;
} }
break; return 0;
} }
} }
@@ -2485,14 +2554,14 @@ int lfs_deorphan(lfs_t *lfs) {
} }
if (moved) { if (moved) {
LFS_DEBUG("Found move %d %d", LFS_DEBUG("Found move %" PRIu32 " %" PRIu32,
entry.d.u.dir[0], entry.d.u.dir[1]); entry.d.u.dir[0], entry.d.u.dir[1]);
err = lfs_dir_remove(lfs, &cwd, &entry); err = lfs_dir_remove(lfs, &cwd, &entry);
if (err) { if (err) {
return err; return err;
} }
} else { } else {
LFS_DEBUG("Found partial move %d %d", LFS_DEBUG("Found partial move %" PRIu32 " %" PRIu32,
entry.d.u.dir[0], entry.d.u.dir[1]); entry.d.u.dir[0], entry.d.u.dir[1]);
entry.d.type &= ~0x80; entry.d.type &= ~0x80;
err = lfs_dir_update(lfs, &cwd, &entry, NULL); err = lfs_dir_update(lfs, &cwd, &entry, NULL);
@@ -2506,6 +2575,7 @@ int lfs_deorphan(lfs_t *lfs) {
memcpy(&pdir, &cwd, sizeof(pdir)); memcpy(&pdir, &cwd, sizeof(pdir));
} }
return 0; // If we reached here, we have more directory pairs than blocks in the
// filesystem... So something must be horribly wrong
return LFS_ERR_CORRUPT;
} }

43
lfs.h
View File

@@ -10,13 +10,18 @@
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#ifdef __cplusplus
extern "C"
{
#endif
/// Version info /// /// Version info ///
// Software library version // Software library version
// Major (top-nibble), incremented on backwards incompatible changes // Major (top-nibble), incremented on backwards incompatible changes
// Minor (bottom-nibble), incremented on feature additions // Minor (bottom-nibble), incremented on feature additions
#define LFS_VERSION 0x00010004 #define LFS_VERSION 0x00010006
#define LFS_VERSION_MAJOR (0xffff & (LFS_VERSION >> 16)) #define LFS_VERSION_MAJOR (0xffff & (LFS_VERSION >> 16))
#define LFS_VERSION_MINOR (0xffff & (LFS_VERSION >> 0)) #define LFS_VERSION_MINOR (0xffff & (LFS_VERSION >> 0))
@@ -162,6 +167,12 @@ struct lfs_config {
void *file_buffer; void *file_buffer;
}; };
// Optional configuration provided during lfs_file_opencfg
struct lfs_file_config {
// Optional, statically allocated buffer for files. Must be program sized.
// If NULL, malloc will be used by default.
void *buffer;
};
// File info structure // File info structure
struct lfs_info { struct lfs_info {
@@ -209,6 +220,7 @@ typedef struct lfs_file {
lfs_block_t head; lfs_block_t head;
lfs_size_t size; lfs_size_t size;
const struct lfs_file_config *cfg;
uint32_t flags; uint32_t flags;
lfs_off_t pos; lfs_off_t pos;
lfs_block_t block; lfs_block_t block;
@@ -276,7 +288,8 @@ typedef struct lfs {
// Format a block device with the littlefs // Format a block device with the littlefs
// //
// Requires a littlefs object and config struct. This clobbers the littlefs // Requires a littlefs object and config struct. This clobbers the littlefs
// object, and does not leave the filesystem mounted. // object, and does not leave the filesystem mounted. The config struct must
// be zeroed for defaults and backwards compatibility.
// //
// Returns a negative error code on failure. // Returns a negative error code on failure.
int lfs_format(lfs_t *lfs, const struct lfs_config *config); int lfs_format(lfs_t *lfs, const struct lfs_config *config);
@@ -285,7 +298,8 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *config);
// //
// Requires a littlefs object and config struct. Multiple filesystems // Requires a littlefs object and config struct. Multiple filesystems
// may be mounted simultaneously with multiple littlefs objects. Both // may be mounted simultaneously with multiple littlefs objects. Both
// lfs and config must be allocated while mounted. // lfs and config must be allocated while mounted. The config struct must
// be zeroed for defaults and backwards compatibility.
// //
// Returns a negative error code on failure. // Returns a negative error code on failure.
int lfs_mount(lfs_t *lfs, const struct lfs_config *config); int lfs_mount(lfs_t *lfs, const struct lfs_config *config);
@@ -323,14 +337,27 @@ int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info);
// Open a file // Open a file
// //
// The mode that the file is opened in is determined // The mode that the file is opened in is determined by the flags, which
// by the flags, which are values from the enum lfs_open_flags // are values from the enum lfs_open_flags that are bitwise-ored together.
// that are bitwise-ored together.
// //
// Returns a negative error code on failure. // Returns a negative error code on failure.
int lfs_file_open(lfs_t *lfs, lfs_file_t *file, int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
const char *path, int flags); const char *path, int flags);
// Open a file with extra configuration
//
// The mode that the file is opened in is determined by the flags, which
// are values from the enum lfs_open_flags that are bitwise-ored together.
//
// The config struct provides additional config options per file as described
// above. The config struct must be allocated while the file is open, and the
// config struct must be zeroed for defaults and backwards compatibility.
//
// Returns a negative error code on failure.
int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file,
const char *path, int flags,
const struct lfs_file_config *config);
// Close a file // Close a file
// //
// Any pending writes are written out to storage as though // Any pending writes are written out to storage as though
@@ -460,4 +487,8 @@ int lfs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data);
int lfs_deorphan(lfs_t *lfs); int lfs_deorphan(lfs_t *lfs);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif #endif

View File

@@ -34,6 +34,11 @@
#include <stdio.h> #include <stdio.h>
#endif #endif
#ifdef __cplusplus
extern "C"
{
#endif
// Macros, may be replaced by system specific wrappers. Arguments to these // Macros, may be replaced by system specific wrappers. Arguments to these
// macros must not have side-effects as the macros can be removed for a smaller // macros must not have side-effects as the macros can be removed for a smaller
@@ -158,6 +163,7 @@ static inline void *lfs_malloc(size_t size) {
#ifndef LFS_NO_MALLOC #ifndef LFS_NO_MALLOC
return malloc(size); return malloc(size);
#else #else
(void)size;
return NULL; return NULL;
#endif #endif
} }
@@ -166,9 +172,15 @@ static inline void *lfs_malloc(size_t size) {
static inline void lfs_free(void *p) { static inline void lfs_free(void *p) {
#ifndef LFS_NO_MALLOC #ifndef LFS_NO_MALLOC
free(p); free(p);
#else
(void)p;
#endif #endif
} }
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif #endif
#endif #endif

View File

@@ -32,18 +32,18 @@ lfs_alloc_singleproc() {
tests/test.py << TEST tests/test.py << TEST
const char *names[] = {"bacon", "eggs", "pancakes"}; const char *names[] = {"bacon", "eggs", "pancakes"};
lfs_mount(&lfs, &cfg) => 0; lfs_mount(&lfs, &cfg) => 0;
for (int n = 0; n < sizeof(names)/sizeof(names[0]); n++) { for (unsigned n = 0; n < sizeof(names)/sizeof(names[0]); n++) {
sprintf((char*)buffer, "$1/%s", names[n]); sprintf((char*)buffer, "$1/%s", names[n]);
lfs_file_open(&lfs, &file[n], (char*)buffer, lfs_file_open(&lfs, &file[n], (char*)buffer,
LFS_O_WRONLY | LFS_O_CREAT | LFS_O_APPEND) => 0; LFS_O_WRONLY | LFS_O_CREAT | LFS_O_APPEND) => 0;
} }
for (int n = 0; n < sizeof(names)/sizeof(names[0]); n++) { for (unsigned n = 0; n < sizeof(names)/sizeof(names[0]); n++) {
size = strlen(names[n]); size = strlen(names[n]);
for (int i = 0; i < $SIZE; i++) { for (int i = 0; i < $SIZE; i++) {
lfs_file_write(&lfs, &file[n], names[n], size) => size; lfs_file_write(&lfs, &file[n], names[n], size) => size;
} }
} }
for (int n = 0; n < sizeof(names)/sizeof(names[0]); n++) { for (unsigned n = 0; n < sizeof(names)/sizeof(names[0]); n++) {
lfs_file_close(&lfs, &file[n]) => 0; lfs_file_close(&lfs, &file[n]) => 0;
} }
lfs_unmount(&lfs) => 0; lfs_unmount(&lfs) => 0;

View File

@@ -30,7 +30,7 @@ TEST
w_test() { w_test() {
tests/test.py << TEST tests/test.py << TEST
lfs_size_t size = $1; size = $1;
lfs_size_t chunk = 31; lfs_size_t chunk = 31;
srand(0); srand(0);
lfs_mount(&lfs, &cfg) => 0; lfs_mount(&lfs, &cfg) => 0;
@@ -50,7 +50,7 @@ TEST
r_test() { r_test() {
tests/test.py << TEST tests/test.py << TEST
lfs_size_t size = $1; size = $1;
lfs_size_t chunk = 29; lfs_size_t chunk = 29;
srand(0); srand(0);
lfs_mount(&lfs, &cfg) => 0; lfs_mount(&lfs, &cfg) => 0;

View File

@@ -153,7 +153,7 @@ tests/test.py << TEST
lfs_file_read(&lfs, &file[0], buffer, size) => size; lfs_file_read(&lfs, &file[0], buffer, size) => size;
memcmp(buffer, "kittycatcat", size) => 0; memcmp(buffer, "kittycatcat", size) => 0;
lfs_size_t size = lfs_file_size(&lfs, &file[0]); size = lfs_file_size(&lfs, &file[0]);
lfs_file_seek(&lfs, &file[0], 0, LFS_SEEK_CUR) => size; lfs_file_seek(&lfs, &file[0], 0, LFS_SEEK_CUR) => size;
lfs_file_close(&lfs, &file[0]) => 0; lfs_file_close(&lfs, &file[0]) => 0;
@@ -202,7 +202,7 @@ tests/test.py << TEST
lfs_file_read(&lfs, &file[0], buffer, size) => size; lfs_file_read(&lfs, &file[0], buffer, size) => size;
memcmp(buffer, "kittycatcat", size) => 0; memcmp(buffer, "kittycatcat", size) => 0;
lfs_size_t size = lfs_file_size(&lfs, &file[0]); size = lfs_file_size(&lfs, &file[0]);
lfs_file_seek(&lfs, &file[0], 0, LFS_SEEK_CUR) => size; lfs_file_seek(&lfs, &file[0], 0, LFS_SEEK_CUR) => size;
lfs_file_close(&lfs, &file[0]) => 0; lfs_file_close(&lfs, &file[0]) => 0;
@@ -243,7 +243,7 @@ tests/test.py << TEST
lfs_file_read(&lfs, &file[0], buffer, size) => size; lfs_file_read(&lfs, &file[0], buffer, size) => size;
memcmp(buffer, "kittycatcat", size) => 0; memcmp(buffer, "kittycatcat", size) => 0;
lfs_size_t size = lfs_file_size(&lfs, &file[0]); size = lfs_file_size(&lfs, &file[0]);
lfs_file_seek(&lfs, &file[0], 0, LFS_SEEK_CUR) => size; lfs_file_seek(&lfs, &file[0], 0, LFS_SEEK_CUR) => size;
lfs_file_close(&lfs, &file[0]) => 0; lfs_file_close(&lfs, &file[0]) => 0;
@@ -286,7 +286,7 @@ tests/test.py << TEST
lfs_file_read(&lfs, &file[0], buffer, size) => size; lfs_file_read(&lfs, &file[0], buffer, size) => size;
memcmp(buffer, "kittycatcat", size) => 0; memcmp(buffer, "kittycatcat", size) => 0;
lfs_size_t size = lfs_file_size(&lfs, &file[0]); size = lfs_file_size(&lfs, &file[0]);
lfs_file_seek(&lfs, &file[0], 0, LFS_SEEK_CUR) => size; lfs_file_seek(&lfs, &file[0], 0, LFS_SEEK_CUR) => size;
lfs_file_close(&lfs, &file[0]) => 0; lfs_file_close(&lfs, &file[0]) => 0;
@@ -301,7 +301,7 @@ tests/test.py << TEST
size = strlen("hedgehoghog"); size = strlen("hedgehoghog");
const lfs_soff_t offsets[] = {512, 1020, 513, 1021, 511, 1019}; const lfs_soff_t offsets[] = {512, 1020, 513, 1021, 511, 1019};
for (int i = 0; i < sizeof(offsets) / sizeof(offsets[0]); i++) { for (unsigned i = 0; i < sizeof(offsets) / sizeof(offsets[0]); i++) {
lfs_soff_t off = offsets[i]; lfs_soff_t off = offsets[i];
memcpy(buffer, "hedgehoghog", size); memcpy(buffer, "hedgehoghog", size);
lfs_file_seek(&lfs, &file[0], off, LFS_SEEK_SET) => off; lfs_file_seek(&lfs, &file[0], off, LFS_SEEK_SET) => off;

View File

@@ -23,14 +23,14 @@ tests/test.py << TEST
lfs_mount(&lfs, &cfg) => 0; lfs_mount(&lfs, &cfg) => 0;
for (int i = 0; i < sizeof(startsizes)/sizeof(startsizes[0]); i++) { for (unsigned i = 0; i < sizeof(startsizes)/sizeof(startsizes[0]); i++) {
sprintf((char*)buffer, "hairyhead%d", i); sprintf((char*)buffer, "hairyhead%d", i);
lfs_file_open(&lfs, &file[0], (const char*)buffer, lfs_file_open(&lfs, &file[0], (const char*)buffer,
LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0; LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
strcpy((char*)buffer, "hair"); strcpy((char*)buffer, "hair");
size = strlen((char*)buffer); size = strlen((char*)buffer);
for (int j = 0; j < startsizes[i]; j += size) { for (lfs_off_t j = 0; j < startsizes[i]; j += size) {
lfs_file_write(&lfs, &file[0], buffer, size) => size; lfs_file_write(&lfs, &file[0], buffer, size) => size;
} }
lfs_file_size(&lfs, &file[0]) => startsizes[i]; lfs_file_size(&lfs, &file[0]) => startsizes[i];
@@ -55,13 +55,13 @@ tests/test.py << TEST
lfs_mount(&lfs, &cfg) => 0; lfs_mount(&lfs, &cfg) => 0;
for (int i = 0; i < sizeof(startsizes)/sizeof(startsizes[0]); i++) { for (unsigned i = 0; i < sizeof(startsizes)/sizeof(startsizes[0]); i++) {
sprintf((char*)buffer, "hairyhead%d", i); sprintf((char*)buffer, "hairyhead%d", i);
lfs_file_open(&lfs, &file[0], (const char*)buffer, LFS_O_RDWR) => 0; lfs_file_open(&lfs, &file[0], (const char*)buffer, LFS_O_RDWR) => 0;
lfs_file_size(&lfs, &file[0]) => hotsizes[i]; lfs_file_size(&lfs, &file[0]) => hotsizes[i];
size = strlen("hair"); size = strlen("hair");
int j = 0; lfs_off_t j = 0;
for (; j < startsizes[i] && j < hotsizes[i]; j += size) { for (; j < startsizes[i] && j < hotsizes[i]; j += size) {
lfs_file_read(&lfs, &file[0], buffer, size) => size; lfs_file_read(&lfs, &file[0], buffer, size) => size;
memcmp(buffer, "hair", size) => 0; memcmp(buffer, "hair", size) => 0;
@@ -87,13 +87,13 @@ tests/test.py << TEST
lfs_mount(&lfs, &cfg) => 0; lfs_mount(&lfs, &cfg) => 0;
for (int i = 0; i < sizeof(startsizes)/sizeof(startsizes[0]); i++) { for (unsigned i = 0; i < sizeof(startsizes)/sizeof(startsizes[0]); i++) {
sprintf((char*)buffer, "hairyhead%d", i); sprintf((char*)buffer, "hairyhead%d", i);
lfs_file_open(&lfs, &file[0], (const char*)buffer, LFS_O_RDONLY) => 0; lfs_file_open(&lfs, &file[0], (const char*)buffer, LFS_O_RDONLY) => 0;
lfs_file_size(&lfs, &file[0]) => coldsizes[i]; lfs_file_size(&lfs, &file[0]) => coldsizes[i];
size = strlen("hair"); size = strlen("hair");
int j = 0; lfs_off_t j = 0;
for (; j < startsizes[i] && j < hotsizes[i] && j < coldsizes[i]; for (; j < startsizes[i] && j < hotsizes[i] && j < coldsizes[i];
j += size) { j += size) {
lfs_file_read(&lfs, &file[0], buffer, size) => size; lfs_file_read(&lfs, &file[0], buffer, size) => size;