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