Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / librbd / managed_lock / ReacquireRequest.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/ReacquireRequest.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 "librbd/ImageCtx.h"
11 #include "librbd/Utils.h"
12 #include "librbd/managed_lock/Utils.h"
13
14 #define dout_subsys ceph_subsys_rbd
15 #undef dout_prefix
16 #define dout_prefix *_dout << "librbd::managed_lock::ReacquireRequest: " \
17                            << this << ": " << __func__
18
19 using std::string;
20
21 namespace librbd {
22 namespace managed_lock {
23
24 using librbd::util::create_rados_callback;
25
26 template <typename I>
27 ReacquireRequest<I>::ReacquireRequest(librados::IoCtx& ioctx,
28                                       const string& oid,
29                                       const string& old_cookie,
30                                       const string &new_cookie,
31                                       bool exclusive,
32                                       Context *on_finish)
33   : m_ioctx(ioctx), m_oid(oid), m_old_cookie(old_cookie),
34     m_new_cookie(new_cookie), m_exclusive(exclusive), m_on_finish(on_finish) {
35 }
36
37
38 template <typename I>
39 void ReacquireRequest<I>::send() {
40   set_cookie();
41 }
42
43 template <typename I>
44 void ReacquireRequest<I>::set_cookie() {
45   CephContext *cct = reinterpret_cast<CephContext *>(m_ioctx.cct());
46   ldout(cct, 10) << dendl;
47
48   librados::ObjectWriteOperation op;
49   rados::cls::lock::set_cookie(&op, RBD_LOCK_NAME,
50                                m_exclusive ? LOCK_EXCLUSIVE : LOCK_SHARED,
51                                m_old_cookie, util::get_watcher_lock_tag(),
52                                m_new_cookie);
53
54   librados::AioCompletion *rados_completion = create_rados_callback<
55     ReacquireRequest, &ReacquireRequest::handle_set_cookie>(this);
56   int r = m_ioctx.aio_operate(m_oid, rados_completion, &op);
57   assert(r == 0);
58   rados_completion->release();
59 }
60
61 template <typename I>
62 void ReacquireRequest<I>::handle_set_cookie(int r) {
63   CephContext *cct = reinterpret_cast<CephContext *>(m_ioctx.cct());
64   ldout(cct, 10) << ": r=" << r << dendl;
65
66   if (r == -EOPNOTSUPP) {
67     ldout(cct, 10) << ": OSD doesn't support updating lock" << dendl;
68   } else if (r < 0) {
69     lderr(cct) << ": failed to update lock: " << cpp_strerror(r) << dendl;
70   }
71
72   m_on_finish->complete(r);
73   delete this;
74 }
75
76 } // namespace managed_lock
77 } // namespace librbd
78
79 template class librbd::managed_lock::ReacquireRequest<librbd::ImageCtx>;