summaryrefslogtreecommitdiffstats
path: root/src/librbd/operation/ObjectMapIterate.cc
blob: 50db3df851fb7ec178af32c662b80aaf70dc3994 (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
304
305
306
307
308
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "librbd/operation/ObjectMapIterate.h"
#include "common/dout.h"
#include "common/errno.h"
#include "osdc/Striper.h"
#include "librbd/AsyncObjectThrottle.h"
#include "librbd/ExclusiveLock.h"
#include "librbd/ImageCtx.h"
#include "librbd/ImageWatcher.h"
#include "librbd/internal.h"
#include "librbd/ObjectMap.h"
#include "librbd/operation/ResizeRequest.h"
#include "librbd/object_map/InvalidateRequest.h"
#include "librbd/Utils.h"
#include <boost/lambda/bind.hpp>
#include <boost/lambda/construct.hpp>

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

namespace librbd {
namespace operation {

namespace {

template <typename I>
class C_VerifyObjectCallback : public C_AsyncObjectThrottle<I> {
public:
  C_VerifyObjectCallback(AsyncObjectThrottle<I> &throttle, I *image_ctx,
			 uint64_t snap_id, uint64_t object_no,
			 ObjectIterateWork<I> handle_mismatch,
			 std::atomic_flag *invalidate)
    : C_AsyncObjectThrottle<I>(throttle, *image_ctx),
    m_snap_id(snap_id), m_object_no(object_no),
    m_oid(image_ctx->get_object_name(m_object_no)),
    m_handle_mismatch(handle_mismatch),
    m_invalidate(invalidate)
  {
    m_io_ctx.dup(image_ctx->data_ctx);
    m_io_ctx.snap_set_read(CEPH_SNAPDIR);
  }

  void complete(int r) override {
    I &image_ctx = this->m_image_ctx;
    if (should_complete(r)) {
      ldout(image_ctx.cct, 20) << m_oid << " C_VerifyObjectCallback completed "
			       << dendl;
      m_io_ctx.close();

      this->finish(r);
      delete this;
    }
  }

  int send() override {
    send_list_snaps();
    return 0;
  }

private:
  librados::IoCtx m_io_ctx;
  uint64_t m_snap_id;
  uint64_t m_object_no;
  std::string m_oid;
  ObjectIterateWork<I> m_handle_mismatch;
  std::atomic_flag *m_invalidate;

  librados::snap_set_t m_snap_set;
  int m_snap_list_ret = 0;

  bool should_complete(int r) {
    I &image_ctx = this->m_image_ctx;
    CephContext *cct = image_ctx.cct;
    if (r == 0) {
      r = m_snap_list_ret;
    }
    if (r < 0 && r != -ENOENT) {
      lderr(cct) << m_oid << " C_VerifyObjectCallback::should_complete: "
                 << "encountered an error: " << cpp_strerror(r) << dendl;
      return true;
    }

    ldout(cct, 20) << m_oid << " C_VerifyObjectCallback::should_complete: "
		   << " r="
                   << r << dendl;
    return object_map_action(get_object_state());
  }

  void send_list_snaps() {
    I &image_ctx = this->m_image_ctx;
    ceph_assert(ceph_mutex_is_locked(image_ctx.owner_lock));
    ldout(image_ctx.cct, 5) << m_oid
			    << " C_VerifyObjectCallback::send_list_snaps"
                            << dendl;

    librados::ObjectReadOperation op;
    op.list_snaps(&m_snap_set, &m_snap_list_ret);

    librados::AioCompletion *comp = util::create_rados_callback(this);
    int r = m_io_ctx.aio_operate(m_oid, comp, &op, NULL);
    ceph_assert(r == 0);
    comp->release();
  }

  uint8_t get_object_state() {
    I &image_ctx = this->m_image_ctx;
    std::shared_lock image_locker{image_ctx.image_lock};
    for (std::vector<librados::clone_info_t>::const_iterator r =
           m_snap_set.clones.begin(); r != m_snap_set.clones.end(); ++r) {
      librados::snap_t from_snap_id;
      librados::snap_t to_snap_id;
      if (r->cloneid == librados::SNAP_HEAD) {
        from_snap_id = next_valid_snap_id(m_snap_set.seq + 1);
        to_snap_id = librados::SNAP_HEAD;
      } else {
        from_snap_id = next_valid_snap_id(r->snaps[0]);
        to_snap_id = r->snaps[r->snaps.size()-1];
      }

      if (to_snap_id < m_snap_id) {
        continue;
      } else if (m_snap_id < from_snap_id) {
        break;
      }

      if ((image_ctx.features & RBD_FEATURE_FAST_DIFF) != 0 &&
          from_snap_id != m_snap_id) {
        return OBJECT_EXISTS_CLEAN;
      }
      return OBJECT_EXISTS;
    }
    return OBJECT_NONEXISTENT;
  }

  uint64_t next_valid_snap_id(uint64_t snap_id) {
    I &image_ctx = this->m_image_ctx;
    ceph_assert(ceph_mutex_is_locked(image_ctx.image_lock));

    std::map<librados::snap_t, SnapInfo>::iterator it =
      image_ctx.snap_info.lower_bound(snap_id);
    if (it == image_ctx.snap_info.end()) {
      return CEPH_NOSNAP;
    }
    return it->first;
  }

  bool object_map_action(uint8_t new_state) {
    I &image_ctx = this->m_image_ctx;
    CephContext *cct = image_ctx.cct;
    std::shared_lock owner_locker{image_ctx.owner_lock};

    // should have been canceled prior to releasing lock
    ceph_assert(image_ctx.exclusive_lock == nullptr ||
                image_ctx.exclusive_lock->is_lock_owner());

    std::shared_lock image_locker{image_ctx.image_lock};
    ceph_assert(image_ctx.object_map != nullptr);

    uint8_t state = (*image_ctx.object_map)[m_object_no];
    ldout(cct, 10) << "C_VerifyObjectCallback::object_map_action"
		   << " object " << image_ctx.get_object_name(m_object_no)
		   << " state " << (int)state
		   << " new_state " << (int)new_state << dendl;

    if (state != new_state) {
      int r = 0;

      ceph_assert(m_handle_mismatch);
      r = m_handle_mismatch(image_ctx, m_object_no, state, new_state);
      if (r) {
	lderr(cct) << "object map error: object "
		   << image_ctx.get_object_name(m_object_no)
		   << " marked as " << (int)state << ", but should be "
		   << (int)new_state << dendl;
	m_invalidate->test_and_set();
      } else {
	ldout(cct, 1) << "object map inconsistent: object "
		   << image_ctx.get_object_name(m_object_no)
		   << " marked as " << (int)state << ", but should be "
		   << (int)new_state << dendl;
      }
    }

    return true;
  }
};

} // anonymous namespace

template <typename I>
void ObjectMapIterateRequest<I>::send() {
  if (!m_image_ctx.data_ctx.is_valid()) {
    this->async_complete(-ENODEV);
    return;
  }

  send_verify_objects();
}

template <typename I>
bool ObjectMapIterateRequest<I>::should_complete(int r) {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 5) << this << " should_complete: " << " r=" << r << dendl;

  if (r == -ENODEV) {
    lderr(cct) << "missing data pool" << dendl;
    return true;
  }

  if (r < 0) {
    lderr(cct) << "object map operation encountered an error: "
	       << cpp_strerror(r) << dendl;
  }

  std::shared_lock owner_lock{m_image_ctx.owner_lock};
  switch (m_state) {
  case STATE_VERIFY_OBJECTS:
    if (m_invalidate.test_and_set()) {
      send_invalidate_object_map();
      return false;
    } else if (r == 0) {
      return true;
    }
    break;

  case STATE_INVALIDATE_OBJECT_MAP:
    if (r == 0) {
      return true;
    }
    break;

  default:
    ceph_abort();
    break;
  }

  if (r < 0) {
    return true;
  }

  return false;
}

template <typename I>
void ObjectMapIterateRequest<I>::send_verify_objects() {
  ceph_assert(ceph_mutex_is_locked(m_image_ctx.owner_lock));
  CephContext *cct = m_image_ctx.cct;

  uint64_t snap_id;
  uint64_t num_objects;
  {
    std::shared_lock l{m_image_ctx.image_lock};
    snap_id = m_image_ctx.snap_id;
    num_objects = Striper::get_num_objects(m_image_ctx.layout,
                                           m_image_ctx.get_image_size(snap_id));
  }
  ldout(cct, 5) << this << " send_verify_objects" << dendl;

  m_state = STATE_VERIFY_OBJECTS;

  typename AsyncObjectThrottle<I>::ContextFactory context_factory(
    boost::lambda::bind(boost::lambda::new_ptr<C_VerifyObjectCallback<I> >(),
			boost::lambda::_1, &m_image_ctx, snap_id,
			boost::lambda::_2, m_handle_mismatch, &m_invalidate));
  AsyncObjectThrottle<I> *throttle = new AsyncObjectThrottle<I>(
    this, m_image_ctx, context_factory, this->create_callback_context(),
    &m_prog_ctx, 0, num_objects);
  throttle->start_ops(
    m_image_ctx.config.template get_val<uint64_t>("rbd_concurrent_management_ops"));
}

template <typename I>
uint64_t ObjectMapIterateRequest<I>::get_image_size() const {
  ceph_assert(ceph_mutex_is_locked(m_image_ctx.image_lock));
  if (m_image_ctx.snap_id == CEPH_NOSNAP) {
    if (!m_image_ctx.resize_reqs.empty()) {
      return m_image_ctx.resize_reqs.front()->get_image_size();
    } else {
      return m_image_ctx.size;
    }
  }
  return  m_image_ctx.get_image_size(m_image_ctx.snap_id);
}

template <typename I>
void ObjectMapIterateRequest<I>::send_invalidate_object_map() {
  CephContext *cct = m_image_ctx.cct;

  ldout(cct, 5) << this << " send_invalidate_object_map" << dendl;
  m_state = STATE_INVALIDATE_OBJECT_MAP;

  object_map::InvalidateRequest<I>*req =
    object_map::InvalidateRequest<I>::create(m_image_ctx, m_image_ctx.snap_id,
					     true,
					     this->create_callback_context());

  ceph_assert(ceph_mutex_is_locked(m_image_ctx.owner_lock));
  std::unique_lock image_locker{m_image_ctx.image_lock};
  req->send();
}

} // namespace operation
} // namespace librbd

template class librbd::operation::ObjectMapIterateRequest<librbd::ImageCtx>;