1 #ifndef CEPH_MDSCACHEOBJECT_H
2 #define CEPH_MDSCACHEOBJECT_H
6 #include "common/config.h"
8 #include "include/Context.h"
9 #include "include/alloc_ptr.h"
10 #include "include/assert.h"
11 #include "include/mempool.h"
12 #include "include/types.h"
13 #include "include/xlist.h"
17 #define MDS_REF_SET // define me for improved debug output, sanity checking
18 //#define MDS_AUTHPIN_SET // define me for debugging auth pin leaks
19 //#define MDS_VERIFY_FRAGSTAT // do (slow) sanity checking on frags
25 class MDSInternalContextBase
;
28 * for metadata leases to clients
32 MDSCacheObject
*parent
;
36 xlist
<ClientLease
*>::item item_session_lease
; // per-session list
37 xlist
<ClientLease
*>::item item_lease
; // global list
39 ClientLease(client_t c
, MDSCacheObject
*p
) :
41 item_session_lease(this),
43 ClientLease() = delete;
48 struct mdsco_db_line_prefix
{
49 MDSCacheObject
*object
;
50 explicit mdsco_db_line_prefix(MDSCacheObject
*o
) : object(o
) {}
52 std::ostream
& operator<<(std::ostream
& out
, const mdsco_db_line_prefix
& o
);
55 std::ostream
& operator<<(std::ostream
& out
, const MDSCacheObject
&o
);
57 class MDSCacheObject
{
60 const static int PIN_REPLICATED
= 1000;
61 const static int PIN_DIRTY
= 1001;
62 const static int PIN_LOCK
= -1002;
63 const static int PIN_REQUEST
= -1003;
64 const static int PIN_WAITER
= 1004;
65 const static int PIN_DIRTYSCATTERED
= -1005;
66 static const int PIN_AUTHPIN
= 1006;
67 static const int PIN_PTRWAITER
= -1007;
68 const static int PIN_TEMPEXPORTING
= 1008; // temp pin between encode_ and finish_export
69 static const int PIN_CLIENTLEASE
= 1009;
70 static const int PIN_DISCOVERBASE
= 1010;
72 const char *generic_pin_name(int p
) const {
74 case PIN_REPLICATED
: return "replicated";
75 case PIN_DIRTY
: return "dirty";
76 case PIN_LOCK
: return "lock";
77 case PIN_REQUEST
: return "request";
78 case PIN_WAITER
: return "waiter";
79 case PIN_DIRTYSCATTERED
: return "dirtyscattered";
80 case PIN_AUTHPIN
: return "authpin";
81 case PIN_PTRWAITER
: return "ptrwaiter";
82 case PIN_TEMPEXPORTING
: return "tempexporting";
83 case PIN_CLIENTLEASE
: return "clientlease";
84 case PIN_DISCOVERBASE
: return "discoverbase";
85 default: ceph_abort(); return 0;
90 const static int STATE_AUTH
= (1<<30);
91 const static int STATE_DIRTY
= (1<<29);
92 const static int STATE_NOTIFYREF
= (1<<28); // notify dropping ref drop through _put()
93 const static int STATE_REJOINING
= (1<<27); // replica has not joined w/ primary copy
94 const static int STATE_REJOINUNDEF
= (1<<26); // contents undefined.
98 const static uint64_t WAIT_ORDERED
= (1ull<<61);
99 const static uint64_t WAIT_SINGLEAUTH
= (1ull<<60);
100 const static uint64_t WAIT_UNFREEZE
= (1ull<<59); // pka AUTHPINNABLE
103 // ============================================
107 virtual ~MDSCacheObject() {}
110 virtual void print(std::ostream
& out
) = 0;
111 virtual std::ostream
& print_db_line_prefix(std::ostream
& out
) {
112 return out
<< "mdscacheobject(" << this << ") ";
115 // --------------------------------------------
118 __u32 state
= 0; // state bits
121 unsigned get_state() const { return state
; }
122 unsigned state_test(unsigned mask
) const { return (state
& mask
); }
123 void state_clear(unsigned mask
) { state
&= ~mask
; }
124 void state_set(unsigned mask
) { state
|= mask
; }
125 void state_reset(unsigned s
) { state
= s
; }
127 bool is_auth() const { return state_test(STATE_AUTH
); }
128 bool is_dirty() const { return state_test(STATE_DIRTY
); }
129 bool is_clean() const { return !is_dirty(); }
130 bool is_rejoining() const { return state_test(STATE_REJOINING
); }
132 // --------------------------------------------
134 virtual mds_authority_t
authority() const = 0;
135 bool is_ambiguous_auth() const {
136 return authority().second
!= CDIR_AUTH_UNKNOWN
;
139 // --------------------------------------------
142 __s32 ref
= 0; // reference count
144 mempool::mds_co::map
<int,int> ref_map
;
148 int get_num_ref(int by
= -1) const {
151 if (ref_map
.find(by
) == ref_map
.end()) {
154 return ref_map
.find(by
)->second
;
160 virtual const char *pin_name(int by
) const = 0;
161 //bool is_pinned_by(int by) { return ref_set.count(by); }
162 //multiset<int>& get_ref_set() { return ref_set; }
164 virtual void last_put() {}
165 virtual void bad_put(int by
) {
167 assert(ref_map
[by
] > 0);
171 virtual void _put() {}
174 if (ref
== 0 || ref_map
[by
] == 0) {
186 if (state_test(STATE_NOTIFYREF
))
191 virtual void first_get() {}
192 virtual void bad_get(int by
) {
194 assert(by
< 0 || ref_map
[by
] == 0);
203 if (ref_map
.find(by
) == ref_map
.end())
209 void print_pin_set(std::ostream
& out
) const {
211 std::map
<int, int>::const_iterator it
= ref_map
.begin();
212 while (it
!= ref_map
.end()) {
213 out
<< " " << pin_name(it
->first
) << "=" << it
->second
;
217 out
<< " nref=" << ref
;
223 int nested_auth_pins
= 0;
224 #ifdef MDS_AUTHPIN_SET
225 mempool::mds_co::multiset
<void*> auth_pin_set
;
229 bool is_auth_pinned() const { return auth_pins
|| nested_auth_pins
; }
230 int get_num_auth_pins() const { return auth_pins
; }
231 int get_num_nested_auth_pins() const { return nested_auth_pins
; }
233 void dump_states(Formatter
*f
) const;
234 void dump(Formatter
*f
) const;
236 // --------------------------------------------
238 virtual bool can_auth_pin() const = 0;
239 virtual void auth_pin(void *who
) = 0;
240 virtual void auth_unpin(void *who
) = 0;
241 virtual bool is_frozen() const = 0;
242 virtual bool is_freezing() const = 0;
243 virtual bool is_freezing_or_frozen() const {
244 return is_frozen() || is_freezing();
248 // --------------------------------------------
249 // replication (across mds cluster)
251 unsigned replica_nonce
= 0; // [replica] defined on replica
252 typedef mempool::mds_co::compact_map
<mds_rank_t
,unsigned> replica_map_type
;
253 replica_map_type replica_map
; // [auth] mds -> nonce
256 bool is_replicated() const { return !get_replicas().empty(); }
257 bool is_replica(mds_rank_t mds
) const { return get_replicas().count(mds
); }
258 int num_replicas() const { return get_replicas().size(); }
259 unsigned add_replica(mds_rank_t mds
) {
260 if (get_replicas().count(mds
))
261 return ++get_replicas()[mds
]; // inc nonce
262 if (get_replicas().empty())
264 return get_replicas()[mds
] = 1;
266 void add_replica(mds_rank_t mds
, unsigned nonce
) {
267 if (get_replicas().empty())
269 get_replicas()[mds
] = nonce
;
271 unsigned get_replica_nonce(mds_rank_t mds
) {
272 assert(get_replicas().count(mds
));
273 return get_replicas()[mds
];
275 void remove_replica(mds_rank_t mds
) {
276 assert(get_replicas().count(mds
));
277 get_replicas().erase(mds
);
278 if (get_replicas().empty()) {
282 void clear_replica_map() {
283 if (!get_replicas().empty())
287 replica_map_type
& get_replicas() { return replica_map
; }
288 const replica_map_type
& get_replicas() const { return replica_map
; }
289 void list_replicas(std::set
<mds_rank_t
>& ls
) const {
290 for (const auto &p
: get_replicas()) {
295 unsigned get_replica_nonce() const { return replica_nonce
; }
296 void set_replica_nonce(unsigned n
) { replica_nonce
= n
; }
299 // ---------------------------------------------
302 alloc_ptr
<mempool::mds_co::multimap
<uint64_t, std::pair
<uint64_t, MDSInternalContextBase
*>>> waiting
;
303 static uint64_t last_wait_seq
;
306 bool is_waiter_for(uint64_t mask
, uint64_t min
=0) {
309 while (min
& (min
-1)) // if more than one bit is set
310 min
&= min
-1; // clear LSB
313 for (auto p
= waiting
->lower_bound(min
); p
!= waiting
->end(); ++p
) {
314 if (p
->first
& mask
) return true;
315 if (p
->first
> mask
) return false;
320 virtual void add_waiter(uint64_t mask
, MDSInternalContextBase
*c
) {
321 if (waiting
->empty())
325 if (mask
& WAIT_ORDERED
) {
326 seq
= ++last_wait_seq
;
327 mask
&= ~WAIT_ORDERED
;
329 waiting
->insert(pair
<uint64_t, pair
<uint64_t, MDSInternalContextBase
*> >(
331 pair
<uint64_t, MDSInternalContextBase
*>(seq
, c
)));
332 // pdout(10,g_conf->debug_mds) << (mdsco_db_line_prefix(this))
333 // << "add_waiter " << hex << mask << dec << " " << c
334 // << " on " << *this
338 virtual void take_waiting(uint64_t mask
, std::list
<MDSInternalContextBase
*>& ls
) {
339 if (!waiting
|| waiting
->empty()) return;
341 // process ordered waiters in the same order that they were added.
342 std::map
<uint64_t, MDSInternalContextBase
*> ordered_waiters
;
344 for (auto it
= waiting
->begin(); it
!= waiting
->end(); ) {
345 if (it
->first
& mask
) {
346 if (it
->second
.first
> 0) {
347 ordered_waiters
.insert(it
->second
);
349 ls
.push_back(it
->second
.second
);
351 // pdout(10,g_conf->debug_mds) << (mdsco_db_line_prefix(this))
352 // << "take_waiting mask " << hex << mask << dec << " took " << it->second
353 // << " tag " << hex << it->first << dec
354 // << " on " << *this
356 waiting
->erase(it
++);
358 // pdout(10,g_conf->debug_mds) << "take_waiting mask " << hex << mask << dec << " SKIPPING " << it->second
359 // << " tag " << hex << it->first << dec
360 // << " on " << *this
365 for (auto it
= ordered_waiters
.begin(); it
!= ordered_waiters
.end(); ++it
) {
366 ls
.push_back(it
->second
);
368 if (waiting
->empty()) {
373 void finish_waiting(uint64_t mask
, int result
= 0);
375 // ---------------------------------------------
377 // noop unless overloaded.
378 virtual SimpleLock
* get_lock(int type
) { ceph_abort(); return 0; }
379 virtual void set_object_info(MDSCacheObjectInfo
&info
) { ceph_abort(); }
380 virtual void encode_lock_state(int type
, bufferlist
& bl
) { ceph_abort(); }
381 virtual void decode_lock_state(int type
, bufferlist
& bl
) { ceph_abort(); }
382 virtual void finish_lock_waiters(int type
, uint64_t mask
, int r
=0) { ceph_abort(); }
383 virtual void add_lock_waiter(int type
, uint64_t mask
, MDSInternalContextBase
*c
) { ceph_abort(); }
384 virtual bool is_lock_waiting(int type
, uint64_t mask
) { ceph_abort(); return false; }
386 virtual void clear_dirty_scattered(int type
) { ceph_abort(); }
388 // ---------------------------------------------
390 virtual bool is_lt(const MDSCacheObject
*r
) const = 0;
392 bool operator()(const MDSCacheObject
* l
, const MDSCacheObject
* r
) const {
399 inline std::ostream
& operator<<(std::ostream
& out
, MDSCacheObject
&o
) {
404 inline std::ostream
& operator<<(std::ostream
& out
, const mdsco_db_line_prefix
& o
) {
405 o
.object
->print_db_line_prefix(out
);