From: Ilya Dryomov Date: Fri, 7 Oct 2022 09:56:27 +0000 (+0200) Subject: librbd: relax image size check in luks::FormatRequest X-Git-Tag: v18.1.0~754^2~17 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=203560999d98e05b46deaa98ecb675e26a521eab;p=ceph.git librbd: relax image size check in luks::FormatRequest 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 --- diff --git a/src/librbd/crypto/luks/FormatRequest.cc b/src/librbd/crypto/luks/FormatRequest.cc index 30541cd318400..ec367717c2c58 100644 --- a/src/librbd/crypto/luks/FormatRequest.cc +++ b/src/librbd/crypto/luks/FormatRequest.cc @@ -108,8 +108,8 @@ void FormatRequest::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; diff --git a/src/test/librbd/test_librbd.cc b/src/test/librbd/test_librbd.cc index a98cc016ee813..48e0d57db5a57 100644 --- a/src/test/librbd/test_librbd.cc +++ b/src/test/librbd/test_librbd.cc @@ -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)