2011-12-16 00:04:31 +05:30
|
|
|
// Copyright (C) 2011 Red Hat, Inc. All rights reserved.
|
2011-12-06 19:23:05 +05:30
|
|
|
//
|
2011-12-06 19:13:56 +05:30
|
|
|
// This file is part of the thin-provisioning-tools source.
|
|
|
|
//
|
|
|
|
// thin-provisioning-tools is free software: you can redistribute it
|
|
|
|
// and/or modify it under the terms of the GNU General Public License
|
|
|
|
// as published by the Free Software Foundation, either version 3 of
|
|
|
|
// the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// thin-provisioning-tools is distributed in the hope that it will be
|
|
|
|
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
|
|
|
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with thin-provisioning-tools. If not, see
|
|
|
|
// <http://www.gnu.org/licenses/>.
|
|
|
|
|
2013-01-11 03:06:38 +05:30
|
|
|
#include "persistent-data/space-maps/recursive.h"
|
2013-04-23 15:27:47 +05:30
|
|
|
#include "persistent-data/space-maps/subtracting_span_iterator.h"
|
2011-11-09 15:51:25 +05:30
|
|
|
|
2014-07-22 21:11:39 +05:30
|
|
|
#include <list>
|
|
|
|
|
2011-11-09 15:51:25 +05:30
|
|
|
using namespace persistent_data;
|
2017-04-29 23:21:52 +05:30
|
|
|
using namespace std;
|
2011-11-09 15:51:25 +05:30
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace {
|
2020-05-26 13:00:52 +05:30
|
|
|
enum op {
|
|
|
|
INC,
|
|
|
|
SET
|
|
|
|
};
|
2011-11-09 15:51:25 +05:30
|
|
|
|
2020-05-26 13:00:52 +05:30
|
|
|
struct block_op {
|
|
|
|
block_op(op o, uint32_t rc)
|
2011-11-09 15:51:25 +05:30
|
|
|
: op_(o),
|
|
|
|
rc_(rc) {
|
|
|
|
}
|
|
|
|
|
|
|
|
op op_;
|
2020-05-26 13:00:52 +05:30
|
|
|
|
|
|
|
// I'm assuming the ref counts never get above 2^31, which is reasonable I think :)
|
|
|
|
int32_t rc_;
|
2011-11-09 15:51:25 +05:30
|
|
|
};
|
|
|
|
|
2020-05-26 13:08:05 +05:30
|
|
|
// Aggregates two block_ops
|
2020-05-26 13:00:52 +05:30
|
|
|
block_op operator +(block_op const &lhs, block_op const &rhs) {
|
|
|
|
switch (lhs.op_) {
|
|
|
|
case INC:
|
|
|
|
switch (rhs.op_) {
|
|
|
|
case INC:
|
|
|
|
return block_op(INC, lhs.rc_ + rhs.rc_);
|
|
|
|
|
|
|
|
case SET:
|
|
|
|
return rhs;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SET:
|
|
|
|
switch (rhs.op_) {
|
|
|
|
case INC:
|
|
|
|
return block_op(SET, lhs.rc_ + rhs.rc_);
|
|
|
|
|
|
|
|
case SET:
|
|
|
|
return rhs;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw runtime_error("can't get here");
|
|
|
|
}
|
|
|
|
|
2011-11-09 15:51:25 +05:30
|
|
|
class sm_recursive : public checked_space_map {
|
|
|
|
public:
|
|
|
|
sm_recursive(checked_space_map::ptr sm)
|
|
|
|
: sm_(sm),
|
2013-04-23 15:27:47 +05:30
|
|
|
depth_(0),
|
|
|
|
flush_in_progress_(false) {
|
2011-11-09 15:51:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
virtual block_address get_nr_blocks() const {
|
|
|
|
return sm_->get_nr_blocks();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual block_address get_nr_free() const {
|
|
|
|
return sm_->get_nr_free();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ref_t get_count(block_address b) const {
|
2020-05-26 13:15:35 +05:30
|
|
|
recursing_const_lock lock(*this);
|
2020-05-26 13:08:05 +05:30
|
|
|
return modify_count(b, sm_->get_count(b));
|
2011-11-09 15:51:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
virtual void set_count(block_address b, ref_t c) {
|
2011-11-10 20:13:15 +05:30
|
|
|
if (depth_)
|
2020-05-26 13:00:52 +05:30
|
|
|
add_op(b, block_op(SET, c));
|
2011-11-09 15:51:25 +05:30
|
|
|
else {
|
|
|
|
recursing_lock lock(*this);
|
|
|
|
return sm_->set_count(b, c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void commit() {
|
|
|
|
cant_recurse("commit");
|
|
|
|
sm_->commit();
|
|
|
|
}
|
|
|
|
|
2020-05-26 13:42:45 +05:30
|
|
|
virtual void inc(block_address b, ref_t count) override {
|
2011-11-10 20:13:15 +05:30
|
|
|
if (depth_)
|
2020-05-26 13:42:45 +05:30
|
|
|
add_op(b, block_op(INC, count));
|
2011-11-09 15:51:25 +05:30
|
|
|
else {
|
|
|
|
recursing_lock lock(*this);
|
2020-05-26 13:42:45 +05:30
|
|
|
return sm_->inc(b, count);
|
2011-11-09 15:51:25 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-26 13:42:45 +05:30
|
|
|
virtual void dec(block_address b, ref_t count) override {
|
2011-11-10 20:13:15 +05:30
|
|
|
if (depth_)
|
2020-05-26 13:42:45 +05:30
|
|
|
add_op(b, block_op(INC, -count));
|
2011-11-09 15:51:25 +05:30
|
|
|
else {
|
|
|
|
recursing_lock lock(*this);
|
2020-05-26 13:42:45 +05:30
|
|
|
return sm_->dec(b, count);
|
2011-11-09 15:51:25 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-11 02:35:10 +05:30
|
|
|
virtual maybe_block
|
2013-04-23 15:27:47 +05:30
|
|
|
find_free(span_iterator &it) {
|
2011-11-09 15:51:25 +05:30
|
|
|
recursing_lock lock(*this);
|
2013-04-23 15:27:47 +05:30
|
|
|
|
2013-07-09 15:55:09 +05:30
|
|
|
subtracting_span_iterator filtered_it(get_nr_blocks(), it, allocated_blocks_);
|
2013-04-23 15:27:47 +05:30
|
|
|
return sm_->find_free(filtered_it);
|
2011-11-16 18:23:37 +05:30
|
|
|
}
|
|
|
|
|
2011-11-09 15:51:25 +05:30
|
|
|
virtual bool count_possibly_greater_than_one(block_address b) const {
|
2013-04-23 15:27:47 +05:30
|
|
|
recursing_const_lock lock(*this);
|
2020-05-26 13:00:52 +05:30
|
|
|
|
2013-04-23 15:27:47 +05:30
|
|
|
bool gto = sm_->count_possibly_greater_than_one(b);
|
2020-05-26 13:00:52 +05:30
|
|
|
if (gto)
|
|
|
|
return true;
|
|
|
|
|
2020-05-26 13:08:05 +05:30
|
|
|
return modify_count(b, 1) > 1;
|
2011-11-09 15:51:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
virtual void extend(block_address extra_blocks) {
|
|
|
|
cant_recurse("extend");
|
|
|
|
recursing_lock lock(*this);
|
|
|
|
return sm_->extend(extra_blocks);
|
|
|
|
}
|
|
|
|
|
2011-11-10 20:13:15 +05:30
|
|
|
virtual void iterate(iterator &it) const {
|
|
|
|
sm_->iterate(it);
|
|
|
|
}
|
|
|
|
|
2014-02-19 21:38:05 +05:30
|
|
|
virtual void count_metadata(block_counter &bc) const {
|
|
|
|
sm_->count_metadata(bc);
|
|
|
|
}
|
|
|
|
|
2011-11-16 18:47:23 +05:30
|
|
|
virtual size_t root_size() const {
|
2011-11-09 15:51:25 +05:30
|
|
|
cant_recurse("root_size");
|
|
|
|
recursing_const_lock lock(*this);
|
|
|
|
return sm_->root_size();
|
|
|
|
}
|
|
|
|
|
2011-11-16 18:47:23 +05:30
|
|
|
virtual void copy_root(void *dest, size_t len) const {
|
2011-11-09 15:51:25 +05:30
|
|
|
cant_recurse("copy_root");
|
|
|
|
recursing_const_lock lock(*this);
|
|
|
|
return sm_->copy_root(dest, len);
|
|
|
|
}
|
|
|
|
|
2013-12-11 22:58:14 +05:30
|
|
|
virtual void visit(space_map_detail::visitor &v) const {
|
2011-11-09 15:51:25 +05:30
|
|
|
cant_recurse("check");
|
|
|
|
recursing_const_lock lock(*this);
|
2013-12-11 22:58:14 +05:30
|
|
|
return sm_->visit(v);
|
2011-11-09 15:51:25 +05:30
|
|
|
}
|
|
|
|
|
2011-11-16 18:45:32 +05:30
|
|
|
virtual checked_space_map::ptr clone() const {
|
|
|
|
return checked_space_map::ptr(new sm_recursive(sm_->clone()));
|
|
|
|
}
|
2011-11-09 15:51:25 +05:30
|
|
|
|
|
|
|
void flush_ops() {
|
2013-04-23 15:27:47 +05:30
|
|
|
if (flush_in_progress_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
flush_in_progress_ = true;
|
|
|
|
flush_ops_();
|
|
|
|
flush_in_progress_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-05-26 13:08:05 +05:30
|
|
|
uint32_t modify_count(block_address b, uint32_t count) const {
|
|
|
|
auto ops_it = ops_.find(b);
|
|
|
|
if (ops_it != ops_.end()) {
|
|
|
|
auto const &op = ops_it->second;
|
|
|
|
|
|
|
|
switch (op.op_) {
|
|
|
|
case INC:
|
|
|
|
count += op.rc_;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SET:
|
|
|
|
count = op.rc_;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2013-04-23 15:27:47 +05:30
|
|
|
void flush_ops_() {
|
2020-05-26 13:15:35 +05:30
|
|
|
recursing_lock lock(*this);
|
|
|
|
|
2020-05-26 13:00:52 +05:30
|
|
|
for (auto const &p : ops_) {
|
|
|
|
block_address b = p.first;
|
|
|
|
auto const &op = p.second;
|
|
|
|
|
|
|
|
switch (op.op_) {
|
|
|
|
case INC:
|
2020-05-26 13:54:15 +05:30
|
|
|
if (op.rc_ > 0)
|
|
|
|
sm_->inc(b, op.rc_);
|
|
|
|
else if (op.rc_ < 0)
|
|
|
|
sm_->dec(b, -op.rc_);
|
2020-05-26 13:00:52 +05:30
|
|
|
break;
|
|
|
|
|
|
|
|
case SET:
|
|
|
|
sm_->set_count(b, op.rc_);
|
|
|
|
break;
|
2011-11-09 15:51:25 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ops_.clear();
|
2013-04-23 15:27:47 +05:30
|
|
|
allocated_blocks_.clear();
|
2011-11-09 15:51:25 +05:30
|
|
|
}
|
|
|
|
|
2020-05-26 13:00:52 +05:30
|
|
|
void add_op(block_address b, block_op const &op) {
|
|
|
|
auto it = ops_.find(b);
|
2013-04-23 15:27:47 +05:30
|
|
|
|
2020-05-26 13:00:52 +05:30
|
|
|
if (it == ops_.end())
|
|
|
|
ops_.insert(make_pair(b, op));
|
|
|
|
else
|
|
|
|
it->second = it->second + op;
|
|
|
|
|
|
|
|
// FIXME: is this the best we can do?
|
|
|
|
if (op.rc_ > 0)
|
|
|
|
allocated_blocks_.add(b, b + 1);
|
2011-11-09 15:51:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void cant_recurse(string const &method) const {
|
2011-11-10 20:13:15 +05:30
|
|
|
if (depth_)
|
2011-11-09 15:51:25 +05:30
|
|
|
throw runtime_error("recursive '" + method + "' not supported");
|
|
|
|
}
|
|
|
|
|
|
|
|
struct recursing_lock {
|
|
|
|
recursing_lock(sm_recursive &smr)
|
|
|
|
: smr_(smr) {
|
2011-11-10 20:13:15 +05:30
|
|
|
smr_.depth_++;
|
2011-11-09 15:51:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
~recursing_lock() {
|
2011-11-10 20:13:15 +05:30
|
|
|
if (!--smr_.depth_)
|
|
|
|
smr_.flush_ops();
|
2011-11-09 15:51:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
sm_recursive &smr_;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct recursing_const_lock {
|
|
|
|
recursing_const_lock(sm_recursive const &smr)
|
|
|
|
: smr_(smr) {
|
2011-11-10 20:13:15 +05:30
|
|
|
smr_.depth_++;
|
2011-11-09 15:51:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
~recursing_const_lock() {
|
2011-11-10 20:13:15 +05:30
|
|
|
smr_.depth_--;
|
2011-11-09 15:51:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
sm_recursive const &smr_;
|
|
|
|
};
|
|
|
|
|
|
|
|
checked_space_map::ptr sm_;
|
2011-11-10 20:13:15 +05:30
|
|
|
mutable int depth_;
|
2011-11-09 15:51:25 +05:30
|
|
|
|
2020-05-26 13:00:52 +05:30
|
|
|
map<block_address, block_op> ops_;
|
2013-04-23 15:27:47 +05:30
|
|
|
|
|
|
|
subtracting_span_iterator::block_set allocated_blocks_;
|
|
|
|
bool flush_in_progress_;
|
2011-11-09 15:51:25 +05:30
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
checked_space_map::ptr
|
|
|
|
persistent_data::create_recursive_sm(checked_space_map::ptr sm)
|
|
|
|
{
|
|
|
|
return checked_space_map::ptr(new sm_recursive(sm));
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|