]> git.proxmox.com Git - ceph.git/blame - ceph/src/librbd/operation/TrimRequest.cc
update sources to v12.1.0
[ceph.git] / ceph / src / librbd / operation / TrimRequest.cc
CommitLineData
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/TrimRequest.h"
5#include "librbd/AsyncObjectThrottle.h"
6#include "librbd/ExclusiveLock.h"
7#include "librbd/ImageCtx.h"
8#include "librbd/internal.h"
9#include "librbd/ObjectMap.h"
10#include "librbd/Utils.h"
11#include "librbd/io/ObjectRequest.h"
12#include "common/ContextCompletion.h"
13#include "common/dout.h"
14#include "common/errno.h"
15#include "osdc/Striper.h"
16
17#include <boost/bind.hpp>
18#include <boost/lambda/bind.hpp>
19#include <boost/lambda/construct.hpp>
20#include <boost/scope_exit.hpp>
21
22#define dout_subsys ceph_subsys_rbd
23#undef dout_prefix
24#define dout_prefix *_dout << "librbd::TrimRequest: "
25
26namespace librbd {
27namespace operation {
28
29template <typename I>
30class C_CopyupObject : public C_AsyncObjectThrottle<I> {
31public:
32 C_CopyupObject(AsyncObjectThrottle<I> &throttle, I *image_ctx,
33 ::SnapContext snapc, uint64_t object_no)
34 : C_AsyncObjectThrottle<I>(throttle, *image_ctx), m_snapc(snapc),
35 m_object_no(object_no)
36 {
37 }
38
39 int send() override {
40 I &image_ctx = this->m_image_ctx;
41 assert(image_ctx.owner_lock.is_locked());
42 assert(image_ctx.exclusive_lock == nullptr ||
43 image_ctx.exclusive_lock->is_lock_owner());
44
45 string oid = image_ctx.get_object_name(m_object_no);
46 ldout(image_ctx.cct, 10) << "removing (with copyup) " << oid << dendl;
47
48 auto req = new io::ObjectTrimRequest(&image_ctx, oid, m_object_no,
31f18b77 49 m_snapc, false, this);
7c673cae
FG
50 req->send();
51 return 0;
52 }
53private:
54 ::SnapContext m_snapc;
55 uint64_t m_object_no;
56};
57
58template <typename I>
59class C_RemoveObject : public C_AsyncObjectThrottle<I> {
60public:
61 C_RemoveObject(AsyncObjectThrottle<I> &throttle, ImageCtx *image_ctx,
62 uint64_t object_no)
63 : C_AsyncObjectThrottle<I>(throttle, *image_ctx), m_object_no(object_no)
64 {
65 }
66
67 int send() override {
68 I &image_ctx = this->m_image_ctx;
69 assert(image_ctx.owner_lock.is_locked());
70 assert(image_ctx.exclusive_lock == nullptr ||
71 image_ctx.exclusive_lock->is_lock_owner());
72
73 {
74 RWLock::RLocker snap_locker(image_ctx.snap_lock);
75 if (image_ctx.object_map != nullptr &&
76 !image_ctx.object_map->object_may_exist(m_object_no)) {
77 return 1;
78 }
79 }
80
81 string oid = image_ctx.get_object_name(m_object_no);
82 ldout(image_ctx.cct, 10) << "removing " << oid << dendl;
83
84 librados::AioCompletion *rados_completion =
85 util::create_rados_callback(this);
86 int r = image_ctx.data_ctx.aio_remove(oid, rados_completion);
87 assert(r == 0);
88 rados_completion->release();
89 return 0;
90 }
91
92private:
93 uint64_t m_object_no;
94};
95
96template <typename I>
97TrimRequest<I>::TrimRequest(I &image_ctx, Context *on_finish,
98 uint64_t original_size, uint64_t new_size,
99 ProgressContext &prog_ctx)
100 : AsyncRequest<I>(image_ctx, on_finish), m_new_size(new_size),
101 m_prog_ctx(prog_ctx)
102{
103 uint64_t period = image_ctx.get_stripe_period();
104 uint64_t new_num_periods = ((m_new_size + period - 1) / period);
105 m_delete_off = MIN(new_num_periods * period, original_size);
106 // first object we can delete free and clear
107 m_delete_start = new_num_periods * image_ctx.get_stripe_count();
108 m_num_objects = Striper::get_num_objects(image_ctx.layout, original_size);
109
110 CephContext *cct = image_ctx.cct;
111 ldout(cct, 10) << this << " trim image " << original_size << " -> "
112 << m_new_size << " periods " << new_num_periods
113 << " discard to offset " << m_delete_off
114 << " delete objects " << m_delete_start
115 << " to " << m_num_objects << dendl;
116}
117
118template <typename I>
119bool TrimRequest<I>::should_complete(int r)
120{
121 I &image_ctx = this->m_image_ctx;
122 CephContext *cct = image_ctx.cct;
123 ldout(cct, 5) << this << " should_complete: r=" << r << dendl;
124 if (r == -ERESTART) {
125 ldout(cct, 5) << "trim operation interrupted" << dendl;
126 return true;
127 } else if (r < 0) {
128 lderr(cct) << "trim encountered an error: " << cpp_strerror(r) << dendl;
129 return true;
130 }
131
132 RWLock::RLocker owner_lock(image_ctx.owner_lock);
133 switch (m_state) {
134 case STATE_PRE_COPYUP:
135 ldout(cct, 5) << " PRE_COPYUP" << dendl;
136 send_copyup_objects();
137 break;
138
139 case STATE_COPYUP_OBJECTS:
140 ldout(cct, 5) << " COPYUP_OBJECTS" << dendl;
141 send_post_copyup();
142 break;
143
144 case STATE_POST_COPYUP:
145 ldout(cct, 5) << " POST_COPYUP" << dendl;
146 send_pre_remove();
147 break;
148
149 case STATE_PRE_REMOVE:
150 ldout(cct, 5) << " PRE_REMOVE" << dendl;
151 send_remove_objects();
152 break;
153
154 case STATE_REMOVE_OBJECTS:
155 ldout(cct, 5) << " REMOVE_OBJECTS" << dendl;
156 send_post_remove();
157 break;
158
159 case STATE_POST_REMOVE:
160 ldout(cct, 5) << " POST_OBJECTS" << dendl;
161 send_clean_boundary();
162 break;
163
164 case STATE_CLEAN_BOUNDARY:
165 ldout(cct, 5) << "CLEAN_BOUNDARY" << dendl;
166 send_finish(0);
167 break;
168
169 case STATE_FINISHED:
170 ldout(cct, 5) << "FINISHED" << dendl;
171 return true;
172
173 default:
174 lderr(cct) << "invalid state: " << m_state << dendl;
175 assert(false);
176 break;
177 }
178 return false;
179}
180
181template <typename I>
182void TrimRequest<I>::send() {
183 send_pre_copyup();
184}
185
186template<typename I>
187void TrimRequest<I>::send_copyup_objects() {
188 I &image_ctx = this->m_image_ctx;
189 assert(image_ctx.owner_lock.is_locked());
190
191 ldout(image_ctx.cct, 5) << this << " send_copyup_objects: "
192 << " start object=" << m_copyup_start << ", "
193 << " end object=" << m_copyup_end << dendl;
194 m_state = STATE_COPYUP_OBJECTS;
195
196 ::SnapContext snapc;
197 {
198 RWLock::RLocker snap_locker(image_ctx.snap_lock);
199 RWLock::RLocker parent_locker(image_ctx.parent_lock);
200 snapc = image_ctx.snapc;
201 }
202
203 Context *ctx = this->create_callback_context();
204 typename AsyncObjectThrottle<I>::ContextFactory context_factory(
205 boost::lambda::bind(boost::lambda::new_ptr<C_CopyupObject<I> >(),
206 boost::lambda::_1, &image_ctx, snapc, boost::lambda::_2));
207 AsyncObjectThrottle<I> *throttle = new AsyncObjectThrottle<I>(
208 this, image_ctx, context_factory, ctx, &m_prog_ctx, m_copyup_start,
209 m_copyup_end);
210 throttle->start_ops(image_ctx.concurrent_management_ops);
211}
212
213template <typename I>
214void TrimRequest<I>::send_remove_objects() {
215 I &image_ctx = this->m_image_ctx;
216 assert(image_ctx.owner_lock.is_locked());
217
218 ldout(image_ctx.cct, 5) << this << " send_remove_objects: "
219 << " delete_start=" << m_delete_start
220 << " num_objects=" << m_num_objects << dendl;
221 m_state = STATE_REMOVE_OBJECTS;
222
223 Context *ctx = this->create_callback_context();
224 typename AsyncObjectThrottle<I>::ContextFactory context_factory(
225 boost::lambda::bind(boost::lambda::new_ptr<C_RemoveObject<I> >(),
226 boost::lambda::_1, &image_ctx, boost::lambda::_2));
227 AsyncObjectThrottle<I> *throttle = new AsyncObjectThrottle<I>(
228 this, image_ctx, context_factory, ctx, &m_prog_ctx, m_delete_start,
229 m_num_objects);
230 throttle->start_ops(image_ctx.concurrent_management_ops);
231}
232
233template<typename I>
234void TrimRequest<I>::send_pre_copyup() {
235 I &image_ctx = this->m_image_ctx;
236 assert(image_ctx.owner_lock.is_locked());
237
238 if (m_delete_start >= m_num_objects) {
239 send_clean_boundary();
240 return;
241 }
242
243 bool has_snapshots;
244 uint64_t parent_overlap;
245 {
246 RWLock::RLocker snap_locker(image_ctx.snap_lock);
247 RWLock::RLocker parent_locker(image_ctx.parent_lock);
248
249 has_snapshots = !image_ctx.snaps.empty();
250 int r = image_ctx.get_parent_overlap(CEPH_NOSNAP, &parent_overlap);
251 assert(r == 0);
252 }
253
254 // copyup is only required for portion of image that overlaps parent
255 m_copyup_end = Striper::get_num_objects(image_ctx.layout, parent_overlap);
256
257 // TODO: protect against concurrent shrink and snap create?
258 // skip to remove if no copyup is required.
259 if (m_copyup_end <= m_delete_start || !has_snapshots) {
260 send_pre_remove();
261 return;
262 }
263
264 m_copyup_start = m_delete_start;
265 m_delete_start = m_copyup_end;
266
267 {
268 RWLock::RLocker snap_locker(image_ctx.snap_lock);
269 if (image_ctx.object_map != nullptr) {
270 ldout(image_ctx.cct, 5) << this << " send_pre_copyup: "
271 << " copyup_start=" << m_copyup_start
272 << " copyup_end=" << m_copyup_end << dendl;
273 m_state = STATE_PRE_COPYUP;
274
275 assert(image_ctx.exclusive_lock->is_lock_owner());
276
277 RWLock::WLocker object_map_locker(image_ctx.object_map_lock);
278 if (image_ctx.object_map->template aio_update<AsyncRequest<I> >(
279 CEPH_NOSNAP, m_copyup_start, m_copyup_end, OBJECT_PENDING,
31f18b77 280 OBJECT_EXISTS, {}, this)) {
7c673cae
FG
281 return;
282 }
283 }
284 }
285
286 send_copyup_objects();
287}
288
289template <typename I>
290void TrimRequest<I>::send_pre_remove() {
291 I &image_ctx = this->m_image_ctx;
292 assert(image_ctx.owner_lock.is_locked());
293 if (m_delete_start >= m_num_objects) {
294 send_clean_boundary();
295 return;
296 }
297
298 {
299 RWLock::RLocker snap_locker(image_ctx.snap_lock);
300 if (image_ctx.object_map != nullptr) {
301 ldout(image_ctx.cct, 5) << this << " send_pre_remove: "
302 << " delete_start=" << m_delete_start
303 << " num_objects=" << m_num_objects << dendl;
304 m_state = STATE_PRE_REMOVE;
305
306 assert(image_ctx.exclusive_lock->is_lock_owner());
307
308 // flag the objects as pending deletion
309 RWLock::WLocker object_map_locker(image_ctx.object_map_lock);
310 if (image_ctx.object_map->template aio_update<AsyncRequest<I> >(
311 CEPH_NOSNAP, m_delete_start, m_num_objects, OBJECT_PENDING,
31f18b77 312 OBJECT_EXISTS, {}, this)) {
7c673cae
FG
313 return;
314 }
315 }
316 }
317
318 // no object map update required
319 send_remove_objects();
320}
321
322template<typename I>
323void TrimRequest<I>::send_post_copyup() {
324 I &image_ctx = this->m_image_ctx;
325 assert(image_ctx.owner_lock.is_locked());
326
327 {
328 RWLock::RLocker snap_locker(image_ctx.snap_lock);
329 if (image_ctx.object_map != nullptr) {
330 ldout(image_ctx.cct, 5) << this << " send_post_copyup:"
331 << " copyup_start=" << m_copyup_start
332 << " copyup_end=" << m_copyup_end << dendl;
333 m_state = STATE_POST_COPYUP;
334
335 assert(image_ctx.exclusive_lock->is_lock_owner());
336
337 RWLock::WLocker object_map_locker(image_ctx.object_map_lock);
338 if (image_ctx.object_map->template aio_update<AsyncRequest<I> >(
339 CEPH_NOSNAP, m_copyup_start, m_copyup_end, OBJECT_NONEXISTENT,
31f18b77 340 OBJECT_PENDING, {}, this)) {
7c673cae
FG
341 return;
342 }
343 }
344 }
345
346 send_pre_remove();
347}
348
349template <typename I>
350void TrimRequest<I>::send_post_remove() {
351 I &image_ctx = this->m_image_ctx;
352 assert(image_ctx.owner_lock.is_locked());
353
354 {
355 RWLock::RLocker snap_locker(image_ctx.snap_lock);
356 if (image_ctx.object_map != nullptr) {
357 ldout(image_ctx.cct, 5) << this << " send_post_remove: "
358 << " delete_start=" << m_delete_start
359 << " num_objects=" << m_num_objects << dendl;
360 m_state = STATE_POST_REMOVE;
361
362 assert(image_ctx.exclusive_lock->is_lock_owner());
363
364 // flag the pending objects as removed
365 RWLock::WLocker object_map_locker(image_ctx.object_map_lock);
366 if (image_ctx.object_map->template aio_update<AsyncRequest<I> >(
367 CEPH_NOSNAP, m_delete_start, m_num_objects, OBJECT_NONEXISTENT,
31f18b77 368 OBJECT_PENDING, {}, this)) {
7c673cae
FG
369 return;
370 }
371 }
372 }
373
374 // no object map update required
375 send_clean_boundary();
376}
377
378template <typename I>
379void TrimRequest<I>::send_clean_boundary() {
380 I &image_ctx = this->m_image_ctx;
381 assert(image_ctx.owner_lock.is_locked());
382 CephContext *cct = image_ctx.cct;
383 if (m_delete_off <= m_new_size) {
384 send_finish(0);
385 return;
386 }
387
388 // should have been canceled prior to releasing lock
389 assert(image_ctx.exclusive_lock == nullptr ||
390 image_ctx.exclusive_lock->is_lock_owner());
391 uint64_t delete_len = m_delete_off - m_new_size;
392 ldout(image_ctx.cct, 5) << this << " send_clean_boundary: "
393 << " delete_off=" << m_delete_off
394 << " length=" << delete_len << dendl;
395 m_state = STATE_CLEAN_BOUNDARY;
396
397 ::SnapContext snapc;
398 {
399 RWLock::RLocker snap_locker(image_ctx.snap_lock);
400 snapc = image_ctx.snapc;
401 }
402
403 // discard the weird boundary
404 std::vector<ObjectExtent> extents;
405 Striper::file_to_extents(cct, image_ctx.format_string,
406 &image_ctx.layout, m_new_size, delete_len, 0,
407 extents);
408
409 ContextCompletion *completion =
410 new ContextCompletion(this->create_async_callback_context(), true);
411 for (vector<ObjectExtent>::iterator p = extents.begin();
412 p != extents.end(); ++p) {
413 ldout(cct, 20) << " ex " << *p << dendl;
414 Context *req_comp = new C_ContextCompletion(*completion);
415
416 io::ObjectRequest<> *req;
417 if (p->offset == 0) {
418 req = new io::ObjectTrimRequest(&image_ctx, p->oid.name, p->objectno,
31f18b77 419 snapc, true, req_comp);
7c673cae
FG
420 } else {
421 req = new io::ObjectTruncateRequest(&image_ctx, p->oid.name, p->objectno,
31f18b77 422 p->offset, snapc, {}, req_comp);
7c673cae
FG
423 }
424 req->send();
425 }
426 completion->finish_adding_requests();
427}
428
429template <typename I>
430void TrimRequest<I>::send_finish(int r) {
431 m_state = STATE_FINISHED;
432 this->async_complete(r);
433}
434
435} // namespace operation
436} // namespace librbd
437
438template class librbd::operation::TrimRequest<librbd::ImageCtx>;