2009-12-08 00:01:24 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
2009-12-11 00:59:53 +00:00
|
|
|
#include <cerrno>
|
2009-12-08 00:01:24 +00:00
|
|
|
|
2009-12-11 00:59:53 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include "BlockDevice.h"
|
|
|
|
#include "BlockCache.h"
|
|
|
|
#include "Exception.h"
|
|
|
|
#include "auto.h"
|
2009-12-08 00:01:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note -- everything is assumed to be single-threaded.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using namespace ProFUSE;
|
|
|
|
|
2009-12-11 00:59:53 +00:00
|
|
|
#pragma mark -
|
|
|
|
#pragma mark AbstractBlockCache
|
|
|
|
|
|
|
|
AbstractBlockCache::~AbstractBlockCache()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark MappedBlockCache
|
2009-12-08 00:01:24 +00:00
|
|
|
|
2009-12-11 00:59:53 +00:00
|
|
|
MappedBlockCache::MappedBlockCache(void *data, unsigned blocks)
|
|
|
|
{
|
|
|
|
_blocks = blocks;
|
|
|
|
_data = (uint8_t *)_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MappedBlockCache::write()
|
|
|
|
{
|
|
|
|
// TODO...
|
|
|
|
}
|
2009-12-08 00:01:24 +00:00
|
|
|
|
2009-12-11 00:59:53 +00:00
|
|
|
void *MappedBlockCache::load(unsigned block)
|
|
|
|
{
|
|
|
|
#undef __METHOD__
|
|
|
|
#define __METHOD__ "MappedBlockCache::load"
|
|
|
|
|
|
|
|
if (block >= _blocks)
|
|
|
|
throw Exception(__METHOD__ ": Invalid block.");
|
|
|
|
|
|
|
|
|
|
|
|
return _data + block * 512;
|
|
|
|
}
|
|
|
|
void MappedBlockCache::unload(unsigned block, bool dirty)
|
|
|
|
{
|
|
|
|
#undef __METHOD__
|
|
|
|
#define __METHOD__ "MappedBlockCache::unload"
|
|
|
|
|
|
|
|
if (!dirty) return;
|
|
|
|
if (::msync(_data + block * 512, 512, MS_ASYNC) < 0)
|
|
|
|
{
|
|
|
|
throw POSIXException(__METHOD__ ": msync failed.", errno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark BlockCache
|
|
|
|
|
|
|
|
typedef std::vector<BlockCache::BlockDescriptor>::iterator BDIter;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BlockCache::BlockCache(BlockDevice *device, unsigned size)
|
2009-12-08 00:01:24 +00:00
|
|
|
{
|
|
|
|
_device = device;
|
|
|
|
_ts = 0;
|
2009-12-11 00:59:53 +00:00
|
|
|
_cacheSize = std::max(16u, size);
|
|
|
|
_blocks.reserve(_cacheSize);
|
2009-12-08 00:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BlockCache::~BlockCache()
|
|
|
|
{
|
|
|
|
for (BDIter iter = _blocks.begin(); iter != _blocks.end(); ++iter)
|
|
|
|
{
|
|
|
|
if (iter->data) delete[] iter->data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-11 00:59:53 +00:00
|
|
|
void BlockCache::write()
|
|
|
|
{
|
|
|
|
for (BDIter iter = _blocks.begin(); iter != _blocks.end(); ++iter)
|
|
|
|
{
|
|
|
|
if (iter->dirty)
|
|
|
|
{
|
|
|
|
_device->write(iter->block, iter->data);
|
|
|
|
iter->dirty = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-08 00:01:24 +00:00
|
|
|
|
2009-12-11 00:59:53 +00:00
|
|
|
void *BlockCache::load(unsigned block)
|
2009-12-08 00:01:24 +00:00
|
|
|
{
|
|
|
|
unsigned mints = -1;
|
|
|
|
unsigned freeCount = 0;
|
|
|
|
|
|
|
|
BDIter oldest;
|
|
|
|
|
|
|
|
|
|
|
|
++_ts;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* keep a pointer to the oldest free entry (according to ts).
|
|
|
|
* and re-use if >= 16 entries.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
for (BDIter iter = _blocks.begin(); iter != _blocks.end(); ++iter)
|
|
|
|
{
|
|
|
|
if (iter->block == block)
|
|
|
|
{
|
|
|
|
++iter->count;
|
|
|
|
iter->ts = _ts;
|
2009-12-11 00:59:53 +00:00
|
|
|
|
2009-12-08 00:01:24 +00:00
|
|
|
return iter->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iter->count == 0)
|
|
|
|
{
|
|
|
|
++freeCount;
|
|
|
|
if (iter->ts < mints)
|
|
|
|
{
|
|
|
|
mints = iter->ts;
|
|
|
|
oldest = iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-11 00:59:53 +00:00
|
|
|
if (freeCount && (_blocks.size() >= _cacheSize))
|
2009-12-08 00:01:24 +00:00
|
|
|
{
|
2009-12-11 00:59:53 +00:00
|
|
|
// re-use old buffer.
|
|
|
|
|
2009-12-08 00:01:24 +00:00
|
|
|
oldest->block = block;
|
|
|
|
oldest->count = 1;
|
|
|
|
oldest->ts = _ts;
|
2009-12-11 00:59:53 +00:00
|
|
|
oldest->dirty = false;
|
2009-12-08 00:01:24 +00:00
|
|
|
|
|
|
|
_device->read(block, oldest->data);
|
|
|
|
return oldest->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
auto_array<uint8_t> buffer(new uint8_t[512]);
|
2009-12-11 00:59:53 +00:00
|
|
|
BlockDescriptor bd = { block, 1, _ts, false, buffer.get() };
|
2009-12-08 00:01:24 +00:00
|
|
|
|
|
|
|
_device->read(block, buffer.get());
|
|
|
|
|
|
|
|
_blocks.push_back(bd);
|
|
|
|
|
|
|
|
|
|
|
|
return buffer.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-11 00:59:53 +00:00
|
|
|
void BlockCache::unload(unsigned block, bool dirty)
|
2009-12-08 00:01:24 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
for (BDIter iter = _blocks.begin(); iter != _blocks.end(); ++iter)
|
|
|
|
{
|
|
|
|
if (iter->block == block)
|
|
|
|
{
|
2009-12-11 00:59:53 +00:00
|
|
|
iter->dirty = dirty || iter->dirty;
|
2009-12-08 00:01:24 +00:00
|
|
|
if (!--iter->count)
|
|
|
|
{
|
2009-12-11 00:59:53 +00:00
|
|
|
if (iter->dirty)
|
|
|
|
{
|
|
|
|
_device->write(block, iter->data);
|
|
|
|
iter->dirty = false;
|
|
|
|
}
|
2009-12-08 00:01:24 +00:00
|
|
|
// trim back if too many entries.
|
2009-12-11 00:59:53 +00:00
|
|
|
if (_blocks.size() > _cacheSize)
|
2009-12-08 00:01:24 +00:00
|
|
|
{
|
|
|
|
delete[] iter->data;
|
|
|
|
_blocks.erase(iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|