]> git.proxmox.com Git - ceph.git/blob - ceph/src/librbd/managed_lock/AcquireRequest.cc
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / librbd / managed_lock / AcquireRequest.cc
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/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"
17
18 #define dout_subsys ceph_subsys_rbd
19 #undef dout_prefix
20 #define dout_prefix *_dout << "librbd::managed_lock::AcquireRequest: " << this \
21 << " " << __func__ << ": "
22
23 using std::string;
24
25 namespace librbd {
26
27 using librbd::util::detail::C_AsyncCallback;
28 using librbd::util::create_context_callback;
29 using librbd::util::create_rados_callback;
30
31 namespace managed_lock {
32
33 template <typename I>
34 AcquireRequest<I>* AcquireRequest<I>::create(librados::IoCtx& ioctx,
35 Watcher *watcher,
36 ContextWQ *work_queue,
37 const string& oid,
38 const string& cookie,
39 bool exclusive,
40 bool blacklist_on_break_lock,
41 uint32_t blacklist_expire_seconds,
42 Context *on_finish) {
43 return new AcquireRequest(ioctx, watcher, work_queue, oid, cookie,
44 exclusive, blacklist_on_break_lock,
45 blacklist_expire_seconds, on_finish);
46 }
47
48 template <typename I>
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,
54 Context *on_finish)
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)) {
62 }
63
64 template <typename I>
65 AcquireRequest<I>::~AcquireRequest() {
66 }
67
68 template <typename I>
69 void AcquireRequest<I>::send() {
70 send_get_locker();
71 }
72
73 template <typename I>
74 void AcquireRequest<I>::send_get_locker() {
75 ldout(m_cct, 10) << dendl;
76
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,
80 &m_locker, ctx);
81 req->send();
82 }
83
84 template <typename I>
85 void AcquireRequest<I>::handle_get_locker(int r) {
86 ldout(m_cct, 10) << "r=" << r << dendl;
87
88 if (r == -ENOENT) {
89 ldout(m_cct, 20) << "no lockers detected" << dendl;
90 m_locker = {};
91 } else if (r == -EBUSY) {
92 ldout(m_cct, 5) << "incompatible lock detected" << dendl;
93 finish(r);
94 return;
95 } else if (r < 0) {
96 lderr(m_cct) << "failed to retrieve lockers: " << cpp_strerror(r) << dendl;
97 finish(r);
98 return;
99 }
100
101 send_lock();
102 }
103
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;
108
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);
113
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);
118 ceph_assert(r == 0);
119 rados_completion->release();
120 }
121
122 template <typename I>
123 void AcquireRequest<I>::handle_lock(int r) {
124 ldout(m_cct, 10) << "r=" << r << dendl;
125
126 if (r == 0) {
127 finish(0);
128 return;
129 } else if (r == -EBUSY && m_locker.cookie.empty()) {
130 ldout(m_cct, 5) << "already locked, refreshing locker" << dendl;
131 send_get_locker();
132 return;
133 } else if (r != -EBUSY) {
134 lderr(m_cct) << "failed to lock: " << cpp_strerror(r) << dendl;
135 finish(r);
136 return;
137 }
138
139 send_break_lock();
140 }
141
142 template <typename I>
143 void AcquireRequest<I>::send_break_lock() {
144 ldout(m_cct, 10) << dendl;
145
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);
151 req->send();
152 }
153
154 template <typename I>
155 void AcquireRequest<I>::handle_break_lock(int r) {
156 ldout(m_cct, 10) << "r=" << r << dendl;
157
158 if (r == -EAGAIN) {
159 ldout(m_cct, 5) << "lock owner is still alive" << dendl;
160 finish(r);
161 return;
162 } else if (r < 0) {
163 lderr(m_cct) << "failed to break lock : " << cpp_strerror(r) << dendl;
164 finish(r);
165 return;
166 }
167
168 m_locker = {};
169 send_lock();
170 }
171
172 template <typename I>
173 void AcquireRequest<I>::finish(int r) {
174 m_on_finish->complete(r);
175 delete this;
176 }
177
178 } // namespace managed_lock
179 } // namespace librbd
180
181 template class librbd::managed_lock::AcquireRequest<librbd::ImageCtx>;