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
291 if (dir
->is_subtree_root())
292 cache
->try_subtree_merge(dir
);
294 (!mds
->is_cluster_degraded() ||
295 mds
->mdsmap
->is_clientreplay_or_active_or_stopping(it
->second
.peer
))) // tell them.
296 mds
->send_message_mds(new MExportDirCancel(dir
->dirfrag(), it
->second
.tid
), it
->second
.peer
);
299 // NOTE: state order reversal, warning comes after prepping
301 dout(10) << "export state=warning : unpinning bounds, unfreezing, notifying" << dendl
;
302 it
->second
.state
= EXPORT_CANCELLING
;
305 case EXPORT_PREPPING
:
306 if (state
!= EXPORT_WARNING
) {
307 dout(10) << "export state=prepping : unpinning bounds, unfreezing" << dendl
;
308 it
->second
.state
= EXPORT_CANCELLED
;
314 cache
->get_subtree_bounds(dir
, bounds
);
315 for (set
<CDir
*>::iterator q
= bounds
.begin();
319 bd
->put(CDir::PIN_EXPORTBOUND
);
320 bd
->state_clear(CDir::STATE_EXPORTBOUND
);
322 if (state
== EXPORT_WARNING
) {
324 export_notify_abort(dir
, bounds
);
325 // process delayed expires
326 cache
->process_delayed_expire(dir
);
329 dir
->unfreeze_tree();
330 cache
->try_subtree_merge(dir
);
332 (!mds
->is_cluster_degraded() ||
333 mds
->mdsmap
->is_clientreplay_or_active_or_stopping(it
->second
.peer
))) // tell them.
334 mds
->send_message_mds(new MExportDirCancel(dir
->dirfrag(), it
->second
.tid
), it
->second
.peer
);
337 case EXPORT_EXPORTING
:
338 dout(10) << "export state=exporting : reversing, and unfreezing" << dendl
;
339 it
->second
.state
= EXPORT_CANCELLING
;
343 case EXPORT_LOGGINGFINISH
:
344 case EXPORT_NOTIFYING
:
345 dout(10) << "export state=loggingfinish|notifying : ignoring dest failure, we were successful." << dendl
;
346 // leave export_state, don't clean up now.
348 case EXPORT_CANCELLING
:
356 if (it
->second
.state
== EXPORT_CANCELLING
||
357 it
->second
.state
== EXPORT_CANCELLED
) {
359 mut
.swap(it
->second
.mut
);
361 if (it
->second
.state
== EXPORT_CANCELLED
) {
362 export_state
.erase(it
);
363 dir
->state_clear(CDir::STATE_EXPORTING
);
364 // send pending import_maps?
365 cache
->maybe_send_pending_resolves();
369 if (state
== EXPORT_LOCKING
|| state
== EXPORT_DISCOVERING
) {
370 MDRequestRef mdr
= static_cast<MDRequestImpl
*>(mut
.get());
372 if (mdr
->more()->waiting_on_slave
.empty())
373 mds
->mdcache
->request_finish(mdr
);
375 mds
->locker
->drop_locks(mut
.get());
379 cache
->show_subtrees();
381 maybe_do_queued_export();
385 void Migrator::export_cancel_finish(CDir
*dir
)
387 assert(dir
->state_test(CDir::STATE_EXPORTING
));
388 dir
->state_clear(CDir::STATE_EXPORTING
);
390 // pinned by Migrator::export_notify_abort()
391 dir
->auth_unpin(this);
392 // send pending import_maps? (these need to go out when all exports have finished.)
393 cache
->maybe_send_pending_resolves();
396 // ==========================================================
397 // mds failure handling
399 void Migrator::handle_mds_failure_or_stop(mds_rank_t who
)
401 dout(5) << "handle_mds_failure_or_stop mds." << who
<< dendl
;
405 // first add an extra auth_pin on any freezes, so that canceling a
406 // nested freeze doesn't complete one further up the hierarchy and
407 // confuse the shit out of us. we'll remove it after canceling the
408 // freeze. this way no freeze completions run before we want them
410 list
<CDir
*> pinned_dirs
;
411 for (map
<CDir
*,export_state_t
>::iterator p
= export_state
.begin();
412 p
!= export_state
.end();
414 if (p
->second
.state
== EXPORT_FREEZING
) {
415 CDir
*dir
= p
->first
;
416 dout(10) << "adding temp auth_pin on freezing " << *dir
<< dendl
;
418 pinned_dirs
.push_back(dir
);
422 map
<CDir
*,export_state_t
>::iterator p
= export_state
.begin();
423 while (p
!= export_state
.end()) {
424 map
<CDir
*,export_state_t
>::iterator next
= p
;
426 CDir
*dir
= p
->first
;
429 // - that are going to the failed node
430 // - that aren't frozen yet (to avoid auth_pin deadlock)
431 // - they havne't prepped yet (they may need to discover bounds to do that)
432 if ((p
->second
.peer
== who
&&
433 p
->second
.state
!= EXPORT_CANCELLING
) ||
434 p
->second
.state
== EXPORT_LOCKING
||
435 p
->second
.state
== EXPORT_DISCOVERING
||
436 p
->second
.state
== EXPORT_FREEZING
||
437 p
->second
.state
== EXPORT_PREPPING
) {
438 // the guy i'm exporting to failed, or we're just freezing.
439 dout(10) << "cleaning up export state (" << p
->second
.state
<< ")"
440 << get_export_statename(p
->second
.state
) << " of " << *dir
<< dendl
;
441 export_try_cancel(dir
);
442 } else if (p
->second
.peer
!= who
) {
444 if (p
->second
.warning_ack_waiting
.erase(who
)) {
445 if (p
->second
.state
== EXPORT_WARNING
) {
446 p
->second
.notify_ack_waiting
.erase(who
); // they won't get a notify either.
447 // exporter waiting for warning acks, let's fake theirs.
448 dout(10) << "faking export_warning_ack from mds." << who
449 << " on " << *dir
<< " to mds." << p
->second
.peer
451 if (p
->second
.warning_ack_waiting
.empty())
455 if (p
->second
.notify_ack_waiting
.erase(who
)) {
456 // exporter is waiting for notify acks, fake it
457 dout(10) << "faking export_notify_ack from mds." << who
458 << " on " << *dir
<< " to mds." << p
->second
.peer
460 if (p
->second
.state
== EXPORT_NOTIFYING
) {
461 if (p
->second
.notify_ack_waiting
.empty())
463 } else if (p
->second
.state
== EXPORT_CANCELLING
) {
464 if (p
->second
.notify_ack_waiting
.empty()) {
465 export_state
.erase(p
);
466 export_cancel_finish(dir
);
478 map
<dirfrag_t
,import_state_t
>::iterator q
= import_state
.begin();
479 while (q
!= import_state
.end()) {
480 map
<dirfrag_t
,import_state_t
>::iterator next
= q
;
482 dirfrag_t df
= q
->first
;
483 CInode
*diri
= mds
->mdcache
->get_inode(df
.ino
);
484 CDir
*dir
= mds
->mdcache
->get_dirfrag(df
);
486 if (q
->second
.peer
== who
) {
488 dout(10) << "cleaning up import state (" << q
->second
.state
<< ")"
489 << get_import_statename(q
->second
.state
) << " of " << *dir
<< dendl
;
491 dout(10) << "cleaning up import state (" << q
->second
.state
<< ")"
492 << get_import_statename(q
->second
.state
) << " of " << df
<< dendl
;
494 switch (q
->second
.state
) {
495 case IMPORT_DISCOVERING
:
496 dout(10) << "import state=discovering : clearing state" << dendl
;
497 import_reverse_discovering(df
);
500 case IMPORT_DISCOVERED
:
502 dout(10) << "import state=discovered : unpinning inode " << *diri
<< dendl
;
503 import_reverse_discovered(df
, diri
);
506 case IMPORT_PREPPING
:
508 dout(10) << "import state=prepping : unpinning base+bounds " << *dir
<< dendl
;
509 import_reverse_prepping(dir
);
514 dout(10) << "import state=prepped : unpinning base+bounds, unfreezing " << *dir
<< dendl
;
517 cache
->get_subtree_bounds(dir
, bounds
);
518 import_remove_pins(dir
, bounds
);
520 // adjust auth back to the exporter
521 cache
->adjust_subtree_auth(dir
, q
->second
.peer
);
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
);
577 while (!pinned_dirs
.empty()) {
578 CDir
*dir
= pinned_dirs
.front();
579 dout(10) << "removing temp auth_pin on " << *dir
<< dendl
;
580 dir
->auth_unpin(this);
581 pinned_dirs
.pop_front();
587 void Migrator::show_importing()
589 dout(10) << "show_importing" << dendl
;
590 for (map
<dirfrag_t
,import_state_t
>::iterator p
= import_state
.begin();
591 p
!= import_state
.end();
593 CDir
*dir
= mds
->mdcache
->get_dirfrag(p
->first
);
595 dout(10) << " importing from " << p
->second
.peer
596 << ": (" << p
->second
.state
<< ") " << get_import_statename(p
->second
.state
)
597 << " " << p
->first
<< " " << *dir
<< dendl
;
599 dout(10) << " importing from " << p
->second
.peer
600 << ": (" << p
->second
.state
<< ") " << get_import_statename(p
->second
.state
)
601 << " " << p
->first
<< dendl
;
606 void Migrator::show_exporting()
608 dout(10) << "show_exporting" << dendl
;
609 for (map
<CDir
*,export_state_t
>::iterator p
= export_state
.begin();
610 p
!= export_state
.end();
612 dout(10) << " exporting to " << p
->second
.peer
613 << ": (" << p
->second
.state
<< ") " << get_export_statename(p
->second
.state
)
614 << " " << p
->first
->dirfrag() << " " << *p
->first
<< dendl
;
619 void Migrator::audit()
621 if (!g_conf
->subsys
.should_gather(ceph_subsys_mds
, 5))
626 for (map
<dirfrag_t
,import_state_t
>::iterator p
= import_state
.begin();
627 p
!= import_state
.end();
629 if (p
->second
.state
== IMPORT_DISCOVERING
)
631 if (p
->second
.state
== IMPORT_DISCOVERED
) {
632 CInode
*in
= cache
->get_inode(p
->first
.ino
);
636 CDir
*dir
= cache
->get_dirfrag(p
->first
);
638 if (p
->second
.state
== IMPORT_PREPPING
)
640 if (p
->second
.state
== IMPORT_ABORTING
) {
641 assert(!dir
->is_ambiguous_dir_auth());
642 assert(dir
->get_dir_auth().first
!= mds
->get_nodeid());
645 assert(dir
->is_ambiguous_dir_auth());
646 assert(dir
->authority().first
== mds
->get_nodeid() ||
647 dir
->authority().second
== mds
->get_nodeid());
652 for (map
<CDir
*,export_state_t
>::iterator p
= export_state
.begin();
653 p
!= export_state
.end();
655 CDir
*dir
= p
->first
;
656 if (p
->second
.state
== EXPORT_LOCKING
||
657 p
->second
.state
== EXPORT_DISCOVERING
||
658 p
->second
.state
== EXPORT_FREEZING
||
659 p
->second
.state
== EXPORT_CANCELLING
)
661 assert(dir
->is_ambiguous_dir_auth());
662 assert(dir
->authority().first
== mds
->get_nodeid() ||
663 dir
->authority().second
== mds
->get_nodeid());
666 // ambiguous+me subtrees should be importing|exporting
675 // ==========================================================
678 void Migrator::export_dir_nicely(CDir
*dir
, mds_rank_t dest
)
681 dout(7) << "export_dir_nicely " << *dir
<< " to " << dest
<< dendl
;
682 export_queue
.push_back(pair
<dirfrag_t
,mds_rank_t
>(dir
->dirfrag(), dest
));
684 maybe_do_queued_export();
687 void Migrator::maybe_do_queued_export()
693 while (!export_queue
.empty() &&
694 export_state
.size() <= 4) {
695 dirfrag_t df
= export_queue
.front().first
;
696 mds_rank_t dest
= export_queue
.front().second
;
697 export_queue
.pop_front();
699 CDir
*dir
= mds
->mdcache
->get_dirfrag(df
);
701 if (!dir
->is_auth()) continue;
703 dout(0) << "nicely exporting to mds." << dest
<< " " << *dir
<< dendl
;
705 export_dir(dir
, dest
);
713 class C_MDC_ExportFreeze
: public MigratorContext
{
714 CDir
*ex
; // dir i'm exporting
717 C_MDC_ExportFreeze(Migrator
*m
, CDir
*e
, uint64_t t
) :
718 MigratorContext(m
), ex(e
), tid(t
) {
721 void finish(int r
) override
{
723 mig
->export_frozen(ex
, tid
);
728 void Migrator::get_export_lock_set(CDir
*dir
, set
<SimpleLock
*>& locks
)
731 vector
<CDentry
*> trace
;
732 cache
->make_trace(trace
, dir
->inode
);
733 for (vector
<CDentry
*>::iterator it
= trace
.begin();
736 locks
.insert(&(*it
)->lock
);
738 // prevent scatter gather race
739 locks
.insert(&dir
->get_inode()->dirfragtreelock
);
742 // NOTE: We need to take an rdlock on bounding dirfrags during
743 // migration for a rather irritating reason: when we export the
744 // bound inode, we need to send scatterlock state for the dirfrags
745 // as well, so that the new auth also gets the correct info. If we
746 // race with a refragment, this info is useless, as we can't
747 // redivvy it up. And it's needed for the scatterlocks to work
748 // properly: when the auth is in a sync/lock state it keeps each
749 // dirfrag's portion in the local (auth OR replica) dirfrag.
750 set
<CDir
*> wouldbe_bounds
;
751 cache
->get_wouldbe_subtree_bounds(dir
, wouldbe_bounds
);
752 for (set
<CDir
*>::iterator p
= wouldbe_bounds
.begin(); p
!= wouldbe_bounds
.end(); ++p
)
753 locks
.insert(&(*p
)->get_inode()->dirfragtreelock
);
757 class C_M_ExportDirWait
: public MigratorContext
{
761 C_M_ExportDirWait(Migrator
*m
, MDRequestRef mdr
, int count
)
762 : MigratorContext(m
), mdr(mdr
), count(count
) {}
763 void finish(int r
) override
{
764 mig
->dispatch_export_dir(mdr
, count
);
769 /** export_dir(dir, dest)
770 * public method to initiate an export.
771 * will fail if the directory is freezing, frozen, unpinnable, or root.
773 void Migrator::export_dir(CDir
*dir
, mds_rank_t dest
)
775 dout(7) << "export_dir " << *dir
<< " to " << dest
<< dendl
;
776 assert(dir
->is_auth());
777 assert(dest
!= mds
->get_nodeid());
779 if (mds
->mdcache
->is_readonly()) {
780 dout(7) << "read-only FS, no exports for now" << dendl
;
783 if (!mds
->mdsmap
->is_active(dest
)) {
784 dout(7) << "dest not active, no exports for now" << dendl
;
787 if (mds
->is_cluster_degraded()) {
788 dout(7) << "cluster degraded, no exports for now" << dendl
;
791 if (dir
->inode
->is_system()) {
792 dout(7) << "i won't export system dirs (root, mdsdirs, stray, /.ceph, etc.)" << dendl
;
797 if (!dir
->inode
->is_base() && dir
->inode
->get_projected_parent_dir()->inode
->is_stray() &&
798 dir
->inode
->get_projected_parent_dir()->get_parent_dir()->ino() != MDS_INO_MDSDIR(dest
)) {
799 dout(7) << "i won't export anything in stray" << dendl
;
803 if (dir
->is_frozen() ||
804 dir
->is_freezing()) {
805 dout(7) << " can't export, freezing|frozen. wait for other exports to finish first." << dendl
;
808 if (dir
->state_test(CDir::STATE_EXPORTING
)) {
809 dout(7) << "already exporting" << dendl
;
813 if (!mds
->is_stopping() && !dir
->inode
->is_exportable(dest
)) {
814 dout(7) << "dir is export pinned" << dendl
;
818 if (dest
== mds
->get_nodeid() || !mds
->mdsmap
->is_up(dest
)) {
819 dout(7) << "cannot export: dest " << dest
<< " is me or is not active" << dendl
;
823 if (g_conf
->mds_thrash_exports
) {
824 // create random subtree bound (which will not be exported)
826 for (auto p
= dir
->begin(); p
!= dir
->end(); ++p
) {
828 CDentry::linkage_t
*dnl
= dn
->get_linkage();
829 if (dnl
->is_primary()) {
830 CInode
*in
= dnl
->get_inode();
832 in
->get_nested_dirfrags(ls
);
836 int n
= rand() % ls
.size();
840 if (!(bd
->is_frozen() || bd
->is_freezing())) {
841 assert(bd
->is_auth());
842 dir
->state_set(CDir::STATE_AUXSUBTREE
);
843 mds
->mdcache
->adjust_subtree_auth(dir
, mds
->get_nodeid());
844 dout(0) << "export_dir: create aux subtree " << *bd
<< " under " << *dir
<< dendl
;
849 mds
->hit_export_target(ceph_clock_now(), dest
, -1);
852 dir
->state_set(CDir::STATE_EXPORTING
);
854 MDRequestRef mdr
= mds
->mdcache
->request_start_internal(CEPH_MDS_OP_EXPORTDIR
);
855 mdr
->more()->export_dir
= dir
;
857 assert(export_state
.count(dir
) == 0);
858 export_state_t
& stat
= export_state
[dir
];
859 stat
.state
= EXPORT_LOCKING
;
861 stat
.tid
= mdr
->reqid
.tid
;
864 return mds
->mdcache
->dispatch_request(mdr
);
867 void Migrator::dispatch_export_dir(MDRequestRef
& mdr
, int count
)
869 dout(7) << "dispatch_export_dir " << *mdr
<< dendl
;
871 CDir
*dir
= mdr
->more()->export_dir
;
872 map
<CDir
*,export_state_t
>::iterator it
= export_state
.find(dir
);
873 if (it
== export_state
.end() || it
->second
.tid
!= mdr
->reqid
.tid
) {
874 // export must have aborted.
875 dout(7) << "export must have aborted " << *mdr
<< dendl
;
876 mds
->mdcache
->request_finish(mdr
);
879 assert(it
->second
.state
== EXPORT_LOCKING
);
881 mds_rank_t dest
= it
->second
.peer
;
883 if (!mds
->is_export_target(dest
)) {
884 dout(7) << "dest is not yet an export target" << dendl
;
886 dout(5) << "dest has not been added as export target after three MDSMap epochs, canceling export" << dendl
;
887 export_try_cancel(dir
);
891 mds
->locker
->drop_locks(mdr
.get());
892 mdr
->drop_local_auth_pins();
894 mds
->wait_for_mdsmap(mds
->mdsmap
->get_epoch(), new C_M_ExportDirWait(this, mdr
, count
+1));
898 if (!dir
->inode
->get_parent_dn()) {
899 dout(7) << "waiting for dir to become stable before export: " << *dir
<< dendl
;
900 dir
->add_waiter(CDir::WAIT_CREATED
, new C_M_ExportDirWait(this, mdr
, 1));
904 if (mdr
->aborted
|| dir
->is_frozen() || dir
->is_freezing()) {
905 dout(7) << "wouldblock|freezing|frozen, canceling export" << dendl
;
906 export_try_cancel(dir
);
911 set
<SimpleLock
*> rdlocks
;
912 set
<SimpleLock
*> xlocks
;
913 set
<SimpleLock
*> wrlocks
;
914 get_export_lock_set(dir
, rdlocks
);
915 // If auth MDS of the subtree root inode is neither the exporter MDS
916 // nor the importer MDS and it gathers subtree root's fragstat/neststat
917 // while the subtree is exporting. It's possible that the exporter MDS
918 // and the importer MDS both are auth MDS of the subtree root or both
919 // are not auth MDS of the subtree root at the time they receive the
920 // lock messages. So the auth MDS of the subtree root inode may get no
921 // or duplicated fragstat/neststat for the subtree root dirfrag.
922 wrlocks
.insert(&dir
->get_inode()->filelock
);
923 wrlocks
.insert(&dir
->get_inode()->nestlock
);
924 if (dir
->get_inode()->is_auth()) {
925 dir
->get_inode()->filelock
.set_scatter_wanted();
926 dir
->get_inode()->nestlock
.set_scatter_wanted();
929 if (!mds
->locker
->acquire_locks(mdr
, rdlocks
, wrlocks
, xlocks
, NULL
, NULL
, true)) {
931 export_try_cancel(dir
);
935 assert(g_conf
->mds_kill_export_at
!= 1);
936 it
->second
.state
= EXPORT_DISCOVERING
;
938 // send ExportDirDiscover (ask target)
940 dir
->inode
->make_path(path
);
941 MExportDirDiscover
*discover
= new MExportDirDiscover(dir
->dirfrag(), path
,
944 mds
->send_message_mds(discover
, dest
);
945 assert(g_conf
->mds_kill_export_at
!= 2);
947 it
->second
.last_cum_auth_pins_change
= ceph_clock_now();
949 // start the freeze, but hold it up with an auth_pin.
951 assert(dir
->is_freezing_tree());
952 dir
->add_waiter(CDir::WAIT_FROZEN
, new C_MDC_ExportFreeze(this, dir
, it
->second
.tid
));
956 * called on receipt of MExportDirDiscoverAck
957 * the importer now has the directory's _inode_ in memory, and pinned.
959 * This function DOES put the passed message before returning
961 void Migrator::handle_export_discover_ack(MExportDirDiscoverAck
*m
)
963 CDir
*dir
= cache
->get_dirfrag(m
->get_dirfrag());
964 mds_rank_t
dest(m
->get_source().num());
965 utime_t now
= ceph_clock_now();
968 dout(7) << "export_discover_ack from " << m
->get_source()
969 << " on " << *dir
<< dendl
;
971 mds
->hit_export_target(now
, dest
, -1);
973 map
<CDir
*,export_state_t
>::iterator it
= export_state
.find(dir
);
974 if (it
== export_state
.end() ||
975 it
->second
.tid
!= m
->get_tid() ||
976 it
->second
.peer
!= dest
) {
977 dout(7) << "must have aborted" << dendl
;
979 assert(it
->second
.state
== EXPORT_DISCOVERING
);
980 // release locks to avoid deadlock
981 MDRequestRef mdr
= static_cast<MDRequestImpl
*>(it
->second
.mut
.get());
983 mds
->mdcache
->request_finish(mdr
);
984 it
->second
.mut
.reset();
985 // freeze the subtree
986 it
->second
.state
= EXPORT_FREEZING
;
987 dir
->auth_unpin(this);
988 assert(g_conf
->mds_kill_export_at
!= 3);
994 class C_M_ExportSessionsFlushed
: public MigratorContext
{
998 C_M_ExportSessionsFlushed(Migrator
*m
, CDir
*d
, uint64_t t
)
999 : MigratorContext(m
), dir(d
), tid(t
) {
1000 assert(dir
!= NULL
);
1002 void finish(int r
) override
{
1003 mig
->export_sessions_flushed(dir
, tid
);
1007 void Migrator::export_sessions_flushed(CDir
*dir
, uint64_t tid
)
1009 dout(7) << "export_sessions_flushed " << *dir
<< dendl
;
1011 map
<CDir
*,export_state_t
>::iterator it
= export_state
.find(dir
);
1012 if (it
== export_state
.end() ||
1013 it
->second
.state
== EXPORT_CANCELLING
||
1014 it
->second
.tid
!= tid
) {
1015 // export must have aborted.
1016 dout(7) << "export must have aborted on " << dir
<< dendl
;
1020 assert(it
->second
.state
== EXPORT_PREPPING
|| it
->second
.state
== EXPORT_WARNING
);
1021 assert(it
->second
.warning_ack_waiting
.count(MDS_RANK_NONE
) > 0);
1022 it
->second
.warning_ack_waiting
.erase(MDS_RANK_NONE
);
1023 if (it
->second
.state
== EXPORT_WARNING
&& it
->second
.warning_ack_waiting
.empty())
1024 export_go(dir
); // start export.
1027 void Migrator::export_frozen(CDir
*dir
, uint64_t tid
)
1029 dout(7) << "export_frozen on " << *dir
<< dendl
;
1031 map
<CDir
*,export_state_t
>::iterator it
= export_state
.find(dir
);
1032 if (it
== export_state
.end() || it
->second
.tid
!= tid
) {
1033 dout(7) << "export must have aborted" << dendl
;
1037 assert(it
->second
.state
== EXPORT_FREEZING
);
1038 assert(dir
->is_frozen_tree_root());
1039 assert(dir
->get_cum_auth_pins() == 0);
1041 CInode
*diri
= dir
->get_inode();
1043 // ok, try to grab all my locks.
1044 set
<SimpleLock
*> rdlocks
;
1045 get_export_lock_set(dir
, rdlocks
);
1046 if ((diri
->is_auth() && diri
->is_frozen()) ||
1047 !mds
->locker
->can_rdlock_set(rdlocks
) ||
1048 !diri
->filelock
.can_wrlock(-1) ||
1049 !diri
->nestlock
.can_wrlock(-1)) {
1050 dout(7) << "export_dir couldn't acquire all needed locks, failing. "
1053 dir
->unfreeze_tree();
1054 cache
->try_subtree_merge(dir
);
1056 mds
->send_message_mds(new MExportDirCancel(dir
->dirfrag(), it
->second
.tid
), it
->second
.peer
);
1057 export_state
.erase(it
);
1059 dir
->state_clear(CDir::STATE_EXPORTING
);
1060 cache
->maybe_send_pending_resolves();
1064 it
->second
.mut
= new MutationImpl();
1065 if (diri
->is_auth())
1066 it
->second
.mut
->auth_pin(diri
);
1067 mds
->locker
->rdlock_take_set(rdlocks
, it
->second
.mut
);
1068 mds
->locker
->wrlock_force(&diri
->filelock
, it
->second
.mut
);
1069 mds
->locker
->wrlock_force(&diri
->nestlock
, it
->second
.mut
);
1071 cache
->show_subtrees();
1073 // CDir::_freeze_tree() should have forced it into subtree.
1074 assert(dir
->get_dir_auth() == mds_authority_t(mds
->get_nodeid(), mds
->get_nodeid()));
1077 cache
->get_subtree_bounds(dir
, bounds
);
1079 // generate prep message, log entry.
1080 MExportDirPrep
*prep
= new MExportDirPrep(dir
->dirfrag(), it
->second
.tid
);
1082 // include list of bystanders
1083 for (compact_map
<mds_rank_t
,unsigned>::iterator p
= dir
->replicas_begin();
1084 p
!= dir
->replicas_end();
1086 if (p
->first
!= it
->second
.peer
) {
1087 dout(10) << "bystander mds." << p
->first
<< dendl
;
1088 prep
->add_bystander(p
->first
);
1092 // include base dirfrag
1093 cache
->replicate_dir(dir
, it
->second
.peer
, prep
->basedir
);
1096 * include spanning tree for all nested exports.
1097 * these need to be on the destination _before_ the final export so that
1098 * dir_auth updates on any nested exports are properly absorbed.
1099 * this includes inodes and dirfrags included in the subtree, but
1100 * only the inodes at the bounds.
1102 * each trace is: df ('-' | ('f' dir | 'd') dentry inode (dir dentry inode)*)
1104 set
<inodeno_t
> inodes_added
;
1105 set
<dirfrag_t
> dirfrags_added
;
1108 for (set
<CDir
*>::iterator p
= bounds
.begin();
1114 bound
->get(CDir::PIN_EXPORTBOUND
);
1115 bound
->state_set(CDir::STATE_EXPORTBOUND
);
1117 dout(7) << " export bound " << *bound
<< dendl
;
1118 prep
->add_bound( bound
->dirfrag() );
1126 // don't repeat inodes
1127 if (inodes_added
.count(cur
->inode
->ino()))
1129 inodes_added
.insert(cur
->inode
->ino());
1131 // prepend dentry + inode
1132 assert(cur
->inode
->is_auth());
1134 cache
->replicate_dentry(cur
->inode
->parent
, it
->second
.peer
, bl
);
1135 dout(7) << " added " << *cur
->inode
->parent
<< dendl
;
1136 cache
->replicate_inode(cur
->inode
, it
->second
.peer
, bl
,
1137 mds
->mdsmap
->get_up_features());
1138 dout(7) << " added " << *cur
->inode
<< dendl
;
1139 bl
.claim_append(tracebl
);
1142 cur
= cur
->get_parent_dir();
1144 // don't repeat dirfrags
1145 if (dirfrags_added
.count(cur
->dirfrag()) ||
1147 start
= 'd'; // start with dentry
1150 dirfrags_added
.insert(cur
->dirfrag());
1153 cache
->replicate_dir(cur
, it
->second
.peer
, bl
);
1154 dout(7) << " added " << *cur
<< dendl
;
1155 bl
.claim_append(tracebl
);
1158 start
= 'f'; // start with dirfrag
1160 bufferlist final_bl
;
1161 dirfrag_t df
= cur
->dirfrag();
1162 ::encode(df
, final_bl
);
1163 ::encode(start
, final_bl
);
1164 final_bl
.claim_append(tracebl
);
1165 prep
->add_trace(final_bl
);
1169 it
->second
.state
= EXPORT_PREPPING
;
1170 mds
->send_message_mds(prep
, it
->second
.peer
);
1171 assert (g_conf
->mds_kill_export_at
!= 4);
1173 // make sure any new instantiations of caps are flushed out
1174 assert(it
->second
.warning_ack_waiting
.empty());
1176 set
<client_t
> export_client_set
;
1177 get_export_client_set(dir
, export_client_set
);
1179 MDSGatherBuilder
gather(g_ceph_context
);
1180 mds
->server
->flush_client_sessions(export_client_set
, gather
);
1181 if (gather
.has_subs()) {
1182 it
->second
.warning_ack_waiting
.insert(MDS_RANK_NONE
);
1183 gather
.set_finisher(new C_M_ExportSessionsFlushed(this, dir
, it
->second
.tid
));
1188 void Migrator::get_export_client_set(CDir
*dir
, set
<client_t
>& client_set
)
1192 while (!dfs
.empty()) {
1193 CDir
*dir
= dfs
.front();
1195 for (CDir::map_t::iterator p
= dir
->begin(); p
!= dir
->end(); ++p
) {
1196 CDentry
*dn
= p
->second
;
1197 if (!dn
->get_linkage()->is_primary())
1199 CInode
*in
= dn
->get_linkage()->get_inode();
1203 in
->get_dirfrags(ls
);
1204 for (list
<CDir
*>::iterator q
= ls
.begin(); q
!= ls
.end(); ++q
) {
1205 if (!(*q
)->state_test(CDir::STATE_EXPORTBOUND
)) {
1206 // include nested dirfrag
1207 assert((*q
)->get_dir_auth().first
== CDIR_AUTH_PARENT
);
1208 dfs
.push_back(*q
); // it's ours, recurse (later)
1212 for (map
<client_t
, Capability
*>::iterator q
= in
->client_caps
.begin();
1213 q
!= in
->client_caps
.end();
1215 client_set
.insert(q
->first
);
1220 void Migrator::get_export_client_set(CInode
*in
, set
<client_t
>& client_set
)
1222 for (map
<client_t
, Capability
*>::iterator q
= in
->client_caps
.begin();
1223 q
!= in
->client_caps
.end();
1225 client_set
.insert(q
->first
);
1228 /* This function DOES put the passed message before returning*/
1229 void Migrator::handle_export_prep_ack(MExportDirPrepAck
*m
)
1231 CDir
*dir
= cache
->get_dirfrag(m
->get_dirfrag());
1232 mds_rank_t
dest(m
->get_source().num());
1233 utime_t now
= ceph_clock_now();
1236 dout(7) << "export_prep_ack " << *dir
<< dendl
;
1238 mds
->hit_export_target(now
, dest
, -1);
1240 map
<CDir
*,export_state_t
>::iterator it
= export_state
.find(dir
);
1241 if (it
== export_state
.end() ||
1242 it
->second
.tid
!= m
->get_tid() ||
1243 it
->second
.peer
!= mds_rank_t(m
->get_source().num())) {
1244 // export must have aborted.
1245 dout(7) << "export must have aborted" << dendl
;
1249 assert(it
->second
.state
== EXPORT_PREPPING
);
1251 if (!m
->is_success()) {
1252 dout(7) << "peer couldn't acquire all needed locks, canceling" << dendl
;
1253 export_try_cancel(dir
, false);
1258 assert (g_conf
->mds_kill_export_at
!= 5);
1261 cache
->get_subtree_bounds(dir
, bounds
);
1263 assert(it
->second
.warning_ack_waiting
.empty() ||
1264 (it
->second
.warning_ack_waiting
.size() == 1 &&
1265 it
->second
.warning_ack_waiting
.count(MDS_RANK_NONE
) > 0));
1266 assert(it
->second
.notify_ack_waiting
.empty());
1268 for (compact_map
<mds_rank_t
,unsigned>::iterator p
= dir
->replicas_begin();
1269 p
!= dir
->replicas_end();
1271 if (p
->first
== it
->second
.peer
) continue;
1272 if (mds
->is_cluster_degraded() &&
1273 !mds
->mdsmap
->is_clientreplay_or_active_or_stopping(p
->first
))
1274 continue; // only if active
1275 it
->second
.warning_ack_waiting
.insert(p
->first
);
1276 it
->second
.notify_ack_waiting
.insert(p
->first
); // we'll eventually get a notifyack, too!
1278 MExportDirNotify
*notify
= new MExportDirNotify(dir
->dirfrag(), it
->second
.tid
, true,
1279 mds_authority_t(mds
->get_nodeid(),CDIR_AUTH_UNKNOWN
),
1280 mds_authority_t(mds
->get_nodeid(),it
->second
.peer
));
1281 for (set
<CDir
*>::iterator q
= bounds
.begin(); q
!= bounds
.end(); ++q
)
1282 notify
->get_bounds().push_back((*q
)->dirfrag());
1283 mds
->send_message_mds(notify
, p
->first
);
1287 it
->second
.state
= EXPORT_WARNING
;
1289 assert(g_conf
->mds_kill_export_at
!= 6);
1291 if (it
->second
.warning_ack_waiting
.empty())
1292 export_go(dir
); // start export.
1299 class C_M_ExportGo
: public MigratorContext
{
1303 C_M_ExportGo(Migrator
*m
, CDir
*d
, uint64_t t
) :
1304 MigratorContext(m
), dir(d
), tid(t
) {
1305 assert(dir
!= NULL
);
1307 void finish(int r
) override
{
1308 mig
->export_go_synced(dir
, tid
);
1312 void Migrator::export_go(CDir
*dir
)
1314 assert(export_state
.count(dir
));
1315 dout(7) << "export_go " << *dir
<< " to " << export_state
[dir
].peer
<< dendl
;
1317 // first sync log to flush out e.g. any cap imports
1318 mds
->mdlog
->wait_for_safe(new C_M_ExportGo(this, dir
, export_state
[dir
].tid
));
1319 mds
->mdlog
->flush();
1322 void Migrator::export_go_synced(CDir
*dir
, uint64_t tid
)
1324 map
<CDir
*,export_state_t
>::iterator it
= export_state
.find(dir
);
1325 if (it
== export_state
.end() ||
1326 it
->second
.state
== EXPORT_CANCELLING
||
1327 it
->second
.tid
!= tid
) {
1328 // export must have aborted.
1329 dout(7) << "export must have aborted on " << dir
<< dendl
;
1332 assert(it
->second
.state
== EXPORT_WARNING
);
1333 mds_rank_t dest
= it
->second
.peer
;
1335 dout(7) << "export_go_synced " << *dir
<< " to " << dest
<< dendl
;
1337 cache
->show_subtrees();
1339 it
->second
.state
= EXPORT_EXPORTING
;
1340 assert(g_conf
->mds_kill_export_at
!= 7);
1342 assert(dir
->is_frozen_tree_root());
1343 assert(dir
->get_cum_auth_pins() == 0);
1345 // set ambiguous auth
1346 cache
->adjust_subtree_auth(dir
, mds
->get_nodeid(), dest
);
1348 // take away the popularity we're sending.
1349 utime_t now
= ceph_clock_now();
1350 mds
->balancer
->subtract_export(dir
, now
);
1352 // fill export message with cache data
1353 MExportDir
*req
= new MExportDir(dir
->dirfrag(), it
->second
.tid
);
1354 map
<client_t
,entity_inst_t
> exported_client_map
;
1355 uint64_t num_exported_inodes
= encode_export_dir(req
->export_data
,
1356 dir
, // recur start point
1357 exported_client_map
,
1359 ::encode(exported_client_map
, req
->client_map
,
1360 mds
->mdsmap
->get_up_features());
1362 // add bounds to message
1364 cache
->get_subtree_bounds(dir
, bounds
);
1365 for (set
<CDir
*>::iterator p
= bounds
.begin();
1368 req
->add_export((*p
)->dirfrag());
1371 mds
->send_message_mds(req
, dest
);
1372 assert(g_conf
->mds_kill_export_at
!= 8);
1374 mds
->hit_export_target(now
, dest
, num_exported_inodes
+1);
1377 if (mds
->logger
) mds
->logger
->inc(l_mds_exported
);
1378 if (mds
->logger
) mds
->logger
->inc(l_mds_exported_inodes
, num_exported_inodes
);
1380 cache
->show_subtrees();
1384 /** encode_export_inode
1385 * update our local state for this inode to export.
1386 * encode relevant state to be sent over the wire.
1387 * used by: encode_export_dir, file_rename (if foreign)
1389 * FIXME: the separation between CInode.encode_export and these methods
1390 * is pretty arbitrary and dumb.
1392 void Migrator::encode_export_inode(CInode
*in
, bufferlist
& enc_state
,
1393 map
<client_t
,entity_inst_t
>& exported_client_map
)
1395 dout(7) << "encode_export_inode " << *in
<< dendl
;
1396 assert(!in
->is_replica(mds
->get_nodeid()));
1399 if (!in
->is_replicated()) {
1400 in
->replicate_relax_locks();
1401 dout(20) << " did replicate_relax_locks, now " << *in
<< dendl
;
1404 ::encode(in
->inode
.ino
, enc_state
);
1405 ::encode(in
->last
, enc_state
);
1406 in
->encode_export(enc_state
);
1409 encode_export_inode_caps(in
, true, enc_state
, exported_client_map
);
1412 void Migrator::encode_export_inode_caps(CInode
*in
, bool auth_cap
, bufferlist
& bl
,
1413 map
<client_t
,entity_inst_t
>& exported_client_map
)
1415 dout(20) << "encode_export_inode_caps " << *in
<< dendl
;
1418 map
<client_t
,Capability::Export
> cap_map
;
1419 in
->export_client_caps(cap_map
);
1420 ::encode(cap_map
, bl
);
1422 ::encode(in
->get_mds_caps_wanted(), bl
);
1424 in
->state_set(CInode::STATE_EXPORTINGCAPS
);
1425 in
->get(CInode::PIN_EXPORTINGCAPS
);
1428 // make note of clients named by exported capabilities
1429 for (map
<client_t
, Capability
*>::iterator it
= in
->client_caps
.begin();
1430 it
!= in
->client_caps
.end();
1432 exported_client_map
[it
->first
] = mds
->sessionmap
.get_inst(entity_name_t::CLIENT(it
->first
.v
));
1435 void Migrator::finish_export_inode_caps(CInode
*in
, mds_rank_t peer
,
1436 map
<client_t
,Capability::Import
>& peer_imported
)
1438 dout(20) << "finish_export_inode_caps " << *in
<< dendl
;
1440 in
->state_clear(CInode::STATE_EXPORTINGCAPS
);
1441 in
->put(CInode::PIN_EXPORTINGCAPS
);
1443 // tell (all) clients about migrating caps..
1444 for (map
<client_t
, Capability
*>::iterator it
= in
->client_caps
.begin();
1445 it
!= in
->client_caps
.end();
1447 Capability
*cap
= it
->second
;
1448 dout(7) << "finish_export_inode_caps telling client." << it
->first
1449 << " exported caps on " << *in
<< dendl
;
1450 MClientCaps
*m
= new MClientCaps(CEPH_CAP_OP_EXPORT
, in
->ino(), 0,
1451 cap
->get_cap_id(), cap
->get_mseq(), mds
->get_osd_epoch_barrier());
1453 map
<client_t
,Capability::Import
>::iterator q
= peer_imported
.find(it
->first
);
1454 assert(q
!= peer_imported
.end());
1455 m
->set_cap_peer(q
->second
.cap_id
, q
->second
.issue_seq
, q
->second
.mseq
, peer
, 0);
1456 mds
->send_message_client_counted(m
, it
->first
);
1458 in
->clear_client_caps_after_export();
1459 mds
->locker
->eval(in
, CEPH_CAP_LOCKS
);
1462 void Migrator::finish_export_inode(CInode
*in
, utime_t now
, mds_rank_t peer
,
1463 map
<client_t
,Capability::Import
>& peer_imported
,
1464 list
<MDSInternalContextBase
*>& finished
)
1466 dout(12) << "finish_export_inode " << *in
<< dendl
;
1472 // clear/unpin cached_by (we're no longer the authority)
1473 in
->clear_replica_map();
1475 // twiddle lock states for auth -> replica transition
1476 in
->authlock
.export_twiddle();
1477 in
->linklock
.export_twiddle();
1478 in
->dirfragtreelock
.export_twiddle();
1479 in
->filelock
.export_twiddle();
1480 in
->nestlock
.export_twiddle();
1481 in
->xattrlock
.export_twiddle();
1482 in
->snaplock
.export_twiddle();
1483 in
->flocklock
.export_twiddle();
1484 in
->policylock
.export_twiddle();
1487 assert(in
->is_auth());
1488 in
->state_clear(CInode::STATE_AUTH
);
1489 in
->replica_nonce
= CInode::EXPORT_NONCE
;
1491 in
->clear_dirty_rstat();
1493 // no more auth subtree? clear scatter dirty
1494 if (!in
->has_subtree_root_dirfrag(mds
->get_nodeid()))
1495 in
->clear_scatter_dirty();
1497 in
->item_open_file
.remove_myself();
1499 in
->clear_dirty_parent();
1501 in
->clear_file_locks();
1504 in
->take_waiting(CInode::WAIT_ANY_MASK
, finished
);
1506 in
->finish_export(now
);
1508 finish_export_inode_caps(in
, peer
, peer_imported
);
1511 uint64_t Migrator::encode_export_dir(bufferlist
& exportbl
,
1513 map
<client_t
,entity_inst_t
>& exported_client_map
,
1516 uint64_t num_exported
= 0;
1518 dout(7) << "encode_export_dir " << *dir
<< " " << dir
->get_num_head_items() << " head items" << dendl
;
1520 assert(dir
->get_projected_version() == dir
->get_version());
1522 #ifdef MDS_VERIFY_FRAGSTAT
1523 if (dir
->is_complete())
1524 dir
->verify_fragstat();
1528 dirfrag_t df
= dir
->dirfrag();
1529 ::encode(df
, exportbl
);
1530 dir
->encode_export(exportbl
);
1532 __u32 nden
= dir
->items
.size();
1533 ::encode(nden
, exportbl
);
1536 list
<CDir
*> subdirs
;
1537 CDir::map_t::iterator it
;
1538 for (it
= dir
->begin(); it
!= dir
->end(); ++it
) {
1539 CDentry
*dn
= it
->second
;
1540 CInode
*in
= dn
->get_linkage()->get_inode();
1542 if (!dn
->is_replicated())
1543 dn
->lock
.replicate_relax();
1548 dout(7) << "encode_export_dir exporting " << *dn
<< dendl
;
1551 ::encode(dn
->name
, exportbl
);
1552 ::encode(dn
->last
, exportbl
);
1555 dn
->encode_export(exportbl
);
1560 if (dn
->get_linkage()->is_null()) {
1561 exportbl
.append("N", 1); // null dentry
1565 if (dn
->get_linkage()->is_remote()) {
1567 exportbl
.append("L", 1); // remote link
1569 inodeno_t ino
= dn
->get_linkage()->get_remote_ino();
1570 unsigned char d_type
= dn
->get_linkage()->get_remote_d_type();
1571 ::encode(ino
, exportbl
);
1572 ::encode(d_type
, exportbl
);
1578 exportbl
.append("I", 1); // inode dentry
1580 encode_export_inode(in
, exportbl
, exported_client_map
); // encode, and (update state for) export
1584 in
->get_dirfrags(dfs
);
1585 for (list
<CDir
*>::iterator p
= dfs
.begin(); p
!= dfs
.end(); ++p
) {
1587 if (!t
->state_test(CDir::STATE_EXPORTBOUND
)) {
1588 // include nested dirfrag
1589 assert(t
->get_dir_auth().first
== CDIR_AUTH_PARENT
);
1590 subdirs
.push_back(t
); // it's ours, recurse (later)
1596 for (list
<CDir
*>::iterator it
= subdirs
.begin(); it
!= subdirs
.end(); ++it
)
1597 num_exported
+= encode_export_dir(exportbl
, *it
, exported_client_map
, now
);
1599 return num_exported
;
1602 void Migrator::finish_export_dir(CDir
*dir
, utime_t now
, mds_rank_t peer
,
1603 map
<inodeno_t
,map
<client_t
,Capability::Import
> >& peer_imported
,
1604 list
<MDSInternalContextBase
*>& finished
, int *num_dentries
)
1606 dout(10) << "finish_export_dir " << *dir
<< dendl
;
1609 dir
->clear_replica_map();
1612 assert(dir
->is_auth());
1613 dir
->state_clear(CDir::STATE_AUTH
);
1614 dir
->remove_bloom();
1615 dir
->replica_nonce
= CDir::EXPORT_NONCE
;
1617 if (dir
->is_dirty())
1620 // suck up all waiters
1621 dir
->take_waiting(CDir::WAIT_ANY_MASK
, finished
); // all dir waiters
1624 dir
->finish_export(now
);
1627 list
<CDir
*> subdirs
;
1628 CDir::map_t::iterator it
;
1629 for (it
= dir
->begin(); it
!= dir
->end(); ++it
) {
1630 CDentry
*dn
= it
->second
;
1631 CInode
*in
= dn
->get_linkage()->get_inode();
1634 dn
->finish_export();
1637 if (dn
->get_linkage()->is_primary()) {
1638 finish_export_inode(in
, now
, peer
, peer_imported
[in
->ino()], finished
);
1641 in
->get_nested_dirfrags(subdirs
);
1644 cache
->touch_dentry_bottom(dn
); // move dentry to tail of LRU
1649 for (list
<CDir
*>::iterator it
= subdirs
.begin(); it
!= subdirs
.end(); ++it
)
1650 finish_export_dir(*it
, now
, peer
, peer_imported
, finished
, num_dentries
);
1653 class C_MDS_ExportFinishLogged
: public MigratorLogContext
{
1656 C_MDS_ExportFinishLogged(Migrator
*m
, CDir
*d
) : MigratorLogContext(m
), dir(d
) {}
1657 void finish(int r
) override
{
1658 mig
->export_logged_finish(dir
);
1664 * i should get an export_ack from the export target.
1666 * This function DOES put the passed message before returning
1668 void Migrator::handle_export_ack(MExportDirAck
*m
)
1670 CDir
*dir
= cache
->get_dirfrag(m
->get_dirfrag());
1671 mds_rank_t
dest(m
->get_source().num());
1672 utime_t now
= ceph_clock_now();
1674 assert(dir
->is_frozen_tree_root()); // i'm exporting!
1677 dout(7) << "handle_export_ack " << *dir
<< dendl
;
1679 mds
->hit_export_target(now
, dest
, -1);
1681 map
<CDir
*,export_state_t
>::iterator it
= export_state
.find(dir
);
1682 assert(it
!= export_state
.end());
1683 assert(it
->second
.state
== EXPORT_EXPORTING
);
1684 assert(it
->second
.tid
== m
->get_tid());
1686 bufferlist::iterator bp
= m
->imported_caps
.begin();
1687 ::decode(it
->second
.peer_imported
, bp
);
1689 it
->second
.state
= EXPORT_LOGGINGFINISH
;
1690 assert (g_conf
->mds_kill_export_at
!= 9);
1692 cache
->get_subtree_bounds(dir
, bounds
);
1695 // include export bounds, to ensure they're in the journal.
1696 EExport
*le
= new EExport(mds
->mdlog
, dir
, it
->second
.peer
);;
1697 mds
->mdlog
->start_entry(le
);
1699 le
->metablob
.add_dir_context(dir
, EMetaBlob::TO_ROOT
);
1700 le
->metablob
.add_dir(dir
, false);
1701 for (set
<CDir
*>::iterator p
= bounds
.begin();
1705 le
->get_bounds().insert(bound
->dirfrag());
1706 le
->metablob
.add_dir_context(bound
);
1707 le
->metablob
.add_dir(bound
, false);
1710 // list us second, them first.
1711 // this keeps authority().first in sync with subtree auth state in the journal.
1712 cache
->adjust_subtree_auth(dir
, it
->second
.peer
, mds
->get_nodeid());
1714 // log export completion, then finish (unfreeze, trigger finish context, etc.)
1715 mds
->mdlog
->submit_entry(le
, new C_MDS_ExportFinishLogged(this, dir
));
1716 mds
->mdlog
->flush();
1717 assert (g_conf
->mds_kill_export_at
!= 10);
1722 void Migrator::export_notify_abort(CDir
*dir
, set
<CDir
*>& bounds
)
1724 dout(7) << "export_notify_abort " << *dir
<< dendl
;
1726 export_state_t
& stat
= export_state
[dir
];
1727 assert(stat
.state
== EXPORT_CANCELLING
);
1729 if (stat
.notify_ack_waiting
.empty()) {
1730 stat
.state
= EXPORT_CANCELLED
;
1734 dir
->auth_pin(this);
1736 for (set
<mds_rank_t
>::iterator p
= stat
.notify_ack_waiting
.begin();
1737 p
!= stat
.notify_ack_waiting
.end();
1739 MExportDirNotify
*notify
= new MExportDirNotify(dir
->dirfrag(),stat
.tid
, true,
1740 pair
<int,int>(mds
->get_nodeid(),stat
.peer
),
1741 pair
<int,int>(mds
->get_nodeid(),CDIR_AUTH_UNKNOWN
));
1742 for (set
<CDir
*>::iterator i
= bounds
.begin(); i
!= bounds
.end(); ++i
)
1743 notify
->get_bounds().push_back((*i
)->dirfrag());
1744 mds
->send_message_mds(notify
, *p
);
1749 * this happens if hte dest failes after i send teh export data but before it is acked
1750 * that is, we don't know they safely received and logged it, so we reverse our changes
1753 void Migrator::export_reverse(CDir
*dir
)
1755 dout(7) << "export_reverse " << *dir
<< dendl
;
1757 set
<CInode
*> to_eval
;
1760 cache
->get_subtree_bounds(dir
, bounds
);
1762 // remove exporting pins
1765 while (!rq
.empty()) {
1766 CDir
*t
= rq
.front();
1769 for (CDir::map_t::iterator p
= t
->items
.begin(); p
!= t
->items
.end(); ++p
) {
1770 p
->second
->abort_export();
1771 if (!p
->second
->get_linkage()->is_primary())
1773 CInode
*in
= p
->second
->get_linkage()->get_inode();
1775 if (in
->state_test(CInode::STATE_EVALSTALECAPS
)) {
1776 in
->state_clear(CInode::STATE_EVALSTALECAPS
);
1780 in
->get_nested_dirfrags(rq
);
1785 for (const auto &bd
: bounds
) {
1786 bd
->put(CDir::PIN_EXPORTBOUND
);
1787 bd
->state_clear(CDir::STATE_EXPORTBOUND
);
1790 // notify bystanders
1791 export_notify_abort(dir
, bounds
);
1793 // unfreeze tree, with possible subtree merge.
1794 cache
->adjust_subtree_auth(dir
, mds
->get_nodeid(), mds
->get_nodeid());
1796 // process delayed expires
1797 cache
->process_delayed_expire(dir
);
1799 dir
->unfreeze_tree();
1800 cache
->try_subtree_merge(dir
);
1802 // revoke/resume stale caps
1803 for (auto in
: to_eval
) {
1804 bool need_issue
= false;
1805 for (auto& p
: in
->get_client_caps()) {
1806 Capability
*cap
= p
.second
;
1807 if (cap
->is_stale()) {
1808 mds
->locker
->revoke_stale_caps(cap
);
1814 (!in
->is_auth() || !mds
->locker
->eval(in
, CEPH_CAP_LOCKS
)))
1815 mds
->locker
->issue_caps(in
);
1818 cache
->show_cache();
1823 * once i get the ack, and logged the EExportFinish(true),
1824 * send notifies (if any), otherwise go straight to finish.
1827 void Migrator::export_logged_finish(CDir
*dir
)
1829 dout(7) << "export_logged_finish " << *dir
<< dendl
;
1831 export_state_t
& stat
= export_state
[dir
];
1835 cache
->get_subtree_bounds(dir
, bounds
);
1837 for (set
<mds_rank_t
>::iterator p
= stat
.notify_ack_waiting
.begin();
1838 p
!= stat
.notify_ack_waiting
.end();
1840 MExportDirNotify
*notify
= new MExportDirNotify(dir
->dirfrag(), stat
.tid
, true,
1841 pair
<int,int>(mds
->get_nodeid(), stat
.peer
),
1842 pair
<int,int>(stat
.peer
, CDIR_AUTH_UNKNOWN
));
1844 for (set
<CDir
*>::iterator i
= bounds
.begin(); i
!= bounds
.end(); ++i
)
1845 notify
->get_bounds().push_back((*i
)->dirfrag());
1847 mds
->send_message_mds(notify
, *p
);
1850 // wait for notifyacks
1851 stat
.state
= EXPORT_NOTIFYING
;
1852 assert (g_conf
->mds_kill_export_at
!= 11);
1854 // no notifies to wait for?
1855 if (stat
.notify_ack_waiting
.empty()) {
1856 export_finish(dir
); // skip notify/notify_ack stage.
1858 // notify peer to send cap import messages to clients
1859 if (!mds
->is_cluster_degraded() ||
1860 mds
->mdsmap
->is_clientreplay_or_active_or_stopping(stat
.peer
)) {
1861 mds
->send_message_mds(new MExportDirFinish(dir
->dirfrag(), false, stat
.tid
), stat
.peer
);
1863 dout(7) << "not sending MExportDirFinish, dest has failed" << dendl
;
1870 * i'll get an ack from each bystander.
1871 * when i get them all, do the export.
1873 * i'll get an ack from each bystander.
1874 * when i get them all, unfreeze and send the finish.
1876 * This function DOES put the passed message before returning
1878 void Migrator::handle_export_notify_ack(MExportDirNotifyAck
*m
)
1880 CDir
*dir
= cache
->get_dirfrag(m
->get_dirfrag());
1881 mds_rank_t
dest(m
->get_source().num());
1882 utime_t now
= ceph_clock_now();
1884 mds_rank_t from
= mds_rank_t(m
->get_source().num());
1886 mds
->hit_export_target(now
, dest
, -1);
1888 auto export_state_entry
= export_state
.find(dir
);
1889 if (export_state_entry
!= export_state
.end()) {
1890 export_state_t
& stat
= export_state_entry
->second
;
1891 if (stat
.state
== EXPORT_WARNING
&&
1892 stat
.warning_ack_waiting
.erase(from
)) {
1893 // exporting. process warning.
1894 dout(7) << "handle_export_notify_ack from " << m
->get_source()
1895 << ": exporting, processing warning on " << *dir
<< dendl
;
1896 if (stat
.warning_ack_waiting
.empty())
1897 export_go(dir
); // start export.
1898 } else if (stat
.state
== EXPORT_NOTIFYING
&&
1899 stat
.notify_ack_waiting
.erase(from
)) {
1900 // exporting. process notify.
1901 dout(7) << "handle_export_notify_ack from " << m
->get_source()
1902 << ": exporting, processing notify on " << *dir
<< dendl
;
1903 if (stat
.notify_ack_waiting
.empty())
1905 } else if (stat
.state
== EXPORT_CANCELLING
&&
1906 m
->get_new_auth().second
== CDIR_AUTH_UNKNOWN
&& // not warning ack
1907 stat
.notify_ack_waiting
.erase(from
)) {
1908 dout(7) << "handle_export_notify_ack from " << m
->get_source()
1909 << ": cancelling export, processing notify on " << *dir
<< dendl
;
1910 if (stat
.notify_ack_waiting
.empty()) {
1911 export_state
.erase(export_state_entry
);
1912 export_cancel_finish(dir
);
1917 auto import_state_entry
= import_state
.find(dir
->dirfrag());
1918 if (import_state_entry
!= import_state
.end()) {
1919 import_state_t
& stat
= import_state_entry
->second
;
1920 if (stat
.state
== IMPORT_ABORTING
) {
1922 dout(7) << "handle_export_notify_ack from " << m
->get_source()
1923 << ": aborting import on " << *dir
<< dendl
;
1924 assert(stat
.bystanders
.count(from
));
1925 stat
.bystanders
.erase(from
);
1926 if (stat
.bystanders
.empty())
1927 import_reverse_unfreeze(dir
);
1935 void Migrator::export_finish(CDir
*dir
)
1937 dout(5) << "export_finish " << *dir
<< dendl
;
1939 assert (g_conf
->mds_kill_export_at
!= 12);
1940 map
<CDir
*,export_state_t
>::iterator it
= export_state
.find(dir
);
1941 if (it
== export_state
.end()) {
1942 dout(7) << "target must have failed, not sending final commit message. export succeeded anyway." << dendl
;
1946 // send finish/commit to new auth
1947 if (!mds
->is_cluster_degraded() ||
1948 mds
->mdsmap
->is_clientreplay_or_active_or_stopping(it
->second
.peer
)) {
1949 mds
->send_message_mds(new MExportDirFinish(dir
->dirfrag(), true, it
->second
.tid
), it
->second
.peer
);
1951 dout(7) << "not sending MExportDirFinish last, dest has failed" << dendl
;
1953 assert(g_conf
->mds_kill_export_at
!= 13);
1955 // finish export (adjust local cache state)
1956 int num_dentries
= 0;
1957 list
<MDSInternalContextBase
*> finished
;
1958 finish_export_dir(dir
, ceph_clock_now(), it
->second
.peer
,
1959 it
->second
.peer_imported
, finished
, &num_dentries
);
1961 assert(!dir
->is_auth());
1962 cache
->adjust_subtree_auth(dir
, it
->second
.peer
);
1966 cache
->get_subtree_bounds(dir
, bounds
);
1967 for (set
<CDir
*>::iterator p
= bounds
.begin();
1971 bd
->put(CDir::PIN_EXPORTBOUND
);
1972 bd
->state_clear(CDir::STATE_EXPORTBOUND
);
1975 if (dir
->state_test(CDir::STATE_AUXSUBTREE
))
1976 dir
->state_clear(CDir::STATE_AUXSUBTREE
);
1978 // discard delayed expires
1979 cache
->discard_delayed_expire(dir
);
1981 dout(7) << "export_finish unfreezing" << dendl
;
1983 // unfreeze tree, with possible subtree merge.
1984 // (we do this _after_ removing EXPORTBOUND pins, to allow merges)
1985 dir
->unfreeze_tree();
1986 cache
->try_subtree_merge(dir
);
1988 // no more auth subtree? clear scatter dirty
1989 if (!dir
->get_inode()->is_auth() &&
1990 !dir
->get_inode()->has_subtree_root_dirfrag(mds
->get_nodeid())) {
1991 dir
->get_inode()->clear_scatter_dirty();
1992 // wake up scatter_nudge waiters
1993 dir
->get_inode()->take_waiting(CInode::WAIT_ANY_MASK
, finished
);
1996 if (!finished
.empty())
1997 mds
->queue_waiters(finished
);
1999 MutationRef mut
= it
->second
.mut
;
2000 // remove from exporting list, clean up state
2001 export_state
.erase(it
);
2002 dir
->state_clear(CDir::STATE_EXPORTING
);
2004 cache
->show_subtrees();
2007 cache
->trim(-1, num_dentries
); // try trimming exported dentries
2009 // send pending import_maps?
2010 mds
->mdcache
->maybe_send_pending_resolves();
2012 // drop locks, unpin path
2014 mds
->locker
->drop_locks(mut
.get());
2018 maybe_do_queued_export();
2028 // ==========================================================
2031 void Migrator::handle_export_discover(MExportDirDiscover
*m
)
2033 mds_rank_t from
= m
->get_source_mds();
2034 assert(from
!= mds
->get_nodeid());
2036 dout(7) << "handle_export_discover on " << m
->get_path() << dendl
;
2038 // note import state
2039 dirfrag_t df
= m
->get_dirfrag();
2040 // only start discovering on this message once.
2041 map
<dirfrag_t
,import_state_t
>::iterator it
= import_state
.find(df
);
2043 assert(it
== import_state
.end());
2045 import_state
[df
].state
= IMPORT_DISCOVERING
;
2046 import_state
[df
].peer
= from
;
2047 import_state
[df
].tid
= m
->get_tid();
2049 // am i retrying after ancient path_traverse results?
2050 if (it
== import_state
.end() ||
2051 it
->second
.peer
!= from
||
2052 it
->second
.tid
!= m
->get_tid()) {
2053 dout(7) << " dropping obsolete message" << dendl
;
2057 assert(it
->second
.state
== IMPORT_DISCOVERING
);
2060 if (!mds
->mdcache
->is_open()) {
2061 dout(5) << " waiting for root" << dendl
;
2062 mds
->mdcache
->wait_for_open(new C_MDS_RetryMessage(mds
, m
));
2066 assert (g_conf
->mds_kill_import_at
!= 1);
2069 CInode
*in
= cache
->get_inode(m
->get_dirfrag().ino
);
2071 // must discover it!
2072 filepath
fpath(m
->get_path());
2073 vector
<CDentry
*> trace
;
2074 MDRequestRef null_ref
;
2075 int r
= cache
->path_traverse(null_ref
, m
, NULL
, fpath
, &trace
, NULL
, MDS_TRAVERSE_DISCOVER
);
2078 dout(7) << "handle_export_discover_2 failed to discover or not dir " << m
->get_path() << ", NAK" << dendl
;
2079 ceph_abort(); // this shouldn't happen if the auth pins its path properly!!!!
2082 ceph_abort(); // this shouldn't happen; the get_inode above would have succeeded.
2086 dout(7) << "handle_export_discover have " << df
<< " inode " << *in
<< dendl
;
2088 import_state
[df
].state
= IMPORT_DISCOVERED
;
2090 // pin inode in the cache (for now)
2091 assert(in
->is_dir());
2092 in
->get(CInode::PIN_IMPORTING
);
2095 dout(7) << " sending export_discover_ack on " << *in
<< dendl
;
2096 mds
->send_message_mds(new MExportDirDiscoverAck(df
, m
->get_tid()), import_state
[df
].peer
);
2098 assert (g_conf
->mds_kill_import_at
!= 2);
2101 void Migrator::import_reverse_discovering(dirfrag_t df
)
2103 import_state
.erase(df
);
2106 void Migrator::import_reverse_discovered(dirfrag_t df
, CInode
*diri
)
2109 diri
->put(CInode::PIN_IMPORTING
);
2110 import_state
.erase(df
);
2113 void Migrator::import_reverse_prepping(CDir
*dir
)
2116 cache
->map_dirfrag_set(import_state
[dir
->dirfrag()].bound_ls
, bounds
);
2117 import_remove_pins(dir
, bounds
);
2118 import_reverse_final(dir
);
2121 /* This function DOES put the passed message before returning*/
2122 void Migrator::handle_export_cancel(MExportDirCancel
*m
)
2124 dout(7) << "handle_export_cancel on " << m
->get_dirfrag() << dendl
;
2125 dirfrag_t df
= m
->get_dirfrag();
2126 map
<dirfrag_t
,import_state_t
>::iterator it
= import_state
.find(df
);
2127 if (it
== import_state
.end()) {
2128 assert(0 == "got export_cancel in weird state");
2129 } else if (it
->second
.state
== IMPORT_DISCOVERING
) {
2130 import_reverse_discovering(df
);
2131 } else if (it
->second
.state
== IMPORT_DISCOVERED
) {
2132 CInode
*in
= cache
->get_inode(df
.ino
);
2134 import_reverse_discovered(df
, in
);
2135 } else if (it
->second
.state
== IMPORT_PREPPING
) {
2136 CDir
*dir
= mds
->mdcache
->get_dirfrag(df
);
2138 import_reverse_prepping(dir
);
2139 } else if (it
->second
.state
== IMPORT_PREPPED
) {
2140 CDir
*dir
= mds
->mdcache
->get_dirfrag(df
);
2143 cache
->get_subtree_bounds(dir
, bounds
);
2144 import_remove_pins(dir
, bounds
);
2145 // adjust auth back to the exportor
2146 cache
->adjust_subtree_auth(dir
, it
->second
.peer
);
2147 import_reverse_unfreeze(dir
);
2149 assert(0 == "got export_cancel in weird state");
2154 /* This function DOES put the passed message before returning*/
2155 void Migrator::handle_export_prep(MExportDirPrep
*m
)
2157 mds_rank_t oldauth
= mds_rank_t(m
->get_source().num());
2158 assert(oldauth
!= mds
->get_nodeid());
2162 list
<MDSInternalContextBase
*> finished
;
2164 // assimilate root dir.
2165 map
<dirfrag_t
,import_state_t
>::iterator it
= import_state
.find(m
->get_dirfrag());
2166 if (!m
->did_assim()) {
2167 assert(it
!= import_state
.end());
2168 assert(it
->second
.state
== IMPORT_DISCOVERED
);
2169 assert(it
->second
.peer
== oldauth
);
2170 diri
= cache
->get_inode(m
->get_dirfrag().ino
);
2172 bufferlist::iterator p
= m
->basedir
.begin();
2173 dir
= cache
->add_replica_dir(p
, diri
, oldauth
, finished
);
2174 dout(7) << "handle_export_prep on " << *dir
<< " (first pass)" << dendl
;
2176 if (it
== import_state
.end() ||
2177 it
->second
.peer
!= oldauth
||
2178 it
->second
.tid
!= m
->get_tid()) {
2179 dout(7) << "handle_export_prep obsolete message, dropping" << dendl
;
2183 assert(it
->second
.state
== IMPORT_PREPPING
);
2184 assert(it
->second
.peer
== oldauth
);
2186 dir
= cache
->get_dirfrag(m
->get_dirfrag());
2188 dout(7) << "handle_export_prep on " << *dir
<< " (subsequent pass)" << dendl
;
2189 diri
= dir
->get_inode();
2191 assert(dir
->is_auth() == false);
2193 cache
->show_subtrees();
2195 // build import bound map
2196 map
<inodeno_t
, fragset_t
> import_bound_fragset
;
2197 for (list
<dirfrag_t
>::iterator p
= m
->get_bounds().begin();
2198 p
!= m
->get_bounds().end();
2200 dout(10) << " bound " << *p
<< dendl
;
2201 import_bound_fragset
[p
->ino
].insert(p
->frag
);
2204 // assimilate contents?
2205 if (!m
->did_assim()) {
2206 dout(7) << "doing assim on " << *dir
<< dendl
;
2207 m
->mark_assim(); // only do this the first time!
2209 // change import state
2210 it
->second
.state
= IMPORT_PREPPING
;
2211 it
->second
.bound_ls
= m
->get_bounds();
2212 it
->second
.bystanders
= m
->get_bystanders();
2213 assert(g_conf
->mds_kill_import_at
!= 3);
2216 dout(7) << "bystanders are " << it
->second
.bystanders
<< dendl
;
2219 diri
->put(CInode::PIN_IMPORTING
);
2220 dir
->get(CDir::PIN_IMPORTING
);
2221 dir
->state_set(CDir::STATE_IMPORTING
);
2223 // assimilate traces to exports
2224 // each trace is: df ('-' | ('f' dir | 'd') dentry inode (dir dentry inode)*)
2225 for (list
<bufferlist
>::iterator p
= m
->traces
.begin();
2226 p
!= m
->traces
.end();
2228 bufferlist::iterator q
= p
->begin();
2233 dout(10) << " trace from " << df
<< " start " << start
<< " len " << p
->length() << dendl
;
2237 cur
= cache
->get_dirfrag(df
);
2239 dout(10) << " had " << *cur
<< dendl
;
2240 } else if (start
== 'f') {
2241 CInode
*in
= cache
->get_inode(df
.ino
);
2243 dout(10) << " had " << *in
<< dendl
;
2244 cur
= cache
->add_replica_dir(q
, in
, oldauth
, finished
);
2245 dout(10) << " added " << *cur
<< dendl
;
2246 } else if (start
== '-') {
2249 assert(0 == "unrecognized start char");
2251 while (start
!= '-') {
2252 CDentry
*dn
= cache
->add_replica_dentry(q
, cur
, finished
);
2253 dout(10) << " added " << *dn
<< dendl
;
2254 CInode
*in
= cache
->add_replica_inode(q
, dn
, finished
);
2255 dout(10) << " added " << *in
<< dendl
;
2258 cur
= cache
->add_replica_dir(q
, in
, oldauth
, finished
);
2259 dout(10) << " added " << *cur
<< dendl
;
2263 // make bound sticky
2264 for (map
<inodeno_t
,fragset_t
>::iterator p
= import_bound_fragset
.begin();
2265 p
!= import_bound_fragset
.end();
2267 CInode
*in
= cache
->get_inode(p
->first
);
2269 in
->get_stickydirs();
2270 dout(7) << " set stickydirs on bound inode " << *in
<< dendl
;
2274 dout(7) << " not doing assim on " << *dir
<< dendl
;
2277 if (!finished
.empty())
2278 mds
->queue_waiters(finished
);
2282 set
<CDir
*> import_bounds
;
2283 for (map
<inodeno_t
,fragset_t
>::iterator p
= import_bound_fragset
.begin();
2284 p
!= import_bound_fragset
.end();
2286 CInode
*in
= cache
->get_inode(p
->first
);
2289 // map fragset into a frag_t list, based on the inode fragtree
2290 list
<frag_t
> fglist
;
2291 for (set
<frag_t
>::iterator q
= p
->second
.begin(); q
!= p
->second
.end(); ++q
)
2292 in
->dirfragtree
.get_leaves_under(*q
, fglist
);
2293 dout(10) << " bound inode " << p
->first
<< " fragset " << p
->second
<< " maps to " << fglist
<< dendl
;
2295 for (list
<frag_t
>::iterator q
= fglist
.begin();
2298 CDir
*bound
= cache
->get_dirfrag(dirfrag_t(p
->first
, *q
));
2300 dout(7) << " opening bounding dirfrag " << *q
<< " on " << *in
<< dendl
;
2301 cache
->open_remote_dirfrag(in
, *q
,
2302 new C_MDS_RetryMessage(mds
, m
));
2306 if (!bound
->state_test(CDir::STATE_IMPORTBOUND
)) {
2307 dout(7) << " pinning import bound " << *bound
<< dendl
;
2308 bound
->get(CDir::PIN_IMPORTBOUND
);
2309 bound
->state_set(CDir::STATE_IMPORTBOUND
);
2311 dout(7) << " already pinned import bound " << *bound
<< dendl
;
2313 import_bounds
.insert(bound
);
2317 dout(7) << " all ready, noting auth and freezing import region" << dendl
;
2319 bool success
= true;
2320 if (!mds
->mdcache
->is_readonly() &&
2321 dir
->get_inode()->filelock
.can_wrlock(-1) &&
2322 dir
->get_inode()->nestlock
.can_wrlock(-1)) {
2323 it
->second
.mut
= new MutationImpl();
2324 // force some locks. hacky.
2325 mds
->locker
->wrlock_force(&dir
->inode
->filelock
, it
->second
.mut
);
2326 mds
->locker
->wrlock_force(&dir
->inode
->nestlock
, it
->second
.mut
);
2328 // note that i am an ambiguous auth for this subtree.
2329 // specify bounds, since the exporter explicitly defines the region.
2330 cache
->adjust_bounded_subtree_auth(dir
, import_bounds
,
2331 pair
<int,int>(oldauth
, mds
->get_nodeid()));
2332 cache
->verify_subtree_bounds(dir
, import_bounds
);
2334 dir
->_freeze_tree();
2336 it
->second
.state
= IMPORT_PREPPED
;
2338 dout(7) << " couldn't acquire all needed locks, failing. " << *dir
<< dendl
;
2340 import_reverse_prepping(dir
);
2344 dout(7) << " sending export_prep_ack on " << *dir
<< dendl
;
2345 mds
->send_message(new MExportDirPrepAck(dir
->dirfrag(), success
, m
->get_tid()), m
->get_connection());
2347 assert(g_conf
->mds_kill_import_at
!= 4);
2355 class C_MDS_ImportDirLoggedStart
: public MigratorLogContext
{
2360 map
<client_t
,entity_inst_t
> imported_client_map
;
2361 map
<client_t
,uint64_t> sseqmap
;
2363 C_MDS_ImportDirLoggedStart(Migrator
*m
, CDir
*d
, mds_rank_t f
) :
2364 MigratorLogContext(m
), df(d
->dirfrag()), dir(d
), from(f
) {
2366 void finish(int r
) override
{
2367 mig
->import_logged_start(df
, dir
, from
, imported_client_map
, sseqmap
);
2371 /* This function DOES put the passed message before returning*/
2372 void Migrator::handle_export_dir(MExportDir
*m
)
2374 assert (g_conf
->mds_kill_import_at
!= 5);
2375 CDir
*dir
= cache
->get_dirfrag(m
->dirfrag
);
2378 mds_rank_t oldauth
= mds_rank_t(m
->get_source().num());
2379 dout(7) << "handle_export_dir importing " << *dir
<< " from " << oldauth
<< dendl
;
2381 assert(!dir
->is_auth());
2383 map
<dirfrag_t
,import_state_t
>::iterator it
= import_state
.find(m
->dirfrag
);
2384 assert(it
!= import_state
.end());
2385 assert(it
->second
.state
== IMPORT_PREPPED
);
2386 assert(it
->second
.tid
== m
->get_tid());
2387 assert(it
->second
.peer
== oldauth
);
2389 utime_t now
= ceph_clock_now();
2391 if (!dir
->get_inode()->dirfragtree
.is_leaf(dir
->get_frag()))
2392 dir
->get_inode()->dirfragtree
.force_to_leaf(g_ceph_context
, dir
->get_frag());
2394 cache
->show_subtrees();
2396 C_MDS_ImportDirLoggedStart
*onlogged
= new C_MDS_ImportDirLoggedStart(this, dir
, oldauth
);
2398 // start the journal entry
2399 EImportStart
*le
= new EImportStart(mds
->mdlog
, dir
->dirfrag(), m
->bounds
, oldauth
);
2400 mds
->mdlog
->start_entry(le
);
2402 le
->metablob
.add_dir_context(dir
);
2404 // adjust auth (list us _first_)
2405 cache
->adjust_subtree_auth(dir
, mds
->get_nodeid(), oldauth
);
2407 // new client sessions, open these after we journal
2408 // include imported sessions in EImportStart
2409 bufferlist::iterator cmp
= m
->client_map
.begin();
2410 ::decode(onlogged
->imported_client_map
, cmp
);
2412 le
->cmapv
= mds
->server
->prepare_force_open_sessions(onlogged
->imported_client_map
, onlogged
->sseqmap
);
2413 le
->client_map
.claim(m
->client_map
);
2415 bufferlist::iterator blp
= m
->export_data
.begin();
2416 int num_imported_inodes
= 0;
2417 while (!blp
.end()) {
2418 num_imported_inodes
+=
2419 decode_import_dir(blp
,
2423 mds
->mdlog
->get_current_segment(),
2424 it
->second
.peer_exports
,
2425 it
->second
.updated_scatterlocks
,
2428 dout(10) << " " << m
->bounds
.size() << " imported bounds" << dendl
;
2430 // include bounds in EImportStart
2431 set
<CDir
*> import_bounds
;
2432 for (vector
<dirfrag_t
>::iterator p
= m
->bounds
.begin();
2433 p
!= m
->bounds
.end();
2435 CDir
*bd
= cache
->get_dirfrag(*p
);
2437 le
->metablob
.add_dir(bd
, false); // note that parent metadata is already in the event
2438 import_bounds
.insert(bd
);
2440 cache
->verify_subtree_bounds(dir
, import_bounds
);
2442 // adjust popularity
2443 mds
->balancer
->add_import(dir
, now
);
2445 dout(7) << "handle_export_dir did " << *dir
<< dendl
;
2448 it
->second
.state
= IMPORT_LOGGINGSTART
;
2449 assert (g_conf
->mds_kill_import_at
!= 6);
2452 mds
->mdlog
->submit_entry(le
, onlogged
);
2453 mds
->mdlog
->flush();
2457 mds
->logger
->inc(l_mds_imported
);
2458 mds
->logger
->inc(l_mds_imported_inodes
, num_imported_inodes
);
2466 * this is an import helper
2467 * called by import_finish, and import_reverse and friends.
2469 void Migrator::import_remove_pins(CDir
*dir
, set
<CDir
*>& bounds
)
2471 import_state_t
& stat
= import_state
[dir
->dirfrag()];
2473 dir
->put(CDir::PIN_IMPORTING
);
2474 dir
->state_clear(CDir::STATE_IMPORTING
);
2478 for (list
<dirfrag_t
>::iterator p
= stat
.bound_ls
.begin();
2479 p
!= stat
.bound_ls
.end();
2481 if (did
.count(p
->ino
))
2484 CInode
*in
= cache
->get_inode(p
->ino
);
2486 in
->put_stickydirs();
2489 if (stat
.state
== IMPORT_PREPPING
) {
2490 for (auto bd
: bounds
) {
2491 if (bd
->state_test(CDir::STATE_IMPORTBOUND
)) {
2492 bd
->put(CDir::PIN_IMPORTBOUND
);
2493 bd
->state_clear(CDir::STATE_IMPORTBOUND
);
2496 } else if (stat
.state
>= IMPORT_PREPPED
) {
2497 // bounding dirfrags
2498 for (auto bd
: bounds
) {
2499 assert(bd
->state_test(CDir::STATE_IMPORTBOUND
));
2500 bd
->put(CDir::PIN_IMPORTBOUND
);
2501 bd
->state_clear(CDir::STATE_IMPORTBOUND
);
2508 * note: this does teh full work of reversing and import and cleaning up
2510 * called by both handle_mds_failure and by handle_resolve (if we are
2511 * a survivor coping with an exporter failure+recovery).
2513 void Migrator::import_reverse(CDir
*dir
)
2515 dout(7) << "import_reverse " << *dir
<< dendl
;
2517 import_state_t
& stat
= import_state
[dir
->dirfrag()];
2518 stat
.state
= IMPORT_ABORTING
;
2521 cache
->get_subtree_bounds(dir
, bounds
);
2524 import_remove_pins(dir
, bounds
);
2526 // update auth, with possible subtree merge.
2527 assert(dir
->is_subtree_root());
2528 if (mds
->is_resolve())
2529 cache
->trim_non_auth_subtree(dir
);
2531 cache
->adjust_subtree_auth(dir
, stat
.peer
);
2533 C_ContextsBase
<MDSInternalContextBase
, MDSInternalContextGather
> *fin
= new C_ContextsBase
<MDSInternalContextBase
, MDSInternalContextGather
>(g_ceph_context
);
2534 if (!dir
->get_inode()->is_auth() &&
2535 !dir
->get_inode()->has_subtree_root_dirfrag(mds
->get_nodeid())) {
2536 dir
->get_inode()->clear_scatter_dirty();
2537 // wake up scatter_nudge waiters
2538 dir
->get_inode()->take_waiting(CInode::WAIT_ANY_MASK
, fin
->contexts
);
2541 int num_dentries
= 0;
2542 // adjust auth bits.
2545 while (!q
.empty()) {
2546 CDir
*cur
= q
.front();
2550 assert(cur
->is_auth());
2551 cur
->state_clear(CDir::STATE_AUTH
);
2552 cur
->remove_bloom();
2553 cur
->clear_replica_map();
2554 cur
->set_replica_nonce(CDir::EXPORT_NONCE
);
2555 if (cur
->is_dirty())
2558 CDir::map_t::iterator it
;
2559 for (it
= cur
->begin(); it
!= cur
->end(); ++it
) {
2560 CDentry
*dn
= it
->second
;
2563 dn
->state_clear(CDentry::STATE_AUTH
);
2564 dn
->clear_replica_map();
2565 dn
->set_replica_nonce(CDentry::EXPORT_NONCE
);
2570 if (dn
->get_linkage()->is_primary()) {
2571 CInode
*in
= dn
->get_linkage()->get_inode();
2572 in
->state_clear(CDentry::STATE_AUTH
);
2573 in
->clear_replica_map();
2574 in
->set_replica_nonce(CInode::EXPORT_NONCE
);
2577 in
->clear_dirty_rstat();
2578 if (!in
->has_subtree_root_dirfrag(mds
->get_nodeid())) {
2579 in
->clear_scatter_dirty();
2580 in
->take_waiting(CInode::WAIT_ANY_MASK
, fin
->contexts
);
2583 in
->clear_dirty_parent();
2585 in
->authlock
.clear_gather();
2586 in
->linklock
.clear_gather();
2587 in
->dirfragtreelock
.clear_gather();
2588 in
->filelock
.clear_gather();
2590 in
->clear_file_locks();
2592 // non-bounding dir?
2594 in
->get_dirfrags(dfs
);
2595 for (list
<CDir
*>::iterator p
= dfs
.begin(); p
!= dfs
.end(); ++p
)
2596 if (bounds
.count(*p
) == 0)
2600 cache
->touch_dentry_bottom(dn
); // move dentry to tail of LRU
2605 dir
->add_waiter(CDir::WAIT_UNFREEZE
, fin
);
2607 if (stat
.state
== IMPORT_ACKING
) {
2608 // remove imported caps
2609 for (map
<CInode
*,map
<client_t
,Capability::Export
> >::iterator p
= stat
.peer_exports
.begin();
2610 p
!= stat
.peer_exports
.end();
2612 CInode
*in
= p
->first
;
2613 for (map
<client_t
,Capability::Export
>::iterator q
= p
->second
.begin();
2614 q
!= p
->second
.end();
2616 Capability
*cap
= in
->get_client_cap(q
->first
);
2618 if (cap
->is_importing())
2619 in
->remove_client_cap(q
->first
);
2621 in
->put(CInode::PIN_IMPORTINGCAPS
);
2623 for (map
<client_t
,entity_inst_t
>::iterator p
= stat
.client_map
.begin();
2624 p
!= stat
.client_map
.end();
2626 Session
*session
= mds
->sessionmap
.get_session(entity_name_t::CLIENT(p
->first
.v
));
2628 session
->dec_importing();
2633 mds
->mdlog
->start_submit_entry(new EImportFinish(dir
, false)); // log failure
2635 cache
->trim(-1, num_dentries
); // try trimming dentries
2637 // notify bystanders; wait in aborting state
2638 import_notify_abort(dir
, bounds
);
2641 void Migrator::import_notify_finish(CDir
*dir
, set
<CDir
*>& bounds
)
2643 dout(7) << "import_notify_finish " << *dir
<< dendl
;
2645 import_state_t
& stat
= import_state
[dir
->dirfrag()];
2646 for (set
<mds_rank_t
>::iterator p
= stat
.bystanders
.begin();
2647 p
!= stat
.bystanders
.end();
2649 MExportDirNotify
*notify
=
2650 new MExportDirNotify(dir
->dirfrag(), stat
.tid
, false,
2651 pair
<int,int>(stat
.peer
, mds
->get_nodeid()),
2652 pair
<int,int>(mds
->get_nodeid(), CDIR_AUTH_UNKNOWN
));
2653 for (set
<CDir
*>::iterator i
= bounds
.begin(); i
!= bounds
.end(); ++i
)
2654 notify
->get_bounds().push_back((*i
)->dirfrag());
2655 mds
->send_message_mds(notify
, *p
);
2659 void Migrator::import_notify_abort(CDir
*dir
, set
<CDir
*>& bounds
)
2661 dout(7) << "import_notify_abort " << *dir
<< dendl
;
2663 import_state_t
& stat
= import_state
[dir
->dirfrag()];
2664 for (set
<mds_rank_t
>::iterator p
= stat
.bystanders
.begin();
2665 p
!= stat
.bystanders
.end(); ) {
2666 if (mds
->is_cluster_degraded() &&
2667 !mds
->mdsmap
->is_clientreplay_or_active_or_stopping(*p
)) {
2668 // this can happen if both exporter and bystander fail in the same mdsmap epoch
2669 stat
.bystanders
.erase(p
++);
2672 MExportDirNotify
*notify
=
2673 new MExportDirNotify(dir
->dirfrag(), stat
.tid
, true,
2674 mds_authority_t(stat
.peer
, mds
->get_nodeid()),
2675 mds_authority_t(stat
.peer
, CDIR_AUTH_UNKNOWN
));
2676 for (set
<CDir
*>::iterator i
= bounds
.begin(); i
!= bounds
.end(); ++i
)
2677 notify
->get_bounds().push_back((*i
)->dirfrag());
2678 mds
->send_message_mds(notify
, *p
);
2681 if (stat
.bystanders
.empty()) {
2682 dout(7) << "no bystanders, finishing reverse now" << dendl
;
2683 import_reverse_unfreeze(dir
);
2685 assert (g_conf
->mds_kill_import_at
!= 10);
2689 void Migrator::import_reverse_unfreeze(CDir
*dir
)
2691 dout(7) << "import_reverse_unfreeze " << *dir
<< dendl
;
2692 assert(!dir
->is_auth());
2693 cache
->discard_delayed_expire(dir
);
2694 dir
->unfreeze_tree();
2695 if (dir
->is_subtree_root())
2696 cache
->try_subtree_merge(dir
);
2697 import_reverse_final(dir
);
2700 void Migrator::import_reverse_final(CDir
*dir
)
2702 dout(7) << "import_reverse_final " << *dir
<< dendl
;
2705 map
<dirfrag_t
, import_state_t
>::iterator it
= import_state
.find(dir
->dirfrag());
2706 assert(it
!= import_state
.end());
2708 MutationRef mut
= it
->second
.mut
;
2709 import_state
.erase(it
);
2711 // send pending import_maps?
2712 mds
->mdcache
->maybe_send_pending_resolves();
2715 mds
->locker
->drop_locks(mut
.get());
2719 cache
->show_subtrees();
2720 //audit(); // this fails, bc we munge up the subtree map during handle_import_map (resolve phase)
2726 void Migrator::import_logged_start(dirfrag_t df
, CDir
*dir
, mds_rank_t from
,
2727 map
<client_t
,entity_inst_t
>& imported_client_map
,
2728 map
<client_t
,uint64_t>& sseqmap
)
2730 map
<dirfrag_t
, import_state_t
>::iterator it
= import_state
.find(dir
->dirfrag());
2731 if (it
== import_state
.end() ||
2732 it
->second
.state
!= IMPORT_LOGGINGSTART
) {
2733 dout(7) << "import " << df
<< " must have aborted" << dendl
;
2734 mds
->server
->finish_force_open_sessions(imported_client_map
, sseqmap
);
2738 dout(7) << "import_logged " << *dir
<< dendl
;
2741 it
->second
.state
= IMPORT_ACKING
;
2743 assert (g_conf
->mds_kill_import_at
!= 7);
2745 // force open client sessions and finish cap import
2746 mds
->server
->finish_force_open_sessions(imported_client_map
, sseqmap
, false);
2747 it
->second
.client_map
.swap(imported_client_map
);
2749 map
<inodeno_t
,map
<client_t
,Capability::Import
> > imported_caps
;
2750 for (map
<CInode
*, map
<client_t
,Capability::Export
> >::iterator p
= it
->second
.peer_exports
.begin();
2751 p
!= it
->second
.peer_exports
.end();
2753 // parameter 'peer' is NONE, delay sending cap import messages to client
2754 finish_import_inode_caps(p
->first
, MDS_RANK_NONE
, true, p
->second
, imported_caps
[p
->first
->ino()]);
2757 // send notify's etc.
2758 dout(7) << "sending ack for " << *dir
<< " to old auth mds." << from
<< dendl
;
2760 // test surviving observer of a failed migration that did not complete
2761 //assert(dir->replica_map.size() < 2 || mds->get_nodeid() != 0);
2763 MExportDirAck
*ack
= new MExportDirAck(dir
->dirfrag(), it
->second
.tid
);
2764 ::encode(imported_caps
, ack
->imported_caps
);
2766 mds
->send_message_mds(ack
, from
);
2767 assert (g_conf
->mds_kill_import_at
!= 8);
2769 cache
->show_subtrees();
2772 /* This function DOES put the passed message before returning*/
2773 void Migrator::handle_export_finish(MExportDirFinish
*m
)
2775 CDir
*dir
= cache
->get_dirfrag(m
->get_dirfrag());
2777 dout(7) << "handle_export_finish on " << *dir
<< (m
->is_last() ? " last" : "") << dendl
;
2779 map
<dirfrag_t
,import_state_t
>::iterator it
= import_state
.find(m
->get_dirfrag());
2780 assert(it
!= import_state
.end());
2781 assert(it
->second
.tid
== m
->get_tid());
2783 import_finish(dir
, false, m
->is_last());
2788 void Migrator::import_finish(CDir
*dir
, bool notify
, bool last
)
2790 dout(7) << "import_finish on " << *dir
<< dendl
;
2792 map
<dirfrag_t
,import_state_t
>::iterator it
= import_state
.find(dir
->dirfrag());
2793 assert(it
!= import_state
.end());
2794 assert(it
->second
.state
== IMPORT_ACKING
|| it
->second
.state
== IMPORT_FINISHING
);
2796 if (it
->second
.state
== IMPORT_ACKING
) {
2797 assert(dir
->is_auth());
2798 cache
->adjust_subtree_auth(dir
, mds
->get_nodeid(), mds
->get_nodeid());
2802 assert(g_conf
->mds_kill_import_at
!= 9);
2804 if (it
->second
.state
== IMPORT_ACKING
) {
2805 for (map
<CInode
*, map
<client_t
,Capability::Export
> >::iterator p
= it
->second
.peer_exports
.begin();
2806 p
!= it
->second
.peer_exports
.end();
2808 CInode
*in
= p
->first
;
2809 assert(in
->is_auth());
2810 for (map
<client_t
,Capability::Export
>::iterator q
= p
->second
.begin();
2811 q
!= p
->second
.end();
2813 Session
*session
= mds
->sessionmap
.get_session(entity_name_t::CLIENT(q
->first
.v
));
2815 Capability
*cap
= in
->get_client_cap(q
->first
);
2817 cap
->merge(q
->second
, true);
2818 cap
->clear_importing();
2819 mds
->mdcache
->do_cap_import(session
, in
, cap
, q
->second
.cap_id
, q
->second
.seq
,
2820 q
->second
.mseq
- 1, it
->second
.peer
, CEPH_CAP_FLAG_AUTH
);
2823 in
->replica_caps_wanted
= 0;
2825 for (map
<client_t
,entity_inst_t
>::iterator p
= it
->second
.client_map
.begin();
2826 p
!= it
->second
.client_map
.end();
2828 Session
*session
= mds
->sessionmap
.get_session(entity_name_t::CLIENT(p
->first
.v
));
2830 session
->dec_importing();
2835 assert(it
->second
.state
== IMPORT_ACKING
);
2836 it
->second
.state
= IMPORT_FINISHING
;
2842 cache
->get_subtree_bounds(dir
, bounds
);
2845 import_notify_finish(dir
, bounds
);
2847 import_remove_pins(dir
, bounds
);
2849 map
<CInode
*, map
<client_t
,Capability::Export
> > peer_exports
;
2850 it
->second
.peer_exports
.swap(peer_exports
);
2852 // clear import state (we're done!)
2853 MutationRef mut
= it
->second
.mut
;
2854 import_state
.erase(it
);
2856 mds
->mdlog
->start_submit_entry(new EImportFinish(dir
, true));
2858 // process delayed expires
2859 cache
->process_delayed_expire(dir
);
2861 // unfreeze tree, with possible subtree merge.
2862 dir
->unfreeze_tree();
2863 cache
->try_subtree_merge(dir
);
2865 cache
->show_subtrees();
2866 //audit(); // this fails, bc we munge up the subtree map during handle_import_map (resolve phase)
2869 mds
->locker
->drop_locks(mut
.get());
2873 // re-eval imported caps
2874 for (map
<CInode
*, map
<client_t
,Capability::Export
> >::iterator p
= peer_exports
.begin();
2875 p
!= peer_exports
.end();
2877 if (p
->first
->is_auth())
2878 mds
->locker
->eval(p
->first
, CEPH_CAP_LOCKS
, true);
2879 p
->first
->put(CInode::PIN_IMPORTINGCAPS
);
2882 // send pending import_maps?
2883 mds
->mdcache
->maybe_send_pending_resolves();
2885 // did i just import mydir?
2886 if (dir
->ino() == MDS_INO_MDSDIR(mds
->get_nodeid()))
2887 cache
->populate_mydir();
2890 if (dir
->get_num_head_items() == 0 &&
2891 !dir
->inode
->is_auth()) {
2893 export_empty_import(dir
);
2898 void Migrator::decode_import_inode(CDentry
*dn
, bufferlist::iterator
& blp
,
2899 mds_rank_t oldauth
, LogSegment
*ls
,
2900 map
<CInode
*, map
<client_t
,Capability::Export
> >& peer_exports
,
2901 list
<ScatterLock
*>& updated_scatterlocks
)
2903 dout(15) << "decode_import_inode on " << *dn
<< dendl
;
2908 ::decode(last
, blp
);
2911 CInode
*in
= cache
->get_inode(ino
, last
);
2913 in
= new CInode(mds
->mdcache
, true, 1, last
);
2917 // state after link -- or not! -sage
2918 in
->decode_import(blp
, ls
); // cap imports are noted for later action
2921 decode_import_inode_caps(in
, true, blp
, peer_exports
);
2923 // link before state -- or not! -sage
2924 if (dn
->get_linkage()->get_inode() != in
) {
2925 assert(!dn
->get_linkage()->get_inode());
2926 dn
->dir
->link_primary_inode(dn
, in
);
2931 cache
->add_inode(in
);
2932 dout(10) << "added " << *in
<< dendl
;
2934 dout(10) << " had " << *in
<< dendl
;
2937 if (in
->inode
.is_dirty_rstat())
2938 in
->mark_dirty_rstat();
2940 // clear if dirtyscattered, since we're going to journal this
2941 // but not until we _actually_ finish the import...
2942 if (in
->filelock
.is_dirty()) {
2943 updated_scatterlocks
.push_back(&in
->filelock
);
2944 mds
->locker
->mark_updated_scatterlock(&in
->filelock
);
2947 if (in
->dirfragtreelock
.is_dirty()) {
2948 updated_scatterlocks
.push_back(&in
->dirfragtreelock
);
2949 mds
->locker
->mark_updated_scatterlock(&in
->dirfragtreelock
);
2952 // adjust replica list
2953 //assert(!in->is_replica(oldauth)); // not true on failed export
2954 in
->add_replica(oldauth
, CInode::EXPORT_NONCE
);
2955 if (in
->is_replica(mds
->get_nodeid()))
2956 in
->remove_replica(mds
->get_nodeid());
2959 void Migrator::decode_import_inode_caps(CInode
*in
, bool auth_cap
,
2960 bufferlist::iterator
&blp
,
2961 map
<CInode
*, map
<client_t
,Capability::Export
> >& peer_exports
)
2963 map
<client_t
,Capability::Export
> cap_map
;
2964 ::decode(cap_map
, blp
);
2966 ::decode(in
->get_mds_caps_wanted(), blp
);
2967 if (!cap_map
.empty() ||
2968 (auth_cap
&& !in
->get_mds_caps_wanted().empty())) {
2969 peer_exports
[in
].swap(cap_map
);
2970 in
->get(CInode::PIN_IMPORTINGCAPS
);
2974 void Migrator::finish_import_inode_caps(CInode
*in
, mds_rank_t peer
, bool auth_cap
,
2975 map
<client_t
,Capability::Export
> &export_map
,
2976 map
<client_t
,Capability::Import
> &import_map
)
2978 for (map
<client_t
,Capability::Export
>::iterator it
= export_map
.begin();
2979 it
!= export_map
.end();
2981 dout(10) << "finish_import_inode_caps for client." << it
->first
<< " on " << *in
<< dendl
;
2982 Session
*session
= mds
->sessionmap
.get_session(entity_name_t::CLIENT(it
->first
.v
));
2985 Capability
*cap
= in
->get_client_cap(it
->first
);
2987 cap
= in
->add_client_cap(it
->first
, session
);
2989 cap
->mark_importing();
2992 Capability::Import
& im
= import_map
[it
->first
];
2993 im
.cap_id
= cap
->get_cap_id();
2994 im
.mseq
= auth_cap
? it
->second
.mseq
: cap
->get_mseq();
2995 im
.issue_seq
= cap
->get_last_seq() + 1;
2998 cap
->merge(it
->second
, auth_cap
);
2999 mds
->mdcache
->do_cap_import(session
, in
, cap
, it
->second
.cap_id
,
3000 it
->second
.seq
, it
->second
.mseq
- 1, peer
,
3001 auth_cap
? CEPH_CAP_FLAG_AUTH
: CEPH_CAP_FLAG_RELEASE
);
3006 in
->replica_caps_wanted
= 0;
3007 in
->put(CInode::PIN_IMPORTINGCAPS
);
3011 int Migrator::decode_import_dir(bufferlist::iterator
& blp
,
3016 map
<CInode
*,map
<client_t
,Capability::Export
> >& peer_exports
,
3017 list
<ScatterLock
*>& updated_scatterlocks
, utime_t now
)
3023 CInode
*diri
= cache
->get_inode(df
.ino
);
3025 CDir
*dir
= diri
->get_or_open_dirfrag(mds
->mdcache
, df
.frag
);
3028 dout(7) << "decode_import_dir " << *dir
<< dendl
;
3031 dir
->decode_import(blp
, now
, ls
);
3033 // adjust replica list
3034 //assert(!dir->is_replica(oldauth)); // not true on failed export
3035 dir
->add_replica(oldauth
, CDir::EXPORT_NONCE
);
3036 if (dir
->is_replica(mds
->get_nodeid()))
3037 dir
->remove_replica(mds
->get_nodeid());
3039 // add to journal entry
3041 le
->metablob
.add_import_dir(dir
);
3043 int num_imported
= 0;
3045 // take all waiters on this dir
3046 // NOTE: a pass of imported data is guaranteed to get all of my waiters because
3047 // a replica's presense in my cache implies/forces it's presense in authority's.
3048 list
<MDSInternalContextBase
*> waiters
;
3050 dir
->take_waiting(CDir::WAIT_ANY_MASK
, waiters
);
3051 for (list
<MDSInternalContextBase
*>::iterator it
= waiters
.begin();
3052 it
!= waiters
.end();
3054 import_root
->add_waiter(CDir::WAIT_UNFREEZE
, *it
); // UNFREEZE will get kicked both on success or failure
3056 dout(15) << "doing contents" << dendl
;
3060 ::decode(nden
, blp
);
3062 for (; nden
>0; nden
--) {
3068 ::decode(dname
, blp
);
3069 ::decode(last
, blp
);
3071 CDentry
*dn
= dir
->lookup_exact_snap(dname
, last
);
3073 dn
= dir
->add_null_dentry(dname
, 1, last
);
3075 dn
->decode_import(blp
, ls
);
3077 dn
->add_replica(oldauth
, CDentry::EXPORT_NONCE
);
3078 if (dn
->is_replica(mds
->get_nodeid()))
3079 dn
->remove_replica(mds
->get_nodeid());
3081 // dentry lock in unreadable state can block path traverse
3082 if (dn
->lock
.get_state() != LOCK_SYNC
)
3083 mds
->locker
->try_eval(&dn
->lock
, NULL
);
3085 dout(15) << "decode_import_dir got " << *dn
<< dendl
;
3089 ::decode(icode
, blp
);
3093 assert(dn
->get_linkage()->is_null());
3097 else if (icode
== 'L') {
3100 unsigned char d_type
;
3102 ::decode(d_type
, blp
);
3103 if (dn
->get_linkage()->is_remote()) {
3104 assert(dn
->get_linkage()->get_remote_ino() == ino
);
3106 dir
->link_remote_inode(dn
, ino
, d_type
);
3109 else if (icode
== 'I') {
3112 decode_import_inode(dn
, blp
, oldauth
, ls
,
3113 peer_exports
, updated_scatterlocks
);
3116 // add dentry to journal entry
3118 le
->metablob
.add_import_dentry(dn
);
3121 #ifdef MDS_VERIFY_FRAGSTAT
3122 if (dir
->is_complete())
3123 dir
->verify_fragstat();
3126 dir
->inode
->maybe_export_pin();
3128 dout(7) << "decode_import_dir done " << *dir
<< dendl
;
3129 return num_imported
;
3136 // authority bystander
3138 /* This function DOES put the passed message before returning*/
3139 void Migrator::handle_export_notify(MExportDirNotify
*m
)
3141 if (!(mds
->is_clientreplay() || mds
->is_active() || mds
->is_stopping())) {
3146 CDir
*dir
= cache
->get_dirfrag(m
->get_dirfrag());
3148 mds_rank_t from
= mds_rank_t(m
->get_source().num());
3149 mds_authority_t old_auth
= m
->get_old_auth();
3150 mds_authority_t new_auth
= m
->get_new_auth();
3153 dout(7) << "handle_export_notify " << old_auth
<< " -> " << new_auth
3154 << " on missing dir " << m
->get_dirfrag() << dendl
;
3155 } else if (dir
->authority() != old_auth
) {
3156 dout(7) << "handle_export_notify old_auth was " << dir
->authority()
3157 << " != " << old_auth
<< " -> " << new_auth
3158 << " on " << *dir
<< dendl
;
3160 dout(7) << "handle_export_notify " << old_auth
<< " -> " << new_auth
3161 << " on " << *dir
<< dendl
;
3164 cache
->map_dirfrag_set(m
->get_bounds(), have
);
3165 cache
->adjust_bounded_subtree_auth(dir
, have
, new_auth
);
3168 cache
->try_subtree_merge(dir
);
3172 if (m
->wants_ack()) {
3173 mds
->send_message_mds(new MExportDirNotifyAck(m
->get_dirfrag(), m
->get_tid(), m
->get_new_auth()), from
);
3176 dout(7) << "handle_export_notify no ack requested" << dendl
;
3183 void Migrator::export_caps(CInode
*in
)
3185 mds_rank_t dest
= in
->authority().first
;
3186 dout(7) << "export_caps to mds." << dest
<< " " << *in
<< dendl
;
3188 assert(in
->is_any_caps());
3189 assert(!in
->is_auth());
3190 assert(!in
->is_ambiguous_auth());
3191 assert(!in
->state_test(CInode::STATE_EXPORTINGCAPS
));
3193 MExportCaps
*ex
= new MExportCaps
;
3194 ex
->ino
= in
->ino();
3196 encode_export_inode_caps(in
, false, ex
->cap_bl
, ex
->client_map
);
3198 mds
->send_message_mds(ex
, dest
);
3201 void Migrator::handle_gather_caps(MGatherCaps
*m
)
3203 CInode
*in
= cache
->get_inode(m
->ino
);
3208 dout(10) << "handle_gather_caps " << *m
<< " from " << m
->get_source()
3211 if (in
->is_any_caps() &&
3213 !in
->is_ambiguous_auth() &&
3214 !in
->state_test(CInode::STATE_EXPORTINGCAPS
))
3221 class C_M_LoggedImportCaps
: public MigratorLogContext
{
3225 map
<CInode
*, map
<client_t
,Capability::Export
> > peer_exports
;
3226 map
<client_t
,entity_inst_t
> client_map
;
3227 map
<client_t
,uint64_t> sseqmap
;
3229 C_M_LoggedImportCaps(Migrator
*m
, CInode
*i
, mds_rank_t f
) : MigratorLogContext(m
), in(i
), from(f
) {}
3230 void finish(int r
) override
{
3231 mig
->logged_import_caps(in
, from
, peer_exports
, client_map
, sseqmap
);
3235 /* This function DOES put the passed message before returning*/
3236 void Migrator::handle_export_caps(MExportCaps
*ex
)
3238 dout(10) << "handle_export_caps " << *ex
<< " from " << ex
->get_source() << dendl
;
3239 CInode
*in
= cache
->get_inode(ex
->ino
);
3242 assert(in
->is_auth());
3245 if (in
->is_frozen())
3248 C_M_LoggedImportCaps
*finish
= new C_M_LoggedImportCaps(
3249 this, in
, mds_rank_t(ex
->get_source().num()));
3250 finish
->client_map
= ex
->client_map
;
3253 bufferlist::iterator blp
= ex
->cap_bl
.begin();
3254 decode_import_inode_caps(in
, false, blp
, finish
->peer_exports
);
3255 assert(!finish
->peer_exports
.empty()); // thus, inode is pinned.
3257 // journal open client sessions
3258 version_t pv
= mds
->server
->prepare_force_open_sessions(finish
->client_map
, finish
->sseqmap
);
3260 ESessions
*le
= new ESessions(pv
, ex
->client_map
);
3261 mds
->mdlog
->start_submit_entry(le
, finish
);
3262 mds
->mdlog
->flush();
3268 void Migrator::logged_import_caps(CInode
*in
,
3270 map
<CInode
*, map
<client_t
,Capability::Export
> >& peer_exports
,
3271 map
<client_t
,entity_inst_t
>& client_map
,
3272 map
<client_t
,uint64_t>& sseqmap
)
3274 dout(10) << "logged_import_caps on " << *in
<< dendl
;
3275 // see export_go() vs export_go_synced()
3276 assert(in
->is_auth());
3278 // force open client sessions and finish cap import
3279 mds
->server
->finish_force_open_sessions(client_map
, sseqmap
);
3281 map
<client_t
,Capability::Import
> imported_caps
;
3283 assert(peer_exports
.count(in
));
3284 // clients will release caps from the exporter when they receive the cap import message.
3285 finish_import_inode_caps(in
, from
, false, peer_exports
[in
], imported_caps
);
3286 mds
->locker
->eval(in
, CEPH_CAP_LOCKS
, true);