2011-06-23 19:17:08 +05:30
|
|
|
#ifndef MULTISNAP_METADATA_H
|
|
|
|
#define MULTISNAP_METADATA_H
|
|
|
|
|
|
|
|
#include "block.h"
|
|
|
|
#include "btree.h"
|
2011-10-10 20:29:37 +05:30
|
|
|
#include "emitter.h"
|
2011-08-22 15:12:13 +05:30
|
|
|
#include "endian_utils.h"
|
2011-08-24 15:15:39 +05:30
|
|
|
#include "error_set.h"
|
2011-07-22 20:39:56 +05:30
|
|
|
#include "metadata_disk_structures.h"
|
2011-08-24 15:15:39 +05:30
|
|
|
#include "space_map_disk.h"
|
|
|
|
#include "transaction_manager.h"
|
2011-06-23 19:17:08 +05:30
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <boost/shared_ptr.hpp>
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2011-07-22 20:39:56 +05:30
|
|
|
namespace thin_provisioning {
|
|
|
|
// FIXME: don't use namespaces in a header
|
|
|
|
using namespace base;
|
|
|
|
using namespace persistent_data;
|
2011-06-23 19:17:08 +05:30
|
|
|
|
|
|
|
typedef uint64_t sector_t;
|
2011-07-22 20:39:56 +05:30
|
|
|
typedef uint32_t thin_dev_t;
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-07-22 20:39:56 +05:30
|
|
|
//------------------------------------------------
|
2011-06-27 15:15:30 +05:30
|
|
|
|
2011-07-22 20:39:56 +05:30
|
|
|
class space_map_ref_counter {
|
|
|
|
public:
|
|
|
|
space_map_ref_counter(space_map::ptr sm)
|
|
|
|
: sm_(sm) {
|
|
|
|
}
|
2011-06-27 15:15:30 +05:30
|
|
|
|
2011-07-22 20:39:56 +05:30
|
|
|
void inc(block_address b) {
|
|
|
|
sm_->inc(b);
|
|
|
|
}
|
2011-06-27 15:15:30 +05:30
|
|
|
|
2011-07-22 20:39:56 +05:30
|
|
|
void dec(block_address b) {
|
|
|
|
sm_->dec(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
space_map::ptr sm_;
|
|
|
|
};
|
|
|
|
|
2011-08-24 15:15:39 +05:30
|
|
|
struct block_time {
|
|
|
|
uint64_t block_;
|
|
|
|
uint32_t time_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class block_time_ref_counter {
|
|
|
|
public:
|
|
|
|
block_time_ref_counter(space_map::ptr sm)
|
|
|
|
: sm_(sm) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void inc(block_time bt) {
|
|
|
|
sm_->inc(bt.block_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dec(block_time bt) {
|
|
|
|
sm_->dec(bt.block_);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
space_map::ptr sm_;
|
|
|
|
};
|
|
|
|
|
2011-07-22 20:39:56 +05:30
|
|
|
struct block_traits {
|
|
|
|
typedef base::__le64 disk_type;
|
2011-08-24 15:15:39 +05:30
|
|
|
typedef block_time value_type;
|
|
|
|
typedef block_time_ref_counter ref_counter;
|
2011-06-27 15:15:30 +05:30
|
|
|
|
2011-07-22 20:39:56 +05:30
|
|
|
static void unpack(disk_type const &disk, value_type &value) {
|
2011-08-24 15:15:39 +05:30
|
|
|
uint64_t v = to_cpu<uint64_t>(disk);
|
|
|
|
value.block_ = v >> 24;
|
|
|
|
value.time_ = v & ((1 << 24) - 1);
|
2011-07-22 20:39:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void pack(value_type const &value, disk_type &disk) {
|
2011-08-24 15:15:39 +05:30
|
|
|
uint64_t v = (value.block_ << 24) | value.time_;
|
|
|
|
disk = base::to_disk<base::__le64>(v);
|
2011-06-27 15:15:30 +05:30
|
|
|
}
|
|
|
|
};
|
2011-07-22 20:39:56 +05:30
|
|
|
|
|
|
|
//------------------------------------------------
|
|
|
|
|
|
|
|
class mtree_ref_counter {
|
2011-06-27 15:15:30 +05:30
|
|
|
public:
|
2011-08-31 17:18:41 +05:30
|
|
|
mtree_ref_counter(transaction_manager::ptr tm)
|
2011-07-22 20:39:56 +05:30
|
|
|
: tm_(tm) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void inc(block_address b) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void dec(block_address b) {
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2011-08-31 17:18:41 +05:30
|
|
|
transaction_manager::ptr tm_;
|
2011-07-22 20:39:56 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
struct mtree_traits {
|
2011-06-27 15:15:30 +05:30
|
|
|
typedef base::__le64 disk_type;
|
2011-07-22 20:39:56 +05:30
|
|
|
typedef uint64_t value_type;
|
2011-08-31 17:18:41 +05:30
|
|
|
typedef mtree_ref_counter ref_counter;
|
2011-06-27 15:15:30 +05:30
|
|
|
|
2011-07-22 20:39:56 +05:30
|
|
|
static void unpack(disk_type const &disk, value_type &value) {
|
|
|
|
value = base::to_cpu<uint64_t>(disk);
|
|
|
|
}
|
2011-06-27 15:15:30 +05:30
|
|
|
|
2011-07-22 20:39:56 +05:30
|
|
|
static void pack(value_type const &value, disk_type &disk) {
|
|
|
|
disk = base::to_disk<base::__le64>(value);
|
2011-06-27 15:15:30 +05:30
|
|
|
}
|
|
|
|
};
|
2011-07-22 20:39:56 +05:30
|
|
|
|
|
|
|
class metadata;
|
|
|
|
class thin {
|
|
|
|
public:
|
|
|
|
typedef boost::shared_ptr<thin> ptr;
|
2011-08-24 15:15:39 +05:30
|
|
|
typedef boost::optional<block_time> maybe_address;
|
2011-07-22 20:39:56 +05:30
|
|
|
|
|
|
|
thin_dev_t get_dev_t() const;
|
|
|
|
maybe_address lookup(block_address thin_block);
|
|
|
|
void insert(block_address thin_block, block_address data_block);
|
|
|
|
void remove(block_address thin_block);
|
|
|
|
|
|
|
|
void set_snapshot_time(uint32_t time);
|
|
|
|
|
|
|
|
block_address get_mapped_blocks() const;
|
|
|
|
void set_mapped_blocks(block_address count);
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class metadata;
|
|
|
|
thin(thin_dev_t dev, metadata *metadata);
|
|
|
|
|
|
|
|
thin_dev_t dev_;
|
|
|
|
metadata *metadata_;
|
|
|
|
};
|
2011-06-27 15:15:30 +05:30
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
class metadata {
|
|
|
|
public:
|
|
|
|
typedef boost::shared_ptr<metadata> ptr;
|
2011-08-31 17:18:41 +05:30
|
|
|
typedef block_manager<>::read_ref read_ref;
|
|
|
|
typedef block_manager<>::write_ref write_ref;
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-08-22 15:12:13 +05:30
|
|
|
metadata(std::string const &dev_path);
|
2011-06-23 19:17:08 +05:30
|
|
|
~metadata();
|
|
|
|
|
|
|
|
void commit();
|
|
|
|
|
2011-07-22 20:39:56 +05:30
|
|
|
void create_thin(thin_dev_t dev);
|
|
|
|
void create_snap(thin_dev_t dev, thin_dev_t origin);
|
|
|
|
void del(thin_dev_t);
|
2011-06-23 19:17:08 +05:30
|
|
|
|
|
|
|
void set_transaction_id(uint64_t id);
|
|
|
|
uint64_t get_transaction_id() const;
|
|
|
|
|
|
|
|
block_address get_held_root() const;
|
|
|
|
|
|
|
|
block_address alloc_data_block();
|
|
|
|
void free_data_block(block_address b);
|
|
|
|
|
|
|
|
// accessors
|
|
|
|
block_address get_nr_free_data_blocks() const;
|
|
|
|
sector_t get_data_block_size() const;
|
|
|
|
block_address get_data_dev_size() const;
|
|
|
|
|
2011-07-22 20:39:56 +05:30
|
|
|
thin::ptr open_thin(thin_dev_t);
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
// Validation
|
2011-08-24 15:15:39 +05:30
|
|
|
boost::optional<persistent_data::error_set::ptr> check();
|
2011-08-23 16:25:37 +05:30
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
// Dumping metadata
|
2011-10-10 20:29:37 +05:30
|
|
|
void dump(emitter::ptr e);
|
2011-08-31 17:18:41 +05:30
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
private:
|
|
|
|
friend class thin;
|
|
|
|
|
2011-07-22 20:39:56 +05:30
|
|
|
bool device_exists(thin_dev_t dev) const;
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
typedef persistent_data::transaction_manager::ptr tm_ptr;
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
typedef persistent_data::btree<1, device_details_traits> detail_tree;
|
|
|
|
typedef persistent_data::btree<1, mtree_traits> dev_tree;
|
|
|
|
typedef persistent_data::btree<2, block_traits> mapping_tree;
|
|
|
|
typedef persistent_data::btree<1, block_traits> single_mapping_tree;
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-08-23 16:25:37 +05:30
|
|
|
// Declaration order is important here
|
2011-07-22 20:39:56 +05:30
|
|
|
tm_ptr tm_;
|
2011-08-23 16:25:37 +05:30
|
|
|
superblock sb_;
|
2011-08-22 15:12:13 +05:30
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
checked_space_map::ptr metadata_sm_;
|
|
|
|
checked_space_map::ptr data_sm_;
|
2011-06-23 19:17:08 +05:30
|
|
|
detail_tree details_;
|
|
|
|
dev_tree mappings_top_level_;
|
|
|
|
mapping_tree mappings_;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
#endif
|