librbd: implement image.snap_exists()

This is a much more convenient way to tell if a snapshot already exists.

Signed-off-by: Sage Weil <sage@inktank.com>
This commit is contained in:
Sage Weil 2013-03-28 21:17:21 -07:00 committed by Josh Durgin
parent c5bd978a1d
commit 3694968a05
5 changed files with 27 additions and 0 deletions

View File

@ -142,6 +142,7 @@ public:
/* snapshots */
int snap_list(std::vector<snap_info_t>& snaps);
bool snap_exists(const char *snapname);
int snap_create(const char *snapname);
int snap_remove(const char *snapname);
int snap_rollback(const char *snap_name);

View File

@ -1462,6 +1462,19 @@ reprotect_and_return_err:
return 0;
}
bool snap_exists(ImageCtx *ictx, const char *snap_name)
{
ldout(ictx->cct, 20) << "snap_exists " << ictx << " " << snap_name << dendl;
int r = ictx_check(ictx);
if (r < 0)
return r;
RWLock::RLocker l(ictx->snap_lock);
return ictx->snaps_by_name.count(snap_name);
}
int add_snap(ImageCtx *ictx, const char *snap_name)
{
uint64_t snap_id;

View File

@ -102,6 +102,7 @@ namespace librbd {
int resize_helper(ImageCtx *ictx, uint64_t size, ProgressContext& prog_ctx);
int snap_create(ImageCtx *ictx, const char *snap_name);
int snap_list(ImageCtx *ictx, std::vector<snap_info_t>& snaps);
bool snap_list(ImageCtx *ictx, const char *snap_name);
int snap_rollback(ImageCtx *ictx, const char *snap_name,
ProgressContext& prog_ctx);
int snap_remove(ImageCtx *ictx, const char *snap_name);

View File

@ -414,6 +414,12 @@ namespace librbd {
return librbd::snap_list(ictx, snaps);
}
bool Image::snap_exists(const char *snap_name)
{
ImageCtx *ictx = (ImageCtx *)ctx;
return librbd::snap_exists(ictx, snap_name);
}
int Image::snap_set(const char *snap_name)
{
ImageCtx *ictx = (ImageCtx *)ctx;

View File

@ -566,14 +566,20 @@ TEST(LibRBD, TestCreateLsDeleteSnapPP)
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name, size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name, NULL));
ASSERT_FALSE(image.snap_exists("snap1"));
ASSERT_EQ(0, image.snap_create("snap1"));
ASSERT_TRUE(image.snap_exists("snap1"));
ASSERT_EQ(1, test_ls_snaps(image, 1, "snap1", size));
ASSERT_EQ(0, image.resize(size2));
ASSERT_FALSE(image.snap_exists("snap2"));
ASSERT_EQ(0, image.snap_create("snap2"));
ASSERT_TRUE(image.snap_exists("snap2"));
ASSERT_EQ(2, test_ls_snaps(image, 2, "snap1", size, "snap2", size2));
ASSERT_EQ(0, image.snap_remove("snap1"));
ASSERT_FALSE(image.snap_exists("snap1"));
ASSERT_EQ(1, test_ls_snaps(image, 1, "snap2", size2));
ASSERT_EQ(0, image.snap_remove("snap2"));
ASSERT_FALSE(image.snap_exists("snap2"));
ASSERT_EQ(0, test_ls_snaps(image, 0));
}