2013-10-09 14:52:06 +05:30
|
|
|
#include "caching/metadata_dump.h"
|
|
|
|
|
2013-10-09 15:19:53 +05:30
|
|
|
#include <set>
|
|
|
|
|
2013-10-09 14:52:06 +05:30
|
|
|
using namespace std;
|
|
|
|
using namespace caching;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
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 cache_check for details).\n"
|
|
|
|
"perhaps you wanted to run with --repair");
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------
|
|
|
|
|
|
|
|
class mapping_emitter : public mapping_visitor {
|
|
|
|
public:
|
2013-10-09 15:19:53 +05:30
|
|
|
mapping_emitter(emitter::ptr e, set<block_address> &valid_blocks)
|
|
|
|
: e_(e),
|
|
|
|
valid_blocks_(valid_blocks) {
|
2013-10-09 14:52:06 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void visit(block_address cblock, mapping const &m) {
|
2013-10-09 15:19:53 +05:30
|
|
|
if (m.flags_ & M_VALID) {
|
2013-10-09 14:52:06 +05:30
|
|
|
e_->mapping(cblock, m.oblock_, m.flags_ & M_DIRTY);
|
2013-10-09 15:19:53 +05:30
|
|
|
mark_valid(cblock);
|
|
|
|
}
|
2013-10-09 14:52:06 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-10-09 15:19:53 +05:30
|
|
|
void mark_valid(block_address cblock) {
|
|
|
|
valid_blocks_.insert(cblock);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-09 14:52:06 +05:30
|
|
|
emitter::ptr e_;
|
2013-10-09 15:19:53 +05:30
|
|
|
set<block_address> &valid_blocks_;
|
2013-10-09 14:52:06 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
struct ignore_mapping_damage : public mapping_array_damage::damage_visitor {
|
|
|
|
virtual void visit(mapping_array_damage::missing_mappings const &d) {}
|
|
|
|
virtual void visit(mapping_array_damage::invalid_mapping const &d) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
class fatal_mapping_damage : public mapping_array_damage::damage_visitor {
|
|
|
|
public:
|
|
|
|
virtual void visit(mapping_array_damage::missing_mappings const &d) {
|
|
|
|
raise_metadata_damage();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void visit(mapping_array_damage::invalid_mapping const &d) {
|
|
|
|
raise_metadata_damage();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//--------------------------------
|
|
|
|
|
|
|
|
class hint_emitter : public hint_visitor {
|
|
|
|
public:
|
2013-10-10 15:57:34 +05:30
|
|
|
hint_emitter(emitter::ptr e, set<block_address> &valid_blocks)
|
2013-10-09 15:19:53 +05:30
|
|
|
: e_(e),
|
|
|
|
valid_blocks_(valid_blocks) {
|
2013-10-09 14:52:06 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
virtual void visit(block_address cblock, std::vector<unsigned char> const &data) {
|
2013-10-09 15:19:53 +05:30
|
|
|
if (valid(cblock))
|
|
|
|
e_->hint(cblock, data);
|
2013-10-09 14:52:06 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-10-09 15:19:53 +05:30
|
|
|
bool valid(block_address cblock) const {
|
|
|
|
return valid_blocks_.find(cblock) != valid_blocks_.end();
|
|
|
|
}
|
|
|
|
|
2013-10-09 14:52:06 +05:30
|
|
|
emitter::ptr e_;
|
2013-10-09 15:19:53 +05:30
|
|
|
set<block_address> &valid_blocks_;
|
2013-10-09 14:52:06 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
struct ignore_hint_damage : public hint_array_damage::damage_visitor {
|
|
|
|
virtual void visit(hint_array_damage::missing_hints const &d) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
class fatal_hint_damage : public hint_array_damage::damage_visitor {
|
|
|
|
virtual void visit(hint_array_damage::missing_hints const &d) {
|
|
|
|
raise_metadata_damage();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
void
|
|
|
|
caching::metadata_dump(metadata::ptr md, emitter::ptr e, bool repair)
|
|
|
|
{
|
2013-10-09 15:19:53 +05:30
|
|
|
set<block_address> valid_blocks;
|
|
|
|
|
2013-10-09 14:52:06 +05:30
|
|
|
superblock const &sb = md->sb_;
|
|
|
|
e->begin_superblock(to_string(sb.uuid), sb.data_block_size,
|
|
|
|
sb.cache_blocks, to_string(sb.policy_name),
|
|
|
|
sb.policy_hint_size);
|
|
|
|
|
|
|
|
e->begin_mappings();
|
|
|
|
{
|
|
|
|
namespace mad = mapping_array_damage;
|
|
|
|
|
2013-10-09 15:19:53 +05:30
|
|
|
mapping_emitter me(e, valid_blocks);
|
2013-10-09 14:52:06 +05:30
|
|
|
ignore_mapping_damage ignore;
|
|
|
|
fatal_mapping_damage fatal;
|
|
|
|
mad::damage_visitor &dv = repair ?
|
|
|
|
static_cast<mad::damage_visitor &>(ignore) :
|
|
|
|
static_cast<mad::damage_visitor &>(fatal);
|
|
|
|
walk_mapping_array(*md->mappings_, me, dv);
|
|
|
|
}
|
|
|
|
e->end_mappings();
|
|
|
|
|
|
|
|
// walk hints
|
|
|
|
e->begin_hints();
|
2013-12-09 16:40:49 +05:30
|
|
|
if (md->hints_) {
|
2013-10-09 14:52:06 +05:30
|
|
|
using namespace hint_array_damage;
|
|
|
|
|
2013-10-09 15:19:53 +05:30
|
|
|
hint_emitter he(e, valid_blocks);
|
2013-10-09 14:52:06 +05:30
|
|
|
ignore_hint_damage ignore;
|
|
|
|
fatal_hint_damage fatal;
|
|
|
|
damage_visitor &dv = repair ?
|
|
|
|
static_cast<damage_visitor &>(ignore) :
|
|
|
|
static_cast<damage_visitor &>(fatal);
|
|
|
|
md->hints_->walk(he, dv);
|
|
|
|
}
|
|
|
|
e->end_hints();
|
|
|
|
|
|
|
|
// FIXME: walk discards
|
|
|
|
|
|
|
|
e->end_superblock();
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|