Split the metadata class into a low level one that other interfaces
can use (metadata_ll), and the existing high level one.
This commit is contained in:
parent
28ab23fe6c
commit
9c9d6f2e15
1
Makefile
1
Makefile
@ -9,6 +9,7 @@ SOURCE=\
|
||||
hex_dump.cc \
|
||||
human_readable_format.cc \
|
||||
metadata.cc \
|
||||
metadata_ll.cc \
|
||||
metadata_dump.cc \
|
||||
metadata_disk_structures.cc \
|
||||
space_map_disk.cc \
|
||||
|
174
metadata.cc
174
metadata.cc
@ -1,9 +1,6 @@
|
||||
#include "metadata.h"
|
||||
|
||||
#include "btree_checker.h"
|
||||
#include "core_map.h"
|
||||
#include "math_utils.h"
|
||||
#include "space_map_disk.h"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <sstream>
|
||||
@ -11,12 +8,6 @@
|
||||
#include <set>
|
||||
#include <map>
|
||||
|
||||
#include <linux/fs.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
using namespace base;
|
||||
using namespace std;
|
||||
using namespace persistent_data;
|
||||
@ -25,71 +16,6 @@ using namespace thin_provisioning;
|
||||
//----------------------------------------------------------------
|
||||
|
||||
namespace {
|
||||
uint32_t const SUPERBLOCK_MAGIC = 27022010;
|
||||
block_address const SUPERBLOCK_LOCATION = 0;
|
||||
uint32_t const VERSION = 1;
|
||||
unsigned const METADATA_CACHE_SIZE = 1024;
|
||||
unsigned const SECTOR_TO_BLOCK_SHIFT = 3;
|
||||
|
||||
block_address get_nr_blocks(string const &path) {
|
||||
struct stat info;
|
||||
block_address nr_blocks;
|
||||
|
||||
int r = ::stat(path.c_str(), &info);
|
||||
if (r)
|
||||
throw runtime_error("Couldn't stat dev path");
|
||||
|
||||
if (S_ISREG(info.st_mode))
|
||||
nr_blocks = div_down<block_address>(info.st_size, MD_BLOCK_SIZE);
|
||||
|
||||
else if (S_ISBLK(info.st_mode)) {
|
||||
// To get the size of a block device we need to
|
||||
// open it, and then make an ioctl call.
|
||||
int fd = ::open(path.c_str(), O_RDONLY);
|
||||
if (fd < 0)
|
||||
throw runtime_error("couldn't open block device to ascertain size");
|
||||
|
||||
r = ::ioctl(fd, BLKGETSIZE64, &nr_blocks);
|
||||
if (r) {
|
||||
::close(fd);
|
||||
throw runtime_error("ioctl BLKGETSIZE64 failed");
|
||||
}
|
||||
::close(fd);
|
||||
nr_blocks = div_down<block_address>(nr_blocks, MD_BLOCK_SIZE);
|
||||
} else
|
||||
throw runtime_error("bad path");
|
||||
|
||||
return nr_blocks;
|
||||
}
|
||||
|
||||
transaction_manager::ptr
|
||||
open_tm(string const &dev_path) {
|
||||
block_address nr_blocks = get_nr_blocks(dev_path);
|
||||
block_manager<>::ptr bm(new block_manager<>(dev_path, nr_blocks, 8));
|
||||
space_map::ptr sm(new core_map(nr_blocks));
|
||||
transaction_manager::ptr tm(new transaction_manager(bm, sm));
|
||||
return tm;
|
||||
}
|
||||
|
||||
superblock read_superblock(block_manager<>::ptr bm) {
|
||||
superblock sb;
|
||||
block_manager<>::read_ref r = bm->read_lock(SUPERBLOCK_LOCATION);
|
||||
superblock_disk const *sbd = reinterpret_cast<superblock_disk const *>(&r.data());
|
||||
|
||||
crc32c sum(160774);
|
||||
sum.append(&sbd->flags_, MD_BLOCK_SIZE - sizeof(uint32_t));
|
||||
if (sum.get_sum() != to_cpu<uint32_t>(sbd->csum_)) {
|
||||
ostringstream out;
|
||||
out << "bad checksum in superblock, calculated "
|
||||
<< sum.get_sum()
|
||||
<< ", superblock contains " << to_cpu<uint32_t>(sbd->csum_);
|
||||
throw runtime_error(out.str());
|
||||
}
|
||||
|
||||
superblock_traits::unpack(*sbd, sb);
|
||||
return sb;
|
||||
}
|
||||
|
||||
// As well as the standard btree checks, we build up a set of what
|
||||
// devices having mappings defined, which can later be cross
|
||||
// referenced with the details tree. A separate block_counter is
|
||||
@ -191,7 +117,7 @@ thin::maybe_address
|
||||
thin::lookup(block_address thin_block)
|
||||
{
|
||||
uint64_t key[2] = {dev_, thin_block};
|
||||
return metadata_->mappings_.lookup(key);
|
||||
return metadata_->md_->mappings_.lookup(key);
|
||||
}
|
||||
|
||||
void
|
||||
@ -201,33 +127,33 @@ thin::insert(block_address thin_block, block_address data_block)
|
||||
block_time bt;
|
||||
bt.block_ = data_block;
|
||||
bt.time_ = 0; // FIXME: use current time.
|
||||
return metadata_->mappings_.insert(key, bt);
|
||||
return metadata_->md_->mappings_.insert(key, bt);
|
||||
}
|
||||
|
||||
void
|
||||
thin::remove(block_address thin_block)
|
||||
{
|
||||
uint64_t key[2] = {dev_, thin_block};
|
||||
metadata_->mappings_.remove(key);
|
||||
metadata_->md_->mappings_.remove(key);
|
||||
}
|
||||
|
||||
void
|
||||
thin::set_snapshot_time(uint32_t time)
|
||||
{
|
||||
uint64_t key[1] = { dev_ };
|
||||
optional<device_details> mdetail = metadata_->details_.lookup(key);
|
||||
optional<device_details> mdetail = metadata_->md_->details_.lookup(key);
|
||||
if (!mdetail)
|
||||
throw runtime_error("no such device");
|
||||
|
||||
mdetail->snapshotted_time_ = time;
|
||||
metadata_->details_.insert(key, *mdetail);
|
||||
metadata_->md_->details_.insert(key, *mdetail);
|
||||
}
|
||||
|
||||
block_address
|
||||
thin::get_mapped_blocks() const
|
||||
{
|
||||
uint64_t key[1] = { dev_ };
|
||||
optional<device_details> mdetail = metadata_->details_.lookup(key);
|
||||
optional<device_details> mdetail = metadata_->md_->details_.lookup(key);
|
||||
if (!mdetail)
|
||||
throw runtime_error("no such device");
|
||||
|
||||
@ -238,32 +164,19 @@ void
|
||||
thin::set_mapped_blocks(block_address count)
|
||||
{
|
||||
uint64_t key[1] = { dev_ };
|
||||
optional<device_details> mdetail = metadata_->details_.lookup(key);
|
||||
optional<device_details> mdetail = metadata_->md_->details_.lookup(key);
|
||||
if (!mdetail)
|
||||
throw runtime_error("no such device");
|
||||
|
||||
mdetail->mapped_blocks_ = count;
|
||||
metadata_->details_.insert(key, *mdetail);
|
||||
metadata_->md_->details_.insert(key, *mdetail);
|
||||
}
|
||||
|
||||
//--------------------------------
|
||||
|
||||
metadata::metadata(std::string const &dev_path)
|
||||
: tm_(open_tm(dev_path)),
|
||||
sb_(read_superblock(tm_->get_bm())),
|
||||
metadata_sm_(open_metadata_sm(tm_, static_cast<void *>(&sb_.metadata_space_map_root_))),
|
||||
data_sm_(open_disk_sm(tm_, static_cast<void *>(&sb_.data_space_map_root_))),
|
||||
details_(tm_, sb_.device_details_root_, device_details_traits::ref_counter()),
|
||||
mappings_top_level_(tm_, sb_.data_mapping_root_, mtree_ref_counter(tm_)),
|
||||
mappings_(tm_, sb_.data_mapping_root_, block_time_ref_counter(data_sm_))
|
||||
metadata::metadata(metadata_ll::ptr md)
|
||||
: md_(md)
|
||||
{
|
||||
#if 0
|
||||
::memset(&sb_, 0, sizeof(sb_));
|
||||
sb_.data_mapping_root_ = mappings_.get_root();
|
||||
sb_.device_details_root_ = details_.get_root();
|
||||
sb_.metadata_block_size_ = MD_BLOCK_SIZE;
|
||||
sb_.metadata_nr_blocks_ = tm_->get_bm()->get_nr_blocks();
|
||||
#endif
|
||||
}
|
||||
|
||||
metadata::~metadata()
|
||||
@ -271,17 +184,6 @@ metadata::~metadata()
|
||||
|
||||
}
|
||||
|
||||
void
|
||||
metadata::commit()
|
||||
{
|
||||
sb_.data_mapping_root_ = mappings_.get_root();
|
||||
sb_.device_details_root_ = details_.get_root();
|
||||
|
||||
write_ref superblock = tm_->get_bm()->superblock(SUPERBLOCK_LOCATION);
|
||||
superblock_disk *disk = reinterpret_cast<superblock_disk *>(superblock.data());
|
||||
superblock_traits::pack(sb_, *disk);
|
||||
}
|
||||
|
||||
void
|
||||
metadata::create_thin(thin_dev_t dev)
|
||||
{
|
||||
@ -290,9 +192,9 @@ metadata::create_thin(thin_dev_t dev)
|
||||
if (device_exists(dev))
|
||||
throw std::runtime_error("Device already exists");
|
||||
|
||||
single_mapping_tree::ptr new_tree(new single_mapping_tree(tm_, block_time_ref_counter(data_sm_)));
|
||||
mappings_top_level_.insert(key, new_tree->get_root());
|
||||
mappings_.set_root(mappings_top_level_.get_root()); // FIXME: ugly
|
||||
single_mapping_tree::ptr new_tree(new single_mapping_tree(md_->tm_, block_time_ref_counter(md_->data_sm_)));
|
||||
md_->mappings_top_level_.insert(key, new_tree->get_root());
|
||||
md_->mappings_.set_root(md_->mappings_top_level_.get_root()); // FIXME: ugly
|
||||
}
|
||||
|
||||
void
|
||||
@ -301,23 +203,23 @@ metadata::create_snap(thin_dev_t dev, thin_dev_t origin)
|
||||
uint64_t snap_key[1] = {dev};
|
||||
uint64_t origin_key[1] = {origin};
|
||||
|
||||
optional<uint64_t> mtree_root = mappings_top_level_.lookup(origin_key);
|
||||
optional<uint64_t> mtree_root = md_->mappings_top_level_.lookup(origin_key);
|
||||
if (!mtree_root)
|
||||
throw std::runtime_error("unknown origin");
|
||||
|
||||
single_mapping_tree otree(tm_, *mtree_root,
|
||||
block_time_ref_counter(data_sm_));
|
||||
single_mapping_tree otree(md_->tm_, *mtree_root,
|
||||
block_time_ref_counter(md_->data_sm_));
|
||||
|
||||
single_mapping_tree::ptr clone(otree.clone());
|
||||
mappings_top_level_.insert(snap_key, clone->get_root());
|
||||
mappings_.set_root(mappings_top_level_.get_root()); // FIXME: ugly
|
||||
md_->mappings_top_level_.insert(snap_key, clone->get_root());
|
||||
md_->mappings_.set_root(md_->mappings_top_level_.get_root()); // FIXME: ugly
|
||||
|
||||
sb_.time_++;
|
||||
md_->sb_.time_++;
|
||||
|
||||
thin::ptr o = open_thin(origin);
|
||||
thin::ptr s = open_thin(dev);
|
||||
o->set_snapshot_time(sb_.time_);
|
||||
s->set_snapshot_time(sb_.time_);
|
||||
o->set_snapshot_time(md_->sb_.time_);
|
||||
s->set_snapshot_time(md_->sb_.time_);
|
||||
s->set_mapped_blocks(o->get_mapped_blocks());
|
||||
}
|
||||
|
||||
@ -325,62 +227,62 @@ void
|
||||
metadata::del(thin_dev_t dev)
|
||||
{
|
||||
uint64_t key[1] = {dev};
|
||||
mappings_top_level_.remove(key);
|
||||
md_->mappings_top_level_.remove(key);
|
||||
}
|
||||
|
||||
void
|
||||
metadata::set_transaction_id(uint64_t id)
|
||||
{
|
||||
sb_.trans_id_ = id;
|
||||
md_->sb_.trans_id_ = id;
|
||||
}
|
||||
|
||||
uint64_t
|
||||
metadata::get_transaction_id() const
|
||||
{
|
||||
return sb_.trans_id_;
|
||||
return md_->sb_.trans_id_;
|
||||
}
|
||||
|
||||
block_address
|
||||
metadata::get_held_root() const
|
||||
{
|
||||
return sb_.held_root_;
|
||||
return md_->sb_.held_root_;
|
||||
}
|
||||
|
||||
block_address
|
||||
metadata::alloc_data_block()
|
||||
{
|
||||
return data_sm_->new_block();
|
||||
return md_->data_sm_->new_block();
|
||||
}
|
||||
|
||||
void
|
||||
metadata::free_data_block(block_address b)
|
||||
{
|
||||
data_sm_->dec(b);
|
||||
md_->data_sm_->dec(b);
|
||||
}
|
||||
|
||||
block_address
|
||||
metadata::get_nr_free_data_blocks() const
|
||||
{
|
||||
return data_sm_->get_nr_free();
|
||||
return md_->data_sm_->get_nr_free();
|
||||
}
|
||||
|
||||
sector_t
|
||||
metadata::get_data_block_size() const
|
||||
{
|
||||
return sb_.data_block_size_;
|
||||
return md_->sb_.data_block_size_;
|
||||
}
|
||||
|
||||
block_address
|
||||
metadata::get_data_dev_size() const
|
||||
{
|
||||
return data_sm_->get_nr_blocks();
|
||||
return md_->data_sm_->get_nr_blocks();
|
||||
}
|
||||
|
||||
thin::ptr
|
||||
metadata::open_thin(thin_dev_t dev)
|
||||
{
|
||||
uint64_t key[1] = {dev};
|
||||
optional<device_details> mdetails = details_.lookup(key);
|
||||
optional<device_details> mdetails = md_->details_.lookup(key);
|
||||
if (!mdetails)
|
||||
throw runtime_error("no such device");
|
||||
|
||||
@ -393,7 +295,7 @@ bool
|
||||
metadata::device_exists(thin_dev_t dev) const
|
||||
{
|
||||
uint64_t key[1] = {dev};
|
||||
return details_.lookup(key);
|
||||
return md_->details_.lookup(key);
|
||||
}
|
||||
|
||||
namespace {
|
||||
@ -440,11 +342,11 @@ metadata::check()
|
||||
|
||||
mapping_validator::ptr mv(new mapping_validator(metadata_counter,
|
||||
data_counter));
|
||||
mappings_.visit(mv);
|
||||
md_->mappings_.visit(mv);
|
||||
set<uint64_t> const &mapped_devs = mv->get_devices();
|
||||
|
||||
details_validator::ptr dv(new details_validator(metadata_counter));
|
||||
details_.visit(dv);
|
||||
md_->details_.visit(dv);
|
||||
set<uint64_t> const &details_devs = dv->get_devices();
|
||||
|
||||
for (set<uint64_t>::const_iterator it = mapped_devs.begin(); it != mapped_devs.end(); ++it)
|
||||
@ -456,12 +358,12 @@ metadata::check()
|
||||
}
|
||||
|
||||
metadata_counter.inc(SUPERBLOCK_LOCATION);
|
||||
metadata_sm_->check(metadata_counter);
|
||||
data_sm_->check(metadata_counter);
|
||||
md_->metadata_sm_->check(metadata_counter);
|
||||
md_->data_sm_->check(metadata_counter);
|
||||
errors->add_child(check_ref_counts("Errors in metadata block reference counts",
|
||||
metadata_counter, metadata_sm_));
|
||||
metadata_counter, md_->metadata_sm_));
|
||||
errors->add_child(check_ref_counts("Errors in data block reference counts",
|
||||
data_counter, data_sm_));
|
||||
data_counter, md_->data_sm_));
|
||||
|
||||
return (errors->get_children().size() > 0) ?
|
||||
optional<error_set::ptr>(errors) :
|
||||
|
131
metadata.h
131
metadata.h
@ -1,14 +1,9 @@
|
||||
#ifndef MULTISNAP_METADATA_H
|
||||
#define MULTISNAP_METADATA_H
|
||||
|
||||
#include "block.h"
|
||||
#include "btree.h"
|
||||
#include "emitter.h"
|
||||
#include "endian_utils.h"
|
||||
#include "error_set.h"
|
||||
#include "metadata_disk_structures.h"
|
||||
#include "space_map_disk.h"
|
||||
#include "transaction_manager.h"
|
||||
#include "metadata_ll.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
@ -17,105 +12,6 @@
|
||||
//----------------------------------------------------------------
|
||||
|
||||
namespace thin_provisioning {
|
||||
// FIXME: don't use namespaces in a header
|
||||
using namespace base;
|
||||
using namespace persistent_data;
|
||||
|
||||
typedef uint64_t sector_t;
|
||||
typedef uint32_t thin_dev_t;
|
||||
|
||||
//------------------------------------------------
|
||||
|
||||
class space_map_ref_counter {
|
||||
public:
|
||||
space_map_ref_counter(space_map::ptr sm)
|
||||
: sm_(sm) {
|
||||
}
|
||||
|
||||
void inc(block_address b) {
|
||||
sm_->inc(b);
|
||||
}
|
||||
|
||||
void dec(block_address b) {
|
||||
sm_->dec(b);
|
||||
}
|
||||
|
||||
private:
|
||||
space_map::ptr sm_;
|
||||
};
|
||||
|
||||
struct block_time {
|
||||
uint64_t block_;
|
||||
uint32_t time_;
|
||||
};
|
||||
|
||||
class block_time_ref_counter {
|
||||
public:
|
||||
block_time_ref_counter(space_map::ptr sm)
|
||||
: sm_(sm) {
|
||||
}
|
||||
|
||||
void inc(block_time bt) {
|
||||
sm_->inc(bt.block_);
|
||||
}
|
||||
|
||||
void dec(block_time bt) {
|
||||
sm_->dec(bt.block_);
|
||||
}
|
||||
|
||||
private:
|
||||
space_map::ptr sm_;
|
||||
};
|
||||
|
||||
struct block_traits {
|
||||
typedef base::__le64 disk_type;
|
||||
typedef block_time value_type;
|
||||
typedef block_time_ref_counter ref_counter;
|
||||
|
||||
static void unpack(disk_type const &disk, value_type &value) {
|
||||
uint64_t v = to_cpu<uint64_t>(disk);
|
||||
value.block_ = v >> 24;
|
||||
value.time_ = v & ((1 << 24) - 1);
|
||||
}
|
||||
|
||||
static void pack(value_type const &value, disk_type &disk) {
|
||||
uint64_t v = (value.block_ << 24) | value.time_;
|
||||
disk = base::to_disk<base::__le64>(v);
|
||||
}
|
||||
};
|
||||
|
||||
//------------------------------------------------
|
||||
|
||||
class mtree_ref_counter {
|
||||
public:
|
||||
mtree_ref_counter(transaction_manager::ptr tm)
|
||||
: tm_(tm) {
|
||||
}
|
||||
|
||||
void inc(block_address b) {
|
||||
}
|
||||
|
||||
void dec(block_address b) {
|
||||
}
|
||||
|
||||
private:
|
||||
transaction_manager::ptr tm_;
|
||||
};
|
||||
|
||||
struct mtree_traits {
|
||||
typedef base::__le64 disk_type;
|
||||
typedef uint64_t value_type;
|
||||
typedef mtree_ref_counter ref_counter;
|
||||
|
||||
static void unpack(disk_type const &disk, value_type &value) {
|
||||
value = base::to_cpu<uint64_t>(disk);
|
||||
}
|
||||
|
||||
static void pack(value_type const &value, disk_type &disk) {
|
||||
disk = base::to_disk<base::__le64>(value);
|
||||
}
|
||||
};
|
||||
|
||||
class metadata;
|
||||
class thin {
|
||||
public:
|
||||
@ -143,14 +39,10 @@ namespace thin_provisioning {
|
||||
class metadata {
|
||||
public:
|
||||
typedef boost::shared_ptr<metadata> ptr;
|
||||
typedef block_manager<>::read_ref read_ref;
|
||||
typedef block_manager<>::write_ref write_ref;
|
||||
|
||||
metadata(std::string const &dev_path);
|
||||
metadata(metadata_ll::ptr md);
|
||||
~metadata();
|
||||
|
||||
void commit();
|
||||
|
||||
void create_thin(thin_dev_t dev);
|
||||
void create_snap(thin_dev_t dev, thin_dev_t origin);
|
||||
void del(thin_dev_t);
|
||||
@ -170,9 +62,11 @@ namespace thin_provisioning {
|
||||
|
||||
thin::ptr open_thin(thin_dev_t);
|
||||
|
||||
// FIXME: split out into a separate interface
|
||||
// Validation
|
||||
boost::optional<persistent_data::error_set::ptr> check();
|
||||
|
||||
// FIXME: split out into a separate interface
|
||||
// Dumping metadata
|
||||
void dump(emitter::ptr e);
|
||||
|
||||
@ -181,22 +75,7 @@ namespace thin_provisioning {
|
||||
|
||||
bool device_exists(thin_dev_t dev) const;
|
||||
|
||||
typedef persistent_data::transaction_manager::ptr tm_ptr;
|
||||
|
||||
typedef persistent_data::btree<1, device_details_traits> detail_tree;
|
||||
typedef persistent_data::btree<1, mtree_traits> dev_tree;
|
||||
typedef persistent_data::btree<2, block_traits> mapping_tree;
|
||||
typedef persistent_data::btree<1, block_traits> single_mapping_tree;
|
||||
|
||||
// Declaration order is important here
|
||||
tm_ptr tm_;
|
||||
superblock sb_;
|
||||
|
||||
checked_space_map::ptr metadata_sm_;
|
||||
checked_space_map::ptr data_sm_;
|
||||
detail_tree details_;
|
||||
dev_tree mappings_top_level_;
|
||||
mapping_tree mappings_;
|
||||
metadata_ll::ptr md_;
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -123,11 +123,11 @@ namespace {
|
||||
void
|
||||
metadata::dump(emitter::ptr e)
|
||||
{
|
||||
e->begin_superblock("", sb_.time_, sb_.trans_id_, sb_.data_block_size_);
|
||||
e->begin_superblock("", md_->sb_.time_, md_->sb_.trans_id_, md_->sb_.data_block_size_);
|
||||
|
||||
details_extractor::ptr de(new details_extractor);
|
||||
|
||||
details_.visit(de);
|
||||
md_->details_.visit(de);
|
||||
map<uint64_t, device_details> const &devs = de->get_devices();
|
||||
|
||||
map<uint64_t, device_details>::const_iterator it, end = devs.end();
|
||||
@ -141,8 +141,8 @@ metadata::dump(emitter::ptr e)
|
||||
dd.creation_time_,
|
||||
dd.snapshotted_time_);
|
||||
|
||||
mappings_extractor::ptr me(new mappings_extractor(dev_id, e, metadata_sm_, data_sm_));
|
||||
mappings_.visit(me);
|
||||
mappings_extractor::ptr me(new mappings_extractor(dev_id, e, md_->metadata_sm_, md_->data_sm_));
|
||||
md_->mappings_.visit(me);
|
||||
|
||||
e->end_device();
|
||||
}
|
||||
|
115
metadata_ll.cc
Normal file
115
metadata_ll.cc
Normal file
@ -0,0 +1,115 @@
|
||||
#include "metadata_ll.h"
|
||||
|
||||
#include "math_utils.h"
|
||||
#include "space_map_disk.h"
|
||||
#include "core_map.h"
|
||||
|
||||
#include <linux/fs.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
using namespace thin_provisioning;
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
namespace {
|
||||
uint32_t const SUPERBLOCK_MAGIC = 27022010;
|
||||
uint32_t const VERSION = 1;
|
||||
unsigned const METADATA_CACHE_SIZE = 1024;
|
||||
unsigned const SECTOR_TO_BLOCK_SHIFT = 3;
|
||||
|
||||
block_address get_nr_blocks(string const &path) {
|
||||
struct stat info;
|
||||
block_address nr_blocks;
|
||||
|
||||
int r = ::stat(path.c_str(), &info);
|
||||
if (r)
|
||||
throw runtime_error("Couldn't stat dev path");
|
||||
|
||||
if (S_ISREG(info.st_mode))
|
||||
nr_blocks = div_down<block_address>(info.st_size, MD_BLOCK_SIZE);
|
||||
|
||||
else if (S_ISBLK(info.st_mode)) {
|
||||
// To get the size of a block device we need to
|
||||
// open it, and then make an ioctl call.
|
||||
int fd = ::open(path.c_str(), O_RDONLY);
|
||||
if (fd < 0)
|
||||
throw runtime_error("couldn't open block device to ascertain size");
|
||||
|
||||
r = ::ioctl(fd, BLKGETSIZE64, &nr_blocks);
|
||||
if (r) {
|
||||
::close(fd);
|
||||
throw runtime_error("ioctl BLKGETSIZE64 failed");
|
||||
}
|
||||
::close(fd);
|
||||
nr_blocks = div_down<block_address>(nr_blocks, MD_BLOCK_SIZE);
|
||||
} else
|
||||
throw runtime_error("bad path");
|
||||
|
||||
return nr_blocks;
|
||||
}
|
||||
|
||||
transaction_manager::ptr
|
||||
open_tm(string const &dev_path) {
|
||||
block_address nr_blocks = get_nr_blocks(dev_path);
|
||||
block_manager<>::ptr bm(new block_manager<>(dev_path, nr_blocks, 8));
|
||||
space_map::ptr sm(new core_map(nr_blocks));
|
||||
transaction_manager::ptr tm(new transaction_manager(bm, sm));
|
||||
return tm;
|
||||
}
|
||||
|
||||
superblock read_superblock(block_manager<>::ptr bm) {
|
||||
superblock sb;
|
||||
block_manager<>::read_ref r = bm->read_lock(SUPERBLOCK_LOCATION);
|
||||
superblock_disk const *sbd = reinterpret_cast<superblock_disk const *>(&r.data());
|
||||
|
||||
crc32c sum(160774);
|
||||
sum.append(&sbd->flags_, MD_BLOCK_SIZE - sizeof(uint32_t));
|
||||
if (sum.get_sum() != to_cpu<uint32_t>(sbd->csum_)) {
|
||||
ostringstream out;
|
||||
out << "bad checksum in superblock, calculated "
|
||||
<< sum.get_sum()
|
||||
<< ", superblock contains " << to_cpu<uint32_t>(sbd->csum_);
|
||||
throw runtime_error(out.str());
|
||||
}
|
||||
|
||||
superblock_traits::unpack(*sbd, sb);
|
||||
return sb;
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
metadata_ll::metadata_ll(std::string const &dev_path)
|
||||
: tm_(open_tm(dev_path)),
|
||||
sb_(read_superblock(tm_->get_bm())),
|
||||
metadata_sm_(open_metadata_sm(tm_, static_cast<void *>(&sb_.metadata_space_map_root_))),
|
||||
data_sm_(open_disk_sm(tm_, static_cast<void *>(&sb_.data_space_map_root_))),
|
||||
details_(tm_, sb_.device_details_root_, device_details_traits::ref_counter()),
|
||||
mappings_top_level_(tm_, sb_.data_mapping_root_, mtree_ref_counter(tm_)),
|
||||
mappings_(tm_, sb_.data_mapping_root_, block_time_ref_counter(data_sm_))
|
||||
{
|
||||
}
|
||||
|
||||
#if 0
|
||||
::memset(&sb_, 0, sizeof(sb_));
|
||||
sb_.data_mapping_root_ = mappings_.get_root();
|
||||
sb_.device_details_root_ = details_.get_root();
|
||||
sb_.metadata_block_size_ = MD_BLOCK_SIZE;
|
||||
sb_.metadata_nr_blocks_ = tm_->get_bm()->get_nr_blocks();
|
||||
#endif
|
||||
|
||||
void
|
||||
metadata_ll::commit()
|
||||
{
|
||||
sb_.data_mapping_root_ = mappings_.get_root();
|
||||
sb_.device_details_root_ = details_.get_root();
|
||||
|
||||
write_ref superblock = tm_->get_bm()->superblock(SUPERBLOCK_LOCATION);
|
||||
superblock_disk *disk = reinterpret_cast<superblock_disk *>(superblock.data());
|
||||
superblock_traits::pack(sb_, *disk);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
148
metadata_ll.h
Normal file
148
metadata_ll.h
Normal file
@ -0,0 +1,148 @@
|
||||
#ifndef METADATA_LL_H
|
||||
#define METADATA_LL_H
|
||||
|
||||
#include "block.h"
|
||||
#include "btree.h"
|
||||
#include "endian_utils.h"
|
||||
#include "metadata_disk_structures.h"
|
||||
#include "space_map_disk.h"
|
||||
#include "transaction_manager.h"
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
namespace thin_provisioning {
|
||||
// FIXME: don't use namespaces in a header
|
||||
using namespace base;
|
||||
using namespace persistent_data;
|
||||
|
||||
block_address const SUPERBLOCK_LOCATION = 0;
|
||||
|
||||
typedef uint64_t sector_t;
|
||||
typedef uint32_t thin_dev_t;
|
||||
|
||||
//------------------------------------------------
|
||||
|
||||
class space_map_ref_counter {
|
||||
public:
|
||||
space_map_ref_counter(space_map::ptr sm)
|
||||
: sm_(sm) {
|
||||
}
|
||||
|
||||
void inc(block_address b) {
|
||||
sm_->inc(b);
|
||||
}
|
||||
|
||||
void dec(block_address b) {
|
||||
sm_->dec(b);
|
||||
}
|
||||
|
||||
private:
|
||||
space_map::ptr sm_;
|
||||
};
|
||||
|
||||
struct block_time {
|
||||
uint64_t block_;
|
||||
uint32_t time_;
|
||||
};
|
||||
|
||||
class block_time_ref_counter {
|
||||
public:
|
||||
block_time_ref_counter(space_map::ptr sm)
|
||||
: sm_(sm) {
|
||||
}
|
||||
|
||||
void inc(block_time bt) {
|
||||
sm_->inc(bt.block_);
|
||||
}
|
||||
|
||||
void dec(block_time bt) {
|
||||
sm_->dec(bt.block_);
|
||||
}
|
||||
|
||||
private:
|
||||
space_map::ptr sm_;
|
||||
};
|
||||
|
||||
struct block_traits {
|
||||
typedef base::__le64 disk_type;
|
||||
typedef block_time value_type;
|
||||
typedef block_time_ref_counter ref_counter;
|
||||
|
||||
static void unpack(disk_type const &disk, value_type &value) {
|
||||
uint64_t v = to_cpu<uint64_t>(disk);
|
||||
value.block_ = v >> 24;
|
||||
value.time_ = v & ((1 << 24) - 1);
|
||||
}
|
||||
|
||||
static void pack(value_type const &value, disk_type &disk) {
|
||||
uint64_t v = (value.block_ << 24) | value.time_;
|
||||
disk = base::to_disk<base::__le64>(v);
|
||||
}
|
||||
};
|
||||
|
||||
//------------------------------------------------
|
||||
|
||||
class mtree_ref_counter {
|
||||
public:
|
||||
mtree_ref_counter(transaction_manager::ptr tm)
|
||||
: tm_(tm) {
|
||||
}
|
||||
|
||||
void inc(block_address b) {
|
||||
}
|
||||
|
||||
void dec(block_address b) {
|
||||
}
|
||||
|
||||
private:
|
||||
transaction_manager::ptr tm_;
|
||||
};
|
||||
|
||||
struct mtree_traits {
|
||||
typedef base::__le64 disk_type;
|
||||
typedef uint64_t value_type;
|
||||
typedef mtree_ref_counter ref_counter;
|
||||
|
||||
static void unpack(disk_type const &disk, value_type &value) {
|
||||
value = base::to_cpu<uint64_t>(disk);
|
||||
}
|
||||
|
||||
static void pack(value_type const &value, disk_type &disk) {
|
||||
disk = base::to_disk<base::__le64>(value);
|
||||
}
|
||||
};
|
||||
|
||||
// FIXME: should these be in a sub-namespace?
|
||||
typedef persistent_data::transaction_manager::ptr tm_ptr;
|
||||
typedef persistent_data::btree<1, device_details_traits> detail_tree;
|
||||
typedef persistent_data::btree<1, mtree_traits> dev_tree;
|
||||
typedef persistent_data::btree<2, block_traits> mapping_tree;
|
||||
typedef persistent_data::btree<1, block_traits> single_mapping_tree;
|
||||
|
||||
// The tools require different interfaces onto the metadata than
|
||||
// the in kernel driver. This class gives access to the low-level
|
||||
// implementation of metadata. Implement more specific interfaces
|
||||
// on top of this.
|
||||
struct metadata_ll {
|
||||
metadata_ll(std::string const &dev_path);
|
||||
|
||||
void commit();
|
||||
|
||||
typedef block_manager<>::read_ref read_ref;
|
||||
typedef block_manager<>::write_ref write_ref;
|
||||
typedef boost::shared_ptr<metadata_ll> ptr;
|
||||
|
||||
tm_ptr tm_;
|
||||
superblock sb_;
|
||||
|
||||
checked_space_map::ptr metadata_sm_;
|
||||
checked_space_map::ptr data_sm_;
|
||||
detail_tree details_;
|
||||
dev_tree mappings_top_level_;
|
||||
mapping_tree mappings_;
|
||||
};
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
#endif
|
@ -16,7 +16,8 @@ namespace po = boost::program_options;
|
||||
|
||||
namespace {
|
||||
void dump(string const &path, string const &format) {
|
||||
metadata md(path);
|
||||
metadata_ll::ptr ll(new metadata_ll(path));
|
||||
metadata md(ll);
|
||||
emitter::ptr e;
|
||||
|
||||
if (format == "xml")
|
||||
|
@ -9,7 +9,8 @@ using namespace thin_provisioning;
|
||||
|
||||
namespace {
|
||||
int check(string const &path) {
|
||||
metadata md(path);
|
||||
metadata_ll::ptr ll(new metadata_ll(path));
|
||||
metadata md(ll);
|
||||
|
||||
optional<error_set::ptr> maybe_errors = md.check();
|
||||
if (maybe_errors) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user