]> git.proxmox.com Git - ceph.git/blame - ceph/src/librbd/mirror/snapshot/RemoveImageStateRequest.cc
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / librbd / mirror / snapshot / RemoveImageStateRequest.cc
CommitLineData
9f95a23c
TL
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/mirror/snapshot/RemoveImageStateRequest.h"
5#include "common/dout.h"
6#include "common/errno.h"
7#include "librbd/ImageCtx.h"
8#include "librbd/Utils.h"
9#include "librbd/mirror/snapshot/Types.h"
10#include "librbd/mirror/snapshot/Utils.h"
11
12#define dout_subsys ceph_subsys_rbd
13
14#undef dout_prefix
15#define dout_prefix *_dout << "librbd::mirror::snapshot::RemoveImageStateRequest: " \
16 << this << " " << __func__ << ": "
17
18namespace librbd {
19namespace mirror {
20namespace snapshot {
21
22using librbd::util::create_rados_callback;
23
24template <typename I>
25void RemoveImageStateRequest<I>::send() {
26 get_object_count();
27}
28
29
30template <typename I>
31void RemoveImageStateRequest<I>::get_object_count() {
32 CephContext *cct = m_image_ctx->cct;
33
34 auto oid = util::image_state_object_name(m_image_ctx, m_snap_id, 0);
f67539c2 35 ldout(cct, 15) << oid << dendl;
9f95a23c
TL
36
37 librados::ObjectReadOperation op;
38 op.read(0, 0, &m_bl, nullptr);
39
40 librados::AioCompletion *comp = create_rados_callback<
41 RemoveImageStateRequest<I>,
42 &RemoveImageStateRequest<I>::handle_get_object_count>(this);
43 int r = m_image_ctx->md_ctx.aio_operate(oid, comp, &op, nullptr);
44 ceph_assert(r == 0);
45 comp->release();
46}
47
48template <typename I>
49void RemoveImageStateRequest<I>::handle_get_object_count(int r) {
50 CephContext *cct = m_image_ctx->cct;
f67539c2 51 ldout(cct, 15) << "r=" << r << dendl;
9f95a23c
TL
52
53 if (r < 0) {
54 lderr(cct) << "failed to read image state object: " << cpp_strerror(r)
55 << dendl;
56 finish(r);
57 return;
58 }
59
60 ImageStateHeader header(1);
61 auto iter = m_bl.cbegin();
62 try {
63 using ceph::decode;
64
65 decode(header, iter);
66 } catch (const buffer::error &err) {
67 lderr(cct) << "failed to decode image state object header" << dendl;
68 // still try to remove it
69 }
70
71 m_object_count = header.object_count > 0 ? header.object_count : 1;
72
73 remove_object();
74}
75
76template <typename I>
77void RemoveImageStateRequest<I>::remove_object() {
78 CephContext *cct = m_image_ctx->cct;
79
80 ceph_assert(m_object_count > 0);
81 m_object_count--;
82
83 auto oid = util::image_state_object_name(m_image_ctx, m_snap_id,
84 m_object_count);
f67539c2 85 ldout(cct, 15) << oid << dendl;
9f95a23c
TL
86
87 librados::ObjectWriteOperation op;
88 op.remove();
89
90 librados::AioCompletion *comp = create_rados_callback<
91 RemoveImageStateRequest<I>,
92 &RemoveImageStateRequest<I>::handle_remove_object>(this);
93 int r = m_image_ctx->md_ctx.aio_operate(oid, comp, &op);
94 ceph_assert(r == 0);
95 comp->release();
96}
97
98template <typename I>
99void RemoveImageStateRequest<I>::handle_remove_object(int r) {
100 CephContext *cct = m_image_ctx->cct;
f67539c2 101 ldout(cct, 15) << "r=" << r << dendl;
9f95a23c
TL
102
103 if (r < 0 && r != -ENOENT) {
104 lderr(cct) << "failed to remove image state object: " << cpp_strerror(r)
105 << dendl;
106 finish(r);
107 return;
108 }
109
110 if (m_object_count == 0) {
111 finish(0);
112 return;
113 }
114
115 remove_object();
116}
117
118template <typename I>
119void RemoveImageStateRequest<I>::finish(int r) {
120 CephContext *cct = m_image_ctx->cct;
f67539c2 121 ldout(cct, 15) << "r=" << r << dendl;
9f95a23c
TL
122
123 m_on_finish->complete(r);
124 delete this;
125}
126
127} // namespace snapshot
128} // namespace mirror
129} // namespace librbd
130
131template class librbd::mirror::snapshot::RemoveImageStateRequest<librbd::ImageCtx>;