1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
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.
15 #include "include/types.h"
16 #include "msg/msg_types.h"
17 #include "include/rados/librados.hpp"
19 using namespace librados
;
27 #include "cls/lock/cls_lock_types.h"
28 #include "cls/lock/cls_lock_ops.h"
29 #include "cls/lock/cls_lock_client.h"
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
)
46 op
.description
= description
;
47 op
.duration
= duration
;
51 rados_op
->exec("lock", "lock", in
);
54 int lock(IoCtx
*ioctx
,
56 const string
& name
, ClsLockType type
,
57 const string
& cookie
, const string
& tag
,
58 const string
& description
, const utime_t
& duration
,
61 ObjectWriteOperation op
;
62 lock(&op
, name
, type
, cookie
, tag
, description
, duration
, flags
);
63 return ioctx
->operate(oid
, &op
);
66 void unlock(ObjectWriteOperation
*rados_op
,
67 const string
& name
, const string
& cookie
)
69 cls_lock_unlock_op op
;
75 rados_op
->exec("lock", "unlock", in
);
78 int unlock(IoCtx
*ioctx
, const string
& oid
,
79 const string
& name
, const string
& cookie
)
81 ObjectWriteOperation op
;
82 unlock(&op
, name
, cookie
);
83 return ioctx
->operate(oid
, &op
);
86 int aio_unlock(IoCtx
*ioctx
, const string
& oid
,
87 const string
& name
, const string
& cookie
,
88 librados::AioCompletion
*completion
)
90 ObjectWriteOperation op
;
91 unlock(&op
, name
, cookie
);
92 return ioctx
->aio_operate(oid
, completion
, &op
);
95 void break_lock(ObjectWriteOperation
*rados_op
,
96 const string
& name
, const string
& cookie
,
97 const entity_name_t
& locker
)
105 rados_op
->exec("lock", "break_lock", in
);
108 int break_lock(IoCtx
*ioctx
, const string
& oid
,
109 const string
& name
, const string
& cookie
,
110 const entity_name_t
& locker
)
112 ObjectWriteOperation op
;
113 break_lock(&op
, name
, cookie
, locker
);
114 return ioctx
->operate(oid
, &op
);
117 int list_locks(IoCtx
*ioctx
, const string
& oid
, list
<string
> *locks
)
120 int r
= ioctx
->exec(oid
, "lock", "list_locks", in
, out
);
124 cls_lock_list_locks_reply ret
;
125 bufferlist::iterator iter
= out
.begin();
128 } catch (buffer::error
& err
) {
137 void get_lock_info_start(ObjectReadOperation
*rados_op
,
141 cls_lock_get_info_op op
;
144 rados_op
->exec("lock", "get_info", in
);
147 int get_lock_info_finish(bufferlist::iterator
*iter
,
148 map
<locker_id_t
, locker_info_t
> *lockers
,
149 ClsLockType
*type
, string
*tag
)
151 cls_lock_get_info_reply ret
;
153 ::decode(ret
, *iter
);
154 } catch (buffer::error
& err
) {
159 *lockers
= ret
.lockers
;
163 *type
= ret
.lock_type
;
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
)
177 ObjectReadOperation op
;
178 get_lock_info_start(&op
, name
);
180 int r
= ioctx
->operate(oid
, &op
, &out
);
183 bufferlist::iterator it
= out
.begin();
184 return get_lock_info_finish(&it
, lockers
, type
, tag
);
187 void assert_locked(librados::ObjectOperation
*rados_op
,
188 const std::string
& name
, ClsLockType type
,
189 const std::string
& cookie
, const std::string
& tag
)
191 cls_lock_assert_op op
;
198 rados_op
->exec("lock", "assert_locked", in
);
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
)
206 cls_lock_set_cookie_op op
;
211 op
.new_cookie
= new_cookie
;
214 rados_op
->exec("lock", "set_cookie", in
);
217 void Lock::assert_locked_exclusive(ObjectOperation
*op
)
219 assert_locked(op
, name
, LOCK_EXCLUSIVE
, cookie
, tag
);
222 void Lock::assert_locked_shared(ObjectOperation
*op
)
224 assert_locked(op
, name
, LOCK_SHARED
, cookie
, tag
);
227 void Lock::lock_shared(ObjectWriteOperation
*op
)
229 lock(op
, name
, LOCK_SHARED
,
230 cookie
, tag
, description
, duration
, flags
);
233 int Lock::lock_shared(IoCtx
*ioctx
, const string
& oid
)
235 return lock(ioctx
, oid
, name
, LOCK_SHARED
,
236 cookie
, tag
, description
, duration
, flags
);
239 void Lock::lock_exclusive(ObjectWriteOperation
*op
)
241 lock(op
, name
, LOCK_EXCLUSIVE
,
242 cookie
, tag
, description
, duration
, flags
);
245 int Lock::lock_exclusive(IoCtx
*ioctx
, const string
& oid
)
247 return lock(ioctx
, oid
, name
, LOCK_EXCLUSIVE
,
248 cookie
, tag
, description
, duration
, flags
);
251 void Lock::unlock(ObjectWriteOperation
*op
)
253 rados::cls::lock::unlock(op
, name
, cookie
);
256 int Lock::unlock(IoCtx
*ioctx
, const string
& oid
)
258 return rados::cls::lock::unlock(ioctx
, oid
, name
, cookie
);
261 void Lock::break_lock(ObjectWriteOperation
*op
, const entity_name_t
& locker
)
263 rados::cls::lock::break_lock(op
, name
, cookie
, locker
);
266 int Lock::break_lock(IoCtx
*ioctx
, const string
& oid
, const entity_name_t
& locker
)
268 return rados::cls::lock::break_lock(ioctx
, oid
, name
, cookie
, locker
);