summaryrefslogtreecommitdiffstats
path: root/src/librbd/exclusive_lock/PreReleaseRequest.cc
blob: 7dbae6c5992e417a625ad72ed5796c98fb222d02 (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
293
294
295
296
297
298
299
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "librbd/exclusive_lock/PreReleaseRequest.h"
#include "common/AsyncOpTracker.h"
#include "common/dout.h"
#include "common/errno.h"
#include "librbd/ExclusiveLock.h"
#include "librbd/ImageState.h"
#include "librbd/ImageWatcher.h"
#include "librbd/Journal.h"
#include "librbd/ObjectMap.h"
#include "librbd/Utils.h"
#include "librbd/io/ImageRequestWQ.h"
#include "librbd/io/ObjectDispatcher.h"

#define dout_subsys ceph_subsys_rbd
#undef dout_prefix
#define dout_prefix *_dout << "librbd::exclusive_lock::PreReleaseRequest: " \
                           << this << " " << __func__ << ": "

namespace librbd {
namespace exclusive_lock {

using util::create_async_context_callback;
using util::create_context_callback;

template <typename I>
PreReleaseRequest<I>* PreReleaseRequest<I>::create(
    I &image_ctx, bool shutting_down, AsyncOpTracker &async_op_tracker,
    Context *on_finish) {
  return new PreReleaseRequest(image_ctx, shutting_down, async_op_tracker,
                               on_finish);
}

template <typename I>
PreReleaseRequest<I>::PreReleaseRequest(I &image_ctx, bool shutting_down,
                                        AsyncOpTracker &async_op_tracker,
                                        Context *on_finish)
  : m_image_ctx(image_ctx), m_shutting_down(shutting_down),
    m_async_op_tracker(async_op_tracker),
    m_on_finish(create_async_context_callback(image_ctx, on_finish)) {
}

template <typename I>
PreReleaseRequest<I>::~PreReleaseRequest() {
  if (!m_shutting_down) {
    m_image_ctx.state->handle_prepare_lock_complete();
  }
}

template <typename I>
void PreReleaseRequest<I>::send() {
  send_prepare_lock();
}

template <typename I>
void PreReleaseRequest<I>::send_prepare_lock() {
  if (m_shutting_down) {
    send_cancel_op_requests();
    return;
  }

  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 10) << dendl;

  // release the lock if the image is not busy performing other actions
  Context *ctx = create_context_callback<
    PreReleaseRequest<I>, &PreReleaseRequest<I>::handle_prepare_lock>(this);
  m_image_ctx.state->prepare_lock(ctx);
}

template <typename I>
void PreReleaseRequest<I>::handle_prepare_lock(int r) {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 10) << "r=" << r << dendl;

  send_cancel_op_requests();
}

template <typename I>
void PreReleaseRequest<I>::send_cancel_op_requests() {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 10) << dendl;

  using klass = PreReleaseRequest<I>;
  Context *ctx = create_context_callback<
    klass, &klass::handle_cancel_op_requests>(this);
  m_image_ctx.cancel_async_requests(ctx);
}

template <typename I>
void PreReleaseRequest<I>::handle_cancel_op_requests(int r) {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 10) << "r=" << r << dendl;

  ceph_assert(r == 0);

  send_block_writes();
}

template <typename I>
void PreReleaseRequest<I>::send_block_writes() {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 10) << dendl;

  using klass = PreReleaseRequest<I>;
  Context *ctx = create_context_callback<
    klass, &klass::handle_block_writes>(this);

  {
    RWLock::RLocker owner_locker(m_image_ctx.owner_lock);
    // setting the lock as required will automatically cause the IO
    // queue to re-request the lock if any IO is queued
    if (m_image_ctx.clone_copy_on_read ||
        m_image_ctx.test_features(RBD_FEATURE_JOURNALING)) {
      m_image_ctx.io_work_queue->set_require_lock(io::DIRECTION_BOTH, true);
    } else {
      m_image_ctx.io_work_queue->set_require_lock(io::DIRECTION_WRITE, true);
    }
    m_image_ctx.io_work_queue->block_writes(ctx);
  }
}

template <typename I>
void PreReleaseRequest<I>::handle_block_writes(int r) {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 10) << "r=" << r << dendl;

  if (r == -EBLACKLISTED) {
    // allow clean shut down if blacklisted
    lderr(cct) << "failed to block writes because client is blacklisted"
               << dendl;
  } else if (r < 0) {
    lderr(cct) << "failed to block writes: " << cpp_strerror(r) << dendl;
    m_image_ctx.io_work_queue->unblock_writes();
    save_result(r);
    finish();
    return;
  }

  send_wait_for_ops();
}

template <typename I>
void PreReleaseRequest<I>::send_wait_for_ops() {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 10) << dendl;

  Context *ctx = create_context_callback<
    PreReleaseRequest<I>, &PreReleaseRequest<I>::handle_wait_for_ops>(this);
  m_async_op_tracker.wait_for_ops(ctx);
}

template <typename I>
void PreReleaseRequest<I>::handle_wait_for_ops(int r) {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 10) << dendl;

  send_invalidate_cache();
}

template <typename I>
void PreReleaseRequest<I>::send_invalidate_cache() {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 10) << dendl;

  RWLock::RLocker owner_lock(m_image_ctx.owner_lock);
  Context *ctx = create_context_callback<
      PreReleaseRequest<I>,
      &PreReleaseRequest<I>::handle_invalidate_cache>(this);
  m_image_ctx.io_object_dispatcher->invalidate_cache(ctx);
}

template <typename I>
void PreReleaseRequest<I>::handle_invalidate_cache(int r) {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 10) << "r=" << r << dendl;

  if (r < 0 && r != -EBLACKLISTED && r != -EBUSY) {
    lderr(cct) << "failed to invalidate cache: " << cpp_strerror(r)
               << dendl;
    m_image_ctx.io_work_queue->unblock_writes();
    save_result(r);
    finish();
    return;
  }

  send_flush_notifies();
}

template <typename I>
void PreReleaseRequest<I>::send_flush_notifies() {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 10) << dendl;

  using klass = PreReleaseRequest<I>;
  Context *ctx =
    create_context_callback<klass, &klass::handle_flush_notifies>(this);
  m_image_ctx.image_watcher->flush(ctx);
}

template <typename I>
void PreReleaseRequest<I>::handle_flush_notifies(int r) {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 10) << dendl;

  ceph_assert(r == 0);
  send_close_journal();
}

template <typename I>
void PreReleaseRequest<I>::send_close_journal() {
  {
    RWLock::WLocker snap_locker(m_image_ctx.snap_lock);
    std::swap(m_journal, m_image_ctx.journal);
  }

  if (m_journal == nullptr) {
    send_close_object_map();
    return;
  }

  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 10) << dendl;

  using klass = PreReleaseRequest<I>;
  Context *ctx = create_context_callback<klass, &klass::handle_close_journal>(
    this);
  m_journal->close(ctx);
}

template <typename I>
void PreReleaseRequest<I>::handle_close_journal(int r) {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 10) << "r=" << r << dendl;

  if (r < 0) {
    // error implies some journal events were not flushed -- continue
    lderr(cct) << "failed to close journal: " << cpp_strerror(r) << dendl;
  }

  delete m_journal;

  send_close_object_map();
}

template <typename I>
void PreReleaseRequest<I>::send_close_object_map() {
  {
    RWLock::WLocker snap_locker(m_image_ctx.snap_lock);
    std::swap(m_object_map, m_image_ctx.object_map);
  }

  if (m_object_map == nullptr) {
    send_unlock();
    return;
  }

  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 10) << dendl;

  using klass = PreReleaseRequest<I>;
  Context *ctx = create_context_callback<
    klass, &klass::handle_close_object_map>(this);
  m_object_map->close(ctx);
}

template <typename I>
void PreReleaseRequest<I>::handle_close_object_map(int r) {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 10) << "r=" << r << dendl;

  if (r < 0) {
    lderr(cct) << "failed to close object map: " << cpp_strerror(r) << dendl;
  }

  delete m_object_map;
  send_unlock();
}

template <typename I>
void PreReleaseRequest<I>::send_unlock() {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 10) << dendl;

  finish();
}

template <typename I>
void PreReleaseRequest<I>::finish() {
  m_on_finish->complete(m_error_result);
  delete this;
}

} // namespace exclusive_lock
} // namespace librbd

template class librbd::exclusive_lock::PreReleaseRequest<librbd::ImageCtx>;