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/>.
|
|
|
|
|
2013-05-20 20:39:13 +05:30
|
|
|
#include "thin-provisioning/device_tree.h"
|
2013-04-23 19:51:44 +05:30
|
|
|
#include "thin-provisioning/file_utils.h"
|
2013-01-02 18:25:41 +05:30
|
|
|
#include "thin-provisioning/metadata.h"
|
2011-10-28 16:01:00 +05:30
|
|
|
|
2013-01-02 18:25:41 +05:30
|
|
|
#include "persistent-data/math_utils.h"
|
2013-01-11 03:06:38 +05:30
|
|
|
#include "persistent-data/space-maps/core.h"
|
|
|
|
#include "persistent-data/space-maps/disk.h"
|
2011-10-28 16:01:00 +05:30
|
|
|
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2013-04-23 19:51:44 +05:30
|
|
|
using namespace base;
|
2011-10-28 16:01:00 +05:30
|
|
|
using namespace thin_provisioning;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
2013-06-19 16:04:01 +05:30
|
|
|
|
2011-10-28 16:01:00 +05:30
|
|
|
namespace {
|
2013-05-21 17:16:07 +05:30
|
|
|
using namespace superblock_detail;
|
|
|
|
|
2011-10-28 16:01:00 +05:30
|
|
|
unsigned const METADATA_CACHE_SIZE = 1024;
|
|
|
|
|
2013-04-25 20:57:07 +05:30
|
|
|
block_manager<>::ptr open_bm(string const &dev_path, bool writeable) {
|
2011-10-28 16:01:00 +05:30
|
|
|
block_address nr_blocks = get_nr_blocks(dev_path);
|
2013-01-12 01:46:40 +05:30
|
|
|
typename block_io<>::mode m = writeable ?
|
|
|
|
block_io<>::READ_WRITE :
|
|
|
|
block_io<>::READ_ONLY;
|
|
|
|
|
2013-04-25 20:57:07 +05:30
|
|
|
return block_manager<>::ptr(new block_manager<>(dev_path, nr_blocks, 1, m));
|
|
|
|
}
|
|
|
|
|
|
|
|
transaction_manager::ptr
|
|
|
|
open_tm(block_manager<>::ptr bm) {
|
|
|
|
space_map::ptr sm(new core_map(bm->get_nr_blocks()));
|
2011-11-03 20:14:00 +05:30
|
|
|
sm->inc(SUPERBLOCK_LOCATION);
|
2011-10-28 16:01:00 +05:30
|
|
|
transaction_manager::ptr tm(new transaction_manager(bm, sm));
|
|
|
|
return tm;
|
|
|
|
}
|
|
|
|
|
2011-11-03 20:14:00 +05:30
|
|
|
void
|
|
|
|
copy_space_maps(space_map::ptr lhs, space_map::ptr rhs) {
|
|
|
|
for (block_address b = 0; b < rhs->get_nr_blocks(); b++) {
|
|
|
|
uint32_t count = rhs->get_count(b);
|
|
|
|
if (count > 0)
|
|
|
|
lhs->set_count(b, rhs->get_count(b));
|
|
|
|
}
|
|
|
|
}
|
2013-04-25 20:57:07 +05:30
|
|
|
|
|
|
|
void print_superblock(superblock const &sb) {
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
cerr << "superblock " << sb.csum_ << endl
|
|
|
|
<< "flags " << sb.flags_ << endl
|
|
|
|
<< "blocknr " << sb.blocknr_ << endl
|
|
|
|
<< "transaction id " << sb.trans_id_ << endl
|
|
|
|
<< "data mapping root " << sb.data_mapping_root_ << endl
|
|
|
|
<< "details root " << sb.device_details_root_ << endl
|
|
|
|
<< "data block size " << sb.data_block_size_ << endl
|
|
|
|
<< "metadata block size " << sb.metadata_block_size_ << endl
|
|
|
|
<< "metadata nr blocks " << sb.metadata_nr_blocks_ << endl
|
|
|
|
;
|
|
|
|
}
|
2011-10-28 16:01:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2011-11-03 20:14:00 +05:30
|
|
|
metadata::metadata(std::string const &dev_path, open_type ot,
|
|
|
|
sector_t data_block_size, block_address nr_data_blocks)
|
2011-10-28 16:01:00 +05:30
|
|
|
{
|
2011-11-03 20:14:00 +05:30
|
|
|
switch (ot) {
|
|
|
|
case OPEN:
|
2013-04-25 20:57:07 +05:30
|
|
|
tm_ = open_tm(open_bm(dev_path, false));
|
2011-11-03 20:14:00 +05:30
|
|
|
sb_ = read_superblock(tm_->get_bm());
|
2012-05-17 17:35:26 +05:30
|
|
|
|
|
|
|
if (sb_.version_ != 1)
|
|
|
|
throw runtime_error("unknown metadata version");
|
|
|
|
|
2011-11-03 20:14:00 +05:30
|
|
|
metadata_sm_ = open_metadata_sm(tm_, &sb_.metadata_space_map_root_);
|
|
|
|
tm_->set_sm(metadata_sm_);
|
|
|
|
|
|
|
|
data_sm_ = open_disk_sm(tm_, static_cast<void *>(&sb_.data_space_map_root_));
|
2013-06-19 16:04:01 +05:30
|
|
|
|
|
|
|
details_ = device_tree::ptr(
|
|
|
|
new device_tree(tm_, sb_.device_details_root_,
|
|
|
|
device_tree_detail::device_details_traits::ref_counter()));
|
|
|
|
|
|
|
|
mappings_top_level_ = dev_tree::ptr(
|
|
|
|
new dev_tree(tm_, sb_.data_mapping_root_,
|
|
|
|
mapping_tree_detail::mtree_ref_counter(tm_)));
|
|
|
|
|
|
|
|
mappings_ = mapping_tree::ptr(
|
|
|
|
new mapping_tree(tm_, sb_.data_mapping_root_,
|
|
|
|
mapping_tree_detail::block_time_ref_counter(data_sm_)));
|
2011-11-03 20:14:00 +05:30
|
|
|
break;
|
|
|
|
|
|
|
|
case CREATE:
|
2013-04-25 20:57:07 +05:30
|
|
|
tm_ = open_tm(open_bm(dev_path, true));
|
2011-11-03 20:14:00 +05:30
|
|
|
space_map::ptr core = tm_->get_sm();
|
|
|
|
metadata_sm_ = create_metadata_sm(tm_, tm_->get_bm()->get_nr_blocks());
|
|
|
|
copy_space_maps(metadata_sm_, core);
|
|
|
|
tm_->set_sm(metadata_sm_);
|
|
|
|
|
|
|
|
data_sm_ = create_disk_sm(tm_, nr_data_blocks);
|
2013-05-20 21:05:26 +05:30
|
|
|
details_ = device_tree::ptr(new device_tree(tm_, device_tree_detail::device_details_traits::ref_counter()));
|
2013-05-20 22:07:46 +05:30
|
|
|
mappings_ = mapping_tree::ptr(new mapping_tree(tm_,
|
|
|
|
mapping_tree_detail::block_time_ref_counter(data_sm_)));
|
|
|
|
mappings_top_level_ = dev_tree::ptr(new dev_tree(tm_, mappings_->get_root(),
|
|
|
|
mapping_tree_detail::mtree_ref_counter(tm_)));
|
2011-11-03 20:14:00 +05:30
|
|
|
|
|
|
|
::memset(&sb_, 0, sizeof(sb_));
|
|
|
|
sb_.magic_ = SUPERBLOCK_MAGIC;
|
2011-11-07 16:04:43 +05:30
|
|
|
sb_.version_ = 1;
|
2011-11-03 20:14:00 +05:30
|
|
|
sb_.data_mapping_root_ = mappings_->get_root();
|
|
|
|
sb_.device_details_root_ = details_->get_root();
|
|
|
|
sb_.data_block_size_ = data_block_size;
|
|
|
|
sb_.metadata_block_size_ = MD_BLOCK_SIZE;
|
|
|
|
sb_.metadata_nr_blocks_ = tm_->get_bm()->get_nr_blocks();
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2011-10-28 16:01:00 +05:30
|
|
|
}
|
|
|
|
|
2013-07-16 21:09:40 +05:30
|
|
|
metadata::metadata(std::string const &dev_path, block_address metadata_snap)
|
2012-05-17 16:58:23 +05:30
|
|
|
{
|
2013-04-25 20:57:07 +05:30
|
|
|
tm_ = open_tm(open_bm(dev_path, false));
|
2013-07-16 21:09:40 +05:30
|
|
|
sb_ = read_superblock(tm_->get_bm(), metadata_snap);
|
2013-06-19 16:04:01 +05:30
|
|
|
|
2012-05-17 16:58:23 +05:30
|
|
|
// We don't open the metadata sm for a held root
|
|
|
|
//metadata_sm_ = open_metadata_sm(tm_, &sb_.metadata_space_map_root_);
|
|
|
|
tm_->set_sm(metadata_sm_);
|
|
|
|
|
|
|
|
data_sm_ = open_disk_sm(tm_, static_cast<void *>(&sb_.data_space_map_root_));
|
2013-05-20 21:05:26 +05:30
|
|
|
details_ = device_tree::ptr(new device_tree(tm_, sb_.device_details_root_, device_tree_detail::device_details_traits::ref_counter()));
|
2013-05-20 22:07:46 +05:30
|
|
|
mappings_top_level_ = dev_tree::ptr(new dev_tree(tm_, sb_.data_mapping_root_,
|
|
|
|
mapping_tree_detail::mtree_ref_counter(tm_)));
|
|
|
|
mappings_ = mapping_tree::ptr(new mapping_tree(tm_, sb_.data_mapping_root_,
|
|
|
|
mapping_tree_detail::block_time_ref_counter(data_sm_)));
|
2012-05-17 16:58:23 +05:30
|
|
|
}
|
|
|
|
|
2013-04-25 20:57:07 +05:30
|
|
|
// FIXME: duplication
|
|
|
|
metadata::metadata(block_manager<>::ptr bm, open_type ot,
|
|
|
|
sector_t data_block_size,
|
|
|
|
block_address nr_data_blocks)
|
|
|
|
{
|
|
|
|
switch (ot) {
|
|
|
|
case OPEN:
|
|
|
|
tm_ = open_tm(bm);
|
|
|
|
sb_ = read_superblock(tm_->get_bm());
|
2011-11-03 20:14:00 +05:30
|
|
|
|
2013-04-25 20:57:07 +05:30
|
|
|
if (sb_.version_ != 1)
|
|
|
|
throw runtime_error("unknown metadata version");
|
|
|
|
|
|
|
|
metadata_sm_ = open_metadata_sm(tm_, &sb_.metadata_space_map_root_);
|
|
|
|
tm_->set_sm(metadata_sm_);
|
|
|
|
|
|
|
|
data_sm_ = open_disk_sm(tm_, static_cast<void *>(&sb_.data_space_map_root_));
|
2013-05-20 21:05:26 +05:30
|
|
|
details_ = device_tree::ptr(new device_tree(tm_, sb_.device_details_root_, device_tree_detail::device_details_traits::ref_counter()));
|
2013-05-20 22:07:46 +05:30
|
|
|
mappings_top_level_ = dev_tree::ptr(new dev_tree(tm_, sb_.data_mapping_root_,
|
|
|
|
mapping_tree_detail::mtree_ref_counter(tm_)));
|
|
|
|
mappings_ = mapping_tree::ptr(new mapping_tree(tm_, sb_.data_mapping_root_,
|
|
|
|
mapping_tree_detail::block_time_ref_counter(data_sm_)));
|
2013-04-25 20:57:07 +05:30
|
|
|
break;
|
|
|
|
|
|
|
|
case CREATE:
|
|
|
|
tm_ = open_tm(bm);
|
|
|
|
space_map::ptr core = tm_->get_sm();
|
|
|
|
metadata_sm_ = create_metadata_sm(tm_, tm_->get_bm()->get_nr_blocks());
|
|
|
|
copy_space_maps(metadata_sm_, core);
|
|
|
|
tm_->set_sm(metadata_sm_);
|
|
|
|
|
|
|
|
data_sm_ = create_disk_sm(tm_, nr_data_blocks);
|
2013-05-20 21:05:26 +05:30
|
|
|
details_ = device_tree::ptr(new device_tree(tm_, device_tree_detail::device_details_traits::ref_counter()));
|
2013-05-20 22:07:46 +05:30
|
|
|
mappings_ = mapping_tree::ptr(new mapping_tree(tm_,
|
|
|
|
mapping_tree_detail::block_time_ref_counter(data_sm_)));
|
|
|
|
mappings_top_level_ = dev_tree::ptr(new dev_tree(tm_, mappings_->get_root(),
|
|
|
|
mapping_tree_detail::mtree_ref_counter(tm_)));
|
2013-04-25 20:57:07 +05:30
|
|
|
|
|
|
|
::memset(&sb_, 0, sizeof(sb_));
|
|
|
|
sb_.magic_ = SUPERBLOCK_MAGIC;
|
|
|
|
sb_.version_ = 1;
|
|
|
|
sb_.data_mapping_root_ = mappings_->get_root();
|
|
|
|
sb_.device_details_root_ = details_->get_root();
|
|
|
|
sb_.data_block_size_ = data_block_size;
|
|
|
|
sb_.metadata_block_size_ = MD_BLOCK_SIZE;
|
|
|
|
sb_.metadata_nr_blocks_ = tm_->get_bm()->get_nr_blocks();
|
|
|
|
|
|
|
|
break;
|
2011-11-03 20:14:00 +05:30
|
|
|
}
|
|
|
|
}
|
2011-10-28 16:01:00 +05:30
|
|
|
|
|
|
|
void
|
2011-10-28 16:55:06 +05:30
|
|
|
metadata::commit()
|
2011-10-28 16:01:00 +05:30
|
|
|
{
|
2011-10-28 20:58:24 +05:30
|
|
|
sb_.data_mapping_root_ = mappings_->get_root();
|
|
|
|
sb_.device_details_root_ = details_->get_root();
|
2011-10-28 16:01:00 +05:30
|
|
|
|
2011-11-03 20:14:00 +05:30
|
|
|
data_sm_->commit();
|
|
|
|
data_sm_->copy_root(&sb_.data_space_map_root_, sizeof(sb_.data_space_map_root_));
|
|
|
|
|
|
|
|
metadata_sm_->commit();
|
|
|
|
metadata_sm_->copy_root(&sb_.metadata_space_map_root_, sizeof(sb_.metadata_space_map_root_));
|
2011-10-28 18:56:06 +05:30
|
|
|
|
2013-04-23 19:51:44 +05:30
|
|
|
write_ref superblock = tm_->get_bm()->superblock_zero(SUPERBLOCK_LOCATION, superblock_validator());
|
2012-05-17 17:35:26 +05:30
|
|
|
superblock_disk *disk = reinterpret_cast<superblock_disk *>(superblock.data().raw());
|
2011-10-28 16:01:00 +05:30
|
|
|
superblock_traits::pack(sb_, *disk);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|