#include #include #include #include #include #include #include #include "block.h" #include "tree.h" static int mufs_do_lookup(struct mufs *fs, uint32_t *page, uint32_t base, uint8_t depth, uint32_t va, unsigned alloc) { struct flash_dev *dev = fs->dev; char data[1 << dev->log2_block_size]; uint32_t *table = (uint32_t *)data; size_t index; if (!base) return -1; if (!depth) { *page = base; return 0; } if (flash_read(dev, base << dev->log2_block_size, data, sizeof data) == 0) return -1; index = va & ((1 << ((depth - 1) * fs->log2_nentries)) - 1); if (!table[index]) { if (!alloc || mufs_alloc_block(fs, &table[index]) < 0) return -1; if (flash_write(dev, base << dev->log2_block_size, data, sizeof data) == 0) return -1; } return mufs_do_lookup(fs, page, table[index], depth - 1, va, alloc); } int mufs_lookup_page(struct mufs_tree *tree, uint32_t *page, uint32_t va) { return mufs_do_lookup(tree->fs, page, tree->root, tree->depth, va, 0); } int mufs_alloc_page(struct mufs_tree *tree, uint32_t *page, uint32_t va) { struct mufs *fs = tree->fs; if (!tree->root && mufs_alloc_block(fs, &tree->root) < 0) return -1; return mufs_do_lookup(fs, page, tree->root, tree->depth, va, 1); } static int mufs_do_free_page(struct mufs *fs, uint32_t base, uint8_t depth, uint32_t va) { struct flash_dev *dev = fs->dev; char data[1 << fs->dev->log2_block_size]; uint32_t *table = (uint32_t *)data; size_t index; if (!base) return -1; if (!depth) { mufs_free_block(fs, base); return 0; } if (flash_read(fs->dev, base << dev->log2_block_size, data, sizeof data) == 0) return -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) << fs->log2_nentries); ++index) { if (!table[index]) continue; if (flash_write(dev, base << dev->log2_block_size, data, sizeof data) == 0) return -1; return 0; } return mufs_free_block(fs, base); } void mufs_free_page(struct mufs_tree *tree, uint32_t va) { mufs_do_free_page(tree->fs, tree->root, tree->depth, va); } int mufs_extend_tree(struct mufs_tree *tree, uint8_t depth) { struct mufs *fs = tree->fs; struct flash_dev *dev = fs->dev; uint32_t root; for (; tree->depth <= depth; ++tree->depth) { if (mufs_alloc_block(fs, &root) < 0) return -1; if (!root) continue; if (flash_write(dev, root << dev->log2_block_size, &tree->root, sizeof tree->root) == 0) { mufs_free_block(fs, root); return -1; } tree->root = root; } return 0; } int mufs_shrink_tree(struct mufs_tree *tree, uint32_t max_size) { struct mufs *fs = tree->fs; struct flash_dev *dev = fs->dev; char data[1 << dev->log2_block_size]; uint32_t *table = (uint32_t *)data; size_t index; uint32_t base, size; uint8_t depth; base = tree->root; for (depth = tree->depth; depth; --depth) { size = 1 << (depth * fs->log2_nentries + dev->log2_block_size); if (size < max_size) return 0; index = max_size >> ((depth - 1) * fs->log2_nentries + dev->log2_block_size); if (flash_read(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) << fs->log2_nentries); ++index) { table[index] = 0; } if (flash_write(dev, base << dev->log2_block_size, data, sizeof data) == 0) return -1; } return 0; } static size_t mufs_do_read(struct mufs_tree *tree, void *data, uint32_t va, size_t len) { struct mufs *fs = tree->fs; struct flash_dev *dev = fs->dev; uint32_t page, offset; offset = va & ((UINT32_C(1) << dev->log2_block_size) - 1); va = align(va, dev->log2_block_size); memset(data, 0, len); len = min(len, (UINT32_C(1) << dev->log2_block_size) - offset); if (mufs_lookup_page(tree, &page, va) < 0) return 0; return flash_read(dev, (page << dev->log2_block_size) + offset, data, len); } size_t mufs_tree_read(struct mufs_tree *tree, void *data, uint32_t va, size_t len) { uint8_t *buf = data; size_t ret, nbytes = 0; while (len) { if ((ret = mufs_do_read(tree, buf, va, len)) == 0) return nbytes; buf += ret; va += ret; nbytes += ret; len -= ret; } return nbytes; } static size_t mufs_do_write(struct mufs_tree *tree, void *data, uint32_t va, size_t len) { struct mufs *fs = tree->fs; struct flash_dev *dev = fs->dev; uint32_t page, offset; offset = va & ((UINT32_C(1) << dev->log2_block_size) - 1); va = align(va, dev->log2_block_size); len = min(len, (UINT32_C(1) << dev->log2_block_size) - offset); if (mufs_extend_tree(tree, 1) < 0) return 0; if (mufs_alloc_page(tree, &page, va) < 0) return 0; return flash_write(dev, (page << dev->log2_block_size) + offset, data, len); } size_t mufs_tree_write(struct mufs_tree *tree, void *data, uint32_t va, size_t len) { uint8_t *buf = data; size_t ret, nbytes = 0; while (len) { if ((ret = mufs_do_write(tree, buf, va, len)) == 0) return nbytes; buf += ret; va += ret; nbytes += ret; len -= ret; } return nbytes; }