mufs: add mufs_shrink_tree() to shrink a file tree to a specific size in terms of VAS
This commit is contained in:
parent
281c1abdfa
commit
ba12823d8b
2 changed files with 45 additions and 0 deletions
|
@ -116,3 +116,47 @@ int mufs_extend_tree(struct mufs *fs, struct mufs_tree *tree, uint8_t depth)
|
|||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mufs_shrink_tree(struct mufs *fs, struct mufs_tree *tree, uint32_t max_size)
|
||||
{
|
||||
char data[1 << fs->dev->log2_block_size];
|
||||
uint32_t *table = (uint32_t *)data;
|
||||
size_t index;
|
||||
uint32_t base, size;
|
||||
uint8_t log2_nentries = fs->dev->log2_block_size - ilog2(sizeof(uint32_t));
|
||||
uint8_t depth;
|
||||
|
||||
base = tree->root;
|
||||
|
||||
for (depth = tree->depth; depth; --depth) {
|
||||
size = 1 << (depth * log2_nentries + fs->dev->log2_block_size);
|
||||
|
||||
if (size < max_size)
|
||||
return 0;
|
||||
|
||||
index = max_size >> ((depth - 1) * log2_nentries +
|
||||
fs->dev->log2_block_size);
|
||||
|
||||
if (flash_read(fs->dev, base, data, sizeof data) == 0)
|
||||
return -1;
|
||||
|
||||
if (index <= 1) {
|
||||
tree->root = table[0];
|
||||
--tree->depth;
|
||||
|
||||
mufs_free_block(fs, base);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
for (; index < (UINT32_C(1) << log2_nentries); ++index) {
|
||||
table[index] = 0;
|
||||
}
|
||||
|
||||
if (flash_write(fs->dev, base << fs->dev->log2_block_size, data,
|
||||
sizeof data) == 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -6,3 +6,4 @@ int mufs_alloc_page(struct mufs *fs, struct mufs_tree *tree, uint32_t *page,
|
|||
uint32_t va);
|
||||
void mufs_free_page(struct mufs *fs, struct mufs_tree *tree, uint32_t va);
|
||||
int mufs_extend_tree(struct mufs *fs, struct mufs_tree *tree, uint8_t depth);
|
||||
int mufs_shrink_tree(struct mufs *fs, struct mufs_tree *tree, uint32_t max_size);
|
||||
|
|
Loading…
Add table
Reference in a new issue