2011-12-15 19:34:31 +01:00
|
|
|
// Copyright (C) 2011 Red Hat, Inc. All rights reserved.
|
2011-12-06 13:53:05 +00:00
|
|
|
//
|
2011-12-06 13:43:56 +00:00
|
|
|
// 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 16:09:13 +01:00
|
|
|
#include "thin-provisioning/device_tree.h"
|
2013-01-02 12:55:41 +00:00
|
|
|
#include "thin-provisioning/metadata.h"
|
2011-10-28 11:31:00 +01:00
|
|
|
|
2013-09-11 11:40:46 +01:00
|
|
|
#include "persistent-data/file_utils.h"
|
2013-01-02 12:55:41 +00:00
|
|
|
#include "persistent-data/math_utils.h"
|
2013-01-10 21:36:38 +00:00
|
|
|
#include "persistent-data/space-maps/core.h"
|
|
|
|
#include "persistent-data/space-maps/disk.h"
|
2011-10-28 11:31:00 +01:00
|
|
|
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2013-04-23 15:21:44 +01:00
|
|
|
using namespace base;
|
2011-10-28 11:31:00 +01:00
|
|
|
using namespace thin_provisioning;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
2013-06-19 11:34:01 +01:00
|
|
|
|
2011-10-28 11:31:00 +01:00
|
|
|
namespace {
|
2013-05-21 12:46:07 +01:00
|
|
|
using namespace superblock_detail;
|
|
|
|
|
2011-11-03 14:44:00 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
2011-10-28 11:31:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2020-04-30 14:30:01 +01:00
|
|
|
metadata::metadata(block_manager::ptr bm, open_type ot,
|
2015-12-14 15:29:57 +00:00
|
|
|
sector_t data_block_size,
|
|
|
|
block_address nr_data_blocks)
|
2011-10-28 11:31:00 +01:00
|
|
|
{
|
2011-11-03 14:44:00 +00:00
|
|
|
switch (ot) {
|
|
|
|
case OPEN:
|
2017-10-05 11:53:40 +01:00
|
|
|
tm_ = open_tm(bm, SUPERBLOCK_LOCATION);
|
2011-11-03 14:44:00 +00:00
|
|
|
sb_ = read_superblock(tm_->get_bm());
|
2012-05-17 13:05:26 +01:00
|
|
|
|
2018-04-17 13:32:49 +01:00
|
|
|
if (sb_.version_ < METADATA_VERSION)
|
2012-05-17 13:05:26 +01:00
|
|
|
throw runtime_error("unknown metadata version");
|
|
|
|
|
2014-08-26 11:14:49 +01:00
|
|
|
metadata_sm_ = open_metadata_sm(*tm_, &sb_.metadata_space_map_root_);
|
2011-11-03 14:44:00 +00:00
|
|
|
tm_->set_sm(metadata_sm_);
|
|
|
|
|
2014-08-26 11:14:49 +01:00
|
|
|
data_sm_ = open_disk_sm(*tm_, static_cast<void *>(&sb_.data_space_map_root_));
|
2015-12-14 15:29:57 +00:00
|
|
|
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_,
|
2019-04-04 14:38:46 +01:00
|
|
|
mapping_tree_detail::mtree_ref_counter(*tm_)));
|
2015-12-14 15:29:57 +00:00
|
|
|
mappings_ = mapping_tree::ptr(new mapping_tree(*tm_, sb_.data_mapping_root_,
|
|
|
|
mapping_tree_detail::block_time_ref_counter(data_sm_)));
|
2011-11-03 14:44:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CREATE:
|
2017-10-05 11:53:40 +01:00
|
|
|
tm_ = open_tm(bm, SUPERBLOCK_LOCATION);
|
2011-11-03 14:44:00 +00:00
|
|
|
space_map::ptr core = tm_->get_sm();
|
2014-08-26 11:14:49 +01:00
|
|
|
metadata_sm_ = create_metadata_sm(*tm_, tm_->get_bm()->get_nr_blocks());
|
2011-11-03 14:44:00 +00:00
|
|
|
copy_space_maps(metadata_sm_, core);
|
|
|
|
tm_->set_sm(metadata_sm_);
|
|
|
|
|
2014-08-26 11:14:49 +01:00
|
|
|
data_sm_ = create_disk_sm(*tm_, nr_data_blocks);
|
2015-12-14 15:29:57 +00:00
|
|
|
details_ = device_tree::ptr(new device_tree(*tm_,
|
|
|
|
device_tree_detail::device_details_traits::ref_counter()));
|
2014-08-26 11:14:49 +01:00
|
|
|
mappings_ = mapping_tree::ptr(new mapping_tree(*tm_,
|
2013-05-20 17:37:46 +01:00
|
|
|
mapping_tree_detail::block_time_ref_counter(data_sm_)));
|
2014-08-26 11:14:49 +01:00
|
|
|
mappings_top_level_ = dev_tree::ptr(new dev_tree(*tm_, mappings_->get_root(),
|
2019-04-04 14:38:46 +01:00
|
|
|
mapping_tree_detail::mtree_ref_counter(*tm_)));
|
2011-11-03 14:44:00 +00:00
|
|
|
|
|
|
|
::memset(&sb_, 0, sizeof(sb_));
|
|
|
|
sb_.magic_ = SUPERBLOCK_MAGIC;
|
2018-04-17 13:32:49 +01:00
|
|
|
sb_.version_ = METADATA_VERSION;
|
2011-11-03 14:44:00 +00:00
|
|
|
sb_.data_mapping_root_ = mappings_->get_root();
|
|
|
|
sb_.device_details_root_ = details_->get_root();
|
|
|
|
sb_.data_block_size_ = data_block_size;
|
2015-11-27 23:38:37 +08:00
|
|
|
sb_.metadata_block_size_ = MD_BLOCK_SIZE >> SECTOR_SHIFT;
|
2018-04-17 21:00:44 +08:00
|
|
|
sb_.metadata_nr_blocks_ = metadata_sm_->get_nr_blocks();
|
2011-11-03 14:44:00 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2011-10-28 11:31:00 +01:00
|
|
|
}
|
|
|
|
|
2020-04-30 14:30:01 +01:00
|
|
|
metadata::metadata(block_manager::ptr bm, bool read_space_maps)
|
2012-05-17 12:28:23 +01:00
|
|
|
{
|
2017-10-05 11:53:40 +01:00
|
|
|
tm_ = open_tm(bm, SUPERBLOCK_LOCATION);
|
2015-12-15 10:08:07 +00:00
|
|
|
sb_ = read_superblock(tm_->get_bm(), SUPERBLOCK_LOCATION);
|
2013-06-19 11:34:01 +01:00
|
|
|
|
2016-02-16 14:08:43 +00:00
|
|
|
if (read_space_maps)
|
|
|
|
open_space_maps();
|
|
|
|
|
2015-12-15 10:08:07 +00:00
|
|
|
open_btrees();
|
|
|
|
}
|
2012-05-17 12:28:23 +01:00
|
|
|
|
2020-04-30 14:30:01 +01:00
|
|
|
metadata::metadata(block_manager::ptr bm,
|
2015-12-15 10:08:07 +00:00
|
|
|
boost::optional<block_address> metadata_snap)
|
|
|
|
{
|
2017-10-05 11:53:40 +01:00
|
|
|
tm_ = open_tm(bm, SUPERBLOCK_LOCATION);
|
2012-05-17 12:28:23 +01:00
|
|
|
|
2015-12-15 10:08:07 +00:00
|
|
|
superblock_detail::superblock actual_sb = read_superblock(bm, SUPERBLOCK_LOCATION);
|
2011-11-03 14:44:00 +00:00
|
|
|
|
2015-12-15 10:08:07 +00:00
|
|
|
if (!actual_sb.metadata_snap_)
|
|
|
|
throw runtime_error("no current metadata snap");
|
2013-04-25 16:27:07 +01:00
|
|
|
|
2015-12-15 10:08:07 +00:00
|
|
|
if (metadata_snap && *metadata_snap != actual_sb.metadata_snap_)
|
|
|
|
throw runtime_error("metadata snapshot does not match that in superblock");
|
2013-04-25 16:27:07 +01:00
|
|
|
|
2015-12-15 10:08:07 +00:00
|
|
|
sb_ = read_superblock(bm, actual_sb.metadata_snap_);
|
2015-12-14 15:29:57 +00:00
|
|
|
|
2015-12-15 10:08:07 +00:00
|
|
|
// metadata snaps don't record the space maps
|
|
|
|
open_btrees();
|
2011-11-03 14:44:00 +00:00
|
|
|
}
|
2011-10-28 11:31:00 +01:00
|
|
|
|
2020-04-30 14:30:01 +01:00
|
|
|
metadata::metadata(block_manager::ptr bm, superblock_detail::superblock const &sb)
|
2019-04-17 12:15:46 +01:00
|
|
|
{
|
|
|
|
tm_ = open_tm(bm, SUPERBLOCK_LOCATION);
|
|
|
|
sb_ = sb;
|
|
|
|
open_btrees();
|
|
|
|
}
|
|
|
|
|
2011-10-28 11:31:00 +01:00
|
|
|
void
|
2011-10-28 12:25:06 +01:00
|
|
|
metadata::commit()
|
2011-10-28 11:31:00 +01:00
|
|
|
{
|
2011-10-28 16:28:24 +01:00
|
|
|
sb_.data_mapping_root_ = mappings_->get_root();
|
|
|
|
sb_.device_details_root_ = details_->get_root();
|
2011-10-28 11:31:00 +01:00
|
|
|
|
2011-11-03 14:44:00 +00:00
|
|
|
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 14:26:06 +01:00
|
|
|
|
2013-04-23 15:21:44 +01:00
|
|
|
write_ref superblock = tm_->get_bm()->superblock_zero(SUPERBLOCK_LOCATION, superblock_validator());
|
2014-07-25 14:46:51 +01:00
|
|
|
superblock_disk *disk = reinterpret_cast<superblock_disk *>(superblock.data());
|
2011-10-28 11:31:00 +01:00
|
|
|
superblock_traits::pack(sb_, *disk);
|
|
|
|
}
|
|
|
|
|
2015-12-15 10:08:07 +00:00
|
|
|
void metadata::open_space_maps()
|
|
|
|
{
|
|
|
|
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_));
|
|
|
|
}
|
|
|
|
|
|
|
|
void metadata::open_btrees()
|
|
|
|
{
|
|
|
|
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_,
|
2019-04-04 14:38:46 +01:00
|
|
|
mapping_tree_detail::mtree_ref_counter(*tm_)));
|
2015-12-15 10:08:07 +00:00
|
|
|
mappings_ = mapping_tree::ptr(new mapping_tree(*tm_, sb_.data_mapping_root_,
|
|
|
|
mapping_tree_detail::block_time_ref_counter(data_sm_)));
|
|
|
|
}
|
|
|
|
|
2011-10-28 11:31:00 +01:00
|
|
|
//----------------------------------------------------------------
|