]> git.proxmox.com Git - ceph.git/blob - ceph/src/librbd/operation/SnapshotUnprotectRequest.cc
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / librbd / operation / SnapshotUnprotectRequest.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include "librbd/operation/SnapshotUnprotectRequest.h"
5 #include "include/rados/librados.hpp"
6 #include "include/stringify.h"
7 #include "common/dout.h"
8 #include "common/errno.h"
9 #include "librbd/AsyncObjectThrottle.h"
10 #include "librbd/ImageCtx.h"
11 #include "librbd/internal.h"
12 #include "librbd/Types.h"
13 #include "librbd/Utils.h"
14 #include <list>
15 #include <set>
16 #include <vector>
17 #include <boost/lambda/bind.hpp>
18 #include <boost/lambda/construct.hpp>
19
20 #define dout_subsys ceph_subsys_rbd
21 #undef dout_prefix
22 #define dout_prefix *_dout << "librbd::SnapshotUnprotectRequest: "
23
24 namespace librbd {
25 namespace operation {
26
27 namespace {
28
29 typedef std::pair<int64_t, std::string> Pool;
30 typedef std::vector<Pool> Pools;
31
32 template <typename I>
33 std::ostream& operator<<(std::ostream& os,
34 const typename SnapshotUnprotectRequest<I>::State& state) {
35 switch(state) {
36 case SnapshotUnprotectRequest<I>::STATE_UNPROTECT_SNAP_START:
37 os << "UNPROTECT_SNAP_START";
38 break;
39 case SnapshotUnprotectRequest<I>::STATE_SCAN_POOL_CHILDREN:
40 os << "SCAN_POOL_CHILDREN";
41 break;
42 case SnapshotUnprotectRequest<I>::STATE_UNPROTECT_SNAP_FINISH:
43 os << "UNPROTECT_SNAP_FINISH";
44 break;
45 case SnapshotUnprotectRequest<I>::STATE_UNPROTECT_SNAP_ROLLBACK:
46 os << "UNPROTECT_SNAP_ROLLBACK";
47 break;
48 default:
49 os << "UNKNOWN (" << static_cast<uint32_t>(state) << ")";
50 break;
51 }
52 return os;
53 }
54
55 template <typename I>
56 class C_ScanPoolChildren : public C_AsyncObjectThrottle<I> {
57 public:
58 C_ScanPoolChildren(AsyncObjectThrottle<I> &throttle, I *image_ctx,
59 const ParentSpec &pspec, const Pools &pools,
60 size_t pool_idx)
61 : C_AsyncObjectThrottle<I>(throttle, *image_ctx), m_pspec(pspec),
62 m_pool(pools[pool_idx]) {
63 }
64
65 int send() override {
66 I &image_ctx = this->m_image_ctx;
67 assert(image_ctx.owner_lock.is_locked());
68
69 CephContext *cct = image_ctx.cct;
70 ldout(cct, 10) << this << " scanning pool '" << m_pool.second << "'"
71 << dendl;
72
73 librados::Rados rados(image_ctx.md_ctx);
74 int64_t base_tier;
75 int r = rados.pool_get_base_tier(m_pool.first, &base_tier);
76 if (r == -ENOENT) {
77 ldout(cct, 1) << "pool '" << m_pool.second << "' no longer exists"
78 << dendl;
79 return 1;
80 } else if (r < 0) {
81 lderr(cct) << "error retrieving base tier for pool '"
82 << m_pool.second << "'" << dendl;
83 return r;
84 }
85 if (m_pool.first != base_tier) {
86 // pool is a cache; skip it
87 return 1;
88 }
89
90 r = rados.ioctx_create2(m_pool.first, m_pool_ioctx);
91 if (r == -ENOENT) {
92 ldout(cct, 1) << "pool '" << m_pool.second << "' no longer exists"
93 << dendl;
94 return 1;
95 } else if (r < 0) {
96 lderr(cct) << "can't create ioctx for pool '" << m_pool.second
97 << "'" << dendl;
98 return r;
99 }
100
101 librados::ObjectReadOperation op;
102 cls_client::get_children_start(&op, m_pspec);
103
104 librados::AioCompletion *rados_completion =
105 util::create_rados_callback(this);
106 r = m_pool_ioctx.aio_operate(RBD_CHILDREN, rados_completion, &op,
107 &m_children_bl);
108 assert(r == 0);
109 rados_completion->release();
110 return 0;
111 }
112
113 protected:
114 void finish(int r) override {
115 I &image_ctx = this->m_image_ctx;
116 CephContext *cct = image_ctx.cct;
117
118 if (r == 0) {
119 bufferlist::iterator it = m_children_bl.begin();
120 r= cls_client::get_children_finish(&it, &m_children);
121 }
122
123 ldout(cct, 10) << this << " retrieved children: r=" << r << dendl;
124 if (r == -ENOENT) {
125 // no children -- proceed with unprotect
126 r = 0;
127 } else if (r < 0) {
128 lderr(cct) << "cannot get children for pool '" << m_pool.second << "'"
129 << dendl;
130 } else {
131 lderr(cct) << "cannot unprotect: at least " << m_children.size() << " "
132 << "child(ren) [" << joinify(m_children.begin(),
133 m_children.end(),
134 std::string(",")) << "] "
135 << "in pool '" << m_pool.second << "'" << dendl;
136 r = -EBUSY;
137 }
138 C_AsyncObjectThrottle<I>::finish(r);
139 }
140
141 private:
142 ParentSpec m_pspec;
143 Pool m_pool;
144
145 IoCtx m_pool_ioctx;
146 std::set<std::string> m_children;
147 bufferlist m_children_bl;
148 };
149
150 } // anonymous namespace
151
152 template <typename I>
153 SnapshotUnprotectRequest<I>::SnapshotUnprotectRequest(I &image_ctx,
154 Context *on_finish,
155 const cls::rbd::SnapshotNamespace &snap_namespace,
156 const std::string &snap_name)
157 : Request<I>(image_ctx, on_finish), m_snap_namespace(snap_namespace),
158 m_snap_name(snap_name), m_ret_val(0), m_snap_id(CEPH_NOSNAP) {
159 }
160
161 template <typename I>
162 void SnapshotUnprotectRequest<I>::send_op() {
163 send_unprotect_snap_start();
164 }
165
166 template <typename I>
167 bool SnapshotUnprotectRequest<I>::should_complete(int r) {
168 I &image_ctx = this->m_image_ctx;
169 CephContext *cct = image_ctx.cct;
170 ldout(cct, 5) << this << " " << __func__ << ": state=" << m_state << ", "
171 << "r=" << r << dendl;
172 if (r < 0) {
173 if (r == -EINVAL) {
174 ldout(cct, 1) << "snapshot is already unprotected" << dendl;
175 } else {
176 lderr(cct) << "encountered error: " << cpp_strerror(r) << dendl;
177 }
178 if (m_ret_val == 0) {
179 m_ret_val = r;
180 }
181 }
182
183 // use a different state machine once an error is encountered
184 if (m_ret_val < 0) {
185 return should_complete_error();
186 }
187
188 RWLock::RLocker owner_lock(image_ctx.owner_lock);
189 bool finished = false;
190 switch (m_state) {
191 case STATE_UNPROTECT_SNAP_START:
192 send_scan_pool_children();
193 break;
194 case STATE_SCAN_POOL_CHILDREN:
195 send_unprotect_snap_finish();
196 break;
197 case STATE_UNPROTECT_SNAP_FINISH:
198 finished = true;
199 break;
200 default:
201 assert(false);
202 break;
203 }
204 return finished;
205 }
206
207 template <typename I>
208 bool SnapshotUnprotectRequest<I>::should_complete_error() {
209 I &image_ctx = this->m_image_ctx;
210 RWLock::RLocker owner_locker(image_ctx.owner_lock);
211 CephContext *cct = image_ctx.cct;
212 lderr(cct) << this << " " << __func__ << ": "
213 << "ret_val=" << m_ret_val << dendl;
214
215 bool finished = true;
216 if (m_state == STATE_SCAN_POOL_CHILDREN ||
217 m_state == STATE_UNPROTECT_SNAP_FINISH) {
218 send_unprotect_snap_rollback();
219 finished = false;
220 }
221 return finished;
222 }
223
224 template <typename I>
225 void SnapshotUnprotectRequest<I>::send_unprotect_snap_start() {
226 I &image_ctx = this->m_image_ctx;
227 assert(image_ctx.owner_lock.is_locked());
228
229 CephContext *cct = image_ctx.cct;
230 ldout(cct, 5) << this << " " << __func__ << dendl;
231
232 m_state = STATE_UNPROTECT_SNAP_START;
233
234 int r = verify_and_send_unprotect_snap_start();
235 if (r < 0) {
236 this->async_complete(r);
237 return;
238 }
239 }
240
241 template <typename I>
242 void SnapshotUnprotectRequest<I>::send_scan_pool_children() {
243 I &image_ctx = this->m_image_ctx;
244 assert(image_ctx.owner_lock.is_locked());
245
246 CephContext *cct = image_ctx.cct;
247 ldout(cct, 5) << this << " " << __func__ << dendl;
248 m_state = STATE_SCAN_POOL_CHILDREN;
249
250 // search all pools for children depending on this snapshot
251 // TODO add async version of wait_for_latest_osdmap
252 librados::Rados rados(image_ctx.md_ctx);
253 rados.wait_for_latest_osdmap();
254
255 // protect against pools being renamed/deleted
256 std::list<Pool> pool_list;
257 rados.pool_list2(pool_list);
258
259 ParentSpec pspec(image_ctx.md_ctx.get_id(), image_ctx.id, m_snap_id);
260 Pools pools(pool_list.begin(), pool_list.end());
261
262 Context *ctx = this->create_callback_context();
263 typename AsyncObjectThrottle<I>::ContextFactory context_factory(
264 boost::lambda::bind(boost::lambda::new_ptr<C_ScanPoolChildren<I> >(),
265 boost::lambda::_1, &image_ctx, pspec, pools, boost::lambda::_2));
266 AsyncObjectThrottle<I> *throttle = new AsyncObjectThrottle<I>(
267 nullptr, image_ctx, context_factory, ctx, NULL, 0, pools.size());
268 throttle->start_ops(image_ctx.concurrent_management_ops);
269 }
270
271 template <typename I>
272 void SnapshotUnprotectRequest<I>::send_unprotect_snap_finish() {
273 I &image_ctx = this->m_image_ctx;
274 assert(image_ctx.owner_lock.is_locked());
275
276 CephContext *cct = image_ctx.cct;
277 ldout(cct, 5) << this << " " << __func__ << dendl;
278
279 m_state = STATE_UNPROTECT_SNAP_FINISH;
280
281 librados::ObjectWriteOperation op;
282 cls_client::set_protection_status(&op, m_snap_id,
283 RBD_PROTECTION_STATUS_UNPROTECTED);
284
285 librados::AioCompletion *comp = this->create_callback_completion();
286 int r = image_ctx.md_ctx.aio_operate(image_ctx.header_oid, comp, &op);
287 assert(r == 0);
288 comp->release();
289 }
290
291 template <typename I>
292 void SnapshotUnprotectRequest<I>::send_unprotect_snap_rollback() {
293 I &image_ctx = this->m_image_ctx;
294 assert(image_ctx.owner_lock.is_locked());
295
296 CephContext *cct = image_ctx.cct;
297 ldout(cct, 5) << this << " " << __func__ << dendl;
298
299 m_state = STATE_UNPROTECT_SNAP_ROLLBACK;
300
301 librados::ObjectWriteOperation op;
302 cls_client::set_protection_status(&op, m_snap_id,
303 RBD_PROTECTION_STATUS_PROTECTED);
304
305 librados::AioCompletion *comp = this->create_callback_completion();
306 int r = image_ctx.md_ctx.aio_operate(image_ctx.header_oid, comp, &op);
307 assert(r == 0);
308 comp->release();
309 }
310
311 template <typename I>
312 int SnapshotUnprotectRequest<I>::verify_and_send_unprotect_snap_start() {
313 I &image_ctx = this->m_image_ctx;
314 RWLock::RLocker md_locker(image_ctx.md_lock);
315 RWLock::RLocker snap_locker(image_ctx.snap_lock);
316
317 CephContext *cct = image_ctx.cct;
318 if ((image_ctx.features & RBD_FEATURE_LAYERING) == 0) {
319 lderr(cct) << "image must support layering" << dendl;
320 return -ENOSYS;
321 }
322
323 m_snap_id = image_ctx.get_snap_id(m_snap_namespace, m_snap_name);
324 if (m_snap_id == CEPH_NOSNAP) {
325 return -ENOENT;
326 }
327
328 bool is_unprotected;
329 int r = image_ctx.is_snap_unprotected(m_snap_id, &is_unprotected);
330 if (r < 0) {
331 return r;
332 }
333
334 if (is_unprotected) {
335 lderr(cct) << "snapshot is already unprotected" << dendl;
336 return -EINVAL;
337 }
338
339 librados::ObjectWriteOperation op;
340 cls_client::set_protection_status(&op, m_snap_id,
341 RBD_PROTECTION_STATUS_UNPROTECTING);
342
343 librados::AioCompletion *comp = this->create_callback_completion();
344 r = image_ctx.md_ctx.aio_operate(image_ctx.header_oid, comp, &op);
345 assert(r == 0);
346 comp->release();
347
348 // TODO legacy code threw a notification post UNPROTECTING update -- required?
349 return 0;
350 }
351
352 } // namespace operation
353 } // namespace librbd
354
355 template class librbd::operation::SnapshotUnprotectRequest<librbd::ImageCtx>;