]> git.proxmox.com Git - ceph.git/blob - ceph/src/librbd/operation/RebuildObjectMapRequest.cc
import 15.2.0 Octopus source
[ceph.git] / 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 "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>
19
20 #define dout_subsys ceph_subsys_rbd
21 #undef dout_prefix
22 #define dout_prefix *_dout << "librbd::RebuildObjectMapRequest: "
23
24 namespace librbd {
25 namespace operation {
26
27 using util::create_context_callback;
28
29 template <typename I>
30 void RebuildObjectMapRequest<I>::send() {
31 send_resize_object_map();
32 }
33
34 template <typename I>
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;
38
39 std::shared_lock owner_lock{m_image_ctx.owner_lock};
40 switch (m_state) {
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;
46 send_trim_image();
47 return false;
48 } else if (r == 0) {
49 send_verify_objects();
50 }
51 break;
52
53 case STATE_TRIM_IMAGE:
54 ldout(cct, 5) << "TRIM_IMAGE" << dendl;
55 if (r == 0) {
56 send_resize_object_map();
57 }
58 break;
59
60 case STATE_VERIFY_OBJECTS:
61 ldout(cct, 5) << "VERIFY_OBJECTS" << dendl;
62 if (r == 0) {
63 send_save_object_map();
64 }
65 break;
66
67 case STATE_SAVE_OBJECT_MAP:
68 ldout(cct, 5) << "SAVE_OBJECT_MAP" << dendl;
69 if (r == 0) {
70 send_update_header();
71 }
72 break;
73 case STATE_UPDATE_HEADER:
74 ldout(cct, 5) << "UPDATE_HEADER" << dendl;
75 if (r == 0) {
76 return true;
77 }
78 break;
79
80 default:
81 ceph_abort();
82 break;
83 }
84
85 if (r == -ERESTART) {
86 ldout(cct, 5) << "rebuild object map operation interrupted" << dendl;
87 return true;
88 } else if (r < 0) {
89 lderr(cct) << "rebuild object map encountered an error: " << cpp_strerror(r)
90 << dendl;
91 return true;
92 }
93 return false;
94 }
95
96 template <typename I>
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;
100
101 m_image_ctx.image_lock.lock_shared();
102 ceph_assert(m_image_ctx.object_map != nullptr);
103
104 uint64_t size = get_image_size();
105 uint64_t num_objects = Striper::get_num_objects(m_image_ctx.layout, size);
106
107 if (m_image_ctx.object_map->size() == num_objects) {
108 m_image_ctx.image_lock.unlock_shared();
109 send_verify_objects();
110 return;
111 }
112
113 ldout(cct, 5) << this << " send_resize_object_map" << dendl;
114 m_state = STATE_RESIZE_OBJECT_MAP;
115
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());
119
120 m_image_ctx.object_map->aio_resize(size, OBJECT_NONEXISTENT,
121 this->create_callback_context());
122 m_image_ctx.image_lock.unlock_shared();
123 }
124
125 template <typename I>
126 void RebuildObjectMapRequest<I>::send_trim_image() {
127 CephContext *cct = m_image_ctx.cct;
128
129 std::shared_lock l{m_image_ctx.owner_lock};
130
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;
136
137 uint64_t new_size;
138 uint64_t orig_size;
139 {
140 std::shared_lock l{m_image_ctx.image_lock};
141 ceph_assert(m_image_ctx.object_map != nullptr);
142
143 new_size = get_image_size();
144 orig_size = m_image_ctx.get_object_size() *
145 m_image_ctx.object_map->size();
146 }
147 TrimRequest<I> *req = TrimRequest<I>::create(m_image_ctx,
148 this->create_callback_context(),
149 orig_size, new_size, m_prog_ctx);
150 req->send();
151 }
152
153 template <typename I>
154 bool update_object_map(I& image_ctx, uint64_t object_no, uint8_t current_state,
155 uint8_t new_state) {
156 CephContext *cct = image_ctx.cct;
157 uint64_t snap_id = image_ctx.snap_id;
158
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;
164 }
165
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);
172 }
173 return false;
174 }
175
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;
180
181 m_state = STATE_VERIFY_OBJECTS;
182 ldout(cct, 5) << this << " send_verify_objects" << dendl;
183
184 ObjectMapIterateRequest<I> *req =
185 new ObjectMapIterateRequest<I>(m_image_ctx,
186 this->create_callback_context(),
187 m_prog_ctx, update_object_map);
188
189 req->send();
190 }
191
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;
196
197 ldout(cct, 5) << this << " send_save_object_map" << dendl;
198 m_state = STATE_SAVE_OBJECT_MAP;
199
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());
203
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());
207 }
208
209 template <typename I>
210 void RebuildObjectMapRequest<I>::send_update_header() {
211 ceph_assert(ceph_mutex_is_locked(m_image_ctx.owner_lock));
212
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());
216
217 ldout(m_image_ctx.cct, 5) << this << " send_update_header" << dendl;
218 m_state = STATE_UPDATE_HEADER;
219
220 librados::ObjectWriteOperation op;
221
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);
224
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);
227 ceph_assert(r == 0);
228 comp->release();
229
230 std::unique_lock image_locker{m_image_ctx.image_lock};
231 m_image_ctx.update_flags(m_image_ctx.snap_id, flags, false);
232 }
233
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();
240 } else {
241 return m_image_ctx.size;
242 }
243 }
244 return m_image_ctx.get_image_size(m_image_ctx.snap_id);
245 }
246
247 } // namespace operation
248 } // namespace librbd
249
250 template class librbd::operation::RebuildObjectMapRequest<librbd::ImageCtx>;