[block manager] Don't allow the cache to be bigger than the block dev.

This commit is contained in:
Joe Thornber 2017-07-24 15:28:23 +01:00
parent fd02dfa5bf
commit 209af19a02
2 changed files with 10 additions and 2 deletions

View File

@ -135,6 +135,7 @@ namespace persistent_data {
bool is_locked(block_address b) const; bool is_locked(block_address b) const;
private: private:
uint64_t choose_cache_size(block_address nr_blocks) const;
int open_or_create_block_file(std::string const &path, off_t file_size, int open_or_create_block_file(std::string const &path, off_t file_size,
mode m, bool excl); mode m, bool excl);
void check(block_address b) const; void check(block_address b) const;

View File

@ -133,15 +133,22 @@ namespace persistent_data {
//---------------------------------------------------------------- //----------------------------------------------------------------
template <uint32_t BlockSize>
uint64_t
block_manager<BlockSize>::choose_cache_size(block_address nr_blocks) const
{
uint64_t const DEFAULT_CACHE_SIZE = 1024 * 1024 * 16;
return std::min<uint64_t>(DEFAULT_CACHE_SIZE, BlockSize * nr_blocks);
}
template <uint32_t BlockSize> template <uint32_t BlockSize>
block_manager<BlockSize>::block_manager(std::string const &path, block_manager<BlockSize>::block_manager(std::string const &path,
block_address nr_blocks, block_address nr_blocks,
unsigned max_concurrent_blocks, unsigned max_concurrent_blocks,
mode m, mode m,
bool excl) bool excl)
// FIXME: * BlockSize ?
: fd_(open_or_create_block_file(path, nr_blocks * BlockSize, m, excl)), : fd_(open_or_create_block_file(path, nr_blocks * BlockSize, m, excl)),
bc_(fd_, BlockSize >> SECTOR_SHIFT, nr_blocks, 1024u * 1024u * 16), bc_(fd_, BlockSize >> SECTOR_SHIFT, nr_blocks, choose_cache_size(nr_blocks)),
superblock_ref_count_(0) superblock_ref_count_(0)
{ {
} }