summaryrefslogtreecommitdiffstats
path: root/src/librbd/journal/RemoveRequest.cc
blob: 6ed5f54c3b53e2d20f34279d9201de83e776ee7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "common/dout.h"
#include "common/errno.h"
#include "common/Timer.h"
#include "common/WorkQueue.h"
#include "journal/Settings.h"
#include "include/ceph_assert.h"
#include "librbd/Utils.h"
#include "librbd/journal/RemoveRequest.h"

#define dout_subsys ceph_subsys_rbd
#undef dout_prefix
#define dout_prefix *_dout << "librbd::Journal::RemoveRequest: "

namespace librbd {

using util::create_context_callback;

namespace journal {

template<typename I>
RemoveRequest<I>::RemoveRequest(IoCtx &ioctx, const std::string &image_id,
                                const std::string &client_id,
                                ContextWQ *op_work_queue,
                                Context *on_finish)
  : m_ioctx(ioctx), m_image_id(image_id), m_image_client_id(client_id),
    m_op_work_queue(op_work_queue), m_on_finish(on_finish) {
  m_cct = reinterpret_cast<CephContext *>(m_ioctx.cct());
}

template<typename I>
void RemoveRequest<I>::send() {
  ldout(m_cct, 20) << this << " " << __func__ << dendl;

  stat_journal();
}

template<typename I>
void RemoveRequest<I>::stat_journal() {
  ldout(m_cct, 20) << this << " " << __func__ << dendl;

  ImageCtx::get_timer_instance(m_cct, &m_timer, &m_timer_lock);
  m_journaler = new Journaler(m_op_work_queue, m_timer, m_timer_lock,
                              m_ioctx, m_image_id, m_image_client_id, {});

  using klass = RemoveRequest<I>;
  Context *ctx = create_context_callback<klass, &klass::handle_stat_journal>(this);

  m_journaler->exists(ctx);
}

template<typename I>
Context *RemoveRequest<I>::handle_stat_journal(int *result) {
  ldout(m_cct, 20) << __func__ << ": r=" << *result << dendl;

  if ((*result < 0) && (*result != -ENOENT)) {
    lderr(m_cct) << "failed to stat journal header: " << cpp_strerror(*result) << dendl;
    shut_down_journaler(*result);
    return nullptr;
  }

  if (*result == -ENOENT) {
    shut_down_journaler(0);
    return nullptr;
  }

  init_journaler();
  return nullptr;
}

template<typename I>
void RemoveRequest<I>::init_journaler() {
  ldout(m_cct, 20) << this << " " << __func__ << dendl;

  using klass = RemoveRequest<I>;
  Context *ctx = create_context_callback<klass, &klass::handle_init_journaler>(this);

  m_journaler->init(ctx);
}

template<typename I>
Context *RemoveRequest<I>::handle_init_journaler(int *result) {
  ldout(m_cct, 20) << __func__ << ": r=" << *result << dendl;

  if ((*result < 0) && (*result != -ENOENT)) {
    lderr(m_cct) << "failed to init journaler: " << cpp_strerror(*result) << dendl;
    shut_down_journaler(*result);
    return nullptr;
  }

  remove_journal();
  return nullptr;
}

template<typename I>
void RemoveRequest<I>::remove_journal() {
  ldout(m_cct, 20) << this << " " << __func__ << dendl;

  using klass = RemoveRequest<I>;
  Context *ctx = create_context_callback<klass, &klass::handle_remove_journal>(this);

  m_journaler->remove(true, ctx);
}

template<typename I>
Context *RemoveRequest<I>::handle_remove_journal(int *result) {
  ldout(m_cct, 20) << __func__ << ": r=" << *result << dendl;

  if (*result < 0) {
    lderr(m_cct) << "failed to remove journal: " << cpp_strerror(*result) << dendl;
  }

  shut_down_journaler(*result);
  return nullptr;
}

template<typename I>
void RemoveRequest<I>::shut_down_journaler(int r) {
  ldout(m_cct, 20) << this << " " << __func__ << dendl;

  m_r_saved = r;

  using klass = RemoveRequest<I>;
  Context *ctx = create_context_callback<klass, &klass::handle_journaler_shutdown>(this);

  m_journaler->shut_down(ctx);
}

template<typename I>
Context *RemoveRequest<I>::handle_journaler_shutdown(int *result) {
  ldout(m_cct, 20) << __func__ << ": r=" << *result << dendl;

  if (*result < 0) {
    lderr(m_cct) << "failed to shut down journaler: " << cpp_strerror(*result) << dendl;
  }

  delete m_journaler;

  if (m_r_saved == 0) {
    ldout(m_cct, 20) << "done." << dendl;
  }

  m_on_finish->complete(m_r_saved);
  delete this;

  return nullptr;
}

} // namespace journal
} // namespace librbd

template class librbd::journal::RemoveRequest<librbd::ImageCtx>;