2011-08-31 17:18:41 +05:30
|
|
|
#include "space_map_disk.h"
|
|
|
|
|
2011-11-03 20:14:00 +05:30
|
|
|
#include "checksum.h"
|
2011-08-31 17:18:41 +05:30
|
|
|
#include "endian_utils.h"
|
|
|
|
#include "math_utils.h"
|
|
|
|
#include "space_map_disk_structures.h"
|
2011-11-09 15:51:25 +05:30
|
|
|
#include "space_map_recursive.h"
|
2011-08-31 17:18:41 +05:30
|
|
|
#include "transaction_manager.h"
|
|
|
|
|
|
|
|
using namespace boost;
|
|
|
|
using namespace persistent_data;
|
|
|
|
using namespace std;
|
|
|
|
using namespace sm_disk_detail;
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace {
|
2011-11-03 20:14:00 +05:30
|
|
|
uint64_t const BITMAP_CSUM_XOR = 240779;
|
|
|
|
|
|
|
|
struct bitmap_block_validator : public block_manager<>::validator {
|
|
|
|
virtual void check(block_manager<>::const_buffer &b, block_address location) const {
|
|
|
|
bitmap_header const *data = reinterpret_cast<bitmap_header const *>(&b);
|
|
|
|
crc32c sum(BITMAP_CSUM_XOR);
|
|
|
|
sum.append(&data->not_used, MD_BLOCK_SIZE - sizeof(uint32_t));
|
|
|
|
if (sum.get_sum() != to_cpu<uint32_t>(data->csum))
|
|
|
|
throw runtime_error("bad checksum in space map bitmap");
|
|
|
|
|
|
|
|
if (to_cpu<uint64_t>(data->blocknr) != location)
|
|
|
|
throw runtime_error("bad block nr in space map bitmap");
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void prepare(block_manager<>::buffer &b, block_address location) const {
|
|
|
|
bitmap_header *data = reinterpret_cast<bitmap_header *>(&b);
|
|
|
|
data->blocknr = to_disk<base::__le64, uint64_t>(location);
|
|
|
|
|
|
|
|
crc32c sum(BITMAP_CSUM_XOR);
|
|
|
|
sum.append(&data->not_used, MD_BLOCK_SIZE - sizeof(uint32_t));
|
|
|
|
data->csum = to_disk<base::__le32>(sum.get_sum());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
block_manager<>::validator::ptr
|
|
|
|
bitmap_validator() {
|
|
|
|
return block_manager<>::validator::ptr(new bitmap_block_validator());
|
|
|
|
}
|
|
|
|
|
2011-11-07 16:04:43 +05:30
|
|
|
//--------------------------------
|
|
|
|
|
|
|
|
uint64_t const INDEX_CSUM_XOR = 160478;
|
|
|
|
|
|
|
|
// FIXME: factor out the common code in these validators
|
|
|
|
struct index_block_validator : public block_manager<>::validator {
|
|
|
|
virtual void check(block_manager<>::const_buffer &b, block_address location) const {
|
|
|
|
metadata_index const *mi = reinterpret_cast<metadata_index const *>(&b);
|
|
|
|
crc32c sum(INDEX_CSUM_XOR);
|
|
|
|
sum.append(&mi->padding_, MD_BLOCK_SIZE - sizeof(uint32_t));
|
|
|
|
if (sum.get_sum() != to_cpu<uint32_t>(mi->csum_))
|
|
|
|
throw runtime_error("bad checksum in metadata index block");
|
|
|
|
|
|
|
|
if (to_cpu<uint64_t>(mi->blocknr_) != location)
|
|
|
|
throw runtime_error("bad block nr in metadata index block");
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void prepare(block_manager<>::buffer &b, block_address location) const {
|
|
|
|
metadata_index *mi = reinterpret_cast<metadata_index *>(&b);
|
|
|
|
mi->blocknr_ = to_disk<base::__le64, uint64_t>(location);
|
|
|
|
|
|
|
|
crc32c sum(INDEX_CSUM_XOR);
|
|
|
|
sum.append(&mi->padding_, MD_BLOCK_SIZE - sizeof(uint32_t));
|
|
|
|
mi->csum_ = to_disk<base::__le32>(sum.get_sum());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
block_manager<>::validator::ptr
|
|
|
|
index_validator() {
|
|
|
|
return block_manager<>::validator::ptr(new index_block_validator());
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
class bitmap {
|
|
|
|
public:
|
|
|
|
typedef transaction_manager::read_ref read_ref;
|
|
|
|
typedef transaction_manager::write_ref write_ref;
|
|
|
|
|
|
|
|
bitmap(transaction_manager::ptr tm,
|
|
|
|
index_entry const &ie)
|
|
|
|
: tm_(tm),
|
|
|
|
ie_(ie) {
|
|
|
|
}
|
|
|
|
|
|
|
|
ref_t lookup(unsigned b) const {
|
2011-11-07 16:04:43 +05:30
|
|
|
read_ref rr = tm_->read_lock(ie_.blocknr_, bitmap_validator());
|
2011-08-31 17:18:41 +05:30
|
|
|
void const *bits = bitmap_data(rr);
|
|
|
|
ref_t b1 = test_bit_le(bits, b * 2);
|
|
|
|
ref_t b2 = test_bit_le(bits, b * 2 + 1);
|
|
|
|
ref_t result = b2 ? 1 : 0;
|
|
|
|
result |= b1 ? 0b10 : 0;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void insert(unsigned b, ref_t n) {
|
2011-11-07 16:04:43 +05:30
|
|
|
write_ref wr = tm_->shadow(ie_.blocknr_, bitmap_validator()).first;
|
2011-08-31 17:18:41 +05:30
|
|
|
void *bits = bitmap_data(wr);
|
|
|
|
bool was_free = !test_bit_le(bits, b * 2) && !test_bit_le(bits, b * 2 + 1);
|
|
|
|
if (n == 1 || n == 3)
|
|
|
|
set_bit_le(bits, b * 2 + 1);
|
|
|
|
else
|
|
|
|
clear_bit_le(bits, b * 2 + 1);
|
|
|
|
|
|
|
|
if (n == 2 || n == 3)
|
|
|
|
set_bit_le(bits, b * 2);
|
|
|
|
else
|
|
|
|
clear_bit_le(bits, b * 2);
|
|
|
|
|
|
|
|
ie_.blocknr_ = wr.get_location();
|
|
|
|
|
|
|
|
if (was_free && n > 0) {
|
|
|
|
ie_.nr_free_--;
|
|
|
|
if (b == ie_.none_free_before_)
|
|
|
|
ie_.none_free_before_++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!was_free && n == 0) {
|
|
|
|
ie_.nr_free_++;
|
|
|
|
if (b < ie_.none_free_before_)
|
|
|
|
ie_.none_free_before_ = b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-03 20:14:00 +05:30
|
|
|
boost::optional<unsigned> find_free(unsigned begin, unsigned end) {
|
|
|
|
for (unsigned i = max(begin, ie_.none_free_before_); i < end; i++) {
|
2011-08-31 17:18:41 +05:30
|
|
|
if (lookup(i) == 0) {
|
|
|
|
insert(i, 1);
|
|
|
|
ie_.none_free_before_ = i + 1;
|
2011-11-03 20:14:00 +05:30
|
|
|
return boost::optional<unsigned>(i);
|
2011-08-31 17:18:41 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-03 20:14:00 +05:30
|
|
|
return boost::optional<unsigned>();
|
2011-08-31 17:18:41 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
index_entry const &get_ie() const {
|
|
|
|
return ie_;
|
|
|
|
}
|
|
|
|
|
2011-10-25 15:57:59 +05:30
|
|
|
void iterate(block_address offset, block_address hi, space_map::iterator &it) const {
|
2011-11-07 16:04:43 +05:30
|
|
|
read_ref rr = tm_->read_lock(ie_.blocknr_, bitmap_validator());
|
2011-10-25 15:57:59 +05:30
|
|
|
void const *bits = bitmap_data(rr);
|
|
|
|
|
|
|
|
for (unsigned b = 0; b < hi; b++) {
|
|
|
|
ref_t b1 = test_bit_le(bits, b * 2);
|
|
|
|
ref_t b2 = test_bit_le(bits, b * 2 + 1);
|
|
|
|
ref_t result = b2 ? 1 : 0;
|
|
|
|
result |= b1 ? 0b10 : 0;
|
|
|
|
it(offset + b, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
private:
|
2011-09-01 15:12:57 +05:30
|
|
|
void *bitmap_data(transaction_manager::write_ref &wr) {
|
2011-08-31 17:18:41 +05:30
|
|
|
bitmap_header *h = reinterpret_cast<bitmap_header *>(&wr.data()[0]);
|
|
|
|
return h + 1;
|
|
|
|
}
|
|
|
|
|
2011-09-01 15:12:57 +05:30
|
|
|
void const *bitmap_data(transaction_manager::read_ref &rr) const {
|
2011-08-31 17:18:41 +05:30
|
|
|
bitmap_header const *h = reinterpret_cast<bitmap_header const *>(&rr.data()[0]);
|
|
|
|
return h + 1;
|
|
|
|
}
|
|
|
|
|
2011-09-01 15:12:57 +05:30
|
|
|
transaction_manager::ptr tm_;
|
2011-08-31 17:18:41 +05:30
|
|
|
index_entry ie_;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ref_count_traits {
|
|
|
|
typedef __le32 disk_type;
|
|
|
|
typedef uint32_t value_type;
|
|
|
|
typedef NoOpRefCounter<uint32_t> ref_counter;
|
|
|
|
|
|
|
|
static void unpack(disk_type const &d, value_type &v) {
|
|
|
|
v = to_cpu<value_type>(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pack(value_type const &v, disk_type &d) {
|
|
|
|
d = to_disk<disk_type>(v);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-09-02 15:56:42 +05:30
|
|
|
class ref_count_checker : public btree_checker<1, ref_count_traits> {
|
2011-08-31 17:18:41 +05:30
|
|
|
public:
|
2011-09-02 15:56:42 +05:30
|
|
|
typedef boost::shared_ptr<ref_count_checker> ptr;
|
2011-08-31 17:18:41 +05:30
|
|
|
|
2011-09-02 15:56:42 +05:30
|
|
|
ref_count_checker(block_counter &counter)
|
2011-08-31 18:08:22 +05:30
|
|
|
: btree_checker<1, ref_count_traits>(counter) {
|
2011-08-31 17:18:41 +05:30
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-11-10 20:13:15 +05:30
|
|
|
class index_store {
|
|
|
|
public:
|
|
|
|
typedef boost::shared_ptr<index_store> ptr;
|
|
|
|
|
|
|
|
virtual void resize(block_address nr_indexes) = 0;
|
|
|
|
virtual index_entry find_ie(block_address b) const = 0;
|
|
|
|
virtual void save_ie(block_address b, struct index_entry ie) = 0;
|
|
|
|
virtual void commit_ies() = 0;
|
|
|
|
virtual ptr clone() const = 0;
|
|
|
|
virtual block_address get_root() const = 0;
|
|
|
|
virtual void check(block_counter &counter, block_address nr_index_entries) const = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
unsigned const ENTRIES_PER_BLOCK = (MD_BLOCK_SIZE - sizeof(bitmap_header)) * 4;
|
|
|
|
|
2011-11-10 21:00:36 +05:30
|
|
|
class sm_disk : public checked_space_map {
|
2011-08-31 17:18:41 +05:30
|
|
|
public:
|
2011-11-10 21:00:36 +05:30
|
|
|
typedef boost::shared_ptr<sm_disk> ptr;
|
2011-08-31 17:18:41 +05:30
|
|
|
typedef transaction_manager::read_ref read_ref;
|
|
|
|
typedef transaction_manager::write_ref write_ref;
|
|
|
|
|
2011-11-10 21:00:36 +05:30
|
|
|
sm_disk(index_store::ptr indexes,
|
|
|
|
transaction_manager::ptr tm)
|
2011-08-31 17:18:41 +05:30
|
|
|
: tm_(tm),
|
2011-11-10 20:13:15 +05:30
|
|
|
indexes_(indexes),
|
2011-08-31 17:18:41 +05:30
|
|
|
nr_blocks_(0),
|
|
|
|
nr_allocated_(0),
|
|
|
|
ref_counts_(tm_, ref_count_traits::ref_counter()) {
|
|
|
|
}
|
|
|
|
|
2011-11-10 21:00:36 +05:30
|
|
|
sm_disk(index_store::ptr indexes,
|
|
|
|
transaction_manager::ptr tm,
|
|
|
|
sm_root const &root)
|
2011-08-31 17:18:41 +05:30
|
|
|
: tm_(tm),
|
2011-11-10 20:13:15 +05:30
|
|
|
indexes_(indexes),
|
2011-08-31 17:18:41 +05:30
|
|
|
nr_blocks_(root.nr_blocks_),
|
|
|
|
nr_allocated_(root.nr_allocated_),
|
|
|
|
ref_counts_(tm_, root.ref_count_root_, ref_count_traits::ref_counter()) {
|
|
|
|
}
|
|
|
|
|
|
|
|
block_address get_nr_blocks() const {
|
|
|
|
return nr_blocks_;
|
|
|
|
}
|
|
|
|
|
|
|
|
block_address get_nr_free() const {
|
|
|
|
return nr_blocks_ - nr_allocated_;
|
|
|
|
}
|
|
|
|
|
|
|
|
ref_t get_count(block_address b) const {
|
|
|
|
ref_t count = lookup_bitmap(b);
|
|
|
|
if (count == 3)
|
|
|
|
return lookup_ref_count(b);
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_count(block_address b, ref_t c) {
|
|
|
|
ref_t old = get_count(b);
|
|
|
|
|
|
|
|
if (c == old)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (c > 2) {
|
|
|
|
if (old < 3)
|
|
|
|
insert_bitmap(b, 3);
|
|
|
|
insert_ref_count(b, c);
|
|
|
|
} else {
|
|
|
|
if (old > 2)
|
|
|
|
remove_ref_count(b);
|
|
|
|
insert_bitmap(b, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (old == 0)
|
|
|
|
nr_allocated_++;
|
|
|
|
else if (c == 0)
|
|
|
|
nr_allocated_--;
|
|
|
|
}
|
|
|
|
|
|
|
|
void commit() {
|
|
|
|
commit_ies();
|
|
|
|
}
|
|
|
|
|
|
|
|
void inc(block_address b) {
|
|
|
|
// FIXME: 2 get_counts
|
|
|
|
ref_t old = get_count(b);
|
|
|
|
set_count(b, old + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dec(block_address b) {
|
|
|
|
ref_t old = get_count(b);
|
|
|
|
set_count(b, old - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
block_address new_block() {
|
2011-11-03 20:14:00 +05:30
|
|
|
// FIXME: silly to always start searching from the
|
2011-08-31 17:18:41 +05:30
|
|
|
// beginning.
|
2011-11-10 20:13:15 +05:30
|
|
|
block_address nr_indexes = div_up<block_address>(nr_blocks_, ENTRIES_PER_BLOCK);
|
2011-08-31 17:18:41 +05:30
|
|
|
for (block_address index = 0; index < nr_indexes; index++) {
|
|
|
|
index_entry ie = find_ie(index);
|
|
|
|
|
|
|
|
bitmap bm(tm_, ie);
|
2011-11-03 20:14:00 +05:30
|
|
|
optional<unsigned> maybe_b = bm.find_free(0, (index == nr_indexes - 1) ?
|
2011-11-10 20:13:15 +05:30
|
|
|
nr_blocks_ % ENTRIES_PER_BLOCK : ENTRIES_PER_BLOCK);
|
2011-11-03 20:14:00 +05:30
|
|
|
if (maybe_b) {
|
|
|
|
block_address b = *maybe_b;
|
|
|
|
save_ie(index, bm.get_ie());
|
|
|
|
nr_allocated_++;
|
2011-11-10 20:13:15 +05:30
|
|
|
b = (index * ENTRIES_PER_BLOCK) + b;
|
2011-11-03 20:14:00 +05:30
|
|
|
assert(get_count(b) == 1);
|
|
|
|
return b;
|
|
|
|
}
|
2011-08-31 17:18:41 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
throw runtime_error("out of space");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool count_possibly_greater_than_one(block_address b) const {
|
|
|
|
return get_count(b) > 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void extend(block_address extra_blocks) {
|
|
|
|
block_address nr_blocks = nr_blocks_ + extra_blocks;
|
|
|
|
|
2011-11-10 20:13:15 +05:30
|
|
|
block_address bitmap_count = div_up<block_address>(nr_blocks, ENTRIES_PER_BLOCK);
|
|
|
|
block_address old_bitmap_count = div_up<block_address>(nr_blocks_, ENTRIES_PER_BLOCK);
|
|
|
|
|
|
|
|
indexes_->resize(bitmap_count);
|
2011-08-31 17:18:41 +05:30
|
|
|
for (block_address i = old_bitmap_count; i < bitmap_count; i++) {
|
2011-11-03 20:14:00 +05:30
|
|
|
write_ref wr = tm_->new_block(bitmap_validator());
|
2011-08-31 17:18:41 +05:30
|
|
|
|
2011-11-03 20:14:00 +05:30
|
|
|
index_entry ie;
|
2011-08-31 17:18:41 +05:30
|
|
|
ie.blocknr_ = wr.get_location();
|
|
|
|
ie.nr_free_ = i == (bitmap_count - 1) ?
|
2011-11-10 20:13:15 +05:30
|
|
|
(nr_blocks % ENTRIES_PER_BLOCK) : ENTRIES_PER_BLOCK;
|
2011-08-31 17:18:41 +05:30
|
|
|
ie.none_free_before_ = 0;
|
|
|
|
|
|
|
|
save_ie(i, ie);
|
|
|
|
}
|
|
|
|
|
|
|
|
nr_blocks_ = nr_blocks;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void check(block_counter &counter) const {
|
2011-09-02 15:56:42 +05:30
|
|
|
ref_count_checker::ptr v(new ref_count_checker(counter));
|
2011-08-31 17:18:41 +05:30
|
|
|
ref_counts_.visit(v);
|
2011-11-10 20:13:15 +05:30
|
|
|
|
|
|
|
block_address nr_entries = div_up<block_address>(get_nr_blocks(), ENTRIES_PER_BLOCK);
|
|
|
|
indexes_->check(counter, nr_entries);
|
2011-08-31 17:18:41 +05:30
|
|
|
}
|
|
|
|
|
2011-10-25 15:57:59 +05:30
|
|
|
struct look_aside_iterator : public iterator {
|
2011-11-10 21:00:36 +05:30
|
|
|
look_aside_iterator(sm_disk const &smd, iterator &it)
|
2011-10-25 15:57:59 +05:30
|
|
|
: smd_(smd),
|
|
|
|
it_(it) {
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void operator () (block_address b, ref_t c) {
|
|
|
|
it_(b, c == 3 ? smd_.lookup_ref_count(b) : c);
|
|
|
|
}
|
|
|
|
|
2011-11-10 21:00:36 +05:30
|
|
|
sm_disk const &smd_;
|
2011-10-25 15:57:59 +05:30
|
|
|
iterator &it_;
|
|
|
|
};
|
|
|
|
|
|
|
|
friend struct look_aside_iterator;
|
|
|
|
|
|
|
|
virtual void iterate(iterator &it) const {
|
|
|
|
look_aside_iterator wrapper(*this, it);
|
2011-11-10 20:13:15 +05:30
|
|
|
unsigned nr_indexes = div_up<block_address>(nr_blocks_, ENTRIES_PER_BLOCK);
|
2011-10-25 15:57:59 +05:30
|
|
|
|
|
|
|
for (unsigned i = 0; i < nr_indexes; i++) {
|
2011-11-10 20:13:15 +05:30
|
|
|
unsigned hi = (i == nr_indexes - 1) ? (nr_blocks_ % ENTRIES_PER_BLOCK) : ENTRIES_PER_BLOCK;
|
2011-10-25 15:57:59 +05:30
|
|
|
index_entry ie = find_ie(i);
|
|
|
|
bitmap bm(tm_, ie);
|
2011-11-10 20:13:15 +05:30
|
|
|
bm.iterate(i * ENTRIES_PER_BLOCK, hi, wrapper);
|
2011-10-25 15:57:59 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-10 20:13:15 +05:30
|
|
|
size_t root_size() {
|
|
|
|
return sizeof(sm_root_disk);
|
|
|
|
}
|
|
|
|
|
|
|
|
void copy_root(void *dest, size_t len) {
|
|
|
|
sm_root_disk d;
|
|
|
|
sm_root v;
|
|
|
|
|
|
|
|
if (len < sizeof(d))
|
|
|
|
throw runtime_error("root too small");
|
|
|
|
|
2011-11-10 21:00:36 +05:30
|
|
|
v.nr_blocks_ = sm_disk::get_nr_blocks();
|
|
|
|
v.nr_allocated_ = sm_disk::get_nr_allocated();
|
2011-11-10 20:13:15 +05:30
|
|
|
v.bitmap_root_ = get_index_store()->get_root();
|
2011-11-10 21:00:36 +05:30
|
|
|
v.ref_count_root_ = sm_disk::get_ref_count_root();
|
2011-11-10 20:13:15 +05:30
|
|
|
|
|
|
|
sm_root_traits::pack(v, d);
|
|
|
|
::memcpy(dest, &d, sizeof(d));
|
|
|
|
}
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
protected:
|
2011-09-01 15:12:57 +05:30
|
|
|
transaction_manager::ptr get_tm() const {
|
2011-08-31 17:18:41 +05:30
|
|
|
return tm_;
|
|
|
|
}
|
|
|
|
|
|
|
|
block_address get_nr_allocated() const {
|
|
|
|
return nr_allocated_;
|
|
|
|
}
|
|
|
|
|
|
|
|
block_address get_ref_count_root() const {
|
|
|
|
return ref_counts_.get_root();
|
|
|
|
}
|
|
|
|
|
2011-11-10 20:13:15 +05:30
|
|
|
index_store::ptr get_index_store() const {
|
|
|
|
return indexes_;
|
2011-08-31 17:18:41 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2011-11-10 20:13:15 +05:30
|
|
|
// FIXME: remove these, they're just for the transistion
|
|
|
|
index_entry find_ie(block_address b) const {
|
|
|
|
return indexes_->find_ie(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
void save_ie(block_address b, struct index_entry ie) {
|
|
|
|
return indexes_->save_ie(b, ie);
|
|
|
|
}
|
|
|
|
|
|
|
|
void commit_ies() {
|
|
|
|
return indexes_->commit_ies();
|
|
|
|
}
|
2011-08-31 17:18:41 +05:30
|
|
|
|
|
|
|
ref_t lookup_bitmap(block_address b) const {
|
2011-11-10 20:13:15 +05:30
|
|
|
index_entry ie = find_ie(b / ENTRIES_PER_BLOCK);
|
2011-08-31 17:18:41 +05:30
|
|
|
bitmap bm(tm_, ie);
|
2011-11-10 20:13:15 +05:30
|
|
|
return bm.lookup(b % ENTRIES_PER_BLOCK);
|
2011-08-31 17:18:41 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void insert_bitmap(block_address b, unsigned n) {
|
|
|
|
if (n > 3)
|
|
|
|
throw runtime_error("bitmap can only hold 2 bit values");
|
|
|
|
|
2011-11-10 20:13:15 +05:30
|
|
|
index_entry ie = find_ie(b / ENTRIES_PER_BLOCK);
|
2011-08-31 17:18:41 +05:30
|
|
|
bitmap bm(tm_, ie);
|
2011-11-10 20:13:15 +05:30
|
|
|
bm.insert(b % ENTRIES_PER_BLOCK, n);
|
|
|
|
save_ie(b / ENTRIES_PER_BLOCK, bm.get_ie());
|
2011-08-31 17:18:41 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
ref_t lookup_ref_count(block_address b) const {
|
|
|
|
uint64_t key[1] = {b};
|
|
|
|
optional<ref_t> mvalue = ref_counts_.lookup(key);
|
|
|
|
if (!mvalue)
|
|
|
|
throw runtime_error("ref count not in tree");
|
|
|
|
return *mvalue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void insert_ref_count(block_address b, ref_t count) {
|
|
|
|
uint64_t key[1] = {b};
|
|
|
|
ref_counts_.insert(key, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove_ref_count(block_address b) {
|
|
|
|
uint64_t key[1] = {b};
|
|
|
|
ref_counts_.remove(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
transaction_manager::ptr tm_;
|
2011-11-10 20:13:15 +05:30
|
|
|
index_store::ptr indexes_;
|
2011-08-31 17:18:41 +05:30
|
|
|
block_address nr_blocks_;
|
|
|
|
block_address nr_allocated_;
|
|
|
|
|
|
|
|
btree<1, ref_count_traits> ref_counts_;
|
|
|
|
};
|
|
|
|
|
2011-08-31 18:08:22 +05:30
|
|
|
class bitmap_tree_validator : public btree_checker<1, index_entry_traits> {
|
2011-08-31 17:18:41 +05:30
|
|
|
public:
|
|
|
|
typedef boost::shared_ptr<bitmap_tree_validator> ptr;
|
|
|
|
|
|
|
|
bitmap_tree_validator(block_counter &counter)
|
2011-08-31 18:08:22 +05:30
|
|
|
: btree_checker<1, index_entry_traits>(counter) {
|
2011-08-31 17:18:41 +05:30
|
|
|
}
|
|
|
|
|
2011-09-02 15:56:42 +05:30
|
|
|
bool visit_leaf(unsigned level,
|
2011-10-10 18:40:30 +05:30
|
|
|
bool sub_root,
|
2011-09-02 15:56:42 +05:30
|
|
|
optional<uint64_t> key,
|
2011-08-31 17:18:41 +05:30
|
|
|
btree_detail::node_ref<index_entry_traits> const &n) {
|
2011-10-10 18:40:30 +05:30
|
|
|
bool r = btree_checker<1, index_entry_traits>::visit_leaf(level, sub_root, key, n);
|
2011-09-01 18:35:01 +05:30
|
|
|
if (!r)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < n.get_nr_entries(); i++) {
|
|
|
|
if (seen_indexes_.count(n.key_at(i)) > 0) {
|
|
|
|
ostringstream out;
|
|
|
|
out << "index entry " << i << " is present twice";
|
|
|
|
throw runtime_error(out.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
seen_indexes_.insert(n.key_at(i));
|
|
|
|
btree_checker<1, index_entry_traits>::get_counter().inc(n.value_at(i).blocknr_);
|
|
|
|
}
|
2011-08-31 17:18:41 +05:30
|
|
|
|
2011-09-01 18:35:01 +05:30
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void check_all_index_entries_present(block_address nr_entries) {
|
|
|
|
for (block_address i = 0; i < nr_entries; i++) {
|
|
|
|
if (seen_indexes_.count(i) == 0) {
|
|
|
|
ostringstream out;
|
|
|
|
out << "missing index entry " << i;
|
|
|
|
throw runtime_error(out.str());
|
|
|
|
}
|
|
|
|
}
|
2011-08-31 17:18:41 +05:30
|
|
|
|
2011-09-01 18:35:01 +05:30
|
|
|
set<block_address>::const_iterator it;
|
|
|
|
for (it = seen_indexes_.begin(); it != seen_indexes_.end(); ++it) {
|
|
|
|
if (*it >= nr_entries) {
|
|
|
|
ostringstream out;
|
|
|
|
out << "unexpected index entry " << *it;
|
|
|
|
throw runtime_error(out.str());
|
|
|
|
}
|
|
|
|
}
|
2011-08-31 17:18:41 +05:30
|
|
|
}
|
2011-09-01 18:35:01 +05:30
|
|
|
|
|
|
|
private:
|
|
|
|
set<block_address> seen_indexes_;
|
2011-08-31 17:18:41 +05:30
|
|
|
};
|
|
|
|
|
2011-11-10 20:13:15 +05:30
|
|
|
class btree_index_store : public index_store {
|
2011-08-31 17:18:41 +05:30
|
|
|
public:
|
2011-11-10 20:13:15 +05:30
|
|
|
typedef boost::shared_ptr<btree_index_store> ptr;
|
2011-08-31 17:18:41 +05:30
|
|
|
|
2011-11-10 20:13:15 +05:30
|
|
|
btree_index_store(transaction_manager::ptr tm)
|
|
|
|
: tm_(tm),
|
|
|
|
bitmaps_(tm, index_entry_traits::ref_counter()) {
|
2011-08-31 17:18:41 +05:30
|
|
|
}
|
|
|
|
|
2011-11-10 20:13:15 +05:30
|
|
|
btree_index_store(transaction_manager::ptr tm,
|
|
|
|
block_address root)
|
|
|
|
: tm_(tm),
|
|
|
|
bitmaps_(tm, root, index_entry_traits::ref_counter()) {
|
2011-08-31 17:18:41 +05:30
|
|
|
}
|
|
|
|
|
2011-11-10 20:13:15 +05:30
|
|
|
virtual void resize(block_address nr_entries) {
|
|
|
|
// No op
|
2011-08-31 17:18:41 +05:30
|
|
|
}
|
|
|
|
|
2011-11-10 20:13:15 +05:30
|
|
|
virtual index_entry find_ie(block_address ie_index) const {
|
|
|
|
uint64_t key[1] = {ie_index};
|
|
|
|
optional<index_entry> mindex = bitmaps_.lookup(key);
|
|
|
|
if (!mindex)
|
|
|
|
throw runtime_error("Couldn't lookup bitmap");
|
2011-08-31 17:18:41 +05:30
|
|
|
|
2011-11-10 20:13:15 +05:30
|
|
|
return *mindex;
|
|
|
|
}
|
2011-08-31 17:18:41 +05:30
|
|
|
|
2011-11-10 20:13:15 +05:30
|
|
|
virtual void save_ie(block_address ie_index, struct index_entry ie) {
|
|
|
|
uint64_t key[1] = {ie_index};
|
|
|
|
bitmaps_.insert(key, ie);
|
|
|
|
}
|
2011-11-03 20:14:00 +05:30
|
|
|
|
2011-11-10 20:13:15 +05:30
|
|
|
virtual void commit_ies() {
|
|
|
|
// No op
|
2011-08-31 17:18:41 +05:30
|
|
|
}
|
|
|
|
|
2011-11-10 20:13:15 +05:30
|
|
|
virtual index_store::ptr clone() const {
|
|
|
|
return index_store::ptr(new btree_index_store(tm_, bitmaps_.get_root()));
|
|
|
|
}
|
2011-08-31 17:18:41 +05:30
|
|
|
|
2011-11-10 20:13:15 +05:30
|
|
|
virtual block_address get_root() const {
|
|
|
|
return bitmaps_.get_root();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void check(block_counter &counter, block_address nr_index_entries) const {
|
2011-09-01 15:12:57 +05:30
|
|
|
bitmap_tree_validator::ptr v(new bitmap_tree_validator(counter));
|
2011-08-31 17:18:41 +05:30
|
|
|
bitmaps_.visit(v);
|
2011-11-10 20:13:15 +05:30
|
|
|
v->check_all_index_entries_present(nr_index_entries);
|
2011-08-31 17:18:41 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2011-11-10 20:13:15 +05:30
|
|
|
transaction_manager::ptr tm_;
|
|
|
|
btree<1, index_entry_traits> bitmaps_;
|
|
|
|
};
|
2011-08-31 17:18:41 +05:30
|
|
|
|
2011-11-10 20:13:15 +05:30
|
|
|
class metadata_index_store : public index_store {
|
2011-08-31 17:18:41 +05:30
|
|
|
public:
|
2011-11-10 20:13:15 +05:30
|
|
|
typedef boost::shared_ptr<metadata_index_store> ptr;
|
2011-08-31 17:18:41 +05:30
|
|
|
|
2011-11-10 20:13:15 +05:30
|
|
|
metadata_index_store(transaction_manager::ptr tm)
|
|
|
|
: tm_(tm) {
|
|
|
|
block_manager<>::write_ref wr = tm_->new_block(index_validator());
|
2011-11-07 16:04:43 +05:30
|
|
|
bitmap_root_ = wr.get_location();
|
2011-08-31 17:18:41 +05:30
|
|
|
}
|
|
|
|
|
2011-11-10 20:13:15 +05:30
|
|
|
metadata_index_store(transaction_manager::ptr tm, block_address root, block_address nr_indexes)
|
|
|
|
: tm_(tm),
|
|
|
|
bitmap_root_(root) {
|
|
|
|
resize(nr_indexes);
|
2011-08-31 17:18:41 +05:30
|
|
|
load_ies();
|
|
|
|
}
|
|
|
|
|
2011-11-10 20:13:15 +05:30
|
|
|
virtual void resize(block_address nr_indexes) {
|
|
|
|
entries_.resize(nr_indexes);
|
2011-08-31 17:18:41 +05:30
|
|
|
}
|
|
|
|
|
2011-11-10 20:13:15 +05:30
|
|
|
virtual index_entry find_ie(block_address ie_index) const {
|
|
|
|
return entries_[ie_index];
|
|
|
|
}
|
2011-08-31 17:18:41 +05:30
|
|
|
|
2011-11-10 20:13:15 +05:30
|
|
|
virtual void save_ie(block_address ie_index, struct index_entry ie) {
|
|
|
|
entries_[ie_index] = ie;
|
|
|
|
}
|
2011-08-31 17:18:41 +05:30
|
|
|
|
2011-11-10 20:13:15 +05:30
|
|
|
virtual void commit_ies() {
|
|
|
|
std::pair<block_manager<>::write_ref, bool> p =
|
|
|
|
tm_->shadow(bitmap_root_, index_validator());
|
2011-11-03 20:14:00 +05:30
|
|
|
|
2011-11-10 20:13:15 +05:30
|
|
|
bitmap_root_ = p.first.get_location();
|
|
|
|
metadata_index *mdi = reinterpret_cast<metadata_index *>(&p.first.data());
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < entries_.size(); i++)
|
|
|
|
index_entry_traits::pack(entries_[i], mdi->index[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual index_store::ptr clone() const {
|
|
|
|
return index_store::ptr(new metadata_index_store(tm_, bitmap_root_, entries_.size()));
|
2011-08-31 17:18:41 +05:30
|
|
|
}
|
|
|
|
|
2011-11-10 20:13:15 +05:30
|
|
|
virtual block_address get_root() const {
|
|
|
|
return bitmap_root_;
|
|
|
|
}
|
2011-08-31 17:18:41 +05:30
|
|
|
|
2011-11-10 20:13:15 +05:30
|
|
|
virtual void check(block_counter &counter, block_address nr_index_entries) const {
|
2011-08-31 17:18:41 +05:30
|
|
|
counter.inc(bitmap_root_);
|
|
|
|
for (unsigned i = 0; i < entries_.size(); i++)
|
2011-11-10 21:00:36 +05:30
|
|
|
// FIXME: this looks like a hack
|
2011-08-31 17:18:41 +05:30
|
|
|
if (entries_[i].blocknr_ != 0) // superblock
|
|
|
|
counter.inc(entries_[i].blocknr_);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void load_ies() {
|
2011-09-01 15:12:57 +05:30
|
|
|
block_manager<>::read_ref rr =
|
2011-11-10 20:13:15 +05:30
|
|
|
tm_->read_lock(bitmap_root_, index_validator());
|
2011-08-31 17:18:41 +05:30
|
|
|
|
|
|
|
metadata_index const *mdi = reinterpret_cast<metadata_index const *>(&rr.data());
|
2011-11-10 20:13:15 +05:30
|
|
|
for (unsigned i = 0; i < entries_.size(); i++)
|
2011-08-31 17:18:41 +05:30
|
|
|
index_entry_traits::unpack(*(mdi->index + i), entries_[i]);
|
|
|
|
}
|
|
|
|
|
2011-11-10 20:13:15 +05:30
|
|
|
transaction_manager::ptr tm_;
|
|
|
|
block_address bitmap_root_;
|
|
|
|
std::vector<index_entry> entries_;
|
|
|
|
};
|
2011-08-31 17:18:41 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
checked_space_map::ptr
|
|
|
|
persistent_data::create_disk_sm(transaction_manager::ptr tm,
|
|
|
|
block_address nr_blocks)
|
|
|
|
{
|
2011-11-10 21:00:36 +05:30
|
|
|
index_store::ptr store(new btree_index_store(tm));
|
|
|
|
checked_space_map::ptr sm(new sm_disk(store, tm));
|
2011-08-31 17:18:41 +05:30
|
|
|
sm->extend(nr_blocks);
|
|
|
|
return sm;
|
|
|
|
}
|
|
|
|
|
|
|
|
checked_space_map::ptr
|
|
|
|
persistent_data::open_disk_sm(transaction_manager::ptr tm, void *root)
|
|
|
|
{
|
|
|
|
sm_root_disk d;
|
|
|
|
sm_root v;
|
|
|
|
|
|
|
|
::memcpy(&d, root, sizeof(d));
|
|
|
|
sm_root_traits::unpack(d, v);
|
2011-11-10 21:00:36 +05:30
|
|
|
index_store::ptr store(new btree_index_store(tm, v.bitmap_root_));
|
|
|
|
return checked_space_map::ptr(new sm_disk(store, tm, v));
|
2011-08-31 17:18:41 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
checked_space_map::ptr
|
2011-11-03 20:14:00 +05:30
|
|
|
persistent_data::create_metadata_sm(transaction_manager::ptr tm, block_address nr_blocks)
|
|
|
|
{
|
2011-11-10 21:00:36 +05:30
|
|
|
index_store::ptr store(new metadata_index_store(tm));
|
|
|
|
checked_space_map::ptr sm(new sm_disk(store, tm));
|
2011-11-03 20:14:00 +05:30
|
|
|
sm->extend(nr_blocks);
|
2011-11-09 15:51:25 +05:30
|
|
|
return create_recursive_sm(sm);
|
2011-11-03 20:14:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
checked_space_map::ptr
|
|
|
|
persistent_data::open_metadata_sm(transaction_manager::ptr tm, void *root)
|
2011-08-31 17:18:41 +05:30
|
|
|
{
|
|
|
|
sm_root_disk d;
|
|
|
|
sm_root v;
|
|
|
|
|
|
|
|
::memcpy(&d, root, sizeof(d));
|
|
|
|
sm_root_traits::unpack(d, v);
|
2011-11-10 21:00:36 +05:30
|
|
|
block_address nr_indexes = div_up<block_address>(v.nr_blocks_, ENTRIES_PER_BLOCK);
|
|
|
|
index_store::ptr store(new metadata_index_store(tm, v.bitmap_root_, nr_indexes));
|
|
|
|
return create_recursive_sm(checked_space_map::ptr(new sm_disk(store, tm, v)));
|
2011-08-31 17:18:41 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|