work in progress

This commit is contained in:
Joe Thornber
2014-07-25 10:35:04 +01:00
parent d9040949fc
commit b32908d5c2
11 changed files with 717 additions and 663 deletions

View File

@@ -20,7 +20,7 @@
#define BLOCK_H
#include "block-cache/block_cache.h"
#include "persistent-data/buffer.h"
#include "block-cache/buffer.h"
#include <stdint.h>
#include <map>
@@ -35,6 +35,8 @@
//----------------------------------------------------------------
namespace persistent_data {
using namespace bcache;
uint32_t const MD_BLOCK_SIZE = 4096;
@@ -77,10 +79,11 @@ namespace persistent_data {
BT_NORMAL
};
// FIXME: eventually this will disappear to be replaced with block_cache::block
struct block : private boost::noncopyable {
typedef boost::shared_ptr<block> ptr;
block(block_cache *bc,
block(block_cache &bc,
block_address location,
block_type bt,
typename validator::ptr v,
@@ -110,7 +113,8 @@ namespace persistent_data {
private:
void check_not_unlocked() const;
bc_block *internal_;
block_cache &bc_;
block_cache::block *internal_;
typename validator::ptr validator_;
block_type bt_;
bool dirty_;
@@ -196,7 +200,9 @@ namespace persistent_data {
void write_block(typename block::ptr b) const;
int fd_;
block_cache *bc_;
// FIXME: the mutable is a fudge to allow flush() to be const, which I'm not sure is necc.
mutable block_cache bc_;
};
// A little utility to help build validators

View File

@@ -40,7 +40,7 @@ namespace {
unsigned const SECTOR_SHIFT = 9;
// FIXME: these will slow it down until we start doing async io.
int const OPEN_FLAGS = O_DIRECT | O_SYNC;
int const OPEN_FLAGS = O_DIRECT;
// FIXME: introduce a new exception for this, or at least lift this
// to exception.h
@@ -106,31 +106,27 @@ namespace {
namespace persistent_data {
template <uint32_t BlockSize>
block_manager<BlockSize>::block::block(block_cache *bc,
block_manager<BlockSize>::block::block(block_cache &bc,
block_address location,
block_type bt,
typename validator::ptr v,
bool zero)
: validator_(v),
bt_(bt),
dirty_(false),
unlocked_(false),
buffer_(0, true) // FIXME: we don't know if it's writeable here :(
: bc_(bc),
validator_(v),
bt_(bt),
dirty_(false),
unlocked_(false),
buffer_(0, true) // FIXME: we don't know if it's writeable here :(
{
if (zero) {
internal_ = block_cache_get(bc, location, GF_ZERO | GF_CAN_BLOCK);
if (!internal_)
throw std::runtime_error("Couldn't get block");
internal_ = &bc.get(location, block_cache::GF_ZERO | block_cache::GF_CAN_BLOCK);
dirty_ = true;
buffer_.set_data(internal_->get_data());
} else {
internal_ = block_cache_get(bc, location, GF_CAN_BLOCK);
if (!internal_)
throw std::runtime_error("Couldn't get block");
validator_->check(buffer_, internal_->index);
internal_ = &bc.get(location, block_cache::GF_CAN_BLOCK);
buffer_.set_data(internal_->get_data());
validator_->check(buffer_, internal_->get_index());
}
buffer_.set_data(internal_->data);
}
template <uint32_t BlockSize>
@@ -144,8 +140,9 @@ namespace persistent_data {
void
block_manager<BlockSize>::block::unlock()
{
validator_->prepare(buffer_, internal_->index);
block_cache_put(internal_, dirty_ ? PF_DIRTY : 0);
if (dirty_)
validator_->prepare(buffer_, internal_->get_index());
bc_.put(*internal_, dirty_ ? block_cache::PF_DIRTY : 0);
unlocked_ = true;
}
@@ -161,7 +158,7 @@ namespace persistent_data {
block_manager<BlockSize>::block::get_location() const
{
check_not_unlocked();
return internal_->index;
return internal_->get_index();
}
template <uint32_t BlockSize>
@@ -196,12 +193,12 @@ namespace persistent_data {
if (dirty_)
// It may have already happened, by calling
// this we ensure we're consistent.
validator_->prepare(*internal_->data, internal_->index);
validator_->prepare(*internal_->get_data(), internal_->get_index());
validator_ = v;
if (check)
validator_->check(*internal_->data, internal_->index);
validator_->check(*internal_->get_data(), internal_->get_index());
}
}
@@ -301,14 +298,9 @@ namespace persistent_data {
block_address nr_blocks,
unsigned max_concurrent_blocks,
mode m)
: fd_(open_block_file(path, nr_blocks * BlockSize, m == READ_WRITE)),
bc_(fd_, BlockSize >> SECTOR_SHIFT, nr_blocks, 1024u * 1024u * 4)
{
// Open the file descriptor
fd_ = open_block_file(path, nr_blocks * BlockSize, m == READ_WRITE);
// Create the cache
bc_ = block_cache_create(fd_, BlockSize << SECTOR_SHIFT, nr_blocks, 1024u * BlockSize * 1.2);
if (!bc_)
throw std::runtime_error("couldn't create block cache");
}
template <uint32_t BlockSize>
@@ -360,7 +352,7 @@ namespace persistent_data {
block_address
block_manager<BlockSize>::get_nr_blocks() const
{
return block_cache_get_nr_blocks(bc_);
return bc_.get_nr_blocks();
}
template <uint32_t BlockSize>
@@ -374,7 +366,7 @@ namespace persistent_data {
void
block_manager<BlockSize>::flush() const
{
block_cache_flush(bc_);
bc_.flush();
}
}

View File

@@ -1,94 +0,0 @@
// Copyright (C) 2013 Red Hat, Inc. All rights reserved.
//
// This file is part of the thin-provisioning-tools source.
//
// thin-provisioning-tools is free software: you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// thin-provisioning-tools is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with thin-provisioning-tools. If not, see
// <http://www.gnu.org/licenses/>.
#ifndef BUFFER_H
#define BUFFER_H
#include <stdint.h>
#include <malloc.h>
#include <boost/optional.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/static_assert.hpp>
#include <stdexcept>
//----------------------------------------------------------------
namespace persistent_data {
uint32_t const DEFAULT_BUFFER_SIZE = 4096;
template <uint32_t Size = DEFAULT_BUFFER_SIZE>
class buffer {
public:
buffer(void *data, bool writeable = true)
: data_(static_cast<unsigned char *>(data)),
writeable_(writeable) {
}
typedef boost::shared_ptr<buffer> ptr;
typedef boost::shared_ptr<buffer const> const_ptr;
size_t size() const {
return Size;
}
unsigned char &operator[](unsigned index) {
check_writeable();
check_index(index);
return data_[index];
}
unsigned char const &operator[](unsigned index) const {
check_index(index);
return data_[index];
}
unsigned char *raw() {
return data_;
}
unsigned char const *raw() const {
return data_;
}
void set_data(void *data) {
data_ = static_cast<unsigned char *>(data);
}
private:
static void check_index(unsigned index) {
if (index >= Size)
throw std::range_error("buffer index out of bounds");
}
void check_writeable() const {
if (!writeable_)
throw std::runtime_error("buffer isn't writeable");
}
unsigned char *data_;
bool writeable_;
};
}
//----------------------------------------------------------------
#endif

View File

@@ -33,7 +33,7 @@ namespace persistent_data {
struct array_block_validator : public block_manager<>::validator {
virtual void check(buffer<> const &b, block_address location) const {
array_block_disk const *data = reinterpret_cast<array_block_disk const *>(&b);
array_block_disk const *data = reinterpret_cast<array_block_disk const *>(b.raw());
crc32c sum(ARRAY_CSUM_XOR);
sum.append(&data->max_entries, MD_BLOCK_SIZE - sizeof(uint32_t));
if (sum.get_sum() != to_cpu<uint32_t>(data->csum))
@@ -44,7 +44,7 @@ namespace persistent_data {
}
virtual void prepare(buffer<> &b, block_address location) const {
array_block_disk *data = reinterpret_cast<array_block_disk *>(&b);
array_block_disk *data = reinterpret_cast<array_block_disk *>(b.raw());
data->blocknr = to_disk<base::le64, uint64_t>(location);
crc32c sum(ARRAY_CSUM_XOR);

View File

@@ -34,7 +34,7 @@ namespace {
struct btree_node_validator : public block_manager<>::validator {
virtual void check(buffer<> const &b, block_address location) const {
disk_node const *data = reinterpret_cast<disk_node const *>(&b);
disk_node const *data = reinterpret_cast<disk_node const *>(b.raw());
node_header const *n = &data->header;
crc32c sum(BTREE_CSUM_XOR);
sum.append(&n->flags, MD_BLOCK_SIZE - sizeof(uint32_t));
@@ -46,7 +46,7 @@ namespace {
}
virtual void prepare(buffer<> &b, block_address location) const {
disk_node *data = reinterpret_cast<disk_node *>(&b);
disk_node *data = reinterpret_cast<disk_node *>(b.raw());
node_header *n = &data->header;
n->blocknr = to_disk<base::le64, uint64_t>(location);

View File

@@ -39,7 +39,7 @@ namespace {
struct bitmap_block_validator : public block_manager<>::validator {
virtual void check(buffer<> const &b, block_address location) const {
bitmap_header const *data = reinterpret_cast<bitmap_header const *>(&b);
bitmap_header const *data = reinterpret_cast<bitmap_header const *>(b.raw());
crc32c sum(BITMAP_CSUM_XOR);
sum.append(&data->not_used, MD_BLOCK_SIZE - sizeof(uint32_t));
if (sum.get_sum() != to_cpu<uint32_t>(data->csum))
@@ -50,7 +50,7 @@ namespace {
}
virtual void prepare(buffer<> &b, block_address location) const {
bitmap_header *data = reinterpret_cast<bitmap_header *>(&b);
bitmap_header *data = reinterpret_cast<bitmap_header *>(b.raw());
data->blocknr = to_disk<base::le64, uint64_t>(location);
crc32c sum(BITMAP_CSUM_XOR);
@@ -66,7 +66,8 @@ namespace {
// FIXME: factor out the common code in these validators
struct index_block_validator : public block_manager<>::validator {
virtual void check(buffer<> const &b, block_address location) const {
metadata_index const *mi = reinterpret_cast<metadata_index const *>(&b);
metadata_index const *mi = reinterpret_cast<metadata_index const *>(b.raw());
std::cerr << "check mi = " << mi << "\n";
crc32c sum(INDEX_CSUM_XOR);
sum.append(&mi->padding_, MD_BLOCK_SIZE - sizeof(uint32_t));
if (sum.get_sum() != to_cpu<uint32_t>(mi->csum_))
@@ -77,7 +78,8 @@ namespace {
}
virtual void prepare(buffer<> &b, block_address location) const {
metadata_index *mi = reinterpret_cast<metadata_index *>(&b);
metadata_index *mi = reinterpret_cast<metadata_index *>(b.raw());
std::cerr << "prepare mi = " << mi << "\n";
mi->blocknr_ = to_disk<base::le64, uint64_t>(location);
crc32c sum(INDEX_CSUM_XOR);
@@ -630,7 +632,7 @@ namespace {
tm_->shadow(bitmap_root_, index_validator());
bitmap_root_ = p.first.get_location();
metadata_index *mdi = reinterpret_cast<metadata_index *>(&p.first.data());
metadata_index *mdi = reinterpret_cast<metadata_index *>(p.first.data().raw());
for (unsigned i = 0; i < entries_.size(); i++)
index_entry_traits::pack(entries_[i], mdi->index[i]);