Skip to content

Add config flag to disable block count check on mount #1096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
10 changes: 8 additions & 2 deletions lfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -4592,15 +4592,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
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