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

@@ -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);