Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / librbd / operation / TrimRequest.cc
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
26 namespace librbd {
27 namespace operation {
28
29 template <typename I>
30 class C_CopyupObject : public C_AsyncObjectThrottle<I> {
31 public:
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 = io::ObjectRequest<I>::create_trim(&image_ctx, oid, m_object_no,
49                                                  m_snapc, false, this);
50     req->send();
51     return 0;
52   }
53 private:
54   ::SnapContext m_snapc;
55   uint64_t m_object_no;
56 };
57
58 template <typename I>
59 class C_RemoveObject : public C_AsyncObjectThrottle<I> {
60 public:
61   C_RemoveObject(AsyncObjectThrottle<I> &throttle, I *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
92 private:
93   uint64_t m_object_no;
94 };
95
96 template <typename I>
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),
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_delete_start_min = m_delete_start;
109   m_num_objects = Striper::get_num_objects(image_ctx.layout, original_size);
110
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;
117 }
118
119 template <typename I>
120 bool TrimRequest<I>::should_complete(int r)
121 {
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;
127     return true;
128   } else if (r < 0) {
129     lderr(cct) << "trim encountered an error: " << cpp_strerror(r) << dendl;
130     return true;
131   }
132
133   RWLock::RLocker owner_lock(image_ctx.owner_lock);
134   switch (m_state) {
135   case STATE_PRE_TRIM:
136     ldout(cct, 5) << " PRE_TRIM" << dendl;
137     send_copyup_objects();
138     break;
139
140   case STATE_COPYUP_OBJECTS:
141     ldout(cct, 5) << " COPYUP_OBJECTS" << dendl;
142     send_remove_objects();
143     break;
144
145   case STATE_REMOVE_OBJECTS:
146     ldout(cct, 5) << " REMOVE_OBJECTS" << dendl;
147     send_post_trim();
148     break;
149
150   case STATE_POST_TRIM:
151     ldout(cct, 5) << " POST_TRIM" << dendl;
152     send_clean_boundary();
153     break;
154
155   case STATE_CLEAN_BOUNDARY:
156     ldout(cct, 5) << "CLEAN_BOUNDARY" << dendl;
157     send_finish(0);
158     break;
159
160   case STATE_FINISHED:
161     ldout(cct, 5) << "FINISHED" << dendl;
162     return true;
163
164   default:
165     lderr(cct) << "invalid state: " << m_state << dendl;
166     assert(false);
167     break;
168   }
169   return false;
170 }
171
172 template <typename I>
173 void TrimRequest<I>::send() {
174   send_pre_trim();
175 }
176
177 template<typename I>
178 void TrimRequest<I>::send_pre_trim() {
179   I &image_ctx = this->m_image_ctx;
180   assert(image_ctx.owner_lock.is_locked());
181
182   if (m_delete_start >= m_num_objects) {
183     send_clean_boundary();
184     return;
185   }
186
187   {
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;
194
195       assert(image_ctx.exclusive_lock->is_lock_owner());
196
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)) {
201         return;
202       }
203     }
204   }
205
206   send_copyup_objects();
207 }
208
209 template<typename I>
210 void TrimRequest<I>::send_copyup_objects() {
211   I &image_ctx = this->m_image_ctx;
212   assert(image_ctx.owner_lock.is_locked());
213
214   ::SnapContext snapc;
215   bool has_snapshots;
216   uint64_t parent_overlap;
217   {
218     RWLock::RLocker snap_locker(image_ctx.snap_lock);
219     RWLock::RLocker parent_locker(image_ctx.parent_lock);
220
221     snapc = image_ctx.snapc;
222     has_snapshots = !image_ctx.snaps.empty();
223     int r = image_ctx.get_parent_overlap(CEPH_NOSNAP, &parent_overlap);
224     assert(r == 0);
225   }
226
227   // copyup is only required for portion of image that overlaps parent
228   uint64_t copyup_end = Striper::get_num_objects(image_ctx.layout,
229                                                  parent_overlap);
230
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();
235     return;
236   }
237
238   uint64_t copyup_start = m_delete_start;
239   m_delete_start = copyup_end;
240
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;
245
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,
252     copyup_end);
253   throttle->start_ops(image_ctx.concurrent_management_ops);
254 }
255
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());
260
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;
265
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,
272     m_num_objects);
273   throttle->start_ops(image_ctx.concurrent_management_ops);
274 }
275
276 template<typename I>
277 void TrimRequest<I>::send_post_trim() {
278   I &image_ctx = this->m_image_ctx;
279   assert(image_ctx.owner_lock.is_locked());
280
281   {
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;
288
289       assert(image_ctx.exclusive_lock->is_lock_owner());
290
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)) {
295         return;
296       }
297     }
298   }
299
300   send_clean_boundary();
301 }
302
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) {
309     send_finish(0);
310     return;
311   }
312
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;
321
322   ::SnapContext snapc;
323   {
324     RWLock::RLocker snap_locker(image_ctx.snap_lock);
325     snapc = image_ctx.snapc;
326   }
327
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,
332                            extents);
333
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);
340
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,
345                                               req_comp);
346     } else {
347       req = io::ObjectRequest<I>::create_truncate(&image_ctx, p->oid.name,
348                                                   p->objectno, p->offset, snapc,
349                                                   {}, req_comp);
350     }
351     req->send();
352   }
353   completion->finish_adding_requests();
354 }
355
356 template <typename I>
357 void TrimRequest<I>::send_finish(int r) {
358   m_state = STATE_FINISHED;
359   this->async_complete(r);
360 }
361
362 } // namespace operation
363 } // namespace librbd
364
365 template class librbd::operation::TrimRequest<librbd::ImageCtx>;