2013-04-23 19:51:44 +05:30
|
|
|
#include "persistent-data/math_utils.h"
|
2013-09-11 16:10:46 +05:30
|
|
|
#include "persistent-data/file_utils.h"
|
2013-04-23 19:51:44 +05:30
|
|
|
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
using namespace base;
|
2016-03-04 16:13:58 +05:30
|
|
|
using namespace bcache;
|
|
|
|
using namespace persistent_data;
|
2013-04-23 19:51:44 +05:30
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2013-05-28 18:18:10 +05:30
|
|
|
persistent_data::block_address
|
2015-08-19 17:02:57 +05:30
|
|
|
persistent_data::get_nr_blocks(string const &path, sector_t block_size)
|
2013-04-23 19:51:44 +05:30
|
|
|
{
|
2013-05-28 18:18:10 +05:30
|
|
|
using namespace persistent_data;
|
|
|
|
|
2013-04-23 19:51:44 +05:30
|
|
|
struct stat info;
|
|
|
|
block_address nr_blocks;
|
|
|
|
|
|
|
|
int r = ::stat(path.c_str(), &info);
|
|
|
|
if (r)
|
2016-03-31 16:50:12 +05:30
|
|
|
throw runtime_error("Couldn't stat dev path " + path + ": " +
|
|
|
|
strerror(errno));
|
2013-04-23 19:51:44 +05:30
|
|
|
|
|
|
|
if (S_ISREG(info.st_mode) && info.st_size)
|
2016-03-04 16:13:58 +05:30
|
|
|
nr_blocks = div_down<block_address>(info.st_size, block_size);
|
2013-04-23 19:51:44 +05:30
|
|
|
|
|
|
|
else if (S_ISBLK(info.st_mode)) {
|
|
|
|
// To get the size of a block device we need to
|
|
|
|
// open it, and then make an ioctl call.
|
|
|
|
int fd = ::open(path.c_str(), O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
throw runtime_error("couldn't open block device to ascertain size");
|
|
|
|
|
|
|
|
r = ::ioctl(fd, BLKGETSIZE64, &nr_blocks);
|
|
|
|
if (r) {
|
|
|
|
::close(fd);
|
|
|
|
throw runtime_error("ioctl BLKGETSIZE64 failed");
|
|
|
|
}
|
|
|
|
::close(fd);
|
2015-08-19 17:02:57 +05:30
|
|
|
nr_blocks = div_down<block_address>(nr_blocks, block_size);
|
2013-04-23 19:51:44 +05:30
|
|
|
} else
|
|
|
|
// FIXME: needs a better message
|
|
|
|
throw runtime_error("bad path");
|
|
|
|
|
|
|
|
return nr_blocks;
|
|
|
|
}
|
|
|
|
|
2016-03-04 16:13:58 +05:30
|
|
|
block_address
|
|
|
|
persistent_data::get_nr_metadata_blocks(string const &path)
|
|
|
|
{
|
|
|
|
return get_nr_blocks(path, MD_BLOCK_SIZE);
|
|
|
|
}
|
|
|
|
|
2013-09-11 16:10:46 +05:30
|
|
|
persistent_data::block_manager<>::ptr
|
2015-07-07 18:17:00 +05:30
|
|
|
persistent_data::open_bm(std::string const &dev_path, block_manager<>::mode m, bool excl)
|
2013-09-11 16:10:46 +05:30
|
|
|
{
|
2016-03-04 16:13:58 +05:30
|
|
|
block_address nr_blocks = get_nr_metadata_blocks(dev_path);
|
2015-07-07 18:17:00 +05:30
|
|
|
return block_manager<>::ptr(new block_manager<>(dev_path, nr_blocks, 1, m, excl));
|
2013-09-11 16:10:46 +05:30
|
|
|
}
|
|
|
|
|
2013-09-16 18:12:39 +05:30
|
|
|
void
|
|
|
|
persistent_data::check_file_exists(string const &file) {
|
|
|
|
struct stat info;
|
|
|
|
int r = ::stat(file.c_str(), &info);
|
|
|
|
if (r)
|
|
|
|
throw runtime_error("Couldn't stat file");
|
|
|
|
|
|
|
|
if (!S_ISREG(info.st_mode))
|
|
|
|
throw runtime_error("Not a regular file");
|
|
|
|
}
|
|
|
|
|
2013-04-23 19:51:44 +05:30
|
|
|
//----------------------------------------------------------------
|