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-02 18:25:41 +05:30
|
|
|
#include "persistent-data/transaction_manager.h"
|
2011-06-23 19:17:08 +05:30
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
using namespace boost;
|
|
|
|
using namespace persistent_data;
|
2011-07-14 20:01:00 +05:30
|
|
|
using namespace std;
|
2011-06-23 19:17:08 +05:30
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2020-04-30 19:00:01 +05:30
|
|
|
transaction_manager::transaction_manager(block_manager::ptr bm,
|
2011-08-31 17:18:41 +05:30
|
|
|
space_map::ptr sm)
|
2011-06-23 19:17:08 +05:30
|
|
|
: bm_(bm),
|
|
|
|
sm_(sm)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
transaction_manager::~transaction_manager()
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-08-16 13:30:04 +05:30
|
|
|
void
|
|
|
|
transaction_manager::commit()
|
|
|
|
{
|
|
|
|
wipe_shadow_table();
|
|
|
|
bm_->flush();
|
|
|
|
}
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
transaction_manager::write_ref
|
|
|
|
transaction_manager::begin(block_address superblock, validator v)
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
2011-08-24 18:57:45 +05:30
|
|
|
write_ref wr = bm_->superblock(superblock, v);
|
2011-07-14 20:01:00 +05:30
|
|
|
wipe_shadow_table();
|
|
|
|
return wr;
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
transaction_manager::write_ref
|
|
|
|
transaction_manager::new_block(validator v)
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
2011-11-16 18:23:37 +05:30
|
|
|
optional<block_address> mb = sm_->new_block();
|
|
|
|
if (!mb)
|
2013-04-23 15:27:47 +05:30
|
|
|
throw runtime_error("transaction_manager::new_block() couldn't allocate new block");
|
2011-11-16 18:23:37 +05:30
|
|
|
|
|
|
|
sm_decrementer decrementer(sm_, *mb);
|
|
|
|
write_ref wr = bm_->write_lock_zero(*mb, v);
|
|
|
|
add_shadow(*mb);
|
2011-11-08 16:38:51 +05:30
|
|
|
decrementer.dont_bother();
|
|
|
|
return wr;
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
pair<transaction_manager::write_ref, bool>
|
|
|
|
transaction_manager::shadow(block_address orig, validator v)
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
2020-05-28 12:08:58 +05:30
|
|
|
bool need_inc = sm_->get_count(orig) > 1;
|
2013-04-23 15:27:47 +05:30
|
|
|
if (is_shadow(orig) && !need_inc)
|
|
|
|
return make_pair(bm_->write_lock(orig, v), need_inc);
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-08-24 18:57:45 +05:30
|
|
|
read_ref src = bm_->read_lock(orig, v);
|
2011-11-16 18:23:37 +05:30
|
|
|
optional<block_address> mb = sm_->new_block();
|
|
|
|
if (!mb)
|
2013-04-23 15:27:47 +05:30
|
|
|
throw runtime_error("transaction_manager::shadow() couldn't allocate new block");
|
2011-11-16 18:23:37 +05:30
|
|
|
|
|
|
|
write_ref dest = bm_->write_lock_zero(*mb, v);
|
2014-07-25 19:16:51 +05:30
|
|
|
::memcpy(dest.data(), src.data(), MD_BLOCK_SIZE);
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-07-14 20:01:00 +05:30
|
|
|
sm_->dec(orig);
|
2011-08-31 17:18:41 +05:30
|
|
|
add_shadow(dest.get_location());
|
2013-04-23 15:27:47 +05:30
|
|
|
return make_pair(dest, need_inc);
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
transaction_manager::read_ref
|
|
|
|
transaction_manager::read_lock(block_address b)
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
|
|
|
return bm_->read_lock(b);
|
|
|
|
}
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
transaction_manager::read_ref
|
|
|
|
transaction_manager::read_lock(block_address b, validator v)
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
|
|
|
return bm_->read_lock(b, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-08-31 17:18:41 +05:30
|
|
|
transaction_manager::add_shadow(block_address b)
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
2011-07-14 20:01:00 +05:30
|
|
|
shadows_.insert(b);
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-08-31 17:18:41 +05:30
|
|
|
transaction_manager::remove_shadow(block_address b)
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
2011-07-14 20:01:00 +05:30
|
|
|
shadows_.erase(b);
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2011-08-31 17:18:41 +05:30
|
|
|
transaction_manager::is_shadow(block_address b) const
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
|
|
|
return shadows_.count(b) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-08-31 17:18:41 +05:30
|
|
|
transaction_manager::wipe_shadow_table()
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
|
|
|
shadows_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|