]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | ||
4 | #include "librbd/operation/SnapshotRollbackRequest.h" | |
5 | #include "include/rados/librados.hpp" | |
6 | #include "common/dout.h" | |
7 | #include "common/errno.h" | |
8 | #include "librbd/AsyncObjectThrottle.h" | |
9 | #include "librbd/ImageCtx.h" | |
10 | #include "librbd/ObjectMap.h" | |
11 | #include "librbd/Utils.h" | |
12 | #include "librbd/io/ImageRequestWQ.h" | |
13 | #include "librbd/operation/ResizeRequest.h" | |
14 | #include "osdc/Striper.h" | |
15 | #include <boost/lambda/bind.hpp> | |
16 | #include <boost/lambda/construct.hpp> | |
17 | ||
18 | #define dout_subsys ceph_subsys_rbd | |
19 | #undef dout_prefix | |
20 | #define dout_prefix *_dout << "librbd::SnapshotRollbackRequest: " | |
21 | ||
22 | namespace librbd { | |
23 | namespace operation { | |
24 | ||
25 | using util::create_context_callback; | |
26 | using util::create_rados_callback; | |
27 | ||
28 | namespace { | |
29 | ||
30 | template <typename I> | |
31 | class C_RollbackObject : public C_AsyncObjectThrottle<I> { | |
32 | public: | |
33 | C_RollbackObject(AsyncObjectThrottle<I> &throttle, I *image_ctx, | |
34 | uint64_t snap_id, uint64_t object_num) | |
35 | : C_AsyncObjectThrottle<I>(throttle, *image_ctx), m_snap_id(snap_id), | |
36 | m_object_num(object_num) { | |
37 | } | |
38 | ||
39 | int send() override { | |
40 | I &image_ctx = this->m_image_ctx; | |
41 | CephContext *cct = image_ctx.cct; | |
42 | ldout(cct, 20) << "C_RollbackObject: " << __func__ << ": object_num=" | |
43 | << m_object_num << dendl; | |
44 | ||
45 | std::string oid = image_ctx.get_object_name(m_object_num); | |
46 | ||
47 | librados::ObjectWriteOperation op; | |
48 | op.selfmanaged_snap_rollback(m_snap_id); | |
49 | ||
50 | librados::AioCompletion *rados_completion = | |
51 | util::create_rados_callback(this); | |
52 | image_ctx.data_ctx.aio_operate(oid, rados_completion, &op); | |
53 | rados_completion->release(); | |
54 | return 0; | |
55 | } | |
56 | ||
57 | private: | |
58 | uint64_t m_snap_id; | |
59 | uint64_t m_object_num; | |
60 | }; | |
61 | ||
62 | } // anonymous namespace | |
63 | ||
64 | template <typename I> | |
65 | SnapshotRollbackRequest<I>::SnapshotRollbackRequest(I &image_ctx, | |
66 | Context *on_finish, | |
67 | const cls::rbd::SnapshotNamespace &snap_namespace, | |
68 | const std::string &snap_name, | |
69 | uint64_t snap_id, | |
70 | uint64_t snap_size, | |
71 | ProgressContext &prog_ctx) | |
72 | : Request<I>(image_ctx, on_finish), m_snap_namespace(snap_namespace), | |
73 | m_snap_name(snap_name), m_snap_id(snap_id), | |
74 | m_snap_size(snap_size), m_prog_ctx(prog_ctx), m_object_map(nullptr) { | |
75 | } | |
76 | ||
77 | template <typename I> | |
78 | SnapshotRollbackRequest<I>::~SnapshotRollbackRequest() { | |
79 | I &image_ctx = this->m_image_ctx; | |
80 | if (m_blocking_writes) { | |
81 | image_ctx.io_work_queue->unblock_writes(); | |
82 | } | |
83 | delete m_object_map; | |
84 | } | |
85 | ||
86 | template <typename I> | |
87 | void SnapshotRollbackRequest<I>::send_op() { | |
88 | send_block_writes(); | |
89 | } | |
90 | ||
91 | template <typename I> | |
92 | void SnapshotRollbackRequest<I>::send_block_writes() { | |
93 | I &image_ctx = this->m_image_ctx; | |
94 | CephContext *cct = image_ctx.cct; | |
95 | ldout(cct, 5) << this << " " << __func__ << dendl; | |
96 | ||
97 | m_blocking_writes = true; | |
98 | image_ctx.io_work_queue->block_writes(create_context_callback< | |
99 | SnapshotRollbackRequest<I>, | |
100 | &SnapshotRollbackRequest<I>::handle_block_writes>(this)); | |
101 | } | |
102 | ||
103 | template <typename I> | |
104 | Context *SnapshotRollbackRequest<I>::handle_block_writes(int *result) { | |
105 | I &image_ctx = this->m_image_ctx; | |
106 | CephContext *cct = image_ctx.cct; | |
107 | ldout(cct, 5) << this << " " << __func__ << ": r=" << *result << dendl; | |
108 | ||
109 | if (*result < 0) { | |
110 | lderr(cct) << "failed to block writes: " << cpp_strerror(*result) << dendl; | |
111 | return this->create_context_finisher(*result); | |
112 | } | |
113 | ||
114 | send_resize_image(); | |
115 | return nullptr; | |
116 | } | |
117 | ||
118 | template <typename I> | |
119 | void SnapshotRollbackRequest<I>::send_resize_image() { | |
120 | I &image_ctx = this->m_image_ctx; | |
121 | ||
122 | uint64_t current_size; | |
123 | { | |
124 | RWLock::RLocker owner_locker(image_ctx.owner_lock); | |
125 | RWLock::RLocker snap_locker(image_ctx.snap_lock); | |
126 | current_size = image_ctx.get_image_size(CEPH_NOSNAP); | |
127 | } | |
128 | ||
129 | if (current_size == m_snap_size) { | |
130 | send_rollback_object_map(); | |
131 | return; | |
132 | } | |
133 | ||
134 | CephContext *cct = image_ctx.cct; | |
135 | ldout(cct, 5) << this << " " << __func__ << dendl; | |
136 | ||
b32b8144 | 137 | RWLock::RLocker owner_locker(image_ctx.owner_lock); |
7c673cae FG |
138 | Context *ctx = create_context_callback< |
139 | SnapshotRollbackRequest<I>, | |
140 | &SnapshotRollbackRequest<I>::handle_resize_image>(this); | |
141 | ResizeRequest<I> *req = ResizeRequest<I>::create(image_ctx, ctx, m_snap_size, | |
142 | true, m_no_op_prog_ctx, 0, true); | |
143 | req->send(); | |
144 | } | |
145 | ||
146 | template <typename I> | |
147 | Context *SnapshotRollbackRequest<I>::handle_resize_image(int *result) { | |
148 | I &image_ctx = this->m_image_ctx; | |
149 | CephContext *cct = image_ctx.cct; | |
150 | ldout(cct, 5) << this << " " << __func__ << ": r=" << *result << dendl; | |
151 | ||
152 | if (*result < 0) { | |
153 | lderr(cct) << "failed to resize image for rollback: " | |
154 | << cpp_strerror(*result) << dendl; | |
155 | return this->create_context_finisher(*result); | |
156 | } | |
157 | ||
158 | send_rollback_object_map(); | |
159 | return nullptr; | |
160 | } | |
161 | ||
162 | template <typename I> | |
163 | void SnapshotRollbackRequest<I>::send_rollback_object_map() { | |
164 | I &image_ctx = this->m_image_ctx; | |
165 | ||
166 | { | |
167 | RWLock::RLocker owner_locker(image_ctx.owner_lock); | |
168 | RWLock::RLocker snap_locker(image_ctx.snap_lock); | |
169 | RWLock::WLocker object_map_lock(image_ctx.object_map_lock); | |
170 | if (image_ctx.object_map != nullptr) { | |
171 | CephContext *cct = image_ctx.cct; | |
172 | ldout(cct, 5) << this << " " << __func__ << dendl; | |
173 | ||
174 | Context *ctx = create_context_callback< | |
175 | SnapshotRollbackRequest<I>, | |
176 | &SnapshotRollbackRequest<I>::handle_rollback_object_map>(this); | |
177 | image_ctx.object_map->rollback(m_snap_id, ctx); | |
178 | return; | |
179 | } | |
180 | } | |
181 | ||
182 | send_rollback_objects(); | |
183 | } | |
184 | ||
185 | template <typename I> | |
186 | Context *SnapshotRollbackRequest<I>::handle_rollback_object_map(int *result) { | |
187 | I &image_ctx = this->m_image_ctx; | |
188 | CephContext *cct = image_ctx.cct; | |
189 | ldout(cct, 5) << this << " " << __func__ << ": r=" << *result << dendl; | |
190 | ||
191 | assert(*result == 0); | |
192 | send_rollback_objects(); | |
193 | return nullptr; | |
194 | } | |
195 | ||
196 | template <typename I> | |
197 | void SnapshotRollbackRequest<I>::send_rollback_objects() { | |
198 | I &image_ctx = this->m_image_ctx; | |
199 | CephContext *cct = image_ctx.cct; | |
200 | ldout(cct, 5) << this << " " << __func__ << dendl; | |
201 | ||
202 | RWLock::RLocker owner_locker(image_ctx.owner_lock); | |
203 | uint64_t num_objects; | |
204 | { | |
205 | RWLock::RLocker snap_locker(image_ctx.snap_lock); | |
206 | num_objects = Striper::get_num_objects(image_ctx.layout, | |
207 | image_ctx.get_current_size()); | |
208 | } | |
209 | ||
210 | Context *ctx = create_context_callback< | |
211 | SnapshotRollbackRequest<I>, | |
212 | &SnapshotRollbackRequest<I>::handle_rollback_objects>(this); | |
213 | typename AsyncObjectThrottle<I>::ContextFactory context_factory( | |
214 | boost::lambda::bind(boost::lambda::new_ptr<C_RollbackObject<I> >(), | |
215 | boost::lambda::_1, &image_ctx, m_snap_id, boost::lambda::_2)); | |
216 | AsyncObjectThrottle<I> *throttle = new AsyncObjectThrottle<I>( | |
217 | this, image_ctx, context_factory, ctx, &m_prog_ctx, 0, num_objects); | |
218 | throttle->start_ops(image_ctx.concurrent_management_ops); | |
219 | } | |
220 | ||
221 | template <typename I> | |
222 | Context *SnapshotRollbackRequest<I>::handle_rollback_objects(int *result) { | |
223 | I &image_ctx = this->m_image_ctx; | |
224 | CephContext *cct = image_ctx.cct; | |
225 | ldout(cct, 5) << this << " " << __func__ << ": r=" << *result << dendl; | |
226 | ||
227 | if (*result == -ERESTART) { | |
228 | ldout(cct, 5) << "snapshot rollback operation interrupted" << dendl; | |
229 | return this->create_context_finisher(*result); | |
230 | } else if (*result < 0) { | |
231 | lderr(cct) << "failed to rollback objects: " << cpp_strerror(*result) | |
232 | << dendl; | |
233 | return this->create_context_finisher(*result); | |
234 | } | |
235 | ||
236 | return send_refresh_object_map(); | |
237 | } | |
238 | ||
239 | template <typename I> | |
240 | Context *SnapshotRollbackRequest<I>::send_refresh_object_map() { | |
241 | I &image_ctx = this->m_image_ctx; | |
242 | ||
243 | bool object_map_enabled; | |
244 | { | |
245 | RWLock::RLocker owner_locker(image_ctx.owner_lock); | |
246 | RWLock::RLocker snap_locker(image_ctx.snap_lock); | |
247 | object_map_enabled = (image_ctx.object_map != nullptr); | |
248 | } | |
249 | if (!object_map_enabled) { | |
250 | return send_invalidate_cache(); | |
251 | } | |
252 | ||
253 | CephContext *cct = image_ctx.cct; | |
254 | ldout(cct, 5) << this << " " << __func__ << dendl; | |
255 | ||
256 | m_object_map = image_ctx.create_object_map(CEPH_NOSNAP); | |
257 | ||
258 | Context *ctx = create_context_callback< | |
259 | SnapshotRollbackRequest<I>, | |
260 | &SnapshotRollbackRequest<I>::handle_refresh_object_map>(this); | |
261 | m_object_map->open(ctx); | |
262 | return nullptr; | |
263 | } | |
264 | ||
265 | template <typename I> | |
266 | Context *SnapshotRollbackRequest<I>::handle_refresh_object_map(int *result) { | |
267 | I &image_ctx = this->m_image_ctx; | |
268 | CephContext *cct = image_ctx.cct; | |
269 | ldout(cct, 5) << this << " " << __func__ << ": r=" << *result << dendl; | |
270 | ||
271 | assert(*result == 0); | |
272 | return send_invalidate_cache(); | |
273 | } | |
274 | ||
275 | template <typename I> | |
276 | Context *SnapshotRollbackRequest<I>::send_invalidate_cache() { | |
277 | I &image_ctx = this->m_image_ctx; | |
278 | ||
279 | apply(); | |
280 | if (image_ctx.object_cacher == NULL) { | |
281 | return this->create_context_finisher(0); | |
282 | } | |
283 | ||
284 | CephContext *cct = image_ctx.cct; | |
285 | ldout(cct, 5) << this << " " << __func__ << dendl; | |
286 | ||
287 | RWLock::RLocker owner_lock(image_ctx.owner_lock); | |
288 | Context *ctx = create_context_callback< | |
289 | SnapshotRollbackRequest<I>, | |
290 | &SnapshotRollbackRequest<I>::handle_invalidate_cache>(this); | |
291 | image_ctx.invalidate_cache(true, ctx); | |
292 | return nullptr; | |
293 | } | |
294 | ||
295 | template <typename I> | |
296 | Context *SnapshotRollbackRequest<I>::handle_invalidate_cache(int *result) { | |
297 | I &image_ctx = this->m_image_ctx; | |
298 | CephContext *cct = image_ctx.cct; | |
299 | ldout(cct, 5) << this << " " << __func__ << ": r=" << *result << dendl; | |
300 | ||
301 | if (*result < 0) { | |
302 | lderr(cct) << "failed to invalidate cache: " << cpp_strerror(*result) | |
303 | << dendl; | |
304 | } | |
305 | return this->create_context_finisher(*result); | |
306 | } | |
307 | ||
308 | template <typename I> | |
309 | void SnapshotRollbackRequest<I>::apply() { | |
310 | I &image_ctx = this->m_image_ctx; | |
311 | ||
312 | RWLock::RLocker owner_locker(image_ctx.owner_lock); | |
313 | RWLock::WLocker snap_locker(image_ctx.snap_lock); | |
314 | if (image_ctx.object_map != nullptr) { | |
315 | std::swap(m_object_map, image_ctx.object_map); | |
316 | } | |
317 | } | |
318 | ||
319 | } // namespace operation | |
320 | } // namespace librbd | |
321 | ||
322 | template class librbd::operation::SnapshotRollbackRequest<librbd::ImageCtx>; |