2020-06-15 11:29:16 +05:30
|
|
|
#include "base/math_utils.h"
|
2013-09-11 16:10:46 +05:30
|
|
|
#include "persistent-data/file_utils.h"
|
2017-10-05 16:23:40 +05:30
|
|
|
#include "persistent-data/space-maps/core.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>
|
2017-04-29 23:21:52 +05:30
|
|
|
#include <fcntl.h>
|
2017-10-05 18:17:10 +05:30
|
|
|
#include <string.h>
|
2017-04-29 23:21:52 +05:30
|
|
|
#include <sstream>
|
2013-04-23 19:51:44 +05:30
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
using namespace base;
|
2016-03-04 16:13:58 +05:30
|
|
|
using namespace bcache;
|
|
|
|
using namespace persistent_data;
|
2017-04-29 23:21:52 +05:30
|
|
|
using namespace std;
|
2013-04-23 19:51:44 +05:30
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2017-10-05 18:17:10 +05:30
|
|
|
bool
|
2020-04-30 19:00:01 +05:30
|
|
|
persistent_data::check_for_xml(block_manager::ptr bm) {
|
|
|
|
block_manager::read_ref b = bm->read_lock(0);
|
2017-10-05 18:17:10 +05:30
|
|
|
const char *data = reinterpret_cast<const char *>(b.data());
|
|
|
|
return (!strncmp(data, "<superblock", 11) || !strncmp(data, "<?xml", 5)
|
|
|
|
|| !strncmp(data, "<!DOCTYPE", 9));
|
|
|
|
}
|
|
|
|
|
2013-05-28 18:18:10 +05:30
|
|
|
persistent_data::block_address
|
2017-04-29 23:21:52 +05:30
|
|
|
persistent_data::get_nr_blocks(std::string const &path, sector_t block_size)
|
2013-04-23 19:51:44 +05:30
|
|
|
{
|
2017-04-29 23:21:52 +05:30
|
|
|
return div_down<block_address>(file_utils::get_file_length(path),
|
|
|
|
block_size);
|
2013-04-23 19:51:44 +05:30
|
|
|
}
|
|
|
|
|
2016-03-04 16:13:58 +05:30
|
|
|
block_address
|
2017-04-29 23:21:52 +05:30
|
|
|
persistent_data::get_nr_metadata_blocks(std::string const &path)
|
2016-03-04 16:13:58 +05:30
|
|
|
{
|
|
|
|
return get_nr_blocks(path, MD_BLOCK_SIZE);
|
|
|
|
}
|
|
|
|
|
2020-04-30 19:00:01 +05:30
|
|
|
persistent_data::block_manager::ptr
|
|
|
|
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);
|
2020-04-30 19:00:01 +05:30
|
|
|
return block_manager::ptr(new block_manager(dev_path, nr_blocks, 1, m, excl));
|
2013-09-11 16:10:46 +05:30
|
|
|
}
|
|
|
|
|
2020-04-30 19:00:01 +05:30
|
|
|
block_manager::ptr
|
2017-10-05 16:23:40 +05:30
|
|
|
persistent_data::open_bm(std::string const &path) {
|
|
|
|
block_address nr_blocks = get_nr_metadata_blocks(path);
|
2020-04-30 19:00:01 +05:30
|
|
|
block_manager::mode m = block_manager::READ_ONLY;
|
|
|
|
return block_manager::ptr(new block_manager(path, nr_blocks, 1, m));
|
2017-10-05 16:23:40 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
transaction_manager::ptr
|
2020-04-30 19:00:01 +05:30
|
|
|
persistent_data::open_tm(block_manager::ptr bm, block_address superblock_location) {
|
2017-10-05 16:23:40 +05:30
|
|
|
auto nr_blocks = bm->get_nr_blocks();
|
|
|
|
if (!nr_blocks)
|
|
|
|
throw runtime_error("Metadata is not large enough for superblock.");
|
|
|
|
|
2020-05-27 16:30:40 +05:30
|
|
|
space_map::ptr sm{create_core_map(nr_blocks)};
|
2017-10-05 16:23:40 +05:30
|
|
|
sm->inc(superblock_location);
|
|
|
|
transaction_manager::ptr tm(new transaction_manager(bm, sm));
|
|
|
|
return tm;
|
|
|
|
}
|
|
|
|
|
2013-04-23 19:51:44 +05:30
|
|
|
//----------------------------------------------------------------
|