2013-09-11 16:10:46 +05:30
|
|
|
#include "caching/metadata.h"
|
2013-09-19 18:15:56 +05:30
|
|
|
#include "caching/superblock.h"
|
2013-09-18 17:30:26 +05:30
|
|
|
#include "persistent-data/space-maps/core.h"
|
2013-09-11 16:10:46 +05:30
|
|
|
|
|
|
|
using namespace caching;
|
2016-09-30 20:51:20 +05:30
|
|
|
namespace pd = persistent_data;
|
2013-09-11 16:10:46 +05:30
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2013-09-18 17:30:26 +05:30
|
|
|
namespace {
|
|
|
|
// FIXME: duplication
|
|
|
|
transaction_manager::ptr
|
2020-04-30 19:00:01 +05:30
|
|
|
open_tm(block_manager::ptr bm) {
|
2017-09-21 14:52:38 +05:30
|
|
|
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));
|
2013-09-18 17:30:26 +05:30
|
|
|
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++) {
|
|
|
|
uint32_t count = rhs->get_count(b);
|
|
|
|
if (count > 0)
|
|
|
|
lhs->set_count(b, rhs->get_count(b));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2020-04-30 19:00:01 +05:30
|
|
|
metadata::metadata(block_manager::ptr bm, open_type ot, unsigned metadata_version)
|
2013-09-11 16:10:46 +05:30
|
|
|
{
|
2013-09-18 17:30:26 +05:30
|
|
|
switch (ot) {
|
|
|
|
case CREATE:
|
2016-09-30 20:51:20 +05:30
|
|
|
create_metadata(bm, metadata_version);
|
2013-09-19 18:15:56 +05:30
|
|
|
break;
|
2013-09-11 16:10:46 +05:30
|
|
|
|
2013-09-19 18:15:56 +05:30
|
|
|
default:
|
|
|
|
throw runtime_error("unhandled open_type");
|
2013-09-18 17:30:26 +05:30
|
|
|
}
|
2013-09-11 16:10:46 +05:30
|
|
|
}
|
|
|
|
|
2020-04-30 19:00:01 +05:30
|
|
|
metadata::metadata(block_manager::ptr bm)
|
2016-09-30 20:51:20 +05:30
|
|
|
{
|
|
|
|
open_metadata(bm);
|
|
|
|
}
|
|
|
|
|
2013-09-18 17:30:26 +05:30
|
|
|
void
|
2013-10-29 18:16:23 +05:30
|
|
|
metadata::commit(bool clean_shutdown)
|
2013-09-11 16:10:46 +05:30
|
|
|
{
|
2013-09-23 15:45:41 +05:30
|
|
|
commit_space_map();
|
|
|
|
commit_mappings();
|
2013-09-24 16:30:09 +05:30
|
|
|
commit_hints();
|
2013-10-10 17:38:04 +05:30
|
|
|
commit_discard_bits();
|
2013-10-29 18:16:23 +05:30
|
|
|
commit_superblock(clean_shutdown);
|
2013-09-11 16:10:46 +05:30
|
|
|
}
|
|
|
|
|
2013-09-26 16:06:01 +05:30
|
|
|
void
|
|
|
|
metadata::setup_hint_array(size_t width)
|
|
|
|
{
|
|
|
|
if (width > 0)
|
|
|
|
hints_ = hint_array::ptr(
|
2014-08-26 15:44:49 +05:30
|
|
|
new hint_array(*tm_, width));
|
2013-09-26 16:06:01 +05:30
|
|
|
}
|
|
|
|
|
2013-09-19 18:15:56 +05:30
|
|
|
void
|
2020-04-30 19:00:01 +05:30
|
|
|
metadata::create_metadata(block_manager::ptr bm, unsigned metadata_version)
|
2013-09-19 18:15:56 +05:30
|
|
|
{
|
|
|
|
tm_ = open_tm(bm);
|
|
|
|
|
|
|
|
space_map::ptr core = tm_->get_sm();
|
2014-08-26 15:44:49 +05:30
|
|
|
metadata_sm_ = create_metadata_sm(*tm_, tm_->get_bm()->get_nr_blocks());
|
2013-09-19 18:15:56 +05:30
|
|
|
copy_space_maps(metadata_sm_, core);
|
|
|
|
tm_->set_sm(metadata_sm_);
|
|
|
|
|
2014-08-26 15:44:49 +05:30
|
|
|
mappings_ = mapping_array::ptr(new mapping_array(*tm_, mapping_array::ref_counter()));
|
2013-09-26 16:06:01 +05:30
|
|
|
|
|
|
|
// We can't instantiate the hint array yet, since we don't know the
|
|
|
|
// hint width.
|
2013-10-10 17:38:04 +05:30
|
|
|
|
2016-09-30 20:51:20 +05:30
|
|
|
discard_bits_ = pd::bitset::ptr(new pd::bitset(*tm_));
|
|
|
|
|
|
|
|
if (metadata_version >= 2)
|
|
|
|
dirty_bits_ = pd::bitset::ptr(new pd::bitset(*tm_));
|
2013-09-19 18:15:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-04-30 19:00:01 +05:30
|
|
|
metadata::open_metadata(block_manager::ptr bm)
|
2013-09-19 18:15:56 +05:30
|
|
|
{
|
|
|
|
tm_ = open_tm(bm);
|
|
|
|
sb_ = read_superblock(tm_->get_bm());
|
2013-09-23 15:45:41 +05:30
|
|
|
|
|
|
|
mappings_ = mapping_array::ptr(
|
2014-08-26 15:44:49 +05:30
|
|
|
new mapping_array(*tm_,
|
2013-09-23 15:45:41 +05:30
|
|
|
mapping_array::ref_counter(),
|
|
|
|
sb_.mapping_root,
|
|
|
|
sb_.cache_blocks));
|
2013-10-10 15:56:55 +05:30
|
|
|
|
|
|
|
if (sb_.hint_root)
|
|
|
|
hints_ = hint_array::ptr(
|
2014-08-26 15:44:49 +05:30
|
|
|
new hint_array(*tm_, sb_.policy_hint_size,
|
2013-10-10 15:56:55 +05:30
|
|
|
sb_.hint_root, sb_.cache_blocks));
|
2013-10-10 17:38:04 +05:30
|
|
|
|
|
|
|
if (sb_.discard_root)
|
2016-09-30 20:51:20 +05:30
|
|
|
discard_bits_ = pd::bitset::ptr(
|
|
|
|
new pd::bitset(*tm_, sb_.discard_root, sb_.discard_nr_blocks));
|
|
|
|
|
|
|
|
if (sb_.version >= 2)
|
|
|
|
dirty_bits_ = pd::bitset::ptr(
|
|
|
|
new pd::bitset(*tm_, *sb_.dirty_root, sb_.cache_blocks));
|
2013-09-23 15:45:41 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
metadata::commit_space_map()
|
|
|
|
{
|
|
|
|
metadata_sm_->commit();
|
|
|
|
metadata_sm_->copy_root(&sb_.metadata_space_map_root, sizeof(sb_.metadata_space_map_root));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
metadata::commit_mappings()
|
|
|
|
{
|
|
|
|
sb_.mapping_root = mappings_->get_root();
|
2017-11-13 19:32:16 +05:30
|
|
|
if (sb_.version >= 2) {
|
|
|
|
dirty_bits_->flush();
|
|
|
|
sb_.dirty_root = dirty_bits_->get_root();
|
|
|
|
}
|
2013-09-23 15:45:41 +05:30
|
|
|
}
|
|
|
|
|
2013-09-24 16:30:09 +05:30
|
|
|
void
|
|
|
|
metadata::commit_hints()
|
|
|
|
{
|
|
|
|
sb_.hint_root = hints_->get_root();
|
|
|
|
}
|
|
|
|
|
2013-10-10 17:38:04 +05:30
|
|
|
void
|
|
|
|
metadata::commit_discard_bits()
|
|
|
|
{
|
2013-10-11 15:32:04 +05:30
|
|
|
sb_.discard_root = discard_bits_->get_root();
|
2013-10-10 17:38:04 +05:30
|
|
|
}
|
|
|
|
|
2013-09-23 15:45:41 +05:30
|
|
|
void
|
2013-10-29 18:16:23 +05:30
|
|
|
metadata::commit_superblock(bool clean_shutdown)
|
2013-09-23 15:45:41 +05:30
|
|
|
{
|
2013-10-29 18:16:23 +05:30
|
|
|
if (clean_shutdown)
|
|
|
|
sb_.flags.set_flag(superblock_flags::CLEAN_SHUTDOWN);
|
|
|
|
|
2013-10-07 19:51:45 +05:30
|
|
|
write_superblock(tm_->get_bm(), sb_);
|
2013-10-29 18:16:23 +05:30
|
|
|
|
2013-10-29 17:05:32 +05:30
|
|
|
sb_.flags.clear_flag(superblock_flags::CLEAN_SHUTDOWN);
|
2013-09-19 18:15:56 +05:30
|
|
|
}
|
|
|
|
|
2013-09-11 16:10:46 +05:30
|
|
|
//----------------------------------------------------------------
|