thin-provisioning-tools/thin-provisioning/restore_emitter.cc

165 lines
4.8 KiB
C++
Raw Normal View History

2011-12-16 00:04:31 +05:30
// Copyright (C) 2011 Red Hat, Inc. All rights reserved.
2011-12-06 19:23:05 +05:30
//
2011-12-06 19:13:56 +05:30
// 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 "thin-provisioning/restore_emitter.h"
#include "thin-provisioning/superblock.h"
2011-10-28 18:31:44 +05:30
using namespace boost;
using namespace std;
using namespace thin_provisioning;
//----------------------------------------------------------------
namespace {
using namespace superblock_detail;
2011-10-28 18:31:44 +05:30
class restorer : public emitter {
public:
restorer(metadata::ptr md)
: md_(md),
in_superblock_(false),
empty_mapping_(new_mapping_tree()) {
2011-10-28 18:31:44 +05:30
}
virtual ~restorer() {
// FIXME: replace with a call to empty_mapping_->destroy()
md_->metadata_sm_->dec(empty_mapping_->get_root());
2011-10-28 18:31:44 +05:30
}
virtual void begin_superblock(std::string const &uuid,
uint64_t time,
uint64_t trans_id,
uint32_t data_block_size,
uint64_t nr_data_blocks,
optional<uint64_t> metadata_snap) {
2011-10-28 18:31:44 +05:30
in_superblock_ = true;
nr_data_blocks_ = nr_data_blocks;
2011-10-28 18:31:44 +05:30
superblock &sb = md_->sb_;
memcpy(&sb.uuid_, &uuid, sizeof(&sb.uuid_));
sb.time_ = time;
sb.trans_id_ = trans_id;
sb.data_block_size_ = data_block_size;
sb.metadata_snap_ = metadata_snap ? *metadata_snap : 0;
md_->data_sm_->extend(nr_data_blocks);
2011-10-28 18:31:44 +05:30
}
virtual void end_superblock() {
if (!in_superblock_)
throw runtime_error("missing superblock");
md_->commit();
in_superblock_ = false;
}
virtual void begin_device(uint32_t dev,
uint64_t mapped_blocks,
uint64_t trans_id,
uint64_t creation_time,
uint64_t snap_time) {
if (!in_superblock_)
throw runtime_error("missing superblock");
if (device_exists(dev))
throw std::runtime_error("Device already exists");
// Add entry to the details tree
uint64_t key[1] = {dev};
device_tree_detail::device_details details = {mapped_blocks, trans_id, (uint32_t)creation_time, (uint32_t)snap_time};
md_->details_->insert(key, details);
2011-10-28 18:31:44 +05:30
current_mapping_ = empty_mapping_->clone();
2011-10-28 18:31:44 +05:30
current_device_ = optional<uint32_t>(dev);
}
virtual void end_device() {
uint64_t key[1] = {*current_device_};
md_->mappings_top_level_->insert(key, current_mapping_->get_root());
md_->mappings_->set_root(md_->mappings_top_level_->get_root()); // FIXME: ugly
2011-10-28 18:31:44 +05:30
current_device_ = optional<uint32_t>();
}
virtual void begin_named_mapping(std::string const &name) {
throw runtime_error("not implemented");
}
virtual void end_named_mapping() {
throw runtime_error("not implemented");
}
virtual void identifier(std::string const &name) {
throw runtime_error("not implemented");
}
virtual void range_map(uint64_t origin_begin, uint64_t data_begin, uint32_t time, uint64_t len) {
for (uint64_t i = 0; i < len; i++)
single_map(origin_begin++, data_begin++, time);
}
virtual void single_map(uint64_t origin_block, uint64_t data_block, uint32_t time) {
if (!current_device_)
throw runtime_error("not in device");
if (data_block >= nr_data_blocks_) {
std::ostringstream out;
out << "mapping beyond end of data device (" << data_block
<< " >= " << nr_data_blocks_ << ")";
throw std::runtime_error(out.str());
}
uint64_t key[1] = {origin_block};
2013-05-20 22:07:46 +05:30
mapping_tree_detail::block_time bt;
2011-10-28 18:31:44 +05:30
bt.block_ = data_block;
bt.time_ = time;
current_mapping_->insert(key, bt);
md_->data_sm_->inc(data_block);
2011-10-28 18:31:44 +05:30
}
private:
single_mapping_tree::ptr new_mapping_tree() {
return single_mapping_tree::ptr(
new single_mapping_tree(md_->tm_,
2013-05-20 22:07:46 +05:30
mapping_tree_detail::block_time_ref_counter(md_->data_sm_)));
}
bool device_exists(thin_dev_t dev) const {
2011-10-28 18:31:44 +05:30
uint64_t key[1] = {dev};
2013-05-20 20:39:13 +05:30
device_tree::maybe_value v = md_->details_->lookup(key);
return v;
2011-10-28 18:31:44 +05:30
}
metadata::ptr md_;
bool in_superblock_;
block_address nr_data_blocks_;
2011-10-28 18:31:44 +05:30
optional<uint32_t> current_device_;
single_mapping_tree::ptr current_mapping_;
single_mapping_tree::ptr empty_mapping_;
2011-10-28 18:31:44 +05:30
};
}
//----------------------------------------------------------------
emitter::ptr
thin_provisioning::create_restore_emitter(metadata::ptr md)
{
return emitter::ptr(new restorer(md));
}
//----------------------------------------------------------------