]> git.proxmox.com Git - ceph.git/blame - ceph/src/librbd/TrashWatcher.cc
import ceph quincy 17.2.1
[ceph.git] / ceph / src / librbd / TrashWatcher.cc
CommitLineData
11fdf7f2
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/TrashWatcher.h"
5#include "include/rbd_types.h"
6#include "include/rados/librados.hpp"
7#include "common/errno.h"
8#include "librbd/Utils.h"
9#include "librbd/watcher/Utils.h"
10
11#define dout_subsys ceph_subsys_rbd
12#undef dout_prefix
13#define dout_prefix *_dout << "librbd::TrashWatcher: " << __func__ << ": "
14
15namespace librbd {
16
17using namespace trash_watcher;
18using namespace watcher;
19
20using librbd::util::create_rados_callback;
21
22namespace {
23
24static const uint64_t NOTIFY_TIMEOUT_MS = 5000;
25
26} // anonymous namespace
27
28template <typename I>
f67539c2
TL
29TrashWatcher<I>::TrashWatcher(librados::IoCtx &io_ctx,
30 asio::ContextWQ *work_queue)
11fdf7f2
TL
31 : Watcher(io_ctx, work_queue, RBD_TRASH) {
32}
33
34template <typename I>
35void TrashWatcher<I>::notify_image_added(
36 librados::IoCtx &io_ctx, const std::string& image_id,
37 const cls::rbd::TrashImageSpec& trash_image_spec, Context *on_finish) {
38 CephContext *cct = reinterpret_cast<CephContext*>(io_ctx.cct());
39 ldout(cct, 20) << dendl;
40
41 bufferlist bl;
42 encode(NotifyMessage{ImageAddedPayload{image_id, trash_image_spec}}, bl);
43
44 librados::AioCompletion *comp = create_rados_callback(on_finish);
45 int r = io_ctx.aio_notify(RBD_TRASH, comp, bl, NOTIFY_TIMEOUT_MS, nullptr);
46 ceph_assert(r == 0);
47 comp->release();
48}
49
50template <typename I>
51void TrashWatcher<I>::notify_image_removed(librados::IoCtx &io_ctx,
52 const std::string& image_id,
53 Context *on_finish) {
54 CephContext *cct = reinterpret_cast<CephContext*>(io_ctx.cct());
55 ldout(cct, 20) << dendl;
56
57 bufferlist bl;
58 encode(NotifyMessage{ImageRemovedPayload{image_id}}, bl);
59
60 librados::AioCompletion *comp = create_rados_callback(on_finish);
61 int r = io_ctx.aio_notify(RBD_TRASH, comp, bl, NOTIFY_TIMEOUT_MS, nullptr);
62 ceph_assert(r == 0);
63 comp->release();
64}
65
66template <typename I>
67void TrashWatcher<I>::handle_notify(uint64_t notify_id, uint64_t handle,
68 uint64_t notifier_id, bufferlist &bl) {
69 CephContext *cct = this->m_cct;
70 ldout(cct, 15) << "notify_id=" << notify_id << ", "
71 << "handle=" << handle << dendl;
72
73
74 NotifyMessage notify_message;
75 try {
76 auto iter = bl.cbegin();
77 decode(notify_message, iter);
78 } catch (const buffer::error &err) {
79 lderr(cct) << "error decoding image notification: " << err.what()
80 << dendl;
81 Context *ctx = new C_NotifyAck(this, notify_id, handle);
82 ctx->complete(0);
83 return;
84 }
85
86 apply_visitor(watcher::util::HandlePayloadVisitor<TrashWatcher<I>>(
87 this, notify_id, handle), notify_message.payload);
88}
89
90template <typename I>
91bool TrashWatcher<I>::handle_payload(const ImageAddedPayload &payload,
92 Context *on_notify_ack) {
93 CephContext *cct = this->m_cct;
94 ldout(cct, 20) << dendl;
95 handle_image_added(payload.image_id, payload.trash_image_spec);
96 return true;
97}
98
99template <typename I>
100bool TrashWatcher<I>::handle_payload(const ImageRemovedPayload &payload,
101 Context *on_notify_ack) {
102 CephContext *cct = this->m_cct;
103 ldout(cct, 20) << dendl;
104 handle_image_removed(payload.image_id);
105 return true;
106}
107
108template <typename I>
109bool TrashWatcher<I>::handle_payload(const UnknownPayload &payload,
110 Context *on_notify_ack) {
111 return true;
112}
113
114} // namespace librbd
115
116template class librbd::TrashWatcher<librbd::ImageCtx>;