2011-10-28 16:51:58 +05:30
|
|
|
#include "thin_pool.h"
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-08-31 18:08:22 +05:30
|
|
|
#include "btree_checker.h"
|
2011-08-23 16:25:37 +05:30
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
#include <stdexcept>
|
2011-08-23 16:25:37 +05:30
|
|
|
#include <sstream>
|
|
|
|
#include <iostream>
|
2011-08-24 15:15:39 +05:30
|
|
|
#include <set>
|
|
|
|
#include <map>
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-09-02 19:52:46 +05:30
|
|
|
using namespace base;
|
2011-08-23 16:25:37 +05:30
|
|
|
using namespace std;
|
2011-06-23 19:17:08 +05:30
|
|
|
using namespace persistent_data;
|
2011-07-22 20:39:56 +05:30
|
|
|
using namespace thin_provisioning;
|
2011-06-23 19:17:08 +05:30
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2011-10-28 16:51:58 +05:30
|
|
|
thin::thin(thin_dev_t dev, thin_pool *pool)
|
2011-07-22 20:39:56 +05:30
|
|
|
: dev_(dev),
|
2011-10-28 16:51:58 +05:30
|
|
|
pool_(pool)
|
2011-07-22 20:39:56 +05:30
|
|
|
{
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
2011-07-22 20:39:56 +05:30
|
|
|
thin_dev_t
|
|
|
|
thin::get_dev_t() const
|
|
|
|
{
|
|
|
|
return dev_;
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-07-22 20:39:56 +05:30
|
|
|
thin::maybe_address
|
|
|
|
thin::lookup(block_address thin_block)
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
|
|
|
uint64_t key[2] = {dev_, thin_block};
|
2011-10-28 16:51:58 +05:30
|
|
|
return pool_->md_->mappings_.lookup(key);
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-07-22 20:39:56 +05:30
|
|
|
thin::insert(block_address thin_block, block_address data_block)
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
|
|
|
uint64_t key[2] = {dev_, thin_block};
|
2011-08-24 15:15:39 +05:30
|
|
|
block_time bt;
|
|
|
|
bt.block_ = data_block;
|
|
|
|
bt.time_ = 0; // FIXME: use current time.
|
2011-10-28 16:51:58 +05:30
|
|
|
return pool_->md_->mappings_.insert(key, bt);
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-07-22 20:39:56 +05:30
|
|
|
thin::remove(block_address thin_block)
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
|
|
|
uint64_t key[2] = {dev_, thin_block};
|
2011-10-28 16:51:58 +05:30
|
|
|
pool_->md_->mappings_.remove(key);
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
2011-07-22 20:39:56 +05:30
|
|
|
|
|
|
|
void
|
|
|
|
thin::set_snapshot_time(uint32_t time)
|
|
|
|
{
|
|
|
|
uint64_t key[1] = { dev_ };
|
2011-10-28 16:51:58 +05:30
|
|
|
optional<device_details> mdetail = pool_->md_->details_.lookup(key);
|
2011-07-22 20:39:56 +05:30
|
|
|
if (!mdetail)
|
|
|
|
throw runtime_error("no such device");
|
|
|
|
|
|
|
|
mdetail->snapshotted_time_ = time;
|
2011-10-28 16:51:58 +05:30
|
|
|
pool_->md_->details_.insert(key, *mdetail);
|
2011-07-22 20:39:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
block_address
|
|
|
|
thin::get_mapped_blocks() const
|
|
|
|
{
|
|
|
|
uint64_t key[1] = { dev_ };
|
2011-10-28 16:51:58 +05:30
|
|
|
optional<device_details> mdetail = pool_->md_->details_.lookup(key);
|
2011-07-22 20:39:56 +05:30
|
|
|
if (!mdetail)
|
|
|
|
throw runtime_error("no such device");
|
|
|
|
|
|
|
|
return mdetail->mapped_blocks_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
thin::set_mapped_blocks(block_address count)
|
|
|
|
{
|
|
|
|
uint64_t key[1] = { dev_ };
|
2011-10-28 16:51:58 +05:30
|
|
|
optional<device_details> mdetail = pool_->md_->details_.lookup(key);
|
2011-07-22 20:39:56 +05:30
|
|
|
if (!mdetail)
|
|
|
|
throw runtime_error("no such device");
|
|
|
|
|
|
|
|
mdetail->mapped_blocks_ = count;
|
2011-10-28 16:51:58 +05:30
|
|
|
pool_->md_->details_.insert(key, *mdetail);
|
2011-07-22 20:39:56 +05:30
|
|
|
}
|
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
//--------------------------------
|
|
|
|
|
2011-10-28 16:55:06 +05:30
|
|
|
thin_pool::thin_pool(metadata::ptr md)
|
2011-10-28 16:01:00 +05:30
|
|
|
: md_(md)
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-10-28 16:51:58 +05:30
|
|
|
thin_pool::~thin_pool()
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-10-28 16:51:58 +05:30
|
|
|
thin_pool::create_thin(thin_dev_t dev)
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
|
|
|
uint64_t key[1] = {dev};
|
|
|
|
|
|
|
|
if (device_exists(dev))
|
|
|
|
throw std::runtime_error("Device already exists");
|
|
|
|
|
2011-10-28 16:01:00 +05:30
|
|
|
single_mapping_tree::ptr new_tree(new single_mapping_tree(md_->tm_, block_time_ref_counter(md_->data_sm_)));
|
|
|
|
md_->mappings_top_level_.insert(key, new_tree->get_root());
|
|
|
|
md_->mappings_.set_root(md_->mappings_top_level_.get_root()); // FIXME: ugly
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-10-28 16:51:58 +05:30
|
|
|
thin_pool::create_snap(thin_dev_t dev, thin_dev_t origin)
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
|
|
|
uint64_t snap_key[1] = {dev};
|
|
|
|
uint64_t origin_key[1] = {origin};
|
|
|
|
|
2011-10-28 16:01:00 +05:30
|
|
|
optional<uint64_t> mtree_root = md_->mappings_top_level_.lookup(origin_key);
|
2011-06-27 15:15:30 +05:30
|
|
|
if (!mtree_root)
|
2011-06-23 19:17:08 +05:30
|
|
|
throw std::runtime_error("unknown origin");
|
|
|
|
|
2011-10-28 16:01:00 +05:30
|
|
|
single_mapping_tree otree(md_->tm_, *mtree_root,
|
|
|
|
block_time_ref_counter(md_->data_sm_));
|
2011-06-27 15:15:30 +05:30
|
|
|
|
|
|
|
single_mapping_tree::ptr clone(otree.clone());
|
2011-10-28 16:01:00 +05:30
|
|
|
md_->mappings_top_level_.insert(snap_key, clone->get_root());
|
|
|
|
md_->mappings_.set_root(md_->mappings_top_level_.get_root()); // FIXME: ugly
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-10-28 16:01:00 +05:30
|
|
|
md_->sb_.time_++;
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-08-24 18:57:45 +05:30
|
|
|
thin::ptr o = open_thin(origin);
|
|
|
|
thin::ptr s = open_thin(dev);
|
2011-10-28 16:01:00 +05:30
|
|
|
o->set_snapshot_time(md_->sb_.time_);
|
|
|
|
s->set_snapshot_time(md_->sb_.time_);
|
2011-06-23 19:17:08 +05:30
|
|
|
s->set_mapped_blocks(o->get_mapped_blocks());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-10-28 16:51:58 +05:30
|
|
|
thin_pool::del(thin_dev_t dev)
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
|
|
|
uint64_t key[1] = {dev};
|
2011-10-28 16:01:00 +05:30
|
|
|
md_->mappings_top_level_.remove(key);
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-10-28 16:51:58 +05:30
|
|
|
thin_pool::set_transaction_id(uint64_t id)
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
2011-10-28 16:01:00 +05:30
|
|
|
md_->sb_.trans_id_ = id;
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
2011-10-28 16:51:58 +05:30
|
|
|
thin_pool::get_transaction_id() const
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
2011-10-28 16:01:00 +05:30
|
|
|
return md_->sb_.trans_id_;
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
block_address
|
2011-10-28 16:51:58 +05:30
|
|
|
thin_pool::get_held_root() const
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
2011-10-28 16:01:00 +05:30
|
|
|
return md_->sb_.held_root_;
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
block_address
|
2011-10-28 16:51:58 +05:30
|
|
|
thin_pool::alloc_data_block()
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
2011-10-28 16:01:00 +05:30
|
|
|
return md_->data_sm_->new_block();
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-10-28 16:51:58 +05:30
|
|
|
thin_pool::free_data_block(block_address b)
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
2011-10-28 16:01:00 +05:30
|
|
|
md_->data_sm_->dec(b);
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
block_address
|
2011-10-28 16:51:58 +05:30
|
|
|
thin_pool::get_nr_free_data_blocks() const
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
2011-10-28 16:01:00 +05:30
|
|
|
return md_->data_sm_->get_nr_free();
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
sector_t
|
2011-10-28 16:51:58 +05:30
|
|
|
thin_pool::get_data_block_size() const
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
2011-10-28 16:01:00 +05:30
|
|
|
return md_->sb_.data_block_size_;
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
block_address
|
2011-10-28 16:51:58 +05:30
|
|
|
thin_pool::get_data_dev_size() const
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
2011-10-28 16:01:00 +05:30
|
|
|
return md_->data_sm_->get_nr_blocks();
|
2011-07-22 20:39:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
thin::ptr
|
2011-10-28 16:51:58 +05:30
|
|
|
thin_pool::open_thin(thin_dev_t dev)
|
2011-07-22 20:39:56 +05:30
|
|
|
{
|
|
|
|
uint64_t key[1] = {dev};
|
2011-10-28 16:01:00 +05:30
|
|
|
optional<device_details> mdetails = md_->details_.lookup(key);
|
2011-07-22 20:39:56 +05:30
|
|
|
if (!mdetails)
|
|
|
|
throw runtime_error("no such device");
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-07-22 20:39:56 +05:30
|
|
|
thin *ptr = new thin(dev, this);
|
|
|
|
thin::ptr r(ptr);
|
|
|
|
return r;
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
2011-07-22 20:39:56 +05:30
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
bool
|
2011-10-28 16:51:58 +05:30
|
|
|
thin_pool::device_exists(thin_dev_t dev) const
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
|
|
|
uint64_t key[1] = {dev};
|
2011-10-28 16:01:00 +05:30
|
|
|
return md_->details_.lookup(key);
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|