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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "librbd/image/PreRemoveRequest.h"
#include "common/dout.h"
#include "common/errno.h"
#include "cls/rbd/cls_rbd_types.h"
#include "librbd/ExclusiveLock.h"
#include "librbd/Utils.h"
#include "librbd/exclusive_lock/StandardPolicy.h"
#include "librbd/image/ListWatchersRequest.h"
#include "librbd/journal/DisabledPolicy.h"
#include "librbd/operation/SnapshotRemoveRequest.h"
#define dout_subsys ceph_subsys_rbd
#undef dout_prefix
#define dout_prefix *_dout << "librbd::image::PreRemoveRequest: " << this \
<< " " << __func__ << ": "
namespace librbd {
namespace image {
namespace {
bool auto_delete_snapshot(const SnapInfo& snap_info) {
auto snap_namespace_type = cls::rbd::get_snap_namespace_type(
snap_info.snap_namespace);
switch (snap_namespace_type) {
case cls::rbd::SNAPSHOT_NAMESPACE_TYPE_TRASH:
return true;
default:
return false;
}
}
bool ignore_snapshot(const SnapInfo& snap_info) {
auto snap_namespace_type = cls::rbd::get_snap_namespace_type(
snap_info.snap_namespace);
switch (snap_namespace_type) {
case cls::rbd::SNAPSHOT_NAMESPACE_TYPE_MIRROR:
return true;
default:
return false;
}
}
} // anonymous namespace
using util::create_context_callback;
using util::create_rados_callback;
template <typename I>
void PreRemoveRequest<I>::send() {
auto cct = m_image_ctx->cct;
if (m_image_ctx->operations_disabled) {
lderr(cct) << "image operations disabled due to unsupported op features"
<< dendl;
finish(-EROFS);
return;
}
acquire_exclusive_lock();
}
template <typename I>
void PreRemoveRequest<I>::acquire_exclusive_lock() {
// lock for write for set_exclusive_lock_policy()
std::unique_lock owner_locker{m_image_ctx->owner_lock};
if (m_image_ctx->exclusive_lock == nullptr) {
owner_locker.unlock();
validate_image_removal();
return;
}
auto cct = m_image_ctx->cct;
ldout(cct, 5) << dendl;
// refuse to release exclusive lock when (in the midst of) removing
// the image
m_image_ctx->set_exclusive_lock_policy(
new exclusive_lock::StandardPolicy<I>(m_image_ctx));
// do not attempt to open the journal when removing the image in case
// it's corrupt
if (m_image_ctx->test_features(RBD_FEATURE_JOURNALING)) {
std::unique_lock image_locker{m_image_ctx->image_lock};
m_image_ctx->set_journal_policy(new journal::DisabledPolicy());
}
m_exclusive_lock = m_image_ctx->exclusive_lock;
auto ctx = create_context_callback<
PreRemoveRequest<I>,
&PreRemoveRequest<I>::handle_exclusive_lock>(this, m_exclusive_lock);
m_exclusive_lock->acquire_lock(ctx);
}
template <typename I>
void PreRemoveRequest<I>::handle_exclusive_lock(int r) {
auto cct = m_image_ctx->cct;
ldout(cct, 5) << "r=" << r << dendl;
if (r < 0 || !m_image_ctx->exclusive_lock->is_lock_owner()) {
if (!m_force) {
lderr(cct) << "cannot obtain exclusive lock - not removing" << dendl;
finish(-EBUSY);
} else {
ldout(cct, 5) << "cannot obtain exclusive lock - "
<< "proceeding due to force flag set" << dendl;
shut_down_exclusive_lock();
}
return;
}
validate_image_removal();
}
template <typename I>
void PreRemoveRequest<I>::shut_down_exclusive_lock() {
std::shared_lock owner_locker{m_image_ctx->owner_lock};
if (m_image_ctx->exclusive_lock == nullptr) {
owner_locker.unlock();
validate_image_removal();
return;
}
auto cct = m_image_ctx->cct;
ldout(cct, 5) << dendl;
auto ctx = create_context_callback<
PreRemoveRequest<I>,
&PreRemoveRequest<I>::handle_shut_down_exclusive_lock>(this);
m_exclusive_lock = m_image_ctx->exclusive_lock;
m_exclusive_lock->shut_down(ctx);
}
template <typename I>
void PreRemoveRequest<I>::handle_shut_down_exclusive_lock(int r) {
auto cct = m_image_ctx->cct;
ldout(cct, 5) << "r=" << r << dendl;
m_exclusive_lock->put();
m_exclusive_lock = nullptr;
if (r < 0) {
lderr(cct) << "error shutting down exclusive lock: " << cpp_strerror(r)
<< dendl;
finish(r);
return;
}
ceph_assert(m_image_ctx->exclusive_lock == nullptr);
validate_image_removal();
}
template <typename I>
void PreRemoveRequest<I>::validate_image_removal() {
auto cct = m_image_ctx->cct;
ldout(cct, 5) << dendl;
if (!m_image_ctx->ignore_migrating &&
m_image_ctx->test_features(RBD_FEATURE_MIGRATING)) {
lderr(cct) << "image in migration state - not removing" << dendl;
finish(-EBUSY);
return;
}
check_image_snaps();
}
template <typename I>
void PreRemoveRequest<I>::check_image_snaps() {
auto cct = m_image_ctx->cct;
ldout(cct, 5) << dendl;
m_image_ctx->image_lock.lock_shared();
for (auto& snap_info : m_image_ctx->snap_info) {
if (auto_delete_snapshot(snap_info.second)) {
m_snap_infos.insert(snap_info);
} else if (!ignore_snapshot(snap_info.second)) {
m_image_ctx->image_lock.unlock_shared();
ldout(cct, 5) << "image has snapshots - not removing" << dendl;
finish(-ENOTEMPTY);
return;
}
}
m_image_ctx->image_lock.unlock_shared();
list_image_watchers();
}
template <typename I>
void PreRemoveRequest<I>::list_image_watchers() {
auto cct = m_image_ctx->cct;
ldout(cct, 5) << dendl;
int flags = LIST_WATCHERS_FILTER_OUT_MY_INSTANCE |
LIST_WATCHERS_FILTER_OUT_MIRROR_INSTANCES;
auto ctx = create_context_callback<
PreRemoveRequest<I>,
&PreRemoveRequest<I>::handle_list_image_watchers>(this);
auto req = ListWatchersRequest<I>::create(*m_image_ctx, flags, &m_watchers,
ctx);
req->send();
}
template <typename I>
void PreRemoveRequest<I>::handle_list_image_watchers(int r) {
auto cct = m_image_ctx->cct;
ldout(cct, 5) << "r=" << r << dendl;
if (r < 0) {
lderr(cct) << "error listing image watchers: " << cpp_strerror(r) << dendl;
finish(r);
return;
}
check_image_watchers();
}
template <typename I>
void PreRemoveRequest<I>::check_image_watchers() {
auto cct = m_image_ctx->cct;
ldout(cct, 5) << dendl;
if (!m_watchers.empty()) {
lderr(cct) << "image has watchers - not removing" << dendl;
finish(-EBUSY);
return;
}
check_group();
}
template <typename I>
void PreRemoveRequest<I>::check_group() {
if (m_image_ctx->old_format) {
finish(0);
return;
}
auto cct = m_image_ctx->cct;
ldout(cct, 5) << dendl;
librados::ObjectReadOperation op;
librbd::cls_client::image_group_get_start(&op);
auto rados_completion = create_rados_callback<
PreRemoveRequest<I>, &PreRemoveRequest<I>::handle_check_group>(this);
m_out_bl.clear();
int r = m_image_ctx->md_ctx.aio_operate(m_image_ctx->header_oid,
rados_completion, &op, &m_out_bl);
ceph_assert(r == 0);
rados_completion->release();
}
template <typename I>
void PreRemoveRequest<I>::handle_check_group(int r) {
auto cct = m_image_ctx->cct;
ldout(cct, 5) << "r=" << r << dendl;
cls::rbd::GroupSpec s;
if (r == 0) {
auto it = m_out_bl.cbegin();
r = librbd::cls_client::image_group_get_finish(&it, &s);
}
if (r < 0 && r != -EOPNOTSUPP) {
lderr(cct) << "error fetching group for image: " << cpp_strerror(r)
<< dendl;
finish(r);
return;
}
if (s.is_valid()) {
lderr(cct) << "image is in a group - not removing" << dendl;
finish(-EMLINK);
return;
}
remove_snapshot();
}
template <typename I>
void PreRemoveRequest<I>::remove_snapshot() {
if (m_snap_infos.empty()) {
finish(0);
return;
}
auto cct = m_image_ctx->cct;
auto snap_id = m_snap_infos.begin()->first;
auto& snap_info = m_snap_infos.begin()->second;
ldout(cct, 20) << "snap_id=" << snap_id << ", "
<< "snap_name=" << snap_info.name << dendl;
std::shared_lock owner_lock{m_image_ctx->owner_lock};
auto ctx = create_context_callback<
PreRemoveRequest<I>, &PreRemoveRequest<I>::handle_remove_snapshot>(this);
auto req = librbd::operation::SnapshotRemoveRequest<I>::create(
*m_image_ctx, snap_info.snap_namespace, snap_info.name,
snap_id, ctx);
req->send();
}
template <typename I>
void PreRemoveRequest<I>::handle_remove_snapshot(int r) {
auto cct = m_image_ctx->cct;
ldout(cct, 5) << "r=" << r << dendl;
if (r == -EBUSY) {
ldout(cct, 5) << "skipping attached child" << dendl;
if (m_ret_val == 0) {
m_ret_val = -ECHILD;
}
} else if (r < 0 && r != -ENOENT) {
auto snap_id = m_snap_infos.begin()->first;
lderr(cct) << "failed to auto-prune snapshot " << snap_id << ": "
<< cpp_strerror(r) << dendl;
finish(r);
return;
}
ceph_assert(!m_snap_infos.empty());
m_snap_infos.erase(m_snap_infos.begin());
remove_snapshot();
}
template <typename I>
void PreRemoveRequest<I>::finish(int r) {
auto cct = m_image_ctx->cct;
ldout(cct, 5) << "r=" << r << dendl;
if (m_ret_val == 0) {
m_ret_val = r;
}
m_on_finish->complete(m_ret_val);
delete this;
}
} // namespace image
} // namespace librbd
template class librbd::image::PreRemoveRequest<librbd::ImageCtx>;
|