1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#ifndef CEPH_SIMPLELOCK_H
#define CEPH_SIMPLELOCK_H
// -- lock types --
// see CEPH_LOCK_*
inline const char *get_lock_type_name(int t) {
switch (t) {
case CEPH_LOCK_DN: return "dn";
case CEPH_LOCK_DVERSION: return "dversion";
case CEPH_LOCK_IVERSION: return "iversion";
case CEPH_LOCK_IFILE: return "ifile";
case CEPH_LOCK_IAUTH: return "iauth";
case CEPH_LOCK_ILINK: return "ilink";
case CEPH_LOCK_IDFT: return "idft";
case CEPH_LOCK_INEST: return "inest";
case CEPH_LOCK_IXATTR: return "ixattr";
case CEPH_LOCK_ISNAP: return "isnap";
case CEPH_LOCK_INO: return "ino";
default: assert(0); return 0;
}
}
class Mutation;
extern "C" {
#include "locks.h"
}
#define CAP_ANY 0
#define CAP_LONER 1
#define CAP_XLOCKER 2
struct LockType {
int type;
sm_t *sm;
LockType(int t) : type(t) {
switch (type) {
case CEPH_LOCK_DN:
case CEPH_LOCK_IAUTH:
case CEPH_LOCK_ILINK:
case CEPH_LOCK_IXATTR:
case CEPH_LOCK_ISNAP:
sm = &sm_simplelock;
break;
case CEPH_LOCK_IDFT:
case CEPH_LOCK_INEST:
sm = &sm_scatterlock;
break;
case CEPH_LOCK_IFILE:
sm = &sm_filelock;
break;
case CEPH_LOCK_DVERSION:
case CEPH_LOCK_IVERSION:
sm = &sm_locallock;
break;
default:
sm = 0;
}
}
};
class SimpleLock {
public:
LockType *type;
virtual const char *get_state_name(int n) {
switch (n) {
case LOCK_UNDEF: return "UNDEF";
case LOCK_SYNC: return "sync";
case LOCK_LOCK: return "lock";
case LOCK_PREXLOCK: return "prexlock";
case LOCK_XLOCK: return "xlock";
case LOCK_XLOCKDONE: return "xlockdone";
case LOCK_LOCK_XLOCK: return "lock->prexlock";
case LOCK_SYNC_LOCK: return "sync->lock";
case LOCK_LOCK_SYNC: return "lock->sync";
case LOCK_REMOTEXLOCK: return "remote_xlock";
case LOCK_EXCL: return "excl";
case LOCK_EXCL_SYNC: return "excl->sync";
case LOCK_EXCL_LOCK: return "excl->lock";
case LOCK_SYNC_EXCL: return "sync->excl";
case LOCK_LOCK_EXCL: return "lock->excl";
case LOCK_SYNC_MIX: return "sync->mix";
case LOCK_SYNC_MIX2: return "sync->mix(2)";
case LOCK_LOCK_TSYN: return "lock->tsyn";
case LOCK_MIX_LOCK: return "mix->lock";
case LOCK_MIX: return "mix";
case LOCK_MIX_TSYN: return "mix->tsyn";
case LOCK_TSYN_MIX: return "tsyn->mix";
case LOCK_TSYN_LOCK: return "tsyn->lock";
case LOCK_TSYN: return "tsyn";
case LOCK_MIX_SYNC: return "mix->sync";
case LOCK_MIX_SYNC2: return "mix->sync(2)";
case LOCK_EXCL_MIX: return "excl->mix";
case LOCK_MIX_EXCL: return "mix->excl";
case LOCK_PRE_SCAN: return "*->scan";
case LOCK_SCAN: return "scan";
default: assert(0); return 0;
}
}
// waiting
static const uint64_t WAIT_RD = (1<<0); // to read
static const uint64_t WAIT_WR = (1<<1); // to write
static const uint64_t WAIT_XLOCK = (1<<2); // to xlock (** dup)
static const uint64_t WAIT_STABLE = (1<<2); // for a stable state
static const uint64_t WAIT_REMOTEXLOCK = (1<<3); // for a remote xlock
static const int WAIT_BITS = 4;
static const uint64_t WAIT_ALL = ((1<<WAIT_BITS)-1);
protected:
// parent (what i lock)
MDSCacheObject *parent;
// lock state
__s16 state;
private:
__s16 num_rdlock;
__s32 num_client_lease;
struct unstable_bits_t {
set<__s32> gather_set; // auth+rep. >= 0 is mds, < 0 is client
// local state
int num_wrlock, num_xlock;
Mutation *xlock_by;
client_t xlock_by_client;
client_t excl_client;
bool empty() {
return
gather_set.empty() &&
num_wrlock == 0 &&
num_xlock == 0 &&
xlock_by == NULL &&
xlock_by_client == -1 &&
excl_client == -1;
}
unstable_bits_t() : num_wrlock(0),
num_xlock(0),
xlock_by(NULL),
xlock_by_client(-1),
excl_client(-1) {}
};
mutable unstable_bits_t *_unstable;
bool have_more() const { return _unstable ? true : false; }
unstable_bits_t *more() const {
if (!_unstable)
_unstable = new unstable_bits_t;
return _unstable;
}
void clear_more() {
if (_unstable) {
assert(_unstable->empty());
delete _unstable;
_unstable = NULL;
}
}
void try_clear_more() {
if (_unstable && _unstable->empty()) {
delete _unstable;
_unstable = NULL;
}
}
public:
void set_excl_client(client_t c) { more()->excl_client = c; }
client_t get_excl_client() { return have_more() ? more()->excl_client : -1; }
SimpleLock(MDSCacheObject *o, LockType *lt) :
type(lt),
parent(o),
state(LOCK_SYNC),
num_rdlock(0),
num_client_lease(0),
_unstable(NULL)
{}
virtual ~SimpleLock() {
delete _unstable;
}
// parent
MDSCacheObject *get_parent() { return parent; }
int get_type() { return type->type; }
const sm_t* get_sm() const { return type->sm; }
int get_wait_shift() {
switch (get_type()) {
case CEPH_LOCK_DN: return 8;
case CEPH_LOCK_IAUTH: return 5 + SimpleLock::WAIT_BITS;
case CEPH_LOCK_ILINK: return 5 + SimpleLock::WAIT_BITS;
case CEPH_LOCK_IDFT: return 5 + 2*SimpleLock::WAIT_BITS;
case CEPH_LOCK_IFILE: return 5 + 3*SimpleLock::WAIT_BITS;
case CEPH_LOCK_IVERSION: return 5 + 4*SimpleLock::WAIT_BITS;
case CEPH_LOCK_IXATTR: return 5 + 5*SimpleLock::WAIT_BITS;
case CEPH_LOCK_ISNAP: return 5 + 6*SimpleLock::WAIT_BITS;
case CEPH_LOCK_INEST: return 5 + 7*SimpleLock::WAIT_BITS;
default:
assert(0);
}
}
int get_cap_shift() {
switch (get_type()) {
case CEPH_LOCK_IAUTH: return CEPH_CAP_SAUTH;
case CEPH_LOCK_ILINK: return CEPH_CAP_SLINK;
case CEPH_LOCK_IFILE: return CEPH_CAP_SFILE;
case CEPH_LOCK_IXATTR: return CEPH_CAP_SXATTR;
default: return 0;
}
}
int get_cap_mask() {
switch (get_type()) {
case CEPH_LOCK_IFILE: return 0xffff;
default: return 0x3;
}
}
struct ptr_lt {
bool operator()(const SimpleLock* l, const SimpleLock* r) const {
// first sort by object type (dn < inode)
if ((l->type->type>CEPH_LOCK_DN) < (r->type->type>CEPH_LOCK_DN)) return true;
if ((l->type->type>CEPH_LOCK_DN) == (r->type->type>CEPH_LOCK_DN)) {
// then sort by object
if (l->parent->is_lt(r->parent)) return true;
if (l->parent == r->parent) {
// then sort by (inode) lock type
if (l->type->type < r->type->type) return true;
}
}
return false;
}
};
void decode_locked_state(bufferlist& bl) {
parent->decode_lock_state(type->type, bl);
}
void encode_locked_state(bufferlist& bl) {
parent->encode_lock_state(type->type, bl);
}
void finish_waiters(uint64_t mask, int r=0) {
parent->finish_waiting(mask << get_wait_shift(), r);
}
void take_waiting(uint64_t mask, list<Context*>& ls) {
parent->take_waiting(mask << get_wait_shift(), ls);
}
void add_waiter(uint64_t mask, Context *c) {
parent->add_waiter(mask << get_wait_shift(), c);
}
bool is_waiter_for(uint64_t mask) {
return parent->is_waiter_for(mask << get_wait_shift());
}
// state
int get_state() { return state; }
int set_state(int s) {
state = s;
//assert(!is_stable() || gather_set.size() == 0); // gather should be empty in stable states.
return s;
}
void set_state_rejoin(int s, list<Context*>& waiters) {
if (!is_stable()) {
state = s;
get_parent()->auth_unpin(this);
} else {
state = s;
}
take_waiting(SimpleLock::WAIT_ALL, waiters);
}
bool is_stable() {
return get_sm()->states[state].next == 0;
}
int get_next_state() {
return get_sm()->states[state].next;
}
bool fw_rdlock_to_auth() {
return get_sm()->states[state].can_rdlock == FW;
}
bool req_rdlock_from_auth() {
return get_sm()->states[state].can_rdlock == REQ;
}
// gather set
static set<int> empty_gather_set;
const set<int>& get_gather_set() { return have_more() ? more()->gather_set : empty_gather_set; }
void init_gather() {
for (map<int,int>::const_iterator p = parent->replicas_begin();
p != parent->replicas_end();
++p)
more()->gather_set.insert(p->first);
}
bool is_gathering() { return have_more() && !more()->gather_set.empty(); }
bool is_gathering(int i) {
return have_more() && more()->gather_set.count(i);
}
void clear_gather() {
if (have_more())
more()->gather_set.clear();
}
void remove_gather(int i) {
if (have_more())
more()->gather_set.erase(i);
}
virtual bool is_dirty() { return false; }
// can_*
bool can_lease(client_t client) {
return get_sm()->states[state].can_lease == ANY ||
(get_sm()->states[state].can_lease == AUTH && parent->is_auth()) ||
(get_sm()->states[state].can_lease == XCL && client >= 0 && get_xlock_by_client() == client);
}
bool can_read(client_t client) {
return get_sm()->states[state].can_read == ANY ||
(get_sm()->states[state].can_read == AUTH && parent->is_auth()) ||
(get_sm()->states[state].can_read == XCL && client >= 0 && get_xlock_by_client() == client);
}
bool can_read_projected(client_t client) {
return get_sm()->states[state].can_read_projected == ANY ||
(get_sm()->states[state].can_read_projected == AUTH && parent->is_auth()) ||
(get_sm()->states[state].can_read_projected == XCL && client >= 0 && get_xlock_by_client() == client);
}
bool can_rdlock(client_t client) {
return get_sm()->states[state].can_rdlock == ANY ||
(get_sm()->states[state].can_rdlock == AUTH && parent->is_auth()) ||
(get_sm()->states[state].can_rdlock == XCL && client >= 0 && get_xlock_by_client() == client);
}
bool can_wrlock(client_t client) {
return get_sm()->states[state].can_wrlock == ANY ||
(get_sm()->states[state].can_wrlock == AUTH && parent->is_auth()) ||
(get_sm()->states[state].can_wrlock == XCL && client >= 0 && (get_xlock_by_client() == client ||
get_excl_client() == client));
}
bool can_force_wrlock(client_t client) {
return get_sm()->states[state].can_force_wrlock == ANY ||
(get_sm()->states[state].can_force_wrlock == AUTH && parent->is_auth()) ||
(get_sm()->states[state].can_force_wrlock == XCL && client >= 0 && (get_xlock_by_client() == client ||
get_excl_client() == client));
}
bool can_xlock(client_t client) {
return get_sm()->states[state].can_xlock == ANY ||
(get_sm()->states[state].can_xlock == AUTH && parent->is_auth()) ||
(get_sm()->states[state].can_xlock == XCL && client >= 0 && get_xlock_by_client() == client);
}
// rdlock
bool is_rdlocked() { return num_rdlock > 0; }
int get_rdlock() {
if (!num_rdlock)
parent->get(MDSCacheObject::PIN_LOCK);
return ++num_rdlock;
}
int put_rdlock() {
assert(num_rdlock>0);
--num_rdlock;
if (num_rdlock == 0)
parent->put(MDSCacheObject::PIN_LOCK);
return num_rdlock;
}
int get_num_rdlocks() { return num_rdlock; }
// wrlock
void get_wrlock(bool force=false) {
//assert(can_wrlock() || force);
if (more()->num_wrlock == 0)
parent->get(MDSCacheObject::PIN_LOCK);
++more()->num_wrlock;
}
void put_wrlock() {
--more()->num_wrlock;
if (more()->num_wrlock == 0) {
parent->put(MDSCacheObject::PIN_LOCK);
try_clear_more();
}
}
bool is_wrlocked() { return have_more() && more()->num_wrlock > 0; }
int get_num_wrlocks() { return have_more() ? more()->num_wrlock : 0; }
// xlock
void get_xlock(Mutation *who, client_t client) {
assert(get_xlock_by() == 0);
assert(state == LOCK_XLOCK);
parent->get(MDSCacheObject::PIN_LOCK);
more()->num_xlock++;
more()->xlock_by = who;
more()->xlock_by_client = client;
}
void set_xlock_done() {
assert(more()->xlock_by);
assert(state == LOCK_XLOCK);
state = LOCK_XLOCKDONE;
more()->xlock_by = 0;
}
void put_xlock() {
assert(state == LOCK_XLOCK || state == LOCK_XLOCKDONE);
--more()->num_xlock;
parent->put(MDSCacheObject::PIN_LOCK);
if (more()->num_xlock == 0) {
more()->xlock_by = 0;
more()->xlock_by_client = -1;
try_clear_more();
}
}
bool is_xlocked() { return have_more() && more()->num_xlock > 0; }
int get_num_xlocks() { return have_more() ? more()->num_xlock : 0; }
client_t get_xlock_by_client() {
return have_more() ? more()->xlock_by_client : -1;
}
bool is_xlocked_by_client(client_t c) {
return have_more() ? more()->xlock_by_client == c : false;
}
Mutation *get_xlock_by() { return have_more() ? more()->xlock_by : NULL; }
// lease
void get_client_lease() {
num_client_lease++;
}
void put_client_lease() {
assert(num_client_lease > 0);
num_client_lease--;
if (num_client_lease == 0) {
try_clear_more();
}
}
bool is_leased() { return num_client_lease > 0; }
int get_num_client_lease() {
return num_client_lease;
}
bool is_used() {
return is_xlocked() || is_rdlocked() || is_wrlocked() || num_client_lease;
}
// encode/decode
void encode(bufferlist& bl) const {
__u8 struct_v = 1;
::encode(struct_v, bl);
::encode(state, bl);
if (have_more())
::encode(more()->gather_set, bl);
else
::encode(empty_gather_set, bl);
}
void decode(bufferlist::iterator& p) {
__u8 struct_v;
::decode(struct_v, p);
::decode(state, p);
set<int> g;
::decode(g, p);
if (!g.empty())
more()->gather_set.swap(g);
}
void encode_state_for_replica(bufferlist& bl) const {
__s16 s = get_replica_state();
::encode(s, bl);
}
void decode_state(bufferlist::iterator& p, bool is_new=true) {
__s16 s;
::decode(s, p);
if (is_new)
state = s;
}
void decode_state_rejoin(bufferlist::iterator& p, list<Context*>& waiters) {
__s16 s;
::decode(s, p);
set_state_rejoin(s, waiters);
}
// caps
bool is_loner_mode() {
return get_sm()->states[state].loner;
}
int gcaps_allowed_ever() {
return parent->is_auth() ? get_sm()->allowed_ever_auth : get_sm()->allowed_ever_replica;
}
int gcaps_allowed(int who, int s=-1) {
if (s < 0) s = state;
if (parent->is_auth()) {
if (get_xlock_by_client() >= 0 && who == CAP_XLOCKER)
return get_sm()->states[s].xlocker_caps;
else if (is_loner_mode() && who == CAP_ANY)
return get_sm()->states[s].caps;
else
return get_sm()->states[s].loner_caps | get_sm()->states[s].caps; // loner always gets more
} else
return get_sm()->states[s].replica_caps;
}
int gcaps_careful() {
if (get_num_wrlocks())
return get_sm()->careful;
return 0;
}
int gcaps_xlocker_mask(client_t client) {
if (client == get_xlock_by_client())
return type->type == CEPH_LOCK_IFILE ? 0xffff : (CEPH_CAP_GSHARED|CEPH_CAP_GEXCL);
return 0;
}
// simplelock specifics
int get_replica_state() const {
return get_sm()->states[state].replica_state;
}
void export_twiddle() {
clear_gather();
state = get_replica_state();
}
/** replicate_relax
* called on first replica creation.
*/
void replicate_relax() {
assert(parent->is_auth());
assert(!parent->is_replicated());
if (state == LOCK_LOCK && !is_used())
state = LOCK_SYNC;
}
bool remove_replica(int from) {
if (is_gathering(from)) {
remove_gather(from);
if (!is_gathering())
return true;
}
return false;
}
bool do_import(int from, int to) {
if (!is_stable()) {
remove_gather(from);
remove_gather(to);
if (!is_gathering())
return true;
}
if (!is_stable() && !is_gathering())
return true;
return false;
}
void _print(ostream& out) {
out << get_lock_type_name(get_type()) << " ";
out << get_state_name(get_state());
if (!get_gather_set().empty())
out << " g=" << get_gather_set();
if (num_client_lease)
out << " l=" << num_client_lease;
if (is_rdlocked())
out << " r=" << get_num_rdlocks();
if (is_wrlocked())
out << " w=" << get_num_wrlocks();
if (is_xlocked()) {
out << " x=" << get_num_xlocks();
if (get_xlock_by())
out << " by " << get_xlock_by();
}
/*if (is_stable())
out << " stable";
else
out << " unstable";
*/
}
virtual void print(ostream& out) {
out << "(";
_print(out);
out << ")";
}
};
WRITE_CLASS_ENCODER(SimpleLock)
inline ostream& operator<<(ostream& out, SimpleLock& l)
{
l.print(out);
return out;
}
#endif
|