whitespace
This commit is contained in:
@ -13,6 +13,8 @@
|
|||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
|
using namespace bcache;
|
||||||
|
|
||||||
//----------------------------------------------------------------
|
//----------------------------------------------------------------
|
||||||
|
|
||||||
// FIXME: get from linux headers
|
// FIXME: get from linux headers
|
||||||
@ -53,10 +55,9 @@ namespace {
|
|||||||
|
|
||||||
//----------------------------------------------------------------
|
//----------------------------------------------------------------
|
||||||
|
|
||||||
namespace bcache {
|
int
|
||||||
int
|
block_cache::init_free_list(unsigned count)
|
||||||
block_cache::init_free_list(unsigned count)
|
{
|
||||||
{
|
|
||||||
size_t len;
|
size_t len;
|
||||||
block *blocks;
|
block *blocks;
|
||||||
size_t block_size = block_size_ << SECTOR_SHIFT;
|
size_t block_size = block_size_ << SECTOR_SHIFT;
|
||||||
@ -88,11 +89,11 @@ namespace bcache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
block_cache::block *
|
block_cache::block *
|
||||||
block_cache::__alloc_block()
|
block_cache::__alloc_block()
|
||||||
{
|
{
|
||||||
block *b;
|
block *b;
|
||||||
|
|
||||||
if (list_empty(&free_))
|
if (list_empty(&free_))
|
||||||
@ -102,9 +103,9 @@ namespace bcache {
|
|||||||
list_del(&b->list_);
|
list_del(&b->list_);
|
||||||
|
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*----------------------------------------------------------------
|
/*----------------------------------------------------------------
|
||||||
* Low level IO handling
|
* Low level IO handling
|
||||||
*
|
*
|
||||||
* We cannot have two concurrent writes on the same block.
|
* We cannot have two concurrent writes on the same block.
|
||||||
@ -117,14 +118,14 @@ namespace bcache {
|
|||||||
*
|
*
|
||||||
*--------------------------------------------------------------*/
|
*--------------------------------------------------------------*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This can be called from the context of the aio thread. So we have a
|
* This can be called from the context of the aio thread. So we have a
|
||||||
* separate 'top half' complete function that we know is only called by the
|
* separate 'top half' complete function that we know is only called by the
|
||||||
* main cache thread.
|
* main cache thread.
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
block_cache::complete_io(block &b, int result)
|
block_cache::complete_io(block &b, int result)
|
||||||
{
|
{
|
||||||
b.error_ = result;
|
b.error_ = result;
|
||||||
b.clear_flags(BF_IO_PENDING);
|
b.clear_flags(BF_IO_PENDING);
|
||||||
nr_io_pending_--;
|
nr_io_pending_--;
|
||||||
@ -139,20 +140,19 @@ namespace bcache {
|
|||||||
|
|
||||||
list_move_tail(&b.list_, &clean_);
|
list_move_tail(&b.list_, &clean_);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* |b->list| should be valid (either pointing to itself, on one of the other
|
* |b->list| should be valid (either pointing to itself, on one of the other
|
||||||
* lists.
|
* lists.
|
||||||
*/
|
*/
|
||||||
// FIXME: add batch issue
|
// FIXME: add batch issue
|
||||||
void
|
void
|
||||||
block_cache::issue_low_level(block &b, enum io_iocb_cmd opcode, const char *desc)
|
block_cache::issue_low_level(block &b, enum io_iocb_cmd opcode, const char *desc)
|
||||||
{
|
{
|
||||||
int r;
|
int r;
|
||||||
iocb *control_blocks[1];
|
iocb *control_blocks[1];
|
||||||
|
|
||||||
// FIXME: put this back in
|
|
||||||
assert(!b.test_flags(BF_IO_PENDING));
|
assert(!b.test_flags(BF_IO_PENDING));
|
||||||
b.set_flags(BF_IO_PENDING);
|
b.set_flags(BF_IO_PENDING);
|
||||||
nr_io_pending_++;
|
nr_io_pending_++;
|
||||||
@ -173,26 +173,26 @@ namespace bcache {
|
|||||||
out << "couldn't issue io (" << desc << ") for block " << b.index_;
|
out << "couldn't issue io (" << desc << ") for block " << b.index_;
|
||||||
throw std::runtime_error(out.str());
|
throw std::runtime_error(out.str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
block_cache::issue_read(block &b)
|
block_cache::issue_read(block &b)
|
||||||
{
|
{
|
||||||
assert(!b.test_flags(BF_IO_PENDING));
|
assert(!b.test_flags(BF_IO_PENDING));
|
||||||
issue_low_level(b, IO_CMD_PREAD, "read");
|
issue_low_level(b, IO_CMD_PREAD, "read");
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
block_cache::issue_write(block &b)
|
block_cache::issue_write(block &b)
|
||||||
{
|
{
|
||||||
assert(!b.test_flags(BF_IO_PENDING));
|
assert(!b.test_flags(BF_IO_PENDING));
|
||||||
b.v_->prepare(b.data_, b.index_);
|
b.v_->prepare(b.data_, b.index_);
|
||||||
issue_low_level(b, IO_CMD_PWRITE, "write");
|
issue_low_level(b, IO_CMD_PWRITE, "write");
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
block_cache::wait_io()
|
block_cache::wait_io()
|
||||||
{
|
{
|
||||||
int r;
|
int r;
|
||||||
unsigned i;
|
unsigned i;
|
||||||
|
|
||||||
@ -223,51 +223,51 @@ namespace bcache {
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*----------------------------------------------------------------
|
/*----------------------------------------------------------------
|
||||||
* Clean/dirty list management
|
* Clean/dirty list management
|
||||||
*--------------------------------------------------------------*/
|
*--------------------------------------------------------------*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* We're using lru lists atm, but I think it would be worth
|
* We're using lru lists atm, but I think it would be worth
|
||||||
* experimenting with a multiqueue approach.
|
* experimenting with a multiqueue approach.
|
||||||
*/
|
*/
|
||||||
list_head *
|
list_head *
|
||||||
block_cache::__categorise(block &b)
|
block_cache::__categorise(block &b)
|
||||||
{
|
{
|
||||||
if (b.error_)
|
if (b.error_)
|
||||||
return &errored_;
|
return &errored_;
|
||||||
|
|
||||||
return b.test_flags(BF_DIRTY) ? &dirty_ : &clean_;
|
return b.test_flags(BF_DIRTY) ? &dirty_ : &clean_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
block_cache::hit(block &b)
|
block_cache::hit(block &b)
|
||||||
{
|
{
|
||||||
list_move_tail(&b.list_, __categorise(b));
|
list_move_tail(&b.list_, __categorise(b));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*----------------------------------------------------------------
|
/*----------------------------------------------------------------
|
||||||
* High level IO handling
|
* High level IO handling
|
||||||
*--------------------------------------------------------------*/
|
*--------------------------------------------------------------*/
|
||||||
void
|
void
|
||||||
block_cache::wait_all()
|
block_cache::wait_all()
|
||||||
{
|
{
|
||||||
while (!list_empty(&io_pending_))
|
while (!list_empty(&io_pending_))
|
||||||
wait_io();
|
wait_io();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
block_cache::wait_specific(block &b)
|
block_cache::wait_specific(block &b)
|
||||||
{
|
{
|
||||||
while (b.test_flags(BF_IO_PENDING))
|
while (b.test_flags(BF_IO_PENDING))
|
||||||
wait_io();
|
wait_io();
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned
|
unsigned
|
||||||
block_cache::writeback(unsigned count)
|
block_cache::writeback(unsigned count)
|
||||||
{
|
{
|
||||||
block *b, *tmp;
|
block *b, *tmp;
|
||||||
unsigned actual = 0, dirty_length = 0;
|
unsigned actual = 0, dirty_length = 0;
|
||||||
|
|
||||||
@ -288,18 +288,18 @@ namespace bcache {
|
|||||||
|
|
||||||
info("writeback: requested %u, actual %u, dirty length %u\n", count, actual, dirty_length);
|
info("writeback: requested %u, actual %u, dirty length %u\n", count, actual, dirty_length);
|
||||||
return actual;
|
return actual;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*----------------------------------------------------------------
|
/*----------------------------------------------------------------
|
||||||
* Hash table
|
* Hash table
|
||||||
*---------------------------------------------------------------*/
|
*---------------------------------------------------------------*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* |nr_buckets| must be a power of two.
|
* |nr_buckets| must be a power of two.
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
block_cache::hash_init(unsigned nr_buckets)
|
block_cache::hash_init(unsigned nr_buckets)
|
||||||
{
|
{
|
||||||
unsigned i;
|
unsigned i;
|
||||||
|
|
||||||
nr_buckets_ = nr_buckets;
|
nr_buckets_ = nr_buckets;
|
||||||
@ -307,18 +307,18 @@ namespace bcache {
|
|||||||
|
|
||||||
for (i = 0; i < nr_buckets; i++)
|
for (i = 0; i < nr_buckets; i++)
|
||||||
INIT_LIST_HEAD(&buckets_[i]);
|
INIT_LIST_HEAD(&buckets_[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned
|
unsigned
|
||||||
block_cache::hash(uint64_t index)
|
block_cache::hash(uint64_t index)
|
||||||
{
|
{
|
||||||
const unsigned BIG_PRIME = 4294967291UL;
|
const unsigned BIG_PRIME = 4294967291UL;
|
||||||
return (((unsigned) index) * BIG_PRIME) & mask_;
|
return (((unsigned) index) * BIG_PRIME) & mask_;
|
||||||
}
|
}
|
||||||
|
|
||||||
block_cache::block *
|
block_cache::block *
|
||||||
block_cache::hash_lookup(block_address index)
|
block_cache::hash_lookup(block_address index)
|
||||||
{
|
{
|
||||||
block *b;
|
block *b;
|
||||||
unsigned bucket = hash(index);
|
unsigned bucket = hash(index);
|
||||||
|
|
||||||
@ -328,27 +328,27 @@ namespace bcache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
block_cache::hash_insert(block &b)
|
block_cache::hash_insert(block &b)
|
||||||
{
|
{
|
||||||
unsigned bucket = hash(b.index_);
|
unsigned bucket = hash(b.index_);
|
||||||
list_move_tail(&b.hash_list_, &buckets_[bucket]);
|
list_move_tail(&b.hash_list_, &buckets_[bucket]);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
block_cache::hash_remove(block &b)
|
block_cache::hash_remove(block &b)
|
||||||
{
|
{
|
||||||
list_del_init(&b.hash_list_);
|
list_del_init(&b.hash_list_);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*----------------------------------------------------------------
|
/*----------------------------------------------------------------
|
||||||
* High level allocation
|
* High level allocation
|
||||||
*--------------------------------------------------------------*/
|
*--------------------------------------------------------------*/
|
||||||
void
|
void
|
||||||
block_cache::setup_control_block(block &b)
|
block_cache::setup_control_block(block &b)
|
||||||
{
|
{
|
||||||
iocb *cb = &b.control_block_;
|
iocb *cb = &b.control_block_;
|
||||||
size_t block_size_bytes = block_size_ << SECTOR_SHIFT;
|
size_t block_size_bytes = block_size_ << SECTOR_SHIFT;
|
||||||
|
|
||||||
@ -358,11 +358,11 @@ namespace bcache {
|
|||||||
cb->u.c.buf = b.data_;
|
cb->u.c.buf = b.data_;
|
||||||
cb->u.c.offset = block_size_bytes * b.index_;
|
cb->u.c.offset = block_size_bytes * b.index_;
|
||||||
cb->u.c.nbytes = block_size_bytes;
|
cb->u.c.nbytes = block_size_bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
block_cache::block *
|
block_cache::block *
|
||||||
block_cache::find_unused_clean_block()
|
block_cache::find_unused_clean_block()
|
||||||
{
|
{
|
||||||
struct block *b, *tmp;
|
struct block *b, *tmp;
|
||||||
|
|
||||||
list_for_each_entry_safe (b, tmp, &clean_, list_) {
|
list_for_each_entry_safe (b, tmp, &clean_, list_) {
|
||||||
@ -375,11 +375,11 @@ namespace bcache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
block_cache::block *
|
block_cache::block *
|
||||||
block_cache::new_block(block_address index)
|
block_cache::new_block(block_address index)
|
||||||
{
|
{
|
||||||
block *b;
|
block *b;
|
||||||
|
|
||||||
b = __alloc_block();
|
b = __alloc_block();
|
||||||
@ -410,23 +410,23 @@ namespace bcache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*----------------------------------------------------------------
|
/*----------------------------------------------------------------
|
||||||
* Block reference counting
|
* Block reference counting
|
||||||
*--------------------------------------------------------------*/
|
*--------------------------------------------------------------*/
|
||||||
unsigned
|
unsigned
|
||||||
block_cache::calc_nr_cache_blocks(size_t mem, sector_t block_size)
|
block_cache::calc_nr_cache_blocks(size_t mem, sector_t block_size)
|
||||||
{
|
{
|
||||||
size_t space_per_block = (block_size << SECTOR_SHIFT) + sizeof(block);
|
size_t space_per_block = (block_size << SECTOR_SHIFT) + sizeof(block);
|
||||||
unsigned r = mem / space_per_block;
|
unsigned r = mem / space_per_block;
|
||||||
|
|
||||||
return (r < MIN_BLOCKS) ? MIN_BLOCKS : r;
|
return (r < MIN_BLOCKS) ? MIN_BLOCKS : r;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned
|
unsigned
|
||||||
block_cache::calc_nr_buckets(unsigned nr_blocks)
|
block_cache::calc_nr_buckets(unsigned nr_blocks)
|
||||||
{
|
{
|
||||||
unsigned r = 8;
|
unsigned r = 8;
|
||||||
unsigned n = nr_blocks / 4;
|
unsigned n = nr_blocks / 4;
|
||||||
|
|
||||||
@ -437,13 +437,13 @@ namespace bcache {
|
|||||||
r <<= 1;
|
r <<= 1;
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
block_cache::block_cache(int fd, sector_t block_size, uint64_t on_disk_blocks, size_t mem)
|
block_cache::block_cache(int fd, sector_t block_size, uint64_t on_disk_blocks, size_t mem)
|
||||||
: nr_locked_(0),
|
: nr_locked_(0),
|
||||||
nr_dirty_(0),
|
nr_dirty_(0),
|
||||||
nr_io_pending_(0)
|
nr_io_pending_(0)
|
||||||
{
|
{
|
||||||
int r;
|
int r;
|
||||||
unsigned nr_cache_blocks = calc_nr_cache_blocks(mem, block_size);
|
unsigned nr_cache_blocks = calc_nr_cache_blocks(mem, block_size);
|
||||||
unsigned nr_buckets = calc_nr_buckets(nr_cache_blocks);
|
unsigned nr_buckets = calc_nr_buckets(nr_cache_blocks);
|
||||||
@ -474,10 +474,10 @@ namespace bcache {
|
|||||||
r = init_free_list(nr_cache_blocks);
|
r = init_free_list(nr_cache_blocks);
|
||||||
if (r)
|
if (r)
|
||||||
throw std::runtime_error("couldn't allocate blocks");
|
throw std::runtime_error("couldn't allocate blocks");
|
||||||
}
|
}
|
||||||
|
|
||||||
block_cache::~block_cache()
|
block_cache::~block_cache()
|
||||||
{
|
{
|
||||||
assert(!nr_locked_);
|
assert(!nr_locked_);
|
||||||
flush();
|
flush();
|
||||||
wait_all();
|
wait_all();
|
||||||
@ -492,25 +492,25 @@ namespace bcache {
|
|||||||
io_destroy(aio_context_);
|
io_destroy(aio_context_);
|
||||||
|
|
||||||
::close(fd_);
|
::close(fd_);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t
|
uint64_t
|
||||||
block_cache::get_nr_blocks() const
|
block_cache::get_nr_blocks() const
|
||||||
{
|
{
|
||||||
return nr_data_blocks_;
|
return nr_data_blocks_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
block_cache::zero_block(block &b)
|
block_cache::zero_block(block &b)
|
||||||
{
|
{
|
||||||
memset(b.data_, 0, block_size_ << SECTOR_SHIFT);
|
memset(b.data_, 0, block_size_ << SECTOR_SHIFT);
|
||||||
b.mark_dirty();
|
b.mark_dirty();
|
||||||
}
|
}
|
||||||
|
|
||||||
block_cache::block *
|
block_cache::block *
|
||||||
block_cache::lookup_or_read_block(block_address index, unsigned flags,
|
block_cache::lookup_or_read_block(block_address index, unsigned flags,
|
||||||
validator::ptr v)
|
validator::ptr v)
|
||||||
{
|
{
|
||||||
block *b = hash_lookup(index);
|
block *b = hash_lookup(index);
|
||||||
|
|
||||||
if (b) {
|
if (b) {
|
||||||
@ -545,11 +545,11 @@ namespace bcache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (!b || b->error_) ? NULL : b;
|
return (!b || b->error_) ? NULL : b;
|
||||||
}
|
}
|
||||||
|
|
||||||
block_cache::block &
|
block_cache::block &
|
||||||
block_cache::get(block_address index, unsigned flags, validator::ptr v)
|
block_cache::get(block_address index, unsigned flags, validator::ptr v)
|
||||||
{
|
{
|
||||||
check_index(index);
|
check_index(index);
|
||||||
|
|
||||||
block *b = lookup_or_read_block(index, flags, v);
|
block *b = lookup_or_read_block(index, flags, v);
|
||||||
@ -575,20 +575,20 @@ namespace bcache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
throw std::runtime_error("couldn't get block");
|
throw std::runtime_error("couldn't get block");
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
block_cache::preemptive_writeback()
|
block_cache::preemptive_writeback()
|
||||||
{
|
{
|
||||||
unsigned nr_available = nr_cache_blocks_ - (nr_dirty_ - nr_io_pending_);
|
unsigned nr_available = nr_cache_blocks_ - (nr_dirty_ - nr_io_pending_);
|
||||||
if (nr_available < (WRITEBACK_LOW_THRESHOLD_PERCENT * nr_cache_blocks_ / 100))
|
if (nr_available < (WRITEBACK_LOW_THRESHOLD_PERCENT * nr_cache_blocks_ / 100))
|
||||||
writeback((WRITEBACK_HIGH_THRESHOLD_PERCENT * nr_cache_blocks_ / 100) - nr_available);
|
writeback((WRITEBACK_HIGH_THRESHOLD_PERCENT * nr_cache_blocks_ / 100) - nr_available);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
block_cache::release(block_cache::block &b)
|
block_cache::release(block_cache::block &b)
|
||||||
{
|
{
|
||||||
assert(!b.ref_count_);
|
assert(!b.ref_count_);
|
||||||
|
|
||||||
nr_locked_--;
|
nr_locked_--;
|
||||||
@ -610,11 +610,11 @@ namespace bcache {
|
|||||||
|
|
||||||
b.clear_flags(BF_FLUSH);
|
b.clear_flags(BF_FLUSH);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
block_cache::flush()
|
block_cache::flush()
|
||||||
{
|
{
|
||||||
block *b, *tmp;
|
block *b, *tmp;
|
||||||
|
|
||||||
list_for_each_entry_safe (b, tmp, &dirty_, list_) {
|
list_for_each_entry_safe (b, tmp, &dirty_, list_) {
|
||||||
@ -628,11 +628,11 @@ namespace bcache {
|
|||||||
wait_all();
|
wait_all();
|
||||||
|
|
||||||
return list_empty(&errored_) ? 0 : -EIO;
|
return list_empty(&errored_) ? 0 : -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
block_cache::prefetch(block_address index)
|
block_cache::prefetch(block_address index)
|
||||||
{
|
{
|
||||||
check_index(index);
|
check_index(index);
|
||||||
|
|
||||||
block *b = hash_lookup(index);
|
block *b = hash_lookup(index);
|
||||||
@ -642,18 +642,17 @@ namespace bcache {
|
|||||||
if (b)
|
if (b)
|
||||||
issue_read(*b);
|
issue_read(*b);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
block_cache::check_index(block_address index) const
|
block_cache::check_index(block_address index) const
|
||||||
{
|
{
|
||||||
if (index >= nr_data_blocks_) {
|
if (index >= nr_data_blocks_) {
|
||||||
std::ostringstream out;
|
std::ostringstream out;
|
||||||
out << "block out of bounds ("
|
out << "block out of bounds ("
|
||||||
<< index << " >= " << nr_data_blocks_ << ")\n";
|
<< index << " >= " << nr_data_blocks_ << ")\n";
|
||||||
throw std::runtime_error(out.str());
|
throw std::runtime_error(out.str());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//----------------------------------------------------------------
|
//----------------------------------------------------------------
|
||||||
|
Reference in New Issue
Block a user