]> git.proxmox.com Git - ceph.git/blame - ceph/src/tools/rbd_mirror/image_replayer/OpenLocalImageRequest.cc
update sources to v12.1.0
[ceph.git] / ceph / src / tools / rbd_mirror / image_replayer / OpenLocalImageRequest.cc
CommitLineData
7c673cae
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
3
4#include "include/compat.h"
5#include "CloseImageRequest.h"
6#include "IsPrimaryRequest.h"
7#include "OpenLocalImageRequest.h"
8#include "common/errno.h"
9#include "common/WorkQueue.h"
10#include "librbd/ExclusiveLock.h"
11#include "librbd/ImageCtx.h"
12#include "librbd/ImageState.h"
13#include "librbd/Journal.h"
14#include "librbd/Utils.h"
15#include "librbd/exclusive_lock/Policy.h"
16#include "librbd/journal/Policy.h"
17#include <type_traits>
18
19#define dout_context g_ceph_context
20#define dout_subsys ceph_subsys_rbd_mirror
21#undef dout_prefix
22#define dout_prefix *_dout << "rbd::mirror::image_replayer::OpenLocalImageRequest: " \
23 << this << " " << __func__ << " "
24
25namespace rbd {
26namespace mirror {
27namespace image_replayer {
28
29using librbd::util::create_context_callback;
30
31namespace {
32
31f18b77 33template <typename I>
7c673cae 34struct MirrorExclusiveLockPolicy : public librbd::exclusive_lock::Policy {
31f18b77
FG
35 I *image_ctx;
36
37 MirrorExclusiveLockPolicy(I *image_ctx) : image_ctx(image_ctx) {
38 }
7c673cae
FG
39
40 bool may_auto_request_lock() override {
41 return false;
42 }
43
44 int lock_requested(bool force) override {
31f18b77
FG
45 int r = -EROFS;
46 {
47 RWLock::RLocker owner_locker(image_ctx->owner_lock);
48 RWLock::RLocker snap_locker(image_ctx->snap_lock);
49 if (image_ctx->journal == nullptr || image_ctx->journal->is_tag_owner()) {
50 r = 0;
51 }
52 }
53
54 if (r == 0) {
55 // if the local image journal has been closed or if it was (force)
56 // promoted allow the lock to be released to another client
57 image_ctx->exclusive_lock->release_lock(nullptr);
58 }
59 return r;
7c673cae
FG
60 }
61
62};
63
64struct MirrorJournalPolicy : public librbd::journal::Policy {
65 ContextWQ *work_queue;
66
67 MirrorJournalPolicy(ContextWQ *work_queue) : work_queue(work_queue) {
68 }
69
70 bool append_disabled() const override {
71 // avoid recording any events to the local journal
72 return true;
73 }
74 bool journal_disabled() const override {
75 return false;
76 }
77
78 void allocate_tag_on_lock(Context *on_finish) override {
79 // rbd-mirror will manually create tags by copying them from the peer
80 work_queue->queue(on_finish, 0);
81 }
82};
83
84} // anonymous namespace
85
86template <typename I>
87OpenLocalImageRequest<I>::OpenLocalImageRequest(librados::IoCtx &local_io_ctx,
88 I **local_image_ctx,
89 const std::string &local_image_id,
90 ContextWQ *work_queue,
91 Context *on_finish)
92 : m_local_io_ctx(local_io_ctx), m_local_image_ctx(local_image_ctx),
93 m_local_image_id(local_image_id), m_work_queue(work_queue),
94 m_on_finish(on_finish) {
95}
96
97template <typename I>
98void OpenLocalImageRequest<I>::send() {
99 send_open_image();
100}
101
102template <typename I>
103void OpenLocalImageRequest<I>::send_open_image() {
104 dout(20) << dendl;
105
106 *m_local_image_ctx = I::create("", m_local_image_id, nullptr,
107 m_local_io_ctx, false);
108 {
109 RWLock::WLocker owner_locker((*m_local_image_ctx)->owner_lock);
110 RWLock::WLocker snap_locker((*m_local_image_ctx)->snap_lock);
111 (*m_local_image_ctx)->set_exclusive_lock_policy(
31f18b77 112 new MirrorExclusiveLockPolicy<I>(*m_local_image_ctx));
7c673cae
FG
113 (*m_local_image_ctx)->set_journal_policy(
114 new MirrorJournalPolicy(m_work_queue));
115 }
116
117 Context *ctx = create_context_callback<
118 OpenLocalImageRequest<I>, &OpenLocalImageRequest<I>::handle_open_image>(
119 this);
120 (*m_local_image_ctx)->state->open(false, ctx);
121}
122
123template <typename I>
124void OpenLocalImageRequest<I>::handle_open_image(int r) {
125 dout(20) << ": r=" << r << dendl;
126
127 if (r < 0) {
128 derr << ": failed to open image '" << m_local_image_id << "': "
129 << cpp_strerror(r) << dendl;
130 (*m_local_image_ctx)->destroy();
131 *m_local_image_ctx = nullptr;
132 finish(r);
133 return;
134 }
135
136 send_is_primary();
137}
138
139template <typename I>
140void OpenLocalImageRequest<I>::send_is_primary() {
141 dout(20) << dendl;
142
143 Context *ctx = create_context_callback<
144 OpenLocalImageRequest<I>, &OpenLocalImageRequest<I>::handle_is_primary>(
145 this);
146 IsPrimaryRequest<I> *request = IsPrimaryRequest<I>::create(*m_local_image_ctx,
147 &m_primary, ctx);
148 request->send();
149}
150
151template <typename I>
152void OpenLocalImageRequest<I>::handle_is_primary(int r) {
153 dout(20) << ": r=" << r << dendl;
154
155 if (r < 0) {
156 derr << ": error querying local image primary status: " << cpp_strerror(r)
157 << dendl;
158 send_close_image(r);
159 return;
160 }
161
162 // if the local image owns the tag -- don't steal the lock since
163 // we aren't going to mirror peer data into this image anyway
164 if (m_primary) {
165 dout(10) << ": local image is primary -- skipping image replay" << dendl;
166 send_close_image(-EREMOTEIO);
167 return;
168 }
169
170 send_lock_image();
171}
172
173template <typename I>
174void OpenLocalImageRequest<I>::send_lock_image() {
175 dout(20) << dendl;
176
177 RWLock::RLocker owner_locker((*m_local_image_ctx)->owner_lock);
178 if ((*m_local_image_ctx)->exclusive_lock == nullptr) {
179 derr << ": image does not support exclusive lock" << dendl;
180 send_close_image(-EINVAL);
181 return;
182 }
183
184 // disallow any proxied maintenance operations before grabbing lock
185 (*m_local_image_ctx)->exclusive_lock->block_requests(-EROFS);
186
187 Context *ctx = create_context_callback<
188 OpenLocalImageRequest<I>, &OpenLocalImageRequest<I>::handle_lock_image>(
189 this);
190
191 (*m_local_image_ctx)->exclusive_lock->acquire_lock(ctx);
192}
193
194template <typename I>
195void OpenLocalImageRequest<I>::handle_lock_image(int r) {
196 dout(20) << ": r=" << r << dendl;
197
198 if (r < 0) {
199 derr << ": failed to lock image '" << m_local_image_id << "': "
200 << cpp_strerror(r) << dendl;
201 send_close_image(r);
202 return;
203 }
204
205 {
206 RWLock::RLocker owner_locker((*m_local_image_ctx)->owner_lock);
207 if ((*m_local_image_ctx)->exclusive_lock == nullptr ||
208 !(*m_local_image_ctx)->exclusive_lock->is_lock_owner()) {
209 derr << ": image is not locked" << dendl;
210 send_close_image(-EBUSY);
211 return;
212 }
213 }
214
215 finish(0);
216}
217
218template <typename I>
219void OpenLocalImageRequest<I>::send_close_image(int r) {
220 dout(20) << dendl;
221
222 if (m_ret_val == 0 && r < 0) {
223 m_ret_val = r;
224 }
225
226 Context *ctx = create_context_callback<
227 OpenLocalImageRequest<I>, &OpenLocalImageRequest<I>::handle_close_image>(
228 this);
229 CloseImageRequest<I> *request = CloseImageRequest<I>::create(
230 m_local_image_ctx, ctx);
231 request->send();
232}
233
234template <typename I>
235void OpenLocalImageRequest<I>::handle_close_image(int r) {
236 dout(20) << dendl;
237
238 assert(r == 0);
239 finish(m_ret_val);
240}
241
242template <typename I>
243void OpenLocalImageRequest<I>::finish(int r) {
244 dout(20) << ": r=" << r << dendl;
245
246 m_on_finish->complete(r);
247 delete this;
248}
249
250} // namespace image_replayer
251} // namespace mirror
252} // namespace rbd
253
254template class rbd::mirror::image_replayer::OpenLocalImageRequest<librbd::ImageCtx>;