1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
24 #include "MDBalancer.h"
29 #include "include/filepath.h"
31 #include "events/EExport.h"
32 #include "events/EImportStart.h"
33 #include "events/EImportFinish.h"
34 #include "events/ESessions.h"
36 #include "msg/Messenger.h"
38 #include "messages/MClientCaps.h"
40 #include "messages/MExportDirDiscover.h"
41 #include "messages/MExportDirDiscoverAck.h"
42 #include "messages/MExportDirCancel.h"
43 #include "messages/MExportDirPrep.h"
44 #include "messages/MExportDirPrepAck.h"
45 #include "messages/MExportDir.h"
46 #include "messages/MExportDirAck.h"
47 #include "messages/MExportDirNotify.h"
48 #include "messages/MExportDirNotifyAck.h"
49 #include "messages/MExportDirFinish.h"
51 #include "messages/MExportCaps.h"
52 #include "messages/MExportCapsAck.h"
53 #include "messages/MGatherCaps.h"
57 * this is what the dir->dir_auth values look like
62 * me, me me - still me, but preparing for export
63 * me, them me - send MExportDir (peer is preparing)
64 * them, me me - journaled EExport
69 * me, them me - journaled EImportStart
73 * - auth bit is set if i am listed as first _or_ second dir_auth.
76 #include "common/config.h"
79 #define dout_context g_ceph_context
80 #define dout_subsys ceph_subsys_mds
82 #define dout_prefix *_dout << "mds." << mds->get_nodeid() << ".migrator "
85 class MigratorContext
: public MDSInternalContextBase
{
88 MDSRank
*get_mds() override
{
92 explicit MigratorContext(Migrator
*mig_
) : mig(mig_
) {
97 class MigratorLogContext
: public MDSLogContextBase
{
100 MDSRank
*get_mds() override
{
104 explicit MigratorLogContext(Migrator
*mig_
) : mig(mig_
) {
109 /* This function DOES put the passed message before returning*/
110 void Migrator::dispatch(Message
*m
)
112 switch (m
->get_type()) {
114 case MSG_MDS_EXPORTDIRDISCOVER
:
115 handle_export_discover(static_cast<MExportDirDiscover
*>(m
));
117 case MSG_MDS_EXPORTDIRPREP
:
118 handle_export_prep(static_cast<MExportDirPrep
*>(m
));
120 case MSG_MDS_EXPORTDIR
:
121 handle_export_dir(static_cast<MExportDir
*>(m
));
123 case MSG_MDS_EXPORTDIRFINISH
:
124 handle_export_finish(static_cast<MExportDirFinish
*>(m
));
126 case MSG_MDS_EXPORTDIRCANCEL
:
127 handle_export_cancel(static_cast<MExportDirCancel
*>(m
));
131 case MSG_MDS_EXPORTDIRDISCOVERACK
:
132 handle_export_discover_ack(static_cast<MExportDirDiscoverAck
*>(m
));
134 case MSG_MDS_EXPORTDIRPREPACK
:
135 handle_export_prep_ack(static_cast<MExportDirPrepAck
*>(m
));
137 case MSG_MDS_EXPORTDIRACK
:
138 handle_export_ack(static_cast<MExportDirAck
*>(m
));
140 case MSG_MDS_EXPORTDIRNOTIFYACK
:
141 handle_export_notify_ack(static_cast<MExportDirNotifyAck
*>(m
));
144 // export 3rd party (dir_auth adjustments)
145 case MSG_MDS_EXPORTDIRNOTIFY
:
146 handle_export_notify(static_cast<MExportDirNotify
*>(m
));
150 case MSG_MDS_EXPORTCAPS
:
151 handle_export_caps(static_cast<MExportCaps
*>(m
));
153 case MSG_MDS_GATHERCAPS
:
154 handle_gather_caps(static_cast<MGatherCaps
*>(m
));
158 derr
<< "migrator unknown message " << m
->get_type() << dendl
;
159 assert(0 == "migrator unknown message");
164 class C_MDC_EmptyImport
: public MigratorContext
{
167 C_MDC_EmptyImport(Migrator
*m
, CDir
*d
) : MigratorContext(m
), dir(d
) {}
168 void finish(int r
) override
{
169 mig
->export_empty_import(dir
);
174 void Migrator::export_empty_import(CDir
*dir
)
176 dout(7) << "export_empty_import " << *dir
<< dendl
;
177 assert(dir
->is_subtree_root());
179 if (dir
->inode
->is_auth()) {
180 dout(7) << " inode is auth" << dendl
;
183 if (!dir
->is_auth()) {
184 dout(7) << " not auth" << dendl
;
187 if (dir
->is_freezing() || dir
->is_frozen()) {
188 dout(7) << " freezing or frozen" << dendl
;
191 if (dir
->get_num_head_items() > 0) {
192 dout(7) << " not actually empty" << dendl
;
195 if (dir
->inode
->is_root()) {
196 dout(7) << " root" << dendl
;
200 mds_rank_t dest
= dir
->inode
->authority().first
;
201 //if (mds->is_shutting_down()) dest = 0; // this is more efficient.
203 dout(7) << " really empty, exporting to " << dest
<< dendl
;
204 assert (dest
!= mds
->get_nodeid());
206 dout(7) << "exporting to mds." << dest
207 << " empty import " << *dir
<< dendl
;
208 export_dir( dir
, dest
);
211 void Migrator::find_stale_export_freeze()
213 utime_t now
= ceph_clock_now();
214 utime_t cutoff
= now
;
215 cutoff
-= g_conf
->mds_freeze_tree_timeout
;
219 * We could have situations like:
221 * - mds.0 authpins an item in subtree A
222 * - mds.0 sends request to mds.1 to authpin an item in subtree B
223 * - mds.0 freezes subtree A
224 * - mds.1 authpins an item in subtree B
225 * - mds.1 sends request to mds.0 to authpin an item in subtree A
226 * - mds.1 freezes subtree B
227 * - mds.1 receives the remote authpin request from mds.0
228 * (wait because subtree B is freezing)
229 * - mds.0 receives the remote authpin request from mds.1
230 * (wait because subtree A is freezing)
233 * - client request authpins items in subtree B
235 * - import subtree A which is parent of subtree B
236 * (authpins parent inode of subtree B, see CDir::set_dir_auth())
238 * - client request tries authpinning items in subtree A
239 * (wait because subtree A is freezing)
241 for (map
<CDir
*,export_state_t
>::iterator p
= export_state
.begin();
242 p
!= export_state
.end(); ) {
243 CDir
* dir
= p
->first
;
244 export_state_t
& stat
= p
->second
;
246 if (stat
.state
!= EXPORT_DISCOVERING
&& stat
.state
!= EXPORT_FREEZING
)
248 if (stat
.last_cum_auth_pins
!= dir
->get_cum_auth_pins()) {
249 stat
.last_cum_auth_pins
= dir
->get_cum_auth_pins();
250 stat
.last_cum_auth_pins_change
= now
;
253 if (stat
.last_cum_auth_pins_change
>= cutoff
)
255 if (stat
.num_remote_waiters
> 0 ||
256 (!dir
->inode
->is_root() && dir
->get_parent_dir()->is_freezing())) {
257 export_try_cancel(dir
);
262 void Migrator::export_try_cancel(CDir
*dir
, bool notify_peer
)
264 dout(10) << "export_try_cancel " << *dir
<< dendl
;
266 map
<CDir
*,export_state_t
>::iterator it
= export_state
.find(dir
);
267 assert(it
!= export_state
.end());
269 int state
= it
->second
.state
;
272 dout(10) << "export state=locking : dropping locks and removing auth_pin" << dendl
;
273 it
->second
.state
= EXPORT_CANCELLED
;
274 dir
->auth_unpin(this);
276 case EXPORT_DISCOVERING
:
277 dout(10) << "export state=discovering : canceling freeze and removing auth_pin" << dendl
;
278 it
->second
.state
= EXPORT_CANCELLED
;
279 dir
->unfreeze_tree(); // cancel the freeze
280 dir
->auth_unpin(this);
282 (!mds
->is_cluster_degraded() ||
283 mds
->mdsmap
->is_clientreplay_or_active_or_stopping(it
->second
.peer
))) // tell them.
284 mds
->send_message_mds(new MExportDirCancel(dir
->dirfrag(), it
->second
.tid
), it
->second
.peer
);
287 case EXPORT_FREEZING
:
288 dout(10) << "export state=freezing : canceling freeze" << dendl
;
289 it
->second
.state
= EXPORT_CANCELLED
;
290 dir
->unfreeze_tree(); // cancel the freeze
292 (!mds
->is_cluster_degraded() ||
293 mds
->mdsmap
->is_clientreplay_or_active_or_stopping(it
->second
.peer
))) // tell them.
294 mds
->send_message_mds(new MExportDirCancel(dir
->dirfrag(), it
->second
.tid
), it
->second
.peer
);
297 // NOTE: state order reversal, warning comes after prepping
299 dout(10) << "export state=warning : unpinning bounds, unfreezing, notifying" << dendl
;
300 it
->second
.state
= EXPORT_CANCELLING
;
303 case EXPORT_PREPPING
:
304 if (state
!= EXPORT_WARNING
) {
305 dout(10) << "export state=prepping : unpinning bounds, unfreezing" << dendl
;
306 it
->second
.state
= EXPORT_CANCELLED
;
312 cache
->get_subtree_bounds(dir
, bounds
);
313 for (set
<CDir
*>::iterator q
= bounds
.begin();
317 bd
->put(CDir::PIN_EXPORTBOUND
);
318 bd
->state_clear(CDir::STATE_EXPORTBOUND
);
320 if (state
== EXPORT_WARNING
) {
322 export_notify_abort(dir
, bounds
);
323 // process delayed expires
324 cache
->process_delayed_expire(dir
);
327 dir
->unfreeze_tree();
328 cache
->adjust_subtree_auth(dir
, mds
->get_nodeid());
329 cache
->try_subtree_merge(dir
);
331 (!mds
->is_cluster_degraded() ||
332 mds
->mdsmap
->is_clientreplay_or_active_or_stopping(it
->second
.peer
))) // tell them.
333 mds
->send_message_mds(new MExportDirCancel(dir
->dirfrag(), it
->second
.tid
), it
->second
.peer
);
336 case EXPORT_EXPORTING
:
337 dout(10) << "export state=exporting : reversing, and unfreezing" << dendl
;
338 it
->second
.state
= EXPORT_CANCELLING
;
342 case EXPORT_LOGGINGFINISH
:
343 case EXPORT_NOTIFYING
:
344 dout(10) << "export state=loggingfinish|notifying : ignoring dest failure, we were successful." << dendl
;
345 // leave export_state, don't clean up now.
347 case EXPORT_CANCELLING
:
355 if (it
->second
.state
== EXPORT_CANCELLING
||
356 it
->second
.state
== EXPORT_CANCELLED
) {
358 mut
.swap(it
->second
.mut
);
360 if (it
->second
.state
== EXPORT_CANCELLED
) {
361 export_state
.erase(it
);
362 dir
->state_clear(CDir::STATE_EXPORTING
);
363 // send pending import_maps?
364 cache
->maybe_send_pending_resolves();
368 if (state
== EXPORT_LOCKING
|| state
== EXPORT_DISCOVERING
) {
369 MDRequestRef mdr
= static_cast<MDRequestImpl
*>(mut
.get());
371 if (mdr
->more()->waiting_on_slave
.empty())
372 mds
->mdcache
->request_finish(mdr
);
374 mds
->locker
->drop_locks(mut
.get());
378 cache
->show_subtrees();
380 maybe_do_queued_export();
384 void Migrator::export_cancel_finish(CDir
*dir
)
386 assert(dir
->state_test(CDir::STATE_EXPORTING
));
387 dir
->state_clear(CDir::STATE_EXPORTING
);
389 // pinned by Migrator::export_notify_abort()
390 dir
->auth_unpin(this);
391 // send pending import_maps? (these need to go out when all exports have finished.)
392 cache
->maybe_send_pending_resolves();
395 // ==========================================================
396 // mds failure handling
398 void Migrator::handle_mds_failure_or_stop(mds_rank_t who
)
400 dout(5) << "handle_mds_failure_or_stop mds." << who
<< dendl
;
404 // first add an extra auth_pin on any freezes, so that canceling a
405 // nested freeze doesn't complete one further up the hierarchy and
406 // confuse the shit out of us. we'll remove it after canceling the
407 // freeze. this way no freeze completions run before we want them
409 list
<CDir
*> pinned_dirs
;
410 for (map
<CDir
*,export_state_t
>::iterator p
= export_state
.begin();
411 p
!= export_state
.end();
413 if (p
->second
.state
== EXPORT_FREEZING
) {
414 CDir
*dir
= p
->first
;
415 dout(10) << "adding temp auth_pin on freezing " << *dir
<< dendl
;
417 pinned_dirs
.push_back(dir
);
421 map
<CDir
*,export_state_t
>::iterator p
= export_state
.begin();
422 while (p
!= export_state
.end()) {
423 map
<CDir
*,export_state_t
>::iterator next
= p
;
425 CDir
*dir
= p
->first
;
428 // - that are going to the failed node
429 // - that aren't frozen yet (to avoid auth_pin deadlock)
430 // - they havne't prepped yet (they may need to discover bounds to do that)
431 if ((p
->second
.peer
== who
&&
432 p
->second
.state
!= EXPORT_CANCELLING
) ||
433 p
->second
.state
== EXPORT_LOCKING
||
434 p
->second
.state
== EXPORT_DISCOVERING
||
435 p
->second
.state
== EXPORT_FREEZING
||
436 p
->second
.state
== EXPORT_PREPPING
) {
437 // the guy i'm exporting to failed, or we're just freezing.
438 dout(10) << "cleaning up export state (" << p
->second
.state
<< ")"
439 << get_export_statename(p
->second
.state
) << " of " << *dir
<< dendl
;
440 export_try_cancel(dir
);
441 } else if (p
->second
.peer
!= who
) {
443 if (p
->second
.warning_ack_waiting
.erase(who
)) {
444 if (p
->second
.state
== EXPORT_WARNING
) {
445 p
->second
.notify_ack_waiting
.erase(who
); // they won't get a notify either.
446 // exporter waiting for warning acks, let's fake theirs.
447 dout(10) << "faking export_warning_ack from mds." << who
448 << " on " << *dir
<< " to mds." << p
->second
.peer
450 if (p
->second
.warning_ack_waiting
.empty())
454 if (p
->second
.notify_ack_waiting
.erase(who
)) {
455 // exporter is waiting for notify acks, fake it
456 dout(10) << "faking export_notify_ack from mds." << who
457 << " on " << *dir
<< " to mds." << p
->second
.peer
459 if (p
->second
.state
== EXPORT_NOTIFYING
) {
460 if (p
->second
.notify_ack_waiting
.empty())
462 } else if (p
->second
.state
== EXPORT_CANCELLING
) {
463 if (p
->second
.notify_ack_waiting
.empty()) {
464 export_state
.erase(p
);
465 export_cancel_finish(dir
);
477 map
<dirfrag_t
,import_state_t
>::iterator q
= import_state
.begin();
478 while (q
!= import_state
.end()) {
479 map
<dirfrag_t
,import_state_t
>::iterator next
= q
;
481 dirfrag_t df
= q
->first
;
482 CInode
*diri
= mds
->mdcache
->get_inode(df
.ino
);
483 CDir
*dir
= mds
->mdcache
->get_dirfrag(df
);
485 if (q
->second
.peer
== who
) {
487 dout(10) << "cleaning up import state (" << q
->second
.state
<< ")"
488 << get_import_statename(q
->second
.state
) << " of " << *dir
<< dendl
;
490 dout(10) << "cleaning up import state (" << q
->second
.state
<< ")"
491 << get_import_statename(q
->second
.state
) << " of " << df
<< dendl
;
493 switch (q
->second
.state
) {
494 case IMPORT_DISCOVERING
:
495 dout(10) << "import state=discovering : clearing state" << dendl
;
496 import_reverse_discovering(df
);
499 case IMPORT_DISCOVERED
:
501 dout(10) << "import state=discovered : unpinning inode " << *diri
<< dendl
;
502 import_reverse_discovered(df
, diri
);
505 case IMPORT_PREPPING
:
507 dout(10) << "import state=prepping : unpinning base+bounds " << *dir
<< dendl
;
508 import_reverse_prepping(dir
);
513 dout(10) << "import state=prepped : unpinning base+bounds, unfreezing " << *dir
<< dendl
;
516 cache
->get_subtree_bounds(dir
, bounds
);
517 import_remove_pins(dir
, bounds
);
519 // adjust auth back to the exporter
520 cache
->adjust_subtree_auth(dir
, q
->second
.peer
);
521 cache
->try_subtree_merge(dir
);
523 // notify bystanders ; wait in aborting state
524 import_state
[df
].state
= IMPORT_ABORTING
;
525 import_notify_abort(dir
, bounds
);
526 assert(g_conf
->mds_kill_import_at
!= 10);
530 case IMPORT_LOGGINGSTART
:
532 dout(10) << "import state=loggingstart : reversing import on " << *dir
<< dendl
;
538 // hrm. make this an ambiguous import, and wait for exporter recovery to disambiguate
539 dout(10) << "import state=acking : noting ambiguous import " << *dir
<< dendl
;
542 cache
->get_subtree_bounds(dir
, bounds
);
543 cache
->add_ambiguous_import(dir
, bounds
);
547 case IMPORT_FINISHING
:
549 dout(10) << "import state=finishing : finishing import on " << *dir
<< dendl
;
550 import_finish(dir
, true);
553 case IMPORT_ABORTING
:
555 dout(10) << "import state=aborting : ignoring repeat failure " << *dir
<< dendl
;
559 auto bystanders_entry
= q
->second
.bystanders
.find(who
);
560 if (bystanders_entry
!= q
->second
.bystanders
.end()) {
561 q
->second
.bystanders
.erase(bystanders_entry
);
562 if (q
->second
.state
== IMPORT_ABORTING
) {
564 dout(10) << "faking export_notify_ack from mds." << who
565 << " on aborting import " << *dir
<< " from mds." << q
->second
.peer
567 if (q
->second
.bystanders
.empty()) {
568 import_reverse_unfreeze(dir
);
578 while (!pinned_dirs
.empty()) {
579 CDir
*dir
= pinned_dirs
.front();
580 dout(10) << "removing temp auth_pin on " << *dir
<< dendl
;
581 dir
->auth_unpin(this);
582 pinned_dirs
.pop_front();
588 void Migrator::show_importing()
590 dout(10) << "show_importing" << dendl
;
591 for (map
<dirfrag_t
,import_state_t
>::iterator p
= import_state
.begin();
592 p
!= import_state
.end();
594 CDir
*dir
= mds
->mdcache
->get_dirfrag(p
->first
);
596 dout(10) << " importing from " << p
->second
.peer
597 << ": (" << p
->second
.state
<< ") " << get_import_statename(p
->second
.state
)
598 << " " << p
->first
<< " " << *dir
<< dendl
;
600 dout(10) << " importing from " << p
->second
.peer
601 << ": (" << p
->second
.state
<< ") " << get_import_statename(p
->second
.state
)
602 << " " << p
->first
<< dendl
;
607 void Migrator::show_exporting()
609 dout(10) << "show_exporting" << dendl
;
610 for (map
<CDir
*,export_state_t
>::iterator p
= export_state
.begin();
611 p
!= export_state
.end();
613 dout(10) << " exporting to " << p
->second
.peer
614 << ": (" << p
->second
.state
<< ") " << get_export_statename(p
->second
.state
)
615 << " " << p
->first
->dirfrag() << " " << *p
->first
<< dendl
;
620 void Migrator::audit()
622 if (!g_conf
->subsys
.should_gather(ceph_subsys_mds
, 5))
627 for (map
<dirfrag_t
,import_state_t
>::iterator p
= import_state
.begin();
628 p
!= import_state
.end();
630 if (p
->second
.state
== IMPORT_DISCOVERING
)
632 if (p
->second
.state
== IMPORT_DISCOVERED
) {
633 CInode
*in
= cache
->get_inode(p
->first
.ino
);
637 CDir
*dir
= cache
->get_dirfrag(p
->first
);
639 if (p
->second
.state
== IMPORT_PREPPING
)
641 if (p
->second
.state
== IMPORT_ABORTING
) {
642 assert(!dir
->is_ambiguous_dir_auth());
643 assert(dir
->get_dir_auth().first
!= mds
->get_nodeid());
646 assert(dir
->is_ambiguous_dir_auth());
647 assert(dir
->authority().first
== mds
->get_nodeid() ||
648 dir
->authority().second
== mds
->get_nodeid());
653 for (map
<CDir
*,export_state_t
>::iterator p
= export_state
.begin();
654 p
!= export_state
.end();
656 CDir
*dir
= p
->first
;
657 if (p
->second
.state
== EXPORT_LOCKING
||
658 p
->second
.state
== EXPORT_DISCOVERING
||
659 p
->second
.state
== EXPORT_FREEZING
||
660 p
->second
.state
== EXPORT_CANCELLING
)
662 assert(dir
->is_ambiguous_dir_auth());
663 assert(dir
->authority().first
== mds
->get_nodeid() ||
664 dir
->authority().second
== mds
->get_nodeid());
667 // ambiguous+me subtrees should be importing|exporting
676 // ==========================================================
679 void Migrator::export_dir_nicely(CDir
*dir
, mds_rank_t dest
)
682 dout(7) << "export_dir_nicely " << *dir
<< " to " << dest
<< dendl
;
683 export_queue
.push_back(pair
<dirfrag_t
,mds_rank_t
>(dir
->dirfrag(), dest
));
685 maybe_do_queued_export();
688 void Migrator::maybe_do_queued_export()
694 while (!export_queue
.empty() &&
695 export_state
.size() <= 4) {
696 dirfrag_t df
= export_queue
.front().first
;
697 mds_rank_t dest
= export_queue
.front().second
;
698 export_queue
.pop_front();
700 CDir
*dir
= mds
->mdcache
->get_dirfrag(df
);
702 if (!dir
->is_auth()) continue;
704 dout(0) << "nicely exporting to mds." << dest
<< " " << *dir
<< dendl
;
706 export_dir(dir
, dest
);
714 class C_MDC_ExportFreeze
: public MigratorContext
{
715 CDir
*ex
; // dir i'm exporting
718 C_MDC_ExportFreeze(Migrator
*m
, CDir
*e
, uint64_t t
) :
719 MigratorContext(m
), ex(e
), tid(t
) {
722 void finish(int r
) override
{
724 mig
->export_frozen(ex
, tid
);
729 void Migrator::get_export_lock_set(CDir
*dir
, set
<SimpleLock
*>& locks
)
732 vector
<CDentry
*> trace
;
733 cache
->make_trace(trace
, dir
->inode
);
734 for (vector
<CDentry
*>::iterator it
= trace
.begin();
737 locks
.insert(&(*it
)->lock
);
739 // prevent scatter gather race
740 locks
.insert(&dir
->get_inode()->dirfragtreelock
);
743 // NOTE: We need to take an rdlock on bounding dirfrags during
744 // migration for a rather irritating reason: when we export the
745 // bound inode, we need to send scatterlock state for the dirfrags
746 // as well, so that the new auth also gets the correct info. If we
747 // race with a refragment, this info is useless, as we can't
748 // redivvy it up. And it's needed for the scatterlocks to work
749 // properly: when the auth is in a sync/lock state it keeps each
750 // dirfrag's portion in the local (auth OR replica) dirfrag.
751 set
<CDir
*> wouldbe_bounds
;
752 cache
->get_wouldbe_subtree_bounds(dir
, wouldbe_bounds
);
753 for (set
<CDir
*>::iterator p
= wouldbe_bounds
.begin(); p
!= wouldbe_bounds
.end(); ++p
)
754 locks
.insert(&(*p
)->get_inode()->dirfragtreelock
);
758 class C_M_ExportDirWait
: public MigratorContext
{
762 C_M_ExportDirWait(Migrator
*m
, MDRequestRef mdr
, int count
)
763 : MigratorContext(m
), mdr(mdr
), count(count
) {}
764 void finish(int r
) override
{
765 mig
->dispatch_export_dir(mdr
, count
);
770 /** export_dir(dir, dest)
771 * public method to initiate an export.
772 * will fail if the directory is freezing, frozen, unpinnable, or root.
774 void Migrator::export_dir(CDir
*dir
, mds_rank_t dest
)
776 dout(7) << "export_dir " << *dir
<< " to " << dest
<< dendl
;
777 assert(dir
->is_auth());
778 assert(dest
!= mds
->get_nodeid());
780 if (mds
->mdcache
->is_readonly()) {
781 dout(7) << "read-only FS, no exports for now" << dendl
;
784 if (!mds
->mdsmap
->is_active(dest
)) {
785 dout(7) << "dest not active, no exports for now" << dendl
;
788 if (mds
->is_cluster_degraded()) {
789 dout(7) << "cluster degraded, no exports for now" << dendl
;
792 if (dir
->inode
->is_system()) {
793 dout(7) << "i won't export system dirs (root, mdsdirs, stray, /.ceph, etc.)" << dendl
;
798 if (!dir
->inode
->is_base() && dir
->inode
->get_projected_parent_dir()->inode
->is_stray() &&
799 dir
->inode
->get_projected_parent_dir()->get_parent_dir()->ino() != MDS_INO_MDSDIR(dest
)) {
800 dout(7) << "i won't export anything in stray" << dendl
;
804 if (dir
->is_frozen() ||
805 dir
->is_freezing()) {
806 dout(7) << " can't export, freezing|frozen. wait for other exports to finish first." << dendl
;
809 if (dir
->state_test(CDir::STATE_EXPORTING
)) {
810 dout(7) << "already exporting" << dendl
;
814 if (!mds
->is_stopping() && !dir
->inode
->is_exportable(dest
)) {
815 dout(7) << "dir is export pinned" << dendl
;
819 if (dest
== mds
->get_nodeid() || !mds
->mdsmap
->is_up(dest
)) {
820 dout(7) << "cannot export: dest " << dest
<< " is me or is not active" << dendl
;
824 if (g_conf
->mds_thrash_exports
) {
825 // create random subtree bound (which will not be exported)
827 for (auto p
= dir
->begin(); p
!= dir
->end(); ++p
) {
829 CDentry::linkage_t
*dnl
= dn
->get_linkage();
830 if (dnl
->is_primary()) {
831 CInode
*in
= dnl
->get_inode();
833 in
->get_nested_dirfrags(ls
);
837 int n
= rand() % ls
.size();
841 if (!(bd
->is_frozen() || bd
->is_freezing())) {
842 assert(bd
->is_auth());
843 dir
->state_set(CDir::STATE_AUXSUBTREE
);
844 mds
->mdcache
->adjust_subtree_auth(dir
, mds
->get_nodeid());
845 dout(0) << "export_dir: create aux subtree " << *bd
<< " under " << *dir
<< dendl
;
850 mds
->hit_export_target(ceph_clock_now(), dest
, -1);
853 dir
->state_set(CDir::STATE_EXPORTING
);
855 MDRequestRef mdr
= mds
->mdcache
->request_start_internal(CEPH_MDS_OP_EXPORTDIR
);
856 mdr
->more()->export_dir
= dir
;
858 assert(export_state
.count(dir
) == 0);
859 export_state_t
& stat
= export_state
[dir
];
860 stat
.state
= EXPORT_LOCKING
;
862 stat
.tid
= mdr
->reqid
.tid
;
865 return mds
->mdcache
->dispatch_request(mdr
);
868 void Migrator::dispatch_export_dir(MDRequestRef
& mdr
, int count
)
870 dout(7) << "dispatch_export_dir " << *mdr
<< dendl
;
872 CDir
*dir
= mdr
->more()->export_dir
;
873 map
<CDir
*,export_state_t
>::iterator it
= export_state
.find(dir
);
874 if (it
== export_state
.end() || it
->second
.tid
!= mdr
->reqid
.tid
) {
875 // export must have aborted.
876 dout(7) << "export must have aborted " << *mdr
<< dendl
;
877 mds
->mdcache
->request_finish(mdr
);
880 assert(it
->second
.state
== EXPORT_LOCKING
);
882 mds_rank_t dest
= it
->second
.peer
;
884 if (!mds
->is_export_target(dest
)) {
885 dout(7) << "dest is not yet an export target" << dendl
;
887 dout(5) << "dest has not been added as export target after three MDSMap epochs, canceling export" << dendl
;
888 export_try_cancel(dir
);
891 mds
->wait_for_mdsmap(mds
->mdsmap
->get_epoch(), new C_M_ExportDirWait(this, mdr
, count
+1));
895 if (!dir
->inode
->get_parent_dn()) {
896 dout(7) << "waiting for dir to become stable before export: " << *dir
<< dendl
;
897 dir
->add_waiter(CDir::WAIT_CREATED
, new C_M_ExportDirWait(this, mdr
, 1));
901 if (mdr
->aborted
|| dir
->is_frozen() || dir
->is_freezing()) {
902 dout(7) << "wouldblock|freezing|frozen, canceling export" << dendl
;
903 export_try_cancel(dir
);
908 set
<SimpleLock
*> rdlocks
;
909 set
<SimpleLock
*> xlocks
;
910 set
<SimpleLock
*> wrlocks
;
911 get_export_lock_set(dir
, rdlocks
);
912 // If auth MDS of the subtree root inode is neither the exporter MDS
913 // nor the importer MDS and it gathers subtree root's fragstat/neststat
914 // while the subtree is exporting. It's possible that the exporter MDS
915 // and the importer MDS both are auth MDS of the subtree root or both
916 // are not auth MDS of the subtree root at the time they receive the
917 // lock messages. So the auth MDS of the subtree root inode may get no
918 // or duplicated fragstat/neststat for the subtree root dirfrag.
919 wrlocks
.insert(&dir
->get_inode()->filelock
);
920 wrlocks
.insert(&dir
->get_inode()->nestlock
);
921 if (dir
->get_inode()->is_auth()) {
922 dir
->get_inode()->filelock
.set_scatter_wanted();
923 dir
->get_inode()->nestlock
.set_scatter_wanted();
926 if (!mds
->locker
->acquire_locks(mdr
, rdlocks
, wrlocks
, xlocks
, NULL
, NULL
, true)) {
928 export_try_cancel(dir
);
932 assert(g_conf
->mds_kill_export_at
!= 1);
933 it
->second
.state
= EXPORT_DISCOVERING
;
935 // send ExportDirDiscover (ask target)
937 dir
->inode
->make_path(path
);
938 MExportDirDiscover
*discover
= new MExportDirDiscover(dir
->dirfrag(), path
,
941 mds
->send_message_mds(discover
, dest
);
942 assert(g_conf
->mds_kill_export_at
!= 2);
944 it
->second
.last_cum_auth_pins_change
= ceph_clock_now();
946 // start the freeze, but hold it up with an auth_pin.
948 assert(dir
->is_freezing_tree());
949 dir
->add_waiter(CDir::WAIT_FROZEN
, new C_MDC_ExportFreeze(this, dir
, it
->second
.tid
));
953 * called on receipt of MExportDirDiscoverAck
954 * the importer now has the directory's _inode_ in memory, and pinned.
956 * This function DOES put the passed message before returning
958 void Migrator::handle_export_discover_ack(MExportDirDiscoverAck
*m
)
960 CDir
*dir
= cache
->get_dirfrag(m
->get_dirfrag());
961 mds_rank_t
dest(m
->get_source().num());
962 utime_t now
= ceph_clock_now();
965 dout(7) << "export_discover_ack from " << m
->get_source()
966 << " on " << *dir
<< dendl
;
968 mds
->hit_export_target(now
, dest
, -1);
970 map
<CDir
*,export_state_t
>::iterator it
= export_state
.find(dir
);
971 if (it
== export_state
.end() ||
972 it
->second
.tid
!= m
->get_tid() ||
973 it
->second
.peer
!= dest
) {
974 dout(7) << "must have aborted" << dendl
;
976 assert(it
->second
.state
== EXPORT_DISCOVERING
);
977 // release locks to avoid deadlock
978 MDRequestRef mdr
= static_cast<MDRequestImpl
*>(it
->second
.mut
.get());
980 mds
->mdcache
->request_finish(mdr
);
981 it
->second
.mut
.reset();
982 // freeze the subtree
983 it
->second
.state
= EXPORT_FREEZING
;
984 dir
->auth_unpin(this);
985 assert(g_conf
->mds_kill_export_at
!= 3);
991 class C_M_ExportSessionsFlushed
: public MigratorContext
{
995 C_M_ExportSessionsFlushed(Migrator
*m
, CDir
*d
, uint64_t t
)
996 : MigratorContext(m
), dir(d
), tid(t
) {
999 void finish(int r
) override
{
1000 mig
->export_sessions_flushed(dir
, tid
);
1004 void Migrator::export_sessions_flushed(CDir
*dir
, uint64_t tid
)
1006 dout(7) << "export_sessions_flushed " << *dir
<< dendl
;
1008 map
<CDir
*,export_state_t
>::iterator it
= export_state
.find(dir
);
1009 if (it
== export_state
.end() ||
1010 it
->second
.state
== EXPORT_CANCELLING
||
1011 it
->second
.tid
!= tid
) {
1012 // export must have aborted.
1013 dout(7) << "export must have aborted on " << dir
<< dendl
;
1017 assert(it
->second
.state
== EXPORT_PREPPING
|| it
->second
.state
== EXPORT_WARNING
);
1018 assert(it
->second
.warning_ack_waiting
.count(MDS_RANK_NONE
) > 0);
1019 it
->second
.warning_ack_waiting
.erase(MDS_RANK_NONE
);
1020 if (it
->second
.state
== EXPORT_WARNING
&& it
->second
.warning_ack_waiting
.empty())
1021 export_go(dir
); // start export.
1024 void Migrator::export_frozen(CDir
*dir
, uint64_t tid
)
1026 dout(7) << "export_frozen on " << *dir
<< dendl
;
1028 map
<CDir
*,export_state_t
>::iterator it
= export_state
.find(dir
);
1029 if (it
== export_state
.end() || it
->second
.tid
!= tid
) {
1030 dout(7) << "export must have aborted" << dendl
;
1034 assert(it
->second
.state
== EXPORT_FREEZING
);
1035 assert(dir
->is_frozen_tree_root());
1036 assert(dir
->get_cum_auth_pins() == 0);
1038 CInode
*diri
= dir
->get_inode();
1040 // ok, try to grab all my locks.
1041 set
<SimpleLock
*> rdlocks
;
1042 get_export_lock_set(dir
, rdlocks
);
1043 if ((diri
->is_auth() && diri
->is_frozen()) ||
1044 !mds
->locker
->can_rdlock_set(rdlocks
) ||
1045 !diri
->filelock
.can_wrlock(-1) ||
1046 !diri
->nestlock
.can_wrlock(-1)) {
1047 dout(7) << "export_dir couldn't acquire all needed locks, failing. "
1051 dir
->unfreeze_tree();
1052 dir
->state_clear(CDir::STATE_EXPORTING
);
1054 mds
->send_message_mds(new MExportDirCancel(dir
->dirfrag(), it
->second
.tid
), it
->second
.peer
);
1056 export_state
.erase(it
);
1060 it
->second
.mut
= new MutationImpl();
1061 if (diri
->is_auth())
1062 it
->second
.mut
->auth_pin(diri
);
1063 mds
->locker
->rdlock_take_set(rdlocks
, it
->second
.mut
);
1064 mds
->locker
->wrlock_force(&diri
->filelock
, it
->second
.mut
);
1065 mds
->locker
->wrlock_force(&diri
->nestlock
, it
->second
.mut
);
1067 cache
->show_subtrees();
1070 // force it into a subtree by listing auth as <me,me>.
1071 cache
->adjust_subtree_auth(dir
, mds
->get_nodeid(), mds
->get_nodeid());
1073 cache
->get_subtree_bounds(dir
, bounds
);
1075 // generate prep message, log entry.
1076 MExportDirPrep
*prep
= new MExportDirPrep(dir
->dirfrag(), it
->second
.tid
);
1078 // include list of bystanders
1079 for (compact_map
<mds_rank_t
,unsigned>::iterator p
= dir
->replicas_begin();
1080 p
!= dir
->replicas_end();
1082 if (p
->first
!= it
->second
.peer
) {
1083 dout(10) << "bystander mds." << p
->first
<< dendl
;
1084 prep
->add_bystander(p
->first
);
1088 // include base dirfrag
1089 cache
->replicate_dir(dir
, it
->second
.peer
, prep
->basedir
);
1092 * include spanning tree for all nested exports.
1093 * these need to be on the destination _before_ the final export so that
1094 * dir_auth updates on any nested exports are properly absorbed.
1095 * this includes inodes and dirfrags included in the subtree, but
1096 * only the inodes at the bounds.
1098 * each trace is: df ('-' | ('f' dir | 'd') dentry inode (dir dentry inode)*)
1100 set
<inodeno_t
> inodes_added
;
1101 set
<dirfrag_t
> dirfrags_added
;
1104 for (set
<CDir
*>::iterator p
= bounds
.begin();
1110 bound
->get(CDir::PIN_EXPORTBOUND
);
1111 bound
->state_set(CDir::STATE_EXPORTBOUND
);
1113 dout(7) << " export bound " << *bound
<< dendl
;
1114 prep
->add_bound( bound
->dirfrag() );
1122 // don't repeat inodes
1123 if (inodes_added
.count(cur
->inode
->ino()))
1125 inodes_added
.insert(cur
->inode
->ino());
1127 // prepend dentry + inode
1128 assert(cur
->inode
->is_auth());
1130 cache
->replicate_dentry(cur
->inode
->parent
, it
->second
.peer
, bl
);
1131 dout(7) << " added " << *cur
->inode
->parent
<< dendl
;
1132 cache
->replicate_inode(cur
->inode
, it
->second
.peer
, bl
,
1133 mds
->mdsmap
->get_up_features());
1134 dout(7) << " added " << *cur
->inode
<< dendl
;
1135 bl
.claim_append(tracebl
);
1138 cur
= cur
->get_parent_dir();
1140 // don't repeat dirfrags
1141 if (dirfrags_added
.count(cur
->dirfrag()) ||
1143 start
= 'd'; // start with dentry
1146 dirfrags_added
.insert(cur
->dirfrag());
1149 cache
->replicate_dir(cur
, it
->second
.peer
, bl
);
1150 dout(7) << " added " << *cur
<< dendl
;
1151 bl
.claim_append(tracebl
);
1154 start
= 'f'; // start with dirfrag
1156 bufferlist final_bl
;
1157 dirfrag_t df
= cur
->dirfrag();
1158 ::encode(df
, final_bl
);
1159 ::encode(start
, final_bl
);
1160 final_bl
.claim_append(tracebl
);
1161 prep
->add_trace(final_bl
);
1165 it
->second
.state
= EXPORT_PREPPING
;
1166 mds
->send_message_mds(prep
, it
->second
.peer
);
1167 assert (g_conf
->mds_kill_export_at
!= 4);
1169 // make sure any new instantiations of caps are flushed out
1170 assert(it
->second
.warning_ack_waiting
.empty());
1172 set
<client_t
> export_client_set
;
1173 get_export_client_set(dir
, export_client_set
);
1175 MDSGatherBuilder
gather(g_ceph_context
);
1176 mds
->server
->flush_client_sessions(export_client_set
, gather
);
1177 if (gather
.has_subs()) {
1178 it
->second
.warning_ack_waiting
.insert(MDS_RANK_NONE
);
1179 gather
.set_finisher(new C_M_ExportSessionsFlushed(this, dir
, it
->second
.tid
));
1184 void Migrator::get_export_client_set(CDir
*dir
, set
<client_t
>& client_set
)
1188 while (!dfs
.empty()) {
1189 CDir
*dir
= dfs
.front();
1191 for (CDir::map_t::iterator p
= dir
->begin(); p
!= dir
->end(); ++p
) {
1192 CDentry
*dn
= p
->second
;
1193 if (!dn
->get_linkage()->is_primary())
1195 CInode
*in
= dn
->get_linkage()->get_inode();
1199 in
->get_dirfrags(ls
);
1200 for (list
<CDir
*>::iterator q
= ls
.begin(); q
!= ls
.end(); ++q
) {
1201 if (!(*q
)->state_test(CDir::STATE_EXPORTBOUND
)) {
1202 // include nested dirfrag
1203 assert((*q
)->get_dir_auth().first
== CDIR_AUTH_PARENT
);
1204 dfs
.push_back(*q
); // it's ours, recurse (later)
1208 for (map
<client_t
, Capability
*>::iterator q
= in
->client_caps
.begin();
1209 q
!= in
->client_caps
.end();
1211 client_set
.insert(q
->first
);
1216 void Migrator::get_export_client_set(CInode
*in
, set
<client_t
>& client_set
)
1218 for (map
<client_t
, Capability
*>::iterator q
= in
->client_caps
.begin();
1219 q
!= in
->client_caps
.end();
1221 client_set
.insert(q
->first
);
1224 /* This function DOES put the passed message before returning*/
1225 void Migrator::handle_export_prep_ack(MExportDirPrepAck
*m
)
1227 CDir
*dir
= cache
->get_dirfrag(m
->get_dirfrag());
1228 mds_rank_t
dest(m
->get_source().num());
1229 utime_t now
= ceph_clock_now();
1232 dout(7) << "export_prep_ack " << *dir
<< dendl
;
1234 mds
->hit_export_target(now
, dest
, -1);
1236 map
<CDir
*,export_state_t
>::iterator it
= export_state
.find(dir
);
1237 if (it
== export_state
.end() ||
1238 it
->second
.tid
!= m
->get_tid() ||
1239 it
->second
.peer
!= mds_rank_t(m
->get_source().num())) {
1240 // export must have aborted.
1241 dout(7) << "export must have aborted" << dendl
;
1245 assert(it
->second
.state
== EXPORT_PREPPING
);
1247 if (!m
->is_success()) {
1248 dout(7) << "peer couldn't acquire all needed locks, canceling" << dendl
;
1249 export_try_cancel(dir
, false);
1254 assert (g_conf
->mds_kill_export_at
!= 5);
1257 cache
->get_subtree_bounds(dir
, bounds
);
1259 assert(it
->second
.warning_ack_waiting
.empty() ||
1260 (it
->second
.warning_ack_waiting
.size() == 1 &&
1261 it
->second
.warning_ack_waiting
.count(MDS_RANK_NONE
) > 0));
1262 assert(it
->second
.notify_ack_waiting
.empty());
1264 for (compact_map
<mds_rank_t
,unsigned>::iterator p
= dir
->replicas_begin();
1265 p
!= dir
->replicas_end();
1267 if (p
->first
== it
->second
.peer
) continue;
1268 if (mds
->is_cluster_degraded() &&
1269 !mds
->mdsmap
->is_clientreplay_or_active_or_stopping(p
->first
))
1270 continue; // only if active
1271 it
->second
.warning_ack_waiting
.insert(p
->first
);
1272 it
->second
.notify_ack_waiting
.insert(p
->first
); // we'll eventually get a notifyack, too!
1274 MExportDirNotify
*notify
= new MExportDirNotify(dir
->dirfrag(), it
->second
.tid
, true,
1275 mds_authority_t(mds
->get_nodeid(),CDIR_AUTH_UNKNOWN
),
1276 mds_authority_t(mds
->get_nodeid(),it
->second
.peer
));
1277 for (set
<CDir
*>::iterator q
= bounds
.begin(); q
!= bounds
.end(); ++q
)
1278 notify
->get_bounds().push_back((*q
)->dirfrag());
1279 mds
->send_message_mds(notify
, p
->first
);
1283 it
->second
.state
= EXPORT_WARNING
;
1285 assert(g_conf
->mds_kill_export_at
!= 6);
1287 if (it
->second
.warning_ack_waiting
.empty())
1288 export_go(dir
); // start export.
1295 class C_M_ExportGo
: public MigratorContext
{
1299 C_M_ExportGo(Migrator
*m
, CDir
*d
, uint64_t t
) :
1300 MigratorContext(m
), dir(d
), tid(t
) {
1301 assert(dir
!= NULL
);
1303 void finish(int r
) override
{
1304 mig
->export_go_synced(dir
, tid
);
1308 void Migrator::export_go(CDir
*dir
)
1310 assert(export_state
.count(dir
));
1311 dout(7) << "export_go " << *dir
<< " to " << export_state
[dir
].peer
<< dendl
;
1313 // first sync log to flush out e.g. any cap imports
1314 mds
->mdlog
->wait_for_safe(new C_M_ExportGo(this, dir
, export_state
[dir
].tid
));
1315 mds
->mdlog
->flush();
1318 void Migrator::export_go_synced(CDir
*dir
, uint64_t tid
)
1320 map
<CDir
*,export_state_t
>::iterator it
= export_state
.find(dir
);
1321 if (it
== export_state
.end() ||
1322 it
->second
.state
== EXPORT_CANCELLING
||
1323 it
->second
.tid
!= tid
) {
1324 // export must have aborted.
1325 dout(7) << "export must have aborted on " << dir
<< dendl
;
1328 assert(it
->second
.state
== EXPORT_WARNING
);
1329 mds_rank_t dest
= it
->second
.peer
;
1331 dout(7) << "export_go_synced " << *dir
<< " to " << dest
<< dendl
;
1333 cache
->show_subtrees();
1335 it
->second
.state
= EXPORT_EXPORTING
;
1336 assert(g_conf
->mds_kill_export_at
!= 7);
1338 assert(dir
->is_frozen_tree_root());
1339 assert(dir
->get_cum_auth_pins() == 0);
1341 // set ambiguous auth
1342 cache
->adjust_subtree_auth(dir
, mds
->get_nodeid(), dest
);
1344 // take away the popularity we're sending.
1345 utime_t now
= ceph_clock_now();
1346 mds
->balancer
->subtract_export(dir
, now
);
1348 // fill export message with cache data
1349 MExportDir
*req
= new MExportDir(dir
->dirfrag(), it
->second
.tid
);
1350 map
<client_t
,entity_inst_t
> exported_client_map
;
1351 uint64_t num_exported_inodes
= encode_export_dir(req
->export_data
,
1352 dir
, // recur start point
1353 exported_client_map
,
1355 ::encode(exported_client_map
, req
->client_map
,
1356 mds
->mdsmap
->get_up_features());
1358 // add bounds to message
1360 cache
->get_subtree_bounds(dir
, bounds
);
1361 for (set
<CDir
*>::iterator p
= bounds
.begin();
1364 req
->add_export((*p
)->dirfrag());
1367 mds
->send_message_mds(req
, dest
);
1368 assert(g_conf
->mds_kill_export_at
!= 8);
1370 mds
->hit_export_target(now
, dest
, num_exported_inodes
+1);
1373 if (mds
->logger
) mds
->logger
->inc(l_mds_exported
);
1374 if (mds
->logger
) mds
->logger
->inc(l_mds_exported_inodes
, num_exported_inodes
);
1376 cache
->show_subtrees();
1380 /** encode_export_inode
1381 * update our local state for this inode to export.
1382 * encode relevant state to be sent over the wire.
1383 * used by: encode_export_dir, file_rename (if foreign)
1385 * FIXME: the separation between CInode.encode_export and these methods
1386 * is pretty arbitrary and dumb.
1388 void Migrator::encode_export_inode(CInode
*in
, bufferlist
& enc_state
,
1389 map
<client_t
,entity_inst_t
>& exported_client_map
)
1391 dout(7) << "encode_export_inode " << *in
<< dendl
;
1392 assert(!in
->is_replica(mds
->get_nodeid()));
1395 if (!in
->is_replicated()) {
1396 in
->replicate_relax_locks();
1397 dout(20) << " did replicate_relax_locks, now " << *in
<< dendl
;
1400 ::encode(in
->inode
.ino
, enc_state
);
1401 ::encode(in
->last
, enc_state
);
1402 in
->encode_export(enc_state
);
1405 encode_export_inode_caps(in
, true, enc_state
, exported_client_map
);
1408 void Migrator::encode_export_inode_caps(CInode
*in
, bool auth_cap
, bufferlist
& bl
,
1409 map
<client_t
,entity_inst_t
>& exported_client_map
)
1411 dout(20) << "encode_export_inode_caps " << *in
<< dendl
;
1414 map
<client_t
,Capability::Export
> cap_map
;
1415 in
->export_client_caps(cap_map
);
1416 ::encode(cap_map
, bl
);
1418 ::encode(in
->get_mds_caps_wanted(), bl
);
1420 in
->state_set(CInode::STATE_EXPORTINGCAPS
);
1421 in
->get(CInode::PIN_EXPORTINGCAPS
);
1424 // make note of clients named by exported capabilities
1425 for (map
<client_t
, Capability
*>::iterator it
= in
->client_caps
.begin();
1426 it
!= in
->client_caps
.end();
1428 exported_client_map
[it
->first
] = mds
->sessionmap
.get_inst(entity_name_t::CLIENT(it
->first
.v
));
1431 void Migrator::finish_export_inode_caps(CInode
*in
, mds_rank_t peer
,
1432 map
<client_t
,Capability::Import
>& peer_imported
)
1434 dout(20) << "finish_export_inode_caps " << *in
<< dendl
;
1436 in
->state_clear(CInode::STATE_EXPORTINGCAPS
);
1437 in
->put(CInode::PIN_EXPORTINGCAPS
);
1439 // tell (all) clients about migrating caps..
1440 for (map
<client_t
, Capability
*>::iterator it
= in
->client_caps
.begin();
1441 it
!= in
->client_caps
.end();
1443 Capability
*cap
= it
->second
;
1444 dout(7) << "finish_export_inode_caps telling client." << it
->first
1445 << " exported caps on " << *in
<< dendl
;
1446 MClientCaps
*m
= new MClientCaps(CEPH_CAP_OP_EXPORT
, in
->ino(), 0,
1447 cap
->get_cap_id(), cap
->get_mseq(), mds
->get_osd_epoch_barrier());
1449 map
<client_t
,Capability::Import
>::iterator q
= peer_imported
.find(it
->first
);
1450 assert(q
!= peer_imported
.end());
1451 m
->set_cap_peer(q
->second
.cap_id
, q
->second
.issue_seq
, q
->second
.mseq
, peer
, 0);
1452 mds
->send_message_client_counted(m
, it
->first
);
1454 in
->clear_client_caps_after_export();
1455 mds
->locker
->eval(in
, CEPH_CAP_LOCKS
);
1458 void Migrator::finish_export_inode(CInode
*in
, utime_t now
, mds_rank_t peer
,
1459 map
<client_t
,Capability::Import
>& peer_imported
,
1460 list
<MDSInternalContextBase
*>& finished
)
1462 dout(12) << "finish_export_inode " << *in
<< dendl
;
1468 // clear/unpin cached_by (we're no longer the authority)
1469 in
->clear_replica_map();
1471 // twiddle lock states for auth -> replica transition
1472 in
->authlock
.export_twiddle();
1473 in
->linklock
.export_twiddle();
1474 in
->dirfragtreelock
.export_twiddle();
1475 in
->filelock
.export_twiddle();
1476 in
->nestlock
.export_twiddle();
1477 in
->xattrlock
.export_twiddle();
1478 in
->snaplock
.export_twiddle();
1479 in
->flocklock
.export_twiddle();
1480 in
->policylock
.export_twiddle();
1483 assert(in
->is_auth());
1484 in
->state_clear(CInode::STATE_AUTH
);
1485 in
->replica_nonce
= CInode::EXPORT_NONCE
;
1487 in
->clear_dirty_rstat();
1489 // no more auth subtree? clear scatter dirty
1490 if (!in
->has_subtree_root_dirfrag(mds
->get_nodeid()))
1491 in
->clear_scatter_dirty();
1493 in
->item_open_file
.remove_myself();
1495 in
->clear_dirty_parent();
1497 in
->clear_file_locks();
1500 in
->take_waiting(CInode::WAIT_ANY_MASK
, finished
);
1502 in
->finish_export(now
);
1504 finish_export_inode_caps(in
, peer
, peer_imported
);
1507 uint64_t Migrator::encode_export_dir(bufferlist
& exportbl
,
1509 map
<client_t
,entity_inst_t
>& exported_client_map
,
1512 uint64_t num_exported
= 0;
1514 dout(7) << "encode_export_dir " << *dir
<< " " << dir
->get_num_head_items() << " head items" << dendl
;
1516 assert(dir
->get_projected_version() == dir
->get_version());
1518 #ifdef MDS_VERIFY_FRAGSTAT
1519 if (dir
->is_complete())
1520 dir
->verify_fragstat();
1524 dirfrag_t df
= dir
->dirfrag();
1525 ::encode(df
, exportbl
);
1526 dir
->encode_export(exportbl
);
1528 __u32 nden
= dir
->items
.size();
1529 ::encode(nden
, exportbl
);
1532 list
<CDir
*> subdirs
;
1533 CDir::map_t::iterator it
;
1534 for (it
= dir
->begin(); it
!= dir
->end(); ++it
) {
1535 CDentry
*dn
= it
->second
;
1536 CInode
*in
= dn
->get_linkage()->get_inode();
1538 if (!dn
->is_replicated())
1539 dn
->lock
.replicate_relax();
1544 dout(7) << "encode_export_dir exporting " << *dn
<< dendl
;
1547 ::encode(dn
->name
, exportbl
);
1548 ::encode(dn
->last
, exportbl
);
1551 dn
->encode_export(exportbl
);
1556 if (dn
->get_linkage()->is_null()) {
1557 exportbl
.append("N", 1); // null dentry
1561 if (dn
->get_linkage()->is_remote()) {
1563 exportbl
.append("L", 1); // remote link
1565 inodeno_t ino
= dn
->get_linkage()->get_remote_ino();
1566 unsigned char d_type
= dn
->get_linkage()->get_remote_d_type();
1567 ::encode(ino
, exportbl
);
1568 ::encode(d_type
, exportbl
);
1574 exportbl
.append("I", 1); // inode dentry
1576 encode_export_inode(in
, exportbl
, exported_client_map
); // encode, and (update state for) export
1580 in
->get_dirfrags(dfs
);
1581 for (list
<CDir
*>::iterator p
= dfs
.begin(); p
!= dfs
.end(); ++p
) {
1583 if (!t
->state_test(CDir::STATE_EXPORTBOUND
)) {
1584 // include nested dirfrag
1585 assert(t
->get_dir_auth().first
== CDIR_AUTH_PARENT
);
1586 subdirs
.push_back(t
); // it's ours, recurse (later)
1592 for (list
<CDir
*>::iterator it
= subdirs
.begin(); it
!= subdirs
.end(); ++it
)
1593 num_exported
+= encode_export_dir(exportbl
, *it
, exported_client_map
, now
);
1595 return num_exported
;
1598 void Migrator::finish_export_dir(CDir
*dir
, utime_t now
, mds_rank_t peer
,
1599 map
<inodeno_t
,map
<client_t
,Capability::Import
> >& peer_imported
,
1600 list
<MDSInternalContextBase
*>& finished
, int *num_dentries
)
1602 dout(10) << "finish_export_dir " << *dir
<< dendl
;
1605 dir
->clear_replica_map();
1608 assert(dir
->is_auth());
1609 dir
->state_clear(CDir::STATE_AUTH
);
1610 dir
->remove_bloom();
1611 dir
->replica_nonce
= CDir::EXPORT_NONCE
;
1613 if (dir
->is_dirty())
1616 // suck up all waiters
1617 dir
->take_waiting(CDir::WAIT_ANY_MASK
, finished
); // all dir waiters
1620 dir
->finish_export(now
);
1623 list
<CDir
*> subdirs
;
1624 CDir::map_t::iterator it
;
1625 for (it
= dir
->begin(); it
!= dir
->end(); ++it
) {
1626 CDentry
*dn
= it
->second
;
1627 CInode
*in
= dn
->get_linkage()->get_inode();
1630 dn
->finish_export();
1633 if (dn
->get_linkage()->is_primary()) {
1634 finish_export_inode(in
, now
, peer
, peer_imported
[in
->ino()], finished
);
1637 in
->get_nested_dirfrags(subdirs
);
1640 cache
->touch_dentry_bottom(dn
); // move dentry to tail of LRU
1645 for (list
<CDir
*>::iterator it
= subdirs
.begin(); it
!= subdirs
.end(); ++it
)
1646 finish_export_dir(*it
, now
, peer
, peer_imported
, finished
, num_dentries
);
1649 class C_MDS_ExportFinishLogged
: public MigratorLogContext
{
1652 C_MDS_ExportFinishLogged(Migrator
*m
, CDir
*d
) : MigratorLogContext(m
), dir(d
) {}
1653 void finish(int r
) override
{
1654 mig
->export_logged_finish(dir
);
1660 * i should get an export_ack from the export target.
1662 * This function DOES put the passed message before returning
1664 void Migrator::handle_export_ack(MExportDirAck
*m
)
1666 CDir
*dir
= cache
->get_dirfrag(m
->get_dirfrag());
1667 mds_rank_t
dest(m
->get_source().num());
1668 utime_t now
= ceph_clock_now();
1670 assert(dir
->is_frozen_tree_root()); // i'm exporting!
1673 dout(7) << "handle_export_ack " << *dir
<< dendl
;
1675 mds
->hit_export_target(now
, dest
, -1);
1677 map
<CDir
*,export_state_t
>::iterator it
= export_state
.find(dir
);
1678 assert(it
!= export_state
.end());
1679 assert(it
->second
.state
== EXPORT_EXPORTING
);
1680 assert(it
->second
.tid
== m
->get_tid());
1682 bufferlist::iterator bp
= m
->imported_caps
.begin();
1683 ::decode(it
->second
.peer_imported
, bp
);
1685 it
->second
.state
= EXPORT_LOGGINGFINISH
;
1686 assert (g_conf
->mds_kill_export_at
!= 9);
1688 cache
->get_subtree_bounds(dir
, bounds
);
1691 // include export bounds, to ensure they're in the journal.
1692 EExport
*le
= new EExport(mds
->mdlog
, dir
, it
->second
.peer
);;
1693 mds
->mdlog
->start_entry(le
);
1695 le
->metablob
.add_dir_context(dir
, EMetaBlob::TO_ROOT
);
1696 le
->metablob
.add_dir(dir
, false);
1697 for (set
<CDir
*>::iterator p
= bounds
.begin();
1701 le
->get_bounds().insert(bound
->dirfrag());
1702 le
->metablob
.add_dir_context(bound
);
1703 le
->metablob
.add_dir(bound
, false);
1706 // list us second, them first.
1707 // this keeps authority().first in sync with subtree auth state in the journal.
1708 cache
->adjust_subtree_auth(dir
, it
->second
.peer
, mds
->get_nodeid());
1710 // log export completion, then finish (unfreeze, trigger finish context, etc.)
1711 mds
->mdlog
->submit_entry(le
, new C_MDS_ExportFinishLogged(this, dir
));
1712 mds
->mdlog
->flush();
1713 assert (g_conf
->mds_kill_export_at
!= 10);
1718 void Migrator::export_notify_abort(CDir
*dir
, set
<CDir
*>& bounds
)
1720 dout(7) << "export_notify_abort " << *dir
<< dendl
;
1722 export_state_t
& stat
= export_state
[dir
];
1723 assert(stat
.state
== EXPORT_CANCELLING
);
1725 if (stat
.notify_ack_waiting
.empty()) {
1726 stat
.state
= EXPORT_CANCELLED
;
1730 dir
->auth_pin(this);
1732 for (set
<mds_rank_t
>::iterator p
= stat
.notify_ack_waiting
.begin();
1733 p
!= stat
.notify_ack_waiting
.end();
1735 MExportDirNotify
*notify
= new MExportDirNotify(dir
->dirfrag(),stat
.tid
, true,
1736 pair
<int,int>(mds
->get_nodeid(),stat
.peer
),
1737 pair
<int,int>(mds
->get_nodeid(),CDIR_AUTH_UNKNOWN
));
1738 for (set
<CDir
*>::iterator i
= bounds
.begin(); i
!= bounds
.end(); ++i
)
1739 notify
->get_bounds().push_back((*i
)->dirfrag());
1740 mds
->send_message_mds(notify
, *p
);
1745 * this happens if hte dest failes after i send teh export data but before it is acked
1746 * that is, we don't know they safely received and logged it, so we reverse our changes
1749 void Migrator::export_reverse(CDir
*dir
)
1751 dout(7) << "export_reverse " << *dir
<< dendl
;
1753 set
<CInode
*> to_eval
;
1756 cache
->get_subtree_bounds(dir
, bounds
);
1758 // remove exporting pins
1761 while (!rq
.empty()) {
1762 CDir
*t
= rq
.front();
1765 for (CDir::map_t::iterator p
= t
->items
.begin(); p
!= t
->items
.end(); ++p
) {
1766 p
->second
->abort_export();
1767 if (!p
->second
->get_linkage()->is_primary())
1769 CInode
*in
= p
->second
->get_linkage()->get_inode();
1771 if (in
->state_test(CInode::STATE_EVALSTALECAPS
)) {
1772 in
->state_clear(CInode::STATE_EVALSTALECAPS
);
1776 in
->get_nested_dirfrags(rq
);
1781 for (const auto &bd
: bounds
) {
1782 bd
->put(CDir::PIN_EXPORTBOUND
);
1783 bd
->state_clear(CDir::STATE_EXPORTBOUND
);
1786 // adjust auth, with possible subtree merge.
1787 cache
->adjust_subtree_auth(dir
, mds
->get_nodeid());
1788 cache
->try_subtree_merge(dir
);
1790 // notify bystanders
1791 export_notify_abort(dir
, bounds
);
1793 // process delayed expires
1794 cache
->process_delayed_expire(dir
);
1797 dir
->unfreeze_tree();
1799 // revoke/resume stale caps
1800 for (auto in
: to_eval
) {
1801 bool need_issue
= false;
1802 for (auto& p
: in
->get_client_caps()) {
1803 Capability
*cap
= p
.second
;
1804 if (cap
->is_stale()) {
1805 mds
->locker
->revoke_stale_caps(cap
);
1811 (!in
->is_auth() || !mds
->locker
->eval(in
, CEPH_CAP_LOCKS
)))
1812 mds
->locker
->issue_caps(in
);
1815 cache
->show_cache();
1820 * once i get the ack, and logged the EExportFinish(true),
1821 * send notifies (if any), otherwise go straight to finish.
1824 void Migrator::export_logged_finish(CDir
*dir
)
1826 dout(7) << "export_logged_finish " << *dir
<< dendl
;
1828 export_state_t
& stat
= export_state
[dir
];
1832 cache
->get_subtree_bounds(dir
, bounds
);
1834 for (set
<mds_rank_t
>::iterator p
= stat
.notify_ack_waiting
.begin();
1835 p
!= stat
.notify_ack_waiting
.end();
1837 MExportDirNotify
*notify
= new MExportDirNotify(dir
->dirfrag(), stat
.tid
, true,
1838 pair
<int,int>(mds
->get_nodeid(), stat
.peer
),
1839 pair
<int,int>(stat
.peer
, CDIR_AUTH_UNKNOWN
));
1841 for (set
<CDir
*>::iterator i
= bounds
.begin(); i
!= bounds
.end(); ++i
)
1842 notify
->get_bounds().push_back((*i
)->dirfrag());
1844 mds
->send_message_mds(notify
, *p
);
1847 // wait for notifyacks
1848 stat
.state
= EXPORT_NOTIFYING
;
1849 assert (g_conf
->mds_kill_export_at
!= 11);
1851 // no notifies to wait for?
1852 if (stat
.notify_ack_waiting
.empty()) {
1853 export_finish(dir
); // skip notify/notify_ack stage.
1855 // notify peer to send cap import messages to clients
1856 if (!mds
->is_cluster_degraded() ||
1857 mds
->mdsmap
->is_clientreplay_or_active_or_stopping(stat
.peer
)) {
1858 mds
->send_message_mds(new MExportDirFinish(dir
->dirfrag(), false, stat
.tid
), stat
.peer
);
1860 dout(7) << "not sending MExportDirFinish, dest has failed" << dendl
;
1867 * i'll get an ack from each bystander.
1868 * when i get them all, do the export.
1870 * i'll get an ack from each bystander.
1871 * when i get them all, unfreeze and send the finish.
1873 * This function DOES put the passed message before returning
1875 void Migrator::handle_export_notify_ack(MExportDirNotifyAck
*m
)
1877 CDir
*dir
= cache
->get_dirfrag(m
->get_dirfrag());
1878 mds_rank_t
dest(m
->get_source().num());
1879 utime_t now
= ceph_clock_now();
1881 mds_rank_t from
= mds_rank_t(m
->get_source().num());
1883 mds
->hit_export_target(now
, dest
, -1);
1885 auto export_state_entry
= export_state
.find(dir
);
1886 if (export_state_entry
!= export_state
.end()) {
1887 export_state_t
& stat
= export_state_entry
->second
;
1888 if (stat
.state
== EXPORT_WARNING
&&
1889 stat
.warning_ack_waiting
.erase(from
)) {
1890 // exporting. process warning.
1891 dout(7) << "handle_export_notify_ack from " << m
->get_source()
1892 << ": exporting, processing warning on " << *dir
<< dendl
;
1893 if (stat
.warning_ack_waiting
.empty())
1894 export_go(dir
); // start export.
1895 } else if (stat
.state
== EXPORT_NOTIFYING
&&
1896 stat
.notify_ack_waiting
.erase(from
)) {
1897 // exporting. process notify.
1898 dout(7) << "handle_export_notify_ack from " << m
->get_source()
1899 << ": exporting, processing notify on " << *dir
<< dendl
;
1900 if (stat
.notify_ack_waiting
.empty())
1902 } else if (stat
.state
== EXPORT_CANCELLING
&&
1903 m
->get_new_auth().second
== CDIR_AUTH_UNKNOWN
&& // not warning ack
1904 stat
.notify_ack_waiting
.erase(from
)) {
1905 dout(7) << "handle_export_notify_ack from " << m
->get_source()
1906 << ": cancelling export, processing notify on " << *dir
<< dendl
;
1907 if (stat
.notify_ack_waiting
.empty()) {
1908 export_state
.erase(export_state_entry
);
1909 export_cancel_finish(dir
);
1914 auto import_state_entry
= import_state
.find(dir
->dirfrag());
1915 if (import_state_entry
!= import_state
.end()) {
1916 import_state_t
& stat
= import_state_entry
->second
;
1917 if (stat
.state
== IMPORT_ABORTING
) {
1919 dout(7) << "handle_export_notify_ack from " << m
->get_source()
1920 << ": aborting import on " << *dir
<< dendl
;
1921 assert(stat
.bystanders
.count(from
));
1922 stat
.bystanders
.erase(from
);
1923 if (stat
.bystanders
.empty())
1924 import_reverse_unfreeze(dir
);
1932 void Migrator::export_finish(CDir
*dir
)
1934 dout(5) << "export_finish " << *dir
<< dendl
;
1936 assert (g_conf
->mds_kill_export_at
!= 12);
1937 map
<CDir
*,export_state_t
>::iterator it
= export_state
.find(dir
);
1938 if (it
== export_state
.end()) {
1939 dout(7) << "target must have failed, not sending final commit message. export succeeded anyway." << dendl
;
1943 // send finish/commit to new auth
1944 if (!mds
->is_cluster_degraded() ||
1945 mds
->mdsmap
->is_clientreplay_or_active_or_stopping(it
->second
.peer
)) {
1946 mds
->send_message_mds(new MExportDirFinish(dir
->dirfrag(), true, it
->second
.tid
), it
->second
.peer
);
1948 dout(7) << "not sending MExportDirFinish last, dest has failed" << dendl
;
1950 assert(g_conf
->mds_kill_export_at
!= 13);
1952 // finish export (adjust local cache state)
1953 int num_dentries
= 0;
1954 C_ContextsBase
<MDSInternalContextBase
, MDSInternalContextGather
> *fin
= new C_ContextsBase
<MDSInternalContextBase
, MDSInternalContextGather
>(g_ceph_context
);
1955 finish_export_dir(dir
, ceph_clock_now(), it
->second
.peer
,
1956 it
->second
.peer_imported
, fin
->contexts
, &num_dentries
);
1960 cache
->get_subtree_bounds(dir
, bounds
);
1961 for (set
<CDir
*>::iterator p
= bounds
.begin();
1965 bd
->put(CDir::PIN_EXPORTBOUND
);
1966 bd
->state_clear(CDir::STATE_EXPORTBOUND
);
1969 if (dir
->state_test(CDir::STATE_AUXSUBTREE
))
1970 dir
->state_clear(CDir::STATE_AUXSUBTREE
);
1972 // adjust auth, with possible subtree merge.
1973 // (we do this _after_ removing EXPORTBOUND pins, to allow merges)
1974 cache
->adjust_subtree_auth(dir
, it
->second
.peer
);
1975 cache
->try_subtree_merge(dir
);
1977 // no more auth subtree? clear scatter dirty
1978 if (!dir
->get_inode()->is_auth() &&
1979 !dir
->get_inode()->has_subtree_root_dirfrag(mds
->get_nodeid())) {
1980 dir
->get_inode()->clear_scatter_dirty();
1981 // wake up scatter_nudge waiters
1982 dir
->get_inode()->take_waiting(CInode::WAIT_ANY_MASK
, fin
->contexts
);
1985 dir
->add_waiter(CDir::WAIT_UNFREEZE
, fin
);
1988 dout(7) << "export_finish unfreezing" << dendl
;
1989 dir
->unfreeze_tree();
1991 // discard delayed expires
1992 cache
->discard_delayed_expire(dir
);
1994 MutationRef mut
= it
->second
.mut
;
1995 // remove from exporting list, clean up state
1996 export_state
.erase(it
);
1997 dir
->state_clear(CDir::STATE_EXPORTING
);
1999 cache
->show_subtrees();
2002 cache
->trim(-1, num_dentries
); // try trimming exported dentries
2004 // send pending import_maps?
2005 mds
->mdcache
->maybe_send_pending_resolves();
2007 // drop locks, unpin path
2009 mds
->locker
->drop_locks(mut
.get());
2013 maybe_do_queued_export();
2023 // ==========================================================
2026 void Migrator::handle_export_discover(MExportDirDiscover
*m
)
2028 mds_rank_t from
= m
->get_source_mds();
2029 assert(from
!= mds
->get_nodeid());
2031 dout(7) << "handle_export_discover on " << m
->get_path() << dendl
;
2033 // note import state
2034 dirfrag_t df
= m
->get_dirfrag();
2035 // only start discovering on this message once.
2036 map
<dirfrag_t
,import_state_t
>::iterator it
= import_state
.find(df
);
2038 assert(it
== import_state
.end());
2040 import_state
[df
].state
= IMPORT_DISCOVERING
;
2041 import_state
[df
].peer
= from
;
2042 import_state
[df
].tid
= m
->get_tid();
2044 // am i retrying after ancient path_traverse results?
2045 if (it
== import_state
.end() ||
2046 it
->second
.peer
!= from
||
2047 it
->second
.tid
!= m
->get_tid()) {
2048 dout(7) << " dropping obsolete message" << dendl
;
2052 assert(it
->second
.state
== IMPORT_DISCOVERING
);
2055 if (!mds
->mdcache
->is_open()) {
2056 dout(5) << " waiting for root" << dendl
;
2057 mds
->mdcache
->wait_for_open(new C_MDS_RetryMessage(mds
, m
));
2061 assert (g_conf
->mds_kill_import_at
!= 1);
2064 CInode
*in
= cache
->get_inode(m
->get_dirfrag().ino
);
2066 // must discover it!
2067 filepath
fpath(m
->get_path());
2068 vector
<CDentry
*> trace
;
2069 MDRequestRef null_ref
;
2070 int r
= cache
->path_traverse(null_ref
, m
, NULL
, fpath
, &trace
, NULL
, MDS_TRAVERSE_DISCOVER
);
2073 dout(7) << "handle_export_discover_2 failed to discover or not dir " << m
->get_path() << ", NAK" << dendl
;
2074 ceph_abort(); // this shouldn't happen if the auth pins its path properly!!!!
2077 ceph_abort(); // this shouldn't happen; the get_inode above would have succeeded.
2081 dout(7) << "handle_export_discover have " << df
<< " inode " << *in
<< dendl
;
2083 import_state
[df
].state
= IMPORT_DISCOVERED
;
2085 // pin inode in the cache (for now)
2086 assert(in
->is_dir());
2087 in
->get(CInode::PIN_IMPORTING
);
2090 dout(7) << " sending export_discover_ack on " << *in
<< dendl
;
2091 mds
->send_message_mds(new MExportDirDiscoverAck(df
, m
->get_tid()), import_state
[df
].peer
);
2093 assert (g_conf
->mds_kill_import_at
!= 2);
2096 void Migrator::import_reverse_discovering(dirfrag_t df
)
2098 import_state
.erase(df
);
2101 void Migrator::import_reverse_discovered(dirfrag_t df
, CInode
*diri
)
2104 diri
->put(CInode::PIN_IMPORTING
);
2105 import_state
.erase(df
);
2108 void Migrator::import_reverse_prepping(CDir
*dir
)
2111 cache
->map_dirfrag_set(import_state
[dir
->dirfrag()].bound_ls
, bounds
);
2112 import_remove_pins(dir
, bounds
);
2113 import_reverse_final(dir
);
2116 /* This function DOES put the passed message before returning*/
2117 void Migrator::handle_export_cancel(MExportDirCancel
*m
)
2119 dout(7) << "handle_export_cancel on " << m
->get_dirfrag() << dendl
;
2120 dirfrag_t df
= m
->get_dirfrag();
2121 map
<dirfrag_t
,import_state_t
>::iterator it
= import_state
.find(df
);
2122 if (it
== import_state
.end()) {
2123 assert(0 == "got export_cancel in weird state");
2124 } else if (it
->second
.state
== IMPORT_DISCOVERING
) {
2125 import_reverse_discovering(df
);
2126 } else if (it
->second
.state
== IMPORT_DISCOVERED
) {
2127 CInode
*in
= cache
->get_inode(df
.ino
);
2129 import_reverse_discovered(df
, in
);
2130 } else if (it
->second
.state
== IMPORT_PREPPING
) {
2131 CDir
*dir
= mds
->mdcache
->get_dirfrag(df
);
2133 import_reverse_prepping(dir
);
2134 } else if (it
->second
.state
== IMPORT_PREPPED
) {
2135 CDir
*dir
= mds
->mdcache
->get_dirfrag(df
);
2138 cache
->get_subtree_bounds(dir
, bounds
);
2139 import_remove_pins(dir
, bounds
);
2140 // adjust auth back to the exportor
2141 cache
->adjust_subtree_auth(dir
, it
->second
.peer
);
2142 cache
->try_subtree_merge(dir
);
2143 import_reverse_unfreeze(dir
);
2145 assert(0 == "got export_cancel in weird state");
2150 /* This function DOES put the passed message before returning*/
2151 void Migrator::handle_export_prep(MExportDirPrep
*m
)
2153 mds_rank_t oldauth
= mds_rank_t(m
->get_source().num());
2154 assert(oldauth
!= mds
->get_nodeid());
2158 list
<MDSInternalContextBase
*> finished
;
2160 // assimilate root dir.
2161 map
<dirfrag_t
,import_state_t
>::iterator it
= import_state
.find(m
->get_dirfrag());
2162 if (!m
->did_assim()) {
2163 assert(it
!= import_state
.end());
2164 assert(it
->second
.state
== IMPORT_DISCOVERED
);
2165 assert(it
->second
.peer
== oldauth
);
2166 diri
= cache
->get_inode(m
->get_dirfrag().ino
);
2168 bufferlist::iterator p
= m
->basedir
.begin();
2169 dir
= cache
->add_replica_dir(p
, diri
, oldauth
, finished
);
2170 dout(7) << "handle_export_prep on " << *dir
<< " (first pass)" << dendl
;
2172 if (it
== import_state
.end() ||
2173 it
->second
.peer
!= oldauth
||
2174 it
->second
.tid
!= m
->get_tid()) {
2175 dout(7) << "handle_export_prep obsolete message, dropping" << dendl
;
2179 assert(it
->second
.state
== IMPORT_PREPPING
);
2180 assert(it
->second
.peer
== oldauth
);
2182 dir
= cache
->get_dirfrag(m
->get_dirfrag());
2184 dout(7) << "handle_export_prep on " << *dir
<< " (subsequent pass)" << dendl
;
2185 diri
= dir
->get_inode();
2187 assert(dir
->is_auth() == false);
2189 cache
->show_subtrees();
2191 // build import bound map
2192 map
<inodeno_t
, fragset_t
> import_bound_fragset
;
2193 for (list
<dirfrag_t
>::iterator p
= m
->get_bounds().begin();
2194 p
!= m
->get_bounds().end();
2196 dout(10) << " bound " << *p
<< dendl
;
2197 import_bound_fragset
[p
->ino
].insert(p
->frag
);
2200 // assimilate contents?
2201 if (!m
->did_assim()) {
2202 dout(7) << "doing assim on " << *dir
<< dendl
;
2203 m
->mark_assim(); // only do this the first time!
2205 // change import state
2206 it
->second
.state
= IMPORT_PREPPING
;
2207 it
->second
.bound_ls
= m
->get_bounds();
2208 it
->second
.bystanders
= m
->get_bystanders();
2209 assert(g_conf
->mds_kill_import_at
!= 3);
2212 dout(7) << "bystanders are " << it
->second
.bystanders
<< dendl
;
2215 diri
->put(CInode::PIN_IMPORTING
);
2216 dir
->get(CDir::PIN_IMPORTING
);
2217 dir
->state_set(CDir::STATE_IMPORTING
);
2219 // assimilate traces to exports
2220 // each trace is: df ('-' | ('f' dir | 'd') dentry inode (dir dentry inode)*)
2221 for (list
<bufferlist
>::iterator p
= m
->traces
.begin();
2222 p
!= m
->traces
.end();
2224 bufferlist::iterator q
= p
->begin();
2229 dout(10) << " trace from " << df
<< " start " << start
<< " len " << p
->length() << dendl
;
2233 cur
= cache
->get_dirfrag(df
);
2235 dout(10) << " had " << *cur
<< dendl
;
2236 } else if (start
== 'f') {
2237 CInode
*in
= cache
->get_inode(df
.ino
);
2239 dout(10) << " had " << *in
<< dendl
;
2240 cur
= cache
->add_replica_dir(q
, in
, oldauth
, finished
);
2241 dout(10) << " added " << *cur
<< dendl
;
2242 } else if (start
== '-') {
2245 assert(0 == "unrecognized start char");
2247 while (start
!= '-') {
2248 CDentry
*dn
= cache
->add_replica_dentry(q
, cur
, finished
);
2249 dout(10) << " added " << *dn
<< dendl
;
2250 CInode
*in
= cache
->add_replica_inode(q
, dn
, finished
);
2251 dout(10) << " added " << *in
<< dendl
;
2254 cur
= cache
->add_replica_dir(q
, in
, oldauth
, finished
);
2255 dout(10) << " added " << *cur
<< dendl
;
2259 // make bound sticky
2260 for (map
<inodeno_t
,fragset_t
>::iterator p
= import_bound_fragset
.begin();
2261 p
!= import_bound_fragset
.end();
2263 CInode
*in
= cache
->get_inode(p
->first
);
2265 in
->get_stickydirs();
2266 dout(7) << " set stickydirs on bound inode " << *in
<< dendl
;
2270 dout(7) << " not doing assim on " << *dir
<< dendl
;
2273 if (!finished
.empty())
2274 mds
->queue_waiters(finished
);
2278 set
<CDir
*> import_bounds
;
2279 for (map
<inodeno_t
,fragset_t
>::iterator p
= import_bound_fragset
.begin();
2280 p
!= import_bound_fragset
.end();
2282 CInode
*in
= cache
->get_inode(p
->first
);
2285 // map fragset into a frag_t list, based on the inode fragtree
2286 list
<frag_t
> fglist
;
2287 for (set
<frag_t
>::iterator q
= p
->second
.begin(); q
!= p
->second
.end(); ++q
)
2288 in
->dirfragtree
.get_leaves_under(*q
, fglist
);
2289 dout(10) << " bound inode " << p
->first
<< " fragset " << p
->second
<< " maps to " << fglist
<< dendl
;
2291 for (list
<frag_t
>::iterator q
= fglist
.begin();
2294 CDir
*bound
= cache
->get_dirfrag(dirfrag_t(p
->first
, *q
));
2296 dout(7) << " opening bounding dirfrag " << *q
<< " on " << *in
<< dendl
;
2297 cache
->open_remote_dirfrag(in
, *q
,
2298 new C_MDS_RetryMessage(mds
, m
));
2302 if (!bound
->state_test(CDir::STATE_IMPORTBOUND
)) {
2303 dout(7) << " pinning import bound " << *bound
<< dendl
;
2304 bound
->get(CDir::PIN_IMPORTBOUND
);
2305 bound
->state_set(CDir::STATE_IMPORTBOUND
);
2307 dout(7) << " already pinned import bound " << *bound
<< dendl
;
2309 import_bounds
.insert(bound
);
2313 dout(7) << " all ready, noting auth and freezing import region" << dendl
;
2315 bool success
= true;
2316 if (!mds
->mdcache
->is_readonly() &&
2317 dir
->get_inode()->filelock
.can_wrlock(-1) &&
2318 dir
->get_inode()->nestlock
.can_wrlock(-1)) {
2319 it
->second
.mut
= new MutationImpl();
2320 // force some locks. hacky.
2321 mds
->locker
->wrlock_force(&dir
->inode
->filelock
, it
->second
.mut
);
2322 mds
->locker
->wrlock_force(&dir
->inode
->nestlock
, it
->second
.mut
);
2324 // note that i am an ambiguous auth for this subtree.
2325 // specify bounds, since the exporter explicitly defines the region.
2326 cache
->adjust_bounded_subtree_auth(dir
, import_bounds
,
2327 pair
<int,int>(oldauth
, mds
->get_nodeid()));
2328 cache
->verify_subtree_bounds(dir
, import_bounds
);
2330 dir
->_freeze_tree();
2332 it
->second
.state
= IMPORT_PREPPED
;
2334 dout(7) << " couldn't acquire all needed locks, failing. " << *dir
<< dendl
;
2336 import_reverse_prepping(dir
);
2340 dout(7) << " sending export_prep_ack on " << *dir
<< dendl
;
2341 mds
->send_message(new MExportDirPrepAck(dir
->dirfrag(), success
, m
->get_tid()), m
->get_connection());
2343 assert(g_conf
->mds_kill_import_at
!= 4);
2351 class C_MDS_ImportDirLoggedStart
: public MigratorLogContext
{
2356 map
<client_t
,entity_inst_t
> imported_client_map
;
2357 map
<client_t
,uint64_t> sseqmap
;
2359 C_MDS_ImportDirLoggedStart(Migrator
*m
, CDir
*d
, mds_rank_t f
) :
2360 MigratorLogContext(m
), df(d
->dirfrag()), dir(d
), from(f
) {
2362 void finish(int r
) override
{
2363 mig
->import_logged_start(df
, dir
, from
, imported_client_map
, sseqmap
);
2367 /* This function DOES put the passed message before returning*/
2368 void Migrator::handle_export_dir(MExportDir
*m
)
2370 assert (g_conf
->mds_kill_import_at
!= 5);
2371 CDir
*dir
= cache
->get_dirfrag(m
->dirfrag
);
2374 mds_rank_t oldauth
= mds_rank_t(m
->get_source().num());
2375 dout(7) << "handle_export_dir importing " << *dir
<< " from " << oldauth
<< dendl
;
2377 assert(!dir
->is_auth());
2379 map
<dirfrag_t
,import_state_t
>::iterator it
= import_state
.find(m
->dirfrag
);
2380 assert(it
!= import_state
.end());
2381 assert(it
->second
.state
== IMPORT_PREPPED
);
2382 assert(it
->second
.tid
== m
->get_tid());
2383 assert(it
->second
.peer
== oldauth
);
2385 utime_t now
= ceph_clock_now();
2387 if (!dir
->get_inode()->dirfragtree
.is_leaf(dir
->get_frag()))
2388 dir
->get_inode()->dirfragtree
.force_to_leaf(g_ceph_context
, dir
->get_frag());
2390 cache
->show_subtrees();
2392 C_MDS_ImportDirLoggedStart
*onlogged
= new C_MDS_ImportDirLoggedStart(this, dir
, oldauth
);
2394 // start the journal entry
2395 EImportStart
*le
= new EImportStart(mds
->mdlog
, dir
->dirfrag(), m
->bounds
, oldauth
);
2396 mds
->mdlog
->start_entry(le
);
2398 le
->metablob
.add_dir_context(dir
);
2400 // adjust auth (list us _first_)
2401 cache
->adjust_subtree_auth(dir
, mds
->get_nodeid(), oldauth
);
2403 // new client sessions, open these after we journal
2404 // include imported sessions in EImportStart
2405 bufferlist::iterator cmp
= m
->client_map
.begin();
2406 ::decode(onlogged
->imported_client_map
, cmp
);
2408 le
->cmapv
= mds
->server
->prepare_force_open_sessions(onlogged
->imported_client_map
, onlogged
->sseqmap
);
2409 le
->client_map
.claim(m
->client_map
);
2411 bufferlist::iterator blp
= m
->export_data
.begin();
2412 int num_imported_inodes
= 0;
2413 while (!blp
.end()) {
2414 num_imported_inodes
+=
2415 decode_import_dir(blp
,
2419 mds
->mdlog
->get_current_segment(),
2420 it
->second
.peer_exports
,
2421 it
->second
.updated_scatterlocks
,
2424 dout(10) << " " << m
->bounds
.size() << " imported bounds" << dendl
;
2426 // include bounds in EImportStart
2427 set
<CDir
*> import_bounds
;
2428 for (vector
<dirfrag_t
>::iterator p
= m
->bounds
.begin();
2429 p
!= m
->bounds
.end();
2431 CDir
*bd
= cache
->get_dirfrag(*p
);
2433 le
->metablob
.add_dir(bd
, false); // note that parent metadata is already in the event
2434 import_bounds
.insert(bd
);
2436 cache
->verify_subtree_bounds(dir
, import_bounds
);
2438 // adjust popularity
2439 mds
->balancer
->add_import(dir
, now
);
2441 dout(7) << "handle_export_dir did " << *dir
<< dendl
;
2444 it
->second
.state
= IMPORT_LOGGINGSTART
;
2445 assert (g_conf
->mds_kill_import_at
!= 6);
2448 mds
->mdlog
->submit_entry(le
, onlogged
);
2449 mds
->mdlog
->flush();
2453 mds
->logger
->inc(l_mds_imported
);
2454 mds
->logger
->inc(l_mds_imported_inodes
, num_imported_inodes
);
2462 * this is an import helper
2463 * called by import_finish, and import_reverse and friends.
2465 void Migrator::import_remove_pins(CDir
*dir
, set
<CDir
*>& bounds
)
2467 import_state_t
& stat
= import_state
[dir
->dirfrag()];
2469 dir
->put(CDir::PIN_IMPORTING
);
2470 dir
->state_clear(CDir::STATE_IMPORTING
);
2474 for (list
<dirfrag_t
>::iterator p
= stat
.bound_ls
.begin();
2475 p
!= stat
.bound_ls
.end();
2477 if (did
.count(p
->ino
))
2480 CInode
*in
= cache
->get_inode(p
->ino
);
2482 in
->put_stickydirs();
2485 if (stat
.state
== IMPORT_PREPPING
) {
2486 for (auto bd
: bounds
) {
2487 if (bd
->state_test(CDir::STATE_IMPORTBOUND
)) {
2488 bd
->put(CDir::PIN_IMPORTBOUND
);
2489 bd
->state_clear(CDir::STATE_IMPORTBOUND
);
2492 } else if (stat
.state
>= IMPORT_PREPPED
) {
2493 // bounding dirfrags
2494 for (auto bd
: bounds
) {
2495 assert(bd
->state_test(CDir::STATE_IMPORTBOUND
));
2496 bd
->put(CDir::PIN_IMPORTBOUND
);
2497 bd
->state_clear(CDir::STATE_IMPORTBOUND
);
2504 * note: this does teh full work of reversing and import and cleaning up
2506 * called by both handle_mds_failure and by handle_resolve (if we are
2507 * a survivor coping with an exporter failure+recovery).
2509 void Migrator::import_reverse(CDir
*dir
)
2511 dout(7) << "import_reverse " << *dir
<< dendl
;
2513 import_state_t
& stat
= import_state
[dir
->dirfrag()];
2514 stat
.state
= IMPORT_ABORTING
;
2517 cache
->get_subtree_bounds(dir
, bounds
);
2520 import_remove_pins(dir
, bounds
);
2522 // update auth, with possible subtree merge.
2523 assert(dir
->is_subtree_root());
2524 if (mds
->is_resolve())
2525 cache
->trim_non_auth_subtree(dir
);
2527 cache
->adjust_subtree_auth(dir
, stat
.peer
);
2529 C_ContextsBase
<MDSInternalContextBase
, MDSInternalContextGather
> *fin
= new C_ContextsBase
<MDSInternalContextBase
, MDSInternalContextGather
>(g_ceph_context
);
2530 if (!dir
->get_inode()->is_auth() &&
2531 !dir
->get_inode()->has_subtree_root_dirfrag(mds
->get_nodeid())) {
2532 dir
->get_inode()->clear_scatter_dirty();
2533 // wake up scatter_nudge waiters
2534 dir
->get_inode()->take_waiting(CInode::WAIT_ANY_MASK
, fin
->contexts
);
2537 int num_dentries
= 0;
2538 // adjust auth bits.
2541 while (!q
.empty()) {
2542 CDir
*cur
= q
.front();
2546 assert(cur
->is_auth());
2547 cur
->state_clear(CDir::STATE_AUTH
);
2548 cur
->remove_bloom();
2549 cur
->clear_replica_map();
2550 cur
->set_replica_nonce(CDir::EXPORT_NONCE
);
2551 if (cur
->is_dirty())
2554 CDir::map_t::iterator it
;
2555 for (it
= cur
->begin(); it
!= cur
->end(); ++it
) {
2556 CDentry
*dn
= it
->second
;
2559 dn
->state_clear(CDentry::STATE_AUTH
);
2560 dn
->clear_replica_map();
2561 dn
->set_replica_nonce(CDentry::EXPORT_NONCE
);
2566 if (dn
->get_linkage()->is_primary()) {
2567 CInode
*in
= dn
->get_linkage()->get_inode();
2568 in
->state_clear(CDentry::STATE_AUTH
);
2569 in
->clear_replica_map();
2570 in
->set_replica_nonce(CInode::EXPORT_NONCE
);
2573 in
->clear_dirty_rstat();
2574 if (!in
->has_subtree_root_dirfrag(mds
->get_nodeid())) {
2575 in
->clear_scatter_dirty();
2576 in
->take_waiting(CInode::WAIT_ANY_MASK
, fin
->contexts
);
2579 in
->clear_dirty_parent();
2581 in
->authlock
.clear_gather();
2582 in
->linklock
.clear_gather();
2583 in
->dirfragtreelock
.clear_gather();
2584 in
->filelock
.clear_gather();
2586 in
->clear_file_locks();
2588 // non-bounding dir?
2590 in
->get_dirfrags(dfs
);
2591 for (list
<CDir
*>::iterator p
= dfs
.begin(); p
!= dfs
.end(); ++p
)
2592 if (bounds
.count(*p
) == 0)
2596 cache
->touch_dentry_bottom(dn
); // move dentry to tail of LRU
2601 dir
->add_waiter(CDir::WAIT_UNFREEZE
, fin
);
2603 if (stat
.state
== IMPORT_ACKING
) {
2604 // remove imported caps
2605 for (map
<CInode
*,map
<client_t
,Capability::Export
> >::iterator p
= stat
.peer_exports
.begin();
2606 p
!= stat
.peer_exports
.end();
2608 CInode
*in
= p
->first
;
2609 for (map
<client_t
,Capability::Export
>::iterator q
= p
->second
.begin();
2610 q
!= p
->second
.end();
2612 Capability
*cap
= in
->get_client_cap(q
->first
);
2614 if (cap
->is_importing())
2615 in
->remove_client_cap(q
->first
);
2617 in
->put(CInode::PIN_IMPORTINGCAPS
);
2619 for (map
<client_t
,entity_inst_t
>::iterator p
= stat
.client_map
.begin();
2620 p
!= stat
.client_map
.end();
2622 Session
*session
= mds
->sessionmap
.get_session(entity_name_t::CLIENT(p
->first
.v
));
2624 session
->dec_importing();
2629 mds
->mdlog
->start_submit_entry(new EImportFinish(dir
, false)); // log failure
2631 cache
->try_subtree_merge(dir
);
2633 cache
->trim(-1, num_dentries
); // try trimming dentries
2635 // notify bystanders; wait in aborting state
2636 import_notify_abort(dir
, bounds
);
2639 void Migrator::import_notify_finish(CDir
*dir
, set
<CDir
*>& bounds
)
2641 dout(7) << "import_notify_finish " << *dir
<< dendl
;
2643 import_state_t
& stat
= import_state
[dir
->dirfrag()];
2644 for (set
<mds_rank_t
>::iterator p
= stat
.bystanders
.begin();
2645 p
!= stat
.bystanders
.end();
2647 MExportDirNotify
*notify
=
2648 new MExportDirNotify(dir
->dirfrag(), stat
.tid
, false,
2649 pair
<int,int>(stat
.peer
, mds
->get_nodeid()),
2650 pair
<int,int>(mds
->get_nodeid(), CDIR_AUTH_UNKNOWN
));
2651 for (set
<CDir
*>::iterator i
= bounds
.begin(); i
!= bounds
.end(); ++i
)
2652 notify
->get_bounds().push_back((*i
)->dirfrag());
2653 mds
->send_message_mds(notify
, *p
);
2657 void Migrator::import_notify_abort(CDir
*dir
, set
<CDir
*>& bounds
)
2659 dout(7) << "import_notify_abort " << *dir
<< dendl
;
2661 import_state_t
& stat
= import_state
[dir
->dirfrag()];
2662 for (set
<mds_rank_t
>::iterator p
= stat
.bystanders
.begin();
2663 p
!= stat
.bystanders
.end(); ) {
2664 if (mds
->is_cluster_degraded() &&
2665 !mds
->mdsmap
->is_clientreplay_or_active_or_stopping(*p
)) {
2666 // this can happen if both exporter and bystander fail in the same mdsmap epoch
2667 stat
.bystanders
.erase(p
++);
2670 MExportDirNotify
*notify
=
2671 new MExportDirNotify(dir
->dirfrag(), stat
.tid
, true,
2672 mds_authority_t(stat
.peer
, mds
->get_nodeid()),
2673 mds_authority_t(stat
.peer
, CDIR_AUTH_UNKNOWN
));
2674 for (set
<CDir
*>::iterator i
= bounds
.begin(); i
!= bounds
.end(); ++i
)
2675 notify
->get_bounds().push_back((*i
)->dirfrag());
2676 mds
->send_message_mds(notify
, *p
);
2679 if (stat
.bystanders
.empty()) {
2680 dout(7) << "no bystanders, finishing reverse now" << dendl
;
2681 import_reverse_unfreeze(dir
);
2683 assert (g_conf
->mds_kill_import_at
!= 10);
2687 void Migrator::import_reverse_unfreeze(CDir
*dir
)
2690 dout(7) << "import_reverse_unfreeze " << *dir
<< dendl
;
2691 dir
->unfreeze_tree();
2692 cache
->discard_delayed_expire(dir
);
2693 import_reverse_final(dir
);
2696 void Migrator::import_reverse_final(CDir
*dir
)
2698 dout(7) << "import_reverse_final " << *dir
<< dendl
;
2701 map
<dirfrag_t
, import_state_t
>::iterator it
= import_state
.find(dir
->dirfrag());
2702 assert(it
!= import_state
.end());
2704 MutationRef mut
= it
->second
.mut
;
2705 import_state
.erase(it
);
2707 // send pending import_maps?
2708 mds
->mdcache
->maybe_send_pending_resolves();
2711 mds
->locker
->drop_locks(mut
.get());
2715 cache
->show_subtrees();
2716 //audit(); // this fails, bc we munge up the subtree map during handle_import_map (resolve phase)
2722 void Migrator::import_logged_start(dirfrag_t df
, CDir
*dir
, mds_rank_t from
,
2723 map
<client_t
,entity_inst_t
>& imported_client_map
,
2724 map
<client_t
,uint64_t>& sseqmap
)
2726 map
<dirfrag_t
, import_state_t
>::iterator it
= import_state
.find(dir
->dirfrag());
2727 if (it
== import_state
.end() ||
2728 it
->second
.state
!= IMPORT_LOGGINGSTART
) {
2729 dout(7) << "import " << df
<< " must have aborted" << dendl
;
2730 mds
->server
->finish_force_open_sessions(imported_client_map
, sseqmap
);
2734 dout(7) << "import_logged " << *dir
<< dendl
;
2737 it
->second
.state
= IMPORT_ACKING
;
2739 assert (g_conf
->mds_kill_import_at
!= 7);
2741 // force open client sessions and finish cap import
2742 mds
->server
->finish_force_open_sessions(imported_client_map
, sseqmap
, false);
2743 it
->second
.client_map
.swap(imported_client_map
);
2745 map
<inodeno_t
,map
<client_t
,Capability::Import
> > imported_caps
;
2746 for (map
<CInode
*, map
<client_t
,Capability::Export
> >::iterator p
= it
->second
.peer_exports
.begin();
2747 p
!= it
->second
.peer_exports
.end();
2749 // parameter 'peer' is NONE, delay sending cap import messages to client
2750 finish_import_inode_caps(p
->first
, MDS_RANK_NONE
, true, p
->second
, imported_caps
[p
->first
->ino()]);
2753 // send notify's etc.
2754 dout(7) << "sending ack for " << *dir
<< " to old auth mds." << from
<< dendl
;
2756 // test surviving observer of a failed migration that did not complete
2757 //assert(dir->replica_map.size() < 2 || mds->get_nodeid() != 0);
2759 MExportDirAck
*ack
= new MExportDirAck(dir
->dirfrag(), it
->second
.tid
);
2760 ::encode(imported_caps
, ack
->imported_caps
);
2762 mds
->send_message_mds(ack
, from
);
2763 assert (g_conf
->mds_kill_import_at
!= 8);
2765 cache
->show_subtrees();
2768 /* This function DOES put the passed message before returning*/
2769 void Migrator::handle_export_finish(MExportDirFinish
*m
)
2771 CDir
*dir
= cache
->get_dirfrag(m
->get_dirfrag());
2773 dout(7) << "handle_export_finish on " << *dir
<< (m
->is_last() ? " last" : "") << dendl
;
2775 map
<dirfrag_t
,import_state_t
>::iterator it
= import_state
.find(m
->get_dirfrag());
2776 assert(it
!= import_state
.end());
2777 assert(it
->second
.tid
== m
->get_tid());
2779 import_finish(dir
, false, m
->is_last());
2784 void Migrator::import_finish(CDir
*dir
, bool notify
, bool last
)
2786 dout(7) << "import_finish on " << *dir
<< dendl
;
2788 map
<dirfrag_t
,import_state_t
>::iterator it
= import_state
.find(dir
->dirfrag());
2789 assert(it
!= import_state
.end());
2790 assert(it
->second
.state
== IMPORT_ACKING
|| it
->second
.state
== IMPORT_FINISHING
);
2793 assert(g_conf
->mds_kill_import_at
!= 9);
2795 if (it
->second
.state
== IMPORT_ACKING
) {
2796 for (map
<CInode
*, map
<client_t
,Capability::Export
> >::iterator p
= it
->second
.peer_exports
.begin();
2797 p
!= it
->second
.peer_exports
.end();
2799 CInode
*in
= p
->first
;
2800 assert(in
->is_auth());
2801 for (map
<client_t
,Capability::Export
>::iterator q
= p
->second
.begin();
2802 q
!= p
->second
.end();
2804 Session
*session
= mds
->sessionmap
.get_session(entity_name_t::CLIENT(q
->first
.v
));
2806 Capability
*cap
= in
->get_client_cap(q
->first
);
2808 cap
->merge(q
->second
, true);
2809 cap
->clear_importing();
2810 mds
->mdcache
->do_cap_import(session
, in
, cap
, q
->second
.cap_id
, q
->second
.seq
,
2811 q
->second
.mseq
- 1, it
->second
.peer
, CEPH_CAP_FLAG_AUTH
);
2814 in
->replica_caps_wanted
= 0;
2816 for (map
<client_t
,entity_inst_t
>::iterator p
= it
->second
.client_map
.begin();
2817 p
!= it
->second
.client_map
.end();
2819 Session
*session
= mds
->sessionmap
.get_session(entity_name_t::CLIENT(p
->first
.v
));
2821 session
->dec_importing();
2826 assert(it
->second
.state
== IMPORT_ACKING
);
2827 it
->second
.state
= IMPORT_FINISHING
;
2833 cache
->get_subtree_bounds(dir
, bounds
);
2836 import_notify_finish(dir
, bounds
);
2838 import_remove_pins(dir
, bounds
);
2840 map
<CInode
*, map
<client_t
,Capability::Export
> > peer_exports
;
2841 it
->second
.peer_exports
.swap(peer_exports
);
2843 // clear import state (we're done!)
2844 MutationRef mut
= it
->second
.mut
;
2845 import_state
.erase(it
);
2847 // adjust auth, with possible subtree merge.
2848 cache
->adjust_subtree_auth(dir
, mds
->get_nodeid());
2850 mds
->mdlog
->start_submit_entry(new EImportFinish(dir
, true));
2852 cache
->try_subtree_merge(dir
);
2854 // process delayed expires
2855 cache
->process_delayed_expire(dir
);
2857 // ok now unfreeze (and thus kick waiters)
2858 dir
->unfreeze_tree();
2859 cache
->show_subtrees();
2860 //audit(); // this fails, bc we munge up the subtree map during handle_import_map (resolve phase)
2863 mds
->locker
->drop_locks(mut
.get());
2867 // re-eval imported caps
2868 for (map
<CInode
*, map
<client_t
,Capability::Export
> >::iterator p
= peer_exports
.begin();
2869 p
!= peer_exports
.end();
2871 if (p
->first
->is_auth())
2872 mds
->locker
->eval(p
->first
, CEPH_CAP_LOCKS
, true);
2873 p
->first
->put(CInode::PIN_IMPORTINGCAPS
);
2876 // send pending import_maps?
2877 mds
->mdcache
->maybe_send_pending_resolves();
2879 // did i just import mydir?
2880 if (dir
->ino() == MDS_INO_MDSDIR(mds
->get_nodeid()))
2881 cache
->populate_mydir();
2884 if (dir
->get_num_head_items() == 0 &&
2885 !dir
->inode
->is_auth()) {
2887 export_empty_import(dir
);
2892 void Migrator::decode_import_inode(CDentry
*dn
, bufferlist::iterator
& blp
,
2893 mds_rank_t oldauth
, LogSegment
*ls
,
2894 map
<CInode
*, map
<client_t
,Capability::Export
> >& peer_exports
,
2895 list
<ScatterLock
*>& updated_scatterlocks
)
2897 dout(15) << "decode_import_inode on " << *dn
<< dendl
;
2902 ::decode(last
, blp
);
2905 CInode
*in
= cache
->get_inode(ino
, last
);
2907 in
= new CInode(mds
->mdcache
, true, 1, last
);
2911 // state after link -- or not! -sage
2912 in
->decode_import(blp
, ls
); // cap imports are noted for later action
2915 decode_import_inode_caps(in
, true, blp
, peer_exports
);
2917 // link before state -- or not! -sage
2918 if (dn
->get_linkage()->get_inode() != in
) {
2919 assert(!dn
->get_linkage()->get_inode());
2920 dn
->dir
->link_primary_inode(dn
, in
);
2925 cache
->add_inode(in
);
2926 dout(10) << "added " << *in
<< dendl
;
2928 dout(10) << " had " << *in
<< dendl
;
2931 if (in
->inode
.is_dirty_rstat())
2932 in
->mark_dirty_rstat();
2934 // clear if dirtyscattered, since we're going to journal this
2935 // but not until we _actually_ finish the import...
2936 if (in
->filelock
.is_dirty()) {
2937 updated_scatterlocks
.push_back(&in
->filelock
);
2938 mds
->locker
->mark_updated_scatterlock(&in
->filelock
);
2941 if (in
->dirfragtreelock
.is_dirty()) {
2942 updated_scatterlocks
.push_back(&in
->dirfragtreelock
);
2943 mds
->locker
->mark_updated_scatterlock(&in
->dirfragtreelock
);
2946 // adjust replica list
2947 //assert(!in->is_replica(oldauth)); // not true on failed export
2948 in
->add_replica(oldauth
, CInode::EXPORT_NONCE
);
2949 if (in
->is_replica(mds
->get_nodeid()))
2950 in
->remove_replica(mds
->get_nodeid());
2953 void Migrator::decode_import_inode_caps(CInode
*in
, bool auth_cap
,
2954 bufferlist::iterator
&blp
,
2955 map
<CInode
*, map
<client_t
,Capability::Export
> >& peer_exports
)
2957 map
<client_t
,Capability::Export
> cap_map
;
2958 ::decode(cap_map
, blp
);
2960 ::decode(in
->get_mds_caps_wanted(), blp
);
2961 if (!cap_map
.empty() ||
2962 (auth_cap
&& !in
->get_mds_caps_wanted().empty())) {
2963 peer_exports
[in
].swap(cap_map
);
2964 in
->get(CInode::PIN_IMPORTINGCAPS
);
2968 void Migrator::finish_import_inode_caps(CInode
*in
, mds_rank_t peer
, bool auth_cap
,
2969 map
<client_t
,Capability::Export
> &export_map
,
2970 map
<client_t
,Capability::Import
> &import_map
)
2972 for (map
<client_t
,Capability::Export
>::iterator it
= export_map
.begin();
2973 it
!= export_map
.end();
2975 dout(10) << "finish_import_inode_caps for client." << it
->first
<< " on " << *in
<< dendl
;
2976 Session
*session
= mds
->sessionmap
.get_session(entity_name_t::CLIENT(it
->first
.v
));
2979 Capability
*cap
= in
->get_client_cap(it
->first
);
2981 cap
= in
->add_client_cap(it
->first
, session
);
2983 cap
->mark_importing();
2986 Capability::Import
& im
= import_map
[it
->first
];
2987 im
.cap_id
= cap
->get_cap_id();
2988 im
.mseq
= auth_cap
? it
->second
.mseq
: cap
->get_mseq();
2989 im
.issue_seq
= cap
->get_last_seq() + 1;
2992 cap
->merge(it
->second
, auth_cap
);
2993 mds
->mdcache
->do_cap_import(session
, in
, cap
, it
->second
.cap_id
,
2994 it
->second
.seq
, it
->second
.mseq
- 1, peer
,
2995 auth_cap
? CEPH_CAP_FLAG_AUTH
: CEPH_CAP_FLAG_RELEASE
);
3000 in
->replica_caps_wanted
= 0;
3001 in
->put(CInode::PIN_IMPORTINGCAPS
);
3005 int Migrator::decode_import_dir(bufferlist::iterator
& blp
,
3010 map
<CInode
*,map
<client_t
,Capability::Export
> >& peer_exports
,
3011 list
<ScatterLock
*>& updated_scatterlocks
, utime_t now
)
3017 CInode
*diri
= cache
->get_inode(df
.ino
);
3019 CDir
*dir
= diri
->get_or_open_dirfrag(mds
->mdcache
, df
.frag
);
3022 dout(7) << "decode_import_dir " << *dir
<< dendl
;
3025 dir
->decode_import(blp
, now
, ls
);
3027 // adjust replica list
3028 //assert(!dir->is_replica(oldauth)); // not true on failed export
3029 dir
->add_replica(oldauth
, CDir::EXPORT_NONCE
);
3030 if (dir
->is_replica(mds
->get_nodeid()))
3031 dir
->remove_replica(mds
->get_nodeid());
3033 // add to journal entry
3035 le
->metablob
.add_import_dir(dir
);
3037 int num_imported
= 0;
3039 // take all waiters on this dir
3040 // NOTE: a pass of imported data is guaranteed to get all of my waiters because
3041 // a replica's presense in my cache implies/forces it's presense in authority's.
3042 list
<MDSInternalContextBase
*> waiters
;
3044 dir
->take_waiting(CDir::WAIT_ANY_MASK
, waiters
);
3045 for (list
<MDSInternalContextBase
*>::iterator it
= waiters
.begin();
3046 it
!= waiters
.end();
3048 import_root
->add_waiter(CDir::WAIT_UNFREEZE
, *it
); // UNFREEZE will get kicked both on success or failure
3050 dout(15) << "doing contents" << dendl
;
3054 ::decode(nden
, blp
);
3056 for (; nden
>0; nden
--) {
3062 ::decode(dname
, blp
);
3063 ::decode(last
, blp
);
3065 CDentry
*dn
= dir
->lookup_exact_snap(dname
, last
);
3067 dn
= dir
->add_null_dentry(dname
, 1, last
);
3069 dn
->decode_import(blp
, ls
);
3071 dn
->add_replica(oldauth
, CDentry::EXPORT_NONCE
);
3072 if (dn
->is_replica(mds
->get_nodeid()))
3073 dn
->remove_replica(mds
->get_nodeid());
3075 // dentry lock in unreadable state can block path traverse
3076 if (dn
->lock
.get_state() != LOCK_SYNC
)
3077 mds
->locker
->try_eval(&dn
->lock
, NULL
);
3079 dout(15) << "decode_import_dir got " << *dn
<< dendl
;
3083 ::decode(icode
, blp
);
3087 assert(dn
->get_linkage()->is_null());
3091 else if (icode
== 'L') {
3094 unsigned char d_type
;
3096 ::decode(d_type
, blp
);
3097 if (dn
->get_linkage()->is_remote()) {
3098 assert(dn
->get_linkage()->get_remote_ino() == ino
);
3100 dir
->link_remote_inode(dn
, ino
, d_type
);
3103 else if (icode
== 'I') {
3106 decode_import_inode(dn
, blp
, oldauth
, ls
,
3107 peer_exports
, updated_scatterlocks
);
3110 // add dentry to journal entry
3112 le
->metablob
.add_import_dentry(dn
);
3115 #ifdef MDS_VERIFY_FRAGSTAT
3116 if (dir
->is_complete())
3117 dir
->verify_fragstat();
3120 dir
->inode
->maybe_export_pin();
3122 dout(7) << "decode_import_dir done " << *dir
<< dendl
;
3123 return num_imported
;
3130 // authority bystander
3132 /* This function DOES put the passed message before returning*/
3133 void Migrator::handle_export_notify(MExportDirNotify
*m
)
3135 if (!(mds
->is_clientreplay() || mds
->is_active() || mds
->is_stopping())) {
3140 CDir
*dir
= cache
->get_dirfrag(m
->get_dirfrag());
3142 mds_rank_t from
= mds_rank_t(m
->get_source().num());
3143 mds_authority_t old_auth
= m
->get_old_auth();
3144 mds_authority_t new_auth
= m
->get_new_auth();
3147 dout(7) << "handle_export_notify " << old_auth
<< " -> " << new_auth
3148 << " on missing dir " << m
->get_dirfrag() << dendl
;
3149 } else if (dir
->authority() != old_auth
) {
3150 dout(7) << "handle_export_notify old_auth was " << dir
->authority()
3151 << " != " << old_auth
<< " -> " << new_auth
3152 << " on " << *dir
<< dendl
;
3154 dout(7) << "handle_export_notify " << old_auth
<< " -> " << new_auth
3155 << " on " << *dir
<< dendl
;
3158 cache
->map_dirfrag_set(m
->get_bounds(), have
);
3159 cache
->adjust_bounded_subtree_auth(dir
, have
, new_auth
);
3162 cache
->try_subtree_merge(dir
);
3166 if (m
->wants_ack()) {
3167 mds
->send_message_mds(new MExportDirNotifyAck(m
->get_dirfrag(), m
->get_tid(), m
->get_new_auth()), from
);
3170 dout(7) << "handle_export_notify no ack requested" << dendl
;
3177 void Migrator::export_caps(CInode
*in
)
3179 mds_rank_t dest
= in
->authority().first
;
3180 dout(7) << "export_caps to mds." << dest
<< " " << *in
<< dendl
;
3182 assert(in
->is_any_caps());
3183 assert(!in
->is_auth());
3184 assert(!in
->is_ambiguous_auth());
3185 assert(!in
->state_test(CInode::STATE_EXPORTINGCAPS
));
3187 MExportCaps
*ex
= new MExportCaps
;
3188 ex
->ino
= in
->ino();
3190 encode_export_inode_caps(in
, false, ex
->cap_bl
, ex
->client_map
);
3192 mds
->send_message_mds(ex
, dest
);
3195 void Migrator::handle_gather_caps(MGatherCaps
*m
)
3197 CInode
*in
= cache
->get_inode(m
->ino
);
3202 dout(10) << "handle_gather_caps " << *m
<< " from " << m
->get_source()
3205 if (in
->is_any_caps() &&
3207 !in
->is_ambiguous_auth() &&
3208 !in
->state_test(CInode::STATE_EXPORTINGCAPS
))
3215 class C_M_LoggedImportCaps
: public MigratorLogContext
{
3219 map
<CInode
*, map
<client_t
,Capability::Export
> > peer_exports
;
3220 map
<client_t
,entity_inst_t
> client_map
;
3221 map
<client_t
,uint64_t> sseqmap
;
3223 C_M_LoggedImportCaps(Migrator
*m
, CInode
*i
, mds_rank_t f
) : MigratorLogContext(m
), in(i
), from(f
) {}
3224 void finish(int r
) override
{
3225 mig
->logged_import_caps(in
, from
, peer_exports
, client_map
, sseqmap
);
3229 /* This function DOES put the passed message before returning*/
3230 void Migrator::handle_export_caps(MExportCaps
*ex
)
3232 dout(10) << "handle_export_caps " << *ex
<< " from " << ex
->get_source() << dendl
;
3233 CInode
*in
= cache
->get_inode(ex
->ino
);
3236 assert(in
->is_auth());
3239 if (in
->is_frozen())
3242 C_M_LoggedImportCaps
*finish
= new C_M_LoggedImportCaps(
3243 this, in
, mds_rank_t(ex
->get_source().num()));
3244 finish
->client_map
= ex
->client_map
;
3247 bufferlist::iterator blp
= ex
->cap_bl
.begin();
3248 decode_import_inode_caps(in
, false, blp
, finish
->peer_exports
);
3249 assert(!finish
->peer_exports
.empty()); // thus, inode is pinned.
3251 // journal open client sessions
3252 version_t pv
= mds
->server
->prepare_force_open_sessions(finish
->client_map
, finish
->sseqmap
);
3254 ESessions
*le
= new ESessions(pv
, ex
->client_map
);
3255 mds
->mdlog
->start_submit_entry(le
, finish
);
3256 mds
->mdlog
->flush();
3262 void Migrator::logged_import_caps(CInode
*in
,
3264 map
<CInode
*, map
<client_t
,Capability::Export
> >& peer_exports
,
3265 map
<client_t
,entity_inst_t
>& client_map
,
3266 map
<client_t
,uint64_t>& sseqmap
)
3268 dout(10) << "logged_import_caps on " << *in
<< dendl
;
3269 // see export_go() vs export_go_synced()
3270 assert(in
->is_auth());
3272 // force open client sessions and finish cap import
3273 mds
->server
->finish_force_open_sessions(client_map
, sseqmap
);
3275 map
<client_t
,Capability::Import
> imported_caps
;
3277 assert(peer_exports
.count(in
));
3278 // clients will release caps from the exporter when they receive the cap import message.
3279 finish_import_inode_caps(in
, from
, false, peer_exports
[in
], imported_caps
);
3280 mds
->locker
->eval(in
, CEPH_CAP_LOCKS
, true);