thin-provisioning-tools/block.h

131 lines
2.7 KiB
C
Raw Normal View History

#ifndef BLOCK_H
#define BLOCK_H
#include <stdint.h>
#include <map>
#include <vector>
#include <boost/noncopyable.hpp>
#include <boost/optional.hpp>
#include <boost/shared_ptr.hpp>
//----------------------------------------------------------------
namespace persistent_data {
typedef uint64_t block_address;
template <uint32_t BlockSize>
class block_manager : private boost::noncopyable {
public:
typedef boost::shared_ptr<block_manager> ptr;
2011-07-13 19:39:33 +05:30
block_manager(std::string const &path, block_address nr_blocks);
~block_manager();
typedef unsigned char buffer[BlockSize];
typedef unsigned char const const_buffer[BlockSize];
class block;
2011-07-13 19:39:33 +05:30
class validator {
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() {}
virtual void check(block const &b) const = 0;
virtual void prepare(block &b) const = 0;
};
struct block {
2011-07-13 19:39:33 +05:30
typedef boost::shared_ptr<block> ptr;
typedef boost::optional<typename validator::ptr> maybe_validator;
block(block_address location,
maybe_validator v = maybe_validator())
: location_(location),
2011-07-13 19:39:33 +05:30
validator_(v),
initialised_(false) {
}
block_address location_;
buffer data_;
2011-06-27 15:15:30 +05:30
maybe_validator validator_;
2011-07-13 19:39:33 +05:30
bool initialised_;
};
class read_ref {
public:
2011-07-13 19:39:33 +05:30
read_ref(typename block::ptr b);
virtual ~read_ref() {}
block_address get_location() const;
const_buffer &data() const;
protected:
2011-07-13 19:39:33 +05:30
typename block::ptr block_;
};
// Inherited from read_ref, since you can read a block that's write
// locked.
class write_ref : public read_ref {
public:
2011-07-13 19:39:33 +05:30
write_ref(typename block::ptr b);
2011-06-27 15:15:30 +05:30
using read_ref::data;
buffer &data();
};
// Locking methods
read_ref
2011-07-13 19:39:33 +05:30
read_lock(block_address location) const;
boost::optional<read_ref>
2011-07-13 19:39:33 +05:30
read_try_lock(block_address location) const;
write_ref
write_lock(block_address location);
write_ref
write_lock_zero(block_address location);
// Validator variants
read_ref
2011-06-27 15:15:30 +05:30
read_lock(block_address location,
2011-07-13 19:39:33 +05:30
typename validator::ptr const &v) const;
boost::optional<read_ref>
2011-06-27 15:15:30 +05:30
read_try_lock(block_address location,
2011-07-13 19:39:33 +05:30
typename validator::ptr const &v) const;
write_ref
2011-06-27 15:15:30 +05:30
write_lock(block_address location,
2011-07-13 19:39:33 +05:30
typename validator::ptr const &v);
write_ref
2011-06-27 15:15:30 +05:30
write_lock_zero(block_address location,
2011-07-13 19:39:33 +05:30
typename validator::ptr const &v);
// Use this to commit changes
void flush(write_ref super_block);
private:
2011-07-13 19:39:33 +05:30
void check(block_address b) const;
void read_block(block &b) const;
void write_block(block const &b);
void zero_block(block &b);
void write_and_release(block *b);
int fd_;
2011-07-13 19:39:33 +05:30
block_address nr_blocks_;
};
}
2011-06-27 15:15:30 +05:30
#include "block.tcc"
//----------------------------------------------------------------
#endif