Skip to content

Commit

Permalink
librbd: relax image size check in luks::FormatRequest
Browse files Browse the repository at this point in the history
Proceed with formatting an image even if all space would be consumed by
the crypto header.  There is no reason to be strict here since we allow
creating zero-sized images as well as shrinking any image to 0.

Signed-off-by: Ilya Dryomov <[email protected]>
  • Loading branch information
idryomov committed Dec 4, 2022
1 parent 4f5b735 commit 2035609
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/librbd/crypto/luks/FormatRequest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ void FormatRequest<I>::send() {
uint64_t image_size = m_image_ctx->get_image_size(CEPH_NOSNAP);
m_image_ctx->image_lock.unlock_shared();

if (m_header.get_data_offset() >= image_size) {
lderr(m_image_ctx->cct) << "image is too small. format requires more than "
if (m_header.get_data_offset() > image_size) {
lderr(m_image_ctx->cct) << "image is too small, format requires "
<< m_header.get_data_offset() << " bytes" << dendl;
finish(-ENOSPC);
return;
Expand Down
43 changes: 43 additions & 0 deletions src/test/librbd/test_librbd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2531,6 +2531,49 @@ TEST_F(TestLibRBD, TestCloneEncryption)
rados_ioctx_destroy(ioctx);
}

TEST_F(TestLibRBD, EncryptionFormatNoData)
{
REQUIRE(!is_feature_enabled(RBD_FEATURE_JOURNALING));

librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));

librbd::RBD rbd;
auto name = get_temp_image_name();
uint64_t luks1_meta_size = 4 << 20;
std::string passphrase = "some passphrase";

{
int order = 0;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), luks1_meta_size - 1,
&order));
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), nullptr));

librbd::encryption_luks1_format_options_t opts = {
RBD_ENCRYPTION_ALGORITHM_AES256, passphrase};
ASSERT_EQ(-ENOSPC, image.encryption_format(RBD_ENCRYPTION_FORMAT_LUKS1,
&opts, sizeof(opts)));
uint64_t size;
ASSERT_EQ(0, image.size(&size));
ASSERT_EQ(luks1_meta_size - 1, size);
}

{
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), nullptr));
ASSERT_EQ(0, image.resize(luks1_meta_size));

librbd::encryption_luks1_format_options_t opts = {
RBD_ENCRYPTION_ALGORITHM_AES256, passphrase};
ASSERT_EQ(0, image.encryption_format(RBD_ENCRYPTION_FORMAT_LUKS1, &opts,
sizeof(opts)));
uint64_t size;
ASSERT_EQ(0, image.size(&size));
ASSERT_EQ(0, size);
}
}

#endif

TEST_F(TestLibRBD, TestIOWithIOHint)
Expand Down

0 comments on commit 2035609

Please sign in to comment.