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