]> git.proxmox.com Git - ceph.git/blame - ceph/src/cls/lock/cls_lock_client.cc
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / cls / lock / cls_lock_client.cc
CommitLineData
7c673cae
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
3/*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15#include "include/types.h"
16#include "msg/msg_types.h"
17#include "include/rados/librados.hpp"
31f18b77 18#include "include/utime.h"
7c673cae 19
7c673cae
FG
20#include "cls/lock/cls_lock_ops.h"
21#include "cls/lock/cls_lock_client.h"
22
f67539c2
TL
23using std::map;
24
25using namespace librados;
26
27
7c673cae
FG
28namespace rados {
29 namespace cls {
30 namespace lock {
31
32 void lock(ObjectWriteOperation *rados_op,
f67539c2
TL
33 const std::string& name, ClsLockType type,
34 const std::string& cookie, const std::string& tag,
35 const std::string& description,
7c673cae
FG
36 const utime_t& duration, uint8_t flags)
37 {
38 cls_lock_lock_op op;
39 op.name = name;
40 op.type = type;
41 op.cookie = cookie;
42 op.tag = tag;
43 op.description = description;
44 op.duration = duration;
45 op.flags = flags;
46 bufferlist in;
11fdf7f2 47 encode(op, in);
7c673cae
FG
48 rados_op->exec("lock", "lock", in);
49 }
50
51 int lock(IoCtx *ioctx,
f67539c2
TL
52 const std::string& oid,
53 const std::string& name, ClsLockType type,
54 const std::string& cookie, const std::string& tag,
55 const std::string& description, const utime_t& duration,
7c673cae
FG
56 uint8_t flags)
57 {
58 ObjectWriteOperation op;
59 lock(&op, name, type, cookie, tag, description, duration, flags);
60 return ioctx->operate(oid, &op);
61 }
62
63 void unlock(ObjectWriteOperation *rados_op,
f67539c2 64 const std::string& name, const std::string& cookie)
7c673cae
FG
65 {
66 cls_lock_unlock_op op;
67 op.name = name;
68 op.cookie = cookie;
69 bufferlist in;
11fdf7f2 70 encode(op, in);
7c673cae
FG
71
72 rados_op->exec("lock", "unlock", in);
73 }
74
f67539c2
TL
75 int unlock(IoCtx *ioctx, const std::string& oid,
76 const std::string& name, const std::string& cookie)
7c673cae
FG
77 {
78 ObjectWriteOperation op;
79 unlock(&op, name, cookie);
80 return ioctx->operate(oid, &op);
81 }
82
f67539c2
TL
83 int aio_unlock(IoCtx *ioctx, const std::string& oid,
84 const std::string& name, const std::string& cookie,
7c673cae
FG
85 librados::AioCompletion *completion)
86 {
87 ObjectWriteOperation op;
88 unlock(&op, name, cookie);
89 return ioctx->aio_operate(oid, completion, &op);
90 }
91
92 void break_lock(ObjectWriteOperation *rados_op,
f67539c2 93 const std::string& name, const std::string& cookie,
7c673cae
FG
94 const entity_name_t& locker)
95 {
96 cls_lock_break_op op;
97 op.name = name;
98 op.cookie = cookie;
99 op.locker = locker;
100 bufferlist in;
11fdf7f2 101 encode(op, in);
7c673cae
FG
102 rados_op->exec("lock", "break_lock", in);
103 }
104
f67539c2
TL
105 int break_lock(IoCtx *ioctx, const std::string& oid,
106 const std::string& name, const std::string& cookie,
7c673cae
FG
107 const entity_name_t& locker)
108 {
109 ObjectWriteOperation op;
110 break_lock(&op, name, cookie, locker);
111 return ioctx->operate(oid, &op);
112 }
113
f67539c2 114 int list_locks(IoCtx *ioctx, const std::string& oid, std::list<std::string> *locks)
7c673cae
FG
115 {
116 bufferlist in, out;
117 int r = ioctx->exec(oid, "lock", "list_locks", in, out);
118 if (r < 0)
119 return r;
120
121 cls_lock_list_locks_reply ret;
f67539c2 122 auto iter = std::cbegin(out);
7c673cae 123 try {
11fdf7f2 124 decode(ret, iter);
f67539c2 125 } catch (ceph::buffer::error& err) {
7c673cae
FG
126 return -EBADMSG;
127 }
128
129 *locks = ret.locks;
130
131 return 0;
132 }
133
134 void get_lock_info_start(ObjectReadOperation *rados_op,
f67539c2 135 const std::string& name)
7c673cae
FG
136 {
137 bufferlist in;
138 cls_lock_get_info_op op;
139 op.name = name;
11fdf7f2 140 encode(op, in);
7c673cae
FG
141 rados_op->exec("lock", "get_info", in);
142 }
143
11fdf7f2 144 int get_lock_info_finish(bufferlist::const_iterator *iter,
7c673cae 145 map<locker_id_t, locker_info_t> *lockers,
f67539c2 146 ClsLockType *type, std::string *tag)
7c673cae
FG
147 {
148 cls_lock_get_info_reply ret;
149 try {
11fdf7f2 150 decode(ret, *iter);
f67539c2 151 } catch (ceph::buffer::error& err) {
7c673cae
FG
152 return -EBADMSG;
153 }
154
155 if (lockers) {
156 *lockers = ret.lockers;
157 }
158
159 if (type) {
160 *type = ret.lock_type;
161 }
162
163 if (tag) {
164 *tag = ret.tag;
165 }
166
167 return 0;
168 }
169
f67539c2 170 int get_lock_info(IoCtx *ioctx, const std::string& oid, const std::string& name,
7c673cae 171 map<locker_id_t, locker_info_t> *lockers,
f67539c2 172 ClsLockType *type, std::string *tag)
7c673cae
FG
173 {
174 ObjectReadOperation op;
175 get_lock_info_start(&op, name);
176 bufferlist out;
177 int r = ioctx->operate(oid, &op, &out);
178 if (r < 0)
179 return r;
11fdf7f2 180 auto it = std::cbegin(out);
7c673cae
FG
181 return get_lock_info_finish(&it, lockers, type, tag);
182 }
183
184 void assert_locked(librados::ObjectOperation *rados_op,
185 const std::string& name, ClsLockType type,
186 const std::string& cookie, const std::string& tag)
187 {
188 cls_lock_assert_op op;
189 op.name = name;
190 op.type = type;
191 op.cookie = cookie;
192 op.tag = tag;
193 bufferlist in;
11fdf7f2 194 encode(op, in);
7c673cae
FG
195 rados_op->exec("lock", "assert_locked", in);
196 }
197
198 void set_cookie(librados::ObjectWriteOperation *rados_op,
199 const std::string& name, ClsLockType type,
200 const std::string& cookie, const std::string& tag,
201 const std::string& new_cookie)
202 {
203 cls_lock_set_cookie_op op;
204 op.name = name;
205 op.type = type;
206 op.cookie = cookie;
207 op.tag = tag;
208 op.new_cookie = new_cookie;
209 bufferlist in;
11fdf7f2 210 encode(op, in);
7c673cae
FG
211 rados_op->exec("lock", "set_cookie", in);
212 }
213
f64942e4
AA
214 void Lock::assert_locked_shared(ObjectOperation *op)
215 {
f67539c2 216 assert_locked(op, name, ClsLockType::SHARED, cookie, tag);
f64942e4
AA
217 }
218
7c673cae
FG
219 void Lock::assert_locked_exclusive(ObjectOperation *op)
220 {
f67539c2 221 assert_locked(op, name, ClsLockType::EXCLUSIVE, cookie, tag);
7c673cae
FG
222 }
223
f64942e4 224 void Lock::assert_locked_exclusive_ephemeral(ObjectOperation *op)
7c673cae 225 {
f67539c2 226 assert_locked(op, name, ClsLockType::EXCLUSIVE_EPHEMERAL, cookie, tag);
7c673cae
FG
227 }
228
229 void Lock::lock_shared(ObjectWriteOperation *op)
230 {
f67539c2 231 lock(op, name, ClsLockType::SHARED,
7c673cae
FG
232 cookie, tag, description, duration, flags);
233 }
234
f67539c2 235 int Lock::lock_shared(IoCtx *ioctx, const std::string& oid)
7c673cae 236 {
f67539c2 237 return lock(ioctx, oid, name, ClsLockType::SHARED,
7c673cae
FG
238 cookie, tag, description, duration, flags);
239 }
240
241 void Lock::lock_exclusive(ObjectWriteOperation *op)
242 {
f67539c2 243 lock(op, name, ClsLockType::EXCLUSIVE,
7c673cae
FG
244 cookie, tag, description, duration, flags);
245 }
246
f67539c2 247 int Lock::lock_exclusive(IoCtx *ioctx, const std::string& oid)
7c673cae 248 {
f67539c2 249 return lock(ioctx, oid, name, ClsLockType::EXCLUSIVE,
7c673cae
FG
250 cookie, tag, description, duration, flags);
251 }
252
f64942e4
AA
253 void Lock::lock_exclusive_ephemeral(ObjectWriteOperation *op)
254 {
f67539c2 255 lock(op, name, ClsLockType::EXCLUSIVE_EPHEMERAL,
f64942e4
AA
256 cookie, tag, description, duration, flags);
257 }
258
f67539c2 259 int Lock::lock_exclusive_ephemeral(IoCtx *ioctx, const std::string& oid)
f64942e4 260 {
f67539c2 261 return lock(ioctx, oid, name, ClsLockType::EXCLUSIVE_EPHEMERAL,
f64942e4
AA
262 cookie, tag, description, duration, flags);
263 }
264
7c673cae
FG
265 void Lock::unlock(ObjectWriteOperation *op)
266 {
267 rados::cls::lock::unlock(op, name, cookie);
268 }
269
f67539c2 270 int Lock::unlock(IoCtx *ioctx, const std::string& oid)
7c673cae
FG
271 {
272 return rados::cls::lock::unlock(ioctx, oid, name, cookie);
273 }
274
275 void Lock::break_lock(ObjectWriteOperation *op, const entity_name_t& locker)
276 {
277 rados::cls::lock::break_lock(op, name, cookie, locker);
278 }
279
f67539c2 280 int Lock::break_lock(IoCtx *ioctx, const std::string& oid, const entity_name_t& locker)
7c673cae
FG
281 {
282 return rados::cls::lock::break_lock(ioctx, oid, name, cookie, locker);
283 }
284 } // namespace lock
285 } // namespace cls
286} // namespace rados