]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
librbd: relax image size check in luks::FormatRequest
authorIlya Dryomov <idryomov@gmail.com>
Fri, 7 Oct 2022 09:56:27 +0000 (11:56 +0200)
committerIlya Dryomov <idryomov@gmail.com>
Sun, 4 Dec 2022 17:19:19 +0000 (18:19 +0100)
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 <idryomov@gmail.com>
src/librbd/crypto/luks/FormatRequest.cc
src/test/librbd/test_librbd.cc

index 30541cd3184000073180234fdcd94bc56e8dae54..ec367717c2c58a2693fce025f9b37cd0ef721ede 100644 (file)
@@ -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;
index a98cc016ee813a8a2943b11525e6a142c9204bb5..48e0d57db5a57bf84ee864a368dfabc1125cc18e 100644 (file)
@@ -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)