1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "librbd/managed_lock/AcquireRequest.h"
5 #include "librbd/Watcher.h"
6 #include "cls/lock/cls_lock_client.h"
7 #include "cls/lock/cls_lock_types.h"
8 #include "common/dout.h"
9 #include "common/errno.h"
10 #include "common/WorkQueue.h"
11 #include "include/stringify.h"
12 #include "librbd/ImageCtx.h"
13 #include "librbd/Utils.h"
14 #include "librbd/managed_lock/BreakRequest.h"
15 #include "librbd/managed_lock/GetLockerRequest.h"
16 #include "librbd/managed_lock/Utils.h"
18 #define dout_subsys ceph_subsys_rbd
20 #define dout_prefix *_dout << "librbd::managed_lock::AcquireRequest: " << this \
21 << " " << __func__ << ": "
27 using librbd::util::detail::C_AsyncCallback;
28 using librbd::util::create_context_callback;
29 using librbd::util::create_rados_callback;
31 namespace managed_lock {
34 AcquireRequest<I>* AcquireRequest<I>::create(librados::IoCtx& ioctx,
36 ContextWQ *work_queue,
40 bool blacklist_on_break_lock,
41 uint32_t blacklist_expire_seconds,
43 return new AcquireRequest(ioctx, watcher, work_queue, oid, cookie,
44 exclusive, blacklist_on_break_lock,
45 blacklist_expire_seconds, on_finish);
49 AcquireRequest<I>::AcquireRequest(librados::IoCtx& ioctx, Watcher *watcher,
50 ContextWQ *work_queue, const string& oid,
51 const string& cookie, bool exclusive,
52 bool blacklist_on_break_lock,
53 uint32_t blacklist_expire_seconds,
55 : m_ioctx(ioctx), m_watcher(watcher),
56 m_cct(reinterpret_cast<CephContext *>(m_ioctx.cct())),
57 m_work_queue(work_queue), m_oid(oid), m_cookie(cookie),
58 m_exclusive(exclusive),
59 m_blacklist_on_break_lock(blacklist_on_break_lock),
60 m_blacklist_expire_seconds(blacklist_expire_seconds),
61 m_on_finish(new C_AsyncCallback<ContextWQ>(work_queue, on_finish)) {
65 AcquireRequest<I>::~AcquireRequest() {
69 void AcquireRequest<I>::send() {
74 void AcquireRequest<I>::send_get_locker() {
75 ldout(m_cct, 10) << dendl;
77 Context *ctx = create_context_callback<
78 AcquireRequest<I>, &AcquireRequest<I>::handle_get_locker>(this);
79 auto req = GetLockerRequest<I>::create(m_ioctx, m_oid, m_exclusive,
85 void AcquireRequest<I>::handle_get_locker(int r) {
86 ldout(m_cct, 10) << "r=" << r << dendl;
89 ldout(m_cct, 20) << "no lockers detected" << dendl;
91 } else if (r == -EBUSY) {
92 ldout(m_cct, 5) << "incompatible lock detected" << dendl;
96 lderr(m_cct) << "failed to retrieve lockers: " << cpp_strerror(r) << dendl;
104 template <typename I>
105 void AcquireRequest<I>::send_lock() {
106 ldout(m_cct, 10) << "entity=client." << m_ioctx.get_instance_id() << ", "
107 << "cookie=" << m_cookie << dendl;
109 librados::ObjectWriteOperation op;
110 rados::cls::lock::lock(&op, RBD_LOCK_NAME,
111 m_exclusive ? LOCK_EXCLUSIVE : LOCK_SHARED, m_cookie,
112 util::get_watcher_lock_tag(), "", utime_t(), 0);
114 using klass = AcquireRequest;
115 librados::AioCompletion *rados_completion =
116 create_rados_callback<klass, &klass::handle_lock>(this);
117 int r = m_ioctx.aio_operate(m_oid, rados_completion, &op);
119 rados_completion->release();
122 template <typename I>
123 void AcquireRequest<I>::handle_lock(int r) {
124 ldout(m_cct, 10) << "r=" << r << dendl;
129 } else if (r == -EBUSY && m_locker.cookie.empty()) {
130 ldout(m_cct, 5) << "already locked, refreshing locker" << dendl;
133 } else if (r != -EBUSY) {
134 lderr(m_cct) << "failed to lock: " << cpp_strerror(r) << dendl;
142 template <typename I>
143 void AcquireRequest<I>::send_break_lock() {
144 ldout(m_cct, 10) << dendl;
146 Context *ctx = create_context_callback<
147 AcquireRequest<I>, &AcquireRequest<I>::handle_break_lock>(this);
148 auto req = BreakRequest<I>::create(
149 m_ioctx, m_work_queue, m_oid, m_locker, m_exclusive,
150 m_blacklist_on_break_lock, m_blacklist_expire_seconds, false, ctx);
154 template <typename I>
155 void AcquireRequest<I>::handle_break_lock(int r) {
156 ldout(m_cct, 10) << "r=" << r << dendl;
159 ldout(m_cct, 5) << "lock owner is still alive" << dendl;
163 lderr(m_cct) << "failed to break lock : " << cpp_strerror(r) << dendl;
172 template <typename I>
173 void AcquireRequest<I>::finish(int r) {
174 m_on_finish->complete(r);
178 } // namespace managed_lock
179 } // namespace librbd
181 template class librbd::managed_lock::AcquireRequest<librbd::ImageCtx>;