Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / librbd / mirror / GetInfoRequest.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include "librbd/mirror/GetInfoRequest.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/Journal.h"
11 #include "librbd/Utils.h"
12
13 #define dout_subsys ceph_subsys_rbd
14 #undef dout_prefix
15 #define dout_prefix *_dout << "librbd::mirror::GetInfoRequest: " << this \
16                            << " " << __func__ << ": "
17
18 namespace librbd {
19 namespace mirror {
20
21 using librbd::util::create_context_callback;
22 using librbd::util::create_rados_callback;
23
24 template <typename I>
25 void GetInfoRequest<I>::send() {
26   refresh_image();
27 }
28
29 template <typename I>
30 void GetInfoRequest<I>::refresh_image() {
31   if (!m_image_ctx.state->is_refresh_required()) {
32     get_mirror_image();
33     return;
34   }
35
36   CephContext *cct = m_image_ctx.cct;
37   ldout(cct, 20) << dendl;
38
39   auto ctx = create_context_callback<
40     GetInfoRequest<I>, &GetInfoRequest<I>::handle_refresh_image>(this);
41   m_image_ctx.state->refresh(ctx);
42 }
43
44 template <typename I>
45 void GetInfoRequest<I>::handle_refresh_image(int r) {
46   CephContext *cct = m_image_ctx.cct;
47   ldout(cct, 20) << "r=" << r << dendl;
48
49   if (r < 0) {
50     lderr(cct) << "failed to refresh image: " << cpp_strerror(r) << dendl;
51     finish(r);
52     return;
53   }
54
55   get_mirror_image();
56 }
57
58 template <typename I>
59 void GetInfoRequest<I>::get_mirror_image() {
60   CephContext *cct = m_image_ctx.cct;
61   ldout(cct, 20) << dendl;
62
63   librados::ObjectReadOperation op;
64   cls_client::mirror_image_get_start(&op, m_image_ctx.id);
65
66   librados::AioCompletion *comp = create_rados_callback<
67     GetInfoRequest<I>, &GetInfoRequest<I>::handle_get_mirror_image>(this);
68   int r = m_image_ctx.md_ctx.aio_operate(RBD_MIRRORING, comp, &op, &m_out_bl);
69   assert(r == 0);
70   comp->release();
71 }
72
73 template <typename I>
74 void GetInfoRequest<I>::handle_get_mirror_image(int r) {
75   CephContext *cct = m_image_ctx.cct;
76   ldout(cct, 20) << "r=" << r << dendl;
77
78   m_mirror_image->state = cls::rbd::MIRROR_IMAGE_STATE_DISABLED;
79   *m_promotion_state = PROMOTION_STATE_NON_PRIMARY;
80   if (r == 0) {
81     bufferlist::iterator iter = m_out_bl.begin();
82     r = cls_client::mirror_image_get_finish(&iter, m_mirror_image);
83   }
84
85   if (r == -ENOENT ||
86       m_mirror_image->state != cls::rbd::MIRROR_IMAGE_STATE_ENABLED) {
87     ldout(cct, 20) << "mirroring is disabled" << dendl;
88     finish(0);
89     return;
90   } else if (r < 0) {
91     lderr(cct) << "failed to retrieve mirroring state: " << cpp_strerror(r)
92                << dendl;
93     finish(r);
94     return;
95   }
96
97   get_tag_owner();
98 }
99
100 template <typename I>
101 void GetInfoRequest<I>::get_tag_owner() {
102   CephContext *cct = m_image_ctx.cct;
103   ldout(cct, 20) << dendl;
104
105   auto ctx = create_context_callback<
106     GetInfoRequest<I>, &GetInfoRequest<I>::handle_get_tag_owner>(this);
107   Journal<I>::get_tag_owner(m_image_ctx.md_ctx, m_image_ctx.id,
108                             &m_mirror_uuid, m_image_ctx.op_work_queue, ctx);
109 }
110
111 template <typename I>
112 void GetInfoRequest<I>::handle_get_tag_owner(int r) {
113   CephContext *cct = m_image_ctx.cct;
114   ldout(cct, 20) << "r=" << r << dendl;
115
116   if (r < 0) {
117     lderr(cct) << "failed to determine tag ownership: " << cpp_strerror(r)
118                << dendl;
119     finish(r);
120     return;
121   }
122
123   if (m_mirror_uuid == Journal<>::LOCAL_MIRROR_UUID) {
124     *m_promotion_state = PROMOTION_STATE_PRIMARY;
125   } else if (m_mirror_uuid == Journal<>::ORPHAN_MIRROR_UUID) {
126     *m_promotion_state = PROMOTION_STATE_ORPHAN;
127   }
128
129   finish(0);
130 }
131
132 template <typename I>
133 void GetInfoRequest<I>::finish(int r) {
134   CephContext *cct = m_image_ctx.cct;
135   ldout(cct, 20) << "r=" << r << dendl;
136
137   m_on_finish->complete(r);
138   delete this;
139 }
140
141 } // namespace mirror
142 } // namespace librbd
143
144 template class librbd::mirror::GetInfoRequest<librbd::ImageCtx>;