fixup! Add support for shrinking a filesystem

This commit is contained in:
Sosthène Guédon
2025-05-05 11:37:39 +02:00
parent 2105e502c5
commit 9b8f802b43
4 changed files with 40 additions and 69 deletions
+31 -53
View File
@@ -5233,12 +5233,41 @@ static int lfs_fs_gc_(lfs_t *lfs) {
#endif
#ifndef LFS_READONLY
static int lfs_fs_rewrite_block_count(lfs_t *lfs, lfs_size_t block_count) {
#ifdef LFS_SHRINKIFCHEAP
static int lfs_shrink_checkblock(void * data, lfs_block_t block) {
lfs_size_t threshold = *((lfs_size_t *) data);
if (block >= threshold) {
return LFS_ERR_NOTEMPTY;
}
return 0;
}
#endif
static int lfs_fs_grow_(lfs_t *lfs, lfs_size_t block_count) {
int err;
if (block_count == lfs->block_count) {
return 0;
}
#ifndef LFS_SHRINKIFCHEAP
// shrinking is not supported
LFS_ASSERT(block_count >= lfs->block_count);
#endif
#ifdef LFS_SHRINKIFCHEAP
lfs_block_t threshold = block_count;
err = lfs_fs_traverse_(lfs, lfs_shrink_checkblock, &threshold, true);
if (err) {
return err;
}
#endif
lfs->block_count = block_count;
// fetch the root
lfs_mdir_t root;
int err = lfs_dir_fetch(lfs, &root, lfs->root);
err = lfs_dir_fetch(lfs, &root, lfs->root);
if (err) {
return err;
}
@@ -5263,41 +5292,6 @@ static int lfs_fs_rewrite_block_count(lfs_t *lfs, lfs_size_t block_count) {
}
return 0;
}
static int lfs_fs_grow_(lfs_t *lfs, lfs_size_t block_count) {
// shrinking is not supported
LFS_ASSERT(block_count >= lfs->block_count);
if (block_count > lfs->block_count) {
return lfs_fs_rewrite_block_count(lfs, block_count);
}
return 0;
}
static int lfs_shrink_check_block(void * data, lfs_block_t block) {
lfs_size_t threshold = *((lfs_size_t *) data);
if (block >= threshold) {
return LFS_ERR_NOTEMPTY;
}
return 0;
}
static int lfs_fs_shrink_(lfs_t *lfs, lfs_size_t block_count) {
if (block_count != lfs->block_count) {
lfs_block_t threshold = block_count;
int err = lfs_fs_traverse_(lfs, lfs_shrink_check_block, &threshold, true);
if (err) {
return err;
}
return lfs_fs_rewrite_block_count(lfs, block_count);
}
return 0;
}
#endif
#ifdef LFS_MIGRATE
@@ -6514,22 +6508,6 @@ int lfs_fs_grow(lfs_t *lfs, lfs_size_t block_count) {
}
#endif
#ifndef LFS_READONLY
int lfs_fs_shrink(lfs_t *lfs, lfs_size_t block_count) {
int err = LFS_LOCK(lfs->cfg);
if (err) {
return err;
}
LFS_TRACE("lfs_fs_shrink(%p, %"PRIu32")", (void*)lfs, block_count);
err = lfs_fs_shrink_(lfs, block_count);
LFS_TRACE("lfs_fs_shrink -> %d", err);
LFS_UNLOCK(lfs->cfg);
return err;
}
#endif
#ifdef LFS_MIGRATE
int lfs_migrate(lfs_t *lfs, const struct lfs_config *cfg) {
int err = LFS_LOCK(cfg);