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/>.
|
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
#include "block.h"
|
|
|
|
|
2011-08-24 18:57:45 +05:30
|
|
|
#include <errno.h>
|
2011-06-23 19:17:08 +05:30
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2013-01-15 08:03:09 +05:30
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-06-27 15:15:30 +05:30
|
|
|
#include <boost/bind.hpp>
|
2011-06-23 19:17:08 +05:30
|
|
|
#include <stdexcept>
|
2011-11-03 20:14:00 +05:30
|
|
|
#include <sstream>
|
2011-06-23 19:17:08 +05:30
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2013-05-28 18:18:10 +05:30
|
|
|
// FIXME: give this namesace a name
|
2013-01-15 08:03:09 +05:30
|
|
|
namespace {
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
int const DEFAULT_MODE = 0666;
|
2014-07-22 21:11:39 +05:30
|
|
|
unsigned const SECTOR_SHIFT = 9;
|
2013-07-09 21:22:31 +05:30
|
|
|
|
2014-07-25 15:05:04 +05:30
|
|
|
int const OPEN_FLAGS = O_DIRECT;
|
2013-01-15 08:03:09 +05:30
|
|
|
|
|
|
|
// FIXME: introduce a new exception for this, or at least lift this
|
|
|
|
// to exception.h
|
|
|
|
void syscall_failed(char const *call) {
|
|
|
|
char buffer[128];
|
|
|
|
char *msg = strerror_r(errno, buffer, sizeof(buffer));
|
|
|
|
|
|
|
|
ostringstream out;
|
|
|
|
out << "syscall '" << call << "' failed: " << msg;
|
|
|
|
throw runtime_error(out.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
int open_file(string const &path, int flags) {
|
|
|
|
int fd = ::open(path.c_str(), OPEN_FLAGS | flags, DEFAULT_MODE);
|
|
|
|
if (fd < 0)
|
|
|
|
syscall_failed("open");
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool file_exists(string const &path) {
|
2013-08-15 15:05:07 +05:30
|
|
|
struct ::stat info;
|
2013-01-15 08:03:09 +05:30
|
|
|
|
|
|
|
int r = ::stat(path.c_str(), &info);
|
|
|
|
if (r) {
|
|
|
|
if (errno == ENOENT)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
syscall_failed("stat");
|
|
|
|
return false; // never get here
|
|
|
|
|
|
|
|
} else
|
|
|
|
return S_ISREG(info.st_mode) || S_ISBLK(info.st_mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
int create_block_file(string const &path, off_t file_size) {
|
|
|
|
if (file_exists(path)) {
|
|
|
|
ostringstream out;
|
|
|
|
out << __FUNCTION__ << ": file '" << path << "' already exists";
|
|
|
|
throw runtime_error(out.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd = open_file(path, O_CREAT | O_RDWR);
|
2013-01-15 09:36:57 +05:30
|
|
|
|
2014-07-29 16:04:26 +05:30
|
|
|
int r = ::ftruncate(fd, file_size);
|
2013-01-15 08:03:09 +05:30
|
|
|
if (r < 0)
|
2014-07-29 16:04:26 +05:30
|
|
|
syscall_failed("ftruncate");
|
2013-01-15 08:03:09 +05:30
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
int open_block_file(string const &path, off_t min_size, bool writeable) {
|
|
|
|
if (!file_exists(path)) {
|
|
|
|
ostringstream out;
|
|
|
|
out << __FUNCTION__ << ": file '" << path << "' doesn't exist";
|
|
|
|
throw runtime_error(out.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
return open_file(path, writeable ? O_RDWR : O_RDONLY);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-28 18:18:10 +05:30
|
|
|
namespace persistent_data {
|
|
|
|
template <uint32_t BlockSize>
|
2014-07-28 18:43:28 +05:30
|
|
|
block_manager<BlockSize>::read_ref::read_ref(block_cache::block &b)
|
|
|
|
: b_(b)
|
2013-05-28 18:18:10 +05:30
|
|
|
{
|
2011-10-24 22:34:19 +05:30
|
|
|
}
|
|
|
|
|
2013-05-28 18:18:10 +05:30
|
|
|
template <uint32_t BlockSize>
|
|
|
|
block_manager<BlockSize>::read_ref::read_ref(read_ref const &rhs)
|
2014-07-28 18:43:28 +05:30
|
|
|
: b_(rhs.b_)
|
2013-05-28 18:18:10 +05:30
|
|
|
{
|
2014-07-28 18:43:28 +05:30
|
|
|
b_.get();
|
2011-10-24 22:34:19 +05:30
|
|
|
}
|
2013-01-10 01:54:11 +05:30
|
|
|
|
2013-05-28 18:18:10 +05:30
|
|
|
template <uint32_t BlockSize>
|
|
|
|
block_manager<BlockSize>::read_ref::~read_ref()
|
|
|
|
{
|
2014-07-28 18:43:28 +05:30
|
|
|
b_.put();
|
2013-05-28 18:18:10 +05:30
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:18:10 +05:30
|
|
|
template <uint32_t BlockSize>
|
|
|
|
typename block_manager<BlockSize>::read_ref const &
|
|
|
|
block_manager<BlockSize>::read_ref::operator =(read_ref const &rhs)
|
|
|
|
{
|
|
|
|
if (this != &rhs) {
|
2014-07-25 20:44:24 +05:30
|
|
|
b_ = rhs.b_;
|
2014-07-28 18:43:28 +05:30
|
|
|
b_.get();
|
2013-05-28 18:18:10 +05:30
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:18:10 +05:30
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
block_address
|
|
|
|
block_manager<BlockSize>::read_ref::get_location() const
|
|
|
|
{
|
2014-07-25 20:44:24 +05:30
|
|
|
return b_.get_index();
|
2013-05-28 18:18:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
2014-07-25 19:16:51 +05:30
|
|
|
void const *
|
2013-05-28 18:18:10 +05:30
|
|
|
block_manager<BlockSize>::read_ref::data() const
|
|
|
|
{
|
2014-07-25 20:44:24 +05:30
|
|
|
return b_.get_data();
|
2013-05-28 18:18:10 +05:30
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2014-07-22 21:11:39 +05:30
|
|
|
//--------------------------------
|
2011-10-24 22:34:19 +05:30
|
|
|
|
2013-05-28 18:18:10 +05:30
|
|
|
template <uint32_t BlockSize>
|
2014-07-28 18:43:28 +05:30
|
|
|
block_manager<BlockSize>::write_ref::write_ref(block_cache::block &b)
|
2014-08-21 15:47:20 +05:30
|
|
|
: read_ref(b),
|
|
|
|
ref_count_(NULL)
|
2013-05-28 18:18:10 +05:30
|
|
|
{
|
|
|
|
}
|
2011-06-27 15:15:30 +05:30
|
|
|
|
2013-05-28 18:18:10 +05:30
|
|
|
template <uint32_t BlockSize>
|
2014-08-21 15:47:20 +05:30
|
|
|
block_manager<BlockSize>::write_ref::write_ref(block_cache::block &b, unsigned &ref_count)
|
|
|
|
: read_ref(b),
|
|
|
|
ref_count_(&ref_count) {
|
|
|
|
if (*ref_count_)
|
|
|
|
throw std::runtime_error("superblock already locked");
|
|
|
|
(*ref_count_)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
block_manager<BlockSize>::write_ref::write_ref(write_ref const &rhs)
|
|
|
|
: read_ref(rhs),
|
|
|
|
ref_count_(rhs.ref_count_) {
|
|
|
|
if (ref_count_)
|
|
|
|
(*ref_count_)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
block_manager<BlockSize>::write_ref::~write_ref()
|
2013-05-28 18:18:10 +05:30
|
|
|
{
|
2014-08-21 15:47:20 +05:30
|
|
|
if (ref_count_) {
|
|
|
|
if (!*ref_count_)
|
|
|
|
throw std::runtime_error("write_ref ref_count going below zero");
|
|
|
|
|
|
|
|
(*ref_count_)--;
|
|
|
|
}
|
2014-07-25 20:44:24 +05:30
|
|
|
}
|
|
|
|
|
2014-08-21 15:47:20 +05:30
|
|
|
template <uint32_t BlockSize>
|
|
|
|
typename block_manager<BlockSize>::write_ref const &
|
|
|
|
block_manager<BlockSize>::write_ref::operator =(write_ref const &rhs)
|
|
|
|
{
|
|
|
|
if (&rhs != this) {
|
|
|
|
read_ref::operator =(rhs);
|
|
|
|
ref_count_ = rhs.ref_count_;
|
|
|
|
if (ref_count_)
|
|
|
|
(*ref_count_)++;
|
|
|
|
}
|
|
|
|
}
|
2014-07-25 20:44:24 +05:30
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
2014-08-21 15:47:20 +05:30
|
|
|
void *
|
|
|
|
block_manager<BlockSize>::write_ref::data()
|
2014-07-28 18:43:28 +05:30
|
|
|
{
|
2014-08-21 15:47:20 +05:30
|
|
|
return read_ref::b_.get_data();
|
2013-05-28 18:18:10 +05:30
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2014-07-22 21:11:39 +05:30
|
|
|
//----------------------------------------------------------------
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:18:10 +05:30
|
|
|
template <uint32_t BlockSize>
|
|
|
|
block_manager<BlockSize>::block_manager(std::string const &path,
|
|
|
|
block_address nr_blocks,
|
|
|
|
unsigned max_concurrent_blocks,
|
2014-07-22 21:11:39 +05:30
|
|
|
mode m)
|
2014-07-29 16:04:26 +05:30
|
|
|
: fd_(open_or_create_block_file(path, nr_blocks * BlockSize, m)),
|
2014-08-21 15:47:20 +05:30
|
|
|
bc_(fd_, BlockSize >> SECTOR_SHIFT, nr_blocks, 1024u * 1024u * 16),
|
|
|
|
superblock_ref_count_(0)
|
2013-05-28 18:18:10 +05:30
|
|
|
{
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2014-07-29 16:04:26 +05:30
|
|
|
template <uint32_t BlockSize>
|
|
|
|
int
|
|
|
|
block_manager<BlockSize>::open_or_create_block_file(string const &path, off_t file_size, mode m)
|
|
|
|
{
|
|
|
|
switch (m) {
|
|
|
|
case READ_ONLY:
|
|
|
|
return open_block_file(path, file_size, false);
|
|
|
|
|
|
|
|
case READ_WRITE:
|
|
|
|
return open_block_file(path, file_size, true);
|
|
|
|
|
|
|
|
case CREATE:
|
|
|
|
return create_block_file(path, file_size);
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw std::runtime_error("unsupported mode");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-28 18:18:10 +05:30
|
|
|
template <uint32_t BlockSize>
|
|
|
|
typename block_manager<BlockSize>::read_ref
|
|
|
|
block_manager<BlockSize>::read_lock(block_address location,
|
2014-07-25 19:16:51 +05:30
|
|
|
typename bcache::validator::ptr v) const
|
2013-05-28 18:18:10 +05:30
|
|
|
{
|
2014-07-28 18:43:28 +05:30
|
|
|
block_cache::block &b = bc_.get(location, 0, v);
|
|
|
|
return read_ref(b);
|
2013-05-28 18:18:10 +05:30
|
|
|
}
|
2011-07-13 19:39:33 +05:30
|
|
|
|
2013-05-28 18:18:10 +05:30
|
|
|
template <uint32_t BlockSize>
|
|
|
|
typename block_manager<BlockSize>::write_ref
|
|
|
|
block_manager<BlockSize>::write_lock(block_address location,
|
2014-07-25 19:16:51 +05:30
|
|
|
typename bcache::validator::ptr v)
|
2013-05-28 18:18:10 +05:30
|
|
|
{
|
2014-07-28 18:43:28 +05:30
|
|
|
block_cache::block &b = bc_.get(location, block_cache::GF_DIRTY, v);
|
|
|
|
return write_ref(b);
|
2011-10-24 22:34:19 +05:30
|
|
|
}
|
|
|
|
|
2013-05-28 18:18:10 +05:30
|
|
|
template <uint32_t BlockSize>
|
|
|
|
typename block_manager<BlockSize>::write_ref
|
|
|
|
block_manager<BlockSize>::write_lock_zero(block_address location,
|
2014-07-25 19:16:51 +05:30
|
|
|
typename bcache::validator::ptr v)
|
2013-05-28 18:18:10 +05:30
|
|
|
{
|
2014-07-28 18:43:28 +05:30
|
|
|
block_cache::block &b = bc_.get(location, block_cache::GF_ZERO, v);
|
|
|
|
return write_ref(b);
|
2013-01-07 20:29:41 +05:30
|
|
|
}
|
2011-10-24 22:34:19 +05:30
|
|
|
|
2013-05-28 18:18:10 +05:30
|
|
|
template <uint32_t BlockSize>
|
|
|
|
typename block_manager<BlockSize>::write_ref
|
|
|
|
block_manager<BlockSize>::superblock(block_address location,
|
2014-07-25 19:16:51 +05:30
|
|
|
typename bcache::validator::ptr v)
|
2013-05-28 18:18:10 +05:30
|
|
|
{
|
2014-07-28 18:43:28 +05:30
|
|
|
block_cache::block &b = bc_.get(location, block_cache::GF_BARRIER, v);
|
2014-08-21 15:47:20 +05:30
|
|
|
return write_ref(b, superblock_ref_count_);
|
2013-01-07 20:29:41 +05:30
|
|
|
}
|
2011-07-14 18:35:07 +05:30
|
|
|
|
2013-05-28 18:18:10 +05:30
|
|
|
template <uint32_t BlockSize>
|
|
|
|
typename block_manager<BlockSize>::write_ref
|
|
|
|
block_manager<BlockSize>::superblock_zero(block_address location,
|
2014-07-25 19:16:51 +05:30
|
|
|
typename bcache::validator::ptr v)
|
2013-05-28 18:18:10 +05:30
|
|
|
{
|
2014-07-28 18:43:28 +05:30
|
|
|
block_cache::block &b = bc_.get(location, block_cache::GF_ZERO | block_cache::GF_BARRIER, v);
|
2014-08-21 15:47:20 +05:30
|
|
|
return write_ref(b, superblock_ref_count_);
|
2013-05-28 18:18:10 +05:30
|
|
|
}
|
2011-07-13 19:39:33 +05:30
|
|
|
|
2013-05-28 18:18:10 +05:30
|
|
|
template <uint32_t BlockSize>
|
|
|
|
block_address
|
|
|
|
block_manager<BlockSize>::get_nr_blocks() const
|
|
|
|
{
|
2014-07-25 15:05:04 +05:30
|
|
|
return bc_.get_nr_blocks();
|
2013-05-28 18:18:10 +05:30
|
|
|
}
|
2011-07-22 20:39:56 +05:30
|
|
|
|
2014-07-25 21:02:59 +05:30
|
|
|
template <uint32_t BlockSize>
|
|
|
|
void
|
|
|
|
block_manager<BlockSize>::prefetch(block_address b) const
|
|
|
|
{
|
|
|
|
bc_.prefetch(b);
|
|
|
|
}
|
|
|
|
|
2013-05-28 18:18:10 +05:30
|
|
|
template <uint32_t BlockSize>
|
|
|
|
void
|
|
|
|
block_manager<BlockSize>::flush() const
|
|
|
|
{
|
2014-07-25 15:05:04 +05:30
|
|
|
bc_.flush();
|
2013-05-28 18:18:10 +05:30
|
|
|
}
|
2013-04-29 17:01:30 +05:30
|
|
|
}
|
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
//----------------------------------------------------------------
|