-
Notifications
You must be signed in to change notification settings - Fork 1
/
Locker.cc
5211 lines (4499 loc) · 157 KB
/
Locker.cc
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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- 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.
*
*/
#include "MDSRank.h"
#include "MDCache.h"
#include "Locker.h"
#include "CInode.h"
#include "CDir.h"
#include "CDentry.h"
#include "Mutation.h"
#include "MDSContext.h"
#include "MDLog.h"
#include "MDSMap.h"
#include "events/EUpdate.h"
#include "events/EOpen.h"
#include "msg/Messenger.h"
#include "osdc/Objecter.h"
#include "messages/MInodeFileCaps.h"
#include "messages/MLock.h"
#include "messages/MClientLease.h"
#include "messages/MClientReply.h"
#include "messages/MClientCaps.h"
#include "messages/MClientCapRelease.h"
#include "messages/MMDSSlaveRequest.h"
#include <errno.h>
#include "common/config.h"
#define dout_subsys ceph_subsys_mds
#undef dout_prefix
#undef DOUT_COND
#define DOUT_COND(cct, l) l<=cct->_conf->debug_mds || l <= cct->_conf->debug_mds_locker
#define dout_prefix _prefix(_dout, mds)
static ostream& _prefix(std::ostream *_dout, MDSRank *mds) {
return *_dout << "mds." << mds->get_nodeid() << ".locker ";
}
class LockerContext : public MDSInternalContextBase {
protected:
Locker *locker;
MDSRank *get_mds()
{
return locker->mds;
}
public:
explicit LockerContext(Locker *locker_) : locker(locker_) {
assert(locker != NULL);
}
};
class LockerLogContext : public MDSLogContextBase {
protected:
Locker *locker;
MDSRank *get_mds()
{
return locker->mds;
}
public:
explicit LockerLogContext(Locker *locker_) : locker(locker_) {
assert(locker != NULL);
}
};
/* This function DOES put the passed message before returning */
void Locker::dispatch(Message *m)
{
switch (m->get_type()) {
// inter-mds locking
case MSG_MDS_LOCK:
handle_lock(static_cast<MLock*>(m));
break;
// inter-mds caps
case MSG_MDS_INODEFILECAPS:
handle_inode_file_caps(static_cast<MInodeFileCaps*>(m));
break;
// client sync
case CEPH_MSG_CLIENT_CAPS:
handle_client_caps(static_cast<MClientCaps*>(m));
break;
case CEPH_MSG_CLIENT_CAPRELEASE:
handle_client_cap_release(static_cast<MClientCapRelease*>(m));
break;
case CEPH_MSG_CLIENT_LEASE:
handle_client_lease(static_cast<MClientLease*>(m));
break;
default:
derr << "locker unknown message " << m->get_type() << dendl;
assert(0 == "locker unknown message");
}
}
void Locker::tick()
{
scatter_tick();
caps_tick();
}
/*
* locks vs rejoin
*
*
*
*/
void Locker::send_lock_message(SimpleLock *lock, int msg)
{
for (compact_map<mds_rank_t,unsigned>::iterator it = lock->get_parent()->replicas_begin();
it != lock->get_parent()->replicas_end();
++it) {
if (mds->mdsmap->get_state(it->first) < MDSMap::STATE_REJOIN)
continue;
MLock *m = new MLock(lock, msg, mds->get_nodeid());
mds->send_message_mds(m, it->first);
}
}
void Locker::send_lock_message(SimpleLock *lock, int msg, const bufferlist &data)
{
for (compact_map<mds_rank_t,unsigned>::iterator it = lock->get_parent()->replicas_begin();
it != lock->get_parent()->replicas_end();
++it) {
if (mds->mdsmap->get_state(it->first) < MDSMap::STATE_REJOIN)
continue;
MLock *m = new MLock(lock, msg, mds->get_nodeid());
m->set_data(data);
mds->send_message_mds(m, it->first);
}
}
void Locker::include_snap_rdlocks(set<SimpleLock*>& rdlocks, CInode *in)
{
// rdlock ancestor snaps
CInode *t = in;
rdlocks.insert(&in->snaplock);
while (t->get_projected_parent_dn()) {
t = t->get_projected_parent_dn()->get_dir()->get_inode();
rdlocks.insert(&t->snaplock);
}
}
void Locker::include_snap_rdlocks_wlayout(set<SimpleLock*>& rdlocks, CInode *in,
file_layout_t **layout)
{
//rdlock ancestor snaps
CInode *t = in;
rdlocks.insert(&in->snaplock);
rdlocks.insert(&in->policylock);
bool found_layout = false;
while (t) {
rdlocks.insert(&t->snaplock);
if (!found_layout) {
rdlocks.insert(&t->policylock);
if (t->get_projected_inode()->has_layout()) {
*layout = &t->get_projected_inode()->layout;
found_layout = true;
}
}
if (t->get_projected_parent_dn() &&
t->get_projected_parent_dn()->get_dir())
t = t->get_projected_parent_dn()->get_dir()->get_inode();
else t = NULL;
}
}
struct MarkEventOnDestruct {
MDRequestRef& mdr;
const char* message;
bool mark_event;
MarkEventOnDestruct(MDRequestRef& _mdr,
const char *_message) : mdr(_mdr),
message(_message),
mark_event(true) {}
~MarkEventOnDestruct() {
if (mark_event)
mdr->mark_event(message);
}
};
/* If this function returns false, the mdr has been placed
* on the appropriate wait list */
bool Locker::acquire_locks(MDRequestRef& mdr,
set<SimpleLock*> &rdlocks,
set<SimpleLock*> &wrlocks,
set<SimpleLock*> &xlocks,
map<SimpleLock*,mds_rank_t> *remote_wrlocks,
CInode *auth_pin_freeze,
bool auth_pin_nonblock)
{
if (mdr->done_locking &&
!mdr->is_slave()) { // not on slaves! master requests locks piecemeal.
dout(10) << "acquire_locks " << *mdr << " - done locking" << dendl;
return true; // at least we had better be!
}
dout(10) << "acquire_locks " << *mdr << dendl;
MarkEventOnDestruct marker(mdr, "failed to acquire_locks");
client_t client = mdr->get_client();
set<SimpleLock*, SimpleLock::ptr_lt> sorted; // sort everything we will lock
set<MDSCacheObject*> mustpin; // items to authpin
// xlocks
for (set<SimpleLock*>::iterator p = xlocks.begin(); p != xlocks.end(); ++p) {
dout(20) << " must xlock " << **p << " " << *(*p)->get_parent() << dendl;
sorted.insert(*p);
mustpin.insert((*p)->get_parent());
// augment xlock with a versionlock?
if ((*p)->get_type() == CEPH_LOCK_DN) {
CDentry *dn = (CDentry*)(*p)->get_parent();
if (!dn->is_auth())
continue;
if (xlocks.count(&dn->versionlock))
continue; // we're xlocking the versionlock too; don't wrlock it!
if (mdr->is_master()) {
// master. wrlock versionlock so we can pipeline dentry updates to journal.
wrlocks.insert(&dn->versionlock);
} else {
// slave. exclusively lock the dentry version (i.e. block other journal updates).
// this makes rollback safe.
xlocks.insert(&dn->versionlock);
sorted.insert(&dn->versionlock);
}
}
if ((*p)->get_type() > CEPH_LOCK_IVERSION) {
// inode version lock?
CInode *in = (CInode*)(*p)->get_parent();
if (!in->is_auth())
continue;
if (mdr->is_master()) {
// master. wrlock versionlock so we can pipeline inode updates to journal.
wrlocks.insert(&in->versionlock);
} else {
// slave. exclusively lock the inode version (i.e. block other journal updates).
// this makes rollback safe.
xlocks.insert(&in->versionlock);
sorted.insert(&in->versionlock);
}
}
}
// wrlocks
for (set<SimpleLock*>::iterator p = wrlocks.begin(); p != wrlocks.end(); ++p) {
MDSCacheObject *object = (*p)->get_parent();
dout(20) << " must wrlock " << **p << " " << *object << dendl;
sorted.insert(*p);
if (object->is_auth())
mustpin.insert(object);
else if (!object->is_auth() &&
!(*p)->can_wrlock(client) && // we might have to request a scatter
!mdr->is_slave()) { // if we are slave (remote_wrlock), the master already authpinned
dout(15) << " will also auth_pin " << *object
<< " in case we need to request a scatter" << dendl;
mustpin.insert(object);
}
}
// remote_wrlocks
if (remote_wrlocks) {
for (map<SimpleLock*,mds_rank_t>::iterator p = remote_wrlocks->begin(); p != remote_wrlocks->end(); ++p) {
MDSCacheObject *object = p->first->get_parent();
dout(20) << " must remote_wrlock on mds." << p->second << " "
<< *p->first << " " << *object << dendl;
sorted.insert(p->first);
mustpin.insert(object);
}
}
// rdlocks
for (set<SimpleLock*>::iterator p = rdlocks.begin();
p != rdlocks.end();
++p) {
MDSCacheObject *object = (*p)->get_parent();
dout(20) << " must rdlock " << **p << " " << *object << dendl;
sorted.insert(*p);
if (object->is_auth())
mustpin.insert(object);
else if (!object->is_auth() &&
!(*p)->can_rdlock(client)) { // we might have to request an rdlock
dout(15) << " will also auth_pin " << *object
<< " in case we need to request a rdlock" << dendl;
mustpin.insert(object);
}
}
// AUTH PINS
map<mds_rank_t, set<MDSCacheObject*> > mustpin_remote; // mds -> (object set)
// can i auth pin them all now?
marker.message = "failed to authpin local pins";
for (set<MDSCacheObject*>::iterator p = mustpin.begin();
p != mustpin.end();
++p) {
MDSCacheObject *object = *p;
dout(10) << " must authpin " << *object << dendl;
if (mdr->is_auth_pinned(object))
continue;
if (!object->is_auth()) {
if (!mdr->locks.empty())
mds->locker->drop_locks(mdr.get());
if (object->is_ambiguous_auth()) {
// wait
dout(10) << " ambiguous auth, waiting to authpin " << *object << dendl;
object->add_waiter(MDSCacheObject::WAIT_SINGLEAUTH, new C_MDS_RetryRequest(mdcache, mdr));
mdr->drop_local_auth_pins();
return false;
}
mustpin_remote[object->authority().first].insert(object);
continue;
}
if (!object->can_auth_pin()) {
// wait
mds->locker->drop_locks(mdr.get());
mdr->drop_local_auth_pins();
if (auth_pin_nonblock) {
dout(10) << " can't auth_pin (freezing?) " << *object << ", nonblocking" << dendl;
mdr->aborted = true;
return false;
}
dout(10) << " can't auth_pin (freezing?), waiting to authpin " << *object << dendl;
object->add_waiter(MDSCacheObject::WAIT_UNFREEZE, new C_MDS_RetryRequest(mdcache, mdr));
return false;
}
}
// ok, grab local auth pins
for (set<MDSCacheObject*>::iterator p = mustpin.begin();
p != mustpin.end();
++p) {
MDSCacheObject *object = *p;
if (mdr->is_auth_pinned(object)) {
dout(10) << " already auth_pinned " << *object << dendl;
} else if (object->is_auth()) {
dout(10) << " auth_pinning " << *object << dendl;
mdr->auth_pin(object);
}
}
// request remote auth_pins
if (!mustpin_remote.empty()) {
marker.message = "requesting remote authpins";
for (map<MDSCacheObject*,mds_rank_t>::iterator p = mdr->remote_auth_pins.begin();
p != mdr->remote_auth_pins.end();
++p) {
if (mustpin.count(p->first)) {
assert(p->second == p->first->authority().first);
map<mds_rank_t, set<MDSCacheObject*> >::iterator q = mustpin_remote.find(p->second);
if (q != mustpin_remote.end())
q->second.insert(p->first);
}
}
for (map<mds_rank_t, set<MDSCacheObject*> >::iterator p = mustpin_remote.begin();
p != mustpin_remote.end();
++p) {
dout(10) << "requesting remote auth_pins from mds." << p->first << dendl;
// wait for active auth
if (!mds->mdsmap->is_clientreplay_or_active_or_stopping(p->first)) {
dout(10) << " mds." << p->first << " is not active" << dendl;
if (mdr->more()->waiting_on_slave.empty())
mds->wait_for_active_peer(p->first, new C_MDS_RetryRequest(mdcache, mdr));
return false;
}
MMDSSlaveRequest *req = new MMDSSlaveRequest(mdr->reqid, mdr->attempt,
MMDSSlaveRequest::OP_AUTHPIN);
for (set<MDSCacheObject*>::iterator q = p->second.begin();
q != p->second.end();
++q) {
dout(10) << " req remote auth_pin of " << **q << dendl;
MDSCacheObjectInfo info;
(*q)->set_object_info(info);
req->get_authpins().push_back(info);
if (*q == auth_pin_freeze)
(*q)->set_object_info(req->get_authpin_freeze());
mdr->pin(*q);
}
if (auth_pin_nonblock)
req->mark_nonblock();
mds->send_message_mds(req, p->first);
// put in waiting list
assert(mdr->more()->waiting_on_slave.count(p->first) == 0);
mdr->more()->waiting_on_slave.insert(p->first);
}
return false;
}
// caps i'll need to issue
set<CInode*> issue_set;
bool result = false;
// acquire locks.
// make sure they match currently acquired locks.
set<SimpleLock*, SimpleLock::ptr_lt>::iterator existing = mdr->locks.begin();
for (set<SimpleLock*, SimpleLock::ptr_lt>::iterator p = sorted.begin();
p != sorted.end();
++p) {
bool need_wrlock = !!wrlocks.count(*p);
bool need_remote_wrlock = !!(remote_wrlocks && remote_wrlocks->count(*p));
// already locked?
if (existing != mdr->locks.end() && *existing == *p) {
// right kind?
SimpleLock *have = *existing;
++existing;
if (xlocks.count(have) && mdr->xlocks.count(have)) {
dout(10) << " already xlocked " << *have << " " << *have->get_parent() << dendl;
continue;
}
if (mdr->remote_wrlocks.count(have)) {
if (!need_remote_wrlock ||
mdr->remote_wrlocks[have] != (*remote_wrlocks)[have]) {
dout(10) << " unlocking remote_wrlock on wrong mds." << mdr->remote_wrlocks[have]
<< " " << *have << " " << *have->get_parent() << dendl;
remote_wrlock_finish(have, mdr->remote_wrlocks[have], mdr.get());
}
}
if (need_wrlock || need_remote_wrlock) {
if (need_wrlock == !!mdr->wrlocks.count(have) &&
need_remote_wrlock == !!mdr->remote_wrlocks.count(have)) {
if (need_wrlock)
dout(10) << " already wrlocked " << *have << " " << *have->get_parent() << dendl;
if (need_remote_wrlock)
dout(10) << " already remote_wrlocked " << *have << " " << *have->get_parent() << dendl;
continue;
}
}
if (rdlocks.count(have) && mdr->rdlocks.count(have)) {
dout(10) << " already rdlocked " << *have << " " << *have->get_parent() << dendl;
continue;
}
}
// hose any stray locks
if (*existing == *p) {
assert(need_wrlock || need_remote_wrlock);
SimpleLock *lock = *existing;
if (mdr->wrlocks.count(lock)) {
if (!need_wrlock)
dout(10) << " unlocking extra " << *lock << " " << *lock->get_parent() << dendl;
else if (need_remote_wrlock) // acquire remote_wrlock first
dout(10) << " unlocking out-of-order " << *lock << " " << *lock->get_parent() << dendl;
bool need_issue = false;
wrlock_finish(lock, mdr.get(), &need_issue);
if (need_issue)
issue_set.insert(static_cast<CInode*>(lock->get_parent()));
}
++existing;
}
while (existing != mdr->locks.end()) {
SimpleLock *stray = *existing;
++existing;
dout(10) << " unlocking out-of-order " << *stray << " " << *stray->get_parent() << dendl;
bool need_issue = false;
if (mdr->xlocks.count(stray)) {
xlock_finish(stray, mdr.get(), &need_issue);
} else if (mdr->rdlocks.count(stray)) {
rdlock_finish(stray, mdr.get(), &need_issue);
} else {
// may have acquired both wrlock and remore wrlock
if (mdr->wrlocks.count(stray))
wrlock_finish(stray, mdr.get(), &need_issue);
if (mdr->remote_wrlocks.count(stray))
remote_wrlock_finish(stray, mdr->remote_wrlocks[stray], mdr.get());
}
if (need_issue)
issue_set.insert(static_cast<CInode*>(stray->get_parent()));
}
// lock
if (mdr->locking && *p != mdr->locking) {
cancel_locking(mdr.get(), &issue_set);
}
if (xlocks.count(*p)) {
marker.message = "failed to xlock, waiting";
if (!xlock_start(*p, mdr))
goto out;
dout(10) << " got xlock on " << **p << " " << *(*p)->get_parent() << dendl;
} else if (need_wrlock || need_remote_wrlock) {
if (need_remote_wrlock && !mdr->remote_wrlocks.count(*p)) {
marker.message = "waiting for remote wrlocks";
remote_wrlock_start(*p, (*remote_wrlocks)[*p], mdr);
goto out;
}
if (need_wrlock && !mdr->wrlocks.count(*p)) {
marker.message = "failed to wrlock, waiting";
if (need_remote_wrlock && !(*p)->can_wrlock(mdr->get_client())) {
marker.message = "failed to wrlock, dropping remote wrlock and waiting";
// can't take the wrlock because the scatter lock is gathering. need to
// release the remote wrlock, so that the gathering process can finish.
remote_wrlock_finish(*p, mdr->remote_wrlocks[*p], mdr.get());
remote_wrlock_start(*p, (*remote_wrlocks)[*p], mdr);
goto out;
}
if (!wrlock_start(*p, mdr))
goto out;
dout(10) << " got wrlock on " << **p << " " << *(*p)->get_parent() << dendl;
}
} else {
marker.message = "failed to rdlock, waiting";
if (!rdlock_start(*p, mdr))
goto out;
dout(10) << " got rdlock on " << **p << " " << *(*p)->get_parent() << dendl;
}
}
// any extra unneeded locks?
while (existing != mdr->locks.end()) {
SimpleLock *stray = *existing;
++existing;
dout(10) << " unlocking extra " << *stray << " " << *stray->get_parent() << dendl;
bool need_issue = false;
if (mdr->xlocks.count(stray)) {
xlock_finish(stray, mdr.get(), &need_issue);
} else if (mdr->rdlocks.count(stray)) {
rdlock_finish(stray, mdr.get(), &need_issue);
} else {
// may have acquired both wrlock and remore wrlock
if (mdr->wrlocks.count(stray))
wrlock_finish(stray, mdr.get(), &need_issue);
if (mdr->remote_wrlocks.count(stray))
remote_wrlock_finish(stray, mdr->remote_wrlocks[stray], mdr.get());
}
if (need_issue)
issue_set.insert(static_cast<CInode*>(stray->get_parent()));
}
mdr->done_locking = true;
mdr->set_mds_stamp(ceph_clock_now());
result = true;
marker.message = "acquired locks";
out:
issue_caps_set(issue_set);
return result;
}
void Locker::set_xlocks_done(MutationImpl *mut, bool skip_dentry)
{
for (set<SimpleLock*>::iterator p = mut->xlocks.begin();
p != mut->xlocks.end();
++p) {
MDSCacheObject *object = (*p)->get_parent();
assert(object->is_auth());
if (skip_dentry &&
((*p)->get_type() == CEPH_LOCK_DN || (*p)->get_type() == CEPH_LOCK_DVERSION))
continue;
dout(10) << "set_xlocks_done on " << **p << " " << *object << dendl;
(*p)->set_xlock_done();
}
}
void Locker::_drop_rdlocks(MutationImpl *mut, set<CInode*> *pneed_issue)
{
while (!mut->rdlocks.empty()) {
bool ni = false;
MDSCacheObject *p = (*mut->rdlocks.begin())->get_parent();
rdlock_finish(*mut->rdlocks.begin(), mut, &ni);
if (ni)
pneed_issue->insert(static_cast<CInode*>(p));
}
}
void Locker::_drop_non_rdlocks(MutationImpl *mut, set<CInode*> *pneed_issue)
{
set<mds_rank_t> slaves;
while (!mut->xlocks.empty()) {
SimpleLock *lock = *mut->xlocks.begin();
MDSCacheObject *p = lock->get_parent();
if (!p->is_auth()) {
assert(lock->get_sm()->can_remote_xlock);
slaves.insert(p->authority().first);
lock->put_xlock();
mut->locks.erase(lock);
mut->xlocks.erase(lock);
continue;
}
bool ni = false;
xlock_finish(lock, mut, &ni);
if (ni)
pneed_issue->insert(static_cast<CInode*>(p));
}
while (!mut->remote_wrlocks.empty()) {
map<SimpleLock*,mds_rank_t>::iterator p = mut->remote_wrlocks.begin();
slaves.insert(p->second);
if (mut->wrlocks.count(p->first) == 0)
mut->locks.erase(p->first);
mut->remote_wrlocks.erase(p);
}
while (!mut->wrlocks.empty()) {
bool ni = false;
MDSCacheObject *p = (*mut->wrlocks.begin())->get_parent();
wrlock_finish(*mut->wrlocks.begin(), mut, &ni);
if (ni)
pneed_issue->insert(static_cast<CInode*>(p));
}
for (set<mds_rank_t>::iterator p = slaves.begin(); p != slaves.end(); ++p) {
if (mds->mdsmap->get_state(*p) >= MDSMap::STATE_REJOIN) {
dout(10) << "_drop_non_rdlocks dropping remote locks on mds." << *p << dendl;
MMDSSlaveRequest *slavereq = new MMDSSlaveRequest(mut->reqid, mut->attempt,
MMDSSlaveRequest::OP_DROPLOCKS);
mds->send_message_mds(slavereq, *p);
}
}
}
void Locker::cancel_locking(MutationImpl *mut, set<CInode*> *pneed_issue)
{
SimpleLock *lock = mut->locking;
assert(lock);
dout(10) << "cancel_locking " << *lock << " on " << *mut << dendl;
if (lock->get_parent()->is_auth()) {
bool need_issue = false;
if (lock->get_state() == LOCK_PREXLOCK) {
_finish_xlock(lock, -1, &need_issue);
} else if (lock->get_state() == LOCK_LOCK_XLOCK &&
lock->get_num_xlocks() == 0) {
lock->set_state(LOCK_XLOCKDONE);
eval_gather(lock, true, &need_issue);
}
if (need_issue)
pneed_issue->insert(static_cast<CInode *>(lock->get_parent()));
}
mut->finish_locking(lock);
}
void Locker::drop_locks(MutationImpl *mut, set<CInode*> *pneed_issue)
{
// leftover locks
set<CInode*> my_need_issue;
if (!pneed_issue)
pneed_issue = &my_need_issue;
if (mut->locking)
cancel_locking(mut, pneed_issue);
_drop_non_rdlocks(mut, pneed_issue);
_drop_rdlocks(mut, pneed_issue);
if (pneed_issue == &my_need_issue)
issue_caps_set(*pneed_issue);
mut->done_locking = false;
}
void Locker::drop_non_rdlocks(MutationImpl *mut, set<CInode*> *pneed_issue)
{
set<CInode*> my_need_issue;
if (!pneed_issue)
pneed_issue = &my_need_issue;
_drop_non_rdlocks(mut, pneed_issue);
if (pneed_issue == &my_need_issue)
issue_caps_set(*pneed_issue);
}
void Locker::drop_rdlocks(MutationImpl *mut, set<CInode*> *pneed_issue)
{
set<CInode*> my_need_issue;
if (!pneed_issue)
pneed_issue = &my_need_issue;
_drop_rdlocks(mut, pneed_issue);
if (pneed_issue == &my_need_issue)
issue_caps_set(*pneed_issue);
}
// generics
void Locker::eval_gather(SimpleLock *lock, bool first, bool *pneed_issue, list<MDSInternalContextBase*> *pfinishers)
{
dout(10) << "eval_gather " << *lock << " on " << *lock->get_parent() << dendl;
assert(!lock->is_stable());
int next = lock->get_next_state();
CInode *in = 0;
bool caps = lock->get_cap_shift();
if (lock->get_type() != CEPH_LOCK_DN)
in = static_cast<CInode *>(lock->get_parent());
bool need_issue = false;
int loner_issued = 0, other_issued = 0, xlocker_issued = 0;
assert(!caps || in != NULL);
if (caps && in->is_head()) {
in->get_caps_issued(&loner_issued, &other_issued, &xlocker_issued,
lock->get_cap_shift(), lock->get_cap_mask());
dout(10) << " next state is " << lock->get_state_name(next)
<< " issued/allows loner " << gcap_string(loner_issued)
<< "/" << gcap_string(lock->gcaps_allowed(CAP_LONER, next))
<< " xlocker " << gcap_string(xlocker_issued)
<< "/" << gcap_string(lock->gcaps_allowed(CAP_XLOCKER, next))
<< " other " << gcap_string(other_issued)
<< "/" << gcap_string(lock->gcaps_allowed(CAP_ANY, next))
<< dendl;
if (first && ((~lock->gcaps_allowed(CAP_ANY, next) & other_issued) ||
(~lock->gcaps_allowed(CAP_LONER, next) & loner_issued) ||
(~lock->gcaps_allowed(CAP_XLOCKER, next) & xlocker_issued)))
need_issue = true;
}
#define IS_TRUE_AND_LT_AUTH(x, auth) (x && ((auth && x <= AUTH) || (!auth && x < AUTH)))
bool auth = lock->get_parent()->is_auth();
if (!lock->is_gathering() &&
(IS_TRUE_AND_LT_AUTH(lock->get_sm()->states[next].can_rdlock, auth) || !lock->is_rdlocked()) &&
(IS_TRUE_AND_LT_AUTH(lock->get_sm()->states[next].can_wrlock, auth) || !lock->is_wrlocked()) &&
(IS_TRUE_AND_LT_AUTH(lock->get_sm()->states[next].can_xlock, auth) || !lock->is_xlocked()) &&
(IS_TRUE_AND_LT_AUTH(lock->get_sm()->states[next].can_lease, auth) || !lock->is_leased()) &&
!(lock->get_parent()->is_auth() && lock->is_flushing()) && // i.e. wait for scatter_writebehind!
(!caps || ((~lock->gcaps_allowed(CAP_ANY, next) & other_issued) == 0 &&
(~lock->gcaps_allowed(CAP_LONER, next) & loner_issued) == 0 &&
(~lock->gcaps_allowed(CAP_XLOCKER, next) & xlocker_issued) == 0)) &&
lock->get_state() != LOCK_SYNC_MIX2 && // these states need an explicit trigger from the auth mds
lock->get_state() != LOCK_MIX_SYNC2
) {
dout(7) << "eval_gather finished gather on " << *lock
<< " on " << *lock->get_parent() << dendl;
if (lock->get_sm() == &sm_filelock) {
assert(in);
if (in->state_test(CInode::STATE_RECOVERING)) {
dout(7) << "eval_gather finished gather, but still recovering" << dendl;
return;
} else if (in->state_test(CInode::STATE_NEEDSRECOVER)) {
dout(7) << "eval_gather finished gather, but need to recover" << dendl;
mds->mdcache->queue_file_recover(in);
mds->mdcache->do_file_recover();
return;
}
}
if (!lock->get_parent()->is_auth()) {
// replica: tell auth
mds_rank_t auth = lock->get_parent()->authority().first;
if (lock->get_parent()->is_rejoining() &&
mds->mdsmap->get_state(auth) == MDSMap::STATE_REJOIN) {
dout(7) << "eval_gather finished gather, but still rejoining "
<< *lock->get_parent() << dendl;
return;
}
if (mds->mdsmap->get_state(auth) >= MDSMap::STATE_REJOIN) {
switch (lock->get_state()) {
case LOCK_SYNC_LOCK:
mds->send_message_mds(new MLock(lock, LOCK_AC_LOCKACK, mds->get_nodeid()),
auth);
break;
case LOCK_MIX_SYNC:
{
MLock *reply = new MLock(lock, LOCK_AC_SYNCACK, mds->get_nodeid());
lock->encode_locked_state(reply->get_data());
mds->send_message_mds(reply, auth);
next = LOCK_MIX_SYNC2;
(static_cast<ScatterLock *>(lock))->start_flush();
}
break;
case LOCK_MIX_SYNC2:
(static_cast<ScatterLock *>(lock))->finish_flush();
(static_cast<ScatterLock *>(lock))->clear_flushed();
case LOCK_SYNC_MIX2:
// do nothing, we already acked
break;
case LOCK_SYNC_MIX:
{
MLock *reply = new MLock(lock, LOCK_AC_MIXACK, mds->get_nodeid());
mds->send_message_mds(reply, auth);
next = LOCK_SYNC_MIX2;
}
break;
case LOCK_MIX_LOCK:
{
bufferlist data;
lock->encode_locked_state(data);
mds->send_message_mds(new MLock(lock, LOCK_AC_LOCKACK, mds->get_nodeid(), data), auth);
(static_cast<ScatterLock *>(lock))->start_flush();
// we'll get an AC_LOCKFLUSHED to complete
}
break;
default:
ceph_abort();
}
}
} else {
// auth
// once the first (local) stage of mix->lock gather complete we can
// gather from replicas
if (lock->get_state() == LOCK_MIX_LOCK &&
lock->get_parent()->is_replicated()) {
dout(10) << " finished (local) gather for mix->lock, now gathering from replicas" << dendl;
send_lock_message(lock, LOCK_AC_LOCK);
lock->init_gather();
lock->set_state(LOCK_MIX_LOCK2);
return;
}
if (lock->is_dirty() && !lock->is_flushed()) {
scatter_writebehind(static_cast<ScatterLock *>(lock));
mds->mdlog->flush();
return;
}
lock->clear_flushed();
switch (lock->get_state()) {
// to mixed
case LOCK_TSYN_MIX:
case LOCK_SYNC_MIX:
case LOCK_EXCL_MIX:
in->start_scatter(static_cast<ScatterLock *>(lock));
if (lock->get_parent()->is_replicated()) {
bufferlist softdata;
lock->encode_locked_state(softdata);
send_lock_message(lock, LOCK_AC_MIX, softdata);
}
(static_cast<ScatterLock *>(lock))->clear_scatter_wanted();
break;
case LOCK_XLOCK:
case LOCK_XLOCKDONE:
if (next != LOCK_SYNC)
break;
// fall-thru
// to sync
case LOCK_EXCL_SYNC:
case LOCK_LOCK_SYNC:
case LOCK_MIX_SYNC:
case LOCK_XSYN_SYNC:
if (lock->get_parent()->is_replicated()) {
bufferlist softdata;
lock->encode_locked_state(softdata);
send_lock_message(lock, LOCK_AC_SYNC, softdata);
}
break;
}
}
lock->set_state(next);
if (lock->get_parent()->is_auth() &&
lock->is_stable())
lock->get_parent()->auth_unpin(lock);
// drop loner before doing waiters
if (caps &&
in->is_head() &&
in->is_auth() &&
in->get_wanted_loner() != in->get_loner()) {
dout(10) << " trying to drop loner" << dendl;
if (in->try_drop_loner()) {
dout(10) << " dropped loner" << dendl;
need_issue = true;
}
}
if (pfinishers)
lock->take_waiting(SimpleLock::WAIT_STABLE|SimpleLock::WAIT_WR|SimpleLock::WAIT_RD|SimpleLock::WAIT_XLOCK,
*pfinishers);
else
lock->finish_waiters(SimpleLock::WAIT_STABLE|SimpleLock::WAIT_WR|SimpleLock::WAIT_RD|SimpleLock::WAIT_XLOCK);
if (caps && in->is_head())
need_issue = true;
if (lock->get_parent()->is_auth() &&
lock->is_stable())
try_eval(lock, &need_issue);
}
if (need_issue) {
if (pneed_issue)
*pneed_issue = true;
else if (in->is_head())
issue_caps(in);
}
}
bool Locker::eval(CInode *in, int mask, bool caps_imported)
{
bool need_issue = caps_imported;
list<MDSInternalContextBase*> finishers;
dout(10) << "eval " << mask << " " << *in << dendl;
// choose loner?
if (in->is_auth() && in->is_head()) {
if (in->choose_ideal_loner() >= 0) {
if (in->try_set_loner()) {
dout(10) << "eval set loner to client." << in->get_loner() << dendl;
need_issue = true;
mask = -1;
} else
dout(10) << "eval want loner client." << in->get_wanted_loner() << " but failed to set it" << dendl;
} else
dout(10) << "eval doesn't want loner" << dendl;
}
retry:
if (mask & CEPH_LOCK_IFILE)
eval_any(&in->filelock, &need_issue, &finishers, caps_imported);
if (mask & CEPH_LOCK_IAUTH)
eval_any(&in->authlock, &need_issue, &finishers, caps_imported);
if (mask & CEPH_LOCK_ILINK)
eval_any(&in->linklock, &need_issue, &finishers, caps_imported);
if (mask & CEPH_LOCK_IXATTR)
eval_any(&in->xattrlock, &need_issue, &finishers, caps_imported);
if (mask & CEPH_LOCK_INEST)
eval_any(&in->nestlock, &need_issue, &finishers, caps_imported);
if (mask & CEPH_LOCK_IFLOCK)
eval_any(&in->flocklock, &need_issue, &finishers, caps_imported);
if (mask & CEPH_LOCK_IPOLICY)
eval_any(&in->policylock, &need_issue, &finishers, caps_imported);
// drop loner?
if (in->is_auth() && in->is_head() && in->get_wanted_loner() != in->get_loner()) {
dout(10) << " trying to drop loner" << dendl;
if (in->try_drop_loner()) {
dout(10) << " dropped loner" << dendl;
need_issue = true;
if (in->get_wanted_loner() >= 0) {
if (in->try_set_loner()) {
dout(10) << "eval end set loner to client." << in->get_loner() << dendl;
mask = -1;
goto retry;
} else {
dout(10) << "eval want loner client." << in->get_wanted_loner() << " but failed to set it" << dendl;
}
}
}
}
finish_contexts(g_ceph_context, finishers);
if (need_issue && in->is_head())
issue_caps(in);
dout(10) << "eval done" << dendl;
return need_issue;
}
class C_Locker_Eval : public LockerContext {
MDSCacheObject *p;
int mask;