start removing block_manager<>::block

This commit is contained in:
Joe Thornber
2014-07-25 14:46:51 +01:00
parent b32908d5c2
commit 7e870ea5a6
18 changed files with 208 additions and 183 deletions

View File

@@ -150,6 +150,7 @@ namespace bcache {
int r;
iocb *control_blocks[1];
// FIXME: put this back in
assert(!test_flags(b, IO_PENDING));
set_flags(b, IO_PENDING);
nr_io_pending_++;
@@ -175,13 +176,16 @@ namespace bcache {
int
block_cache::issue_read(block &b)
{
std::cerr << "issuing read - that's a shame: " << b.index_ << "\n";
assert(!test_flags(b, IO_PENDING));
return issue_low_level(b, IO_CMD_PREAD, "read");
}
int
block_cache::issue_write(block &b)
{
std::cerr << "issuing write for block " << b.index_ << "\n";
assert(!test_flags(b, IO_PENDING));
b.v_->prepare(b.data_, b.index_);
return issue_low_level(b, IO_CMD_PWRITE, "write");
}
@@ -303,7 +307,7 @@ namespace bcache {
}
block_cache::block *
block_cache::hash_lookup(block_index index)
block_cache::hash_lookup(block_address index)
{
block *b;
unsigned bucket = hash(index);
@@ -347,7 +351,7 @@ namespace bcache {
}
block_cache::block *
block_cache::new_block(block_index index)
block_cache::new_block(block_address index)
{
block *b;
@@ -485,7 +489,8 @@ namespace bcache {
}
block_cache::block *
block_cache::lookup_or_read_block(block_index index, unsigned flags)
block_cache::lookup_or_read_block(block_address index, unsigned flags,
validator::ptr v)
{
block *b = hash_lookup(index);
@@ -495,16 +500,26 @@ namespace bcache {
if (flags & GF_ZERO)
zero_block(*b);
else {
if (b->v_.get() &&
b->v_.get() != v.get() &&
test_flags(*b, DIRTY))
b->v_->prepare(b->data_, b->index_);
}
b->v_ = v;
} else {
if (flags & GF_CAN_BLOCK) {
b = new_block(index);
if (b) {
b->v_ = v;
if (flags & GF_ZERO)
zero_block(*b);
else {
issue_read(*b);
wait_specific(*b);
v->check(b->data_, b->index_);
}
}
}
@@ -514,9 +529,9 @@ namespace bcache {
}
block_cache::block &
block_cache::get(block_index index, unsigned flags)
block_cache::get(block_address index, unsigned flags, validator::ptr v)
{
block *b = lookup_or_read_block(index, flags);
block *b = lookup_or_read_block(index, flags, v);
if (b) {
hit(*b);
@@ -549,24 +564,26 @@ namespace bcache {
int
block_cache::flush()
{
block *b;
block *b, *tmp;
list_for_each_entry (b, &dirty_, list_) {
if (b->ref_count_) {
info("attempt to lock an already locked block\n");
return -EAGAIN;
}
std::cerr << "in flush\n";
list_for_each_entry_safe (b, tmp, &dirty_, list_) {
if (b->ref_count_ || test_flags(*b, IO_PENDING))
// The superblock may well be still locked.
continue;
issue_write(*b);
}
std::cerr << "issued all writes\n";
wait_all();
std::cerr << "wait all returned\n";
return list_empty(&errored_) ? 0 : -EIO;
}
void
block_cache::prefetch(block_index index)
block_cache::prefetch(block_address index)
{
block *b = hash_lookup(index);

View File

@@ -16,23 +16,25 @@
//----------------------------------------------------------------
namespace bcache {
#if 0
typedef uint64_t block_address;
typedef uint64_t sector_t;
class validator {
public:
typedef boost::shared_ptr<validator> ptr;
virtual ~validator() {}
virtual void check(buffer<BlockSize> const &b, block_address location) const = 0;
virtual void prepare(buffer<BlockSize> &b, block_address location) const = 0;
virtual void check(void const *data, block_address location) const = 0;
virtual void prepare(void *data, block_address location) const = 0;
};
class noop_validator : public validator {
public:
void check(buffer<BlockSize> const &b, block_address location) const {}
void prepare(buffer<BlockSize> &b, block_address location) const {}
void check(void const *data, block_address location) const {}
void prepare(void *data, block_address location) const {}
};
#endif
//----------------------------------------------------------------
// FIXME: throw exceptions rather than returning errors
@@ -45,6 +47,10 @@ namespace bcache {
class block : private boost::noncopyable {
public:
block()
: v_() {
}
uint64_t get_index() const {
return index_;
}
@@ -69,11 +75,9 @@ namespace bcache {
unsigned flags_;
iocb control_block_;
validator::ptr v_;
};
typedef uint64_t block_index;
typedef uint64_t sector_t;
//--------------------------------
block_cache(int fd, sector_t block_size,
@@ -88,7 +92,7 @@ namespace bcache {
};
// FIXME: what if !GF_CAN_BLOCK?
block_cache::block &get(block_index index, unsigned flags);
block_cache::block &get(block_address index, unsigned flags, validator::ptr v);
enum put_flags {
PF_DIRTY = (1 << 0),
@@ -101,7 +105,7 @@ namespace bcache {
* failed. Make sure you build your recovery with this in mind.
*/
int flush();
void prefetch(block_index index);
void prefetch(block_address index);
private:
int init_free_list(unsigned count);
@@ -118,16 +122,16 @@ namespace bcache {
unsigned writeback(unsigned count);
void hash_init(unsigned nr_buckets);
unsigned hash(uint64_t index);
block *hash_lookup(block_index index);
block *hash_lookup(block_address index);
void hash_insert(block &b);
void hash_remove(block &b);
void setup_control_block(block &b);
block *new_block(block_index index);
block *new_block(block_address index);
void mark_dirty(block &b);
unsigned calc_nr_cache_blocks(size_t mem, sector_t block_size);
unsigned calc_nr_buckets(unsigned nr_blocks);
void zero_block(block &b);
block *lookup_or_read_block(block_index index, unsigned flags);
block *lookup_or_read_block(block_address index, unsigned flags, validator::ptr v);
unsigned test_flags(block &b, unsigned flags);
void clear_flags(block &b, unsigned flags);
void set_flags(block &b, unsigned flags);
@@ -154,11 +158,11 @@ namespace bcache {
list_head dirty_;
list_head clean_;
unsigned nr_dirty_;
unsigned nr_io_pending_;
struct list_head io_pending_;
unsigned nr_dirty_;
/*
* Hash table fields.
*/
@@ -166,6 +170,60 @@ namespace bcache {
unsigned mask_;
std::vector<list_head> buckets_;
};
#if 0
class auto_lock {
public:
auto_lock(block_cache &bc, block_address index, bool zero, validator::ptr v, unsigned put_flags)
: bc_(bc),
b_(bc.get(index, (zero ? block_cache::GF_ZERO : 0) | block_cache::GF_CAN_BLOCK, v)),
put_flags_(put_flags),
holders_(new unsigned) {
*holders_ = 1;
}
virtual ~auto_lock() {
bc_.put(b_, put_flags_);
}
auto_lock operator =(auto_lock const &rhs) {
if (this != &rhs) {
bc_ = rhs.bc_;
void const *data() const {
return b_.get_data();
}
private:
block_cache &bc_;
block_cache::block &b_;
unsigned put_flags_;
unsigned *holders_;
};
class auto_read_lock : public auto_lock {
public:
auto_read_lock(block_cache &bc, block_address index, bool zero, validator::ptr v)
: auto_lock(bc, index, zero, v, 0) {
}
using auto_lock::data();
};
class auto_write_lock : public auto_lock {
public:
auto_write_lock(block_cache &bc, block_address index, bool zero, validator::ptr v)
: auto_lock(bc, index, zero, v, block_cache::DIRTY) {
}
using auto_lock::data();
void *data() {
return b_.get_data();
}
};
#endif
}
//----------------------------------------------------------------

View File

@@ -30,7 +30,7 @@
//----------------------------------------------------------------
namespace persistent_data {
namespace bcache {
uint32_t const DEFAULT_BUFFER_SIZE = 4096;
template <uint32_t Size = DEFAULT_BUFFER_SIZE>