[all tools] Factor out open_bm() and open_tm.
Many duplicates of this code.
This commit is contained in:
		@@ -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);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
		{
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
#include "persistent-data/math_utils.h"
 | 
			
		||||
#include "persistent-data/file_utils.h"
 | 
			
		||||
#include "persistent-data/space-maps/core.h"
 | 
			
		||||
 | 
			
		||||
#include <linux/fs.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));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//----------------------------------------------------------------
 | 
			
		||||
 
 | 
			
		||||
@@ -2,6 +2,7 @@
 | 
			
		||||
#define THIN_FILE_UTILS_H
 | 
			
		||||
 | 
			
		||||
#include "persistent-data/block.h"
 | 
			
		||||
#include "persistent-data/transaction_manager.h"
 | 
			
		||||
 | 
			
		||||
#include <string>
 | 
			
		||||
 | 
			
		||||
@@ -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);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//----------------------------------------------------------------
 | 
			
		||||
 
 | 
			
		||||
@@ -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<block_address> metadata_snap)
 | 
			
		||||
{
 | 
			
		||||
	tm_ = open_tm(bm);
 | 
			
		||||
	tm_ = open_tm(bm, SUPERBLOCK_LOCATION);
 | 
			
		||||
 | 
			
		||||
	superblock_detail::superblock actual_sb = read_superblock(bm, SUPERBLOCK_LOCATION);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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();
 | 
			
		||||
 
 | 
			
		||||
@@ -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),
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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_,
 | 
			
		||||
 
 | 
			
		||||
@@ -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) {
 | 
			
		||||
 
 | 
			
		||||
@@ -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<uint64_t>(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);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user