thin-provisioning-tools/era/metadata_dump.cc

213 lines
5.3 KiB
C++
Raw Normal View History

2014-01-23 06:16:03 +05:30
#include "era/metadata_dump.h"
#include "era/era_array.h"
using namespace era;
using namespace std;
//----------------------------------------------------------------
namespace {
string to_string(unsigned char const *data) {
// FIXME: we're assuming the data is zero terminated here
return std::string(reinterpret_cast<char const *>(data));
}
void raise_metadata_damage() {
throw std::runtime_error("metadata contains errors (run era_check for details).\n"
"perhaps you wanted to run with --repair ?");
}
class writeset_tree_emitter : public writeset_tree_detail::writeset_visitor {
2014-01-23 06:16:03 +05:30
public:
writeset_tree_emitter(emitter::ptr e)
2014-01-23 06:16:03 +05:30
: e_(e) {
}
virtual void writeset_begin(uint32_t era, uint32_t nr_bits) {
e_->begin_writeset(era, nr_bits);
2014-01-23 06:16:03 +05:30
}
virtual void bit(uint32_t bit, bool value) {
e_->writeset_bit(bit, value);
2014-01-23 06:16:03 +05:30
}
virtual void writeset_end() {
e_->end_writeset();
2014-01-23 06:16:03 +05:30
}
private:
emitter::ptr e_;
};
2014-02-14 20:05:25 +05:30
class writeset_tree_collator : public writeset_tree_detail::writeset_visitor {
public:
writeset_tree_collator(map<uint32_t, uint32_t> &exceptions)
: exceptions_(exceptions),
current_era_(0) {
}
virtual void writeset_begin(uint32_t era, uint32_t nr_bits) {
current_era_ = era;
}
virtual void bit(uint32_t bit, bool value) {
if (value) {
map<uint32_t, uint32_t>::const_iterator it = exceptions_.find(bit);
if (it == exceptions_.end() || it->second < current_era_)
exceptions_.insert(make_pair(bit, current_era_));
}
}
virtual void writeset_end() {
}
private:
map<uint32_t, uint32_t> &exceptions_;
uint32_t current_era_;
};
struct ignore_writeset_tree_damage : public writeset_tree_detail::damage_visitor {
void visit(writeset_tree_detail::missing_eras const &d) {
2014-01-23 06:16:03 +05:30
}
void visit(writeset_tree_detail::damaged_writeset const &d) {
2014-01-23 06:16:03 +05:30
}
};
struct fatal_writeset_tree_damage : public writeset_tree_detail::damage_visitor {
void visit(writeset_tree_detail::missing_eras const &d) {
2014-01-23 06:16:03 +05:30
raise_metadata_damage();
}
void visit(writeset_tree_detail::damaged_writeset const &d) {
2014-01-23 06:16:03 +05:30
raise_metadata_damage();
}
};
//--------------------------------
class era_array_emitter : public era_array_visitor {
public:
2014-02-14 20:05:25 +05:30
era_array_emitter(emitter::ptr e, map<uint32_t, uint32_t> const &exceptions)
: e_(e),
exceptions_(exceptions) {
2014-01-23 06:16:03 +05:30
}
virtual void visit(uint32_t index, uint32_t era) {
2014-02-14 20:05:25 +05:30
map<uint32_t, uint32_t>::const_iterator it = exceptions_.find(index);
if (it != exceptions_.end() && it->second > era)
e_->era(index, it->second);
else
e_->era(index, era);
2014-01-23 06:16:03 +05:30
}
private:
emitter::ptr e_;
2014-02-14 20:05:25 +05:30
map<uint32_t, uint32_t> exceptions_;
2014-01-23 06:16:03 +05:30
};
struct ignore_era_array_damage : public era_array_detail::damage_visitor {
void visit(era_array_detail::missing_eras const &d) {
}
void visit(era_array_detail::invalid_era const &d) {
}
};
class fatal_era_array_damage : public era_array_detail::damage_visitor {
void visit(era_array_detail::missing_eras const &d) {
raise_metadata_damage();
}
void visit(era_array_detail::invalid_era const &d) {
raise_metadata_damage();
}
};
2014-02-14 20:05:25 +05:30
void
dump(metadata::ptr md, emitter::ptr e, bool repair)
{
{
writeset_tree_emitter visitor(e);
2014-01-23 06:16:03 +05:30
2014-02-14 20:05:25 +05:30
ignore_writeset_tree_damage ignore;
fatal_writeset_tree_damage fatal;
writeset_tree_detail::damage_visitor &dv = repair ?
static_cast<writeset_tree_detail::damage_visitor &>(ignore) :
static_cast<writeset_tree_detail::damage_visitor &>(fatal);
2014-01-23 06:16:03 +05:30
2014-02-14 20:05:25 +05:30
walk_writeset_tree(md->tm_, *md->writeset_tree_, visitor, dv);
}
e->begin_era_array();
{
map<uint32_t, uint32_t> exceptions;
era_array_emitter visitor(e, exceptions);
ignore_era_array_damage ignore;
fatal_era_array_damage fatal;
era_array_detail::damage_visitor &dv = repair ?
static_cast<era_array_detail::damage_visitor &>(ignore) :
static_cast<era_array_detail::damage_visitor &>(fatal);
walk_era_array(*md->era_array_, visitor, dv);
}
e->end_era_array();
}
void dump_logical(metadata::ptr md, emitter::ptr e, bool repair)
2014-01-23 06:16:03 +05:30
{
2014-02-14 20:05:25 +05:30
// This will potentially use a lot of memory, but I don't
// see a way around it.
map<uint32_t, uint32_t> exceptions;
2014-01-23 06:16:03 +05:30
{
2014-02-14 20:05:25 +05:30
writeset_tree_collator visitor(exceptions);
2014-01-23 06:16:03 +05:30
ignore_writeset_tree_damage ignore;
fatal_writeset_tree_damage fatal;
writeset_tree_detail::damage_visitor &dv = repair ?
static_cast<writeset_tree_detail::damage_visitor &>(ignore) :
static_cast<writeset_tree_detail::damage_visitor &>(fatal);
2014-01-23 06:16:03 +05:30
walk_writeset_tree(md->tm_, *md->writeset_tree_, visitor, dv);
2014-01-23 06:16:03 +05:30
}
e->begin_era_array();
{
2014-02-14 20:05:25 +05:30
era_array_emitter visitor(e, exceptions);
2014-01-23 06:16:03 +05:30
ignore_era_array_damage ignore;
fatal_era_array_damage fatal;
era_array_detail::damage_visitor &dv = repair ?
static_cast<era_array_detail::damage_visitor &>(ignore) :
static_cast<era_array_detail::damage_visitor &>(fatal);
walk_era_array(*md->era_array_, visitor, dv);
}
e->end_era_array();
}
2014-02-14 20:05:25 +05:30
}
//----------------------------------------------------------------
void
era::metadata_dump(metadata::ptr md, emitter::ptr e,
bool repair, bool logical)
{
superblock const &sb = md->sb_;
e->begin_superblock(to_string(sb.uuid), sb.data_block_size,
sb.nr_blocks,
sb.current_era);
{
if (logical)
dump_logical(md, e, repair);
else
dump(md, e, repair);
}
2014-01-23 06:16:03 +05:30
e->end_superblock();
}
//----------------------------------------------------------------