]> git.proxmox.com Git - ceph.git/blob - ceph/src/cls/lock/cls_lock_client.cc
update sources to v12.1.0
[ceph.git] / ceph / src / cls / lock / cls_lock_client.cc
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"
18 #include "include/utime.h"
19
20 using namespace librados;
21
22 #include "cls/lock/cls_lock_ops.h"
23 #include "cls/lock/cls_lock_client.h"
24
25 namespace rados {
26 namespace cls {
27 namespace lock {
28
29 void lock(ObjectWriteOperation *rados_op,
30 const string& name, ClsLockType type,
31 const string& cookie, const string& tag,
32 const string& description,
33 const utime_t& duration, uint8_t flags)
34 {
35 cls_lock_lock_op op;
36 op.name = name;
37 op.type = type;
38 op.cookie = cookie;
39 op.tag = tag;
40 op.description = description;
41 op.duration = duration;
42 op.flags = flags;
43 bufferlist in;
44 ::encode(op, in);
45 rados_op->exec("lock", "lock", in);
46 }
47
48 int lock(IoCtx *ioctx,
49 const string& oid,
50 const string& name, ClsLockType type,
51 const string& cookie, const string& tag,
52 const string& description, const utime_t& duration,
53 uint8_t flags)
54 {
55 ObjectWriteOperation op;
56 lock(&op, name, type, cookie, tag, description, duration, flags);
57 return ioctx->operate(oid, &op);
58 }
59
60 void unlock(ObjectWriteOperation *rados_op,
61 const string& name, const string& cookie)
62 {
63 cls_lock_unlock_op op;
64 op.name = name;
65 op.cookie = cookie;
66 bufferlist in;
67 ::encode(op, in);
68
69 rados_op->exec("lock", "unlock", in);
70 }
71
72 int unlock(IoCtx *ioctx, const string& oid,
73 const string& name, const string& cookie)
74 {
75 ObjectWriteOperation op;
76 unlock(&op, name, cookie);
77 return ioctx->operate(oid, &op);
78 }
79
80 int aio_unlock(IoCtx *ioctx, const string& oid,
81 const string& name, const string& cookie,
82 librados::AioCompletion *completion)
83 {
84 ObjectWriteOperation op;
85 unlock(&op, name, cookie);
86 return ioctx->aio_operate(oid, completion, &op);
87 }
88
89 void break_lock(ObjectWriteOperation *rados_op,
90 const string& name, const string& cookie,
91 const entity_name_t& locker)
92 {
93 cls_lock_break_op op;
94 op.name = name;
95 op.cookie = cookie;
96 op.locker = locker;
97 bufferlist in;
98 ::encode(op, in);
99 rados_op->exec("lock", "break_lock", in);
100 }
101
102 int break_lock(IoCtx *ioctx, const string& oid,
103 const string& name, const string& cookie,
104 const entity_name_t& locker)
105 {
106 ObjectWriteOperation op;
107 break_lock(&op, name, cookie, locker);
108 return ioctx->operate(oid, &op);
109 }
110
111 int list_locks(IoCtx *ioctx, const string& oid, list<string> *locks)
112 {
113 bufferlist in, out;
114 int r = ioctx->exec(oid, "lock", "list_locks", in, out);
115 if (r < 0)
116 return r;
117
118 cls_lock_list_locks_reply ret;
119 bufferlist::iterator iter = out.begin();
120 try {
121 ::decode(ret, iter);
122 } catch (buffer::error& err) {
123 return -EBADMSG;
124 }
125
126 *locks = ret.locks;
127
128 return 0;
129 }
130
131 void get_lock_info_start(ObjectReadOperation *rados_op,
132 const string& name)
133 {
134 bufferlist in;
135 cls_lock_get_info_op op;
136 op.name = name;
137 ::encode(op, in);
138 rados_op->exec("lock", "get_info", in);
139 }
140
141 int get_lock_info_finish(bufferlist::iterator *iter,
142 map<locker_id_t, locker_info_t> *lockers,
143 ClsLockType *type, string *tag)
144 {
145 cls_lock_get_info_reply ret;
146 try {
147 ::decode(ret, *iter);
148 } catch (buffer::error& err) {
149 return -EBADMSG;
150 }
151
152 if (lockers) {
153 *lockers = ret.lockers;
154 }
155
156 if (type) {
157 *type = ret.lock_type;
158 }
159
160 if (tag) {
161 *tag = ret.tag;
162 }
163
164 return 0;
165 }
166
167 int get_lock_info(IoCtx *ioctx, const string& oid, const string& name,
168 map<locker_id_t, locker_info_t> *lockers,
169 ClsLockType *type, string *tag)
170 {
171 ObjectReadOperation op;
172 get_lock_info_start(&op, name);
173 bufferlist out;
174 int r = ioctx->operate(oid, &op, &out);
175 if (r < 0)
176 return r;
177 bufferlist::iterator it = out.begin();
178 return get_lock_info_finish(&it, lockers, type, tag);
179 }
180
181 void assert_locked(librados::ObjectOperation *rados_op,
182 const std::string& name, ClsLockType type,
183 const std::string& cookie, const std::string& tag)
184 {
185 cls_lock_assert_op op;
186 op.name = name;
187 op.type = type;
188 op.cookie = cookie;
189 op.tag = tag;
190 bufferlist in;
191 ::encode(op, in);
192 rados_op->exec("lock", "assert_locked", in);
193 }
194
195 void set_cookie(librados::ObjectWriteOperation *rados_op,
196 const std::string& name, ClsLockType type,
197 const std::string& cookie, const std::string& tag,
198 const std::string& new_cookie)
199 {
200 cls_lock_set_cookie_op op;
201 op.name = name;
202 op.type = type;
203 op.cookie = cookie;
204 op.tag = tag;
205 op.new_cookie = new_cookie;
206 bufferlist in;
207 ::encode(op, in);
208 rados_op->exec("lock", "set_cookie", in);
209 }
210
211 void Lock::assert_locked_exclusive(ObjectOperation *op)
212 {
213 assert_locked(op, name, LOCK_EXCLUSIVE, cookie, tag);
214 }
215
216 void Lock::assert_locked_shared(ObjectOperation *op)
217 {
218 assert_locked(op, name, LOCK_SHARED, cookie, tag);
219 }
220
221 void Lock::lock_shared(ObjectWriteOperation *op)
222 {
223 lock(op, name, LOCK_SHARED,
224 cookie, tag, description, duration, flags);
225 }
226
227 int Lock::lock_shared(IoCtx *ioctx, const string& oid)
228 {
229 return lock(ioctx, oid, name, LOCK_SHARED,
230 cookie, tag, description, duration, flags);
231 }
232
233 void Lock::lock_exclusive(ObjectWriteOperation *op)
234 {
235 lock(op, name, LOCK_EXCLUSIVE,
236 cookie, tag, description, duration, flags);
237 }
238
239 int Lock::lock_exclusive(IoCtx *ioctx, const string& oid)
240 {
241 return lock(ioctx, oid, name, LOCK_EXCLUSIVE,
242 cookie, tag, description, duration, flags);
243 }
244
245 void Lock::unlock(ObjectWriteOperation *op)
246 {
247 rados::cls::lock::unlock(op, name, cookie);
248 }
249
250 int Lock::unlock(IoCtx *ioctx, const string& oid)
251 {
252 return rados::cls::lock::unlock(ioctx, oid, name, cookie);
253 }
254
255 void Lock::break_lock(ObjectWriteOperation *op, const entity_name_t& locker)
256 {
257 rados::cls::lock::break_lock(op, name, cookie, locker);
258 }
259
260 int Lock::break_lock(IoCtx *ioctx, const string& oid, const entity_name_t& locker)
261 {
262 return rados::cls::lock::break_lock(ioctx, oid, name, cookie, locker);
263 }
264 } // namespace lock
265 } // namespace cls
266 } // namespace rados
267