2011-10-28 16:55:06 +05:30
|
|
|
#include "metadata.h"
|
2011-10-28 16:01:00 +05:30
|
|
|
|
|
|
|
#include "math_utils.h"
|
2011-10-28 18:58:59 +05:30
|
|
|
#include "space_map_core.h"
|
2011-11-09 15:51:25 +05:30
|
|
|
#include "space_map_disk.h"
|
2011-10-28 16:01:00 +05:30
|
|
|
|
|
|
|
#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;
|
2011-11-03 20:14:00 +05:30
|
|
|
uint32_t const SUPERBLOCK_CSUM_SEED = 160774;
|
|
|
|
|
|
|
|
struct superblock_validator : public block_manager<>::validator {
|
|
|
|
virtual void check(block_manager<>::const_buffer &b, block_address location) const {
|
|
|
|
superblock_disk const *sbd = reinterpret_cast<superblock_disk const *>(&b);
|
|
|
|
crc32c sum(SUPERBLOCK_CSUM_SEED);
|
|
|
|
sum.append(&sbd->flags_, MD_BLOCK_SIZE - sizeof(uint32_t));
|
|
|
|
if (sum.get_sum() != to_cpu<uint32_t>(sbd->csum_))
|
|
|
|
throw runtime_error("bad checksum in superblock");
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void prepare(block_manager<>::buffer &b, block_address location) const {
|
|
|
|
superblock_disk *sbd = reinterpret_cast<superblock_disk *>(&b);
|
|
|
|
crc32c sum(SUPERBLOCK_CSUM_SEED);
|
|
|
|
sum.append(&sbd->flags_, MD_BLOCK_SIZE - sizeof(uint32_t));
|
|
|
|
sbd->csum_ = to_disk<base::__le32>(sum.get_sum());
|
|
|
|
}
|
|
|
|
};
|
2011-10-28 16:01:00 +05:30
|
|
|
|
2011-11-03 20:14:00 +05:30
|
|
|
block_address
|
|
|
|
get_nr_blocks(string const &path) {
|
2011-10-28 16:01:00 +05:30
|
|
|
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
|
2011-11-03 20:14:00 +05:30
|
|
|
open_tm(string const &dev_path, bool writeable) {
|
2011-10-28 16:01:00 +05:30
|
|
|
block_address nr_blocks = get_nr_blocks(dev_path);
|
2011-11-03 20:14:00 +05:30
|
|
|
block_manager<>::ptr bm(new block_manager<>(dev_path, nr_blocks, 1, writeable));
|
2011-10-28 16:01:00 +05:30
|
|
|
space_map::ptr sm(new core_map(nr_blocks));
|
2011-11-03 20:14:00 +05:30
|
|
|
sm->inc(SUPERBLOCK_LOCATION);
|
2011-10-28 16:01:00 +05:30
|
|
|
transaction_manager::ptr tm(new transaction_manager(bm, sm));
|
|
|
|
return tm;
|
|
|
|
}
|
|
|
|
|
2011-11-03 20:14:00 +05:30
|
|
|
superblock
|
|
|
|
read_superblock(block_manager<>::ptr bm) {
|
2011-10-28 16:01:00 +05:30
|
|
|
superblock sb;
|
2011-11-03 20:14:00 +05:30
|
|
|
block_manager<>::read_ref r = bm->read_lock(SUPERBLOCK_LOCATION,
|
|
|
|
mk_validator(new superblock_validator));
|
2011-10-28 16:01:00 +05:30
|
|
|
superblock_disk const *sbd = reinterpret_cast<superblock_disk const *>(&r.data());
|
|
|
|
superblock_traits::unpack(*sbd, sb);
|
|
|
|
return sb;
|
|
|
|
}
|
2011-11-03 20:14:00 +05:30
|
|
|
|
|
|
|
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 16:01:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2011-11-03 20:14:00 +05:30
|
|
|
metadata::metadata(std::string const &dev_path, open_type ot,
|
|
|
|
sector_t data_block_size, block_address nr_data_blocks)
|
2011-10-28 16:01:00 +05:30
|
|
|
{
|
2011-11-03 20:14:00 +05:30
|
|
|
switch (ot) {
|
|
|
|
case OPEN:
|
|
|
|
tm_ = open_tm(dev_path, false);
|
|
|
|
sb_ = read_superblock(tm_->get_bm());
|
|
|
|
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_));
|
|
|
|
details_ = detail_tree::ptr(new detail_tree(tm_, sb_.device_details_root_, device_details_traits::ref_counter()));
|
|
|
|
mappings_top_level_ = dev_tree::ptr(new dev_tree(tm_, sb_.data_mapping_root_, mtree_ref_counter(tm_)));
|
|
|
|
mappings_ = mapping_tree::ptr(new mapping_tree(tm_, sb_.data_mapping_root_, block_time_ref_counter(data_sm_)));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CREATE:
|
|
|
|
tm_ = open_tm(dev_path, true);
|
|
|
|
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);
|
|
|
|
tm_->set_sm(metadata_sm_);
|
|
|
|
|
|
|
|
data_sm_ = create_disk_sm(tm_, nr_data_blocks);
|
|
|
|
details_ = detail_tree::ptr(new detail_tree(tm_, device_details_traits::ref_counter()));
|
|
|
|
mappings_ = mapping_tree::ptr(new mapping_tree(tm_, block_time_ref_counter(data_sm_)));
|
|
|
|
mappings_top_level_ = dev_tree::ptr(new dev_tree(tm_, mappings_->get_root(), mtree_ref_counter(tm_)));
|
|
|
|
|
|
|
|
::memset(&sb_, 0, sizeof(sb_));
|
|
|
|
sb_.magic_ = SUPERBLOCK_MAGIC;
|
2011-11-07 16:04:43 +05:30
|
|
|
sb_.version_ = 1;
|
2011-11-03 20:14:00 +05:30
|
|
|
sb_.data_mapping_root_ = mappings_->get_root();
|
|
|
|
sb_.device_details_root_ = details_->get_root();
|
|
|
|
sb_.data_block_size_ = data_block_size;
|
|
|
|
sb_.metadata_block_size_ = MD_BLOCK_SIZE;
|
|
|
|
sb_.metadata_nr_blocks_ = tm_->get_bm()->get_nr_blocks();
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2011-10-28 16:01:00 +05:30
|
|
|
}
|
|
|
|
|
2011-11-03 20:14:00 +05:30
|
|
|
namespace {
|
|
|
|
void print_superblock(superblock const &sb) {
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
cerr << "superblock " << sb.csum_ << endl
|
|
|
|
<< "flags " << sb.flags_ << endl
|
|
|
|
<< "blocknr " << sb.blocknr_ << endl
|
|
|
|
<< "transaction id " << sb.trans_id_ << endl
|
|
|
|
<< "data mapping root " << sb.data_mapping_root_ << endl
|
|
|
|
<< "details root " << sb.device_details_root_ << endl
|
|
|
|
<< "data block size " << sb.data_block_size_ << endl
|
|
|
|
<< "metadata block size " << sb.metadata_block_size_ << endl
|
|
|
|
<< "metadata nr blocks " << sb.metadata_nr_blocks_ << endl
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
2011-10-28 16:01:00 +05:30
|
|
|
|
|
|
|
void
|
2011-10-28 16:55:06 +05:30
|
|
|
metadata::commit()
|
2011-10-28 16:01:00 +05:30
|
|
|
{
|
2011-10-28 20:58:24 +05:30
|
|
|
sb_.data_mapping_root_ = mappings_->get_root();
|
|
|
|
sb_.device_details_root_ = details_->get_root();
|
2011-10-28 16:01:00 +05:30
|
|
|
|
2011-11-03 20:14:00 +05:30
|
|
|
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_));
|
|
|
|
print_superblock(sb_);
|
2011-10-28 18:56:06 +05:30
|
|
|
|
2011-11-03 20:14:00 +05:30
|
|
|
superblock_validator v;
|
|
|
|
write_ref superblock = tm_->get_bm()->superblock_zero(SUPERBLOCK_LOCATION,
|
|
|
|
mk_validator(new superblock_validator()));
|
2011-10-28 16:01:00 +05:30
|
|
|
superblock_disk *disk = reinterpret_cast<superblock_disk *>(superblock.data());
|
|
|
|
superblock_traits::pack(sb_, *disk);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|