summaryrefslogtreecommitdiffstats
path: root/src/tools/rbd_mirror/image_replayer/OpenLocalImageRequest.cc
blob: 7f8d9608eb4d899f84c81bdad3eef33c3c9df277 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "include/compat.h"
#include "CloseImageRequest.h"
#include "OpenLocalImageRequest.h"
#include "common/debug.h"
#include "common/errno.h"
#include "librbd/ExclusiveLock.h"
#include "librbd/ImageCtx.h"
#include "librbd/ImageState.h"
#include "librbd/Journal.h"
#include "librbd/Utils.h"
#include "librbd/asio/ContextWQ.h"
#include "librbd/exclusive_lock/Policy.h"
#include "librbd/journal/Policy.h"
#include "librbd/mirror/GetInfoRequest.h"
#include <type_traits>

#define dout_context g_ceph_context
#define dout_subsys ceph_subsys_rbd_mirror
#undef dout_prefix
#define dout_prefix *_dout << "rbd::mirror::image_replayer::OpenLocalImageRequest: " \
                           << this << " " << __func__ << " "

namespace rbd {
namespace mirror {
namespace image_replayer {

using librbd::util::create_context_callback;

namespace {

template <typename I>
struct MirrorExclusiveLockPolicy : public librbd::exclusive_lock::Policy {
  I *image_ctx;

  MirrorExclusiveLockPolicy(I *image_ctx) : image_ctx(image_ctx) {
  }

  bool may_auto_request_lock() override {
    return false;
  }

  int lock_requested(bool force) override {
    int r = -EROFS;
    {
      std::shared_lock owner_locker{image_ctx->owner_lock};
      std::shared_lock image_locker{image_ctx->image_lock};
      if (image_ctx->journal == nullptr || image_ctx->journal->is_tag_owner()) {
        r = 0;
      }
    }

    if (r == 0) {
      // if the local image journal has been closed or if it was (force)
      // promoted allow the lock to be released to another client
      image_ctx->exclusive_lock->release_lock(nullptr);
    }
    return r;
  }

  bool accept_blocked_request(
      librbd::exclusive_lock::OperationRequestType request_type) override {
    switch (request_type) {
    case librbd::exclusive_lock::OPERATION_REQUEST_TYPE_TRASH_SNAP_REMOVE:
    case librbd::exclusive_lock::OPERATION_REQUEST_TYPE_FORCE_PROMOTION:
      return true;
    default:
      return false;
    }
  }
};

struct MirrorJournalPolicy : public librbd::journal::Policy {
  librbd::asio::ContextWQ *work_queue;

  MirrorJournalPolicy(librbd::asio::ContextWQ *work_queue)
    : work_queue(work_queue) {
  }

  bool append_disabled() const override {
    // avoid recording any events to the local journal
    return true;
  }
  bool journal_disabled() const override {
    return false;
  }

  void allocate_tag_on_lock(Context *on_finish) override {
    // rbd-mirror will manually create tags by copying them from the peer
    work_queue->queue(on_finish, 0);
  }
};

} // anonymous namespace

template <typename I>
OpenLocalImageRequest<I>::OpenLocalImageRequest(
    librados::IoCtx &local_io_ctx,
    I **local_image_ctx,
    const std::string &local_image_id,
    librbd::asio::ContextWQ *work_queue,
    Context *on_finish)
  : m_local_io_ctx(local_io_ctx), m_local_image_ctx(local_image_ctx),
    m_local_image_id(local_image_id), m_work_queue(work_queue),
    m_on_finish(on_finish) {
}

template <typename I>
void OpenLocalImageRequest<I>::send() {
  send_open_image();
}

template <typename I>
void OpenLocalImageRequest<I>::send_open_image() {
  dout(20) << dendl;

  *m_local_image_ctx = I::create("", m_local_image_id, nullptr,
                                 m_local_io_ctx, false);

  // ensure non-primary images can be modified
  (*m_local_image_ctx)->read_only_mask =
    ~librbd::IMAGE_READ_ONLY_FLAG_NON_PRIMARY;

  {
    std::scoped_lock locker{(*m_local_image_ctx)->owner_lock,
			    (*m_local_image_ctx)->image_lock};
    (*m_local_image_ctx)->set_exclusive_lock_policy(
      new MirrorExclusiveLockPolicy<I>(*m_local_image_ctx));
    (*m_local_image_ctx)->set_journal_policy(
      new MirrorJournalPolicy(m_work_queue));
  }

  Context *ctx = create_context_callback<
    OpenLocalImageRequest<I>, &OpenLocalImageRequest<I>::handle_open_image>(
      this);
  (*m_local_image_ctx)->state->open(0, ctx);
}

template <typename I>
void OpenLocalImageRequest<I>::handle_open_image(int r) {
  dout(20) << ": r=" << r << dendl;

  if (r < 0) {
    if (r == -ENOENT) {
      dout(10) << ": local image does not exist" << dendl;
    } else {
      derr << ": failed to open image '" << m_local_image_id << "': "
           << cpp_strerror(r) << dendl;
    }
    *m_local_image_ctx = nullptr;
    finish(r);
    return;
  }

  send_get_mirror_info();
}

template <typename I>
void OpenLocalImageRequest<I>::send_get_mirror_info() {
  dout(20) << dendl;

  Context *ctx = create_context_callback<
    OpenLocalImageRequest<I>,
    &OpenLocalImageRequest<I>::handle_get_mirror_info>(
      this);
  auto request = librbd::mirror::GetInfoRequest<I>::create(
    **m_local_image_ctx, &m_mirror_image, &m_promotion_state,
    &m_primary_mirror_uuid, ctx);
  request->send();
}

template <typename I>
void OpenLocalImageRequest<I>::handle_get_mirror_info(int r) {
  dout(20) << ": r=" << r << dendl;

  if (r == -ENOENT) {
    dout(5) << ": local image is not mirrored" << dendl;
    send_close_image(r);
    return;
  } else if (r < 0) {
    derr << ": error querying local image primary status: " << cpp_strerror(r)
         << dendl;
    send_close_image(r);
    return;
  }

  if (m_mirror_image.state == cls::rbd::MIRROR_IMAGE_STATE_DISABLING) {
    dout(5) << ": local image mirroring is being disabled" << dendl;
    send_close_image(-ENOENT);
    return;
  }

  // if the local image owns the tag -- don't steal the lock since
  // we aren't going to mirror peer data into this image anyway
  if (m_promotion_state == librbd::mirror::PROMOTION_STATE_PRIMARY) {
    dout(10) << ": local image is primary -- skipping image replay" << dendl;
    send_close_image(-EREMOTEIO);
    return;
  }

  send_lock_image();
}

template <typename I>
void OpenLocalImageRequest<I>::send_lock_image() {
  std::shared_lock owner_locker{(*m_local_image_ctx)->owner_lock};
  if ((*m_local_image_ctx)->exclusive_lock == nullptr) {
    owner_locker.unlock();
    if (m_mirror_image.mode == cls::rbd::MIRROR_IMAGE_MODE_SNAPSHOT) {
      finish(0);
    } else {
      derr << ": image does not support exclusive lock" << dendl;
      send_close_image(-EINVAL);
    }
    return;
  }

  dout(20) << dendl;

  // disallow any proxied maintenance operations before grabbing lock
  (*m_local_image_ctx)->exclusive_lock->block_requests(-EROFS);

  Context *ctx = create_context_callback<
    OpenLocalImageRequest<I>, &OpenLocalImageRequest<I>::handle_lock_image>(
      this);

  (*m_local_image_ctx)->exclusive_lock->acquire_lock(ctx);
}

template <typename I>
void OpenLocalImageRequest<I>::handle_lock_image(int r) {
  dout(20) << ": r=" << r << dendl;

  if (r < 0) {
    derr << ": failed to lock image '" << m_local_image_id << "': "
       << cpp_strerror(r) << dendl;
    send_close_image(r);
    return;
  }

  {
    std::shared_lock owner_locker{(*m_local_image_ctx)->owner_lock};
    if ((*m_local_image_ctx)->exclusive_lock == nullptr ||
	!(*m_local_image_ctx)->exclusive_lock->is_lock_owner()) {
      derr << ": image is not locked" << dendl;
      send_close_image(-EBUSY);
      return;
    }
  }

  finish(0);
}

template <typename I>
void OpenLocalImageRequest<I>::send_close_image(int r) {
  dout(20) << dendl;

  if (m_ret_val == 0 && r < 0) {
    m_ret_val = r;
  }

  Context *ctx = create_context_callback<
    OpenLocalImageRequest<I>, &OpenLocalImageRequest<I>::handle_close_image>(
      this);
  CloseImageRequest<I> *request = CloseImageRequest<I>::create(
    m_local_image_ctx, ctx);
  request->send();
}

template <typename I>
void OpenLocalImageRequest<I>::handle_close_image(int r) {
  dout(20) << dendl;

  ceph_assert(r == 0);
  finish(m_ret_val);
}

template <typename I>
void OpenLocalImageRequest<I>::finish(int r) {
  dout(20) << ": r=" << r << dendl;

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

} // namespace image_replayer
} // namespace mirror
} // namespace rbd

template class rbd::mirror::image_replayer::OpenLocalImageRequest<librbd::ImageCtx>;