From 9480a91eccd65d62849e9390fa65ccdc0da4b6d0 Mon Sep 17 00:00:00 2001 From: "S.J.R. van Schaik" Date: Mon, 29 May 2017 15:07:11 +0200 Subject: [PATCH] mufs: calculate log2_nentries during mount --- include/fs/mufs.h | 3 ++- source/fs/mufs/super.c | 2 ++ source/fs/mufs/tree.c | 15 ++++++--------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/include/fs/mufs.h b/include/fs/mufs.h index 32e5694..445c18e 100644 --- a/include/fs/mufs.h +++ b/include/fs/mufs.h @@ -9,9 +9,10 @@ struct mufs_tree { }; struct mufs { + struct mufs_tree root; struct flash_dev *dev; uint32_t nblocks; - struct mufs_tree root; + uint8_t log2_nentries; }; int mufs_mount(struct mufs *fs, struct flash_dev *dev); diff --git a/source/fs/mufs/super.c b/source/fs/mufs/super.c index 4dcdf7d..c473195 100644 --- a/source/fs/mufs/super.c +++ b/source/fs/mufs/super.c @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -29,6 +30,7 @@ int mufs_mount(struct mufs *fs, struct flash_dev *dev) fs->dev = dev; 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.root = super.root; diff --git a/source/fs/mufs/tree.c b/source/fs/mufs/tree.c index 0af1384..8d47633 100644 --- a/source/fs/mufs/tree.c +++ b/source/fs/mufs/tree.c @@ -14,7 +14,6 @@ static int mufs_do_lookup(struct mufs *fs, uint32_t *page, char data[1 << fs->dev->log2_block_size]; uint32_t *table = (uint32_t *)data; size_t index; - uint8_t log2_nentries = fs->dev->log2_block_size - ilog2(sizeof(uint32_t)); if (!base) return -1; @@ -28,7 +27,7 @@ static int mufs_do_lookup(struct mufs *fs, uint32_t *page, sizeof data) == 0) return -1; - index = va & ((1 << (log2_nentries * (depth - 1))) - 1); + index = va & ((1 << ((depth - 1) * fs->log2_nentries)) - 1); if (!table[index]) { 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]; uint32_t *table = (uint32_t *)data; size_t index; - uint8_t log2_nentries = fs->dev->log2_block_size - ilog2(sizeof(uint32_t)); if (!base) return -1; @@ -70,14 +68,14 @@ static int mufs_do_free_page(struct mufs *fs, uint32_t base, sizeof data) == 0) 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) return -1; 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]) 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; 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); + size = 1 << (depth * fs->log2_nentries + fs->dev->log2_block_size); if (size < max_size) return 0; - index = max_size >> ((depth - 1) * log2_nentries + + index = max_size >> ((depth - 1) * fs->log2_nentries + fs->dev->log2_block_size); 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; } - for (; index < (UINT32_C(1) << log2_nentries); ++index) { + for (; index < (UINT32_C(1) << fs->log2_nentries); ++index) { table[index] = 0; }