thin-provisioning-tools/block.tcc

245 lines
5.6 KiB
Plaintext
Raw Normal View History

#include "block.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
2011-06-27 15:15:30 +05:30
#include <boost/bind.hpp>
#include <iostream>
#include <stdexcept>
using namespace boost;
using namespace persistent_data;
2011-06-27 15:15:30 +05:30
using namespace std;
//----------------------------------------------------------------
template <uint32_t BlockSize>
2011-07-13 19:39:33 +05:30
block_manager<BlockSize>::read_ref::read_ref(typename block_manager::block::ptr b)
: block_(b)
{
}
template <uint32_t BlockSize>
block_address
block_manager<BlockSize>::read_ref::get_location() const
{
return block_->location_;
}
template <uint32_t BlockSize>
typename block_manager<BlockSize>::const_buffer &
block_manager<BlockSize>::read_ref::data() const
{
return block_->data_;
}
2011-06-27 15:15:30 +05:30
template <uint32_t BlockSize>
2011-07-13 19:39:33 +05:30
block_manager<BlockSize>::write_ref::write_ref(typename block_manager::block::ptr b)
2011-06-27 15:15:30 +05:30
: read_ref(b)
{
}
template <uint32_t BlockSize>
typename block_manager<BlockSize>::buffer &
block_manager<BlockSize>::write_ref::data()
{
return read_ref::block_->data_;
}
//----------------------------------------------------------------
template <uint32_t BlockSize>
2011-07-13 19:39:33 +05:30
block_manager<BlockSize>::block_manager(std::string const &path, block_address nr_blocks)
: nr_blocks_(nr_blocks)
{
2011-07-13 19:39:33 +05:30
fd_ = ::open(path.c_str(), O_RDWR | O_CREAT, 0666);
if (fd_ < 0)
throw std::runtime_error("couldn't open file");
}
template <uint32_t BlockSize>
block_manager<BlockSize>::~block_manager()
{
::close(fd_);
}
template <uint32_t BlockSize>
typename block_manager<BlockSize>::read_ref
2011-07-13 19:39:33 +05:30
block_manager<BlockSize>::read_lock(block_address location) const
{
2011-07-13 19:39:33 +05:30
check(location);
typename block::ptr b(new block(location));
2011-06-27 15:15:30 +05:30
read_block(*b);
return read_ref(b);
}
template <uint32_t BlockSize>
optional<typename block_manager<BlockSize>::read_ref>
2011-07-13 19:39:33 +05:30
block_manager<BlockSize>::read_try_lock(block_address location) const
{
return read_lock(location);
}
template <uint32_t BlockSize>
typename block_manager<BlockSize>::write_ref
block_manager<BlockSize>::write_lock(block_address location)
{
2011-07-13 19:39:33 +05:30
check(location);
typename block::ptr b(new block(location), bind(&block_manager::write_and_release, this, _1));
2011-06-27 15:15:30 +05:30
read_block(*b);
return write_ref(b);
}
template <uint32_t BlockSize>
typename block_manager<BlockSize>::write_ref
block_manager<BlockSize>::write_lock_zero(block_address location)
{
2011-07-13 19:39:33 +05:30
check(location);
typename block::ptr b(new block(location), bind(&block_manager<BlockSize>::write_and_release, this, _1));
2011-06-27 15:15:30 +05:30
zero_block(*b);
return write_ref(b);
}
template <uint32_t BlockSize>
typename block_manager<BlockSize>::read_ref
block_manager<BlockSize>::read_lock(block_address location,
2011-07-13 19:39:33 +05:30
typename block_manager<BlockSize>::validator::ptr const &v) const
{
2011-07-13 19:39:33 +05:30
check(location);
typename block::ptr b(new block(location, v));
2011-06-27 15:15:30 +05:30
read_block(*b);
return read_ref(b);
}
template <uint32_t BlockSize>
optional<typename block_manager<BlockSize>::read_ref>
block_manager<BlockSize>::read_try_lock(block_address location,
2011-07-13 19:39:33 +05:30
typename block_manager<BlockSize>::validator::ptr const &v) const
{
return read_lock(location, v);
}
template <uint32_t BlockSize>
typename block_manager<BlockSize>::write_ref
block_manager<BlockSize>::write_lock(block_address location,
2011-07-13 19:39:33 +05:30
typename block_manager<BlockSize>::validator::ptr const &v)
{
2011-07-13 19:39:33 +05:30
check(location);
typename block::ptr b(new block(location, v),
bind(&block_manager::write_and_release, this, _1));
2011-06-27 15:15:30 +05:30
read_block(*b);
return write_ref(b);
}
template <uint32_t BlockSize>
typename block_manager<BlockSize>::write_ref
block_manager<BlockSize>::write_lock_zero(block_address location,
2011-07-13 19:39:33 +05:30
typename block_manager<BlockSize>::validator::ptr const &v)
{
2011-07-13 19:39:33 +05:30
check(location);
typename block::ptr b(new block(location, v),
bind(&block_manager::write_and_release, this, _1));
2011-06-27 15:15:30 +05:30
zero_block(*b);
return write_ref(b);
}
template <uint32_t BlockSize>
void
block_manager<BlockSize>::flush(block_manager<BlockSize>::write_ref super_block)
{
2011-07-13 19:39:33 +05:30
// FIXME: the caller still holds the write_ref, so the superblock
// will get written twice
write_block(super_block);
::fsync(fd_);
}
template <uint32_t BlockSize>
void
2011-07-13 19:39:33 +05:30
block_manager<BlockSize>::read_block(block &b) const
{
off_t r;
2011-06-27 15:15:30 +05:30
r = ::lseek(fd_, BlockSize * b.location_, SEEK_SET);
if (r == (off_t) -1)
throw std::runtime_error("lseek failed");
ssize_t n;
size_t remaining = BlockSize;
2011-06-27 15:15:30 +05:30
unsigned char *buf = b.data_;
do {
n = ::read(fd_, buf, remaining);
if (n > 0) {
remaining -= n;
buf += n;
}
} while (remaining && ((n > 0) || (n == EINTR) || (n == EAGAIN)));
if (n < 0)
throw std::runtime_error("read failed");
2011-07-13 19:39:33 +05:30
b.initialised_ = true;
}
template <uint32_t BlockSize>
void
block_manager<BlockSize>::write_block(block const &b)
{
off_t r;
2011-06-27 15:15:30 +05:30
r = ::lseek(fd_, BlockSize * b.location_, SEEK_SET);
if (r == (off_t) -1)
throw std::runtime_error("lseek failed");
ssize_t n;
size_t remaining = BlockSize;
2011-06-27 15:15:30 +05:30
unsigned char const *buf = b.data_;
do {
2011-06-27 15:15:30 +05:30
n = ::write(fd_, buf, remaining);
if (n > 0) {
remaining -= n;
buf += n;
}
} while (remaining && ((n > 0) || (n == EINTR) || (n == EAGAIN)));
if (n < 0)
throw std::runtime_error("write failed");
}
template <uint32_t BlockSize>
void
block_manager<BlockSize>::zero_block(block &b)
{
2011-06-27 15:15:30 +05:30
memset(b.data_, 0, BlockSize);
2011-07-13 19:39:33 +05:30
b.initialised_ = true;
}
template <uint32_t BlockSize>
void
block_manager<BlockSize>::write_and_release(block *b)
{
2011-07-13 19:39:33 +05:30
if (b->initialised_) {
if (b->validator_)
(*b->validator_)->prepare(*b);
write_block(*b);
}
delete b;
}
2011-07-13 19:39:33 +05:30
template <uint32_t BlockSize>
void
block_manager<BlockSize>::check(block_address b) const
{
if (b >= nr_blocks_)
throw std::runtime_error("block address out of bounds");
}
//----------------------------------------------------------------