From 9390ee3ad00a1e75d4fae17f2d64452e180e5874 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Wed, 23 Apr 2025 17:33:24 +0200 Subject: [PATCH 1/2] Add config flag to disable block count check on mount When the block count check was added to `lfs_mount` in [#584][] it was mentionned to have a mount flag to disable this check in the future. Therefore, this PR: - Adds a new `flags` field to lfs_config - Defines a new `lfs_fs_flags` enum that contains these flags - Defines one flag `LFS_CFG_DISABLE_BLOCK_COUNT_CHECK` that disables the check inserted in [#584][] [#584]: https://github.com/littlefs-project/littlefs/pull/584 --- lfs.c | 10 ++++++++-- lfs.h | 9 +++++++++ tests/test_superblocks.toml | 6 ++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lfs.c b/lfs.c index c0b0ba38..41011448 100644 --- a/lfs.c +++ b/lfs.c @@ -4596,7 +4596,9 @@ static int lfs_mount_(lfs_t *lfs, const struct lfs_config *cfg) { } // this is where we get the block_count from disk if block_count=0 - if (lfs->cfg->block_count + + if ((lfs->cfg->flags & LFS_CFG_DISABLE_BLOCK_COUNT_CHECK) == 0 + && lfs->cfg->block_count && superblock.block_count != lfs->cfg->block_count) { LFS_ERROR("Invalid block count (%"PRIu32" != %"PRIu32")", superblock.block_count, lfs->cfg->block_count); @@ -4604,7 +4606,11 @@ static int lfs_mount_(lfs_t *lfs, const struct lfs_config *cfg) { goto cleanup; } - lfs->block_count = superblock.block_count; + if (lfs->cfg->block_count) { + lfs->block_count = lfs->cfg->block_count; + } else { + lfs->block_count = superblock.block_count; + } if (superblock.block_size != lfs->cfg->block_size) { LFS_ERROR("Invalid block size (%"PRIu32" != %"PRIu32")", diff --git a/lfs.h b/lfs.h index 215309c5..8ac4ec5a 100644 --- a/lfs.h +++ b/lfs.h @@ -283,6 +283,11 @@ struct lfs_config { // Set to -1 to disable inlined files. lfs_size_t inline_max; + // Configuration flags for the filesystem + // + // See variants of lfs_fs_flags + uint32_t flags; + #ifdef LFS_MULTIVERSION // On-disk version to use when writing in the form of 16-bit major version // + 16-bit minor version. This limiting metadata to what is supported by @@ -292,6 +297,10 @@ struct lfs_config { #endif }; +enum lfs_fs_flags { + LFS_CFG_DISABLE_BLOCK_COUNT_CHECK = 1, // Allow mounting a filesystem with a different block count in the config and the superblock +}; + // File info structure struct lfs_info { // Type of the file, either LFS_TYPE_REG or LFS_TYPE_DIR diff --git a/tests/test_superblocks.toml b/tests/test_superblocks.toml index 78050f13..c6ecfd8d 100644 --- a/tests/test_superblocks.toml +++ b/tests/test_superblocks.toml @@ -465,7 +465,13 @@ code = ''' lfs_unmount(&lfs) => 0; if (KNOWN_BLOCK_COUNT) { + cfg->block_count = BLOCK_COUNT_2 + 1; + lfs_mount(&lfs, cfg) => LFS_ERR_INVAL; + cfg->flags |= LFS_CFG_DISABLE_BLOCK_COUNT_CHECK; + lfs_mount(&lfs, cfg) => 0; + lfs_unmount(&lfs) => 0; cfg->block_count = BLOCK_COUNT_2; + cfg->flags &= ~LFS_CFG_DISABLE_BLOCK_COUNT_CHECK; } else { cfg->block_count = 0; } From bfc30d2d23da711613c8a0c1f17f0ddf41340308 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Thu, 15 May 2025 09:39:20 +0200 Subject: [PATCH 2/2] Adapt to #1094 --- lfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lfs.c b/lfs.c index 41011448..49cd22a9 100644 --- a/lfs.c +++ b/lfs.c @@ -5256,7 +5256,7 @@ static int lfs_shrink_checkblock(void *data, lfs_block_t block) { static int lfs_fs_grow_(lfs_t *lfs, lfs_size_t block_count) { int err; - if (block_count == lfs->block_count) { + if (block_count == lfs->block_count && (lfs->cfg->flags & LFS_CFG_DISABLE_BLOCK_COUNT_CHECK) == 0) { return 0; }