Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -4596,15 +4596,21 @@ 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);
err = LFS_ERR_INVAL;
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")",
Expand Down Expand Up @@ -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;
}

Expand Down
9 changes: 9 additions & 0 deletions lfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions tests/test_superblocks.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down