mufs: calculate log2_nentries during mount
This commit is contained in:
parent
b634e3c715
commit
9480a91ecc
3 changed files with 10 additions and 10 deletions
|
@ -9,9 +9,10 @@ struct mufs_tree {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct mufs {
|
struct mufs {
|
||||||
|
struct mufs_tree root;
|
||||||
struct flash_dev *dev;
|
struct flash_dev *dev;
|
||||||
uint32_t nblocks;
|
uint32_t nblocks;
|
||||||
struct mufs_tree root;
|
uint8_t log2_nentries;
|
||||||
};
|
};
|
||||||
|
|
||||||
int mufs_mount(struct mufs *fs, struct flash_dev *dev);
|
int mufs_mount(struct mufs *fs, struct flash_dev *dev);
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <bitops.h>
|
||||||
#include <flash.h>
|
#include <flash.h>
|
||||||
|
|
||||||
#include <fs/mufs.h>
|
#include <fs/mufs.h>
|
||||||
|
@ -29,6 +30,7 @@ int mufs_mount(struct mufs *fs, struct flash_dev *dev)
|
||||||
|
|
||||||
fs->dev = dev;
|
fs->dev = dev;
|
||||||
fs->nblocks = super.nblocks;
|
fs->nblocks = super.nblocks;
|
||||||
|
fs->log2_nentries = fs->dev->log2_block_size - ilog2(sizeof(uint32_t));
|
||||||
|
|
||||||
fs->root.file_size = super.file_size;
|
fs->root.file_size = super.file_size;
|
||||||
fs->root.root = super.root;
|
fs->root.root = super.root;
|
||||||
|
|
|
@ -14,7 +14,6 @@ static int mufs_do_lookup(struct mufs *fs, uint32_t *page,
|
||||||
char data[1 << fs->dev->log2_block_size];
|
char data[1 << fs->dev->log2_block_size];
|
||||||
uint32_t *table = (uint32_t *)data;
|
uint32_t *table = (uint32_t *)data;
|
||||||
size_t index;
|
size_t index;
|
||||||
uint8_t log2_nentries = fs->dev->log2_block_size - ilog2(sizeof(uint32_t));
|
|
||||||
|
|
||||||
if (!base)
|
if (!base)
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -28,7 +27,7 @@ static int mufs_do_lookup(struct mufs *fs, uint32_t *page,
|
||||||
sizeof data) == 0)
|
sizeof data) == 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
index = va & ((1 << (log2_nentries * (depth - 1))) - 1);
|
index = va & ((1 << ((depth - 1) * fs->log2_nentries)) - 1);
|
||||||
|
|
||||||
if (!table[index]) {
|
if (!table[index]) {
|
||||||
if (!alloc || mufs_alloc_block(fs, &table[index]) < 0)
|
if (!alloc || mufs_alloc_block(fs, &table[index]) < 0)
|
||||||
|
@ -56,7 +55,6 @@ static int mufs_do_free_page(struct mufs *fs, uint32_t base,
|
||||||
char data[1 << fs->dev->log2_block_size];
|
char data[1 << fs->dev->log2_block_size];
|
||||||
uint32_t *table = (uint32_t *)data;
|
uint32_t *table = (uint32_t *)data;
|
||||||
size_t index;
|
size_t index;
|
||||||
uint8_t log2_nentries = fs->dev->log2_block_size - ilog2(sizeof(uint32_t));
|
|
||||||
|
|
||||||
if (!base)
|
if (!base)
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -70,14 +68,14 @@ static int mufs_do_free_page(struct mufs *fs, uint32_t base,
|
||||||
sizeof data) == 0)
|
sizeof data) == 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
index = va & ((1 << (log2_nentries * (depth - 1))) - 1);
|
index = va & ((1 << (fs->log2_nentries * (depth - 1))) - 1);
|
||||||
|
|
||||||
if (mufs_do_free_page(fs, table[index], depth - 1, va) < 0)
|
if (mufs_do_free_page(fs, table[index], depth - 1, va) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
table[index] = 0;
|
table[index] = 0;
|
||||||
|
|
||||||
for (index = 0; index < (UINT32_C(1) << log2_nentries); ++index) {
|
for (index = 0; index < (UINT32_C(1) << fs->log2_nentries); ++index) {
|
||||||
if (!table[index])
|
if (!table[index])
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
@ -123,18 +121,17 @@ int mufs_shrink_tree(struct mufs *fs, struct mufs_tree *tree, uint32_t max_size)
|
||||||
uint32_t *table = (uint32_t *)data;
|
uint32_t *table = (uint32_t *)data;
|
||||||
size_t index;
|
size_t index;
|
||||||
uint32_t base, size;
|
uint32_t base, size;
|
||||||
uint8_t log2_nentries = fs->dev->log2_block_size - ilog2(sizeof(uint32_t));
|
|
||||||
uint8_t depth;
|
uint8_t depth;
|
||||||
|
|
||||||
base = tree->root;
|
base = tree->root;
|
||||||
|
|
||||||
for (depth = tree->depth; depth; --depth) {
|
for (depth = tree->depth; depth; --depth) {
|
||||||
size = 1 << (depth * log2_nentries + fs->dev->log2_block_size);
|
size = 1 << (depth * fs->log2_nentries + fs->dev->log2_block_size);
|
||||||
|
|
||||||
if (size < max_size)
|
if (size < max_size)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
index = max_size >> ((depth - 1) * log2_nentries +
|
index = max_size >> ((depth - 1) * fs->log2_nentries +
|
||||||
fs->dev->log2_block_size);
|
fs->dev->log2_block_size);
|
||||||
|
|
||||||
if (flash_read(fs->dev, base, data, sizeof data) == 0)
|
if (flash_read(fs->dev, base, data, sizeof data) == 0)
|
||||||
|
@ -149,7 +146,7 @@ int mufs_shrink_tree(struct mufs *fs, struct mufs_tree *tree, uint32_t max_size)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (; index < (UINT32_C(1) << log2_nentries); ++index) {
|
for (; index < (UINT32_C(1) << fs->log2_nentries); ++index) {
|
||||||
table[index] = 0;
|
table[index] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue