thin-provisioning-tools/caching/restore_emitter.cc

100 lines
2.2 KiB
C++
Raw Normal View History

2013-09-12 17:03:32 +05:30
#include "caching/restore_emitter.h"
#include "caching/superblock.h"
#include "caching/mapping_array.h"
2013-09-12 17:03:32 +05:30
using namespace caching;
using namespace mapping_array_detail;
using namespace std;
using namespace superblock_damage;
2013-09-12 17:03:32 +05:30
//----------------------------------------------------------------
namespace {
class restorer : public emitter {
public:
restorer(metadata::ptr md)
2013-09-18 17:30:26 +05:30
: in_superblock_(false),
md_(md) {
}
virtual ~restorer() {
2013-09-12 17:03:32 +05:30
}
virtual void begin_superblock(std::string const &uuid,
pd::block_address block_size,
pd::block_address nr_cache_blocks,
std::string const &policy,
size_t hint_width) {
2013-09-18 17:30:26 +05:30
superblock &sb = md_->sb_;
strncpy((char *) sb.policy_name, policy.c_str(), sizeof(sb.policy_name));
memset(sb.policy_version, 0, sizeof(sb.policy_version)); // FIXME: should come from xml
sb.policy_hint_size = hint_width;
md_->setup_hint_array(hint_width);
2013-09-18 17:30:26 +05:30
sb.data_block_size = block_size;
sb.cache_blocks = nr_cache_blocks;
struct mapping unmapped_value;
unmapped_value.oblock_ = 0;
unmapped_value.flags_ = 0;
md_->mappings_->grow(nr_cache_blocks, unmapped_value);
vector<unsigned char> hint_value(hint_width, '\0');
md_->hints_->grow(nr_cache_blocks, hint_value);
2013-09-12 17:03:32 +05:30
}
virtual void end_superblock() {
2013-09-18 17:30:26 +05:30
md_->commit();
2013-09-12 17:03:32 +05:30
}
virtual void begin_mappings() {
2013-09-18 17:30:26 +05:30
// noop
2013-09-12 17:03:32 +05:30
}
virtual void end_mappings() {
2013-09-18 17:30:26 +05:30
// noop
2013-09-12 17:03:32 +05:30
}
virtual void mapping(pd::block_address cblock,
pd::block_address oblock,
bool dirty) {
2013-09-18 17:30:26 +05:30
mapping_array_detail::mapping m;
m.oblock_ = oblock;
m.flags_ = M_VALID;
if (dirty)
m.flags_ = m.flags_ | M_DIRTY;
2013-09-18 17:30:26 +05:30
md_->mappings_->set(cblock, m);
2013-09-12 17:03:32 +05:30
}
virtual void begin_hints() {
// noop
2013-09-12 17:03:32 +05:30
}
virtual void end_hints() {
// noop
2013-09-12 17:03:32 +05:30
}
virtual void hint(pd::block_address cblock,
vector<unsigned char> const &data) {
2013-09-12 17:03:32 +05:30
}
private:
2013-09-18 17:30:26 +05:30
bool in_superblock_;
2013-09-12 17:03:32 +05:30
metadata::ptr md_;
};
}
//----------------------------------------------------------------
emitter::ptr
caching::create_restore_emitter(metadata::ptr md)
{
return emitter::ptr(new restorer(md));
}
//----------------------------------------------------------------