]>
Commit | Line | Data |
---|---|---|
f64942e4 AA |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | ||
7c673cae FG |
4 | #ifndef CEPH_CLS_LOCK_TYPES_H |
5 | #define CEPH_CLS_LOCK_TYPES_H | |
6 | ||
7 | #include "include/encoding.h" | |
8 | #include "include/types.h" | |
9 | #include "include/utime.h" | |
10 | #include "msg/msg_types.h" | |
11 | ||
12 | /* lock flags */ | |
f64942e4 AA |
13 | #define LOCK_FLAG_MAY_RENEW 0x1 /* idempotent lock acquire */ |
14 | #define LOCK_FLAG_MUST_RENEW 0x2 /* lock must already be acquired */ | |
7c673cae | 15 | |
f67539c2 TL |
16 | enum class ClsLockType { |
17 | NONE = 0, | |
18 | EXCLUSIVE = 1, | |
19 | SHARED = 2, | |
20 | EXCLUSIVE_EPHEMERAL = 3, /* lock object is removed @ unlock */ | |
7c673cae FG |
21 | }; |
22 | ||
11fdf7f2 | 23 | inline const char *cls_lock_type_str(ClsLockType type) |
7c673cae FG |
24 | { |
25 | switch (type) { | |
f67539c2 | 26 | case ClsLockType::NONE: |
7c673cae | 27 | return "none"; |
f67539c2 | 28 | case ClsLockType::EXCLUSIVE: |
7c673cae | 29 | return "exclusive"; |
f67539c2 | 30 | case ClsLockType::SHARED: |
7c673cae | 31 | return "shared"; |
f67539c2 | 32 | case ClsLockType::EXCLUSIVE_EPHEMERAL: |
f64942e4 | 33 | return "exclusive-ephemeral"; |
7c673cae FG |
34 | default: |
35 | return "<unknown>"; | |
36 | } | |
37 | } | |
38 | ||
f64942e4 | 39 | inline bool cls_lock_is_exclusive(ClsLockType type) { |
f67539c2 | 40 | return ClsLockType::EXCLUSIVE == type || ClsLockType::EXCLUSIVE_EPHEMERAL == type; |
f64942e4 AA |
41 | } |
42 | ||
43 | inline bool cls_lock_is_ephemeral(ClsLockType type) { | |
f67539c2 | 44 | return ClsLockType::EXCLUSIVE_EPHEMERAL == type; |
f64942e4 AA |
45 | } |
46 | ||
47 | inline bool cls_lock_is_valid(ClsLockType type) { | |
f67539c2 TL |
48 | return ClsLockType::SHARED == type || |
49 | ClsLockType::EXCLUSIVE == type || | |
50 | ClsLockType::EXCLUSIVE_EPHEMERAL == type; | |
f64942e4 AA |
51 | } |
52 | ||
7c673cae FG |
53 | namespace rados { |
54 | namespace cls { | |
55 | namespace lock { | |
56 | ||
57 | /* | |
58 | * locker_id_t: the locker id, needs to be unique in a single lock | |
59 | */ | |
60 | struct locker_id_t { | |
61 | entity_name_t locker; // locker's client name | |
9f95a23c | 62 | std::string cookie; // locker's cookie. |
7c673cae FG |
63 | |
64 | locker_id_t() {} | |
9f95a23c | 65 | locker_id_t(entity_name_t& _n, const std::string& _c) : locker(_n), cookie(_c) {} |
7c673cae | 66 | |
9f95a23c | 67 | void encode(ceph::buffer::list &bl) const { |
7c673cae | 68 | ENCODE_START(1, 1, bl); |
11fdf7f2 TL |
69 | encode(locker, bl); |
70 | encode(cookie, bl); | |
7c673cae FG |
71 | ENCODE_FINISH(bl); |
72 | } | |
9f95a23c | 73 | void decode(ceph::buffer::list::const_iterator &bl) { |
7c673cae | 74 | DECODE_START_LEGACY_COMPAT_LEN(1, 1, 1, bl); |
11fdf7f2 TL |
75 | decode(locker, bl); |
76 | decode(cookie, bl); | |
7c673cae FG |
77 | DECODE_FINISH(bl); |
78 | } | |
79 | ||
80 | bool operator<(const locker_id_t& rhs) const { | |
81 | if (locker == rhs.locker) | |
82 | return cookie.compare(rhs.cookie) < 0; | |
83 | if (locker < rhs.locker) | |
84 | return true; | |
85 | return false; | |
86 | } | |
f67539c2 | 87 | void dump(ceph::Formatter *f) const; |
81eedcae TL |
88 | friend std::ostream& operator<<(std::ostream& out, |
89 | const locker_id_t& data) { | |
90 | out << data.locker; | |
91 | return out; | |
92 | } | |
f67539c2 | 93 | static void generate_test_instances(std::list<locker_id_t*>& o); |
7c673cae | 94 | }; |
11fdf7f2 | 95 | WRITE_CLASS_ENCODER(locker_id_t) |
7c673cae FG |
96 | |
97 | struct locker_info_t | |
98 | { | |
99 | utime_t expiration; // expiration: non-zero means epoch of locker expiration | |
100 | entity_addr_t addr; // addr: locker address | |
9f95a23c | 101 | std::string description; // description: locker description, may be empty |
7c673cae FG |
102 | |
103 | locker_info_t() {} | |
104 | locker_info_t(const utime_t& _e, const entity_addr_t& _a, | |
9f95a23c | 105 | const std::string& _d) : expiration(_e), addr(_a), description(_d) {} |
7c673cae | 106 | |
9f95a23c | 107 | void encode(ceph::buffer::list &bl, uint64_t features) const { |
7c673cae | 108 | ENCODE_START(1, 1, bl); |
11fdf7f2 TL |
109 | encode(expiration, bl); |
110 | encode(addr, bl, features); | |
111 | encode(description, bl); | |
7c673cae FG |
112 | ENCODE_FINISH(bl); |
113 | } | |
9f95a23c | 114 | void decode(ceph::buffer::list::const_iterator &bl) { |
7c673cae | 115 | DECODE_START_LEGACY_COMPAT_LEN(1, 1, 1, bl); |
11fdf7f2 TL |
116 | decode(expiration, bl); |
117 | decode(addr, bl); | |
118 | decode(description, bl); | |
7c673cae FG |
119 | DECODE_FINISH(bl); |
120 | } | |
f67539c2 | 121 | void dump(ceph::Formatter *f) const; |
81eedcae TL |
122 | friend std::ostream& operator<<(std::ostream& out, |
123 | const locker_info_t& data) { | |
f67539c2 | 124 | using ceph::operator <<; |
81eedcae TL |
125 | out << "{addr:" << data.addr << ", exp:"; |
126 | ||
127 | const auto& exp = data.expiration; | |
128 | if (exp.is_zero()) { | |
129 | out << "never}"; | |
130 | } else { | |
131 | out << exp.to_real_time() << "}"; | |
132 | } | |
133 | ||
134 | return out; | |
135 | } | |
f67539c2 | 136 | static void generate_test_instances(std::list<locker_info_t *>& o); |
7c673cae | 137 | }; |
11fdf7f2 | 138 | WRITE_CLASS_ENCODER_FEATURES(locker_info_t) |
224ce89b WB |
139 | |
140 | struct lock_info_t { | |
f67539c2 | 141 | std::map<locker_id_t, locker_info_t> lockers; // map of lockers |
224ce89b | 142 | ClsLockType lock_type; // lock type (exclusive / shared) |
9f95a23c | 143 | std::string tag; // tag: operations on lock can only succeed with this tag |
224ce89b WB |
144 | // as long as set of non expired lockers |
145 | // is bigger than 0. | |
146 | ||
9f95a23c | 147 | void encode(ceph::buffer::list &bl, uint64_t features) const { |
224ce89b | 148 | ENCODE_START(1, 1, bl); |
11fdf7f2 | 149 | encode(lockers, bl, features); |
224ce89b | 150 | uint8_t t = (uint8_t)lock_type; |
11fdf7f2 TL |
151 | encode(t, bl); |
152 | encode(tag, bl); | |
224ce89b WB |
153 | ENCODE_FINISH(bl); |
154 | } | |
9f95a23c | 155 | void decode(ceph::buffer::list::const_iterator &bl) { |
224ce89b | 156 | DECODE_START_LEGACY_COMPAT_LEN(1, 1, 1, bl); |
11fdf7f2 | 157 | decode(lockers, bl); |
224ce89b | 158 | uint8_t t; |
11fdf7f2 | 159 | decode(t, bl); |
224ce89b | 160 | lock_type = (ClsLockType)t; |
11fdf7f2 | 161 | decode(tag, bl); |
224ce89b WB |
162 | DECODE_FINISH(bl); |
163 | } | |
f67539c2 TL |
164 | |
165 | lock_info_t() : lock_type(ClsLockType::NONE) {} | |
166 | void dump(ceph::Formatter *f) const; | |
167 | static void generate_test_instances(std::list<lock_info_t *>& o); | |
224ce89b | 168 | }; |
11fdf7f2 | 169 | WRITE_CLASS_ENCODER_FEATURES(lock_info_t); |
7c673cae FG |
170 | } |
171 | } | |
172 | } | |
7c673cae FG |
173 | |
174 | #endif |