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

#include "librbd/object_map/SnapshotRemoveRequest.h"
#include "common/dout.h"
#include "common/errno.h"
#include "librbd/ImageCtx.h"
#include "librbd/ObjectMap.h"
#include "librbd/object_map/InvalidateRequest.h"
#include "cls/lock/cls_lock_client.h"

#define dout_subsys ceph_subsys_rbd
#undef dout_prefix
#define dout_prefix *_dout << "librbd::object_map::SnapshotRemoveRequest: " \
                           << this << " " << __func__ << ": "

namespace librbd {
namespace object_map {

void SnapshotRemoveRequest::send() {
  ceph_assert(m_image_ctx.owner_lock.is_locked());
  ceph_assert(m_image_ctx.snap_lock.is_wlocked());

  if ((m_image_ctx.features & RBD_FEATURE_FAST_DIFF) != 0) {
    int r = m_image_ctx.get_flags(m_snap_id, &m_flags);
    ceph_assert(r == 0);

    compute_next_snap_id();
    load_map();
  } else {
    remove_map();
  }
}

void SnapshotRemoveRequest::load_map() {
  CephContext *cct = m_image_ctx.cct;
  std::string snap_oid(ObjectMap<>::object_map_name(m_image_ctx.id, m_snap_id));
  ldout(cct, 5) << "snap_oid=" << snap_oid << dendl;

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

  auto rados_completion = librbd::util::create_rados_callback<
    SnapshotRemoveRequest, &SnapshotRemoveRequest::handle_load_map>(this);
  int r = m_image_ctx.md_ctx.aio_operate(snap_oid, rados_completion, &op,
                                         &m_out_bl);
  ceph_assert(r == 0);
  rados_completion->release();
}

void SnapshotRemoveRequest::handle_load_map(int r) {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 5) << "r=" << r << dendl;

  if (r == 0) {
    auto it = m_out_bl.cbegin();
    r = cls_client::object_map_load_finish(&it, &m_snap_object_map);
  }
  if (r == -ENOENT) {
    // implies we have already deleted this snapshot and handled the
    // necessary fast-diff cleanup
    complete(0);
    return;
  } else if (r < 0) {
    std::string oid(ObjectMap<>::object_map_name(m_image_ctx.id, m_snap_id));
    lderr(cct) << "failed to load object map " << oid << ": "
               << cpp_strerror(r) << dendl;

    RWLock::RLocker owner_locker(m_image_ctx.owner_lock);
    RWLock::WLocker snap_locker(m_image_ctx.snap_lock);
    invalidate_next_map();
    return;
  }

  remove_snapshot();
}

void SnapshotRemoveRequest::remove_snapshot() {
  if ((m_flags & RBD_FLAG_OBJECT_MAP_INVALID) != 0) {
    // snapshot object map exists on disk but is invalid. cannot clean fast-diff
    // on next snapshot if current snapshot was invalid.
    RWLock::RLocker owner_locker(m_image_ctx.owner_lock);
    RWLock::WLocker snap_locker(m_image_ctx.snap_lock);
    invalidate_next_map();
    return;
  }

  CephContext *cct = m_image_ctx.cct;
  std::string oid(ObjectMap<>::object_map_name(m_image_ctx.id, m_next_snap_id));
  ldout(cct, 5) << "oid=" << oid << dendl;

  librados::ObjectWriteOperation op;
  if (m_next_snap_id == CEPH_NOSNAP) {
    rados::cls::lock::assert_locked(&op, RBD_LOCK_NAME, LOCK_EXCLUSIVE, "", "");
  }
  cls_client::object_map_snap_remove(&op, m_snap_object_map);

  auto rados_completion = librbd::util::create_rados_callback<
    SnapshotRemoveRequest,
    &SnapshotRemoveRequest::handle_remove_snapshot>(this);
  int r = m_image_ctx.md_ctx.aio_operate(oid, rados_completion, &op);
  ceph_assert(r == 0);
  rados_completion->release();
}

void SnapshotRemoveRequest::handle_remove_snapshot(int r) {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 5) << "r=" << r << dendl;
  if (r < 0 && r != -ENOENT) {
    std::string oid(ObjectMap<>::object_map_name(m_image_ctx.id,
                                                 m_next_snap_id));
    lderr(cct) << "failed to remove object map snapshot " << oid << ": "
               << cpp_strerror(r) << dendl;

    RWLock::RLocker owner_locker(m_image_ctx.owner_lock);
    RWLock::WLocker snap_locker(m_image_ctx.snap_lock);
    invalidate_next_map();
    return;
  }

  RWLock::RLocker snap_locker(m_image_ctx.snap_lock);
  update_object_map();
  remove_map();
}

void SnapshotRemoveRequest::invalidate_next_map() {
  ceph_assert(m_image_ctx.owner_lock.is_locked());
  ceph_assert(m_image_ctx.snap_lock.is_wlocked());

  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 5) << dendl;

  auto ctx = librbd::util::create_context_callback<
    SnapshotRemoveRequest,
    &SnapshotRemoveRequest::handle_invalidate_next_map>(this);
  InvalidateRequest<> *req = new InvalidateRequest<>(m_image_ctx,
                                                     m_next_snap_id, true, ctx);
  req->send();
}

void SnapshotRemoveRequest::handle_invalidate_next_map(int r) {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 5) << "r=" << r << dendl;

  if (r < 0) {
    std::string oid(ObjectMap<>::object_map_name(m_image_ctx.id,
                                                 m_next_snap_id));
    lderr(cct) << "failed to invalidate object map " << oid << ": "
               << cpp_strerror(r) << dendl;
    complete(r);
    return;
  }

  remove_map();
}

void SnapshotRemoveRequest::remove_map() {
  CephContext *cct = m_image_ctx.cct;
  std::string oid(ObjectMap<>::object_map_name(m_image_ctx.id, m_snap_id));
  ldout(cct, 5) << "oid=" << oid << dendl;

  librados::ObjectWriteOperation op;
  op.remove();

  auto rados_completion = librbd::util::create_rados_callback<
    SnapshotRemoveRequest, &SnapshotRemoveRequest::handle_remove_map>(this);
  int r = m_image_ctx.md_ctx.aio_operate(oid, rados_completion, &op);
  ceph_assert(r == 0);
  rados_completion->release();
}

void SnapshotRemoveRequest::handle_remove_map(int r) {
  CephContext *cct = m_image_ctx.cct;
  ldout(cct, 5) << "r=" << r << dendl;

  if (r < 0 && r != -ENOENT) {
    std::string oid(ObjectMap<>::object_map_name(m_image_ctx.id, m_snap_id));
    lderr(cct) << "failed to remove object map " << oid << ": "
               << cpp_strerror(r) << dendl;
    complete(r);
    return;
  }

  complete(0);
}

void SnapshotRemoveRequest::compute_next_snap_id() {
  ceph_assert(m_image_ctx.snap_lock.is_locked());

  m_next_snap_id = CEPH_NOSNAP;
  std::map<librados::snap_t, SnapInfo>::const_iterator it =
    m_image_ctx.snap_info.find(m_snap_id);
  ceph_assert(it != m_image_ctx.snap_info.end());

  ++it;
  if (it != m_image_ctx.snap_info.end()) {
    m_next_snap_id = it->first;
  }
}

void SnapshotRemoveRequest::update_object_map() {
  assert(m_image_ctx.snap_lock.is_locked());
  RWLock::WLocker object_map_locker(m_image_ctx.object_map_lock);
  if (m_next_snap_id == m_image_ctx.snap_id && m_next_snap_id == CEPH_NOSNAP) {
    CephContext *cct = m_image_ctx.cct;
    ldout(cct, 5) << dendl;

    auto it = m_object_map.begin();
    auto end_it = m_object_map.end();
    auto snap_it = m_snap_object_map.begin();
    uint64_t i = 0;
    for (; it != end_it; ++it) {
      if (*it == OBJECT_EXISTS_CLEAN &&
          (i >= m_snap_object_map.size() ||
           *snap_it == OBJECT_EXISTS)) {
        *it = OBJECT_EXISTS;
      }
      if (i < m_snap_object_map.size()) {
        ++snap_it;
      }
      ++i;
    }
  }
}

} // namespace object_map
} // namespace librbd