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/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>
19 #define dout_subsys ceph_subsys_rbd
21 #define dout_prefix *_dout << "librbd::RebuildObjectMapRequest: "
27 void RebuildObjectMapRequest<I>::send() {
28 send_resize_object_map();
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;
36 RWLock::RLocker owner_lock(m_image_ctx.owner_lock);
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;
46 send_verify_objects();
50 case STATE_TRIM_IMAGE:
51 ldout(cct, 5) << "TRIM_IMAGE" << dendl;
53 send_resize_object_map();
57 case STATE_VERIFY_OBJECTS:
58 ldout(cct, 5) << "VERIFY_OBJECTS" << dendl;
60 send_save_object_map();
64 case STATE_SAVE_OBJECT_MAP:
65 ldout(cct, 5) << "SAVE_OBJECT_MAP" << dendl;
70 case STATE_UPDATE_HEADER:
71 ldout(cct, 5) << "UPDATE_HEADER" << dendl;
83 ldout(cct, 5) << "rebuild object map operation interrupted" << dendl;
86 lderr(cct) << "rebuild object map encountered an error: " << cpp_strerror(r)
94 void RebuildObjectMapRequest<I>::send_resize_object_map() {
95 assert(m_image_ctx.owner_lock.is_locked());
96 CephContext *cct = m_image_ctx.cct;
98 m_image_ctx.snap_lock.get_read();
99 assert(m_image_ctx.object_map != nullptr);
101 uint64_t size = get_image_size();
102 uint64_t num_objects = Striper::get_num_objects(m_image_ctx.layout, size);
104 if (m_image_ctx.object_map->size() == num_objects) {
105 m_image_ctx.snap_lock.put_read();
106 send_verify_objects();
110 ldout(cct, 5) << this << " send_resize_object_map" << dendl;
111 m_state = STATE_RESIZE_OBJECT_MAP;
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());
117 m_image_ctx.object_map->aio_resize(size, OBJECT_NONEXISTENT,
118 this->create_callback_context());
119 m_image_ctx.snap_lock.put_read();
122 template <typename I>
123 void RebuildObjectMapRequest<I>::send_trim_image() {
124 CephContext *cct = m_image_ctx.cct;
126 RWLock::RLocker l(m_image_ctx.owner_lock);
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;
137 RWLock::RLocker l(m_image_ctx.snap_lock);
138 assert(m_image_ctx.object_map != nullptr);
140 new_size = get_image_size();
141 orig_size = m_image_ctx.get_object_size() *
142 m_image_ctx.object_map->size();
144 TrimRequest<I> *req = TrimRequest<I>::create(m_image_ctx,
145 this->create_callback_context(),
146 orig_size, new_size, m_prog_ctx);
150 template <typename I>
151 bool update_object_map(I& image_ctx, uint64_t object_no, uint8_t current_state,
153 CephContext *cct = image_ctx.cct;
154 uint64_t snap_id = image_ctx.snap_id;
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
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;
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;
178 m_state = STATE_VERIFY_OBJECTS;
179 ldout(cct, 5) << this << " send_verify_objects" << dendl;
181 ObjectMapIterateRequest<I> *req =
182 new ObjectMapIterateRequest<I>(m_image_ctx,
183 this->create_callback_context(),
184 m_prog_ctx, update_object_map);
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;
194 ldout(cct, 5) << this << " send_save_object_map" << dendl;
195 m_state = STATE_SAVE_OBJECT_MAP;
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());
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());
206 template <typename I>
207 void RebuildObjectMapRequest<I>::send_update_header() {
208 assert(m_image_ctx.owner_lock.is_locked());
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());
214 ldout(m_image_ctx.cct, 5) << this << " send_update_header" << dendl;
215 m_state = STATE_UPDATE_HEADER;
217 librados::ObjectWriteOperation op;
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);
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);
227 RWLock::WLocker snap_locker(m_image_ctx.snap_lock);
228 m_image_ctx.update_flags(m_image_ctx.snap_id, flags, false);
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();
238 return m_image_ctx.size;
241 return m_image_ctx.get_image_size(m_image_ctx.snap_id);
244 } // namespace operation
245 } // namespace librbd
247 template class librbd::operation::RebuildObjectMapRequest<librbd::ImageCtx>;