thin-provisioning-tools/persistent-data/data-structures/array.h

442 lines
12 KiB
C
Raw Normal View History

2013-01-11 04:30:50 +05:30
// Copyright (C) 2012 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 ARRAY_H
#define ARRAY_H
2013-03-13 18:58:03 +05:30
#include "persistent-data/math_utils.h"
#include "persistent-data/data-structures/btree.h"
2013-12-11 22:58:14 +05:30
#include "persistent-data/data-structures/btree_counter.h"
#include "persistent-data/data-structures/btree_damage_visitor.h"
2013-03-13 18:58:03 +05:30
#include "persistent-data/data-structures/array_block.h"
2013-01-11 04:30:50 +05:30
//----------------------------------------------------------------
namespace persistent_data {
2013-03-13 18:58:03 +05:30
namespace array_detail {
uint32_t const ARRAY_CSUM_XOR = 595846735;
struct array_block_validator : public block_manager<>::validator {
virtual void check(buffer<> const &b, block_address location) const {
2014-07-25 15:05:04 +05:30
array_block_disk const *data = reinterpret_cast<array_block_disk const *>(b.raw());
2013-03-13 18:58:03 +05:30
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))
throw checksum_error("bad checksum in array block node");
if (to_cpu<uint64_t>(data->blocknr) != location)
throw checksum_error("bad block nr in array block");
}
virtual void prepare(buffer<> &b, block_address location) const {
2014-07-25 15:05:04 +05:30
array_block_disk *data = reinterpret_cast<array_block_disk *>(b.raw());
2013-03-22 19:12:20 +05:30
data->blocknr = to_disk<base::le64, uint64_t>(location);
2013-03-13 18:58:03 +05:30
crc32c sum(ARRAY_CSUM_XOR);
sum.append(&data->max_entries, MD_BLOCK_SIZE - sizeof(uint32_t));
2013-03-22 19:12:20 +05:30
data->csum = to_disk<base::le32>(sum.get_sum());
2013-03-13 18:58:03 +05:30
}
};
struct array_dim {
array_dim(unsigned nr_entries, unsigned entries_per_block)
: nr_full_blocks(nr_entries / entries_per_block),
2013-03-14 18:43:18 +05:30
nr_entries_in_last_block(nr_entries % entries_per_block),
nr_total_blocks(nr_full_blocks + (nr_entries_in_last_block ? 1 : 0)) {
2013-03-13 18:58:03 +05:30
}
unsigned nr_full_blocks;
unsigned nr_entries_in_last_block;
2013-03-14 18:43:18 +05:30
unsigned nr_total_blocks;
2013-03-13 18:58:03 +05:30
};
struct damage {
typedef boost::shared_ptr<damage> ptr;
damage(run<uint32_t> lost_keys,
std::string const &desc)
: lost_keys_(lost_keys),
desc_(desc) {
}
run<uint32_t> lost_keys_;
std::string desc_;
};
inline std::ostream &operator <<(std::ostream &out, damage const &d) {
out << "array damage[lost_keys = " << d.lost_keys_
<< ", \"" << d.desc_ << "\"]";
return out;
}
2013-03-13 18:58:03 +05:30
}
2013-01-11 04:30:50 +05:30
2013-09-24 16:30:09 +05:30
class array_base {
public:
virtual ~array_base() {}
virtual void set_root(block_address root) = 0;
virtual block_address get_root() const = 0;
};
2013-01-11 04:30:50 +05:30
template <typename ValueTraits>
2013-09-24 16:30:09 +05:30
class array : public array_base {
2013-01-11 04:30:50 +05:30
public:
2013-03-13 18:58:03 +05:30
class block_ref_counter : public ref_counter<uint64_t> {
public:
block_ref_counter(space_map::ptr sm,
array<ValueTraits> &a)
: sm_(sm),
a_(a) {
}
virtual void set(uint64_t b, uint32_t rc) {
sm_->set_count(b, rc);
if (rc == 0)
dec_values(b);
}
virtual void inc(uint64_t b) {
sm_->inc(b);
}
virtual void dec(uint64_t b) {
sm_->dec(b);
if (sm_->get_count(b) == 0)
dec_values(b);
}
private:
void dec_values(uint64_t b) {
a_.dec_ablock_entries(b);
}
space_map::ptr sm_;
array<ValueTraits> &a_;
};
friend class block_ref_counter;
struct block_traits {
2013-03-22 19:12:20 +05:30
typedef base::le64 disk_type;
2013-03-13 18:58:03 +05:30
typedef block_address value_type;
typedef block_ref_counter ref_counter;
static void unpack(disk_type const &disk, value_type &value) {
value = base::to_cpu<uint64_t>(disk);
}
static void pack(value_type const &value, disk_type &disk) {
2013-03-22 19:12:20 +05:30
disk = base::to_disk<base::le64>(value);
2013-03-13 18:58:03 +05:30
}
};
2013-01-11 04:30:50 +05:30
template <typename ValueVisitor>
struct block_value_visitor {
block_value_visitor(array<ValueTraits> const &a, ValueVisitor &vv)
: a_(a),
vv_(vv),
highest_index_() {
}
void visit(btree_path const &p,
typename block_traits::value_type const &block) {
highest_index_ = max<unsigned>(highest_index_,
a_.visit_array_block(vv_, p, block));
}
unsigned get_highest_seen() const {
return highest_index_;
}
private:
array<ValueTraits> const &a_;
ValueVisitor &vv_;
unsigned highest_index_;
};
// Returns the highest index visited
template <typename ValueVisitor>
unsigned visit_array_block(ValueVisitor &vv,
btree_path const &p,
typename block_traits::value_type const &v) const {
rblock rb(tm_->read_lock(v, validator_), rc_);
for (uint32_t i = 0; i < rb.nr_entries(); i++)
vv.visit(p[0] * rb.max_entries() + i, rb.get(i));
return p[0] * rb.max_entries() + (rb.nr_entries() - 1);
}
template <typename DamageVisitor>
struct block_damage_visitor {
block_damage_visitor(DamageVisitor &dv, unsigned entries_per_block)
: dv_(dv),
entries_per_block_(entries_per_block) {
}
void visit(btree_path const &path, btree_detail::damage const &d) {
dv_.visit(array_detail::damage(convert_run(d.lost_keys_), d.desc_));
}
private:
run<uint32_t>::maybe convert_maybe(run<uint64_t>::maybe const &v) const {
if (v)
return run<uint32_t>::maybe(*v * entries_per_block_);
return run<uint32_t>::maybe();
}
run<uint32_t> convert_run(run<uint64_t> const &v) const {
return run<uint32_t>(convert_maybe(v.begin_), convert_maybe(v.end_));
}
DamageVisitor &dv_;
unsigned entries_per_block_;
};
2013-03-13 18:58:03 +05:30
typedef typename persistent_data::transaction_manager::ptr tm_ptr;
2013-01-11 04:30:50 +05:30
typedef block_manager<>::write_ref write_ref;
2013-03-13 18:58:03 +05:30
typedef block_manager<>::read_ref read_ref;
2013-01-11 04:30:50 +05:30
2013-03-13 18:58:03 +05:30
typedef array_block<ValueTraits, block_manager<>::write_ref> wblock;
typedef array_block<ValueTraits, block_manager<>::read_ref> rblock;
2013-01-11 04:30:50 +05:30
2013-03-13 18:58:03 +05:30
typedef boost::shared_ptr<array<ValueTraits> > ptr;
typedef typename ValueTraits::value_type value_type;
2013-09-18 17:30:26 +05:30
typedef typename ValueTraits::ref_counter ref_counter;
2013-01-11 04:30:50 +05:30
2013-09-18 17:30:26 +05:30
array(tm_ptr tm, ref_counter rc)
2013-03-13 18:58:03 +05:30
: tm_(tm),
entries_per_block_(rblock::calc_max_entries()),
nr_entries_(0),
block_rc_(tm->get_sm(), *this),
block_tree_(tm, block_rc_),
rc_(rc),
2013-10-02 15:37:33 +05:30
validator_(new array_detail::array_block_validator) {
2013-03-13 18:58:03 +05:30
}
2013-01-11 04:30:50 +05:30
2013-09-18 17:30:26 +05:30
array(tm_ptr tm, ref_counter rc,
2013-03-14 18:43:18 +05:30
block_address root,
unsigned nr_entries)
2013-03-13 18:58:03 +05:30
: tm_(tm),
entries_per_block_(rblock::calc_max_entries()),
nr_entries_(nr_entries),
block_rc_(tm->get_sm(), *this),
block_tree_(tm, root, block_rc_),
rc_(rc),
2013-10-02 15:37:33 +05:30
validator_(new array_detail::array_block_validator) {
2013-03-13 18:58:03 +05:30
}
2013-01-11 04:30:50 +05:30
2013-03-13 18:58:03 +05:30
unsigned get_nr_entries() const {
return nr_entries_;
}
2013-01-11 04:30:50 +05:30
2013-03-13 18:58:03 +05:30
// FIXME: why is this needed?
void set_root(block_address root) {
block_tree_.set_root(root);
}
2013-01-11 04:30:50 +05:30
2013-03-13 18:58:03 +05:30
block_address get_root() const {
return block_tree_.get_root();
}
2013-01-11 04:30:50 +05:30
2013-03-13 18:58:03 +05:30
void destroy() {
block_tree_.destroy(); // FIXME: not implemented
}
2013-01-11 04:30:50 +05:30
2013-03-13 18:58:03 +05:30
void grow(unsigned new_nr_entries, value_type const &v) {
resizer r(*this, nr_entries_, new_nr_entries, entries_per_block_, v);
r.grow(new_nr_entries, v);
}
2013-01-11 04:30:50 +05:30
2013-03-13 18:58:03 +05:30
value_type get(unsigned index) const {
rblock b = get_ablock(index / entries_per_block_);
return b.get(index % entries_per_block_);
2013-01-11 04:30:50 +05:30
}
2013-03-13 18:58:03 +05:30
void set(unsigned index, value_type const &value) {
wblock b = shadow_ablock(index / entries_per_block_);
b.set(index % entries_per_block_, value);
2013-01-11 04:30:50 +05:30
}
template <typename ValueVisitor, typename DamageVisitor>
void visit_values(ValueVisitor &value_visitor,
DamageVisitor &damage_visitor) const {
block_value_visitor<ValueVisitor> bvisitor(*this, value_visitor);
block_damage_visitor<DamageVisitor> dvisitor(damage_visitor, entries_per_block_);
2013-12-11 22:58:14 +05:30
btree_visit_values(block_tree_, bvisitor, dvisitor);
// check that all blocks were seen
unsigned h = bvisitor.get_highest_seen();
if (h != nr_entries_ - 1) {
array_detail::damage d(run<unsigned>(h + 1, nr_entries_), "missing blocks");
damage_visitor.visit(d);
}
}
2013-12-11 22:58:14 +05:30
//--------------------------------
struct ablock_counter {
ablock_counter(block_counter &bc)
: bc_(bc) {
}
void visit(btree_detail::node_location const &loc, block_address b) {
bc_.inc(b);
}
private:
block_counter &bc_;
};
void count_metadata_blocks(block_counter &bc) const {
ablock_counter vc(bc);
count_btree_blocks(block_tree_, bc, vc);
}
2013-03-13 18:58:03 +05:30
private:
2013-01-11 04:30:50 +05:30
2013-03-13 18:58:03 +05:30
struct resizer {
resizer(array<ValueTraits> &a,
unsigned old_size,
unsigned new_size,
unsigned entries_per_block,
typename ValueTraits::value_type const &v)
: a_(a),
old_dim_(old_size, entries_per_block),
new_dim_(new_size, entries_per_block),
entries_per_block_(entries_per_block),
v_(v) {
}
void grow(unsigned new_nr_entries, value_type const &v) {
if (new_dim_.nr_full_blocks > old_dim_.nr_full_blocks)
grow_needs_more_blocks();
else if (old_dim_.nr_entries_in_last_block > 0)
grow_extend_tail_block(new_dim_.nr_entries_in_last_block);
2013-03-14 18:43:18 +05:30
else if (new_dim_.nr_entries_in_last_block)
2013-03-13 18:58:03 +05:30
grow_add_tail_block();
2013-03-14 18:43:18 +05:30
a_.nr_entries_ = new_nr_entries;
2013-03-13 18:58:03 +05:30
}
private:
void insert_full_ablocks(unsigned begin_index, unsigned end_index) {
while (begin_index != end_index) {
wblock b = a_.new_ablock(begin_index);
b.grow(entries_per_block_, v_);
begin_index++;
}
}
void grow_add_tail_block() {
wblock b = a_.new_ablock(new_dim_.nr_full_blocks);
b.grow(new_dim_.nr_entries_in_last_block, v_);
}
void grow_needs_more_blocks() {
if (old_dim_.nr_entries_in_last_block > 0)
grow_extend_tail_block(entries_per_block_);
2013-03-14 18:43:18 +05:30
insert_full_ablocks(old_dim_.nr_total_blocks, new_dim_.nr_full_blocks);
2013-03-13 18:58:03 +05:30
if (new_dim_.nr_entries_in_last_block > 0)
grow_add_tail_block();
}
void grow_extend_tail_block(unsigned new_nr_entries) {
2013-03-14 18:43:18 +05:30
uint64_t last_block = a_.nr_entries_ / entries_per_block_;
2013-03-13 18:58:03 +05:30
wblock b = a_.shadow_ablock(last_block);
2013-03-14 18:43:18 +05:30
b.grow(new_nr_entries, v_);
2013-03-13 18:58:03 +05:30
}
array<ValueTraits> &a_;
array_detail::array_dim old_dim_;
array_detail::array_dim new_dim_;
unsigned entries_per_block_;
typename ValueTraits::value_type const &v_;
};
friend class resizer;
//--------------------------------
block_address lookup_block_address(unsigned array_index) const {
uint64_t key[1] = {array_index};
boost::optional<uint64_t> addr = block_tree_.lookup(key);
if (!addr) {
std::ostringstream str;
str << "lookup of array block " << array_index << " failed";
throw runtime_error(str.str());
}
2013-01-11 04:30:50 +05:30
2013-03-13 18:58:03 +05:30
return *addr;
}
2013-01-11 04:30:50 +05:30
2013-03-13 18:58:03 +05:30
wblock new_ablock(unsigned ablock_index) {
uint64_t key[1] = {ablock_index};
write_ref b = tm_->new_block(validator_);
2013-03-13 18:58:03 +05:30
block_address location = b.get_location();
2013-01-11 04:30:50 +05:30
2013-03-13 18:58:03 +05:30
wblock wb(b, rc_);
wb.setup_empty();
block_tree_.insert(key, location);
return wblock(b, rc_);
}
2013-01-11 04:30:50 +05:30
2013-03-13 18:58:03 +05:30
rblock get_ablock(unsigned ablock_index) const {
block_address addr = lookup_block_address(ablock_index);
return rblock(tm_->read_lock(addr, validator_), rc_);
2013-03-13 18:58:03 +05:30
}
2013-01-11 04:30:50 +05:30
2013-03-13 18:58:03 +05:30
wblock shadow_ablock(unsigned ablock_index) {
uint64_t key[1] = {ablock_index};
block_address addr = lookup_block_address(ablock_index);
std::pair<write_ref, bool> p = tm_->shadow(addr, validator_);
2013-03-13 18:58:03 +05:30
wblock wb = wblock(p.first, rc_);
2013-01-11 04:30:50 +05:30
2013-03-13 18:58:03 +05:30
if (p.second)
wb.inc_all_entries();
2013-01-11 04:30:50 +05:30
2013-03-13 18:58:03 +05:30
block_tree_.insert(key, p.first.get_location());
2013-01-11 04:30:50 +05:30
2013-03-13 18:58:03 +05:30
return wb;
}
2013-01-11 04:30:50 +05:30
2013-03-13 18:58:03 +05:30
void dec_ablock_entries(block_address addr) {
rblock b(tm_->read_lock(addr, validator_), rc_);
2013-03-13 18:58:03 +05:30
b.dec_all_entries();
}
2013-01-11 04:30:50 +05:30
tm_ptr tm_;
2013-03-13 18:58:03 +05:30
unsigned entries_per_block_;
unsigned nr_entries_;
block_ref_counter block_rc_;
btree<1, block_traits> block_tree_;
2013-01-11 04:30:50 +05:30
typename ValueTraits::ref_counter rc_;
block_manager<>::validator::ptr validator_;
2013-01-11 04:30:50 +05:30
};
}
//----------------------------------------------------------------
#endif