1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
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"
11 #define dout_subsys ceph_subsys_rbd
13 #define dout_prefix *_dout << "librbd::TrashWatcher: " << __func__ << ": "
17 using namespace trash_watcher
;
18 using namespace watcher
;
20 using librbd::util::create_rados_callback
;
24 static const uint64_t NOTIFY_TIMEOUT_MS
= 5000;
26 } // anonymous namespace
29 TrashWatcher
<I
>::TrashWatcher(librados::IoCtx
&io_ctx
, ContextWQ
*work_queue
)
30 : Watcher(io_ctx
, work_queue
, RBD_TRASH
) {
34 void TrashWatcher
<I
>::notify_image_added(
35 librados::IoCtx
&io_ctx
, const std::string
& image_id
,
36 const cls::rbd::TrashImageSpec
& trash_image_spec
, Context
*on_finish
) {
37 CephContext
*cct
= reinterpret_cast<CephContext
*>(io_ctx
.cct());
38 ldout(cct
, 20) << dendl
;
41 encode(NotifyMessage
{ImageAddedPayload
{image_id
, trash_image_spec
}}, bl
);
43 librados::AioCompletion
*comp
= create_rados_callback(on_finish
);
44 int r
= io_ctx
.aio_notify(RBD_TRASH
, comp
, bl
, NOTIFY_TIMEOUT_MS
, nullptr);
50 void TrashWatcher
<I
>::notify_image_removed(librados::IoCtx
&io_ctx
,
51 const std::string
& image_id
,
53 CephContext
*cct
= reinterpret_cast<CephContext
*>(io_ctx
.cct());
54 ldout(cct
, 20) << dendl
;
57 encode(NotifyMessage
{ImageRemovedPayload
{image_id
}}, bl
);
59 librados::AioCompletion
*comp
= create_rados_callback(on_finish
);
60 int r
= io_ctx
.aio_notify(RBD_TRASH
, comp
, bl
, NOTIFY_TIMEOUT_MS
, nullptr);
66 void TrashWatcher
<I
>::handle_notify(uint64_t notify_id
, uint64_t handle
,
67 uint64_t notifier_id
, bufferlist
&bl
) {
68 CephContext
*cct
= this->m_cct
;
69 ldout(cct
, 15) << "notify_id=" << notify_id
<< ", "
70 << "handle=" << handle
<< dendl
;
73 NotifyMessage notify_message
;
75 auto iter
= bl
.cbegin();
76 decode(notify_message
, iter
);
77 } catch (const buffer::error
&err
) {
78 lderr(cct
) << "error decoding image notification: " << err
.what()
80 Context
*ctx
= new C_NotifyAck(this, notify_id
, handle
);
85 apply_visitor(watcher::util::HandlePayloadVisitor
<TrashWatcher
<I
>>(
86 this, notify_id
, handle
), notify_message
.payload
);
90 bool TrashWatcher
<I
>::handle_payload(const ImageAddedPayload
&payload
,
91 Context
*on_notify_ack
) {
92 CephContext
*cct
= this->m_cct
;
93 ldout(cct
, 20) << dendl
;
94 handle_image_added(payload
.image_id
, payload
.trash_image_spec
);
99 bool TrashWatcher
<I
>::handle_payload(const ImageRemovedPayload
&payload
,
100 Context
*on_notify_ack
) {
101 CephContext
*cct
= this->m_cct
;
102 ldout(cct
, 20) << dendl
;
103 handle_image_removed(payload
.image_id
);
107 template <typename I
>
108 bool TrashWatcher
<I
>::handle_payload(const UnknownPayload
&payload
,
109 Context
*on_notify_ack
) {
113 } // namespace librbd
115 template class librbd::TrashWatcher
<librbd::ImageCtx
>;