Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / librbd / operation / RebuildObjectMapRequest.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/RebuildObjectMapRequest.h"
5 #include "common/dout.h"
6 #include "common/errno.h"
7 #include "librbd/AsyncObjectThrottle.h"
8 #include "librbd/ExclusiveLock.h"
9 #include "librbd/ImageCtx.h"
10 #include "librbd/internal.h"
11 #include "librbd/ObjectMap.h"
12 #include "librbd/operation/ResizeRequest.h"
13 #include "librbd/operation/TrimRequest.h"
14 #include "librbd/operation/ObjectMapIterate.h"
15 #include "librbd/Utils.h"
16 #include <boost/lambda/bind.hpp>
17 #include <boost/lambda/construct.hpp>
18
19 #define dout_subsys ceph_subsys_rbd
20 #undef dout_prefix
21 #define dout_prefix *_dout << "librbd::RebuildObjectMapRequest: "
22
23 namespace librbd {
24 namespace operation {
25
26 template <typename I>
27 void RebuildObjectMapRequest<I>::send() {
28   send_resize_object_map();
29 }
30
31 template <typename I>
32 bool RebuildObjectMapRequest<I>::should_complete(int r) {
33   CephContext *cct = m_image_ctx.cct;
34   ldout(cct, 5) << this << " should_complete: " << " r=" << r << dendl;
35
36   RWLock::RLocker owner_lock(m_image_ctx.owner_lock);
37   switch (m_state) {
38   case STATE_RESIZE_OBJECT_MAP:
39     ldout(cct, 5) << "RESIZE_OBJECT_MAP" << dendl;
40     if (r == -ESTALE && !m_attempted_trim) {
41       // objects are still flagged as in-use -- delete them
42       m_attempted_trim = true;
43       send_trim_image();
44       return false;
45     } else if (r == 0) {
46       send_verify_objects();
47     }
48     break;
49
50   case STATE_TRIM_IMAGE:
51     ldout(cct, 5) << "TRIM_IMAGE" << dendl;
52     if (r == 0) {
53       send_resize_object_map();
54     }
55     break;
56
57   case STATE_VERIFY_OBJECTS:
58     ldout(cct, 5) << "VERIFY_OBJECTS" << dendl;
59     if (r == 0) {
60       send_save_object_map();
61     }
62     break;
63
64   case STATE_SAVE_OBJECT_MAP:
65     ldout(cct, 5) << "SAVE_OBJECT_MAP" << dendl;
66     if (r == 0) {
67       send_update_header();
68     }
69     break;
70   case STATE_UPDATE_HEADER:
71     ldout(cct, 5) << "UPDATE_HEADER" << dendl;
72     if (r == 0) {
73       return true;
74     }
75     break;
76
77   default:
78     assert(false);
79     break;
80   }
81
82   if (r == -ERESTART) {
83     ldout(cct, 5) << "rebuild object map operation interrupted" << dendl;
84     return true;
85   } else if (r < 0) {
86     lderr(cct) << "rebuild object map encountered an error: " << cpp_strerror(r)
87                << dendl;
88     return true;
89   }
90   return false;
91 }
92
93 template <typename I>
94 void RebuildObjectMapRequest<I>::send_resize_object_map() {
95   assert(m_image_ctx.owner_lock.is_locked());
96   CephContext *cct = m_image_ctx.cct;
97
98   m_image_ctx.snap_lock.get_read();
99   assert(m_image_ctx.object_map != nullptr);
100
101   uint64_t size = get_image_size();
102   uint64_t num_objects = Striper::get_num_objects(m_image_ctx.layout, size);
103
104   if (m_image_ctx.object_map->size() == num_objects) {
105     m_image_ctx.snap_lock.put_read();
106     send_verify_objects();
107     return;
108   }
109
110   ldout(cct, 5) << this << " send_resize_object_map" << dendl;
111   m_state = STATE_RESIZE_OBJECT_MAP;
112
113   // should have been canceled prior to releasing lock
114   assert(m_image_ctx.exclusive_lock == nullptr ||
115          m_image_ctx.exclusive_lock->is_lock_owner());
116
117   m_image_ctx.object_map->aio_resize(size, OBJECT_NONEXISTENT,
118                                      this->create_callback_context());
119   m_image_ctx.snap_lock.put_read();
120 }
121
122 template <typename I>
123 void RebuildObjectMapRequest<I>::send_trim_image() {
124   CephContext *cct = m_image_ctx.cct;
125
126   RWLock::RLocker l(m_image_ctx.owner_lock);
127
128   // should have been canceled prior to releasing lock
129   assert(m_image_ctx.exclusive_lock == nullptr ||
130          m_image_ctx.exclusive_lock->is_lock_owner());
131   ldout(cct, 5) << this << " send_trim_image" << dendl;
132   m_state = STATE_TRIM_IMAGE;
133
134   uint64_t new_size;
135   uint64_t orig_size;
136   {
137     RWLock::RLocker l(m_image_ctx.snap_lock);
138     assert(m_image_ctx.object_map != nullptr);
139
140     new_size = get_image_size();
141     orig_size = m_image_ctx.get_object_size() *
142                 m_image_ctx.object_map->size();
143   }
144   TrimRequest<I> *req = TrimRequest<I>::create(m_image_ctx,
145                                                this->create_callback_context(),
146                                                orig_size, new_size, m_prog_ctx);
147   req->send();
148 }
149
150 template <typename I>
151 bool update_object_map(I& image_ctx, uint64_t object_no, uint8_t current_state,
152                       uint8_t new_state) {
153   CephContext *cct = image_ctx.cct;
154   uint64_t snap_id = image_ctx.snap_id;
155
156   uint8_t state = (*image_ctx.object_map)[object_no];
157   if (state == OBJECT_EXISTS && new_state == OBJECT_NONEXISTENT &&
158       snap_id == CEPH_NOSNAP) {
159     // might be writing object to OSD concurrently
160     new_state = state;
161   }
162
163   if (new_state != state) {
164     ldout(cct, 15) << image_ctx.get_object_name(object_no)
165       << " rebuild updating object map "
166       << static_cast<uint32_t>(state) << "->"
167       << static_cast<uint32_t>(new_state) << dendl;
168     (*image_ctx.object_map)[object_no] = new_state;
169   }
170   return false;
171 }
172
173 template <typename I>
174 void RebuildObjectMapRequest<I>::send_verify_objects() {
175   assert(m_image_ctx.owner_lock.is_locked());
176   CephContext *cct = m_image_ctx.cct;
177
178   m_state = STATE_VERIFY_OBJECTS;
179   ldout(cct, 5) << this << " send_verify_objects" << dendl;
180
181   ObjectMapIterateRequest<I> *req =
182     new ObjectMapIterateRequest<I>(m_image_ctx,
183                                    this->create_callback_context(),
184                                    m_prog_ctx, update_object_map);
185
186   req->send();
187 }
188
189 template <typename I>
190 void RebuildObjectMapRequest<I>::send_save_object_map() {
191   assert(m_image_ctx.owner_lock.is_locked());
192   CephContext *cct = m_image_ctx.cct;
193
194   ldout(cct, 5) << this << " send_save_object_map" << dendl;
195   m_state = STATE_SAVE_OBJECT_MAP;
196
197   // should have been canceled prior to releasing lock
198   assert(m_image_ctx.exclusive_lock == nullptr ||
199          m_image_ctx.exclusive_lock->is_lock_owner());
200
201   RWLock::RLocker snap_locker(m_image_ctx.snap_lock);
202   assert(m_image_ctx.object_map != nullptr);
203   m_image_ctx.object_map->aio_save(this->create_callback_context());
204 }
205
206 template <typename I>
207 void RebuildObjectMapRequest<I>::send_update_header() {
208   assert(m_image_ctx.owner_lock.is_locked());
209
210   // should have been canceled prior to releasing lock
211   assert(m_image_ctx.exclusive_lock == nullptr ||
212          m_image_ctx.exclusive_lock->is_lock_owner());
213
214   ldout(m_image_ctx.cct, 5) << this << " send_update_header" << dendl;
215   m_state = STATE_UPDATE_HEADER;
216
217   librados::ObjectWriteOperation op;
218
219   uint64_t flags = RBD_FLAG_OBJECT_MAP_INVALID | RBD_FLAG_FAST_DIFF_INVALID;
220   cls_client::set_flags(&op, m_image_ctx.snap_id, 0, flags);
221
222   librados::AioCompletion *comp = this->create_callback_completion();
223   int r = m_image_ctx.md_ctx.aio_operate(m_image_ctx.header_oid, comp, &op);
224   assert(r == 0);
225   comp->release();
226
227   RWLock::WLocker snap_locker(m_image_ctx.snap_lock);
228   m_image_ctx.update_flags(m_image_ctx.snap_id, flags, false);
229 }
230
231 template <typename I>
232 uint64_t RebuildObjectMapRequest<I>::get_image_size() const {
233   assert(m_image_ctx.snap_lock.is_locked());
234   if (m_image_ctx.snap_id == CEPH_NOSNAP) {
235     if (!m_image_ctx.resize_reqs.empty()) {
236       return m_image_ctx.resize_reqs.front()->get_image_size();
237     } else {
238       return m_image_ctx.size;
239     }
240   }
241   return  m_image_ctx.get_image_size(m_image_ctx.snap_id);
242 }
243
244 } // namespace operation
245 } // namespace librbd
246
247 template class librbd::operation::RebuildObjectMapRequest<librbd::ImageCtx>;