-
Notifications
You must be signed in to change notification settings - Fork 1
/
Locker.cc
5944 lines (5160 loc) · 179 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 "CDir.h"
#include "CDentry.h"
#include "CInode.h"
#include "common/config.h"
#include "events/EOpen.h"
#include "events/EUpdate.h"
#include "Locker.h"
#include "MDBalancer.h"
#include "MDCache.h"
#include "MDLog.h"
#include "MDSRank.h"
#include "MDSMap.h"
#include "messages/MInodeFileCaps.h"
#include "messages/MMDSPeerRequest.h"
#include "Migrator.h"
#include "msg/Messenger.h"
#include "osdc/Objecter.h"
#define dout_subsys ceph_subsys_mds
#undef dout_prefix
#define dout_context g_ceph_context
#define dout_prefix _prefix(_dout, mds)
using namespace std;
static ostream& _prefix(std::ostream *_dout, MDSRank *mds) {
return *_dout << "mds." << mds->get_nodeid() << ".locker ";
}
class LockerContext : public MDSContext {
protected:
Locker *locker;
MDSRank *get_mds() override
{
return locker->mds;
}
public:
explicit LockerContext(Locker *locker_) : locker(locker_) {
ceph_assert(locker != NULL);
}
};
class LockerLogContext : public MDSLogContextBase {
protected:
Locker *locker;
MDSRank *get_mds() override
{
return locker->mds;
}
public:
explicit LockerLogContext(Locker *locker_) : locker(locker_) {
ceph_assert(locker != NULL);
}
};
Locker::Locker(MDSRank *m, MDCache *c) :
need_snapflush_inodes(member_offset(CInode, item_caps)), mds(m), mdcache(c) {}
void Locker::dispatch(const cref_t<Message> &m)
{
switch (m->get_type()) {
// inter-mds locking
case MSG_MDS_LOCK:
handle_lock(ref_cast<MLock>(m));
break;
// inter-mds caps
case MSG_MDS_INODEFILECAPS:
handle_inode_file_caps(ref_cast<MInodeFileCaps>(m));
break;
// client sync
case CEPH_MSG_CLIENT_CAPS:
handle_client_caps(ref_cast<MClientCaps>(m));
break;
case CEPH_MSG_CLIENT_CAPRELEASE:
handle_client_cap_release(ref_cast<MClientCapRelease>(m));
break;
case CEPH_MSG_CLIENT_LEASE:
handle_client_lease(ref_cast<MClientLease>(m));
break;
default:
derr << "locker unknown message " << m->get_type() << dendl;
ceph_abort_msg("locker unknown message");
}
}
void Locker::tick()
{
scatter_tick();
caps_tick();
}
/*
* locks vs rejoin
*
*
*
*/
void Locker::send_lock_message(SimpleLock *lock, int msg)
{
for (const auto &it : lock->get_parent()->get_replicas()) {
if (mds->is_cluster_degraded() &&
mds->mdsmap->get_state(it.first) < MDSMap::STATE_REJOIN)
continue;
auto m = make_message<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 (const auto &it : lock->get_parent()->get_replicas()) {
if (mds->is_cluster_degraded() &&
mds->mdsmap->get_state(it.first) < MDSMap::STATE_REJOIN)
continue;
auto m = make_message<MLock>(lock, msg, mds->get_nodeid());
m->set_data(data);
mds->send_message_mds(m, it.first);
}
}
bool Locker::try_rdlock_snap_layout(CInode *in, MDRequestRef& mdr,
int n, bool want_layout)
{
dout(10) << __func__ << " " << *mdr << " " << *in << dendl;
// rdlock ancestor snaps
inodeno_t root;
int depth = -1;
bool found_locked = false;
bool found_layout = false;
if (want_layout)
ceph_assert(n == 0);
client_t client = mdr->get_client();
CInode *t = in;
while (true) {
++depth;
if (!found_locked && mdr->is_rdlocked(&t->snaplock))
found_locked = true;
if (!found_locked) {
if (!t->snaplock.can_rdlock(client)) {
t->snaplock.add_waiter(SimpleLock::WAIT_RD, new C_MDS_RetryRequest(mdcache, mdr));
goto failed;
}
t->snaplock.get_rdlock();
mdr->locks.emplace(&t->snaplock, MutationImpl::LockOp::RDLOCK);
dout(20) << " got rdlock on " << t->snaplock << " " << *t << dendl;
}
if (want_layout && !found_layout) {
if (!mdr->is_rdlocked(&t->policylock)) {
if (!t->policylock.can_rdlock(client)) {
t->policylock.add_waiter(SimpleLock::WAIT_RD, new C_MDS_RetryRequest(mdcache, mdr));
goto failed;
}
t->policylock.get_rdlock();
mdr->locks.emplace(&t->policylock, MutationImpl::LockOp::RDLOCK);
dout(20) << " got rdlock on " << t->policylock << " " << *t << dendl;
}
if (t->get_projected_inode()->has_layout()) {
mdr->dir_layout = t->get_projected_inode()->layout;
found_layout = true;
}
}
CDentry* pdn = t->get_projected_parent_dn();
if (!pdn) {
root = t->ino();
break;
}
t = pdn->get_dir()->get_inode();
}
mdr->dir_root[n] = root;
mdr->dir_depth[n] = depth;
return true;
failed:
dout(10) << __func__ << " failed" << dendl;
drop_locks(mdr.get(), nullptr);
mdr->drop_local_auth_pins();
return false;
}
struct MarkEventOnDestruct {
MDRequestRef& mdr;
std::string_view message;
bool mark_event;
MarkEventOnDestruct(MDRequestRef& _mdr, std::string_view _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,
MutationImpl::LockOpVec& lov,
CInode *auth_pin_freeze,
bool auth_pin_nonblocking)
{
dout(10) << "acquire_locks " << *mdr << dendl;
MarkEventOnDestruct marker(mdr, "failed to acquire_locks");
client_t client = mdr->get_client();
set<MDSCacheObject*> mustpin; // items to authpin
if (auth_pin_freeze)
mustpin.insert(auth_pin_freeze);
// xlocks
for (size_t i = 0; i < lov.size(); ++i) {
auto& p = lov[i];
SimpleLock *lock = p.lock;
MDSCacheObject *object = lock->get_parent();
if (p.is_xlock()) {
if ((lock->get_type() == CEPH_LOCK_ISNAP ||
lock->get_type() == CEPH_LOCK_IPOLICY) &&
mds->is_cluster_degraded() &&
mdr->is_leader() &&
!mdr->is_queued_for_replay()) {
// waiting for recovering mds, to guarantee replayed requests and mksnap/setlayout
// get processed in proper order.
bool wait = false;
if (object->is_auth()) {
if (!mdr->is_xlocked(lock)) {
set<mds_rank_t> ls;
object->list_replicas(ls);
for (auto m : ls) {
if (mds->mdsmap->get_state(m) < MDSMap::STATE_ACTIVE) {
wait = true;
break;
}
}
}
} else {
// if the lock is the latest locked one, it's possible that peer mds got the lock
// while there are recovering mds.
if (!mdr->is_xlocked(lock) || mdr->is_last_locked(lock))
wait = true;
}
if (wait) {
dout(10) << " must xlock " << *lock << " " << *object
<< ", waiting for cluster recovered" << dendl;
mds->locker->drop_locks(mdr.get(), NULL);
mdr->drop_local_auth_pins();
mds->wait_for_cluster_recovered(new C_MDS_RetryRequest(mdcache, mdr));
return false;
}
}
dout(20) << " must xlock " << *lock << " " << *object << dendl;
mustpin.insert(object);
// augment xlock with a versionlock?
if (lock->get_type() == CEPH_LOCK_DN) {
CDentry *dn = static_cast<CDentry*>(object);
if (!dn->is_auth())
continue;
if (mdr->is_leader()) {
// leader. wrlock versionlock so we can pipeline dentry updates to journal.
lov.add_wrlock(&dn->versionlock, i + 1);
} else {
// peer. exclusively lock the dentry version (i.e. block other journal updates).
// this makes rollback safe.
lov.add_xlock(&dn->versionlock, i + 1);
}
}
if (lock->get_type() >= CEPH_LOCK_IFIRST && lock->get_type() != CEPH_LOCK_IVERSION) {
// inode version lock?
CInode *in = static_cast<CInode*>(object);
if (!in->is_auth())
continue;
if (mdr->is_leader()) {
// leader. wrlock versionlock so we can pipeline inode updates to journal.
lov.add_wrlock(&in->versionlock, i + 1);
} else {
// peer. exclusively lock the inode version (i.e. block other journal updates).
// this makes rollback safe.
lov.add_xlock(&in->versionlock, i + 1);
}
}
} else if (p.is_wrlock()) {
dout(20) << " must wrlock " << *lock << " " << *object << dendl;
client_t _client = p.is_state_pin() ? lock->get_excl_client() : client;
if (object->is_auth()) {
mustpin.insert(object);
} else if (!object->is_auth() &&
!lock->can_wrlock(_client) && // we might have to request a scatter
!mdr->is_peer()) { // if we are peer (remote_wrlock), the leader already authpinned
dout(15) << " will also auth_pin " << *object
<< " in case we need to request a scatter" << dendl;
mustpin.insert(object);
}
} else if (p.is_remote_wrlock()) {
dout(20) << " must remote_wrlock on mds." << p.wrlock_target << " "
<< *lock << " " << *object << dendl;
mustpin.insert(object);
} else if (p.is_rdlock()) {
dout(20) << " must rdlock " << *lock << " " << *object << dendl;
if (object->is_auth()) {
mustpin.insert(object);
} else if (!object->is_auth() &&
!lock->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);
}
} else {
ceph_assert(0 == "locker unknown lock operation");
}
}
lov.sort_and_merge();
// 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 (const auto &p : mustpin) {
MDSCacheObject *object = p;
dout(10) << " must authpin " << *object << dendl;
if (mdr->is_auth_pinned(object)) {
if (object != (MDSCacheObject*)auth_pin_freeze)
continue;
if (mdr->more()->is_remote_frozen_authpin) {
if (mdr->more()->rename_inode == auth_pin_freeze)
continue;
// unfreeze auth pin for the wrong inode
mustpin_remote[mdr->more()->rename_inode->authority().first].size();
}
}
if (!object->is_auth()) {
if (mdr->lock_cache) { // debug
ceph_assert(mdr->lock_cache->opcode == CEPH_MDS_OP_UNLINK);
CDentry *dn = mdr->dn[0].back();
ceph_assert(dn->get_projected_linkage()->is_remote());
}
if (object->is_ambiguous_auth()) {
// wait
dout(10) << " ambiguous auth, waiting to authpin " << *object << dendl;
mdr->disable_lock_cache();
drop_locks(mdr.get());
mdr->drop_local_auth_pins();
marker.message = "waiting for single auth, object is being migrated";
object->add_waiter(MDSCacheObject::WAIT_SINGLEAUTH, new C_MDS_RetryRequest(mdcache, mdr));
return false;
}
mustpin_remote[object->authority().first].insert(object);
continue;
}
int err = 0;
if (!object->can_auth_pin(&err)) {
if (mdr->lock_cache) {
CDir *dir;
if (CInode *in = dynamic_cast<CInode*>(object)) {
ceph_assert(!in->is_frozen_inode() && !in->is_frozen_auth_pin());
dir = in->get_projected_parent_dir();
} else if (CDentry *dn = dynamic_cast<CDentry*>(object)) {
dir = dn->get_dir();
} else {
ceph_assert(0 == "unknown type of lock parent");
}
if (dir->get_inode() == mdr->lock_cache->get_dir_inode()) {
// forcibly auth pin if there is lock cache on parent dir
continue;
}
{ // debug
ceph_assert(mdr->lock_cache->opcode == CEPH_MDS_OP_UNLINK);
CDentry *dn = mdr->dn[0].back();
ceph_assert(dn->get_projected_linkage()->is_remote());
}
}
// wait
mdr->disable_lock_cache();
drop_locks(mdr.get());
mdr->drop_local_auth_pins();
if (auth_pin_nonblocking) {
dout(10) << " can't auth_pin (freezing?) " << *object << ", nonblocking" << dendl;
mdr->aborted = true;
return false;
}
if (err == MDSCacheObject::ERR_EXPORTING_TREE) {
marker.message = "failed to authpin, subtree is being exported";
} else if (err == MDSCacheObject::ERR_FRAGMENTING_DIR) {
marker.message = "failed to authpin, dir is being fragmented";
} else if (err == MDSCacheObject::ERR_EXPORTING_INODE) {
marker.message = "failed to authpin, inode is being exported";
}
dout(10) << " can't auth_pin (freezing?), waiting to authpin " << *object << dendl;
object->add_waiter(MDSCacheObject::WAIT_UNFREEZE, new C_MDS_RetryRequest(mdcache, mdr));
if (mdr->is_any_remote_auth_pin())
notify_freeze_waiter(object);
return false;
}
}
// ok, grab local auth pins
for (const auto& p : mustpin) {
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 (const auto& p : mdr->object_states) {
if (p.second.remote_auth_pinned == MDS_RANK_NONE)
continue;
ceph_assert(p.second.remote_auth_pinned == p.first->authority().first);
auto q = mustpin_remote.find(p.second.remote_auth_pinned);
if (q != mustpin_remote.end())
q->second.insert(p.first);
}
for (auto& p : mustpin_remote) {
dout(10) << "requesting remote auth_pins from mds." << p.first << dendl;
// wait for active auth
if (mds->is_cluster_degraded() &&
!mds->mdsmap->is_clientreplay_or_active_or_stopping(p.first)) {
dout(10) << " mds." << p.first << " is not active" << dendl;
if (mdr->more()->waiting_on_peer.empty())
mds->wait_for_active_peer(p.first, new C_MDS_RetryRequest(mdcache, mdr));
return false;
}
auto req = make_message<MMDSPeerRequest>(mdr->reqid, mdr->attempt,
MMDSPeerRequest::OP_AUTHPIN);
for (auto& o : p.second) {
dout(10) << " req remote auth_pin of " << *o << dendl;
MDSCacheObjectInfo info;
o->set_object_info(info);
req->get_authpins().push_back(info);
if (o == auth_pin_freeze)
o->set_object_info(req->get_authpin_freeze());
mdr->pin(o);
}
if (auth_pin_nonblocking)
req->mark_nonblocking();
else if (!mdr->locks.empty())
req->mark_notify_blocking();
mds->send_message_mds(req, p.first);
// put in waiting list
auto ret = mdr->more()->waiting_on_peer.insert(p.first);
ceph_assert(ret.second);
}
return false;
}
// caps i'll need to issue
set<CInode*> issue_set;
bool result = false;
// acquire locks.
// make sure they match currently acquired locks.
for (const auto& p : lov) {
auto lock = p.lock;
if (p.is_xlock()) {
if (mdr->is_xlocked(lock)) {
dout(10) << " already xlocked " << *lock << " " << *lock->get_parent() << dendl;
continue;
}
if (mdr->locking && lock != mdr->locking)
cancel_locking(mdr.get(), &issue_set);
if (!xlock_start(lock, mdr)) {
marker.message = "failed to xlock, waiting";
goto out;
}
dout(10) << " got xlock on " << *lock << " " << *lock->get_parent() << dendl;
} else if (p.is_wrlock() || p.is_remote_wrlock()) {
auto it = mdr->locks.find(lock);
if (p.is_remote_wrlock()) {
if (it != mdr->locks.end() && it->is_remote_wrlock()) {
dout(10) << " already remote_wrlocked " << *lock << " " << *lock->get_parent() << dendl;
} else {
if (mdr->locking && lock != mdr->locking)
cancel_locking(mdr.get(), &issue_set);
marker.message = "waiting for remote wrlocks";
remote_wrlock_start(lock, p.wrlock_target, mdr);
goto out;
}
}
if (p.is_wrlock()) {
if (it != mdr->locks.end() && it->is_wrlock()) {
dout(10) << " already wrlocked " << *lock << " " << *lock->get_parent() << dendl;
continue;
}
client_t _client = p.is_state_pin() ? lock->get_excl_client() : client;
if (p.is_remote_wrlock()) {
// nowait if we have already gotten remote wrlock
if (!wrlock_try(lock, mdr, _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.
ceph_assert(it != mdr->locks.end());
remote_wrlock_finish(it, mdr.get());
remote_wrlock_start(lock, p.wrlock_target, mdr);
goto out;
}
} else {
if (!wrlock_start(p, mdr)) {
ceph_assert(!p.is_remote_wrlock());
marker.message = "failed to wrlock, waiting";
goto out;
}
}
dout(10) << " got wrlock on " << *lock << " " << *lock->get_parent() << dendl;
}
} else {
if (mdr->is_rdlocked(lock)) {
dout(10) << " already rdlocked " << *lock << " " << *lock->get_parent() << dendl;
continue;
}
ceph_assert(mdr->is_leader());
if (lock->needs_recover()) {
if (mds->is_cluster_degraded()) {
if (!mdr->is_queued_for_replay()) {
// see comments in SimpleLock::set_state_rejoin() and
// ScatterLock::encode_state_for_rejoin()
drop_locks(mdr.get());
mds->wait_for_cluster_recovered(new C_MDS_RetryRequest(mdcache, mdr));
dout(10) << " rejoin recovering " << *lock << " " << *lock->get_parent()
<< ", waiting for cluster recovered" << dendl;
marker.message = "rejoin recovering lock, waiting for cluster recovered";
return false;
}
} else {
lock->clear_need_recover();
}
}
if (!rdlock_start(lock, mdr)) {
marker.message = "failed to rdlock, waiting";
goto out;
}
dout(10) << " got rdlock on " << *lock << " " << *lock->get_parent() << dendl;
}
}
mdr->set_mds_stamp(ceph_clock_now());
result = true;
marker.message = "acquired locks";
out:
issue_caps_set(issue_set);
return result;
}
void Locker::notify_freeze_waiter(MDSCacheObject *o)
{
CDir *dir = NULL;
if (CInode *in = dynamic_cast<CInode*>(o)) {
if (!in->is_root())
dir = in->get_parent_dir();
} else if (CDentry *dn = dynamic_cast<CDentry*>(o)) {
dir = dn->get_dir();
} else {
dir = dynamic_cast<CDir*>(o);
ceph_assert(dir);
}
if (dir) {
if (dir->is_freezing_dir())
mdcache->fragment_freeze_inc_num_waiters(dir);
if (dir->is_freezing_tree()) {
while (!dir->is_freezing_tree_root())
dir = dir->get_parent_dir();
mdcache->migrator->export_freeze_inc_num_waiters(dir);
}
}
}
void Locker::set_xlocks_done(MutationImpl *mut, bool skip_dentry)
{
for (const auto &p : mut->locks) {
if (!p.is_xlock())
continue;
MDSCacheObject *obj = p.lock->get_parent();
ceph_assert(obj->is_auth());
if (skip_dentry &&
(p.lock->get_type() == CEPH_LOCK_DN || p.lock->get_type() == CEPH_LOCK_DVERSION))
continue;
dout(10) << "set_xlocks_done on " << *p.lock << " " << *obj << dendl;
p.lock->set_xlock_done();
}
}
void Locker::_drop_locks(MutationImpl *mut, set<CInode*> *pneed_issue,
bool drop_rdlocks)
{
set<mds_rank_t> peers;
for (auto it = mut->locks.begin(); it != mut->locks.end(); ) {
SimpleLock *lock = it->lock;
MDSCacheObject *obj = lock->get_parent();
if (it->is_xlock()) {
if (obj->is_auth()) {
bool ni = false;
xlock_finish(it++, mut, &ni);
if (ni)
pneed_issue->insert(static_cast<CInode*>(obj));
} else {
ceph_assert(lock->get_sm()->can_remote_xlock);
peers.insert(obj->authority().first);
lock->put_xlock();
mut->locks.erase(it++);
}
} else if (it->is_wrlock() || it->is_remote_wrlock()) {
if (it->is_remote_wrlock()) {
peers.insert(it->wrlock_target);
it->clear_remote_wrlock();
}
if (it->is_wrlock()) {
bool ni = false;
wrlock_finish(it++, mut, &ni);
if (ni)
pneed_issue->insert(static_cast<CInode*>(obj));
} else {
mut->locks.erase(it++);
}
} else if (drop_rdlocks && it->is_rdlock()) {
bool ni = false;
rdlock_finish(it++, mut, &ni);
if (ni)
pneed_issue->insert(static_cast<CInode*>(obj));
} else {
++it;
}
}
if (drop_rdlocks) {
if (mut->lock_cache) {
put_lock_cache(mut->lock_cache);
mut->lock_cache = nullptr;
}
}
for (set<mds_rank_t>::iterator p = peers.begin(); p != peers.end(); ++p) {
if (!mds->is_cluster_degraded() ||
mds->mdsmap->get_state(*p) >= MDSMap::STATE_REJOIN) {
dout(10) << "_drop_non_rdlocks dropping remote locks on mds." << *p << dendl;
auto peerreq = make_message<MMDSPeerRequest>(mut->reqid, mut->attempt,
MMDSPeerRequest::OP_DROPLOCKS);
mds->send_message_mds(peerreq, *p);
}
}
}
void Locker::cancel_locking(MutationImpl *mut, set<CInode*> *pneed_issue)
{
SimpleLock *lock = mut->locking;
ceph_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->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_locks(mut, pneed_issue, true);
if (pneed_issue == &my_need_issue)
issue_caps_set(*pneed_issue);
mut->locking_state = 0;
}
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_locks(mut, pneed_issue, false);
if (pneed_issue == &my_need_issue)
issue_caps_set(*pneed_issue);
}
void Locker::drop_rdlocks_for_early_reply(MutationImpl *mut)
{
set<CInode*> need_issue;
for (auto it = mut->locks.begin(); it != mut->locks.end(); ) {
if (!it->is_rdlock()) {
++it;
continue;
}
SimpleLock *lock = it->lock;
// make later mksnap/setlayout (at other mds) wait for this unsafe request
if (lock->get_type() == CEPH_LOCK_ISNAP ||
lock->get_type() == CEPH_LOCK_IPOLICY) {
++it;
continue;
}
bool ni = false;
rdlock_finish(it++, mut, &ni);
if (ni)
need_issue.insert(static_cast<CInode*>(lock->get_parent()));
}
issue_caps_set(need_issue);
}
void Locker::drop_locks_for_fragment_unfreeze(MutationImpl *mut)
{
set<CInode*> need_issue;
for (auto it = mut->locks.begin(); it != mut->locks.end(); ) {
SimpleLock *lock = it->lock;
if (lock->get_type() == CEPH_LOCK_IDFT) {
++it;
continue;
}
bool ni = false;
wrlock_finish(it++, mut, &ni);
if (ni)
need_issue.insert(static_cast<CInode*>(lock->get_parent()));
}
issue_caps_set(need_issue);
}
class C_MDL_DropCache : public LockerContext {
MDLockCache *lock_cache;
public:
C_MDL_DropCache(Locker *l, MDLockCache *lc) :
LockerContext(l), lock_cache(lc) { }
void finish(int r) override {
locker->drop_locks(lock_cache);
lock_cache->cleanup();
delete lock_cache;
}
};
void Locker::put_lock_cache(MDLockCache* lock_cache)
{
ceph_assert(lock_cache->ref > 0);
if (--lock_cache->ref > 0)
return;
ceph_assert(lock_cache->invalidating);
lock_cache->detach_locks();
CInode *diri = lock_cache->get_dir_inode();
for (auto dir : lock_cache->auth_pinned_dirfrags) {
if (dir->get_inode() != diri)
continue;
dir->enable_frozen_inode();
}
mds->queue_waiter(new C_MDL_DropCache(this, lock_cache));
}
int Locker::get_cap_bit_for_lock_cache(int op)
{
switch(op) {
case CEPH_MDS_OP_CREATE:
return CEPH_CAP_DIR_CREATE;
case CEPH_MDS_OP_UNLINK:
return CEPH_CAP_DIR_UNLINK;
default:
ceph_assert(0 == "unsupported operation");
return 0;
}
}
void Locker::invalidate_lock_cache(MDLockCache *lock_cache)
{
ceph_assert(lock_cache->item_cap_lock_cache.is_on_list());
if (lock_cache->invalidating) {
ceph_assert(!lock_cache->client_cap);
} else {
lock_cache->invalidating = true;
lock_cache->detach_dirfrags();
}
Capability *cap = lock_cache->client_cap;
if (cap) {
int cap_bit = get_cap_bit_for_lock_cache(lock_cache->opcode);
cap->clear_lock_cache_allowed(cap_bit);
if (cap->issued() & cap_bit)
issue_caps(lock_cache->get_dir_inode(), cap);
else
cap = nullptr;
}
if (!cap) {
lock_cache->item_cap_lock_cache.remove_myself();
put_lock_cache(lock_cache);
}
}
void Locker::eval_lock_caches(Capability *cap)
{
for (auto p = cap->lock_caches.begin(); !p.end(); ) {
MDLockCache *lock_cache = *p;
++p;
if (!lock_cache->invalidating)
continue;
int cap_bit = get_cap_bit_for_lock_cache(lock_cache->opcode);
if (!(cap->issued() & cap_bit)) {
lock_cache->item_cap_lock_cache.remove_myself();
put_lock_cache(lock_cache);
}
}
}
// ask lock caches to release auth pins
void Locker::invalidate_lock_caches(CDir *dir)
{
dout(10) << "invalidate_lock_caches on " << *dir << dendl;
auto &lock_caches = dir->lock_caches_with_auth_pins;
while (!lock_caches.empty()) {
invalidate_lock_cache(lock_caches.front()->parent);
}
}
// ask lock caches to release locks
void Locker::invalidate_lock_caches(SimpleLock *lock)
{
dout(10) << "invalidate_lock_caches " << *lock << " on " << *lock->get_parent() << dendl;
if (lock->is_cached()) {
auto&& lock_caches = lock->get_active_caches();
for (auto& lc : lock_caches)
invalidate_lock_cache(lc);
}
}
void Locker::create_lock_cache(MDRequestRef& mdr, CInode *diri, file_layout_t *dir_layout)
{
if (mdr->lock_cache)
return;
client_t client = mdr->get_client();
int opcode = mdr->client_request->get_op();
dout(10) << "create_lock_cache for client." << client << "/" << ceph_mds_op_name(opcode)<< " on " << *diri << dendl;
if (!diri->is_auth()) {
dout(10) << " dir inode is not auth, noop" << dendl;
return;
}
if (mdr->has_more() && !mdr->more()->peers.empty()) {
dout(10) << " there are peers requests for " << *mdr << ", noop" << dendl;
return;
}
Capability *cap = diri->get_client_cap(client);
if (!cap) {
dout(10) << " there is no cap for client." << client << ", noop" << dendl;
return;
}
for (auto p = cap->lock_caches.begin(); !p.end(); ++p) {
if ((*p)->opcode == opcode) {
dout(10) << " lock cache already exists for " << ceph_mds_op_name(opcode) << ", noop" << dendl;
return;
}
}
set<MDSCacheObject*> ancestors;
for (CInode *in = diri; ; ) {
CDentry *pdn = in->get_projected_parent_dn();
if (!pdn)
break;
// ancestors.insert(pdn);
in = pdn->get_dir()->get_inode();
ancestors.insert(in);
}
for (auto& p : mdr->object_states) {
if (p.first != diri && !ancestors.count(p.first))
continue;
auto& stat = p.second;
if (stat.auth_pinned) {
if (!p.first->can_auth_pin()) {
dout(10) << " can't auth_pin(freezing?) lock parent " << *p.first << ", noop" << dendl;
return;
}
if (CInode *in = dynamic_cast<CInode*>(p.first); in->is_parent_projected()) {
CDir *dir = in->get_projected_parent_dir();
if (!dir->can_auth_pin()) {
dout(10) << " can't auth_pin(!auth|freezing?) dirfrag " << *dir << ", noop" << dendl;
return;
}
}
}
}
std::vector<CDir*> dfv;
dfv.reserve(diri->get_num_dirfrags());
diri->get_dirfrags(dfv);
for (auto dir : dfv) {
if (!dir->is_auth() || !dir->can_auth_pin()) {
dout(10) << " can't auth_pin(!auth|freezing?) dirfrag " << *dir << ", noop" << dendl;
return;
}
if (dir->is_any_freezing_or_frozen_inode()) {
dout(10) << " there is freezing/frozen inode in " << *dir << ", noop" << dendl;
return;
}
}
for (auto& p : mdr->locks) {
MDSCacheObject *obj = p.lock->get_parent();
if (obj != diri && !ancestors.count(obj))
continue;
if (!p.lock->is_stable()) {
dout(10) << " unstable " << *p.lock << " on " << *obj << ", noop" << dendl;
return;
}
}
auto lock_cache = new MDLockCache(cap, opcode);
if (dir_layout)
lock_cache->set_dir_layout(*dir_layout);
cap->set_lock_cache_allowed(get_cap_bit_for_lock_cache(opcode));
for (auto dir : dfv) {
// prevent subtree migration
lock_cache->auth_pin(dir);
// prevent frozen inode
dir->disable_frozen_inode();
}
for (auto& p : mdr->object_states) {
if (p.first != diri && !ancestors.count(p.first))
continue;
auto& stat = p.second;
if (stat.auth_pinned)
lock_cache->auth_pin(p.first);
else
lock_cache->pin(p.first);