From 3c761e68221b4d06ae4cb2805dcc9ce1e2c90a5d Mon Sep 17 00:00:00 2001 From: Joe Thornber Date: Thu, 5 Oct 2017 11:53:40 +0100 Subject: [PATCH] [all tools] Factor out open_bm() and open_tm. Many duplicates of this code. --- caching/cache_check.cc | 11 +---------- era/era_check.cc | 11 +---------- persistent-data/file_utils.cc | 20 ++++++++++++++++++++ persistent-data/file_utils.h | 6 ++++++ thin-provisioning/metadata.cc | 22 ++++------------------ thin-provisioning/thin_check.cc | 21 +++------------------ thin-provisioning/thin_delta.cc | 10 ---------- thin-provisioning/thin_ll_dump.cc | 15 ++------------- thin-provisioning/thin_rmap.cc | 21 +++------------------ thin-provisioning/thin_scan.cc | 10 +--------- thin-provisioning/thin_show_duplicates.cc | 19 +++---------------- 11 files changed, 44 insertions(+), 122 deletions(-) diff --git a/caching/cache_check.cc b/caching/cache_check.cc index d1f84c9..64ed669 100644 --- a/caching/cache_check.cc +++ b/caching/cache_check.cc @@ -171,15 +171,6 @@ namespace { //-------------------------------- - transaction_manager::ptr open_tm(block_manager<>::ptr bm) { - space_map::ptr sm(new core_map(bm->get_nr_blocks())); - sm->inc(SUPERBLOCK_LOCATION); - transaction_manager::ptr tm(new transaction_manager(bm, sm)); - return tm; - } - - //-------------------------------- - struct flags { flags() : check_mappings_(true), @@ -242,7 +233,7 @@ namespace { return FATAL; superblock sb = read_superblock(bm); - transaction_manager::ptr tm = open_tm(bm); + transaction_manager::ptr tm = open_tm(bm, SUPERBLOCK_LOCATION); needs_check_set = sb.flags.get_flag(superblock_flags::NEEDS_CHECK); diff --git a/era/era_check.cc b/era/era_check.cc index 73e5169..b640c3f 100644 --- a/era/era_check.cc +++ b/era/era_check.cc @@ -162,15 +162,6 @@ namespace { //-------------------------------- - transaction_manager::ptr open_tm(block_manager<>::ptr bm) { - space_map::ptr sm(new core_map(bm->get_nr_blocks())); - sm->inc(SUPERBLOCK_LOCATION); - transaction_manager::ptr tm(new transaction_manager(bm, sm)); - return tm; - } - - //-------------------------------- - struct flags { flags() : superblock_only_(false), @@ -211,7 +202,7 @@ namespace { return FATAL; superblock sb = read_superblock(bm); - transaction_manager::ptr tm = open_tm(bm); + transaction_manager::ptr tm = open_tm(bm, SUPERBLOCK_LOCATION); writeset_tree_reporter wt_rep(out); { diff --git a/persistent-data/file_utils.cc b/persistent-data/file_utils.cc index 337ac69..38986fc 100644 --- a/persistent-data/file_utils.cc +++ b/persistent-data/file_utils.cc @@ -1,5 +1,6 @@ #include "persistent-data/math_utils.h" #include "persistent-data/file_utils.h" +#include "persistent-data/space-maps/core.h" #include #include @@ -36,4 +37,23 @@ persistent_data::open_bm(std::string const &dev_path, block_manager<>::mode m, b return block_manager<>::ptr(new block_manager<>(dev_path, nr_blocks, 1, m, excl)); } +block_manager<>::ptr +persistent_data::open_bm(std::string const &path) { + block_address nr_blocks = get_nr_metadata_blocks(path); + block_manager<>::mode m = block_manager<>::READ_ONLY; + return block_manager<>::ptr(new block_manager<>(path, nr_blocks, 1, m)); +} + +transaction_manager::ptr +persistent_data::open_tm(block_manager<>::ptr bm, block_address superblock_location) { + auto nr_blocks = bm->get_nr_blocks(); + if (!nr_blocks) + throw runtime_error("Metadata is not large enough for superblock."); + + space_map::ptr sm(new core_map(nr_blocks)); + sm->inc(superblock_location); + transaction_manager::ptr tm(new transaction_manager(bm, sm)); + return tm; +} + //---------------------------------------------------------------- diff --git a/persistent-data/file_utils.h b/persistent-data/file_utils.h index c3449a7..52a8d42 100644 --- a/persistent-data/file_utils.h +++ b/persistent-data/file_utils.h @@ -2,6 +2,7 @@ #define THIN_FILE_UTILS_H #include "persistent-data/block.h" +#include "persistent-data/transaction_manager.h" #include @@ -14,6 +15,11 @@ namespace persistent_data { block_manager<>::ptr open_bm(std::string const &dev_path, block_manager<>::mode m, bool excl = true); + + block_manager<>::ptr open_bm(std::string const &path); + transaction_manager::ptr open_tm(block_manager<>::ptr bm, + block_address superblock_location); + } //---------------------------------------------------------------- diff --git a/thin-provisioning/metadata.cc b/thin-provisioning/metadata.cc index ac8b63e..08dd666 100644 --- a/thin-provisioning/metadata.cc +++ b/thin-provisioning/metadata.cc @@ -38,20 +38,6 @@ using namespace thin_provisioning; namespace { using namespace superblock_detail; - unsigned const METADATA_CACHE_SIZE = 1024; - - transaction_manager::ptr - open_tm(block_manager<>::ptr bm) { - auto nr_blocks = bm->get_nr_blocks(); - if (!nr_blocks) - throw runtime_error("Metadata is not large enough for superblock."); - - space_map::ptr sm(new core_map(nr_blocks)); - sm->inc(SUPERBLOCK_LOCATION); - transaction_manager::ptr tm(new transaction_manager(bm, sm)); - return tm; - } - void copy_space_maps(space_map::ptr lhs, space_map::ptr rhs) { for (block_address b = 0; b < rhs->get_nr_blocks(); b++) { @@ -70,7 +56,7 @@ metadata::metadata(block_manager<>::ptr bm, open_type ot, { switch (ot) { case OPEN: - tm_ = open_tm(bm); + tm_ = open_tm(bm, SUPERBLOCK_LOCATION); sb_ = read_superblock(tm_->get_bm()); if (sb_.version_ != 1) @@ -89,7 +75,7 @@ metadata::metadata(block_manager<>::ptr bm, open_type ot, break; case CREATE: - tm_ = open_tm(bm); + tm_ = open_tm(bm, SUPERBLOCK_LOCATION); 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); @@ -118,7 +104,7 @@ metadata::metadata(block_manager<>::ptr bm, open_type ot, metadata::metadata(block_manager<>::ptr bm, bool read_space_maps) { - tm_ = open_tm(bm); + tm_ = open_tm(bm, SUPERBLOCK_LOCATION); sb_ = read_superblock(tm_->get_bm(), SUPERBLOCK_LOCATION); if (read_space_maps) @@ -130,7 +116,7 @@ metadata::metadata(block_manager<>::ptr bm, bool read_space_maps) metadata::metadata(block_manager<>::ptr bm, boost::optional metadata_snap) { - tm_ = open_tm(bm); + tm_ = open_tm(bm, SUPERBLOCK_LOCATION); superblock_detail::superblock actual_sb = read_superblock(bm, SUPERBLOCK_LOCATION); diff --git a/thin-provisioning/thin_check.cc b/thin-provisioning/thin_check.cc index cb3e8df..5645f52 100644 --- a/thin-provisioning/thin_check.cc +++ b/thin-provisioning/thin_check.cc @@ -33,6 +33,7 @@ #include "persistent-data/space-maps/core.h" #include "persistent-data/space-maps/disk.h" #include "persistent-data/file_utils.h" +#include "thin-provisioning/metadata.h" #include "thin-provisioning/device_tree.h" #include "thin-provisioning/mapping_tree.h" #include "thin-provisioning/metadata_counter.h" @@ -46,23 +47,6 @@ using namespace thin_provisioning; //---------------------------------------------------------------- namespace { - block_manager<>::ptr - open_bm(string const &path) { - block_address nr_blocks = get_nr_metadata_blocks(path); - block_manager<>::mode m = block_manager<>::READ_ONLY; - return block_manager<>::ptr(new block_manager<>(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())); - sm->inc(superblock_detail::SUPERBLOCK_LOCATION); - transaction_manager::ptr tm(new transaction_manager(bm, sm)); - return tm; - } - - //-------------------------------- - class superblock_reporter : public superblock_detail::damage_visitor { public: superblock_reporter(nested_output &out) @@ -246,7 +230,8 @@ namespace { } superblock_detail::superblock sb = read_superblock(bm); - transaction_manager::ptr tm = open_tm(bm); + transaction_manager::ptr tm = + open_tm(bm, superblock_detail::SUPERBLOCK_LOCATION); if (fs.check_device_tree) { out << "examining devices tree" << end_message(); diff --git a/thin-provisioning/thin_delta.cc b/thin-provisioning/thin_delta.cc index 59ff8ff..5d54759 100644 --- a/thin-provisioning/thin_delta.cc +++ b/thin-provisioning/thin_delta.cc @@ -80,16 +80,6 @@ namespace local { //-------------------------------- - transaction_manager::ptr - open_tm(block_manager<>::ptr bm) { - space_map::ptr sm(new core_map(bm->get_nr_blocks())); - sm->inc(superblock_detail::SUPERBLOCK_LOCATION); - transaction_manager::ptr tm(new transaction_manager(bm, sm)); - return tm; - } - - //-------------------------------- - struct mapping { mapping() : vbegin_(0), diff --git a/thin-provisioning/thin_ll_dump.cc b/thin-provisioning/thin_ll_dump.cc index 94f6a08..bc11cf2 100644 --- a/thin-provisioning/thin_ll_dump.cc +++ b/thin-provisioning/thin_ll_dump.cc @@ -37,18 +37,6 @@ using namespace thin_provisioning; using namespace persistent_data; -//---------------------------------------------------------------- - -namespace { - transaction_manager::ptr - open_tm(block_manager<>::ptr bm) { - space_map::ptr sm(new core_map(bm->get_nr_blocks())); - sm->inc(superblock_detail::SUPERBLOCK_LOCATION); - transaction_manager::ptr tm(new transaction_manager(bm, sm)); - return tm; - } -} - //--------------------------------------------------------------------------- namespace { @@ -212,7 +200,8 @@ namespace { if (f.device_details_root_) sb.device_details_root_ = *f.device_details_root_; - transaction_manager::ptr tm = open_tm(bm); + transaction_manager::ptr tm = + open_tm(bm, superblock_detail::SUPERBLOCK_LOCATION); indented_stream out(output); diff --git a/thin-provisioning/thin_rmap.cc b/thin-provisioning/thin_rmap.cc index 4d6b059..3622ea2 100644 --- a/thin-provisioning/thin_rmap.cc +++ b/thin-provisioning/thin_rmap.cc @@ -13,6 +13,7 @@ #include "thin-provisioning/commands.h" #include "thin-provisioning/superblock.h" #include "thin-provisioning/mapping_tree.h" +#include "thin-provisioning/metadata.h" #include "thin-provisioning/rmap_visitor.h" using namespace std; @@ -21,23 +22,6 @@ using namespace thin_provisioning; //---------------------------------------------------------------- namespace { - block_manager<>::ptr - open_bm(string const &path) { - block_address nr_blocks = get_nr_metadata_blocks(path); - block_manager<>::mode m = block_manager<>::READ_ONLY; - return block_manager<>::ptr(new block_manager<>(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())); - sm->inc(superblock_detail::SUPERBLOCK_LOCATION); - transaction_manager::ptr tm(new transaction_manager(bm, sm)); - return tm; - } - - //-------------------------------- - using namespace mapping_tree_detail; typedef rmap_visitor::region region; @@ -73,7 +57,8 @@ namespace { rv.add_data_region(*it); block_manager<>::ptr bm = open_bm(path); - transaction_manager::ptr tm = open_tm(bm); + transaction_manager::ptr tm = + open_tm(bm, superblock_detail::SUPERBLOCK_LOCATION); superblock_detail::superblock sb = read_superblock(bm); mapping_tree mtree(*tm, sb.data_mapping_root_, diff --git a/thin-provisioning/thin_scan.cc b/thin-provisioning/thin_scan.cc index 6f63df0..81a4e88 100644 --- a/thin-provisioning/thin_scan.cc +++ b/thin-provisioning/thin_scan.cc @@ -94,14 +94,6 @@ namespace { return true; } - - transaction_manager::ptr - open_tm(block_manager<>::ptr bm) { - space_map::ptr sm(new core_map(bm->get_nr_blocks())); - sm->inc(superblock_detail::SUPERBLOCK_LOCATION); - transaction_manager::ptr tm(new transaction_manager(bm, sm)); - return tm; - } } namespace { @@ -243,7 +235,7 @@ namespace { checked_space_map::ptr metadata_sm; try { superblock_detail::superblock sb = read_superblock(bm); - tm = open_tm(bm); + tm = open_tm(bm, superblock_detail::SUPERBLOCK_LOCATION); metadata_sm = open_metadata_sm(*tm, &sb.metadata_space_map_root_); tm->set_sm(metadata_sm); } catch (std::exception &e) { diff --git a/thin-provisioning/thin_show_duplicates.cc b/thin-provisioning/thin_show_duplicates.cc index 6c1dc27..f9faaaf 100644 --- a/thin-provisioning/thin_show_duplicates.cc +++ b/thin-provisioning/thin_show_duplicates.cc @@ -35,6 +35,7 @@ #include "thin-provisioning/commands.h" #include "thin-provisioning/device_tree.h" #include "thin-provisioning/mapping_tree.h" +#include "thin-provisioning/metadata.h" #include "thin-provisioning/rmap_visitor.h" #include "thin-provisioning/superblock.h" #include "thin-provisioning/variable_chunk_stream.h" @@ -58,21 +59,6 @@ namespace { return (n % f) == 0; } - block_manager<>::ptr - open_bm(string const &path) { - block_address nr_blocks = get_nr_blocks(path); - block_manager<>::mode m = block_manager<>::READ_ONLY; - return block_manager<>::ptr(new block_manager<>(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())); - sm->inc(superblock_detail::SUPERBLOCK_LOCATION); - transaction_manager::ptr tm(new transaction_manager(bm, sm)); - return tm; - } - uint64_t parse_int(string const &str, string const &desc) { try { return boost::lexical_cast(str); @@ -222,7 +208,8 @@ namespace { int show_dups_pool(flags const &fs) { block_manager<>::ptr bm = open_bm(*fs.metadata_dev); - transaction_manager::ptr tm = open_tm(bm); + transaction_manager::ptr tm = + open_tm(bm, superblock_detail::SUPERBLOCK_LOCATION); superblock_detail::superblock sb = read_superblock(bm); block_address block_size = sb.data_block_size_ * 512; block_address nr_blocks = get_nr_blocks(fs.data_dev, block_size);