]> git.proxmox.com Git - ceph.git/blob - ceph/src/librbd/ObjectMap.cc
0d60cc95f94e5790c012993c2e951a8b62c073f9
[ceph.git] / ceph / src / librbd / ObjectMap.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/ObjectMap.h"
5 #include "librbd/BlockGuard.h"
6 #include "librbd/ExclusiveLock.h"
7 #include "librbd/ImageCtx.h"
8 #include "librbd/object_map/RefreshRequest.h"
9 #include "librbd/object_map/ResizeRequest.h"
10 #include "librbd/object_map/SnapshotCreateRequest.h"
11 #include "librbd/object_map/SnapshotRemoveRequest.h"
12 #include "librbd/object_map/SnapshotRollbackRequest.h"
13 #include "librbd/object_map/UnlockRequest.h"
14 #include "librbd/object_map/UpdateRequest.h"
15 #include "librbd/Utils.h"
16 #include "common/dout.h"
17 #include "common/errno.h"
18 #include "common/WorkQueue.h"
19
20 #include "include/rados/librados.hpp"
21
22 #include "cls/lock/cls_lock_client.h"
23 #include "cls/rbd/cls_rbd_types.h"
24 #include "include/stringify.h"
25 #include "osdc/Striper.h"
26 #include <sstream>
27
28 #define dout_subsys ceph_subsys_rbd
29 #undef dout_prefix
30 #define dout_prefix *_dout << "librbd::ObjectMap: " << this << " " << __func__ \
31 << ": "
32
33 namespace librbd {
34
35 using librbd::util::create_context_callback;
36
37 template <typename I>
38 ObjectMap<I>::ObjectMap(I &image_ctx, uint64_t snap_id)
39 : RefCountedObject(image_ctx.cct),
40 m_image_ctx(image_ctx), m_snap_id(snap_id),
41 m_lock(ceph::make_shared_mutex(util::unique_lock_name("librbd::ObjectMap::lock", this))),
42 m_update_guard(new UpdateGuard(m_image_ctx.cct)) {
43 }
44
45 template <typename I>
46 ObjectMap<I>::~ObjectMap() {
47 delete m_update_guard;
48 }
49
50 template <typename I>
51 int ObjectMap<I>::aio_remove(librados::IoCtx &io_ctx, const std::string &image_id,
52 librados::AioCompletion *c) {
53 return io_ctx.aio_remove(object_map_name(image_id, CEPH_NOSNAP), c);
54 }
55
56 template <typename I>
57 std::string ObjectMap<I>::object_map_name(const std::string &image_id,
58 uint64_t snap_id) {
59 std::string oid(RBD_OBJECT_MAP_PREFIX + image_id);
60 if (snap_id != CEPH_NOSNAP) {
61 std::stringstream snap_suffix;
62 snap_suffix << "." << std::setfill('0') << std::setw(16) << std::hex
63 << snap_id;
64 oid += snap_suffix.str();
65 }
66 return oid;
67 }
68
69 template <typename I>
70 bool ObjectMap<I>::is_compatible(const file_layout_t& layout, uint64_t size) {
71 uint64_t object_count = Striper::get_num_objects(layout, size);
72 return (object_count <= cls::rbd::MAX_OBJECT_MAP_OBJECT_COUNT);
73 }
74
75 template <typename I>
76 uint8_t ObjectMap<I>::operator[](uint64_t object_no) const
77 {
78 std::shared_lock locker{m_lock};
79 ceph_assert(object_no < m_object_map.size());
80 return m_object_map[object_no];
81 }
82
83 template <typename I>
84 bool ObjectMap<I>::object_may_exist(uint64_t object_no) const
85 {
86 ceph_assert(ceph_mutex_is_locked(m_image_ctx.image_lock));
87
88 // Fall back to default logic if object map is disabled or invalid
89 if (!m_image_ctx.test_features(RBD_FEATURE_OBJECT_MAP,
90 m_image_ctx.image_lock)) {
91 return true;
92 }
93
94 bool flags_set;
95 int r = m_image_ctx.test_flags(m_image_ctx.snap_id,
96 RBD_FLAG_OBJECT_MAP_INVALID,
97 m_image_ctx.image_lock, &flags_set);
98 if (r < 0 || flags_set) {
99 return true;
100 }
101
102 uint8_t state = (*this)[object_no];
103 bool exists = (state != OBJECT_NONEXISTENT);
104 ldout(m_image_ctx.cct, 20) << "object_no=" << object_no << " r=" << exists
105 << dendl;
106 return exists;
107 }
108
109 template <typename I>
110 bool ObjectMap<I>::object_may_not_exist(uint64_t object_no) const
111 {
112 ceph_assert(ceph_mutex_is_locked(m_image_ctx.image_lock));
113
114 // Fall back to default logic if object map is disabled or invalid
115 if (!m_image_ctx.test_features(RBD_FEATURE_OBJECT_MAP,
116 m_image_ctx.image_lock)) {
117 return true;
118 }
119
120 bool flags_set;
121 int r = m_image_ctx.test_flags(m_image_ctx.snap_id,
122 RBD_FLAG_OBJECT_MAP_INVALID,
123 m_image_ctx.image_lock, &flags_set);
124 if (r < 0 || flags_set) {
125 return true;
126 }
127
128 uint8_t state = (*this)[object_no];
129 bool nonexistent = (state != OBJECT_EXISTS && state != OBJECT_EXISTS_CLEAN);
130 ldout(m_image_ctx.cct, 20) << "object_no=" << object_no << " r="
131 << nonexistent << dendl;
132 return nonexistent;
133 }
134
135 template <typename I>
136 bool ObjectMap<I>::update_required(const ceph::BitVector<2>::Iterator& it,
137 uint8_t new_state) {
138 ceph_assert(ceph_mutex_is_locked(m_lock));
139 uint8_t state = *it;
140 if ((state == new_state) ||
141 (new_state == OBJECT_PENDING && state == OBJECT_NONEXISTENT) ||
142 (new_state == OBJECT_NONEXISTENT && state != OBJECT_PENDING)) {
143 return false;
144 }
145 return true;
146 }
147
148 template <typename I>
149 void ObjectMap<I>::open(Context *on_finish) {
150 Context *ctx = create_context_callback<Context>(on_finish, this);
151
152 auto req = object_map::RefreshRequest<I>::create(
153 m_image_ctx, &m_lock, &m_object_map, m_snap_id, ctx);
154 req->send();
155 }
156
157 template <typename I>
158 void ObjectMap<I>::close(Context *on_finish) {
159 Context *ctx = create_context_callback<Context>(on_finish, this);
160
161 if (m_snap_id != CEPH_NOSNAP) {
162 m_image_ctx.op_work_queue->queue(ctx, 0);
163 return;
164 }
165
166 auto req = object_map::UnlockRequest<I>::create(m_image_ctx, ctx);
167 req->send();
168 }
169
170 template <typename I>
171 bool ObjectMap<I>::set_object_map(ceph::BitVector<2> &target_object_map) {
172 ceph_assert(ceph_mutex_is_locked(m_image_ctx.owner_lock));
173 ceph_assert(ceph_mutex_is_locked(m_image_ctx.image_lock));
174 ceph_assert(m_image_ctx.test_features(RBD_FEATURE_OBJECT_MAP,
175 m_image_ctx.image_lock));
176 std::unique_lock locker{m_lock};
177 m_object_map = target_object_map;
178 return true;
179 }
180
181 template <typename I>
182 void ObjectMap<I>::rollback(uint64_t snap_id, Context *on_finish) {
183 ceph_assert(ceph_mutex_is_locked(m_image_ctx.image_lock));
184
185 std::unique_lock locker{m_lock};
186 Context *ctx = create_context_callback<Context>(on_finish, this);
187
188 object_map::SnapshotRollbackRequest *req =
189 new object_map::SnapshotRollbackRequest(m_image_ctx, snap_id, ctx);
190 req->send();
191 }
192
193 template <typename I>
194 void ObjectMap<I>::snapshot_add(uint64_t snap_id, Context *on_finish) {
195 ceph_assert(ceph_mutex_is_locked(m_image_ctx.image_lock));
196 ceph_assert((m_image_ctx.features & RBD_FEATURE_OBJECT_MAP) != 0);
197 ceph_assert(snap_id != CEPH_NOSNAP);
198
199 Context *ctx = create_context_callback<Context>(on_finish, this);
200
201 object_map::SnapshotCreateRequest *req =
202 new object_map::SnapshotCreateRequest(m_image_ctx, &m_lock, &m_object_map,
203 snap_id, ctx);
204 req->send();
205 }
206
207 template <typename I>
208 void ObjectMap<I>::snapshot_remove(uint64_t snap_id, Context *on_finish) {
209 ceph_assert(ceph_mutex_is_wlocked(m_image_ctx.image_lock));
210 ceph_assert((m_image_ctx.features & RBD_FEATURE_OBJECT_MAP) != 0);
211 ceph_assert(snap_id != CEPH_NOSNAP);
212
213 Context *ctx = create_context_callback<Context>(on_finish, this);
214
215 object_map::SnapshotRemoveRequest *req =
216 new object_map::SnapshotRemoveRequest(m_image_ctx, &m_lock, &m_object_map,
217 snap_id, ctx);
218 req->send();
219 }
220
221 template <typename I>
222 void ObjectMap<I>::aio_save(Context *on_finish) {
223 ceph_assert(ceph_mutex_is_locked(m_image_ctx.owner_lock));
224 ceph_assert(ceph_mutex_is_locked(m_image_ctx.image_lock));
225 ceph_assert(m_image_ctx.test_features(RBD_FEATURE_OBJECT_MAP,
226 m_image_ctx.image_lock));
227 std::shared_lock locker{m_lock};
228
229 librados::ObjectWriteOperation op;
230 if (m_snap_id == CEPH_NOSNAP) {
231 rados::cls::lock::assert_locked(&op, RBD_LOCK_NAME, LOCK_EXCLUSIVE, "", "");
232 }
233 cls_client::object_map_save(&op, m_object_map);
234
235 Context *ctx = create_context_callback<Context>(on_finish, this);
236
237 std::string oid(object_map_name(m_image_ctx.id, m_snap_id));
238 librados::AioCompletion *comp = util::create_rados_callback(ctx);
239
240 int r = m_image_ctx.md_ctx.aio_operate(oid, comp, &op);
241 ceph_assert(r == 0);
242 comp->release();
243 }
244
245 template <typename I>
246 void ObjectMap<I>::aio_resize(uint64_t new_size, uint8_t default_object_state,
247 Context *on_finish) {
248 ceph_assert(ceph_mutex_is_locked(m_image_ctx.owner_lock));
249 ceph_assert(ceph_mutex_is_locked(m_image_ctx.image_lock));
250 ceph_assert(m_image_ctx.test_features(RBD_FEATURE_OBJECT_MAP,
251 m_image_ctx.image_lock));
252 ceph_assert(m_image_ctx.image_watcher != NULL);
253 ceph_assert(m_image_ctx.exclusive_lock == nullptr ||
254 m_image_ctx.exclusive_lock->is_lock_owner());
255
256 Context *ctx = create_context_callback<Context>(on_finish, this);
257
258 object_map::ResizeRequest *req = new object_map::ResizeRequest(
259 m_image_ctx, &m_lock, &m_object_map, m_snap_id, new_size,
260 default_object_state, ctx);
261 req->send();
262 }
263
264 template <typename I>
265 void ObjectMap<I>::detained_aio_update(UpdateOperation &&op) {
266 CephContext *cct = m_image_ctx.cct;
267 ldout(cct, 20) << dendl;
268
269 ceph_assert(ceph_mutex_is_locked(m_image_ctx.image_lock));
270 ceph_assert(ceph_mutex_is_wlocked(m_lock));
271
272 BlockGuardCell *cell;
273 int r = m_update_guard->detain({op.start_object_no, op.end_object_no},
274 &op, &cell);
275 if (r < 0) {
276 lderr(cct) << "failed to detain object map update: " << cpp_strerror(r)
277 << dendl;
278 m_image_ctx.op_work_queue->queue(op.on_finish, r);
279 return;
280 } else if (r > 0) {
281 ldout(cct, 20) << "detaining object map update due to in-flight update: "
282 << "start=" << op.start_object_no << ", "
283 << "end=" << op.end_object_no << ", "
284 << (op.current_state ?
285 stringify(static_cast<uint32_t>(*op.current_state)) :
286 "")
287 << "->" << static_cast<uint32_t>(op.new_state) << dendl;
288 return;
289 }
290
291 ldout(cct, 20) << "in-flight update cell: " << cell << dendl;
292 Context *on_finish = op.on_finish;
293 Context *ctx = new LambdaContext([this, cell, on_finish](int r) {
294 handle_detained_aio_update(cell, r, on_finish);
295 });
296 aio_update(CEPH_NOSNAP, op.start_object_no, op.end_object_no, op.new_state,
297 op.current_state, op.parent_trace, op.ignore_enoent, ctx);
298 }
299
300 template <typename I>
301 void ObjectMap<I>::handle_detained_aio_update(BlockGuardCell *cell, int r,
302 Context *on_finish) {
303 CephContext *cct = m_image_ctx.cct;
304 ldout(cct, 20) << "cell=" << cell << ", r=" << r << dendl;
305
306 typename UpdateGuard::BlockOperations block_ops;
307 m_update_guard->release(cell, &block_ops);
308
309 {
310 std::shared_lock image_locker{m_image_ctx.image_lock};
311 std::unique_lock locker{m_lock};
312 for (auto &op : block_ops) {
313 detained_aio_update(std::move(op));
314 }
315 }
316
317 on_finish->complete(r);
318 }
319
320 template <typename I>
321 void ObjectMap<I>::aio_update(uint64_t snap_id, uint64_t start_object_no,
322 uint64_t end_object_no, uint8_t new_state,
323 const boost::optional<uint8_t> &current_state,
324 const ZTracer::Trace &parent_trace,
325 bool ignore_enoent, Context *on_finish) {
326 ceph_assert(ceph_mutex_is_locked(m_image_ctx.image_lock));
327 ceph_assert((m_image_ctx.features & RBD_FEATURE_OBJECT_MAP) != 0);
328 ceph_assert(m_image_ctx.image_watcher != nullptr);
329 ceph_assert(m_image_ctx.exclusive_lock == nullptr ||
330 m_image_ctx.exclusive_lock->is_lock_owner());
331 ceph_assert(start_object_no < end_object_no);
332
333 CephContext *cct = m_image_ctx.cct;
334 ldout(cct, 20) << "start=" << start_object_no << ", "
335 << "end=" << end_object_no << ", "
336 << (current_state ?
337 stringify(static_cast<uint32_t>(*current_state)) : "")
338 << "->" << static_cast<uint32_t>(new_state) << dendl;
339 if (snap_id == CEPH_NOSNAP) {
340 ceph_assert(ceph_mutex_is_wlocked(m_lock));
341 end_object_no = std::min(end_object_no, m_object_map.size());
342 if (start_object_no >= end_object_no) {
343 ldout(cct, 20) << "skipping update of invalid object map" << dendl;
344 m_image_ctx.op_work_queue->queue(on_finish, 0);
345 return;
346 }
347
348 auto it = m_object_map.begin() + start_object_no;
349 auto end_it = m_object_map.begin() + end_object_no;
350 for (; it != end_it; ++it) {
351 if (update_required(it, new_state)) {
352 break;
353 }
354 }
355 if (it == end_it) {
356 ldout(cct, 20) << "object map update not required" << dendl;
357 m_image_ctx.op_work_queue->queue(on_finish, 0);
358 return;
359 }
360 }
361
362 auto req = object_map::UpdateRequest<I>::create(
363 m_image_ctx, &m_lock, &m_object_map, snap_id, start_object_no,
364 end_object_no, new_state, current_state, parent_trace, ignore_enoent,
365 on_finish);
366 req->send();
367 }
368
369 } // namespace librbd
370
371 template class librbd::ObjectMap<librbd::ImageCtx>;
372