]> git.proxmox.com Git - ceph.git/blob - ceph/src/mds/Migrator.cc
update sources to v12.2.3
[ceph.git] / ceph / src / mds / Migrator.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7 *
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.
12 *
13 */
14
15 #include "MDSRank.h"
16 #include "MDCache.h"
17 #include "CInode.h"
18 #include "CDir.h"
19 #include "CDentry.h"
20 #include "Migrator.h"
21 #include "Locker.h"
22 #include "Server.h"
23
24 #include "MDBalancer.h"
25 #include "MDLog.h"
26 #include "MDSMap.h"
27 #include "Mutation.h"
28
29 #include "include/filepath.h"
30
31 #include "events/EExport.h"
32 #include "events/EImportStart.h"
33 #include "events/EImportFinish.h"
34 #include "events/ESessions.h"
35
36 #include "msg/Messenger.h"
37
38 #include "messages/MClientCaps.h"
39
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"
50
51 #include "messages/MExportCaps.h"
52 #include "messages/MExportCapsAck.h"
53 #include "messages/MGatherCaps.h"
54
55
56 /*
57 * this is what the dir->dir_auth values look like
58 *
59 * dir_auth authbits
60 * export
61 * me me - before
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
65 * them them - done
66 *
67 * import:
68 * them them - before
69 * me, them me - journaled EImportStart
70 * me me - done
71 *
72 * which implies:
73 * - auth bit is set if i am listed as first _or_ second dir_auth.
74 */
75
76 #include "common/config.h"
77
78
79 #define dout_context g_ceph_context
80 #define dout_subsys ceph_subsys_mds
81 #undef dout_prefix
82 #define dout_prefix *_dout << "mds." << mds->get_nodeid() << ".migrator "
83
84
85 class MigratorContext : public MDSInternalContextBase {
86 protected:
87 Migrator *mig;
88 MDSRank *get_mds() override {
89 return mig->mds;
90 }
91 public:
92 explicit MigratorContext(Migrator *mig_) : mig(mig_) {
93 assert(mig != NULL);
94 }
95 };
96
97 class MigratorLogContext : public MDSLogContextBase {
98 protected:
99 Migrator *mig;
100 MDSRank *get_mds() override {
101 return mig->mds;
102 }
103 public:
104 explicit MigratorLogContext(Migrator *mig_) : mig(mig_) {
105 assert(mig != NULL);
106 }
107 };
108
109 /* This function DOES put the passed message before returning*/
110 void Migrator::dispatch(Message *m)
111 {
112 switch (m->get_type()) {
113 // import
114 case MSG_MDS_EXPORTDIRDISCOVER:
115 handle_export_discover(static_cast<MExportDirDiscover*>(m));
116 break;
117 case MSG_MDS_EXPORTDIRPREP:
118 handle_export_prep(static_cast<MExportDirPrep*>(m));
119 break;
120 case MSG_MDS_EXPORTDIR:
121 handle_export_dir(static_cast<MExportDir*>(m));
122 break;
123 case MSG_MDS_EXPORTDIRFINISH:
124 handle_export_finish(static_cast<MExportDirFinish*>(m));
125 break;
126 case MSG_MDS_EXPORTDIRCANCEL:
127 handle_export_cancel(static_cast<MExportDirCancel*>(m));
128 break;
129
130 // export
131 case MSG_MDS_EXPORTDIRDISCOVERACK:
132 handle_export_discover_ack(static_cast<MExportDirDiscoverAck*>(m));
133 break;
134 case MSG_MDS_EXPORTDIRPREPACK:
135 handle_export_prep_ack(static_cast<MExportDirPrepAck*>(m));
136 break;
137 case MSG_MDS_EXPORTDIRACK:
138 handle_export_ack(static_cast<MExportDirAck*>(m));
139 break;
140 case MSG_MDS_EXPORTDIRNOTIFYACK:
141 handle_export_notify_ack(static_cast<MExportDirNotifyAck*>(m));
142 break;
143
144 // export 3rd party (dir_auth adjustments)
145 case MSG_MDS_EXPORTDIRNOTIFY:
146 handle_export_notify(static_cast<MExportDirNotify*>(m));
147 break;
148
149 // caps
150 case MSG_MDS_EXPORTCAPS:
151 handle_export_caps(static_cast<MExportCaps*>(m));
152 break;
153 case MSG_MDS_GATHERCAPS:
154 handle_gather_caps(static_cast<MGatherCaps*>(m));
155 break;
156
157 default:
158 derr << "migrator unknown message " << m->get_type() << dendl;
159 assert(0 == "migrator unknown message");
160 }
161 }
162
163
164 class C_MDC_EmptyImport : public MigratorContext {
165 CDir *dir;
166 public:
167 C_MDC_EmptyImport(Migrator *m, CDir *d) : MigratorContext(m), dir(d) {}
168 void finish(int r) override {
169 mig->export_empty_import(dir);
170 }
171 };
172
173
174 void Migrator::export_empty_import(CDir *dir)
175 {
176 dout(7) << "export_empty_import " << *dir << dendl;
177 assert(dir->is_subtree_root());
178
179 if (dir->inode->is_auth()) {
180 dout(7) << " inode is auth" << dendl;
181 return;
182 }
183 if (!dir->is_auth()) {
184 dout(7) << " not auth" << dendl;
185 return;
186 }
187 if (dir->is_freezing() || dir->is_frozen()) {
188 dout(7) << " freezing or frozen" << dendl;
189 return;
190 }
191 if (dir->get_num_head_items() > 0) {
192 dout(7) << " not actually empty" << dendl;
193 return;
194 }
195 if (dir->inode->is_root()) {
196 dout(7) << " root" << dendl;
197 return;
198 }
199
200 mds_rank_t dest = dir->inode->authority().first;
201 //if (mds->is_shutting_down()) dest = 0; // this is more efficient.
202
203 dout(7) << " really empty, exporting to " << dest << dendl;
204 assert (dest != mds->get_nodeid());
205
206 dout(7) << "exporting to mds." << dest
207 << " empty import " << *dir << dendl;
208 export_dir( dir, dest );
209 }
210
211 void Migrator::find_stale_export_freeze()
212 {
213 utime_t now = ceph_clock_now();
214 utime_t cutoff = now;
215 cutoff -= g_conf->mds_freeze_tree_timeout;
216
217
218 /*
219 * We could have situations like:
220 *
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)
231 *
232 *
233 * - client request authpins items in subtree B
234 * - freeze subtree B
235 * - import subtree A which is parent of subtree B
236 * (authpins parent inode of subtree B, see CDir::set_dir_auth())
237 * - freeze subtree A
238 * - client request tries authpinning items in subtree A
239 * (wait because subtree A is freezing)
240 */
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;
245 ++p;
246 if (stat.state != EXPORT_DISCOVERING && stat.state != EXPORT_FREEZING)
247 continue;
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;
251 continue;
252 }
253 if (stat.last_cum_auth_pins_change >= cutoff)
254 continue;
255 if (stat.num_remote_waiters > 0 ||
256 (!dir->inode->is_root() && dir->get_parent_dir()->is_freezing())) {
257 export_try_cancel(dir);
258 }
259 }
260 }
261
262 void Migrator::export_try_cancel(CDir *dir, bool notify_peer)
263 {
264 dout(10) << "export_try_cancel " << *dir << dendl;
265
266 map<CDir*,export_state_t>::iterator it = export_state.find(dir);
267 assert(it != export_state.end());
268
269 int state = it->second.state;
270 switch (state) {
271 case EXPORT_LOCKING:
272 dout(10) << "export state=locking : dropping locks and removing auth_pin" << dendl;
273 it->second.state = EXPORT_CANCELLED;
274 dir->auth_unpin(this);
275 break;
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);
281 if (notify_peer &&
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);
285 break;
286
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);
293 if (notify_peer &&
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);
297 break;
298
299 // NOTE: state order reversal, warning comes after prepping
300 case EXPORT_WARNING:
301 dout(10) << "export state=warning : unpinning bounds, unfreezing, notifying" << dendl;
302 it->second.state = EXPORT_CANCELLING;
303 // fall-thru
304
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;
309 }
310
311 {
312 // unpin bounds
313 set<CDir*> bounds;
314 cache->get_subtree_bounds(dir, bounds);
315 for (set<CDir*>::iterator q = bounds.begin();
316 q != bounds.end();
317 ++q) {
318 CDir *bd = *q;
319 bd->put(CDir::PIN_EXPORTBOUND);
320 bd->state_clear(CDir::STATE_EXPORTBOUND);
321 }
322 if (state == EXPORT_WARNING) {
323 // notify bystanders
324 export_notify_abort(dir, it->second, bounds);
325 // process delayed expires
326 cache->process_delayed_expire(dir);
327 }
328 }
329 dir->unfreeze_tree();
330 cache->try_subtree_merge(dir);
331 for (auto bd : it->second.residual_dirs) {
332 bd->unfreeze_tree();
333 cache->try_subtree_merge(bd);
334 }
335 if (notify_peer &&
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);
339 break;
340
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);
345 break;
346
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.
351 break;
352 case EXPORT_CANCELLING:
353 break;
354
355 default:
356 ceph_abort();
357 }
358
359 // finish clean-up?
360 if (it->second.state == EXPORT_CANCELLING ||
361 it->second.state == EXPORT_CANCELLED) {
362 MutationRef mut;
363 mut.swap(it->second.mut);
364
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();
370 }
371
372 // drop locks
373 if (state == EXPORT_LOCKING || state == EXPORT_DISCOVERING) {
374 MDRequestRef mdr = static_cast<MDRequestImpl*>(mut.get());
375 assert(mdr);
376 if (mdr->more()->waiting_on_slave.empty())
377 mds->mdcache->request_finish(mdr);
378 } else if (mut) {
379 mds->locker->drop_locks(mut.get());
380 mut->cleanup();
381 }
382
383 cache->show_subtrees();
384
385 maybe_do_queued_export();
386 }
387 }
388
389 void Migrator::export_cancel_finish(CDir *dir)
390 {
391 assert(dir->state_test(CDir::STATE_EXPORTING));
392 dir->state_clear(CDir::STATE_EXPORTING);
393
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();
398 }
399
400 // ==========================================================
401 // mds failure handling
402
403 void Migrator::handle_mds_failure_or_stop(mds_rank_t who)
404 {
405 dout(5) << "handle_mds_failure_or_stop mds." << who << dendl;
406
407 // check my exports
408
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
413 // to.
414 list<CDir*> pinned_dirs;
415 for (map<CDir*,export_state_t>::iterator p = export_state.begin();
416 p != export_state.end();
417 ++p) {
418 if (p->second.state == EXPORT_FREEZING) {
419 CDir *dir = p->first;
420 dout(10) << "adding temp auth_pin on freezing " << *dir << dendl;
421 dir->auth_pin(this);
422 pinned_dirs.push_back(dir);
423 }
424 }
425
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;
429 ++next;
430 CDir *dir = p->first;
431
432 // abort exports:
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) {
447 // bystander failed.
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
454 << dendl;
455 if (p->second.warning_ack_waiting.empty())
456 export_go(dir);
457 }
458 }
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
463 << dendl;
464 if (p->second.state == EXPORT_NOTIFYING) {
465 if (p->second.notify_ack_waiting.empty())
466 export_finish(dir);
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);
471 }
472 }
473 }
474 }
475
476 // next!
477 p = next;
478 }
479
480
481 // check my imports
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;
485 ++next;
486 dirfrag_t df = q->first;
487 CInode *diri = mds->mdcache->get_inode(df.ino);
488 CDir *dir = mds->mdcache->get_dirfrag(df);
489
490 if (q->second.peer == who) {
491 if (dir)
492 dout(10) << "cleaning up import state (" << q->second.state << ")"
493 << get_import_statename(q->second.state) << " of " << *dir << dendl;
494 else
495 dout(10) << "cleaning up import state (" << q->second.state << ")"
496 << get_import_statename(q->second.state) << " of " << df << dendl;
497
498 switch (q->second.state) {
499 case IMPORT_DISCOVERING:
500 dout(10) << "import state=discovering : clearing state" << dendl;
501 import_reverse_discovering(df);
502 break;
503
504 case IMPORT_DISCOVERED:
505 assert(diri);
506 dout(10) << "import state=discovered : unpinning inode " << *diri << dendl;
507 import_reverse_discovered(df, diri);
508 break;
509
510 case IMPORT_PREPPING:
511 assert(dir);
512 dout(10) << "import state=prepping : unpinning base+bounds " << *dir << dendl;
513 import_reverse_prepping(dir, q->second);
514 break;
515
516 case IMPORT_PREPPED:
517 assert(dir);
518 dout(10) << "import state=prepped : unpinning base+bounds, unfreezing " << *dir << dendl;
519 {
520 set<CDir*> bounds;
521 cache->get_subtree_bounds(dir, bounds);
522 import_remove_pins(dir, bounds);
523
524 // adjust auth back to the exporter
525 cache->adjust_subtree_auth(dir, q->second.peer);
526
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);
531 }
532 break;
533
534 case IMPORT_LOGGINGSTART:
535 assert(dir);
536 dout(10) << "import state=loggingstart : reversing import on " << *dir << dendl;
537 import_reverse(dir);
538 break;
539
540 case IMPORT_ACKING:
541 assert(dir);
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;
544 {
545 set<CDir*> bounds;
546 cache->get_subtree_bounds(dir, bounds);
547 cache->add_ambiguous_import(dir, bounds);
548 }
549 break;
550
551 case IMPORT_FINISHING:
552 assert(dir);
553 dout(10) << "import state=finishing : finishing import on " << *dir << dendl;
554 import_finish(dir, true);
555 break;
556
557 case IMPORT_ABORTING:
558 assert(dir);
559 dout(10) << "import state=aborting : ignoring repeat failure " << *dir << dendl;
560 break;
561 }
562 } else {
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) {
567 assert(dir);
568 dout(10) << "faking export_notify_ack from mds." << who
569 << " on aborting import " << *dir << " from mds." << q->second.peer
570 << dendl;
571 if (q->second.bystanders.empty())
572 import_reverse_unfreeze(dir);
573 }
574 }
575 }
576
577 // next!
578 q = next;
579 }
580
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();
586 }
587 }
588
589
590
591 void Migrator::show_importing()
592 {
593 dout(10) << "show_importing" << dendl;
594 for (map<dirfrag_t,import_state_t>::iterator p = import_state.begin();
595 p != import_state.end();
596 ++p) {
597 CDir *dir = mds->mdcache->get_dirfrag(p->first);
598 if (dir) {
599 dout(10) << " importing from " << p->second.peer
600 << ": (" << p->second.state << ") " << get_import_statename(p->second.state)
601 << " " << p->first << " " << *dir << dendl;
602 } else {
603 dout(10) << " importing from " << p->second.peer
604 << ": (" << p->second.state << ") " << get_import_statename(p->second.state)
605 << " " << p->first << dendl;
606 }
607 }
608 }
609
610 void Migrator::show_exporting()
611 {
612 dout(10) << "show_exporting" << dendl;
613 for (map<CDir*,export_state_t>::iterator p = export_state.begin();
614 p != export_state.end();
615 ++p)
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;
619 }
620
621
622
623 void Migrator::audit()
624 {
625 if (!g_conf->subsys.should_gather(ceph_subsys_mds, 5))
626 return; // hrm.
627
628 // import_state
629 show_importing();
630 for (map<dirfrag_t,import_state_t>::iterator p = import_state.begin();
631 p != import_state.end();
632 ++p) {
633 if (p->second.state == IMPORT_DISCOVERING)
634 continue;
635 if (p->second.state == IMPORT_DISCOVERED) {
636 CInode *in = cache->get_inode(p->first.ino);
637 assert(in);
638 continue;
639 }
640 CDir *dir = cache->get_dirfrag(p->first);
641 assert(dir);
642 if (p->second.state == IMPORT_PREPPING)
643 continue;
644 if (p->second.state == IMPORT_ABORTING) {
645 assert(!dir->is_ambiguous_dir_auth());
646 assert(dir->get_dir_auth().first != mds->get_nodeid());
647 continue;
648 }
649 assert(dir->is_ambiguous_dir_auth());
650 assert(dir->authority().first == mds->get_nodeid() ||
651 dir->authority().second == mds->get_nodeid());
652 }
653
654 // export_state
655 show_exporting();
656 for (map<CDir*,export_state_t>::iterator p = export_state.begin();
657 p != export_state.end();
658 ++p) {
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)
664 continue;
665 assert(dir->is_ambiguous_dir_auth());
666 assert(dir->authority().first == mds->get_nodeid() ||
667 dir->authority().second == mds->get_nodeid());
668 }
669
670 // ambiguous+me subtrees should be importing|exporting
671
672 // write me
673 }
674
675
676
677
678
679 // ==========================================================
680 // EXPORT
681
682 void Migrator::export_dir_nicely(CDir *dir, mds_rank_t dest)
683 {
684 // enqueue
685 dout(7) << "export_dir_nicely " << *dir << " to " << dest << dendl;
686 export_queue.push_back(pair<dirfrag_t,mds_rank_t>(dir->dirfrag(), dest));
687
688 maybe_do_queued_export();
689 }
690
691 void Migrator::maybe_do_queued_export()
692 {
693 static bool running;
694 if (running)
695 return;
696 running = true;
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();
702
703 CDir *dir = mds->mdcache->get_dirfrag(df);
704 if (!dir) continue;
705 if (!dir->is_auth()) continue;
706
707 dout(0) << "nicely exporting to mds." << dest << " " << *dir << dendl;
708
709 export_dir(dir, dest);
710 }
711 running = false;
712 }
713
714
715
716
717 class C_MDC_ExportFreeze : public MigratorContext {
718 CDir *ex; // dir i'm exporting
719 uint64_t tid;
720 public:
721 C_MDC_ExportFreeze(Migrator *m, CDir *e, uint64_t t) :
722 MigratorContext(m), ex(e), tid(t) {
723 assert(ex != NULL);
724 }
725 void finish(int r) override {
726 if (r >= 0)
727 mig->export_frozen(ex, tid);
728 }
729 };
730
731
732 void Migrator::get_export_lock_set(CDir *dir, set<SimpleLock*>& locks)
733 {
734 // path
735 vector<CDentry*> trace;
736 cache->make_trace(trace, dir->inode);
737 for (vector<CDentry*>::iterator it = trace.begin();
738 it != trace.end();
739 ++it)
740 locks.insert(&(*it)->lock);
741
742 // prevent scatter gather race
743 locks.insert(&dir->get_inode()->dirfragtreelock);
744
745 // bound dftlocks:
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);
758 }
759
760
761 class C_M_ExportDirWait : public MigratorContext {
762 MDRequestRef mdr;
763 int count;
764 public:
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);
769 }
770 };
771
772
773 /** export_dir(dir, dest)
774 * public method to initiate an export.
775 * will fail if the directory is freezing, frozen, unpinnable, or root.
776 */
777 void Migrator::export_dir(CDir *dir, mds_rank_t dest)
778 {
779 dout(7) << "export_dir " << *dir << " to " << dest << dendl;
780 assert(dir->is_auth());
781 assert(dest != mds->get_nodeid());
782
783 if (!(mds->is_active() || mds->is_stopping())) {
784 dout(7) << "i'm not active, no exports for now" << dendl;
785 return;
786 }
787 if (mds->mdcache->is_readonly()) {
788 dout(7) << "read-only FS, no exports for now" << dendl;
789 return;
790 }
791 if (!mds->mdsmap->is_active(dest)) {
792 dout(7) << "dest not active, no exports for now" << dendl;
793 return;
794 }
795 if (mds->is_cluster_degraded()) {
796 dout(7) << "cluster degraded, no exports for now" << dendl;
797 return;
798 }
799 if (dir->inode->is_system()) {
800 dout(7) << "i won't export system dirs (root, mdsdirs, stray, /.ceph, etc.)" << dendl;
801 //ceph_abort();
802 return;
803 }
804
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;
809 return;
810 }
811 } else {
812 if (!mds->is_stopping() && !dir->inode->is_exportable(dest)) {
813 dout(7) << "dir is export pinned" << dendl;
814 return;
815 }
816 }
817
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;
821 return;
822 }
823 if (dir->state_test(CDir::STATE_EXPORTING)) {
824 dout(7) << "already exporting" << dendl;
825 return;
826 }
827
828 if (g_conf->mds_thrash_exports) {
829 // create random subtree bound (which will not be exported)
830 list<CDir*> ls;
831 for (auto p = dir->begin(); p != dir->end(); ++p) {
832 auto dn = p->second;
833 CDentry::linkage_t *dnl= dn->get_linkage();
834 if (dnl->is_primary()) {
835 CInode *in = dnl->get_inode();
836 if (in->is_dir())
837 in->get_nested_dirfrags(ls);
838 }
839 }
840 if (ls.size() > 0) {
841 int n = rand() % ls.size();
842 auto p = ls.begin();
843 while (n--) ++p;
844 CDir *bd = *p;
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;
850 }
851 }
852 }
853
854 mds->hit_export_target(ceph_clock_now(), dest, -1);
855
856 dir->auth_pin(this);
857 dir->state_set(CDir::STATE_EXPORTING);
858
859 MDRequestRef mdr = mds->mdcache->request_start_internal(CEPH_MDS_OP_EXPORTDIR);
860 mdr->more()->export_dir = dir;
861
862 assert(export_state.count(dir) == 0);
863 export_state_t& stat = export_state[dir];
864 stat.state = EXPORT_LOCKING;
865 stat.peer = dest;
866 stat.tid = mdr->reqid.tid;
867 stat.mut = mdr;
868
869 return mds->mdcache->dispatch_request(mdr);
870 }
871
872 void Migrator::dispatch_export_dir(MDRequestRef& mdr, int count)
873 {
874 dout(7) << "dispatch_export_dir " << *mdr << dendl;
875
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);
882 return;
883 }
884 assert(it->second.state == EXPORT_LOCKING);
885
886 mds_rank_t dest = it->second.peer;
887
888 if (!mds->is_export_target(dest)) {
889 dout(7) << "dest is not yet an export target" << dendl;
890 if (count > 3) {
891 dout(5) << "dest has not been added as export target after three MDSMap epochs, canceling export" << dendl;
892 export_try_cancel(dir);
893 return;
894 }
895
896 mds->locker->drop_locks(mdr.get());
897 mdr->drop_local_auth_pins();
898
899 mds->wait_for_mdsmap(mds->mdsmap->get_epoch(), new C_M_ExportDirWait(this, mdr, count+1));
900 return;
901 }
902
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));
906 return;
907 }
908
909 if (mdr->aborted || dir->is_frozen() || dir->is_freezing()) {
910 dout(7) << "wouldblock|freezing|frozen, canceling export" << dendl;
911 export_try_cancel(dir);
912 return;
913 }
914
915 // locks?
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();
932 }
933
934 if (!mds->locker->acquire_locks(mdr, rdlocks, wrlocks, xlocks, NULL, NULL, true)) {
935 if (mdr->aborted)
936 export_try_cancel(dir);
937 return;
938 }
939
940 assert(g_conf->mds_kill_export_at != 1);
941 it->second.state = EXPORT_DISCOVERING;
942
943 // send ExportDirDiscover (ask target)
944 filepath path;
945 dir->inode->make_path(path);
946 MExportDirDiscover *discover = new MExportDirDiscover(dir->dirfrag(), path,
947 mds->get_nodeid(),
948 it->second.tid);
949 mds->send_message_mds(discover, dest);
950 assert(g_conf->mds_kill_export_at != 2);
951
952 it->second.last_cum_auth_pins_change = ceph_clock_now();
953
954 // start the freeze, but hold it up with an auth_pin.
955 dir->freeze_tree();
956 assert(dir->is_freezing_tree());
957 dir->add_waiter(CDir::WAIT_FROZEN, new C_MDC_ExportFreeze(this, dir, it->second.tid));
958 }
959
960 /*
961 * called on receipt of MExportDirDiscoverAck
962 * the importer now has the directory's _inode_ in memory, and pinned.
963 *
964 * This function DOES put the passed message before returning
965 */
966 void Migrator::handle_export_discover_ack(MExportDirDiscoverAck *m)
967 {
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();
971 assert(dir);
972
973 dout(7) << "export_discover_ack from " << m->get_source()
974 << " on " << *dir << dendl;
975
976 mds->hit_export_target(now, dest, -1);
977
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;
983 } else {
984 assert(it->second.state == EXPORT_DISCOVERING);
985
986 if (m->is_success()) {
987 // release locks to avoid deadlock
988 MDRequestRef mdr = static_cast<MDRequestImpl*>(it->second.mut.get());
989 assert(mdr);
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);
996
997 } else {
998 dout(7) << "peer failed to discover (not active?), canceling" << dendl;
999 export_try_cancel(dir, false);
1000 }
1001 }
1002
1003 m->put(); // done
1004 }
1005
1006 class C_M_ExportSessionsFlushed : public MigratorContext {
1007 CDir *dir;
1008 uint64_t tid;
1009 public:
1010 C_M_ExportSessionsFlushed(Migrator *m, CDir *d, uint64_t t)
1011 : MigratorContext(m), dir(d), tid(t) {
1012 assert(dir != NULL);
1013 }
1014 void finish(int r) override {
1015 mig->export_sessions_flushed(dir, tid);
1016 }
1017 };
1018
1019 void Migrator::export_sessions_flushed(CDir *dir, uint64_t tid)
1020 {
1021 dout(7) << "export_sessions_flushed " << *dir << dendl;
1022
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;
1029 return;
1030 }
1031
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.
1037 }
1038
1039 void Migrator::export_frozen(CDir *dir, uint64_t tid)
1040 {
1041 dout(7) << "export_frozen on " << *dir << dendl;
1042
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;
1046 return;
1047 }
1048
1049 assert(it->second.state == EXPORT_FREEZING);
1050 assert(dir->is_frozen_tree_root());
1051 assert(dir->get_cum_auth_pins() == 0);
1052
1053 CInode *diri = dir->get_inode();
1054
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. "
1063 << *dir << dendl;
1064 // .. unwind ..
1065 dir->unfreeze_tree();
1066 cache->try_subtree_merge(dir);
1067
1068 mds->send_message_mds(new MExportDirCancel(dir->dirfrag(), it->second.tid), it->second.peer);
1069 export_state.erase(it);
1070
1071 dir->state_clear(CDir::STATE_EXPORTING);
1072 cache->maybe_send_pending_resolves();
1073 return;
1074 }
1075
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);
1082
1083 cache->show_subtrees();
1084
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()));
1087
1088 set<client_t> export_client_set;
1089 check_export_size(dir, it->second, export_client_set);
1090
1091 // note the bounds.
1092 set<CDir*> bounds;
1093 cache->get_subtree_bounds(dir, bounds);
1094
1095 // generate prep message, log entry.
1096 MExportDirPrep *prep = new MExportDirPrep(dir->dirfrag(), it->second.tid);
1097
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);
1103 }
1104 }
1105
1106 // include base dirfrag
1107 cache->replicate_dir(dir, it->second.peer, prep->basedir);
1108
1109 /*
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.
1115 *
1116 * each trace is: df ('-' | ('f' dir | 'd') dentry inode (dir dentry inode)*)
1117 */
1118 set<inodeno_t> inodes_added;
1119 set<dirfrag_t> dirfrags_added;
1120
1121 // check bounds
1122 for (set<CDir*>::iterator p = bounds.begin();
1123 p != bounds.end();
1124 ++p) {
1125 CDir *bound = *p;
1126
1127 // pin it.
1128 assert(bound->state_test(CDir::STATE_EXPORTBOUND));
1129
1130 dout(7) << " export bound " << *bound << dendl;
1131 prep->add_bound( bound->dirfrag() );
1132
1133 // trace to bound
1134 bufferlist tracebl;
1135 CDir *cur = bound;
1136
1137 char start = '-';
1138 if (it->second.residual_dirs.count(bound)) {
1139 start = 'f';
1140 cache->replicate_dir(bound, it->second.peer, tracebl);
1141 dout(7) << " added " << *bound << dendl;
1142 }
1143
1144 while (1) {
1145 // don't repeat inodes
1146 if (inodes_added.count(cur->inode->ino()))
1147 break;
1148 inodes_added.insert(cur->inode->ino());
1149
1150 // prepend dentry + inode
1151 assert(cur->inode->is_auth());
1152 bufferlist bl;
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);
1159 tracebl.claim(bl);
1160
1161 cur = cur->get_parent_dir();
1162
1163 // don't repeat dirfrags
1164 if (dirfrags_added.count(cur->dirfrag()) ||
1165 cur == dir) {
1166 start = 'd'; // start with dentry
1167 break;
1168 }
1169 dirfrags_added.insert(cur->dirfrag());
1170
1171 // prepend dir
1172 cache->replicate_dir(cur, it->second.peer, bl);
1173 dout(7) << " added " << *cur << dendl;
1174 bl.claim_append(tracebl);
1175 tracebl.claim(bl);
1176
1177 start = 'f'; // start with dirfrag
1178 }
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);
1185 }
1186
1187 // send.
1188 it->second.state = EXPORT_PREPPING;
1189 mds->send_message_mds(prep, it->second.peer);
1190 assert (g_conf->mds_kill_export_at != 4);
1191
1192 // make sure any new instantiations of caps are flushed out
1193 assert(it->second.warning_ack_waiting.empty());
1194
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));
1200 gather.activate();
1201 }
1202 }
1203
1204 void Migrator::check_export_size(CDir *dir, export_state_t& stat, set<client_t>& client_set)
1205 {
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;
1211
1212 uint64_t max_size = g_conf->get_val<uint64_t>("mds_max_export_size");
1213 uint64_t approx_size = 0;
1214
1215 list<CDir*> dfs;
1216 dfs.push_back(dir);
1217 while (!dfs.empty()) {
1218 CDir *dir = dfs.front();
1219 dfs.pop_front();
1220
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;
1226 continue;
1227 }
1228 if (dn->get_linkage()->is_remote()) {
1229 approx_size += link_size;
1230 continue;
1231 }
1232
1233 approx_size += inode_size;
1234 CInode *in = dn->get_linkage()->get_inode();
1235 if (in->is_dir()) {
1236 // directory?
1237 list<CDir*> ls;
1238 in->get_dirfrags(ls);
1239 for (auto q : ls) {
1240 if (q->is_subtree_root()) {
1241 q->state_set(CDir::STATE_EXPORTBOUND);
1242 q->get(CDir::PIN_EXPORTBOUND);
1243 } else {
1244 // include nested dirfrag
1245 assert(q->get_dir_auth().first == CDIR_AUTH_PARENT);
1246 dfs.push_front(q);
1247 }
1248 }
1249 }
1250 for (map<client_t, Capability*>::iterator q = in->client_caps.begin();
1251 q != in->client_caps.end();
1252 ++q) {
1253 approx_size += cap_size;
1254 client_set.insert(q->first);
1255 }
1256 }
1257
1258 if (approx_size >= max_size)
1259 break;
1260 }
1261
1262 while (!dfs.empty()) {
1263 CDir *dir = dfs.front();
1264 dfs.pop_front();
1265
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);
1270
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();
1275
1276 stat.residual_dirs.insert(dir);
1277 }
1278 }
1279
1280 void Migrator::get_export_client_set(CInode *in, set<client_t>& client_set)
1281 {
1282 for (map<client_t, Capability*>::iterator q = in->client_caps.begin();
1283 q != in->client_caps.end();
1284 ++q)
1285 client_set.insert(q->first);
1286 }
1287
1288 /* This function DOES put the passed message before returning*/
1289 void Migrator::handle_export_prep_ack(MExportDirPrepAck *m)
1290 {
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();
1294 assert(dir);
1295
1296 dout(7) << "export_prep_ack " << *dir << dendl;
1297
1298 mds->hit_export_target(now, dest, -1);
1299
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;
1306 m->put();
1307 return;
1308 }
1309 assert(it->second.state == EXPORT_PREPPING);
1310
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);
1314 m->put();
1315 return;
1316 }
1317
1318 assert (g_conf->mds_kill_export_at != 5);
1319 // send warnings
1320 set<CDir*> bounds;
1321 cache->get_subtree_bounds(dir, bounds);
1322
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());
1327
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!
1335
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);
1342
1343 }
1344
1345 it->second.state = EXPORT_WARNING;
1346
1347 assert(g_conf->mds_kill_export_at != 6);
1348 // nobody to warn?
1349 if (it->second.warning_ack_waiting.empty())
1350 export_go(dir); // start export.
1351
1352 // done.
1353 m->put();
1354 }
1355
1356
1357 class C_M_ExportGo : public MigratorContext {
1358 CDir *dir;
1359 uint64_t tid;
1360 public:
1361 C_M_ExportGo(Migrator *m, CDir *d, uint64_t t) :
1362 MigratorContext(m), dir(d), tid(t) {
1363 assert(dir != NULL);
1364 }
1365 void finish(int r) override {
1366 mig->export_go_synced(dir, tid);
1367 }
1368 };
1369
1370 void Migrator::export_go(CDir *dir)
1371 {
1372 auto it = export_state.find(dir);
1373 assert(it != export_state.end());
1374 dout(7) << "export_go " << *dir << " to " << it->second.peer << dendl;
1375
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();
1379 }
1380
1381 void Migrator::export_go_synced(CDir *dir, uint64_t tid)
1382 {
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;
1389 return;
1390 }
1391 assert(it->second.state == EXPORT_WARNING);
1392 mds_rank_t dest = it->second.peer;
1393
1394 dout(7) << "export_go_synced " << *dir << " to " << dest << dendl;
1395
1396 cache->show_subtrees();
1397
1398 it->second.state = EXPORT_EXPORTING;
1399 assert(g_conf->mds_kill_export_at != 7);
1400
1401 assert(dir->is_frozen_tree_root());
1402 assert(dir->get_cum_auth_pins() == 0);
1403
1404 // set ambiguous auth
1405 cache->adjust_subtree_auth(dir, mds->get_nodeid(), dest);
1406
1407 // take away the popularity we're sending.
1408 utime_t now = ceph_clock_now();
1409 mds->balancer->subtract_export(dir, now);
1410
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,
1417 now);
1418 ::encode(exported_client_map, req->client_map,
1419 mds->mdsmap->get_up_features());
1420
1421 // add bounds to message
1422 set<CDir*> bounds;
1423 cache->get_subtree_bounds(dir, bounds);
1424 for (set<CDir*>::iterator p = bounds.begin();
1425 p != bounds.end();
1426 ++p)
1427 req->add_export((*p)->dirfrag());
1428
1429 // send
1430 mds->send_message_mds(req, dest);
1431 assert(g_conf->mds_kill_export_at != 8);
1432
1433 mds->hit_export_target(now, dest, num_exported_inodes+1);
1434
1435 // stats
1436 if (mds->logger) mds->logger->inc(l_mds_exported);
1437 if (mds->logger) mds->logger->inc(l_mds_exported_inodes, num_exported_inodes);
1438
1439 cache->show_subtrees();
1440 }
1441
1442
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)
1447 *
1448 * FIXME: the separation between CInode.encode_export and these methods
1449 * is pretty arbitrary and dumb.
1450 */
1451 void Migrator::encode_export_inode(CInode *in, bufferlist& enc_state,
1452 map<client_t,entity_inst_t>& exported_client_map)
1453 {
1454 dout(7) << "encode_export_inode " << *in << dendl;
1455 assert(!in->is_replica(mds->get_nodeid()));
1456
1457 // relax locks?
1458 if (!in->is_replicated()) {
1459 in->replicate_relax_locks();
1460 dout(20) << " did replicate_relax_locks, now " << *in << dendl;
1461 }
1462
1463 ::encode(in->inode.ino, enc_state);
1464 ::encode(in->last, enc_state);
1465 in->encode_export(enc_state);
1466
1467 // caps
1468 encode_export_inode_caps(in, true, enc_state, exported_client_map);
1469 }
1470
1471 void Migrator::encode_export_inode_caps(CInode *in, bool auth_cap, bufferlist& bl,
1472 map<client_t,entity_inst_t>& exported_client_map)
1473 {
1474 dout(20) << "encode_export_inode_caps " << *in << dendl;
1475
1476 // encode caps
1477 map<client_t,Capability::Export> cap_map;
1478 in->export_client_caps(cap_map);
1479 ::encode(cap_map, bl);
1480 if (auth_cap) {
1481 ::encode(in->get_mds_caps_wanted(), bl);
1482
1483 in->state_set(CInode::STATE_EXPORTINGCAPS);
1484 in->get(CInode::PIN_EXPORTINGCAPS);
1485 }
1486
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();
1490 ++it)
1491 exported_client_map[it->first] = mds->sessionmap.get_inst(entity_name_t::CLIENT(it->first.v));
1492 }
1493
1494 void Migrator::finish_export_inode_caps(CInode *in, mds_rank_t peer,
1495 map<client_t,Capability::Import>& peer_imported)
1496 {
1497 dout(20) << "finish_export_inode_caps " << *in << dendl;
1498
1499 in->state_clear(CInode::STATE_EXPORTINGCAPS);
1500 in->put(CInode::PIN_EXPORTINGCAPS);
1501
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();
1505 ++it) {
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());
1511
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);
1516 }
1517 in->clear_client_caps_after_export();
1518 mds->locker->eval(in, CEPH_CAP_LOCKS);
1519 }
1520
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)
1524 {
1525 dout(12) << "finish_export_inode " << *in << dendl;
1526
1527 // clean
1528 if (in->is_dirty())
1529 in->mark_clean();
1530
1531 // clear/unpin cached_by (we're no longer the authority)
1532 in->clear_replica_map();
1533
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();
1544
1545 // mark auth
1546 assert(in->is_auth());
1547 in->state_clear(CInode::STATE_AUTH);
1548 in->replica_nonce = CInode::EXPORT_NONCE;
1549
1550 in->clear_dirty_rstat();
1551
1552 // no more auth subtree? clear scatter dirty
1553 if (!in->has_subtree_root_dirfrag(mds->get_nodeid()))
1554 in->clear_scatter_dirty();
1555
1556 in->item_open_file.remove_myself();
1557
1558 in->clear_dirty_parent();
1559
1560 in->clear_file_locks();
1561
1562 // waiters
1563 in->take_waiting(CInode::WAIT_ANY_MASK, finished);
1564
1565 in->finish_export(now);
1566
1567 finish_export_inode_caps(in, peer, peer_imported);
1568 }
1569
1570 uint64_t Migrator::encode_export_dir(bufferlist& exportbl,
1571 CDir *dir,
1572 map<client_t,entity_inst_t>& exported_client_map,
1573 utime_t now)
1574 {
1575 uint64_t num_exported = 0;
1576
1577 dout(7) << "encode_export_dir " << *dir << " " << dir->get_num_head_items() << " head items" << dendl;
1578
1579 assert(dir->get_projected_version() == dir->get_version());
1580
1581 #ifdef MDS_VERIFY_FRAGSTAT
1582 if (dir->is_complete())
1583 dir->verify_fragstat();
1584 #endif
1585
1586 // dir
1587 dirfrag_t df = dir->dirfrag();
1588 ::encode(df, exportbl);
1589 dir->encode_export(exportbl);
1590
1591 __u32 nden = dir->items.size();
1592 ::encode(nden, exportbl);
1593
1594 // dentries
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();
1600
1601 if (!dn->is_replicated())
1602 dn->lock.replicate_relax();
1603
1604 num_exported++;
1605
1606 // -- dentry
1607 dout(7) << "encode_export_dir exporting " << *dn << dendl;
1608
1609 // dn name
1610 ::encode(dn->name, exportbl);
1611 ::encode(dn->last, exportbl);
1612
1613 // state
1614 dn->encode_export(exportbl);
1615
1616 // points to...
1617
1618 // null dentry?
1619 if (dn->get_linkage()->is_null()) {
1620 exportbl.append("N", 1); // null dentry
1621 continue;
1622 }
1623
1624 if (dn->get_linkage()->is_remote()) {
1625 // remote link
1626 exportbl.append("L", 1); // remote link
1627
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);
1632 continue;
1633 }
1634
1635 // primary link
1636 // -- inode
1637 exportbl.append("I", 1); // inode dentry
1638
1639 encode_export_inode(in, exportbl, exported_client_map); // encode, and (update state for) export
1640
1641 // directory?
1642 list<CDir*> dfs;
1643 in->get_dirfrags(dfs);
1644 for (list<CDir*>::iterator p = dfs.begin(); p != dfs.end(); ++p) {
1645 CDir *t = *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)
1650 }
1651 }
1652 }
1653
1654 // subdirs
1655 for (list<CDir*>::iterator it = subdirs.begin(); it != subdirs.end(); ++it)
1656 num_exported += encode_export_dir(exportbl, *it, exported_client_map, now);
1657
1658 return num_exported;
1659 }
1660
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)
1664 {
1665 dout(10) << "finish_export_dir " << *dir << dendl;
1666
1667 // release open_by
1668 dir->clear_replica_map();
1669
1670 // mark
1671 assert(dir->is_auth());
1672 dir->state_clear(CDir::STATE_AUTH);
1673 dir->remove_bloom();
1674 dir->replica_nonce = CDir::EXPORT_NONCE;
1675
1676 if (dir->is_dirty())
1677 dir->mark_clean();
1678
1679 // suck up all waiters
1680 dir->take_waiting(CDir::WAIT_ANY_MASK, finished); // all dir waiters
1681
1682 // pop
1683 dir->finish_export(now);
1684
1685 // dentries
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();
1691
1692 // dentry
1693 dn->finish_export();
1694
1695 // inode?
1696 if (dn->get_linkage()->is_primary()) {
1697 finish_export_inode(in, now, peer, peer_imported[in->ino()], finished);
1698
1699 // subdirs?
1700 in->get_nested_dirfrags(subdirs);
1701 }
1702
1703 cache->touch_dentry_bottom(dn); // move dentry to tail of LRU
1704 ++(*num_dentries);
1705 }
1706
1707 // subdirs
1708 for (list<CDir*>::iterator it = subdirs.begin(); it != subdirs.end(); ++it)
1709 finish_export_dir(*it, now, peer, peer_imported, finished, num_dentries);
1710 }
1711
1712 class C_MDS_ExportFinishLogged : public MigratorLogContext {
1713 CDir *dir;
1714 public:
1715 C_MDS_ExportFinishLogged(Migrator *m, CDir *d) : MigratorLogContext(m), dir(d) {}
1716 void finish(int r) override {
1717 mig->export_logged_finish(dir);
1718 }
1719 };
1720
1721
1722 /*
1723 * i should get an export_ack from the export target.
1724 *
1725 * This function DOES put the passed message before returning
1726 */
1727 void Migrator::handle_export_ack(MExportDirAck *m)
1728 {
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();
1732 assert(dir);
1733 assert(dir->is_frozen_tree_root()); // i'm exporting!
1734
1735 // yay!
1736 dout(7) << "handle_export_ack " << *dir << dendl;
1737
1738 mds->hit_export_target(now, dest, -1);
1739
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());
1744
1745 bufferlist::iterator bp = m->imported_caps.begin();
1746 ::decode(it->second.peer_imported, bp);
1747
1748 it->second.state = EXPORT_LOGGINGFINISH;
1749 assert (g_conf->mds_kill_export_at != 9);
1750 set<CDir*> bounds;
1751 cache->get_subtree_bounds(dir, bounds);
1752
1753 // log completion.
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);
1757
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();
1761 p != bounds.end();
1762 ++p) {
1763 CDir *bound = *p;
1764 le->get_bounds().insert(bound->dirfrag());
1765 le->metablob.add_dir_context(bound);
1766 le->metablob.add_dir(bound, false);
1767 }
1768
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());
1772
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);
1777
1778 m->put();
1779 }
1780
1781 void Migrator::export_notify_abort(CDir *dir, export_state_t& stat, set<CDir*>& bounds)
1782 {
1783 dout(7) << "export_notify_abort " << *dir << dendl;
1784
1785 assert(stat.state == EXPORT_CANCELLING);
1786
1787 if (stat.notify_ack_waiting.empty()) {
1788 stat.state = EXPORT_CANCELLED;
1789 return;
1790 }
1791
1792 dir->auth_pin(this);
1793
1794 for (set<mds_rank_t>::iterator p = stat.notify_ack_waiting.begin();
1795 p != stat.notify_ack_waiting.end();
1796 ++p) {
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);
1803 }
1804 }
1805
1806 /*
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
1809 * and go on.
1810 */
1811 void Migrator::export_reverse(CDir *dir, export_state_t& stat)
1812 {
1813 dout(7) << "export_reverse " << *dir << dendl;
1814
1815 set<CInode*> to_eval;
1816
1817 set<CDir*> bounds;
1818 cache->get_subtree_bounds(dir, bounds);
1819
1820 // remove exporting pins
1821 list<CDir*> rq;
1822 rq.push_back(dir);
1823 while (!rq.empty()) {
1824 CDir *t = rq.front();
1825 rq.pop_front();
1826 t->abort_export();
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())
1830 continue;
1831 CInode *in = p->second->get_linkage()->get_inode();
1832 in->abort_export();
1833 if (in->state_test(CInode::STATE_EVALSTALECAPS)) {
1834 in->state_clear(CInode::STATE_EVALSTALECAPS);
1835 to_eval.insert(in);
1836 }
1837 if (in->is_dir())
1838 in->get_nested_dirfrags(rq);
1839 }
1840 }
1841
1842 // unpin bounds
1843 for (auto bd : bounds) {
1844 bd->put(CDir::PIN_EXPORTBOUND);
1845 bd->state_clear(CDir::STATE_EXPORTBOUND);
1846 }
1847
1848 // notify bystanders
1849 export_notify_abort(dir, stat, bounds);
1850
1851 // unfreeze tree, with possible subtree merge.
1852 cache->adjust_subtree_auth(dir, mds->get_nodeid(), mds->get_nodeid());
1853
1854 // process delayed expires
1855 cache->process_delayed_expire(dir);
1856
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);
1862 }
1863
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);
1871 } else {
1872 need_issue = true;
1873 }
1874 }
1875 if (need_issue &&
1876 (!in->is_auth() || !mds->locker->eval(in, CEPH_CAP_LOCKS)))
1877 mds->locker->issue_caps(in);
1878 }
1879
1880 cache->show_cache();
1881 }
1882
1883
1884 /*
1885 * once i get the ack, and logged the EExportFinish(true),
1886 * send notifies (if any), otherwise go straight to finish.
1887 *
1888 */
1889 void Migrator::export_logged_finish(CDir *dir)
1890 {
1891 dout(7) << "export_logged_finish " << *dir << dendl;
1892
1893 export_state_t& stat = export_state[dir];
1894
1895 // send notifies
1896 set<CDir*> bounds;
1897 cache->get_subtree_bounds(dir, bounds);
1898
1899 for (set<mds_rank_t>::iterator p = stat.notify_ack_waiting.begin();
1900 p != stat.notify_ack_waiting.end();
1901 ++p) {
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));
1905
1906 for (set<CDir*>::iterator i = bounds.begin(); i != bounds.end(); ++i)
1907 notify->get_bounds().push_back((*i)->dirfrag());
1908
1909 mds->send_message_mds(notify, *p);
1910 }
1911
1912 // wait for notifyacks
1913 stat.state = EXPORT_NOTIFYING;
1914 assert (g_conf->mds_kill_export_at != 11);
1915
1916 // no notifies to wait for?
1917 if (stat.notify_ack_waiting.empty()) {
1918 export_finish(dir); // skip notify/notify_ack stage.
1919 } else {
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);
1924 } else {
1925 dout(7) << "not sending MExportDirFinish, dest has failed" << dendl;
1926 }
1927 }
1928 }
1929
1930 /*
1931 * warning:
1932 * i'll get an ack from each bystander.
1933 * when i get them all, do the export.
1934 * notify:
1935 * i'll get an ack from each bystander.
1936 * when i get them all, unfreeze and send the finish.
1937 *
1938 * This function DOES put the passed message before returning
1939 */
1940 void Migrator::handle_export_notify_ack(MExportDirNotifyAck *m)
1941 {
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();
1945 assert(dir);
1946 mds_rank_t from = mds_rank_t(m->get_source().num());
1947
1948 mds->hit_export_target(now, dest, -1);
1949
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())
1966 export_finish(dir);
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);
1975 }
1976 }
1977 }
1978 else {
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) {
1983 // reversing import
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);
1990 }
1991 }
1992 }
1993
1994 m->put();
1995 }
1996
1997 void Migrator::export_finish(CDir *dir)
1998 {
1999 dout(5) << "export_finish " << *dir << dendl;
2000
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;
2005 return;
2006 }
2007
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);
2012 } else {
2013 dout(7) << "not sending MExportDirFinish last, dest has failed" << dendl;
2014 }
2015 assert(g_conf->mds_kill_export_at != 13);
2016
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);
2022
2023 assert(!dir->is_auth());
2024 cache->adjust_subtree_auth(dir, it->second.peer);
2025
2026 // unpin bounds
2027 set<CDir*> bounds;
2028 cache->get_subtree_bounds(dir, bounds);
2029 for (set<CDir*>::iterator p = bounds.begin();
2030 p != bounds.end();
2031 ++p) {
2032 CDir *bd = *p;
2033 bd->put(CDir::PIN_EXPORTBOUND);
2034 bd->state_clear(CDir::STATE_EXPORTBOUND);
2035 }
2036
2037 if (dir->state_test(CDir::STATE_AUXSUBTREE))
2038 dir->state_clear(CDir::STATE_AUXSUBTREE);
2039
2040 // discard delayed expires
2041 cache->discard_delayed_expire(dir);
2042
2043 dout(7) << "export_finish unfreezing" << dendl;
2044
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);
2054 }
2055
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);
2062 }
2063
2064 if (!finished.empty())
2065 mds->queue_waiters(finished);
2066
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);
2071
2072 cache->show_subtrees();
2073 audit();
2074
2075 cache->trim(num_dentries); // try trimming exported dentries
2076
2077 // send pending import_maps?
2078 mds->mdcache->maybe_send_pending_resolves();
2079
2080 // drop locks, unpin path
2081 if (mut) {
2082 mds->locker->drop_locks(mut.get());
2083 mut->cleanup();
2084 }
2085
2086 maybe_do_queued_export();
2087 }
2088
2089
2090
2091
2092
2093
2094
2095
2096 // ==========================================================
2097 // IMPORT
2098
2099 void Migrator::handle_export_discover(MExportDirDiscover *m)
2100 {
2101 mds_rank_t from = m->get_source_mds();
2102 assert(from != mds->get_nodeid());
2103
2104 dout(7) << "handle_export_discover on " << m->get_path() << dendl;
2105
2106 // note import state
2107 dirfrag_t df = m->get_dirfrag();
2108
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);
2112 m->put();
2113 return;
2114 }
2115
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);
2119 if (!m->started) {
2120 assert(it == import_state.end());
2121 m->started = true;
2122 p_state = &import_state[df];
2123 p_state->state = IMPORT_DISCOVERING;
2124 p_state->peer = from;
2125 p_state->tid = m->get_tid();
2126 } else {
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;
2132 m->put();
2133 return;
2134 }
2135 assert(it->second.state == IMPORT_DISCOVERING);
2136 p_state = &it->second;
2137 }
2138
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));
2142 return;
2143 }
2144
2145 assert (g_conf->mds_kill_import_at != 1);
2146
2147 // do we have it?
2148 CInode *in = cache->get_inode(m->get_dirfrag().ino);
2149 if (!in) {
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);
2155 if (r > 0) return;
2156 if (r < 0) {
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!!!!
2159 }
2160
2161 ceph_abort(); // this shouldn't happen; the get_inode above would have succeeded.
2162 }
2163
2164 // yay
2165 dout(7) << "handle_export_discover have " << df << " inode " << *in << dendl;
2166
2167 p_state->state = IMPORT_DISCOVERED;
2168
2169 // pin inode in the cache (for now)
2170 assert(in->is_dir());
2171 in->get(CInode::PIN_IMPORTING);
2172
2173 // reply
2174 dout(7) << " sending export_discover_ack on " << *in << dendl;
2175 mds->send_message_mds(new MExportDirDiscoverAck(df, m->get_tid()), p_state->peer);
2176 m->put();
2177 assert (g_conf->mds_kill_import_at != 2);
2178 }
2179
2180 void Migrator::import_reverse_discovering(dirfrag_t df)
2181 {
2182 import_state.erase(df);
2183 }
2184
2185 void Migrator::import_reverse_discovered(dirfrag_t df, CInode *diri)
2186 {
2187 // unpin base
2188 diri->put(CInode::PIN_IMPORTING);
2189 import_state.erase(df);
2190 }
2191
2192 void Migrator::import_reverse_prepping(CDir *dir, import_state_t& stat)
2193 {
2194 set<CDir*> bounds;
2195 cache->map_dirfrag_set(stat.bound_ls, bounds);
2196 import_remove_pins(dir, bounds);
2197 import_reverse_final(dir);
2198 }
2199
2200 /* This function DOES put the passed message before returning*/
2201 void Migrator::handle_export_cancel(MExportDirCancel *m)
2202 {
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);
2212 assert(in);
2213 import_reverse_discovered(df, in);
2214 } else if (it->second.state == IMPORT_PREPPING) {
2215 CDir *dir = mds->mdcache->get_dirfrag(df);
2216 assert(dir);
2217 import_reverse_prepping(dir, it->second);
2218 } else if (it->second.state == IMPORT_PREPPED) {
2219 CDir *dir = mds->mdcache->get_dirfrag(df);
2220 assert(dir);
2221 set<CDir*> bounds;
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);
2227 } else {
2228 assert(0 == "got export_cancel in weird state");
2229 }
2230 m->put();
2231 }
2232
2233 /* This function DOES put the passed message before returning*/
2234 void Migrator::handle_export_prep(MExportDirPrep *m)
2235 {
2236 mds_rank_t oldauth = mds_rank_t(m->get_source().num());
2237 assert(oldauth != mds->get_nodeid());
2238
2239 CDir *dir;
2240 CInode *diri;
2241 list<MDSInternalContextBase*> finished;
2242
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);
2250 assert(diri);
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;
2254 } else {
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;
2259 m->put();
2260 return;
2261 }
2262 assert(it->second.state == IMPORT_PREPPING);
2263 assert(it->second.peer == oldauth);
2264
2265 dir = cache->get_dirfrag(m->get_dirfrag());
2266 assert(dir);
2267 dout(7) << "handle_export_prep on " << *dir << " (subsequent pass)" << dendl;
2268 diri = dir->get_inode();
2269 }
2270 assert(dir->is_auth() == false);
2271
2272 cache->show_subtrees();
2273
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();
2278 ++p) {
2279 dout(10) << " bound " << *p << dendl;
2280 import_bound_fragset[p->ino].insert(p->frag);
2281 }
2282
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!
2287
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);
2293
2294 // bystander list
2295 dout(7) << "bystanders are " << it->second.bystanders << dendl;
2296
2297 // move pin to dir
2298 diri->put(CInode::PIN_IMPORTING);
2299 dir->get(CDir::PIN_IMPORTING);
2300 dir->state_set(CDir::STATE_IMPORTING);
2301
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();
2306 ++p) {
2307 bufferlist::iterator q = p->begin();
2308 dirfrag_t df;
2309 ::decode(df, q);
2310 char start;
2311 ::decode(start, q);
2312 dout(10) << " trace from " << df << " start " << start << " len " << p->length() << dendl;
2313
2314 CDir *cur = 0;
2315 if (start == 'd') {
2316 cur = cache->get_dirfrag(df);
2317 assert(cur);
2318 dout(10) << " had " << *cur << dendl;
2319 } else if (start == 'f') {
2320 CInode *in = cache->get_inode(df.ino);
2321 assert(in);
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 == '-') {
2326 // nothing
2327 } else
2328 assert(0 == "unrecognized start char");
2329
2330 while (!q.end()) {
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;
2335 if (q.end())
2336 break;
2337 cur = cache->add_replica_dir(q, in, oldauth, finished);
2338 dout(10) << " added " << *cur << dendl;
2339 }
2340 }
2341
2342 // make bound sticky
2343 for (map<inodeno_t,fragset_t>::iterator p = import_bound_fragset.begin();
2344 p != import_bound_fragset.end();
2345 ++p) {
2346 CInode *in = cache->get_inode(p->first);
2347 assert(in);
2348 in->get_stickydirs();
2349 dout(7) << " set stickydirs on bound inode " << *in << dendl;
2350 }
2351
2352 } else {
2353 dout(7) << " not doing assim on " << *dir << dendl;
2354 }
2355
2356 if (!finished.empty())
2357 mds->queue_waiters(finished);
2358
2359
2360 bool success = true;
2361 if (mds->is_active()) {
2362 // open all bounds
2363 set<CDir*> import_bounds;
2364 for (map<inodeno_t,fragset_t>::iterator p = import_bound_fragset.begin();
2365 p != import_bound_fragset.end();
2366 ++p) {
2367 CInode *in = cache->get_inode(p->first);
2368 assert(in);
2369
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;
2375
2376 for (list<frag_t>::iterator q = fglist.begin();
2377 q != fglist.end();
2378 ++q) {
2379 CDir *bound = cache->get_dirfrag(dirfrag_t(p->first, *q));
2380 if (!bound) {
2381 dout(7) << " opening bounding dirfrag " << *q << " on " << *in << dendl;
2382 cache->open_remote_dirfrag(in, *q,
2383 new C_MDS_RetryMessage(mds, m));
2384 return;
2385 }
2386
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);
2391 } else {
2392 dout(7) << " already pinned import bound " << *bound << dendl;
2393 }
2394 import_bounds.insert(bound);
2395 }
2396 }
2397
2398 dout(7) << " all ready, noting auth and freezing import region" << dendl;
2399
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);
2407
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);
2413 // freeze.
2414 dir->_freeze_tree();
2415 // note new state
2416 it->second.state = IMPORT_PREPPED;
2417 } else {
2418 dout(7) << " couldn't acquire all needed locks, failing. " << *dir << dendl;
2419 success = false;
2420 }
2421 } else {
2422 dout(7) << " not active, failing. " << *dir << dendl;
2423 success = false;
2424 }
2425
2426 if (!success)
2427 import_reverse_prepping(dir, it->second);
2428
2429 // ok!
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());
2432
2433 assert(g_conf->mds_kill_import_at != 4);
2434 // done
2435 m->put();
2436 }
2437
2438
2439
2440
2441 class C_MDS_ImportDirLoggedStart : public MigratorLogContext {
2442 dirfrag_t df;
2443 CDir *dir;
2444 mds_rank_t from;
2445 public:
2446 map<client_t,entity_inst_t> imported_client_map;
2447 map<client_t,uint64_t> sseqmap;
2448
2449 C_MDS_ImportDirLoggedStart(Migrator *m, CDir *d, mds_rank_t f) :
2450 MigratorLogContext(m), df(d->dirfrag()), dir(d), from(f) {
2451 }
2452 void finish(int r) override {
2453 mig->import_logged_start(df, dir, from, imported_client_map, sseqmap);
2454 }
2455 };
2456
2457 /* This function DOES put the passed message before returning*/
2458 void Migrator::handle_export_dir(MExportDir *m)
2459 {
2460 assert (g_conf->mds_kill_import_at != 5);
2461 CDir *dir = cache->get_dirfrag(m->dirfrag);
2462 assert(dir);
2463
2464 mds_rank_t oldauth = mds_rank_t(m->get_source().num());
2465 dout(7) << "handle_export_dir importing " << *dir << " from " << oldauth << dendl;
2466
2467 assert(!dir->is_auth());
2468
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);
2474
2475 utime_t now = ceph_clock_now();
2476
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());
2479
2480 cache->show_subtrees();
2481
2482 C_MDS_ImportDirLoggedStart *onlogged = new C_MDS_ImportDirLoggedStart(this, dir, oldauth);
2483
2484 // start the journal entry
2485 EImportStart *le = new EImportStart(mds->mdlog, dir->dirfrag(), m->bounds, oldauth);
2486 mds->mdlog->start_entry(le);
2487
2488 le->metablob.add_dir_context(dir);
2489
2490 // adjust auth (list us _first_)
2491 cache->adjust_subtree_auth(dir, mds->get_nodeid(), oldauth);
2492
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);
2497 assert(cmp.end());
2498 le->cmapv = mds->server->prepare_force_open_sessions(onlogged->imported_client_map, onlogged->sseqmap);
2499 le->client_map.claim(m->client_map);
2500
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,
2506 oldauth,
2507 dir, // import root
2508 le,
2509 mds->mdlog->get_current_segment(),
2510 it->second.peer_exports,
2511 it->second.updated_scatterlocks,
2512 now);
2513 }
2514 dout(10) << " " << m->bounds.size() << " imported bounds" << dendl;
2515
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();
2520 ++p) {
2521 CDir *bd = cache->get_dirfrag(*p);
2522 assert(bd);
2523 le->metablob.add_dir(bd, false); // note that parent metadata is already in the event
2524 import_bounds.insert(bd);
2525 }
2526 cache->verify_subtree_bounds(dir, import_bounds);
2527
2528 // adjust popularity
2529 mds->balancer->add_import(dir, now);
2530
2531 dout(7) << "handle_export_dir did " << *dir << dendl;
2532
2533 // note state
2534 it->second.state = IMPORT_LOGGINGSTART;
2535 assert (g_conf->mds_kill_import_at != 6);
2536
2537 // log it
2538 mds->mdlog->submit_entry(le, onlogged);
2539 mds->mdlog->flush();
2540
2541 // some stats
2542 if (mds->logger) {
2543 mds->logger->inc(l_mds_imported);
2544 mds->logger->inc(l_mds_imported_inodes, num_imported_inodes);
2545 }
2546
2547 m->put();
2548 }
2549
2550
2551 /*
2552 * this is an import helper
2553 * called by import_finish, and import_reverse and friends.
2554 */
2555 void Migrator::import_remove_pins(CDir *dir, set<CDir*>& bounds)
2556 {
2557 import_state_t& stat = import_state[dir->dirfrag()];
2558 // root
2559 dir->put(CDir::PIN_IMPORTING);
2560 dir->state_clear(CDir::STATE_IMPORTING);
2561
2562 // bounding inodes
2563 set<inodeno_t> did;
2564 for (list<dirfrag_t>::iterator p = stat.bound_ls.begin();
2565 p != stat.bound_ls.end();
2566 ++p) {
2567 if (did.count(p->ino))
2568 continue;
2569 did.insert(p->ino);
2570 CInode *in = cache->get_inode(p->ino);
2571 assert(in);
2572 in->put_stickydirs();
2573 }
2574
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);
2580 }
2581 }
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);
2588 }
2589 }
2590 }
2591
2592
2593 /*
2594 * note: this does teh full work of reversing and import and cleaning up
2595 * state.
2596 * called by both handle_mds_failure and by handle_resolve (if we are
2597 * a survivor coping with an exporter failure+recovery).
2598 */
2599 void Migrator::import_reverse(CDir *dir)
2600 {
2601 dout(7) << "import_reverse " << *dir << dendl;
2602
2603 import_state_t& stat = import_state[dir->dirfrag()];
2604 stat.state = IMPORT_ABORTING;
2605
2606 set<CDir*> bounds;
2607 cache->get_subtree_bounds(dir, bounds);
2608
2609 // remove pins
2610 import_remove_pins(dir, bounds);
2611
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);
2616
2617 cache->adjust_subtree_auth(dir, stat.peer);
2618
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);
2625 }
2626
2627 int num_dentries = 0;
2628 // adjust auth bits.
2629 list<CDir*> q;
2630 q.push_back(dir);
2631 while (!q.empty()) {
2632 CDir *cur = q.front();
2633 q.pop_front();
2634
2635 // dir
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())
2642 cur->mark_clean();
2643
2644 CDir::map_t::iterator it;
2645 for (it = cur->begin(); it != cur->end(); ++it) {
2646 CDentry *dn = it->second;
2647
2648 // dentry
2649 dn->state_clear(CDentry::STATE_AUTH);
2650 dn->clear_replica_map();
2651 dn->set_replica_nonce(CDentry::EXPORT_NONCE);
2652 if (dn->is_dirty())
2653 dn->mark_clean();
2654
2655 // inode?
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);
2661 if (in->is_dirty())
2662 in->mark_clean();
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);
2667 }
2668
2669 in->clear_dirty_parent();
2670
2671 in->authlock.clear_gather();
2672 in->linklock.clear_gather();
2673 in->dirfragtreelock.clear_gather();
2674 in->filelock.clear_gather();
2675
2676 in->clear_file_locks();
2677
2678 // non-bounding dir?
2679 list<CDir*> dfs;
2680 in->get_dirfrags(dfs);
2681 for (list<CDir*>::iterator p = dfs.begin(); p != dfs.end(); ++p)
2682 if (bounds.count(*p) == 0)
2683 q.push_back(*p);
2684 }
2685
2686 cache->touch_dentry_bottom(dn); // move dentry to tail of LRU
2687 ++num_dentries;
2688 }
2689 }
2690
2691 dir->add_waiter(CDir::WAIT_UNFREEZE, fin);
2692
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();
2697 ++p) {
2698 CInode *in = p->first;
2699 for (map<client_t,Capability::Export>::iterator q = p->second.begin();
2700 q != p->second.end();
2701 ++q) {
2702 Capability *cap = in->get_client_cap(q->first);
2703 assert(cap);
2704 if (cap->is_importing())
2705 in->remove_client_cap(q->first);
2706 }
2707 in->put(CInode::PIN_IMPORTINGCAPS);
2708 }
2709 for (map<client_t,entity_inst_t>::iterator p = stat.client_map.begin();
2710 p != stat.client_map.end();
2711 ++p) {
2712 Session *session = mds->sessionmap.get_session(entity_name_t::CLIENT(p->first.v));
2713 assert(session);
2714 session->dec_importing();
2715 }
2716 }
2717
2718 // log our failure
2719 mds->mdlog->start_submit_entry(new EImportFinish(dir, false)); // log failure
2720
2721 cache->trim(num_dentries); // try trimming dentries
2722
2723 // notify bystanders; wait in aborting state
2724 import_notify_abort(dir, bounds);
2725 }
2726
2727 void Migrator::import_notify_finish(CDir *dir, set<CDir*>& bounds)
2728 {
2729 dout(7) << "import_notify_finish " << *dir << dendl;
2730
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();
2734 ++p) {
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);
2742 }
2743 }
2744
2745 void Migrator::import_notify_abort(CDir *dir, set<CDir*>& bounds)
2746 {
2747 dout(7) << "import_notify_abort " << *dir << dendl;
2748
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++);
2756 continue;
2757 }
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);
2765 ++p;
2766 }
2767 if (stat.bystanders.empty()) {
2768 dout(7) << "no bystanders, finishing reverse now" << dendl;
2769 import_reverse_unfreeze(dir);
2770 } else {
2771 assert (g_conf->mds_kill_import_at != 10);
2772 }
2773 }
2774
2775 void Migrator::import_reverse_unfreeze(CDir *dir)
2776 {
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);
2784 }
2785
2786 void Migrator::import_reverse_final(CDir *dir)
2787 {
2788 dout(7) << "import_reverse_final " << *dir << dendl;
2789
2790 // clean up
2791 map<dirfrag_t, import_state_t>::iterator it = import_state.find(dir->dirfrag());
2792 assert(it != import_state.end());
2793
2794 MutationRef mut = it->second.mut;
2795 import_state.erase(it);
2796
2797 // send pending import_maps?
2798 mds->mdcache->maybe_send_pending_resolves();
2799
2800 if (mut) {
2801 mds->locker->drop_locks(mut.get());
2802 mut->cleanup();
2803 }
2804
2805 cache->show_subtrees();
2806 //audit(); // this fails, bc we munge up the subtree map during handle_import_map (resolve phase)
2807 }
2808
2809
2810
2811
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)
2815 {
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);
2821 return;
2822 }
2823
2824 dout(7) << "import_logged " << *dir << dendl;
2825
2826 // note state
2827 it->second.state = IMPORT_ACKING;
2828
2829 assert (g_conf->mds_kill_import_at != 7);
2830
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);
2834
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();
2838 ++p) {
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()]);
2841 }
2842
2843 // send notify's etc.
2844 dout(7) << "sending ack for " << *dir << " to old auth mds." << from << dendl;
2845
2846 // test surviving observer of a failed migration that did not complete
2847 //assert(dir->replica_map.size() < 2 || mds->get_nodeid() != 0);
2848
2849 MExportDirAck *ack = new MExportDirAck(dir->dirfrag(), it->second.tid);
2850 ::encode(imported_caps, ack->imported_caps);
2851
2852 mds->send_message_mds(ack, from);
2853 assert (g_conf->mds_kill_import_at != 8);
2854
2855 cache->show_subtrees();
2856 }
2857
2858 /* This function DOES put the passed message before returning*/
2859 void Migrator::handle_export_finish(MExportDirFinish *m)
2860 {
2861 CDir *dir = cache->get_dirfrag(m->get_dirfrag());
2862 assert(dir);
2863 dout(7) << "handle_export_finish on " << *dir << (m->is_last() ? " last" : "") << dendl;
2864
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());
2868
2869 import_finish(dir, false, m->is_last());
2870
2871 m->put();
2872 }
2873
2874 void Migrator::import_finish(CDir *dir, bool notify, bool last)
2875 {
2876 dout(7) << "import_finish on " << *dir << dendl;
2877
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);
2881
2882 if (it->second.state == IMPORT_ACKING) {
2883 assert(dir->is_auth());
2884 cache->adjust_subtree_auth(dir, mds->get_nodeid(), mds->get_nodeid());
2885 }
2886
2887 // log finish
2888 assert(g_conf->mds_kill_import_at != 9);
2889
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();
2893 ++p) {
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();
2898 ++q) {
2899 Session *session = mds->sessionmap.get_session(entity_name_t::CLIENT(q->first.v));
2900 assert(session);
2901 Capability *cap = in->get_client_cap(q->first);
2902 assert(cap);
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);
2907 }
2908 p->second.clear();
2909 in->replica_caps_wanted = 0;
2910 }
2911 for (map<client_t,entity_inst_t>::iterator p = it->second.client_map.begin();
2912 p != it->second.client_map.end();
2913 ++p) {
2914 Session *session = mds->sessionmap.get_session(entity_name_t::CLIENT(p->first.v));
2915 assert(session);
2916 session->dec_importing();
2917 }
2918 }
2919
2920 if (!last) {
2921 assert(it->second.state == IMPORT_ACKING);
2922 it->second.state = IMPORT_FINISHING;
2923 return;
2924 }
2925
2926 // remove pins
2927 set<CDir*> bounds;
2928 cache->get_subtree_bounds(dir, bounds);
2929
2930 if (notify)
2931 import_notify_finish(dir, bounds);
2932
2933 import_remove_pins(dir, bounds);
2934
2935 map<CInode*, map<client_t,Capability::Export> > peer_exports;
2936 it->second.peer_exports.swap(peer_exports);
2937
2938 // clear import state (we're done!)
2939 MutationRef mut = it->second.mut;
2940 import_state.erase(it);
2941
2942 mds->mdlog->start_submit_entry(new EImportFinish(dir, true));
2943
2944 // process delayed expires
2945 cache->process_delayed_expire(dir);
2946
2947 // unfreeze tree, with possible subtree merge.
2948 dir->unfreeze_tree();
2949 cache->try_subtree_merge(dir);
2950
2951 cache->show_subtrees();
2952 //audit(); // this fails, bc we munge up the subtree map during handle_import_map (resolve phase)
2953
2954 if (mut) {
2955 mds->locker->drop_locks(mut.get());
2956 mut->cleanup();
2957 }
2958
2959 // re-eval imported caps
2960 for (map<CInode*, map<client_t,Capability::Export> >::iterator p = peer_exports.begin();
2961 p != peer_exports.end();
2962 ++p) {
2963 if (p->first->is_auth())
2964 mds->locker->eval(p->first, CEPH_CAP_LOCKS, true);
2965 p->first->put(CInode::PIN_IMPORTINGCAPS);
2966 }
2967
2968 // send pending import_maps?
2969 mds->mdcache->maybe_send_pending_resolves();
2970
2971 // did i just import mydir?
2972 if (dir->ino() == MDS_INO_MDSDIR(mds->get_nodeid()))
2973 cache->populate_mydir();
2974
2975 // is it empty?
2976 if (dir->get_num_head_items() == 0 &&
2977 !dir->inode->is_auth()) {
2978 // reexport!
2979 export_empty_import(dir);
2980 }
2981 }
2982
2983
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)
2988 {
2989 dout(15) << "decode_import_inode on " << *dn << dendl;
2990
2991 inodeno_t ino;
2992 snapid_t last;
2993 ::decode(ino, blp);
2994 ::decode(last, blp);
2995
2996 bool added = false;
2997 CInode *in = cache->get_inode(ino, last);
2998 if (!in) {
2999 in = new CInode(mds->mdcache, true, 1, last);
3000 added = true;
3001 }
3002
3003 // state after link -- or not! -sage
3004 in->decode_import(blp, ls); // cap imports are noted for later action
3005
3006 // caps
3007 decode_import_inode_caps(in, true, blp, peer_exports);
3008
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);
3013 }
3014
3015 // add inode?
3016 if (added) {
3017 cache->add_inode(in);
3018 dout(10) << "added " << *in << dendl;
3019 } else {
3020 dout(10) << " had " << *in << dendl;
3021 }
3022
3023 if (in->inode.is_dirty_rstat())
3024 in->mark_dirty_rstat();
3025
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);
3031 }
3032
3033 if (in->dirfragtreelock.is_dirty()) {
3034 updated_scatterlocks.push_back(&in->dirfragtreelock);
3035 mds->locker->mark_updated_scatterlock(&in->dirfragtreelock);
3036 }
3037
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());
3043 }
3044
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)
3048 {
3049 map<client_t,Capability::Export> cap_map;
3050 ::decode(cap_map, blp);
3051 if (auth_cap)
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);
3057 }
3058 }
3059
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)
3063 {
3064 for (map<client_t,Capability::Export>::iterator it = export_map.begin();
3065 it != export_map.end();
3066 ++it) {
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));
3069 assert(session);
3070
3071 Capability *cap = in->get_client_cap(it->first);
3072 if (!cap) {
3073 cap = in->add_client_cap(it->first, session);
3074 if (peer < 0)
3075 cap->mark_importing();
3076 }
3077
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;
3082
3083 if (peer >= 0) {
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);
3088 }
3089 }
3090
3091 if (peer >= 0) {
3092 in->replica_caps_wanted = 0;
3093 in->put(CInode::PIN_IMPORTINGCAPS);
3094 }
3095 }
3096
3097 int Migrator::decode_import_dir(bufferlist::iterator& blp,
3098 mds_rank_t oldauth,
3099 CDir *import_root,
3100 EImportStart *le,
3101 LogSegment *ls,
3102 map<CInode*,map<client_t,Capability::Export> >& peer_exports,
3103 list<ScatterLock*>& updated_scatterlocks, utime_t now)
3104 {
3105 // set up dir
3106 dirfrag_t df;
3107 ::decode(df, blp);
3108
3109 CInode *diri = cache->get_inode(df.ino);
3110 assert(diri);
3111 CDir *dir = diri->get_or_open_dirfrag(mds->mdcache, df.frag);
3112 assert(dir);
3113
3114 dout(7) << "decode_import_dir " << *dir << dendl;
3115
3116 // assimilate state
3117 dir->decode_import(blp, now, ls);
3118
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());
3124
3125 // add to journal entry
3126 if (le)
3127 le->metablob.add_import_dir(dir);
3128
3129 int num_imported = 0;
3130
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;
3135
3136 dir->take_waiting(CDir::WAIT_ANY_MASK, waiters);
3137 for (list<MDSInternalContextBase*>::iterator it = waiters.begin();
3138 it != waiters.end();
3139 ++it)
3140 import_root->add_waiter(CDir::WAIT_UNFREEZE, *it); // UNFREEZE will get kicked both on success or failure
3141
3142 dout(15) << "doing contents" << dendl;
3143
3144 // contents
3145 __u32 nden;
3146 ::decode(nden, blp);
3147
3148 for (; nden>0; nden--) {
3149 num_imported++;
3150
3151 // dentry
3152 string dname;
3153 snapid_t last;
3154 ::decode(dname, blp);
3155 ::decode(last, blp);
3156
3157 CDentry *dn = dir->lookup_exact_snap(dname, last);
3158 if (!dn)
3159 dn = dir->add_null_dentry(dname, 1, last);
3160
3161 dn->decode_import(blp, ls);
3162
3163 dn->add_replica(oldauth, CDentry::EXPORT_NONCE);
3164 if (dn->is_replica(mds->get_nodeid()))
3165 dn->remove_replica(mds->get_nodeid());
3166
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);
3170
3171 dout(15) << "decode_import_dir got " << *dn << dendl;
3172
3173 // points to...
3174 char icode;
3175 ::decode(icode, blp);
3176
3177 if (icode == 'N') {
3178 // null dentry
3179 assert(dn->get_linkage()->is_null());
3180
3181 // fall thru
3182 }
3183 else if (icode == 'L') {
3184 // remote link
3185 inodeno_t ino;
3186 unsigned char d_type;
3187 ::decode(ino, blp);
3188 ::decode(d_type, blp);
3189 if (dn->get_linkage()->is_remote()) {
3190 assert(dn->get_linkage()->get_remote_ino() == ino);
3191 } else {
3192 dir->link_remote_inode(dn, ino, d_type);
3193 }
3194 }
3195 else if (icode == 'I') {
3196 // inode
3197 assert(le);
3198 decode_import_inode(dn, blp, oldauth, ls,
3199 peer_exports, updated_scatterlocks);
3200 }
3201
3202 // add dentry to journal entry
3203 if (le)
3204 le->metablob.add_import_dentry(dn);
3205 }
3206
3207 #ifdef MDS_VERIFY_FRAGSTAT
3208 if (dir->is_complete())
3209 dir->verify_fragstat();
3210 #endif
3211
3212 dir->inode->maybe_export_pin();
3213
3214 dout(7) << "decode_import_dir done " << *dir << dendl;
3215 return num_imported;
3216 }
3217
3218
3219
3220
3221
3222 // authority bystander
3223
3224 /* This function DOES put the passed message before returning*/
3225 void Migrator::handle_export_notify(MExportDirNotify *m)
3226 {
3227 if (!(mds->is_clientreplay() || mds->is_active() || mds->is_stopping())) {
3228 m->put();
3229 return;
3230 }
3231
3232 CDir *dir = cache->get_dirfrag(m->get_dirfrag());
3233
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();
3237
3238 if (!dir) {
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;
3245 } else {
3246 dout(7) << "handle_export_notify " << old_auth << " -> " << new_auth
3247 << " on " << *dir << dendl;
3248 // adjust auth
3249 set<CDir*> have;
3250 cache->map_dirfrag_set(m->get_bounds(), have);
3251 cache->adjust_bounded_subtree_auth(dir, have, new_auth);
3252
3253 // induce a merge?
3254 cache->try_subtree_merge(dir);
3255 }
3256
3257 // send ack
3258 if (m->wants_ack()) {
3259 mds->send_message_mds(new MExportDirNotifyAck(m->get_dirfrag(), m->get_tid(), m->get_new_auth()), from);
3260 } else {
3261 // aborted. no ack.
3262 dout(7) << "handle_export_notify no ack requested" << dendl;
3263 }
3264
3265 m->put();
3266 }
3267
3268 /** cap exports **/
3269 void Migrator::export_caps(CInode *in)
3270 {
3271 mds_rank_t dest = in->authority().first;
3272 dout(7) << "export_caps to mds." << dest << " " << *in << dendl;
3273
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));
3278
3279 MExportCaps *ex = new MExportCaps;
3280 ex->ino = in->ino();
3281
3282 encode_export_inode_caps(in, false, ex->cap_bl, ex->client_map);
3283
3284 mds->send_message_mds(ex, dest);
3285 }
3286
3287 void Migrator::handle_gather_caps(MGatherCaps *m)
3288 {
3289 CInode *in = cache->get_inode(m->ino);
3290
3291 if (!in)
3292 goto out;
3293
3294 dout(10) << "handle_gather_caps " << *m << " from " << m->get_source()
3295 << " on " << *in
3296 << dendl;
3297 if (in->is_any_caps() &&
3298 !in->is_auth() &&
3299 !in->is_ambiguous_auth() &&
3300 !in->state_test(CInode::STATE_EXPORTINGCAPS))
3301 export_caps(in);
3302
3303 out:
3304 m->put();
3305 }
3306
3307 class C_M_LoggedImportCaps : public MigratorLogContext {
3308 CInode *in;
3309 mds_rank_t from;
3310 public:
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;
3314
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);
3318 }
3319 };
3320
3321 /* This function DOES put the passed message before returning*/
3322 void Migrator::handle_export_caps(MExportCaps *ex)
3323 {
3324 dout(10) << "handle_export_caps " << *ex << " from " << ex->get_source() << dendl;
3325 CInode *in = cache->get_inode(ex->ino);
3326
3327 assert(in);
3328 assert(in->is_auth());
3329
3330 // FIXME
3331 if (!in->can_auth_pin())
3332 return;
3333 in->auth_pin(this);
3334
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;
3338
3339 // decode new caps
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.
3343
3344 // journal open client sessions
3345 version_t pv = mds->server->prepare_force_open_sessions(finish->client_map, finish->sseqmap);
3346
3347 ESessions *le = new ESessions(pv, ex->client_map);
3348 mds->mdlog->start_submit_entry(le, finish);
3349 mds->mdlog->flush();
3350
3351 ex->put();
3352 }
3353
3354
3355 void Migrator::logged_import_caps(CInode *in,
3356 mds_rank_t from,
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)
3360 {
3361 dout(10) << "logged_import_caps on " << *in << dendl;
3362 // see export_go() vs export_go_synced()
3363 assert(in->is_auth());
3364
3365 // force open client sessions and finish cap import
3366 mds->server->finish_force_open_sessions(client_map, sseqmap);
3367
3368 map<client_t,Capability::Import> imported_caps;
3369
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);
3375 }