Tools now open the metadata device in O_EXCL mode.

An attempt to stop people running the tools on active metadata.
This commit is contained in:
Joe Thornber
2015-07-07 13:47:00 +01:00
parent 7d8b6adf4e
commit 642ae6e7c2
6 changed files with 39 additions and 26 deletions

View File

@ -52,7 +52,8 @@ namespace persistent_data {
block_manager(std::string const &path,
block_address nr_blocks,
unsigned max_concurrent_locks,
mode m);
mode m,
bool excl = true);
class read_ref {
public:
@ -134,7 +135,8 @@ namespace persistent_data {
bool is_locked(block_address b) const;
private:
int open_or_create_block_file(std::string const &path, off_t file_size, mode m);
int open_or_create_block_file(std::string const &path, off_t file_size,
mode m, bool excl);
void check(block_address b) const;
int fd_;

View File

@ -80,7 +80,7 @@ namespace {
throw runtime_error(out.str());
}
int fd = open_file(path, O_CREAT | O_RDWR);
int fd = open_file(path, O_CREAT | O_EXCL | O_RDWR);
int r = ::ftruncate(fd, file_size);
if (r < 0)
@ -89,14 +89,18 @@ namespace {
return fd;
}
int open_block_file(string const &path, off_t min_size, bool writeable) {
int open_block_file(string const &path, off_t min_size, bool writeable, bool excl = true) {
if (!file_exists(path)) {
ostringstream out;
out << __FUNCTION__ << ": file '" << path << "' doesn't exist";
throw runtime_error(out.str());
}
return open_file(path, writeable ? O_RDWR : O_RDONLY);
int flags = writeable ? O_RDWR : O_RDONLY;
if (excl)
flags |= O_EXCL;
return open_file(path, flags);
}
};
@ -208,8 +212,9 @@ namespace persistent_data {
block_manager<BlockSize>::block_manager(std::string const &path,
block_address nr_blocks,
unsigned max_concurrent_blocks,
mode m)
: fd_(open_or_create_block_file(path, nr_blocks * BlockSize, m)),
mode m,
bool excl)
: fd_(open_or_create_block_file(path, nr_blocks * BlockSize, m, excl)),
bc_(fd_, BlockSize >> SECTOR_SHIFT, nr_blocks, 1024u * 1024u * 16),
superblock_ref_count_(0)
{
@ -217,14 +222,14 @@ namespace persistent_data {
template <uint32_t BlockSize>
int
block_manager<BlockSize>::open_or_create_block_file(string const &path, off_t file_size, mode m)
block_manager<BlockSize>::open_or_create_block_file(string const &path, off_t file_size, mode m, bool excl)
{
switch (m) {
case READ_ONLY:
return open_block_file(path, file_size, false);
return open_block_file(path, file_size, false, excl);
case READ_WRITE:
return open_block_file(path, file_size, true);
return open_block_file(path, file_size, true, excl);
case CREATE:
return create_block_file(path, file_size);

View File

@ -48,10 +48,10 @@ persistent_data::get_nr_blocks(string const &path)
}
persistent_data::block_manager<>::ptr
persistent_data::open_bm(std::string const &dev_path, block_manager<>::mode m)
persistent_data::open_bm(std::string const &dev_path, block_manager<>::mode m, bool excl)
{
block_address nr_blocks = get_nr_blocks(dev_path);
return block_manager<>::ptr(new block_manager<>(dev_path, nr_blocks, 1, m));
return block_manager<>::ptr(new block_manager<>(dev_path, nr_blocks, 1, m, excl));
}
void

View File

@ -10,7 +10,8 @@
// FIXME: move to a different unit
namespace persistent_data {
persistent_data::block_address get_nr_blocks(string const &path);
block_manager<>::ptr open_bm(std::string const &dev_path, block_manager<>::mode m);
block_manager<>::ptr open_bm(std::string const &dev_path,
block_manager<>::mode m, bool excl = true);
void check_file_exists(std::string const &file);
}