1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
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"
17 #include <boost/bind.hpp>
18 #include <boost/lambda/bind.hpp>
19 #include <boost/lambda/construct.hpp>
20 #include <boost/scope_exit.hpp>
22 #define dout_subsys ceph_subsys_rbd
24 #define dout_prefix *_dout << "librbd::TrimRequest: "
30 class C_CopyupObject : public C_AsyncObjectThrottle<I> {
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)
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());
45 string oid = image_ctx.get_object_name(m_object_no);
46 ldout(image_ctx.cct, 10) << "removing (with copyup) " << oid << dendl;
48 auto req = io::ObjectRequest<I>::create_trim(&image_ctx, oid, m_object_no,
49 m_snapc, false, this);
54 ::SnapContext m_snapc;
59 class C_RemoveObject : public C_AsyncObjectThrottle<I> {
61 C_RemoveObject(AsyncObjectThrottle<I> &throttle, I *image_ctx,
63 : C_AsyncObjectThrottle<I>(throttle, *image_ctx), m_object_no(object_no)
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());
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)) {
81 string oid = image_ctx.get_object_name(m_object_no);
82 ldout(image_ctx.cct, 10) << "removing " << oid << dendl;
84 librados::AioCompletion *rados_completion =
85 util::create_rados_callback(this);
86 int r = image_ctx.data_ctx.aio_remove(oid, rados_completion);
88 rados_completion->release();
97 TrimRequest<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),
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_delete_start_min = m_delete_start;
109 m_num_objects = Striper::get_num_objects(image_ctx.layout, original_size);
111 CephContext *cct = image_ctx.cct;
112 ldout(cct, 10) << this << " trim image " << original_size << " -> "
113 << m_new_size << " periods " << new_num_periods
114 << " discard to offset " << m_delete_off
115 << " delete objects " << m_delete_start
116 << " to " << m_num_objects << dendl;
119 template <typename I>
120 bool TrimRequest<I>::should_complete(int r)
122 I &image_ctx = this->m_image_ctx;
123 CephContext *cct = image_ctx.cct;
124 ldout(cct, 5) << this << " should_complete: r=" << r << dendl;
125 if (r == -ERESTART) {
126 ldout(cct, 5) << "trim operation interrupted" << dendl;
129 lderr(cct) << "trim encountered an error: " << cpp_strerror(r) << dendl;
133 RWLock::RLocker owner_lock(image_ctx.owner_lock);
136 ldout(cct, 5) << " PRE_TRIM" << dendl;
137 send_copyup_objects();
140 case STATE_COPYUP_OBJECTS:
141 ldout(cct, 5) << " COPYUP_OBJECTS" << dendl;
142 send_remove_objects();
145 case STATE_REMOVE_OBJECTS:
146 ldout(cct, 5) << " REMOVE_OBJECTS" << dendl;
150 case STATE_POST_TRIM:
151 ldout(cct, 5) << " POST_TRIM" << dendl;
152 send_clean_boundary();
155 case STATE_CLEAN_BOUNDARY:
156 ldout(cct, 5) << "CLEAN_BOUNDARY" << dendl;
161 ldout(cct, 5) << "FINISHED" << dendl;
165 lderr(cct) << "invalid state: " << m_state << dendl;
172 template <typename I>
173 void TrimRequest<I>::send() {
178 void TrimRequest<I>::send_pre_trim() {
179 I &image_ctx = this->m_image_ctx;
180 assert(image_ctx.owner_lock.is_locked());
182 if (m_delete_start >= m_num_objects) {
183 send_clean_boundary();
188 RWLock::RLocker snap_locker(image_ctx.snap_lock);
189 if (image_ctx.object_map != nullptr) {
190 ldout(image_ctx.cct, 5) << this << " send_pre_trim: "
191 << " delete_start_min=" << m_delete_start_min
192 << " num_objects=" << m_num_objects << dendl;
193 m_state = STATE_PRE_TRIM;
195 assert(image_ctx.exclusive_lock->is_lock_owner());
197 RWLock::WLocker object_map_locker(image_ctx.object_map_lock);
198 if (image_ctx.object_map->template aio_update<AsyncRequest<I> >(
199 CEPH_NOSNAP, m_delete_start_min, m_num_objects, OBJECT_PENDING,
200 OBJECT_EXISTS, {}, this)) {
206 send_copyup_objects();
210 void TrimRequest<I>::send_copyup_objects() {
211 I &image_ctx = this->m_image_ctx;
212 assert(image_ctx.owner_lock.is_locked());
216 uint64_t parent_overlap;
218 RWLock::RLocker snap_locker(image_ctx.snap_lock);
219 RWLock::RLocker parent_locker(image_ctx.parent_lock);
221 snapc = image_ctx.snapc;
222 has_snapshots = !image_ctx.snaps.empty();
223 int r = image_ctx.get_parent_overlap(CEPH_NOSNAP, &parent_overlap);
227 // copyup is only required for portion of image that overlaps parent
228 uint64_t copyup_end = Striper::get_num_objects(image_ctx.layout,
231 // TODO: protect against concurrent shrink and snap create?
232 // skip to remove if no copyup is required.
233 if (copyup_end <= m_delete_start || !has_snapshots) {
234 send_remove_objects();
238 uint64_t copyup_start = m_delete_start;
239 m_delete_start = copyup_end;
241 ldout(image_ctx.cct, 5) << this << " send_copyup_objects: "
242 << " start object=" << copyup_start << ", "
243 << " end object=" << copyup_end << dendl;
244 m_state = STATE_COPYUP_OBJECTS;
246 Context *ctx = this->create_callback_context();
247 typename AsyncObjectThrottle<I>::ContextFactory context_factory(
248 boost::lambda::bind(boost::lambda::new_ptr<C_CopyupObject<I> >(),
249 boost::lambda::_1, &image_ctx, snapc, boost::lambda::_2));
250 AsyncObjectThrottle<I> *throttle = new AsyncObjectThrottle<I>(
251 this, image_ctx, context_factory, ctx, &m_prog_ctx, copyup_start,
253 throttle->start_ops(image_ctx.concurrent_management_ops);
256 template <typename I>
257 void TrimRequest<I>::send_remove_objects() {
258 I &image_ctx = this->m_image_ctx;
259 assert(image_ctx.owner_lock.is_locked());
261 ldout(image_ctx.cct, 5) << this << " send_remove_objects: "
262 << " delete_start=" << m_delete_start
263 << " num_objects=" << m_num_objects << dendl;
264 m_state = STATE_REMOVE_OBJECTS;
266 Context *ctx = this->create_callback_context();
267 typename AsyncObjectThrottle<I>::ContextFactory context_factory(
268 boost::lambda::bind(boost::lambda::new_ptr<C_RemoveObject<I> >(),
269 boost::lambda::_1, &image_ctx, boost::lambda::_2));
270 AsyncObjectThrottle<I> *throttle = new AsyncObjectThrottle<I>(
271 this, image_ctx, context_factory, ctx, &m_prog_ctx, m_delete_start,
273 throttle->start_ops(image_ctx.concurrent_management_ops);
277 void TrimRequest<I>::send_post_trim() {
278 I &image_ctx = this->m_image_ctx;
279 assert(image_ctx.owner_lock.is_locked());
282 RWLock::RLocker snap_locker(image_ctx.snap_lock);
283 if (image_ctx.object_map != nullptr) {
284 ldout(image_ctx.cct, 5) << this << " send_post_trim:"
285 << " delete_start_min=" << m_delete_start_min
286 << " num_objects=" << m_num_objects << dendl;
287 m_state = STATE_POST_TRIM;
289 assert(image_ctx.exclusive_lock->is_lock_owner());
291 RWLock::WLocker object_map_locker(image_ctx.object_map_lock);
292 if (image_ctx.object_map->template aio_update<AsyncRequest<I> >(
293 CEPH_NOSNAP, m_delete_start_min, m_num_objects, OBJECT_NONEXISTENT,
294 OBJECT_PENDING, {}, this)) {
300 send_clean_boundary();
303 template <typename I>
304 void TrimRequest<I>::send_clean_boundary() {
305 I &image_ctx = this->m_image_ctx;
306 assert(image_ctx.owner_lock.is_locked());
307 CephContext *cct = image_ctx.cct;
308 if (m_delete_off <= m_new_size) {
313 // should have been canceled prior to releasing lock
314 assert(image_ctx.exclusive_lock == nullptr ||
315 image_ctx.exclusive_lock->is_lock_owner());
316 uint64_t delete_len = m_delete_off - m_new_size;
317 ldout(image_ctx.cct, 5) << this << " send_clean_boundary: "
318 << " delete_off=" << m_delete_off
319 << " length=" << delete_len << dendl;
320 m_state = STATE_CLEAN_BOUNDARY;
324 RWLock::RLocker snap_locker(image_ctx.snap_lock);
325 snapc = image_ctx.snapc;
328 // discard the weird boundary
329 std::vector<ObjectExtent> extents;
330 Striper::file_to_extents(cct, image_ctx.format_string,
331 &image_ctx.layout, m_new_size, delete_len, 0,
334 ContextCompletion *completion =
335 new ContextCompletion(this->create_async_callback_context(), true);
336 for (vector<ObjectExtent>::iterator p = extents.begin();
337 p != extents.end(); ++p) {
338 ldout(cct, 20) << " ex " << *p << dendl;
339 Context *req_comp = new C_ContextCompletion(*completion);
341 io::ObjectRequest<I> *req;
342 if (p->offset == 0) {
343 req = io::ObjectRequest<I>::create_trim(&image_ctx, p->oid.name,
344 p->objectno, snapc, true,
347 req = io::ObjectRequest<I>::create_truncate(&image_ctx, p->oid.name,
348 p->objectno, p->offset, snapc,
353 completion->finish_adding_requests();
356 template <typename I>
357 void TrimRequest<I>::send_finish(int r) {
358 m_state = STATE_FINISHED;
359 this->async_complete(r);
362 } // namespace operation
363 } // namespace librbd
365 template class librbd::operation::TrimRequest<librbd::ImageCtx>;