[all tools] Factor out open_bm() and open_tm.

Many duplicates of this code.
This commit is contained in:
Joe Thornber 2017-10-05 11:53:40 +01:00
parent 924a996fa7
commit 3c761e6822
11 changed files with 44 additions and 122 deletions

View File

@ -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 { struct flags {
flags() flags()
: check_mappings_(true), : check_mappings_(true),
@ -242,7 +233,7 @@ namespace {
return FATAL; return FATAL;
superblock sb = read_superblock(bm); 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); needs_check_set = sb.flags.get_flag(superblock_flags::NEEDS_CHECK);

View File

@ -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 { struct flags {
flags() flags()
: superblock_only_(false), : superblock_only_(false),
@ -211,7 +202,7 @@ namespace {
return FATAL; return FATAL;
superblock sb = read_superblock(bm); 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); writeset_tree_reporter wt_rep(out);
{ {

View File

@ -1,5 +1,6 @@
#include "persistent-data/math_utils.h" #include "persistent-data/math_utils.h"
#include "persistent-data/file_utils.h" #include "persistent-data/file_utils.h"
#include "persistent-data/space-maps/core.h"
#include <linux/fs.h> #include <linux/fs.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
@ -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)); 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;
}
//---------------------------------------------------------------- //----------------------------------------------------------------

View File

@ -2,6 +2,7 @@
#define THIN_FILE_UTILS_H #define THIN_FILE_UTILS_H
#include "persistent-data/block.h" #include "persistent-data/block.h"
#include "persistent-data/transaction_manager.h"
#include <string> #include <string>
@ -14,6 +15,11 @@ namespace persistent_data {
block_manager<>::ptr open_bm(std::string const &dev_path, block_manager<>::ptr open_bm(std::string const &dev_path,
block_manager<>::mode m, bool excl = true); 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);
} }
//---------------------------------------------------------------- //----------------------------------------------------------------

View File

@ -38,20 +38,6 @@ using namespace thin_provisioning;
namespace { namespace {
using namespace superblock_detail; 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 void
copy_space_maps(space_map::ptr lhs, space_map::ptr rhs) { copy_space_maps(space_map::ptr lhs, space_map::ptr rhs) {
for (block_address b = 0; b < rhs->get_nr_blocks(); b++) { 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) { switch (ot) {
case OPEN: case OPEN:
tm_ = open_tm(bm); tm_ = open_tm(bm, SUPERBLOCK_LOCATION);
sb_ = read_superblock(tm_->get_bm()); sb_ = read_superblock(tm_->get_bm());
if (sb_.version_ != 1) if (sb_.version_ != 1)
@ -89,7 +75,7 @@ metadata::metadata(block_manager<>::ptr bm, open_type ot,
break; break;
case CREATE: case CREATE:
tm_ = open_tm(bm); tm_ = open_tm(bm, SUPERBLOCK_LOCATION);
space_map::ptr core = tm_->get_sm(); space_map::ptr core = tm_->get_sm();
metadata_sm_ = create_metadata_sm(*tm_, tm_->get_bm()->get_nr_blocks()); metadata_sm_ = create_metadata_sm(*tm_, tm_->get_bm()->get_nr_blocks());
copy_space_maps(metadata_sm_, core); 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) 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); sb_ = read_superblock(tm_->get_bm(), SUPERBLOCK_LOCATION);
if (read_space_maps) if (read_space_maps)
@ -130,7 +116,7 @@ metadata::metadata(block_manager<>::ptr bm, bool read_space_maps)
metadata::metadata(block_manager<>::ptr bm, metadata::metadata(block_manager<>::ptr bm,
boost::optional<block_address> metadata_snap) boost::optional<block_address> metadata_snap)
{ {
tm_ = open_tm(bm); tm_ = open_tm(bm, SUPERBLOCK_LOCATION);
superblock_detail::superblock actual_sb = read_superblock(bm, SUPERBLOCK_LOCATION); superblock_detail::superblock actual_sb = read_superblock(bm, SUPERBLOCK_LOCATION);

View File

@ -33,6 +33,7 @@
#include "persistent-data/space-maps/core.h" #include "persistent-data/space-maps/core.h"
#include "persistent-data/space-maps/disk.h" #include "persistent-data/space-maps/disk.h"
#include "persistent-data/file_utils.h" #include "persistent-data/file_utils.h"
#include "thin-provisioning/metadata.h"
#include "thin-provisioning/device_tree.h" #include "thin-provisioning/device_tree.h"
#include "thin-provisioning/mapping_tree.h" #include "thin-provisioning/mapping_tree.h"
#include "thin-provisioning/metadata_counter.h" #include "thin-provisioning/metadata_counter.h"
@ -46,23 +47,6 @@ using namespace thin_provisioning;
//---------------------------------------------------------------- //----------------------------------------------------------------
namespace { 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 { class superblock_reporter : public superblock_detail::damage_visitor {
public: public:
superblock_reporter(nested_output &out) superblock_reporter(nested_output &out)
@ -246,7 +230,8 @@ namespace {
} }
superblock_detail::superblock sb = read_superblock(bm); 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) { if (fs.check_device_tree) {
out << "examining devices tree" << end_message(); out << "examining devices tree" << end_message();

View File

@ -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 { struct mapping {
mapping() mapping()
: vbegin_(0), : vbegin_(0),

View File

@ -37,18 +37,6 @@
using namespace thin_provisioning; using namespace thin_provisioning;
using namespace persistent_data; 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 { namespace {
@ -212,7 +200,8 @@ namespace {
if (f.device_details_root_) if (f.device_details_root_)
sb.device_details_root_ = *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); indented_stream out(output);

View File

@ -13,6 +13,7 @@
#include "thin-provisioning/commands.h" #include "thin-provisioning/commands.h"
#include "thin-provisioning/superblock.h" #include "thin-provisioning/superblock.h"
#include "thin-provisioning/mapping_tree.h" #include "thin-provisioning/mapping_tree.h"
#include "thin-provisioning/metadata.h"
#include "thin-provisioning/rmap_visitor.h" #include "thin-provisioning/rmap_visitor.h"
using namespace std; using namespace std;
@ -21,23 +22,6 @@ using namespace thin_provisioning;
//---------------------------------------------------------------- //----------------------------------------------------------------
namespace { 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; using namespace mapping_tree_detail;
typedef rmap_visitor::region region; typedef rmap_visitor::region region;
@ -73,7 +57,8 @@ namespace {
rv.add_data_region(*it); rv.add_data_region(*it);
block_manager<>::ptr bm = open_bm(path); 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); superblock_detail::superblock sb = read_superblock(bm);
mapping_tree mtree(*tm, sb.data_mapping_root_, mapping_tree mtree(*tm, sb.data_mapping_root_,

View File

@ -94,14 +94,6 @@ namespace {
return true; 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 { namespace {
@ -243,7 +235,7 @@ namespace {
checked_space_map::ptr metadata_sm; checked_space_map::ptr metadata_sm;
try { try {
superblock_detail::superblock sb = read_superblock(bm); 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_); metadata_sm = open_metadata_sm(*tm, &sb.metadata_space_map_root_);
tm->set_sm(metadata_sm); tm->set_sm(metadata_sm);
} catch (std::exception &e) { } catch (std::exception &e) {

View File

@ -35,6 +35,7 @@
#include "thin-provisioning/commands.h" #include "thin-provisioning/commands.h"
#include "thin-provisioning/device_tree.h" #include "thin-provisioning/device_tree.h"
#include "thin-provisioning/mapping_tree.h" #include "thin-provisioning/mapping_tree.h"
#include "thin-provisioning/metadata.h"
#include "thin-provisioning/rmap_visitor.h" #include "thin-provisioning/rmap_visitor.h"
#include "thin-provisioning/superblock.h" #include "thin-provisioning/superblock.h"
#include "thin-provisioning/variable_chunk_stream.h" #include "thin-provisioning/variable_chunk_stream.h"
@ -58,21 +59,6 @@ namespace {
return (n % f) == 0; 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) { uint64_t parse_int(string const &str, string const &desc) {
try { try {
return boost::lexical_cast<uint64_t>(str); return boost::lexical_cast<uint64_t>(str);
@ -222,7 +208,8 @@ namespace {
int show_dups_pool(flags const &fs) { int show_dups_pool(flags const &fs) {
block_manager<>::ptr bm = open_bm(*fs.metadata_dev); 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); superblock_detail::superblock sb = read_superblock(bm);
block_address block_size = sb.data_block_size_ * 512; block_address block_size = sb.data_block_size_ * 512;
block_address nr_blocks = get_nr_blocks(fs.data_dev, block_size); block_address nr_blocks = get_nr_blocks(fs.data_dev, block_size);