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 "osdc/Striper.h"
8 #include "librbd/AsyncObjectThrottle.h"
9 #include "librbd/ExclusiveLock.h"
10 #include "librbd/ImageCtx.h"
11 #include "librbd/internal.h"
12 #include "librbd/ObjectMap.h"
13 #include "librbd/operation/ResizeRequest.h"
14 #include "librbd/operation/TrimRequest.h"
15 #include "librbd/operation/ObjectMapIterate.h"
16 #include "librbd/Utils.h"
17 #include <boost/lambda/bind.hpp>
18 #include <boost/lambda/construct.hpp>
20 #define dout_subsys ceph_subsys_rbd
22 #define dout_prefix *_dout << "librbd::RebuildObjectMapRequest: "
27 using util::create_context_callback
;
30 void RebuildObjectMapRequest
<I
>::send() {
31 send_resize_object_map();
35 bool RebuildObjectMapRequest
<I
>::should_complete(int r
) {
36 CephContext
*cct
= m_image_ctx
.cct
;
37 ldout(cct
, 5) << this << " should_complete: " << " r=" << r
<< dendl
;
39 std::shared_lock owner_lock
{m_image_ctx
.owner_lock
};
41 case STATE_RESIZE_OBJECT_MAP
:
42 ldout(cct
, 5) << "RESIZE_OBJECT_MAP" << dendl
;
43 if (r
== -ESTALE
&& !m_attempted_trim
) {
44 // objects are still flagged as in-use -- delete them
45 m_attempted_trim
= true;
49 send_verify_objects();
53 case STATE_TRIM_IMAGE
:
54 ldout(cct
, 5) << "TRIM_IMAGE" << dendl
;
56 send_resize_object_map();
60 case STATE_VERIFY_OBJECTS
:
61 ldout(cct
, 5) << "VERIFY_OBJECTS" << dendl
;
63 send_save_object_map();
67 case STATE_SAVE_OBJECT_MAP
:
68 ldout(cct
, 5) << "SAVE_OBJECT_MAP" << dendl
;
73 case STATE_UPDATE_HEADER
:
74 ldout(cct
, 5) << "UPDATE_HEADER" << dendl
;
86 ldout(cct
, 5) << "rebuild object map operation interrupted" << dendl
;
89 lderr(cct
) << "rebuild object map encountered an error: " << cpp_strerror(r
)
97 void RebuildObjectMapRequest
<I
>::send_resize_object_map() {
98 ceph_assert(ceph_mutex_is_locked(m_image_ctx
.owner_lock
));
99 CephContext
*cct
= m_image_ctx
.cct
;
101 m_image_ctx
.image_lock
.lock_shared();
102 ceph_assert(m_image_ctx
.object_map
!= nullptr);
104 uint64_t size
= get_image_size();
105 uint64_t num_objects
= Striper::get_num_objects(m_image_ctx
.layout
, size
);
107 if (m_image_ctx
.object_map
->size() == num_objects
) {
108 m_image_ctx
.image_lock
.unlock_shared();
109 send_verify_objects();
113 ldout(cct
, 5) << this << " send_resize_object_map" << dendl
;
114 m_state
= STATE_RESIZE_OBJECT_MAP
;
116 // should have been canceled prior to releasing lock
117 ceph_assert(m_image_ctx
.exclusive_lock
== nullptr ||
118 m_image_ctx
.exclusive_lock
->is_lock_owner());
120 m_image_ctx
.object_map
->aio_resize(size
, OBJECT_NONEXISTENT
,
121 this->create_callback_context());
122 m_image_ctx
.image_lock
.unlock_shared();
125 template <typename I
>
126 void RebuildObjectMapRequest
<I
>::send_trim_image() {
127 CephContext
*cct
= m_image_ctx
.cct
;
129 std::shared_lock l
{m_image_ctx
.owner_lock
};
131 // should have been canceled prior to releasing lock
132 ceph_assert(m_image_ctx
.exclusive_lock
== nullptr ||
133 m_image_ctx
.exclusive_lock
->is_lock_owner());
134 ldout(cct
, 5) << this << " send_trim_image" << dendl
;
135 m_state
= STATE_TRIM_IMAGE
;
140 std::shared_lock l
{m_image_ctx
.image_lock
};
141 ceph_assert(m_image_ctx
.object_map
!= nullptr);
143 new_size
= get_image_size();
144 orig_size
= m_image_ctx
.get_object_size() *
145 m_image_ctx
.object_map
->size();
147 TrimRequest
<I
> *req
= TrimRequest
<I
>::create(m_image_ctx
,
148 this->create_callback_context(),
149 orig_size
, new_size
, m_prog_ctx
);
153 template <typename I
>
154 bool update_object_map(I
& image_ctx
, uint64_t object_no
, uint8_t current_state
,
156 CephContext
*cct
= image_ctx
.cct
;
157 uint64_t snap_id
= image_ctx
.snap_id
;
159 current_state
= (*image_ctx
.object_map
)[object_no
];
160 if (current_state
== OBJECT_EXISTS
&& new_state
== OBJECT_NONEXISTENT
&&
161 snap_id
== CEPH_NOSNAP
) {
162 // might be writing object to OSD concurrently
163 new_state
= current_state
;
166 if (new_state
!= current_state
) {
167 ldout(cct
, 15) << image_ctx
.get_object_name(object_no
)
168 << " rebuild updating object map "
169 << static_cast<uint32_t>(current_state
) << "->"
170 << static_cast<uint32_t>(new_state
) << dendl
;
171 image_ctx
.object_map
->set_state(object_no
, new_state
, current_state
);
176 template <typename I
>
177 void RebuildObjectMapRequest
<I
>::send_verify_objects() {
178 ceph_assert(ceph_mutex_is_locked(m_image_ctx
.owner_lock
));
179 CephContext
*cct
= m_image_ctx
.cct
;
181 m_state
= STATE_VERIFY_OBJECTS
;
182 ldout(cct
, 5) << this << " send_verify_objects" << dendl
;
184 ObjectMapIterateRequest
<I
> *req
=
185 new ObjectMapIterateRequest
<I
>(m_image_ctx
,
186 this->create_callback_context(),
187 m_prog_ctx
, update_object_map
);
192 template <typename I
>
193 void RebuildObjectMapRequest
<I
>::send_save_object_map() {
194 ceph_assert(ceph_mutex_is_locked(m_image_ctx
.owner_lock
));
195 CephContext
*cct
= m_image_ctx
.cct
;
197 ldout(cct
, 5) << this << " send_save_object_map" << dendl
;
198 m_state
= STATE_SAVE_OBJECT_MAP
;
200 // should have been canceled prior to releasing lock
201 ceph_assert(m_image_ctx
.exclusive_lock
== nullptr ||
202 m_image_ctx
.exclusive_lock
->is_lock_owner());
204 std::shared_lock image_locker
{m_image_ctx
.image_lock
};
205 ceph_assert(m_image_ctx
.object_map
!= nullptr);
206 m_image_ctx
.object_map
->aio_save(this->create_callback_context());
209 template <typename I
>
210 void RebuildObjectMapRequest
<I
>::send_update_header() {
211 ceph_assert(ceph_mutex_is_locked(m_image_ctx
.owner_lock
));
213 // should have been canceled prior to releasing lock
214 ceph_assert(m_image_ctx
.exclusive_lock
== nullptr ||
215 m_image_ctx
.exclusive_lock
->is_lock_owner());
217 ldout(m_image_ctx
.cct
, 5) << this << " send_update_header" << dendl
;
218 m_state
= STATE_UPDATE_HEADER
;
220 librados::ObjectWriteOperation op
;
222 uint64_t flags
= RBD_FLAG_OBJECT_MAP_INVALID
| RBD_FLAG_FAST_DIFF_INVALID
;
223 cls_client::set_flags(&op
, m_image_ctx
.snap_id
, 0, flags
);
225 librados::AioCompletion
*comp
= this->create_callback_completion();
226 int r
= m_image_ctx
.md_ctx
.aio_operate(m_image_ctx
.header_oid
, comp
, &op
);
230 std::unique_lock image_locker
{m_image_ctx
.image_lock
};
231 m_image_ctx
.update_flags(m_image_ctx
.snap_id
, flags
, false);
234 template <typename I
>
235 uint64_t RebuildObjectMapRequest
<I
>::get_image_size() const {
236 ceph_assert(ceph_mutex_is_locked(m_image_ctx
.image_lock
));
237 if (m_image_ctx
.snap_id
== CEPH_NOSNAP
) {
238 if (!m_image_ctx
.resize_reqs
.empty()) {
239 return m_image_ctx
.resize_reqs
.front()->get_image_size();
241 return m_image_ctx
.size
;
244 return m_image_ctx
.get_image_size(m_image_ctx
.snap_id
);
247 } // namespace operation
248 } // namespace librbd
250 template class librbd::operation::RebuildObjectMapRequest
<librbd::ImageCtx
>;