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: "
28 void RebuildObjectMapRequest
<I
>::send() {
29 send_resize_object_map();
33 bool RebuildObjectMapRequest
<I
>::should_complete(int r
) {
34 CephContext
*cct
= m_image_ctx
.cct
;
35 ldout(cct
, 5) << this << " should_complete: " << " r=" << r
<< dendl
;
37 RWLock::RLocker
owner_lock(m_image_ctx
.owner_lock
);
39 case STATE_RESIZE_OBJECT_MAP
:
40 ldout(cct
, 5) << "RESIZE_OBJECT_MAP" << dendl
;
41 if (r
== -ESTALE
&& !m_attempted_trim
) {
42 // objects are still flagged as in-use -- delete them
43 m_attempted_trim
= true;
47 send_verify_objects();
51 case STATE_TRIM_IMAGE
:
52 ldout(cct
, 5) << "TRIM_IMAGE" << dendl
;
54 send_resize_object_map();
58 case STATE_VERIFY_OBJECTS
:
59 ldout(cct
, 5) << "VERIFY_OBJECTS" << dendl
;
61 send_save_object_map();
65 case STATE_SAVE_OBJECT_MAP
:
66 ldout(cct
, 5) << "SAVE_OBJECT_MAP" << dendl
;
71 case STATE_UPDATE_HEADER
:
72 ldout(cct
, 5) << "UPDATE_HEADER" << dendl
;
84 ldout(cct
, 5) << "rebuild object map operation interrupted" << dendl
;
87 lderr(cct
) << "rebuild object map encountered an error: " << cpp_strerror(r
)
95 void RebuildObjectMapRequest
<I
>::send_resize_object_map() {
96 ceph_assert(m_image_ctx
.owner_lock
.is_locked());
97 CephContext
*cct
= m_image_ctx
.cct
;
99 m_image_ctx
.snap_lock
.get_read();
100 ceph_assert(m_image_ctx
.object_map
!= nullptr);
102 uint64_t size
= get_image_size();
103 uint64_t num_objects
= Striper::get_num_objects(m_image_ctx
.layout
, size
);
105 if (m_image_ctx
.object_map
->size() == num_objects
) {
106 m_image_ctx
.snap_lock
.put_read();
107 send_verify_objects();
111 ldout(cct
, 5) << this << " send_resize_object_map" << dendl
;
112 m_state
= STATE_RESIZE_OBJECT_MAP
;
114 // should have been canceled prior to releasing lock
115 ceph_assert(m_image_ctx
.exclusive_lock
== nullptr ||
116 m_image_ctx
.exclusive_lock
->is_lock_owner());
118 m_image_ctx
.object_map
->aio_resize(size
, OBJECT_NONEXISTENT
,
119 this->create_callback_context());
120 m_image_ctx
.snap_lock
.put_read();
123 template <typename I
>
124 void RebuildObjectMapRequest
<I
>::send_trim_image() {
125 CephContext
*cct
= m_image_ctx
.cct
;
127 RWLock::RLocker
l(m_image_ctx
.owner_lock
);
129 // should have been canceled prior to releasing lock
130 ceph_assert(m_image_ctx
.exclusive_lock
== nullptr ||
131 m_image_ctx
.exclusive_lock
->is_lock_owner());
132 ldout(cct
, 5) << this << " send_trim_image" << dendl
;
133 m_state
= STATE_TRIM_IMAGE
;
138 RWLock::RLocker
l(m_image_ctx
.snap_lock
);
139 ceph_assert(m_image_ctx
.object_map
!= nullptr);
141 new_size
= get_image_size();
142 orig_size
= m_image_ctx
.get_object_size() *
143 m_image_ctx
.object_map
->size();
145 TrimRequest
<I
> *req
= TrimRequest
<I
>::create(m_image_ctx
,
146 this->create_callback_context(),
147 orig_size
, new_size
, m_prog_ctx
);
151 template <typename I
>
152 bool update_object_map(I
& image_ctx
, uint64_t object_no
, uint8_t current_state
,
154 CephContext
*cct
= image_ctx
.cct
;
155 uint64_t snap_id
= image_ctx
.snap_id
;
157 uint8_t state
= (*image_ctx
.object_map
)[object_no
];
158 if (state
== OBJECT_EXISTS
&& new_state
== OBJECT_NONEXISTENT
&&
159 snap_id
== CEPH_NOSNAP
) {
160 // might be writing object to OSD concurrently
164 if (new_state
!= state
) {
165 ldout(cct
, 15) << image_ctx
.get_object_name(object_no
)
166 << " rebuild updating object map "
167 << static_cast<uint32_t>(state
) << "->"
168 << static_cast<uint32_t>(new_state
) << dendl
;
169 (*image_ctx
.object_map
)[object_no
] = new_state
;
174 template <typename I
>
175 void RebuildObjectMapRequest
<I
>::send_verify_objects() {
176 ceph_assert(m_image_ctx
.owner_lock
.is_locked());
177 CephContext
*cct
= m_image_ctx
.cct
;
179 m_state
= STATE_VERIFY_OBJECTS
;
180 ldout(cct
, 5) << this << " send_verify_objects" << dendl
;
182 ObjectMapIterateRequest
<I
> *req
=
183 new ObjectMapIterateRequest
<I
>(m_image_ctx
,
184 this->create_callback_context(),
185 m_prog_ctx
, update_object_map
);
190 template <typename I
>
191 void RebuildObjectMapRequest
<I
>::send_save_object_map() {
192 ceph_assert(m_image_ctx
.owner_lock
.is_locked());
193 CephContext
*cct
= m_image_ctx
.cct
;
195 ldout(cct
, 5) << this << " send_save_object_map" << dendl
;
196 m_state
= STATE_SAVE_OBJECT_MAP
;
198 // should have been canceled prior to releasing lock
199 ceph_assert(m_image_ctx
.exclusive_lock
== nullptr ||
200 m_image_ctx
.exclusive_lock
->is_lock_owner());
202 RWLock::RLocker
snap_locker(m_image_ctx
.snap_lock
);
203 ceph_assert(m_image_ctx
.object_map
!= nullptr);
204 m_image_ctx
.object_map
->aio_save(this->create_callback_context());
207 template <typename I
>
208 void RebuildObjectMapRequest
<I
>::send_update_header() {
209 ceph_assert(m_image_ctx
.owner_lock
.is_locked());
211 // should have been canceled prior to releasing lock
212 ceph_assert(m_image_ctx
.exclusive_lock
== nullptr ||
213 m_image_ctx
.exclusive_lock
->is_lock_owner());
215 ldout(m_image_ctx
.cct
, 5) << this << " send_update_header" << dendl
;
216 m_state
= STATE_UPDATE_HEADER
;
218 librados::ObjectWriteOperation op
;
220 uint64_t flags
= RBD_FLAG_OBJECT_MAP_INVALID
| RBD_FLAG_FAST_DIFF_INVALID
;
221 cls_client::set_flags(&op
, m_image_ctx
.snap_id
, 0, flags
);
223 librados::AioCompletion
*comp
= this->create_callback_completion();
224 int r
= m_image_ctx
.md_ctx
.aio_operate(m_image_ctx
.header_oid
, comp
, &op
);
228 RWLock::WLocker
snap_locker(m_image_ctx
.snap_lock
);
229 m_image_ctx
.update_flags(m_image_ctx
.snap_id
, flags
, false);
232 template <typename I
>
233 uint64_t RebuildObjectMapRequest
<I
>::get_image_size() const {
234 ceph_assert(m_image_ctx
.snap_lock
.is_locked());
235 if (m_image_ctx
.snap_id
== CEPH_NOSNAP
) {
236 if (!m_image_ctx
.resize_reqs
.empty()) {
237 return m_image_ctx
.resize_reqs
.front()->get_image_size();
239 return m_image_ctx
.size
;
242 return m_image_ctx
.get_image_size(m_image_ctx
.snap_id
);
245 } // namespace operation
246 } // namespace librbd
248 template class librbd::operation::RebuildObjectMapRequest
<librbd::ImageCtx
>;