diff --git a/lfs.c b/lfs.c index c0b0ba38..49cd22a9 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")", @@ -5250,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; } 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; }