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"
18 #include "include/utime.h"
20 using namespace librados;
22 #include "cls/lock/cls_lock_ops.h"
23 #include "cls/lock/cls_lock_client.h"
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)
40 op.description = description;
41 op.duration = duration;
45 rados_op->exec("lock", "lock", in);
48 int lock(IoCtx *ioctx,
50 const string& name, ClsLockType type,
51 const string& cookie, const string& tag,
52 const string& description, const utime_t& duration,
55 ObjectWriteOperation op;
56 lock(&op, name, type, cookie, tag, description, duration, flags);
57 return ioctx->operate(oid, &op);
60 void unlock(ObjectWriteOperation *rados_op,
61 const string& name, const string& cookie)
63 cls_lock_unlock_op op;
69 rados_op->exec("lock", "unlock", in);
72 int unlock(IoCtx *ioctx, const string& oid,
73 const string& name, const string& cookie)
75 ObjectWriteOperation op;
76 unlock(&op, name, cookie);
77 return ioctx->operate(oid, &op);
80 int aio_unlock(IoCtx *ioctx, const string& oid,
81 const string& name, const string& cookie,
82 librados::AioCompletion *completion)
84 ObjectWriteOperation op;
85 unlock(&op, name, cookie);
86 return ioctx->aio_operate(oid, completion, &op);
89 void break_lock(ObjectWriteOperation *rados_op,
90 const string& name, const string& cookie,
91 const entity_name_t& locker)
99 rados_op->exec("lock", "break_lock", in);
102 int break_lock(IoCtx *ioctx, const string& oid,
103 const string& name, const string& cookie,
104 const entity_name_t& locker)
106 ObjectWriteOperation op;
107 break_lock(&op, name, cookie, locker);
108 return ioctx->operate(oid, &op);
111 int list_locks(IoCtx *ioctx, const string& oid, list<string> *locks)
114 int r = ioctx->exec(oid, "lock", "list_locks", in, out);
118 cls_lock_list_locks_reply ret;
119 bufferlist::iterator iter = out.begin();
122 } catch (buffer::error& err) {
131 void get_lock_info_start(ObjectReadOperation *rados_op,
135 cls_lock_get_info_op op;
138 rados_op->exec("lock", "get_info", in);
141 int get_lock_info_finish(bufferlist::iterator *iter,
142 map<locker_id_t, locker_info_t> *lockers,
143 ClsLockType *type, string *tag)
145 cls_lock_get_info_reply ret;
147 ::decode(ret, *iter);
148 } catch (buffer::error& err) {
153 *lockers = ret.lockers;
157 *type = ret.lock_type;
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)
171 ObjectReadOperation op;
172 get_lock_info_start(&op, name);
174 int r = ioctx->operate(oid, &op, &out);
177 bufferlist::iterator it = out.begin();
178 return get_lock_info_finish(&it, lockers, type, tag);
181 void assert_locked(librados::ObjectOperation *rados_op,
182 const std::string& name, ClsLockType type,
183 const std::string& cookie, const std::string& tag)
185 cls_lock_assert_op op;
192 rados_op->exec("lock", "assert_locked", in);
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)
200 cls_lock_set_cookie_op op;
205 op.new_cookie = new_cookie;
208 rados_op->exec("lock", "set_cookie", in);
211 void Lock::assert_locked_exclusive(ObjectOperation *op)
213 assert_locked(op, name, LOCK_EXCLUSIVE, cookie, tag);
216 void Lock::assert_locked_shared(ObjectOperation *op)
218 assert_locked(op, name, LOCK_SHARED, cookie, tag);
221 void Lock::lock_shared(ObjectWriteOperation *op)
223 lock(op, name, LOCK_SHARED,
224 cookie, tag, description, duration, flags);
227 int Lock::lock_shared(IoCtx *ioctx, const string& oid)
229 return lock(ioctx, oid, name, LOCK_SHARED,
230 cookie, tag, description, duration, flags);
233 void Lock::lock_exclusive(ObjectWriteOperation *op)
235 lock(op, name, LOCK_EXCLUSIVE,
236 cookie, tag, description, duration, flags);
239 int Lock::lock_exclusive(IoCtx *ioctx, const string& oid)
241 return lock(ioctx, oid, name, LOCK_EXCLUSIVE,
242 cookie, tag, description, duration, flags);
245 void Lock::unlock(ObjectWriteOperation *op)
247 rados::cls::lock::unlock(op, name, cookie);
250 int Lock::unlock(IoCtx *ioctx, const string& oid)
252 return rados::cls::lock::unlock(ioctx, oid, name, cookie);
255 void Lock::break_lock(ObjectWriteOperation *op, const entity_name_t& locker)
257 rados::cls::lock::break_lock(op, name, cookie, locker);
260 int Lock::break_lock(IoCtx *ioctx, const string& oid, const entity_name_t& locker)
262 return rados::cls::lock::break_lock(ioctx, oid, name, cookie, locker);