summaryrefslogtreecommitdiffstats
path: root/src/tools/rbd_mirror/image_deleter/TrashRemoveRequest.cc
blob: 4d7c1c9df200d960716a0d5f1378b93b17b972aa (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "tools/rbd_mirror/image_deleter/TrashRemoveRequest.h"
#include "include/ceph_assert.h"
#include "common/debug.h"
#include "common/errno.h"
#include "common/WorkQueue.h"
#include "cls/rbd/cls_rbd_client.h"
#include "librbd/ImageCtx.h"
#include "librbd/TrashWatcher.h"
#include "librbd/Utils.h"
#include "librbd/asio/ContextWQ.h"
#include "librbd/trash/RemoveRequest.h"
#include "tools/rbd_mirror/image_deleter/SnapshotPurgeRequest.h"

#define dout_context g_ceph_context
#define dout_subsys ceph_subsys_rbd_mirror
#undef dout_prefix
#define dout_prefix *_dout << "rbd::mirror::image_deleter::TrashRemoveRequest: " \
                           << this << " " << __func__ << ": "

namespace rbd {
namespace mirror {
namespace image_deleter {

using librbd::util::create_context_callback;
using librbd::util::create_rados_callback;

template <typename I>
void TrashRemoveRequest<I>::send() {
  *m_error_result = ERROR_RESULT_RETRY;

  get_trash_image_spec();
}

template <typename I>
void TrashRemoveRequest<I>::get_trash_image_spec() {
  dout(10) << dendl;

  librados::ObjectReadOperation op;
  librbd::cls_client::trash_get_start(&op, m_image_id);

  auto aio_comp = create_rados_callback<
    TrashRemoveRequest<I>,
    &TrashRemoveRequest<I>::handle_get_trash_image_spec>(this);
  m_out_bl.clear();
  int r = m_io_ctx.aio_operate(RBD_TRASH, aio_comp, &op, &m_out_bl);
  ceph_assert(r == 0);
  aio_comp->release();
}

template <typename I>
void TrashRemoveRequest<I>::handle_get_trash_image_spec(int r) {
  dout(10) << "r=" << r << dendl;

  if (r == 0) {
    auto bl_it = m_out_bl.cbegin();
    r = librbd::cls_client::trash_get_finish(&bl_it, &m_trash_image_spec);
  }

  if (r == -ENOENT || (r >= 0 && m_trash_image_spec.source !=
                                   cls::rbd::TRASH_IMAGE_SOURCE_MIRRORING)) {
    dout(10) << "image id " << m_image_id << " not in mirroring trash" << dendl;
    finish(0);
    return;
  } else if (r < 0) {
    derr << "error getting image id " << m_image_id << " info from trash: "
         << cpp_strerror(r) << dendl;
    finish(r);
    return;
  }

  if (m_trash_image_spec.state != cls::rbd::TRASH_IMAGE_STATE_NORMAL &&
      m_trash_image_spec.state != cls::rbd::TRASH_IMAGE_STATE_REMOVING) {
    dout(10) << "image " << m_image_id << " is not in an expected trash state: "
             << m_trash_image_spec.state << dendl;
    *m_error_result = ERROR_RESULT_RETRY_IMMEDIATELY;
    finish(-EBUSY);
    return;
  }

  set_trash_state();
}

template <typename I>
void TrashRemoveRequest<I>::set_trash_state() {
  if (m_trash_image_spec.state == cls::rbd::TRASH_IMAGE_STATE_REMOVING) {
    get_snap_context();
    return;
  }

  dout(10) << dendl;

  librados::ObjectWriteOperation op;
  librbd::cls_client::trash_state_set(&op, m_image_id,
                                      cls::rbd::TRASH_IMAGE_STATE_REMOVING,
                                      cls::rbd::TRASH_IMAGE_STATE_NORMAL);

  auto aio_comp = create_rados_callback<
    TrashRemoveRequest<I>,
    &TrashRemoveRequest<I>::handle_set_trash_state>(this);
  int r = m_io_ctx.aio_operate(RBD_TRASH, aio_comp, &op);
  ceph_assert(r == 0);
  aio_comp->release();
}

template <typename I>
void TrashRemoveRequest<I>::handle_set_trash_state(int r) {
  dout(10) << "r=" << r << dendl;

  if (r == -ENOENT) {
    dout(10) << "image id " << m_image_id << " not in mirroring trash" << dendl;
    finish(0);
    return;
  } else if (r < 0 && r != -EOPNOTSUPP) {
    derr << "error setting trash image state for image id " << m_image_id
         << ": " << cpp_strerror(r) << dendl;
    finish(r);
    return;
  }

  get_snap_context();
}

template <typename I>
void TrashRemoveRequest<I>::get_snap_context() {
  dout(10) << dendl;

  librados::ObjectReadOperation op;
  librbd::cls_client::get_snapcontext_start(&op);

  std::string header_oid = librbd::util::header_name(m_image_id);

  auto aio_comp = create_rados_callback<
    TrashRemoveRequest<I>,
    &TrashRemoveRequest<I>::handle_get_snap_context>(this);
  m_out_bl.clear();
  int r = m_io_ctx.aio_operate(header_oid, aio_comp, &op, &m_out_bl);
  ceph_assert(r == 0);
  aio_comp->release();
}

template <typename I>
void TrashRemoveRequest<I>::handle_get_snap_context(int r) {
  dout(10) << "r=" << r << dendl;

  ::SnapContext snapc;
  if (r == 0) {
    auto bl_it = m_out_bl.cbegin();
    r = librbd::cls_client::get_snapcontext_finish(&bl_it, &snapc);
  }
  if (r < 0 && r != -ENOENT) {
    derr << "error retrieving snapshot context for image "
         << m_image_id << ": " << cpp_strerror(r) << dendl;
    finish(r);
    return;
  }

  m_has_snapshots = (!snapc.empty());
  purge_snapshots();
}

template <typename I>
void TrashRemoveRequest<I>::purge_snapshots() {
  if (!m_has_snapshots) {
    remove_image();
    return;
  }

  dout(10) << dendl;
  auto ctx = create_context_callback<
    TrashRemoveRequest<I>,
    &TrashRemoveRequest<I>::handle_purge_snapshots>(this);
  auto req = SnapshotPurgeRequest<I>::create(m_io_ctx, m_image_id, ctx);
  req->send();
}

template <typename I>
void TrashRemoveRequest<I>::handle_purge_snapshots(int r) {
  dout(10) << "r=" << r << dendl;

  if (r == -EBUSY) {
    dout(10) << "snapshots still in-use" << dendl;
    *m_error_result = ERROR_RESULT_RETRY_IMMEDIATELY;
    finish(r);
    return;
  } else if (r < 0) {
    derr << "failed to purge image snapshots: " << cpp_strerror(r) << dendl;
    finish(r);
    return;
  }

  remove_image();
}

template <typename I>
void TrashRemoveRequest<I>::remove_image() {
  dout(10) << dendl;

  auto ctx = create_context_callback<
    TrashRemoveRequest<I>,
    &TrashRemoveRequest<I>::handle_remove_image>(this);
  auto req = librbd::trash::RemoveRequest<I>::create(
    m_io_ctx, m_image_id, m_op_work_queue, true, m_progress_ctx,
    ctx);
  req->send();
}

template <typename I>
void TrashRemoveRequest<I>::handle_remove_image(int r) {
  dout(10) << "r=" << r << dendl;
  if (r == -ENOTEMPTY) {
    // image must have clone v2 snapshot still associated to child
    dout(10) << "snapshots still in-use" << dendl;
    *m_error_result = ERROR_RESULT_RETRY_IMMEDIATELY;
    finish(-EBUSY);
    return;
  }

  if (r < 0 && r != -ENOENT) {
    derr << "error removing image " << m_image_id << " "
         << "(" << m_image_id << ") from local pool: "
         << cpp_strerror(r) << dendl;
    finish(r);
    return;
  }

  notify_trash_removed();
}

template <typename I>
void TrashRemoveRequest<I>::notify_trash_removed() {
  dout(10) << dendl;

  Context *ctx = create_context_callback<
    TrashRemoveRequest<I>,
    &TrashRemoveRequest<I>::handle_notify_trash_removed>(this);
  librbd::TrashWatcher<I>::notify_image_removed(m_io_ctx, m_image_id, ctx);
}

template <typename I>
void TrashRemoveRequest<I>::handle_notify_trash_removed(int r) {
  dout(10) << "r=" << r << dendl;

  if (r < 0) {
    derr << "failed to notify trash watchers: " << cpp_strerror(r) << dendl;
  }

  finish(0);
}

template <typename I>
void TrashRemoveRequest<I>::finish(int r) {
  dout(10) << "r=" << r << dendl;

  m_on_finish->complete(r);
  delete this;
}

} // namespace image_deleter
} // namespace mirror
} // namespace rbd

template class rbd::mirror::image_deleter::TrashRemoveRequest<librbd::ImageCtx>;