]> git.proxmox.com Git - ceph.git/blob - ceph/src/librbd/operation/RebuildObjectMapRequest.cc
update sources to ceph Nautilus 14.2.1
[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 template <typename I>
28 void RebuildObjectMapRequest<I>::send() {
29 send_resize_object_map();
30 }
31
32 template <typename I>
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;
36
37 RWLock::RLocker owner_lock(m_image_ctx.owner_lock);
38 switch (m_state) {
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;
44 send_trim_image();
45 return false;
46 } else if (r == 0) {
47 send_verify_objects();
48 }
49 break;
50
51 case STATE_TRIM_IMAGE:
52 ldout(cct, 5) << "TRIM_IMAGE" << dendl;
53 if (r == 0) {
54 send_resize_object_map();
55 }
56 break;
57
58 case STATE_VERIFY_OBJECTS:
59 ldout(cct, 5) << "VERIFY_OBJECTS" << dendl;
60 if (r == 0) {
61 send_save_object_map();
62 }
63 break;
64
65 case STATE_SAVE_OBJECT_MAP:
66 ldout(cct, 5) << "SAVE_OBJECT_MAP" << dendl;
67 if (r == 0) {
68 send_update_header();
69 }
70 break;
71 case STATE_UPDATE_HEADER:
72 ldout(cct, 5) << "UPDATE_HEADER" << dendl;
73 if (r == 0) {
74 return true;
75 }
76 break;
77
78 default:
79 ceph_abort();
80 break;
81 }
82
83 if (r == -ERESTART) {
84 ldout(cct, 5) << "rebuild object map operation interrupted" << dendl;
85 return true;
86 } else if (r < 0) {
87 lderr(cct) << "rebuild object map encountered an error: " << cpp_strerror(r)
88 << dendl;
89 return true;
90 }
91 return false;
92 }
93
94 template <typename I>
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;
98
99 m_image_ctx.snap_lock.get_read();
100 ceph_assert(m_image_ctx.object_map != nullptr);
101
102 uint64_t size = get_image_size();
103 uint64_t num_objects = Striper::get_num_objects(m_image_ctx.layout, size);
104
105 if (m_image_ctx.object_map->size() == num_objects) {
106 m_image_ctx.snap_lock.put_read();
107 send_verify_objects();
108 return;
109 }
110
111 ldout(cct, 5) << this << " send_resize_object_map" << dendl;
112 m_state = STATE_RESIZE_OBJECT_MAP;
113
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());
117
118 m_image_ctx.object_map->aio_resize(size, OBJECT_NONEXISTENT,
119 this->create_callback_context());
120 m_image_ctx.snap_lock.put_read();
121 }
122
123 template <typename I>
124 void RebuildObjectMapRequest<I>::send_trim_image() {
125 CephContext *cct = m_image_ctx.cct;
126
127 RWLock::RLocker l(m_image_ctx.owner_lock);
128
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;
134
135 uint64_t new_size;
136 uint64_t orig_size;
137 {
138 RWLock::RLocker l(m_image_ctx.snap_lock);
139 ceph_assert(m_image_ctx.object_map != nullptr);
140
141 new_size = get_image_size();
142 orig_size = m_image_ctx.get_object_size() *
143 m_image_ctx.object_map->size();
144 }
145 TrimRequest<I> *req = TrimRequest<I>::create(m_image_ctx,
146 this->create_callback_context(),
147 orig_size, new_size, m_prog_ctx);
148 req->send();
149 }
150
151 template <typename I>
152 bool update_object_map(I& image_ctx, uint64_t object_no, uint8_t current_state,
153 uint8_t new_state) {
154 CephContext *cct = image_ctx.cct;
155 uint64_t snap_id = image_ctx.snap_id;
156
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
161 new_state = state;
162 }
163
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;
170 }
171 return false;
172 }
173
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;
178
179 m_state = STATE_VERIFY_OBJECTS;
180 ldout(cct, 5) << this << " send_verify_objects" << dendl;
181
182 ObjectMapIterateRequest<I> *req =
183 new ObjectMapIterateRequest<I>(m_image_ctx,
184 this->create_callback_context(),
185 m_prog_ctx, update_object_map);
186
187 req->send();
188 }
189
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;
194
195 ldout(cct, 5) << this << " send_save_object_map" << dendl;
196 m_state = STATE_SAVE_OBJECT_MAP;
197
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());
201
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());
205 }
206
207 template <typename I>
208 void RebuildObjectMapRequest<I>::send_update_header() {
209 ceph_assert(m_image_ctx.owner_lock.is_locked());
210
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());
214
215 ldout(m_image_ctx.cct, 5) << this << " send_update_header" << dendl;
216 m_state = STATE_UPDATE_HEADER;
217
218 librados::ObjectWriteOperation op;
219
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);
222
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);
225 ceph_assert(r == 0);
226 comp->release();
227
228 RWLock::WLocker snap_locker(m_image_ctx.snap_lock);
229 m_image_ctx.update_flags(m_image_ctx.snap_id, flags, false);
230 }
231
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();
238 } else {
239 return m_image_ctx.size;
240 }
241 }
242 return m_image_ctx.get_image_size(m_image_ctx.snap_id);
243 }
244
245 } // namespace operation
246 } // namespace librbd
247
248 template class librbd::operation::RebuildObjectMapRequest<librbd::ImageCtx>;