Skip to content

Commit

Permalink
CSnapshotId hide in class CSnapshotCtl
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeiShtepa committed Nov 10, 2023
1 parent 6ca9e38 commit 5e6de37
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 57 deletions.
28 changes: 18 additions & 10 deletions include/blksnap/SnapshotCtl.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* flexibility. Uses structures that are directly passed to the kernel module.
*/

#include <memory>
#include <string>
#include <uuid/uuid.h>
#include <cstring>
Expand Down Expand Up @@ -102,20 +103,27 @@ namespace blksnap
class CSnapshotCtl
{
public:
CSnapshotCtl();
~CSnapshotCtl();
static void Collect(std::vector<CSnapshotId>& ids);
static void Version(struct blksnap_version& version);
static std::shared_ptr<CSnapshotCtl> Create(const std::string& filePath, const unsigned long long limit);
static std::shared_ptr<CSnapshotCtl> Open(const CSnapshotId& id);

CSnapshotId Create(const std::string& filePath, const unsigned long long limit);
void Destroy(const CSnapshotId& id);
void Collect(std::vector<CSnapshotId>& ids);
void AppendDiffStorage(const CSnapshotId& id, const std::string& filePath);
void Take(const CSnapshotId& id);
bool WaitEvent(const CSnapshotId& id, unsigned int timeoutMs, SBlksnapEvent& ev);
public:
virtual ~CSnapshotCtl() {};

void Version(struct blksnap_version& version);
void Take();
void Destroy();
bool WaitEvent(unsigned int timeoutMs, SBlksnapEvent& ev);

const uuid_t& Id() const
{
return m_id.Get();
}
private:
int m_fd;
CSnapshotCtl(const CSnapshotId& id, const std::shared_ptr<OpenFileHolder>& ctl);

CSnapshotId m_id;
std::shared_ptr<OpenFileHolder> m_ctl;
};

}
3 changes: 1 addition & 2 deletions lib/blksnap/Service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ std::string blksnap::Version()
{
struct blksnap_version version;

CSnapshotCtl ctl;
ctl.Version(version);
CSnapshotCtl::Version(version);

std::stringstream ss;
ss << version.major << "." << version.minor << "." << version.revision << "." << version.build;
Expand Down
17 changes: 6 additions & 11 deletions lib/blksnap/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ struct SState
{
std::atomic<bool> stop;
std::string diffStorage;
CSnapshotId id;
std::mutex lock;
std::list<std::string> errorMessage;
};
Expand Down Expand Up @@ -87,7 +86,7 @@ static void BlksnapThread(std::shared_ptr<CSnapshotCtl> ptrCtl, std::shared_ptr<
{
try
{
is_eventReady = ptrCtl->WaitEvent(ptrState->id, 100, ev);
is_eventReady = ptrCtl->WaitEvent(100, ev);
}
catch (std::exception& ex)
{
Expand Down Expand Up @@ -122,26 +121,23 @@ static void BlksnapThread(std::shared_ptr<CSnapshotCtl> ptrCtl, std::shared_ptr<

CSession::CSession(const std::vector<std::string>& devices, const std::string& diffStorageFilePath, const unsigned long long limit)
{
m_ptrCtl = std::make_shared<CSnapshotCtl>();

for (const auto& name : devices)
CTrackerCtl(name).Attach();

// Create snapshot
m_id = m_ptrCtl->Create(diffStorageFilePath, limit);
auto snapshot = CSnapshotCtl::Create(diffStorageFilePath, limit);

// Add devices to snapshot
for (const auto& name : devices)
CTrackerCtl(name).SnapshotAdd(m_id.Get());
CTrackerCtl(name).SnapshotAdd(snapshot->Id());

// Prepare state structure for thread
m_ptrState = std::make_shared<SState>();
m_ptrState->stop = false;
m_ptrState->id = m_id;

// Append first portion for diff storage
struct SBlksnapEvent ev;
if (m_ptrCtl->WaitEvent(m_id, 100, ev))
if (snapshot->WaitEvent(100, ev))
{
switch (ev.code)
{
Expand All @@ -162,8 +158,7 @@ CSession::CSession(const std::vector<std::string>& devices, const std::string& d


// Take snapshot
m_ptrCtl->Take(m_id);

snapshot->Take();
}

CSession::~CSession()
Expand All @@ -177,7 +172,7 @@ CSession::~CSession()
// Destroy snapshot
try
{
m_ptrCtl->Destroy(m_id);
m_ptrCtl->Destroy();
}
catch (std::exception& ex)
{
Expand Down
68 changes: 34 additions & 34 deletions lib/blksnap/SnapshotCtl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,31 +63,25 @@ static inline bool isBlockFile(const std::string& path)
return S_ISBLK(st.st_mode);
}

CSnapshotCtl::CSnapshotCtl()
: m_fd(0)
static inline std::shared_ptr<OpenFileHolder> OpenBlksnapCtl()
{
int fd = ::open(blksnap_filename, O_RDWR);
if (fd < 0)
throw std::system_error(errno, std::generic_category(),
"Failed to open file [" + std::string(blksnap_filename) + "]");

m_fd = fd;
return std::make_shared<OpenFileHolder>(blksnap_filename, O_RDWR);
}

CSnapshotCtl::~CSnapshotCtl()
{
if (m_fd)
::close(m_fd);
}
CSnapshotCtl::CSnapshotCtl(const CSnapshotId& id, const std::shared_ptr<OpenFileHolder>& ctl)
: m_id(id)
, m_ctl(ctl)
{ }

void CSnapshotCtl::Version(struct blksnap_version& version)
{
if (::ioctl(m_fd, IOCTL_BLKSNAP_VERSION, &version))
auto ctl = OpenBlksnapCtl();
if (::ioctl(ctl->Get(), IOCTL_BLKSNAP_VERSION, &version))
throw std::system_error(errno, std::generic_category(),
"Failed to get version.");
}

CSnapshotId CSnapshotCtl::Create(const std::string& filePath, const unsigned long long limit)
std::shared_ptr<CSnapshotCtl> CSnapshotCtl::Create(const std::string& filePath, const unsigned long long limit)
{
int flags = O_RDWR | O_EXCL;

Expand All @@ -101,30 +95,27 @@ CSnapshotId CSnapshotCtl::Create(const std::string& filePath, const unsigned lon
struct blksnap_snapshot_create param = {0};
param.diff_storage_limit_sect = limit / 512;
param.diff_storage_fd = fd.Get();
if (::ioctl(m_fd, IOCTL_BLKSNAP_SNAPSHOT_CREATE, &param))

auto ctl = OpenBlksnapCtl();
if (::ioctl(ctl->Get(), IOCTL_BLKSNAP_SNAPSHOT_CREATE, &param))
throw std::system_error(errno, std::generic_category(),
"Failed to create snapshot object.");

return CSnapshotId(param.id.b);
return std::shared_ptr<CSnapshotCtl>(new CSnapshotCtl(CSnapshotId(param.id.b), ctl));
}

void CSnapshotCtl::Destroy(const CSnapshotId& id)
std::shared_ptr<CSnapshotCtl> CSnapshotCtl::Open(const CSnapshotId& id)
{
struct blksnap_uuid param;

uuid_copy(param.b, id.Get());

if (::ioctl(m_fd, IOCTL_BLKSNAP_SNAPSHOT_DESTROY, &param))
throw std::system_error(errno, std::generic_category(),
"Failed to destroy snapshot.");
return std::shared_ptr<CSnapshotCtl>(new CSnapshotCtl(id, OpenBlksnapCtl()));
}

void CSnapshotCtl::Collect(std::vector<CSnapshotId>& ids)
{
struct blksnap_snapshot_collect param = {0};
auto ctl = OpenBlksnapCtl();

ids.clear();
if (::ioctl(m_fd, IOCTL_BLKSNAP_SNAPSHOT_COLLECT, &param))
if (::ioctl(ctl->Get(), IOCTL_BLKSNAP_SNAPSHOT_COLLECT, &param))
throw std::system_error(errno, std::generic_category(),
"Failed to get list of active snapshots.");

Expand All @@ -134,33 +125,42 @@ void CSnapshotCtl::Collect(std::vector<CSnapshotId>& ids)
std::vector<struct blksnap_uuid> id_array(param.count);
param.ids = (__u64)id_array.data();

if (::ioctl(m_fd, IOCTL_BLKSNAP_SNAPSHOT_COLLECT, &param))
if (::ioctl(ctl->Get(), IOCTL_BLKSNAP_SNAPSHOT_COLLECT, &param))
throw std::system_error(errno, std::generic_category(),
"Failed to get list of snapshots.");

for (size_t inx = 0; inx < param.count; inx++)
ids.emplace_back(id_array[inx].b);
}

void CSnapshotCtl::Take(const CSnapshotId& id)
void CSnapshotCtl::Take()
{
struct blksnap_uuid param;

uuid_copy(param.b, id.Get());

if (::ioctl(m_fd, IOCTL_BLKSNAP_SNAPSHOT_TAKE, &param))
uuid_copy(param.b, m_id.Get());
if (::ioctl(m_ctl->Get(), IOCTL_BLKSNAP_SNAPSHOT_TAKE, &param))
throw std::system_error(errno, std::generic_category(),
"Failed to take snapshot.");
}

bool CSnapshotCtl::WaitEvent(const CSnapshotId& id, unsigned int timeoutMs, SBlksnapEvent& ev)
void CSnapshotCtl::Destroy()
{
struct blksnap_uuid param;

uuid_copy(param.b, m_id.Get());
if (::ioctl(m_ctl->Get(), IOCTL_BLKSNAP_SNAPSHOT_DESTROY, &param))
throw std::system_error(errno, std::generic_category(),
"Failed to destroy snapshot.");
}

bool CSnapshotCtl::WaitEvent(unsigned int timeoutMs, SBlksnapEvent& ev)
{
struct blksnap_snapshot_event param;

uuid_copy(param.id.b, id.Get());
uuid_copy(param.id.b, m_id.Get());
param.timeout_ms = timeoutMs;

if (::ioctl(m_fd, IOCTL_BLKSNAP_SNAPSHOT_WAIT_EVENT, &param))
if (::ioctl(m_ctl->Get(), IOCTL_BLKSNAP_SNAPSHOT_WAIT_EVENT, &param))
{
if ((errno == ENOENT) || (errno == EINTR))
return false;
Expand Down

0 comments on commit 5e6de37

Please sign in to comment.