[block_manager] Hard code block size to 4k.

We're never going to use anything other than 4k, and by hard coding it we
avoid making block_manager a template.
This commit is contained in:
Joe Thornber
2020-04-30 14:30:01 +01:00
parent 3e5de399a7
commit e801cc607b
57 changed files with 390 additions and 448 deletions

203
persistent-data/block.cc Normal file
View File

@ -0,0 +1,203 @@
// Copyright (C) 2011 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/>.
#include "block.h"
#include "base/error_string.h"
#include "base/file_utils.h"
#include "block-cache/io_engine.h"
#include <boost/bind.hpp>
#include <stdexcept>
using namespace persistent_data;
//----------------------------------------------------------------
block_manager::read_ref::read_ref(block_cache::block &b)
: b_(b)
{
}
block_manager::read_ref::read_ref(read_ref const &rhs)
: b_(rhs.b_)
{
b_.get();
}
block_manager::read_ref::~read_ref()
{
b_.put();
}
block_address
block_manager::read_ref::get_location() const
{
return b_.get_index();
}
void const *
block_manager::read_ref::data() const
{
return b_.get_data();
}
//--------------------------------
block_manager::write_ref::write_ref(block_cache::block &b)
: read_ref(b),
ref_count_(NULL)
{
}
block_manager::write_ref::write_ref(block_cache::block &b, unsigned &ref_count)
: read_ref(b),
ref_count_(&ref_count) {
if (*ref_count_)
throw std::runtime_error("superblock already locked");
(*ref_count_)++;
}
block_manager::write_ref::write_ref(write_ref const &rhs)
: read_ref(rhs),
ref_count_(rhs.ref_count_) {
if (ref_count_)
(*ref_count_)++;
}
block_manager::write_ref::~write_ref()
{
if (ref_count_) {
if (!*ref_count_) {
std::cerr << "write_ref ref_count going below zero";
::exit(1);
}
(*ref_count_)--;
}
}
void *
block_manager::write_ref::data()
{
return read_ref::b_.get_data();
}
//----------------------------------------------------------------
uint64_t
block_manager::choose_cache_size(block_address nr_blocks) const
{
uint64_t const DEFAULT_CACHE_SIZE = 1024 * 1024 * 16;
return std::min<uint64_t>(DEFAULT_CACHE_SIZE, MD_BLOCK_SIZE * nr_blocks);
}
block_manager::block_manager(std::string const &path,
block_address nr_blocks,
unsigned max_concurrent_blocks,
mode m,
bool excl)
: fd_(open_or_create_block_file(path, nr_blocks * MD_BLOCK_SIZE, m, excl)),
bc_(fd_, MD_BLOCK_SIZE >> SECTOR_SHIFT, nr_blocks, choose_cache_size(nr_blocks)),
superblock_ref_count_(0)
{
}
file_utils::file_descriptor
block_manager::open_or_create_block_file(std::string const &path, off_t file_size, mode m, bool excl)
{
switch (m) {
case READ_ONLY:
return file_utils::open_block_file(path, file_size, false, excl);
case READ_WRITE:
return file_utils::open_block_file(path, file_size, true, excl);
case CREATE:
return file_utils::create_block_file(path, file_size);
default:
throw std::runtime_error("unsupported mode");
}
}
block_manager::read_ref
block_manager::read_lock(block_address location,
typename bcache::validator::ptr v) const
{
block_cache::block &b = bc_.get(location, 0, v);
return read_ref(b);
}
block_manager::write_ref
block_manager::write_lock(block_address location,
typename bcache::validator::ptr v)
{
block_cache::block &b = bc_.get(location, block_cache::GF_DIRTY, v);
return write_ref(b);
}
block_manager::write_ref
block_manager::write_lock_zero(block_address location,
typename bcache::validator::ptr v)
{
block_cache::block &b = bc_.get(location, block_cache::GF_ZERO, v);
return write_ref(b);
}
block_manager::write_ref
block_manager::superblock(block_address location,
typename bcache::validator::ptr v)
{
if (bc_.get_nr_locked() > 0)
throw std::runtime_error("attempt to lock superblock while other locks are still held");
block_cache::block &b = bc_.get(location, block_cache::GF_DIRTY | block_cache::GF_BARRIER, v);
return write_ref(b, superblock_ref_count_);
}
block_manager::write_ref
block_manager::superblock_zero(block_address location,
typename bcache::validator::ptr v)
{
if (bc_.get_nr_locked() > 0)
throw std::runtime_error("attempt to lock superblock while other locks are still held");
block_cache::block &b = bc_.get(location, block_cache::GF_ZERO | block_cache::GF_BARRIER, v);
return write_ref(b, superblock_ref_count_);
}
block_address
block_manager::get_nr_blocks() const
{
return bc_.get_nr_blocks();
}
void
block_manager::prefetch(block_address b) const
{
bc_.prefetch(b);
}
void
block_manager::flush() const
{
bc_.flush();
}
//----------------------------------------------------------------

View File

@ -38,7 +38,6 @@ namespace persistent_data {
uint32_t const MD_BLOCK_SIZE = 4096;
template <uint32_t BlockSize = MD_BLOCK_SIZE>
class block_manager : private boost::noncopyable {
public:
typedef boost::shared_ptr<block_manager> ptr;
@ -57,7 +56,7 @@ namespace persistent_data {
class read_ref {
public:
static uint32_t const BLOCK_SIZE = BlockSize;
static uint32_t const BLOCK_SIZE = MD_BLOCK_SIZE;
read_ref(block_cache::block &b);
@ -149,8 +148,6 @@ namespace persistent_data {
}
}
#include "block.tcc"
//----------------------------------------------------------------
#endif

View File

@ -1,224 +0,0 @@
// Copyright (C) 2011 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/>.
#include "block.h"
#include "base/error_string.h"
#include "base/file_utils.h"
#include "block-cache/io_engine.h"
#include <boost/bind.hpp>
#include <stdexcept>
//----------------------------------------------------------------
namespace persistent_data {
template <uint32_t BlockSize>
block_manager<BlockSize>::read_ref::read_ref(block_cache::block &b)
: b_(b)
{
}
template <uint32_t BlockSize>
block_manager<BlockSize>::read_ref::read_ref(read_ref const &rhs)
: b_(rhs.b_)
{
b_.get();
}
template <uint32_t BlockSize>
block_manager<BlockSize>::read_ref::~read_ref()
{
b_.put();
}
template <uint32_t BlockSize>
block_address
block_manager<BlockSize>::read_ref::get_location() const
{
return b_.get_index();
}
template <uint32_t BlockSize>
void const *
block_manager<BlockSize>::read_ref::data() const
{
return b_.get_data();
}
//--------------------------------
template <uint32_t BlockSize>
block_manager<BlockSize>::write_ref::write_ref(block_cache::block &b)
: read_ref(b),
ref_count_(NULL)
{
}
template <uint32_t BlockSize>
block_manager<BlockSize>::write_ref::write_ref(block_cache::block &b, unsigned &ref_count)
: read_ref(b),
ref_count_(&ref_count) {
if (*ref_count_)
throw std::runtime_error("superblock already locked");
(*ref_count_)++;
}
template <uint32_t BlockSize>
block_manager<BlockSize>::write_ref::write_ref(write_ref const &rhs)
: read_ref(rhs),
ref_count_(rhs.ref_count_) {
if (ref_count_)
(*ref_count_)++;
}
template <uint32_t BlockSize>
block_manager<BlockSize>::write_ref::~write_ref()
{
if (ref_count_) {
if (!*ref_count_) {
std::cerr << "write_ref ref_count going below zero";
::exit(1);
}
(*ref_count_)--;
}
}
template <uint32_t BlockSize>
void *
block_manager<BlockSize>::write_ref::data()
{
return read_ref::b_.get_data();
}
//----------------------------------------------------------------
template <uint32_t BlockSize>
uint64_t
block_manager<BlockSize>::choose_cache_size(block_address nr_blocks) const
{
uint64_t const DEFAULT_CACHE_SIZE = 1024 * 1024 * 16;
return std::min<uint64_t>(DEFAULT_CACHE_SIZE, BlockSize * nr_blocks);
}
template <uint32_t BlockSize>
block_manager<BlockSize>::block_manager(std::string const &path,
block_address nr_blocks,
unsigned max_concurrent_blocks,
mode m,
bool excl)
: fd_(open_or_create_block_file(path, nr_blocks * BlockSize, m, excl)),
bc_(fd_, BlockSize >> SECTOR_SHIFT, nr_blocks, choose_cache_size(nr_blocks)),
superblock_ref_count_(0)
{
}
template <uint32_t BlockSize>
file_utils::file_descriptor
block_manager<BlockSize>::open_or_create_block_file(std::string const &path, off_t file_size, mode m, bool excl)
{
switch (m) {
case READ_ONLY:
return file_utils::open_block_file(path, file_size, false, excl);
case READ_WRITE:
return file_utils::open_block_file(path, file_size, true, excl);
case CREATE:
return file_utils::create_block_file(path, file_size);
default:
throw std::runtime_error("unsupported mode");
}
}
template <uint32_t BlockSize>
typename block_manager<BlockSize>::read_ref
block_manager<BlockSize>::read_lock(block_address location,
typename bcache::validator::ptr v) const
{
block_cache::block &b = bc_.get(location, 0, v);
return read_ref(b);
}
template <uint32_t BlockSize>
typename block_manager<BlockSize>::write_ref
block_manager<BlockSize>::write_lock(block_address location,
typename bcache::validator::ptr v)
{
block_cache::block &b = bc_.get(location, block_cache::GF_DIRTY, v);
return write_ref(b);
}
template <uint32_t BlockSize>
typename block_manager<BlockSize>::write_ref
block_manager<BlockSize>::write_lock_zero(block_address location,
typename bcache::validator::ptr v)
{
block_cache::block &b = bc_.get(location, block_cache::GF_ZERO, v);
return write_ref(b);
}
template <uint32_t BlockSize>
typename block_manager<BlockSize>::write_ref
block_manager<BlockSize>::superblock(block_address location,
typename bcache::validator::ptr v)
{
if (bc_.get_nr_locked() > 0)
throw std::runtime_error("attempt to lock superblock while other locks are still held");
block_cache::block &b = bc_.get(location, block_cache::GF_DIRTY | block_cache::GF_BARRIER, v);
return write_ref(b, superblock_ref_count_);
}
template <uint32_t BlockSize>
typename block_manager<BlockSize>::write_ref
block_manager<BlockSize>::superblock_zero(block_address location,
typename bcache::validator::ptr v)
{
if (bc_.get_nr_locked() > 0)
throw std::runtime_error("attempt to lock superblock while other locks are still held");
block_cache::block &b = bc_.get(location, block_cache::GF_ZERO | block_cache::GF_BARRIER, v);
return write_ref(b, superblock_ref_count_);
}
template <uint32_t BlockSize>
block_address
block_manager<BlockSize>::get_nr_blocks() const
{
return bc_.get_nr_blocks();
}
template <uint32_t BlockSize>
void
block_manager<BlockSize>::prefetch(block_address b) const
{
bc_.prefetch(b);
}
template <uint32_t BlockSize>
void
block_manager<BlockSize>::flush() const
{
bc_.flush();
}
}
//----------------------------------------------------------------

View File

@ -216,11 +216,11 @@ namespace persistent_data {
unsigned entries_per_block_;
};
typedef block_manager<>::write_ref write_ref;
typedef block_manager<>::read_ref read_ref;
typedef block_manager::write_ref write_ref;
typedef block_manager::read_ref read_ref;
typedef array_block<ValueTraits, block_manager<>::write_ref> wblock;
typedef array_block<ValueTraits, block_manager<>::read_ref> rblock;
typedef array_block<ValueTraits, block_manager::write_ref> wblock;
typedef array_block<ValueTraits, block_manager::read_ref> rblock;
typedef boost::shared_ptr<array<ValueTraits> > ptr;
typedef typename ValueTraits::value_type value_type;

View File

@ -152,7 +152,7 @@ namespace persistent_data {
//
template <typename ValueTraits>
node_ref<ValueTraits>
to_node(typename block_manager<>::read_ref &b)
to_node(typename block_manager::read_ref &b)
{
// FIXME: this should return a const read_ref somehow.
return node_ref<ValueTraits>(
@ -163,7 +163,7 @@ namespace persistent_data {
template <typename ValueTraits>
node_ref<ValueTraits>
to_node(typename block_manager<>::write_ref &b)
to_node(typename block_manager::write_ref &b)
{
return node_ref<ValueTraits>(
b.get_location(),
@ -188,7 +188,7 @@ namespace persistent_data {
private:
transaction_manager &tm_;
bcache::validator::ptr validator_;
std::list<block_manager<>::read_ref> spine_;
std::list<block_manager::read_ref> spine_;
};
class shadow_spine : private boost::noncopyable {
@ -252,7 +252,7 @@ namespace persistent_data {
private:
transaction_manager &tm_;
bcache::validator::ptr validator_;
std::list<block_manager<>::write_ref> spine_;
std::list<block_manager::write_ref> spine_;
maybe_block root_;
};
@ -304,8 +304,8 @@ namespace persistent_data {
typedef typename ValueTraits::value_type value_type;
typedef boost::optional<value_type> maybe_value;
typedef boost::optional<std::pair<unsigned, value_type> > maybe_pair;
typedef typename block_manager<>::read_ref read_ref;
typedef typename block_manager<>::write_ref write_ref;
typedef typename block_manager::read_ref read_ref;
typedef typename block_manager::write_ref write_ref;
typedef typename btree_detail::node_ref<ValueTraits> leaf_node;
typedef typename btree_detail::node_ref<block_traits> internal_node;

View File

@ -19,8 +19,8 @@ using namespace std;
//----------------------------------------------------------------
bool
persistent_data::check_for_xml(block_manager<>::ptr bm) {
block_manager<>::read_ref b = bm->read_lock(0);
persistent_data::check_for_xml(block_manager::ptr bm) {
block_manager::read_ref b = bm->read_lock(0);
const char *data = reinterpret_cast<const char *>(b.data());
return (!strncmp(data, "<superblock", 11) || !strncmp(data, "<?xml", 5)
|| !strncmp(data, "<!DOCTYPE", 9));
@ -39,22 +39,22 @@ persistent_data::get_nr_metadata_blocks(std::string const &path)
return get_nr_blocks(path, MD_BLOCK_SIZE);
}
persistent_data::block_manager<>::ptr
persistent_data::open_bm(std::string const &dev_path, block_manager<>::mode m, bool excl)
persistent_data::block_manager::ptr
persistent_data::open_bm(std::string const &dev_path, block_manager::mode m, bool excl)
{
block_address nr_blocks = get_nr_metadata_blocks(dev_path);
return block_manager<>::ptr(new block_manager<>(dev_path, nr_blocks, 1, m, excl));
return block_manager::ptr(new block_manager(dev_path, nr_blocks, 1, m, excl));
}
block_manager<>::ptr
block_manager::ptr
persistent_data::open_bm(std::string const &path) {
block_address nr_blocks = get_nr_metadata_blocks(path);
block_manager<>::mode m = block_manager<>::READ_ONLY;
return block_manager<>::ptr(new block_manager<>(path, nr_blocks, 1, m));
block_manager::mode m = block_manager::READ_ONLY;
return block_manager::ptr(new block_manager(path, nr_blocks, 1, m));
}
transaction_manager::ptr
persistent_data::open_tm(block_manager<>::ptr bm, block_address superblock_location) {
persistent_data::open_tm(block_manager::ptr bm, block_address superblock_location) {
auto nr_blocks = bm->get_nr_blocks();
if (!nr_blocks)
throw runtime_error("Metadata is not large enough for superblock.");

View File

@ -10,15 +10,15 @@
// FIXME: move to a different unit
namespace persistent_data {
bool check_for_xml(block_manager<>::ptr bm);
bool check_for_xml(block_manager::ptr bm);
persistent_data::block_address get_nr_blocks(std::string const &path, sector_t block_size = MD_BLOCK_SIZE);
block_address get_nr_metadata_blocks(std::string const &path);
block_manager<>::ptr open_bm(std::string const &dev_path,
block_manager<>::mode m, bool excl = true);
block_manager::ptr open_bm(std::string const &dev_path,
block_manager::mode m, bool excl = true);
block_manager<>::ptr open_bm(std::string const &path);
transaction_manager::ptr open_tm(block_manager<>::ptr bm,
block_manager::ptr open_bm(std::string const &path);
transaction_manager::ptr open_tm(block_manager::ptr bm,
block_address superblock_location);
}

View File

@ -718,7 +718,7 @@ namespace {
metadata_index_store(transaction_manager &tm)
: tm_(tm) {
block_manager<>::write_ref wr = tm_.new_block(index_validator());
block_manager::write_ref wr = tm_.new_block(index_validator());
bitmap_root_ = wr.get_location();
}
@ -753,7 +753,7 @@ namespace {
}
virtual void commit_ies() {
std::pair<block_manager<>::write_ref, bool> p =
std::pair<block_manager::write_ref, bool> p =
tm_.shadow(bitmap_root_, index_validator());
bitmap_root_ = p.first.get_location();
@ -787,7 +787,7 @@ namespace {
private:
void load_ies() {
block_manager<>::read_ref rr =
block_manager::read_ref rr =
tm_.read_lock(bitmap_root_, index_validator());
metadata_index const *mdi = reinterpret_cast<metadata_index const *>(rr.data());

View File

@ -26,7 +26,7 @@ using namespace std;
//----------------------------------------------------------------
transaction_manager::transaction_manager(block_manager<>::ptr bm,
transaction_manager::transaction_manager(block_manager::ptr bm,
space_map::ptr sm)
: bm_(bm),
sm_(sm)

View File

@ -31,15 +31,15 @@ namespace persistent_data {
class transaction_manager : boost::noncopyable {
public:
typedef boost::shared_ptr<transaction_manager> ptr;
typedef block_manager<>::read_ref read_ref;
typedef block_manager<>::write_ref write_ref;
typedef block_manager::read_ref read_ref;
typedef block_manager::write_ref write_ref;
typedef bcache::validator::ptr validator;
// If the space map is persistent, then the caller should
// hold onto a reference and remember to call sm_->commit()
// and update the superblock before dropping the superblock
// reference.
transaction_manager(block_manager<>::ptr bm,
transaction_manager(block_manager::ptr bm,
space_map::ptr sm);
~transaction_manager();
@ -62,7 +62,7 @@ namespace persistent_data {
sm_ = sm;
}
block_manager<>::ptr get_bm() {
block_manager::ptr get_bm() {
return bm_;
}
@ -76,7 +76,7 @@ namespace persistent_data {
bool is_shadow(block_address b) const;
void wipe_shadow_table();
block_manager<>::ptr bm_;
block_manager::ptr bm_;
space_map::ptr sm_;
std::set<block_address> shadows_;