1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "common/dout.h"
5 #include "common/errno.h"
6 #include "include/assert.h"
7 #include "librbd/Utils.h"
8 #include "common/Timer.h"
9 #include "common/WorkQueue.h"
10 #include "journal/Settings.h"
11 #include "librbd/journal/CreateRequest.h"
12 #include "librbd/journal/RemoveRequest.h"
14 #define dout_subsys ceph_subsys_rbd
16 #define dout_prefix *_dout << "librbd::Journal::CreateRequest: "
20 using util::create_context_callback;
25 CreateRequest<I>::CreateRequest(IoCtx &ioctx, const std::string &imageid,
26 uint8_t order, uint8_t splay_width,
27 const std::string &object_pool,
28 uint64_t tag_class, TagData &tag_data,
29 const std::string &client_id,
30 ContextWQ *op_work_queue,
32 : m_ioctx(ioctx), m_image_id(imageid), m_order(order),
33 m_splay_width(splay_width), m_object_pool(object_pool),
34 m_tag_class(tag_class), m_tag_data(tag_data), m_image_client_id(client_id),
35 m_op_work_queue(op_work_queue), m_on_finish(on_finish) {
36 m_cct = reinterpret_cast<CephContext *>(m_ioctx.cct());
40 void CreateRequest<I>::send() {
41 ldout(m_cct, 20) << this << " " << __func__ << dendl;
43 if (m_order > 64 || m_order < 12) {
44 lderr(m_cct) << "order must be in the range [12, 64]" << dendl;
48 if (m_splay_width == 0) {
57 void CreateRequest<I>::get_pool_id() {
58 ldout(m_cct, 20) << this << " " << __func__ << dendl;
60 if (m_object_pool.empty()) {
65 librados::Rados rados(m_ioctx);
67 int r = rados.ioctx_create(m_object_pool.c_str(), data_ioctx);
69 lderr(m_cct) << "failed to create journal: "
70 << "error opening journal object pool '" << m_object_pool
71 << "': " << cpp_strerror(r) << dendl;
76 m_pool_id = data_ioctx.get_id();
81 void CreateRequest<I>::create_journal() {
82 ldout(m_cct, 20) << this << " " << __func__ << dendl;
84 ImageCtx::get_timer_instance(m_cct, &m_timer, &m_timer_lock);
85 m_journaler = new Journaler(m_op_work_queue, m_timer, m_timer_lock,
86 m_ioctx, m_image_id, m_image_client_id, {});
88 using klass = CreateRequest<I>;
89 Context *ctx = create_context_callback<klass, &klass::handle_create_journal>(this);
91 m_journaler->create(m_order, m_splay_width, m_pool_id, ctx);
95 Context *CreateRequest<I>::handle_create_journal(int *result) {
96 ldout(m_cct, 20) << __func__ << ": r=" << *result << dendl;
99 lderr(m_cct) << "failed to create journal: " << cpp_strerror(*result) << dendl;
100 shut_down_journaler(*result);
104 allocate_journal_tag();
109 void CreateRequest<I>::allocate_journal_tag() {
110 ldout(m_cct, 20) << this << " " << __func__ << dendl;
112 using klass = CreateRequest<I>;
113 Context *ctx = create_context_callback<klass, &klass::handle_journal_tag>(this);
115 ::encode(m_tag_data, m_bl);
116 m_journaler->allocate_tag(m_tag_class, m_bl, &m_tag, ctx);
120 Context *CreateRequest<I>::handle_journal_tag(int *result) {
121 ldout(m_cct, 20) << __func__ << ": r=" << *result << dendl;
124 lderr(m_cct) << "failed to allocate tag: " << cpp_strerror(*result) << dendl;
125 shut_down_journaler(*result);
134 void CreateRequest<I>::register_client() {
135 ldout(m_cct, 20) << this << " " << __func__ << dendl;
138 ::encode(ClientData{ImageClientMeta{m_tag.tag_class}}, m_bl);
140 using klass = CreateRequest<I>;
141 Context *ctx = create_context_callback<klass, &klass::handle_register_client>(this);
143 m_journaler->register_client(m_bl, ctx);
147 Context *CreateRequest<I>::handle_register_client(int *result) {
148 ldout(m_cct, 20) << __func__ << ": r=" << *result << dendl;
151 lderr(m_cct) << "failed to register client: " << cpp_strerror(*result) << dendl;
154 shut_down_journaler(*result);
159 void CreateRequest<I>::shut_down_journaler(int r) {
160 ldout(m_cct, 20) << this << " " << __func__ << dendl;
164 using klass = CreateRequest<I>;
165 Context *ctx = create_context_callback<klass, &klass::handle_journaler_shutdown>(this);
167 m_journaler->shut_down(ctx);
171 Context *CreateRequest<I>::handle_journaler_shutdown(int *result) {
172 ldout(m_cct, 20) << __func__ << ": r=" << *result << dendl;
175 lderr(m_cct) << "failed to shut down journaler: " << cpp_strerror(*result) << dendl;
185 // there was an error during journal creation, so we rollback
186 // what ever was done. the easiest way to do this is to invoke
187 // journal remove state machine, although it's not the most
188 // cleanest approach when it comes to redundancy, but that's
189 // ok during the failure path.
195 void CreateRequest<I>::remove_journal() {
196 ldout(m_cct, 20) << this << " " << __func__ << dendl;
198 using klass = CreateRequest<I>;
199 Context *ctx = create_context_callback<klass, &klass::handle_remove_journal>(this);
201 RemoveRequest<I> *req = RemoveRequest<I>::create(
202 m_ioctx, m_image_id, m_image_client_id, m_op_work_queue, ctx);
207 Context *CreateRequest<I>::handle_remove_journal(int *result) {
208 ldout(m_cct, 20) << __func__ << ": r=" << *result << dendl;
211 lderr(m_cct) << "error cleaning up journal after creation failed: "
212 << cpp_strerror(*result) << dendl;
220 void CreateRequest<I>::complete(int r) {
221 ldout(m_cct, 20) << this << " " << __func__ << dendl;
224 ldout(m_cct, 20) << "done." << dendl;
227 m_on_finish->complete(r);
231 } // namespace journal
232 } // namespace librbd
234 template class librbd::journal::CreateRequest<librbd::ImageCtx>;