summaryrefslogtreecommitdiffstats
path: root/src/librbd/crypto/LoadRequest.cc
blob: 5bc57d693c5d311db647de0cd3500b13ccf06864 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "LoadRequest.h"

#include "common/dout.h"
#include "common/errno.h"
#include "librbd/Utils.h"
#include "librbd/ImageCtx.h"
#include "librbd/crypto/EncryptionFormat.h"
#include "librbd/crypto/Types.h"
#include "librbd/crypto/Utils.h"
#include "librbd/io/AioCompletion.h"
#include "librbd/io/ImageDispatcherInterface.h"
#include "librbd/io/ImageDispatchSpec.h"
#include "librbd/io/Types.h"

#define dout_subsys ceph_subsys_rbd
#undef dout_prefix
#define dout_prefix *_dout << "librbd::crypto::LoadRequest: " << this \
                           << " " << __func__ << ": "

namespace librbd {
namespace crypto {

using librbd::util::create_context_callback;

template <typename I>
LoadRequest<I>::LoadRequest(
        I* image_ctx, std::vector<EncryptionFormat>&& formats,
        Context* on_finish) : m_image_ctx(image_ctx),
                              m_on_finish(on_finish),
                              m_format_idx(0),
                              m_is_current_format_cloned(false),
                              m_formats(std::move(formats)) {
}

template <typename I>
void LoadRequest<I>::send() {
  if (m_formats.empty()) {
    lderr(m_image_ctx->cct) << "no encryption formats were specified" << dendl;
    finish(-EINVAL);
    return;
  }

  ldout(m_image_ctx->cct, 20) << "got " << m_formats.size() << " formats"
                              << dendl;

  if (m_image_ctx->encryption_format.get() != nullptr) {
    lderr(m_image_ctx->cct) << "encryption already loaded" << dendl;
    finish(-EEXIST);
    return;
  }

  auto ictx = m_image_ctx;
  while (ictx != nullptr) {
    if (ictx->test_features(RBD_FEATURE_JOURNALING)) {
      lderr(m_image_ctx->cct) << "cannot use encryption with journal."
                              << " image name: " << ictx->name << dendl;
      finish(-ENOTSUP);
      return;
    }
    ictx = ictx->parent;
  }

  m_current_image_ctx = m_image_ctx;
  flush();
}

template <typename I>
void LoadRequest<I>::flush() {
  auto ctx = create_context_callback<
          LoadRequest<I>, &LoadRequest<I>::handle_flush>(this);
  auto aio_comp = io::AioCompletion::create_and_start(
    ctx, librbd::util::get_image_ctx(m_image_ctx), io::AIO_TYPE_FLUSH);
  auto req = io::ImageDispatchSpec::create_flush(
    *m_image_ctx, io::IMAGE_DISPATCH_LAYER_INTERNAL_START, aio_comp,
    io::FLUSH_SOURCE_INTERNAL, {});
  req->send();
}

template <typename I>
void LoadRequest<I>::handle_flush(int r) {
  ldout(m_image_ctx->cct, 20) << "r=" << r << dendl;

  if (r < 0) {
    lderr(m_image_ctx->cct) << "failed to flush image" << dendl;
    finish(r);
    return;
  }

  load();
}

template <typename I>
void LoadRequest<I>::load() {
  ldout(m_image_ctx->cct, 20) << "format_idx=" << m_format_idx << dendl;

  m_detected_format_name = "";
  auto ctx = create_context_callback<
          LoadRequest<I>, &LoadRequest<I>::handle_load>(this);
  m_formats[m_format_idx]->load(m_current_image_ctx, &m_detected_format_name,
                                ctx);
}

template <typename I>
void LoadRequest<I>::handle_load(int r) {
  ldout(m_image_ctx->cct, 20) << "r=" << r << dendl;

  if (r < 0) {
    if (m_is_current_format_cloned &&
        m_detected_format_name == UNKNOWN_FORMAT) {
      // encryption format was not detected, assume plaintext
      ldout(m_image_ctx->cct, 5) << "assuming plaintext for image "
                                 << m_current_image_ctx->name << dendl;
      m_formats.pop_back();
      invalidate_cache();
      return;
    }

    lderr(m_image_ctx->cct) << "failed to load encryption. image name: "
                            << m_current_image_ctx->name << dendl;
    finish(r);
    return;
  }

  ldout(m_image_ctx->cct, 5) << "loaded format " << m_detected_format_name
                             << (m_is_current_format_cloned ? " (cloned)" : "")
                             << " for image " << m_current_image_ctx->name
                             << dendl;

  m_format_idx++;
  m_current_image_ctx = m_current_image_ctx->parent;
  if (m_current_image_ctx != nullptr) {
    // move on to loading parent
    if (m_format_idx >= m_formats.size()) {
      // try to load next ancestor using the same format
      ldout(m_image_ctx->cct, 20) << "cloning format" << dendl;
      m_is_current_format_cloned = true;
      m_formats.push_back(m_formats[m_formats.size() - 1]->clone());
    }

    load();
  } else {
    if (m_formats.size() != m_format_idx) {
      lderr(m_image_ctx->cct) << "got " << m_formats.size()
                              << " encryption specs to load, "
                              << "but image has " << m_format_idx - 1
                              << " ancestors" << dendl;
      finish(-EINVAL);
      return;
    }

    invalidate_cache();
  }
}

template <typename I>
void LoadRequest<I>::invalidate_cache() {
  auto ctx = create_context_callback<
          LoadRequest<I>, &LoadRequest<I>::handle_invalidate_cache>(this);
  m_image_ctx->io_image_dispatcher->invalidate_cache(ctx);
}

template <typename I>
void LoadRequest<I>::handle_invalidate_cache(int r) {
  ldout(m_image_ctx->cct, 20) << "r=" << r << dendl;

  if (r < 0) {
    lderr(m_image_ctx->cct) << "failed to invalidate image cache" << dendl;
  }

  finish(r);
}

template <typename I>
void LoadRequest<I>::finish(int r) {
  ldout(m_image_ctx->cct, 20) << "r=" << r << dendl;

  if (r == 0) {
    auto ictx = m_image_ctx;
    for (auto& format : m_formats) {
      util::set_crypto(ictx, std::move(format));
      ictx = ictx->parent;
    }
  }

  m_on_finish->complete(r);
  delete this;
}

} // namespace crypto
} // namespace librbd

template class librbd::crypto::LoadRequest<librbd::ImageCtx>;