2011-02-19 18:05:57 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include <Cache/BlockCache.h>
|
|
|
|
#include <Device/BlockDevice.h>
|
|
|
|
|
|
|
|
#include <ProFUSE/Exception.h>
|
|
|
|
#include <ProFUSE/auto.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using namespace Device;
|
|
|
|
|
|
|
|
using ProFUSE::Exception;
|
|
|
|
using ProFUSE::POSIXException;
|
|
|
|
|
|
|
|
|
2011-02-20 23:05:09 +00:00
|
|
|
BlockCache::BlockCache(BlockDevicePointer device) :
|
|
|
|
_device(device)
|
2011-02-19 18:05:57 +00:00
|
|
|
{
|
|
|
|
_blocks = device->blocks();
|
|
|
|
_readOnly = device->readOnly();
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockCache::~BlockCache()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlockCache::write(unsigned block, const void *bp)
|
|
|
|
{
|
|
|
|
void *address = acquire(block);
|
|
|
|
std::memcpy(address, bp, 512);
|
|
|
|
release(block, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlockCache::read(unsigned block, void *bp)
|
|
|
|
{
|
|
|
|
void *address = acquire(block);
|
|
|
|
std::memcpy(bp, address, 512);
|
|
|
|
release(block, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-20 23:05:09 +00:00
|
|
|
BlockCachePointer BlockCache::Create(BlockDevicePointer device)
|
2011-02-19 18:05:57 +00:00
|
|
|
{
|
2011-02-20 23:05:09 +00:00
|
|
|
if (!device.get()) return BlockCachePointer();
|
2011-02-19 18:05:57 +00:00
|
|
|
|
2011-02-21 22:47:56 +00:00
|
|
|
return device->createBlockCache();
|
2011-02-19 18:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void BlockCache::zeroBlock(unsigned block)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
void *address = acquire(block);
|
|
|
|
std::memset(address, 0, 512);
|
|
|
|
release(block, true);
|
|
|
|
*/
|
|
|
|
|
|
|
|
uint8_t buffer[512];
|
|
|
|
|
|
|
|
std::memset(buffer, 0, 512);
|
|
|
|
write(block, buffer);
|
|
|
|
}
|