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
, it
->second
, bounds
);
325 // process delayed expires
326 cache
->process_delayed_expire(dir
);
329 dir
->unfreeze_tree();
330 cache
->try_subtree_merge(dir
);
331 for (auto bd
: it
->second
.residual_dirs
) {
333 cache
->try_subtree_merge(bd
);
336 (!mds
->is_cluster_degraded() ||
337 mds
->mdsmap
->is_clientreplay_or_active_or_stopping(it
->second
.peer
))) // tell them.
338 mds
->send_message_mds(new MExportDirCancel(dir
->dirfrag(), it
->second
.tid
), it
->second
.peer
);
341 case EXPORT_EXPORTING
:
342 dout(10) << "export state=exporting : reversing, and unfreezing" << dendl
;
343 it
->second
.state
= EXPORT_CANCELLING
;
344 export_reverse(dir
, it
->second
);
347 case EXPORT_LOGGINGFINISH
:
348 case EXPORT_NOTIFYING
:
349 dout(10) << "export state=loggingfinish|notifying : ignoring dest failure, we were successful." << dendl
;
350 // leave export_state, don't clean up now.
352 case EXPORT_CANCELLING
:
360 if (it
->second
.state
== EXPORT_CANCELLING
||
361 it
->second
.state
== EXPORT_CANCELLED
) {
363 mut
.swap(it
->second
.mut
);
365 if (it
->second
.state
== EXPORT_CANCELLED
) {
366 export_state
.erase(it
);
367 dir
->state_clear(CDir::STATE_EXPORTING
);
368 // send pending import_maps?
369 cache
->maybe_send_pending_resolves();
373 if (state
== EXPORT_LOCKING
|| state
== EXPORT_DISCOVERING
) {
374 MDRequestRef mdr
= static_cast<MDRequestImpl
*>(mut
.get());
376 if (mdr
->more()->waiting_on_slave
.empty())
377 mds
->mdcache
->request_finish(mdr
);
379 mds
->locker
->drop_locks(mut
.get());
383 cache
->show_subtrees();
385 maybe_do_queued_export();
389 void Migrator::export_cancel_finish(CDir
*dir
)
391 assert(dir
->state_test(CDir::STATE_EXPORTING
));
392 dir
->state_clear(CDir::STATE_EXPORTING
);
394 // pinned by Migrator::export_notify_abort()
395 dir
->auth_unpin(this);
396 // send pending import_maps? (these need to go out when all exports have finished.)
397 cache
->maybe_send_pending_resolves();
400 // ==========================================================
401 // mds failure handling
403 void Migrator::handle_mds_failure_or_stop(mds_rank_t who
)
405 dout(5) << "handle_mds_failure_or_stop mds." << who
<< dendl
;
409 // first add an extra auth_pin on any freezes, so that canceling a
410 // nested freeze doesn't complete one further up the hierarchy and
411 // confuse the shit out of us. we'll remove it after canceling the
412 // freeze. this way no freeze completions run before we want them
414 list
<CDir
*> pinned_dirs
;
415 for (map
<CDir
*,export_state_t
>::iterator p
= export_state
.begin();
416 p
!= export_state
.end();
418 if (p
->second
.state
== EXPORT_FREEZING
) {
419 CDir
*dir
= p
->first
;
420 dout(10) << "adding temp auth_pin on freezing " << *dir
<< dendl
;
422 pinned_dirs
.push_back(dir
);
426 map
<CDir
*,export_state_t
>::iterator p
= export_state
.begin();
427 while (p
!= export_state
.end()) {
428 map
<CDir
*,export_state_t
>::iterator next
= p
;
430 CDir
*dir
= p
->first
;
433 // - that are going to the failed node
434 // - that aren't frozen yet (to avoid auth_pin deadlock)
435 // - they havne't prepped yet (they may need to discover bounds to do that)
436 if ((p
->second
.peer
== who
&&
437 p
->second
.state
!= EXPORT_CANCELLING
) ||
438 p
->second
.state
== EXPORT_LOCKING
||
439 p
->second
.state
== EXPORT_DISCOVERING
||
440 p
->second
.state
== EXPORT_FREEZING
||
441 p
->second
.state
== EXPORT_PREPPING
) {
442 // the guy i'm exporting to failed, or we're just freezing.
443 dout(10) << "cleaning up export state (" << p
->second
.state
<< ")"
444 << get_export_statename(p
->second
.state
) << " of " << *dir
<< dendl
;
445 export_try_cancel(dir
);
446 } else if (p
->second
.peer
!= who
) {
448 if (p
->second
.warning_ack_waiting
.erase(who
)) {
449 if (p
->second
.state
== EXPORT_WARNING
) {
450 p
->second
.notify_ack_waiting
.erase(who
); // they won't get a notify either.
451 // exporter waiting for warning acks, let's fake theirs.
452 dout(10) << "faking export_warning_ack from mds." << who
453 << " on " << *dir
<< " to mds." << p
->second
.peer
455 if (p
->second
.warning_ack_waiting
.empty())
459 if (p
->second
.notify_ack_waiting
.erase(who
)) {
460 // exporter is waiting for notify acks, fake it
461 dout(10) << "faking export_notify_ack from mds." << who
462 << " on " << *dir
<< " to mds." << p
->second
.peer
464 if (p
->second
.state
== EXPORT_NOTIFYING
) {
465 if (p
->second
.notify_ack_waiting
.empty())
467 } else if (p
->second
.state
== EXPORT_CANCELLING
) {
468 if (p
->second
.notify_ack_waiting
.empty()) {
469 export_state
.erase(p
);
470 export_cancel_finish(dir
);
482 map
<dirfrag_t
,import_state_t
>::iterator q
= import_state
.begin();
483 while (q
!= import_state
.end()) {
484 map
<dirfrag_t
,import_state_t
>::iterator next
= q
;
486 dirfrag_t df
= q
->first
;
487 CInode
*diri
= mds
->mdcache
->get_inode(df
.ino
);
488 CDir
*dir
= mds
->mdcache
->get_dirfrag(df
);
490 if (q
->second
.peer
== who
) {
492 dout(10) << "cleaning up import state (" << q
->second
.state
<< ")"
493 << get_import_statename(q
->second
.state
) << " of " << *dir
<< dendl
;
495 dout(10) << "cleaning up import state (" << q
->second
.state
<< ")"
496 << get_import_statename(q
->second
.state
) << " of " << df
<< dendl
;
498 switch (q
->second
.state
) {
499 case IMPORT_DISCOVERING
:
500 dout(10) << "import state=discovering : clearing state" << dendl
;
501 import_reverse_discovering(df
);
504 case IMPORT_DISCOVERED
:
506 dout(10) << "import state=discovered : unpinning inode " << *diri
<< dendl
;
507 import_reverse_discovered(df
, diri
);
510 case IMPORT_PREPPING
:
512 dout(10) << "import state=prepping : unpinning base+bounds " << *dir
<< dendl
;
513 import_reverse_prepping(dir
, q
->second
);
518 dout(10) << "import state=prepped : unpinning base+bounds, unfreezing " << *dir
<< dendl
;
521 cache
->get_subtree_bounds(dir
, bounds
);
522 import_remove_pins(dir
, bounds
);
524 // adjust auth back to the exporter
525 cache
->adjust_subtree_auth(dir
, q
->second
.peer
);
527 // notify bystanders ; wait in aborting state
528 q
->second
.state
= IMPORT_ABORTING
;
529 import_notify_abort(dir
, bounds
);
530 assert(g_conf
->mds_kill_import_at
!= 10);
534 case IMPORT_LOGGINGSTART
:
536 dout(10) << "import state=loggingstart : reversing import on " << *dir
<< dendl
;
542 // hrm. make this an ambiguous import, and wait for exporter recovery to disambiguate
543 dout(10) << "import state=acking : noting ambiguous import " << *dir
<< dendl
;
546 cache
->get_subtree_bounds(dir
, bounds
);
547 cache
->add_ambiguous_import(dir
, bounds
);
551 case IMPORT_FINISHING
:
553 dout(10) << "import state=finishing : finishing import on " << *dir
<< dendl
;
554 import_finish(dir
, true);
557 case IMPORT_ABORTING
:
559 dout(10) << "import state=aborting : ignoring repeat failure " << *dir
<< dendl
;
563 auto bystanders_entry
= q
->second
.bystanders
.find(who
);
564 if (bystanders_entry
!= q
->second
.bystanders
.end()) {
565 q
->second
.bystanders
.erase(bystanders_entry
);
566 if (q
->second
.state
== IMPORT_ABORTING
) {
568 dout(10) << "faking export_notify_ack from mds." << who
569 << " on aborting import " << *dir
<< " from mds." << q
->second
.peer
571 if (q
->second
.bystanders
.empty())
572 import_reverse_unfreeze(dir
);
581 while (!pinned_dirs
.empty()) {
582 CDir
*dir
= pinned_dirs
.front();
583 dout(10) << "removing temp auth_pin on " << *dir
<< dendl
;
584 dir
->auth_unpin(this);
585 pinned_dirs
.pop_front();
591 void Migrator::show_importing()
593 dout(10) << "show_importing" << dendl
;
594 for (map
<dirfrag_t
,import_state_t
>::iterator p
= import_state
.begin();
595 p
!= import_state
.end();
597 CDir
*dir
= mds
->mdcache
->get_dirfrag(p
->first
);
599 dout(10) << " importing from " << p
->second
.peer
600 << ": (" << p
->second
.state
<< ") " << get_import_statename(p
->second
.state
)
601 << " " << p
->first
<< " " << *dir
<< dendl
;
603 dout(10) << " importing from " << p
->second
.peer
604 << ": (" << p
->second
.state
<< ") " << get_import_statename(p
->second
.state
)
605 << " " << p
->first
<< dendl
;
610 void Migrator::show_exporting()
612 dout(10) << "show_exporting" << dendl
;
613 for (map
<CDir
*,export_state_t
>::iterator p
= export_state
.begin();
614 p
!= export_state
.end();
616 dout(10) << " exporting to " << p
->second
.peer
617 << ": (" << p
->second
.state
<< ") " << get_export_statename(p
->second
.state
)
618 << " " << p
->first
->dirfrag() << " " << *p
->first
<< dendl
;
623 void Migrator::audit()
625 if (!g_conf
->subsys
.should_gather(ceph_subsys_mds
, 5))
630 for (map
<dirfrag_t
,import_state_t
>::iterator p
= import_state
.begin();
631 p
!= import_state
.end();
633 if (p
->second
.state
== IMPORT_DISCOVERING
)
635 if (p
->second
.state
== IMPORT_DISCOVERED
) {
636 CInode
*in
= cache
->get_inode(p
->first
.ino
);
640 CDir
*dir
= cache
->get_dirfrag(p
->first
);
642 if (p
->second
.state
== IMPORT_PREPPING
)
644 if (p
->second
.state
== IMPORT_ABORTING
) {
645 assert(!dir
->is_ambiguous_dir_auth());
646 assert(dir
->get_dir_auth().first
!= mds
->get_nodeid());
649 assert(dir
->is_ambiguous_dir_auth());
650 assert(dir
->authority().first
== mds
->get_nodeid() ||
651 dir
->authority().second
== mds
->get_nodeid());
656 for (map
<CDir
*,export_state_t
>::iterator p
= export_state
.begin();
657 p
!= export_state
.end();
659 CDir
*dir
= p
->first
;
660 if (p
->second
.state
== EXPORT_LOCKING
||
661 p
->second
.state
== EXPORT_DISCOVERING
||
662 p
->second
.state
== EXPORT_FREEZING
||
663 p
->second
.state
== EXPORT_CANCELLING
)
665 assert(dir
->is_ambiguous_dir_auth());
666 assert(dir
->authority().first
== mds
->get_nodeid() ||
667 dir
->authority().second
== mds
->get_nodeid());
670 // ambiguous+me subtrees should be importing|exporting
679 // ==========================================================
682 void Migrator::export_dir_nicely(CDir
*dir
, mds_rank_t dest
)
685 dout(7) << "export_dir_nicely " << *dir
<< " to " << dest
<< dendl
;
686 export_queue
.push_back(pair
<dirfrag_t
,mds_rank_t
>(dir
->dirfrag(), dest
));
688 maybe_do_queued_export();
691 void Migrator::maybe_do_queued_export()
697 while (!export_queue
.empty() &&
698 export_state
.size() <= 4) {
699 dirfrag_t df
= export_queue
.front().first
;
700 mds_rank_t dest
= export_queue
.front().second
;
701 export_queue
.pop_front();
703 CDir
*dir
= mds
->mdcache
->get_dirfrag(df
);
705 if (!dir
->is_auth()) continue;
707 dout(0) << "nicely exporting to mds." << dest
<< " " << *dir
<< dendl
;
709 export_dir(dir
, dest
);
717 class C_MDC_ExportFreeze
: public MigratorContext
{
718 CDir
*ex
; // dir i'm exporting
721 C_MDC_ExportFreeze(Migrator
*m
, CDir
*e
, uint64_t t
) :
722 MigratorContext(m
), ex(e
), tid(t
) {
725 void finish(int r
) override
{
727 mig
->export_frozen(ex
, tid
);
732 void Migrator::get_export_lock_set(CDir
*dir
, set
<SimpleLock
*>& locks
)
735 vector
<CDentry
*> trace
;
736 cache
->make_trace(trace
, dir
->inode
);
737 for (vector
<CDentry
*>::iterator it
= trace
.begin();
740 locks
.insert(&(*it
)->lock
);
742 // prevent scatter gather race
743 locks
.insert(&dir
->get_inode()->dirfragtreelock
);
746 // NOTE: We need to take an rdlock on bounding dirfrags during
747 // migration for a rather irritating reason: when we export the
748 // bound inode, we need to send scatterlock state for the dirfrags
749 // as well, so that the new auth also gets the correct info. If we
750 // race with a refragment, this info is useless, as we can't
751 // redivvy it up. And it's needed for the scatterlocks to work
752 // properly: when the auth is in a sync/lock state it keeps each
753 // dirfrag's portion in the local (auth OR replica) dirfrag.
754 set
<CDir
*> wouldbe_bounds
;
755 cache
->get_wouldbe_subtree_bounds(dir
, wouldbe_bounds
);
756 for (set
<CDir
*>::iterator p
= wouldbe_bounds
.begin(); p
!= wouldbe_bounds
.end(); ++p
)
757 locks
.insert(&(*p
)->get_inode()->dirfragtreelock
);
761 class C_M_ExportDirWait
: public MigratorContext
{
765 C_M_ExportDirWait(Migrator
*m
, MDRequestRef mdr
, int count
)
766 : MigratorContext(m
), mdr(mdr
), count(count
) {}
767 void finish(int r
) override
{
768 mig
->dispatch_export_dir(mdr
, count
);
773 /** export_dir(dir, dest)
774 * public method to initiate an export.
775 * will fail if the directory is freezing, frozen, unpinnable, or root.
777 void Migrator::export_dir(CDir
*dir
, mds_rank_t dest
)
779 dout(7) << "export_dir " << *dir
<< " to " << dest
<< dendl
;
780 assert(dir
->is_auth());
781 assert(dest
!= mds
->get_nodeid());
783 if (!(mds
->is_active() || mds
->is_stopping())) {
784 dout(7) << "i'm not active, no exports for now" << dendl
;
787 if (mds
->mdcache
->is_readonly()) {
788 dout(7) << "read-only FS, no exports for now" << dendl
;
791 if (!mds
->mdsmap
->is_active(dest
)) {
792 dout(7) << "dest not active, no exports for now" << dendl
;
795 if (mds
->is_cluster_degraded()) {
796 dout(7) << "cluster degraded, no exports for now" << dendl
;
799 if (dir
->inode
->is_system()) {
800 dout(7) << "i won't export system dirs (root, mdsdirs, stray, /.ceph, etc.)" << dendl
;
805 CDir
* parent_dir
= dir
->inode
->get_projected_parent_dir();
806 if (parent_dir
&& parent_dir
->inode
->is_stray()) {
807 if (parent_dir
->get_parent_dir()->ino() != MDS_INO_MDSDIR(dest
)) {
808 dout(7) << "i won't export anything in stray" << dendl
;
812 if (!mds
->is_stopping() && !dir
->inode
->is_exportable(dest
)) {
813 dout(7) << "dir is export pinned" << dendl
;
818 if (dir
->is_frozen() ||
819 dir
->is_freezing()) {
820 dout(7) << " can't export, freezing|frozen. wait for other exports to finish first." << dendl
;
823 if (dir
->state_test(CDir::STATE_EXPORTING
)) {
824 dout(7) << "already exporting" << dendl
;
828 if (g_conf
->mds_thrash_exports
) {
829 // create random subtree bound (which will not be exported)
831 for (auto p
= dir
->begin(); p
!= dir
->end(); ++p
) {
833 CDentry::linkage_t
*dnl
= dn
->get_linkage();
834 if (dnl
->is_primary()) {
835 CInode
*in
= dnl
->get_inode();
837 in
->get_nested_dirfrags(ls
);
841 int n
= rand() % ls
.size();
845 if (!(bd
->is_frozen() || bd
->is_freezing())) {
846 assert(bd
->is_auth());
847 dir
->state_set(CDir::STATE_AUXSUBTREE
);
848 mds
->mdcache
->adjust_subtree_auth(dir
, mds
->get_nodeid());
849 dout(0) << "export_dir: create aux subtree " << *bd
<< " under " << *dir
<< dendl
;
854 mds
->hit_export_target(ceph_clock_now(), dest
, -1);
857 dir
->state_set(CDir::STATE_EXPORTING
);
859 MDRequestRef mdr
= mds
->mdcache
->request_start_internal(CEPH_MDS_OP_EXPORTDIR
);
860 mdr
->more()->export_dir
= dir
;
862 assert(export_state
.count(dir
) == 0);
863 export_state_t
& stat
= export_state
[dir
];
864 stat
.state
= EXPORT_LOCKING
;
866 stat
.tid
= mdr
->reqid
.tid
;
869 return mds
->mdcache
->dispatch_request(mdr
);
872 void Migrator::dispatch_export_dir(MDRequestRef
& mdr
, int count
)
874 dout(7) << "dispatch_export_dir " << *mdr
<< dendl
;
876 CDir
*dir
= mdr
->more()->export_dir
;
877 map
<CDir
*,export_state_t
>::iterator it
= export_state
.find(dir
);
878 if (it
== export_state
.end() || it
->second
.tid
!= mdr
->reqid
.tid
) {
879 // export must have aborted.
880 dout(7) << "export must have aborted " << *mdr
<< dendl
;
881 mds
->mdcache
->request_finish(mdr
);
884 assert(it
->second
.state
== EXPORT_LOCKING
);
886 mds_rank_t dest
= it
->second
.peer
;
888 if (!mds
->is_export_target(dest
)) {
889 dout(7) << "dest is not yet an export target" << dendl
;
891 dout(5) << "dest has not been added as export target after three MDSMap epochs, canceling export" << dendl
;
892 export_try_cancel(dir
);
896 mds
->locker
->drop_locks(mdr
.get());
897 mdr
->drop_local_auth_pins();
899 mds
->wait_for_mdsmap(mds
->mdsmap
->get_epoch(), new C_M_ExportDirWait(this, mdr
, count
+1));
903 if (!dir
->inode
->get_parent_dn()) {
904 dout(7) << "waiting for dir to become stable before export: " << *dir
<< dendl
;
905 dir
->add_waiter(CDir::WAIT_CREATED
, new C_M_ExportDirWait(this, mdr
, 1));
909 if (mdr
->aborted
|| dir
->is_frozen() || dir
->is_freezing()) {
910 dout(7) << "wouldblock|freezing|frozen, canceling export" << dendl
;
911 export_try_cancel(dir
);
916 set
<SimpleLock
*> rdlocks
;
917 set
<SimpleLock
*> xlocks
;
918 set
<SimpleLock
*> wrlocks
;
919 get_export_lock_set(dir
, rdlocks
);
920 // If auth MDS of the subtree root inode is neither the exporter MDS
921 // nor the importer MDS and it gathers subtree root's fragstat/neststat
922 // while the subtree is exporting. It's possible that the exporter MDS
923 // and the importer MDS both are auth MDS of the subtree root or both
924 // are not auth MDS of the subtree root at the time they receive the
925 // lock messages. So the auth MDS of the subtree root inode may get no
926 // or duplicated fragstat/neststat for the subtree root dirfrag.
927 wrlocks
.insert(&dir
->get_inode()->filelock
);
928 wrlocks
.insert(&dir
->get_inode()->nestlock
);
929 if (dir
->get_inode()->is_auth()) {
930 dir
->get_inode()->filelock
.set_scatter_wanted();
931 dir
->get_inode()->nestlock
.set_scatter_wanted();
934 if (!mds
->locker
->acquire_locks(mdr
, rdlocks
, wrlocks
, xlocks
, NULL
, NULL
, true)) {
936 export_try_cancel(dir
);
940 assert(g_conf
->mds_kill_export_at
!= 1);
941 it
->second
.state
= EXPORT_DISCOVERING
;
943 // send ExportDirDiscover (ask target)
945 dir
->inode
->make_path(path
);
946 MExportDirDiscover
*discover
= new MExportDirDiscover(dir
->dirfrag(), path
,
949 mds
->send_message_mds(discover
, dest
);
950 assert(g_conf
->mds_kill_export_at
!= 2);
952 it
->second
.last_cum_auth_pins_change
= ceph_clock_now();
954 // start the freeze, but hold it up with an auth_pin.
956 assert(dir
->is_freezing_tree());
957 dir
->add_waiter(CDir::WAIT_FROZEN
, new C_MDC_ExportFreeze(this, dir
, it
->second
.tid
));
961 * called on receipt of MExportDirDiscoverAck
962 * the importer now has the directory's _inode_ in memory, and pinned.
964 * This function DOES put the passed message before returning
966 void Migrator::handle_export_discover_ack(MExportDirDiscoverAck
*m
)
968 CDir
*dir
= cache
->get_dirfrag(m
->get_dirfrag());
969 mds_rank_t
dest(m
->get_source().num());
970 utime_t now
= ceph_clock_now();
973 dout(7) << "export_discover_ack from " << m
->get_source()
974 << " on " << *dir
<< dendl
;
976 mds
->hit_export_target(now
, dest
, -1);
978 map
<CDir
*,export_state_t
>::iterator it
= export_state
.find(dir
);
979 if (it
== export_state
.end() ||
980 it
->second
.tid
!= m
->get_tid() ||
981 it
->second
.peer
!= dest
) {
982 dout(7) << "must have aborted" << dendl
;
984 assert(it
->second
.state
== EXPORT_DISCOVERING
);
986 if (m
->is_success()) {
987 // release locks to avoid deadlock
988 MDRequestRef mdr
= static_cast<MDRequestImpl
*>(it
->second
.mut
.get());
990 mds
->mdcache
->request_finish(mdr
);
991 it
->second
.mut
.reset();
992 // freeze the subtree
993 it
->second
.state
= EXPORT_FREEZING
;
994 dir
->auth_unpin(this);
995 assert(g_conf
->mds_kill_export_at
!= 3);
998 dout(7) << "peer failed to discover (not active?), canceling" << dendl
;
999 export_try_cancel(dir
, false);
1006 class C_M_ExportSessionsFlushed
: public MigratorContext
{
1010 C_M_ExportSessionsFlushed(Migrator
*m
, CDir
*d
, uint64_t t
)
1011 : MigratorContext(m
), dir(d
), tid(t
) {
1012 assert(dir
!= NULL
);
1014 void finish(int r
) override
{
1015 mig
->export_sessions_flushed(dir
, tid
);
1019 void Migrator::export_sessions_flushed(CDir
*dir
, uint64_t tid
)
1021 dout(7) << "export_sessions_flushed " << *dir
<< dendl
;
1023 map
<CDir
*,export_state_t
>::iterator it
= export_state
.find(dir
);
1024 if (it
== export_state
.end() ||
1025 it
->second
.state
== EXPORT_CANCELLING
||
1026 it
->second
.tid
!= tid
) {
1027 // export must have aborted.
1028 dout(7) << "export must have aborted on " << dir
<< dendl
;
1032 assert(it
->second
.state
== EXPORT_PREPPING
|| it
->second
.state
== EXPORT_WARNING
);
1033 assert(it
->second
.warning_ack_waiting
.count(MDS_RANK_NONE
) > 0);
1034 it
->second
.warning_ack_waiting
.erase(MDS_RANK_NONE
);
1035 if (it
->second
.state
== EXPORT_WARNING
&& it
->second
.warning_ack_waiting
.empty())
1036 export_go(dir
); // start export.
1039 void Migrator::export_frozen(CDir
*dir
, uint64_t tid
)
1041 dout(7) << "export_frozen on " << *dir
<< dendl
;
1043 map
<CDir
*,export_state_t
>::iterator it
= export_state
.find(dir
);
1044 if (it
== export_state
.end() || it
->second
.tid
!= tid
) {
1045 dout(7) << "export must have aborted" << dendl
;
1049 assert(it
->second
.state
== EXPORT_FREEZING
);
1050 assert(dir
->is_frozen_tree_root());
1051 assert(dir
->get_cum_auth_pins() == 0);
1053 CInode
*diri
= dir
->get_inode();
1055 // ok, try to grab all my locks.
1056 set
<SimpleLock
*> rdlocks
;
1057 get_export_lock_set(dir
, rdlocks
);
1058 if ((diri
->is_auth() && diri
->is_frozen()) ||
1059 !mds
->locker
->can_rdlock_set(rdlocks
) ||
1060 !diri
->filelock
.can_wrlock(-1) ||
1061 !diri
->nestlock
.can_wrlock(-1)) {
1062 dout(7) << "export_dir couldn't acquire all needed locks, failing. "
1065 dir
->unfreeze_tree();
1066 cache
->try_subtree_merge(dir
);
1068 mds
->send_message_mds(new MExportDirCancel(dir
->dirfrag(), it
->second
.tid
), it
->second
.peer
);
1069 export_state
.erase(it
);
1071 dir
->state_clear(CDir::STATE_EXPORTING
);
1072 cache
->maybe_send_pending_resolves();
1076 it
->second
.mut
= new MutationImpl();
1077 if (diri
->is_auth())
1078 it
->second
.mut
->auth_pin(diri
);
1079 mds
->locker
->rdlock_take_set(rdlocks
, it
->second
.mut
);
1080 mds
->locker
->wrlock_force(&diri
->filelock
, it
->second
.mut
);
1081 mds
->locker
->wrlock_force(&diri
->nestlock
, it
->second
.mut
);
1083 cache
->show_subtrees();
1085 // CDir::_freeze_tree() should have forced it into subtree.
1086 assert(dir
->get_dir_auth() == mds_authority_t(mds
->get_nodeid(), mds
->get_nodeid()));
1088 set
<client_t
> export_client_set
;
1089 check_export_size(dir
, it
->second
, export_client_set
);
1093 cache
->get_subtree_bounds(dir
, bounds
);
1095 // generate prep message, log entry.
1096 MExportDirPrep
*prep
= new MExportDirPrep(dir
->dirfrag(), it
->second
.tid
);
1098 // include list of bystanders
1099 for (const auto &p
: dir
->get_replicas()) {
1100 if (p
.first
!= it
->second
.peer
) {
1101 dout(10) << "bystander mds." << p
.first
<< dendl
;
1102 prep
->add_bystander(p
.first
);
1106 // include base dirfrag
1107 cache
->replicate_dir(dir
, it
->second
.peer
, prep
->basedir
);
1110 * include spanning tree for all nested exports.
1111 * these need to be on the destination _before_ the final export so that
1112 * dir_auth updates on any nested exports are properly absorbed.
1113 * this includes inodes and dirfrags included in the subtree, but
1114 * only the inodes at the bounds.
1116 * each trace is: df ('-' | ('f' dir | 'd') dentry inode (dir dentry inode)*)
1118 set
<inodeno_t
> inodes_added
;
1119 set
<dirfrag_t
> dirfrags_added
;
1122 for (set
<CDir
*>::iterator p
= bounds
.begin();
1128 assert(bound
->state_test(CDir::STATE_EXPORTBOUND
));
1130 dout(7) << " export bound " << *bound
<< dendl
;
1131 prep
->add_bound( bound
->dirfrag() );
1138 if (it
->second
.residual_dirs
.count(bound
)) {
1140 cache
->replicate_dir(bound
, it
->second
.peer
, tracebl
);
1141 dout(7) << " added " << *bound
<< dendl
;
1145 // don't repeat inodes
1146 if (inodes_added
.count(cur
->inode
->ino()))
1148 inodes_added
.insert(cur
->inode
->ino());
1150 // prepend dentry + inode
1151 assert(cur
->inode
->is_auth());
1153 cache
->replicate_dentry(cur
->inode
->parent
, it
->second
.peer
, bl
);
1154 dout(7) << " added " << *cur
->inode
->parent
<< dendl
;
1155 cache
->replicate_inode(cur
->inode
, it
->second
.peer
, bl
,
1156 mds
->mdsmap
->get_up_features());
1157 dout(7) << " added " << *cur
->inode
<< dendl
;
1158 bl
.claim_append(tracebl
);
1161 cur
= cur
->get_parent_dir();
1163 // don't repeat dirfrags
1164 if (dirfrags_added
.count(cur
->dirfrag()) ||
1166 start
= 'd'; // start with dentry
1169 dirfrags_added
.insert(cur
->dirfrag());
1172 cache
->replicate_dir(cur
, it
->second
.peer
, bl
);
1173 dout(7) << " added " << *cur
<< dendl
;
1174 bl
.claim_append(tracebl
);
1177 start
= 'f'; // start with dirfrag
1179 bufferlist final_bl
;
1180 dirfrag_t df
= cur
->dirfrag();
1181 ::encode(df
, final_bl
);
1182 ::encode(start
, final_bl
);
1183 final_bl
.claim_append(tracebl
);
1184 prep
->add_trace(final_bl
);
1188 it
->second
.state
= EXPORT_PREPPING
;
1189 mds
->send_message_mds(prep
, it
->second
.peer
);
1190 assert (g_conf
->mds_kill_export_at
!= 4);
1192 // make sure any new instantiations of caps are flushed out
1193 assert(it
->second
.warning_ack_waiting
.empty());
1195 MDSGatherBuilder
gather(g_ceph_context
);
1196 mds
->server
->flush_client_sessions(export_client_set
, gather
);
1197 if (gather
.has_subs()) {
1198 it
->second
.warning_ack_waiting
.insert(MDS_RANK_NONE
);
1199 gather
.set_finisher(new C_M_ExportSessionsFlushed(this, dir
, it
->second
.tid
));
1204 void Migrator::check_export_size(CDir
*dir
, export_state_t
& stat
, set
<client_t
>& client_set
)
1206 const unsigned frag_size
= 800;
1207 const unsigned inode_size
= 1000;
1208 const unsigned cap_size
= 80;
1209 const unsigned link_size
= 10;
1210 const unsigned null_size
= 1;
1212 uint64_t max_size
= g_conf
->get_val
<uint64_t>("mds_max_export_size");
1213 uint64_t approx_size
= 0;
1217 while (!dfs
.empty()) {
1218 CDir
*dir
= dfs
.front();
1221 approx_size
+= frag_size
;
1222 for (CDir::map_t::iterator p
= dir
->begin(); p
!= dir
->end(); ++p
) {
1223 CDentry
*dn
= p
->second
;
1224 if (dn
->get_linkage()->is_null()) {
1225 approx_size
+= null_size
;
1228 if (dn
->get_linkage()->is_remote()) {
1229 approx_size
+= link_size
;
1233 approx_size
+= inode_size
;
1234 CInode
*in
= dn
->get_linkage()->get_inode();
1238 in
->get_dirfrags(ls
);
1240 if (q
->is_subtree_root()) {
1241 q
->state_set(CDir::STATE_EXPORTBOUND
);
1242 q
->get(CDir::PIN_EXPORTBOUND
);
1244 // include nested dirfrag
1245 assert(q
->get_dir_auth().first
== CDIR_AUTH_PARENT
);
1250 for (map
<client_t
, Capability
*>::iterator q
= in
->client_caps
.begin();
1251 q
!= in
->client_caps
.end();
1253 approx_size
+= cap_size
;
1254 client_set
.insert(q
->first
);
1258 if (approx_size
>= max_size
)
1262 while (!dfs
.empty()) {
1263 CDir
*dir
= dfs
.front();
1266 dout(7) << "check_export_size: creating bound " << *dir
<< dendl
;
1267 assert(dir
->is_auth());
1268 dir
->state_set(CDir::STATE_EXPORTBOUND
);
1269 dir
->get(CDir::PIN_EXPORTBOUND
);
1271 mds
->mdcache
->adjust_subtree_auth(dir
, mds
->get_nodeid());
1272 // Another choice here is finishing all WAIT_UNFREEZE contexts and keeping
1273 // the newly created subtree unfreeze.
1274 dir
->_freeze_tree();
1276 stat
.residual_dirs
.insert(dir
);
1280 void Migrator::get_export_client_set(CInode
*in
, set
<client_t
>& client_set
)
1282 for (map
<client_t
, Capability
*>::iterator q
= in
->client_caps
.begin();
1283 q
!= in
->client_caps
.end();
1285 client_set
.insert(q
->first
);
1288 /* This function DOES put the passed message before returning*/
1289 void Migrator::handle_export_prep_ack(MExportDirPrepAck
*m
)
1291 CDir
*dir
= cache
->get_dirfrag(m
->get_dirfrag());
1292 mds_rank_t
dest(m
->get_source().num());
1293 utime_t now
= ceph_clock_now();
1296 dout(7) << "export_prep_ack " << *dir
<< dendl
;
1298 mds
->hit_export_target(now
, dest
, -1);
1300 map
<CDir
*,export_state_t
>::iterator it
= export_state
.find(dir
);
1301 if (it
== export_state
.end() ||
1302 it
->second
.tid
!= m
->get_tid() ||
1303 it
->second
.peer
!= mds_rank_t(m
->get_source().num())) {
1304 // export must have aborted.
1305 dout(7) << "export must have aborted" << dendl
;
1309 assert(it
->second
.state
== EXPORT_PREPPING
);
1311 if (!m
->is_success()) {
1312 dout(7) << "peer couldn't acquire all needed locks or wasn't active, canceling" << dendl
;
1313 export_try_cancel(dir
, false);
1318 assert (g_conf
->mds_kill_export_at
!= 5);
1321 cache
->get_subtree_bounds(dir
, bounds
);
1323 assert(it
->second
.warning_ack_waiting
.empty() ||
1324 (it
->second
.warning_ack_waiting
.size() == 1 &&
1325 it
->second
.warning_ack_waiting
.count(MDS_RANK_NONE
) > 0));
1326 assert(it
->second
.notify_ack_waiting
.empty());
1328 for (const auto &p
: dir
->get_replicas()) {
1329 if (p
.first
== it
->second
.peer
) continue;
1330 if (mds
->is_cluster_degraded() &&
1331 !mds
->mdsmap
->is_clientreplay_or_active_or_stopping(p
.first
))
1332 continue; // only if active
1333 it
->second
.warning_ack_waiting
.insert(p
.first
);
1334 it
->second
.notify_ack_waiting
.insert(p
.first
); // we'll eventually get a notifyack, too!
1336 MExportDirNotify
*notify
= new MExportDirNotify(dir
->dirfrag(), it
->second
.tid
, true,
1337 mds_authority_t(mds
->get_nodeid(),CDIR_AUTH_UNKNOWN
),
1338 mds_authority_t(mds
->get_nodeid(),it
->second
.peer
));
1339 for (set
<CDir
*>::iterator q
= bounds
.begin(); q
!= bounds
.end(); ++q
)
1340 notify
->get_bounds().push_back((*q
)->dirfrag());
1341 mds
->send_message_mds(notify
, p
.first
);
1345 it
->second
.state
= EXPORT_WARNING
;
1347 assert(g_conf
->mds_kill_export_at
!= 6);
1349 if (it
->second
.warning_ack_waiting
.empty())
1350 export_go(dir
); // start export.
1357 class C_M_ExportGo
: public MigratorContext
{
1361 C_M_ExportGo(Migrator
*m
, CDir
*d
, uint64_t t
) :
1362 MigratorContext(m
), dir(d
), tid(t
) {
1363 assert(dir
!= NULL
);
1365 void finish(int r
) override
{
1366 mig
->export_go_synced(dir
, tid
);
1370 void Migrator::export_go(CDir
*dir
)
1372 auto it
= export_state
.find(dir
);
1373 assert(it
!= export_state
.end());
1374 dout(7) << "export_go " << *dir
<< " to " << it
->second
.peer
<< dendl
;
1376 // first sync log to flush out e.g. any cap imports
1377 mds
->mdlog
->wait_for_safe(new C_M_ExportGo(this, dir
, it
->second
.tid
));
1378 mds
->mdlog
->flush();
1381 void Migrator::export_go_synced(CDir
*dir
, uint64_t tid
)
1383 map
<CDir
*,export_state_t
>::iterator it
= export_state
.find(dir
);
1384 if (it
== export_state
.end() ||
1385 it
->second
.state
== EXPORT_CANCELLING
||
1386 it
->second
.tid
!= tid
) {
1387 // export must have aborted.
1388 dout(7) << "export must have aborted on " << dir
<< dendl
;
1391 assert(it
->second
.state
== EXPORT_WARNING
);
1392 mds_rank_t dest
= it
->second
.peer
;
1394 dout(7) << "export_go_synced " << *dir
<< " to " << dest
<< dendl
;
1396 cache
->show_subtrees();
1398 it
->second
.state
= EXPORT_EXPORTING
;
1399 assert(g_conf
->mds_kill_export_at
!= 7);
1401 assert(dir
->is_frozen_tree_root());
1402 assert(dir
->get_cum_auth_pins() == 0);
1404 // set ambiguous auth
1405 cache
->adjust_subtree_auth(dir
, mds
->get_nodeid(), dest
);
1407 // take away the popularity we're sending.
1408 utime_t now
= ceph_clock_now();
1409 mds
->balancer
->subtract_export(dir
, now
);
1411 // fill export message with cache data
1412 MExportDir
*req
= new MExportDir(dir
->dirfrag(), it
->second
.tid
);
1413 map
<client_t
,entity_inst_t
> exported_client_map
;
1414 uint64_t num_exported_inodes
= encode_export_dir(req
->export_data
,
1415 dir
, // recur start point
1416 exported_client_map
,
1418 ::encode(exported_client_map
, req
->client_map
,
1419 mds
->mdsmap
->get_up_features());
1421 // add bounds to message
1423 cache
->get_subtree_bounds(dir
, bounds
);
1424 for (set
<CDir
*>::iterator p
= bounds
.begin();
1427 req
->add_export((*p
)->dirfrag());
1430 mds
->send_message_mds(req
, dest
);
1431 assert(g_conf
->mds_kill_export_at
!= 8);
1433 mds
->hit_export_target(now
, dest
, num_exported_inodes
+1);
1436 if (mds
->logger
) mds
->logger
->inc(l_mds_exported
);
1437 if (mds
->logger
) mds
->logger
->inc(l_mds_exported_inodes
, num_exported_inodes
);
1439 cache
->show_subtrees();
1443 /** encode_export_inode
1444 * update our local state for this inode to export.
1445 * encode relevant state to be sent over the wire.
1446 * used by: encode_export_dir, file_rename (if foreign)
1448 * FIXME: the separation between CInode.encode_export and these methods
1449 * is pretty arbitrary and dumb.
1451 void Migrator::encode_export_inode(CInode
*in
, bufferlist
& enc_state
,
1452 map
<client_t
,entity_inst_t
>& exported_client_map
)
1454 dout(7) << "encode_export_inode " << *in
<< dendl
;
1455 assert(!in
->is_replica(mds
->get_nodeid()));
1458 if (!in
->is_replicated()) {
1459 in
->replicate_relax_locks();
1460 dout(20) << " did replicate_relax_locks, now " << *in
<< dendl
;
1463 ::encode(in
->inode
.ino
, enc_state
);
1464 ::encode(in
->last
, enc_state
);
1465 in
->encode_export(enc_state
);
1468 encode_export_inode_caps(in
, true, enc_state
, exported_client_map
);
1471 void Migrator::encode_export_inode_caps(CInode
*in
, bool auth_cap
, bufferlist
& bl
,
1472 map
<client_t
,entity_inst_t
>& exported_client_map
)
1474 dout(20) << "encode_export_inode_caps " << *in
<< dendl
;
1477 map
<client_t
,Capability::Export
> cap_map
;
1478 in
->export_client_caps(cap_map
);
1479 ::encode(cap_map
, bl
);
1481 ::encode(in
->get_mds_caps_wanted(), bl
);
1483 in
->state_set(CInode::STATE_EXPORTINGCAPS
);
1484 in
->get(CInode::PIN_EXPORTINGCAPS
);
1487 // make note of clients named by exported capabilities
1488 for (map
<client_t
, Capability
*>::iterator it
= in
->client_caps
.begin();
1489 it
!= in
->client_caps
.end();
1491 exported_client_map
[it
->first
] = mds
->sessionmap
.get_inst(entity_name_t::CLIENT(it
->first
.v
));
1494 void Migrator::finish_export_inode_caps(CInode
*in
, mds_rank_t peer
,
1495 map
<client_t
,Capability::Import
>& peer_imported
)
1497 dout(20) << "finish_export_inode_caps " << *in
<< dendl
;
1499 in
->state_clear(CInode::STATE_EXPORTINGCAPS
);
1500 in
->put(CInode::PIN_EXPORTINGCAPS
);
1502 // tell (all) clients about migrating caps..
1503 for (map
<client_t
, Capability
*>::iterator it
= in
->client_caps
.begin();
1504 it
!= in
->client_caps
.end();
1506 Capability
*cap
= it
->second
;
1507 dout(7) << "finish_export_inode_caps telling client." << it
->first
1508 << " exported caps on " << *in
<< dendl
;
1509 MClientCaps
*m
= new MClientCaps(CEPH_CAP_OP_EXPORT
, in
->ino(), 0,
1510 cap
->get_cap_id(), cap
->get_mseq(), mds
->get_osd_epoch_barrier());
1512 map
<client_t
,Capability::Import
>::iterator q
= peer_imported
.find(it
->first
);
1513 assert(q
!= peer_imported
.end());
1514 m
->set_cap_peer(q
->second
.cap_id
, q
->second
.issue_seq
, q
->second
.mseq
, peer
, 0);
1515 mds
->send_message_client_counted(m
, it
->first
);
1517 in
->clear_client_caps_after_export();
1518 mds
->locker
->eval(in
, CEPH_CAP_LOCKS
);
1521 void Migrator::finish_export_inode(CInode
*in
, utime_t now
, mds_rank_t peer
,
1522 map
<client_t
,Capability::Import
>& peer_imported
,
1523 list
<MDSInternalContextBase
*>& finished
)
1525 dout(12) << "finish_export_inode " << *in
<< dendl
;
1531 // clear/unpin cached_by (we're no longer the authority)
1532 in
->clear_replica_map();
1534 // twiddle lock states for auth -> replica transition
1535 in
->authlock
.export_twiddle();
1536 in
->linklock
.export_twiddle();
1537 in
->dirfragtreelock
.export_twiddle();
1538 in
->filelock
.export_twiddle();
1539 in
->nestlock
.export_twiddle();
1540 in
->xattrlock
.export_twiddle();
1541 in
->snaplock
.export_twiddle();
1542 in
->flocklock
.export_twiddle();
1543 in
->policylock
.export_twiddle();
1546 assert(in
->is_auth());
1547 in
->state_clear(CInode::STATE_AUTH
);
1548 in
->replica_nonce
= CInode::EXPORT_NONCE
;
1550 in
->clear_dirty_rstat();
1552 // no more auth subtree? clear scatter dirty
1553 if (!in
->has_subtree_root_dirfrag(mds
->get_nodeid()))
1554 in
->clear_scatter_dirty();
1556 in
->item_open_file
.remove_myself();
1558 in
->clear_dirty_parent();
1560 in
->clear_file_locks();
1563 in
->take_waiting(CInode::WAIT_ANY_MASK
, finished
);
1565 in
->finish_export(now
);
1567 finish_export_inode_caps(in
, peer
, peer_imported
);
1570 uint64_t Migrator::encode_export_dir(bufferlist
& exportbl
,
1572 map
<client_t
,entity_inst_t
>& exported_client_map
,
1575 uint64_t num_exported
= 0;
1577 dout(7) << "encode_export_dir " << *dir
<< " " << dir
->get_num_head_items() << " head items" << dendl
;
1579 assert(dir
->get_projected_version() == dir
->get_version());
1581 #ifdef MDS_VERIFY_FRAGSTAT
1582 if (dir
->is_complete())
1583 dir
->verify_fragstat();
1587 dirfrag_t df
= dir
->dirfrag();
1588 ::encode(df
, exportbl
);
1589 dir
->encode_export(exportbl
);
1591 __u32 nden
= dir
->items
.size();
1592 ::encode(nden
, exportbl
);
1595 list
<CDir
*> subdirs
;
1596 CDir::map_t::iterator it
;
1597 for (it
= dir
->begin(); it
!= dir
->end(); ++it
) {
1598 CDentry
*dn
= it
->second
;
1599 CInode
*in
= dn
->get_linkage()->get_inode();
1601 if (!dn
->is_replicated())
1602 dn
->lock
.replicate_relax();
1607 dout(7) << "encode_export_dir exporting " << *dn
<< dendl
;
1610 ::encode(dn
->name
, exportbl
);
1611 ::encode(dn
->last
, exportbl
);
1614 dn
->encode_export(exportbl
);
1619 if (dn
->get_linkage()->is_null()) {
1620 exportbl
.append("N", 1); // null dentry
1624 if (dn
->get_linkage()->is_remote()) {
1626 exportbl
.append("L", 1); // remote link
1628 inodeno_t ino
= dn
->get_linkage()->get_remote_ino();
1629 unsigned char d_type
= dn
->get_linkage()->get_remote_d_type();
1630 ::encode(ino
, exportbl
);
1631 ::encode(d_type
, exportbl
);
1637 exportbl
.append("I", 1); // inode dentry
1639 encode_export_inode(in
, exportbl
, exported_client_map
); // encode, and (update state for) export
1643 in
->get_dirfrags(dfs
);
1644 for (list
<CDir
*>::iterator p
= dfs
.begin(); p
!= dfs
.end(); ++p
) {
1646 if (!t
->state_test(CDir::STATE_EXPORTBOUND
)) {
1647 // include nested dirfrag
1648 assert(t
->get_dir_auth().first
== CDIR_AUTH_PARENT
);
1649 subdirs
.push_front(t
); // it's ours, recurse (later)
1655 for (list
<CDir
*>::iterator it
= subdirs
.begin(); it
!= subdirs
.end(); ++it
)
1656 num_exported
+= encode_export_dir(exportbl
, *it
, exported_client_map
, now
);
1658 return num_exported
;
1661 void Migrator::finish_export_dir(CDir
*dir
, utime_t now
, mds_rank_t peer
,
1662 map
<inodeno_t
,map
<client_t
,Capability::Import
> >& peer_imported
,
1663 list
<MDSInternalContextBase
*>& finished
, int *num_dentries
)
1665 dout(10) << "finish_export_dir " << *dir
<< dendl
;
1668 dir
->clear_replica_map();
1671 assert(dir
->is_auth());
1672 dir
->state_clear(CDir::STATE_AUTH
);
1673 dir
->remove_bloom();
1674 dir
->replica_nonce
= CDir::EXPORT_NONCE
;
1676 if (dir
->is_dirty())
1679 // suck up all waiters
1680 dir
->take_waiting(CDir::WAIT_ANY_MASK
, finished
); // all dir waiters
1683 dir
->finish_export(now
);
1686 list
<CDir
*> subdirs
;
1687 CDir::map_t::iterator it
;
1688 for (it
= dir
->begin(); it
!= dir
->end(); ++it
) {
1689 CDentry
*dn
= it
->second
;
1690 CInode
*in
= dn
->get_linkage()->get_inode();
1693 dn
->finish_export();
1696 if (dn
->get_linkage()->is_primary()) {
1697 finish_export_inode(in
, now
, peer
, peer_imported
[in
->ino()], finished
);
1700 in
->get_nested_dirfrags(subdirs
);
1703 cache
->touch_dentry_bottom(dn
); // move dentry to tail of LRU
1708 for (list
<CDir
*>::iterator it
= subdirs
.begin(); it
!= subdirs
.end(); ++it
)
1709 finish_export_dir(*it
, now
, peer
, peer_imported
, finished
, num_dentries
);
1712 class C_MDS_ExportFinishLogged
: public MigratorLogContext
{
1715 C_MDS_ExportFinishLogged(Migrator
*m
, CDir
*d
) : MigratorLogContext(m
), dir(d
) {}
1716 void finish(int r
) override
{
1717 mig
->export_logged_finish(dir
);
1723 * i should get an export_ack from the export target.
1725 * This function DOES put the passed message before returning
1727 void Migrator::handle_export_ack(MExportDirAck
*m
)
1729 CDir
*dir
= cache
->get_dirfrag(m
->get_dirfrag());
1730 mds_rank_t
dest(m
->get_source().num());
1731 utime_t now
= ceph_clock_now();
1733 assert(dir
->is_frozen_tree_root()); // i'm exporting!
1736 dout(7) << "handle_export_ack " << *dir
<< dendl
;
1738 mds
->hit_export_target(now
, dest
, -1);
1740 map
<CDir
*,export_state_t
>::iterator it
= export_state
.find(dir
);
1741 assert(it
!= export_state
.end());
1742 assert(it
->second
.state
== EXPORT_EXPORTING
);
1743 assert(it
->second
.tid
== m
->get_tid());
1745 bufferlist::iterator bp
= m
->imported_caps
.begin();
1746 ::decode(it
->second
.peer_imported
, bp
);
1748 it
->second
.state
= EXPORT_LOGGINGFINISH
;
1749 assert (g_conf
->mds_kill_export_at
!= 9);
1751 cache
->get_subtree_bounds(dir
, bounds
);
1754 // include export bounds, to ensure they're in the journal.
1755 EExport
*le
= new EExport(mds
->mdlog
, dir
, it
->second
.peer
);;
1756 mds
->mdlog
->start_entry(le
);
1758 le
->metablob
.add_dir_context(dir
, EMetaBlob::TO_ROOT
);
1759 le
->metablob
.add_dir(dir
, false);
1760 for (set
<CDir
*>::iterator p
= bounds
.begin();
1764 le
->get_bounds().insert(bound
->dirfrag());
1765 le
->metablob
.add_dir_context(bound
);
1766 le
->metablob
.add_dir(bound
, false);
1769 // list us second, them first.
1770 // this keeps authority().first in sync with subtree auth state in the journal.
1771 cache
->adjust_subtree_auth(dir
, it
->second
.peer
, mds
->get_nodeid());
1773 // log export completion, then finish (unfreeze, trigger finish context, etc.)
1774 mds
->mdlog
->submit_entry(le
, new C_MDS_ExportFinishLogged(this, dir
));
1775 mds
->mdlog
->flush();
1776 assert (g_conf
->mds_kill_export_at
!= 10);
1781 void Migrator::export_notify_abort(CDir
*dir
, export_state_t
& stat
, set
<CDir
*>& bounds
)
1783 dout(7) << "export_notify_abort " << *dir
<< dendl
;
1785 assert(stat
.state
== EXPORT_CANCELLING
);
1787 if (stat
.notify_ack_waiting
.empty()) {
1788 stat
.state
= EXPORT_CANCELLED
;
1792 dir
->auth_pin(this);
1794 for (set
<mds_rank_t
>::iterator p
= stat
.notify_ack_waiting
.begin();
1795 p
!= stat
.notify_ack_waiting
.end();
1797 MExportDirNotify
*notify
= new MExportDirNotify(dir
->dirfrag(), stat
.tid
, true,
1798 pair
<int,int>(mds
->get_nodeid(), stat
.peer
),
1799 pair
<int,int>(mds
->get_nodeid(), CDIR_AUTH_UNKNOWN
));
1800 for (set
<CDir
*>::iterator i
= bounds
.begin(); i
!= bounds
.end(); ++i
)
1801 notify
->get_bounds().push_back((*i
)->dirfrag());
1802 mds
->send_message_mds(notify
, *p
);
1807 * this happens if hte dest failes after i send teh export data but before it is acked
1808 * that is, we don't know they safely received and logged it, so we reverse our changes
1811 void Migrator::export_reverse(CDir
*dir
, export_state_t
& stat
)
1813 dout(7) << "export_reverse " << *dir
<< dendl
;
1815 set
<CInode
*> to_eval
;
1818 cache
->get_subtree_bounds(dir
, bounds
);
1820 // remove exporting pins
1823 while (!rq
.empty()) {
1824 CDir
*t
= rq
.front();
1827 for (CDir::map_t::iterator p
= t
->items
.begin(); p
!= t
->items
.end(); ++p
) {
1828 p
->second
->abort_export();
1829 if (!p
->second
->get_linkage()->is_primary())
1831 CInode
*in
= p
->second
->get_linkage()->get_inode();
1833 if (in
->state_test(CInode::STATE_EVALSTALECAPS
)) {
1834 in
->state_clear(CInode::STATE_EVALSTALECAPS
);
1838 in
->get_nested_dirfrags(rq
);
1843 for (auto bd
: bounds
) {
1844 bd
->put(CDir::PIN_EXPORTBOUND
);
1845 bd
->state_clear(CDir::STATE_EXPORTBOUND
);
1848 // notify bystanders
1849 export_notify_abort(dir
, stat
, bounds
);
1851 // unfreeze tree, with possible subtree merge.
1852 cache
->adjust_subtree_auth(dir
, mds
->get_nodeid(), mds
->get_nodeid());
1854 // process delayed expires
1855 cache
->process_delayed_expire(dir
);
1857 dir
->unfreeze_tree();
1858 cache
->try_subtree_merge(dir
);
1859 for (auto bd
: stat
.residual_dirs
) {
1860 bd
->unfreeze_tree();
1861 cache
->try_subtree_merge(bd
);
1864 // revoke/resume stale caps
1865 for (auto in
: to_eval
) {
1866 bool need_issue
= false;
1867 for (auto& p
: in
->get_client_caps()) {
1868 Capability
*cap
= p
.second
;
1869 if (cap
->is_stale()) {
1870 mds
->locker
->revoke_stale_caps(cap
);
1876 (!in
->is_auth() || !mds
->locker
->eval(in
, CEPH_CAP_LOCKS
)))
1877 mds
->locker
->issue_caps(in
);
1880 cache
->show_cache();
1885 * once i get the ack, and logged the EExportFinish(true),
1886 * send notifies (if any), otherwise go straight to finish.
1889 void Migrator::export_logged_finish(CDir
*dir
)
1891 dout(7) << "export_logged_finish " << *dir
<< dendl
;
1893 export_state_t
& stat
= export_state
[dir
];
1897 cache
->get_subtree_bounds(dir
, bounds
);
1899 for (set
<mds_rank_t
>::iterator p
= stat
.notify_ack_waiting
.begin();
1900 p
!= stat
.notify_ack_waiting
.end();
1902 MExportDirNotify
*notify
= new MExportDirNotify(dir
->dirfrag(), stat
.tid
, true,
1903 pair
<int,int>(mds
->get_nodeid(), stat
.peer
),
1904 pair
<int,int>(stat
.peer
, CDIR_AUTH_UNKNOWN
));
1906 for (set
<CDir
*>::iterator i
= bounds
.begin(); i
!= bounds
.end(); ++i
)
1907 notify
->get_bounds().push_back((*i
)->dirfrag());
1909 mds
->send_message_mds(notify
, *p
);
1912 // wait for notifyacks
1913 stat
.state
= EXPORT_NOTIFYING
;
1914 assert (g_conf
->mds_kill_export_at
!= 11);
1916 // no notifies to wait for?
1917 if (stat
.notify_ack_waiting
.empty()) {
1918 export_finish(dir
); // skip notify/notify_ack stage.
1920 // notify peer to send cap import messages to clients
1921 if (!mds
->is_cluster_degraded() ||
1922 mds
->mdsmap
->is_clientreplay_or_active_or_stopping(stat
.peer
)) {
1923 mds
->send_message_mds(new MExportDirFinish(dir
->dirfrag(), false, stat
.tid
), stat
.peer
);
1925 dout(7) << "not sending MExportDirFinish, dest has failed" << dendl
;
1932 * i'll get an ack from each bystander.
1933 * when i get them all, do the export.
1935 * i'll get an ack from each bystander.
1936 * when i get them all, unfreeze and send the finish.
1938 * This function DOES put the passed message before returning
1940 void Migrator::handle_export_notify_ack(MExportDirNotifyAck
*m
)
1942 CDir
*dir
= cache
->get_dirfrag(m
->get_dirfrag());
1943 mds_rank_t
dest(m
->get_source().num());
1944 utime_t now
= ceph_clock_now();
1946 mds_rank_t from
= mds_rank_t(m
->get_source().num());
1948 mds
->hit_export_target(now
, dest
, -1);
1950 auto export_state_entry
= export_state
.find(dir
);
1951 if (export_state_entry
!= export_state
.end()) {
1952 export_state_t
& stat
= export_state_entry
->second
;
1953 if (stat
.state
== EXPORT_WARNING
&&
1954 stat
.warning_ack_waiting
.erase(from
)) {
1955 // exporting. process warning.
1956 dout(7) << "handle_export_notify_ack from " << m
->get_source()
1957 << ": exporting, processing warning on " << *dir
<< dendl
;
1958 if (stat
.warning_ack_waiting
.empty())
1959 export_go(dir
); // start export.
1960 } else if (stat
.state
== EXPORT_NOTIFYING
&&
1961 stat
.notify_ack_waiting
.erase(from
)) {
1962 // exporting. process notify.
1963 dout(7) << "handle_export_notify_ack from " << m
->get_source()
1964 << ": exporting, processing notify on " << *dir
<< dendl
;
1965 if (stat
.notify_ack_waiting
.empty())
1967 } else if (stat
.state
== EXPORT_CANCELLING
&&
1968 m
->get_new_auth().second
== CDIR_AUTH_UNKNOWN
&& // not warning ack
1969 stat
.notify_ack_waiting
.erase(from
)) {
1970 dout(7) << "handle_export_notify_ack from " << m
->get_source()
1971 << ": cancelling export, processing notify on " << *dir
<< dendl
;
1972 if (stat
.notify_ack_waiting
.empty()) {
1973 export_state
.erase(export_state_entry
);
1974 export_cancel_finish(dir
);
1979 auto import_state_entry
= import_state
.find(dir
->dirfrag());
1980 if (import_state_entry
!= import_state
.end()) {
1981 import_state_t
& stat
= import_state_entry
->second
;
1982 if (stat
.state
== IMPORT_ABORTING
) {
1984 dout(7) << "handle_export_notify_ack from " << m
->get_source()
1985 << ": aborting import on " << *dir
<< dendl
;
1986 assert(stat
.bystanders
.count(from
));
1987 stat
.bystanders
.erase(from
);
1988 if (stat
.bystanders
.empty())
1989 import_reverse_unfreeze(dir
);
1997 void Migrator::export_finish(CDir
*dir
)
1999 dout(5) << "export_finish " << *dir
<< dendl
;
2001 assert (g_conf
->mds_kill_export_at
!= 12);
2002 map
<CDir
*,export_state_t
>::iterator it
= export_state
.find(dir
);
2003 if (it
== export_state
.end()) {
2004 dout(7) << "target must have failed, not sending final commit message. export succeeded anyway." << dendl
;
2008 // send finish/commit to new auth
2009 if (!mds
->is_cluster_degraded() ||
2010 mds
->mdsmap
->is_clientreplay_or_active_or_stopping(it
->second
.peer
)) {
2011 mds
->send_message_mds(new MExportDirFinish(dir
->dirfrag(), true, it
->second
.tid
), it
->second
.peer
);
2013 dout(7) << "not sending MExportDirFinish last, dest has failed" << dendl
;
2015 assert(g_conf
->mds_kill_export_at
!= 13);
2017 // finish export (adjust local cache state)
2018 int num_dentries
= 0;
2019 list
<MDSInternalContextBase
*> finished
;
2020 finish_export_dir(dir
, ceph_clock_now(), it
->second
.peer
,
2021 it
->second
.peer_imported
, finished
, &num_dentries
);
2023 assert(!dir
->is_auth());
2024 cache
->adjust_subtree_auth(dir
, it
->second
.peer
);
2028 cache
->get_subtree_bounds(dir
, bounds
);
2029 for (set
<CDir
*>::iterator p
= bounds
.begin();
2033 bd
->put(CDir::PIN_EXPORTBOUND
);
2034 bd
->state_clear(CDir::STATE_EXPORTBOUND
);
2037 if (dir
->state_test(CDir::STATE_AUXSUBTREE
))
2038 dir
->state_clear(CDir::STATE_AUXSUBTREE
);
2040 // discard delayed expires
2041 cache
->discard_delayed_expire(dir
);
2043 dout(7) << "export_finish unfreezing" << dendl
;
2045 // unfreeze tree, with possible subtree merge.
2046 // (we do this _after_ removing EXPORTBOUND pins, to allow merges)
2047 dir
->unfreeze_tree();
2048 cache
->try_subtree_merge(dir
);
2049 for (auto bd
: it
->second
.residual_dirs
) {
2050 export_queue
.push_front(pair
<dirfrag_t
,mds_rank_t
>(bd
->dirfrag(), it
->second
.peer
));
2051 bd
->take_waiting(CDir::WAIT_ANY_MASK
, finished
);
2052 bd
->unfreeze_tree();
2053 cache
->try_subtree_merge(bd
);
2056 // no more auth subtree? clear scatter dirty
2057 if (!dir
->get_inode()->is_auth() &&
2058 !dir
->get_inode()->has_subtree_root_dirfrag(mds
->get_nodeid())) {
2059 dir
->get_inode()->clear_scatter_dirty();
2060 // wake up scatter_nudge waiters
2061 dir
->get_inode()->take_waiting(CInode::WAIT_ANY_MASK
, finished
);
2064 if (!finished
.empty())
2065 mds
->queue_waiters(finished
);
2067 MutationRef mut
= it
->second
.mut
;
2068 // remove from exporting list, clean up state
2069 export_state
.erase(it
);
2070 dir
->state_clear(CDir::STATE_EXPORTING
);
2072 cache
->show_subtrees();
2075 cache
->trim(num_dentries
); // try trimming exported dentries
2077 // send pending import_maps?
2078 mds
->mdcache
->maybe_send_pending_resolves();
2080 // drop locks, unpin path
2082 mds
->locker
->drop_locks(mut
.get());
2086 maybe_do_queued_export();
2096 // ==========================================================
2099 void Migrator::handle_export_discover(MExportDirDiscover
*m
)
2101 mds_rank_t from
= m
->get_source_mds();
2102 assert(from
!= mds
->get_nodeid());
2104 dout(7) << "handle_export_discover on " << m
->get_path() << dendl
;
2106 // note import state
2107 dirfrag_t df
= m
->get_dirfrag();
2109 if (!mds
->is_active()) {
2110 dout(7) << " not active, send NACK " << dendl
;
2111 mds
->send_message_mds(new MExportDirDiscoverAck(df
, m
->get_tid(), false), from
);
2116 // only start discovering on this message once.
2117 import_state_t
*p_state
;
2118 map
<dirfrag_t
,import_state_t
>::iterator it
= import_state
.find(df
);
2120 assert(it
== import_state
.end());
2122 p_state
= &import_state
[df
];
2123 p_state
->state
= IMPORT_DISCOVERING
;
2124 p_state
->peer
= from
;
2125 p_state
->tid
= m
->get_tid();
2127 // am i retrying after ancient path_traverse results?
2128 if (it
== import_state
.end() ||
2129 it
->second
.peer
!= from
||
2130 it
->second
.tid
!= m
->get_tid()) {
2131 dout(7) << " dropping obsolete message" << dendl
;
2135 assert(it
->second
.state
== IMPORT_DISCOVERING
);
2136 p_state
= &it
->second
;
2139 if (!mds
->mdcache
->is_open()) {
2140 dout(5) << " waiting for root" << dendl
;
2141 mds
->mdcache
->wait_for_open(new C_MDS_RetryMessage(mds
, m
));
2145 assert (g_conf
->mds_kill_import_at
!= 1);
2148 CInode
*in
= cache
->get_inode(m
->get_dirfrag().ino
);
2150 // must discover it!
2151 filepath
fpath(m
->get_path());
2152 vector
<CDentry
*> trace
;
2153 MDRequestRef null_ref
;
2154 int r
= cache
->path_traverse(null_ref
, m
, NULL
, fpath
, &trace
, NULL
, MDS_TRAVERSE_DISCOVER
);
2157 dout(7) << "handle_export_discover_2 failed to discover or not dir " << m
->get_path() << ", NAK" << dendl
;
2158 ceph_abort(); // this shouldn't happen if the auth pins its path properly!!!!
2161 ceph_abort(); // this shouldn't happen; the get_inode above would have succeeded.
2165 dout(7) << "handle_export_discover have " << df
<< " inode " << *in
<< dendl
;
2167 p_state
->state
= IMPORT_DISCOVERED
;
2169 // pin inode in the cache (for now)
2170 assert(in
->is_dir());
2171 in
->get(CInode::PIN_IMPORTING
);
2174 dout(7) << " sending export_discover_ack on " << *in
<< dendl
;
2175 mds
->send_message_mds(new MExportDirDiscoverAck(df
, m
->get_tid()), p_state
->peer
);
2177 assert (g_conf
->mds_kill_import_at
!= 2);
2180 void Migrator::import_reverse_discovering(dirfrag_t df
)
2182 import_state
.erase(df
);
2185 void Migrator::import_reverse_discovered(dirfrag_t df
, CInode
*diri
)
2188 diri
->put(CInode::PIN_IMPORTING
);
2189 import_state
.erase(df
);
2192 void Migrator::import_reverse_prepping(CDir
*dir
, import_state_t
& stat
)
2195 cache
->map_dirfrag_set(stat
.bound_ls
, bounds
);
2196 import_remove_pins(dir
, bounds
);
2197 import_reverse_final(dir
);
2200 /* This function DOES put the passed message before returning*/
2201 void Migrator::handle_export_cancel(MExportDirCancel
*m
)
2203 dout(7) << "handle_export_cancel on " << m
->get_dirfrag() << dendl
;
2204 dirfrag_t df
= m
->get_dirfrag();
2205 map
<dirfrag_t
,import_state_t
>::iterator it
= import_state
.find(df
);
2206 if (it
== import_state
.end()) {
2207 assert(0 == "got export_cancel in weird state");
2208 } else if (it
->second
.state
== IMPORT_DISCOVERING
) {
2209 import_reverse_discovering(df
);
2210 } else if (it
->second
.state
== IMPORT_DISCOVERED
) {
2211 CInode
*in
= cache
->get_inode(df
.ino
);
2213 import_reverse_discovered(df
, in
);
2214 } else if (it
->second
.state
== IMPORT_PREPPING
) {
2215 CDir
*dir
= mds
->mdcache
->get_dirfrag(df
);
2217 import_reverse_prepping(dir
, it
->second
);
2218 } else if (it
->second
.state
== IMPORT_PREPPED
) {
2219 CDir
*dir
= mds
->mdcache
->get_dirfrag(df
);
2222 cache
->get_subtree_bounds(dir
, bounds
);
2223 import_remove_pins(dir
, bounds
);
2224 // adjust auth back to the exportor
2225 cache
->adjust_subtree_auth(dir
, it
->second
.peer
);
2226 import_reverse_unfreeze(dir
);
2228 assert(0 == "got export_cancel in weird state");
2233 /* This function DOES put the passed message before returning*/
2234 void Migrator::handle_export_prep(MExportDirPrep
*m
)
2236 mds_rank_t oldauth
= mds_rank_t(m
->get_source().num());
2237 assert(oldauth
!= mds
->get_nodeid());
2241 list
<MDSInternalContextBase
*> finished
;
2243 // assimilate root dir.
2244 map
<dirfrag_t
,import_state_t
>::iterator it
= import_state
.find(m
->get_dirfrag());
2245 if (!m
->did_assim()) {
2246 assert(it
!= import_state
.end());
2247 assert(it
->second
.state
== IMPORT_DISCOVERED
);
2248 assert(it
->second
.peer
== oldauth
);
2249 diri
= cache
->get_inode(m
->get_dirfrag().ino
);
2251 bufferlist::iterator p
= m
->basedir
.begin();
2252 dir
= cache
->add_replica_dir(p
, diri
, oldauth
, finished
);
2253 dout(7) << "handle_export_prep on " << *dir
<< " (first pass)" << dendl
;
2255 if (it
== import_state
.end() ||
2256 it
->second
.peer
!= oldauth
||
2257 it
->second
.tid
!= m
->get_tid()) {
2258 dout(7) << "handle_export_prep obsolete message, dropping" << dendl
;
2262 assert(it
->second
.state
== IMPORT_PREPPING
);
2263 assert(it
->second
.peer
== oldauth
);
2265 dir
= cache
->get_dirfrag(m
->get_dirfrag());
2267 dout(7) << "handle_export_prep on " << *dir
<< " (subsequent pass)" << dendl
;
2268 diri
= dir
->get_inode();
2270 assert(dir
->is_auth() == false);
2272 cache
->show_subtrees();
2274 // build import bound map
2275 map
<inodeno_t
, fragset_t
> import_bound_fragset
;
2276 for (list
<dirfrag_t
>::iterator p
= m
->get_bounds().begin();
2277 p
!= m
->get_bounds().end();
2279 dout(10) << " bound " << *p
<< dendl
;
2280 import_bound_fragset
[p
->ino
].insert(p
->frag
);
2283 // assimilate contents?
2284 if (!m
->did_assim()) {
2285 dout(7) << "doing assim on " << *dir
<< dendl
;
2286 m
->mark_assim(); // only do this the first time!
2288 // change import state
2289 it
->second
.state
= IMPORT_PREPPING
;
2290 it
->second
.bound_ls
= m
->get_bounds();
2291 it
->second
.bystanders
= m
->get_bystanders();
2292 assert(g_conf
->mds_kill_import_at
!= 3);
2295 dout(7) << "bystanders are " << it
->second
.bystanders
<< dendl
;
2298 diri
->put(CInode::PIN_IMPORTING
);
2299 dir
->get(CDir::PIN_IMPORTING
);
2300 dir
->state_set(CDir::STATE_IMPORTING
);
2302 // assimilate traces to exports
2303 // each trace is: df ('-' | ('f' dir | 'd') dentry inode (dir dentry inode)*)
2304 for (list
<bufferlist
>::iterator p
= m
->traces
.begin();
2305 p
!= m
->traces
.end();
2307 bufferlist::iterator q
= p
->begin();
2312 dout(10) << " trace from " << df
<< " start " << start
<< " len " << p
->length() << dendl
;
2316 cur
= cache
->get_dirfrag(df
);
2318 dout(10) << " had " << *cur
<< dendl
;
2319 } else if (start
== 'f') {
2320 CInode
*in
= cache
->get_inode(df
.ino
);
2322 dout(10) << " had " << *in
<< dendl
;
2323 cur
= cache
->add_replica_dir(q
, in
, oldauth
, finished
);
2324 dout(10) << " added " << *cur
<< dendl
;
2325 } else if (start
== '-') {
2328 assert(0 == "unrecognized start char");
2331 CDentry
*dn
= cache
->add_replica_dentry(q
, cur
, finished
);
2332 dout(10) << " added " << *dn
<< dendl
;
2333 CInode
*in
= cache
->add_replica_inode(q
, dn
, finished
);
2334 dout(10) << " added " << *in
<< dendl
;
2337 cur
= cache
->add_replica_dir(q
, in
, oldauth
, finished
);
2338 dout(10) << " added " << *cur
<< dendl
;
2342 // make bound sticky
2343 for (map
<inodeno_t
,fragset_t
>::iterator p
= import_bound_fragset
.begin();
2344 p
!= import_bound_fragset
.end();
2346 CInode
*in
= cache
->get_inode(p
->first
);
2348 in
->get_stickydirs();
2349 dout(7) << " set stickydirs on bound inode " << *in
<< dendl
;
2353 dout(7) << " not doing assim on " << *dir
<< dendl
;
2356 if (!finished
.empty())
2357 mds
->queue_waiters(finished
);
2360 bool success
= true;
2361 if (mds
->is_active()) {
2363 set
<CDir
*> import_bounds
;
2364 for (map
<inodeno_t
,fragset_t
>::iterator p
= import_bound_fragset
.begin();
2365 p
!= import_bound_fragset
.end();
2367 CInode
*in
= cache
->get_inode(p
->first
);
2370 // map fragset into a frag_t list, based on the inode fragtree
2371 list
<frag_t
> fglist
;
2372 for (set
<frag_t
>::iterator q
= p
->second
.begin(); q
!= p
->second
.end(); ++q
)
2373 in
->dirfragtree
.get_leaves_under(*q
, fglist
);
2374 dout(10) << " bound inode " << p
->first
<< " fragset " << p
->second
<< " maps to " << fglist
<< dendl
;
2376 for (list
<frag_t
>::iterator q
= fglist
.begin();
2379 CDir
*bound
= cache
->get_dirfrag(dirfrag_t(p
->first
, *q
));
2381 dout(7) << " opening bounding dirfrag " << *q
<< " on " << *in
<< dendl
;
2382 cache
->open_remote_dirfrag(in
, *q
,
2383 new C_MDS_RetryMessage(mds
, m
));
2387 if (!bound
->state_test(CDir::STATE_IMPORTBOUND
)) {
2388 dout(7) << " pinning import bound " << *bound
<< dendl
;
2389 bound
->get(CDir::PIN_IMPORTBOUND
);
2390 bound
->state_set(CDir::STATE_IMPORTBOUND
);
2392 dout(7) << " already pinned import bound " << *bound
<< dendl
;
2394 import_bounds
.insert(bound
);
2398 dout(7) << " all ready, noting auth and freezing import region" << dendl
;
2400 if (!mds
->mdcache
->is_readonly() &&
2401 dir
->get_inode()->filelock
.can_wrlock(-1) &&
2402 dir
->get_inode()->nestlock
.can_wrlock(-1)) {
2403 it
->second
.mut
= new MutationImpl();
2404 // force some locks. hacky.
2405 mds
->locker
->wrlock_force(&dir
->inode
->filelock
, it
->second
.mut
);
2406 mds
->locker
->wrlock_force(&dir
->inode
->nestlock
, it
->second
.mut
);
2408 // note that i am an ambiguous auth for this subtree.
2409 // specify bounds, since the exporter explicitly defines the region.
2410 cache
->adjust_bounded_subtree_auth(dir
, import_bounds
,
2411 pair
<int,int>(oldauth
, mds
->get_nodeid()));
2412 cache
->verify_subtree_bounds(dir
, import_bounds
);
2414 dir
->_freeze_tree();
2416 it
->second
.state
= IMPORT_PREPPED
;
2418 dout(7) << " couldn't acquire all needed locks, failing. " << *dir
<< dendl
;
2422 dout(7) << " not active, failing. " << *dir
<< dendl
;
2427 import_reverse_prepping(dir
, it
->second
);
2430 dout(7) << " sending export_prep_ack on " << *dir
<< dendl
;
2431 mds
->send_message(new MExportDirPrepAck(dir
->dirfrag(), success
, m
->get_tid()), m
->get_connection());
2433 assert(g_conf
->mds_kill_import_at
!= 4);
2441 class C_MDS_ImportDirLoggedStart
: public MigratorLogContext
{
2446 map
<client_t
,entity_inst_t
> imported_client_map
;
2447 map
<client_t
,uint64_t> sseqmap
;
2449 C_MDS_ImportDirLoggedStart(Migrator
*m
, CDir
*d
, mds_rank_t f
) :
2450 MigratorLogContext(m
), df(d
->dirfrag()), dir(d
), from(f
) {
2452 void finish(int r
) override
{
2453 mig
->import_logged_start(df
, dir
, from
, imported_client_map
, sseqmap
);
2457 /* This function DOES put the passed message before returning*/
2458 void Migrator::handle_export_dir(MExportDir
*m
)
2460 assert (g_conf
->mds_kill_import_at
!= 5);
2461 CDir
*dir
= cache
->get_dirfrag(m
->dirfrag
);
2464 mds_rank_t oldauth
= mds_rank_t(m
->get_source().num());
2465 dout(7) << "handle_export_dir importing " << *dir
<< " from " << oldauth
<< dendl
;
2467 assert(!dir
->is_auth());
2469 map
<dirfrag_t
,import_state_t
>::iterator it
= import_state
.find(m
->dirfrag
);
2470 assert(it
!= import_state
.end());
2471 assert(it
->second
.state
== IMPORT_PREPPED
);
2472 assert(it
->second
.tid
== m
->get_tid());
2473 assert(it
->second
.peer
== oldauth
);
2475 utime_t now
= ceph_clock_now();
2477 if (!dir
->get_inode()->dirfragtree
.is_leaf(dir
->get_frag()))
2478 dir
->get_inode()->dirfragtree
.force_to_leaf(g_ceph_context
, dir
->get_frag());
2480 cache
->show_subtrees();
2482 C_MDS_ImportDirLoggedStart
*onlogged
= new C_MDS_ImportDirLoggedStart(this, dir
, oldauth
);
2484 // start the journal entry
2485 EImportStart
*le
= new EImportStart(mds
->mdlog
, dir
->dirfrag(), m
->bounds
, oldauth
);
2486 mds
->mdlog
->start_entry(le
);
2488 le
->metablob
.add_dir_context(dir
);
2490 // adjust auth (list us _first_)
2491 cache
->adjust_subtree_auth(dir
, mds
->get_nodeid(), oldauth
);
2493 // new client sessions, open these after we journal
2494 // include imported sessions in EImportStart
2495 bufferlist::iterator cmp
= m
->client_map
.begin();
2496 ::decode(onlogged
->imported_client_map
, cmp
);
2498 le
->cmapv
= mds
->server
->prepare_force_open_sessions(onlogged
->imported_client_map
, onlogged
->sseqmap
);
2499 le
->client_map
.claim(m
->client_map
);
2501 bufferlist::iterator blp
= m
->export_data
.begin();
2502 int num_imported_inodes
= 0;
2503 while (!blp
.end()) {
2504 num_imported_inodes
+=
2505 decode_import_dir(blp
,
2509 mds
->mdlog
->get_current_segment(),
2510 it
->second
.peer_exports
,
2511 it
->second
.updated_scatterlocks
,
2514 dout(10) << " " << m
->bounds
.size() << " imported bounds" << dendl
;
2516 // include bounds in EImportStart
2517 set
<CDir
*> import_bounds
;
2518 for (vector
<dirfrag_t
>::iterator p
= m
->bounds
.begin();
2519 p
!= m
->bounds
.end();
2521 CDir
*bd
= cache
->get_dirfrag(*p
);
2523 le
->metablob
.add_dir(bd
, false); // note that parent metadata is already in the event
2524 import_bounds
.insert(bd
);
2526 cache
->verify_subtree_bounds(dir
, import_bounds
);
2528 // adjust popularity
2529 mds
->balancer
->add_import(dir
, now
);
2531 dout(7) << "handle_export_dir did " << *dir
<< dendl
;
2534 it
->second
.state
= IMPORT_LOGGINGSTART
;
2535 assert (g_conf
->mds_kill_import_at
!= 6);
2538 mds
->mdlog
->submit_entry(le
, onlogged
);
2539 mds
->mdlog
->flush();
2543 mds
->logger
->inc(l_mds_imported
);
2544 mds
->logger
->inc(l_mds_imported_inodes
, num_imported_inodes
);
2552 * this is an import helper
2553 * called by import_finish, and import_reverse and friends.
2555 void Migrator::import_remove_pins(CDir
*dir
, set
<CDir
*>& bounds
)
2557 import_state_t
& stat
= import_state
[dir
->dirfrag()];
2559 dir
->put(CDir::PIN_IMPORTING
);
2560 dir
->state_clear(CDir::STATE_IMPORTING
);
2564 for (list
<dirfrag_t
>::iterator p
= stat
.bound_ls
.begin();
2565 p
!= stat
.bound_ls
.end();
2567 if (did
.count(p
->ino
))
2570 CInode
*in
= cache
->get_inode(p
->ino
);
2572 in
->put_stickydirs();
2575 if (stat
.state
== IMPORT_PREPPING
) {
2576 for (auto bd
: bounds
) {
2577 if (bd
->state_test(CDir::STATE_IMPORTBOUND
)) {
2578 bd
->put(CDir::PIN_IMPORTBOUND
);
2579 bd
->state_clear(CDir::STATE_IMPORTBOUND
);
2582 } else if (stat
.state
>= IMPORT_PREPPED
) {
2583 // bounding dirfrags
2584 for (auto bd
: bounds
) {
2585 assert(bd
->state_test(CDir::STATE_IMPORTBOUND
));
2586 bd
->put(CDir::PIN_IMPORTBOUND
);
2587 bd
->state_clear(CDir::STATE_IMPORTBOUND
);
2594 * note: this does teh full work of reversing and import and cleaning up
2596 * called by both handle_mds_failure and by handle_resolve (if we are
2597 * a survivor coping with an exporter failure+recovery).
2599 void Migrator::import_reverse(CDir
*dir
)
2601 dout(7) << "import_reverse " << *dir
<< dendl
;
2603 import_state_t
& stat
= import_state
[dir
->dirfrag()];
2604 stat
.state
= IMPORT_ABORTING
;
2607 cache
->get_subtree_bounds(dir
, bounds
);
2610 import_remove_pins(dir
, bounds
);
2612 // update auth, with possible subtree merge.
2613 assert(dir
->is_subtree_root());
2614 if (mds
->is_resolve())
2615 cache
->trim_non_auth_subtree(dir
);
2617 cache
->adjust_subtree_auth(dir
, stat
.peer
);
2619 C_ContextsBase
<MDSInternalContextBase
, MDSInternalContextGather
> *fin
= new C_ContextsBase
<MDSInternalContextBase
, MDSInternalContextGather
>(g_ceph_context
);
2620 if (!dir
->get_inode()->is_auth() &&
2621 !dir
->get_inode()->has_subtree_root_dirfrag(mds
->get_nodeid())) {
2622 dir
->get_inode()->clear_scatter_dirty();
2623 // wake up scatter_nudge waiters
2624 dir
->get_inode()->take_waiting(CInode::WAIT_ANY_MASK
, fin
->contexts
);
2627 int num_dentries
= 0;
2628 // adjust auth bits.
2631 while (!q
.empty()) {
2632 CDir
*cur
= q
.front();
2636 assert(cur
->is_auth());
2637 cur
->state_clear(CDir::STATE_AUTH
);
2638 cur
->remove_bloom();
2639 cur
->clear_replica_map();
2640 cur
->set_replica_nonce(CDir::EXPORT_NONCE
);
2641 if (cur
->is_dirty())
2644 CDir::map_t::iterator it
;
2645 for (it
= cur
->begin(); it
!= cur
->end(); ++it
) {
2646 CDentry
*dn
= it
->second
;
2649 dn
->state_clear(CDentry::STATE_AUTH
);
2650 dn
->clear_replica_map();
2651 dn
->set_replica_nonce(CDentry::EXPORT_NONCE
);
2656 if (dn
->get_linkage()->is_primary()) {
2657 CInode
*in
= dn
->get_linkage()->get_inode();
2658 in
->state_clear(CDentry::STATE_AUTH
);
2659 in
->clear_replica_map();
2660 in
->set_replica_nonce(CInode::EXPORT_NONCE
);
2663 in
->clear_dirty_rstat();
2664 if (!in
->has_subtree_root_dirfrag(mds
->get_nodeid())) {
2665 in
->clear_scatter_dirty();
2666 in
->take_waiting(CInode::WAIT_ANY_MASK
, fin
->contexts
);
2669 in
->clear_dirty_parent();
2671 in
->authlock
.clear_gather();
2672 in
->linklock
.clear_gather();
2673 in
->dirfragtreelock
.clear_gather();
2674 in
->filelock
.clear_gather();
2676 in
->clear_file_locks();
2678 // non-bounding dir?
2680 in
->get_dirfrags(dfs
);
2681 for (list
<CDir
*>::iterator p
= dfs
.begin(); p
!= dfs
.end(); ++p
)
2682 if (bounds
.count(*p
) == 0)
2686 cache
->touch_dentry_bottom(dn
); // move dentry to tail of LRU
2691 dir
->add_waiter(CDir::WAIT_UNFREEZE
, fin
);
2693 if (stat
.state
== IMPORT_ACKING
) {
2694 // remove imported caps
2695 for (map
<CInode
*,map
<client_t
,Capability::Export
> >::iterator p
= stat
.peer_exports
.begin();
2696 p
!= stat
.peer_exports
.end();
2698 CInode
*in
= p
->first
;
2699 for (map
<client_t
,Capability::Export
>::iterator q
= p
->second
.begin();
2700 q
!= p
->second
.end();
2702 Capability
*cap
= in
->get_client_cap(q
->first
);
2704 if (cap
->is_importing())
2705 in
->remove_client_cap(q
->first
);
2707 in
->put(CInode::PIN_IMPORTINGCAPS
);
2709 for (map
<client_t
,entity_inst_t
>::iterator p
= stat
.client_map
.begin();
2710 p
!= stat
.client_map
.end();
2712 Session
*session
= mds
->sessionmap
.get_session(entity_name_t::CLIENT(p
->first
.v
));
2714 session
->dec_importing();
2719 mds
->mdlog
->start_submit_entry(new EImportFinish(dir
, false)); // log failure
2721 cache
->trim(num_dentries
); // try trimming dentries
2723 // notify bystanders; wait in aborting state
2724 import_notify_abort(dir
, bounds
);
2727 void Migrator::import_notify_finish(CDir
*dir
, set
<CDir
*>& bounds
)
2729 dout(7) << "import_notify_finish " << *dir
<< dendl
;
2731 import_state_t
& stat
= import_state
[dir
->dirfrag()];
2732 for (set
<mds_rank_t
>::iterator p
= stat
.bystanders
.begin();
2733 p
!= stat
.bystanders
.end();
2735 MExportDirNotify
*notify
=
2736 new MExportDirNotify(dir
->dirfrag(), stat
.tid
, false,
2737 pair
<int,int>(stat
.peer
, mds
->get_nodeid()),
2738 pair
<int,int>(mds
->get_nodeid(), CDIR_AUTH_UNKNOWN
));
2739 for (set
<CDir
*>::iterator i
= bounds
.begin(); i
!= bounds
.end(); ++i
)
2740 notify
->get_bounds().push_back((*i
)->dirfrag());
2741 mds
->send_message_mds(notify
, *p
);
2745 void Migrator::import_notify_abort(CDir
*dir
, set
<CDir
*>& bounds
)
2747 dout(7) << "import_notify_abort " << *dir
<< dendl
;
2749 import_state_t
& stat
= import_state
[dir
->dirfrag()];
2750 for (set
<mds_rank_t
>::iterator p
= stat
.bystanders
.begin();
2751 p
!= stat
.bystanders
.end(); ) {
2752 if (mds
->is_cluster_degraded() &&
2753 !mds
->mdsmap
->is_clientreplay_or_active_or_stopping(*p
)) {
2754 // this can happen if both exporter and bystander fail in the same mdsmap epoch
2755 stat
.bystanders
.erase(p
++);
2758 MExportDirNotify
*notify
=
2759 new MExportDirNotify(dir
->dirfrag(), stat
.tid
, true,
2760 mds_authority_t(stat
.peer
, mds
->get_nodeid()),
2761 mds_authority_t(stat
.peer
, CDIR_AUTH_UNKNOWN
));
2762 for (set
<CDir
*>::iterator i
= bounds
.begin(); i
!= bounds
.end(); ++i
)
2763 notify
->get_bounds().push_back((*i
)->dirfrag());
2764 mds
->send_message_mds(notify
, *p
);
2767 if (stat
.bystanders
.empty()) {
2768 dout(7) << "no bystanders, finishing reverse now" << dendl
;
2769 import_reverse_unfreeze(dir
);
2771 assert (g_conf
->mds_kill_import_at
!= 10);
2775 void Migrator::import_reverse_unfreeze(CDir
*dir
)
2777 dout(7) << "import_reverse_unfreeze " << *dir
<< dendl
;
2778 assert(!dir
->is_auth());
2779 cache
->discard_delayed_expire(dir
);
2780 dir
->unfreeze_tree();
2781 if (dir
->is_subtree_root())
2782 cache
->try_subtree_merge(dir
);
2783 import_reverse_final(dir
);
2786 void Migrator::import_reverse_final(CDir
*dir
)
2788 dout(7) << "import_reverse_final " << *dir
<< dendl
;
2791 map
<dirfrag_t
, import_state_t
>::iterator it
= import_state
.find(dir
->dirfrag());
2792 assert(it
!= import_state
.end());
2794 MutationRef mut
= it
->second
.mut
;
2795 import_state
.erase(it
);
2797 // send pending import_maps?
2798 mds
->mdcache
->maybe_send_pending_resolves();
2801 mds
->locker
->drop_locks(mut
.get());
2805 cache
->show_subtrees();
2806 //audit(); // this fails, bc we munge up the subtree map during handle_import_map (resolve phase)
2812 void Migrator::import_logged_start(dirfrag_t df
, CDir
*dir
, mds_rank_t from
,
2813 map
<client_t
,entity_inst_t
>& imported_client_map
,
2814 map
<client_t
,uint64_t>& sseqmap
)
2816 map
<dirfrag_t
, import_state_t
>::iterator it
= import_state
.find(dir
->dirfrag());
2817 if (it
== import_state
.end() ||
2818 it
->second
.state
!= IMPORT_LOGGINGSTART
) {
2819 dout(7) << "import " << df
<< " must have aborted" << dendl
;
2820 mds
->server
->finish_force_open_sessions(imported_client_map
, sseqmap
);
2824 dout(7) << "import_logged " << *dir
<< dendl
;
2827 it
->second
.state
= IMPORT_ACKING
;
2829 assert (g_conf
->mds_kill_import_at
!= 7);
2831 // force open client sessions and finish cap import
2832 mds
->server
->finish_force_open_sessions(imported_client_map
, sseqmap
, false);
2833 it
->second
.client_map
.swap(imported_client_map
);
2835 map
<inodeno_t
,map
<client_t
,Capability::Import
> > imported_caps
;
2836 for (map
<CInode
*, map
<client_t
,Capability::Export
> >::iterator p
= it
->second
.peer_exports
.begin();
2837 p
!= it
->second
.peer_exports
.end();
2839 // parameter 'peer' is NONE, delay sending cap import messages to client
2840 finish_import_inode_caps(p
->first
, MDS_RANK_NONE
, true, p
->second
, imported_caps
[p
->first
->ino()]);
2843 // send notify's etc.
2844 dout(7) << "sending ack for " << *dir
<< " to old auth mds." << from
<< dendl
;
2846 // test surviving observer of a failed migration that did not complete
2847 //assert(dir->replica_map.size() < 2 || mds->get_nodeid() != 0);
2849 MExportDirAck
*ack
= new MExportDirAck(dir
->dirfrag(), it
->second
.tid
);
2850 ::encode(imported_caps
, ack
->imported_caps
);
2852 mds
->send_message_mds(ack
, from
);
2853 assert (g_conf
->mds_kill_import_at
!= 8);
2855 cache
->show_subtrees();
2858 /* This function DOES put the passed message before returning*/
2859 void Migrator::handle_export_finish(MExportDirFinish
*m
)
2861 CDir
*dir
= cache
->get_dirfrag(m
->get_dirfrag());
2863 dout(7) << "handle_export_finish on " << *dir
<< (m
->is_last() ? " last" : "") << dendl
;
2865 map
<dirfrag_t
,import_state_t
>::iterator it
= import_state
.find(m
->get_dirfrag());
2866 assert(it
!= import_state
.end());
2867 assert(it
->second
.tid
== m
->get_tid());
2869 import_finish(dir
, false, m
->is_last());
2874 void Migrator::import_finish(CDir
*dir
, bool notify
, bool last
)
2876 dout(7) << "import_finish on " << *dir
<< dendl
;
2878 map
<dirfrag_t
,import_state_t
>::iterator it
= import_state
.find(dir
->dirfrag());
2879 assert(it
!= import_state
.end());
2880 assert(it
->second
.state
== IMPORT_ACKING
|| it
->second
.state
== IMPORT_FINISHING
);
2882 if (it
->second
.state
== IMPORT_ACKING
) {
2883 assert(dir
->is_auth());
2884 cache
->adjust_subtree_auth(dir
, mds
->get_nodeid(), mds
->get_nodeid());
2888 assert(g_conf
->mds_kill_import_at
!= 9);
2890 if (it
->second
.state
== IMPORT_ACKING
) {
2891 for (map
<CInode
*, map
<client_t
,Capability::Export
> >::iterator p
= it
->second
.peer_exports
.begin();
2892 p
!= it
->second
.peer_exports
.end();
2894 CInode
*in
= p
->first
;
2895 assert(in
->is_auth());
2896 for (map
<client_t
,Capability::Export
>::iterator q
= p
->second
.begin();
2897 q
!= p
->second
.end();
2899 Session
*session
= mds
->sessionmap
.get_session(entity_name_t::CLIENT(q
->first
.v
));
2901 Capability
*cap
= in
->get_client_cap(q
->first
);
2903 cap
->merge(q
->second
, true);
2904 cap
->clear_importing();
2905 mds
->mdcache
->do_cap_import(session
, in
, cap
, q
->second
.cap_id
, q
->second
.seq
,
2906 q
->second
.mseq
- 1, it
->second
.peer
, CEPH_CAP_FLAG_AUTH
);
2909 in
->replica_caps_wanted
= 0;
2911 for (map
<client_t
,entity_inst_t
>::iterator p
= it
->second
.client_map
.begin();
2912 p
!= it
->second
.client_map
.end();
2914 Session
*session
= mds
->sessionmap
.get_session(entity_name_t::CLIENT(p
->first
.v
));
2916 session
->dec_importing();
2921 assert(it
->second
.state
== IMPORT_ACKING
);
2922 it
->second
.state
= IMPORT_FINISHING
;
2928 cache
->get_subtree_bounds(dir
, bounds
);
2931 import_notify_finish(dir
, bounds
);
2933 import_remove_pins(dir
, bounds
);
2935 map
<CInode
*, map
<client_t
,Capability::Export
> > peer_exports
;
2936 it
->second
.peer_exports
.swap(peer_exports
);
2938 // clear import state (we're done!)
2939 MutationRef mut
= it
->second
.mut
;
2940 import_state
.erase(it
);
2942 mds
->mdlog
->start_submit_entry(new EImportFinish(dir
, true));
2944 // process delayed expires
2945 cache
->process_delayed_expire(dir
);
2947 // unfreeze tree, with possible subtree merge.
2948 dir
->unfreeze_tree();
2949 cache
->try_subtree_merge(dir
);
2951 cache
->show_subtrees();
2952 //audit(); // this fails, bc we munge up the subtree map during handle_import_map (resolve phase)
2955 mds
->locker
->drop_locks(mut
.get());
2959 // re-eval imported caps
2960 for (map
<CInode
*, map
<client_t
,Capability::Export
> >::iterator p
= peer_exports
.begin();
2961 p
!= peer_exports
.end();
2963 if (p
->first
->is_auth())
2964 mds
->locker
->eval(p
->first
, CEPH_CAP_LOCKS
, true);
2965 p
->first
->put(CInode::PIN_IMPORTINGCAPS
);
2968 // send pending import_maps?
2969 mds
->mdcache
->maybe_send_pending_resolves();
2971 // did i just import mydir?
2972 if (dir
->ino() == MDS_INO_MDSDIR(mds
->get_nodeid()))
2973 cache
->populate_mydir();
2976 if (dir
->get_num_head_items() == 0 &&
2977 !dir
->inode
->is_auth()) {
2979 export_empty_import(dir
);
2984 void Migrator::decode_import_inode(CDentry
*dn
, bufferlist::iterator
& blp
,
2985 mds_rank_t oldauth
, LogSegment
*ls
,
2986 map
<CInode
*, map
<client_t
,Capability::Export
> >& peer_exports
,
2987 list
<ScatterLock
*>& updated_scatterlocks
)
2989 dout(15) << "decode_import_inode on " << *dn
<< dendl
;
2994 ::decode(last
, blp
);
2997 CInode
*in
= cache
->get_inode(ino
, last
);
2999 in
= new CInode(mds
->mdcache
, true, 1, last
);
3003 // state after link -- or not! -sage
3004 in
->decode_import(blp
, ls
); // cap imports are noted for later action
3007 decode_import_inode_caps(in
, true, blp
, peer_exports
);
3009 // link before state -- or not! -sage
3010 if (dn
->get_linkage()->get_inode() != in
) {
3011 assert(!dn
->get_linkage()->get_inode());
3012 dn
->dir
->link_primary_inode(dn
, in
);
3017 cache
->add_inode(in
);
3018 dout(10) << "added " << *in
<< dendl
;
3020 dout(10) << " had " << *in
<< dendl
;
3023 if (in
->inode
.is_dirty_rstat())
3024 in
->mark_dirty_rstat();
3026 // clear if dirtyscattered, since we're going to journal this
3027 // but not until we _actually_ finish the import...
3028 if (in
->filelock
.is_dirty()) {
3029 updated_scatterlocks
.push_back(&in
->filelock
);
3030 mds
->locker
->mark_updated_scatterlock(&in
->filelock
);
3033 if (in
->dirfragtreelock
.is_dirty()) {
3034 updated_scatterlocks
.push_back(&in
->dirfragtreelock
);
3035 mds
->locker
->mark_updated_scatterlock(&in
->dirfragtreelock
);
3038 // adjust replica list
3039 //assert(!in->is_replica(oldauth)); // not true on failed export
3040 in
->add_replica(oldauth
, CInode::EXPORT_NONCE
);
3041 if (in
->is_replica(mds
->get_nodeid()))
3042 in
->remove_replica(mds
->get_nodeid());
3045 void Migrator::decode_import_inode_caps(CInode
*in
, bool auth_cap
,
3046 bufferlist::iterator
&blp
,
3047 map
<CInode
*, map
<client_t
,Capability::Export
> >& peer_exports
)
3049 map
<client_t
,Capability::Export
> cap_map
;
3050 ::decode(cap_map
, blp
);
3052 ::decode(in
->get_mds_caps_wanted(), blp
);
3053 if (!cap_map
.empty() ||
3054 (auth_cap
&& (in
->get_caps_wanted() & ~CEPH_CAP_PIN
))) {
3055 peer_exports
[in
].swap(cap_map
);
3056 in
->get(CInode::PIN_IMPORTINGCAPS
);
3060 void Migrator::finish_import_inode_caps(CInode
*in
, mds_rank_t peer
, bool auth_cap
,
3061 map
<client_t
,Capability::Export
> &export_map
,
3062 map
<client_t
,Capability::Import
> &import_map
)
3064 for (map
<client_t
,Capability::Export
>::iterator it
= export_map
.begin();
3065 it
!= export_map
.end();
3067 dout(10) << "finish_import_inode_caps for client." << it
->first
<< " on " << *in
<< dendl
;
3068 Session
*session
= mds
->sessionmap
.get_session(entity_name_t::CLIENT(it
->first
.v
));
3071 Capability
*cap
= in
->get_client_cap(it
->first
);
3073 cap
= in
->add_client_cap(it
->first
, session
);
3075 cap
->mark_importing();
3078 Capability::Import
& im
= import_map
[it
->first
];
3079 im
.cap_id
= cap
->get_cap_id();
3080 im
.mseq
= auth_cap
? it
->second
.mseq
: cap
->get_mseq();
3081 im
.issue_seq
= cap
->get_last_seq() + 1;
3084 cap
->merge(it
->second
, auth_cap
);
3085 mds
->mdcache
->do_cap_import(session
, in
, cap
, it
->second
.cap_id
,
3086 it
->second
.seq
, it
->second
.mseq
- 1, peer
,
3087 auth_cap
? CEPH_CAP_FLAG_AUTH
: CEPH_CAP_FLAG_RELEASE
);
3092 in
->replica_caps_wanted
= 0;
3093 in
->put(CInode::PIN_IMPORTINGCAPS
);
3097 int Migrator::decode_import_dir(bufferlist::iterator
& blp
,
3102 map
<CInode
*,map
<client_t
,Capability::Export
> >& peer_exports
,
3103 list
<ScatterLock
*>& updated_scatterlocks
, utime_t now
)
3109 CInode
*diri
= cache
->get_inode(df
.ino
);
3111 CDir
*dir
= diri
->get_or_open_dirfrag(mds
->mdcache
, df
.frag
);
3114 dout(7) << "decode_import_dir " << *dir
<< dendl
;
3117 dir
->decode_import(blp
, now
, ls
);
3119 // adjust replica list
3120 //assert(!dir->is_replica(oldauth)); // not true on failed export
3121 dir
->add_replica(oldauth
, CDir::EXPORT_NONCE
);
3122 if (dir
->is_replica(mds
->get_nodeid()))
3123 dir
->remove_replica(mds
->get_nodeid());
3125 // add to journal entry
3127 le
->metablob
.add_import_dir(dir
);
3129 int num_imported
= 0;
3131 // take all waiters on this dir
3132 // NOTE: a pass of imported data is guaranteed to get all of my waiters because
3133 // a replica's presense in my cache implies/forces it's presense in authority's.
3134 list
<MDSInternalContextBase
*> waiters
;
3136 dir
->take_waiting(CDir::WAIT_ANY_MASK
, waiters
);
3137 for (list
<MDSInternalContextBase
*>::iterator it
= waiters
.begin();
3138 it
!= waiters
.end();
3140 import_root
->add_waiter(CDir::WAIT_UNFREEZE
, *it
); // UNFREEZE will get kicked both on success or failure
3142 dout(15) << "doing contents" << dendl
;
3146 ::decode(nden
, blp
);
3148 for (; nden
>0; nden
--) {
3154 ::decode(dname
, blp
);
3155 ::decode(last
, blp
);
3157 CDentry
*dn
= dir
->lookup_exact_snap(dname
, last
);
3159 dn
= dir
->add_null_dentry(dname
, 1, last
);
3161 dn
->decode_import(blp
, ls
);
3163 dn
->add_replica(oldauth
, CDentry::EXPORT_NONCE
);
3164 if (dn
->is_replica(mds
->get_nodeid()))
3165 dn
->remove_replica(mds
->get_nodeid());
3167 // dentry lock in unreadable state can block path traverse
3168 if (dn
->lock
.get_state() != LOCK_SYNC
)
3169 mds
->locker
->try_eval(&dn
->lock
, NULL
);
3171 dout(15) << "decode_import_dir got " << *dn
<< dendl
;
3175 ::decode(icode
, blp
);
3179 assert(dn
->get_linkage()->is_null());
3183 else if (icode
== 'L') {
3186 unsigned char d_type
;
3188 ::decode(d_type
, blp
);
3189 if (dn
->get_linkage()->is_remote()) {
3190 assert(dn
->get_linkage()->get_remote_ino() == ino
);
3192 dir
->link_remote_inode(dn
, ino
, d_type
);
3195 else if (icode
== 'I') {
3198 decode_import_inode(dn
, blp
, oldauth
, ls
,
3199 peer_exports
, updated_scatterlocks
);
3202 // add dentry to journal entry
3204 le
->metablob
.add_import_dentry(dn
);
3207 #ifdef MDS_VERIFY_FRAGSTAT
3208 if (dir
->is_complete())
3209 dir
->verify_fragstat();
3212 dir
->inode
->maybe_export_pin();
3214 dout(7) << "decode_import_dir done " << *dir
<< dendl
;
3215 return num_imported
;
3222 // authority bystander
3224 /* This function DOES put the passed message before returning*/
3225 void Migrator::handle_export_notify(MExportDirNotify
*m
)
3227 if (!(mds
->is_clientreplay() || mds
->is_active() || mds
->is_stopping())) {
3232 CDir
*dir
= cache
->get_dirfrag(m
->get_dirfrag());
3234 mds_rank_t from
= mds_rank_t(m
->get_source().num());
3235 mds_authority_t old_auth
= m
->get_old_auth();
3236 mds_authority_t new_auth
= m
->get_new_auth();
3239 dout(7) << "handle_export_notify " << old_auth
<< " -> " << new_auth
3240 << " on missing dir " << m
->get_dirfrag() << dendl
;
3241 } else if (dir
->authority() != old_auth
) {
3242 dout(7) << "handle_export_notify old_auth was " << dir
->authority()
3243 << " != " << old_auth
<< " -> " << new_auth
3244 << " on " << *dir
<< dendl
;
3246 dout(7) << "handle_export_notify " << old_auth
<< " -> " << new_auth
3247 << " on " << *dir
<< dendl
;
3250 cache
->map_dirfrag_set(m
->get_bounds(), have
);
3251 cache
->adjust_bounded_subtree_auth(dir
, have
, new_auth
);
3254 cache
->try_subtree_merge(dir
);
3258 if (m
->wants_ack()) {
3259 mds
->send_message_mds(new MExportDirNotifyAck(m
->get_dirfrag(), m
->get_tid(), m
->get_new_auth()), from
);
3262 dout(7) << "handle_export_notify no ack requested" << dendl
;
3269 void Migrator::export_caps(CInode
*in
)
3271 mds_rank_t dest
= in
->authority().first
;
3272 dout(7) << "export_caps to mds." << dest
<< " " << *in
<< dendl
;
3274 assert(in
->is_any_caps());
3275 assert(!in
->is_auth());
3276 assert(!in
->is_ambiguous_auth());
3277 assert(!in
->state_test(CInode::STATE_EXPORTINGCAPS
));
3279 MExportCaps
*ex
= new MExportCaps
;
3280 ex
->ino
= in
->ino();
3282 encode_export_inode_caps(in
, false, ex
->cap_bl
, ex
->client_map
);
3284 mds
->send_message_mds(ex
, dest
);
3287 void Migrator::handle_gather_caps(MGatherCaps
*m
)
3289 CInode
*in
= cache
->get_inode(m
->ino
);
3294 dout(10) << "handle_gather_caps " << *m
<< " from " << m
->get_source()
3297 if (in
->is_any_caps() &&
3299 !in
->is_ambiguous_auth() &&
3300 !in
->state_test(CInode::STATE_EXPORTINGCAPS
))
3307 class C_M_LoggedImportCaps
: public MigratorLogContext
{
3311 map
<CInode
*, map
<client_t
,Capability::Export
> > peer_exports
;
3312 map
<client_t
,entity_inst_t
> client_map
;
3313 map
<client_t
,uint64_t> sseqmap
;
3315 C_M_LoggedImportCaps(Migrator
*m
, CInode
*i
, mds_rank_t f
) : MigratorLogContext(m
), in(i
), from(f
) {}
3316 void finish(int r
) override
{
3317 mig
->logged_import_caps(in
, from
, peer_exports
, client_map
, sseqmap
);
3321 /* This function DOES put the passed message before returning*/
3322 void Migrator::handle_export_caps(MExportCaps
*ex
)
3324 dout(10) << "handle_export_caps " << *ex
<< " from " << ex
->get_source() << dendl
;
3325 CInode
*in
= cache
->get_inode(ex
->ino
);
3328 assert(in
->is_auth());
3331 if (!in
->can_auth_pin())
3335 C_M_LoggedImportCaps
*finish
= new C_M_LoggedImportCaps(
3336 this, in
, mds_rank_t(ex
->get_source().num()));
3337 finish
->client_map
= ex
->client_map
;
3340 bufferlist::iterator blp
= ex
->cap_bl
.begin();
3341 decode_import_inode_caps(in
, false, blp
, finish
->peer_exports
);
3342 assert(!finish
->peer_exports
.empty()); // thus, inode is pinned.
3344 // journal open client sessions
3345 version_t pv
= mds
->server
->prepare_force_open_sessions(finish
->client_map
, finish
->sseqmap
);
3347 ESessions
*le
= new ESessions(pv
, ex
->client_map
);
3348 mds
->mdlog
->start_submit_entry(le
, finish
);
3349 mds
->mdlog
->flush();
3355 void Migrator::logged_import_caps(CInode
*in
,
3357 map
<CInode
*, map
<client_t
,Capability::Export
> >& peer_exports
,
3358 map
<client_t
,entity_inst_t
>& client_map
,
3359 map
<client_t
,uint64_t>& sseqmap
)
3361 dout(10) << "logged_import_caps on " << *in
<< dendl
;
3362 // see export_go() vs export_go_synced()
3363 assert(in
->is_auth());
3365 // force open client sessions and finish cap import
3366 mds
->server
->finish_force_open_sessions(client_map
, sseqmap
);
3368 map
<client_t
,Capability::Import
> imported_caps
;
3370 assert(peer_exports
.count(in
));
3371 // clients will release caps from the exporter when they receive the cap import message.
3372 finish_import_inode_caps(in
, from
, false, peer_exports
[in
], imported_caps
);
3373 mds
->locker
->eval(in
, CEPH_CAP_LOCKS
, true);
3374 in
->auth_unpin(this);