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
|
|
|
#ifndef BLOCK_H
|
|
|
|
#define BLOCK_H
|
|
|
|
|
2013-01-02 18:25:41 +05:30
|
|
|
#include "persistent-data/cache.h"
|
2013-01-07 20:29:41 +05:30
|
|
|
#include "persistent-data/lock_tracker.h"
|
2011-10-24 22:34:19 +05:30
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
#include <stdint.h>
|
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
#include <boost/optional.hpp>
|
|
|
|
#include <boost/shared_ptr.hpp>
|
|
|
|
|
2011-07-14 18:35:07 +05:30
|
|
|
#include <string.h>
|
2012-05-17 17:35:26 +05:30
|
|
|
#include <malloc.h>
|
2011-07-14 18:35:07 +05:30
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace persistent_data {
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
uint32_t const MD_BLOCK_SIZE = 4096;
|
|
|
|
|
2011-10-24 22:34:19 +05:30
|
|
|
typedef uint64_t block_address;
|
|
|
|
|
2012-05-17 17:35:26 +05:30
|
|
|
template <uint32_t BlockSize = MD_BLOCK_SIZE, uint32_t Alignment = 512>
|
|
|
|
class buffer : private boost::noncopyable {
|
|
|
|
public:
|
|
|
|
unsigned char &operator[](unsigned index) {
|
|
|
|
if (index >= BlockSize)
|
|
|
|
throw std::runtime_error("buffer index out of bounds");
|
|
|
|
|
|
|
|
return data_[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char const &operator[](unsigned index) const {
|
|
|
|
if (index >= BlockSize)
|
|
|
|
throw std::runtime_error("buffer index out of bounds");
|
|
|
|
|
|
|
|
return data_[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char *raw() {
|
|
|
|
return data_;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char const *raw() const {
|
|
|
|
return data_;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *operator new(size_t s) {
|
|
|
|
return ::memalign(Alignment, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void operator delete(void *p) {
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
unsigned char data_[BlockSize];
|
|
|
|
};
|
|
|
|
|
|
|
|
template <uint32_t BlockSize = MD_BLOCK_SIZE>
|
2011-10-24 22:34:19 +05:30
|
|
|
class block_io : private boost::noncopyable {
|
2011-07-14 18:35:07 +05:30
|
|
|
public:
|
2011-10-24 22:34:19 +05:30
|
|
|
typedef boost::shared_ptr<block_io> ptr;
|
2011-07-14 18:35:07 +05:30
|
|
|
|
2011-10-24 22:34:19 +05:30
|
|
|
block_io(std::string const &path, block_address nr_blocks, bool writeable = false);
|
|
|
|
~block_io();
|
|
|
|
|
|
|
|
block_address get_nr_blocks() const {
|
|
|
|
return nr_blocks_;
|
2011-07-14 18:35:07 +05:30
|
|
|
}
|
|
|
|
|
2012-05-17 17:35:26 +05:30
|
|
|
void read_buffer(block_address location, buffer<BlockSize> &buf) const;
|
|
|
|
void write_buffer(block_address location, buffer<BlockSize> const &buf);
|
2011-10-24 22:34:19 +05:30
|
|
|
|
2011-07-14 18:35:07 +05:30
|
|
|
private:
|
2011-10-24 22:34:19 +05:30
|
|
|
int fd_;
|
|
|
|
block_address nr_blocks_;
|
|
|
|
bool writeable_;
|
2011-07-14 18:35:07 +05:30
|
|
|
};
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <uint32_t BlockSize = MD_BLOCK_SIZE>
|
2011-06-23 19:17:08 +05:30
|
|
|
class block_manager : private boost::noncopyable {
|
|
|
|
public:
|
|
|
|
typedef boost::shared_ptr<block_manager> ptr;
|
|
|
|
|
2011-10-24 22:34:19 +05:30
|
|
|
block_manager(std::string const &path,
|
|
|
|
block_address nr_blocks,
|
|
|
|
unsigned max_concurrent_locks,
|
|
|
|
bool writeable = false);
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-07-13 19:39:33 +05:30
|
|
|
class validator {
|
2011-06-23 19:17:08 +05:30
|
|
|
public:
|
2011-07-13 19:39:33 +05:30
|
|
|
typedef boost::shared_ptr<validator> ptr;
|
2011-06-27 15:15:30 +05:30
|
|
|
|
2011-07-13 19:39:33 +05:30
|
|
|
virtual ~validator() {}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2012-05-17 17:35:26 +05:30
|
|
|
virtual void check(buffer<BlockSize> const &b, block_address location) const = 0;
|
|
|
|
virtual void prepare(buffer<BlockSize> &b, block_address location) const = 0;
|
2011-06-23 19:17:08 +05:30
|
|
|
};
|
|
|
|
|
2011-08-31 17:48:28 +05:30
|
|
|
class noop_validator : public validator {
|
|
|
|
public:
|
2012-05-17 17:35:26 +05:30
|
|
|
void check(buffer<BlockSize> const &b, block_address location) const {}
|
|
|
|
void prepare(buffer<BlockSize> &b, block_address location) const {}
|
2011-08-31 17:48:28 +05:30
|
|
|
};
|
|
|
|
|
2011-08-31 17:52:37 +05:30
|
|
|
enum block_type {
|
|
|
|
BT_SUPERBLOCK,
|
|
|
|
BT_NORMAL
|
|
|
|
};
|
|
|
|
|
2012-05-17 17:35:26 +05:30
|
|
|
struct block : private boost::noncopyable {
|
2011-07-13 19:39:33 +05:30
|
|
|
typedef boost::shared_ptr<block> ptr;
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-10-24 22:34:19 +05:30
|
|
|
block(typename block_io<BlockSize>::ptr io,
|
|
|
|
block_address location,
|
2011-08-31 17:52:37 +05:30
|
|
|
block_type bt,
|
2011-10-24 22:34:19 +05:30
|
|
|
typename validator::ptr v,
|
|
|
|
bool zero = false);
|
|
|
|
~block();
|
|
|
|
|
|
|
|
void check_read_lockable() const {
|
|
|
|
// FIXME: finish
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
2011-10-24 22:34:19 +05:30
|
|
|
void check_write_lockable() const {
|
|
|
|
// FIXME: finish
|
|
|
|
}
|
|
|
|
|
|
|
|
void flush();
|
|
|
|
|
|
|
|
typename block_io<BlockSize>::ptr io_;
|
2011-06-23 19:17:08 +05:30
|
|
|
block_address location_;
|
2012-05-17 17:35:26 +05:30
|
|
|
std::auto_ptr<buffer<BlockSize> > data_;
|
2011-08-31 17:48:28 +05:30
|
|
|
typename validator::ptr validator_;
|
2011-08-31 17:52:37 +05:30
|
|
|
block_type bt_;
|
2011-10-24 22:34:19 +05:30
|
|
|
bool dirty_;
|
2011-06-23 19:17:08 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
class read_ref {
|
|
|
|
public:
|
2011-10-24 22:34:19 +05:30
|
|
|
read_ref(block_manager<BlockSize> const &bm,
|
2013-01-10 16:32:06 +05:30
|
|
|
typename block::ptr b);
|
2011-10-24 22:34:19 +05:30
|
|
|
read_ref(read_ref const &rhs);
|
|
|
|
virtual ~read_ref();
|
|
|
|
|
|
|
|
read_ref const &operator =(read_ref const &rhs);
|
2011-06-23 19:17:08 +05:30
|
|
|
|
|
|
|
block_address get_location() const;
|
2012-05-17 17:35:26 +05:30
|
|
|
buffer<BlockSize> const &data() const;
|
2011-06-23 19:17:08 +05:30
|
|
|
|
|
|
|
protected:
|
2013-01-10 01:54:11 +05:30
|
|
|
block_manager<BlockSize> const *bm_;
|
2013-01-10 16:32:06 +05:30
|
|
|
typename block::ptr block_;
|
2011-10-24 22:34:19 +05:30
|
|
|
unsigned *holders_;
|
2011-06-23 19:17:08 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
// Inherited from read_ref, since you can read a block that's write
|
|
|
|
// locked.
|
|
|
|
class write_ref : public read_ref {
|
|
|
|
public:
|
2011-10-24 22:34:19 +05:30
|
|
|
write_ref(block_manager<BlockSize> const &bm,
|
|
|
|
typename block::ptr b);
|
2011-06-27 15:15:30 +05:30
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
using read_ref::data;
|
2012-05-17 17:35:26 +05:30
|
|
|
buffer<BlockSize> &data();
|
2011-06-23 19:17:08 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
// Locking methods
|
|
|
|
read_ref
|
2011-06-27 15:15:30 +05:30
|
|
|
read_lock(block_address location,
|
2011-08-31 17:48:28 +05:30
|
|
|
typename validator::ptr v =
|
|
|
|
typename validator::ptr(new noop_validator())) const;
|
2011-06-23 19:17:08 +05:30
|
|
|
|
|
|
|
write_ref
|
2011-06-27 15:15:30 +05:30
|
|
|
write_lock(block_address location,
|
2011-08-31 17:48:28 +05:30
|
|
|
typename validator::ptr v =
|
|
|
|
typename validator::ptr(new noop_validator()));
|
2011-06-23 19:17:08 +05:30
|
|
|
|
|
|
|
write_ref
|
2011-06-27 15:15:30 +05:30
|
|
|
write_lock_zero(block_address location,
|
2011-08-31 17:48:28 +05:30
|
|
|
typename validator::ptr v =
|
|
|
|
typename validator::ptr(new noop_validator()));
|
2011-07-14 18:35:07 +05:30
|
|
|
|
|
|
|
// The super block is the one that should be written last.
|
|
|
|
// Unlocking this block triggers the following events:
|
|
|
|
//
|
|
|
|
// i) synchronous write of all dirty blocks _except_ the
|
|
|
|
// superblock.
|
|
|
|
//
|
|
|
|
// ii) synchronous write of superblock
|
|
|
|
//
|
|
|
|
// If any locks are held at the time of the superblock
|
|
|
|
// being unlocked then an exception will be thrown.
|
|
|
|
write_ref superblock(block_address b,
|
2011-08-31 17:48:28 +05:30
|
|
|
typename validator::ptr v =
|
|
|
|
typename validator::ptr(new noop_validator()));
|
2011-07-14 18:35:07 +05:30
|
|
|
write_ref superblock_zero(block_address b,
|
2011-08-31 17:48:28 +05:30
|
|
|
typename validator::ptr v =
|
|
|
|
typename validator::ptr(new noop_validator()));
|
2011-07-14 18:35:07 +05:30
|
|
|
|
2011-07-22 20:39:56 +05:30
|
|
|
block_address get_nr_blocks() const;
|
|
|
|
|
2011-10-24 22:34:19 +05:30
|
|
|
void flush() const;
|
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
private:
|
2011-07-13 19:39:33 +05:30
|
|
|
void check(block_address b) const;
|
2013-01-10 16:32:06 +05:30
|
|
|
void write_block(typename block::ptr b) const;
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-07-22 20:39:56 +05:30
|
|
|
enum lock_type {
|
|
|
|
READ_LOCK,
|
|
|
|
WRITE_LOCK
|
|
|
|
};
|
|
|
|
|
2011-10-24 22:34:19 +05:30
|
|
|
struct cache_traits {
|
|
|
|
typedef typename block::ptr value_type;
|
|
|
|
typedef block_address key_type;
|
2011-07-22 20:39:56 +05:30
|
|
|
|
2011-10-24 22:34:19 +05:30
|
|
|
static key_type get_key(value_type const &v) {
|
|
|
|
return v->location_;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typename block_io<BlockSize>::ptr io_;
|
|
|
|
mutable base::cache<cache_traits> cache_;
|
|
|
|
|
|
|
|
// FIXME: we need a dirty list as well as a cache
|
2013-01-07 20:29:41 +05:30
|
|
|
mutable lock_tracker tracker_;
|
2011-06-23 19:17:08 +05:30
|
|
|
};
|
2011-11-03 20:14:00 +05:30
|
|
|
|
|
|
|
// A little utility to help build validators
|
|
|
|
inline block_manager<>::validator::ptr
|
|
|
|
mk_validator(block_manager<>::validator *v) {
|
|
|
|
return block_manager<>::validator::ptr(v);
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
2011-06-27 15:15:30 +05:30
|
|
|
#include "block.tcc"
|
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
#endif
|