summaryrefslogtreecommitdiffstats
path: root/src/librbd/operation/RebuildObjectMapRequest.cc
blob: 5deb182e5602acc021e6eff1c422e7c51016b6ea (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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "librbd/operation/RebuildObjectMapRequest.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/internal.h"
#include "librbd/ObjectMap.h"
#include "librbd/operation/ResizeRequest.h"
#include "librbd/operation/TrimRequest.h"
#include "librbd/operation/ObjectMapIterate.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::RebuildObjectMapRequest: "

namespace librbd {
namespace operation {

using util::create_context_callback;

template <typename I>
void RebuildObjectMapRequest<I>::send() {
  send_resize_object_map();
}

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

  std::shared_lock owner_lock{m_image_ctx.owner_lock};
  switch (m_state) {
  case STATE_RESIZE_OBJECT_MAP:
    ldout(cct, 5) << "RESIZE_OBJECT_MAP" << dendl;
    if (r == -ESTALE && !m_attempted_trim) {
      // objects are still flagged as in-use -- delete them
      m_attempted_trim = true;
      send_trim_image();
      return false;
    } else if (r == 0) {
      send_verify_objects();
    }
    break;

  case STATE_TRIM_IMAGE:
    ldout(cct, 5) << "TRIM_IMAGE" << dendl;
    if (r == 0) {
      send_resize_object_map();
    }
    break;

  case STATE_VERIFY_OBJECTS:
    ldout(cct, 5) << "VERIFY_OBJECTS" << dendl;
    if (r == 0) {
      send_save_object_map();
    }
    break;

  case STATE_SAVE_OBJECT_MAP:
    ldout(cct, 5) << "SAVE_OBJECT_MAP" << dendl;
    if (r == 0) {
      send_update_header();
    }
    break;
  case STATE_UPDATE_HEADER:
    ldout(cct, 5) << "UPDATE_HEADER" << dendl;
    if (r == 0) {
      return true;
    }
    break;

  default:
    ceph_abort();
    break;
  }

  if (r == -ERESTART) {
    ldout(cct, 5) << "rebuild object map operation interrupted" << dendl;
    return true;
  } else if (r < 0) {
    lderr(cct) << "rebuild object map encountered an error: " << cpp_strerror(r)
               << dendl;
    return true;
  }
  return false;
}

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

  m_image_ctx.image_lock.lock_shared();
  ceph_assert(m_image_ctx.object_map != nullptr);

  uint64_t size = get_image_size();
  uint64_t num_objects = Striper::get_num_objects(m_image_ctx.layout, size);

  if (m_image_ctx.object_map->size() == num_objects) {
    m_image_ctx.image_lock.unlock_shared();
    send_verify_objects();
    return;
  }

  ldout(cct, 5) << this << " send_resize_object_map" << dendl;
  m_state = STATE_RESIZE_OBJECT_MAP;

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

  m_image_ctx.object_map->aio_resize(size, OBJECT_NONEXISTENT,
                                     this->create_callback_context());
  m_image_ctx.image_lock.unlock_shared();
}

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

  std::shared_lock l{m_image_ctx.owner_lock};

  // should have been canceled prior to releasing lock
  ceph_assert(m_image_ctx.exclusive_lock == nullptr ||
              m_image_ctx.exclusive_lock->is_lock_owner());
  ldout(cct, 5) << this << " send_trim_image" << dendl;
  m_state = STATE_TRIM_IMAGE;

  uint64_t new_size;
  uint64_t orig_size;
  {
    std::shared_lock l{m_image_ctx.image_lock};
    ceph_assert(m_image_ctx.object_map != nullptr);

    new_size = get_image_size();
    orig_size = m_image_ctx.get_object_size() *
                m_image_ctx.object_map->size();
  }
  TrimRequest<I> *req = TrimRequest<I>::create(m_image_ctx,
                                               this->create_callback_context(),
                                               orig_size, new_size, m_prog_ctx);
  req->send();
}

template <typename I>
bool update_object_map(I& image_ctx, uint64_t object_no, uint8_t current_state,
		      uint8_t new_state) {
  CephContext *cct = image_ctx.cct;
  uint64_t snap_id = image_ctx.snap_id;

  current_state = (*image_ctx.object_map)[object_no];
  if (current_state == OBJECT_EXISTS && new_state == OBJECT_NONEXISTENT &&
      snap_id == CEPH_NOSNAP) {
    // might be writing object to OSD concurrently
    new_state = current_state;
  }

  if (new_state != current_state) {
    ldout(cct, 15) << image_ctx.get_object_name(object_no)
                   << " rebuild updating object map "
                   << static_cast<uint32_t>(current_state) << "->"
                   << static_cast<uint32_t>(new_state) << dendl;
    image_ctx.object_map->set_state(object_no, new_state, current_state);
  }
  return false;
}

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

  m_state = STATE_VERIFY_OBJECTS;
  ldout(cct, 5) << this << " send_verify_objects" << dendl;

  ObjectMapIterateRequest<I> *req =
    new ObjectMapIterateRequest<I>(m_image_ctx,
				   this->create_callback_context(),
				   m_prog_ctx, update_object_map);

  req->send();
}

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

  ldout(cct, 5) << this << " send_save_object_map" << dendl;
  m_state = STATE_SAVE_OBJECT_MAP;

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

  std::shared_lock image_locker{m_image_ctx.image_lock};
  ceph_assert(m_image_ctx.object_map != nullptr);
  m_image_ctx.object_map->aio_save(this->create_callback_context());
}

template <typename I>
void RebuildObjectMapRequest<I>::send_update_header() {
  ceph_assert(ceph_mutex_is_locked(m_image_ctx.owner_lock));

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

  ldout(m_image_ctx.cct, 5) << this << " send_update_header" << dendl;
  m_state = STATE_UPDATE_HEADER;

  librados::ObjectWriteOperation op;

  uint64_t flags = RBD_FLAG_OBJECT_MAP_INVALID | RBD_FLAG_FAST_DIFF_INVALID;
  cls_client::set_flags(&op, m_image_ctx.snap_id, 0, flags);

  librados::AioCompletion *comp = this->create_callback_completion();
  int r = m_image_ctx.md_ctx.aio_operate(m_image_ctx.header_oid, comp, &op);
  ceph_assert(r == 0);
  comp->release();

  std::unique_lock image_locker{m_image_ctx.image_lock};
  m_image_ctx.update_flags(m_image_ctx.snap_id, flags, false);
}

template <typename I>
uint64_t RebuildObjectMapRequest<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);
}

} // namespace operation
} // namespace librbd

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