Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / librbd / mirror / EnableRequest.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/EnableRequest.h"
5 #include "common/dout.h"
6 #include "common/errno.h"
7 #include "cls/rbd/cls_rbd_client.h"
8 #include "librbd/ImageState.h"
9 #include "librbd/Journal.h"
10 #include "librbd/MirroringWatcher.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::EnableRequest: "
16
17 namespace librbd {
18 namespace mirror {
19
20 using util::create_context_callback;
21 using util::create_rados_callback;
22
23 template <typename I>
24 EnableRequest<I>::EnableRequest(librados::IoCtx &io_ctx,
25                                 const std::string &image_id,
26                                 const std::string &non_primary_global_image_id,
27                                 ContextWQ *op_work_queue, Context *on_finish)
28   : m_io_ctx(io_ctx), m_image_id(image_id),
29     m_non_primary_global_image_id(non_primary_global_image_id),
30     m_op_work_queue(op_work_queue), m_on_finish(on_finish),
31     m_cct(reinterpret_cast<CephContext*>(io_ctx.cct())) {
32 }
33
34 template <typename I>
35 void EnableRequest<I>::send() {
36   send_get_tag_owner();
37 }
38
39 template <typename I>
40 void EnableRequest<I>::send_get_tag_owner() {
41   if (!m_non_primary_global_image_id.empty()) {
42     return
43     send_get_mirror_image();
44   }
45   ldout(m_cct, 10) << this << " " << __func__ << dendl;
46
47   using klass = EnableRequest<I>;
48   Context *ctx = create_context_callback<
49       klass, &klass::handle_get_tag_owner>(this);
50   librbd::Journal<>::is_tag_owner(m_io_ctx, m_image_id, &m_is_primary,
51                                   m_op_work_queue, ctx);
52 }
53
54 template <typename I>
55 Context *EnableRequest<I>::handle_get_tag_owner(int *result) {
56   ldout(m_cct, 10) << this << " " << __func__ << ": r=" << *result << dendl;
57
58   if (*result < 0) {
59     lderr(m_cct) << "failed to check tag ownership: " << cpp_strerror(*result)
60                  << dendl;
61     return m_on_finish;
62   }
63
64   if (!m_is_primary) {
65     lderr(m_cct) << "last journal tag not owned by local cluster" << dendl;
66     *result = -EINVAL;
67     return m_on_finish;
68   }
69
70   send_get_mirror_image();
71   return nullptr;
72 }
73
74 template <typename I>
75 void EnableRequest<I>::send_get_mirror_image() {
76   ldout(m_cct, 10) << this << " " << __func__ << dendl;
77
78   librados::ObjectReadOperation op;
79   cls_client::mirror_image_get_start(&op, m_image_id);
80
81   using klass = EnableRequest<I>;
82   librados::AioCompletion *comp =
83     create_rados_callback<klass, &klass::handle_get_mirror_image>(this);
84   m_out_bl.clear();
85   int r = m_io_ctx.aio_operate(RBD_MIRRORING, comp, &op, &m_out_bl);
86   assert(r == 0);
87   comp->release();
88 }
89
90 template <typename I>
91 Context *EnableRequest<I>::handle_get_mirror_image(int *result) {
92   ldout(m_cct, 10) << this << " " << __func__ << ": r=" << *result << dendl;
93
94   if (*result == 0) {
95     bufferlist::iterator iter = m_out_bl.begin();
96     *result = cls_client::mirror_image_get_finish(&iter, &m_mirror_image);
97   }
98
99   if (*result == 0) {
100     if (m_mirror_image.state == cls::rbd::MIRROR_IMAGE_STATE_ENABLED) {
101       ldout(m_cct, 10) << this << " " << __func__
102                        << ": mirroring is already enabled" << dendl;
103     } else {
104       lderr(m_cct) << "currently disabling" << dendl;
105       *result = -EINVAL;
106     }
107     return m_on_finish;
108   }
109
110   if (*result != -ENOENT) {
111     lderr(m_cct) << "failed to retreive mirror image: " << cpp_strerror(*result)
112                  << dendl;
113     return m_on_finish;
114   }
115
116   *result = 0;
117   m_mirror_image.state = cls::rbd::MIRROR_IMAGE_STATE_ENABLED;
118   if (m_non_primary_global_image_id.empty()) {
119     uuid_d uuid_gen;
120     uuid_gen.generate_random();
121     m_mirror_image.global_image_id = uuid_gen.to_string();
122   } else {
123     m_mirror_image.global_image_id = m_non_primary_global_image_id;
124   }
125
126   send_set_mirror_image();
127   return nullptr;
128 }
129
130 template <typename I>
131 void EnableRequest<I>::send_set_mirror_image() {
132   ldout(m_cct, 10) << this << " " << __func__ << dendl;
133
134   librados::ObjectWriteOperation op;
135   cls_client::mirror_image_set(&op, m_image_id, m_mirror_image);
136
137   using klass = EnableRequest<I>;
138   librados::AioCompletion *comp =
139     create_rados_callback<klass, &klass::handle_set_mirror_image>(this);
140   m_out_bl.clear();
141   int r = m_io_ctx.aio_operate(RBD_MIRRORING, comp, &op);
142   assert(r == 0);
143   comp->release();
144 }
145
146 template <typename I>
147 Context *EnableRequest<I>::handle_set_mirror_image(int *result) {
148   ldout(m_cct, 10) << this << " " << __func__ << ": r=" << *result << dendl;
149
150   if (*result < 0) {
151     lderr(m_cct) << "failed to enable mirroring: " << cpp_strerror(*result)
152                  << dendl;
153     return m_on_finish;
154   }
155
156   send_notify_mirroring_watcher();
157   return nullptr;
158 }
159
160 template <typename I>
161 void EnableRequest<I>::send_notify_mirroring_watcher() {
162   ldout(m_cct, 10) << this << " " << __func__ << dendl;
163
164   using klass = EnableRequest<I>;
165   Context *ctx = create_context_callback<
166     klass, &klass::handle_notify_mirroring_watcher>(this);
167
168   MirroringWatcher<>::notify_image_updated(m_io_ctx,
169                                            cls::rbd::MIRROR_IMAGE_STATE_ENABLED,
170                                            m_image_id,
171                                            m_mirror_image.global_image_id, ctx);
172 }
173
174 template <typename I>
175 Context *EnableRequest<I>::handle_notify_mirroring_watcher(int *result) {
176   ldout(m_cct, 10) << this << " " << __func__ << ": r=" << *result << dendl;
177
178   if (*result < 0) {
179     lderr(m_cct) << "failed to send update notification: "
180                  << cpp_strerror(*result) << dendl;
181     *result = 0;
182   }
183
184   return m_on_finish;
185 }
186
187 } // namespace mirror
188 } // namespace librbd
189
190 template class librbd::mirror::EnableRequest<librbd::ImageCtx>;