1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "librbd/mirror/snapshot/CreatePrimaryRequest.h"
5 #include "common/dout.h"
6 #include "common/errno.h"
7 #include "cls/rbd/cls_rbd_client.h"
8 #include "librbd/ImageCtx.h"
9 #include "librbd/ImageState.h"
10 #include "librbd/Operations.h"
11 #include "librbd/Utils.h"
12 #include "librbd/mirror/snapshot/UnlinkPeerRequest.h"
13 #include "librbd/mirror/snapshot/Utils.h"
15 #define dout_subsys ceph_subsys_rbd
18 #define dout_prefix *_dout << "librbd::mirror::snapshot::CreatePrimaryRequest: " \
19 << this << " " << __func__ << ": "
25 using librbd::util::create_context_callback
;
26 using librbd::util::create_rados_callback
;
29 CreatePrimaryRequest
<I
>::CreatePrimaryRequest(
30 I
*image_ctx
, const std::string
& global_image_id
, uint32_t flags
,
31 uint64_t *snap_id
, Context
*on_finish
)
32 : m_image_ctx(image_ctx
), m_global_image_id(global_image_id
),
33 m_flags(flags
), m_snap_id(snap_id
), m_on_finish(on_finish
) {
34 m_default_ns_ctx
.dup(m_image_ctx
->md_ctx
);
35 m_default_ns_ctx
.set_namespace("");
39 void CreatePrimaryRequest
<I
>::send() {
40 if (!util::can_create_primary_snapshot(
42 ((m_flags
& CREATE_PRIMARY_FLAG_DEMOTED
) != 0),
43 ((m_flags
& CREATE_PRIMARY_FLAG_FORCE
) != 0), nullptr, nullptr)) {
49 uuid_gen
.generate_random();
50 m_snap_name
= ".mirror.primary." + m_global_image_id
+ "." +
57 void CreatePrimaryRequest
<I
>::get_mirror_peers() {
58 CephContext
*cct
= m_image_ctx
->cct
;
59 ldout(cct
, 20) << dendl
;
61 librados::ObjectReadOperation op
;
62 cls_client::mirror_peer_list_start(&op
);
64 librados::AioCompletion
*comp
= create_rados_callback
<
65 CreatePrimaryRequest
<I
>,
66 &CreatePrimaryRequest
<I
>::handle_get_mirror_peers
>(this);
68 int r
= m_default_ns_ctx
.aio_operate(RBD_MIRRORING
, comp
, &op
, &m_out_bl
);
74 void CreatePrimaryRequest
<I
>::handle_get_mirror_peers(int r
) {
75 CephContext
*cct
= m_image_ctx
->cct
;
76 ldout(cct
, 20) << "r=" << r
<< dendl
;
78 std::vector
<cls::rbd::MirrorPeer
> peers
;
80 auto iter
= m_out_bl
.cbegin();
81 r
= cls_client::mirror_peer_list_finish(&iter
, &peers
);
85 lderr(cct
) << "failed to retrieve mirror peers: " << cpp_strerror(r
)
91 for (auto &peer
: peers
) {
92 if (peer
.mirror_peer_direction
== cls::rbd::MIRROR_PEER_DIRECTION_RX
) {
95 m_mirror_peer_uuids
.insert(peer
.uuid
);
98 if (m_mirror_peer_uuids
.empty() &&
99 ((m_flags
& CREATE_PRIMARY_FLAG_IGNORE_EMPTY_PEERS
) == 0)) {
100 lderr(cct
) << "no mirror tx peers configured for the pool" << dendl
;
108 template <typename I
>
109 void CreatePrimaryRequest
<I
>::create_snapshot() {
110 cls::rbd::MirrorSnapshotNamespace ns
{
111 ((m_flags
& CREATE_PRIMARY_FLAG_DEMOTED
) != 0 ?
112 cls::rbd::MIRROR_SNAPSHOT_STATE_PRIMARY_DEMOTED
:
113 cls::rbd::MIRROR_SNAPSHOT_STATE_PRIMARY
),
114 m_mirror_peer_uuids
, "", CEPH_NOSNAP
};
116 CephContext
*cct
= m_image_ctx
->cct
;
117 ldout(cct
, 20) << "name=" << m_snap_name
<< ", "
118 << "ns=" << ns
<< dendl
;
119 auto ctx
= create_context_callback
<
120 CreatePrimaryRequest
<I
>,
121 &CreatePrimaryRequest
<I
>::handle_create_snapshot
>(this);
122 m_image_ctx
->operations
->snap_create(ns
, m_snap_name
, ctx
);
125 template <typename I
>
126 void CreatePrimaryRequest
<I
>::handle_create_snapshot(int r
) {
127 CephContext
*cct
= m_image_ctx
->cct
;
128 ldout(cct
, 20) << "r=" << r
<< dendl
;
131 lderr(cct
) << "failed to create mirror snapshot: " << cpp_strerror(r
)
140 template <typename I
>
141 void CreatePrimaryRequest
<I
>::refresh_image() {
142 // if snapshot created via remote RPC, refresh is required to retrieve
144 if (m_snap_id
== nullptr) {
149 CephContext
*cct
= m_image_ctx
->cct
;
150 ldout(cct
, 20) << dendl
;
152 auto ctx
= create_context_callback
<
153 CreatePrimaryRequest
<I
>,
154 &CreatePrimaryRequest
<I
>::handle_refresh_image
>(this);
155 m_image_ctx
->state
->refresh(ctx
);
158 template <typename I
>
159 void CreatePrimaryRequest
<I
>::handle_refresh_image(int r
) {
160 CephContext
*cct
= m_image_ctx
->cct
;
161 ldout(cct
, 20) << "r=" << r
<< dendl
;
164 lderr(cct
) << "failed to refresh image: " << cpp_strerror(r
) << dendl
;
170 std::shared_lock image_locker
{m_image_ctx
->image_lock
};
171 *m_snap_id
= m_image_ctx
->get_snap_id(
172 cls::rbd::MirrorSnapshotNamespace
{}, m_snap_name
);
173 ldout(cct
, 20) << "snap_id=" << *m_snap_id
<< dendl
;
179 template <typename I
>
180 void CreatePrimaryRequest
<I
>::unlink_peer() {
181 uint64_t max_snapshots
= m_image_ctx
->config
.template get_val
<uint64_t>(
182 "rbd_mirroring_max_mirroring_snapshots");
183 ceph_assert(max_snapshots
>= 3);
185 std::string peer_uuid
;
186 uint64_t snap_id
= CEPH_NOSNAP
;
188 for (auto &peer
: m_mirror_peer_uuids
) {
189 std::shared_lock image_locker
{m_image_ctx
->image_lock
};
191 uint64_t prev_snap_id
= 0;
192 for (auto &snap_it
: m_image_ctx
->snap_info
) {
193 auto info
= boost::get
<cls::rbd::MirrorSnapshotNamespace
>(
194 &snap_it
.second
.snap_namespace
);
195 if (info
== nullptr) {
198 if (info
->state
!= cls::rbd::MIRROR_SNAPSHOT_STATE_PRIMARY
) {
199 // reset counters -- we count primary snapshots after the last promotion
205 if (count
== max_snapshots
- 1) {
206 prev_snap_id
= snap_it
.first
;
207 } else if (count
> max_snapshots
) {
209 snap_id
= prev_snap_id
;
213 if (snap_id
!= CEPH_NOSNAP
) {
218 if (snap_id
== CEPH_NOSNAP
) {
223 CephContext
*cct
= m_image_ctx
->cct
;
224 ldout(cct
, 20) << "peer=" << peer_uuid
<< ", snap_id=" << snap_id
<< dendl
;
226 auto ctx
= create_context_callback
<
227 CreatePrimaryRequest
<I
>,
228 &CreatePrimaryRequest
<I
>::handle_unlink_peer
>(this);
229 auto req
= UnlinkPeerRequest
<I
>::create(m_image_ctx
, snap_id
, peer_uuid
, ctx
);
233 template <typename I
>
234 void CreatePrimaryRequest
<I
>::handle_unlink_peer(int r
) {
235 CephContext
*cct
= m_image_ctx
->cct
;
236 ldout(cct
, 20) << "r=" << r
<< dendl
;
239 lderr(cct
) << "failed to unlink peer: " << cpp_strerror(r
) << dendl
;
240 finish(0); // not fatal
247 template <typename I
>
248 void CreatePrimaryRequest
<I
>::finish(int r
) {
249 CephContext
*cct
= m_image_ctx
->cct
;
250 ldout(cct
, 20) << "r=" << r
<< dendl
;
252 m_on_finish
->complete(r
);
256 } // namespace snapshot
257 } // namespace mirror
258 } // namespace librbd
260 template class librbd::mirror::snapshot::CreatePrimaryRequest
<librbd::ImageCtx
>;