1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "ListWatchersRequest.h"
5 #include "common/RWLock.h"
6 #include "common/dout.h"
7 #include "common/errno.h"
8 #include "cls/rbd/cls_rbd_client.h"
9 #include "librbd/ImageCtx.h"
10 #include "librbd/ImageWatcher.h"
11 #include "librbd/Utils.h"
15 #define dout_subsys ceph_subsys_rbd
17 #define dout_prefix *_dout << "librbd::image::ListWatchersRequest: " << this \
18 << " " << __func__ << ": "
23 using librados::IoCtx
;
24 using util::create_rados_callback
;
27 ListWatchersRequest
<I
>::ListWatchersRequest(I
&image_ctx
, int flags
,
28 std::list
<obj_watch_t
> *watchers
,
30 : m_image_ctx(image_ctx
), m_flags(flags
), m_watchers(watchers
),
31 m_on_finish(on_finish
), m_cct(m_image_ctx
.cct
) {
32 ceph_assert((m_flags
& LIST_WATCHERS_FILTER_OUT_MIRROR_INSTANCES
) == 0 ||
33 (m_flags
& LIST_WATCHERS_MIRROR_INSTANCES_ONLY
) == 0);
37 void ListWatchersRequest
<I
>::send() {
38 ldout(m_cct
, 20) << dendl
;
40 list_image_watchers();
44 void ListWatchersRequest
<I
>::list_image_watchers() {
45 ldout(m_cct
, 20) << dendl
;
47 librados::ObjectReadOperation op
;
48 op
.list_watchers(&m_object_watchers
, &m_ret_val
);
50 using klass
= ListWatchersRequest
<I
>;
51 librados::AioCompletion
*rados_completion
=
52 create_rados_callback
<klass
, &klass::handle_list_image_watchers
>(this);
54 int r
= m_image_ctx
.md_ctx
.aio_operate(m_image_ctx
.header_oid
,
55 rados_completion
, &op
, &m_out_bl
);
57 rados_completion
->release();
61 void ListWatchersRequest
<I
>::handle_list_image_watchers(int r
) {
62 ldout(m_cct
, 20) << "r=" << r
<< dendl
;
64 if (r
== 0 && m_ret_val
< 0) {
68 lderr(m_cct
) << "error listing image watchers: " << cpp_strerror(r
)
74 list_mirror_watchers();
78 void ListWatchersRequest
<I
>::list_mirror_watchers() {
79 if ((m_object_watchers
.empty()) ||
80 (m_flags
& (LIST_WATCHERS_FILTER_OUT_MIRROR_INSTANCES
|
81 LIST_WATCHERS_MIRROR_INSTANCES_ONLY
)) == 0) {
86 ldout(m_cct
, 20) << dendl
;
88 librados::ObjectReadOperation op
;
89 op
.list_watchers(&m_mirror_watchers
, &m_ret_val
);
91 using klass
= ListWatchersRequest
<I
>;
92 librados::AioCompletion
*rados_completion
=
93 create_rados_callback
<klass
, &klass::handle_list_mirror_watchers
>(this);
95 int r
= m_image_ctx
.md_ctx
.aio_operate(RBD_MIRRORING
, rados_completion
,
98 rados_completion
->release();
102 void ListWatchersRequest
<I
>::handle_list_mirror_watchers(int r
) {
103 ldout(m_cct
, 20) << "r=" << r
<< dendl
;
105 if (r
== 0 && m_ret_val
< 0) {
108 if (r
< 0 && r
!= -ENOENT
) {
109 ldout(m_cct
, 1) << "error listing mirror watchers: " << cpp_strerror(r
)
116 void ListWatchersRequest
<I
>::finish(int r
) {
117 ldout(m_cct
, 20) << "r=" << r
<< dendl
;
122 if (m_object_watchers
.size() > 0) {
123 std::shared_lock owner_locker
{m_image_ctx
.owner_lock
};
124 uint64_t watch_handle
= m_image_ctx
.image_watcher
!= nullptr ?
125 m_image_ctx
.image_watcher
->get_watch_handle() : 0;
127 for (auto &w
: m_object_watchers
) {
128 if ((m_flags
& LIST_WATCHERS_FILTER_OUT_MY_INSTANCE
) != 0) {
129 if (w
.cookie
== watch_handle
) {
133 auto it
= std::find_if(m_mirror_watchers
.begin(),
134 m_mirror_watchers
.end(),
135 [w
] (obj_watch_t
&watcher
) {
136 return (strncmp(w
.addr
, watcher
.addr
,
137 sizeof(w
.addr
)) == 0);
139 if ((m_flags
& LIST_WATCHERS_FILTER_OUT_MIRROR_INSTANCES
) != 0) {
140 if (it
!= m_mirror_watchers
.end()) {
143 } else if ((m_flags
& LIST_WATCHERS_MIRROR_INSTANCES_ONLY
) != 0) {
144 if (it
== m_mirror_watchers
.end()) {
148 m_watchers
->push_back(w
);
153 m_on_finish
->complete(r
);
158 } // namespace librbd
160 template class librbd::image::ListWatchersRequest
<librbd::ImageCtx
>;