summaryrefslogtreecommitdiffstats
path: root/src/librbd/mirror/DemoteRequest.cc
blob: c5d38752ea9200b4c1f73a0f7d96210eb7e69c38 (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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "librbd/mirror/DemoteRequest.h"
#include "common/dout.h"
#include "common/errno.h"
#include "cls/rbd/cls_rbd_client.h"
#include "librbd/ExclusiveLock.h"
#include "librbd/ImageCtx.h"
#include "librbd/ImageState.h"
#include "librbd/Journal.h"
#include "librbd/Utils.h"
#include "librbd/mirror/GetInfoRequest.h"

#define dout_subsys ceph_subsys_rbd
#undef dout_prefix
#define dout_prefix *_dout << "librbd::mirror::DemoteRequest: " << this \
                           << " " << __func__ << ": "

namespace librbd {
namespace mirror {

using librbd::util::create_context_callback;

template <typename I>
void DemoteRequest<I>::send() {
  get_info();
}

template <typename I>
void DemoteRequest<I>::get_info() {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 20) << dendl;

  auto ctx = create_context_callback<
    DemoteRequest<I>, &DemoteRequest<I>::handle_get_info>(this);
  auto req = GetInfoRequest<I>::create(m_image_ctx, &m_mirror_image,
                                       &m_promotion_state, ctx);
  req->send();
}

template <typename I>
void DemoteRequest<I>::handle_get_info(int r) {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 20) << "r=" << r << dendl;

  if (r < 0) {
    lderr(cct) << "failed to retrieve mirroring state: " << cpp_strerror(r)
               << dendl;
    finish(r);
    return;
  } else if (m_mirror_image.state != cls::rbd::MIRROR_IMAGE_STATE_ENABLED) {
    lderr(cct) << "mirroring is not currently enabled" << dendl;
    finish(-EINVAL);
    return;
  } else if (m_promotion_state != PROMOTION_STATE_PRIMARY) {
    lderr(cct) << "image is not primary" << dendl;
    finish(-EINVAL);
    return;
  }

  acquire_lock();
}

template <typename I>
void DemoteRequest<I>::acquire_lock() {
  CephContext *cct = m_image_ctx.cct;

  m_image_ctx.owner_lock.get_read();
  if (m_image_ctx.exclusive_lock == nullptr) {
    m_image_ctx.owner_lock.put_read();
    lderr(cct) << "exclusive lock is not active" << dendl;
    finish(-EINVAL);
    return;
  }

  // avoid accepting new requests from peers while we demote
  // the image
  m_image_ctx.exclusive_lock->block_requests(0);
  m_blocked_requests = true;

  if (m_image_ctx.exclusive_lock->is_lock_owner()) {
    m_image_ctx.owner_lock.put_read();
    demote();
    return;
  }

  ldout(cct, 20) << dendl;

  auto ctx = create_context_callback<
    DemoteRequest<I>, &DemoteRequest<I>::handle_acquire_lock>(this);
  m_image_ctx.exclusive_lock->acquire_lock(ctx);
  m_image_ctx.owner_lock.put_read();
}

template <typename I>
void DemoteRequest<I>::handle_acquire_lock(int r) {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 20) << "r=" << r << dendl;

  if (r < 0) {
    lderr(cct) << "failed to lock image: " << cpp_strerror(r) << dendl;
    finish(r);
    return;
  }

  m_image_ctx.owner_lock.get_read();
  if (m_image_ctx.exclusive_lock != nullptr &&
      !m_image_ctx.exclusive_lock->is_lock_owner()) {
    r = m_image_ctx.exclusive_lock->get_unlocked_op_error();
    m_image_ctx.owner_lock.put_read();
    lderr(cct) << "failed to acquire exclusive lock" << dendl;
    finish(r);
    return;
  }
  m_image_ctx.owner_lock.put_read();

  demote();
}

template <typename I>
void DemoteRequest<I>::demote() {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 20) << dendl;

  auto ctx = create_context_callback<
    DemoteRequest<I>, &DemoteRequest<I>::handle_demote>(this);
  Journal<I>::demote(&m_image_ctx, ctx);
}

template <typename I>
void DemoteRequest<I>::handle_demote(int r) {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 20) << "r=" << r << dendl;

  if (r < 0) {
    m_ret_val = r;
    lderr(cct) << "failed to demote image: " << cpp_strerror(r) << dendl;
  }

  release_lock();
}

template <typename I>
void DemoteRequest<I>::release_lock() {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 20) << dendl;

  m_image_ctx.owner_lock.get_read();
  if (m_image_ctx.exclusive_lock == nullptr) {
    m_image_ctx.owner_lock.put_read();
    finish(0);
    return;
  }

  auto ctx = create_context_callback<
    DemoteRequest<I>, &DemoteRequest<I>::handle_release_lock>(this);
  m_image_ctx.exclusive_lock->release_lock(ctx);
  m_image_ctx.owner_lock.put_read();
}

template <typename I>
void DemoteRequest<I>::handle_release_lock(int r) {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 20) << "r=" << r << dendl;

  if (r < 0) {
    lderr(cct) << "failed to release exclusive lock: " << cpp_strerror(r)
               << dendl;
  }

  finish(r);
}

template <typename I>
void DemoteRequest<I>::finish(int r) {
  if (m_ret_val < 0) {
    r = m_ret_val;
  }

  {
    RWLock::RLocker owner_locker(m_image_ctx.owner_lock);
    if (m_blocked_requests && m_image_ctx.exclusive_lock != nullptr) {
      m_image_ctx.exclusive_lock->unblock_requests();
    }
  }

  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 20) << "r=" << r << dendl;

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

} // namespace mirror
} // namespace librbd

template class librbd::mirror::DemoteRequest<librbd::ImageCtx>;