|
|
|
@ -30,13 +30,9 @@
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
|
|
// FIXME: remove these from a header!
|
|
|
|
|
using namespace boost;
|
|
|
|
|
using namespace persistent_data;
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
// FIXME: give this namesace a name
|
|
|
|
|
namespace {
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
@ -105,11 +101,12 @@ namespace {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
block_io<BlockSize>::block_io(std::string const &path, block_address nr_blocks, mode m)
|
|
|
|
|
namespace persistent_data {
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
block_io<BlockSize>::block_io(std::string const &path, block_address nr_blocks, mode m)
|
|
|
|
|
: nr_blocks_(nr_blocks),
|
|
|
|
|
mode_(m)
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
off_t file_size = nr_blocks * BlockSize;
|
|
|
|
|
|
|
|
|
|
switch (m) {
|
|
|
|
@ -128,19 +125,19 @@ block_io<BlockSize>::block_io(std::string const &path, block_address nr_blocks,
|
|
|
|
|
default:
|
|
|
|
|
throw runtime_error("unsupported mode");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
block_io<BlockSize>::~block_io()
|
|
|
|
|
{
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
block_io<BlockSize>::~block_io()
|
|
|
|
|
{
|
|
|
|
|
if (::close(fd_) < 0)
|
|
|
|
|
syscall_failed("close");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
void
|
|
|
|
|
block_io<BlockSize>::read_buffer(block_address location, buffer<BlockSize> &buffer) const
|
|
|
|
|
{
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
void
|
|
|
|
|
block_io<BlockSize>::read_buffer(block_address location, buffer<BlockSize> &buffer) const
|
|
|
|
|
{
|
|
|
|
|
off_t r;
|
|
|
|
|
r = ::lseek(fd_, BlockSize * location, SEEK_SET);
|
|
|
|
|
if (r == (off_t) -1)
|
|
|
|
@ -159,12 +156,12 @@ block_io<BlockSize>::read_buffer(block_address location, buffer<BlockSize> &buff
|
|
|
|
|
|
|
|
|
|
if (n < 0)
|
|
|
|
|
throw std::runtime_error("read failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
void
|
|
|
|
|
block_io<BlockSize>::write_buffer(block_address location, buffer<BlockSize> const &buffer)
|
|
|
|
|
{
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
void
|
|
|
|
|
block_io<BlockSize>::write_buffer(block_address location, buffer<BlockSize> const &buffer)
|
|
|
|
|
{
|
|
|
|
|
off_t r;
|
|
|
|
|
r = ::lseek(fd_, BlockSize * location, SEEK_SET);
|
|
|
|
|
if (r == (off_t) -1)
|
|
|
|
@ -192,12 +189,12 @@ block_io<BlockSize>::write_buffer(block_address location, buffer<BlockSize> cons
|
|
|
|
|
<< std::endl;
|
|
|
|
|
throw std::runtime_error(out.str());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
block_manager<BlockSize>::block::block(typename block_io<BlockSize>::ptr io,
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
block_manager<BlockSize>::block::block(typename block_io<BlockSize>::ptr io,
|
|
|
|
|
block_address location,
|
|
|
|
|
block_type bt,
|
|
|
|
|
typename validator::ptr v,
|
|
|
|
@ -208,7 +205,7 @@ block_manager<BlockSize>::block::block(typename block_io<BlockSize>::ptr io,
|
|
|
|
|
validator_(v),
|
|
|
|
|
bt_(bt),
|
|
|
|
|
dirty_(false)
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
if (zero) {
|
|
|
|
|
// FIXME: duplicate memset
|
|
|
|
|
memset(data_->raw(), 0, BlockSize);
|
|
|
|
@ -217,30 +214,30 @@ block_manager<BlockSize>::block::block(typename block_io<BlockSize>::ptr io,
|
|
|
|
|
io_->read_buffer(location_, *data_);
|
|
|
|
|
validator_->check(*data_, location_);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
block_manager<BlockSize>::block::~block()
|
|
|
|
|
{
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
block_manager<BlockSize>::block::~block()
|
|
|
|
|
{
|
|
|
|
|
flush();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
void
|
|
|
|
|
block_manager<BlockSize>::block::flush()
|
|
|
|
|
{
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
void
|
|
|
|
|
block_manager<BlockSize>::block::flush()
|
|
|
|
|
{
|
|
|
|
|
if (dirty_) {
|
|
|
|
|
validator_->prepare(*data_, location_);
|
|
|
|
|
io_->write_buffer(location_, *data_);
|
|
|
|
|
dirty_ = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
void
|
|
|
|
|
block_manager<BlockSize>::block::change_validator(typename block_manager<BlockSize>::validator::ptr v,
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
void
|
|
|
|
|
block_manager<BlockSize>::block::change_validator(typename block_manager<BlockSize>::validator::ptr v,
|
|
|
|
|
bool check)
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
if (v.get() != validator_.get()) {
|
|
|
|
|
if (dirty_)
|
|
|
|
|
// It may have already happened, by calling
|
|
|
|
@ -252,32 +249,32 @@ block_manager<BlockSize>::block::change_validator(typename block_manager<BlockSi
|
|
|
|
|
if (check)
|
|
|
|
|
validator_->check(*data_, location_);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
block_manager<BlockSize>::read_ref::read_ref(block_manager<BlockSize> const &bm,
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
block_manager<BlockSize>::read_ref::read_ref(block_manager<BlockSize> const &bm,
|
|
|
|
|
typename block::ptr b)
|
|
|
|
|
: bm_(&bm),
|
|
|
|
|
block_(b),
|
|
|
|
|
holders_(new unsigned)
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
*holders_ = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
block_manager<BlockSize>::read_ref::read_ref(read_ref const &rhs)
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
block_manager<BlockSize>::read_ref::read_ref(read_ref const &rhs)
|
|
|
|
|
: bm_(rhs.bm_),
|
|
|
|
|
block_(rhs.block_),
|
|
|
|
|
holders_(rhs.holders_)
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
(*holders_)++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
block_manager<BlockSize>::read_ref::~read_ref()
|
|
|
|
|
{
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
block_manager<BlockSize>::read_ref::~read_ref()
|
|
|
|
|
{
|
|
|
|
|
if (!--(*holders_)) {
|
|
|
|
|
if (block_->bt_ == BT_SUPERBLOCK) {
|
|
|
|
|
bm_->flush();
|
|
|
|
@ -289,12 +286,12 @@ block_manager<BlockSize>::read_ref::~read_ref()
|
|
|
|
|
bm_->tracker_.unlock(block_->location_);
|
|
|
|
|
delete holders_;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
typename block_manager<BlockSize>::read_ref const &
|
|
|
|
|
block_manager<BlockSize>::read_ref::operator =(read_ref const &rhs)
|
|
|
|
|
{
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
typename block_manager<BlockSize>::read_ref const &
|
|
|
|
|
block_manager<BlockSize>::read_ref::operator =(read_ref const &rhs)
|
|
|
|
|
{
|
|
|
|
|
if (this != &rhs) {
|
|
|
|
|
block_ = rhs.block_;
|
|
|
|
|
bm_ = rhs.bm_;
|
|
|
|
@ -303,57 +300,57 @@ block_manager<BlockSize>::read_ref::operator =(read_ref const &rhs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
block_address
|
|
|
|
|
block_manager<BlockSize>::read_ref::get_location() const
|
|
|
|
|
{
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
block_address
|
|
|
|
|
block_manager<BlockSize>::read_ref::get_location() const
|
|
|
|
|
{
|
|
|
|
|
return block_->location_;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
buffer<BlockSize> const &
|
|
|
|
|
block_manager<BlockSize>::read_ref::data() const
|
|
|
|
|
{
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
buffer<BlockSize> const &
|
|
|
|
|
block_manager<BlockSize>::read_ref::data() const
|
|
|
|
|
{
|
|
|
|
|
return *block_->data_;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//--------------------------------
|
|
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
block_manager<BlockSize>::write_ref::write_ref(block_manager<BlockSize> const &bm,
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
block_manager<BlockSize>::write_ref::write_ref(block_manager<BlockSize> const &bm,
|
|
|
|
|
typename block::ptr b)
|
|
|
|
|
: read_ref(bm, b)
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
b->dirty_ = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
buffer<BlockSize> &
|
|
|
|
|
block_manager<BlockSize>::write_ref::data()
|
|
|
|
|
{
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
buffer<BlockSize> &
|
|
|
|
|
block_manager<BlockSize>::write_ref::data()
|
|
|
|
|
{
|
|
|
|
|
return *read_ref::block_->data_;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
block_manager<BlockSize>::block_manager(std::string const &path,
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
block_manager<BlockSize>::block_manager(std::string const &path,
|
|
|
|
|
block_address nr_blocks,
|
|
|
|
|
unsigned max_concurrent_blocks,
|
|
|
|
|
typename block_io<BlockSize>::mode mode)
|
|
|
|
|
: io_(new block_io<BlockSize>(path, nr_blocks, mode)),
|
|
|
|
|
cache_(max(1024u, max_concurrent_blocks)),
|
|
|
|
|
tracker_(0, nr_blocks)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
typename block_manager<BlockSize>::read_ref
|
|
|
|
|
block_manager<BlockSize>::read_lock(block_address location,
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
typename block_manager<BlockSize>::read_ref
|
|
|
|
|
block_manager<BlockSize>::read_lock(block_address location,
|
|
|
|
|
typename block_manager<BlockSize>::validator::ptr v) const
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
tracker_.read_lock(location);
|
|
|
|
|
try {
|
|
|
|
|
check(location);
|
|
|
|
@ -375,13 +372,13 @@ block_manager<BlockSize>::read_lock(block_address location,
|
|
|
|
|
tracker_.unlock(location);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
typename block_manager<BlockSize>::write_ref
|
|
|
|
|
block_manager<BlockSize>::write_lock(block_address location,
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
typename block_manager<BlockSize>::write_ref
|
|
|
|
|
block_manager<BlockSize>::write_lock(block_address location,
|
|
|
|
|
typename block_manager<BlockSize>::validator::ptr v)
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
tracker_.write_lock(location);
|
|
|
|
|
try {
|
|
|
|
|
check(location);
|
|
|
|
@ -405,13 +402,13 @@ block_manager<BlockSize>::write_lock(block_address location,
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
typename block_manager<BlockSize>::write_ref
|
|
|
|
|
block_manager<BlockSize>::write_lock_zero(block_address location,
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
typename block_manager<BlockSize>::write_ref
|
|
|
|
|
block_manager<BlockSize>::write_lock_zero(block_address location,
|
|
|
|
|
typename block_manager<BlockSize>::validator::ptr v)
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
tracker_.write_lock(location);
|
|
|
|
|
try {
|
|
|
|
|
check(location);
|
|
|
|
@ -434,13 +431,13 @@ block_manager<BlockSize>::write_lock_zero(block_address location,
|
|
|
|
|
tracker_.unlock(location);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
typename block_manager<BlockSize>::write_ref
|
|
|
|
|
block_manager<BlockSize>::superblock(block_address location,
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
typename block_manager<BlockSize>::write_ref
|
|
|
|
|
block_manager<BlockSize>::superblock(block_address location,
|
|
|
|
|
typename block_manager<BlockSize>::validator::ptr v)
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
tracker_.superblock_lock(location);
|
|
|
|
|
try {
|
|
|
|
|
check(location);
|
|
|
|
@ -464,13 +461,13 @@ block_manager<BlockSize>::superblock(block_address location,
|
|
|
|
|
tracker_.unlock(location);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
typename block_manager<BlockSize>::write_ref
|
|
|
|
|
block_manager<BlockSize>::superblock_zero(block_address location,
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
typename block_manager<BlockSize>::write_ref
|
|
|
|
|
block_manager<BlockSize>::superblock_zero(block_address location,
|
|
|
|
|
typename block_manager<BlockSize>::validator::ptr v)
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
tracker_.superblock_lock(location);
|
|
|
|
|
try {
|
|
|
|
|
check(location);
|
|
|
|
@ -495,43 +492,44 @@ block_manager<BlockSize>::superblock_zero(block_address location,
|
|
|
|
|
tracker_.unlock(location);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
void
|
|
|
|
|
block_manager<BlockSize>::check(block_address b) const
|
|
|
|
|
{
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
void
|
|
|
|
|
block_manager<BlockSize>::check(block_address b) const
|
|
|
|
|
{
|
|
|
|
|
if (b >= io_->get_nr_blocks())
|
|
|
|
|
throw std::runtime_error("block address out of bounds");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
block_address
|
|
|
|
|
block_manager<BlockSize>::get_nr_blocks() const
|
|
|
|
|
{
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
block_address
|
|
|
|
|
block_manager<BlockSize>::get_nr_blocks() const
|
|
|
|
|
{
|
|
|
|
|
return io_->get_nr_blocks();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
void
|
|
|
|
|
block_manager<BlockSize>::write_block(typename block::ptr b) const
|
|
|
|
|
{
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
void
|
|
|
|
|
block_manager<BlockSize>::write_block(typename block::ptr b) const
|
|
|
|
|
{
|
|
|
|
|
b->flush();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
void
|
|
|
|
|
block_manager<BlockSize>::flush() const
|
|
|
|
|
{
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
void
|
|
|
|
|
block_manager<BlockSize>::flush() const
|
|
|
|
|
{
|
|
|
|
|
cache_.iterate_unheld(
|
|
|
|
|
boost::bind(&block_manager<BlockSize>::write_block, this, _1));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
bool
|
|
|
|
|
block_manager<BlockSize>::is_locked(block_address b) const
|
|
|
|
|
{
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
|
bool
|
|
|
|
|
block_manager<BlockSize>::is_locked(block_address b) const
|
|
|
|
|
{
|
|
|
|
|
return tracker_.is_locked(b);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|