summaryrefslogtreecommitdiffstats
path: root/src/librbd/object_map/RefreshRequest.cc
blob: c4fc2539c05dd822674a2f45bf8c0d2566297b46 (plain)
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
// -*- mode:C; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "librbd/object_map/RefreshRequest.h"
#include "cls/lock/cls_lock_client.h"
#include "common/dout.h"
#include "common/errno.h"
#include "librbd/ImageCtx.h"
#include "librbd/ObjectMap.h"
#include "librbd/object_map/InvalidateRequest.h"
#include "librbd/object_map/LockRequest.h"
#include "librbd/object_map/ResizeRequest.h"
#include "librbd/Utils.h"
#include "osdc/Striper.h"

#define dout_subsys ceph_subsys_rbd
#undef dout_prefix
#define dout_prefix *_dout << "librbd::object_map::RefreshRequest: "

namespace librbd {

using util::create_context_callback;
using util::create_rados_callback;

namespace object_map {

template <typename I>
RefreshRequest<I>::RefreshRequest(I &image_ctx, ceph::BitVector<2> *object_map,
                                  uint64_t snap_id, Context *on_finish)
  : m_image_ctx(image_ctx), m_object_map(object_map), m_snap_id(snap_id),
    m_on_finish(on_finish), m_object_count(0),
    m_truncate_on_disk_object_map(false) {
}

template <typename I>
void RefreshRequest<I>::send() {
  {
    RWLock::RLocker snap_locker(m_image_ctx.snap_lock);
    m_object_count = Striper::get_num_objects(
      m_image_ctx.layout, m_image_ctx.get_image_size(m_snap_id));
  }


  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 20) << this << " " << __func__ << ": "
                 << "object_count=" << m_object_count << dendl;
  send_lock();
}

template <typename I>
void RefreshRequest<I>::apply() {
  uint64_t num_objs;
  {
    RWLock::RLocker snap_locker(m_image_ctx.snap_lock);
    num_objs = Striper::get_num_objects(
      m_image_ctx.layout, m_image_ctx.get_image_size(m_snap_id));
  }
  ceph_assert(m_on_disk_object_map.size() >= num_objs);

  *m_object_map = m_on_disk_object_map;
}

template <typename I>
void RefreshRequest<I>::send_lock() {
  CephContext *cct = m_image_ctx.cct;
  if (m_object_count > cls::rbd::MAX_OBJECT_MAP_OBJECT_COUNT) {
    send_invalidate_and_close();
    return;
  } else if (m_snap_id != CEPH_NOSNAP) {
    send_load();
    return;
  }

  std::string oid(ObjectMap<>::object_map_name(m_image_ctx.id, m_snap_id));
  ldout(cct, 10) << this << " " << __func__ << ": oid=" << oid << dendl;

  using klass = RefreshRequest<I>;
  Context *ctx = create_context_callback<
    klass, &klass::handle_lock>(this);

  LockRequest<I> *req = LockRequest<I>::create(m_image_ctx, ctx);
  req->send();
}

template <typename I>
Context *RefreshRequest<I>::handle_lock(int *ret_val) {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 10) << this << " " << __func__ << dendl;

  ceph_assert(*ret_val == 0);
  send_load();
  return nullptr;
}

template <typename I>
void RefreshRequest<I>::send_load() {
  CephContext *cct = m_image_ctx.cct;
  std::string oid(ObjectMap<>::object_map_name(m_image_ctx.id, m_snap_id));
  ldout(cct, 10) << this << " " << __func__ << ": oid=" << oid << dendl;

  librados::ObjectReadOperation op;
  cls_client::object_map_load_start(&op);

  using klass = RefreshRequest<I>;
  m_out_bl.clear();
  librados::AioCompletion *rados_completion =
    create_rados_callback<klass, &klass::handle_load>(this);
  int r = m_image_ctx.md_ctx.aio_operate(oid, rados_completion, &op, &m_out_bl);
  ceph_assert(r == 0);
  rados_completion->release();
}

template <typename I>
Context *RefreshRequest<I>::handle_load(int *ret_val) {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 10) << this << " " << __func__ << ": r=" << *ret_val << dendl;

  if (*ret_val == 0) {
    auto bl_it = m_out_bl.cbegin();
    *ret_val = cls_client::object_map_load_finish(&bl_it,
                                                  &m_on_disk_object_map);
  }

  std::string oid(ObjectMap<>::object_map_name(m_image_ctx.id, m_snap_id));
  if (*ret_val == -EINVAL) {
     // object map is corrupt on-disk -- clear it and properly size it
     // so future IO can keep the object map in sync
    lderr(cct) << "object map corrupt on-disk: " << oid << dendl;
    m_truncate_on_disk_object_map = true;
    send_resize_invalidate();
    return nullptr;
  } else if (*ret_val < 0) {
    lderr(cct) << "failed to load object map: " << oid << dendl;
    send_invalidate();
    return nullptr;
  }

  if (m_on_disk_object_map.size() < m_object_count) {
    lderr(cct) << "object map smaller than current object count: "
               << m_on_disk_object_map.size() << " != "
               << m_object_count << dendl;
    send_resize_invalidate();
    return nullptr;
  }

  ldout(cct, 20) << "refreshed object map: num_objs="
                 << m_on_disk_object_map.size() << dendl;
  if (m_on_disk_object_map.size() > m_object_count) {
    // resize op might have been interrupted
    ldout(cct, 1) << "object map larger than current object count: "
                  << m_on_disk_object_map.size() << " != "
                  << m_object_count << dendl;
  }

  apply();
  return m_on_finish;
}

template <typename I>
void RefreshRequest<I>::send_invalidate() {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 10) << this << " " << __func__ << dendl;

  m_on_disk_object_map.clear();
  object_map::ResizeRequest::resize(&m_on_disk_object_map, m_object_count,
                                    OBJECT_EXISTS);

  using klass = RefreshRequest<I>;
  Context *ctx = create_context_callback<
    klass, &klass::handle_invalidate>(this);
  InvalidateRequest<I> *req = InvalidateRequest<I>::create(
    m_image_ctx, m_snap_id, true, ctx);

  RWLock::RLocker owner_locker(m_image_ctx.owner_lock);
  RWLock::WLocker snap_locker(m_image_ctx.snap_lock);
  req->send();
}

template <typename I>
Context *RefreshRequest<I>::handle_invalidate(int *ret_val) {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 10) << this << " " << __func__ << ": r=" << *ret_val << dendl;

  if (*ret_val < 0) {
    lderr(cct) << "failed to invalidate object map: " << cpp_strerror(*ret_val)
               << dendl;
  }

  apply();
  return m_on_finish;
}

template <typename I>
void RefreshRequest<I>::send_resize_invalidate() {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 10) << this << " " << __func__ << dendl;

  m_on_disk_object_map.clear();
  object_map::ResizeRequest::resize(&m_on_disk_object_map, m_object_count,
                                    OBJECT_EXISTS);

  using klass = RefreshRequest<I>;
  Context *ctx = create_context_callback<
    klass, &klass::handle_resize_invalidate>(this);
  InvalidateRequest<I> *req = InvalidateRequest<I>::create(
    m_image_ctx, m_snap_id, true, ctx);

  RWLock::RLocker owner_locker(m_image_ctx.owner_lock);
  RWLock::WLocker snap_locker(m_image_ctx.snap_lock);
  req->send();
}

template <typename I>
Context *RefreshRequest<I>::handle_resize_invalidate(int *ret_val) {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 10) << this << " " << __func__ << ": r=" << *ret_val << dendl;

  if (*ret_val < 0) {
    lderr(cct) << "failed to invalidate object map: " << cpp_strerror(*ret_val)
               << dendl;
    apply();
    return m_on_finish;
  }

  send_resize();
  return nullptr;
}

template <typename I>
void RefreshRequest<I>::send_resize() {
  CephContext *cct = m_image_ctx.cct;
  std::string oid(ObjectMap<>::object_map_name(m_image_ctx.id, m_snap_id));
  ldout(cct, 10) << this << " " << __func__ << ": oid=" << oid << dendl;

  librados::ObjectWriteOperation op;
  if (m_snap_id == CEPH_NOSNAP) {
    rados::cls::lock::assert_locked(&op, RBD_LOCK_NAME, LOCK_EXCLUSIVE, "", "");
  }
  if (m_truncate_on_disk_object_map) {
    op.truncate(0);
  }
  cls_client::object_map_resize(&op, m_object_count, OBJECT_NONEXISTENT);

  using klass = RefreshRequest<I>;
  librados::AioCompletion *rados_completion =
    create_rados_callback<klass, &klass::handle_resize>(this);
  int r = m_image_ctx.md_ctx.aio_operate(oid, rados_completion, &op);
  ceph_assert(r == 0);
  rados_completion->release();
}

template <typename I>
Context *RefreshRequest<I>::handle_resize(int *ret_val) {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 10) << this << " " << __func__ << ": r=" << *ret_val << dendl;

  if (*ret_val < 0) {
    lderr(cct) << "failed to adjust object map size: " << cpp_strerror(*ret_val)
               << dendl;
    *ret_val = 0;
  }

  apply();
  return m_on_finish;
}

template <typename I>
void RefreshRequest<I>::send_invalidate_and_close() {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 10) << this << " " << __func__ << dendl;

  using klass = RefreshRequest<I>;
  Context *ctx = create_context_callback<
    klass, &klass::handle_invalidate_and_close>(this);
  InvalidateRequest<I> *req = InvalidateRequest<I>::create(
    m_image_ctx, m_snap_id, false, ctx);

  lderr(cct) << "object map too large: " << m_object_count << dendl;
  RWLock::RLocker owner_locker(m_image_ctx.owner_lock);
  RWLock::WLocker snap_locker(m_image_ctx.snap_lock);
  req->send();
}

template <typename I>
Context *RefreshRequest<I>::handle_invalidate_and_close(int *ret_val) {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 10) << this << " " << __func__ << ": r=" << *ret_val << dendl;

  if (*ret_val < 0) {
    lderr(cct) << "failed to invalidate object map: " << cpp_strerror(*ret_val)
               << dendl;
  } else {
    *ret_val = -EFBIG;
  }

  m_object_map->clear();
  return m_on_finish;
}

} // namespace object_map
} // namespace librbd

template class librbd::object_map::RefreshRequest<librbd::ImageCtx>;