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 "librbd/operation/FlattenRequest.h"
#include "librbd/AsyncObjectThrottle.h"
#include "librbd/ExclusiveLock.h"
#include "librbd/ImageCtx.h"
#include "librbd/crypto/CryptoInterface.h"
#include "librbd/crypto/EncryptionFormat.h"
#include "librbd/image/DetachChildRequest.h"
#include "librbd/image/DetachParentRequest.h"
#include "librbd/Types.h"
#include "librbd/io/ObjectRequest.h"
#include "librbd/io/Utils.h"
#include "common/dout.h"
#include "common/errno.h"
#include "osdc/Striper.h"
#include <boost/lambda/bind.hpp>
#include <boost/lambda/construct.hpp>
#define dout_subsys ceph_subsys_rbd
#undef dout_prefix
#define dout_prefix *_dout << "librbd::operation::FlattenRequest: " << this \
<< " " << __func__ << ": "
namespace librbd {
namespace operation {
using util::create_context_callback;
using util::create_rados_callback;
template <typename I>
class C_FlattenObject : public C_AsyncObjectThrottle<I> {
public:
C_FlattenObject(AsyncObjectThrottle<I> &throttle, I *image_ctx,
IOContext io_context, uint64_t object_no)
: C_AsyncObjectThrottle<I>(throttle, *image_ctx), m_io_context(io_context),
m_object_no(object_no) {
}
int send() override {
I &image_ctx = this->m_image_ctx;
ceph_assert(ceph_mutex_is_locked(image_ctx.owner_lock));
CephContext *cct = image_ctx.cct;
if (image_ctx.exclusive_lock != nullptr &&
!image_ctx.exclusive_lock->is_lock_owner()) {
ldout(cct, 1) << "lost exclusive lock during flatten" << dendl;
return -ERESTART;
}
{
std::shared_lock image_lock{image_ctx.image_lock};
if (image_ctx.object_map != nullptr &&
!image_ctx.object_map->object_may_not_exist(m_object_no)) {
// can skip because the object already exists
return 1;
}
}
if (!io::util::trigger_copyup(
&image_ctx, m_object_no, m_io_context, this)) {
// stop early if the parent went away - it just means
// another flatten finished first or the image was resized
return 1;
}
return 0;
}
private:
IOContext m_io_context;
uint64_t m_object_no;
};
template <typename I>
bool FlattenRequest<I>::should_complete(int r) {
I &image_ctx = this->m_image_ctx;
CephContext *cct = image_ctx.cct;
ldout(cct, 5) << "r=" << r << dendl;
if (r < 0) {
lderr(cct) << "encountered error: " << cpp_strerror(r) << dendl;
}
return true;
}
template <typename I>
void FlattenRequest<I>::send_op() {
flatten_objects();
}
template <typename I>
void FlattenRequest<I>::flatten_objects() {
I &image_ctx = this->m_image_ctx;
ceph_assert(ceph_mutex_is_locked(image_ctx.owner_lock));
CephContext *cct = image_ctx.cct;
ldout(cct, 5) << dendl;
assert(ceph_mutex_is_locked(image_ctx.owner_lock));
auto ctx = create_context_callback<
FlattenRequest<I>,
&FlattenRequest<I>::handle_flatten_objects>(this);
typename AsyncObjectThrottle<I>::ContextFactory context_factory(
boost::lambda::bind(boost::lambda::new_ptr<C_FlattenObject<I> >(),
boost::lambda::_1, &image_ctx, image_ctx.get_data_io_context(),
boost::lambda::_2));
AsyncObjectThrottle<I> *throttle = new AsyncObjectThrottle<I>(
this, image_ctx, context_factory, ctx, &m_prog_ctx, m_start_object_no,
m_start_object_no + m_overlap_objects);
throttle->start_ops(
image_ctx.config.template get_val<uint64_t>("rbd_concurrent_management_ops"));
}
template <typename I>
void FlattenRequest<I>::handle_flatten_objects(int r) {
I &image_ctx = this->m_image_ctx;
CephContext *cct = image_ctx.cct;
ldout(cct, 5) << "r=" << r << dendl;
if (r == -ERESTART) {
ldout(cct, 5) << "flatten operation interrupted" << dendl;
this->complete(r);
return;
} else if (r < 0) {
lderr(cct) << "flatten encountered an error: " << cpp_strerror(r) << dendl;
this->complete(r);
return;
}
crypto_flatten();
}
template <typename I>
void FlattenRequest<I>::crypto_flatten() {
I &image_ctx = this->m_image_ctx;
CephContext *cct = image_ctx.cct;
auto encryption_format = image_ctx.encryption_format.get();
if (encryption_format == nullptr) {
detach_child();
return;
}
ldout(cct, 5) << dendl;
auto ctx = create_context_callback<
FlattenRequest<I>,
&FlattenRequest<I>::handle_crypto_flatten>(this);
encryption_format->flatten(&image_ctx, ctx);
}
template <typename I>
void FlattenRequest<I>::handle_crypto_flatten(int r) {
I &image_ctx = this->m_image_ctx;
CephContext *cct = image_ctx.cct;
ldout(cct, 5) << "r=" << r << dendl;
if (r < 0) {
lderr(cct) << "error flattening crypto: " << cpp_strerror(r) << dendl;
this->complete(r);
return;
}
detach_child();
}
template <typename I>
void FlattenRequest<I>::detach_child() {
I &image_ctx = this->m_image_ctx;
CephContext *cct = image_ctx.cct;
// should have been canceled prior to releasing lock
image_ctx.owner_lock.lock_shared();
ceph_assert(image_ctx.exclusive_lock == nullptr ||
image_ctx.exclusive_lock->is_lock_owner());
// if there are no snaps, remove from the children object as well
// (if snapshots remain, they have their own parent info, and the child
// will be removed when the last snap goes away)
image_ctx.image_lock.lock_shared();
if ((image_ctx.features & RBD_FEATURE_DEEP_FLATTEN) == 0 &&
!image_ctx.snaps.empty()) {
image_ctx.image_lock.unlock_shared();
image_ctx.owner_lock.unlock_shared();
detach_parent();
return;
}
image_ctx.image_lock.unlock_shared();
ldout(cct, 5) << dendl;
auto ctx = create_context_callback<
FlattenRequest<I>,
&FlattenRequest<I>::handle_detach_child>(this);
auto req = image::DetachChildRequest<I>::create(image_ctx, ctx);
req->send();
image_ctx.owner_lock.unlock_shared();
}
template <typename I>
void FlattenRequest<I>::handle_detach_child(int r) {
I &image_ctx = this->m_image_ctx;
CephContext *cct = image_ctx.cct;
ldout(cct, 5) << "r=" << r << dendl;
if (r < 0 && r != -ENOENT) {
lderr(cct) << "detach encountered an error: " << cpp_strerror(r) << dendl;
this->complete(r);
return;
}
detach_parent();
}
template <typename I>
void FlattenRequest<I>::detach_parent() {
I &image_ctx = this->m_image_ctx;
CephContext *cct = image_ctx.cct;
ldout(cct, 5) << dendl;
// should have been canceled prior to releasing lock
image_ctx.owner_lock.lock_shared();
ceph_assert(image_ctx.exclusive_lock == nullptr ||
image_ctx.exclusive_lock->is_lock_owner());
// stop early if the parent went away - it just means
// another flatten finished first, so this one is useless.
image_ctx.image_lock.lock_shared();
if (!image_ctx.parent) {
ldout(cct, 5) << "image already flattened" << dendl;
image_ctx.image_lock.unlock_shared();
image_ctx.owner_lock.unlock_shared();
this->complete(0);
return;
}
image_ctx.image_lock.unlock_shared();
// remove parent from this (base) image
auto ctx = create_context_callback<
FlattenRequest<I>,
&FlattenRequest<I>::handle_detach_parent>(this);
auto req = image::DetachParentRequest<I>::create(image_ctx, ctx);
req->send();
image_ctx.owner_lock.unlock_shared();
}
template <typename I>
void FlattenRequest<I>::handle_detach_parent(int r) {
I &image_ctx = this->m_image_ctx;
CephContext *cct = image_ctx.cct;
ldout(cct, 5) << "r=" << r << dendl;
if (r < 0) {
lderr(cct) << "remove parent encountered an error: " << cpp_strerror(r)
<< dendl;
}
this->complete(r);
}
} // namespace operation
} // namespace librbd
template class librbd::operation::FlattenRequest<librbd::ImageCtx>;
|