]> git.proxmox.com Git - ceph.git/blob - ceph/src/mds/MDCache.cc
bump version to 18.2.2-pve1
[ceph.git] / ceph / src / mds / MDCache.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 <errno.h>
16 #include <ostream>
17 #include <string>
18 #include <string_view>
19 #include <map>
20
21 #include "MDCache.h"
22 #include "MDSRank.h"
23 #include "Server.h"
24 #include "Locker.h"
25 #include "MDLog.h"
26 #include "MDBalancer.h"
27 #include "Migrator.h"
28 #include "ScrubStack.h"
29
30 #include "SnapClient.h"
31
32 #include "MDSMap.h"
33
34 #include "CInode.h"
35 #include "CDir.h"
36
37 #include "Mutation.h"
38
39 #include "include/ceph_fs.h"
40 #include "include/filepath.h"
41 #include "include/util.h"
42
43 #include "messages/MClientCaps.h"
44
45 #include "msg/Message.h"
46 #include "msg/Messenger.h"
47
48 #include "common/MemoryModel.h"
49 #include "common/errno.h"
50 #include "common/perf_counters.h"
51 #include "common/safe_io.h"
52
53 #include "osdc/Journaler.h"
54 #include "osdc/Filer.h"
55
56 #include "events/ESubtreeMap.h"
57 #include "events/EUpdate.h"
58 #include "events/EPeerUpdate.h"
59 #include "events/EImportFinish.h"
60 #include "events/EFragment.h"
61 #include "events/ECommitted.h"
62 #include "events/EPurged.h"
63 #include "events/ESessions.h"
64
65 #include "InoTable.h"
66 #include "fscrypt.h"
67
68 #include "common/Timer.h"
69
70 #include "perfglue/heap_profiler.h"
71
72
73 #include "common/config.h"
74 #include "include/ceph_assert.h"
75
76 #define dout_context g_ceph_context
77 #define dout_subsys ceph_subsys_mds
78 #undef dout_prefix
79 #define dout_prefix _prefix(_dout, mds)
80
81 using namespace std;
82
83 static ostream& _prefix(std::ostream *_dout, MDSRank *mds) {
84 return *_dout << "mds." << mds->get_nodeid() << ".cache ";
85 }
86
87 set<int> SimpleLock::empty_gather_set;
88
89
90 /**
91 * All non-I/O contexts that require a reference
92 * to an MDCache instance descend from this.
93 */
94 class MDCacheContext : public virtual MDSContext {
95 protected:
96 MDCache *mdcache;
97 MDSRank *get_mds() override
98 {
99 ceph_assert(mdcache != NULL);
100 return mdcache->mds;
101 }
102 public:
103 explicit MDCacheContext(MDCache *mdc_) : mdcache(mdc_) {}
104 };
105
106 class MDCacheLogContext : public virtual MDSLogContextBase {
107 protected:
108 MDCache *mdcache;
109 MDSRank *get_mds() override
110 {
111 ceph_assert(mdcache != NULL);
112 return mdcache->mds;
113 }
114 public:
115 explicit MDCacheLogContext(MDCache *mdc_) : mdcache(mdc_) {}
116 };
117
118 MDCache::MDCache(MDSRank *m, PurgeQueue &purge_queue_) :
119 mds(m),
120 open_file_table(m),
121 filer(m->objecter, m->finisher),
122 stray_manager(m, purge_queue_),
123 recovery_queue(m),
124 trim_counter(g_conf().get_val<double>("mds_cache_trim_decay_rate"))
125 {
126 migrator.reset(new Migrator(mds, this));
127
128 max_dir_commit_size = g_conf()->mds_dir_max_commit_size ?
129 (g_conf()->mds_dir_max_commit_size << 20) :
130 (0.9 *(g_conf()->osd_max_write_size << 20));
131
132 cache_memory_limit = g_conf().get_val<Option::size_t>("mds_cache_memory_limit");
133 cache_reservation = g_conf().get_val<double>("mds_cache_reservation");
134 cache_health_threshold = g_conf().get_val<double>("mds_health_cache_threshold");
135
136 export_ephemeral_distributed_config = g_conf().get_val<bool>("mds_export_ephemeral_distributed");
137 export_ephemeral_random_config = g_conf().get_val<bool>("mds_export_ephemeral_random");
138 export_ephemeral_random_max = g_conf().get_val<double>("mds_export_ephemeral_random_max");
139
140 symlink_recovery = g_conf().get_val<bool>("mds_symlink_recovery");
141
142 lru.lru_set_midpoint(g_conf().get_val<double>("mds_cache_mid"));
143
144 bottom_lru.lru_set_midpoint(0);
145
146 decayrate.set_halflife(g_conf()->mds_decay_halflife);
147
148 upkeeper = std::thread(&MDCache::upkeep_main, this);
149 }
150
151 MDCache::~MDCache()
152 {
153 if (logger) {
154 g_ceph_context->get_perfcounters_collection()->remove(logger.get());
155 }
156 if (upkeeper.joinable())
157 upkeeper.join();
158 }
159
160 void MDCache::handle_conf_change(const std::set<std::string>& changed, const MDSMap& mdsmap)
161 {
162 dout(20) << "config changes: " << changed << dendl;
163 if (changed.count("mds_cache_memory_limit"))
164 cache_memory_limit = g_conf().get_val<Option::size_t>("mds_cache_memory_limit");
165 if (changed.count("mds_cache_reservation"))
166 cache_reservation = g_conf().get_val<double>("mds_cache_reservation");
167
168 bool ephemeral_pin_config_changed = false;
169 if (changed.count("mds_export_ephemeral_distributed")) {
170 export_ephemeral_distributed_config = g_conf().get_val<bool>("mds_export_ephemeral_distributed");
171 dout(10) << "Migrating any ephemeral distributed pinned inodes" << dendl;
172 /* copy to vector to avoid removals during iteration */
173 ephemeral_pin_config_changed = true;
174 }
175 if (changed.count("mds_export_ephemeral_random")) {
176 export_ephemeral_random_config = g_conf().get_val<bool>("mds_export_ephemeral_random");
177 dout(10) << "Migrating any ephemeral random pinned inodes" << dendl;
178 /* copy to vector to avoid removals during iteration */
179 ephemeral_pin_config_changed = true;
180 }
181 if (ephemeral_pin_config_changed) {
182 std::vector<CInode*> migrate;
183 migrate.assign(export_ephemeral_pins.begin(), export_ephemeral_pins.end());
184 for (auto& in : migrate) {
185 in->maybe_export_pin(true);
186 }
187 }
188 if (changed.count("mds_export_ephemeral_random_max")) {
189 export_ephemeral_random_max = g_conf().get_val<double>("mds_export_ephemeral_random_max");
190 }
191 if (changed.count("mds_health_cache_threshold"))
192 cache_health_threshold = g_conf().get_val<double>("mds_health_cache_threshold");
193 if (changed.count("mds_cache_mid"))
194 lru.lru_set_midpoint(g_conf().get_val<double>("mds_cache_mid"));
195 if (changed.count("mds_cache_trim_decay_rate")) {
196 trim_counter = DecayCounter(g_conf().get_val<double>("mds_cache_trim_decay_rate"));
197 }
198 if (changed.count("mds_symlink_recovery")) {
199 symlink_recovery = g_conf().get_val<bool>("mds_symlink_recovery");
200 dout(10) << "Storing symlink targets on file object's head " << symlink_recovery << dendl;
201 }
202
203 migrator->handle_conf_change(changed, mdsmap);
204 mds->balancer->handle_conf_change(changed, mdsmap);
205 }
206
207 void MDCache::log_stat()
208 {
209 mds->logger->set(l_mds_inodes, lru.lru_get_size());
210 mds->logger->set(l_mds_inodes_pinned, lru.lru_get_num_pinned());
211 mds->logger->set(l_mds_inodes_top, lru.lru_get_top());
212 mds->logger->set(l_mds_inodes_bottom, lru.lru_get_bot());
213 mds->logger->set(l_mds_inodes_pin_tail, lru.lru_get_pintail());
214 mds->logger->set(l_mds_inodes_with_caps, num_inodes_with_caps);
215 mds->logger->set(l_mds_caps, Capability::count());
216 if (root) {
217 mds->logger->set(l_mds_root_rfiles, root->get_inode()->rstat.rfiles);
218 mds->logger->set(l_mds_root_rbytes, root->get_inode()->rstat.rbytes);
219 mds->logger->set(l_mds_root_rsnaps, root->get_inode()->rstat.rsnaps);
220 }
221 }
222
223
224 //
225
226 bool MDCache::shutdown()
227 {
228 {
229 std::scoped_lock lock(upkeep_mutex);
230 upkeep_trim_shutdown = true;
231 upkeep_cvar.notify_one();
232 }
233 if (lru.lru_get_size() > 0) {
234 dout(7) << "WARNING: mdcache shutdown with non-empty cache" << dendl;
235 //show_cache();
236 show_subtrees();
237 //dump();
238 }
239 return true;
240 }
241
242
243 // ====================================================================
244 // some inode functions
245
246 void MDCache::add_inode(CInode *in)
247 {
248 // add to inode map
249 if (in->last == CEPH_NOSNAP) {
250 auto &p = inode_map[in->ino()];
251 ceph_assert(!p); // should be no dup inos!
252 p = in;
253 } else {
254 auto &p = snap_inode_map[in->vino()];
255 ceph_assert(!p); // should be no dup inos!
256 p = in;
257 }
258
259 if (in->ino() < MDS_INO_SYSTEM_BASE) {
260 if (in->ino() == CEPH_INO_ROOT)
261 root = in;
262 else if (in->ino() == MDS_INO_MDSDIR(mds->get_nodeid()))
263 myin = in;
264 else if (in->is_stray()) {
265 if (MDS_INO_STRAY_OWNER(in->ino()) == mds->get_nodeid()) {
266 strays[MDS_INO_STRAY_INDEX(in->ino())] = in;
267 }
268 }
269 if (in->is_base())
270 base_inodes.insert(in);
271 }
272 }
273
274 void MDCache::remove_inode(CInode *o)
275 {
276 dout(14) << "remove_inode " << *o << dendl;
277
278 if (o->get_parent_dn()) {
279 // FIXME: multiple parents?
280 CDentry *dn = o->get_parent_dn();
281 ceph_assert(!dn->is_dirty());
282 dn->dir->unlink_inode(dn); // leave dentry ... FIXME?
283 }
284
285 if (o->is_dirty())
286 o->mark_clean();
287 if (o->is_dirty_parent())
288 o->clear_dirty_parent();
289
290 o->clear_scatter_dirty();
291
292 o->clear_clientwriteable();
293
294 o->item_open_file.remove_myself();
295
296 if (o->state_test(CInode::STATE_QUEUEDEXPORTPIN))
297 export_pin_queue.erase(o);
298
299 if (o->state_test(CInode::STATE_DELAYEDEXPORTPIN))
300 export_pin_delayed_queue.erase(o);
301
302 o->clear_ephemeral_pin(true, true);
303
304 // remove from inode map
305 if (o->last == CEPH_NOSNAP) {
306 inode_map.erase(o->ino());
307 } else {
308 o->item_caps.remove_myself();
309 snap_inode_map.erase(o->vino());
310 }
311
312 clear_taken_inos(o->ino());
313
314 if (o->ino() < MDS_INO_SYSTEM_BASE) {
315 if (o == root) root = 0;
316 if (o == myin) myin = 0;
317 if (o->is_stray()) {
318 if (MDS_INO_STRAY_OWNER(o->ino()) == mds->get_nodeid()) {
319 strays[MDS_INO_STRAY_INDEX(o->ino())] = 0;
320 }
321 }
322 if (o->is_base())
323 base_inodes.erase(o);
324 }
325
326 // delete it
327 ceph_assert(o->get_num_ref() == 0);
328 delete o;
329 }
330
331 file_layout_t MDCache::gen_default_file_layout(const MDSMap &mdsmap)
332 {
333 file_layout_t result = file_layout_t::get_default();
334 result.pool_id = mdsmap.get_first_data_pool();
335 return result;
336 }
337
338 file_layout_t MDCache::gen_default_log_layout(const MDSMap &mdsmap)
339 {
340 file_layout_t result = file_layout_t::get_default();
341 result.pool_id = mdsmap.get_metadata_pool();
342 if (g_conf()->mds_log_segment_size > 0) {
343 result.object_size = g_conf()->mds_log_segment_size;
344 result.stripe_unit = g_conf()->mds_log_segment_size;
345 }
346 return result;
347 }
348
349 void MDCache::init_layouts()
350 {
351 default_file_layout = gen_default_file_layout(*(mds->mdsmap));
352 default_log_layout = gen_default_log_layout(*(mds->mdsmap));
353 }
354
355 void MDCache::create_unlinked_system_inode(CInode *in, inodeno_t ino, int mode) const
356 {
357 auto _inode = in->_get_inode();
358 _inode->ino = ino;
359 _inode->version = 1;
360 _inode->xattr_version = 1;
361 _inode->mode = 0500 | mode;
362 _inode->size = 0;
363 _inode->ctime = _inode->mtime = _inode->btime = ceph_clock_now();
364 _inode->nlink = 1;
365 _inode->truncate_size = -1ull;
366 _inode->change_attr = 0;
367 _inode->export_pin = MDS_RANK_NONE;
368
369 // FIPS zeroization audit 20191117: this memset is not security related.
370 memset(&_inode->dir_layout, 0, sizeof(_inode->dir_layout));
371 if (_inode->is_dir()) {
372 _inode->dir_layout.dl_dir_hash = g_conf()->mds_default_dir_hash;
373 _inode->rstat.rsubdirs = 1; /* itself */
374 _inode->rstat.rctime = in->get_inode()->ctime;
375 } else {
376 _inode->layout = default_file_layout;
377 ++_inode->rstat.rfiles;
378 }
379 _inode->accounted_rstat = _inode->rstat;
380
381 if (in->is_base()) {
382 if (in->is_root())
383 in->inode_auth = mds_authority_t(mds->get_nodeid(), CDIR_AUTH_UNKNOWN);
384 else
385 in->inode_auth = mds_authority_t(mds_rank_t(in->ino() - MDS_INO_MDSDIR_OFFSET), CDIR_AUTH_UNKNOWN);
386 in->open_snaprealm(); // empty snaprealm
387 ceph_assert(!in->snaprealm->parent); // created its own
388 in->snaprealm->srnode.seq = 1;
389 }
390 }
391
392 CInode *MDCache::create_system_inode(inodeno_t ino, int mode)
393 {
394 dout(0) << "creating system inode with ino:" << ino << dendl;
395 CInode *in = new CInode(this);
396 create_unlinked_system_inode(in, ino, mode);
397 add_inode(in);
398 return in;
399 }
400
401 CInode *MDCache::create_root_inode()
402 {
403 CInode *in = create_system_inode(CEPH_INO_ROOT, S_IFDIR|0755);
404 auto _inode = in->_get_inode();
405 _inode->uid = g_conf()->mds_root_ino_uid;
406 _inode->gid = g_conf()->mds_root_ino_gid;
407 _inode->layout = default_file_layout;
408 _inode->layout.pool_id = mds->mdsmap->get_first_data_pool();
409 return in;
410 }
411
412 void MDCache::create_empty_hierarchy(MDSGather *gather)
413 {
414 // create root dir
415 CInode *root = create_root_inode();
416
417 // force empty root dir
418 CDir *rootdir = root->get_or_open_dirfrag(this, frag_t());
419 adjust_subtree_auth(rootdir, mds->get_nodeid());
420 rootdir->dir_rep = CDir::REP_ALL; //NONE;
421
422 ceph_assert(rootdir->get_fnode()->accounted_fragstat == rootdir->get_fnode()->fragstat);
423 ceph_assert(rootdir->get_fnode()->fragstat == root->get_inode()->dirstat);
424 ceph_assert(rootdir->get_fnode()->accounted_rstat == rootdir->get_fnode()->rstat);
425 /* Do no update rootdir rstat information of the fragment, rstat upkeep magic
426 * assume version 0 is stale/invalid.
427 */
428
429 rootdir->mark_complete();
430 rootdir->_get_fnode()->version = rootdir->pre_dirty();
431 rootdir->mark_dirty(mds->mdlog->get_current_segment());
432 rootdir->commit(0, gather->new_sub());
433
434 root->store(gather->new_sub());
435 root->mark_dirty_parent(mds->mdlog->get_current_segment(), true);
436 root->store_backtrace(gather->new_sub());
437 }
438
439 void MDCache::create_mydir_hierarchy(MDSGather *gather)
440 {
441 // create mds dir
442 CInode *my = create_system_inode(MDS_INO_MDSDIR(mds->get_nodeid()), S_IFDIR);
443
444 CDir *mydir = my->get_or_open_dirfrag(this, frag_t());
445 auto mydir_fnode = mydir->_get_fnode();
446
447 adjust_subtree_auth(mydir, mds->get_nodeid());
448
449 LogSegment *ls = mds->mdlog->get_current_segment();
450
451 // stray dir
452 for (int i = 0; i < NUM_STRAY; ++i) {
453 CInode *stray = create_system_inode(MDS_INO_STRAY(mds->get_nodeid(), i), S_IFDIR);
454 CDir *straydir = stray->get_or_open_dirfrag(this, frag_t());
455 CachedStackStringStream css;
456 *css << "stray" << i;
457 CDentry *sdn = mydir->add_primary_dentry(css->str(), stray, "");
458 sdn->_mark_dirty(mds->mdlog->get_current_segment());
459
460 stray->_get_inode()->dirstat = straydir->get_fnode()->fragstat;
461
462 mydir_fnode->rstat.add(stray->get_inode()->rstat);
463 mydir_fnode->fragstat.nsubdirs++;
464 // save them
465 straydir->mark_complete();
466 straydir->_get_fnode()->version = straydir->pre_dirty();
467 straydir->mark_dirty(ls);
468 straydir->commit(0, gather->new_sub());
469 stray->mark_dirty_parent(ls, true);
470 stray->store_backtrace(gather->new_sub());
471 }
472
473 mydir_fnode->accounted_fragstat = mydir->get_fnode()->fragstat;
474 mydir_fnode->accounted_rstat = mydir->get_fnode()->rstat;
475
476 auto inode = myin->_get_inode();
477 inode->dirstat = mydir->get_fnode()->fragstat;
478 inode->rstat = mydir->get_fnode()->rstat;
479 ++inode->rstat.rsubdirs;
480 inode->accounted_rstat = inode->rstat;
481
482 mydir->mark_complete();
483 mydir_fnode->version = mydir->pre_dirty();
484 mydir->mark_dirty(ls);
485 mydir->commit(0, gather->new_sub());
486
487 myin->store(gather->new_sub());
488 }
489
490 struct C_MDC_CreateSystemFile : public MDCacheLogContext {
491 MutationRef mut;
492 CDentry *dn;
493 version_t dpv;
494 MDSContext *fin;
495 C_MDC_CreateSystemFile(MDCache *c, MutationRef& mu, CDentry *d, version_t v, MDSContext *f) :
496 MDCacheLogContext(c), mut(mu), dn(d), dpv(v), fin(f) {}
497 void finish(int r) override {
498 mdcache->_create_system_file_finish(mut, dn, dpv, fin);
499 }
500 };
501
502 void MDCache::_create_system_file(CDir *dir, std::string_view name, CInode *in, MDSContext *fin)
503 {
504 dout(10) << "_create_system_file " << name << " in " << *dir << dendl;
505 CDentry *dn = dir->add_null_dentry(name);
506
507 dn->push_projected_linkage(in);
508 version_t dpv = dn->pre_dirty();
509
510 CDir *mdir = 0;
511 auto inode = in->_get_inode();
512 if (in->is_dir()) {
513 inode->rstat.rsubdirs = 1;
514
515 mdir = in->get_or_open_dirfrag(this, frag_t());
516 mdir->mark_complete();
517 mdir->_get_fnode()->version = mdir->pre_dirty();
518 } else {
519 inode->rstat.rfiles = 1;
520 }
521
522 inode->version = dn->pre_dirty();
523
524 SnapRealm *realm = dir->get_inode()->find_snaprealm();
525 dn->first = in->first = realm->get_newest_seq() + 1;
526
527 MutationRef mut(new MutationImpl());
528
529 // force some locks. hacky.
530 mds->locker->wrlock_force(&dir->inode->filelock, mut);
531 mds->locker->wrlock_force(&dir->inode->nestlock, mut);
532
533 mut->ls = mds->mdlog->get_current_segment();
534 EUpdate *le = new EUpdate(mds->mdlog, "create system file");
535 mds->mdlog->start_entry(le);
536
537 if (!in->is_mdsdir()) {
538 predirty_journal_parents(mut, &le->metablob, in, dir, PREDIRTY_PRIMARY|PREDIRTY_DIR, 1);
539 le->metablob.add_primary_dentry(dn, in, true);
540 } else {
541 predirty_journal_parents(mut, &le->metablob, in, dir, PREDIRTY_DIR, 1);
542 journal_dirty_inode(mut.get(), &le->metablob, in);
543 dn->push_projected_linkage(in->ino(), in->d_type());
544 le->metablob.add_remote_dentry(dn, true, in->ino(), in->d_type());
545 le->metablob.add_root(true, in);
546 }
547 if (mdir)
548 le->metablob.add_new_dir(mdir); // dirty AND complete AND new
549
550 mds->mdlog->submit_entry(le, new C_MDC_CreateSystemFile(this, mut, dn, dpv, fin));
551 mds->mdlog->flush();
552 }
553
554 void MDCache::_create_system_file_finish(MutationRef& mut, CDentry *dn, version_t dpv, MDSContext *fin)
555 {
556 dout(10) << "_create_system_file_finish " << *dn << dendl;
557
558 dn->pop_projected_linkage();
559 dn->mark_dirty(dpv, mut->ls);
560
561 CInode *in = dn->get_linkage()->get_inode();
562 in->mark_dirty(mut->ls);
563
564 if (in->is_dir()) {
565 CDir *dir = in->get_dirfrag(frag_t());
566 ceph_assert(dir);
567 dir->mark_dirty(mut->ls);
568 dir->mark_new(mut->ls);
569 }
570
571 mut->apply();
572 mds->locker->drop_locks(mut.get());
573 mut->cleanup();
574
575 fin->complete(0);
576
577 //if (dir && MDS_INO_IS_MDSDIR(in->ino()))
578 //migrator->export_dir(dir, (int)in->ino() - MDS_INO_MDSDIR_OFFSET);
579 }
580
581
582
583 struct C_MDS_RetryOpenRoot : public MDSInternalContext {
584 MDCache *cache;
585 explicit C_MDS_RetryOpenRoot(MDCache *c) : MDSInternalContext(c->mds), cache(c) {}
586 void finish(int r) override {
587 if (r < 0) {
588 // If we can't open root, something disastrous has happened: mark
589 // this rank damaged for operator intervention. Note that
590 // it is not okay to call suicide() here because we are in
591 // a Finisher callback.
592 cache->mds->damaged();
593 ceph_abort(); // damaged should never return
594 } else {
595 cache->open_root();
596 }
597 }
598 };
599
600 void MDCache::open_root_inode(MDSContext *c)
601 {
602 if (mds->get_nodeid() == mds->mdsmap->get_root()) {
603 CInode *in;
604 in = create_system_inode(CEPH_INO_ROOT, S_IFDIR|0755); // initially inaccurate!
605 in->fetch(c);
606 } else {
607 discover_base_ino(CEPH_INO_ROOT, c, mds->mdsmap->get_root());
608 }
609 }
610
611 void MDCache::open_mydir_inode(MDSContext *c)
612 {
613 CInode *in = create_system_inode(MDS_INO_MDSDIR(mds->get_nodeid()), S_IFDIR|0755); // initially inaccurate!
614 in->fetch(c);
615 }
616
617 void MDCache::open_mydir_frag(MDSContext *c)
618 {
619 open_mydir_inode(
620 new MDSInternalContextWrapper(mds,
621 new LambdaContext([this, c](int r) {
622 if (r < 0) {
623 c->complete(r);
624 return;
625 }
626 CDir *mydir = myin->get_or_open_dirfrag(this, frag_t());
627 ceph_assert(mydir);
628 adjust_subtree_auth(mydir, mds->get_nodeid());
629 mydir->fetch(c);
630 })
631 )
632 );
633 }
634
635 void MDCache::open_root()
636 {
637 dout(10) << "open_root" << dendl;
638
639 if (!root) {
640 open_root_inode(new C_MDS_RetryOpenRoot(this));
641 return;
642 }
643 if (mds->get_nodeid() == mds->mdsmap->get_root()) {
644 ceph_assert(root->is_auth());
645 CDir *rootdir = root->get_or_open_dirfrag(this, frag_t());
646 ceph_assert(rootdir);
647 if (!rootdir->is_subtree_root())
648 adjust_subtree_auth(rootdir, mds->get_nodeid());
649 if (!rootdir->is_complete()) {
650 rootdir->fetch(new C_MDS_RetryOpenRoot(this));
651 return;
652 }
653 } else {
654 ceph_assert(!root->is_auth());
655 CDir *rootdir = root->get_dirfrag(frag_t());
656 if (!rootdir) {
657 open_remote_dirfrag(root, frag_t(), new C_MDS_RetryOpenRoot(this));
658 return;
659 }
660 }
661
662 if (!myin) {
663 CInode *in = create_system_inode(MDS_INO_MDSDIR(mds->get_nodeid()), S_IFDIR|0755); // initially inaccurate!
664 in->fetch(new C_MDS_RetryOpenRoot(this));
665 return;
666 }
667 CDir *mydir = myin->get_or_open_dirfrag(this, frag_t());
668 ceph_assert(mydir);
669 adjust_subtree_auth(mydir, mds->get_nodeid());
670
671 populate_mydir();
672 }
673
674 void MDCache::advance_stray() {
675 // check whether the directory has been fragmented
676 if (stray_fragmenting_index >= 0) {
677 auto&& dfs = strays[stray_fragmenting_index]->get_dirfrags();
678 bool any_fragmenting = false;
679 for (const auto& dir : dfs) {
680 if (dir->state_test(CDir::STATE_FRAGMENTING) ||
681 mds->balancer->is_fragment_pending(dir->dirfrag())) {
682 any_fragmenting = true;
683 break;
684 }
685 }
686 if (!any_fragmenting)
687 stray_fragmenting_index = -1;
688 }
689
690 for (int i = 1; i < NUM_STRAY; i++){
691 stray_index = (stray_index + i) % NUM_STRAY;
692 if (stray_index != stray_fragmenting_index)
693 break;
694 }
695
696 if (stray_fragmenting_index == -1 && is_open()) {
697 // Fragment later stray dir in advance. We don't choose past
698 // stray dir because in-flight requests may still use it.
699 stray_fragmenting_index = (stray_index + 3) % NUM_STRAY;
700 auto&& dfs = strays[stray_fragmenting_index]->get_dirfrags();
701 bool any_fragmenting = false;
702 for (const auto& dir : dfs) {
703 if (dir->should_split()) {
704 mds->balancer->queue_split(dir, true);
705 any_fragmenting = true;
706 } else if (dir->should_merge()) {
707 mds->balancer->queue_merge(dir);
708 any_fragmenting = true;
709 }
710 }
711 if (!any_fragmenting)
712 stray_fragmenting_index = -1;
713 }
714
715 dout(10) << "advance_stray to index " << stray_index
716 << " fragmenting index " << stray_fragmenting_index << dendl;
717 }
718
719 void MDCache::populate_mydir()
720 {
721 ceph_assert(myin);
722 CDir *mydir = myin->get_or_open_dirfrag(this, frag_t());
723 ceph_assert(mydir);
724
725 dout(10) << "populate_mydir " << *mydir << dendl;
726
727 if (!mydir->is_complete()) {
728 mydir->fetch(new C_MDS_RetryOpenRoot(this));
729 return;
730 }
731
732 if (mydir->get_version() == 0 && mydir->state_test(CDir::STATE_BADFRAG)) {
733 // A missing dirfrag, we will recreate it. Before that, we must dirty
734 // it before dirtying any of the strays we create within it.
735 mds->clog->warn() << "fragment " << mydir->dirfrag() << " was unreadable, "
736 "recreating it now";
737 LogSegment *ls = mds->mdlog->get_current_segment();
738 mydir->state_clear(CDir::STATE_BADFRAG);
739 mydir->mark_complete();
740 mydir->_get_fnode()->version = mydir->pre_dirty();
741 mydir->mark_dirty(ls);
742 }
743
744 // open or create stray
745 uint64_t num_strays = 0;
746 for (int i = 0; i < NUM_STRAY; ++i) {
747 CachedStackStringStream css;
748 *css << "stray" << i;
749 CDentry *straydn = mydir->lookup(css->str());
750
751 // allow for older fs's with stray instead of stray0
752 if (straydn == NULL && i == 0)
753 straydn = mydir->lookup("stray");
754
755 if (!straydn || !straydn->get_linkage()->get_inode()) {
756 _create_system_file(mydir, css->strv(), create_system_inode(MDS_INO_STRAY(mds->get_nodeid(), i), S_IFDIR),
757 new C_MDS_RetryOpenRoot(this));
758 return;
759 }
760 ceph_assert(straydn);
761 ceph_assert(strays[i]);
762 // we make multiple passes through this method; make sure we only pin each stray once.
763 if (!strays[i]->state_test(CInode::STATE_STRAYPINNED)) {
764 strays[i]->get(CInode::PIN_STRAY);
765 strays[i]->state_set(CInode::STATE_STRAYPINNED);
766 strays[i]->get_stickydirs();
767 }
768 dout(20) << " stray num " << i << " is " << *strays[i] << dendl;
769
770 // open all frags
771 frag_vec_t leaves;
772 strays[i]->dirfragtree.get_leaves(leaves);
773 for (const auto& leaf : leaves) {
774 CDir *dir = strays[i]->get_dirfrag(leaf);
775 if (!dir) {
776 dir = strays[i]->get_or_open_dirfrag(this, leaf);
777 }
778
779 // DamageTable applies special handling to strays: it will
780 // have damaged() us out if one is damaged.
781 ceph_assert(!dir->state_test(CDir::STATE_BADFRAG));
782
783 if (dir->get_version() == 0) {
784 dir->fetch_keys({}, new C_MDS_RetryOpenRoot(this));
785 return;
786 }
787
788 if (dir->get_frag_size() > 0)
789 num_strays += dir->get_frag_size();
790 }
791 }
792
793 // okay!
794 dout(10) << "populate_mydir done" << dendl;
795 ceph_assert(!open);
796 open = true;
797 mds->queue_waiters(waiting_for_open);
798
799 stray_manager.set_num_strays(num_strays);
800 stray_manager.activate();
801
802 scan_stray_dir();
803 }
804
805 void MDCache::open_foreign_mdsdir(inodeno_t ino, MDSContext *fin)
806 {
807 discover_base_ino(ino, fin, mds_rank_t(ino & (MAX_MDS-1)));
808 }
809
810 CDir *MDCache::get_stray_dir(CInode *in)
811 {
812 string straydname;
813 in->name_stray_dentry(straydname);
814
815 CInode *strayi = get_stray();
816 ceph_assert(strayi);
817 frag_t fg = strayi->pick_dirfrag(straydname);
818 CDir *straydir = strayi->get_dirfrag(fg);
819 ceph_assert(straydir);
820 return straydir;
821 }
822
823 MDSCacheObject *MDCache::get_object(const MDSCacheObjectInfo &info)
824 {
825 // inode?
826 if (info.ino)
827 return get_inode(info.ino, info.snapid);
828
829 // dir or dentry.
830 CDir *dir = get_dirfrag(info.dirfrag);
831 if (!dir) return 0;
832
833 if (info.dname.length())
834 return dir->lookup(info.dname, info.snapid);
835 else
836 return dir;
837 }
838
839
840 // ====================================================================
841 // consistent hash ring
842
843 /*
844 * hashing implementation based on Lamping and Veach's Jump Consistent Hash: https://arxiv.org/pdf/1406.2294.pdf
845 */
846 mds_rank_t MDCache::hash_into_rank_bucket(inodeno_t ino, frag_t fg)
847 {
848 const mds_rank_t max_mds = mds->mdsmap->get_max_mds();
849 uint64_t hash = rjhash64(ino);
850 if (fg)
851 hash = rjhash64(hash + rjhash64(fg.value()));
852
853 int64_t b = -1, j = 0;
854 while (j < max_mds) {
855 b = j;
856 hash = hash*2862933555777941757ULL + 1;
857 j = (b + 1) * (double(1LL << 31) / double((hash >> 33) + 1));
858 }
859 // verify bounds before returning
860 auto result = mds_rank_t(b);
861 ceph_assert(result >= 0 && result < max_mds);
862 return result;
863 }
864
865
866 // ====================================================================
867 // subtree management
868
869 /*
870 * adjust the dir_auth of a subtree.
871 * merge with parent and/or child subtrees, if is it appropriate.
872 * merge can ONLY happen if both parent and child have unambiguous auth.
873 */
874 void MDCache::adjust_subtree_auth(CDir *dir, mds_authority_t auth, bool adjust_pop)
875 {
876 dout(7) << "adjust_subtree_auth " << dir->get_dir_auth() << " -> " << auth
877 << " on " << *dir << dendl;
878
879 show_subtrees();
880
881 CDir *root;
882 if (dir->inode->is_base()) {
883 root = dir; // bootstrap hack.
884 if (subtrees.count(root) == 0) {
885 subtrees[root];
886 root->get(CDir::PIN_SUBTREE);
887 }
888 } else {
889 root = get_subtree_root(dir); // subtree root
890 }
891 ceph_assert(root);
892 ceph_assert(subtrees.count(root));
893 dout(7) << " current root is " << *root << dendl;
894
895 if (root == dir) {
896 // i am already a subtree.
897 dir->set_dir_auth(auth);
898 } else {
899 // i am a new subtree.
900 dout(10) << " new subtree at " << *dir << dendl;
901 ceph_assert(subtrees.count(dir) == 0);
902 subtrees[dir]; // create empty subtree bounds list for me.
903 dir->get(CDir::PIN_SUBTREE);
904
905 // set dir_auth
906 dir->set_dir_auth(auth);
907
908 // move items nested beneath me, under me.
909 set<CDir*>::iterator p = subtrees[root].begin();
910 while (p != subtrees[root].end()) {
911 set<CDir*>::iterator next = p;
912 ++next;
913 if (get_subtree_root((*p)->get_parent_dir()) == dir) {
914 // move under me
915 dout(10) << " claiming child bound " << **p << dendl;
916 subtrees[dir].insert(*p);
917 subtrees[root].erase(p);
918 }
919 p = next;
920 }
921
922 // i am a bound of the parent subtree.
923 subtrees[root].insert(dir);
924
925 // i am now the subtree root.
926 root = dir;
927
928 // adjust recursive pop counters
929 if (adjust_pop && dir->is_auth()) {
930 CDir *p = dir->get_parent_dir();
931 while (p) {
932 p->pop_auth_subtree.sub(dir->pop_auth_subtree);
933 if (p->is_subtree_root()) break;
934 p = p->inode->get_parent_dir();
935 }
936 }
937 }
938
939 show_subtrees();
940 }
941
942
943 void MDCache::try_subtree_merge(CDir *dir)
944 {
945 dout(7) << "try_subtree_merge " << *dir << dendl;
946 // record my old bounds
947 auto oldbounds = subtrees.at(dir);
948
949 set<CInode*> to_eval;
950 // try merge at my root
951 try_subtree_merge_at(dir, &to_eval);
952
953 // try merge at my old bounds
954 for (auto bound : oldbounds)
955 try_subtree_merge_at(bound, &to_eval);
956
957 if (!(mds->is_any_replay() || mds->is_resolve())) {
958 for(auto in : to_eval)
959 eval_subtree_root(in);
960 }
961 }
962
963 void MDCache::try_subtree_merge_at(CDir *dir, set<CInode*> *to_eval, bool adjust_pop)
964 {
965 dout(10) << "try_subtree_merge_at " << *dir << dendl;
966
967 if (dir->dir_auth.second != CDIR_AUTH_UNKNOWN ||
968 dir->state_test(CDir::STATE_EXPORTBOUND) ||
969 dir->state_test(CDir::STATE_AUXSUBTREE))
970 return;
971
972 auto it = subtrees.find(dir);
973 ceph_assert(it != subtrees.end());
974
975 // merge with parent?
976 CDir *parent = dir;
977 if (!dir->inode->is_base())
978 parent = get_subtree_root(dir->get_parent_dir());
979
980 if (parent != dir && // we have a parent,
981 parent->dir_auth == dir->dir_auth) { // auth matches,
982 // merge with parent.
983 dout(10) << " subtree merge at " << *dir << dendl;
984 dir->set_dir_auth(CDIR_AUTH_DEFAULT);
985
986 // move our bounds under the parent
987 subtrees[parent].insert(it->second.begin(), it->second.end());
988
989 // we are no longer a subtree or bound
990 dir->put(CDir::PIN_SUBTREE);
991 subtrees.erase(it);
992 subtrees[parent].erase(dir);
993
994 // adjust popularity?
995 if (adjust_pop && dir->is_auth()) {
996 CDir *cur = dir;
997 CDir *p = dir->get_parent_dir();
998 while (p) {
999 p->pop_auth_subtree.add(dir->pop_auth_subtree);
1000 p->pop_lru_subdirs.push_front(&cur->get_inode()->item_pop_lru);
1001 if (p->is_subtree_root()) break;
1002 cur = p;
1003 p = p->inode->get_parent_dir();
1004 }
1005 }
1006
1007 if (to_eval && dir->get_inode()->is_auth())
1008 to_eval->insert(dir->get_inode());
1009
1010 show_subtrees(15);
1011 }
1012 }
1013
1014 void MDCache::eval_subtree_root(CInode *diri)
1015 {
1016 // evaluate subtree inode filelock?
1017 // (we should scatter the filelock on subtree bounds)
1018 ceph_assert(diri->is_auth());
1019 mds->locker->try_eval(diri, CEPH_LOCK_IFILE | CEPH_LOCK_INEST);
1020 }
1021
1022
1023 void MDCache::adjust_bounded_subtree_auth(CDir *dir, const set<CDir*>& bounds, mds_authority_t auth)
1024 {
1025 dout(7) << "adjust_bounded_subtree_auth " << dir->get_dir_auth() << " -> " << auth
1026 << " on " << *dir
1027 << " bounds " << bounds
1028 << dendl;
1029
1030 show_subtrees();
1031
1032 CDir *root;
1033 if (dir->ino() == CEPH_INO_ROOT) {
1034 root = dir; // bootstrap hack.
1035 if (subtrees.count(root) == 0) {
1036 subtrees[root];
1037 root->get(CDir::PIN_SUBTREE);
1038 }
1039 } else {
1040 root = get_subtree_root(dir); // subtree root
1041 }
1042 ceph_assert(root);
1043 ceph_assert(subtrees.count(root));
1044 dout(7) << " current root is " << *root << dendl;
1045
1046 mds_authority_t oldauth = dir->authority();
1047
1048 if (root == dir) {
1049 // i am already a subtree.
1050 dir->set_dir_auth(auth);
1051 } else {
1052 // i am a new subtree.
1053 dout(10) << " new subtree at " << *dir << dendl;
1054 ceph_assert(subtrees.count(dir) == 0);
1055 subtrees[dir]; // create empty subtree bounds list for me.
1056 dir->get(CDir::PIN_SUBTREE);
1057
1058 // set dir_auth
1059 dir->set_dir_auth(auth);
1060
1061 // move items nested beneath me, under me.
1062 set<CDir*>::iterator p = subtrees[root].begin();
1063 while (p != subtrees[root].end()) {
1064 set<CDir*>::iterator next = p;
1065 ++next;
1066 if (get_subtree_root((*p)->get_parent_dir()) == dir) {
1067 // move under me
1068 dout(10) << " claiming child bound " << **p << dendl;
1069 subtrees[dir].insert(*p);
1070 subtrees[root].erase(p);
1071 }
1072 p = next;
1073 }
1074
1075 // i am a bound of the parent subtree.
1076 subtrees[root].insert(dir);
1077
1078 // i am now the subtree root.
1079 root = dir;
1080 }
1081
1082 set<CInode*> to_eval;
1083
1084 // verify/adjust bounds.
1085 // - these may be new, or
1086 // - beneath existing ambiguous bounds (which will be collapsed),
1087 // - but NOT beneath unambiguous bounds.
1088 for (const auto& bound : bounds) {
1089 // new bound?
1090 if (subtrees[dir].count(bound) == 0) {
1091 if (get_subtree_root(bound) == dir) {
1092 dout(10) << " new bound " << *bound << ", adjusting auth back to old " << oldauth << dendl;
1093 adjust_subtree_auth(bound, oldauth); // otherwise, adjust at bound.
1094 }
1095 else {
1096 dout(10) << " want bound " << *bound << dendl;
1097 CDir *t = get_subtree_root(bound->get_parent_dir());
1098 if (subtrees[t].count(bound) == 0) {
1099 ceph_assert(t != dir);
1100 dout(10) << " new bound " << *bound << dendl;
1101 adjust_subtree_auth(bound, t->authority());
1102 }
1103 // make sure it's nested beneath ambiguous subtree(s)
1104 while (1) {
1105 while (subtrees[dir].count(t) == 0)
1106 t = get_subtree_root(t->get_parent_dir());
1107 dout(10) << " swallowing intervening subtree at " << *t << dendl;
1108 adjust_subtree_auth(t, auth);
1109 try_subtree_merge_at(t, &to_eval);
1110 t = get_subtree_root(bound->get_parent_dir());
1111 if (t == dir) break;
1112 }
1113 }
1114 }
1115 else {
1116 dout(10) << " already have bound " << *bound << dendl;
1117 }
1118 }
1119 // merge stray bounds?
1120 while (!subtrees[dir].empty()) {
1121 set<CDir*> copy = subtrees[dir];
1122 for (set<CDir*>::iterator p = copy.begin(); p != copy.end(); ++p) {
1123 if (bounds.count(*p) == 0) {
1124 CDir *stray = *p;
1125 dout(10) << " swallowing extra subtree at " << *stray << dendl;
1126 adjust_subtree_auth(stray, auth);
1127 try_subtree_merge_at(stray, &to_eval);
1128 }
1129 }
1130 // swallowing subtree may add new subtree bounds
1131 if (copy == subtrees[dir])
1132 break;
1133 }
1134
1135 // bound should now match.
1136 verify_subtree_bounds(dir, bounds);
1137
1138 show_subtrees();
1139
1140 if (!(mds->is_any_replay() || mds->is_resolve())) {
1141 for(auto in : to_eval)
1142 eval_subtree_root(in);
1143 }
1144 }
1145
1146
1147 /*
1148 * return a set of CDir*'s that correspond to the given bound set. Only adjust
1149 * fragmentation as necessary to get an equivalent bounding set. That is, only
1150 * split if one of our frags spans the provided bounding set. Never merge.
1151 */
1152 void MDCache::get_force_dirfrag_bound_set(const vector<dirfrag_t>& dfs, set<CDir*>& bounds)
1153 {
1154 dout(10) << "get_force_dirfrag_bound_set " << dfs << dendl;
1155
1156 // sort by ino
1157 map<inodeno_t, fragset_t> byino;
1158 for (auto& frag : dfs) {
1159 byino[frag.ino].insert_raw(frag.frag);
1160 }
1161 dout(10) << " by ino: " << byino << dendl;
1162
1163 for (map<inodeno_t,fragset_t>::iterator p = byino.begin(); p != byino.end(); ++p) {
1164 p->second.simplify();
1165 CInode *diri = get_inode(p->first);
1166 if (!diri)
1167 continue;
1168 dout(10) << " checking fragset " << p->second.get() << " on " << *diri << dendl;
1169
1170 fragtree_t tmpdft;
1171 for (set<frag_t>::iterator q = p->second.begin(); q != p->second.end(); ++q)
1172 tmpdft.force_to_leaf(g_ceph_context, *q);
1173
1174 for (const auto& fg : p->second) {
1175 frag_vec_t leaves;
1176 diri->dirfragtree.get_leaves_under(fg, leaves);
1177 if (leaves.empty()) {
1178 frag_t approx_fg = diri->dirfragtree[fg.value()];
1179 frag_vec_t approx_leaves;
1180 tmpdft.get_leaves_under(approx_fg, approx_leaves);
1181 for (const auto& leaf : approx_leaves) {
1182 if (p->second.get().count(leaf) == 0) {
1183 // not bound, so the resolve message is from auth MDS of the dirfrag
1184 force_dir_fragment(diri, leaf);
1185 }
1186 }
1187 }
1188
1189 auto&& [complete, sibs] = diri->get_dirfrags_under(fg);
1190 for (const auto& sib : sibs)
1191 bounds.insert(sib);
1192 }
1193 }
1194 }
1195
1196 void MDCache::adjust_bounded_subtree_auth(CDir *dir, const vector<dirfrag_t>& bound_dfs, const mds_authority_t &auth)
1197 {
1198 dout(7) << "adjust_bounded_subtree_auth " << dir->get_dir_auth() << " -> " << auth
1199 << " on " << *dir << " bound_dfs " << bound_dfs << dendl;
1200
1201 set<CDir*> bounds;
1202 get_force_dirfrag_bound_set(bound_dfs, bounds);
1203 adjust_bounded_subtree_auth(dir, bounds, auth);
1204 }
1205
1206 void MDCache::map_dirfrag_set(const list<dirfrag_t>& dfs, set<CDir*>& result)
1207 {
1208 dout(10) << "map_dirfrag_set " << dfs << dendl;
1209
1210 // group by inode
1211 map<inodeno_t, fragset_t> ino_fragset;
1212 for (const auto &df : dfs) {
1213 ino_fragset[df.ino].insert_raw(df.frag);
1214 }
1215 // get frags
1216 for (map<inodeno_t, fragset_t>::iterator p = ino_fragset.begin();
1217 p != ino_fragset.end();
1218 ++p) {
1219 p->second.simplify();
1220 CInode *in = get_inode(p->first);
1221 if (!in)
1222 continue;
1223
1224 frag_vec_t fgs;
1225 for (const auto& fg : p->second) {
1226 in->dirfragtree.get_leaves_under(fg, fgs);
1227 }
1228
1229 dout(15) << "map_dirfrag_set " << p->second << " -> " << fgs
1230 << " on " << *in << dendl;
1231
1232 for (const auto& fg : fgs) {
1233 CDir *dir = in->get_dirfrag(fg);
1234 if (dir)
1235 result.insert(dir);
1236 }
1237 }
1238 }
1239
1240
1241
1242 CDir *MDCache::get_subtree_root(CDir *dir)
1243 {
1244 // find the underlying dir that delegates (or is about to delegate) auth
1245 while (true) {
1246 if (dir->is_subtree_root())
1247 return dir;
1248 dir = dir->get_inode()->get_parent_dir();
1249 if (!dir)
1250 return 0; // none
1251 }
1252 }
1253
1254 CDir *MDCache::get_projected_subtree_root(CDir *dir)
1255 {
1256 // find the underlying dir that delegates (or is about to delegate) auth
1257 while (true) {
1258 if (dir->is_subtree_root())
1259 return dir;
1260 dir = dir->get_inode()->get_projected_parent_dir();
1261 if (!dir)
1262 return 0; // none
1263 }
1264 }
1265
1266 void MDCache::remove_subtree(CDir *dir)
1267 {
1268 dout(10) << "remove_subtree " << *dir << dendl;
1269 auto it = subtrees.find(dir);
1270 ceph_assert(it != subtrees.end());
1271 subtrees.erase(it);
1272 dir->put(CDir::PIN_SUBTREE);
1273 if (dir->get_parent_dir()) {
1274 CDir *p = get_subtree_root(dir->get_parent_dir());
1275 auto it = subtrees.find(p);
1276 ceph_assert(it != subtrees.end());
1277 auto count = it->second.erase(dir);
1278 ceph_assert(count == 1);
1279 }
1280 }
1281
1282 void MDCache::get_subtree_bounds(CDir *dir, set<CDir*>& bounds)
1283 {
1284 ceph_assert(subtrees.count(dir));
1285 bounds = subtrees[dir];
1286 }
1287
1288 void MDCache::get_wouldbe_subtree_bounds(CDir *dir, set<CDir*>& bounds)
1289 {
1290 if (subtrees.count(dir)) {
1291 // just copy them, dir is a subtree.
1292 get_subtree_bounds(dir, bounds);
1293 } else {
1294 // find them
1295 CDir *root = get_subtree_root(dir);
1296 for (set<CDir*>::iterator p = subtrees[root].begin();
1297 p != subtrees[root].end();
1298 ++p) {
1299 CDir *t = *p;
1300 while (t != root) {
1301 t = t->get_parent_dir();
1302 ceph_assert(t);
1303 if (t == dir) {
1304 bounds.insert(*p);
1305 continue;
1306 }
1307 }
1308 }
1309 }
1310 }
1311
1312 void MDCache::verify_subtree_bounds(CDir *dir, const set<CDir*>& bounds)
1313 {
1314 // for debugging only.
1315 ceph_assert(subtrees.count(dir));
1316 if (bounds != subtrees[dir]) {
1317 dout(0) << "verify_subtree_bounds failed" << dendl;
1318 set<CDir*> b = bounds;
1319 for (auto &cd : subtrees[dir]) {
1320 if (bounds.count(cd)) {
1321 b.erase(cd);
1322 continue;
1323 }
1324 dout(0) << " missing bound " << *cd << dendl;
1325 }
1326 for (const auto &cd : b)
1327 dout(0) << " extra bound " << *cd << dendl;
1328 }
1329 ceph_assert(bounds == subtrees[dir]);
1330 }
1331
1332 void MDCache::verify_subtree_bounds(CDir *dir, const list<dirfrag_t>& bounds)
1333 {
1334 // for debugging only.
1335 ceph_assert(subtrees.count(dir));
1336
1337 // make sure that any bounds i do have are properly noted as such.
1338 int failed = 0;
1339 for (const auto &fg : bounds) {
1340 CDir *bd = get_dirfrag(fg);
1341 if (!bd) continue;
1342 if (subtrees[dir].count(bd) == 0) {
1343 dout(0) << "verify_subtree_bounds failed: extra bound " << *bd << dendl;
1344 failed++;
1345 }
1346 }
1347 ceph_assert(failed == 0);
1348 }
1349
1350 void MDCache::project_subtree_rename(CInode *diri, CDir *olddir, CDir *newdir)
1351 {
1352 dout(10) << "project_subtree_rename " << *diri << " from " << *olddir
1353 << " to " << *newdir << dendl;
1354 projected_subtree_renames[diri].push_back(pair<CDir*,CDir*>(olddir, newdir));
1355 }
1356
1357 void MDCache::adjust_subtree_after_rename(CInode *diri, CDir *olddir, bool pop)
1358 {
1359 dout(10) << "adjust_subtree_after_rename " << *diri << " from " << *olddir << dendl;
1360
1361 CDir *newdir = diri->get_parent_dir();
1362
1363 if (pop) {
1364 map<CInode*,list<pair<CDir*,CDir*> > >::iterator p = projected_subtree_renames.find(diri);
1365 ceph_assert(p != projected_subtree_renames.end());
1366 ceph_assert(!p->second.empty());
1367 ceph_assert(p->second.front().first == olddir);
1368 ceph_assert(p->second.front().second == newdir);
1369 p->second.pop_front();
1370 if (p->second.empty())
1371 projected_subtree_renames.erase(p);
1372 }
1373
1374 // adjust total auth pin of freezing subtree
1375 if (olddir != newdir) {
1376 auto&& dfls = diri->get_nested_dirfrags();
1377 for (const auto& dir : dfls)
1378 olddir->adjust_freeze_after_rename(dir);
1379 }
1380
1381 // adjust subtree
1382 // N.B. make sure subtree dirfrags are at the front of the list
1383 auto dfls = diri->get_subtree_dirfrags();
1384 diri->get_nested_dirfrags(dfls);
1385 for (const auto& dir : dfls) {
1386 dout(10) << "dirfrag " << *dir << dendl;
1387 CDir *oldparent = get_subtree_root(olddir);
1388 dout(10) << " old parent " << *oldparent << dendl;
1389 CDir *newparent = get_subtree_root(newdir);
1390 dout(10) << " new parent " << *newparent << dendl;
1391
1392 auto& oldbounds = subtrees[oldparent];
1393 auto& newbounds = subtrees[newparent];
1394
1395 if (olddir != newdir)
1396 mds->balancer->adjust_pop_for_rename(olddir, dir, false);
1397
1398 if (oldparent == newparent) {
1399 dout(10) << "parent unchanged for " << *dir << " at " << *oldparent << dendl;
1400 } else if (dir->is_subtree_root()) {
1401 // children are fine. change parent.
1402 dout(10) << "moving " << *dir << " from " << *oldparent << " to " << *newparent << dendl;
1403 {
1404 auto n = oldbounds.erase(dir);
1405 ceph_assert(n == 1);
1406 }
1407 newbounds.insert(dir);
1408 // caller is responsible for 'eval diri'
1409 try_subtree_merge_at(dir, NULL, false);
1410 } else {
1411 // mid-subtree.
1412
1413 // see if any old bounds move to the new parent.
1414 std::vector<CDir*> tomove;
1415 for (const auto& bound : oldbounds) {
1416 CDir *broot = get_subtree_root(bound->get_parent_dir());
1417 if (broot != oldparent) {
1418 ceph_assert(broot == newparent);
1419 tomove.push_back(bound);
1420 }
1421 }
1422 for (const auto& bound : tomove) {
1423 dout(10) << "moving bound " << *bound << " from " << *oldparent << " to " << *newparent << dendl;
1424 oldbounds.erase(bound);
1425 newbounds.insert(bound);
1426 }
1427
1428 // did auth change?
1429 if (oldparent->authority() != newparent->authority()) {
1430 adjust_subtree_auth(dir, oldparent->authority(), false);
1431 // caller is responsible for 'eval diri'
1432 try_subtree_merge_at(dir, NULL, false);
1433 }
1434 }
1435
1436 if (olddir != newdir)
1437 mds->balancer->adjust_pop_for_rename(newdir, dir, true);
1438 }
1439
1440 show_subtrees();
1441 }
1442
1443 // ===================================
1444 // journal and snap/cow helpers
1445
1446
1447 /*
1448 * find first inode in cache that follows given snapid. otherwise, return current.
1449 */
1450 CInode *MDCache::pick_inode_snap(CInode *in, snapid_t follows)
1451 {
1452 dout(10) << "pick_inode_snap follows " << follows << " on " << *in << dendl;
1453 ceph_assert(in->last == CEPH_NOSNAP);
1454
1455 auto p = snap_inode_map.upper_bound(vinodeno_t(in->ino(), follows));
1456 if (p != snap_inode_map.end() && p->second->ino() == in->ino()) {
1457 dout(10) << "pick_inode_snap found " << *p->second << dendl;
1458 in = p->second;
1459 }
1460
1461 return in;
1462 }
1463
1464
1465 /*
1466 * note: i'm currently cheating wrt dirty and inode.version on cow
1467 * items. instead of doing a full dir predirty, i just take the
1468 * original item's version, and set the dirty flag (via
1469 * mutation::add_cow_{inode,dentry}() and mutation::apply(). that
1470 * means a special case in the dir commit clean sweep assertions.
1471 * bah.
1472 */
1473 CInode *MDCache::cow_inode(CInode *in, snapid_t last)
1474 {
1475 ceph_assert(last >= in->first);
1476
1477 CInode *oldin = new CInode(this, true, in->first, last);
1478 auto _inode = CInode::allocate_inode(*in->get_previous_projected_inode());
1479 _inode->trim_client_ranges(last);
1480 oldin->reset_inode(std::move(_inode));
1481 auto _xattrs = in->get_previous_projected_xattrs();
1482 oldin->reset_xattrs(std::move(_xattrs));
1483
1484 oldin->symlink = in->symlink;
1485
1486 if (in->first < in->oldest_snap)
1487 in->oldest_snap = in->first;
1488
1489 in->first = last+1;
1490
1491 dout(10) << "cow_inode " << *in << " to " << *oldin << dendl;
1492 add_inode(oldin);
1493
1494 if (in->last != CEPH_NOSNAP) {
1495 CInode *head_in = get_inode(in->ino());
1496 ceph_assert(head_in);
1497 auto ret = head_in->split_need_snapflush(oldin, in);
1498 if (ret.first) {
1499 oldin->client_snap_caps = in->client_snap_caps;
1500 if (!oldin->client_snap_caps.empty()) {
1501 for (int i = 0; i < num_cinode_locks; i++) {
1502 SimpleLock *lock = oldin->get_lock(cinode_lock_info[i].lock);
1503 ceph_assert(lock);
1504 if (lock->get_state() != LOCK_SNAP_SYNC) {
1505 ceph_assert(lock->is_stable());
1506 lock->set_state(LOCK_SNAP_SYNC); // gathering
1507 oldin->auth_pin(lock);
1508 }
1509 lock->get_wrlock(true);
1510 }
1511 }
1512 }
1513 if (!ret.second) {
1514 auto client_snap_caps = std::move(in->client_snap_caps);
1515 in->client_snap_caps.clear();
1516 in->item_open_file.remove_myself();
1517 in->item_caps.remove_myself();
1518
1519 if (!client_snap_caps.empty()) {
1520 MDSContext::vec finished;
1521 for (int i = 0; i < num_cinode_locks; i++) {
1522 SimpleLock *lock = in->get_lock(cinode_lock_info[i].lock);
1523 ceph_assert(lock);
1524 ceph_assert(lock->get_state() == LOCK_SNAP_SYNC); // gathering
1525 lock->put_wrlock();
1526 if (!lock->get_num_wrlocks()) {
1527 lock->set_state(LOCK_SYNC);
1528 lock->take_waiting(SimpleLock::WAIT_STABLE|SimpleLock::WAIT_RD, finished);
1529 in->auth_unpin(lock);
1530 }
1531 }
1532 mds->queue_waiters(finished);
1533 }
1534 }
1535 return oldin;
1536 }
1537
1538 if (!in->client_caps.empty()) {
1539 const set<snapid_t>& snaps = in->find_snaprealm()->get_snaps();
1540 // clone caps?
1541 for (auto &p : in->client_caps) {
1542 client_t client = p.first;
1543 Capability *cap = &p.second;
1544 int issued = cap->need_snapflush() ? CEPH_CAP_ANY_WR : cap->issued();
1545 if ((issued & CEPH_CAP_ANY_WR) &&
1546 cap->client_follows < last) {
1547 dout(10) << " client." << client << " cap " << ccap_string(issued) << dendl;
1548 oldin->client_snap_caps.insert(client);
1549 cap->client_follows = last;
1550
1551 // we need snapflushes for any intervening snaps
1552 dout(10) << " snaps " << snaps << dendl;
1553 for (auto q = snaps.lower_bound(oldin->first);
1554 q != snaps.end() && *q <= last;
1555 ++q) {
1556 in->add_need_snapflush(oldin, *q, client);
1557 }
1558 } else {
1559 dout(10) << " ignoring client." << client << " cap follows " << cap->client_follows << dendl;
1560 }
1561 }
1562
1563 if (!oldin->client_snap_caps.empty()) {
1564 for (int i = 0; i < num_cinode_locks; i++) {
1565 SimpleLock *lock = oldin->get_lock(cinode_lock_info[i].lock);
1566 ceph_assert(lock);
1567 if (lock->get_state() != LOCK_SNAP_SYNC) {
1568 ceph_assert(lock->is_stable());
1569 lock->set_state(LOCK_SNAP_SYNC); // gathering
1570 oldin->auth_pin(lock);
1571 }
1572 lock->get_wrlock(true);
1573 }
1574 }
1575 }
1576 return oldin;
1577 }
1578
1579 void MDCache::journal_cow_dentry(MutationImpl *mut, EMetaBlob *metablob,
1580 CDentry *dn, snapid_t follows,
1581 CInode **pcow_inode, CDentry::linkage_t *dnl)
1582 {
1583 if (!dn) {
1584 dout(10) << "journal_cow_dentry got null CDentry, returning" << dendl;
1585 return;
1586 }
1587 dout(10) << "journal_cow_dentry follows " << follows << " on " << *dn << dendl;
1588 ceph_assert(dn->is_auth());
1589
1590 // nothing to cow on a null dentry, fix caller
1591 if (!dnl)
1592 dnl = dn->get_projected_linkage();
1593 ceph_assert(!dnl->is_null());
1594
1595 CInode *in = dnl->is_primary() ? dnl->get_inode() : NULL;
1596 bool cow_head = false;
1597 if (in && in->state_test(CInode::STATE_AMBIGUOUSAUTH)) {
1598 ceph_assert(in->is_frozen_inode());
1599 cow_head = true;
1600 }
1601 if (in && (in->is_multiversion() || cow_head)) {
1602 // multiversion inode.
1603 SnapRealm *realm = NULL;
1604
1605 if (in->get_projected_parent_dn() != dn) {
1606 ceph_assert(follows == CEPH_NOSNAP);
1607 realm = dn->dir->inode->find_snaprealm();
1608 snapid_t dir_follows = get_global_snaprealm()->get_newest_seq();
1609 ceph_assert(dir_follows >= realm->get_newest_seq());
1610
1611 if (dir_follows+1 > dn->first) {
1612 snapid_t oldfirst = dn->first;
1613 dn->first = dir_follows+1;
1614 if (realm->has_snaps_in_range(oldfirst, dir_follows)) {
1615 CDir *dir = dn->dir;
1616 CDentry *olddn = dir->add_remote_dentry(dn->get_name(), in->ino(), in->d_type(), dn->alternate_name, oldfirst, dir_follows);
1617 dout(10) << " olddn " << *olddn << dendl;
1618 ceph_assert(dir->is_projected());
1619 olddn->set_projected_version(dir->get_projected_version());
1620 metablob->add_remote_dentry(olddn, true);
1621 mut->add_cow_dentry(olddn);
1622 // FIXME: adjust link count here? hmm.
1623
1624 if (dir_follows+1 > in->first)
1625 in->cow_old_inode(dir_follows, cow_head);
1626 }
1627 }
1628
1629 follows = dir_follows;
1630 if (in->snaprealm) {
1631 realm = in->snaprealm;
1632 ceph_assert(follows >= realm->get_newest_seq());
1633 }
1634 } else {
1635 realm = in->find_snaprealm();
1636 if (follows == CEPH_NOSNAP) {
1637 follows = get_global_snaprealm()->get_newest_seq();
1638 ceph_assert(follows >= realm->get_newest_seq());
1639 }
1640 }
1641
1642 // already cloned?
1643 if (follows < in->first) {
1644 dout(10) << "journal_cow_dentry follows " << follows << " < first on " << *in << dendl;
1645 return;
1646 }
1647
1648 if (!realm->has_snaps_in_range(in->first, follows)) {
1649 dout(10) << "journal_cow_dentry no snapshot follows " << follows << " on " << *in << dendl;
1650 in->first = follows + 1;
1651 return;
1652 }
1653
1654 in->cow_old_inode(follows, cow_head);
1655
1656 } else {
1657 SnapRealm *realm = dn->dir->inode->find_snaprealm();
1658 if (follows == CEPH_NOSNAP) {
1659 follows = get_global_snaprealm()->get_newest_seq();
1660 ceph_assert(follows >= realm->get_newest_seq());
1661 }
1662
1663 // already cloned?
1664 if (follows < dn->first) {
1665 dout(10) << "journal_cow_dentry follows " << follows << " < first on " << *dn << dendl;
1666 return;
1667 }
1668
1669 // update dn.first before adding old dentry to cdir's map
1670 snapid_t oldfirst = dn->first;
1671 dn->first = follows+1;
1672
1673 if (!realm->has_snaps_in_range(oldfirst, follows)) {
1674 dout(10) << "journal_cow_dentry no snapshot follows " << follows << " on " << *dn << dendl;
1675 if (in)
1676 in->first = follows+1;
1677 return;
1678 }
1679
1680 dout(10) << " dn " << *dn << dendl;
1681 CDir *dir = dn->get_dir();
1682 ceph_assert(dir->is_projected());
1683
1684 if (in) {
1685 CInode *oldin = cow_inode(in, follows);
1686 ceph_assert(in->is_projected());
1687 mut->add_cow_inode(oldin);
1688 if (pcow_inode)
1689 *pcow_inode = oldin;
1690 CDentry *olddn = dir->add_primary_dentry(dn->get_name(), oldin, dn->alternate_name, oldfirst, follows);
1691 dout(10) << " olddn " << *olddn << dendl;
1692 bool need_snapflush = !oldin->client_snap_caps.empty();
1693 if (need_snapflush) {
1694 mut->ls->open_files.push_back(&oldin->item_open_file);
1695 mds->locker->mark_need_snapflush_inode(oldin);
1696 }
1697 olddn->set_projected_version(dir->get_projected_version());
1698 metablob->add_primary_dentry(olddn, 0, true, false, false, need_snapflush);
1699 mut->add_cow_dentry(olddn);
1700 } else {
1701 ceph_assert(dnl->is_remote());
1702 CDentry *olddn = dir->add_remote_dentry(dn->get_name(), dnl->get_remote_ino(), dnl->get_remote_d_type(), dn->alternate_name, oldfirst, follows);
1703 dout(10) << " olddn " << *olddn << dendl;
1704
1705 olddn->set_projected_version(dir->get_projected_version());
1706 metablob->add_remote_dentry(olddn, true);
1707 mut->add_cow_dentry(olddn);
1708 }
1709 }
1710 }
1711
1712 void MDCache::journal_dirty_inode(MutationImpl *mut, EMetaBlob *metablob, CInode *in, snapid_t follows)
1713 {
1714 if (in->is_base()) {
1715 metablob->add_root(true, in);
1716 } else {
1717 if (follows == CEPH_NOSNAP && in->last != CEPH_NOSNAP)
1718 follows = in->first - 1;
1719 CDentry *dn = in->get_projected_parent_dn();
1720 if (!dn->get_projected_linkage()->is_null()) // no need to cow a null dentry
1721 journal_cow_dentry(mut, metablob, dn, follows);
1722 if (in->get_projected_inode()->is_backtrace_updated()) {
1723 bool dirty_pool = in->get_projected_inode()->layout.pool_id !=
1724 in->get_previous_projected_inode()->layout.pool_id;
1725 metablob->add_primary_dentry(dn, in, true, true, dirty_pool);
1726 } else {
1727 metablob->add_primary_dentry(dn, in, true);
1728 }
1729 }
1730 }
1731
1732
1733
1734 // nested ---------------------------------------------------------------
1735
1736 void MDCache::project_rstat_inode_to_frag(const MutationRef& mut,
1737 CInode *cur, CDir *parent, snapid_t first,
1738 int linkunlink, SnapRealm *prealm)
1739 {
1740 CDentry *parentdn = cur->get_projected_parent_dn();
1741
1742 if (cur->first > first)
1743 first = cur->first;
1744
1745 dout(10) << "projected_rstat_inode_to_frag first " << first << " linkunlink " << linkunlink
1746 << " " << *cur << dendl;
1747 dout(20) << " frag head is [" << parent->first << ",head] " << dendl;
1748 dout(20) << " inode update is [" << first << "," << cur->last << "]" << dendl;
1749
1750 /*
1751 * FIXME. this incompletely propagates rstats to _old_ parents
1752 * (i.e. shortly after a directory rename). but we need full
1753 * blown hard link backpointers to make this work properly...
1754 */
1755 snapid_t floor = parentdn->first;
1756 dout(20) << " floor of " << floor << " from parent dn " << *parentdn << dendl;
1757
1758 if (!prealm)
1759 prealm = parent->inode->find_snaprealm();
1760 const set<snapid_t> snaps = prealm->get_snaps();
1761
1762 if (cur->last != CEPH_NOSNAP) {
1763 ceph_assert(cur->dirty_old_rstats.empty());
1764 set<snapid_t>::const_iterator q = snaps.lower_bound(std::max(first, floor));
1765 if (q == snaps.end() || *q > cur->last)
1766 return;
1767 }
1768
1769 if (cur->last >= floor) {
1770 bool update = true;
1771 if (cur->state_test(CInode::STATE_AMBIGUOUSAUTH) && cur->is_auth()) {
1772 // rename src inode is not projected in the peer rename prep case. so we should
1773 // avoid updateing the inode.
1774 ceph_assert(linkunlink < 0);
1775 ceph_assert(cur->is_frozen_inode());
1776 update = false;
1777 }
1778 // hacky
1779 const CInode::mempool_inode *pi;
1780 if (update && mut->is_projected(cur)) {
1781 pi = cur->_get_projected_inode();
1782 } else {
1783 pi = cur->get_projected_inode().get();
1784 if (update) {
1785 // new inode
1786 ceph_assert(pi->rstat == pi->accounted_rstat);
1787 update = false;
1788 }
1789 }
1790 _project_rstat_inode_to_frag(pi, std::max(first, floor), cur->last, parent,
1791 linkunlink, update);
1792 }
1793
1794 if (g_conf()->mds_snap_rstat) {
1795 for (const auto &p : cur->dirty_old_rstats) {
1796 const auto &old = cur->get_old_inodes()->at(p);
1797 snapid_t ofirst = std::max(old.first, floor);
1798 auto it = snaps.lower_bound(ofirst);
1799 if (it == snaps.end() || *it > p)
1800 continue;
1801 if (p >= floor)
1802 _project_rstat_inode_to_frag(&old.inode, ofirst, p, parent, 0, false);
1803 }
1804 }
1805 cur->dirty_old_rstats.clear();
1806 }
1807
1808
1809 void MDCache::_project_rstat_inode_to_frag(const CInode::mempool_inode* inode, snapid_t ofirst, snapid_t last,
1810 CDir *parent, int linkunlink, bool update_inode)
1811 {
1812 dout(10) << "_project_rstat_inode_to_frag [" << ofirst << "," << last << "]" << dendl;
1813 dout(20) << " inode rstat " << inode->rstat << dendl;
1814 dout(20) << " inode accounted_rstat " << inode->accounted_rstat << dendl;
1815 nest_info_t delta;
1816 if (linkunlink == 0) {
1817 delta.add(inode->rstat);
1818 delta.sub(inode->accounted_rstat);
1819 } else if (linkunlink < 0) {
1820 delta.sub(inode->accounted_rstat);
1821 } else {
1822 delta.add(inode->rstat);
1823 }
1824 dout(20) << " delta " << delta << dendl;
1825
1826
1827 while (last >= ofirst) {
1828 /*
1829 * pick fnode version to update. at each iteration, we want to
1830 * pick a segment ending in 'last' to update. split as necessary
1831 * to make that work. then, adjust first up so that we only
1832 * update one segment at a time. then loop to cover the whole
1833 * [ofirst,last] interval.
1834 */
1835 nest_info_t *prstat;
1836 snapid_t first;
1837 auto pf = parent->_get_projected_fnode();
1838 if (last == CEPH_NOSNAP) {
1839 if (g_conf()->mds_snap_rstat)
1840 first = std::max(ofirst, parent->first);
1841 else
1842 first = parent->first;
1843 prstat = &pf->rstat;
1844 dout(20) << " projecting to head [" << first << "," << last << "] " << *prstat << dendl;
1845
1846 if (first > parent->first &&
1847 !(pf->rstat == pf->accounted_rstat)) {
1848 dout(10) << " target snapped and not fully accounted, cow to dirty_old_rstat ["
1849 << parent->first << "," << (first-1) << "] "
1850 << " " << *prstat << "/" << pf->accounted_rstat
1851 << dendl;
1852 parent->dirty_old_rstat[first-1].first = parent->first;
1853 parent->dirty_old_rstat[first-1].rstat = pf->rstat;
1854 parent->dirty_old_rstat[first-1].accounted_rstat = pf->accounted_rstat;
1855 }
1856 parent->first = first;
1857 } else if (!g_conf()->mds_snap_rstat) {
1858 // drop snapshots' rstats
1859 break;
1860 } else if (last >= parent->first) {
1861 first = parent->first;
1862 parent->dirty_old_rstat[last].first = first;
1863 parent->dirty_old_rstat[last].rstat = pf->rstat;
1864 parent->dirty_old_rstat[last].accounted_rstat = pf->accounted_rstat;
1865 prstat = &parent->dirty_old_rstat[last].rstat;
1866 dout(10) << " projecting to newly split dirty_old_fnode [" << first << "," << last << "] "
1867 << " " << *prstat << "/" << pf->accounted_rstat << dendl;
1868 } else {
1869 // be careful, dirty_old_rstat is a _sparse_ map.
1870 // sorry, this is ugly.
1871 first = ofirst;
1872
1873 // find any intersection with last
1874 auto it = parent->dirty_old_rstat.lower_bound(last);
1875 if (it == parent->dirty_old_rstat.end()) {
1876 dout(20) << " no dirty_old_rstat with last >= last " << last << dendl;
1877 if (!parent->dirty_old_rstat.empty() && parent->dirty_old_rstat.rbegin()->first >= first) {
1878 dout(20) << " last dirty_old_rstat ends at " << parent->dirty_old_rstat.rbegin()->first << dendl;
1879 first = parent->dirty_old_rstat.rbegin()->first+1;
1880 }
1881 } else {
1882 // *it last is >= last
1883 if (it->second.first <= last) {
1884 // *it intersects [first,last]
1885 if (it->second.first < first) {
1886 dout(10) << " splitting off left bit [" << it->second.first << "," << first-1 << "]" << dendl;
1887 parent->dirty_old_rstat[first-1] = it->second;
1888 it->second.first = first;
1889 }
1890 if (it->second.first > first)
1891 first = it->second.first;
1892 if (last < it->first) {
1893 dout(10) << " splitting off right bit [" << last+1 << "," << it->first << "]" << dendl;
1894 parent->dirty_old_rstat[last] = it->second;
1895 it->second.first = last+1;
1896 }
1897 } else {
1898 // *it is to the _right_ of [first,last]
1899 it = parent->dirty_old_rstat.lower_bound(first);
1900 // new *it last is >= first
1901 if (it->second.first <= last && // new *it isn't also to the right, and
1902 it->first >= first) { // it intersects our first bit,
1903 dout(10) << " staying to the right of [" << it->second.first << "," << it->first << "]..." << dendl;
1904 first = it->first+1;
1905 }
1906 dout(10) << " projecting to new dirty_old_rstat [" << first << "," << last << "]" << dendl;
1907 }
1908 }
1909 dout(20) << " projecting to dirty_old_rstat [" << first << "," << last << "]" << dendl;
1910 parent->dirty_old_rstat[last].first = first;
1911 prstat = &parent->dirty_old_rstat[last].rstat;
1912 }
1913
1914 // apply
1915 dout(20) << " project to [" << first << "," << last << "] " << *prstat << dendl;
1916 ceph_assert(last >= first);
1917 prstat->add(delta);
1918 dout(20) << " result [" << first << "," << last << "] " << *prstat << " " << *parent << dendl;
1919
1920 last = first-1;
1921 }
1922
1923 if (update_inode) {
1924 auto _inode = const_cast<CInode::mempool_inode*>(inode);
1925 _inode->accounted_rstat = _inode->rstat;
1926 }
1927 }
1928
1929 void MDCache::project_rstat_frag_to_inode(const nest_info_t& rstat,
1930 const nest_info_t& accounted_rstat,
1931 snapid_t ofirst, snapid_t last,
1932 CInode *pin, bool cow_head)
1933 {
1934 dout(10) << "project_rstat_frag_to_inode [" << ofirst << "," << last << "]" << dendl;
1935 dout(20) << " frag rstat " << rstat << dendl;
1936 dout(20) << " frag accounted_rstat " << accounted_rstat << dendl;
1937 nest_info_t delta = rstat;
1938 delta.sub(accounted_rstat);
1939 dout(20) << " delta " << delta << dendl;
1940
1941 CInode::old_inode_map_ptr _old_inodes;
1942 while (last >= ofirst) {
1943 CInode::mempool_inode *pi;
1944 snapid_t first;
1945 if (last == pin->last) {
1946 pi = pin->_get_projected_inode();
1947 first = std::max(ofirst, pin->first);
1948 if (first > pin->first) {
1949 auto& old = pin->cow_old_inode(first-1, cow_head);
1950 dout(20) << " cloned old_inode rstat is " << old.inode.rstat << dendl;
1951 }
1952 } else {
1953 if (!_old_inodes) {
1954 _old_inodes = CInode::allocate_old_inode_map();
1955 if (pin->is_any_old_inodes())
1956 *_old_inodes = *pin->get_old_inodes();
1957 }
1958 if (last >= pin->first) {
1959 first = pin->first;
1960 pin->cow_old_inode(last, cow_head);
1961 } else {
1962 // our life is easier here because old_inodes is not sparse
1963 // (although it may not begin at snapid 1)
1964 auto it = _old_inodes->lower_bound(last);
1965 if (it == _old_inodes->end()) {
1966 dout(10) << " no old_inode <= " << last << ", done." << dendl;
1967 break;
1968 }
1969 first = it->second.first;
1970 if (first > last) {
1971 dout(10) << " oldest old_inode is [" << first << "," << it->first << "], done." << dendl;
1972 //assert(p == pin->old_inodes.begin());
1973 break;
1974 }
1975 if (it->first > last) {
1976 dout(10) << " splitting right old_inode [" << first << "," << it->first << "] to ["
1977 << (last+1) << "," << it->first << "]" << dendl;
1978 (*_old_inodes)[last] = it->second;
1979 it->second.first = last+1;
1980 pin->dirty_old_rstats.insert(it->first);
1981 }
1982 }
1983 if (first < ofirst) {
1984 dout(10) << " splitting left old_inode [" << first << "," << last << "] to ["
1985 << first << "," << ofirst-1 << "]" << dendl;
1986 (*_old_inodes)[ofirst-1] = (*_old_inodes)[last];
1987 pin->dirty_old_rstats.insert(ofirst-1);
1988 (*_old_inodes)[last].first = first = ofirst;
1989 }
1990 pi = &(*_old_inodes)[last].inode;
1991 pin->dirty_old_rstats.insert(last);
1992 }
1993 dout(20) << " projecting to [" << first << "," << last << "] " << pi->rstat << dendl;
1994 pi->rstat.add(delta);
1995 dout(20) << " result [" << first << "," << last << "] " << pi->rstat << dendl;
1996
1997 last = first-1;
1998 }
1999 if (_old_inodes)
2000 pin->reset_old_inodes(std::move(_old_inodes));
2001 }
2002
2003 void MDCache::broadcast_quota_to_client(CInode *in, client_t exclude_ct, bool quota_change)
2004 {
2005 if (!(mds->is_active() || mds->is_stopping()))
2006 return;
2007
2008 if (!in->is_auth() || in->is_frozen())
2009 return;
2010
2011 const auto& pi = in->get_projected_inode();
2012 if (!pi->quota.is_enabled() && !quota_change)
2013 return;
2014
2015 // creaete snaprealm for quota inode (quota was set before mimic)
2016 if (!in->get_projected_srnode())
2017 mds->server->create_quota_realm(in);
2018
2019 for (auto &p : in->client_caps) {
2020 Capability *cap = &p.second;
2021 if (cap->is_noquota())
2022 continue;
2023
2024 if (exclude_ct >= 0 && exclude_ct != p.first)
2025 goto update;
2026
2027 if (cap->last_rbytes == pi->rstat.rbytes &&
2028 cap->last_rsize == pi->rstat.rsize())
2029 continue;
2030
2031 if (pi->quota.max_files > 0) {
2032 if (pi->rstat.rsize() >= pi->quota.max_files)
2033 goto update;
2034
2035 if ((abs(cap->last_rsize - pi->quota.max_files) >> 4) <
2036 abs(cap->last_rsize - pi->rstat.rsize()))
2037 goto update;
2038 }
2039
2040 if (pi->quota.max_bytes > 0) {
2041 if (pi->rstat.rbytes > pi->quota.max_bytes - (pi->quota.max_bytes >> 3))
2042 goto update;
2043
2044 if ((abs(cap->last_rbytes - pi->quota.max_bytes) >> 4) <
2045 abs(cap->last_rbytes - pi->rstat.rbytes))
2046 goto update;
2047 }
2048
2049 continue;
2050
2051 update:
2052 cap->last_rsize = pi->rstat.rsize();
2053 cap->last_rbytes = pi->rstat.rbytes;
2054
2055 auto msg = make_message<MClientQuota>();
2056 msg->ino = in->ino();
2057 msg->rstat = pi->rstat;
2058 msg->quota = pi->quota;
2059 mds->send_message_client_counted(msg, cap->get_session());
2060 }
2061 for (const auto &it : in->get_replicas()) {
2062 auto msg = make_message<MGatherCaps>();
2063 msg->ino = in->ino();
2064 mds->send_message_mds(msg, it.first);
2065 }
2066 }
2067
2068 /*
2069 * NOTE: we _have_ to delay the scatter if we are called during a
2070 * rejoin, because we can't twiddle locks between when the
2071 * rejoin_(weak|strong) is received and when we send the rejoin_ack.
2072 * normally, this isn't a problem: a recover mds doesn't twiddle locks
2073 * (no requests), and a survivor acks immediately. _except_ that
2074 * during rejoin_(weak|strong) processing, we may complete a lock
2075 * gather, and do a scatter_writebehind.. and we _can't_ twiddle the
2076 * scatterlock state in that case or the lock states will get out of
2077 * sync between the auth and replica.
2078 *
2079 * the simple solution is to never do the scatter here. instead, put
2080 * the scatterlock on a list if it isn't already wrlockable. this is
2081 * probably the best plan anyway, since we avoid too many
2082 * scatters/locks under normal usage.
2083 */
2084 /*
2085 * some notes on dirlock/nestlock scatterlock semantics:
2086 *
2087 * the fragstat (dirlock) will never be updated without
2088 * dirlock+nestlock wrlock held by the caller.
2089 *
2090 * the rstat (nestlock) _may_ get updated without a wrlock when nested
2091 * data is pushed up the tree. this could be changed with some
2092 * restructuring here, but in its current form we ensure that the
2093 * fragstat+rstat _always_ reflect an accurrate summation over the dir
2094 * frag, which is nice. and, we only need to track frags that need to
2095 * be nudged (and not inodes with pending rstat changes that need to
2096 * be pushed into the frag). a consequence of this is that the
2097 * accounted_rstat on scatterlock sync may not match our current
2098 * rstat. this is normal and expected.
2099 */
2100 void MDCache::predirty_journal_parents(MutationRef mut, EMetaBlob *blob,
2101 CInode *in, CDir *parent,
2102 int flags, int linkunlink,
2103 snapid_t cfollows)
2104 {
2105 bool primary_dn = flags & PREDIRTY_PRIMARY;
2106 bool do_parent_mtime = flags & PREDIRTY_DIR;
2107 bool shallow = flags & PREDIRTY_SHALLOW;
2108
2109 ceph_assert(mds->mdlog->entry_is_open());
2110
2111 // make sure stamp is set
2112 if (mut->get_mds_stamp() == utime_t())
2113 mut->set_mds_stamp(ceph_clock_now());
2114
2115 if (in->is_base())
2116 return;
2117
2118 dout(10) << "predirty_journal_parents"
2119 << (do_parent_mtime ? " do_parent_mtime":"")
2120 << " linkunlink=" << linkunlink
2121 << (primary_dn ? " primary_dn":" remote_dn")
2122 << (shallow ? " SHALLOW":"")
2123 << " follows " << cfollows
2124 << " " << *in << dendl;
2125
2126 if (!parent) {
2127 ceph_assert(primary_dn);
2128 parent = in->get_projected_parent_dn()->get_dir();
2129 }
2130
2131 if (flags == 0 && linkunlink == 0) {
2132 dout(10) << " no flags/linkunlink, just adding dir context to blob(s)" << dendl;
2133 blob->add_dir_context(parent);
2134 return;
2135 }
2136
2137 // build list of inodes to wrlock, dirty, and update
2138 list<CInode*> lsi;
2139 CInode *cur = in;
2140 CDentry *parentdn = NULL;
2141 bool first = true;
2142 while (parent) {
2143 //assert(cur->is_auth() || !primary_dn); // this breaks the rename auth twiddle hack
2144 ceph_assert(parent->is_auth());
2145
2146 // opportunistically adjust parent dirfrag
2147 CInode *pin = parent->get_inode();
2148
2149 // inode -> dirfrag
2150 mut->auth_pin(parent);
2151
2152 auto pf = parent->project_fnode(mut);
2153 pf->version = parent->pre_dirty();
2154
2155 if (do_parent_mtime || linkunlink) {
2156 ceph_assert(mut->is_wrlocked(&pin->filelock));
2157 ceph_assert(mut->is_wrlocked(&pin->nestlock));
2158 ceph_assert(cfollows == CEPH_NOSNAP);
2159
2160 // update stale fragstat/rstat?
2161 parent->resync_accounted_fragstat();
2162 parent->resync_accounted_rstat();
2163
2164 if (do_parent_mtime) {
2165 pf->fragstat.mtime = mut->get_op_stamp();
2166 pf->fragstat.change_attr++;
2167 dout(10) << "predirty_journal_parents bumping fragstat change_attr to " << pf->fragstat.change_attr << " on " << parent << dendl;
2168 if (pf->fragstat.mtime > pf->rstat.rctime) {
2169 dout(10) << "predirty_journal_parents updating mtime on " << *parent << dendl;
2170 pf->rstat.rctime = pf->fragstat.mtime;
2171 } else {
2172 dout(10) << "predirty_journal_parents updating mtime UNDERWATER on " << *parent << dendl;
2173 }
2174 }
2175 if (linkunlink) {
2176 dout(10) << "predirty_journal_parents updating size on " << *parent << dendl;
2177 if (in->is_dir()) {
2178 pf->fragstat.nsubdirs += linkunlink;
2179 //pf->rstat.rsubdirs += linkunlink;
2180 } else {
2181 pf->fragstat.nfiles += linkunlink;
2182 //pf->rstat.rfiles += linkunlink;
2183 }
2184 }
2185 }
2186
2187 // rstat
2188 if (!primary_dn) {
2189 // don't update parent this pass
2190 } else if (!linkunlink && !(pin->nestlock.can_wrlock(-1) &&
2191 pin->versionlock.can_wrlock())) {
2192 dout(20) << " unwritable parent nestlock " << pin->nestlock
2193 << ", marking dirty rstat on " << *cur << dendl;
2194 cur->mark_dirty_rstat();
2195 } else {
2196 // if we don't hold a wrlock reference on this nestlock, take one,
2197 // because we are about to write into the dirfrag fnode and that needs
2198 // to commit before the lock can cycle.
2199 if (linkunlink) {
2200 ceph_assert(pin->nestlock.get_num_wrlocks() || mut->is_peer());
2201 }
2202
2203 if (!mut->is_wrlocked(&pin->nestlock)) {
2204 dout(10) << " taking wrlock on " << pin->nestlock << " on " << *pin << dendl;
2205 mds->locker->wrlock_force(&pin->nestlock, mut);
2206 }
2207
2208 // now we can project the inode rstat diff the dirfrag
2209 SnapRealm *prealm = pin->find_snaprealm();
2210
2211 snapid_t follows = cfollows;
2212 if (follows == CEPH_NOSNAP)
2213 follows = prealm->get_newest_seq();
2214
2215 snapid_t first = follows+1;
2216
2217 // first, if the frag is stale, bring it back in sync.
2218 parent->resync_accounted_rstat();
2219
2220 // now push inode rstats into frag
2221 project_rstat_inode_to_frag(mut, cur, parent, first, linkunlink, prealm);
2222 cur->clear_dirty_rstat();
2223 }
2224
2225 bool stop = false;
2226 if (!pin->is_auth() || (!mut->is_auth_pinned(pin) && !pin->can_auth_pin())) {
2227 dout(10) << "predirty_journal_parents !auth or ambig or can't authpin on " << *pin << dendl;
2228 stop = true;
2229 }
2230
2231 // delay propagating until later?
2232 if (!stop && !first &&
2233 g_conf()->mds_dirstat_min_interval > 0) {
2234 double since_last_prop = mut->get_mds_stamp() - pin->last_dirstat_prop;
2235 if (since_last_prop < g_conf()->mds_dirstat_min_interval) {
2236 dout(10) << "predirty_journal_parents last prop " << since_last_prop
2237 << " < " << g_conf()->mds_dirstat_min_interval
2238 << ", stopping" << dendl;
2239 stop = true;
2240 } else {
2241 dout(10) << "predirty_journal_parents last prop " << since_last_prop << " ago, continuing" << dendl;
2242 }
2243 }
2244
2245 // can cast only because i'm passing nowait=true in the sole user
2246 if (!stop &&
2247 !mut->is_wrlocked(&pin->nestlock) &&
2248 (!pin->versionlock.can_wrlock() || // make sure we can take versionlock, too
2249 !mds->locker->wrlock_try(&pin->nestlock, mut)
2250 )) { // ** do not initiate.. see above comment **
2251 dout(10) << "predirty_journal_parents can't wrlock one of " << pin->versionlock << " or " << pin->nestlock
2252 << " on " << *pin << dendl;
2253 stop = true;
2254 }
2255 if (stop) {
2256 dout(10) << "predirty_journal_parents stop. marking nestlock on " << *pin << dendl;
2257 mds->locker->mark_updated_scatterlock(&pin->nestlock);
2258 mut->ls->dirty_dirfrag_nest.push_back(&pin->item_dirty_dirfrag_nest);
2259 mut->add_updated_lock(&pin->nestlock);
2260 if (do_parent_mtime || linkunlink) {
2261 mds->locker->mark_updated_scatterlock(&pin->filelock);
2262 mut->ls->dirty_dirfrag_dir.push_back(&pin->item_dirty_dirfrag_dir);
2263 mut->add_updated_lock(&pin->filelock);
2264 }
2265 break;
2266 }
2267 if (!mut->is_wrlocked(&pin->versionlock))
2268 mds->locker->local_wrlock_grab(&pin->versionlock, mut);
2269
2270 ceph_assert(mut->is_wrlocked(&pin->nestlock) || mut->is_peer());
2271
2272 pin->last_dirstat_prop = mut->get_mds_stamp();
2273
2274 // dirfrag -> diri
2275 mut->auth_pin(pin);
2276 lsi.push_front(pin);
2277
2278 pin->pre_cow_old_inode(); // avoid cow mayhem!
2279
2280 auto pi = pin->project_inode(mut);
2281 pi.inode->version = pin->pre_dirty();
2282
2283 // dirstat
2284 if (do_parent_mtime || linkunlink) {
2285 dout(20) << "predirty_journal_parents add_delta " << pf->fragstat << dendl;
2286 dout(20) << "predirty_journal_parents - " << pf->accounted_fragstat << dendl;
2287 bool touched_mtime = false, touched_chattr = false;
2288 pi.inode->dirstat.add_delta(pf->fragstat, pf->accounted_fragstat, &touched_mtime, &touched_chattr);
2289 pf->accounted_fragstat = pf->fragstat;
2290 if (touched_mtime)
2291 pi.inode->mtime = pi.inode->ctime = pi.inode->dirstat.mtime;
2292 if (touched_chattr)
2293 pi.inode->change_attr++;
2294 dout(20) << "predirty_journal_parents gives " << pi.inode->dirstat << " on " << *pin << dendl;
2295
2296 if (parent->get_frag() == frag_t()) { // i.e., we are the only frag
2297 if (pi.inode->dirstat.size() < 0)
2298 ceph_assert(!"negative dirstat size" == g_conf()->mds_verify_scatter);
2299 if (pi.inode->dirstat.size() != pf->fragstat.size()) {
2300 mds->clog->error() << "unmatched fragstat size on single dirfrag "
2301 << parent->dirfrag() << ", inode has " << pi.inode->dirstat
2302 << ", dirfrag has " << pf->fragstat;
2303
2304 // trust the dirfrag for now
2305 pi.inode->dirstat = pf->fragstat;
2306
2307 ceph_assert(!"unmatched fragstat size" == g_conf()->mds_verify_scatter);
2308 }
2309 }
2310 }
2311
2312 // rstat
2313 dout(10) << "predirty_journal_parents frag->inode on " << *parent << dendl;
2314
2315 // first, if the frag is stale, bring it back in sync.
2316 parent->resync_accounted_rstat();
2317
2318 if (g_conf()->mds_snap_rstat) {
2319 for (auto &p : parent->dirty_old_rstat) {
2320 project_rstat_frag_to_inode(p.second.rstat, p.second.accounted_rstat, p.second.first,
2321 p.first, pin, true);
2322 }
2323 }
2324 parent->dirty_old_rstat.clear();
2325 project_rstat_frag_to_inode(pf->rstat, pf->accounted_rstat, parent->first, CEPH_NOSNAP, pin, true);//false);
2326
2327 pf->accounted_rstat = pf->rstat;
2328
2329 if (parent->get_frag() == frag_t()) { // i.e., we are the only frag
2330 if (pi.inode->rstat.rbytes != pf->rstat.rbytes) {
2331 mds->clog->error() << "unmatched rstat rbytes on single dirfrag "
2332 << parent->dirfrag() << ", inode has " << pi.inode->rstat
2333 << ", dirfrag has " << pf->rstat;
2334
2335 // trust the dirfrag for now
2336 pi.inode->rstat = pf->rstat;
2337
2338 ceph_assert(!"unmatched rstat rbytes" == g_conf()->mds_verify_scatter);
2339 }
2340 }
2341
2342 parent->check_rstats();
2343 broadcast_quota_to_client(pin);
2344 if (pin->is_base())
2345 break;
2346 // next parent!
2347 cur = pin;
2348 parentdn = pin->get_projected_parent_dn();
2349 ceph_assert(parentdn);
2350 parent = parentdn->get_dir();
2351 linkunlink = 0;
2352 do_parent_mtime = false;
2353 primary_dn = true;
2354 first = false;
2355 }
2356
2357 // now, stick it in the blob
2358 ceph_assert(parent);
2359 ceph_assert(parent->is_auth());
2360 blob->add_dir_context(parent);
2361 blob->add_dir(parent, true);
2362 for (const auto& in : lsi) {
2363 journal_dirty_inode(mut.get(), blob, in);
2364 }
2365
2366 }
2367
2368
2369
2370
2371
2372 // ===================================
2373 // peer requests
2374
2375
2376 /*
2377 * some handlers for leader requests with peers. we need to make
2378 * sure leader journal commits before we forget we leadered them and
2379 * remove them from the uncommitted_leaders map (used during recovery
2380 * to commit|abort peers).
2381 */
2382 struct C_MDC_CommittedLeader : public MDCacheLogContext {
2383 metareqid_t reqid;
2384 C_MDC_CommittedLeader(MDCache *s, metareqid_t r) : MDCacheLogContext(s), reqid(r) {}
2385 void finish(int r) override {
2386 mdcache->_logged_leader_commit(reqid);
2387 }
2388 };
2389
2390 void MDCache::log_leader_commit(metareqid_t reqid)
2391 {
2392 dout(10) << "log_leader_commit " << reqid << dendl;
2393 uncommitted_leaders[reqid].committing = true;
2394 mds->mdlog->start_submit_entry(new ECommitted(reqid),
2395 new C_MDC_CommittedLeader(this, reqid));
2396 }
2397
2398 void MDCache::_logged_leader_commit(metareqid_t reqid)
2399 {
2400 dout(10) << "_logged_leader_commit " << reqid << dendl;
2401 ceph_assert(uncommitted_leaders.count(reqid));
2402 uncommitted_leaders[reqid].ls->uncommitted_leaders.erase(reqid);
2403 mds->queue_waiters(uncommitted_leaders[reqid].waiters);
2404 uncommitted_leaders.erase(reqid);
2405 }
2406
2407 // while active...
2408
2409 void MDCache::committed_leader_peer(metareqid_t r, mds_rank_t from)
2410 {
2411 dout(10) << "committed_leader_peer mds." << from << " on " << r << dendl;
2412 ceph_assert(uncommitted_leaders.count(r));
2413 uncommitted_leaders[r].peers.erase(from);
2414 if (!uncommitted_leaders[r].recovering && uncommitted_leaders[r].peers.empty())
2415 log_leader_commit(r);
2416 }
2417
2418 void MDCache::logged_leader_update(metareqid_t reqid)
2419 {
2420 dout(10) << "logged_leader_update " << reqid << dendl;
2421 ceph_assert(uncommitted_leaders.count(reqid));
2422 uncommitted_leaders[reqid].safe = true;
2423 auto p = pending_leaders.find(reqid);
2424 if (p != pending_leaders.end()) {
2425 pending_leaders.erase(p);
2426 if (pending_leaders.empty())
2427 process_delayed_resolve();
2428 }
2429 }
2430
2431 /*
2432 * Leader may crash after receiving all peers' commit acks, but before journalling
2433 * the final commit. Peers may crash after journalling the peer commit, but before
2434 * sending commit ack to the leader. Commit leaders with no uncommitted peer when
2435 * resolve finishes.
2436 */
2437 void MDCache::finish_committed_leaders()
2438 {
2439 for (map<metareqid_t, uleader>::iterator p = uncommitted_leaders.begin();
2440 p != uncommitted_leaders.end();
2441 ++p) {
2442 p->second.recovering = false;
2443 if (!p->second.committing && p->second.peers.empty()) {
2444 dout(10) << "finish_committed_leaders " << p->first << dendl;
2445 log_leader_commit(p->first);
2446 }
2447 }
2448 }
2449
2450 /*
2451 * at end of resolve... we must journal a commit|abort for all peer
2452 * updates, before moving on.
2453 *
2454 * this is so that the leader can safely journal ECommitted on ops it
2455 * leaders when it reaches up:active (all other recovering nodes must
2456 * complete resolve before that happens).
2457 */
2458 struct C_MDC_PeerCommit : public MDCacheLogContext {
2459 mds_rank_t from;
2460 metareqid_t reqid;
2461 C_MDC_PeerCommit(MDCache *c, int f, metareqid_t r) : MDCacheLogContext(c), from(f), reqid(r) {}
2462 void finish(int r) override {
2463 mdcache->_logged_peer_commit(from, reqid);
2464 }
2465 };
2466
2467 void MDCache::_logged_peer_commit(mds_rank_t from, metareqid_t reqid)
2468 {
2469 dout(10) << "_logged_peer_commit from mds." << from << " " << reqid << dendl;
2470
2471 // send a message
2472 auto req = make_message<MMDSPeerRequest>(reqid, 0, MMDSPeerRequest::OP_COMMITTED);
2473 mds->send_message_mds(req, from);
2474 }
2475
2476
2477
2478
2479
2480
2481 // ====================================================================
2482 // import map, recovery
2483
2484 void MDCache::_move_subtree_map_bound(dirfrag_t df, dirfrag_t oldparent, dirfrag_t newparent,
2485 map<dirfrag_t,vector<dirfrag_t> >& subtrees)
2486 {
2487 if (subtrees.count(oldparent)) {
2488 vector<dirfrag_t>& v = subtrees[oldparent];
2489 dout(10) << " removing " << df << " from " << oldparent << " bounds " << v << dendl;
2490 for (vector<dirfrag_t>::iterator it = v.begin(); it != v.end(); ++it)
2491 if (*it == df) {
2492 v.erase(it);
2493 break;
2494 }
2495 }
2496 if (subtrees.count(newparent)) {
2497 vector<dirfrag_t>& v = subtrees[newparent];
2498 dout(10) << " adding " << df << " to " << newparent << " bounds " << v << dendl;
2499 v.push_back(df);
2500 }
2501 }
2502
2503 ESubtreeMap *MDCache::create_subtree_map()
2504 {
2505 dout(10) << "create_subtree_map " << num_subtrees() << " subtrees, "
2506 << num_subtrees_fullauth() << " fullauth"
2507 << dendl;
2508
2509 show_subtrees();
2510
2511 ESubtreeMap *le = new ESubtreeMap();
2512 mds->mdlog->_start_entry(le);
2513
2514 map<dirfrag_t, CDir*> dirs_to_add;
2515
2516 if (myin) {
2517 CDir* mydir = myin->get_dirfrag(frag_t());
2518 dirs_to_add[mydir->dirfrag()] = mydir;
2519 }
2520
2521 // include all auth subtrees, and their bounds.
2522 // and a spanning tree to tie it to the root.
2523 for (auto& [dir, bounds] : subtrees) {
2524 // journal subtree as "ours" if we are
2525 // me, -2
2526 // me, me
2527 // me, !me (may be importing and ambiguous!)
2528
2529 // so not
2530 // !me, *
2531 if (dir->get_dir_auth().first != mds->get_nodeid())
2532 continue;
2533
2534 if (migrator->is_ambiguous_import(dir->dirfrag()) ||
2535 my_ambiguous_imports.count(dir->dirfrag())) {
2536 dout(15) << " ambig subtree " << *dir << dendl;
2537 le->ambiguous_subtrees.insert(dir->dirfrag());
2538 } else {
2539 dout(15) << " auth subtree " << *dir << dendl;
2540 }
2541
2542 dirs_to_add[dir->dirfrag()] = dir;
2543 le->subtrees[dir->dirfrag()].clear();
2544
2545 // bounds
2546 size_t nbounds = bounds.size();
2547 if (nbounds > 3) {
2548 dout(15) << " subtree has " << nbounds << " bounds" << dendl;
2549 }
2550 for (auto& bound : bounds) {
2551 if (nbounds <= 3) {
2552 dout(15) << " subtree bound " << *bound << dendl;
2553 }
2554 dirs_to_add[bound->dirfrag()] = bound;
2555 le->subtrees[dir->dirfrag()].push_back(bound->dirfrag());
2556 }
2557 }
2558
2559 // apply projected renames
2560 for (const auto& [diri, renames] : projected_subtree_renames) {
2561 for (const auto& [olddir, newdir] : renames) {
2562 dout(15) << " adjusting for projected rename of " << *diri << " to " << *newdir << dendl;
2563
2564 auto&& dfls = diri->get_dirfrags();
2565 for (const auto& dir : dfls) {
2566 dout(15) << "dirfrag " << dir->dirfrag() << " " << *dir << dendl;
2567 CDir *oldparent = get_projected_subtree_root(olddir);
2568 dout(15) << " old parent " << oldparent->dirfrag() << " " << *oldparent << dendl;
2569 CDir *newparent = get_projected_subtree_root(newdir);
2570 dout(15) << " new parent " << newparent->dirfrag() << " " << *newparent << dendl;
2571
2572 if (oldparent == newparent) {
2573 dout(15) << "parent unchanged for " << dir->dirfrag() << " at "
2574 << oldparent->dirfrag() << dendl;
2575 continue;
2576 }
2577
2578 if (dir->is_subtree_root()) {
2579 if (le->subtrees.count(newparent->dirfrag()) &&
2580 oldparent->get_dir_auth() != newparent->get_dir_auth())
2581 dirs_to_add[dir->dirfrag()] = dir;
2582 // children are fine. change parent.
2583 _move_subtree_map_bound(dir->dirfrag(), oldparent->dirfrag(), newparent->dirfrag(),
2584 le->subtrees);
2585 } else {
2586 // mid-subtree.
2587
2588 if (oldparent->get_dir_auth() != newparent->get_dir_auth()) {
2589 dout(10) << " creating subtree for " << dir->dirfrag() << dendl;
2590 // if oldparent is auth, subtree is mine; include it.
2591 if (le->subtrees.count(oldparent->dirfrag())) {
2592 dirs_to_add[dir->dirfrag()] = dir;
2593 le->subtrees[dir->dirfrag()].clear();
2594 }
2595 // if newparent is auth, subtree is a new bound
2596 if (le->subtrees.count(newparent->dirfrag())) {
2597 dirs_to_add[dir->dirfrag()] = dir;
2598 le->subtrees[newparent->dirfrag()].push_back(dir->dirfrag()); // newparent is auth; new bound
2599 }
2600 newparent = dir;
2601 }
2602
2603 // see if any old bounds move to the new parent.
2604 for (auto& bound : subtrees.at(oldparent)) {
2605 if (dir->contains(bound->get_parent_dir()))
2606 _move_subtree_map_bound(bound->dirfrag(), oldparent->dirfrag(), newparent->dirfrag(),
2607 le->subtrees);
2608 }
2609 }
2610 }
2611 }
2612 }
2613
2614 // simplify the journaled map. our in memory map may have more
2615 // subtrees than needed due to migrations that are just getting
2616 // started or just completing. but on replay, the "live" map will
2617 // be simple and we can do a straight comparison.
2618 for (auto& [frag, bfrags] : le->subtrees) {
2619 if (le->ambiguous_subtrees.count(frag))
2620 continue;
2621 unsigned i = 0;
2622 while (i < bfrags.size()) {
2623 dirfrag_t b = bfrags[i];
2624 if (le->subtrees.count(b) &&
2625 le->ambiguous_subtrees.count(b) == 0) {
2626 auto& bb = le->subtrees.at(b);
2627 dout(10) << "simplify: " << frag << " swallowing " << b << " with bounds " << bb << dendl;
2628 for (auto& r : bb) {
2629 bfrags.push_back(r);
2630 }
2631 dirs_to_add.erase(b);
2632 le->subtrees.erase(b);
2633 bfrags.erase(bfrags.begin() + i);
2634 } else {
2635 ++i;
2636 }
2637 }
2638 }
2639
2640 for (auto &p : dirs_to_add) {
2641 CDir *dir = p.second;
2642 le->metablob.add_dir_context(dir, EMetaBlob::TO_ROOT);
2643 le->metablob.add_dir(dir, false);
2644 }
2645
2646 dout(15) << " subtrees " << le->subtrees << dendl;
2647 dout(15) << " ambiguous_subtrees " << le->ambiguous_subtrees << dendl;
2648
2649 //le->metablob.print(cout);
2650 le->expire_pos = mds->mdlog->journaler->get_expire_pos();
2651 return le;
2652 }
2653
2654 void MDCache::dump_resolve_status(Formatter *f) const
2655 {
2656 f->open_object_section("resolve_status");
2657 f->dump_stream("resolve_gather") << resolve_gather;
2658 f->dump_stream("resolve_ack_gather") << resolve_gather;
2659 f->close_section();
2660 }
2661
2662 void MDCache::resolve_start(MDSContext *resolve_done_)
2663 {
2664 dout(10) << "resolve_start" << dendl;
2665 ceph_assert(!resolve_done);
2666 resolve_done.reset(resolve_done_);
2667
2668 if (mds->mdsmap->get_root() != mds->get_nodeid()) {
2669 // if we don't have the root dir, adjust it to UNKNOWN. during
2670 // resolve we want mds0 to explicit claim the portion of it that
2671 // it owns, so that anything beyond its bounds get left as
2672 // unknown.
2673 CDir *rootdir = root->get_dirfrag(frag_t());
2674 if (rootdir)
2675 adjust_subtree_auth(rootdir, CDIR_AUTH_UNKNOWN);
2676 }
2677 resolve_gather = recovery_set;
2678
2679 resolve_snapclient_commits = mds->snapclient->get_journaled_tids();
2680 }
2681
2682 void MDCache::send_resolves()
2683 {
2684 send_peer_resolves();
2685
2686 if (!resolve_done) {
2687 // I'm survivor: refresh snap cache
2688 mds->snapclient->sync(
2689 new MDSInternalContextWrapper(mds,
2690 new LambdaContext([this](int r) {
2691 maybe_finish_peer_resolve();
2692 })
2693 )
2694 );
2695 dout(10) << "send_resolves waiting for snapclient cache to sync" << dendl;
2696 return;
2697 }
2698 if (!resolve_ack_gather.empty()) {
2699 dout(10) << "send_resolves still waiting for resolve ack from ("
2700 << resolve_ack_gather << ")" << dendl;
2701 return;
2702 }
2703 if (!resolve_need_rollback.empty()) {
2704 dout(10) << "send_resolves still waiting for rollback to commit on ("
2705 << resolve_need_rollback << ")" << dendl;
2706 return;
2707 }
2708
2709 send_subtree_resolves();
2710 }
2711
2712 void MDCache::send_peer_resolves()
2713 {
2714 dout(10) << "send_peer_resolves" << dendl;
2715
2716 map<mds_rank_t, ref_t<MMDSResolve>> resolves;
2717
2718 if (mds->is_resolve()) {
2719 for (map<metareqid_t, upeer>::iterator p = uncommitted_peers.begin();
2720 p != uncommitted_peers.end();
2721 ++p) {
2722 mds_rank_t leader = p->second.leader;
2723 auto &m = resolves[leader];
2724 if (!m) m = make_message<MMDSResolve>();
2725 m->add_peer_request(p->first, false);
2726 }
2727 } else {
2728 set<mds_rank_t> resolve_set;
2729 mds->mdsmap->get_mds_set(resolve_set, MDSMap::STATE_RESOLVE);
2730 for (ceph::unordered_map<metareqid_t, MDRequestRef>::iterator p = active_requests.begin();
2731 p != active_requests.end();
2732 ++p) {
2733 MDRequestRef& mdr = p->second;
2734 if (!mdr->is_peer())
2735 continue;
2736 if (!mdr->peer_did_prepare() && !mdr->committing) {
2737 continue;
2738 }
2739 mds_rank_t leader = mdr->peer_to_mds;
2740 if (resolve_set.count(leader) || is_ambiguous_peer_update(p->first, leader)) {
2741 dout(10) << " including uncommitted " << *mdr << dendl;
2742 if (!resolves.count(leader))
2743 resolves[leader] = make_message<MMDSResolve>();
2744 if (!mdr->committing &&
2745 mdr->has_more() && mdr->more()->is_inode_exporter) {
2746 // re-send cap exports
2747 CInode *in = mdr->more()->rename_inode;
2748 map<client_t, Capability::Export> cap_map;
2749 in->export_client_caps(cap_map);
2750 bufferlist bl;
2751 MMDSResolve::peer_inode_cap inode_caps(in->ino(), cap_map);
2752 encode(inode_caps, bl);
2753 resolves[leader]->add_peer_request(p->first, bl);
2754 } else {
2755 resolves[leader]->add_peer_request(p->first, mdr->committing);
2756 }
2757 }
2758 }
2759 }
2760
2761 for (auto &p : resolves) {
2762 dout(10) << "sending peer resolve to mds." << p.first << dendl;
2763 mds->send_message_mds(p.second, p.first);
2764 resolve_ack_gather.insert(p.first);
2765 }
2766 }
2767
2768 void MDCache::send_subtree_resolves()
2769 {
2770 dout(10) << "send_subtree_resolves" << dendl;
2771
2772 if (migrator->is_exporting() || migrator->is_importing()) {
2773 dout(7) << "send_subtree_resolves waiting, imports/exports still in progress" << dendl;
2774 migrator->show_importing();
2775 migrator->show_exporting();
2776 resolves_pending = true;
2777 return; // not now
2778 }
2779
2780 map<mds_rank_t, ref_t<MMDSResolve>> resolves;
2781 for (set<mds_rank_t>::iterator p = recovery_set.begin();
2782 p != recovery_set.end();
2783 ++p) {
2784 if (*p == mds->get_nodeid())
2785 continue;
2786 if (mds->is_resolve() || mds->mdsmap->is_resolve(*p))
2787 resolves[*p] = make_message<MMDSResolve>();
2788 }
2789
2790 map<dirfrag_t, vector<dirfrag_t> > my_subtrees;
2791 map<dirfrag_t, vector<dirfrag_t> > my_ambig_imports;
2792
2793 // known
2794 for (map<CDir*,set<CDir*> >::iterator p = subtrees.begin();
2795 p != subtrees.end();
2796 ++p) {
2797 CDir *dir = p->first;
2798
2799 // only our subtrees
2800 if (dir->authority().first != mds->get_nodeid())
2801 continue;
2802
2803 if (mds->is_resolve() && my_ambiguous_imports.count(dir->dirfrag()))
2804 continue; // we'll add it below
2805
2806 if (migrator->is_ambiguous_import(dir->dirfrag())) {
2807 // ambiguous (mid-import)
2808 set<CDir*> bounds;
2809 get_subtree_bounds(dir, bounds);
2810 vector<dirfrag_t> dfls;
2811 for (set<CDir*>::iterator q = bounds.begin(); q != bounds.end(); ++q)
2812 dfls.push_back((*q)->dirfrag());
2813
2814 my_ambig_imports[dir->dirfrag()] = dfls;
2815 dout(10) << " ambig " << dir->dirfrag() << " " << dfls << dendl;
2816 } else {
2817 // not ambiguous.
2818 for (auto &q : resolves) {
2819 resolves[q.first]->add_subtree(dir->dirfrag());
2820 }
2821 // bounds too
2822 vector<dirfrag_t> dfls;
2823 for (set<CDir*>::iterator q = subtrees[dir].begin();
2824 q != subtrees[dir].end();
2825 ++q) {
2826 CDir *bound = *q;
2827 dfls.push_back(bound->dirfrag());
2828 }
2829
2830 my_subtrees[dir->dirfrag()] = dfls;
2831 dout(10) << " claim " << dir->dirfrag() << " " << dfls << dendl;
2832 }
2833 }
2834
2835 // ambiguous
2836 for (map<dirfrag_t, vector<dirfrag_t> >::iterator p = my_ambiguous_imports.begin();
2837 p != my_ambiguous_imports.end();
2838 ++p) {
2839 my_ambig_imports[p->first] = p->second;
2840 dout(10) << " ambig " << p->first << " " << p->second << dendl;
2841 }
2842
2843 // simplify the claimed subtree.
2844 for (auto p = my_subtrees.begin(); p != my_subtrees.end(); ++p) {
2845 unsigned i = 0;
2846 while (i < p->second.size()) {
2847 dirfrag_t b = p->second[i];
2848 if (my_subtrees.count(b)) {
2849 vector<dirfrag_t>& bb = my_subtrees[b];
2850 dout(10) << " simplify: " << p->first << " swallowing " << b << " with bounds " << bb << dendl;
2851 for (vector<dirfrag_t>::iterator r = bb.begin(); r != bb.end(); ++r)
2852 p->second.push_back(*r);
2853 my_subtrees.erase(b);
2854 p->second.erase(p->second.begin() + i);
2855 } else {
2856 ++i;
2857 }
2858 }
2859 }
2860
2861 // send
2862 for (auto &p : resolves) {
2863 const ref_t<MMDSResolve> &m = p.second;
2864 if (mds->is_resolve()) {
2865 m->add_table_commits(TABLE_SNAP, resolve_snapclient_commits);
2866 } else {
2867 m->add_table_commits(TABLE_SNAP, mds->snapclient->get_journaled_tids());
2868 }
2869 m->subtrees = my_subtrees;
2870 m->ambiguous_imports = my_ambig_imports;
2871 dout(10) << "sending subtee resolve to mds." << p.first << dendl;
2872 mds->send_message_mds(m, p.first);
2873 }
2874 resolves_pending = false;
2875 }
2876
2877 void MDCache::maybe_finish_peer_resolve() {
2878 if (resolve_ack_gather.empty() && resolve_need_rollback.empty()) {
2879 // snap cache get synced or I'm in resolve state
2880 if (mds->snapclient->is_synced() || resolve_done)
2881 send_subtree_resolves();
2882 process_delayed_resolve();
2883 }
2884 }
2885
2886 void MDCache::handle_mds_failure(mds_rank_t who)
2887 {
2888 dout(7) << "handle_mds_failure mds." << who << dendl;
2889
2890 dout(1) << "handle_mds_failure mds." << who << " : recovery peers are " << recovery_set << dendl;
2891
2892 resolve_gather.insert(who);
2893 discard_delayed_resolve(who);
2894 ambiguous_peer_updates.erase(who);
2895
2896 rejoin_gather.insert(who);
2897 rejoin_sent.erase(who); // i need to send another
2898 rejoin_ack_sent.erase(who); // i need to send another
2899 rejoin_ack_gather.erase(who); // i'll need/get another.
2900
2901 dout(10) << " resolve_gather " << resolve_gather << dendl;
2902 dout(10) << " resolve_ack_gather " << resolve_ack_gather << dendl;
2903 dout(10) << " rejoin_sent " << rejoin_sent << dendl;
2904 dout(10) << " rejoin_gather " << rejoin_gather << dendl;
2905 dout(10) << " rejoin_ack_gather " << rejoin_ack_gather << dendl;
2906
2907
2908 // tell the migrator too.
2909 migrator->handle_mds_failure_or_stop(who);
2910
2911 // tell the balancer too.
2912 mds->balancer->handle_mds_failure(who);
2913
2914 // clean up any requests peer to/from this node
2915 list<MDRequestRef> finish;
2916 for (ceph::unordered_map<metareqid_t, MDRequestRef>::iterator p = active_requests.begin();
2917 p != active_requests.end();
2918 ++p) {
2919 MDRequestRef& mdr = p->second;
2920 // peer to the failed node?
2921 if (mdr->peer_to_mds == who) {
2922 if (mdr->peer_did_prepare()) {
2923 dout(10) << " peer request " << *mdr << " uncommitted, will resolve shortly" << dendl;
2924 if (is_ambiguous_peer_update(p->first, mdr->peer_to_mds))
2925 remove_ambiguous_peer_update(p->first, mdr->peer_to_mds);
2926
2927 if (!mdr->more()->waiting_on_peer.empty()) {
2928 ceph_assert(mdr->more()->srcdn_auth_mds == mds->get_nodeid());
2929 // will rollback, no need to wait
2930 mdr->reset_peer_request();
2931 mdr->more()->waiting_on_peer.clear();
2932 }
2933 } else if (!mdr->committing) {
2934 dout(10) << " peer request " << *mdr << " has no prepare, finishing up" << dendl;
2935 if (mdr->peer_request || mdr->peer_rolling_back())
2936 mdr->aborted = true;
2937 else
2938 finish.push_back(mdr);
2939 }
2940 }
2941
2942 if (mdr->is_peer() && mdr->peer_did_prepare()) {
2943 if (mdr->more()->waiting_on_peer.count(who)) {
2944 ceph_assert(mdr->more()->srcdn_auth_mds == mds->get_nodeid());
2945 dout(10) << " peer request " << *mdr << " no longer need rename notity ack from mds."
2946 << who << dendl;
2947 mdr->more()->waiting_on_peer.erase(who);
2948 if (mdr->more()->waiting_on_peer.empty() && mdr->peer_request)
2949 mds->queue_waiter(new C_MDS_RetryRequest(this, mdr));
2950 }
2951
2952 if (mdr->more()->srcdn_auth_mds == who &&
2953 mds->mdsmap->is_clientreplay_or_active_or_stopping(mdr->peer_to_mds)) {
2954 // rename srcdn's auth mds failed, resolve even I'm a survivor.
2955 dout(10) << " peer request " << *mdr << " uncommitted, will resolve shortly" << dendl;
2956 add_ambiguous_peer_update(p->first, mdr->peer_to_mds);
2957 }
2958 } else if (mdr->peer_request) {
2959 const cref_t<MMDSPeerRequest> &peer_req = mdr->peer_request;
2960 // FIXME: Peer rename request can arrive after we notice mds failure.
2961 // This can cause mds to crash (does not affect integrity of FS).
2962 if (peer_req->get_op() == MMDSPeerRequest::OP_RENAMEPREP &&
2963 peer_req->srcdn_auth == who)
2964 peer_req->mark_interrupted();
2965 }
2966
2967 // failed node is peer?
2968 if (mdr->is_leader() && !mdr->committing) {
2969 if (mdr->more()->srcdn_auth_mds == who) {
2970 dout(10) << " leader request " << *mdr << " waiting for rename srcdn's auth mds."
2971 << who << " to recover" << dendl;
2972 ceph_assert(mdr->more()->witnessed.count(who) == 0);
2973 if (mdr->more()->is_ambiguous_auth)
2974 mdr->clear_ambiguous_auth();
2975 // rename srcdn's auth mds failed, all witnesses will rollback
2976 mdr->more()->witnessed.clear();
2977 pending_leaders.erase(p->first);
2978 }
2979
2980 if (mdr->more()->witnessed.count(who)) {
2981 mds_rank_t srcdn_auth = mdr->more()->srcdn_auth_mds;
2982 if (srcdn_auth >= 0 && mdr->more()->waiting_on_peer.count(srcdn_auth)) {
2983 dout(10) << " leader request " << *mdr << " waiting for rename srcdn's auth mds."
2984 << mdr->more()->srcdn_auth_mds << " to reply" << dendl;
2985 // waiting for the peer (rename srcdn's auth mds), delay sending resolve ack
2986 // until either the request is committing or the peer also fails.
2987 ceph_assert(mdr->more()->waiting_on_peer.size() == 1);
2988 pending_leaders.insert(p->first);
2989 } else {
2990 dout(10) << " leader request " << *mdr << " no longer witnessed by peer mds."
2991 << who << " to recover" << dendl;
2992 if (srcdn_auth >= 0)
2993 ceph_assert(mdr->more()->witnessed.count(srcdn_auth) == 0);
2994
2995 // discard this peer's prepare (if any)
2996 mdr->more()->witnessed.erase(who);
2997 }
2998 }
2999
3000 if (mdr->more()->waiting_on_peer.count(who)) {
3001 dout(10) << " leader request " << *mdr << " waiting for peer mds." << who
3002 << " to recover" << dendl;
3003 // retry request when peer recovers
3004 mdr->more()->waiting_on_peer.erase(who);
3005 if (mdr->more()->waiting_on_peer.empty())
3006 mds->wait_for_active_peer(who, new C_MDS_RetryRequest(this, mdr));
3007 }
3008
3009 if (mdr->locking && mdr->locking_target_mds == who)
3010 mdr->finish_locking(mdr->locking);
3011 }
3012 }
3013
3014 for (map<metareqid_t, uleader>::iterator p = uncommitted_leaders.begin();
3015 p != uncommitted_leaders.end();
3016 ++p) {
3017 // The failed MDS may have already committed the peer update
3018 if (p->second.peers.count(who)) {
3019 p->second.recovering = true;
3020 p->second.peers.erase(who);
3021 }
3022 }
3023
3024 while (!finish.empty()) {
3025 dout(10) << "cleaning up peer request " << *finish.front() << dendl;
3026 request_finish(finish.front());
3027 finish.pop_front();
3028 }
3029
3030 kick_find_ino_peers(who);
3031 kick_open_ino_peers(who);
3032
3033 for (map<dirfrag_t,fragment_info_t>::iterator p = fragments.begin();
3034 p != fragments.end(); ) {
3035 dirfrag_t df = p->first;
3036 fragment_info_t& info = p->second;
3037
3038 if (info.is_fragmenting()) {
3039 if (info.notify_ack_waiting.erase(who) &&
3040 info.notify_ack_waiting.empty()) {
3041 fragment_drop_locks(info);
3042 fragment_maybe_finish(p++);
3043 } else {
3044 ++p;
3045 }
3046 continue;
3047 }
3048
3049 ++p;
3050 dout(10) << "cancelling fragment " << df << " bit " << info.bits << dendl;
3051 std::vector<CDir*> dirs;
3052 info.dirs.swap(dirs);
3053 fragments.erase(df);
3054 fragment_unmark_unfreeze_dirs(dirs);
3055 }
3056
3057 // MDCache::shutdown_export_strays() always exports strays to mds.0
3058 if (who == mds_rank_t(0))
3059 shutdown_exporting_strays.clear();
3060
3061 show_subtrees();
3062 }
3063
3064 /*
3065 * handle_mds_recovery - called on another node's transition
3066 * from resolve -> active.
3067 */
3068 void MDCache::handle_mds_recovery(mds_rank_t who)
3069 {
3070 dout(7) << "handle_mds_recovery mds." << who << dendl;
3071
3072 // exclude all discover waiters. kick_discovers() will do the job
3073 static const uint64_t i_mask = CInode::WAIT_ANY_MASK & ~CInode::WAIT_DIR;
3074 static const uint64_t d_mask = CDir::WAIT_ANY_MASK & ~CDir::WAIT_DENTRY;
3075
3076 MDSContext::vec waiters;
3077
3078 // wake up any waiters in their subtrees
3079 for (map<CDir*,set<CDir*> >::iterator p = subtrees.begin();
3080 p != subtrees.end();
3081 ++p) {
3082 CDir *dir = p->first;
3083
3084 if (dir->authority().first != who ||
3085 dir->authority().second == mds->get_nodeid())
3086 continue;
3087 ceph_assert(!dir->is_auth());
3088
3089 // wake any waiters
3090 std::queue<CDir*> q;
3091 q.push(dir);
3092
3093 while (!q.empty()) {
3094 CDir *d = q.front();
3095 q.pop();
3096 d->take_waiting(d_mask, waiters);
3097
3098 // inode waiters too
3099 for (auto &p : d->items) {
3100 CDentry *dn = p.second;
3101 CDentry::linkage_t *dnl = dn->get_linkage();
3102 if (dnl->is_primary()) {
3103 dnl->get_inode()->take_waiting(i_mask, waiters);
3104
3105 // recurse?
3106 auto&& ls = dnl->get_inode()->get_dirfrags();
3107 for (const auto& subdir : ls) {
3108 if (!subdir->is_subtree_root())
3109 q.push(subdir);
3110 }
3111 }
3112 }
3113 }
3114 }
3115
3116 kick_open_ino_peers(who);
3117 kick_find_ino_peers(who);
3118
3119 // queue them up.
3120 mds->queue_waiters(waiters);
3121 }
3122
3123 void MDCache::set_recovery_set(set<mds_rank_t>& s)
3124 {
3125 dout(7) << "set_recovery_set " << s << dendl;
3126 recovery_set = s;
3127 }
3128
3129
3130 /*
3131 * during resolve state, we share resolves to determine who
3132 * is authoritative for which trees. we expect to get an resolve
3133 * from _everyone_ in the recovery_set (the mds cluster at the time of
3134 * the first failure).
3135 *
3136 * This functions puts the passed message before returning
3137 */
3138 void MDCache::handle_resolve(const cref_t<MMDSResolve> &m)
3139 {
3140 dout(7) << "handle_resolve from " << m->get_source() << dendl;
3141 mds_rank_t from = mds_rank_t(m->get_source().num());
3142
3143 if (mds->get_state() < MDSMap::STATE_RESOLVE) {
3144 if (mds->get_want_state() == CEPH_MDS_STATE_RESOLVE) {
3145 mds->wait_for_resolve(new C_MDS_RetryMessage(mds, m));
3146 return;
3147 }
3148 // wait until we reach the resolve stage!
3149 return;
3150 }
3151
3152 discard_delayed_resolve(from);
3153
3154 // ambiguous peer requests?
3155 if (!m->peer_requests.empty()) {
3156 if (mds->is_clientreplay() || mds->is_active() || mds->is_stopping()) {
3157 for (auto p = m->peer_requests.begin(); p != m->peer_requests.end(); ++p) {
3158 if (uncommitted_leaders.count(p->first) && !uncommitted_leaders[p->first].safe) {
3159 ceph_assert(!p->second.committing);
3160 pending_leaders.insert(p->first);
3161 }
3162 }
3163
3164 if (!pending_leaders.empty()) {
3165 dout(10) << " still have pending updates, delay processing peer resolve" << dendl;
3166 delayed_resolve[from] = m;
3167 return;
3168 }
3169 }
3170
3171 auto ack = make_message<MMDSResolveAck>();
3172 for (const auto &p : m->peer_requests) {
3173 if (uncommitted_leaders.count(p.first)) { //mds->sessionmap.have_completed_request(p.first)) {
3174 // COMMIT
3175 if (p.second.committing) {
3176 // already committing, waiting for the OP_COMMITTED peer reply
3177 dout(10) << " already committing peer request " << p << " noop "<< dendl;
3178 } else {
3179 dout(10) << " ambiguous peer request " << p << " will COMMIT" << dendl;
3180 ack->add_commit(p.first);
3181 }
3182 uncommitted_leaders[p.first].peers.insert(from); // wait for peer OP_COMMITTED before we log ECommitted
3183
3184 if (p.second.inode_caps.length() > 0) {
3185 // peer wants to export caps (rename)
3186 ceph_assert(mds->is_resolve());
3187 MMDSResolve::peer_inode_cap inode_caps;
3188 auto q = p.second.inode_caps.cbegin();
3189 decode(inode_caps, q);
3190 inodeno_t ino = inode_caps.ino;
3191 map<client_t,Capability::Export> cap_exports = inode_caps.cap_exports;
3192 ceph_assert(get_inode(ino));
3193
3194 for (map<client_t,Capability::Export>::iterator q = cap_exports.begin();
3195 q != cap_exports.end();
3196 ++q) {
3197 Capability::Import& im = rejoin_imported_caps[from][ino][q->first];
3198 im.cap_id = ++last_cap_id; // assign a new cap ID
3199 im.issue_seq = 1;
3200 im.mseq = q->second.mseq;
3201
3202 Session *session = mds->sessionmap.get_session(entity_name_t::CLIENT(q->first.v));
3203 if (session)
3204 rejoin_client_map.emplace(q->first, session->info.inst);
3205 }
3206
3207 // will process these caps in rejoin stage
3208 rejoin_peer_exports[ino].first = from;
3209 rejoin_peer_exports[ino].second.swap(cap_exports);
3210
3211 // send information of imported caps back to peer
3212 encode(rejoin_imported_caps[from][ino], ack->commit[p.first]);
3213 }
3214 } else {
3215 // ABORT
3216 dout(10) << " ambiguous peer request " << p << " will ABORT" << dendl;
3217 ceph_assert(!p.second.committing);
3218 ack->add_abort(p.first);
3219 }
3220 }
3221 mds->send_message(ack, m->get_connection());
3222 return;
3223 }
3224
3225 if (!resolve_ack_gather.empty() || !resolve_need_rollback.empty()) {
3226 dout(10) << "delay processing subtree resolve" << dendl;
3227 delayed_resolve[from] = m;
3228 return;
3229 }
3230
3231 bool survivor = false;
3232 // am i a surviving ambiguous importer?
3233 if (mds->is_clientreplay() || mds->is_active() || mds->is_stopping()) {
3234 survivor = true;
3235 // check for any import success/failure (from this node)
3236 map<dirfrag_t, vector<dirfrag_t> >::iterator p = my_ambiguous_imports.begin();
3237 while (p != my_ambiguous_imports.end()) {
3238 map<dirfrag_t, vector<dirfrag_t> >::iterator next = p;
3239 ++next;
3240 CDir *dir = get_dirfrag(p->first);
3241 ceph_assert(dir);
3242 dout(10) << "checking ambiguous import " << *dir << dendl;
3243 if (migrator->is_importing(dir->dirfrag()) &&
3244 migrator->get_import_peer(dir->dirfrag()) == from) {
3245 ceph_assert(migrator->get_import_state(dir->dirfrag()) == Migrator::IMPORT_ACKING);
3246
3247 // check if sender claims the subtree
3248 bool claimed_by_sender = false;
3249 for (const auto &q : m->subtrees) {
3250 // an ambiguous import won't race with a refragmentation; it's appropriate to force here.
3251 CDir *base = get_force_dirfrag(q.first, false);
3252 if (!base || !base->contains(dir))
3253 continue; // base not dir or an ancestor of dir, clearly doesn't claim dir.
3254
3255 bool inside = true;
3256 set<CDir*> bounds;
3257 get_force_dirfrag_bound_set(q.second, bounds);
3258 for (set<CDir*>::iterator p = bounds.begin(); p != bounds.end(); ++p) {
3259 CDir *bound = *p;
3260 if (bound->contains(dir)) {
3261 inside = false; // nope, bound is dir or parent of dir, not inside.
3262 break;
3263 }
3264 }
3265 if (inside)
3266 claimed_by_sender = true;
3267 }
3268
3269 my_ambiguous_imports.erase(p); // no longer ambiguous.
3270 if (claimed_by_sender) {
3271 dout(7) << "ambiguous import failed on " << *dir << dendl;
3272 migrator->import_reverse(dir);
3273 } else {
3274 dout(7) << "ambiguous import succeeded on " << *dir << dendl;
3275 migrator->import_finish(dir, true);
3276 }
3277 }
3278 p = next;
3279 }
3280 }
3281
3282 // update my dir_auth values
3283 // need to do this on recoverying nodes _and_ bystanders (to resolve ambiguous
3284 // migrations between other nodes)
3285 for (const auto& p : m->subtrees) {
3286 dout(10) << "peer claims " << p.first << " bounds " << p.second << dendl;
3287 CDir *dir = get_force_dirfrag(p.first, !survivor);
3288 if (!dir)
3289 continue;
3290 adjust_bounded_subtree_auth(dir, p.second, from);
3291 try_subtree_merge(dir);
3292 }
3293
3294 show_subtrees();
3295
3296 // note ambiguous imports too
3297 for (const auto& p : m->ambiguous_imports) {
3298 dout(10) << "noting ambiguous import on " << p.first << " bounds " << p.second << dendl;
3299 other_ambiguous_imports[from][p.first] = p.second;
3300 }
3301
3302 // learn other mds' pendina snaptable commits. later when resolve finishes, we will reload
3303 // snaptable cache from snapserver. By this way, snaptable cache get synced among all mds
3304 for (const auto& p : m->table_clients) {
3305 dout(10) << " noting " << get_mdstable_name(p.type)
3306 << " pending_commits " << p.pending_commits << dendl;
3307 MDSTableClient *client = mds->get_table_client(p.type);
3308 for (const auto& q : p.pending_commits)
3309 client->notify_commit(q);
3310 }
3311
3312 // did i get them all?
3313 resolve_gather.erase(from);
3314
3315 maybe_resolve_finish();
3316 }
3317
3318 void MDCache::process_delayed_resolve()
3319 {
3320 dout(10) << "process_delayed_resolve" << dendl;
3321 map<mds_rank_t, cref_t<MMDSResolve>> tmp;
3322 tmp.swap(delayed_resolve);
3323 for (auto &p : tmp) {
3324 handle_resolve(p.second);
3325 }
3326 }
3327
3328 void MDCache::discard_delayed_resolve(mds_rank_t who)
3329 {
3330 delayed_resolve.erase(who);
3331 }
3332
3333 void MDCache::maybe_resolve_finish()
3334 {
3335 ceph_assert(resolve_ack_gather.empty());
3336 ceph_assert(resolve_need_rollback.empty());
3337
3338 if (!resolve_gather.empty()) {
3339 dout(10) << "maybe_resolve_finish still waiting for resolves ("
3340 << resolve_gather << ")" << dendl;
3341 return;
3342 }
3343
3344 dout(10) << "maybe_resolve_finish got all resolves+resolve_acks, done." << dendl;
3345 disambiguate_my_imports();
3346 finish_committed_leaders();
3347
3348 if (resolve_done) {
3349 ceph_assert(mds->is_resolve());
3350 trim_unlinked_inodes();
3351 recalc_auth_bits(false);
3352 resolve_done.release()->complete(0);
3353 } else {
3354 // I am survivor.
3355 maybe_send_pending_rejoins();
3356 }
3357 }
3358
3359 void MDCache::handle_resolve_ack(const cref_t<MMDSResolveAck> &ack)
3360 {
3361 dout(10) << "handle_resolve_ack " << *ack << " from " << ack->get_source() << dendl;
3362 mds_rank_t from = mds_rank_t(ack->get_source().num());
3363
3364 if (!resolve_ack_gather.count(from) ||
3365 mds->mdsmap->get_state(from) < MDSMap::STATE_RESOLVE) {
3366 return;
3367 }
3368
3369 if (ambiguous_peer_updates.count(from)) {
3370 ceph_assert(mds->mdsmap->is_clientreplay_or_active_or_stopping(from));
3371 ceph_assert(mds->is_clientreplay() || mds->is_active() || mds->is_stopping());
3372 }
3373
3374 for (const auto &p : ack->commit) {
3375 dout(10) << " commit on peer " << p.first << dendl;
3376
3377 if (ambiguous_peer_updates.count(from)) {
3378 remove_ambiguous_peer_update(p.first, from);
3379 continue;
3380 }
3381
3382 if (mds->is_resolve()) {
3383 // replay
3384 MDPeerUpdate *su = get_uncommitted_peer(p.first, from);
3385 ceph_assert(su);
3386
3387 // log commit
3388 mds->mdlog->start_submit_entry(new EPeerUpdate(mds->mdlog, "unknown", p.first, from,
3389 EPeerUpdate::OP_COMMIT, su->origop),
3390 new C_MDC_PeerCommit(this, from, p.first));
3391 mds->mdlog->flush();
3392
3393 finish_uncommitted_peer(p.first);
3394 } else {
3395 MDRequestRef mdr = request_get(p.first);
3396 // information about leader imported caps
3397 if (p.second.length() > 0)
3398 mdr->more()->inode_import.share(p.second);
3399
3400 ceph_assert(mdr->peer_request == 0); // shouldn't be doing anything!
3401 request_finish(mdr);
3402 }
3403 }
3404
3405 for (const auto &metareq : ack->abort) {
3406 dout(10) << " abort on peer " << metareq << dendl;
3407
3408 if (mds->is_resolve()) {
3409 MDPeerUpdate *su = get_uncommitted_peer(metareq, from);
3410 ceph_assert(su);
3411
3412 // perform rollback (and journal a rollback entry)
3413 // note: this will hold up the resolve a bit, until the rollback entries journal.
3414 MDRequestRef null_ref;
3415 switch (su->origop) {
3416 case EPeerUpdate::LINK:
3417 mds->server->do_link_rollback(su->rollback, from, null_ref);
3418 break;
3419 case EPeerUpdate::RENAME:
3420 mds->server->do_rename_rollback(su->rollback, from, null_ref);
3421 break;
3422 case EPeerUpdate::RMDIR:
3423 mds->server->do_rmdir_rollback(su->rollback, from, null_ref);
3424 break;
3425 default:
3426 ceph_abort();
3427 }
3428 } else {
3429 MDRequestRef mdr = request_get(metareq);
3430 mdr->aborted = true;
3431 if (mdr->peer_request) {
3432 if (mdr->peer_did_prepare()) // journaling peer prepare ?
3433 add_rollback(metareq, from);
3434 } else {
3435 request_finish(mdr);
3436 }
3437 }
3438 }
3439
3440 if (!ambiguous_peer_updates.count(from)) {
3441 resolve_ack_gather.erase(from);
3442 maybe_finish_peer_resolve();
3443 }
3444 }
3445
3446 void MDCache::add_uncommitted_peer(metareqid_t reqid, LogSegment *ls, mds_rank_t leader, MDPeerUpdate *su)
3447 {
3448 auto const &ret = uncommitted_peers.emplace(std::piecewise_construct,
3449 std::forward_as_tuple(reqid),
3450 std::forward_as_tuple());
3451 ceph_assert(ret.second);
3452 ls->uncommitted_peers.insert(reqid);
3453 upeer &u = ret.first->second;
3454 u.leader = leader;
3455 u.ls = ls;
3456 u.su = su;
3457 if (su == nullptr) {
3458 return;
3459 }
3460 for(set<CInode*>::iterator p = su->olddirs.begin(); p != su->olddirs.end(); ++p)
3461 uncommitted_peer_rename_olddir[*p]++;
3462 for(set<CInode*>::iterator p = su->unlinked.begin(); p != su->unlinked.end(); ++p)
3463 uncommitted_peer_unlink[*p]++;
3464 }
3465
3466 void MDCache::finish_uncommitted_peer(metareqid_t reqid, bool assert_exist)
3467 {
3468 auto it = uncommitted_peers.find(reqid);
3469 if (it == uncommitted_peers.end()) {
3470 ceph_assert(!assert_exist);
3471 return;
3472 }
3473 upeer &u = it->second;
3474 MDPeerUpdate* su = u.su;
3475
3476 if (!u.waiters.empty()) {
3477 mds->queue_waiters(u.waiters);
3478 }
3479 u.ls->uncommitted_peers.erase(reqid);
3480 uncommitted_peers.erase(it);
3481
3482 if (su == nullptr) {
3483 return;
3484 }
3485 // discard the non-auth subtree we renamed out of
3486 for(set<CInode*>::iterator p = su->olddirs.begin(); p != su->olddirs.end(); ++p) {
3487 CInode *diri = *p;
3488 map<CInode*, int>::iterator it = uncommitted_peer_rename_olddir.find(diri);
3489 ceph_assert(it != uncommitted_peer_rename_olddir.end());
3490 it->second--;
3491 if (it->second == 0) {
3492 uncommitted_peer_rename_olddir.erase(it);
3493 auto&& ls = diri->get_dirfrags();
3494 for (const auto& dir : ls) {
3495 CDir *root = get_subtree_root(dir);
3496 if (root->get_dir_auth() == CDIR_AUTH_UNDEF) {
3497 try_trim_non_auth_subtree(root);
3498 if (dir != root)
3499 break;
3500 }
3501 }
3502 } else
3503 ceph_assert(it->second > 0);
3504 }
3505 // removed the inodes that were unlinked by peer update
3506 for(set<CInode*>::iterator p = su->unlinked.begin(); p != su->unlinked.end(); ++p) {
3507 CInode *in = *p;
3508 map<CInode*, int>::iterator it = uncommitted_peer_unlink.find(in);
3509 ceph_assert(it != uncommitted_peer_unlink.end());
3510 it->second--;
3511 if (it->second == 0) {
3512 uncommitted_peer_unlink.erase(it);
3513 if (!in->get_projected_parent_dn())
3514 mds->mdcache->remove_inode_recursive(in);
3515 } else
3516 ceph_assert(it->second > 0);
3517 }
3518 delete su;
3519 }
3520
3521 MDPeerUpdate* MDCache::get_uncommitted_peer(metareqid_t reqid, mds_rank_t leader)
3522 {
3523
3524 MDPeerUpdate* su = nullptr;
3525 auto it = uncommitted_peers.find(reqid);
3526 if (it != uncommitted_peers.end() &&
3527 it->second.leader == leader) {
3528 su = it->second.su;
3529 }
3530 return su;
3531 }
3532
3533 void MDCache::finish_rollback(metareqid_t reqid, MDRequestRef& mdr) {
3534 auto p = resolve_need_rollback.find(reqid);
3535 ceph_assert(p != resolve_need_rollback.end());
3536 if (mds->is_resolve()) {
3537 finish_uncommitted_peer(reqid, false);
3538 } else if (mdr) {
3539 finish_uncommitted_peer(mdr->reqid, mdr->more()->peer_update_journaled);
3540 }
3541 resolve_need_rollback.erase(p);
3542 maybe_finish_peer_resolve();
3543 }
3544
3545 void MDCache::disambiguate_other_imports()
3546 {
3547 dout(10) << "disambiguate_other_imports" << dendl;
3548
3549 bool recovering = !(mds->is_clientreplay() || mds->is_active() || mds->is_stopping());
3550 // other nodes' ambiguous imports
3551 for (map<mds_rank_t, map<dirfrag_t, vector<dirfrag_t> > >::iterator p = other_ambiguous_imports.begin();
3552 p != other_ambiguous_imports.end();
3553 ++p) {
3554 mds_rank_t who = p->first;
3555 dout(10) << "ambiguous imports for mds." << who << dendl;
3556
3557 for (map<dirfrag_t, vector<dirfrag_t> >::iterator q = p->second.begin();
3558 q != p->second.end();
3559 ++q) {
3560 dout(10) << " ambiguous import " << q->first << " bounds " << q->second << dendl;
3561 // an ambiguous import will not race with a refragmentation; it's appropriate to force here.
3562 CDir *dir = get_force_dirfrag(q->first, recovering);
3563 if (!dir) continue;
3564
3565 if (dir->is_ambiguous_auth() || // works for me_ambig or if i am a surviving bystander
3566 dir->authority() == CDIR_AUTH_UNDEF) { // resolving
3567 dout(10) << " mds." << who << " did import " << *dir << dendl;
3568 adjust_bounded_subtree_auth(dir, q->second, who);
3569 try_subtree_merge(dir);
3570 } else {
3571 dout(10) << " mds." << who << " did not import " << *dir << dendl;
3572 }
3573 }
3574 }
3575 other_ambiguous_imports.clear();
3576 }
3577
3578 void MDCache::disambiguate_my_imports()
3579 {
3580 dout(10) << "disambiguate_my_imports" << dendl;
3581
3582 if (!mds->is_resolve()) {
3583 ceph_assert(my_ambiguous_imports.empty());
3584 return;
3585 }
3586
3587 disambiguate_other_imports();
3588
3589 // my ambiguous imports
3590 mds_authority_t me_ambig(mds->get_nodeid(), mds->get_nodeid());
3591 while (!my_ambiguous_imports.empty()) {
3592 map<dirfrag_t, vector<dirfrag_t> >::iterator q = my_ambiguous_imports.begin();
3593
3594 CDir *dir = get_dirfrag(q->first);
3595 ceph_assert(dir);
3596
3597 if (dir->authority() != me_ambig) {
3598 dout(10) << "ambiguous import auth known, must not be me " << *dir << dendl;
3599 cancel_ambiguous_import(dir);
3600
3601 mds->mdlog->start_submit_entry(new EImportFinish(dir, false));
3602
3603 // subtree may have been swallowed by another node claiming dir
3604 // as their own.
3605 CDir *root = get_subtree_root(dir);
3606 if (root != dir)
3607 dout(10) << " subtree root is " << *root << dendl;
3608 ceph_assert(root->dir_auth.first != mds->get_nodeid()); // no us!
3609 try_trim_non_auth_subtree(root);
3610 } else {
3611 dout(10) << "ambiguous import auth unclaimed, must be me " << *dir << dendl;
3612 finish_ambiguous_import(q->first);
3613 mds->mdlog->start_submit_entry(new EImportFinish(dir, true));
3614 }
3615 }
3616 ceph_assert(my_ambiguous_imports.empty());
3617 mds->mdlog->flush();
3618
3619 // verify all my subtrees are unambiguous!
3620 for (map<CDir*,set<CDir*> >::iterator p = subtrees.begin();
3621 p != subtrees.end();
3622 ++p) {
3623 CDir *dir = p->first;
3624 if (dir->is_ambiguous_dir_auth()) {
3625 dout(0) << "disambiguate_imports uh oh, dir_auth is still ambiguous for " << *dir << dendl;
3626 }
3627 ceph_assert(!dir->is_ambiguous_dir_auth());
3628 }
3629
3630 show_subtrees();
3631 }
3632
3633
3634 void MDCache::add_ambiguous_import(dirfrag_t base, const vector<dirfrag_t>& bounds)
3635 {
3636 ceph_assert(my_ambiguous_imports.count(base) == 0);
3637 my_ambiguous_imports[base] = bounds;
3638 }
3639
3640
3641 void MDCache::add_ambiguous_import(CDir *base, const set<CDir*>& bounds)
3642 {
3643 // make a list
3644 vector<dirfrag_t> binos;
3645 for (set<CDir*>::iterator p = bounds.begin();
3646 p != bounds.end();
3647 ++p)
3648 binos.push_back((*p)->dirfrag());
3649
3650 // note: this can get called twice if the exporter fails during recovery
3651 if (my_ambiguous_imports.count(base->dirfrag()))
3652 my_ambiguous_imports.erase(base->dirfrag());
3653
3654 add_ambiguous_import(base->dirfrag(), binos);
3655 }
3656
3657 void MDCache::cancel_ambiguous_import(CDir *dir)
3658 {
3659 dirfrag_t df = dir->dirfrag();
3660 ceph_assert(my_ambiguous_imports.count(df));
3661 dout(10) << "cancel_ambiguous_import " << df
3662 << " bounds " << my_ambiguous_imports[df]
3663 << " " << *dir
3664 << dendl;
3665 my_ambiguous_imports.erase(df);
3666 }
3667
3668 void MDCache::finish_ambiguous_import(dirfrag_t df)
3669 {
3670 ceph_assert(my_ambiguous_imports.count(df));
3671 vector<dirfrag_t> bounds;
3672 bounds.swap(my_ambiguous_imports[df]);
3673 my_ambiguous_imports.erase(df);
3674
3675 dout(10) << "finish_ambiguous_import " << df
3676 << " bounds " << bounds
3677 << dendl;
3678 CDir *dir = get_dirfrag(df);
3679 ceph_assert(dir);
3680
3681 // adjust dir_auth, import maps
3682 adjust_bounded_subtree_auth(dir, bounds, mds->get_nodeid());
3683 try_subtree_merge(dir);
3684 }
3685
3686 void MDCache::remove_inode_recursive(CInode *in)
3687 {
3688 dout(10) << "remove_inode_recursive " << *in << dendl;
3689 auto&& ls = in->get_dirfrags();
3690 for (const auto& subdir : ls) {
3691 dout(10) << " removing dirfrag " << *subdir << dendl;
3692 auto it = subdir->items.begin();
3693 while (it != subdir->items.end()) {
3694 CDentry *dn = it->second;
3695 ++it;
3696 CDentry::linkage_t *dnl = dn->get_linkage();
3697 if (dnl->is_primary()) {
3698 CInode *tin = dnl->get_inode();
3699 subdir->unlink_inode(dn, false);
3700 remove_inode_recursive(tin);
3701 }
3702 subdir->remove_dentry(dn);
3703 }
3704
3705 if (subdir->is_subtree_root())
3706 remove_subtree(subdir);
3707 in->close_dirfrag(subdir->dirfrag().frag);
3708 }
3709 remove_inode(in);
3710 }
3711
3712 bool MDCache::expire_recursive(CInode *in, expiremap &expiremap)
3713 {
3714 ceph_assert(!in->is_auth());
3715
3716 dout(10) << __func__ << ":" << *in << dendl;
3717
3718 // Recurse into any dirfrags beneath this inode
3719 auto&& ls = in->get_dirfrags();
3720 for (const auto& subdir : ls) {
3721 if (!in->is_mdsdir() && subdir->is_subtree_root()) {
3722 dout(10) << __func__ << ": stray still has subtree " << *in << dendl;
3723 return true;
3724 }
3725
3726 for (auto it = subdir->items.begin(); it != subdir->items.end();) {
3727 CDentry *dn = it->second;
3728 it++;
3729 CDentry::linkage_t *dnl = dn->get_linkage();
3730 if (dnl->is_primary()) {
3731 CInode *tin = dnl->get_inode();
3732
3733 /* Remote strays with linkage (i.e. hardlinks) should not be
3734 * expired, because they may be the target of
3735 * a rename() as the owning MDS shuts down */
3736 if (!tin->is_stray() && tin->get_inode()->nlink) {
3737 dout(10) << __func__ << ": stray still has linkage " << *tin << dendl;
3738 return true;
3739 }
3740
3741 const bool abort = expire_recursive(tin, expiremap);
3742 if (abort) {
3743 return true;
3744 }
3745 }
3746 if (dn->lru_is_expireable()) {
3747 trim_dentry(dn, expiremap);
3748 } else {
3749 dout(10) << __func__ << ": stray dn is not expireable " << *dn << dendl;
3750 return true;
3751 }
3752 }
3753 }
3754
3755 return false;
3756 }
3757
3758 void MDCache::trim_unlinked_inodes()
3759 {
3760 dout(7) << "trim_unlinked_inodes" << dendl;
3761 int count = 0;
3762 vector<CInode*> q;
3763 for (auto &p : inode_map) {
3764 CInode *in = p.second;
3765 if (in->get_parent_dn() == NULL && !in->is_base()) {
3766 dout(7) << " will trim from " << *in << dendl;
3767 q.push_back(in);
3768 }
3769
3770 if (!(++count % mds->heartbeat_reset_grace()))
3771 mds->heartbeat_reset();
3772 }
3773 for (auto& in : q) {
3774 remove_inode_recursive(in);
3775
3776 if (!(++count % mds->heartbeat_reset_grace()))
3777 mds->heartbeat_reset();
3778 }
3779 }
3780
3781 /** recalc_auth_bits()
3782 * once subtree auth is disambiguated, we need to adjust all the
3783 * auth and dirty bits in our cache before moving on.
3784 */
3785 void MDCache::recalc_auth_bits(bool replay)
3786 {
3787 dout(7) << "recalc_auth_bits " << (replay ? "(replay)" : "") << dendl;
3788
3789 if (root) {
3790 root->inode_auth.first = mds->mdsmap->get_root();
3791 bool auth = mds->get_nodeid() == root->inode_auth.first;
3792 if (auth) {
3793 root->state_set(CInode::STATE_AUTH);
3794 } else {
3795 root->state_clear(CInode::STATE_AUTH);
3796 if (!replay)
3797 root->state_set(CInode::STATE_REJOINING);
3798 }
3799 }
3800
3801 set<CInode*> subtree_inodes;
3802 for (map<CDir*,set<CDir*> >::iterator p = subtrees.begin();
3803 p != subtrees.end();
3804 ++p) {
3805 if (p->first->dir_auth.first == mds->get_nodeid())
3806 subtree_inodes.insert(p->first->inode);
3807 }
3808
3809 for (map<CDir*,set<CDir*> >::iterator p = subtrees.begin();
3810 p != subtrees.end();
3811 ++p) {
3812 if (p->first->inode->is_mdsdir()) {
3813 CInode *in = p->first->inode;
3814 bool auth = in->ino() == MDS_INO_MDSDIR(mds->get_nodeid());
3815 if (auth) {
3816 in->state_set(CInode::STATE_AUTH);
3817 } else {
3818 in->state_clear(CInode::STATE_AUTH);
3819 if (!replay)
3820 in->state_set(CInode::STATE_REJOINING);
3821 }
3822 }
3823
3824 std::queue<CDir*> dfq; // dirfrag queue
3825 dfq.push(p->first);
3826
3827 bool auth = p->first->authority().first == mds->get_nodeid();
3828 dout(10) << " subtree auth=" << auth << " for " << *p->first << dendl;
3829
3830 while (!dfq.empty()) {
3831 CDir *dir = dfq.front();
3832 dfq.pop();
3833
3834 // dir
3835 if (auth) {
3836 dir->state_set(CDir::STATE_AUTH);
3837 } else {
3838 dir->state_clear(CDir::STATE_AUTH);
3839 if (!replay) {
3840 // close empty non-auth dirfrag
3841 if (!dir->is_subtree_root() && dir->get_num_any() == 0) {
3842 dir->inode->close_dirfrag(dir->get_frag());
3843 continue;
3844 }
3845 dir->state_set(CDir::STATE_REJOINING);
3846 dir->state_clear(CDir::STATE_COMPLETE);
3847 if (dir->is_dirty())
3848 dir->mark_clean();
3849 }
3850 }
3851
3852 // dentries in this dir
3853 for (auto &p : dir->items) {
3854 // dn
3855 CDentry *dn = p.second;
3856 CDentry::linkage_t *dnl = dn->get_linkage();
3857 if (auth) {
3858 dn->mark_auth();
3859 } else {
3860 dn->clear_auth();
3861 if (!replay) {
3862 dn->state_set(CDentry::STATE_REJOINING);
3863 if (dn->is_dirty())
3864 dn->mark_clean();
3865 }
3866 }
3867
3868 if (dnl->is_primary()) {
3869 // inode
3870 CInode *in = dnl->get_inode();
3871 if (auth) {
3872 in->state_set(CInode::STATE_AUTH);
3873 } else {
3874 in->state_clear(CInode::STATE_AUTH);
3875 if (!replay) {
3876 in->state_set(CInode::STATE_REJOINING);
3877 if (in->is_dirty())
3878 in->mark_clean();
3879 if (in->is_dirty_parent())
3880 in->clear_dirty_parent();
3881 // avoid touching scatterlocks for our subtree roots!
3882 if (subtree_inodes.count(in) == 0)
3883 in->clear_scatter_dirty();
3884 }
3885 }
3886 // recurse?
3887 if (in->is_dir()) {
3888 auto&& dfv = in->get_nested_dirfrags();
3889 for (const auto& dir : dfv) {
3890 dfq.push(dir);
3891 }
3892 }
3893 }
3894 }
3895 }
3896 }
3897
3898 show_subtrees();
3899 show_cache();
3900 }
3901
3902
3903
3904 // ===========================================================================
3905 // REJOIN
3906
3907 /*
3908 * notes on scatterlock recovery:
3909 *
3910 * - recovering inode replica sends scatterlock data for any subtree
3911 * roots (the only ones that are possibly dirty).
3912 *
3913 * - surviving auth incorporates any provided scatterlock data. any
3914 * pending gathers are then finished, as with the other lock types.
3915 *
3916 * that takes care of surviving auth + (recovering replica)*.
3917 *
3918 * - surviving replica sends strong_inode, which includes current
3919 * scatterlock state, AND any dirty scatterlock data. this
3920 * provides the recovering auth with everything it might need.
3921 *
3922 * - recovering auth must pick initial scatterlock state based on
3923 * (weak|strong) rejoins.
3924 * - always assimilate scatterlock data (it can't hurt)
3925 * - any surviving replica in SCATTER state -> SCATTER. otherwise, SYNC.
3926 * - include base inode in ack for all inodes that saw scatterlock content
3927 *
3928 * also, for scatter gather,
3929 *
3930 * - auth increments {frag,r}stat.version on completion of any gather.
3931 *
3932 * - auth incorporates changes in a gather _only_ if the version
3933 * matches.
3934 *
3935 * - replica discards changes any time the scatterlock syncs, and
3936 * after recovery.
3937 */
3938
3939 void MDCache::dump_rejoin_status(Formatter *f) const
3940 {
3941 f->open_object_section("rejoin_status");
3942 f->dump_stream("rejoin_gather") << rejoin_gather;
3943 f->dump_stream("rejoin_ack_gather") << rejoin_ack_gather;
3944 f->dump_unsigned("num_opening_inodes", cap_imports_num_opening);
3945 f->close_section();
3946 }
3947
3948 void MDCache::rejoin_start(MDSContext *rejoin_done_)
3949 {
3950 dout(10) << "rejoin_start" << dendl;
3951 ceph_assert(!rejoin_done);
3952 rejoin_done.reset(rejoin_done_);
3953
3954 rejoin_gather = recovery_set;
3955 // need finish opening cap inodes before sending cache rejoins
3956 rejoin_gather.insert(mds->get_nodeid());
3957 process_imported_caps();
3958 }
3959
3960 /*
3961 * rejoin phase!
3962 *
3963 * this initiates rejoin. it should be called before we get any
3964 * rejoin or rejoin_ack messages (or else mdsmap distribution is broken).
3965 *
3966 * we start out by sending rejoins to everyone in the recovery set.
3967 *
3968 * if we are rejoin, send for all regions in our cache.
3969 * if we are active|stopping, send only to nodes that are rejoining.
3970 */
3971 void MDCache::rejoin_send_rejoins()
3972 {
3973 dout(10) << "rejoin_send_rejoins with recovery_set " << recovery_set << dendl;
3974
3975 if (rejoin_gather.count(mds->get_nodeid())) {
3976 dout(7) << "rejoin_send_rejoins still processing imported caps, delaying" << dendl;
3977 rejoins_pending = true;
3978 return;
3979 }
3980 if (!resolve_gather.empty()) {
3981 dout(7) << "rejoin_send_rejoins still waiting for resolves ("
3982 << resolve_gather << ")" << dendl;
3983 rejoins_pending = true;
3984 return;
3985 }
3986
3987 ceph_assert(!migrator->is_importing());
3988 ceph_assert(!migrator->is_exporting());
3989
3990 if (!mds->is_rejoin()) {
3991 disambiguate_other_imports();
3992 }
3993
3994 map<mds_rank_t, ref_t<MMDSCacheRejoin>> rejoins;
3995
3996
3997 // if i am rejoining, send a rejoin to everyone.
3998 // otherwise, just send to others who are rejoining.
3999 for (const auto& rank : recovery_set) {
4000 if (rank == mds->get_nodeid()) continue; // nothing to myself!
4001 if (rejoin_sent.count(rank)) continue; // already sent a rejoin to this node!
4002 if (mds->is_rejoin())
4003 rejoins[rank] = make_message<MMDSCacheRejoin>(MMDSCacheRejoin::OP_WEAK);
4004 else if (mds->mdsmap->is_rejoin(rank))
4005 rejoins[rank] = make_message<MMDSCacheRejoin>(MMDSCacheRejoin::OP_STRONG);
4006 }
4007
4008 if (mds->is_rejoin()) {
4009 map<client_t, pair<Session*, set<mds_rank_t> > > client_exports;
4010 for (auto& p : cap_exports) {
4011 mds_rank_t target = p.second.first;
4012 if (rejoins.count(target) == 0)
4013 continue;
4014 for (auto q = p.second.second.begin(); q != p.second.second.end(); ) {
4015 Session *session = nullptr;
4016 auto it = client_exports.find(q->first);
4017 if (it != client_exports.end()) {
4018 session = it->second.first;
4019 if (session)
4020 it->second.second.insert(target);
4021 } else {
4022 session = mds->sessionmap.get_session(entity_name_t::CLIENT(q->first.v));
4023 auto& r = client_exports[q->first];
4024 r.first = session;
4025 if (session)
4026 r.second.insert(target);
4027 }
4028 if (session) {
4029 ++q;
4030 } else {
4031 // remove reconnect with no session
4032 p.second.second.erase(q++);
4033 }
4034 }
4035 rejoins[target]->cap_exports[p.first] = p.second.second;
4036 }
4037 for (auto& p : client_exports) {
4038 Session *session = p.second.first;
4039 for (auto& q : p.second.second) {
4040 auto rejoin = rejoins[q];
4041 rejoin->client_map[p.first] = session->info.inst;
4042 rejoin->client_metadata_map[p.first] = session->info.client_metadata;
4043 }
4044 }
4045 }
4046
4047
4048 // check all subtrees
4049 for (map<CDir*, set<CDir*> >::iterator p = subtrees.begin();
4050 p != subtrees.end();
4051 ++p) {
4052 CDir *dir = p->first;
4053 ceph_assert(dir->is_subtree_root());
4054 if (dir->is_ambiguous_dir_auth()) {
4055 // exporter is recovering, importer is survivor.
4056 ceph_assert(rejoins.count(dir->authority().first));
4057 ceph_assert(!rejoins.count(dir->authority().second));
4058 continue;
4059 }
4060
4061 // my subtree?
4062 if (dir->is_auth())
4063 continue; // skip my own regions!
4064
4065 mds_rank_t auth = dir->get_dir_auth().first;
4066 ceph_assert(auth >= 0);
4067 if (rejoins.count(auth) == 0)
4068 continue; // don't care about this node's subtrees
4069
4070 rejoin_walk(dir, rejoins[auth]);
4071 }
4072
4073 // rejoin root inodes, too
4074 for (auto &p : rejoins) {
4075 if (mds->is_rejoin()) {
4076 // weak
4077 if (p.first == 0 && root) {
4078 p.second->add_weak_inode(root->vino());
4079 if (root->is_dirty_scattered()) {
4080 dout(10) << " sending scatterlock state on root " << *root << dendl;
4081 p.second->add_scatterlock_state(root);
4082 }
4083 }
4084 if (CInode *in = get_inode(MDS_INO_MDSDIR(p.first))) {
4085 if (in)
4086 p.second->add_weak_inode(in->vino());
4087 }
4088 } else {
4089 // strong
4090 if (p.first == 0 && root) {
4091 p.second->add_strong_inode(root->vino(),
4092 root->get_replica_nonce(),
4093 root->get_caps_wanted(),
4094 root->filelock.get_state(),
4095 root->nestlock.get_state(),
4096 root->dirfragtreelock.get_state());
4097 root->state_set(CInode::STATE_REJOINING);
4098 if (root->is_dirty_scattered()) {
4099 dout(10) << " sending scatterlock state on root " << *root << dendl;
4100 p.second->add_scatterlock_state(root);
4101 }
4102 }
4103
4104 if (CInode *in = get_inode(MDS_INO_MDSDIR(p.first))) {
4105 p.second->add_strong_inode(in->vino(),
4106 in->get_replica_nonce(),
4107 in->get_caps_wanted(),
4108 in->filelock.get_state(),
4109 in->nestlock.get_state(),
4110 in->dirfragtreelock.get_state());
4111 in->state_set(CInode::STATE_REJOINING);
4112 }
4113 }
4114 }
4115
4116 if (!mds->is_rejoin()) {
4117 // i am survivor. send strong rejoin.
4118 // note request remote_auth_pins, xlocks
4119 for (ceph::unordered_map<metareqid_t, MDRequestRef>::iterator p = active_requests.begin();
4120 p != active_requests.end();
4121 ++p) {
4122 MDRequestRef& mdr = p->second;
4123 if (mdr->is_peer())
4124 continue;
4125 // auth pins
4126 for (const auto& q : mdr->object_states) {
4127 if (q.second.remote_auth_pinned == MDS_RANK_NONE)
4128 continue;
4129 if (!q.first->is_auth()) {
4130 mds_rank_t target = q.second.remote_auth_pinned;
4131 ceph_assert(target == q.first->authority().first);
4132 if (rejoins.count(target) == 0) continue;
4133 const auto& rejoin = rejoins[target];
4134
4135 dout(15) << " " << *mdr << " authpin on " << *q.first << dendl;
4136 MDSCacheObjectInfo i;
4137 q.first->set_object_info(i);
4138 if (i.ino)
4139 rejoin->add_inode_authpin(vinodeno_t(i.ino, i.snapid), mdr->reqid, mdr->attempt);
4140 else
4141 rejoin->add_dentry_authpin(i.dirfrag, i.dname, i.snapid, mdr->reqid, mdr->attempt);
4142
4143 if (mdr->has_more() && mdr->more()->is_remote_frozen_authpin &&
4144 mdr->more()->rename_inode == q.first)
4145 rejoin->add_inode_frozen_authpin(vinodeno_t(i.ino, i.snapid),
4146 mdr->reqid, mdr->attempt);
4147 }
4148 }
4149 // xlocks
4150 for (const auto& q : mdr->locks) {
4151 auto lock = q.lock;
4152 auto obj = lock->get_parent();
4153 if (q.is_xlock() && !obj->is_auth()) {
4154 mds_rank_t who = obj->authority().first;
4155 if (rejoins.count(who) == 0) continue;
4156 const auto& rejoin = rejoins[who];
4157
4158 dout(15) << " " << *mdr << " xlock on " << *lock << " " << *obj << dendl;
4159 MDSCacheObjectInfo i;
4160 obj->set_object_info(i);
4161 if (i.ino)
4162 rejoin->add_inode_xlock(vinodeno_t(i.ino, i.snapid), lock->get_type(),
4163 mdr->reqid, mdr->attempt);
4164 else
4165 rejoin->add_dentry_xlock(i.dirfrag, i.dname, i.snapid,
4166 mdr->reqid, mdr->attempt);
4167 } else if (q.is_remote_wrlock()) {
4168 mds_rank_t who = q.wrlock_target;
4169 if (rejoins.count(who) == 0) continue;
4170 const auto& rejoin = rejoins[who];
4171
4172 dout(15) << " " << *mdr << " wrlock on " << *lock << " " << *obj << dendl;
4173 MDSCacheObjectInfo i;
4174 obj->set_object_info(i);
4175 ceph_assert(i.ino);
4176 rejoin->add_inode_wrlock(vinodeno_t(i.ino, i.snapid), lock->get_type(),
4177 mdr->reqid, mdr->attempt);
4178 }
4179 }
4180 }
4181 }
4182
4183 // send the messages
4184 for (auto &p : rejoins) {
4185 ceph_assert(rejoin_sent.count(p.first) == 0);
4186 ceph_assert(rejoin_ack_gather.count(p.first) == 0);
4187 rejoin_sent.insert(p.first);
4188 rejoin_ack_gather.insert(p.first);
4189 mds->send_message_mds(p.second, p.first);
4190 }
4191 rejoin_ack_gather.insert(mds->get_nodeid()); // we need to complete rejoin_gather_finish, too
4192 rejoins_pending = false;
4193
4194 // nothing?
4195 if (mds->is_rejoin() && rejoin_gather.empty()) {
4196 dout(10) << "nothing to rejoin" << dendl;
4197 rejoin_gather_finish();
4198 }
4199 }
4200
4201
4202 /**
4203 * rejoin_walk - build rejoin declarations for a subtree
4204 *
4205 * @param dir subtree root
4206 * @param rejoin rejoin message
4207 *
4208 * from a rejoining node:
4209 * weak dirfrag
4210 * weak dentries (w/ connectivity)
4211 *
4212 * from a surviving node:
4213 * strong dirfrag
4214 * strong dentries (no connectivity!)
4215 * strong inodes
4216 */
4217 void MDCache::rejoin_walk(CDir *dir, const ref_t<MMDSCacheRejoin> &rejoin)
4218 {
4219 dout(10) << "rejoin_walk " << *dir << dendl;
4220
4221 std::vector<CDir*> nested; // finish this dir, then do nested items
4222
4223 if (mds->is_rejoin()) {
4224 // WEAK
4225 rejoin->add_weak_dirfrag(dir->dirfrag());
4226 for (auto &p : dir->items) {
4227 CDentry *dn = p.second;
4228 ceph_assert(dn->last == CEPH_NOSNAP);
4229 CDentry::linkage_t *dnl = dn->get_linkage();
4230 dout(15) << " add_weak_primary_dentry " << *dn << dendl;
4231 ceph_assert(dnl->is_primary());
4232 CInode *in = dnl->get_inode();
4233 ceph_assert(dnl->get_inode()->is_dir());
4234 rejoin->add_weak_primary_dentry(dir->ino(), dn->get_name(), dn->first, dn->last, in->ino());
4235 {
4236 auto&& dirs = in->get_nested_dirfrags();
4237 nested.insert(std::end(nested), std::begin(dirs), std::end(dirs));
4238 }
4239 if (in->is_dirty_scattered()) {
4240 dout(10) << " sending scatterlock state on " << *in << dendl;
4241 rejoin->add_scatterlock_state(in);
4242 }
4243 }
4244 } else {
4245 // STRONG
4246 dout(15) << " add_strong_dirfrag " << *dir << dendl;
4247 rejoin->add_strong_dirfrag(dir->dirfrag(), dir->get_replica_nonce(), dir->get_dir_rep());
4248 dir->state_set(CDir::STATE_REJOINING);
4249
4250 for (auto it = dir->items.begin(); it != dir->items.end(); ) {
4251 CDentry *dn = it->second;
4252 ++it;
4253 dn->state_set(CDentry::STATE_REJOINING);
4254 CDentry::linkage_t *dnl = dn->get_linkage();
4255 CInode *in = dnl->is_primary() ? dnl->get_inode() : NULL;
4256
4257 // trim snap dentries. because they may have been pruned by
4258 // their auth mds (snap deleted)
4259 if (dn->last != CEPH_NOSNAP) {
4260 if (in && !in->remote_parents.empty()) {
4261 // unlink any stale remote snap dentry.
4262 for (auto it2 = in->remote_parents.begin(); it2 != in->remote_parents.end(); ) {
4263 CDentry *remote_dn = *it2;
4264 ++it2;
4265 ceph_assert(remote_dn->last != CEPH_NOSNAP);
4266 remote_dn->unlink_remote(remote_dn->get_linkage());
4267 }
4268 }
4269 if (dn->lru_is_expireable()) {
4270 if (!dnl->is_null())
4271 dir->unlink_inode(dn, false);
4272 if (in)
4273 remove_inode(in);
4274 dir->remove_dentry(dn);
4275 continue;
4276 } else {
4277 // Inventing null/remote dentry shouldn't cause problem
4278 ceph_assert(!dnl->is_primary());
4279 }
4280 }
4281
4282 dout(15) << " add_strong_dentry " << *dn << dendl;
4283 rejoin->add_strong_dentry(dir->dirfrag(), dn->get_name(), dn->get_alternate_name(),
4284 dn->first, dn->last,
4285 dnl->is_primary() ? dnl->get_inode()->ino():inodeno_t(0),
4286 dnl->is_remote() ? dnl->get_remote_ino():inodeno_t(0),
4287 dnl->is_remote() ? dnl->get_remote_d_type():0,
4288 dn->get_replica_nonce(),
4289 dn->lock.get_state());
4290 dn->state_set(CDentry::STATE_REJOINING);
4291 if (dnl->is_primary()) {
4292 CInode *in = dnl->get_inode();
4293 dout(15) << " add_strong_inode " << *in << dendl;
4294 rejoin->add_strong_inode(in->vino(),
4295 in->get_replica_nonce(),
4296 in->get_caps_wanted(),
4297 in->filelock.get_state(),
4298 in->nestlock.get_state(),
4299 in->dirfragtreelock.get_state());
4300 in->state_set(CInode::STATE_REJOINING);
4301 {
4302 auto&& dirs = in->get_nested_dirfrags();
4303 nested.insert(std::end(nested), std::begin(dirs), std::end(dirs));
4304 }
4305 if (in->is_dirty_scattered()) {
4306 dout(10) << " sending scatterlock state on " << *in << dendl;
4307 rejoin->add_scatterlock_state(in);
4308 }
4309 }
4310 }
4311 }
4312
4313 // recurse into nested dirs
4314 for (const auto& dir : nested) {
4315 rejoin_walk(dir, rejoin);
4316 }
4317 }
4318
4319
4320 /*
4321 * i got a rejoin.
4322 * - reply with the lockstate
4323 *
4324 * if i am active|stopping,
4325 * - remove source from replica list for everything not referenced here.
4326 */
4327 void MDCache::handle_cache_rejoin(const cref_t<MMDSCacheRejoin> &m)
4328 {
4329 dout(7) << "handle_cache_rejoin " << *m << " from " << m->get_source()
4330 << " (" << m->get_payload().length() << " bytes)"
4331 << dendl;
4332
4333 switch (m->op) {
4334 case MMDSCacheRejoin::OP_WEAK:
4335 handle_cache_rejoin_weak(m);
4336 break;
4337 case MMDSCacheRejoin::OP_STRONG:
4338 handle_cache_rejoin_strong(m);
4339 break;
4340 case MMDSCacheRejoin::OP_ACK:
4341 handle_cache_rejoin_ack(m);
4342 break;
4343
4344 default:
4345 ceph_abort();
4346 }
4347 }
4348
4349
4350 /*
4351 * handle_cache_rejoin_weak
4352 *
4353 * the sender
4354 * - is recovering from their journal.
4355 * - may have incorrect (out of date) inode contents
4356 * - will include weak dirfrag if sender is dirfrag auth and parent inode auth is recipient
4357 *
4358 * if the sender didn't trim_non_auth(), they
4359 * - may have incorrect (out of date) dentry/inode linkage
4360 * - may have deleted/purged inodes
4361 * and i may have to go to disk to get accurate inode contents. yuck.
4362 */
4363 void MDCache::handle_cache_rejoin_weak(const cref_t<MMDSCacheRejoin> &weak)
4364 {
4365 mds_rank_t from = mds_rank_t(weak->get_source().num());
4366
4367 // possible response(s)
4368 ref_t<MMDSCacheRejoin> ack; // if survivor
4369 set<vinodeno_t> acked_inodes; // if survivor
4370 set<SimpleLock *> gather_locks; // if survivor
4371 bool survivor = false; // am i a survivor?
4372
4373 if (mds->is_clientreplay() || mds->is_active() || mds->is_stopping()) {
4374 survivor = true;
4375 dout(10) << "i am a surivivor, and will ack immediately" << dendl;
4376 ack = make_message<MMDSCacheRejoin>(MMDSCacheRejoin::OP_ACK);
4377
4378 map<inodeno_t,map<client_t,Capability::Import> > imported_caps;
4379
4380 // check cap exports
4381 for (auto p = weak->cap_exports.begin(); p != weak->cap_exports.end(); ++p) {
4382 CInode *in = get_inode(p->first);
4383 ceph_assert(!in || in->is_auth());
4384 for (auto q = p->second.begin(); q != p->second.end(); ++q) {
4385 dout(10) << " claiming cap import " << p->first << " client." << q->first << " on " << *in << dendl;
4386 Capability *cap = rejoin_import_cap(in, q->first, q->second, from);
4387 Capability::Import& im = imported_caps[p->first][q->first];
4388 if (cap) {
4389 im.cap_id = cap->get_cap_id();
4390 im.issue_seq = cap->get_last_seq();
4391 im.mseq = cap->get_mseq();
4392 } else {
4393 // all are zero
4394 }
4395 }
4396 mds->locker->eval(in, CEPH_CAP_LOCKS, true);
4397 }
4398
4399 encode(imported_caps, ack->imported_caps);
4400 } else {
4401 ceph_assert(mds->is_rejoin());
4402
4403 // we may have already received a strong rejoin from the sender.
4404 rejoin_scour_survivor_replicas(from, NULL, acked_inodes, gather_locks);
4405 ceph_assert(gather_locks.empty());
4406
4407 // check cap exports.
4408 rejoin_client_map.insert(weak->client_map.begin(), weak->client_map.end());
4409 rejoin_client_metadata_map.insert(weak->client_metadata_map.begin(),
4410 weak->client_metadata_map.end());
4411
4412 for (auto p = weak->cap_exports.begin(); p != weak->cap_exports.end(); ++p) {
4413 CInode *in = get_inode(p->first);
4414 ceph_assert(!in || in->is_auth());
4415 // note
4416 for (auto q = p->second.begin(); q != p->second.end(); ++q) {
4417 dout(10) << " claiming cap import " << p->first << " client." << q->first << dendl;
4418 cap_imports[p->first][q->first][from] = q->second;
4419 }
4420 }
4421 }
4422
4423 // assimilate any potentially dirty scatterlock state
4424 for (const auto &p : weak->inode_scatterlocks) {
4425 CInode *in = get_inode(p.first);
4426 ceph_assert(in);
4427 in->decode_lock_state(CEPH_LOCK_IFILE, p.second.file);
4428 in->decode_lock_state(CEPH_LOCK_INEST, p.second.nest);
4429 in->decode_lock_state(CEPH_LOCK_IDFT, p.second.dft);
4430 if (!survivor)
4431 rejoin_potential_updated_scatterlocks.insert(in);
4432 }
4433
4434 // recovering peer may send incorrect dirfrags here. we need to
4435 // infer which dirfrag they meant. the ack will include a
4436 // strong_dirfrag that will set them straight on the fragmentation.
4437
4438 // walk weak map
4439 set<CDir*> dirs_to_share;
4440 for (const auto &p : weak->weak_dirfrags) {
4441 CInode *diri = get_inode(p.ino);
4442 if (!diri)
4443 dout(0) << " missing dir ino " << p.ino << dendl;
4444 ceph_assert(diri);
4445
4446 frag_vec_t leaves;
4447 if (diri->dirfragtree.is_leaf(p.frag)) {
4448 leaves.push_back(p.frag);
4449 } else {
4450 diri->dirfragtree.get_leaves_under(p.frag, leaves);
4451 if (leaves.empty())
4452 leaves.push_back(diri->dirfragtree[p.frag.value()]);
4453 }
4454 for (const auto& leaf : leaves) {
4455 CDir *dir = diri->get_dirfrag(leaf);
4456 if (!dir) {
4457 dout(0) << " missing dir for " << p.frag << " (which maps to " << leaf << ") on " << *diri << dendl;
4458 continue;
4459 }
4460 ceph_assert(dir);
4461 if (dirs_to_share.count(dir)) {
4462 dout(10) << " already have " << p.frag << " -> " << leaf << " " << *dir << dendl;
4463 } else {
4464 dirs_to_share.insert(dir);
4465 unsigned nonce = dir->add_replica(from);
4466 dout(10) << " have " << p.frag << " -> " << leaf << " " << *dir << dendl;
4467 if (ack) {
4468 ack->add_strong_dirfrag(dir->dirfrag(), nonce, dir->dir_rep);
4469 ack->add_dirfrag_base(dir);
4470 }
4471 }
4472 }
4473 }
4474
4475 for (const auto &p : weak->weak) {
4476 CInode *diri = get_inode(p.first);
4477 if (!diri)
4478 dout(0) << " missing dir ino " << p.first << dendl;
4479 ceph_assert(diri);
4480
4481 // weak dentries
4482 CDir *dir = 0;
4483 for (const auto &q : p.second) {
4484 // locate proper dirfrag.
4485 // optimize for common case (one dirfrag) to avoid dirs_to_share set check
4486 frag_t fg = diri->pick_dirfrag(q.first.name);
4487 if (!dir || dir->get_frag() != fg) {
4488 dir = diri->get_dirfrag(fg);
4489 if (!dir)
4490 dout(0) << " missing dir frag " << fg << " on " << *diri << dendl;
4491 ceph_assert(dir);
4492 ceph_assert(dirs_to_share.count(dir));
4493 }
4494
4495 // and dentry
4496 CDentry *dn = dir->lookup(q.first.name, q.first.snapid);
4497 ceph_assert(dn);
4498 CDentry::linkage_t *dnl = dn->get_linkage();
4499 ceph_assert(dnl->is_primary());
4500
4501 if (survivor && dn->is_replica(from))
4502 dentry_remove_replica(dn, from, gather_locks);
4503 unsigned dnonce = dn->add_replica(from);
4504 dout(10) << " have " << *dn << dendl;
4505 if (ack)
4506 ack->add_strong_dentry(dir->dirfrag(), dn->get_name(), dn->get_alternate_name(),
4507 dn->first, dn->last,
4508 dnl->get_inode()->ino(), inodeno_t(0), 0,
4509 dnonce, dn->lock.get_replica_state());
4510
4511 // inode
4512 CInode *in = dnl->get_inode();
4513 ceph_assert(in);
4514
4515 if (survivor && in->is_replica(from))
4516 inode_remove_replica(in, from, true, gather_locks);
4517 unsigned inonce = in->add_replica(from);
4518 dout(10) << " have " << *in << dendl;
4519
4520 // scatter the dirlock, just in case?
4521 if (!survivor && in->is_dir() && in->has_subtree_root_dirfrag())
4522 in->filelock.set_state(LOCK_MIX);
4523
4524 if (ack) {
4525 acked_inodes.insert(in->vino());
4526 ack->add_inode_base(in, mds->mdsmap->get_up_features());
4527 bufferlist bl;
4528 in->_encode_locks_state_for_rejoin(bl, from);
4529 ack->add_inode_locks(in, inonce, bl);
4530 }
4531 }
4532 }
4533
4534 // weak base inodes? (root, stray, etc.)
4535 for (set<vinodeno_t>::iterator p = weak->weak_inodes.begin();
4536 p != weak->weak_inodes.end();
4537 ++p) {
4538 CInode *in = get_inode(*p);
4539 ceph_assert(in); // hmm fixme wrt stray?
4540 if (survivor && in->is_replica(from))
4541 inode_remove_replica(in, from, true, gather_locks);
4542 unsigned inonce = in->add_replica(from);
4543 dout(10) << " have base " << *in << dendl;
4544
4545 if (ack) {
4546 acked_inodes.insert(in->vino());
4547 ack->add_inode_base(in, mds->mdsmap->get_up_features());
4548 bufferlist bl;
4549 in->_encode_locks_state_for_rejoin(bl, from);
4550 ack->add_inode_locks(in, inonce, bl);
4551 }
4552 }
4553
4554 ceph_assert(rejoin_gather.count(from));
4555 rejoin_gather.erase(from);
4556 if (survivor) {
4557 // survivor. do everything now.
4558 for (const auto &p : weak->inode_scatterlocks) {
4559 CInode *in = get_inode(p.first);
4560 ceph_assert(in);
4561 dout(10) << " including base inode (due to potential scatterlock update) " << *in << dendl;
4562 acked_inodes.insert(in->vino());
4563 ack->add_inode_base(in, mds->mdsmap->get_up_features());
4564 }
4565
4566 rejoin_scour_survivor_replicas(from, ack, acked_inodes, gather_locks);
4567 mds->send_message(ack, weak->get_connection());
4568
4569 for (set<SimpleLock*>::iterator p = gather_locks.begin(); p != gather_locks.end(); ++p) {
4570 if (!(*p)->is_stable())
4571 mds->locker->eval_gather(*p);
4572 }
4573 } else {
4574 // done?
4575 if (rejoin_gather.empty() && rejoin_ack_gather.count(mds->get_nodeid())) {
4576 rejoin_gather_finish();
4577 } else {
4578 dout(7) << "still need rejoin from (" << rejoin_gather << ")" << dendl;
4579 }
4580 }
4581 }
4582
4583 /*
4584 * rejoin_scour_survivor_replica - remove source from replica list on unmentioned objects
4585 *
4586 * all validated replicas are acked with a strong nonce, etc. if that isn't in the
4587 * ack, the replica dne, and we can remove it from our replica maps.
4588 */
4589 void MDCache::rejoin_scour_survivor_replicas(mds_rank_t from, const cref_t<MMDSCacheRejoin> &ack,
4590 set<vinodeno_t>& acked_inodes,
4591 set<SimpleLock *>& gather_locks)
4592 {
4593 dout(10) << "rejoin_scour_survivor_replicas from mds." << from << dendl;
4594
4595 auto scour_func = [this, from, ack, &acked_inodes, &gather_locks] (CInode *in) {
4596 // inode?
4597 if (in->is_auth() &&
4598 in->is_replica(from) &&
4599 (ack == NULL || acked_inodes.count(in->vino()) == 0)) {
4600 inode_remove_replica(in, from, false, gather_locks);
4601 dout(10) << " rem " << *in << dendl;
4602 }
4603
4604 if (!in->is_dir())
4605 return;
4606
4607 const auto&& dfs = in->get_dirfrags();
4608 for (const auto& dir : dfs) {
4609 if (!dir->is_auth())
4610 continue;
4611
4612 if (dir->is_replica(from) &&
4613 (ack == NULL || ack->strong_dirfrags.count(dir->dirfrag()) == 0)) {
4614 dir->remove_replica(from);
4615 dout(10) << " rem " << *dir << dendl;
4616 }
4617
4618 // dentries
4619 for (auto &p : dir->items) {
4620 CDentry *dn = p.second;
4621
4622 if (dn->is_replica(from)) {
4623 if (ack) {
4624 const auto it = ack->strong_dentries.find(dir->dirfrag());
4625 if (it != ack->strong_dentries.end() && it->second.count(string_snap_t(dn->get_name(), dn->last)) > 0) {
4626 continue;
4627 }
4628 }
4629 dentry_remove_replica(dn, from, gather_locks);
4630 dout(10) << " rem " << *dn << dendl;
4631 }
4632 }
4633 }
4634 };
4635
4636 for (auto &p : inode_map)
4637 scour_func(p.second);
4638 for (auto &p : snap_inode_map)
4639 scour_func(p.second);
4640 }
4641
4642
4643 CInode *MDCache::rejoin_invent_inode(inodeno_t ino, snapid_t last)
4644 {
4645 CInode *in = new CInode(this, true, 2, last);
4646 in->_get_inode()->ino = ino;
4647 in->state_set(CInode::STATE_REJOINUNDEF);
4648 add_inode(in);
4649 rejoin_undef_inodes.insert(in);
4650 dout(10) << " invented " << *in << dendl;
4651 return in;
4652 }
4653
4654 CDir *MDCache::rejoin_invent_dirfrag(dirfrag_t df)
4655 {
4656 CInode *in = get_inode(df.ino);
4657 if (!in)
4658 in = rejoin_invent_inode(df.ino, CEPH_NOSNAP);
4659 if (!in->is_dir()) {
4660 ceph_assert(in->state_test(CInode::STATE_REJOINUNDEF));
4661 in->_get_inode()->mode = S_IFDIR;
4662 in->_get_inode()->dir_layout.dl_dir_hash = g_conf()->mds_default_dir_hash;
4663 }
4664 CDir *dir = in->get_or_open_dirfrag(this, df.frag);
4665 dir->state_set(CDir::STATE_REJOINUNDEF);
4666 rejoin_undef_dirfrags.insert(dir);
4667 dout(10) << " invented " << *dir << dendl;
4668 return dir;
4669 }
4670
4671 void MDCache::handle_cache_rejoin_strong(const cref_t<MMDSCacheRejoin> &strong)
4672 {
4673 mds_rank_t from = mds_rank_t(strong->get_source().num());
4674
4675 // only a recovering node will get a strong rejoin.
4676 if (!mds->is_rejoin()) {
4677 if (mds->get_want_state() == MDSMap::STATE_REJOIN) {
4678 mds->wait_for_rejoin(new C_MDS_RetryMessage(mds, strong));
4679 return;
4680 }
4681 ceph_abort_msg("got unexpected rejoin message during recovery");
4682 }
4683
4684 // assimilate any potentially dirty scatterlock state
4685 for (const auto &p : strong->inode_scatterlocks) {
4686 CInode *in = get_inode(p.first);
4687 ceph_assert(in);
4688 in->decode_lock_state(CEPH_LOCK_IFILE, p.second.file);
4689 in->decode_lock_state(CEPH_LOCK_INEST, p.second.nest);
4690 in->decode_lock_state(CEPH_LOCK_IDFT, p.second.dft);
4691 rejoin_potential_updated_scatterlocks.insert(in);
4692 }
4693
4694 rejoin_unlinked_inodes[from].clear();
4695
4696 // surviving peer may send incorrect dirfrag here (maybe they didn't
4697 // get the fragment notify, or maybe we rolled back?). we need to
4698 // infer the right frag and get them with the program. somehow.
4699 // we don't normally send ACK.. so we'll need to bundle this with
4700 // MISSING or something.
4701
4702 // strong dirfrags/dentries.
4703 // also process auth_pins, xlocks.
4704 for (const auto &p : strong->strong_dirfrags) {
4705 auto& dirfrag = p.first;
4706 CInode *diri = get_inode(dirfrag.ino);
4707 if (!diri)
4708 diri = rejoin_invent_inode(dirfrag.ino, CEPH_NOSNAP);
4709 CDir *dir = diri->get_dirfrag(dirfrag.frag);
4710 bool refragged = false;
4711 if (dir) {
4712 dout(10) << " have " << *dir << dendl;
4713 } else {
4714 if (diri->state_test(CInode::STATE_REJOINUNDEF))
4715 dir = rejoin_invent_dirfrag(dirfrag_t(diri->ino(), frag_t()));
4716 else if (diri->dirfragtree.is_leaf(dirfrag.frag))
4717 dir = rejoin_invent_dirfrag(dirfrag);
4718 }
4719 if (dir) {
4720 dir->add_replica(from, p.second.nonce);
4721 dir->dir_rep = p.second.dir_rep;
4722 } else {
4723 dout(10) << " frag " << dirfrag << " doesn't match dirfragtree " << *diri << dendl;
4724 frag_vec_t leaves;
4725 diri->dirfragtree.get_leaves_under(dirfrag.frag, leaves);
4726 if (leaves.empty())
4727 leaves.push_back(diri->dirfragtree[dirfrag.frag.value()]);
4728 dout(10) << " maps to frag(s) " << leaves << dendl;
4729 for (const auto& leaf : leaves) {
4730 CDir *dir = diri->get_dirfrag(leaf);
4731 if (!dir)
4732 dir = rejoin_invent_dirfrag(dirfrag_t(diri->ino(), leaf));
4733 else
4734 dout(10) << " have(approx) " << *dir << dendl;
4735 dir->add_replica(from, p.second.nonce);
4736 dir->dir_rep = p.second.dir_rep;
4737 }
4738 refragged = true;
4739 }
4740
4741 const auto it = strong->strong_dentries.find(dirfrag);
4742 if (it != strong->strong_dentries.end()) {
4743 const auto& dmap = it->second;
4744 for (const auto &q : dmap) {
4745 const string_snap_t& ss = q.first;
4746 const MMDSCacheRejoin::dn_strong& d = q.second;
4747 CDentry *dn;
4748 if (!refragged)
4749 dn = dir->lookup(ss.name, ss.snapid);
4750 else {
4751 frag_t fg = diri->pick_dirfrag(ss.name);
4752 dir = diri->get_dirfrag(fg);
4753 ceph_assert(dir);
4754 dn = dir->lookup(ss.name, ss.snapid);
4755 }
4756 if (!dn) {
4757 if (d.is_remote()) {
4758 dn = dir->add_remote_dentry(ss.name, d.remote_ino, d.remote_d_type, mempool::mds_co::string(d.alternate_name), d.first, ss.snapid);
4759 } else if (d.is_null()) {
4760 dn = dir->add_null_dentry(ss.name, d.first, ss.snapid);
4761 } else {
4762 CInode *in = get_inode(d.ino, ss.snapid);
4763 if (!in) in = rejoin_invent_inode(d.ino, ss.snapid);
4764 dn = dir->add_primary_dentry(ss.name, in, mempool::mds_co::string(d.alternate_name), d.first, ss.snapid);
4765 }
4766 dout(10) << " invented " << *dn << dendl;
4767 }
4768 CDentry::linkage_t *dnl = dn->get_linkage();
4769
4770 // dn auth_pin?
4771 const auto pinned_it = strong->authpinned_dentries.find(dirfrag);
4772 if (pinned_it != strong->authpinned_dentries.end()) {
4773 const auto peer_reqid_it = pinned_it->second.find(ss);
4774 if (peer_reqid_it != pinned_it->second.end()) {
4775 for (const auto &r : peer_reqid_it->second) {
4776 dout(10) << " dn authpin by " << r << " on " << *dn << dendl;
4777
4778 // get/create peer mdrequest
4779 MDRequestRef mdr;
4780 if (have_request(r.reqid))
4781 mdr = request_get(r.reqid);
4782 else
4783 mdr = request_start_peer(r.reqid, r.attempt, strong);
4784 mdr->auth_pin(dn);
4785 }
4786 }
4787 }
4788
4789 // dn xlock?
4790 const auto xlocked_it = strong->xlocked_dentries.find(dirfrag);
4791 if (xlocked_it != strong->xlocked_dentries.end()) {
4792 const auto ss_req_it = xlocked_it->second.find(ss);
4793 if (ss_req_it != xlocked_it->second.end()) {
4794 const MMDSCacheRejoin::peer_reqid& r = ss_req_it->second;
4795 dout(10) << " dn xlock by " << r << " on " << *dn << dendl;
4796 MDRequestRef mdr = request_get(r.reqid); // should have this from auth_pin above.
4797 ceph_assert(mdr->is_auth_pinned(dn));
4798 if (!mdr->is_xlocked(&dn->versionlock)) {
4799 ceph_assert(dn->versionlock.can_xlock_local());
4800 dn->versionlock.get_xlock(mdr, mdr->get_client());
4801 mdr->emplace_lock(&dn->versionlock, MutationImpl::LockOp::XLOCK);
4802 }
4803 if (dn->lock.is_stable())
4804 dn->auth_pin(&dn->lock);
4805 dn->lock.set_state(LOCK_XLOCK);
4806 dn->lock.get_xlock(mdr, mdr->get_client());
4807 mdr->emplace_lock(&dn->lock, MutationImpl::LockOp::XLOCK);
4808 }
4809 }
4810
4811 dn->add_replica(from, d.nonce);
4812 dout(10) << " have " << *dn << dendl;
4813
4814 if (dnl->is_primary()) {
4815 if (d.is_primary()) {
4816 if (vinodeno_t(d.ino, ss.snapid) != dnl->get_inode()->vino()) {
4817 // the survivor missed MDentryUnlink+MDentryLink messages ?
4818 ceph_assert(strong->strong_inodes.count(dnl->get_inode()->vino()) == 0);
4819 CInode *in = get_inode(d.ino, ss.snapid);
4820 ceph_assert(in);
4821 ceph_assert(in->get_parent_dn());
4822 rejoin_unlinked_inodes[from].insert(in);
4823 dout(7) << " sender has primary dentry but wrong inode" << dendl;
4824 }
4825 } else {
4826 // the survivor missed MDentryLink message ?
4827 ceph_assert(strong->strong_inodes.count(dnl->get_inode()->vino()) == 0);
4828 dout(7) << " sender doesn't have primay dentry" << dendl;
4829 }
4830 } else {
4831 if (d.is_primary()) {
4832 // the survivor missed MDentryUnlink message ?
4833 CInode *in = get_inode(d.ino, ss.snapid);
4834 ceph_assert(in);
4835 ceph_assert(in->get_parent_dn());
4836 rejoin_unlinked_inodes[from].insert(in);
4837 dout(7) << " sender has primary dentry but we don't" << dendl;
4838 }
4839 }
4840 }
4841 }
4842 }
4843
4844 for (const auto &p : strong->strong_inodes) {
4845 CInode *in = get_inode(p.first);
4846 ceph_assert(in);
4847 in->add_replica(from, p.second.nonce);
4848 dout(10) << " have " << *in << dendl;
4849
4850 const MMDSCacheRejoin::inode_strong& is = p.second;
4851
4852 // caps_wanted
4853 if (is.caps_wanted) {
4854 in->set_mds_caps_wanted(from, is.caps_wanted);
4855 dout(15) << " inode caps_wanted " << ccap_string(is.caps_wanted)
4856 << " on " << *in << dendl;
4857 }
4858
4859 // scatterlocks?
4860 // infer state from replica state:
4861 // * go to MIX if they might have wrlocks
4862 // * go to LOCK if they are LOCK (just bc identify_files_to_recover might start twiddling filelock)
4863 in->filelock.infer_state_from_strong_rejoin(is.filelock, !in->is_dir()); // maybe also go to LOCK
4864 in->nestlock.infer_state_from_strong_rejoin(is.nestlock, false);
4865 in->dirfragtreelock.infer_state_from_strong_rejoin(is.dftlock, false);
4866
4867 // auth pin?
4868 const auto authpinned_inodes_it = strong->authpinned_inodes.find(in->vino());
4869 if (authpinned_inodes_it != strong->authpinned_inodes.end()) {
4870 for (const auto& r : authpinned_inodes_it->second) {
4871 dout(10) << " inode authpin by " << r << " on " << *in << dendl;
4872
4873 // get/create peer mdrequest
4874 MDRequestRef mdr;
4875 if (have_request(r.reqid))
4876 mdr = request_get(r.reqid);
4877 else
4878 mdr = request_start_peer(r.reqid, r.attempt, strong);
4879 if (strong->frozen_authpin_inodes.count(in->vino())) {
4880 ceph_assert(!in->get_num_auth_pins());
4881 mdr->freeze_auth_pin(in);
4882 } else {
4883 ceph_assert(!in->is_frozen_auth_pin());
4884 }
4885 mdr->auth_pin(in);
4886 }
4887 }
4888 // xlock(s)?
4889 const auto xlocked_inodes_it = strong->xlocked_inodes.find(in->vino());
4890 if (xlocked_inodes_it != strong->xlocked_inodes.end()) {
4891 for (const auto &q : xlocked_inodes_it->second) {
4892 SimpleLock *lock = in->get_lock(q.first);
4893 dout(10) << " inode xlock by " << q.second << " on " << *lock << " on " << *in << dendl;
4894 MDRequestRef mdr = request_get(q.second.reqid); // should have this from auth_pin above.
4895 ceph_assert(mdr->is_auth_pinned(in));
4896 if (!mdr->is_xlocked(&in->versionlock)) {
4897 ceph_assert(in->versionlock.can_xlock_local());
4898 in->versionlock.get_xlock(mdr, mdr->get_client());
4899 mdr->emplace_lock(&in->versionlock, MutationImpl::LockOp::XLOCK);
4900 }
4901 if (lock->is_stable())
4902 in->auth_pin(lock);
4903 lock->set_state(LOCK_XLOCK);
4904 if (lock == &in->filelock)
4905 in->loner_cap = -1;
4906 lock->get_xlock(mdr, mdr->get_client());
4907 mdr->emplace_lock(lock, MutationImpl::LockOp::XLOCK);
4908 }
4909 }
4910 }
4911 // wrlock(s)?
4912 for (const auto &p : strong->wrlocked_inodes) {
4913 CInode *in = get_inode(p.first);
4914 for (const auto &q : p.second) {
4915 SimpleLock *lock = in->get_lock(q.first);
4916 for (const auto &r : q.second) {
4917 dout(10) << " inode wrlock by " << r << " on " << *lock << " on " << *in << dendl;
4918 MDRequestRef mdr = request_get(r.reqid); // should have this from auth_pin above.
4919 if (in->is_auth())
4920 ceph_assert(mdr->is_auth_pinned(in));
4921 lock->set_state(LOCK_MIX);
4922 if (lock == &in->filelock)
4923 in->loner_cap = -1;
4924 lock->get_wrlock(true);
4925 mdr->emplace_lock(lock, MutationImpl::LockOp::WRLOCK);
4926 }
4927 }
4928 }
4929
4930 // done?
4931 ceph_assert(rejoin_gather.count(from));
4932 rejoin_gather.erase(from);
4933 if (rejoin_gather.empty() && rejoin_ack_gather.count(mds->get_nodeid())) {
4934 rejoin_gather_finish();
4935 } else {
4936 dout(7) << "still need rejoin from (" << rejoin_gather << ")" << dendl;
4937 }
4938 }
4939
4940 void MDCache::handle_cache_rejoin_ack(const cref_t<MMDSCacheRejoin> &ack)
4941 {
4942 dout(7) << "handle_cache_rejoin_ack from " << ack->get_source() << dendl;
4943 mds_rank_t from = mds_rank_t(ack->get_source().num());
4944
4945 ceph_assert(mds->get_state() >= MDSMap::STATE_REJOIN);
4946 bool survivor = !mds->is_rejoin();
4947
4948 // for sending cache expire message
4949 set<CInode*> isolated_inodes;
4950 set<CInode*> refragged_inodes;
4951 list<pair<CInode*,int> > updated_realms;
4952
4953 // dirs
4954 for (const auto &p : ack->strong_dirfrags) {
4955 // we may have had incorrect dir fragmentation; refragment based
4956 // on what they auth tells us.
4957 CDir *dir = get_dirfrag(p.first);
4958 if (!dir) {
4959 dir = get_force_dirfrag(p.first, false);
4960 if (dir)
4961 refragged_inodes.insert(dir->get_inode());
4962 }
4963 if (!dir) {
4964 CInode *diri = get_inode(p.first.ino);
4965 if (!diri) {
4966 // barebones inode; the full inode loop below will clean up.
4967 diri = new CInode(this, false);
4968 auto _inode = diri->_get_inode();
4969 _inode->ino = p.first.ino;
4970 _inode->mode = S_IFDIR;
4971 _inode->dir_layout.dl_dir_hash = g_conf()->mds_default_dir_hash;
4972
4973 add_inode(diri);
4974 if (MDS_INO_MDSDIR(from) == p.first.ino) {
4975 diri->inode_auth = mds_authority_t(from, CDIR_AUTH_UNKNOWN);
4976 dout(10) << " add inode " << *diri << dendl;
4977 } else {
4978 diri->inode_auth = CDIR_AUTH_DEFAULT;
4979 isolated_inodes.insert(diri);
4980 dout(10) << " unconnected dirfrag " << p.first << dendl;
4981 }
4982 }
4983 // barebones dirfrag; the full dirfrag loop below will clean up.
4984 dir = diri->add_dirfrag(new CDir(diri, p.first.frag, this, false));
4985 if (MDS_INO_MDSDIR(from) == p.first.ino ||
4986 (dir->authority() != CDIR_AUTH_UNDEF &&
4987 dir->authority().first != from))
4988 adjust_subtree_auth(dir, from);
4989 dout(10) << " add dirfrag " << *dir << dendl;
4990 }
4991
4992 dir->set_replica_nonce(p.second.nonce);
4993 dir->state_clear(CDir::STATE_REJOINING);
4994 dout(10) << " got " << *dir << dendl;
4995
4996 // dentries
4997 auto it = ack->strong_dentries.find(p.first);
4998 if (it != ack->strong_dentries.end()) {
4999 for (const auto &q : it->second) {
5000 CDentry *dn = dir->lookup(q.first.name, q.first.snapid);
5001 if(!dn)
5002 dn = dir->add_null_dentry(q.first.name, q.second.first, q.first.snapid);
5003
5004 CDentry::linkage_t *dnl = dn->get_linkage();
5005
5006 ceph_assert(dn->last == q.first.snapid);
5007 if (dn->first != q.second.first) {
5008 dout(10) << " adjust dn.first " << dn->first << " -> " << q.second.first << " on " << *dn << dendl;
5009 dn->first = q.second.first;
5010 }
5011
5012 // may have bad linkage if we missed dentry link/unlink messages
5013 if (dnl->is_primary()) {
5014 CInode *in = dnl->get_inode();
5015 if (!q.second.is_primary() ||
5016 vinodeno_t(q.second.ino, q.first.snapid) != in->vino()) {
5017 dout(10) << " had bad linkage for " << *dn << ", unlinking " << *in << dendl;
5018 dir->unlink_inode(dn);
5019 }
5020 } else if (dnl->is_remote()) {
5021 if (!q.second.is_remote() ||
5022 q.second.remote_ino != dnl->get_remote_ino() ||
5023 q.second.remote_d_type != dnl->get_remote_d_type()) {
5024 dout(10) << " had bad linkage for " << *dn << dendl;
5025 dir->unlink_inode(dn);
5026 }
5027 } else {
5028 if (!q.second.is_null())
5029 dout(10) << " had bad linkage for " << *dn << dendl;
5030 }
5031
5032 // hmm, did we have the proper linkage here?
5033 if (dnl->is_null() && !q.second.is_null()) {
5034 if (q.second.is_remote()) {
5035 dn->dir->link_remote_inode(dn, q.second.remote_ino, q.second.remote_d_type);
5036 } else {
5037 CInode *in = get_inode(q.second.ino, q.first.snapid);
5038 if (!in) {
5039 // barebones inode; assume it's dir, the full inode loop below will clean up.
5040 in = new CInode(this, false, q.second.first, q.first.snapid);
5041 auto _inode = in->_get_inode();
5042 _inode->ino = q.second.ino;
5043 _inode->mode = S_IFDIR;
5044 _inode->dir_layout.dl_dir_hash = g_conf()->mds_default_dir_hash;
5045 add_inode(in);
5046 dout(10) << " add inode " << *in << dendl;
5047 } else if (in->get_parent_dn()) {
5048 dout(10) << " had bad linkage for " << *(in->get_parent_dn())
5049 << ", unlinking " << *in << dendl;
5050 in->get_parent_dir()->unlink_inode(in->get_parent_dn());
5051 }
5052 dn->dir->link_primary_inode(dn, in);
5053 isolated_inodes.erase(in);
5054 }
5055 }
5056
5057 dn->set_replica_nonce(q.second.nonce);
5058 dn->lock.set_state_rejoin(q.second.lock, rejoin_waiters, survivor);
5059 dn->state_clear(CDentry::STATE_REJOINING);
5060 dout(10) << " got " << *dn << dendl;
5061 }
5062 }
5063 }
5064
5065 for (const auto& in : refragged_inodes) {
5066 auto&& ls = in->get_nested_dirfrags();
5067 for (const auto& dir : ls) {
5068 if (dir->is_auth() || ack->strong_dirfrags.count(dir->dirfrag()))
5069 continue;
5070 ceph_assert(dir->get_num_any() == 0);
5071 in->close_dirfrag(dir->get_frag());
5072 }
5073 }
5074
5075 // full dirfrags
5076 for (const auto &p : ack->dirfrag_bases) {
5077 CDir *dir = get_dirfrag(p.first);
5078 ceph_assert(dir);
5079 auto q = p.second.cbegin();
5080 dir->_decode_base(q);
5081 dout(10) << " got dir replica " << *dir << dendl;
5082 }
5083
5084 // full inodes
5085 auto p = ack->inode_base.cbegin();
5086 while (!p.end()) {
5087 inodeno_t ino;
5088 snapid_t last;
5089 bufferlist basebl;
5090 decode(ino, p);
5091 decode(last, p);
5092 decode(basebl, p);
5093 CInode *in = get_inode(ino, last);
5094 ceph_assert(in);
5095 auto q = basebl.cbegin();
5096 snapid_t sseq = 0;
5097 if (in->snaprealm)
5098 sseq = in->snaprealm->srnode.seq;
5099 in->_decode_base(q);
5100 if (in->snaprealm && in->snaprealm->srnode.seq != sseq) {
5101 int snap_op = sseq > 0 ? CEPH_SNAP_OP_UPDATE : CEPH_SNAP_OP_SPLIT;
5102 updated_realms.push_back(pair<CInode*,int>(in, snap_op));
5103 }
5104 dout(10) << " got inode base " << *in << dendl;
5105 }
5106
5107 // inodes
5108 p = ack->inode_locks.cbegin();
5109 //dout(10) << "inode_locks len " << ack->inode_locks.length() << " is " << ack->inode_locks << dendl;
5110 while (!p.end()) {
5111 inodeno_t ino;
5112 snapid_t last;
5113 __u32 nonce;
5114 bufferlist lockbl;
5115 decode(ino, p);
5116 decode(last, p);
5117 decode(nonce, p);
5118 decode(lockbl, p);
5119
5120 CInode *in = get_inode(ino, last);
5121 ceph_assert(in);
5122 in->set_replica_nonce(nonce);
5123 auto q = lockbl.cbegin();
5124 in->_decode_locks_rejoin(q, rejoin_waiters, rejoin_eval_locks, survivor);
5125 in->state_clear(CInode::STATE_REJOINING);
5126 dout(10) << " got inode locks " << *in << dendl;
5127 }
5128
5129 // FIXME: This can happen if entire subtree, together with the inode subtree root
5130 // belongs to, were trimmed between sending cache rejoin and receiving rejoin ack.
5131 ceph_assert(isolated_inodes.empty());
5132
5133 map<inodeno_t,map<client_t,Capability::Import> > peer_imported;
5134 auto bp = ack->imported_caps.cbegin();
5135 decode(peer_imported, bp);
5136
5137 for (map<inodeno_t,map<client_t,Capability::Import> >::iterator p = peer_imported.begin();
5138 p != peer_imported.end();
5139 ++p) {
5140 auto& ex = cap_exports.at(p->first);
5141 ceph_assert(ex.first == from);
5142 for (map<client_t,Capability::Import>::iterator q = p->second.begin();
5143 q != p->second.end();
5144 ++q) {
5145 auto r = ex.second.find(q->first);
5146 ceph_assert(r != ex.second.end());
5147
5148 dout(10) << " exporting caps for client." << q->first << " ino " << p->first << dendl;
5149 Session *session = mds->sessionmap.get_session(entity_name_t::CLIENT(q->first.v));
5150 if (!session) {
5151 dout(10) << " no session for client." << p->first << dendl;
5152 ex.second.erase(r);
5153 continue;
5154 }
5155
5156 // mark client caps stale.
5157 auto m = make_message<MClientCaps>(CEPH_CAP_OP_EXPORT, p->first, 0,
5158 r->second.capinfo.cap_id, 0,
5159 mds->get_osd_epoch_barrier());
5160 m->set_cap_peer(q->second.cap_id, q->second.issue_seq, q->second.mseq,
5161 (q->second.cap_id > 0 ? from : -1), 0);
5162 mds->send_message_client_counted(m, session);
5163
5164 ex.second.erase(r);
5165 }
5166 ceph_assert(ex.second.empty());
5167 }
5168
5169 for (auto p : updated_realms) {
5170 CInode *in = p.first;
5171 bool notify_clients;
5172 if (mds->is_rejoin()) {
5173 if (!rejoin_pending_snaprealms.count(in)) {
5174 in->get(CInode::PIN_OPENINGSNAPPARENTS);
5175 rejoin_pending_snaprealms.insert(in);
5176 }
5177 notify_clients = false;
5178 } else {
5179 // notify clients if I'm survivor
5180 notify_clients = true;
5181 }
5182 do_realm_invalidate_and_update_notify(in, p.second, notify_clients);
5183 }
5184
5185 // done?
5186 ceph_assert(rejoin_ack_gather.count(from));
5187 rejoin_ack_gather.erase(from);
5188 if (!survivor) {
5189 if (rejoin_gather.empty()) {
5190 // eval unstable scatter locks after all wrlocks are rejoined.
5191 while (!rejoin_eval_locks.empty()) {
5192 SimpleLock *lock = rejoin_eval_locks.front();
5193 rejoin_eval_locks.pop_front();
5194 if (!lock->is_stable())
5195 mds->locker->eval_gather(lock);
5196 }
5197 }
5198
5199 if (rejoin_gather.empty() && // make sure we've gotten our FULL inodes, too.
5200 rejoin_ack_gather.empty()) {
5201 // finally, kickstart past snap parent opens
5202 open_snaprealms();
5203 } else {
5204 dout(7) << "still need rejoin from (" << rejoin_gather << ")"
5205 << ", rejoin_ack from (" << rejoin_ack_gather << ")" << dendl;
5206 }
5207 } else {
5208 // survivor.
5209 mds->queue_waiters(rejoin_waiters);
5210 }
5211 }
5212
5213 /**
5214 * rejoin_trim_undef_inodes() -- remove REJOINUNDEF flagged inodes
5215 *
5216 * FIXME: wait, can this actually happen? a survivor should generate cache trim
5217 * messages that clean these guys up...
5218 */
5219 void MDCache::rejoin_trim_undef_inodes()
5220 {
5221 dout(10) << "rejoin_trim_undef_inodes" << dendl;
5222
5223 while (!rejoin_undef_inodes.empty()) {
5224 set<CInode*>::iterator p = rejoin_undef_inodes.begin();
5225 CInode *in = *p;
5226 rejoin_undef_inodes.erase(p);
5227
5228 in->clear_replica_map();
5229
5230 // close out dirfrags
5231 if (in->is_dir()) {
5232 const auto&& dfls = in->get_dirfrags();
5233 for (const auto& dir : dfls) {
5234 dir->clear_replica_map();
5235
5236 for (auto &p : dir->items) {
5237 CDentry *dn = p.second;
5238 dn->clear_replica_map();
5239
5240 dout(10) << " trimming " << *dn << dendl;
5241 dir->remove_dentry(dn);
5242 }
5243
5244 dout(10) << " trimming " << *dir << dendl;
5245 in->close_dirfrag(dir->dirfrag().frag);
5246 }
5247 }
5248
5249 CDentry *dn = in->get_parent_dn();
5250 if (dn) {
5251 dn->clear_replica_map();
5252 dout(10) << " trimming " << *dn << dendl;
5253 dn->dir->remove_dentry(dn);
5254 } else {
5255 dout(10) << " trimming " << *in << dendl;
5256 remove_inode(in);
5257 }
5258 }
5259
5260 ceph_assert(rejoin_undef_inodes.empty());
5261 }
5262
5263 void MDCache::rejoin_gather_finish()
5264 {
5265 dout(10) << "rejoin_gather_finish" << dendl;
5266 ceph_assert(mds->is_rejoin());
5267 ceph_assert(rejoin_ack_gather.count(mds->get_nodeid()));
5268
5269 if (open_undef_inodes_dirfrags())
5270 return;
5271
5272 if (process_imported_caps())
5273 return;
5274
5275 choose_lock_states_and_reconnect_caps();
5276
5277 identify_files_to_recover();
5278 rejoin_send_acks();
5279
5280 // signal completion of fetches, rejoin_gather_finish, etc.
5281 rejoin_ack_gather.erase(mds->get_nodeid());
5282
5283 // did we already get our acks too?
5284 if (rejoin_ack_gather.empty()) {
5285 // finally, open snaprealms
5286 open_snaprealms();
5287 }
5288 }
5289
5290 class C_MDC_RejoinOpenInoFinish: public MDCacheContext {
5291 inodeno_t ino;
5292 public:
5293 C_MDC_RejoinOpenInoFinish(MDCache *c, inodeno_t i) : MDCacheContext(c), ino(i) {}
5294 void finish(int r) override {
5295 mdcache->rejoin_open_ino_finish(ino, r);
5296 }
5297 };
5298
5299 void MDCache::rejoin_open_ino_finish(inodeno_t ino, int ret)
5300 {
5301 dout(10) << "open_caps_inode_finish ino " << ino << " ret " << ret << dendl;
5302
5303 if (ret < 0) {
5304 cap_imports_missing.insert(ino);
5305 } else if (ret == mds->get_nodeid()) {
5306 ceph_assert(get_inode(ino));
5307 } else {
5308 auto p = cap_imports.find(ino);
5309 ceph_assert(p != cap_imports.end());
5310 for (auto q = p->second.begin(); q != p->second.end(); ++q) {
5311 ceph_assert(q->second.count(MDS_RANK_NONE));
5312 ceph_assert(q->second.size() == 1);
5313 rejoin_export_caps(p->first, q->first, q->second[MDS_RANK_NONE], ret);
5314 }
5315 cap_imports.erase(p);
5316 }
5317
5318 ceph_assert(cap_imports_num_opening > 0);
5319 cap_imports_num_opening--;
5320
5321 if (cap_imports_num_opening == 0) {
5322 if (rejoin_gather.empty() && rejoin_ack_gather.count(mds->get_nodeid()))
5323 rejoin_gather_finish();
5324 else if (rejoin_gather.count(mds->get_nodeid()))
5325 process_imported_caps();
5326 }
5327 }
5328
5329 class C_MDC_RejoinSessionsOpened : public MDCacheLogContext {
5330 public:
5331 map<client_t,pair<Session*,uint64_t> > session_map;
5332 C_MDC_RejoinSessionsOpened(MDCache *c) : MDCacheLogContext(c) {}
5333 void finish(int r) override {
5334 ceph_assert(r == 0);
5335 mdcache->rejoin_open_sessions_finish(session_map);
5336 }
5337 };
5338
5339 void MDCache::rejoin_open_sessions_finish(map<client_t,pair<Session*,uint64_t> >& session_map)
5340 {
5341 dout(10) << "rejoin_open_sessions_finish" << dendl;
5342 mds->server->finish_force_open_sessions(session_map);
5343 rejoin_session_map.swap(session_map);
5344 if (rejoin_gather.empty() && rejoin_ack_gather.count(mds->get_nodeid()))
5345 rejoin_gather_finish();
5346 }
5347
5348 void MDCache::rejoin_prefetch_ino_finish(inodeno_t ino, int ret)
5349 {
5350 auto p = cap_imports.find(ino);
5351 if (p != cap_imports.end()) {
5352 dout(10) << __func__ << " ino " << ino << " ret " << ret << dendl;
5353 if (ret < 0) {
5354 cap_imports_missing.insert(ino);
5355 } else if (ret != mds->get_nodeid()) {
5356 for (auto q = p->second.begin(); q != p->second.end(); ++q) {
5357 ceph_assert(q->second.count(MDS_RANK_NONE));
5358 ceph_assert(q->second.size() == 1);
5359 rejoin_export_caps(p->first, q->first, q->second[MDS_RANK_NONE], ret);
5360 }
5361 cap_imports.erase(p);
5362 }
5363 }
5364 }
5365
5366 bool MDCache::process_imported_caps()
5367 {
5368 dout(10) << "process_imported_caps" << dendl;
5369
5370 if (!open_file_table.is_prefetched() &&
5371 open_file_table.prefetch_inodes()) {
5372 open_file_table.wait_for_prefetch(
5373 new MDSInternalContextWrapper(mds,
5374 new LambdaContext([this](int r) {
5375 ceph_assert(rejoin_gather.count(mds->get_nodeid()));
5376 process_imported_caps();
5377 })
5378 )
5379 );
5380 return true;
5381 }
5382
5383 open_ino_batch_start();
5384
5385 for (auto& p : cap_imports) {
5386 CInode *in = get_inode(p.first);
5387 if (in) {
5388 ceph_assert(in->is_auth());
5389 cap_imports_missing.erase(p.first);
5390 continue;
5391 }
5392 if (cap_imports_missing.count(p.first) > 0)
5393 continue;
5394
5395 uint64_t parent_ino = 0;
5396 std::string_view d_name;
5397 for (auto& q : p.second) {
5398 for (auto& r : q.second) {
5399 auto &icr = r.second;
5400 if (icr.capinfo.pathbase &&
5401 icr.path.length() > 0 &&
5402 icr.path.find('/') == string::npos) {
5403 parent_ino = icr.capinfo.pathbase;
5404 d_name = icr.path;
5405 break;
5406 }
5407 }
5408 if (parent_ino)
5409 break;
5410 }
5411
5412 dout(10) << " opening missing ino " << p.first << dendl;
5413 cap_imports_num_opening++;
5414 auto fin = new C_MDC_RejoinOpenInoFinish(this, p.first);
5415 if (parent_ino) {
5416 vector<inode_backpointer_t> ancestors;
5417 ancestors.push_back(inode_backpointer_t(parent_ino, string{d_name}, 0));
5418 open_ino(p.first, (int64_t)-1, fin, false, false, &ancestors);
5419 } else {
5420 open_ino(p.first, (int64_t)-1, fin, false);
5421 }
5422 if (!(cap_imports_num_opening % mds->heartbeat_reset_grace()))
5423 mds->heartbeat_reset();
5424 }
5425
5426 open_ino_batch_submit();
5427
5428 if (cap_imports_num_opening > 0)
5429 return true;
5430
5431 // called by rejoin_gather_finish() ?
5432 if (rejoin_gather.count(mds->get_nodeid()) == 0) {
5433 if (!rejoin_client_map.empty() &&
5434 rejoin_session_map.empty()) {
5435 C_MDC_RejoinSessionsOpened *finish = new C_MDC_RejoinSessionsOpened(this);
5436 version_t pv = mds->server->prepare_force_open_sessions(rejoin_client_map,
5437 rejoin_client_metadata_map,
5438 finish->session_map);
5439 ESessions *le = new ESessions(pv, std::move(rejoin_client_map),
5440 std::move(rejoin_client_metadata_map));
5441 mds->mdlog->start_submit_entry(le, finish);
5442 mds->mdlog->flush();
5443 rejoin_client_map.clear();
5444 rejoin_client_metadata_map.clear();
5445 return true;
5446 }
5447
5448 // process caps that were exported by peer rename
5449 for (map<inodeno_t,pair<mds_rank_t,map<client_t,Capability::Export> > >::iterator p = rejoin_peer_exports.begin();
5450 p != rejoin_peer_exports.end();
5451 ++p) {
5452 CInode *in = get_inode(p->first);
5453 ceph_assert(in);
5454 for (map<client_t,Capability::Export>::iterator q = p->second.second.begin();
5455 q != p->second.second.end();
5456 ++q) {
5457 auto r = rejoin_session_map.find(q->first);
5458 if (r == rejoin_session_map.end())
5459 continue;
5460
5461 Session *session = r->second.first;
5462 Capability *cap = in->get_client_cap(q->first);
5463 if (!cap) {
5464 cap = in->add_client_cap(q->first, session);
5465 // add empty item to reconnected_caps
5466 (void)reconnected_caps[p->first][q->first];
5467 }
5468 cap->merge(q->second, true);
5469
5470 Capability::Import& im = rejoin_imported_caps[p->second.first][p->first][q->first];
5471 ceph_assert(cap->get_last_seq() == im.issue_seq);
5472 ceph_assert(cap->get_mseq() == im.mseq);
5473 cap->set_cap_id(im.cap_id);
5474 // send cap import because we assigned a new cap ID
5475 do_cap_import(session, in, cap, q->second.cap_id, q->second.seq, q->second.mseq - 1,
5476 p->second.first, CEPH_CAP_FLAG_AUTH);
5477 }
5478 }
5479 rejoin_peer_exports.clear();
5480 rejoin_imported_caps.clear();
5481
5482 // process cap imports
5483 // ino -> client -> frommds -> capex
5484 for (auto p = cap_imports.begin(); p != cap_imports.end(); ) {
5485 CInode *in = get_inode(p->first);
5486 if (!in) {
5487 dout(10) << " still missing ino " << p->first
5488 << ", will try again after replayed client requests" << dendl;
5489 ++p;
5490 continue;
5491 }
5492 ceph_assert(in->is_auth());
5493 for (auto q = p->second.begin(); q != p->second.end(); ++q) {
5494 Session *session;
5495 {
5496 auto r = rejoin_session_map.find(q->first);
5497 session = (r != rejoin_session_map.end() ? r->second.first : nullptr);
5498 }
5499
5500 for (auto r = q->second.begin(); r != q->second.end(); ++r) {
5501 if (!session) {
5502 if (r->first >= 0)
5503 (void)rejoin_imported_caps[r->first][p->first][q->first]; // all are zero
5504 continue;
5505 }
5506
5507 Capability *cap = in->reconnect_cap(q->first, r->second, session);
5508 add_reconnected_cap(q->first, in->ino(), r->second);
5509 if (r->first >= 0) {
5510 if (cap->get_last_seq() == 0) // don't increase mseq if cap already exists
5511 cap->inc_mseq();
5512 do_cap_import(session, in, cap, r->second.capinfo.cap_id, 0, 0, r->first, 0);
5513
5514 Capability::Import& im = rejoin_imported_caps[r->first][p->first][q->first];
5515 im.cap_id = cap->get_cap_id();
5516 im.issue_seq = cap->get_last_seq();
5517 im.mseq = cap->get_mseq();
5518 }
5519 }
5520 }
5521 cap_imports.erase(p++); // remove and move on
5522 }
5523 } else {
5524 trim_non_auth();
5525
5526 ceph_assert(rejoin_gather.count(mds->get_nodeid()));
5527 rejoin_gather.erase(mds->get_nodeid());
5528 ceph_assert(!rejoin_ack_gather.count(mds->get_nodeid()));
5529 maybe_send_pending_rejoins();
5530 }
5531 return false;
5532 }
5533
5534 void MDCache::rebuild_need_snapflush(CInode *head_in, SnapRealm *realm,
5535 client_t client, snapid_t snap_follows)
5536 {
5537 dout(10) << "rebuild_need_snapflush " << snap_follows << " on " << *head_in << dendl;
5538
5539 if (!realm->has_snaps_in_range(snap_follows + 1, head_in->first - 1))
5540 return;
5541
5542 const set<snapid_t>& snaps = realm->get_snaps();
5543 snapid_t follows = snap_follows;
5544
5545 while (true) {
5546 CInode *in = pick_inode_snap(head_in, follows);
5547 if (in == head_in)
5548 break;
5549
5550 bool need_snapflush = false;
5551 for (auto p = snaps.lower_bound(std::max<snapid_t>(in->first, (follows + 1)));
5552 p != snaps.end() && *p <= in->last;
5553 ++p) {
5554 head_in->add_need_snapflush(in, *p, client);
5555 need_snapflush = true;
5556 }
5557 follows = in->last;
5558 if (!need_snapflush)
5559 continue;
5560
5561 dout(10) << " need snapflush from client." << client << " on " << *in << dendl;
5562
5563 if (in->client_snap_caps.empty()) {
5564 for (int i = 0; i < num_cinode_locks; i++) {
5565 int lockid = cinode_lock_info[i].lock;
5566 SimpleLock *lock = in->get_lock(lockid);
5567 ceph_assert(lock);
5568 in->auth_pin(lock);
5569 lock->set_state(LOCK_SNAP_SYNC);
5570 lock->get_wrlock(true);
5571 }
5572 }
5573 in->client_snap_caps.insert(client);
5574 mds->locker->mark_need_snapflush_inode(in);
5575 }
5576 }
5577
5578 /*
5579 * choose lock states based on reconnected caps
5580 */
5581 void MDCache::choose_lock_states_and_reconnect_caps()
5582 {
5583 dout(10) << "choose_lock_states_and_reconnect_caps" << dendl;
5584
5585 int count = 0;
5586 for (auto p : inode_map) {
5587 CInode *in = p.second;
5588 if (in->last != CEPH_NOSNAP)
5589 continue;
5590
5591 if (in->is_auth() && !in->is_base() && in->get_inode()->is_dirty_rstat())
5592 in->mark_dirty_rstat();
5593
5594 int dirty_caps = 0;
5595 auto q = reconnected_caps.find(in->ino());
5596 if (q != reconnected_caps.end()) {
5597 for (const auto &it : q->second)
5598 dirty_caps |= it.second.dirty_caps;
5599 }
5600 in->choose_lock_states(dirty_caps);
5601 dout(15) << " chose lock states on " << *in << dendl;
5602
5603 if (in->snaprealm && !rejoin_pending_snaprealms.count(in)) {
5604 in->get(CInode::PIN_OPENINGSNAPPARENTS);
5605 rejoin_pending_snaprealms.insert(in);
5606 }
5607
5608 if (!(++count % mds->heartbeat_reset_grace()))
5609 mds->heartbeat_reset();
5610 }
5611 }
5612
5613 void MDCache::prepare_realm_split(SnapRealm *realm, client_t client, inodeno_t ino,
5614 map<client_t,ref_t<MClientSnap>>& splits)
5615 {
5616 ref_t<MClientSnap> snap;
5617 auto it = splits.find(client);
5618 if (it != splits.end()) {
5619 snap = it->second;
5620 snap->head.op = CEPH_SNAP_OP_SPLIT;
5621 } else {
5622 snap = make_message<MClientSnap>(CEPH_SNAP_OP_SPLIT);
5623 splits.emplace(std::piecewise_construct, std::forward_as_tuple(client), std::forward_as_tuple(snap));
5624 snap->head.split = realm->inode->ino();
5625 snap->bl = mds->server->get_snap_trace(client, realm);
5626
5627 for (const auto& child : realm->open_children)
5628 snap->split_realms.push_back(child->inode->ino());
5629 }
5630 snap->split_inos.push_back(ino);
5631 }
5632
5633 void MDCache::prepare_realm_merge(SnapRealm *realm, SnapRealm *parent_realm,
5634 map<client_t,ref_t<MClientSnap>>& splits)
5635 {
5636 ceph_assert(parent_realm);
5637
5638 vector<inodeno_t> split_inos;
5639 vector<inodeno_t> split_realms;
5640
5641 for (auto p = realm->inodes_with_caps.begin(); !p.end(); ++p)
5642 split_inos.push_back((*p)->ino());
5643 for (set<SnapRealm*>::iterator p = realm->open_children.begin();
5644 p != realm->open_children.end();
5645 ++p)
5646 split_realms.push_back((*p)->inode->ino());
5647
5648 for (const auto& p : realm->client_caps) {
5649 ceph_assert(!p.second->empty());
5650 auto em = splits.emplace(std::piecewise_construct, std::forward_as_tuple(p.first), std::forward_as_tuple());
5651 if (em.second) {
5652 auto update = make_message<MClientSnap>(CEPH_SNAP_OP_SPLIT);
5653 update->head.split = parent_realm->inode->ino();
5654 update->split_inos = split_inos;
5655 update->split_realms = split_realms;
5656 update->bl = mds->server->get_snap_trace(p.first, parent_realm);
5657 em.first->second = std::move(update);
5658 }
5659 }
5660 }
5661
5662 void MDCache::send_snaps(map<client_t,ref_t<MClientSnap>>& splits)
5663 {
5664 dout(10) << "send_snaps" << dendl;
5665
5666 for (auto &p : splits) {
5667 Session *session = mds->sessionmap.get_session(entity_name_t::CLIENT(p.first.v));
5668 if (session) {
5669 dout(10) << " client." << p.first
5670 << " split " << p.second->head.split
5671 << " inos " << p.second->split_inos
5672 << dendl;
5673 mds->send_message_client_counted(p.second, session);
5674 } else {
5675 dout(10) << " no session for client." << p.first << dendl;
5676 }
5677 }
5678 splits.clear();
5679 }
5680
5681
5682 /*
5683 * remove any items from logsegment open_file lists that don't have
5684 * any caps
5685 */
5686 void MDCache::clean_open_file_lists()
5687 {
5688 dout(10) << "clean_open_file_lists" << dendl;
5689
5690 for (map<uint64_t,LogSegment*>::iterator p = mds->mdlog->segments.begin();
5691 p != mds->mdlog->segments.end();
5692 ++p) {
5693 LogSegment *ls = p->second;
5694
5695 elist<CInode*>::iterator q = ls->open_files.begin(member_offset(CInode, item_open_file));
5696 while (!q.end()) {
5697 CInode *in = *q;
5698 ++q;
5699 if (in->last == CEPH_NOSNAP) {
5700 dout(10) << " unlisting unwanted/capless inode " << *in << dendl;
5701 in->item_open_file.remove_myself();
5702 } else {
5703 if (in->client_snap_caps.empty()) {
5704 dout(10) << " unlisting flushed snap inode " << *in << dendl;
5705 in->item_open_file.remove_myself();
5706 }
5707 }
5708 }
5709 }
5710 }
5711
5712 void MDCache::dump_openfiles(Formatter *f)
5713 {
5714 f->open_array_section("openfiles");
5715 for (auto p = mds->mdlog->segments.begin();
5716 p != mds->mdlog->segments.end();
5717 ++p) {
5718 LogSegment *ls = p->second;
5719
5720 auto q = ls->open_files.begin(member_offset(CInode, item_open_file));
5721 while (!q.end()) {
5722 CInode *in = *q;
5723 ++q;
5724 if ((in->last == CEPH_NOSNAP && !in->is_any_caps_wanted())
5725 || (in->last != CEPH_NOSNAP && in->client_snap_caps.empty()))
5726 continue;
5727 f->open_object_section("file");
5728 in->dump(f, CInode::DUMP_PATH | CInode::DUMP_INODE_STORE_BASE | CInode::DUMP_CAPS);
5729 f->close_section();
5730 }
5731 }
5732 f->close_section();
5733 }
5734
5735 Capability* MDCache::rejoin_import_cap(CInode *in, client_t client, const cap_reconnect_t& icr, mds_rank_t frommds)
5736 {
5737 dout(10) << "rejoin_import_cap for client." << client << " from mds." << frommds
5738 << " on " << *in << dendl;
5739 Session *session = mds->sessionmap.get_session(entity_name_t::CLIENT(client.v));
5740 if (!session) {
5741 dout(10) << " no session for client." << client << dendl;
5742 return NULL;
5743 }
5744
5745 Capability *cap = in->reconnect_cap(client, icr, session);
5746
5747 if (frommds >= 0) {
5748 if (cap->get_last_seq() == 0) // don't increase mseq if cap already exists
5749 cap->inc_mseq();
5750 do_cap_import(session, in, cap, icr.capinfo.cap_id, 0, 0, frommds, 0);
5751 }
5752
5753 return cap;
5754 }
5755
5756 void MDCache::export_remaining_imported_caps()
5757 {
5758 dout(10) << "export_remaining_imported_caps" << dendl;
5759
5760 CachedStackStringStream css;
5761
5762 int count = 0;
5763 for (auto p = cap_imports.begin(); p != cap_imports.end(); ++p) {
5764 *css << " ino " << p->first << "\n";
5765 for (auto q = p->second.begin(); q != p->second.end(); ++q) {
5766 Session *session = mds->sessionmap.get_session(entity_name_t::CLIENT(q->first.v));
5767 if (session) {
5768 // mark client caps stale.
5769 auto stale = make_message<MClientCaps>(CEPH_CAP_OP_EXPORT, p->first,
5770 0, 0, 0,
5771 mds->get_osd_epoch_barrier());
5772 stale->set_cap_peer(0, 0, 0, -1, 0);
5773 mds->send_message_client_counted(stale, q->first);
5774 }
5775 }
5776
5777 if (!(++count % mds->heartbeat_reset_grace()))
5778 mds->heartbeat_reset();
5779 }
5780
5781 for (map<inodeno_t, MDSContext::vec >::iterator p = cap_reconnect_waiters.begin();
5782 p != cap_reconnect_waiters.end();
5783 ++p)
5784 mds->queue_waiters(p->second);
5785
5786 cap_imports.clear();
5787 cap_reconnect_waiters.clear();
5788
5789 if (css->strv().length()) {
5790 mds->clog->warn() << "failed to reconnect caps for missing inodes:"
5791 << css->strv();
5792 }
5793 }
5794
5795 Capability* MDCache::try_reconnect_cap(CInode *in, Session *session)
5796 {
5797 client_t client = session->info.get_client();
5798 Capability *cap = nullptr;
5799 const cap_reconnect_t *rc = get_replay_cap_reconnect(in->ino(), client);
5800 if (rc) {
5801 cap = in->reconnect_cap(client, *rc, session);
5802 dout(10) << "try_reconnect_cap client." << client
5803 << " reconnect wanted " << ccap_string(rc->capinfo.wanted)
5804 << " issue " << ccap_string(rc->capinfo.issued)
5805 << " on " << *in << dendl;
5806 remove_replay_cap_reconnect(in->ino(), client);
5807
5808 if (in->is_replicated()) {
5809 mds->locker->try_eval(in, CEPH_CAP_LOCKS);
5810 } else {
5811 int dirty_caps = 0;
5812 auto p = reconnected_caps.find(in->ino());
5813 if (p != reconnected_caps.end()) {
5814 auto q = p->second.find(client);
5815 if (q != p->second.end())
5816 dirty_caps = q->second.dirty_caps;
5817 }
5818 in->choose_lock_states(dirty_caps);
5819 dout(15) << " chose lock states on " << *in << dendl;
5820 }
5821
5822 map<inodeno_t, MDSContext::vec >::iterator it =
5823 cap_reconnect_waiters.find(in->ino());
5824 if (it != cap_reconnect_waiters.end()) {
5825 mds->queue_waiters(it->second);
5826 cap_reconnect_waiters.erase(it);
5827 }
5828 }
5829 return cap;
5830 }
5831
5832
5833
5834 // -------
5835 // cap imports and delayed snap parent opens
5836
5837 void MDCache::do_cap_import(Session *session, CInode *in, Capability *cap,
5838 uint64_t p_cap_id, ceph_seq_t p_seq, ceph_seq_t p_mseq,
5839 int peer, int p_flags)
5840 {
5841 SnapRealm *realm = in->find_snaprealm();
5842 dout(10) << "do_cap_import " << session->info.inst.name << " mseq " << cap->get_mseq() << " on " << *in << dendl;
5843 if (cap->get_last_seq() == 0) // reconnected cap
5844 cap->inc_last_seq();
5845 cap->set_last_issue();
5846 cap->set_last_issue_stamp(ceph_clock_now());
5847 cap->clear_new();
5848 auto reap = make_message<MClientCaps>(CEPH_CAP_OP_IMPORT,
5849 in->ino(), realm->inode->ino(), cap->get_cap_id(),
5850 cap->get_last_seq(), cap->pending(), cap->wanted(),
5851 0, cap->get_mseq(), mds->get_osd_epoch_barrier());
5852 in->encode_cap_message(reap, cap);
5853 reap->snapbl = mds->server->get_snap_trace(session, realm);
5854 reap->set_cap_peer(p_cap_id, p_seq, p_mseq, peer, p_flags);
5855 mds->send_message_client_counted(reap, session);
5856 }
5857
5858 void MDCache::do_delayed_cap_imports()
5859 {
5860 dout(10) << "do_delayed_cap_imports" << dendl;
5861
5862 ceph_assert(delayed_imported_caps.empty());
5863 }
5864
5865 struct C_MDC_OpenSnapRealms : public MDCacheContext {
5866 explicit C_MDC_OpenSnapRealms(MDCache *c) : MDCacheContext(c) {}
5867 void finish(int r) override {
5868 mdcache->open_snaprealms();
5869 }
5870 };
5871
5872 void MDCache::open_snaprealms()
5873 {
5874 dout(10) << "open_snaprealms" << dendl;
5875
5876 auto it = rejoin_pending_snaprealms.begin();
5877 while (it != rejoin_pending_snaprealms.end()) {
5878 CInode *in = *it;
5879 SnapRealm *realm = in->snaprealm;
5880 ceph_assert(realm);
5881
5882 map<client_t,ref_t<MClientSnap>> splits;
5883 // finish off client snaprealm reconnects?
5884 auto q = reconnected_snaprealms.find(in->ino());
5885 if (q != reconnected_snaprealms.end()) {
5886 for (const auto& r : q->second)
5887 finish_snaprealm_reconnect(r.first, realm, r.second, splits);
5888 reconnected_snaprealms.erase(q);
5889 }
5890
5891 for (auto p = realm->inodes_with_caps.begin(); !p.end(); ++p) {
5892 CInode *child = *p;
5893 auto q = reconnected_caps.find(child->ino());
5894 ceph_assert(q != reconnected_caps.end());
5895 for (auto r = q->second.begin(); r != q->second.end(); ++r) {
5896 Capability *cap = child->get_client_cap(r->first);
5897 if (!cap)
5898 continue;
5899 if (r->second.snap_follows > 0) {
5900 if (r->second.snap_follows < child->first - 1) {
5901 rebuild_need_snapflush(child, realm, r->first, r->second.snap_follows);
5902 } else if (r->second.snapflush) {
5903 // When processing a cap flush message that is re-sent, it's possble
5904 // that the sender has already released all WR caps. So we should
5905 // force MDCache::cow_inode() to setup CInode::client_need_snapflush.
5906 cap->mark_needsnapflush();
5907 }
5908 }
5909 // make sure client's cap is in the correct snaprealm.
5910 if (r->second.realm_ino != in->ino()) {
5911 prepare_realm_split(realm, r->first, child->ino(), splits);
5912 }
5913 }
5914 }
5915
5916 rejoin_pending_snaprealms.erase(it++);
5917 in->put(CInode::PIN_OPENINGSNAPPARENTS);
5918
5919 send_snaps(splits);
5920 }
5921
5922 notify_global_snaprealm_update(CEPH_SNAP_OP_UPDATE);
5923
5924 if (!reconnected_snaprealms.empty()) {
5925 dout(5) << "open_snaprealms has unconnected snaprealm:" << dendl;
5926 for (auto& p : reconnected_snaprealms) {
5927 CachedStackStringStream css;
5928 *css << " " << p.first << " {";
5929 bool first = true;
5930 for (auto& q : p.second) {
5931 if (!first)
5932 *css << ", ";
5933 *css << "client." << q.first << "/" << q.second;
5934 }
5935 *css << "}";
5936 dout(5) << css->strv() << dendl;
5937 }
5938 }
5939 ceph_assert(rejoin_waiters.empty());
5940 ceph_assert(rejoin_pending_snaprealms.empty());
5941 dout(10) << "open_snaprealms - all open" << dendl;
5942 do_delayed_cap_imports();
5943
5944 ceph_assert(rejoin_done);
5945 rejoin_done.release()->complete(0);
5946 reconnected_caps.clear();
5947 }
5948
5949 bool MDCache::open_undef_inodes_dirfrags()
5950 {
5951 dout(10) << "open_undef_inodes_dirfrags "
5952 << rejoin_undef_inodes.size() << " inodes "
5953 << rejoin_undef_dirfrags.size() << " dirfrags" << dendl;
5954
5955 // dirfrag -> (fetch_complete, keys_to_fetch)
5956 map<CDir*, pair<bool, std::vector<dentry_key_t> > > fetch_queue;
5957 for (auto& dir : rejoin_undef_dirfrags) {
5958 ceph_assert(dir->get_version() == 0);
5959 fetch_queue.emplace(std::piecewise_construct, std::make_tuple(dir), std::make_tuple());
5960 }
5961
5962 if (g_conf().get_val<bool>("mds_dir_prefetch")) {
5963 for (auto& in : rejoin_undef_inodes) {
5964 ceph_assert(!in->is_base());
5965 ceph_assert(in->get_parent_dir());
5966 fetch_queue.emplace(std::piecewise_construct, std::make_tuple(in->get_parent_dir()), std::make_tuple());
5967 }
5968 } else {
5969 for (auto& in : rejoin_undef_inodes) {
5970 assert(!in->is_base());
5971 CDentry *dn = in->get_parent_dn();
5972 auto& p = fetch_queue[dn->get_dir()];
5973
5974 if (dn->last != CEPH_NOSNAP) {
5975 p.first = true;
5976 p.second.clear();
5977 } else if (!p.first) {
5978 p.second.push_back(dn->key());
5979 }
5980 }
5981 }
5982
5983 if (fetch_queue.empty())
5984 return false;
5985
5986 MDSGatherBuilder gather(g_ceph_context,
5987 new MDSInternalContextWrapper(mds,
5988 new LambdaContext([this](int r) {
5989 if (rejoin_gather.empty() && rejoin_ack_gather.count(mds->get_nodeid()))
5990 rejoin_gather_finish();
5991 })
5992 )
5993 );
5994
5995 for (auto& p : fetch_queue) {
5996 CDir *dir = p.first;
5997 CInode *diri = dir->get_inode();
5998 if (diri->state_test(CInode::STATE_REJOINUNDEF))
5999 continue;
6000 if (dir->state_test(CDir::STATE_REJOINUNDEF))
6001 ceph_assert(diri->dirfragtree.is_leaf(dir->get_frag()));
6002 if (p.second.first || p.second.second.empty()) {
6003 dir->fetch(gather.new_sub());
6004 } else {
6005 dir->fetch_keys(p.second.second, gather.new_sub());
6006 }
6007 }
6008 ceph_assert(gather.has_subs());
6009 gather.activate();
6010 return true;
6011 }
6012
6013 void MDCache::opened_undef_inode(CInode *in) {
6014 dout(10) << "opened_undef_inode " << *in << dendl;
6015 rejoin_undef_inodes.erase(in);
6016 if (in->is_dir()) {
6017 // FIXME: re-hash dentries if necessary
6018 ceph_assert(in->get_inode()->dir_layout.dl_dir_hash == g_conf()->mds_default_dir_hash);
6019 if (in->get_num_dirfrags() && !in->dirfragtree.is_leaf(frag_t())) {
6020 CDir *dir = in->get_dirfrag(frag_t());
6021 ceph_assert(dir);
6022 rejoin_undef_dirfrags.erase(dir);
6023 in->force_dirfrags();
6024 auto&& ls = in->get_dirfrags();
6025 for (const auto& dir : ls) {
6026 rejoin_undef_dirfrags.insert(dir);
6027 }
6028 }
6029 }
6030 }
6031
6032 void MDCache::finish_snaprealm_reconnect(client_t client, SnapRealm *realm, snapid_t seq,
6033 map<client_t,ref_t<MClientSnap>>& updates)
6034 {
6035 if (seq < realm->get_newest_seq()) {
6036 dout(10) << "finish_snaprealm_reconnect client." << client << " has old seq " << seq << " < "
6037 << realm->get_newest_seq() << " on " << *realm << dendl;
6038 auto snap = make_message<MClientSnap>(CEPH_SNAP_OP_UPDATE);
6039 snap->bl = mds->server->get_snap_trace(client, realm);
6040 updates.emplace(std::piecewise_construct, std::forward_as_tuple(client), std::forward_as_tuple(snap));
6041 } else {
6042 dout(10) << "finish_snaprealm_reconnect client." << client << " up to date"
6043 << " on " << *realm << dendl;
6044 }
6045 }
6046
6047
6048
6049 void MDCache::rejoin_send_acks()
6050 {
6051 dout(7) << "rejoin_send_acks" << dendl;
6052
6053 // replicate stray
6054 for (map<mds_rank_t, set<CInode*> >::iterator p = rejoin_unlinked_inodes.begin();
6055 p != rejoin_unlinked_inodes.end();
6056 ++p) {
6057 for (set<CInode*>::iterator q = p->second.begin();
6058 q != p->second.end();
6059 ++q) {
6060 CInode *in = *q;
6061 dout(7) << " unlinked inode " << *in << dendl;
6062 // inode expired
6063 if (!in->is_replica(p->first))
6064 continue;
6065 while (1) {
6066 CDentry *dn = in->get_parent_dn();
6067 if (dn->is_replica(p->first))
6068 break;
6069 dn->add_replica(p->first);
6070 CDir *dir = dn->get_dir();
6071 if (dir->is_replica(p->first))
6072 break;
6073 dir->add_replica(p->first);
6074 in = dir->get_inode();
6075 if (in->is_replica(p->first))
6076 break;
6077 in->add_replica(p->first);
6078 if (in->is_base())
6079 break;
6080 }
6081 }
6082 }
6083 rejoin_unlinked_inodes.clear();
6084
6085 // send acks to everyone in the recovery set
6086 map<mds_rank_t,ref_t<MMDSCacheRejoin>> acks;
6087 for (set<mds_rank_t>::iterator p = recovery_set.begin();
6088 p != recovery_set.end();
6089 ++p) {
6090 if (rejoin_ack_sent.count(*p))
6091 continue;
6092 acks[*p] = make_message<MMDSCacheRejoin>(MMDSCacheRejoin::OP_ACK);
6093 }
6094
6095 rejoin_ack_sent = recovery_set;
6096
6097 // walk subtrees
6098 for (map<CDir*,set<CDir*> >::iterator p = subtrees.begin();
6099 p != subtrees.end();
6100 ++p) {
6101 CDir *dir = p->first;
6102 if (!dir->is_auth())
6103 continue;
6104 dout(10) << "subtree " << *dir << dendl;
6105
6106 // auth items in this subtree
6107 std::queue<CDir*> dq;
6108 dq.push(dir);
6109
6110 while (!dq.empty()) {
6111 CDir *dir = dq.front();
6112 dq.pop();
6113
6114 // dir
6115 for (auto &r : dir->get_replicas()) {
6116 auto it = acks.find(r.first);
6117 if (it == acks.end())
6118 continue;
6119 it->second->add_strong_dirfrag(dir->dirfrag(), ++r.second, dir->dir_rep);
6120 it->second->add_dirfrag_base(dir);
6121 }
6122
6123 for (auto &p : dir->items) {
6124 CDentry *dn = p.second;
6125 CDentry::linkage_t *dnl = dn->get_linkage();
6126
6127 // inode
6128 CInode *in = NULL;
6129 if (dnl->is_primary())
6130 in = dnl->get_inode();
6131
6132 // dentry
6133 for (auto &r : dn->get_replicas()) {
6134 auto it = acks.find(r.first);
6135 if (it == acks.end())
6136 continue;
6137 it->second->add_strong_dentry(dir->dirfrag(), dn->get_name(), dn->get_alternate_name(),
6138 dn->first, dn->last,
6139 dnl->is_primary() ? dnl->get_inode()->ino():inodeno_t(0),
6140 dnl->is_remote() ? dnl->get_remote_ino():inodeno_t(0),
6141 dnl->is_remote() ? dnl->get_remote_d_type():0,
6142 ++r.second,
6143 dn->lock.get_replica_state());
6144 // peer missed MDentrylink message ?
6145 if (in && !in->is_replica(r.first))
6146 in->add_replica(r.first);
6147 }
6148
6149 if (!in)
6150 continue;
6151
6152 for (auto &r : in->get_replicas()) {
6153 auto it = acks.find(r.first);
6154 if (it == acks.end())
6155 continue;
6156 it->second->add_inode_base(in, mds->mdsmap->get_up_features());
6157 bufferlist bl;
6158 in->_encode_locks_state_for_rejoin(bl, r.first);
6159 it->second->add_inode_locks(in, ++r.second, bl);
6160 }
6161
6162 // subdirs in this subtree?
6163 {
6164 auto&& dirs = in->get_nested_dirfrags();
6165 for (const auto& dir : dirs) {
6166 dq.push(dir);
6167 }
6168 }
6169 }
6170 }
6171 }
6172
6173 // base inodes too
6174 if (root && root->is_auth())
6175 for (auto &r : root->get_replicas()) {
6176 auto it = acks.find(r.first);
6177 if (it == acks.end())
6178 continue;
6179 it->second->add_inode_base(root, mds->mdsmap->get_up_features());
6180 bufferlist bl;
6181 root->_encode_locks_state_for_rejoin(bl, r.first);
6182 it->second->add_inode_locks(root, ++r.second, bl);
6183 }
6184 if (myin)
6185 for (auto &r : myin->get_replicas()) {
6186 auto it = acks.find(r.first);
6187 if (it == acks.end())
6188 continue;
6189 it->second->add_inode_base(myin, mds->mdsmap->get_up_features());
6190 bufferlist bl;
6191 myin->_encode_locks_state_for_rejoin(bl, r.first);
6192 it->second->add_inode_locks(myin, ++r.second, bl);
6193 }
6194
6195 // include inode base for any inodes whose scatterlocks may have updated
6196 for (set<CInode*>::iterator p = rejoin_potential_updated_scatterlocks.begin();
6197 p != rejoin_potential_updated_scatterlocks.end();
6198 ++p) {
6199 CInode *in = *p;
6200 for (const auto &r : in->get_replicas()) {
6201 auto it = acks.find(r.first);
6202 if (it == acks.end())
6203 continue;
6204 it->second->add_inode_base(in, mds->mdsmap->get_up_features());
6205 }
6206 }
6207
6208 // send acks
6209 for (auto p = acks.begin(); p != acks.end(); ++p) {
6210 encode(rejoin_imported_caps[p->first], p->second->imported_caps);
6211 mds->send_message_mds(p->second, p->first);
6212 }
6213
6214 rejoin_imported_caps.clear();
6215 }
6216
6217 class C_MDC_ReIssueCaps : public MDCacheContext {
6218 CInode *in;
6219 public:
6220 C_MDC_ReIssueCaps(MDCache *mdc, CInode *i) :
6221 MDCacheContext(mdc), in(i)
6222 {
6223 in->get(CInode::PIN_PTRWAITER);
6224 }
6225 void finish(int r) override {
6226 if (!mdcache->mds->locker->eval(in, CEPH_CAP_LOCKS))
6227 mdcache->mds->locker->issue_caps(in);
6228 in->put(CInode::PIN_PTRWAITER);
6229 }
6230 };
6231
6232 void MDCache::reissue_all_caps()
6233 {
6234 dout(10) << "reissue_all_caps" << dendl;
6235
6236 int count = 0;
6237 for (auto &p : inode_map) {
6238 int n = 1;
6239 CInode *in = p.second;
6240 if (in->is_head() && in->is_any_caps()) {
6241 // called by MDSRank::active_start(). There shouldn't be any frozen subtree.
6242 if (in->is_frozen_inode()) {
6243 in->add_waiter(CInode::WAIT_UNFREEZE, new C_MDC_ReIssueCaps(this, in));
6244 continue;
6245 }
6246 if (!mds->locker->eval(in, CEPH_CAP_LOCKS))
6247 n += mds->locker->issue_caps(in);
6248 }
6249
6250 if ((count % mds->heartbeat_reset_grace()) + n >= mds->heartbeat_reset_grace())
6251 mds->heartbeat_reset();
6252 count += n;
6253 }
6254 }
6255
6256
6257 // ===============================================================================
6258
6259 struct C_MDC_QueuedCow : public MDCacheContext {
6260 CInode *in;
6261 MutationRef mut;
6262 C_MDC_QueuedCow(MDCache *mdc, CInode *i, MutationRef& m) :
6263 MDCacheContext(mdc), in(i), mut(m) {}
6264 void finish(int r) override {
6265 mdcache->_queued_file_recover_cow(in, mut);
6266 }
6267 };
6268
6269
6270 void MDCache::queue_file_recover(CInode *in)
6271 {
6272 dout(10) << "queue_file_recover " << *in << dendl;
6273 ceph_assert(in->is_auth());
6274
6275 // cow?
6276 /*
6277 SnapRealm *realm = in->find_snaprealm();
6278 set<snapid_t> s = realm->get_snaps();
6279 while (!s.empty() && *s.begin() < in->first)
6280 s.erase(s.begin());
6281 while (!s.empty() && *s.rbegin() > in->last)
6282 s.erase(*s.rbegin());
6283 dout(10) << " snaps in [" << in->first << "," << in->last << "] are " << s << dendl;
6284 if (s.size() > 1) {
6285 auto pi = in->project_inode(mut);
6286 pi.inode.version = in->pre_dirty();
6287
6288 auto mut(std::make_shared<MutationImpl>());
6289 mut->ls = mds->mdlog->get_current_segment();
6290 EUpdate *le = new EUpdate(mds->mdlog, "queue_file_recover cow");
6291 mds->mdlog->start_entry(le);
6292 predirty_journal_parents(mut, &le->metablob, in, 0, PREDIRTY_PRIMARY);
6293
6294 s.erase(*s.begin());
6295 while (!s.empty()) {
6296 snapid_t snapid = *s.begin();
6297 CInode *cow_inode = 0;
6298 journal_cow_inode(mut, &le->metablob, in, snapid-1, &cow_inode);
6299 ceph_assert(cow_inode);
6300 recovery_queue.enqueue(cow_inode);
6301 s.erase(*s.begin());
6302 }
6303
6304 in->parent->first = in->first;
6305 le->metablob.add_primary_dentry(in->parent, in, true);
6306 mds->mdlog->submit_entry(le, new C_MDC_QueuedCow(this, in, mut));
6307 mds->mdlog->flush();
6308 }
6309 */
6310
6311 recovery_queue.enqueue(in);
6312 }
6313
6314 void MDCache::_queued_file_recover_cow(CInode *in, MutationRef& mut)
6315 {
6316 mut->apply();
6317 mds->locker->drop_locks(mut.get());
6318 mut->cleanup();
6319 }
6320
6321
6322 /*
6323 * called after recovery to recover file sizes for previously opened (for write)
6324 * files. that is, those where max_size > size.
6325 */
6326 void MDCache::identify_files_to_recover()
6327 {
6328 dout(10) << "identify_files_to_recover" << dendl;
6329 int count = 0;
6330
6331 // Clear the recover and check queues in case the monitor sends rejoin mdsmap twice.
6332 rejoin_recover_q.clear();
6333 rejoin_check_q.clear();
6334
6335 for (auto &p : inode_map) {
6336 CInode *in = p.second;
6337 if (!in->is_auth())
6338 continue;
6339
6340 if (in->last != CEPH_NOSNAP)
6341 continue;
6342
6343 // Only normal files need file size recovery
6344 if (!in->is_file()) {
6345 continue;
6346 }
6347
6348 bool recover = false;
6349 const auto& client_ranges = in->get_projected_inode()->client_ranges;
6350 if (!client_ranges.empty()) {
6351 in->mark_clientwriteable();
6352 for (auto& p : client_ranges) {
6353 Capability *cap = in->get_client_cap(p.first);
6354 if (cap) {
6355 cap->mark_clientwriteable();
6356 } else {
6357 dout(10) << " client." << p.first << " has range " << p.second << " but no cap on " << *in << dendl;
6358 recover = true;
6359 break;
6360 }
6361 }
6362 }
6363
6364 if (recover) {
6365 if (in->filelock.is_stable()) {
6366 in->auth_pin(&in->filelock);
6367 } else {
6368 ceph_assert(in->filelock.get_state() == LOCK_XLOCKSNAP);
6369 }
6370 in->filelock.set_state(LOCK_PRE_SCAN);
6371 rejoin_recover_q.push_back(in);
6372 } else {
6373 rejoin_check_q.push_back(in);
6374 }
6375
6376 if (!(++count % mds->heartbeat_reset_grace()))
6377 mds->heartbeat_reset();
6378 }
6379 }
6380
6381 void MDCache::start_files_to_recover()
6382 {
6383 int count = 0;
6384 for (CInode *in : rejoin_check_q) {
6385 if (in->filelock.get_state() == LOCK_XLOCKSNAP)
6386 mds->locker->issue_caps(in);
6387 mds->locker->check_inode_max_size(in);
6388 if (!(++count % mds->heartbeat_reset_grace()))
6389 mds->heartbeat_reset();
6390 }
6391 rejoin_check_q.clear();
6392 for (CInode *in : rejoin_recover_q) {
6393 mds->locker->file_recover(&in->filelock);
6394 if (!(++count % mds->heartbeat_reset_grace()))
6395 mds->heartbeat_reset();
6396 }
6397 if (!rejoin_recover_q.empty()) {
6398 rejoin_recover_q.clear();
6399 do_file_recover();
6400 }
6401 }
6402
6403 void MDCache::do_file_recover()
6404 {
6405 recovery_queue.advance();
6406 }
6407
6408 // ===============================================================================
6409
6410
6411 // ----------------------------
6412 // truncate
6413
6414 class C_MDC_RetryTruncate : public MDCacheContext {
6415 CInode *in;
6416 LogSegment *ls;
6417 public:
6418 C_MDC_RetryTruncate(MDCache *c, CInode *i, LogSegment *l) :
6419 MDCacheContext(c), in(i), ls(l) {}
6420 void finish(int r) override {
6421 mdcache->_truncate_inode(in, ls);
6422 }
6423 };
6424
6425 void MDCache::truncate_inode(CInode *in, LogSegment *ls)
6426 {
6427 const auto& pi = in->get_projected_inode();
6428 dout(10) << "truncate_inode "
6429 << pi->truncate_from << " -> " << pi->truncate_size
6430 << " on " << *in
6431 << dendl;
6432
6433 ls->truncating_inodes.insert(in);
6434 in->get(CInode::PIN_TRUNCATING);
6435 in->auth_pin(this);
6436
6437 if (!in->client_need_snapflush.empty() &&
6438 (in->get_caps_issued() & CEPH_CAP_FILE_BUFFER)) {
6439 ceph_assert(in->filelock.is_xlocked());
6440 in->filelock.set_xlock_snap_sync(new C_MDC_RetryTruncate(this, in, ls));
6441 mds->locker->issue_caps(in);
6442 return;
6443 }
6444
6445 _truncate_inode(in, ls);
6446 }
6447
6448 struct C_IO_MDC_TruncateWriteFinish : public MDCacheIOContext {
6449 CInode *in;
6450 LogSegment *ls;
6451 uint32_t block_size;
6452 C_IO_MDC_TruncateWriteFinish(MDCache *c, CInode *i, LogSegment *l, uint32_t bs) :
6453 MDCacheIOContext(c, false), in(i), ls(l), block_size(bs) {
6454 }
6455 void finish(int r) override {
6456 ceph_assert(r == 0 || r == -CEPHFS_ENOENT);
6457 mdcache->truncate_inode_write_finish(in, ls, block_size);
6458 }
6459 void print(ostream& out) const override {
6460 out << "file_truncate_write(" << in->ino() << ")";
6461 }
6462 };
6463
6464 struct C_IO_MDC_TruncateFinish : public MDCacheIOContext {
6465 CInode *in;
6466 LogSegment *ls;
6467 C_IO_MDC_TruncateFinish(MDCache *c, CInode *i, LogSegment *l) :
6468 MDCacheIOContext(c, false), in(i), ls(l) {
6469 }
6470 void finish(int r) override {
6471 ceph_assert(r == 0 || r == -CEPHFS_ENOENT);
6472 mdcache->truncate_inode_finish(in, ls);
6473 }
6474 void print(ostream& out) const override {
6475 out << "file_truncate(" << in->ino() << ")";
6476 }
6477 };
6478
6479 void MDCache::_truncate_inode(CInode *in, LogSegment *ls)
6480 {
6481 const auto& pi = in->get_inode();
6482 dout(10) << "_truncate_inode "
6483 << pi->truncate_from << " -> " << pi->truncate_size
6484 << " fscrypt last block length is " << pi->fscrypt_last_block.length()
6485 << " on " << *in << dendl;
6486
6487 ceph_assert(pi->is_truncating());
6488 ceph_assert(pi->truncate_size < (1ULL << 63));
6489 ceph_assert(pi->truncate_from < (1ULL << 63));
6490 ceph_assert(pi->truncate_size < pi->truncate_from ||
6491 (pi->truncate_size == pi->truncate_from &&
6492 pi->fscrypt_last_block.length()));
6493
6494
6495 SnapRealm *realm = in->find_snaprealm();
6496 SnapContext nullsnap;
6497 const SnapContext *snapc;
6498 if (realm) {
6499 dout(10) << " realm " << *realm << dendl;
6500 snapc = &realm->get_snap_context();
6501 } else {
6502 dout(10) << " NO realm, using null context" << dendl;
6503 snapc = &nullsnap;
6504 ceph_assert(in->last == CEPH_NOSNAP);
6505 }
6506 dout(10) << "_truncate_inode snapc " << snapc << " on " << *in
6507 << " fscrypt_last_block length is " << pi->fscrypt_last_block.length()
6508 << dendl;
6509 auto layout = pi->layout;
6510 struct ceph_fscrypt_last_block_header header;
6511 memset(&header, 0, sizeof(header));
6512 bufferlist data;
6513 if (pi->fscrypt_last_block.length()) {
6514 auto bl = pi->fscrypt_last_block.cbegin();
6515 DECODE_START(1, bl);
6516 decode(header.change_attr, bl);
6517 decode(header.file_offset, bl);
6518 decode(header.block_size, bl);
6519
6520 /*
6521 * The block_size will be in unit of KB, so if the last block is not
6522 * located in a file hole, the struct_len should be larger than the
6523 * header.block_size.
6524 */
6525 if (struct_len > header.block_size) {
6526 bl.copy(header.block_size, data);
6527 }
6528 DECODE_FINISH(bl);
6529 }
6530
6531 if (data.length()) {
6532 dout(10) << "_truncate_inode write on inode " << *in << " change_attr: "
6533 << header.change_attr << " offset: " << header.file_offset << " blen: "
6534 << header.block_size << dendl;
6535 filer.write(in->ino(), &layout, *snapc, header.file_offset, header.block_size,
6536 data, ceph::real_time::min(), 0,
6537 new C_OnFinisher(new C_IO_MDC_TruncateWriteFinish(this, in, ls,
6538 header.block_size),
6539 mds->finisher));
6540 } else { // located in file hole.
6541 uint64_t length = pi->truncate_from - pi->truncate_size;
6542
6543 /*
6544 * When the fscrypt is enabled the truncate_from and truncate_size
6545 * possibly equal and both are aligned up to header.block_size. In
6546 * this case we will always request a larger length to make sure the
6547 * OSD won't miss truncating the last object.
6548 */
6549 if (pi->fscrypt_last_block.length()) {
6550 dout(10) << "_truncate_inode truncate on inode " << *in << " hits a hole!" << dendl;
6551 length += header.block_size;
6552 }
6553 ceph_assert(length);
6554
6555 dout(10) << "_truncate_inode truncate on inode " << *in << dendl;
6556 filer.truncate(in->ino(), &layout, *snapc, pi->truncate_size, length,
6557 pi->truncate_seq, ceph::real_time::min(), 0,
6558 new C_OnFinisher(new C_IO_MDC_TruncateFinish(this, in, ls),
6559 mds->finisher));
6560 }
6561
6562 }
6563
6564 struct C_MDC_TruncateLogged : public MDCacheLogContext {
6565 CInode *in;
6566 MutationRef mut;
6567 C_MDC_TruncateLogged(MDCache *m, CInode *i, MutationRef& mu) :
6568 MDCacheLogContext(m), in(i), mut(mu) {}
6569 void finish(int r) override {
6570 mdcache->truncate_inode_logged(in, mut);
6571 }
6572 };
6573
6574 void MDCache::truncate_inode_write_finish(CInode *in, LogSegment *ls,
6575 uint32_t block_size)
6576 {
6577 const auto& pi = in->get_inode();
6578 dout(10) << "_truncate_inode_write "
6579 << pi->truncate_from << " -> " << pi->truncate_size
6580 << " on " << *in << dendl;
6581
6582 ceph_assert(pi->is_truncating());
6583 ceph_assert(pi->truncate_size < (1ULL << 63));
6584 ceph_assert(pi->truncate_from < (1ULL << 63));
6585 ceph_assert(pi->truncate_size < pi->truncate_from ||
6586 (pi->truncate_size == pi->truncate_from &&
6587 pi->fscrypt_last_block.length()));
6588
6589
6590 SnapRealm *realm = in->find_snaprealm();
6591 SnapContext nullsnap;
6592 const SnapContext *snapc;
6593 if (realm) {
6594 dout(10) << " realm " << *realm << dendl;
6595 snapc = &realm->get_snap_context();
6596 } else {
6597 dout(10) << " NO realm, using null context" << dendl;
6598 snapc = &nullsnap;
6599 ceph_assert(in->last == CEPH_NOSNAP);
6600 }
6601 dout(10) << "_truncate_inode_write snapc " << snapc << " on " << *in
6602 << " fscrypt_last_block length is " << pi->fscrypt_last_block.length()
6603 << dendl;
6604 auto layout = pi->layout;
6605 /*
6606 * When the fscrypt is enabled the truncate_from and truncate_size
6607 * possibly equal and both are aligned up to header.block_size. In
6608 * this case we will always request a larger length to make sure the
6609 * OSD won't miss truncating the last object.
6610 */
6611 uint64_t length = pi->truncate_from - pi->truncate_size + block_size;
6612 filer.truncate(in->ino(), &layout, *snapc, pi->truncate_size, length,
6613 pi->truncate_seq, ceph::real_time::min(), 0,
6614 new C_OnFinisher(new C_IO_MDC_TruncateFinish(this, in, ls),
6615 mds->finisher));
6616 }
6617
6618 void MDCache::truncate_inode_finish(CInode *in, LogSegment *ls)
6619 {
6620 dout(10) << "truncate_inode_finish " << *in << dendl;
6621
6622 set<CInode*>::iterator p = ls->truncating_inodes.find(in);
6623 ceph_assert(p != ls->truncating_inodes.end());
6624 ls->truncating_inodes.erase(p);
6625
6626 MutationRef mut(new MutationImpl());
6627 mut->ls = mds->mdlog->get_current_segment();
6628
6629 // update
6630 auto pi = in->project_inode(mut);
6631 pi.inode->version = in->pre_dirty();
6632 pi.inode->truncate_from = 0;
6633 pi.inode->truncate_pending--;
6634 pi.inode->fscrypt_last_block = bufferlist();
6635
6636 EUpdate *le = new EUpdate(mds->mdlog, "truncate finish");
6637 mds->mdlog->start_entry(le);
6638
6639 predirty_journal_parents(mut, &le->metablob, in, 0, PREDIRTY_PRIMARY);
6640 journal_dirty_inode(mut.get(), &le->metablob, in);
6641 le->metablob.add_truncate_finish(in->ino(), ls->seq);
6642 mds->mdlog->submit_entry(le, new C_MDC_TruncateLogged(this, in, mut));
6643
6644 // flush immediately if there are readers/writers waiting
6645 if (in->is_waiter_for(CInode::WAIT_TRUNC) ||
6646 (in->get_caps_wanted() & (CEPH_CAP_FILE_RD|CEPH_CAP_FILE_WR)))
6647 mds->mdlog->flush();
6648 }
6649
6650 void MDCache::truncate_inode_logged(CInode *in, MutationRef& mut)
6651 {
6652 dout(10) << "truncate_inode_logged " << *in << dendl;
6653 mut->apply();
6654 mds->locker->drop_locks(mut.get());
6655 mut->cleanup();
6656
6657 in->put(CInode::PIN_TRUNCATING);
6658 in->auth_unpin(this);
6659
6660 MDSContext::vec waiters;
6661 in->take_waiting(CInode::WAIT_TRUNC, waiters);
6662 mds->queue_waiters(waiters);
6663 }
6664
6665
6666 void MDCache::add_recovered_truncate(CInode *in, LogSegment *ls)
6667 {
6668 dout(20) << "add_recovered_truncate " << *in << " in log segment "
6669 << ls->seq << "/" << ls->offset << dendl;
6670 ls->truncating_inodes.insert(in);
6671 in->get(CInode::PIN_TRUNCATING);
6672 }
6673
6674 void MDCache::remove_recovered_truncate(CInode *in, LogSegment *ls)
6675 {
6676 dout(20) << "remove_recovered_truncate " << *in << " in log segment "
6677 << ls->seq << "/" << ls->offset << dendl;
6678 // if we have the logseg the truncate started in, it must be in our list.
6679 set<CInode*>::iterator p = ls->truncating_inodes.find(in);
6680 ceph_assert(p != ls->truncating_inodes.end());
6681 ls->truncating_inodes.erase(p);
6682 in->put(CInode::PIN_TRUNCATING);
6683 }
6684
6685 void MDCache::start_recovered_truncates()
6686 {
6687 dout(10) << "start_recovered_truncates" << dendl;
6688 for (map<uint64_t,LogSegment*>::iterator p = mds->mdlog->segments.begin();
6689 p != mds->mdlog->segments.end();
6690 ++p) {
6691 LogSegment *ls = p->second;
6692 for (set<CInode*>::iterator q = ls->truncating_inodes.begin();
6693 q != ls->truncating_inodes.end();
6694 ++q) {
6695 CInode *in = *q;
6696 in->auth_pin(this);
6697
6698 if (!in->client_need_snapflush.empty() &&
6699 (in->get_caps_issued() & CEPH_CAP_FILE_BUFFER)) {
6700 ceph_assert(in->filelock.is_stable());
6701 in->filelock.set_state(LOCK_XLOCKDONE);
6702 in->auth_pin(&in->filelock);
6703 in->filelock.set_xlock_snap_sync(new C_MDC_RetryTruncate(this, in, ls));
6704 // start_files_to_recover will revoke caps
6705 continue;
6706 }
6707 _truncate_inode(in, ls);
6708 }
6709 }
6710 }
6711
6712
6713 class C_MDS_purge_completed_finish : public MDCacheLogContext {
6714 interval_set<inodeno_t> inos;
6715 LogSegment *ls;
6716 version_t inotablev;
6717 public:
6718 C_MDS_purge_completed_finish(MDCache *m, const interval_set<inodeno_t>& _inos,
6719 LogSegment *_ls, version_t iv)
6720 : MDCacheLogContext(m), inos(_inos), ls(_ls), inotablev(iv) {}
6721 void finish(int r) override {
6722 ceph_assert(r == 0);
6723 if (inotablev) {
6724 get_mds()->inotable->apply_release_ids(inos);
6725 ceph_assert(get_mds()->inotable->get_version() == inotablev);
6726 }
6727 ls->purge_inodes_finish(inos);
6728 }
6729 };
6730
6731 void MDCache::start_purge_inodes(){
6732 dout(10) << "start_purge_inodes" << dendl;
6733 for (auto& p : mds->mdlog->segments){
6734 LogSegment *ls = p.second;
6735 if (ls->purging_inodes.size()){
6736 purge_inodes(ls->purging_inodes, ls);
6737 }
6738 }
6739 }
6740
6741 void MDCache::purge_inodes(const interval_set<inodeno_t>& inos, LogSegment *ls)
6742 {
6743 dout(10) << __func__ << " purging inos " << inos << " logseg " << ls->seq << dendl;
6744 // FIXME: handle non-default data pool and namespace
6745
6746 auto cb = new LambdaContext([this, inos, ls](int r){
6747 ceph_assert(r == 0 || r == -2);
6748 mds->inotable->project_release_ids(inos);
6749 version_t piv = mds->inotable->get_projected_version();
6750 ceph_assert(piv != 0);
6751 mds->mdlog->start_submit_entry(new EPurged(inos, ls->seq, piv),
6752 new C_MDS_purge_completed_finish(this, inos, ls, piv));
6753 mds->mdlog->flush();
6754 });
6755
6756 C_GatherBuilder gather(g_ceph_context,
6757 new C_OnFinisher(new MDSIOContextWrapper(mds, cb), mds->finisher));
6758 SnapContext nullsnapc;
6759 for (const auto& [start, len] : inos) {
6760 for (auto i = start; i < start + len ; i += 1) {
6761 filer.purge_range(i, &default_file_layout, nullsnapc, 0, 1,
6762 ceph::real_clock::now(), 0, gather.new_sub());
6763 }
6764 }
6765 gather.activate();
6766 }
6767
6768 // ================================================================================
6769 // cache trimming
6770
6771 std::pair<bool, uint64_t> MDCache::trim_lru(uint64_t count, expiremap& expiremap)
6772 {
6773 bool is_standby_replay = mds->is_standby_replay();
6774 std::vector<CDentry *> unexpirables;
6775 uint64_t trimmed = 0;
6776
6777 auto trim_threshold = g_conf().get_val<Option::size_t>("mds_cache_trim_threshold");
6778
6779 dout(7) << "trim_lru trimming " << count
6780 << " items from LRU"
6781 << " size=" << lru.lru_get_size()
6782 << " mid=" << lru.lru_get_top()
6783 << " pintail=" << lru.lru_get_pintail()
6784 << " pinned=" << lru.lru_get_num_pinned()
6785 << dendl;
6786
6787 const uint64_t trim_counter_start = trim_counter.get();
6788 bool throttled = false;
6789 while (1) {
6790 throttled |= trim_counter_start+trimmed >= trim_threshold;
6791 if (throttled) break;
6792 CDentry *dn = static_cast<CDentry*>(bottom_lru.lru_expire());
6793 if (!dn)
6794 break;
6795 if (trim_dentry(dn, expiremap)) {
6796 unexpirables.push_back(dn);
6797 } else {
6798 trimmed++;
6799 }
6800 }
6801
6802 for (auto &dn : unexpirables) {
6803 bottom_lru.lru_insert_mid(dn);
6804 }
6805 unexpirables.clear();
6806
6807 // trim dentries from the LRU until count is reached
6808 // if mds is in standby_replay and skip trimming the inodes
6809 while (!throttled && (cache_toofull() || count > 0 || is_standby_replay)) {
6810 throttled |= trim_counter_start+trimmed >= trim_threshold;
6811 if (throttled) break;
6812 CDentry *dn = static_cast<CDentry*>(lru.lru_expire());
6813 if (!dn) {
6814 break;
6815 }
6816 if (is_standby_replay && dn->get_linkage()->inode) {
6817 // we move the inodes that need to be trimmed to the end of the lru queue.
6818 // refer to MDCache::standby_trim_segment
6819 lru.lru_insert_bot(dn);
6820 break;
6821 } else if (trim_dentry(dn, expiremap)) {
6822 unexpirables.push_back(dn);
6823 } else {
6824 trimmed++;
6825 if (count > 0) count--;
6826 }
6827 }
6828 trim_counter.hit(trimmed);
6829
6830 for (auto &dn : unexpirables) {
6831 lru.lru_insert_mid(dn);
6832 }
6833 unexpirables.clear();
6834
6835 dout(7) << "trim_lru trimmed " << trimmed << " items" << dendl;
6836 return std::pair<bool, uint64_t>(throttled, trimmed);
6837 }
6838
6839 /*
6840 * note: only called while MDS is active or stopping... NOT during recovery.
6841 * however, we may expire a replica whose authority is recovering.
6842 *
6843 * @param count is number of dentries to try to expire
6844 */
6845 std::pair<bool, uint64_t> MDCache::trim(uint64_t count)
6846 {
6847 uint64_t used = cache_size();
6848 uint64_t limit = cache_memory_limit;
6849 expiremap expiremap;
6850
6851 dout(7) << "trim bytes_used=" << bytes2str(used)
6852 << " limit=" << bytes2str(limit)
6853 << " reservation=" << cache_reservation
6854 << "% count=" << count << dendl;
6855
6856 // process delayed eval_stray()
6857 stray_manager.advance_delayed();
6858
6859 auto result = trim_lru(count, expiremap);
6860 auto& trimmed = result.second;
6861
6862 // trim non-auth, non-bound subtrees
6863 for (auto p = subtrees.begin(); p != subtrees.end();) {
6864 CDir *dir = p->first;
6865 ++p;
6866 CInode *diri = dir->get_inode();
6867 if (dir->is_auth()) {
6868 if (diri->is_auth() && !diri->is_base()) {
6869 /* this situation should correspond to an export pin */
6870 if (dir->get_num_head_items() == 0 && dir->get_num_ref() == 1) {
6871 /* pinned empty subtree, try to drop */
6872 if (dir->state_test(CDir::STATE_AUXSUBTREE)) {
6873 dout(20) << "trimming empty pinned subtree " << *dir << dendl;
6874 dir->state_clear(CDir::STATE_AUXSUBTREE);
6875 remove_subtree(dir);
6876 diri->close_dirfrag(dir->dirfrag().frag);
6877 }
6878 }
6879 } else if (!diri->is_auth() && !diri->is_base() && dir->get_num_head_items() == 0) {
6880 if (dir->state_test(CDir::STATE_EXPORTING) ||
6881 !(mds->is_active() || mds->is_stopping()) ||
6882 dir->is_freezing() || dir->is_frozen())
6883 continue;
6884
6885 migrator->export_empty_import(dir);
6886 ++trimmed;
6887 }
6888 } else if (!diri->is_auth() && dir->get_num_ref() <= 1) {
6889 // only subtree pin
6890 if (diri->get_num_ref() > diri->get_num_subtree_roots()) {
6891 continue;
6892 }
6893
6894 // don't trim subtree root if its auth MDS is recovering.
6895 // This simplify the cache rejoin code.
6896 if (dir->is_subtree_root() && rejoin_ack_gather.count(dir->get_dir_auth().first))
6897 continue;
6898 trim_dirfrag(dir, 0, expiremap);
6899 ++trimmed;
6900 }
6901 }
6902
6903 // trim root?
6904 if (mds->is_stopping() && root) {
6905 auto&& ls = root->get_dirfrags();
6906 for (const auto& dir : ls) {
6907 if (dir->get_num_ref() == 1) { // subtree pin
6908 trim_dirfrag(dir, 0, expiremap);
6909 ++trimmed;
6910 }
6911 }
6912 if (root->get_num_ref() == 0) {
6913 trim_inode(0, root, 0, expiremap);
6914 ++trimmed;
6915 }
6916 }
6917
6918 std::set<mds_rank_t> stopping;
6919 mds->mdsmap->get_mds_set(stopping, MDSMap::STATE_STOPPING);
6920 stopping.erase(mds->get_nodeid());
6921 for (auto rank : stopping) {
6922 CInode* mdsdir_in = get_inode(MDS_INO_MDSDIR(rank));
6923 if (!mdsdir_in)
6924 continue;
6925
6926 auto em = expiremap.emplace(std::piecewise_construct, std::forward_as_tuple(rank), std::forward_as_tuple());
6927 if (em.second) {
6928 em.first->second = make_message<MCacheExpire>(mds->get_nodeid());
6929 }
6930
6931 dout(20) << __func__ << ": try expiring " << *mdsdir_in << " for stopping mds." << mds->get_nodeid() << dendl;
6932
6933 const bool aborted = expire_recursive(mdsdir_in, expiremap);
6934 if (!aborted) {
6935 dout(20) << __func__ << ": successfully expired mdsdir" << dendl;
6936 auto&& ls = mdsdir_in->get_dirfrags();
6937 for (auto dir : ls) {
6938 if (dir->get_num_ref() == 1) { // subtree pin
6939 trim_dirfrag(dir, dir, expiremap);
6940 ++trimmed;
6941 }
6942 }
6943 if (mdsdir_in->get_num_ref() == 0) {
6944 trim_inode(NULL, mdsdir_in, NULL, expiremap);
6945 ++trimmed;
6946 }
6947 } else {
6948 dout(20) << __func__ << ": some unexpirable contents in mdsdir" << dendl;
6949 }
6950 }
6951
6952 // Other rank's base inodes (when I'm stopping)
6953 if (mds->is_stopping()) {
6954 for (set<CInode*>::iterator p = base_inodes.begin();
6955 p != base_inodes.end();) {
6956 CInode *base_in = *p;
6957 ++p;
6958 if (MDS_INO_IS_MDSDIR(base_in->ino()) &&
6959 MDS_INO_MDSDIR_OWNER(base_in->ino()) != mds->get_nodeid()) {
6960 dout(20) << __func__ << ": maybe trimming base: " << *base_in << dendl;
6961 if (base_in->get_num_ref() == 0) {
6962 trim_inode(NULL, base_in, NULL, expiremap);
6963 ++trimmed;
6964 }
6965 }
6966 }
6967 }
6968
6969 // send any expire messages
6970 send_expire_messages(expiremap);
6971
6972 return result;
6973 }
6974
6975 void MDCache::send_expire_messages(expiremap& expiremap)
6976 {
6977 // send expires
6978 for (const auto &p : expiremap) {
6979 if (mds->is_cluster_degraded() &&
6980 (mds->mdsmap->get_state(p.first) < MDSMap::STATE_REJOIN ||
6981 (mds->mdsmap->get_state(p.first) == MDSMap::STATE_REJOIN &&
6982 rejoin_sent.count(p.first) == 0))) {
6983 continue;
6984 }
6985 dout(7) << "sending cache_expire to " << p.first << dendl;
6986 mds->send_message_mds(p.second, p.first);
6987 }
6988 expiremap.clear();
6989 }
6990
6991
6992 bool MDCache::trim_dentry(CDentry *dn, expiremap& expiremap)
6993 {
6994 dout(12) << "trim_dentry " << *dn << dendl;
6995
6996 CDentry::linkage_t *dnl = dn->get_linkage();
6997
6998 CDir *dir = dn->get_dir();
6999 ceph_assert(dir);
7000
7001 CDir *con = get_subtree_root(dir);
7002 if (con)
7003 dout(12) << " in container " << *con << dendl;
7004 else {
7005 dout(12) << " no container; under a not-yet-linked dir" << dendl;
7006 ceph_assert(dn->is_auth());
7007 }
7008
7009 // If replica dentry is not readable, it's likely we will receive
7010 // MDentryLink/MDentryUnlink message soon (It's possible we first
7011 // receive a MDentryUnlink message, then MDentryLink message)
7012 // MDentryLink message only replicates an inode, so we should
7013 // avoid trimming the inode's parent dentry. This is because that
7014 // unconnected replicas are problematic for subtree migration.
7015 if (!dn->is_auth() && !dn->lock.can_read(-1) &&
7016 !dn->get_dir()->get_inode()->is_stray())
7017 return true;
7018
7019 // adjust the dir state
7020 // NOTE: we can safely remove a clean, null dentry without effecting
7021 // directory completeness.
7022 // (check this _before_ we unlink the inode, below!)
7023 bool clear_complete = false;
7024 if (dn->is_auth() && !(dnl->is_null() && dn->is_clean()))
7025 clear_complete = true;
7026
7027 // unlink the dentry
7028 if (dnl->is_remote()) {
7029 // just unlink.
7030 dir->unlink_inode(dn, false);
7031 } else if (dnl->is_primary()) {
7032 // expire the inode, too.
7033 CInode *in = dnl->get_inode();
7034 ceph_assert(in);
7035 if (trim_inode(dn, in, con, expiremap))
7036 return true; // purging stray instead of trimming
7037 } else {
7038 ceph_assert(dnl->is_null());
7039 }
7040
7041 if (!dn->is_auth()) {
7042 // notify dentry authority.
7043 mds_authority_t auth = dn->authority();
7044
7045 for (int p=0; p<2; p++) {
7046 mds_rank_t a = auth.first;
7047 if (p) a = auth.second;
7048 if (a < 0 || (p == 1 && auth.second == auth.first)) break;
7049 if (mds->get_nodeid() == auth.second &&
7050 con->is_importing()) break; // don't send any expire while importing.
7051 if (a == mds->get_nodeid()) continue; // on export, ignore myself.
7052
7053 dout(12) << " sending expire to mds." << a << " on " << *dn << dendl;
7054 ceph_assert(a != mds->get_nodeid());
7055 auto em = expiremap.emplace(std::piecewise_construct, std::forward_as_tuple(a), std::forward_as_tuple());
7056 if (em.second)
7057 em.first->second = make_message<MCacheExpire>(mds->get_nodeid());
7058 em.first->second->add_dentry(con->dirfrag(), dir->dirfrag(), dn->get_name(), dn->last, dn->get_replica_nonce());
7059 }
7060 }
7061
7062 if (clear_complete) {
7063 if (dn->last == CEPH_NOSNAP)
7064 dir->add_to_bloom(dn);
7065 dir->state_clear(CDir::STATE_COMPLETE);
7066 }
7067
7068 // remove dentry
7069 dir->remove_dentry(dn);
7070
7071 if (mds->logger) mds->logger->inc(l_mds_inodes_expired);
7072 return false;
7073 }
7074
7075
7076 void MDCache::trim_dirfrag(CDir *dir, CDir *con, expiremap& expiremap)
7077 {
7078 dout(15) << "trim_dirfrag " << *dir << dendl;
7079
7080 if (dir->is_subtree_root()) {
7081 ceph_assert(!dir->is_auth() ||
7082 (!dir->is_replicated() && dir->inode->is_base()));
7083 remove_subtree(dir); // remove from subtree map
7084 }
7085 ceph_assert(dir->get_num_ref() == 0);
7086
7087 CInode *in = dir->get_inode();
7088
7089 if (!dir->is_auth()) {
7090 mds_authority_t auth = dir->authority();
7091
7092 // was this an auth delegation? (if so, slightly modified container)
7093 dirfrag_t condf;
7094 if (dir->is_subtree_root()) {
7095 dout(12) << " subtree root, container is " << *dir << dendl;
7096 con = dir;
7097 condf = dir->dirfrag();
7098 } else {
7099 condf = con->dirfrag();
7100 }
7101
7102 for (int p=0; p<2; p++) {
7103 mds_rank_t a = auth.first;
7104 if (p) a = auth.second;
7105 if (a < 0 || (p == 1 && auth.second == auth.first)) break;
7106 if (mds->get_nodeid() == auth.second &&
7107 con->is_importing()) break; // don't send any expire while importing.
7108 if (a == mds->get_nodeid()) continue; // on export, ignore myself.
7109
7110 dout(12) << " sending expire to mds." << a << " on " << *dir << dendl;
7111 ceph_assert(a != mds->get_nodeid());
7112 auto em = expiremap.emplace(std::piecewise_construct, std::forward_as_tuple(a), std::forward_as_tuple());
7113 if (em.second)
7114 em.first->second = make_message<MCacheExpire>(mds->get_nodeid()); /* new */
7115 em.first->second->add_dir(condf, dir->dirfrag(), dir->replica_nonce);
7116 }
7117 }
7118
7119 in->close_dirfrag(dir->dirfrag().frag);
7120 }
7121
7122 /**
7123 * Try trimming an inode from the cache
7124 *
7125 * @return true if the inode is still in cache, else false if it was trimmed
7126 */
7127 bool MDCache::trim_inode(CDentry *dn, CInode *in, CDir *con, expiremap& expiremap)
7128 {
7129 dout(15) << "trim_inode " << *in << dendl;
7130 ceph_assert(in->get_num_ref() == 0);
7131
7132 if (in->is_dir()) {
7133 // If replica inode's dirfragtreelock is not readable, it's likely
7134 // some dirfrags of the inode are being fragmented and we will receive
7135 // MMDSFragmentNotify soon. MMDSFragmentNotify only replicates the new
7136 // dirfrags, so we should avoid trimming these dirfrags' parent inode.
7137 // This is because that unconnected replicas are problematic for
7138 // subtree migration.
7139 //
7140 if (!in->is_auth() && !mds->locker->rdlock_try(&in->dirfragtreelock, -1)) {
7141 return true;
7142 }
7143
7144 // DIR
7145 auto&& dfls = in->get_dirfrags();
7146 for (const auto& dir : dfls) {
7147 ceph_assert(!dir->is_subtree_root());
7148 trim_dirfrag(dir, con ? con:dir, expiremap); // if no container (e.g. root dirfrag), use *p
7149 }
7150 }
7151
7152 // INODE
7153 if (in->is_auth()) {
7154 // eval stray after closing dirfrags
7155 if (dn && !dn->state_test(CDentry::STATE_PURGING)) {
7156 maybe_eval_stray(in);
7157 if (dn->state_test(CDentry::STATE_PURGING) || dn->get_num_ref() > 0)
7158 return true;
7159 }
7160 } else {
7161 mds_authority_t auth = in->authority();
7162
7163 dirfrag_t df;
7164 if (con)
7165 df = con->dirfrag();
7166 else
7167 df = dirfrag_t(0,frag_t()); // must be a root or stray inode.
7168
7169 for (int p=0; p<2; p++) {
7170 mds_rank_t a = auth.first;
7171 if (p) a = auth.second;
7172 if (a < 0 || (p == 1 && auth.second == auth.first)) break;
7173 if (con && mds->get_nodeid() == auth.second &&
7174 con->is_importing()) break; // don't send any expire while importing.
7175 if (a == mds->get_nodeid()) continue; // on export, ignore myself.
7176
7177 dout(12) << " sending expire to mds." << a << " on " << *in << dendl;
7178 ceph_assert(a != mds->get_nodeid());
7179 auto em = expiremap.emplace(std::piecewise_construct, std::forward_as_tuple(a), std::forward_as_tuple());
7180 if (em.second)
7181 em.first->second = make_message<MCacheExpire>(mds->get_nodeid()); /* new */
7182 em.first->second->add_inode(df, in->vino(), in->get_replica_nonce());
7183 }
7184 }
7185
7186 /*
7187 if (in->is_auth()) {
7188 if (in->hack_accessed)
7189 mds->logger->inc("outt");
7190 else {
7191 mds->logger->inc("outut");
7192 mds->logger->fset("oututl", ceph_clock_now() - in->hack_load_stamp);
7193 }
7194 }
7195 */
7196
7197 // unlink
7198 if (dn)
7199 dn->get_dir()->unlink_inode(dn, false);
7200 remove_inode(in);
7201 return false;
7202 }
7203
7204
7205 /**
7206 * trim_non_auth - remove any non-auth items from our cache
7207 *
7208 * this reduces the amount of non-auth metadata in our cache, reducing the
7209 * load incurred by the rejoin phase.
7210 *
7211 * the only non-auth items that remain are those that are needed to
7212 * attach our own subtrees to the root.
7213 *
7214 * when we are done, all dentries will be in the top bit of the lru.
7215 *
7216 * why we have to do this:
7217 * we may not have accurate linkage for non-auth items. which means we will
7218 * know which subtree it falls into, and can not be sure to declare it to the
7219 * correct authority.
7220 */
7221 void MDCache::trim_non_auth()
7222 {
7223 dout(7) << "trim_non_auth" << dendl;
7224
7225 // temporarily pin all subtree roots
7226 for (map<CDir*, set<CDir*> >::iterator p = subtrees.begin();
7227 p != subtrees.end();
7228 ++p)
7229 p->first->get(CDir::PIN_SUBTREETEMP);
7230
7231 list<CDentry*> auth_list;
7232
7233 // trim non-auth items from the lru
7234 for (;;) {
7235 CDentry *dn = NULL;
7236 if (bottom_lru.lru_get_size() > 0)
7237 dn = static_cast<CDentry*>(bottom_lru.lru_expire());
7238 if (!dn && lru.lru_get_size() > 0)
7239 dn = static_cast<CDentry*>(lru.lru_expire());
7240 if (!dn)
7241 break;
7242
7243 CDentry::linkage_t *dnl = dn->get_linkage();
7244
7245 if (dn->is_auth()) {
7246 // add back into lru (at the top)
7247 auth_list.push_back(dn);
7248
7249 if (dnl->is_remote() && dnl->get_inode() && !dnl->get_inode()->is_auth())
7250 dn->unlink_remote(dnl);
7251 } else {
7252 // non-auth. expire.
7253 CDir *dir = dn->get_dir();
7254 ceph_assert(dir);
7255
7256 // unlink the dentry
7257 dout(10) << " removing " << *dn << dendl;
7258 if (dnl->is_remote()) {
7259 dir->unlink_inode(dn, false);
7260 }
7261 else if (dnl->is_primary()) {
7262 CInode *in = dnl->get_inode();
7263 dout(10) << " removing " << *in << dendl;
7264 auto&& ls = in->get_dirfrags();
7265 for (const auto& subdir : ls) {
7266 ceph_assert(!subdir->is_subtree_root());
7267 in->close_dirfrag(subdir->dirfrag().frag);
7268 }
7269 dir->unlink_inode(dn, false);
7270 remove_inode(in);
7271 }
7272 else {
7273 ceph_assert(dnl->is_null());
7274 }
7275
7276 ceph_assert(!dir->has_bloom());
7277 dir->remove_dentry(dn);
7278 // adjust the dir state
7279 dir->state_clear(CDir::STATE_COMPLETE); // dir incomplete!
7280 // close empty non-auth dirfrag
7281 if (!dir->is_subtree_root() && dir->get_num_any() == 0)
7282 dir->inode->close_dirfrag(dir->get_frag());
7283 }
7284 }
7285
7286 for (const auto& dn : auth_list) {
7287 if (dn->state_test(CDentry::STATE_BOTTOMLRU))
7288 bottom_lru.lru_insert_mid(dn);
7289 else
7290 lru.lru_insert_top(dn);
7291 }
7292
7293 // move everything in the pintail to the top bit of the lru.
7294 lru.lru_touch_entire_pintail();
7295
7296 // unpin all subtrees
7297 for (map<CDir*, set<CDir*> >::iterator p = subtrees.begin();
7298 p != subtrees.end();
7299 ++p)
7300 p->first->put(CDir::PIN_SUBTREETEMP);
7301
7302 if (lru.lru_get_size() == 0 &&
7303 bottom_lru.lru_get_size() == 0) {
7304 // root, stray, etc.?
7305 auto p = inode_map.begin();
7306 while (p != inode_map.end()) {
7307 CInode *in = p->second;
7308 ++p;
7309 if (!in->is_auth()) {
7310 auto&& ls = in->get_dirfrags();
7311 for (const auto& dir : ls) {
7312 dout(10) << " removing " << *dir << dendl;
7313 ceph_assert(dir->get_num_ref() == 1); // SUBTREE
7314 remove_subtree(dir);
7315 in->close_dirfrag(dir->dirfrag().frag);
7316 }
7317 dout(10) << " removing " << *in << dendl;
7318 ceph_assert(!in->get_parent_dn());
7319 ceph_assert(in->get_num_ref() == 0);
7320 remove_inode(in);
7321 }
7322 }
7323 }
7324
7325 show_subtrees();
7326 }
7327
7328 /**
7329 * Recursively trim the subtree rooted at directory to remove all
7330 * CInodes/CDentrys/CDirs that aren't links to remote MDSes, or ancestors
7331 * of those links. This is used to clear invalid data out of the cache.
7332 * Note that it doesn't clear the passed-in directory, since that's not
7333 * always safe.
7334 */
7335 bool MDCache::trim_non_auth_subtree(CDir *dir)
7336 {
7337 dout(10) << "trim_non_auth_subtree(" << dir << ") " << *dir << dendl;
7338
7339 bool keep_dir = !can_trim_non_auth_dirfrag(dir);
7340
7341 auto j = dir->begin();
7342 auto i = j;
7343 while (j != dir->end()) {
7344 i = j++;
7345 CDentry *dn = i->second;
7346 dout(10) << "trim_non_auth_subtree(" << dir << ") Checking dentry " << dn << dendl;
7347 CDentry::linkage_t *dnl = dn->get_linkage();
7348 if (dnl->is_primary()) { // check for subdirectories, etc
7349 CInode *in = dnl->get_inode();
7350 bool keep_inode = false;
7351 if (in->is_dir()) {
7352 auto&& subdirs = in->get_dirfrags();
7353 for (const auto& subdir : subdirs) {
7354 if (subdir->is_subtree_root()) {
7355 keep_inode = true;
7356 dout(10) << "trim_non_auth_subtree(" << dir << ") keeping " << *subdir << dendl;
7357 } else {
7358 if (trim_non_auth_subtree(subdir))
7359 keep_inode = true;
7360 else {
7361 in->close_dirfrag(subdir->get_frag());
7362 dir->state_clear(CDir::STATE_COMPLETE); // now incomplete!
7363 }
7364 }
7365 }
7366
7367 }
7368 if (!keep_inode) { // remove it!
7369 dout(20) << "trim_non_auth_subtree(" << dir << ") removing inode " << in << " with dentry" << dn << dendl;
7370 dir->unlink_inode(dn, false);
7371 remove_inode(in);
7372 ceph_assert(!dir->has_bloom());
7373 dir->remove_dentry(dn);
7374 } else {
7375 dout(20) << "trim_non_auth_subtree(" << dir << ") keeping inode " << in << " with dentry " << dn <<dendl;
7376 dn->clear_auth();
7377 in->state_clear(CInode::STATE_AUTH);
7378 }
7379 } else if (keep_dir && dnl->is_null()) { // keep null dentry for peer rollback
7380 dout(20) << "trim_non_auth_subtree(" << dir << ") keeping dentry " << dn <<dendl;
7381 } else { // just remove it
7382 dout(20) << "trim_non_auth_subtree(" << dir << ") removing dentry " << dn << dendl;
7383 if (dnl->is_remote())
7384 dir->unlink_inode(dn, false);
7385 dir->remove_dentry(dn);
7386 }
7387 }
7388 dir->state_clear(CDir::STATE_AUTH);
7389 /**
7390 * We've now checked all our children and deleted those that need it.
7391 * Now return to caller, and tell them if *we're* a keeper.
7392 */
7393 return keep_dir || dir->get_num_any();
7394 }
7395
7396 /*
7397 * during replay, when we determine a subtree is no longer ours, we
7398 * try to trim it from our cache. because subtrees must be connected
7399 * to the root, the fact that we can trim this tree may mean that our
7400 * children or parents can also be trimmed.
7401 */
7402 void MDCache::try_trim_non_auth_subtree(CDir *dir)
7403 {
7404 dout(10) << "try_trim_nonauth_subtree " << *dir << dendl;
7405
7406 // can we now trim child subtrees?
7407 set<CDir*> bounds;
7408 get_subtree_bounds(dir, bounds);
7409 for (set<CDir*>::iterator p = bounds.begin(); p != bounds.end(); ++p) {
7410 CDir *bd = *p;
7411 if (bd->get_dir_auth().first != mds->get_nodeid() && // we are not auth
7412 bd->get_num_any() == 0 && // and empty
7413 can_trim_non_auth_dirfrag(bd)) {
7414 CInode *bi = bd->get_inode();
7415 dout(10) << " closing empty non-auth child subtree " << *bd << dendl;
7416 remove_subtree(bd);
7417 bd->mark_clean();
7418 bi->close_dirfrag(bd->get_frag());
7419 }
7420 }
7421
7422 if (trim_non_auth_subtree(dir)) {
7423 // keep
7424 try_subtree_merge(dir);
7425 } else {
7426 // can we trim this subtree (and possibly our ancestors) too?
7427 while (true) {
7428 CInode *diri = dir->get_inode();
7429 if (diri->is_base()) {
7430 if (!diri->is_root() && diri->authority().first != mds->get_nodeid()) {
7431 dout(10) << " closing empty non-auth subtree " << *dir << dendl;
7432 remove_subtree(dir);
7433 dir->mark_clean();
7434 diri->close_dirfrag(dir->get_frag());
7435
7436 dout(10) << " removing " << *diri << dendl;
7437 ceph_assert(!diri->get_parent_dn());
7438 ceph_assert(diri->get_num_ref() == 0);
7439 remove_inode(diri);
7440 }
7441 break;
7442 }
7443
7444 CDir *psub = get_subtree_root(diri->get_parent_dir());
7445 dout(10) << " parent subtree is " << *psub << dendl;
7446 if (psub->get_dir_auth().first == mds->get_nodeid())
7447 break; // we are auth, keep.
7448
7449 dout(10) << " closing empty non-auth subtree " << *dir << dendl;
7450 remove_subtree(dir);
7451 dir->mark_clean();
7452 diri->close_dirfrag(dir->get_frag());
7453
7454 dout(10) << " parent subtree also non-auth: " << *psub << dendl;
7455 if (trim_non_auth_subtree(psub))
7456 break;
7457 dir = psub;
7458 }
7459 }
7460
7461 show_subtrees();
7462 }
7463
7464 void MDCache::standby_trim_segment(LogSegment *ls)
7465 {
7466 auto try_trim_inode = [this](CInode *in) {
7467 if (in->get_num_ref() == 0 &&
7468 !in->item_open_file.is_on_list() &&
7469 in->parent != NULL &&
7470 in->parent->get_num_ref() == 0){
7471 touch_dentry_bottom(in->parent);
7472 }
7473 };
7474
7475 auto try_trim_dentry = [this](CDentry *dn) {
7476 if (dn->get_num_ref() > 0)
7477 return;
7478 auto in = dn->get_linkage()->inode;
7479 if(in && in->item_open_file.is_on_list())
7480 return;
7481 touch_dentry_bottom(dn);
7482 };
7483
7484 ls->new_dirfrags.clear_list();
7485 ls->open_files.clear_list();
7486
7487 while (!ls->dirty_dirfrags.empty()) {
7488 CDir *dir = ls->dirty_dirfrags.front();
7489 dir->mark_clean();
7490 if (dir->inode)
7491 try_trim_inode(dir->inode);
7492 }
7493 while (!ls->dirty_inodes.empty()) {
7494 CInode *in = ls->dirty_inodes.front();
7495 in->mark_clean();
7496 try_trim_inode(in);
7497 }
7498 while (!ls->dirty_dentries.empty()) {
7499 CDentry *dn = ls->dirty_dentries.front();
7500 dn->mark_clean();
7501 try_trim_dentry(dn);
7502 }
7503 while (!ls->dirty_parent_inodes.empty()) {
7504 CInode *in = ls->dirty_parent_inodes.front();
7505 in->clear_dirty_parent();
7506 try_trim_inode(in);
7507 }
7508 while (!ls->dirty_dirfrag_dir.empty()) {
7509 CInode *in = ls->dirty_dirfrag_dir.front();
7510 in->filelock.remove_dirty();
7511 try_trim_inode(in);
7512 }
7513 while (!ls->dirty_dirfrag_nest.empty()) {
7514 CInode *in = ls->dirty_dirfrag_nest.front();
7515 in->nestlock.remove_dirty();
7516 try_trim_inode(in);
7517 }
7518 while (!ls->dirty_dirfrag_dirfragtree.empty()) {
7519 CInode *in = ls->dirty_dirfrag_dirfragtree.front();
7520 in->dirfragtreelock.remove_dirty();
7521 try_trim_inode(in);
7522 }
7523 while (!ls->truncating_inodes.empty()) {
7524 auto it = ls->truncating_inodes.begin();
7525 CInode *in = *it;
7526 ls->truncating_inodes.erase(it);
7527 in->put(CInode::PIN_TRUNCATING);
7528 try_trim_inode(in);
7529 }
7530 }
7531
7532 void MDCache::handle_cache_expire(const cref_t<MCacheExpire> &m)
7533 {
7534 mds_rank_t from = mds_rank_t(m->get_from());
7535
7536 dout(7) << "cache_expire from mds." << from << dendl;
7537
7538 if (mds->get_state() < MDSMap::STATE_REJOIN) {
7539 return;
7540 }
7541
7542 set<SimpleLock *> gather_locks;
7543 // loop over realms
7544 for (const auto &p : m->realms) {
7545 // check container?
7546 if (p.first.ino > 0) {
7547 CInode *expired_inode = get_inode(p.first.ino);
7548 ceph_assert(expired_inode); // we had better have this.
7549 CDir *parent_dir = expired_inode->get_approx_dirfrag(p.first.frag);
7550 ceph_assert(parent_dir);
7551
7552 int export_state = -1;
7553 if (parent_dir->is_auth() && parent_dir->is_exporting()) {
7554 export_state = migrator->get_export_state(parent_dir);
7555 ceph_assert(export_state >= 0);
7556 }
7557
7558 if (!parent_dir->is_auth() ||
7559 (export_state != -1 &&
7560 ((export_state == Migrator::EXPORT_WARNING &&
7561 migrator->export_has_warned(parent_dir,from)) ||
7562 export_state == Migrator::EXPORT_EXPORTING ||
7563 export_state == Migrator::EXPORT_LOGGINGFINISH ||
7564 (export_state == Migrator::EXPORT_NOTIFYING &&
7565 !migrator->export_has_notified(parent_dir,from))))) {
7566
7567 // not auth.
7568 dout(7) << "delaying nonauth|warned expires for " << *parent_dir << dendl;
7569 ceph_assert(parent_dir->is_frozen_tree_root());
7570
7571 // make a message container
7572
7573 auto em = delayed_expire[parent_dir].emplace(std::piecewise_construct, std::forward_as_tuple(from), std::forward_as_tuple());
7574 if (em.second)
7575 em.first->second = make_message<MCacheExpire>(from); /* new */
7576
7577 // merge these expires into it
7578 em.first->second->add_realm(p.first, p.second);
7579 continue;
7580 }
7581 ceph_assert(export_state <= Migrator::EXPORT_PREPPING ||
7582 (export_state == Migrator::EXPORT_WARNING &&
7583 !migrator->export_has_warned(parent_dir, from)));
7584
7585 dout(7) << "expires for " << *parent_dir << dendl;
7586 } else {
7587 dout(7) << "containerless expires (root, stray inodes)" << dendl;
7588 }
7589
7590 // INODES
7591 for (const auto &q : p.second.inodes) {
7592 CInode *in = get_inode(q.first);
7593 unsigned nonce = q.second;
7594
7595 if (!in) {
7596 dout(0) << " inode expire on " << q.first << " from " << from
7597 << ", don't have it" << dendl;
7598 ceph_assert(in);
7599 }
7600 ceph_assert(in->is_auth());
7601 dout(20) << __func__ << ": expiring inode " << *in << dendl;
7602
7603 // check nonce
7604 if (nonce == in->get_replica_nonce(from)) {
7605 // remove from our cached_by
7606 dout(7) << " inode expire on " << *in << " from mds." << from
7607 << " cached_by was " << in->get_replicas() << dendl;
7608 inode_remove_replica(in, from, false, gather_locks);
7609 }
7610 else {
7611 // this is an old nonce, ignore expire.
7612 dout(7) << " inode expire on " << *in << " from mds." << from
7613 << " with old nonce " << nonce
7614 << " (current " << in->get_replica_nonce(from) << "), dropping"
7615 << dendl;
7616 }
7617 }
7618
7619 // DIRS
7620 for (const auto &q : p.second.dirs) {
7621 CDir *dir = get_dirfrag(q.first);
7622 unsigned nonce = q.second;
7623
7624 if (!dir) {
7625 CInode *diri = get_inode(q.first.ino);
7626 if (diri) {
7627 if (mds->is_rejoin() &&
7628 rejoin_ack_gather.count(mds->get_nodeid()) && // haven't sent rejoin ack yet
7629 !diri->is_replica(from)) {
7630 auto&& ls = diri->get_nested_dirfrags();
7631 dout(7) << " dir expire on dirfrag " << q.first << " from mds." << from
7632 << " while rejoining, inode isn't replicated" << dendl;
7633 for (const auto& d : ls) {
7634 dir = d;
7635 if (dir->is_replica(from)) {
7636 dout(7) << " dir expire on " << *dir << " from mds." << from << dendl;
7637 dir->remove_replica(from);
7638 }
7639 }
7640 continue;
7641 }
7642 CDir *other = diri->get_approx_dirfrag(q.first.frag);
7643 if (other) {
7644 dout(7) << " dir expire on dirfrag " << q.first << " from mds." << from
7645 << " have " << *other << ", mismatched frags, dropping" << dendl;
7646 continue;
7647 }
7648 }
7649 dout(0) << " dir expire on " << q.first << " from " << from
7650 << ", don't have it" << dendl;
7651 ceph_assert(dir);
7652 }
7653 dout(20) << __func__ << ": expiring dirfrag " << *dir << dendl;
7654
7655 ceph_assert(dir->is_auth());
7656
7657 // check nonce
7658 if (nonce == dir->get_replica_nonce(from)) {
7659 // remove from our cached_by
7660 dout(7) << " dir expire on " << *dir << " from mds." << from
7661 << " replicas was " << dir->get_replicas() << dendl;
7662 dir->remove_replica(from);
7663 }
7664 else {
7665 // this is an old nonce, ignore expire.
7666 dout(7) << " dir expire on " << *dir << " from mds." << from
7667 << " with old nonce " << nonce << " (current " << dir->get_replica_nonce(from)
7668 << "), dropping" << dendl;
7669 }
7670 }
7671
7672 // DENTRIES
7673 for (const auto &pd : p.second.dentries) {
7674 dout(10) << " dn expires in dir " << pd.first << dendl;
7675 CInode *diri = get_inode(pd.first.ino);
7676 ceph_assert(diri);
7677 CDir *dir = diri->get_dirfrag(pd.first.frag);
7678
7679 if (!dir) {
7680 dout(0) << " dn expires on " << pd.first << " from " << from
7681 << ", must have refragmented" << dendl;
7682 } else {
7683 ceph_assert(dir->is_auth());
7684 }
7685
7686 for (const auto &p : pd.second) {
7687 unsigned nonce = p.second;
7688 CDentry *dn;
7689
7690 if (dir) {
7691 dn = dir->lookup(p.first.first, p.first.second);
7692 } else {
7693 // which dirfrag for this dentry?
7694 CDir *dir = diri->get_dirfrag(diri->pick_dirfrag(p.first.first));
7695 ceph_assert(dir);
7696 ceph_assert(dir->is_auth());
7697 dn = dir->lookup(p.first.first, p.first.second);
7698 }
7699
7700 if (!dn) {
7701 if (dir)
7702 dout(0) << " missing dentry for " << p.first.first << " snap " << p.first.second << " in " << *dir << dendl;
7703 else
7704 dout(0) << " missing dentry for " << p.first.first << " snap " << p.first.second << dendl;
7705 }
7706 ceph_assert(dn);
7707
7708 if (nonce == dn->get_replica_nonce(from)) {
7709 dout(7) << " dentry_expire on " << *dn << " from mds." << from << dendl;
7710 dentry_remove_replica(dn, from, gather_locks);
7711 }
7712 else {
7713 dout(7) << " dentry_expire on " << *dn << " from mds." << from
7714 << " with old nonce " << nonce << " (current " << dn->get_replica_nonce(from)
7715 << "), dropping" << dendl;
7716 }
7717 }
7718 }
7719 }
7720
7721 for (set<SimpleLock*>::iterator p = gather_locks.begin(); p != gather_locks.end(); ++p) {
7722 if (!(*p)->is_stable())
7723 mds->locker->eval_gather(*p);
7724 }
7725 }
7726
7727 void MDCache::process_delayed_expire(CDir *dir)
7728 {
7729 dout(7) << "process_delayed_expire on " << *dir << dendl;
7730 for (const auto &p : delayed_expire[dir]) {
7731 handle_cache_expire(p.second);
7732 }
7733 delayed_expire.erase(dir);
7734 }
7735
7736 void MDCache::discard_delayed_expire(CDir *dir)
7737 {
7738 dout(7) << "discard_delayed_expire on " << *dir << dendl;
7739 delayed_expire.erase(dir);
7740 }
7741
7742 void MDCache::inode_remove_replica(CInode *in, mds_rank_t from, bool rejoin,
7743 set<SimpleLock *>& gather_locks)
7744 {
7745 in->remove_replica(from);
7746 in->set_mds_caps_wanted(from, 0);
7747
7748 // note: this code calls _eval more often than it needs to!
7749 // fix lock
7750 if (in->authlock.remove_replica(from)) gather_locks.insert(&in->authlock);
7751 if (in->linklock.remove_replica(from)) gather_locks.insert(&in->linklock);
7752 if (in->snaplock.remove_replica(from)) gather_locks.insert(&in->snaplock);
7753 if (in->xattrlock.remove_replica(from)) gather_locks.insert(&in->xattrlock);
7754 if (in->flocklock.remove_replica(from)) gather_locks.insert(&in->flocklock);
7755 if (in->policylock.remove_replica(from)) gather_locks.insert(&in->policylock);
7756
7757 // If 'rejoin' is true and the scatter lock is in LOCK_MIX_* state.
7758 // Don't remove the recovering mds from lock's gathering list because
7759 // it may hold rejoined wrlocks.
7760 if (in->dirfragtreelock.remove_replica(from, rejoin)) gather_locks.insert(&in->dirfragtreelock);
7761 if (in->filelock.remove_replica(from, rejoin)) gather_locks.insert(&in->filelock);
7762 if (in->nestlock.remove_replica(from, rejoin)) gather_locks.insert(&in->nestlock);
7763 }
7764
7765 void MDCache::dentry_remove_replica(CDentry *dn, mds_rank_t from, set<SimpleLock *>& gather_locks)
7766 {
7767 dn->remove_replica(from);
7768
7769 // fix lock
7770 if (dn->lock.remove_replica(from))
7771 gather_locks.insert(&dn->lock);
7772
7773 // Replicated strays might now be elegible for purge
7774 CDentry::linkage_t *dnl = dn->get_projected_linkage();
7775 if (dnl->is_primary()) {
7776 maybe_eval_stray(dnl->get_inode());
7777 }
7778 }
7779
7780 void MDCache::trim_client_leases()
7781 {
7782 utime_t now = ceph_clock_now();
7783
7784 dout(10) << "trim_client_leases" << dendl;
7785
7786 std::size_t pool = 0;
7787 for (const auto& list : client_leases) {
7788 pool += 1;
7789 if (list.empty())
7790 continue;
7791
7792 auto before = list.size();
7793 while (!list.empty()) {
7794 ClientLease *r = list.front();
7795 if (r->ttl > now) break;
7796 CDentry *dn = static_cast<CDentry*>(r->parent);
7797 dout(10) << " expiring client." << r->client << " lease of " << *dn << dendl;
7798 dn->remove_client_lease(r, mds->locker);
7799 }
7800 auto after = list.size();
7801 dout(10) << "trim_client_leases pool " << pool << " trimmed "
7802 << (before-after) << " leases, " << after << " left" << dendl;
7803 }
7804 }
7805
7806 void MDCache::check_memory_usage()
7807 {
7808 static MemoryModel mm(g_ceph_context);
7809 static MemoryModel::snap last;
7810 mm.sample(&last);
7811 static MemoryModel::snap baseline = last;
7812
7813 // check client caps
7814 ceph_assert(CInode::count() == inode_map.size() + snap_inode_map.size() + num_shadow_inodes);
7815 double caps_per_inode = 0.0;
7816 if (CInode::count())
7817 caps_per_inode = (double)Capability::count() / (double)CInode::count();
7818
7819 dout(2) << "Memory usage: "
7820 << " total " << last.get_total()
7821 << ", rss " << last.get_rss()
7822 << ", heap " << last.get_heap()
7823 << ", baseline " << baseline.get_heap()
7824 << ", " << num_inodes_with_caps << " / " << CInode::count() << " inodes have caps"
7825 << ", " << Capability::count() << " caps, " << caps_per_inode << " caps per inode"
7826 << dendl;
7827
7828 mds->update_mlogger();
7829 mds->mlogger->set(l_mdm_rss, last.get_rss());
7830 mds->mlogger->set(l_mdm_heap, last.get_heap());
7831 }
7832
7833
7834
7835 // =========================================================================================
7836 // shutdown
7837
7838 class C_MDC_ShutdownCheck : public MDCacheContext {
7839 public:
7840 explicit C_MDC_ShutdownCheck(MDCache *m) : MDCacheContext(m) {}
7841 void finish(int) override {
7842 mdcache->shutdown_check();
7843 }
7844 };
7845
7846 void MDCache::shutdown_check()
7847 {
7848 dout(0) << "shutdown_check at " << ceph_clock_now() << dendl;
7849
7850 // cache
7851 char old_val[32] = { 0 };
7852 char *o = old_val;
7853 g_conf().get_val("debug_mds", &o, sizeof(old_val));
7854 g_conf().set_val("debug_mds", "10");
7855 g_conf().apply_changes(nullptr);
7856 show_cache();
7857 g_conf().set_val("debug_mds", old_val);
7858 g_conf().apply_changes(nullptr);
7859 mds->timer.add_event_after(g_conf()->mds_shutdown_check, new C_MDC_ShutdownCheck(this));
7860
7861 // this
7862 dout(0) << "lru size now " << lru.lru_get_size() << "/" << bottom_lru.lru_get_size() << dendl;
7863 dout(0) << "log len " << mds->mdlog->get_num_events() << dendl;
7864
7865
7866 if (mds->objecter->is_active()) {
7867 dout(0) << "objecter still active" << dendl;
7868 mds->objecter->dump_active();
7869 }
7870 }
7871
7872
7873 void MDCache::shutdown_start()
7874 {
7875 dout(5) << "shutdown_start" << dendl;
7876
7877 if (g_conf()->mds_shutdown_check)
7878 mds->timer.add_event_after(g_conf()->mds_shutdown_check, new C_MDC_ShutdownCheck(this));
7879
7880 // g_conf()->debug_mds = 10;
7881 }
7882
7883
7884
7885 bool MDCache::shutdown_pass()
7886 {
7887 dout(7) << "shutdown_pass" << dendl;
7888
7889 if (mds->is_stopped()) {
7890 dout(7) << " already shut down" << dendl;
7891 show_cache();
7892 show_subtrees();
7893 return true;
7894 }
7895
7896 // empty stray dir
7897 bool strays_all_exported = shutdown_export_strays();
7898
7899 // trim cache
7900 trim(UINT64_MAX);
7901 dout(5) << "lru size now " << lru.lru_get_size() << "/" << bottom_lru.lru_get_size() << dendl;
7902
7903 // Export all subtrees to another active (usually rank 0) if not rank 0
7904 int num_auth_subtree = 0;
7905 if (!subtrees.empty() && mds->get_nodeid() != 0) {
7906 dout(7) << "looking for subtrees to export" << dendl;
7907 std::vector<CDir*> ls;
7908 for (auto& [dir, bounds] : subtrees) {
7909 dout(10) << " examining " << *dir << " bounds " << bounds << dendl;
7910 if (dir->get_inode()->is_mdsdir() || !dir->is_auth())
7911 continue;
7912 num_auth_subtree++;
7913 if (dir->is_frozen() ||
7914 dir->is_freezing() ||
7915 dir->is_ambiguous_dir_auth() ||
7916 dir->state_test(CDir::STATE_EXPORTING) ||
7917 dir->get_inode()->is_ephemerally_pinned()) {
7918 continue;
7919 }
7920 ls.push_back(dir);
7921 }
7922
7923 migrator->clear_export_queue();
7924 // stopping mds does not call MDBalancer::tick()
7925 mds->balancer->handle_export_pins();
7926 for (const auto& dir : ls) {
7927 mds_rank_t dest = dir->get_inode()->authority().first;
7928 if (dest > 0 && !mds->mdsmap->is_active(dest))
7929 dest = 0;
7930 dout(7) << "sending " << *dir << " back to mds." << dest << dendl;
7931 migrator->export_dir_nicely(dir, dest);
7932 }
7933 }
7934
7935 if (!strays_all_exported) {
7936 dout(7) << "waiting for strays to migrate" << dendl;
7937 return false;
7938 }
7939
7940 if (num_auth_subtree > 0) {
7941 ceph_assert(mds->get_nodeid() > 0);
7942 dout(7) << "still have " << num_auth_subtree << " auth subtrees" << dendl;
7943 show_subtrees();
7944 return false;
7945 }
7946
7947 // close out any sessions (and open files!) before we try to trim the log, etc.
7948 if (mds->sessionmap.have_unclosed_sessions()) {
7949 if (!mds->server->terminating_sessions)
7950 mds->server->terminate_sessions();
7951 return false;
7952 }
7953
7954 // Fully trim the log so that all objects in cache are clean and may be
7955 // trimmed by a future MDCache::trim. Note that MDSRank::tick does not
7956 // trim the log such that the cache eventually becomes clean.
7957 if (mds->mdlog->get_num_segments() > 0) {
7958 auto ls = mds->mdlog->get_current_segment();
7959 if (ls->num_events > 1 || !ls->dirty_dirfrags.empty()) {
7960 // Current segment contains events other than subtreemap or
7961 // there are dirty dirfrags (see CDir::log_mark_dirty())
7962 mds->mdlog->start_new_segment();
7963 mds->mdlog->flush();
7964 }
7965 }
7966 mds->mdlog->trim_all();
7967 if (mds->mdlog->get_num_segments() > 1) {
7968 dout(7) << "still >1 segments, waiting for log to trim" << dendl;
7969 return false;
7970 }
7971
7972 // drop our reference to our stray dir inode
7973 for (int i = 0; i < NUM_STRAY; ++i) {
7974 if (strays[i] &&
7975 strays[i]->state_test(CInode::STATE_STRAYPINNED)) {
7976 strays[i]->state_clear(CInode::STATE_STRAYPINNED);
7977 strays[i]->put(CInode::PIN_STRAY);
7978 strays[i]->put_stickydirs();
7979 }
7980 }
7981
7982 CDir *mydir = myin ? myin->get_dirfrag(frag_t()) : NULL;
7983 if (mydir && !mydir->is_subtree_root())
7984 mydir = NULL;
7985
7986 // subtrees map not empty yet?
7987 if (subtrees.size() > (mydir ? 1 : 0)) {
7988 dout(7) << "still have " << num_subtrees() << " subtrees" << dendl;
7989 show_subtrees();
7990 migrator->show_importing();
7991 migrator->show_exporting();
7992 if (!migrator->is_importing() && !migrator->is_exporting())
7993 show_cache();
7994 return false;
7995 }
7996 ceph_assert(!migrator->is_exporting());
7997 ceph_assert(!migrator->is_importing());
7998
7999 // replicas may dirty scatter locks
8000 if (myin && myin->is_replicated()) {
8001 dout(7) << "still have replicated objects" << dendl;
8002 return false;
8003 }
8004
8005 if ((myin && myin->get_num_auth_pins()) ||
8006 (mydir && (mydir->get_auth_pins() || mydir->get_dir_auth_pins()))) {
8007 dout(7) << "still have auth pinned objects" << dendl;
8008 return false;
8009 }
8010
8011 // (only do this once!)
8012 if (!mds->mdlog->is_capped()) {
8013 dout(7) << "capping the mdlog" << dendl;
8014 mds->mdlog->cap();
8015 }
8016
8017 if (!mds->mdlog->empty())
8018 mds->mdlog->trim(0);
8019
8020 if (!mds->mdlog->empty()) {
8021 dout(7) << "waiting for log to flush.. " << mds->mdlog->get_num_events()
8022 << " in " << mds->mdlog->get_num_segments() << " segments" << dendl;
8023 return false;
8024 }
8025
8026 if (!did_shutdown_log_cap) {
8027 // flush journal header
8028 dout(7) << "writing header for (now-empty) journal" << dendl;
8029 ceph_assert(mds->mdlog->empty());
8030 mds->mdlog->write_head(0);
8031 // NOTE: filer active checker below will block us until this completes.
8032 did_shutdown_log_cap = true;
8033 return false;
8034 }
8035
8036 // filer active?
8037 if (mds->objecter->is_active()) {
8038 dout(7) << "objecter still active" << dendl;
8039 mds->objecter->dump_active();
8040 return false;
8041 }
8042
8043 // trim what we can from the cache
8044 if (lru.lru_get_size() > 0 || bottom_lru.lru_get_size() > 0) {
8045 dout(7) << "there's still stuff in the cache: " << lru.lru_get_size() << "/" << bottom_lru.lru_get_size() << dendl;
8046 show_cache();
8047 //dump();
8048 return false;
8049 }
8050
8051 // make mydir subtree go away
8052 if (mydir) {
8053 if (mydir->get_num_ref() > 1) { // subtree pin
8054 dout(7) << "there's still reference to mydir " << *mydir << dendl;
8055 show_cache();
8056 return false;
8057 }
8058
8059 remove_subtree(mydir);
8060 myin->close_dirfrag(mydir->get_frag());
8061 }
8062 ceph_assert(subtrees.empty());
8063
8064 if (myin) {
8065 remove_inode(myin);
8066 ceph_assert(!myin);
8067 }
8068
8069 if (global_snaprealm) {
8070 remove_inode(global_snaprealm->inode);
8071 global_snaprealm = nullptr;
8072 }
8073
8074 // done!
8075 dout(5) << "shutdown done." << dendl;
8076 return true;
8077 }
8078
8079 bool MDCache::shutdown_export_strays()
8080 {
8081 static const unsigned MAX_EXPORTING = 100;
8082
8083 if (mds->get_nodeid() == 0)
8084 return true;
8085
8086 if (shutdown_exporting_strays.size() * 3 >= MAX_EXPORTING * 2)
8087 return false;
8088
8089 dout(10) << "shutdown_export_strays " << shutdown_export_next.first
8090 << " '" << shutdown_export_next.second << "'" << dendl;
8091
8092 bool mds0_active = mds->mdsmap->is_active(mds_rank_t(0));
8093 bool all_exported = false;
8094
8095 again:
8096 auto next = shutdown_export_next;
8097
8098 for (int i = 0; i < NUM_STRAY; ++i) {
8099 CInode *strayi = strays[i];
8100 if (!strayi ||
8101 !strayi->state_test(CInode::STATE_STRAYPINNED))
8102 continue;
8103 if (strayi->ino() < next.first.ino)
8104 continue;
8105
8106 deque<CDir*> dfls;
8107 strayi->get_dirfrags(dfls);
8108
8109 while (!dfls.empty()) {
8110 CDir *dir = dfls.front();
8111 dfls.pop_front();
8112
8113 if (dir->dirfrag() < next.first)
8114 continue;
8115 if (next.first < dir->dirfrag()) {
8116 next.first = dir->dirfrag();
8117 next.second.clear();
8118 }
8119
8120 if (!dir->is_complete()) {
8121 MDSContext *fin = nullptr;
8122 if (shutdown_exporting_strays.empty()) {
8123 fin = new MDSInternalContextWrapper(mds,
8124 new LambdaContext([this](int r) {
8125 shutdown_export_strays();
8126 })
8127 );
8128 }
8129 dir->fetch(fin);
8130 goto done;
8131 }
8132
8133 CDir::dentry_key_map::iterator it;
8134 if (next.second.empty()) {
8135 it = dir->begin();
8136 } else {
8137 auto hash = ceph_frag_value(strayi->hash_dentry_name(next.second));
8138 it = dir->lower_bound(dentry_key_t(0, next.second, hash));
8139 }
8140
8141 for (; it != dir->end(); ++it) {
8142 CDentry *dn = it->second;
8143 CDentry::linkage_t *dnl = dn->get_projected_linkage();
8144 if (dnl->is_null())
8145 continue;
8146
8147 if (!mds0_active && !dn->state_test(CDentry::STATE_PURGING)) {
8148 next.second = it->first.name;
8149 goto done;
8150 }
8151
8152 auto ret = shutdown_exporting_strays.insert(dnl->get_inode()->ino());
8153 if (!ret.second) {
8154 dout(10) << "already exporting/purging " << *dn << dendl;
8155 continue;
8156 }
8157
8158 // Don't try to migrate anything that is actually
8159 // being purged right now
8160 if (!dn->state_test(CDentry::STATE_PURGING))
8161 stray_manager.migrate_stray(dn, mds_rank_t(0)); // send to root!
8162
8163 if (shutdown_exporting_strays.size() >= MAX_EXPORTING) {
8164 ++it;
8165 if (it != dir->end()) {
8166 next.second = it->first.name;
8167 } else {
8168 if (dfls.empty())
8169 next.first.ino.val++;
8170 else
8171 next.first = dfls.front()->dirfrag();
8172 next.second.clear();
8173 }
8174 goto done;
8175 }
8176 }
8177 }
8178 }
8179
8180 if (shutdown_exporting_strays.empty()) {
8181 dirfrag_t first_df(MDS_INO_STRAY(mds->get_nodeid(), 0), 0);
8182 if (first_df < shutdown_export_next.first ||
8183 !shutdown_export_next.second.empty()) {
8184 shutdown_export_next.first = first_df;
8185 shutdown_export_next.second.clear();
8186 goto again;
8187 }
8188 all_exported = true;
8189 }
8190
8191 done:
8192 shutdown_export_next = next;
8193 return all_exported;
8194 }
8195
8196 // ========= messaging ==============
8197
8198 void MDCache::dispatch(const cref_t<Message> &m)
8199 {
8200 switch (m->get_type()) {
8201
8202 // RESOLVE
8203 case MSG_MDS_RESOLVE:
8204 handle_resolve(ref_cast<MMDSResolve>(m));
8205 break;
8206 case MSG_MDS_RESOLVEACK:
8207 handle_resolve_ack(ref_cast<MMDSResolveAck>(m));
8208 break;
8209
8210 // REJOIN
8211 case MSG_MDS_CACHEREJOIN:
8212 handle_cache_rejoin(ref_cast<MMDSCacheRejoin>(m));
8213 break;
8214
8215 case MSG_MDS_DISCOVER:
8216 handle_discover(ref_cast<MDiscover>(m));
8217 break;
8218 case MSG_MDS_DISCOVERREPLY:
8219 handle_discover_reply(ref_cast<MDiscoverReply>(m));
8220 break;
8221
8222 case MSG_MDS_DIRUPDATE:
8223 handle_dir_update(ref_cast<MDirUpdate>(m));
8224 break;
8225
8226 case MSG_MDS_CACHEEXPIRE:
8227 handle_cache_expire(ref_cast<MCacheExpire>(m));
8228 break;
8229
8230 case MSG_MDS_DENTRYLINK:
8231 handle_dentry_link(ref_cast<MDentryLink>(m));
8232 break;
8233 case MSG_MDS_DENTRYUNLINK:
8234 handle_dentry_unlink(ref_cast<MDentryUnlink>(m));
8235 break;
8236
8237 case MSG_MDS_FRAGMENTNOTIFY:
8238 handle_fragment_notify(ref_cast<MMDSFragmentNotify>(m));
8239 break;
8240 case MSG_MDS_FRAGMENTNOTIFYACK:
8241 handle_fragment_notify_ack(ref_cast<MMDSFragmentNotifyAck>(m));
8242 break;
8243
8244 case MSG_MDS_FINDINO:
8245 handle_find_ino(ref_cast<MMDSFindIno>(m));
8246 break;
8247 case MSG_MDS_FINDINOREPLY:
8248 handle_find_ino_reply(ref_cast<MMDSFindInoReply>(m));
8249 break;
8250
8251 case MSG_MDS_OPENINO:
8252 handle_open_ino(ref_cast<MMDSOpenIno>(m));
8253 break;
8254 case MSG_MDS_OPENINOREPLY:
8255 handle_open_ino_reply(ref_cast<MMDSOpenInoReply>(m));
8256 break;
8257
8258 case MSG_MDS_SNAPUPDATE:
8259 handle_snap_update(ref_cast<MMDSSnapUpdate>(m));
8260 break;
8261
8262 default:
8263 derr << "cache unknown message " << m->get_type() << dendl;
8264 ceph_abort_msg("cache unknown message");
8265 }
8266 }
8267
8268 int MDCache::path_traverse(MDRequestRef& mdr, MDSContextFactory& cf,
8269 const filepath& path, int flags,
8270 vector<CDentry*> *pdnvec, CInode **pin)
8271 {
8272 bool discover = (flags & MDS_TRAVERSE_DISCOVER);
8273 bool forward = !discover;
8274 bool path_locked = (flags & MDS_TRAVERSE_PATH_LOCKED);
8275 bool want_dentry = (flags & MDS_TRAVERSE_WANT_DENTRY);
8276 bool want_inode = (flags & MDS_TRAVERSE_WANT_INODE);
8277 bool want_auth = (flags & MDS_TRAVERSE_WANT_AUTH);
8278 bool rdlock_snap = (flags & (MDS_TRAVERSE_RDLOCK_SNAP | MDS_TRAVERSE_RDLOCK_SNAP2));
8279 bool rdlock_path = (flags & MDS_TRAVERSE_RDLOCK_PATH);
8280 bool xlock_dentry = (flags & MDS_TRAVERSE_XLOCK_DENTRY);
8281 bool rdlock_authlock = (flags & MDS_TRAVERSE_RDLOCK_AUTHLOCK);
8282
8283 if (forward)
8284 ceph_assert(mdr); // forward requires a request
8285
8286 snapid_t snapid = CEPH_NOSNAP;
8287 if (mdr)
8288 mdr->snapid = snapid;
8289
8290 client_t client = mdr ? mdr->get_client() : -1;
8291
8292 if (mds->logger) mds->logger->inc(l_mds_traverse);
8293
8294 dout(7) << "traverse: opening base ino " << path.get_ino() << " snap " << snapid << dendl;
8295 CInode *cur = get_inode(path.get_ino());
8296 if (!cur) {
8297 if (MDS_INO_IS_MDSDIR(path.get_ino())) {
8298 open_foreign_mdsdir(path.get_ino(), cf.build());
8299 return 1;
8300 }
8301 if (MDS_INO_IS_STRAY(path.get_ino())) {
8302 mds_rank_t rank = MDS_INO_STRAY_OWNER(path.get_ino());
8303 unsigned idx = MDS_INO_STRAY_INDEX(path.get_ino());
8304 filepath path(strays[idx]->get_parent_dn()->get_name(),
8305 MDS_INO_MDSDIR(rank));
8306 MDRequestRef null_ref;
8307 return path_traverse(null_ref, cf, path, MDS_TRAVERSE_DISCOVER, nullptr);
8308 }
8309 return -CEPHFS_ESTALE;
8310 }
8311 if (cur->state_test(CInode::STATE_PURGING))
8312 return -CEPHFS_ESTALE;
8313
8314 if (flags & MDS_TRAVERSE_CHECK_LOCKCACHE)
8315 mds->locker->find_and_attach_lock_cache(mdr, cur);
8316
8317 if (mdr && mdr->lock_cache) {
8318 if (flags & MDS_TRAVERSE_WANT_DIRLAYOUT)
8319 mdr->dir_layout = mdr->lock_cache->get_dir_layout();
8320 } else if (rdlock_snap) {
8321 int n = (flags & MDS_TRAVERSE_RDLOCK_SNAP2) ? 1 : 0;
8322 if ((n == 0 && !(mdr->locking_state & MutationImpl::SNAP_LOCKED)) ||
8323 (n == 1 && !(mdr->locking_state & MutationImpl::SNAP2_LOCKED))) {
8324 bool want_layout = (flags & MDS_TRAVERSE_WANT_DIRLAYOUT);
8325 if (!mds->locker->try_rdlock_snap_layout(cur, mdr, n, want_layout))
8326 return 1;
8327 }
8328 }
8329
8330 // start trace
8331 if (pdnvec)
8332 pdnvec->clear();
8333 if (pin)
8334 *pin = cur;
8335
8336 CInode *target_inode = nullptr;
8337 MutationImpl::LockOpVec lov;
8338 int r;
8339
8340 for (unsigned depth = 0; depth < path.depth(); ) {
8341 dout(12) << "traverse: path seg depth " << depth << " '" << path[depth]
8342 << "' snapid " << snapid << dendl;
8343
8344 if (!cur->is_dir()) {
8345 dout(7) << "traverse: " << *cur << " not a dir " << dendl;
8346 return -CEPHFS_ENOTDIR;
8347 }
8348
8349 // walk into snapdir?
8350 if (path[depth].length() == 0) {
8351 dout(10) << "traverse: snapdir" << dendl;
8352 if (!mdr || depth > 0) // snapdir must be the first component
8353 return -CEPHFS_EINVAL;
8354 snapid = CEPH_SNAPDIR;
8355 mdr->snapid = snapid;
8356 depth++;
8357 continue;
8358 }
8359 // walk thru snapdir?
8360 if (snapid == CEPH_SNAPDIR) {
8361 if (!mdr)
8362 return -CEPHFS_EINVAL;
8363 SnapRealm *realm = cur->find_snaprealm();
8364 snapid = realm->resolve_snapname(path[depth], cur->ino());
8365 dout(10) << "traverse: snap " << path[depth] << " -> " << snapid << dendl;
8366 if (!snapid) {
8367 if (pdnvec)
8368 pdnvec->clear(); // do not confuse likes of rdlock_path_pin_ref();
8369 return -CEPHFS_ENOENT;
8370 }
8371 if (depth == path.depth() - 1)
8372 target_inode = cur;
8373 mdr->snapid = snapid;
8374 depth++;
8375 continue;
8376 }
8377
8378 // open dir
8379 frag_t fg = cur->pick_dirfrag(path[depth]);
8380 CDir *curdir = cur->get_dirfrag(fg);
8381 if (!curdir) {
8382 if (cur->is_auth()) {
8383 // parent dir frozen_dir?
8384 if (cur->is_frozen()) {
8385 dout(7) << "traverse: " << *cur << " is frozen, waiting" << dendl;
8386 cur->add_waiter(CDir::WAIT_UNFREEZE, cf.build());
8387 return 1;
8388 }
8389 curdir = cur->get_or_open_dirfrag(this, fg);
8390 } else {
8391 // discover?
8392 dout(10) << "traverse: need dirfrag " << fg << ", doing discover from " << *cur << dendl;
8393 discover_path(cur, snapid, path.postfixpath(depth), cf.build(),
8394 path_locked);
8395 if (mds->logger) mds->logger->inc(l_mds_traverse_discover);
8396 return 1;
8397 }
8398 }
8399 ceph_assert(curdir);
8400
8401 #ifdef MDS_VERIFY_FRAGSTAT
8402 if (curdir->is_complete())
8403 curdir->verify_fragstat();
8404 #endif
8405
8406 // frozen?
8407 /*
8408 if (curdir->is_frozen()) {
8409 // doh!
8410 // FIXME: traverse is allowed?
8411 dout(7) << "traverse: " << *curdir << " is frozen, waiting" << dendl;
8412 curdir->add_waiter(CDir::WAIT_UNFREEZE, _get_waiter(mdr, req, fin));
8413 if (onfinish) delete onfinish;
8414 return 1;
8415 }
8416 */
8417
8418 // Defer the auth check until the target inode is determined not to exist
8419 // if want_inode is true.
8420 if (want_auth && want_dentry && !want_inode && depth == path.depth() - 1 &&
8421 (r = maybe_request_forward_to_auth(mdr, cf, curdir)) != 0)
8422 return r;
8423
8424 // Before doing dirfrag->dn lookup, compare with DamageTable's
8425 // record of which dentries were unreadable
8426 if (mds->damage_table.is_dentry_damaged(curdir, path[depth], snapid)) {
8427 dout(4) << "traverse: stopped lookup at damaged dentry "
8428 << *curdir << "/" << path[depth] << " snap=" << snapid << dendl;
8429 return -CEPHFS_EIO;
8430 }
8431
8432 // dentry
8433 CDentry *dn = curdir->lookup(path[depth], snapid);
8434 if (dn) {
8435 if (dn->state_test(CDentry::STATE_PURGING))
8436 return -CEPHFS_ENOENT;
8437
8438 CDentry::linkage_t *dnl = dn->get_projected_linkage();
8439 // If an auth check was deferred before and the target inode is found
8440 // not to exist now, do the auth check here if necessary.
8441 if (want_auth && want_dentry && want_inode && depth == path.depth() - 1 &&
8442 dnl->is_null() && (r = maybe_request_forward_to_auth(mdr, cf, dn)) != 0)
8443 return r;
8444
8445 if (rdlock_path) {
8446 lov.clear();
8447 // do not xlock the tail dentry if target inode exists and caller wants it
8448 if (xlock_dentry && (dnl->is_null() || !want_inode) &&
8449 depth == path.depth() - 1) {
8450 ceph_assert(dn->is_auth());
8451 if (depth > 0 || !mdr->lock_cache) {
8452 lov.add_wrlock(&cur->filelock);
8453 lov.add_wrlock(&cur->nestlock);
8454 if (rdlock_authlock)
8455 lov.add_rdlock(&cur->authlock);
8456 }
8457 lov.add_xlock(&dn->lock);
8458 } else {
8459 // force client to flush async dir operation if necessary
8460 if (cur->filelock.is_cached())
8461 lov.add_wrlock(&cur->filelock);
8462 lov.add_rdlock(&dn->lock);
8463 }
8464 if (!mds->locker->acquire_locks(mdr, lov)) {
8465 dout(10) << "traverse: failed to rdlock " << dn->lock << " " << *dn << dendl;
8466 return 1;
8467 }
8468 } else if (!path_locked &&
8469 !dn->lock.can_read(client) &&
8470 !(dn->lock.is_xlocked() && dn->lock.get_xlock_by() == mdr)) {
8471 dout(10) << "traverse: non-readable dentry at " << *dn << dendl;
8472 dn->lock.add_waiter(SimpleLock::WAIT_RD, cf.build());
8473 if (mds->logger)
8474 mds->logger->inc(l_mds_traverse_lock);
8475 if (dn->is_auth() && dn->lock.is_unstable_and_locked())
8476 mds->mdlog->flush();
8477 return 1;
8478 }
8479
8480 if (pdnvec)
8481 pdnvec->push_back(dn);
8482
8483 // can we conclude CEPHFS_ENOENT?
8484 if (dnl->is_null()) {
8485 dout(10) << "traverse: null+readable dentry at " << *dn << dendl;
8486 if (depth == path.depth() - 1) {
8487 if (want_dentry)
8488 break;
8489 } else {
8490 if (pdnvec)
8491 pdnvec->clear(); // do not confuse likes of rdlock_path_pin_ref();
8492 }
8493 return -CEPHFS_ENOENT;
8494 }
8495
8496 // do we have inode?
8497 CInode *in = dnl->get_inode();
8498 if (!in) {
8499 ceph_assert(dnl->is_remote());
8500 // do i have it?
8501 in = get_inode(dnl->get_remote_ino());
8502 if (in) {
8503 dout(7) << "linking in remote in " << *in << dendl;
8504 dn->link_remote(dnl, in);
8505 } else {
8506 dout(7) << "remote link to " << dnl->get_remote_ino() << ", which i don't have" << dendl;
8507 ceph_assert(mdr); // we shouldn't hit non-primary dentries doing a non-mdr traversal!
8508 if (mds->damage_table.is_remote_damaged(dnl->get_remote_ino())) {
8509 dout(4) << "traverse: remote dentry points to damaged ino "
8510 << *dn << dendl;
8511 return -CEPHFS_EIO;
8512 }
8513 open_remote_dentry(dn, true, cf.build(),
8514 (path_locked && depth == path.depth() - 1));
8515 if (mds->logger) mds->logger->inc(l_mds_traverse_remote_ino);
8516 return 1;
8517 }
8518 }
8519
8520 cur = in;
8521
8522 if (rdlock_snap && !(want_dentry && !want_inode && depth == path.depth() - 1)) {
8523 lov.clear();
8524 lov.add_rdlock(&cur->snaplock);
8525 if (!mds->locker->acquire_locks(mdr, lov)) {
8526 dout(10) << "traverse: failed to rdlock " << cur->snaplock << " " << *cur << dendl;
8527 return 1;
8528 }
8529 }
8530
8531 if (depth == path.depth() - 1)
8532 target_inode = cur;
8533
8534 // add to trace, continue.
8535 touch_inode(cur);
8536 if (pin)
8537 *pin = cur;
8538 depth++;
8539 continue;
8540 }
8541
8542 ceph_assert(!dn);
8543
8544 // MISS. dentry doesn't exist.
8545 dout(12) << "traverse: miss on dentry " << path[depth] << " in " << *curdir << dendl;
8546
8547 if (curdir->is_auth()) {
8548 // dentry is mine.
8549 if (curdir->is_complete() ||
8550 (snapid == CEPH_NOSNAP &&
8551 curdir->has_bloom() &&
8552 !curdir->is_in_bloom(path[depth]))) {
8553 // file not found
8554 if (pdnvec) {
8555 // instantiate a null dn?
8556 if (depth < path.depth() - 1) {
8557 dout(20) << " didn't traverse full path; not returning pdnvec" << dendl;
8558 } else if (snapid < CEPH_MAXSNAP) {
8559 dout(20) << " not adding null for snapid " << snapid << dendl;
8560 } else if (curdir->is_frozen()) {
8561 dout(7) << "traverse: " << *curdir << " is frozen, waiting" << dendl;
8562 curdir->add_waiter(CDir::WAIT_UNFREEZE, cf.build());
8563 return 1;
8564 } else {
8565 // create a null dentry
8566 dn = curdir->add_null_dentry(path[depth]);
8567 dout(20) << " added null " << *dn << dendl;
8568
8569 if (rdlock_path) {
8570 lov.clear();
8571 if (xlock_dentry) {
8572 if (depth > 0 || !mdr->lock_cache) {
8573 lov.add_wrlock(&cur->filelock);
8574 lov.add_wrlock(&cur->nestlock);
8575 if (rdlock_authlock)
8576 lov.add_rdlock(&cur->authlock);
8577 }
8578 lov.add_xlock(&dn->lock);
8579 } else {
8580 // force client to flush async dir operation if necessary
8581 if (cur->filelock.is_cached())
8582 lov.add_wrlock(&cur->filelock);
8583 lov.add_rdlock(&dn->lock);
8584 }
8585 if (!mds->locker->acquire_locks(mdr, lov)) {
8586 dout(10) << "traverse: failed to rdlock " << dn->lock << " " << *dn << dendl;
8587 return 1;
8588 }
8589 }
8590 }
8591 if (dn) {
8592 pdnvec->push_back(dn);
8593 if (want_dentry)
8594 break;
8595 } else {
8596 pdnvec->clear(); // do not confuse likes of rdlock_path_pin_ref();
8597 }
8598 }
8599 return -CEPHFS_ENOENT;
8600 } else {
8601
8602 // Check DamageTable for missing fragments before trying to fetch
8603 // this
8604 if (mds->damage_table.is_dirfrag_damaged(curdir)) {
8605 dout(4) << "traverse: damaged dirfrag " << *curdir
8606 << ", blocking fetch" << dendl;
8607 return -CEPHFS_EIO;
8608 }
8609
8610 // directory isn't complete; reload
8611 dout(7) << "traverse: incomplete dir contents for " << *cur << ", fetching" << dendl;
8612 touch_inode(cur);
8613 curdir->fetch(path[depth], snapid, cf.build());
8614 if (mds->logger) mds->logger->inc(l_mds_traverse_dir_fetch);
8615 return 1;
8616 }
8617 } else {
8618 // dirfrag/dentry is not mine.
8619
8620 if (forward &&
8621 mdr && mdr->client_request &&
8622 (int)depth < mdr->client_request->get_num_fwd()){
8623 dout(7) << "traverse: snap " << snapid << " and depth " << depth
8624 << " < fwd " << mdr->client_request->get_num_fwd()
8625 << ", discovering instead of forwarding" << dendl;
8626 discover = true;
8627 }
8628
8629 if ((discover)) {
8630 dout(7) << "traverse: discover from " << path[depth] << " from " << *curdir << dendl;
8631 discover_path(curdir, snapid, path.postfixpath(depth), cf.build(),
8632 path_locked);
8633 if (mds->logger) mds->logger->inc(l_mds_traverse_discover);
8634 return 1;
8635 }
8636 if (forward) {
8637 // forward
8638 dout(7) << "traverse: not auth for " << path << " in " << *curdir << dendl;
8639
8640 r = maybe_request_forward_to_auth(mdr, cf, curdir);
8641 ceph_assert(r != 0);
8642
8643 if (r == 2 && mds->logger)
8644 mds->logger->inc(l_mds_traverse_forward);
8645
8646 return r;
8647 }
8648 }
8649
8650 ceph_abort(); // i shouldn't get here
8651 }
8652
8653 if (path.depth() == 0) {
8654 dout(7) << "no tail dentry, base " << *cur << dendl;
8655 if (want_dentry && !want_inode) {
8656 return -CEPHFS_ENOENT;
8657 }
8658 target_inode = cur;
8659 }
8660
8661 if (target_inode) {
8662 dout(7) << "found target " << *target_inode << dendl;
8663 if (want_auth && !(want_dentry && !want_inode) &&
8664 (r = maybe_request_forward_to_auth(mdr, cf, target_inode)) != 0)
8665 return r;
8666 }
8667
8668 // success.
8669 if (mds->logger) mds->logger->inc(l_mds_traverse_hit);
8670 dout(10) << "path_traverse finish on snapid " << snapid << dendl;
8671 if (mdr)
8672 ceph_assert(mdr->snapid == snapid);
8673
8674 if (flags & MDS_TRAVERSE_RDLOCK_SNAP)
8675 mdr->locking_state |= MutationImpl::SNAP_LOCKED;
8676 else if (flags & MDS_TRAVERSE_RDLOCK_SNAP2)
8677 mdr->locking_state |= MutationImpl::SNAP2_LOCKED;
8678
8679 if (rdlock_path)
8680 mdr->locking_state |= MutationImpl::PATH_LOCKED;
8681
8682 return 0;
8683 }
8684
8685 int MDCache::maybe_request_forward_to_auth(MDRequestRef& mdr, MDSContextFactory& cf,
8686 MDSCacheObject *p)
8687 {
8688 if (p->is_ambiguous_auth()) {
8689 dout(7) << "waiting for single auth on " << *p << dendl;
8690 p->add_waiter(CInode::WAIT_SINGLEAUTH, cf.build());
8691 return 1;
8692 }
8693 if (!p->is_auth()) {
8694 dout(7) << "fw to auth for " << *p << dendl;
8695 request_forward(mdr, p->authority().first);
8696 return 2;
8697 }
8698 return 0;
8699 }
8700
8701 CInode *MDCache::cache_traverse(const filepath& fp)
8702 {
8703 dout(10) << "cache_traverse " << fp << dendl;
8704
8705 CInode *in;
8706 unsigned depth = 0;
8707 char mdsdir_name[16];
8708 sprintf(mdsdir_name, "~mds%d", mds->get_nodeid());
8709
8710 if (fp.get_ino()) {
8711 in = get_inode(fp.get_ino());
8712 } else if (fp.depth() > 0 && (fp[0] == "~mdsdir" || fp[0] == mdsdir_name)) {
8713 in = myin;
8714 depth = 1;
8715 } else {
8716 in = root;
8717 }
8718 if (!in)
8719 return NULL;
8720
8721 for (; depth < fp.depth(); depth++) {
8722 std::string_view dname = fp[depth];
8723 frag_t fg = in->pick_dirfrag(dname);
8724 dout(20) << " " << depth << " " << dname << " frag " << fg << " from " << *in << dendl;
8725 CDir *curdir = in->get_dirfrag(fg);
8726 if (!curdir)
8727 return NULL;
8728 CDentry *dn = curdir->lookup(dname, CEPH_NOSNAP);
8729 if (!dn)
8730 return NULL;
8731 in = dn->get_linkage()->get_inode();
8732 if (!in)
8733 return NULL;
8734 }
8735 dout(10) << " got " << *in << dendl;
8736 return in;
8737 }
8738
8739
8740 /**
8741 * open_remote_dir -- open up a remote dirfrag
8742 *
8743 * @param diri base inode
8744 * @param approxfg approximate fragment.
8745 * @param fin completion callback
8746 */
8747 void MDCache::open_remote_dirfrag(CInode *diri, frag_t approxfg, MDSContext *fin)
8748 {
8749 dout(10) << "open_remote_dir on " << *diri << dendl;
8750 ceph_assert(diri->is_dir());
8751 ceph_assert(!diri->is_auth());
8752 ceph_assert(diri->get_dirfrag(approxfg) == 0);
8753
8754 discover_dir_frag(diri, approxfg, fin);
8755 }
8756
8757
8758 /**
8759 * get_dentry_inode - get or open inode
8760 *
8761 * @param dn the dentry
8762 * @param mdr current request
8763 *
8764 * will return inode for primary, or link up/open up remote link's inode as necessary.
8765 * If it's not available right now, puts mdr on wait list and returns null.
8766 */
8767 CInode *MDCache::get_dentry_inode(CDentry *dn, MDRequestRef& mdr, bool projected)
8768 {
8769 CDentry::linkage_t *dnl;
8770 if (projected)
8771 dnl = dn->get_projected_linkage();
8772 else
8773 dnl = dn->get_linkage();
8774
8775 ceph_assert(!dnl->is_null());
8776
8777 if (dnl->is_primary())
8778 return dnl->inode;
8779
8780 ceph_assert(dnl->is_remote());
8781 CInode *in = get_inode(dnl->get_remote_ino());
8782 if (in) {
8783 dout(7) << "get_dentry_inode linking in remote in " << *in << dendl;
8784 dn->link_remote(dnl, in);
8785 return in;
8786 } else {
8787 dout(10) << "get_dentry_inode on remote dn, opening inode for " << *dn << dendl;
8788 open_remote_dentry(dn, projected, new C_MDS_RetryRequest(this, mdr));
8789 return 0;
8790 }
8791 }
8792
8793 struct C_MDC_OpenRemoteDentry : public MDCacheContext {
8794 CDentry *dn;
8795 inodeno_t ino;
8796 MDSContext *onfinish;
8797 bool want_xlocked;
8798 C_MDC_OpenRemoteDentry(MDCache *m, CDentry *d, inodeno_t i, MDSContext *f, bool wx) :
8799 MDCacheContext(m), dn(d), ino(i), onfinish(f), want_xlocked(wx) {
8800 dn->get(MDSCacheObject::PIN_PTRWAITER);
8801 }
8802 void finish(int r) override {
8803 mdcache->_open_remote_dentry_finish(dn, ino, onfinish, want_xlocked, r);
8804 dn->put(MDSCacheObject::PIN_PTRWAITER);
8805 }
8806 };
8807
8808 void MDCache::open_remote_dentry(CDentry *dn, bool projected, MDSContext *fin, bool want_xlocked)
8809 {
8810 dout(10) << "open_remote_dentry " << *dn << dendl;
8811 CDentry::linkage_t *dnl = projected ? dn->get_projected_linkage() : dn->get_linkage();
8812 inodeno_t ino = dnl->get_remote_ino();
8813 int64_t pool = dnl->get_remote_d_type() == DT_DIR ? mds->get_metadata_pool() : -1;
8814 open_ino(ino, pool,
8815 new C_MDC_OpenRemoteDentry(this, dn, ino, fin, want_xlocked), true, want_xlocked); // backtrace
8816 }
8817
8818 void MDCache::_open_remote_dentry_finish(CDentry *dn, inodeno_t ino, MDSContext *fin,
8819 bool want_xlocked, int r)
8820 {
8821 if (r < 0) {
8822 CDentry::linkage_t *dnl = dn->get_projected_linkage();
8823 if (dnl->is_remote() && dnl->get_remote_ino() == ino) {
8824 dout(0) << "open_remote_dentry_finish bad remote dentry " << *dn << dendl;
8825 dn->state_set(CDentry::STATE_BADREMOTEINO);
8826
8827 std::string path;
8828 CDir *dir = dn->get_dir();
8829 if (dir) {
8830 dir->get_inode()->make_path_string(path);
8831 path += "/";
8832 path += dn->get_name();
8833 }
8834
8835 bool fatal = mds->damage_table.notify_remote_damaged(ino, path);
8836 if (fatal) {
8837 mds->damaged();
8838 ceph_abort(); // unreachable, damaged() respawns us
8839 }
8840 } else {
8841 r = 0;
8842 }
8843 }
8844 fin->complete(r < 0 ? r : 0);
8845 }
8846
8847
8848 void MDCache::make_trace(vector<CDentry*>& trace, CInode *in)
8849 {
8850 // empty trace if we're a base inode
8851 if (in->is_base())
8852 return;
8853
8854 CInode *parent = in->get_parent_inode();
8855 ceph_assert(parent);
8856 make_trace(trace, parent);
8857
8858 CDentry *dn = in->get_parent_dn();
8859 dout(15) << "make_trace adding " << *dn << dendl;
8860 trace.push_back(dn);
8861 }
8862
8863
8864 // -------------------------------------------------------------------------------
8865 // Open inode by inode number
8866
8867 class C_IO_MDC_OpenInoBacktraceFetched : public MDCacheIOContext {
8868 inodeno_t ino;
8869 public:
8870 bufferlist bl;
8871 C_IO_MDC_OpenInoBacktraceFetched(MDCache *c, inodeno_t i) :
8872 MDCacheIOContext(c), ino(i) {}
8873 void finish(int r) override {
8874 mdcache->_open_ino_backtrace_fetched(ino, bl, r);
8875 }
8876 void print(ostream& out) const override {
8877 out << "openino_backtrace_fetch" << ino << ")";
8878 }
8879 };
8880
8881 struct C_MDC_OpenInoTraverseDir : public MDCacheContext {
8882 inodeno_t ino;
8883 cref_t<MMDSOpenIno> msg;
8884 bool parent;
8885 public:
8886 C_MDC_OpenInoTraverseDir(MDCache *c, inodeno_t i, const cref_t<MMDSOpenIno> &m, bool p) :
8887 MDCacheContext(c), ino(i), msg(m), parent(p) {}
8888 void finish(int r) override {
8889 if (r < 0 && !parent)
8890 r = -CEPHFS_EAGAIN;
8891 if (msg) {
8892 mdcache->handle_open_ino(msg, r);
8893 return;
8894 }
8895 auto& info = mdcache->opening_inodes.at(ino);
8896 mdcache->_open_ino_traverse_dir(ino, info, r);
8897 }
8898 };
8899
8900 struct C_MDC_OpenInoParentOpened : public MDCacheContext {
8901 inodeno_t ino;
8902 public:
8903 C_MDC_OpenInoParentOpened(MDCache *c, inodeno_t i) : MDCacheContext(c), ino(i) {}
8904 void finish(int r) override {
8905 mdcache->_open_ino_parent_opened(ino, r);
8906 }
8907 };
8908
8909 void MDCache::_open_ino_backtrace_fetched(inodeno_t ino, bufferlist& bl, int err)
8910 {
8911 dout(10) << "_open_ino_backtrace_fetched ino " << ino << " errno " << err << dendl;
8912
8913 open_ino_info_t& info = opening_inodes.at(ino);
8914
8915 CInode *in = get_inode(ino);
8916 if (in) {
8917 dout(10) << " found cached " << *in << dendl;
8918 open_ino_finish(ino, info, in->authority().first);
8919 return;
8920 }
8921
8922 inode_backtrace_t backtrace;
8923 if (err == 0) {
8924 try {
8925 decode(backtrace, bl);
8926 } catch (const buffer::error &decode_exc) {
8927 derr << "corrupt backtrace on ino x0" << std::hex << ino
8928 << std::dec << ": " << decode_exc.what() << dendl;
8929 open_ino_finish(ino, info, -CEPHFS_EIO);
8930 return;
8931 }
8932 if (backtrace.pool != info.pool && backtrace.pool != -1) {
8933 dout(10) << " old object in pool " << info.pool
8934 << ", retrying pool " << backtrace.pool << dendl;
8935 info.pool = backtrace.pool;
8936 C_IO_MDC_OpenInoBacktraceFetched *fin =
8937 new C_IO_MDC_OpenInoBacktraceFetched(this, ino);
8938 fetch_backtrace(ino, info.pool, fin->bl,
8939 new C_OnFinisher(fin, mds->finisher));
8940 return;
8941 }
8942 } else if (err == -CEPHFS_ENOENT) {
8943 int64_t meta_pool = mds->get_metadata_pool();
8944 if (info.pool != meta_pool) {
8945 dout(10) << " no object in pool " << info.pool
8946 << ", retrying pool " << meta_pool << dendl;
8947 info.pool = meta_pool;
8948 C_IO_MDC_OpenInoBacktraceFetched *fin =
8949 new C_IO_MDC_OpenInoBacktraceFetched(this, ino);
8950 fetch_backtrace(ino, info.pool, fin->bl,
8951 new C_OnFinisher(fin, mds->finisher));
8952 return;
8953 }
8954 err = 0; // backtrace.ancestors.empty() is checked below
8955 }
8956
8957 if (err == 0) {
8958 if (backtrace.ancestors.empty()) {
8959 dout(10) << " got empty backtrace " << dendl;
8960 err = -CEPHFS_ESTALE;
8961 } else if (!info.ancestors.empty()) {
8962 if (info.ancestors[0] == backtrace.ancestors[0]) {
8963 dout(10) << " got same parents " << info.ancestors[0] << " 2 times" << dendl;
8964 err = -CEPHFS_EINVAL;
8965 } else {
8966 info.last_err = 0;
8967 }
8968 }
8969 }
8970 if (err) {
8971 dout(0) << " failed to open ino " << ino << " err " << err << "/" << info.last_err << dendl;
8972 if (info.last_err)
8973 err = info.last_err;
8974 open_ino_finish(ino, info, err);
8975 return;
8976 }
8977
8978 dout(10) << " got backtrace " << backtrace << dendl;
8979 info.ancestors = backtrace.ancestors;
8980
8981 _open_ino_traverse_dir(ino, info, 0);
8982 }
8983
8984 void MDCache::_open_ino_parent_opened(inodeno_t ino, int ret)
8985 {
8986 dout(10) << "_open_ino_parent_opened ino " << ino << " ret " << ret << dendl;
8987
8988 open_ino_info_t& info = opening_inodes.at(ino);
8989
8990 CInode *in = get_inode(ino);
8991 if (in) {
8992 dout(10) << " found cached " << *in << dendl;
8993 open_ino_finish(ino, info, in->authority().first);
8994 return;
8995 }
8996
8997 if (ret == mds->get_nodeid()) {
8998 _open_ino_traverse_dir(ino, info, 0);
8999 } else {
9000 if (ret >= 0) {
9001 mds_rank_t checked_rank = mds_rank_t(ret);
9002 info.check_peers = true;
9003 info.auth_hint = checked_rank;
9004 info.checked.erase(checked_rank);
9005 }
9006 do_open_ino(ino, info, ret);
9007 }
9008 }
9009
9010 void MDCache::_open_ino_traverse_dir(inodeno_t ino, open_ino_info_t& info, int ret)
9011 {
9012 dout(10) << __func__ << ": ino " << ino << " ret " << ret << dendl;
9013
9014 CInode *in = get_inode(ino);
9015 if (in) {
9016 dout(10) << " found cached " << *in << dendl;
9017 open_ino_finish(ino, info, in->authority().first);
9018 return;
9019 }
9020
9021 if (ret) {
9022 do_open_ino(ino, info, ret);
9023 return;
9024 }
9025
9026 mds_rank_t hint = info.auth_hint;
9027 ret = open_ino_traverse_dir(ino, NULL, info.ancestors,
9028 info.discover, info.want_xlocked, &hint);
9029 if (ret > 0)
9030 return;
9031 if (hint != mds->get_nodeid())
9032 info.auth_hint = hint;
9033 do_open_ino(ino, info, ret);
9034 }
9035
9036 void MDCache::_open_ino_fetch_dir(inodeno_t ino, const cref_t<MMDSOpenIno> &m, bool parent,
9037 CDir *dir, std::string_view dname)
9038 {
9039 if (dir->state_test(CDir::STATE_REJOINUNDEF))
9040 ceph_assert(dir->get_inode()->dirfragtree.is_leaf(dir->get_frag()));
9041
9042 auto fin = new C_MDC_OpenInoTraverseDir(this, ino, m, parent);
9043 if (open_ino_batch && !dname.empty()) {
9044 auto& p = open_ino_batched_fetch[dir];
9045 p.first.emplace_back(dname);
9046 p.second.emplace_back(fin);
9047 return;
9048 }
9049
9050 dir->fetch(dname, CEPH_NOSNAP, fin);
9051 if (mds->logger)
9052 mds->logger->inc(l_mds_openino_dir_fetch);
9053 }
9054
9055 int MDCache::open_ino_traverse_dir(inodeno_t ino, const cref_t<MMDSOpenIno> &m,
9056 const vector<inode_backpointer_t>& ancestors,
9057 bool discover, bool want_xlocked, mds_rank_t *hint)
9058 {
9059 dout(10) << "open_ino_traverse_dir ino " << ino << " " << ancestors << dendl;
9060 int err = 0;
9061 for (unsigned i = 0; i < ancestors.size(); i++) {
9062 const auto& ancestor = ancestors.at(i);
9063 CInode *diri = get_inode(ancestor.dirino);
9064
9065 if (!diri) {
9066 if (discover && MDS_INO_IS_MDSDIR(ancestor.dirino)) {
9067 open_foreign_mdsdir(ancestor.dirino, new C_MDC_OpenInoTraverseDir(this, ino, m, i == 0));
9068 return 1;
9069 }
9070 continue;
9071 }
9072
9073 if (diri->state_test(CInode::STATE_REJOINUNDEF)) {
9074 CDentry *dn = diri->get_parent_dn();
9075 CDir *dir = dn->get_dir();
9076 while (dir->state_test(CDir::STATE_REJOINUNDEF) &&
9077 dir->get_inode()->state_test(CInode::STATE_REJOINUNDEF)) {
9078 dn = dir->get_inode()->get_parent_dn();
9079 dir = dn->get_dir();
9080 }
9081 _open_ino_fetch_dir(ino, m, i == 0, dir, dn->name);
9082 return 1;
9083 }
9084
9085 if (!diri->is_dir()) {
9086 dout(10) << " " << *diri << " is not dir" << dendl;
9087 if (i == 0)
9088 err = -CEPHFS_ENOTDIR;
9089 break;
9090 }
9091
9092 const string& name = ancestor.dname;
9093 frag_t fg = diri->pick_dirfrag(name);
9094 CDir *dir = diri->get_dirfrag(fg);
9095 if (!dir) {
9096 if (diri->is_auth()) {
9097 if (diri->is_frozen()) {
9098 dout(10) << " " << *diri << " is frozen, waiting " << dendl;
9099 diri->add_waiter(CDir::WAIT_UNFREEZE, new C_MDC_OpenInoTraverseDir(this, ino, m, i == 0));
9100 return 1;
9101 }
9102 dir = diri->get_or_open_dirfrag(this, fg);
9103 } else if (discover) {
9104 open_remote_dirfrag(diri, fg, new C_MDC_OpenInoTraverseDir(this, ino, m, i == 0));
9105 return 1;
9106 }
9107 }
9108 if (dir) {
9109 inodeno_t next_ino = i > 0 ? ancestors.at(i-1).dirino : ino;
9110 CDentry *dn = dir->lookup(name);
9111 CDentry::linkage_t *dnl = dn ? dn->get_linkage() : NULL;
9112 if (dir->is_auth()) {
9113 if (dnl && dnl->is_primary() &&
9114 dnl->get_inode()->state_test(CInode::STATE_REJOINUNDEF)) {
9115 dout(10) << " fetching undef " << *dnl->get_inode() << dendl;
9116 _open_ino_fetch_dir(ino, m, i == 0, dir, name);
9117 return 1;
9118 }
9119
9120 if (!dnl && !dir->is_complete() &&
9121 (!dir->has_bloom() || dir->is_in_bloom(name))) {
9122 dout(10) << " fetching incomplete " << *dir << dendl;
9123 _open_ino_fetch_dir(ino, m, i == 0, dir, name);
9124 return 1;
9125 }
9126
9127 dout(10) << " no ino " << next_ino << " in " << *dir << dendl;
9128 if (i == 0)
9129 err = -CEPHFS_ENOENT;
9130 } else if (discover) {
9131 if (!dnl) {
9132 filepath path(name, 0);
9133 discover_path(dir, CEPH_NOSNAP, path, new C_MDC_OpenInoTraverseDir(this, ino, m, i == 0),
9134 (i == 0 && want_xlocked));
9135 return 1;
9136 }
9137 if (dnl->is_null() && !dn->lock.can_read(-1)) {
9138 dout(10) << " null " << *dn << " is not readable, waiting" << dendl;
9139 dn->lock.add_waiter(SimpleLock::WAIT_RD, new C_MDC_OpenInoTraverseDir(this, ino, m, i == 0));
9140 return 1;
9141 }
9142 dout(10) << " no ino " << next_ino << " in " << *dir << dendl;
9143 if (i == 0)
9144 err = -CEPHFS_ENOENT;
9145 }
9146 }
9147 if (hint && i == 0)
9148 *hint = dir ? dir->authority().first : diri->authority().first;
9149 break;
9150 }
9151 return err;
9152 }
9153
9154 void MDCache::open_ino_finish(inodeno_t ino, open_ino_info_t& info, int ret)
9155 {
9156 dout(10) << "open_ino_finish ino " << ino << " ret " << ret << dendl;
9157
9158 MDSContext::vec waiters;
9159 waiters.swap(info.waiters);
9160 opening_inodes.erase(ino);
9161 finish_contexts(g_ceph_context, waiters, ret);
9162 }
9163
9164 void MDCache::do_open_ino(inodeno_t ino, open_ino_info_t& info, int err)
9165 {
9166 if (err < 0 && err != -CEPHFS_EAGAIN) {
9167 info.checked.clear();
9168 info.checking = MDS_RANK_NONE;
9169 info.check_peers = true;
9170 info.fetch_backtrace = true;
9171 if (info.discover) {
9172 info.discover = false;
9173 info.ancestors.clear();
9174 }
9175 if (err != -CEPHFS_ENOENT && err != -CEPHFS_ENOTDIR)
9176 info.last_err = err;
9177 }
9178
9179 if (info.check_peers || info.discover) {
9180 if (info.discover) {
9181 // got backtrace from peer, but failed to find inode. re-check peers
9182 info.discover = false;
9183 info.ancestors.clear();
9184 info.checked.clear();
9185 }
9186 info.check_peers = false;
9187 info.checking = MDS_RANK_NONE;
9188 do_open_ino_peer(ino, info);
9189 } else if (info.fetch_backtrace) {
9190 info.check_peers = true;
9191 info.fetch_backtrace = false;
9192 info.checking = mds->get_nodeid();
9193 info.checked.clear();
9194 C_IO_MDC_OpenInoBacktraceFetched *fin =
9195 new C_IO_MDC_OpenInoBacktraceFetched(this, ino);
9196 fetch_backtrace(ino, info.pool, fin->bl,
9197 new C_OnFinisher(fin, mds->finisher));
9198 } else {
9199 ceph_assert(!info.ancestors.empty());
9200 info.checking = mds->get_nodeid();
9201 open_ino(info.ancestors[0].dirino, mds->get_metadata_pool(),
9202 new C_MDC_OpenInoParentOpened(this, ino), info.want_replica);
9203 }
9204 }
9205
9206 void MDCache::do_open_ino_peer(inodeno_t ino, open_ino_info_t& info)
9207 {
9208 set<mds_rank_t> all, active;
9209 mds->mdsmap->get_mds_set(all);
9210 if (mds->get_state() == MDSMap::STATE_REJOIN)
9211 mds->mdsmap->get_mds_set_lower_bound(active, MDSMap::STATE_REJOIN);
9212 else
9213 mds->mdsmap->get_mds_set_lower_bound(active, MDSMap::STATE_CLIENTREPLAY);
9214
9215 dout(10) << "do_open_ino_peer " << ino << " active " << active
9216 << " all " << all << " checked " << info.checked << dendl;
9217
9218 mds_rank_t whoami = mds->get_nodeid();
9219 mds_rank_t peer = MDS_RANK_NONE;
9220 if (info.auth_hint >= 0 && info.auth_hint != whoami) {
9221 if (active.count(info.auth_hint)) {
9222 peer = info.auth_hint;
9223 info.auth_hint = MDS_RANK_NONE;
9224 }
9225 } else {
9226 for (set<mds_rank_t>::iterator p = active.begin(); p != active.end(); ++p)
9227 if (*p != whoami && info.checked.count(*p) == 0) {
9228 peer = *p;
9229 break;
9230 }
9231 }
9232 if (peer < 0) {
9233 all.erase(whoami);
9234 if (all != info.checked) {
9235 dout(10) << " waiting for more peers to be active" << dendl;
9236 } else {
9237 dout(10) << " all MDS peers have been checked " << dendl;
9238 do_open_ino(ino, info, 0);
9239 }
9240 } else {
9241 info.checking = peer;
9242 vector<inode_backpointer_t> *pa = NULL;
9243 // got backtrace from peer or backtrace just fetched
9244 if (info.discover || !info.fetch_backtrace)
9245 pa = &info.ancestors;
9246 mds->send_message_mds(make_message<MMDSOpenIno>(info.tid, ino, pa), peer);
9247 if (mds->logger)
9248 mds->logger->inc(l_mds_openino_peer_discover);
9249 }
9250 }
9251
9252 void MDCache::handle_open_ino(const cref_t<MMDSOpenIno> &m, int err)
9253 {
9254 if (mds->get_state() < MDSMap::STATE_REJOIN &&
9255 mds->get_want_state() != CEPH_MDS_STATE_REJOIN) {
9256 return;
9257 }
9258
9259 dout(10) << "handle_open_ino " << *m << " err " << err << dendl;
9260
9261 auto from = mds_rank_t(m->get_source().num());
9262 inodeno_t ino = m->ino;
9263 ref_t<MMDSOpenInoReply> reply;
9264 CInode *in = get_inode(ino);
9265 if (in) {
9266 dout(10) << " have " << *in << dendl;
9267 reply = make_message<MMDSOpenInoReply>(m->get_tid(), ino, mds_rank_t(0));
9268 if (in->is_auth()) {
9269 touch_inode(in);
9270 while (1) {
9271 CDentry *pdn = in->get_parent_dn();
9272 if (!pdn)
9273 break;
9274 CInode *diri = pdn->get_dir()->get_inode();
9275 reply->ancestors.push_back(inode_backpointer_t(diri->ino(), pdn->get_name(),
9276 in->get_version()));
9277 in = diri;
9278 }
9279 } else {
9280 reply->hint = in->authority().first;
9281 }
9282 } else if (err < 0) {
9283 reply = make_message<MMDSOpenInoReply>(m->get_tid(), ino, MDS_RANK_NONE, err);
9284 } else {
9285 mds_rank_t hint = MDS_RANK_NONE;
9286 int ret = open_ino_traverse_dir(ino, m, m->ancestors, false, false, &hint);
9287 if (ret > 0)
9288 return;
9289 reply = make_message<MMDSOpenInoReply>(m->get_tid(), ino, hint, ret);
9290 }
9291 mds->send_message_mds(reply, from);
9292 }
9293
9294 void MDCache::handle_open_ino_reply(const cref_t<MMDSOpenInoReply> &m)
9295 {
9296 dout(10) << "handle_open_ino_reply " << *m << dendl;
9297
9298 inodeno_t ino = m->ino;
9299 mds_rank_t from = mds_rank_t(m->get_source().num());
9300 auto it = opening_inodes.find(ino);
9301 if (it != opening_inodes.end() && it->second.checking == from) {
9302 open_ino_info_t& info = it->second;
9303 info.checking = MDS_RANK_NONE;
9304 info.checked.insert(from);
9305
9306 CInode *in = get_inode(ino);
9307 if (in) {
9308 dout(10) << " found cached " << *in << dendl;
9309 open_ino_finish(ino, info, in->authority().first);
9310 } else if (!m->ancestors.empty()) {
9311 dout(10) << " found ino " << ino << " on mds." << from << dendl;
9312 if (!info.want_replica) {
9313 open_ino_finish(ino, info, from);
9314 return;
9315 }
9316
9317 info.ancestors = m->ancestors;
9318 info.auth_hint = from;
9319 info.checking = mds->get_nodeid();
9320 info.discover = true;
9321 _open_ino_traverse_dir(ino, info, 0);
9322 } else if (m->error) {
9323 dout(10) << " error " << m->error << " from mds." << from << dendl;
9324 do_open_ino(ino, info, m->error);
9325 } else {
9326 if (m->hint >= 0 && m->hint != mds->get_nodeid()) {
9327 info.auth_hint = m->hint;
9328 info.checked.erase(m->hint);
9329 }
9330 do_open_ino_peer(ino, info);
9331 }
9332 }
9333 }
9334
9335 void MDCache::kick_open_ino_peers(mds_rank_t who)
9336 {
9337 dout(10) << "kick_open_ino_peers mds." << who << dendl;
9338
9339 for (map<inodeno_t, open_ino_info_t>::iterator p = opening_inodes.begin();
9340 p != opening_inodes.end();
9341 ++p) {
9342 open_ino_info_t& info = p->second;
9343 if (info.checking == who) {
9344 dout(10) << " kicking ino " << p->first << " who was checking mds." << who << dendl;
9345 info.checking = MDS_RANK_NONE;
9346 do_open_ino_peer(p->first, info);
9347 } else if (info.checking == MDS_RANK_NONE) {
9348 dout(10) << " kicking ino " << p->first << " who was waiting" << dendl;
9349 do_open_ino_peer(p->first, info);
9350 }
9351 }
9352 }
9353
9354 void MDCache::open_ino_batch_start()
9355 {
9356 dout(10) << __func__ << dendl;
9357 open_ino_batch = true;
9358 }
9359
9360 void MDCache::open_ino_batch_submit()
9361 {
9362 dout(10) << __func__ << dendl;
9363 open_ino_batch = false;
9364
9365 for (auto& [dir, p] : open_ino_batched_fetch) {
9366 CInode *in = dir->inode;
9367 std::vector<dentry_key_t> keys;
9368 for (auto& dname : p.first)
9369 keys.emplace_back(CEPH_NOSNAP, dname, in->hash_dentry_name(dname));
9370 dir->fetch_keys(keys,
9371 new MDSInternalContextWrapper(mds,
9372 new LambdaContext([this, waiters = std::move(p.second)](int r) mutable {
9373 mds->queue_waiters_front(waiters);
9374 })
9375 )
9376 );
9377 if (mds->logger)
9378 mds->logger->inc(l_mds_openino_dir_fetch);
9379 }
9380 open_ino_batched_fetch.clear();
9381 }
9382
9383 void MDCache::open_ino(inodeno_t ino, int64_t pool, MDSContext* fin,
9384 bool want_replica, bool want_xlocked,
9385 vector<inode_backpointer_t> *ancestors_hint,
9386 mds_rank_t auth_hint)
9387 {
9388 dout(10) << "open_ino " << ino << " pool " << pool << " want_replica "
9389 << want_replica << dendl;
9390
9391 auto it = opening_inodes.find(ino);
9392 if (it != opening_inodes.end()) {
9393 open_ino_info_t& info = it->second;
9394 if (want_replica) {
9395 info.want_replica = true;
9396 if (want_xlocked && !info.want_xlocked) {
9397 if (!info.ancestors.empty()) {
9398 CInode *diri = get_inode(info.ancestors[0].dirino);
9399 if (diri) {
9400 frag_t fg = diri->pick_dirfrag(info.ancestors[0].dname);
9401 CDir *dir = diri->get_dirfrag(fg);
9402 if (dir && !dir->is_auth()) {
9403 filepath path(info.ancestors[0].dname, 0);
9404 discover_path(dir, CEPH_NOSNAP, path, NULL, true);
9405 }
9406 }
9407 }
9408 info.want_xlocked = true;
9409 }
9410 }
9411 info.waiters.push_back(fin);
9412 } else {
9413 open_ino_info_t& info = opening_inodes[ino];
9414 info.want_replica = want_replica;
9415 info.want_xlocked = want_xlocked;
9416 info.tid = ++open_ino_last_tid;
9417 info.pool = pool >= 0 ? pool : default_file_layout.pool_id;
9418 info.waiters.push_back(fin);
9419 if (auth_hint != MDS_RANK_NONE)
9420 info.auth_hint = auth_hint;
9421 if (ancestors_hint) {
9422 info.ancestors = std::move(*ancestors_hint);
9423 info.fetch_backtrace = false;
9424 info.checking = mds->get_nodeid();
9425 _open_ino_traverse_dir(ino, info, 0);
9426 } else {
9427 do_open_ino(ino, info, 0);
9428 }
9429 }
9430 }
9431
9432 /* ---------------------------- */
9433
9434 /*
9435 * search for a given inode on MDS peers. optionally start with the given node.
9436
9437
9438 TODO
9439 - recover from mds node failure, recovery
9440 - traverse path
9441
9442 */
9443 void MDCache::find_ino_peers(inodeno_t ino, MDSContext *c,
9444 mds_rank_t hint, bool path_locked)
9445 {
9446 dout(5) << "find_ino_peers " << ino << " hint " << hint << dendl;
9447 CInode *in = get_inode(ino);
9448 if (in && in->state_test(CInode::STATE_PURGING)) {
9449 c->complete(-CEPHFS_ESTALE);
9450 return;
9451 }
9452 ceph_assert(!in);
9453
9454 ceph_tid_t tid = ++find_ino_peer_last_tid;
9455 find_ino_peer_info_t& fip = find_ino_peer[tid];
9456 fip.ino = ino;
9457 fip.tid = tid;
9458 fip.fin = c;
9459 fip.path_locked = path_locked;
9460 fip.hint = hint;
9461 _do_find_ino_peer(fip);
9462 }
9463
9464 void MDCache::_do_find_ino_peer(find_ino_peer_info_t& fip)
9465 {
9466 set<mds_rank_t> all, active;
9467 mds->mdsmap->get_mds_set(all);
9468 mds->mdsmap->get_mds_set_lower_bound(active, MDSMap::STATE_CLIENTREPLAY);
9469
9470 dout(10) << "_do_find_ino_peer " << fip.tid << " " << fip.ino
9471 << " active " << active << " all " << all
9472 << " checked " << fip.checked
9473 << dendl;
9474
9475 mds_rank_t m = MDS_RANK_NONE;
9476 if (fip.hint >= 0) {
9477 m = fip.hint;
9478 fip.hint = MDS_RANK_NONE;
9479 } else {
9480 for (set<mds_rank_t>::iterator p = active.begin(); p != active.end(); ++p)
9481 if (*p != mds->get_nodeid() &&
9482 fip.checked.count(*p) == 0) {
9483 m = *p;
9484 break;
9485 }
9486 }
9487 if (m == MDS_RANK_NONE) {
9488 all.erase(mds->get_nodeid());
9489 if (all != fip.checked) {
9490 dout(10) << "_do_find_ino_peer waiting for more peers to be active" << dendl;
9491 } else {
9492 dout(10) << "_do_find_ino_peer failed on " << fip.ino << dendl;
9493 fip.fin->complete(-CEPHFS_ESTALE);
9494 find_ino_peer.erase(fip.tid);
9495 }
9496 } else {
9497 fip.checking = m;
9498 mds->send_message_mds(make_message<MMDSFindIno>(fip.tid, fip.ino), m);
9499 }
9500 }
9501
9502 void MDCache::handle_find_ino(const cref_t<MMDSFindIno> &m)
9503 {
9504 if (mds->get_state() < MDSMap::STATE_REJOIN) {
9505 return;
9506 }
9507
9508 dout(10) << "handle_find_ino " << *m << dendl;
9509 auto r = make_message<MMDSFindInoReply>(m->tid);
9510 CInode *in = get_inode(m->ino);
9511 if (in) {
9512 in->make_path(r->path);
9513 dout(10) << " have " << r->path << " " << *in << dendl;
9514
9515 /*
9516 * If the the CInode was just created by using openc in current
9517 * auth MDS, but the client just sends a getattr request to another
9518 * replica MDS. Then here it will make a path of '#INODE-NUMBER'
9519 * only because the CInode hasn't been linked yet, and the replica
9520 * MDS will keep retrying until the auth MDS flushes the mdlog and
9521 * the C_MDS_openc_finish and link_primary_inode are called at most
9522 * 5 seconds later.
9523 */
9524 if (!in->get_parent_dn() && in->is_auth()) {
9525 mds->mdlog->flush();
9526 }
9527 }
9528 mds->send_message_mds(r, mds_rank_t(m->get_source().num()));
9529 }
9530
9531
9532 void MDCache::handle_find_ino_reply(const cref_t<MMDSFindInoReply> &m)
9533 {
9534 auto p = find_ino_peer.find(m->tid);
9535 if (p != find_ino_peer.end()) {
9536 dout(10) << "handle_find_ino_reply " << *m << dendl;
9537 find_ino_peer_info_t& fip = p->second;
9538
9539 // success?
9540 if (get_inode(fip.ino)) {
9541 dout(10) << "handle_find_ino_reply successfully found " << fip.ino << dendl;
9542 mds->queue_waiter(fip.fin);
9543 find_ino_peer.erase(p);
9544 return;
9545 }
9546
9547 mds_rank_t from = mds_rank_t(m->get_source().num());
9548 if (fip.checking == from)
9549 fip.checking = MDS_RANK_NONE;
9550 fip.checked.insert(from);
9551
9552 if (!m->path.empty()) {
9553 // we got a path!
9554 vector<CDentry*> trace;
9555 CF_MDS_RetryMessageFactory cf(mds, m);
9556 MDRequestRef null_ref;
9557 int flags = MDS_TRAVERSE_DISCOVER;
9558 if (fip.path_locked)
9559 flags |= MDS_TRAVERSE_PATH_LOCKED;
9560 int r = path_traverse(null_ref, cf, m->path, flags, &trace);
9561 if (r > 0)
9562 return;
9563 dout(0) << "handle_find_ino_reply failed with " << r << " on " << m->path
9564 << ", retrying" << dendl;
9565 fip.checked.clear();
9566 _do_find_ino_peer(fip);
9567 } else {
9568 // nope, continue.
9569 _do_find_ino_peer(fip);
9570 }
9571 } else {
9572 dout(10) << "handle_find_ino_reply tid " << m->tid << " dne" << dendl;
9573 }
9574 }
9575
9576 void MDCache::kick_find_ino_peers(mds_rank_t who)
9577 {
9578 // find_ino_peers requests we should move on from
9579 for (map<ceph_tid_t,find_ino_peer_info_t>::iterator p = find_ino_peer.begin();
9580 p != find_ino_peer.end();
9581 ++p) {
9582 find_ino_peer_info_t& fip = p->second;
9583 if (fip.checking == who) {
9584 dout(10) << "kicking find_ino_peer " << fip.tid << " who was checking mds." << who << dendl;
9585 fip.checking = MDS_RANK_NONE;
9586 _do_find_ino_peer(fip);
9587 } else if (fip.checking == MDS_RANK_NONE) {
9588 dout(10) << "kicking find_ino_peer " << fip.tid << " who was waiting" << dendl;
9589 _do_find_ino_peer(fip);
9590 }
9591 }
9592 }
9593
9594 /* ---------------------------- */
9595
9596 int MDCache::get_num_client_requests()
9597 {
9598 int count = 0;
9599 for (ceph::unordered_map<metareqid_t, MDRequestRef>::iterator p = active_requests.begin();
9600 p != active_requests.end();
9601 ++p) {
9602 MDRequestRef& mdr = p->second;
9603 if (mdr->reqid.name.is_client() && !mdr->is_peer())
9604 count++;
9605 }
9606 return count;
9607 }
9608
9609 MDRequestRef MDCache::request_start(const cref_t<MClientRequest>& req)
9610 {
9611 // did we win a forward race against a peer?
9612 if (active_requests.count(req->get_reqid())) {
9613 MDRequestRef& mdr = active_requests[req->get_reqid()];
9614 ceph_assert(mdr);
9615 if (mdr->is_peer()) {
9616 dout(10) << "request_start already had " << *mdr << ", waiting for finish" << dendl;
9617 mdr->more()->waiting_for_finish.push_back(new C_MDS_RetryMessage(mds, req));
9618 } else {
9619 dout(10) << "request_start already processing " << *mdr << ", dropping new msg" << dendl;
9620 }
9621 return MDRequestRef();
9622 }
9623
9624 // register new client request
9625 MDRequestImpl::Params params;
9626 params.reqid = req->get_reqid();
9627 params.attempt = req->get_num_fwd();
9628 params.client_req = req;
9629 params.initiated = req->get_recv_stamp();
9630 params.throttled = req->get_throttle_stamp();
9631 params.all_read = req->get_recv_complete_stamp();
9632 params.dispatched = req->get_dispatch_stamp();
9633
9634 MDRequestRef mdr =
9635 mds->op_tracker.create_request<MDRequestImpl,MDRequestImpl::Params*>(&params);
9636 active_requests[params.reqid] = mdr;
9637 mdr->set_op_stamp(req->get_stamp());
9638 dout(7) << "request_start " << *mdr << dendl;
9639 return mdr;
9640 }
9641
9642 MDRequestRef MDCache::request_start_peer(metareqid_t ri, __u32 attempt, const cref_t<Message> &m)
9643 {
9644 int by = m->get_source().num();
9645 MDRequestImpl::Params params;
9646 params.reqid = ri;
9647 params.attempt = attempt;
9648 params.triggering_peer_req = m;
9649 params.peer_to = by;
9650 params.initiated = m->get_recv_stamp();
9651 params.throttled = m->get_throttle_stamp();
9652 params.all_read = m->get_recv_complete_stamp();
9653 params.dispatched = m->get_dispatch_stamp();
9654 MDRequestRef mdr =
9655 mds->op_tracker.create_request<MDRequestImpl,MDRequestImpl::Params*>(&params);
9656 ceph_assert(active_requests.count(mdr->reqid) == 0);
9657 active_requests[mdr->reqid] = mdr;
9658 dout(7) << "request_start_peer " << *mdr << " by mds." << by << dendl;
9659 return mdr;
9660 }
9661
9662 MDRequestRef MDCache::request_start_internal(int op)
9663 {
9664 utime_t now = ceph_clock_now();
9665 MDRequestImpl::Params params;
9666 params.reqid.name = entity_name_t::MDS(mds->get_nodeid());
9667 params.reqid.tid = mds->issue_tid();
9668 params.initiated = now;
9669 params.throttled = now;
9670 params.all_read = now;
9671 params.dispatched = now;
9672 params.internal_op = op;
9673 MDRequestRef mdr =
9674 mds->op_tracker.create_request<MDRequestImpl,MDRequestImpl::Params*>(&params);
9675
9676 if (active_requests.count(mdr->reqid)) {
9677 auto& _mdr = active_requests[mdr->reqid];
9678 dout(0) << __func__ << " existing " << *_mdr << " op " << _mdr->internal_op << dendl;
9679 dout(0) << __func__ << " new " << *mdr << " op " << op << dendl;
9680 ceph_abort();
9681 }
9682 active_requests[mdr->reqid] = mdr;
9683 dout(7) << __func__ << " " << *mdr << " op " << op << dendl;
9684 return mdr;
9685 }
9686
9687 MDRequestRef MDCache::request_get(metareqid_t rid)
9688 {
9689 ceph::unordered_map<metareqid_t, MDRequestRef>::iterator p = active_requests.find(rid);
9690 ceph_assert(p != active_requests.end());
9691 dout(7) << "request_get " << rid << " " << *p->second << dendl;
9692 return p->second;
9693 }
9694
9695 void MDCache::request_finish(MDRequestRef& mdr)
9696 {
9697 dout(7) << "request_finish " << *mdr << dendl;
9698 mdr->mark_event("finishing request");
9699
9700 // peer finisher?
9701 if (mdr->has_more() && mdr->more()->peer_commit) {
9702 Context *fin = mdr->more()->peer_commit;
9703 mdr->more()->peer_commit = 0;
9704 int ret;
9705 if (mdr->aborted) {
9706 mdr->aborted = false;
9707 ret = -1;
9708 mdr->more()->peer_rolling_back = true;
9709 } else {
9710 ret = 0;
9711 mdr->committing = true;
9712 }
9713 fin->complete(ret); // this must re-call request_finish.
9714 return;
9715 }
9716
9717 switch(mdr->internal_op) {
9718 case CEPH_MDS_OP_FRAGMENTDIR:
9719 logger->inc(l_mdss_ireq_fragmentdir);
9720 break;
9721 case CEPH_MDS_OP_EXPORTDIR:
9722 logger->inc(l_mdss_ireq_exportdir);
9723 break;
9724 case CEPH_MDS_OP_ENQUEUE_SCRUB:
9725 logger->inc(l_mdss_ireq_enqueue_scrub);
9726 break;
9727 case CEPH_MDS_OP_FLUSH:
9728 logger->inc(l_mdss_ireq_flush);
9729 break;
9730 case CEPH_MDS_OP_REPAIR_FRAGSTATS:
9731 logger->inc(l_mdss_ireq_fragstats);
9732 break;
9733 case CEPH_MDS_OP_REPAIR_INODESTATS:
9734 logger->inc(l_mdss_ireq_inodestats);
9735 break;
9736 }
9737
9738 request_cleanup(mdr);
9739 }
9740
9741
9742 void MDCache::request_forward(MDRequestRef& mdr, mds_rank_t who, int port)
9743 {
9744 CachedStackStringStream css;
9745 *css << "forwarding request to mds." << who;
9746 mdr->mark_event(css->strv());
9747 if (mdr->client_request && mdr->client_request->get_source().is_client()) {
9748 dout(7) << "request_forward " << *mdr << " to mds." << who << " req "
9749 << *mdr->client_request << dendl;
9750 if (mdr->is_batch_head()) {
9751 mdr->release_batch_op()->forward(who);
9752 } else {
9753 mds->forward_message_mds(mdr, who);
9754 }
9755 if (mds->logger) mds->logger->inc(l_mds_forward);
9756 } else if (mdr->internal_op >= 0) {
9757 dout(10) << "request_forward on internal op; cancelling" << dendl;
9758 mdr->internal_op_finish->complete(-CEPHFS_EXDEV);
9759 } else {
9760 dout(7) << "request_forward drop " << *mdr << " req " << *mdr->client_request
9761 << " was from mds" << dendl;
9762 }
9763 request_cleanup(mdr);
9764 }
9765
9766
9767 void MDCache::dispatch_request(MDRequestRef& mdr)
9768 {
9769 if (mdr->client_request) {
9770 mds->server->dispatch_client_request(mdr);
9771 } else if (mdr->peer_request) {
9772 mds->server->dispatch_peer_request(mdr);
9773 } else {
9774 switch (mdr->internal_op) {
9775 case CEPH_MDS_OP_FRAGMENTDIR:
9776 dispatch_fragment_dir(mdr);
9777 break;
9778 case CEPH_MDS_OP_EXPORTDIR:
9779 migrator->dispatch_export_dir(mdr, 0);
9780 break;
9781 case CEPH_MDS_OP_ENQUEUE_SCRUB:
9782 enqueue_scrub_work(mdr);
9783 break;
9784 case CEPH_MDS_OP_FLUSH:
9785 flush_dentry_work(mdr);
9786 break;
9787 case CEPH_MDS_OP_REPAIR_FRAGSTATS:
9788 repair_dirfrag_stats_work(mdr);
9789 break;
9790 case CEPH_MDS_OP_REPAIR_INODESTATS:
9791 repair_inode_stats_work(mdr);
9792 break;
9793 case CEPH_MDS_OP_RDLOCK_FRAGSSTATS:
9794 rdlock_dirfrags_stats_work(mdr);
9795 break;
9796 default:
9797 ceph_abort();
9798 }
9799 }
9800 }
9801
9802
9803 void MDCache::request_drop_foreign_locks(MDRequestRef& mdr)
9804 {
9805 if (!mdr->has_more())
9806 return;
9807
9808 // clean up peers
9809 // (will implicitly drop remote dn pins)
9810 for (set<mds_rank_t>::iterator p = mdr->more()->peers.begin();
9811 p != mdr->more()->peers.end();
9812 ++p) {
9813 auto r = make_message<MMDSPeerRequest>(mdr->reqid, mdr->attempt,
9814 MMDSPeerRequest::OP_FINISH);
9815
9816 if (mdr->killed && !mdr->committing) {
9817 r->mark_abort();
9818 } else if (mdr->more()->srcdn_auth_mds == *p &&
9819 mdr->more()->inode_import.length() > 0) {
9820 // information about rename imported caps
9821 r->inode_export = std::move(mdr->more()->inode_import);
9822 }
9823
9824 mds->send_message_mds(r, *p);
9825 }
9826
9827 /* strip foreign xlocks out of lock lists, since the OP_FINISH drops them
9828 * implicitly. Note that we don't call the finishers -- there shouldn't
9829 * be any on a remote lock and the request finish wakes up all
9830 * the waiters anyway! */
9831
9832 for (auto it = mdr->locks.begin(); it != mdr->locks.end(); ) {
9833 SimpleLock *lock = it->lock;
9834 if (it->is_xlock() && !lock->get_parent()->is_auth()) {
9835 dout(10) << "request_drop_foreign_locks forgetting lock " << *lock
9836 << " on " << lock->get_parent() << dendl;
9837 lock->put_xlock();
9838 mdr->locks.erase(it++);
9839 } else if (it->is_remote_wrlock()) {
9840 dout(10) << "request_drop_foreign_locks forgetting remote_wrlock " << *lock
9841 << " on mds." << it->wrlock_target << " on " << *lock->get_parent() << dendl;
9842 if (it->is_wrlock()) {
9843 it->clear_remote_wrlock();
9844 ++it;
9845 } else {
9846 mdr->locks.erase(it++);
9847 }
9848 } else {
9849 ++it;
9850 }
9851 }
9852
9853 mdr->more()->peers.clear(); /* we no longer have requests out to them, and
9854 * leaving them in can cause double-notifies as
9855 * this function can get called more than once */
9856 }
9857
9858 void MDCache::request_drop_non_rdlocks(MDRequestRef& mdr)
9859 {
9860 request_drop_foreign_locks(mdr);
9861 mds->locker->drop_non_rdlocks(mdr.get());
9862 }
9863
9864 void MDCache::request_drop_locks(MDRequestRef& mdr)
9865 {
9866 request_drop_foreign_locks(mdr);
9867 mds->locker->drop_locks(mdr.get());
9868 }
9869
9870 void MDCache::request_cleanup(MDRequestRef& mdr)
9871 {
9872 dout(15) << "request_cleanup " << *mdr << dendl;
9873
9874 if (mdr->has_more()) {
9875 if (mdr->more()->is_ambiguous_auth)
9876 mdr->clear_ambiguous_auth();
9877 if (!mdr->more()->waiting_for_finish.empty())
9878 mds->queue_waiters(mdr->more()->waiting_for_finish);
9879 }
9880
9881 request_drop_locks(mdr);
9882
9883 // drop (local) auth pins
9884 mdr->drop_local_auth_pins();
9885
9886 // drop stickydirs
9887 mdr->put_stickydirs();
9888
9889 mds->locker->kick_cap_releases(mdr);
9890
9891 // drop cache pins
9892 mdr->drop_pins();
9893
9894 // remove from session
9895 mdr->item_session_request.remove_myself();
9896
9897 // remove from map
9898 active_requests.erase(mdr->reqid);
9899
9900 if (mds->logger)
9901 log_stat();
9902
9903 mdr->mark_event("cleaned up request");
9904 }
9905
9906 void MDCache::request_kill(MDRequestRef& mdr)
9907 {
9908 // rollback peer requests is tricky. just let the request proceed.
9909 if (mdr->has_more() &&
9910 (!mdr->more()->witnessed.empty() || !mdr->more()->waiting_on_peer.empty())) {
9911 if (!(mdr->locking_state & MutationImpl::ALL_LOCKED)) {
9912 ceph_assert(mdr->more()->witnessed.empty());
9913 mdr->aborted = true;
9914 dout(10) << "request_kill " << *mdr << " -- waiting for peer reply, delaying" << dendl;
9915 } else {
9916 dout(10) << "request_kill " << *mdr << " -- already started peer prep, no-op" << dendl;
9917 }
9918
9919 ceph_assert(mdr->used_prealloc_ino == 0);
9920 ceph_assert(mdr->prealloc_inos.empty());
9921
9922 mdr->session = NULL;
9923 mdr->item_session_request.remove_myself();
9924 return;
9925 }
9926
9927 mdr->killed = true;
9928 mdr->mark_event("killing request");
9929
9930 if (mdr->committing) {
9931 dout(10) << "request_kill " << *mdr << " -- already committing, remove it from sesssion requests" << dendl;
9932 mdr->item_session_request.remove_myself();
9933 } else {
9934 dout(10) << "request_kill " << *mdr << dendl;
9935 request_cleanup(mdr);
9936 }
9937 }
9938
9939 // -------------------------------------------------------------------------------
9940 // SNAPREALMS
9941
9942 void MDCache::create_global_snaprealm()
9943 {
9944 CInode *in = new CInode(this); // dummy inode
9945 create_unlinked_system_inode(in, CEPH_INO_GLOBAL_SNAPREALM, S_IFDIR|0755);
9946 add_inode(in);
9947 global_snaprealm = in->snaprealm;
9948 }
9949
9950 void MDCache::do_realm_invalidate_and_update_notify(CInode *in, int snapop, bool notify_clients)
9951 {
9952 dout(10) << "do_realm_invalidate_and_update_notify " << *in->snaprealm << " " << *in << dendl;
9953
9954 vector<inodeno_t> split_inos;
9955 vector<inodeno_t> split_realms;
9956
9957 if (notify_clients) {
9958 if (snapop == CEPH_SNAP_OP_SPLIT) {
9959 // notify clients of update|split
9960 for (auto p = in->snaprealm->inodes_with_caps.begin(); !p.end(); ++p)
9961 split_inos.push_back((*p)->ino());
9962
9963 for (auto& r : in->snaprealm->open_children)
9964 split_realms.push_back(r->inode->ino());
9965 }
9966 }
9967
9968 map<client_t, ref_t<MClientSnap>> updates;
9969 list<SnapRealm*> q;
9970 q.push_back(in->snaprealm);
9971 while (!q.empty()) {
9972 SnapRealm *realm = q.front();
9973 q.pop_front();
9974
9975 dout(10) << " realm " << *realm << " on " << *realm->inode << dendl;
9976 realm->invalidate_cached_snaps();
9977
9978 if (notify_clients) {
9979 for (const auto& p : realm->client_caps) {
9980 const auto& client = p.first;
9981 const auto& caps = p.second;
9982 ceph_assert(!caps->empty());
9983
9984 auto em = updates.emplace(std::piecewise_construct, std::forward_as_tuple(client), std::forward_as_tuple());
9985 if (em.second) {
9986 auto update = make_message<MClientSnap>(CEPH_SNAP_OP_SPLIT);
9987 update->head.split = in->ino();
9988 update->split_inos = split_inos;
9989 update->split_realms = split_realms;
9990 update->bl = mds->server->get_snap_trace(em.first->first, in->snaprealm);
9991 em.first->second = std::move(update);
9992 }
9993 }
9994 }
9995
9996 // notify for active children, too.
9997 dout(10) << " " << realm << " open_children are " << realm->open_children << dendl;
9998 for (auto& r : realm->open_children)
9999 q.push_back(r);
10000 }
10001
10002 if (notify_clients)
10003 send_snaps(updates);
10004 }
10005
10006 void MDCache::send_snap_update(CInode *in, version_t stid, int snap_op)
10007 {
10008 dout(10) << __func__ << " " << *in << " stid " << stid << dendl;
10009 ceph_assert(in->is_auth());
10010
10011 set<mds_rank_t> mds_set;
10012 if (stid > 0) {
10013 mds->mdsmap->get_mds_set_lower_bound(mds_set, MDSMap::STATE_RESOLVE);
10014 mds_set.erase(mds->get_nodeid());
10015 } else {
10016 in->list_replicas(mds_set);
10017 }
10018
10019 if (!mds_set.empty()) {
10020 bufferlist snap_blob;
10021 in->encode_snap(snap_blob);
10022
10023 for (auto p : mds_set) {
10024 auto m = make_message<MMDSSnapUpdate>(in->ino(), stid, snap_op);
10025 m->snap_blob = snap_blob;
10026 mds->send_message_mds(m, p);
10027 }
10028 }
10029
10030 if (stid > 0)
10031 notify_global_snaprealm_update(snap_op);
10032 }
10033
10034 void MDCache::handle_snap_update(const cref_t<MMDSSnapUpdate> &m)
10035 {
10036 mds_rank_t from = mds_rank_t(m->get_source().num());
10037 dout(10) << __func__ << " " << *m << " from mds." << from << dendl;
10038
10039 if (mds->get_state() < MDSMap::STATE_RESOLVE &&
10040 mds->get_want_state() != CEPH_MDS_STATE_RESOLVE) {
10041 return;
10042 }
10043
10044 // null rejoin_done means open_snaprealms() has already been called
10045 bool notify_clients = mds->get_state() > MDSMap::STATE_REJOIN ||
10046 (mds->is_rejoin() && !rejoin_done);
10047
10048 if (m->get_tid() > 0) {
10049 mds->snapclient->notify_commit(m->get_tid());
10050 if (notify_clients)
10051 notify_global_snaprealm_update(m->get_snap_op());
10052 }
10053
10054 CInode *in = get_inode(m->get_ino());
10055 if (in) {
10056 ceph_assert(!in->is_auth());
10057 if (mds->get_state() > MDSMap::STATE_REJOIN ||
10058 (mds->is_rejoin() && !in->is_rejoining())) {
10059 auto p = m->snap_blob.cbegin();
10060 in->decode_snap(p);
10061
10062 if (!notify_clients) {
10063 if (!rejoin_pending_snaprealms.count(in)) {
10064 in->get(CInode::PIN_OPENINGSNAPPARENTS);
10065 rejoin_pending_snaprealms.insert(in);
10066 }
10067 }
10068 do_realm_invalidate_and_update_notify(in, m->get_snap_op(), notify_clients);
10069 }
10070 }
10071 }
10072
10073 void MDCache::notify_global_snaprealm_update(int snap_op)
10074 {
10075 if (snap_op != CEPH_SNAP_OP_DESTROY)
10076 snap_op = CEPH_SNAP_OP_UPDATE;
10077 set<Session*> sessions;
10078 mds->sessionmap.get_client_session_set(sessions);
10079 for (auto &session : sessions) {
10080 if (!session->is_open() && !session->is_stale())
10081 continue;
10082 auto update = make_message<MClientSnap>(snap_op);
10083 update->head.split = global_snaprealm->inode->ino();
10084 update->bl = mds->server->get_snap_trace(session, global_snaprealm);
10085 mds->send_message_client_counted(update, session);
10086 }
10087 }
10088
10089 // -------------------------------------------------------------------------------
10090 // STRAYS
10091
10092 struct C_MDC_RetryScanStray : public MDCacheContext {
10093 dirfrag_t next;
10094 C_MDC_RetryScanStray(MDCache *c, dirfrag_t n) : MDCacheContext(c), next(n) { }
10095 void finish(int r) override {
10096 mdcache->scan_stray_dir(next);
10097 }
10098 };
10099
10100 void MDCache::scan_stray_dir(dirfrag_t next)
10101 {
10102 dout(10) << "scan_stray_dir " << next << dendl;
10103
10104 if (next.ino)
10105 next.frag = strays[MDS_INO_STRAY_INDEX(next.ino)]->dirfragtree[next.frag.value()];
10106
10107 for (int i = 0; i < NUM_STRAY; ++i) {
10108 if (strays[i]->ino() < next.ino)
10109 continue;
10110
10111 std::vector<CDir*> ls;
10112 strays[i]->get_dirfrags(ls);
10113
10114 for (const auto& dir : ls) {
10115 if (dir->get_frag() < next.frag)
10116 continue;
10117
10118 if (!dir->can_auth_pin()) {
10119 dir->add_waiter(CDir::WAIT_UNFREEZE, new C_MDC_RetryScanStray(this, dir->dirfrag()));
10120 return;
10121 }
10122
10123 if (!dir->is_complete()) {
10124 dir->fetch(new C_MDC_RetryScanStray(this, dir->dirfrag()));
10125 return;
10126 }
10127
10128 for (auto &p : dir->items) {
10129 CDentry *dn = p.second;
10130 dn->state_set(CDentry::STATE_STRAY);
10131 CDentry::linkage_t *dnl = dn->get_projected_linkage();
10132 if (dnl->is_primary()) {
10133 CInode *in = dnl->get_inode();
10134 if (in->get_inode()->nlink == 0)
10135 in->state_set(CInode::STATE_ORPHAN);
10136 maybe_eval_stray(in);
10137 }
10138 }
10139 }
10140 next.frag = frag_t();
10141 }
10142 }
10143
10144 void MDCache::fetch_backtrace(inodeno_t ino, int64_t pool, bufferlist& bl, Context *fin)
10145 {
10146 object_t oid = CInode::get_object_name(ino, frag_t(), "");
10147 mds->objecter->getxattr(oid, object_locator_t(pool), "parent", CEPH_NOSNAP, &bl, 0, fin);
10148 if (mds->logger)
10149 mds->logger->inc(l_mds_openino_backtrace_fetch);
10150 }
10151
10152
10153
10154
10155
10156 // ========================================================================================
10157 // DISCOVER
10158 /*
10159
10160 - for all discovers (except base_inos, e.g. root, stray), waiters are attached
10161 to the parent metadata object in the cache (pinning it).
10162
10163 - all discovers are tracked by tid, so that we can ignore potentially dup replies.
10164
10165 */
10166
10167 void MDCache::_send_discover(discover_info_t& d)
10168 {
10169 auto dis = make_message<MDiscover>(d.ino, d.frag, d.snap, d.want_path,
10170 d.want_base_dir, d.path_locked);
10171 logger->inc(l_mdc_dir_send_discover);
10172 dis->set_tid(d.tid);
10173 mds->send_message_mds(dis, d.mds);
10174 }
10175
10176 void MDCache::discover_base_ino(inodeno_t want_ino,
10177 MDSContext *onfinish,
10178 mds_rank_t from)
10179 {
10180 dout(7) << "discover_base_ino " << want_ino << " from mds." << from << dendl;
10181 if (waiting_for_base_ino[from].count(want_ino) == 0) {
10182 discover_info_t& d = _create_discover(from);
10183 d.ino = want_ino;
10184 _send_discover(d);
10185 }
10186 waiting_for_base_ino[from][want_ino].push_back(onfinish);
10187 }
10188
10189
10190 void MDCache::discover_dir_frag(CInode *base,
10191 frag_t approx_fg,
10192 MDSContext *onfinish,
10193 mds_rank_t from)
10194 {
10195 if (from < 0)
10196 from = base->authority().first;
10197
10198 dirfrag_t df(base->ino(), approx_fg);
10199 dout(7) << "discover_dir_frag " << df
10200 << " from mds." << from << dendl;
10201
10202 if (!base->is_waiting_for_dir(approx_fg) || !onfinish) {
10203 discover_info_t& d = _create_discover(from);
10204 d.pin_base(base);
10205 d.ino = base->ino();
10206 d.frag = approx_fg;
10207 d.want_base_dir = true;
10208 _send_discover(d);
10209 }
10210
10211 if (onfinish)
10212 base->add_dir_waiter(approx_fg, onfinish);
10213 }
10214
10215 struct C_MDC_RetryDiscoverPath : public MDCacheContext {
10216 CInode *base;
10217 snapid_t snapid;
10218 filepath path;
10219 mds_rank_t from;
10220 C_MDC_RetryDiscoverPath(MDCache *c, CInode *b, snapid_t s, filepath &p, mds_rank_t f) :
10221 MDCacheContext(c), base(b), snapid(s), path(p), from(f) {}
10222 void finish(int r) override {
10223 mdcache->discover_path(base, snapid, path, 0, from);
10224 }
10225 };
10226
10227 void MDCache::discover_path(CInode *base,
10228 snapid_t snap,
10229 filepath want_path,
10230 MDSContext *onfinish,
10231 bool path_locked,
10232 mds_rank_t from)
10233 {
10234 if (from < 0)
10235 from = base->authority().first;
10236
10237 dout(7) << "discover_path " << base->ino() << " " << want_path << " snap " << snap << " from mds." << from
10238 << (path_locked ? " path_locked":"")
10239 << dendl;
10240
10241 if (base->is_ambiguous_auth()) {
10242 dout(10) << " waiting for single auth on " << *base << dendl;
10243 if (!onfinish)
10244 onfinish = new C_MDC_RetryDiscoverPath(this, base, snap, want_path, from);
10245 base->add_waiter(CInode::WAIT_SINGLEAUTH, onfinish);
10246 return;
10247 } else if (from == mds->get_nodeid()) {
10248 MDSContext::vec finished;
10249 base->take_waiting(CInode::WAIT_DIR, finished);
10250 mds->queue_waiters(finished);
10251 return;
10252 }
10253
10254 frag_t fg = base->pick_dirfrag(want_path[0]);
10255 if ((path_locked && want_path.depth() == 1) ||
10256 !base->is_waiting_for_dir(fg) || !onfinish) {
10257 discover_info_t& d = _create_discover(from);
10258 d.ino = base->ino();
10259 d.pin_base(base);
10260 d.frag = fg;
10261 d.snap = snap;
10262 d.want_path = want_path;
10263 d.want_base_dir = true;
10264 d.path_locked = path_locked;
10265 _send_discover(d);
10266 }
10267
10268 // register + wait
10269 if (onfinish)
10270 base->add_dir_waiter(fg, onfinish);
10271 }
10272
10273 struct C_MDC_RetryDiscoverPath2 : public MDCacheContext {
10274 CDir *base;
10275 snapid_t snapid;
10276 filepath path;
10277 C_MDC_RetryDiscoverPath2(MDCache *c, CDir *b, snapid_t s, filepath &p) :
10278 MDCacheContext(c), base(b), snapid(s), path(p) {}
10279 void finish(int r) override {
10280 mdcache->discover_path(base, snapid, path, 0);
10281 }
10282 };
10283
10284 void MDCache::discover_path(CDir *base,
10285 snapid_t snap,
10286 filepath want_path,
10287 MDSContext *onfinish,
10288 bool path_locked)
10289 {
10290 mds_rank_t from = base->authority().first;
10291
10292 dout(7) << "discover_path " << base->dirfrag() << " " << want_path << " snap " << snap << " from mds." << from
10293 << (path_locked ? " path_locked":"")
10294 << dendl;
10295
10296 if (base->is_ambiguous_auth()) {
10297 dout(7) << " waiting for single auth on " << *base << dendl;
10298 if (!onfinish)
10299 onfinish = new C_MDC_RetryDiscoverPath2(this, base, snap, want_path);
10300 base->add_waiter(CDir::WAIT_SINGLEAUTH, onfinish);
10301 return;
10302 }
10303
10304 if ((path_locked && want_path.depth() == 1) ||
10305 !base->is_waiting_for_dentry(want_path[0].c_str(), snap) || !onfinish) {
10306 discover_info_t& d = _create_discover(from);
10307 d.ino = base->ino();
10308 d.pin_base(base->inode);
10309 d.frag = base->get_frag();
10310 d.snap = snap;
10311 d.want_path = want_path;
10312 d.want_base_dir = false;
10313 d.path_locked = path_locked;
10314 _send_discover(d);
10315 }
10316
10317 // register + wait
10318 if (onfinish)
10319 base->add_dentry_waiter(want_path[0], snap, onfinish);
10320 }
10321
10322 void MDCache::kick_discovers(mds_rank_t who)
10323 {
10324 for (map<ceph_tid_t,discover_info_t>::iterator p = discovers.begin();
10325 p != discovers.end();
10326 ++p) {
10327 if (p->second.mds != who)
10328 continue;
10329 _send_discover(p->second);
10330 }
10331 }
10332
10333
10334 void MDCache::handle_discover(const cref_t<MDiscover> &dis)
10335 {
10336 mds_rank_t whoami = mds->get_nodeid();
10337 mds_rank_t from = mds_rank_t(dis->get_source().num());
10338
10339 ceph_assert(from != whoami);
10340
10341 if (mds->get_state() <= MDSMap::STATE_REJOIN) {
10342 if (mds->get_state() < MDSMap::STATE_REJOIN &&
10343 mds->get_want_state() < CEPH_MDS_STATE_REJOIN) {
10344 return;
10345 }
10346
10347 // proceed if requester is in the REJOIN stage, the request is from parallel_fetch().
10348 // delay processing request from survivor because we may not yet choose lock states.
10349 if (!mds->mdsmap->is_rejoin(from)) {
10350 dout(0) << "discover_reply not yet active(|still rejoining), delaying" << dendl;
10351 mds->wait_for_replay(new C_MDS_RetryMessage(mds, dis));
10352 return;
10353 }
10354 }
10355
10356
10357 CInode *cur = 0;
10358 auto reply = make_message<MDiscoverReply>(*dis);
10359
10360 snapid_t snapid = dis->get_snapid();
10361
10362 logger->inc(l_mdc_dir_handle_discover);
10363
10364 // get started.
10365 if (MDS_INO_IS_BASE(dis->get_base_ino()) &&
10366 !dis->wants_base_dir() && dis->get_want().depth() == 0) {
10367 // wants root
10368 dout(7) << "handle_discover from mds." << from
10369 << " wants base + " << dis->get_want().get_path()
10370 << " snap " << snapid
10371 << dendl;
10372
10373 cur = get_inode(dis->get_base_ino());
10374 ceph_assert(cur);
10375
10376 // add root
10377 reply->starts_with = MDiscoverReply::INODE;
10378 encode_replica_inode(cur, from, reply->trace, mds->mdsmap->get_up_features());
10379 dout(10) << "added base " << *cur << dendl;
10380 }
10381 else {
10382 // there's a base inode
10383 cur = get_inode(dis->get_base_ino(), snapid);
10384 if (!cur && snapid != CEPH_NOSNAP) {
10385 cur = get_inode(dis->get_base_ino());
10386 if (cur && !cur->is_multiversion())
10387 cur = NULL; // nope!
10388 }
10389
10390 if (!cur) {
10391 dout(7) << "handle_discover mds." << from
10392 << " don't have base ino " << dis->get_base_ino() << "." << snapid
10393 << dendl;
10394 if (!dis->wants_base_dir() && dis->get_want().depth() > 0)
10395 reply->set_error_dentry(dis->get_dentry(0));
10396 reply->set_flag_error_dir();
10397 } else if (dis->wants_base_dir()) {
10398 dout(7) << "handle_discover mds." << from
10399 << " wants basedir+" << dis->get_want().get_path()
10400 << " has " << *cur
10401 << dendl;
10402 } else {
10403 dout(7) << "handle_discover mds." << from
10404 << " wants " << dis->get_want().get_path()
10405 << " has " << *cur
10406 << dendl;
10407 }
10408 }
10409
10410 ceph_assert(reply);
10411
10412 // add content
10413 // do some fidgeting to include a dir if they asked for the base dir, or just root.
10414 for (unsigned i = 0;
10415 cur && (i < dis->get_want().depth() || dis->get_want().depth() == 0);
10416 i++) {
10417
10418 // -- figure out the dir
10419
10420 // is *cur even a dir at all?
10421 if (!cur->is_dir()) {
10422 dout(7) << *cur << " not a dir" << dendl;
10423 reply->set_flag_error_dir();
10424 break;
10425 }
10426
10427 // pick frag
10428 frag_t fg;
10429 if (dis->get_want().depth()) {
10430 // dentry specifies
10431 fg = cur->pick_dirfrag(dis->get_dentry(i));
10432 } else {
10433 // requester explicity specified the frag
10434 ceph_assert(dis->wants_base_dir() || MDS_INO_IS_BASE(dis->get_base_ino()));
10435 fg = dis->get_base_dir_frag();
10436 if (!cur->dirfragtree.is_leaf(fg))
10437 fg = cur->dirfragtree[fg.value()];
10438 }
10439 CDir *curdir = cur->get_dirfrag(fg);
10440
10441 if ((!curdir && !cur->is_auth()) ||
10442 (curdir && !curdir->is_auth())) {
10443
10444 /* before:
10445 * ONLY set flag if empty!!
10446 * otherwise requester will wake up waiter(s) _and_ continue with discover,
10447 * resulting in duplicate discovers in flight,
10448 * which can wreak havoc when discovering rename srcdn (which may move)
10449 */
10450
10451 if (reply->is_empty()) {
10452 // only hint if empty.
10453 // someday this could be better, but right now the waiter logic isn't smart enough.
10454
10455 // hint
10456 if (curdir) {
10457 dout(7) << " not dirfrag auth, setting dir_auth_hint for " << *curdir << dendl;
10458 reply->set_dir_auth_hint(curdir->authority().first);
10459 } else {
10460 dout(7) << " dirfrag not open, not inode auth, setting dir_auth_hint for "
10461 << *cur << dendl;
10462 reply->set_dir_auth_hint(cur->authority().first);
10463 }
10464
10465 // note error dentry, if any
10466 // NOTE: important, as it allows requester to issue an equivalent discover
10467 // to whomever we hint at.
10468 if (dis->get_want().depth() > i)
10469 reply->set_error_dentry(dis->get_dentry(i));
10470 }
10471
10472 break;
10473 }
10474
10475 if (!curdir) { // open dir?
10476 if (cur->is_frozen()) {
10477 if (!reply->is_empty()) {
10478 dout(7) << *cur << " is frozen, non-empty reply, stopping" << dendl;
10479 break;
10480 }
10481 dout(7) << *cur << " is frozen, empty reply, waiting" << dendl;
10482 cur->add_waiter(CInode::WAIT_UNFREEZE, new C_MDS_RetryMessage(mds, dis));
10483 return;
10484 }
10485 curdir = cur->get_or_open_dirfrag(this, fg);
10486 } else if (curdir->is_frozen_tree() ||
10487 (curdir->is_frozen_dir() && fragment_are_all_frozen(curdir))) {
10488 if (!reply->is_empty()) {
10489 dout(7) << *curdir << " is frozen, non-empty reply, stopping" << dendl;
10490 break;
10491 }
10492 if (dis->wants_base_dir() && dis->get_base_dir_frag() != curdir->get_frag()) {
10493 dout(7) << *curdir << " is frozen, dirfrag mismatch, stopping" << dendl;
10494 reply->set_flag_error_dir();
10495 break;
10496 }
10497 dout(7) << *curdir << " is frozen, empty reply, waiting" << dendl;
10498 curdir->add_waiter(CDir::WAIT_UNFREEZE, new C_MDS_RetryMessage(mds, dis));
10499 return;
10500 }
10501
10502 // add dir
10503 if (curdir->get_version() == 0) {
10504 // fetch newly opened dir
10505 } else if (reply->is_empty() && !dis->wants_base_dir()) {
10506 dout(7) << "handle_discover not adding unwanted base dir " << *curdir << dendl;
10507 // make sure the base frag is correct, though, in there was a refragment since the
10508 // original request was sent.
10509 reply->set_base_dir_frag(curdir->get_frag());
10510 } else {
10511 ceph_assert(!curdir->is_ambiguous_auth()); // would be frozen.
10512 if (!reply->trace.length())
10513 reply->starts_with = MDiscoverReply::DIR;
10514 encode_replica_dir(curdir, from, reply->trace);
10515 dout(7) << "handle_discover added dir " << *curdir << dendl;
10516 }
10517
10518 // lookup
10519 CDentry *dn = 0;
10520 std::string_view dname;
10521 if (dis->get_want().depth() > 0)
10522 dname = dis->get_dentry(i);
10523 if (curdir->get_version() == 0) {
10524 // fetch newly opened dir
10525 ceph_assert(!curdir->has_bloom());
10526 } else if (dname.size() > 0) {
10527 // lookup dentry
10528 dn = curdir->lookup(dname, snapid);
10529 } else
10530 break; // done!
10531
10532 // incomplete dir?
10533 if (!dn) {
10534 if (!curdir->is_complete() &&
10535 !(dname.size() > 0 &&
10536 snapid == CEPH_NOSNAP &&
10537 curdir->has_bloom() &&
10538 !curdir->is_in_bloom(dname))) {
10539 // readdir
10540 dout(7) << "incomplete dir contents for " << *curdir << ", fetching" << dendl;
10541 if (reply->is_empty()) {
10542 // fetch and wait
10543 curdir->fetch(dname, snapid, new C_MDS_RetryMessage(mds, dis),
10544 dis->wants_base_dir() && curdir->get_version() == 0);
10545 return;
10546 } else {
10547 // initiate fetch, but send what we have so far
10548 curdir->fetch(dname, snapid, nullptr);
10549 break;
10550 }
10551 }
10552
10553 if (snapid != CEPH_NOSNAP && !reply->is_empty()) {
10554 dout(7) << "dentry " << dis->get_dentry(i) << " snap " << snapid
10555 << " dne, non-empty reply, stopping" << dendl;
10556 break;
10557 }
10558
10559 // send null dentry
10560 dout(7) << "dentry " << dis->get_dentry(i) << " dne, returning null in "
10561 << *curdir << dendl;
10562 if (snapid == CEPH_NOSNAP)
10563 dn = curdir->add_null_dentry(dis->get_dentry(i));
10564 else
10565 dn = curdir->add_null_dentry(dis->get_dentry(i), snapid, snapid);
10566 }
10567 ceph_assert(dn);
10568
10569 // don't add replica to purging dentry/inode
10570 if (dn->state_test(CDentry::STATE_PURGING)) {
10571 if (reply->is_empty())
10572 reply->set_flag_error_dn(dis->get_dentry(i));
10573 break;
10574 }
10575
10576 CDentry::linkage_t *dnl = dn->get_linkage();
10577
10578 // xlocked dentry?
10579 // ...always block on non-tail items (they are unrelated)
10580 // ...allow xlocked tail disocvery _only_ if explicitly requested
10581 if (dn->lock.is_xlocked()) {
10582 // is this the last (tail) item in the discover traversal?
10583 if (dis->is_path_locked()) {
10584 dout(7) << "handle_discover allowing discovery of xlocked " << *dn << dendl;
10585 } else if (reply->is_empty()) {
10586 dout(7) << "handle_discover blocking on xlocked " << *dn << dendl;
10587 dn->lock.add_waiter(SimpleLock::WAIT_RD, new C_MDS_RetryMessage(mds, dis));
10588 return;
10589 } else {
10590 dout(7) << "handle_discover non-empty reply, xlocked tail " << *dn << dendl;
10591 break;
10592 }
10593 }
10594
10595 // frozen inode?
10596 bool tailitem = (dis->get_want().depth() == 0) || (i == dis->get_want().depth() - 1);
10597 if (dnl->is_primary() && dnl->get_inode()->is_frozen_inode()) {
10598 if (tailitem && dis->is_path_locked()) {
10599 dout(7) << "handle_discover allowing discovery of frozen tail " << *dnl->get_inode() << dendl;
10600 } else if (reply->is_empty()) {
10601 dout(7) << *dnl->get_inode() << " is frozen, empty reply, waiting" << dendl;
10602 dnl->get_inode()->add_waiter(CDir::WAIT_UNFREEZE, new C_MDS_RetryMessage(mds, dis));
10603 return;
10604 } else {
10605 dout(7) << *dnl->get_inode() << " is frozen, non-empty reply, stopping" << dendl;
10606 break;
10607 }
10608 }
10609
10610 // add dentry
10611 if (!reply->trace.length())
10612 reply->starts_with = MDiscoverReply::DENTRY;
10613 encode_replica_dentry(dn, from, reply->trace);
10614 dout(7) << "handle_discover added dentry " << *dn << dendl;
10615
10616 if (!dnl->is_primary()) break; // stop on null or remote link.
10617
10618 // add inode
10619 CInode *next = dnl->get_inode();
10620 ceph_assert(next->is_auth());
10621
10622 encode_replica_inode(next, from, reply->trace, mds->mdsmap->get_up_features());
10623 dout(7) << "handle_discover added inode " << *next << dendl;
10624
10625 // descend, keep going.
10626 cur = next;
10627 continue;
10628 }
10629
10630 // how did we do?
10631 ceph_assert(!reply->is_empty());
10632 dout(7) << "handle_discover sending result back to asker mds." << from << dendl;
10633 mds->send_message(reply, dis->get_connection());
10634 }
10635
10636 void MDCache::handle_discover_reply(const cref_t<MDiscoverReply> &m)
10637 {
10638 /*
10639 if (mds->get_state() < MDSMap::STATE_ACTIVE) {
10640 dout(0) << "discover_reply NOT ACTIVE YET" << dendl;
10641 return;
10642 }
10643 */
10644 dout(7) << "discover_reply " << *m << dendl;
10645 if (m->is_flag_error_dir())
10646 dout(7) << " flag error, dir" << dendl;
10647 if (m->is_flag_error_dn())
10648 dout(7) << " flag error, dentry = " << m->get_error_dentry() << dendl;
10649
10650 MDSContext::vec finished, error;
10651 mds_rank_t from = mds_rank_t(m->get_source().num());
10652
10653 // starting point
10654 CInode *cur = get_inode(m->get_base_ino());
10655 auto p = m->trace.cbegin();
10656
10657 int next = m->starts_with;
10658
10659 // decrement discover counters
10660 if (m->get_tid()) {
10661 map<ceph_tid_t,discover_info_t>::iterator p = discovers.find(m->get_tid());
10662 if (p != discovers.end()) {
10663 dout(10) << " found tid " << m->get_tid() << dendl;
10664 discovers.erase(p);
10665 } else {
10666 dout(10) << " tid " << m->get_tid() << " not found, must be dup reply" << dendl;
10667 }
10668 }
10669
10670 // discover may start with an inode
10671 if (!p.end() && next == MDiscoverReply::INODE) {
10672 decode_replica_inode(cur, p, NULL, finished);
10673 dout(7) << "discover_reply got base inode " << *cur << dendl;
10674 ceph_assert(cur->is_base());
10675
10676 next = MDiscoverReply::DIR;
10677
10678 // take waiters?
10679 if (cur->is_base() &&
10680 waiting_for_base_ino[from].count(cur->ino())) {
10681 finished.swap(waiting_for_base_ino[from][cur->ino()]);
10682 waiting_for_base_ino[from].erase(cur->ino());
10683 }
10684 }
10685 ceph_assert(cur);
10686
10687 // loop over discover results.
10688 // indexes follow each ([[dir] dentry] inode)
10689 // can start, end with any type.
10690 while (!p.end()) {
10691 // dir
10692 frag_t fg;
10693 CDir *curdir = nullptr;
10694 if (next == MDiscoverReply::DIR) {
10695 decode_replica_dir(curdir, p, cur, mds_rank_t(m->get_source().num()), finished);
10696 if (cur->ino() == m->get_base_ino() && curdir->get_frag() != m->get_base_dir_frag()) {
10697 ceph_assert(m->get_wanted_base_dir());
10698 cur->take_dir_waiting(m->get_base_dir_frag(), finished);
10699 }
10700 } else {
10701 // note: this can only happen our first way around this loop.
10702 if (p.end() && m->is_flag_error_dn()) {
10703 fg = cur->pick_dirfrag(m->get_error_dentry());
10704 curdir = cur->get_dirfrag(fg);
10705 } else
10706 curdir = cur->get_dirfrag(m->get_base_dir_frag());
10707 }
10708
10709 if (p.end())
10710 break;
10711
10712 // dentry
10713 CDentry *dn = nullptr;
10714 decode_replica_dentry(dn, p, curdir, finished);
10715
10716 if (p.end())
10717 break;
10718
10719 // inode
10720 decode_replica_inode(cur, p, dn, finished);
10721
10722 next = MDiscoverReply::DIR;
10723 }
10724
10725 // dir error?
10726 // or dir_auth hint?
10727 if (m->is_flag_error_dir() && !cur->is_dir()) {
10728 // not a dir.
10729 cur->take_waiting(CInode::WAIT_DIR, error);
10730 } else if (m->is_flag_error_dir() || m->get_dir_auth_hint() != CDIR_AUTH_UNKNOWN) {
10731 mds_rank_t who = m->get_dir_auth_hint();
10732 if (who == mds->get_nodeid()) who = -1;
10733 if (who >= 0)
10734 dout(7) << " dir_auth_hint is " << m->get_dir_auth_hint() << dendl;
10735
10736
10737 if (m->get_wanted_base_dir()) {
10738 frag_t fg = m->get_base_dir_frag();
10739 CDir *dir = cur->get_dirfrag(fg);
10740
10741 if (cur->is_waiting_for_dir(fg)) {
10742 if (cur->is_auth())
10743 cur->take_waiting(CInode::WAIT_DIR, finished);
10744 else if (dir || !cur->dirfragtree.is_leaf(fg))
10745 cur->take_dir_waiting(fg, finished);
10746 else
10747 discover_dir_frag(cur, fg, 0, who);
10748 } else
10749 dout(7) << " doing nothing, nobody is waiting for dir" << dendl;
10750 }
10751
10752 // try again?
10753 if (m->get_error_dentry().length()) {
10754 frag_t fg = cur->pick_dirfrag(m->get_error_dentry());
10755 CDir *dir = cur->get_dirfrag(fg);
10756 // wanted a dentry
10757 if (dir && dir->is_waiting_for_dentry(m->get_error_dentry(), m->get_wanted_snapid())) {
10758 if (dir->is_auth() || dir->lookup(m->get_error_dentry())) {
10759 dir->take_dentry_waiting(m->get_error_dentry(), m->get_wanted_snapid(),
10760 m->get_wanted_snapid(), finished);
10761 } else {
10762 filepath relpath(m->get_error_dentry(), 0);
10763 discover_path(dir, m->get_wanted_snapid(), relpath, 0, m->is_path_locked());
10764 }
10765 } else
10766 dout(7) << " doing nothing, have dir but nobody is waiting on dentry "
10767 << m->get_error_dentry() << dendl;
10768 }
10769 } else if (m->is_flag_error_dn()) {
10770 frag_t fg = cur->pick_dirfrag(m->get_error_dentry());
10771 CDir *dir = cur->get_dirfrag(fg);
10772 if (dir && !dir->is_auth()) {
10773 dir->take_dentry_waiting(m->get_error_dentry(), m->get_wanted_snapid(),
10774 m->get_wanted_snapid(), error);
10775 }
10776 }
10777
10778 // waiters
10779 finish_contexts(g_ceph_context, error, -CEPHFS_ENOENT); // finish errors directly
10780 mds->queue_waiters(finished);
10781 }
10782
10783
10784
10785 // ----------------------------
10786 // REPLICAS
10787
10788
10789 void MDCache::encode_replica_dir(CDir *dir, mds_rank_t to, bufferlist& bl)
10790 {
10791 ENCODE_START(1, 1, bl);
10792 dirfrag_t df = dir->dirfrag();
10793 encode(df, bl);
10794 __u32 nonce = dir->add_replica(to);
10795 encode(nonce, bl);
10796 dir->_encode_base(bl);
10797 ENCODE_FINISH(bl);
10798 }
10799
10800 void MDCache::encode_replica_dentry(CDentry *dn, mds_rank_t to, bufferlist& bl)
10801 {
10802 ENCODE_START(2, 1, bl);
10803 encode(dn->get_name(), bl);
10804 encode(dn->last, bl);
10805
10806 __u32 nonce = dn->add_replica(to);
10807 encode(nonce, bl);
10808 encode(dn->first, bl);
10809 encode(dn->linkage.remote_ino, bl);
10810 encode(dn->linkage.remote_d_type, bl);
10811 dn->lock.encode_state_for_replica(bl);
10812 bool need_recover = mds->get_state() < MDSMap::STATE_ACTIVE;
10813 encode(need_recover, bl);
10814 encode(dn->alternate_name, bl);
10815 ENCODE_FINISH(bl);
10816 }
10817
10818 void MDCache::encode_replica_inode(CInode *in, mds_rank_t to, bufferlist& bl,
10819 uint64_t features)
10820 {
10821 ceph_assert(in->is_auth());
10822
10823 ENCODE_START(2, 1, bl);
10824 encode(in->ino(), bl); // bleh, minor assymetry here
10825 encode(in->last, bl);
10826
10827 __u32 nonce = in->add_replica(to);
10828 encode(nonce, bl);
10829
10830 in->_encode_base(bl, features);
10831 in->_encode_locks_state_for_replica(bl, mds->get_state() < MDSMap::STATE_ACTIVE);
10832
10833 __u32 state = in->state;
10834 encode(state, bl);
10835
10836 ENCODE_FINISH(bl);
10837 }
10838
10839 void MDCache::decode_replica_dir(CDir *&dir, bufferlist::const_iterator& p, CInode *diri, mds_rank_t from,
10840 MDSContext::vec& finished)
10841 {
10842 DECODE_START(1, p);
10843 dirfrag_t df;
10844 decode(df, p);
10845
10846 ceph_assert(diri->ino() == df.ino);
10847
10848 // add it (_replica_)
10849 dir = diri->get_dirfrag(df.frag);
10850
10851 if (dir) {
10852 // had replica. update w/ new nonce.
10853 __u32 nonce;
10854 decode(nonce, p);
10855 dir->set_replica_nonce(nonce);
10856 dir->_decode_base(p);
10857 dout(7) << __func__ << " had " << *dir << " nonce " << dir->replica_nonce << dendl;
10858 } else {
10859 // force frag to leaf in the diri tree
10860 if (!diri->dirfragtree.is_leaf(df.frag)) {
10861 dout(7) << __func__ << " forcing frag " << df.frag << " to leaf in the fragtree "
10862 << diri->dirfragtree << dendl;
10863 diri->dirfragtree.force_to_leaf(g_ceph_context, df.frag);
10864 }
10865 // add replica.
10866 dir = diri->add_dirfrag( new CDir(diri, df.frag, this, false) );
10867 __u32 nonce;
10868 decode(nonce, p);
10869 dir->set_replica_nonce(nonce);
10870 dir->_decode_base(p);
10871 // is this a dir_auth delegation boundary?
10872 if (from != diri->authority().first ||
10873 diri->is_ambiguous_auth() ||
10874 diri->is_base())
10875 adjust_subtree_auth(dir, from);
10876
10877 dout(7) << __func__ << " added " << *dir << " nonce " << dir->replica_nonce << dendl;
10878 // get waiters
10879 diri->take_dir_waiting(df.frag, finished);
10880 }
10881 DECODE_FINISH(p);
10882 }
10883
10884 void MDCache::decode_replica_dentry(CDentry *&dn, bufferlist::const_iterator& p, CDir *dir, MDSContext::vec& finished)
10885 {
10886 DECODE_START(1, p);
10887 string name;
10888 snapid_t last;
10889 decode(name, p);
10890 decode(last, p);
10891
10892 dn = dir->lookup(name, last);
10893
10894 // have it?
10895 bool is_new = false;
10896 if (dn) {
10897 is_new = false;
10898 dout(7) << __func__ << " had " << *dn << dendl;
10899 } else {
10900 is_new = true;
10901 dn = dir->add_null_dentry(name, 1 /* this will get updated below */, last);
10902 dout(7) << __func__ << " added " << *dn << dendl;
10903 }
10904
10905 __u32 nonce;
10906 decode(nonce, p);
10907 dn->set_replica_nonce(nonce);
10908 decode(dn->first, p);
10909
10910 inodeno_t rino;
10911 unsigned char rdtype;
10912 decode(rino, p);
10913 decode(rdtype, p);
10914 dn->lock.decode_state(p, is_new);
10915
10916 bool need_recover;
10917 decode(need_recover, p);
10918
10919 mempool::mds_co::string alternate_name;
10920 if (struct_v >= 2) {
10921 decode(alternate_name, p);
10922 }
10923
10924 if (is_new) {
10925 dn->set_alternate_name(std::move(alternate_name));
10926 if (rino)
10927 dir->link_remote_inode(dn, rino, rdtype);
10928 if (need_recover)
10929 dn->lock.mark_need_recover();
10930 } else {
10931 ceph_assert(dn->alternate_name == alternate_name);
10932 }
10933
10934 dir->take_dentry_waiting(name, dn->first, dn->last, finished);
10935 DECODE_FINISH(p);
10936 }
10937
10938 void MDCache::decode_replica_inode(CInode *&in, bufferlist::const_iterator& p, CDentry *dn, MDSContext::vec& finished)
10939 {
10940 DECODE_START(2, p);
10941 inodeno_t ino;
10942 snapid_t last;
10943 __u32 nonce;
10944 decode(ino, p);
10945 decode(last, p);
10946 decode(nonce, p);
10947 in = get_inode(ino, last);
10948 if (!in) {
10949 in = new CInode(this, false, 2, last);
10950 in->set_replica_nonce(nonce);
10951 in->_decode_base(p);
10952 in->_decode_locks_state_for_replica(p, true);
10953 add_inode(in);
10954 if (in->ino() == CEPH_INO_ROOT)
10955 in->inode_auth.first = 0;
10956 else if (in->is_mdsdir())
10957 in->inode_auth.first = in->ino() - MDS_INO_MDSDIR_OFFSET;
10958 dout(10) << __func__ << " added " << *in << dendl;
10959 if (dn) {
10960 ceph_assert(dn->get_linkage()->is_null());
10961 dn->dir->link_primary_inode(dn, in);
10962 }
10963 } else {
10964 in->set_replica_nonce(nonce);
10965 in->_decode_base(p);
10966 in->_decode_locks_state_for_replica(p, false);
10967 dout(10) << __func__ << " had " << *in << dendl;
10968 }
10969
10970 if (dn) {
10971 if (!dn->get_linkage()->is_primary() || dn->get_linkage()->get_inode() != in)
10972 dout(10) << __func__ << " different linkage in dentry " << *dn << dendl;
10973 }
10974
10975 if (struct_v >= 2) {
10976 __u32 s;
10977 decode(s, p);
10978 s &= CInode::MASK_STATE_REPLICATED;
10979 if (s & CInode::STATE_RANDEPHEMERALPIN) {
10980 dout(10) << "replica inode is random ephemeral pinned" << dendl;
10981 in->set_ephemeral_pin(false, true);
10982 }
10983 }
10984
10985 DECODE_FINISH(p);
10986 }
10987
10988
10989 void MDCache::encode_replica_stray(CDentry *straydn, mds_rank_t who, bufferlist& bl)
10990 {
10991 ceph_assert(straydn->get_num_auth_pins());
10992 ENCODE_START(2, 1, bl);
10993 uint64_t features = mds->mdsmap->get_up_features();
10994 encode_replica_inode(get_myin(), who, bl, features);
10995 encode_replica_dir(straydn->get_dir()->inode->get_parent_dn()->get_dir(), who, bl);
10996 encode_replica_dentry(straydn->get_dir()->inode->get_parent_dn(), who, bl);
10997 encode_replica_inode(straydn->get_dir()->inode, who, bl, features);
10998 encode_replica_dir(straydn->get_dir(), who, bl);
10999 encode_replica_dentry(straydn, who, bl);
11000 if (!straydn->get_projected_linkage()->is_null()) {
11001 encode_replica_inode(straydn->get_projected_linkage()->get_inode(), who, bl, features);
11002 }
11003 ENCODE_FINISH(bl);
11004 }
11005
11006 void MDCache::decode_replica_stray(CDentry *&straydn, CInode **in, const bufferlist &bl, mds_rank_t from)
11007 {
11008 MDSContext::vec finished;
11009 auto p = bl.cbegin();
11010
11011 DECODE_START(2, p);
11012 CInode *mdsin = nullptr;
11013 decode_replica_inode(mdsin, p, NULL, finished);
11014 CDir *mdsdir = nullptr;
11015 decode_replica_dir(mdsdir, p, mdsin, from, finished);
11016 CDentry *straydirdn = nullptr;
11017 decode_replica_dentry(straydirdn, p, mdsdir, finished);
11018 CInode *strayin = nullptr;
11019 decode_replica_inode(strayin, p, straydirdn, finished);
11020 CDir *straydir = nullptr;
11021 decode_replica_dir(straydir, p, strayin, from, finished);
11022
11023 decode_replica_dentry(straydn, p, straydir, finished);
11024 if (struct_v >= 2 && in) {
11025 decode_replica_inode(*in, p, straydn, finished);
11026 }
11027 if (!finished.empty())
11028 mds->queue_waiters(finished);
11029 DECODE_FINISH(p);
11030 }
11031
11032
11033 int MDCache::send_dir_updates(CDir *dir, bool bcast)
11034 {
11035 // this is an FYI, re: replication
11036
11037 set<mds_rank_t> who;
11038 if (bcast) {
11039 set<mds_rank_t> mds_set;
11040 mds->get_mds_map()->get_active_mds_set(mds_set);
11041
11042 set<mds_rank_t> replica_set;
11043 for (const auto &p : dir->get_replicas()) {
11044 replica_set.insert(p.first);
11045 }
11046
11047 std::set_difference(mds_set.begin(), mds_set.end(),
11048 replica_set.begin(), replica_set.end(),
11049 std::inserter(who, who.end()));
11050 } else {
11051 for (const auto &p : dir->get_replicas()) {
11052 who.insert(p.first);
11053 }
11054 }
11055
11056 dout(7) << "sending dir_update on " << *dir << " bcast " << bcast << " to " << who << dendl;
11057
11058 filepath path;
11059 dir->inode->make_path(path);
11060
11061 std::set<int32_t> dir_rep_set;
11062 for (const auto &r : dir->dir_rep_by) {
11063 dir_rep_set.insert(r);
11064 }
11065
11066 mds_rank_t whoami = mds->get_nodeid();
11067 for (set<mds_rank_t>::iterator it = who.begin();
11068 it != who.end();
11069 ++it) {
11070 if (*it == whoami) continue;
11071 //if (*it == except) continue;
11072 dout(7) << "sending dir_update on " << *dir << " to " << *it << dendl;
11073
11074 logger->inc(l_mdc_dir_update);
11075 mds->send_message_mds(make_message<MDirUpdate>(mds->get_nodeid(), dir->dirfrag(), dir->dir_rep, dir_rep_set, path, bcast), *it);
11076 }
11077
11078 return 0;
11079 }
11080
11081 void MDCache::handle_dir_update(const cref_t<MDirUpdate> &m)
11082 {
11083 dirfrag_t df = m->get_dirfrag();
11084 CDir *dir = get_dirfrag(df);
11085 logger->inc(l_mdc_dir_update_receipt);
11086 if (!dir) {
11087 dout(5) << "dir_update on " << df << ", don't have it" << dendl;
11088
11089 // discover it?
11090 if (m->should_discover()) {
11091 // only try once!
11092 // this is key to avoid a fragtree update race, among other things.
11093 m->inc_tried_discover();
11094 vector<CDentry*> trace;
11095 CInode *in;
11096 filepath path = m->get_path();
11097 dout(5) << "trying discover on dir_update for " << path << dendl;
11098 logger->inc(l_mdc_dir_try_discover);
11099 CF_MDS_RetryMessageFactory cf(mds, m);
11100 MDRequestRef null_ref;
11101 int r = path_traverse(null_ref, cf, path, MDS_TRAVERSE_DISCOVER, &trace, &in);
11102 if (r > 0)
11103 return;
11104 if (r == 0 &&
11105 in->ino() == df.ino &&
11106 in->get_approx_dirfrag(df.frag) == NULL) {
11107 open_remote_dirfrag(in, df.frag, new C_MDS_RetryMessage(mds, m));
11108 return;
11109 }
11110 }
11111
11112 return;
11113 }
11114
11115 if (!m->has_tried_discover()) {
11116 // Update if it already exists. Othwerwise it got updated by discover reply.
11117 dout(5) << "dir_update on " << *dir << dendl;
11118 dir->dir_rep = m->get_dir_rep();
11119 dir->dir_rep_by.clear();
11120 for (const auto &e : m->get_dir_rep_by()) {
11121 dir->dir_rep_by.insert(e);
11122 }
11123 }
11124 }
11125
11126
11127
11128
11129
11130 // LINK
11131
11132 void MDCache::encode_remote_dentry_link(CDentry::linkage_t *dnl, bufferlist& bl)
11133 {
11134 ENCODE_START(1, 1, bl);
11135 inodeno_t ino = dnl->get_remote_ino();
11136 encode(ino, bl);
11137 __u8 d_type = dnl->get_remote_d_type();
11138 encode(d_type, bl);
11139 ENCODE_FINISH(bl);
11140 }
11141
11142 void MDCache::decode_remote_dentry_link(CDir *dir, CDentry *dn, bufferlist::const_iterator& p)
11143 {
11144 DECODE_START(1, p);
11145 inodeno_t ino;
11146 __u8 d_type;
11147 decode(ino, p);
11148 decode(d_type, p);
11149 dout(10) << __func__ << " remote " << ino << " " << d_type << dendl;
11150 dir->link_remote_inode(dn, ino, d_type);
11151 DECODE_FINISH(p);
11152 }
11153
11154 void MDCache::send_dentry_link(CDentry *dn, MDRequestRef& mdr)
11155 {
11156 dout(7) << __func__ << " " << *dn << dendl;
11157
11158 CDir *subtree = get_subtree_root(dn->get_dir());
11159 for (const auto &p : dn->get_replicas()) {
11160 // don't tell (rename) witnesses; they already know
11161 if (mdr.get() && mdr->more()->witnessed.count(p.first))
11162 continue;
11163 if (mds->mdsmap->get_state(p.first) < MDSMap::STATE_REJOIN ||
11164 (mds->mdsmap->get_state(p.first) == MDSMap::STATE_REJOIN &&
11165 rejoin_gather.count(p.first)))
11166 continue;
11167 CDentry::linkage_t *dnl = dn->get_linkage();
11168 auto m = make_message<MDentryLink>(subtree->dirfrag(), dn->get_dir()->dirfrag(), dn->get_name(), dnl->is_primary());
11169 if (dnl->is_primary()) {
11170 dout(10) << __func__ << " primary " << *dnl->get_inode() << dendl;
11171 encode_replica_inode(dnl->get_inode(), p.first, m->bl,
11172 mds->mdsmap->get_up_features());
11173 } else if (dnl->is_remote()) {
11174 encode_remote_dentry_link(dnl, m->bl);
11175 } else
11176 ceph_abort(); // aie, bad caller!
11177 mds->send_message_mds(m, p.first);
11178 }
11179 }
11180
11181 void MDCache::handle_dentry_link(const cref_t<MDentryLink> &m)
11182 {
11183 CDentry *dn = NULL;
11184 CDir *dir = get_dirfrag(m->get_dirfrag());
11185 if (!dir) {
11186 dout(7) << __func__ << " don't have dirfrag " << m->get_dirfrag() << dendl;
11187 } else {
11188 dn = dir->lookup(m->get_dn());
11189 if (!dn) {
11190 dout(7) << __func__ << " don't have dentry " << *dir << " dn " << m->get_dn() << dendl;
11191 } else {
11192 dout(7) << __func__ << " on " << *dn << dendl;
11193 CDentry::linkage_t *dnl = dn->get_linkage();
11194
11195 ceph_assert(!dn->is_auth());
11196 ceph_assert(dnl->is_null());
11197 }
11198 }
11199
11200 auto p = m->bl.cbegin();
11201 MDSContext::vec finished;
11202 if (dn) {
11203 if (m->get_is_primary()) {
11204 // primary link.
11205 CInode *in = nullptr;
11206 decode_replica_inode(in, p, dn, finished);
11207 } else {
11208 // remote link, easy enough.
11209 decode_remote_dentry_link(dir, dn, p);
11210 }
11211 } else {
11212 ceph_abort();
11213 }
11214
11215 if (!finished.empty())
11216 mds->queue_waiters(finished);
11217
11218 return;
11219 }
11220
11221
11222 // UNLINK
11223
11224 void MDCache::send_dentry_unlink(CDentry *dn, CDentry *straydn, MDRequestRef& mdr)
11225 {
11226 dout(10) << __func__ << " " << *dn << dendl;
11227 // share unlink news with replicas
11228 set<mds_rank_t> replicas;
11229 dn->list_replicas(replicas);
11230 bufferlist snapbl;
11231 if (straydn) {
11232 straydn->list_replicas(replicas);
11233 CInode *strayin = straydn->get_linkage()->get_inode();
11234 strayin->encode_snap_blob(snapbl);
11235 }
11236 for (set<mds_rank_t>::iterator it = replicas.begin();
11237 it != replicas.end();
11238 ++it) {
11239 // don't tell (rmdir) witnesses; they already know
11240 if (mdr.get() && mdr->more()->witnessed.count(*it))
11241 continue;
11242
11243 if (mds->mdsmap->get_state(*it) < MDSMap::STATE_REJOIN ||
11244 (mds->mdsmap->get_state(*it) == MDSMap::STATE_REJOIN &&
11245 rejoin_gather.count(*it)))
11246 continue;
11247
11248 auto unlink = make_message<MDentryUnlink>(dn->get_dir()->dirfrag(), dn->get_name());
11249 if (straydn) {
11250 encode_replica_stray(straydn, *it, unlink->straybl);
11251 unlink->snapbl = snapbl;
11252 }
11253 mds->send_message_mds(unlink, *it);
11254 }
11255 }
11256
11257 void MDCache::handle_dentry_unlink(const cref_t<MDentryUnlink> &m)
11258 {
11259 // straydn
11260 CDentry *straydn = nullptr;
11261 CInode *strayin = nullptr;
11262 if (m->straybl.length())
11263 decode_replica_stray(straydn, &strayin, m->straybl, mds_rank_t(m->get_source().num()));
11264
11265 CDir *dir = get_dirfrag(m->get_dirfrag());
11266 if (!dir) {
11267 dout(7) << __func__ << " don't have dirfrag " << m->get_dirfrag() << dendl;
11268 } else {
11269 CDentry *dn = dir->lookup(m->get_dn());
11270 if (!dn) {
11271 dout(7) << __func__ << " don't have dentry " << *dir << " dn " << m->get_dn() << dendl;
11272 } else {
11273 dout(7) << __func__ << " on " << *dn << dendl;
11274 CDentry::linkage_t *dnl = dn->get_linkage();
11275
11276 // open inode?
11277 if (dnl->is_primary()) {
11278 CInode *in = dnl->get_inode();
11279 dn->dir->unlink_inode(dn);
11280 ceph_assert(straydn);
11281 straydn->dir->link_primary_inode(straydn, in);
11282
11283 // in->first is lazily updated on replica; drag it forward so
11284 // that we always keep it in sync with the dnq
11285 ceph_assert(straydn->first >= in->first);
11286 in->first = straydn->first;
11287
11288 // update subtree map?
11289 if (in->is_dir())
11290 adjust_subtree_after_rename(in, dir, false);
11291
11292 if (m->snapbl.length()) {
11293 bool hadrealm = (in->snaprealm ? true : false);
11294 in->decode_snap_blob(m->snapbl);
11295 ceph_assert(in->snaprealm);
11296 if (!hadrealm)
11297 do_realm_invalidate_and_update_notify(in, CEPH_SNAP_OP_SPLIT, false);
11298 }
11299
11300 // send caps to auth (if we're not already)
11301 if (in->is_any_caps() &&
11302 !in->state_test(CInode::STATE_EXPORTINGCAPS))
11303 migrator->export_caps(in);
11304
11305 straydn = NULL;
11306 } else {
11307 ceph_assert(!straydn);
11308 ceph_assert(dnl->is_remote());
11309 dn->dir->unlink_inode(dn);
11310 }
11311 ceph_assert(dnl->is_null());
11312 }
11313 }
11314
11315 // race with trim_dentry()
11316 if (straydn) {
11317 ceph_assert(straydn->get_num_ref() == 0);
11318 ceph_assert(straydn->get_linkage()->is_null());
11319 expiremap ex;
11320 trim_dentry(straydn, ex);
11321 send_expire_messages(ex);
11322 }
11323 }
11324
11325
11326
11327
11328
11329
11330 // ===================================================================
11331
11332
11333
11334 // ===================================================================
11335 // FRAGMENT
11336
11337
11338 /**
11339 * adjust_dir_fragments -- adjust fragmentation for a directory
11340 *
11341 * @param diri directory inode
11342 * @param basefrag base fragment
11343 * @param bits bit adjustment. positive for split, negative for merge.
11344 */
11345 void MDCache::adjust_dir_fragments(CInode *diri, frag_t basefrag, int bits,
11346 std::vector<CDir*>* resultfrags,
11347 MDSContext::vec& waiters,
11348 bool replay)
11349 {
11350 dout(10) << "adjust_dir_fragments " << basefrag << " " << bits
11351 << " on " << *diri << dendl;
11352
11353 auto&& p = diri->get_dirfrags_under(basefrag);
11354
11355 adjust_dir_fragments(diri, p.second, basefrag, bits, resultfrags, waiters, replay);
11356 }
11357
11358 CDir *MDCache::force_dir_fragment(CInode *diri, frag_t fg, bool replay)
11359 {
11360 CDir *dir = diri->get_dirfrag(fg);
11361 if (dir)
11362 return dir;
11363
11364 dout(10) << "force_dir_fragment " << fg << " on " << *diri << dendl;
11365
11366 std::vector<CDir*> src, result;
11367 MDSContext::vec waiters;
11368
11369 // split a parent?
11370 frag_t parent = diri->dirfragtree.get_branch_or_leaf(fg);
11371 while (1) {
11372 CDir *pdir = diri->get_dirfrag(parent);
11373 if (pdir) {
11374 int split = fg.bits() - parent.bits();
11375 dout(10) << " splitting parent by " << split << " " << *pdir << dendl;
11376 src.push_back(pdir);
11377 adjust_dir_fragments(diri, src, parent, split, &result, waiters, replay);
11378 dir = diri->get_dirfrag(fg);
11379 if (dir) {
11380 dout(10) << "force_dir_fragment result " << *dir << dendl;
11381 break;
11382 }
11383 }
11384 if (parent == frag_t())
11385 break;
11386 frag_t last = parent;
11387 parent = parent.parent();
11388 dout(10) << " " << last << " parent is " << parent << dendl;
11389 }
11390
11391 if (!dir) {
11392 // hoover up things under fg?
11393 {
11394 auto&& p = diri->get_dirfrags_under(fg);
11395 src.insert(std::end(src), std::cbegin(p.second), std::cend(p.second));
11396 }
11397 if (src.empty()) {
11398 dout(10) << "force_dir_fragment no frags under " << fg << dendl;
11399 } else {
11400 dout(10) << " will combine frags under " << fg << ": " << src << dendl;
11401 adjust_dir_fragments(diri, src, fg, 0, &result, waiters, replay);
11402 dir = result.front();
11403 dout(10) << "force_dir_fragment result " << *dir << dendl;
11404 }
11405 }
11406 if (!replay)
11407 mds->queue_waiters(waiters);
11408 return dir;
11409 }
11410
11411 void MDCache::adjust_dir_fragments(CInode *diri,
11412 const std::vector<CDir*>& srcfrags,
11413 frag_t basefrag, int bits,
11414 std::vector<CDir*>* resultfrags,
11415 MDSContext::vec& waiters,
11416 bool replay)
11417 {
11418 dout(10) << "adjust_dir_fragments " << basefrag << " bits " << bits
11419 << " srcfrags " << srcfrags
11420 << " on " << *diri << dendl;
11421
11422 // adjust fragtree
11423 // yuck. we may have discovered the inode while it was being fragmented.
11424 if (!diri->dirfragtree.is_leaf(basefrag))
11425 diri->dirfragtree.force_to_leaf(g_ceph_context, basefrag);
11426
11427 if (bits > 0)
11428 diri->dirfragtree.split(basefrag, bits);
11429 dout(10) << " new fragtree is " << diri->dirfragtree << dendl;
11430
11431 if (srcfrags.empty())
11432 return;
11433
11434 // split
11435 CDir *parent_dir = diri->get_parent_dir();
11436 CDir *parent_subtree = 0;
11437 if (parent_dir)
11438 parent_subtree = get_subtree_root(parent_dir);
11439
11440 ceph_assert(srcfrags.size() >= 1);
11441 if (bits > 0) {
11442 // SPLIT
11443 ceph_assert(srcfrags.size() == 1);
11444 CDir *dir = srcfrags.front();
11445
11446 dir->split(bits, resultfrags, waiters, replay);
11447
11448 // did i change the subtree map?
11449 if (dir->is_subtree_root()) {
11450 // new frags are now separate subtrees
11451 for (const auto& dir : *resultfrags) {
11452 subtrees[dir].clear(); // new frag is now its own subtree
11453 }
11454
11455 // was i a bound?
11456 if (parent_subtree) {
11457 ceph_assert(subtrees[parent_subtree].count(dir));
11458 subtrees[parent_subtree].erase(dir);
11459 for (const auto& dir : *resultfrags) {
11460 ceph_assert(dir->is_subtree_root());
11461 subtrees[parent_subtree].insert(dir);
11462 }
11463 }
11464
11465 // adjust my bounds.
11466 set<CDir*> bounds;
11467 bounds.swap(subtrees[dir]);
11468 subtrees.erase(dir);
11469 for (set<CDir*>::iterator p = bounds.begin();
11470 p != bounds.end();
11471 ++p) {
11472 CDir *frag = get_subtree_root((*p)->get_parent_dir());
11473 subtrees[frag].insert(*p);
11474 }
11475
11476 show_subtrees(10);
11477 }
11478
11479 diri->close_dirfrag(dir->get_frag());
11480
11481 } else {
11482 // MERGE
11483
11484 // are my constituent bits subtrees? if so, i will be too.
11485 // (it's all or none, actually.)
11486 bool any_subtree = false, any_non_subtree = false;
11487 for (const auto& dir : srcfrags) {
11488 if (dir->is_subtree_root())
11489 any_subtree = true;
11490 else
11491 any_non_subtree = true;
11492 }
11493 ceph_assert(!any_subtree || !any_non_subtree);
11494
11495 set<CDir*> new_bounds;
11496 if (any_subtree) {
11497 for (const auto& dir : srcfrags) {
11498 // this simplifies the code that find subtrees underneath the dirfrag
11499 if (!dir->is_subtree_root()) {
11500 dir->state_set(CDir::STATE_AUXSUBTREE);
11501 adjust_subtree_auth(dir, mds->get_nodeid());
11502 }
11503 }
11504
11505 for (const auto& dir : srcfrags) {
11506 ceph_assert(dir->is_subtree_root());
11507 dout(10) << " taking srcfrag subtree bounds from " << *dir << dendl;
11508 map<CDir*, set<CDir*> >::iterator q = subtrees.find(dir);
11509 set<CDir*>::iterator r = q->second.begin();
11510 while (r != subtrees[dir].end()) {
11511 new_bounds.insert(*r);
11512 subtrees[dir].erase(r++);
11513 }
11514 subtrees.erase(q);
11515
11516 // remove myself as my parent's bound
11517 if (parent_subtree)
11518 subtrees[parent_subtree].erase(dir);
11519 }
11520 }
11521
11522 // merge
11523 CDir *f = new CDir(diri, basefrag, this, srcfrags.front()->is_auth());
11524 f->merge(srcfrags, waiters, replay);
11525
11526 if (any_subtree) {
11527 ceph_assert(f->is_subtree_root());
11528 subtrees[f].swap(new_bounds);
11529 if (parent_subtree)
11530 subtrees[parent_subtree].insert(f);
11531
11532 show_subtrees(10);
11533 }
11534
11535 resultfrags->push_back(f);
11536 }
11537 }
11538
11539
11540 class C_MDC_FragmentFrozen : public MDSInternalContext {
11541 MDCache *mdcache;
11542 MDRequestRef mdr;
11543 public:
11544 C_MDC_FragmentFrozen(MDCache *m, MDRequestRef& r) :
11545 MDSInternalContext(m->mds), mdcache(m), mdr(r) {}
11546 void finish(int r) override {
11547 mdcache->fragment_frozen(mdr, r);
11548 }
11549 };
11550
11551 bool MDCache::can_fragment(CInode *diri, const std::vector<CDir*>& dirs)
11552 {
11553 if (is_readonly()) {
11554 dout(7) << "can_fragment: read-only FS, no fragmenting for now" << dendl;
11555 return false;
11556 }
11557 if (mds->is_cluster_degraded()) {
11558 dout(7) << "can_fragment: cluster degraded, no fragmenting for now" << dendl;
11559 return false;
11560 }
11561 if (diri->get_parent_dir() &&
11562 diri->get_parent_dir()->get_inode()->is_stray()) {
11563 dout(7) << "can_fragment: i won't merge|split anything in stray" << dendl;
11564 return false;
11565 }
11566 if (diri->is_mdsdir() || diri->ino() == CEPH_INO_CEPH) {
11567 dout(7) << "can_fragment: i won't fragment mdsdir or .ceph" << dendl;
11568 return false;
11569 }
11570
11571 for (const auto& dir : dirs) {
11572 if (dir->scrub_is_in_progress()) {
11573 dout(7) << "can_fragment: scrub in progress " << *dir << dendl;
11574 return false;
11575 }
11576
11577 if (dir->state_test(CDir::STATE_FRAGMENTING)) {
11578 dout(7) << "can_fragment: already fragmenting " << *dir << dendl;
11579 return false;
11580 }
11581 if (!dir->is_auth()) {
11582 dout(7) << "can_fragment: not auth on " << *dir << dendl;
11583 return false;
11584 }
11585 if (dir->is_bad()) {
11586 dout(7) << "can_fragment: bad dirfrag " << *dir << dendl;
11587 return false;
11588 }
11589 if (dir->is_frozen() ||
11590 dir->is_freezing()) {
11591 dout(7) << "can_fragment: can't merge, freezing|frozen. wait for other exports to finish first." << dendl;
11592 return false;
11593 }
11594 }
11595
11596 return true;
11597 }
11598
11599 void MDCache::split_dir(CDir *dir, int bits)
11600 {
11601 dout(7) << __func__ << " " << *dir << " bits " << bits << dendl;
11602 ceph_assert(dir->is_auth());
11603 CInode *diri = dir->inode;
11604
11605 std::vector<CDir*> dirs;
11606 dirs.push_back(dir);
11607
11608 if (!can_fragment(diri, dirs)) {
11609 dout(7) << __func__ << " cannot fragment right now, dropping" << dendl;
11610 return;
11611 }
11612
11613 if (dir->frag.bits() + bits > 24) {
11614 dout(7) << __func__ << " frag bits > 24, dropping" << dendl;
11615 return;
11616 }
11617
11618 MDRequestRef mdr = request_start_internal(CEPH_MDS_OP_FRAGMENTDIR);
11619 mdr->more()->fragment_base = dir->dirfrag();
11620
11621 ceph_assert(fragments.count(dir->dirfrag()) == 0);
11622 fragment_info_t& info = fragments[dir->dirfrag()];
11623 info.mdr = mdr;
11624 info.dirs.push_back(dir);
11625 info.bits = bits;
11626 info.last_cum_auth_pins_change = ceph_clock_now();
11627
11628 fragment_freeze_dirs(dirs);
11629 // initial mark+complete pass
11630 fragment_mark_and_complete(mdr);
11631 }
11632
11633 void MDCache::merge_dir(CInode *diri, frag_t frag)
11634 {
11635 dout(7) << "merge_dir to " << frag << " on " << *diri << dendl;
11636
11637 auto&& [all, dirs] = diri->get_dirfrags_under(frag);
11638 if (!all) {
11639 dout(7) << "don't have all frags under " << frag << " for " << *diri << dendl;
11640 return;
11641 }
11642
11643 if (diri->dirfragtree.is_leaf(frag)) {
11644 dout(10) << " " << frag << " already a leaf for " << *diri << dendl;
11645 return;
11646 }
11647
11648 if (!can_fragment(diri, dirs))
11649 return;
11650
11651 CDir *first = dirs.front();
11652 int bits = first->get_frag().bits() - frag.bits();
11653 dout(10) << " we are merging by " << bits << " bits" << dendl;
11654
11655 dirfrag_t basedirfrag(diri->ino(), frag);
11656 MDRequestRef mdr = request_start_internal(CEPH_MDS_OP_FRAGMENTDIR);
11657 mdr->more()->fragment_base = basedirfrag;
11658
11659 ceph_assert(fragments.count(basedirfrag) == 0);
11660 fragment_info_t& info = fragments[basedirfrag];
11661 info.mdr = mdr;
11662 info.dirs = dirs;
11663 info.bits = -bits;
11664 info.last_cum_auth_pins_change = ceph_clock_now();
11665
11666 fragment_freeze_dirs(dirs);
11667 // initial mark+complete pass
11668 fragment_mark_and_complete(mdr);
11669 }
11670
11671 void MDCache::fragment_freeze_dirs(const std::vector<CDir*>& dirs)
11672 {
11673 bool any_subtree = false, any_non_subtree = false;
11674 for (const auto& dir : dirs) {
11675 dir->auth_pin(dir); // until we mark and complete them
11676 dir->state_set(CDir::STATE_FRAGMENTING);
11677 dir->freeze_dir();
11678 ceph_assert(dir->is_freezing_dir());
11679
11680 if (dir->is_subtree_root())
11681 any_subtree = true;
11682 else
11683 any_non_subtree = true;
11684 }
11685
11686 if (any_subtree && any_non_subtree) {
11687 // either all dirfrags are subtree roots or all are not.
11688 for (const auto& dir : dirs) {
11689 if (dir->is_subtree_root()) {
11690 ceph_assert(dir->state_test(CDir::STATE_AUXSUBTREE));
11691 } else {
11692 dir->state_set(CDir::STATE_AUXSUBTREE);
11693 adjust_subtree_auth(dir, mds->get_nodeid());
11694 }
11695 }
11696 }
11697 }
11698
11699 class C_MDC_FragmentMarking : public MDCacheContext {
11700 MDRequestRef mdr;
11701 public:
11702 C_MDC_FragmentMarking(MDCache *m, MDRequestRef& r) : MDCacheContext(m), mdr(r) {}
11703 void finish(int r) override {
11704 mdcache->fragment_mark_and_complete(mdr);
11705 }
11706 };
11707
11708 void MDCache::fragment_mark_and_complete(MDRequestRef& mdr)
11709 {
11710 dirfrag_t basedirfrag = mdr->more()->fragment_base;
11711 map<dirfrag_t,fragment_info_t>::iterator it = fragments.find(basedirfrag);
11712 if (it == fragments.end() || it->second.mdr != mdr) {
11713 dout(7) << "fragment_mark_and_complete " << basedirfrag << " must have aborted" << dendl;
11714 request_finish(mdr);
11715 return;
11716 }
11717
11718 fragment_info_t& info = it->second;
11719 CInode *diri = info.dirs.front()->get_inode();
11720 dout(10) << "fragment_mark_and_complete " << info.dirs << " on " << *diri << dendl;
11721
11722 MDSGatherBuilder gather(g_ceph_context);
11723
11724 for (const auto& dir : info.dirs) {
11725 bool ready = true;
11726 if (!dir->is_complete()) {
11727 dout(15) << " fetching incomplete " << *dir << dendl;
11728 dir->fetch(gather.new_sub(), true); // ignore authpinnability
11729 ready = false;
11730 } else if (dir->get_frag() == frag_t()) {
11731 // The COMPLETE flag gets lost if we fragment a new dirfrag, then rollback
11732 // the operation. To avoid CDir::fetch() complaining about missing object,
11733 // we commit new dirfrag first.
11734 if (dir->state_test(CDir::STATE_CREATING)) {
11735 dout(15) << " waiting until new dir gets journaled " << *dir << dendl;
11736 dir->add_waiter(CDir::WAIT_CREATED, gather.new_sub());
11737 ready = false;
11738 } else if (dir->is_new()) {
11739 dout(15) << " committing new " << *dir << dendl;
11740 ceph_assert(dir->is_dirty());
11741 dir->commit(0, gather.new_sub(), true);
11742 ready = false;
11743 }
11744 }
11745 if (!ready)
11746 continue;
11747
11748 if (!dir->state_test(CDir::STATE_DNPINNEDFRAG)) {
11749 dout(15) << " marking " << *dir << dendl;
11750 for (auto &p : dir->items) {
11751 CDentry *dn = p.second;
11752 dn->get(CDentry::PIN_FRAGMENTING);
11753 ceph_assert(!dn->state_test(CDentry::STATE_FRAGMENTING));
11754 dn->state_set(CDentry::STATE_FRAGMENTING);
11755 }
11756 dir->state_set(CDir::STATE_DNPINNEDFRAG);
11757 dir->auth_unpin(dir);
11758 } else {
11759 dout(15) << " already marked " << *dir << dendl;
11760 }
11761 }
11762 if (gather.has_subs()) {
11763 gather.set_finisher(new C_MDC_FragmentMarking(this, mdr));
11764 gather.activate();
11765 return;
11766 }
11767
11768 for (const auto& dir : info.dirs) {
11769 if (!dir->is_frozen_dir()) {
11770 ceph_assert(dir->is_freezing_dir());
11771 dir->add_waiter(CDir::WAIT_FROZEN, gather.new_sub());
11772 }
11773 }
11774 if (gather.has_subs()) {
11775 gather.set_finisher(new C_MDC_FragmentFrozen(this, mdr));
11776 gather.activate();
11777 // flush log so that request auth_pins are retired
11778 mds->mdlog->flush();
11779 return;
11780 }
11781
11782 fragment_frozen(mdr, 0);
11783 }
11784
11785 void MDCache::fragment_unmark_unfreeze_dirs(const std::vector<CDir*>& dirs)
11786 {
11787 dout(10) << "fragment_unmark_unfreeze_dirs " << dirs << dendl;
11788 for (const auto& dir : dirs) {
11789 dout(10) << " frag " << *dir << dendl;
11790
11791 ceph_assert(dir->state_test(CDir::STATE_FRAGMENTING));
11792 dir->state_clear(CDir::STATE_FRAGMENTING);
11793
11794 if (dir->state_test(CDir::STATE_DNPINNEDFRAG)) {
11795 dir->state_clear(CDir::STATE_DNPINNEDFRAG);
11796
11797 for (auto &p : dir->items) {
11798 CDentry *dn = p.second;
11799 ceph_assert(dn->state_test(CDentry::STATE_FRAGMENTING));
11800 dn->state_clear(CDentry::STATE_FRAGMENTING);
11801 dn->put(CDentry::PIN_FRAGMENTING);
11802 }
11803 } else {
11804 dir->auth_unpin(dir);
11805 }
11806
11807 dir->unfreeze_dir();
11808 }
11809 }
11810
11811 bool MDCache::fragment_are_all_frozen(CDir *dir)
11812 {
11813 ceph_assert(dir->is_frozen_dir());
11814 map<dirfrag_t,fragment_info_t>::iterator p;
11815 for (p = fragments.lower_bound(dirfrag_t(dir->ino(), 0));
11816 p != fragments.end() && p->first.ino == dir->ino();
11817 ++p) {
11818 if (p->first.frag.contains(dir->get_frag()))
11819 return p->second.all_frozen;
11820 }
11821 ceph_abort();
11822 return false;
11823 }
11824
11825 void MDCache::fragment_freeze_inc_num_waiters(CDir *dir)
11826 {
11827 map<dirfrag_t,fragment_info_t>::iterator p;
11828 for (p = fragments.lower_bound(dirfrag_t(dir->ino(), 0));
11829 p != fragments.end() && p->first.ino == dir->ino();
11830 ++p) {
11831 if (p->first.frag.contains(dir->get_frag())) {
11832 p->second.num_remote_waiters++;
11833 return;
11834 }
11835 }
11836 ceph_abort();
11837 }
11838
11839 void MDCache::find_stale_fragment_freeze()
11840 {
11841 dout(10) << "find_stale_fragment_freeze" << dendl;
11842 // see comment in Migrator::find_stale_export_freeze()
11843 utime_t now = ceph_clock_now();
11844 utime_t cutoff = now;
11845 cutoff -= g_conf()->mds_freeze_tree_timeout;
11846
11847 for (map<dirfrag_t,fragment_info_t>::iterator p = fragments.begin();
11848 p != fragments.end(); ) {
11849 dirfrag_t df = p->first;
11850 fragment_info_t& info = p->second;
11851 ++p;
11852 if (info.all_frozen)
11853 continue;
11854 CDir *dir;
11855 int total_auth_pins = 0;
11856 for (const auto& d : info.dirs) {
11857 dir = d;
11858 if (!dir->state_test(CDir::STATE_DNPINNEDFRAG)) {
11859 total_auth_pins = -1;
11860 break;
11861 }
11862 if (dir->is_frozen_dir())
11863 continue;
11864 total_auth_pins += dir->get_auth_pins() + dir->get_dir_auth_pins();
11865 }
11866 if (total_auth_pins < 0)
11867 continue;
11868 if (info.last_cum_auth_pins != total_auth_pins) {
11869 info.last_cum_auth_pins = total_auth_pins;
11870 info.last_cum_auth_pins_change = now;
11871 continue;
11872 }
11873 if (info.last_cum_auth_pins_change >= cutoff)
11874 continue;
11875 dir = info.dirs.front();
11876 if (info.num_remote_waiters > 0 ||
11877 (!dir->inode->is_root() && dir->get_parent_dir()->is_freezing())) {
11878 dout(10) << " cancel fragmenting " << df << " bit " << info.bits << dendl;
11879 std::vector<CDir*> dirs;
11880 info.dirs.swap(dirs);
11881 fragments.erase(df);
11882 fragment_unmark_unfreeze_dirs(dirs);
11883 }
11884 }
11885 }
11886
11887 class C_MDC_FragmentPrep : public MDCacheLogContext {
11888 MDRequestRef mdr;
11889 public:
11890 C_MDC_FragmentPrep(MDCache *m, MDRequestRef& r) : MDCacheLogContext(m), mdr(r) {}
11891 void finish(int r) override {
11892 mdcache->_fragment_logged(mdr);
11893 }
11894 };
11895
11896 class C_MDC_FragmentStore : public MDCacheContext {
11897 MDRequestRef mdr;
11898 public:
11899 C_MDC_FragmentStore(MDCache *m, MDRequestRef& r) : MDCacheContext(m), mdr(r) {}
11900 void finish(int r) override {
11901 mdcache->_fragment_stored(mdr);
11902 }
11903 };
11904
11905 class C_MDC_FragmentCommit : public MDCacheLogContext {
11906 dirfrag_t basedirfrag;
11907 MDRequestRef mdr;
11908 public:
11909 C_MDC_FragmentCommit(MDCache *m, dirfrag_t df, const MDRequestRef& r) :
11910 MDCacheLogContext(m), basedirfrag(df), mdr(r) {}
11911 void finish(int r) override {
11912 mdcache->_fragment_committed(basedirfrag, mdr);
11913 }
11914 };
11915
11916 class C_IO_MDC_FragmentPurgeOld : public MDCacheIOContext {
11917 dirfrag_t basedirfrag;
11918 int bits;
11919 MDRequestRef mdr;
11920 public:
11921 C_IO_MDC_FragmentPurgeOld(MDCache *m, dirfrag_t f, int b,
11922 const MDRequestRef& r) :
11923 MDCacheIOContext(m), basedirfrag(f), bits(b), mdr(r) {}
11924 void finish(int r) override {
11925 ceph_assert(r == 0 || r == -CEPHFS_ENOENT);
11926 mdcache->_fragment_old_purged(basedirfrag, bits, mdr);
11927 }
11928 void print(ostream& out) const override {
11929 out << "fragment_purge_old(" << basedirfrag << ")";
11930 }
11931 };
11932
11933 void MDCache::fragment_frozen(MDRequestRef& mdr, int r)
11934 {
11935 dirfrag_t basedirfrag = mdr->more()->fragment_base;
11936 map<dirfrag_t,fragment_info_t>::iterator it = fragments.find(basedirfrag);
11937 if (it == fragments.end() || it->second.mdr != mdr) {
11938 dout(7) << "fragment_frozen " << basedirfrag << " must have aborted" << dendl;
11939 request_finish(mdr);
11940 return;
11941 }
11942
11943 ceph_assert(r == 0);
11944 fragment_info_t& info = it->second;
11945 dout(10) << "fragment_frozen " << basedirfrag.frag << " by " << info.bits
11946 << " on " << info.dirs.front()->get_inode() << dendl;
11947
11948 info.all_frozen = true;
11949 dispatch_fragment_dir(mdr);
11950 }
11951
11952 void MDCache::dispatch_fragment_dir(MDRequestRef& mdr)
11953 {
11954 dirfrag_t basedirfrag = mdr->more()->fragment_base;
11955 map<dirfrag_t,fragment_info_t>::iterator it = fragments.find(basedirfrag);
11956 if (it == fragments.end() || it->second.mdr != mdr) {
11957 dout(7) << "dispatch_fragment_dir " << basedirfrag << " must have aborted" << dendl;
11958 request_finish(mdr);
11959 return;
11960 }
11961
11962 fragment_info_t& info = it->second;
11963 CInode *diri = info.dirs.front()->get_inode();
11964
11965 dout(10) << "dispatch_fragment_dir " << basedirfrag << " bits " << info.bits
11966 << " on " << *diri << dendl;
11967
11968 if (mdr->more()->peer_error)
11969 mdr->aborted = true;
11970
11971 if (!mdr->aborted) {
11972 MutationImpl::LockOpVec lov;
11973 lov.add_wrlock(&diri->dirfragtreelock);
11974 // prevent a racing gather on any other scatterlocks too
11975 lov.lock_scatter_gather(&diri->nestlock);
11976 lov.lock_scatter_gather(&diri->filelock);
11977 if (!mds->locker->acquire_locks(mdr, lov, NULL, true)) {
11978 if (!mdr->aborted)
11979 return;
11980 }
11981 }
11982
11983 if (mdr->aborted) {
11984 dout(10) << " can't auth_pin " << *diri << ", requeuing dir "
11985 << info.dirs.front()->dirfrag() << dendl;
11986 if (info.bits > 0)
11987 mds->balancer->queue_split(info.dirs.front(), false);
11988 else
11989 mds->balancer->queue_merge(info.dirs.front());
11990 fragment_unmark_unfreeze_dirs(info.dirs);
11991 fragments.erase(it);
11992 request_finish(mdr);
11993 return;
11994 }
11995
11996 mdr->ls = mds->mdlog->get_current_segment();
11997 EFragment *le = new EFragment(mds->mdlog, EFragment::OP_PREPARE, basedirfrag, info.bits);
11998 mds->mdlog->start_entry(le);
11999
12000 for (const auto& dir : info.dirs) {
12001 dirfrag_rollback rollback;
12002 rollback.fnode = dir->fnode;
12003 le->add_orig_frag(dir->get_frag(), &rollback);
12004 }
12005
12006 // refragment
12007 MDSContext::vec waiters;
12008 adjust_dir_fragments(diri, info.dirs, basedirfrag.frag, info.bits,
12009 &info.resultfrags, waiters, false);
12010 if (g_conf()->mds_debug_frag)
12011 diri->verify_dirfrags();
12012 mds->queue_waiters(waiters);
12013
12014 for (const auto& fg : le->orig_frags)
12015 ceph_assert(!diri->dirfragtree.is_leaf(fg));
12016
12017 le->metablob.add_dir_context(info.resultfrags.front());
12018 for (const auto& dir : info.resultfrags) {
12019 if (diri->is_auth()) {
12020 le->metablob.add_fragmented_dir(dir, false, false);
12021 } else {
12022 dir->state_set(CDir::STATE_DIRTYDFT);
12023 le->metablob.add_fragmented_dir(dir, false, true);
12024 }
12025 }
12026
12027 // dft lock
12028 if (diri->is_auth()) {
12029 // journal dirfragtree
12030 auto pi = diri->project_inode(mdr);
12031 pi.inode->version = diri->pre_dirty();
12032 predirty_journal_parents(mdr, &le->metablob, diri, 0, PREDIRTY_PRIMARY);
12033 journal_dirty_inode(mdr.get(), &le->metablob, diri);
12034 } else {
12035 mds->locker->mark_updated_scatterlock(&diri->dirfragtreelock);
12036 mdr->ls->dirty_dirfrag_dirfragtree.push_back(&diri->item_dirty_dirfrag_dirfragtree);
12037 mdr->add_updated_lock(&diri->dirfragtreelock);
12038 }
12039
12040 /*
12041 // filelock
12042 mds->locker->mark_updated_scatterlock(&diri->filelock);
12043 mut->ls->dirty_dirfrag_dir.push_back(&diri->item_dirty_dirfrag_dir);
12044 mut->add_updated_lock(&diri->filelock);
12045
12046 // dirlock
12047 mds->locker->mark_updated_scatterlock(&diri->nestlock);
12048 mut->ls->dirty_dirfrag_nest.push_back(&diri->item_dirty_dirfrag_nest);
12049 mut->add_updated_lock(&diri->nestlock);
12050 */
12051
12052 add_uncommitted_fragment(basedirfrag, info.bits, le->orig_frags, mdr->ls);
12053 mds->server->submit_mdlog_entry(le, new C_MDC_FragmentPrep(this, mdr),
12054 mdr, __func__);
12055 mds->mdlog->flush();
12056 }
12057
12058 void MDCache::_fragment_logged(MDRequestRef& mdr)
12059 {
12060 dirfrag_t basedirfrag = mdr->more()->fragment_base;
12061 auto& info = fragments.at(basedirfrag);
12062 CInode *diri = info.resultfrags.front()->get_inode();
12063
12064 dout(10) << "fragment_logged " << basedirfrag << " bits " << info.bits
12065 << " on " << *diri << dendl;
12066 mdr->mark_event("prepare logged");
12067
12068 mdr->apply(); // mark scatterlock
12069
12070 // store resulting frags
12071 MDSGatherBuilder gather(g_ceph_context, new C_MDC_FragmentStore(this, mdr));
12072
12073 for (const auto& dir : info.resultfrags) {
12074 dout(10) << " storing result frag " << *dir << dendl;
12075
12076 dir->mark_dirty(mdr->ls);
12077 dir->mark_new(mdr->ls);
12078
12079 // freeze and store them too
12080 dir->auth_pin(this);
12081 dir->state_set(CDir::STATE_FRAGMENTING);
12082 dir->commit(0, gather.new_sub(), true); // ignore authpinnability
12083 }
12084
12085 gather.activate();
12086 }
12087
12088 void MDCache::_fragment_stored(MDRequestRef& mdr)
12089 {
12090 dirfrag_t basedirfrag = mdr->more()->fragment_base;
12091 fragment_info_t &info = fragments.at(basedirfrag);
12092 CDir *first = info.resultfrags.front();
12093 CInode *diri = first->get_inode();
12094
12095 dout(10) << "fragment_stored " << basedirfrag << " bits " << info.bits
12096 << " on " << *diri << dendl;
12097 mdr->mark_event("new frags stored");
12098
12099 // tell peers
12100 mds_rank_t diri_auth = (first->is_subtree_root() && !diri->is_auth()) ?
12101 diri->authority().first : CDIR_AUTH_UNKNOWN;
12102 for (const auto &p : first->get_replicas()) {
12103 if (mds->mdsmap->get_state(p.first) < MDSMap::STATE_REJOIN ||
12104 (mds->mdsmap->get_state(p.first) == MDSMap::STATE_REJOIN &&
12105 rejoin_gather.count(p.first)))
12106 continue;
12107
12108 auto notify = make_message<MMDSFragmentNotify>(basedirfrag, info.bits, mdr->reqid.tid);
12109 if (diri_auth != CDIR_AUTH_UNKNOWN && // subtree root
12110 diri_auth != p.first) { // not auth mds of diri
12111 /*
12112 * In the nornal case, mds does not trim dir inode whose child dirfrags
12113 * are likely being fragmented (see trim_inode()). But when fragmenting
12114 * subtree roots, following race can happen:
12115 *
12116 * - mds.a (auth mds of dirfrag) sends fragment_notify message to
12117 * mds.c and drops wrlock on dirfragtreelock.
12118 * - mds.b (auth mds of dir inode) changes dirfragtreelock state to
12119 * SYNC and send lock message mds.c
12120 * - mds.c receives the lock message and changes dirfragtreelock state
12121 * to SYNC
12122 * - mds.c trim dirfrag and dir inode from its cache
12123 * - mds.c receives the fragment_notify message
12124 *
12125 * So we need to ensure replicas have received the notify, then unlock
12126 * the dirfragtreelock.
12127 */
12128 notify->mark_ack_wanted();
12129 info.notify_ack_waiting.insert(p.first);
12130 }
12131
12132 // freshly replicate new dirs to peers
12133 for (const auto& dir : info.resultfrags) {
12134 encode_replica_dir(dir, p.first, notify->basebl);
12135 }
12136
12137 mds->send_message_mds(notify, p.first);
12138 }
12139
12140 // journal commit
12141 EFragment *le = new EFragment(mds->mdlog, EFragment::OP_COMMIT, basedirfrag, info.bits);
12142 mds->mdlog->start_submit_entry(le, new C_MDC_FragmentCommit(this, basedirfrag, mdr));
12143
12144
12145 // unfreeze resulting frags
12146 for (const auto& dir : info.resultfrags) {
12147 dout(10) << " result frag " << *dir << dendl;
12148
12149 for (auto &p : dir->items) {
12150 CDentry *dn = p.second;
12151 ceph_assert(dn->state_test(CDentry::STATE_FRAGMENTING));
12152 dn->state_clear(CDentry::STATE_FRAGMENTING);
12153 dn->put(CDentry::PIN_FRAGMENTING);
12154 }
12155
12156 // unfreeze
12157 dir->unfreeze_dir();
12158 }
12159
12160 if (info.notify_ack_waiting.empty()) {
12161 fragment_drop_locks(info);
12162 } else {
12163 mds->locker->drop_locks_for_fragment_unfreeze(mdr.get());
12164 }
12165 }
12166
12167 void MDCache::_fragment_committed(dirfrag_t basedirfrag, const MDRequestRef& mdr)
12168 {
12169 dout(10) << "fragment_committed " << basedirfrag << dendl;
12170 if (mdr)
12171 mdr->mark_event("commit logged");
12172
12173 ufragment &uf = uncommitted_fragments.at(basedirfrag);
12174
12175 // remove old frags
12176 C_GatherBuilder gather(
12177 g_ceph_context,
12178 new C_OnFinisher(
12179 new C_IO_MDC_FragmentPurgeOld(this, basedirfrag, uf.bits, mdr),
12180 mds->finisher));
12181
12182 SnapContext nullsnapc;
12183 object_locator_t oloc(mds->get_metadata_pool());
12184 for (const auto& fg : uf.old_frags) {
12185 object_t oid = CInode::get_object_name(basedirfrag.ino, fg, "");
12186 ObjectOperation op;
12187 if (fg == frag_t()) {
12188 // backtrace object
12189 dout(10) << " truncate orphan dirfrag " << oid << dendl;
12190 op.truncate(0);
12191 op.omap_clear();
12192 } else {
12193 dout(10) << " removing orphan dirfrag " << oid << dendl;
12194 op.remove();
12195 }
12196 mds->objecter->mutate(oid, oloc, op, nullsnapc,
12197 ceph::real_clock::now(),
12198 0, gather.new_sub());
12199 }
12200
12201 ceph_assert(gather.has_subs());
12202 gather.activate();
12203 }
12204
12205 void MDCache::_fragment_old_purged(dirfrag_t basedirfrag, int bits, const MDRequestRef& mdr)
12206 {
12207 dout(10) << "fragment_old_purged " << basedirfrag << dendl;
12208 if (mdr)
12209 mdr->mark_event("old frags purged");
12210
12211 EFragment *le = new EFragment(mds->mdlog, EFragment::OP_FINISH, basedirfrag, bits);
12212 mds->mdlog->start_submit_entry(le);
12213
12214 finish_uncommitted_fragment(basedirfrag, EFragment::OP_FINISH);
12215
12216 if (mds->logger) {
12217 if (bits > 0) {
12218 mds->logger->inc(l_mds_dir_split);
12219 } else {
12220 mds->logger->inc(l_mds_dir_merge);
12221 }
12222 }
12223
12224 if (mdr) {
12225 auto it = fragments.find(basedirfrag);
12226 ceph_assert(it != fragments.end());
12227 it->second.finishing = true;
12228 if (it->second.notify_ack_waiting.empty())
12229 fragment_maybe_finish(it);
12230 else
12231 mdr->mark_event("wating for notify acks");
12232 }
12233 }
12234
12235 void MDCache::fragment_drop_locks(fragment_info_t& info)
12236 {
12237 mds->locker->drop_locks(info.mdr.get());
12238 request_finish(info.mdr);
12239 //info.mdr.reset();
12240 }
12241
12242 void MDCache::fragment_maybe_finish(const fragment_info_iterator& it)
12243 {
12244 if (!it->second.finishing)
12245 return;
12246
12247 // unmark & auth_unpin
12248 for (const auto &dir : it->second.resultfrags) {
12249 dir->state_clear(CDir::STATE_FRAGMENTING);
12250 dir->auth_unpin(this);
12251
12252 // In case the resulting fragments are beyond the split size,
12253 // we might need to split them again right away (they could
12254 // have been taking inserts between unfreezing and getting
12255 // here)
12256 mds->balancer->maybe_fragment(dir, false);
12257 }
12258
12259 fragments.erase(it);
12260 }
12261
12262
12263 void MDCache::handle_fragment_notify_ack(const cref_t<MMDSFragmentNotifyAck> &ack)
12264 {
12265 dout(10) << "handle_fragment_notify_ack " << *ack << " from " << ack->get_source() << dendl;
12266 mds_rank_t from = mds_rank_t(ack->get_source().num());
12267
12268 if (mds->get_state() < MDSMap::STATE_ACTIVE) {
12269 return;
12270 }
12271
12272 auto it = fragments.find(ack->get_base_dirfrag());
12273 if (it == fragments.end() ||
12274 it->second.get_tid() != ack->get_tid()) {
12275 dout(10) << "handle_fragment_notify_ack obsolete message, dropping" << dendl;
12276 return;
12277 }
12278
12279 if (it->second.notify_ack_waiting.erase(from) &&
12280 it->second.notify_ack_waiting.empty()) {
12281 fragment_drop_locks(it->second);
12282 fragment_maybe_finish(it);
12283 }
12284 }
12285
12286 void MDCache::handle_fragment_notify(const cref_t<MMDSFragmentNotify> &notify)
12287 {
12288 dout(10) << "handle_fragment_notify " << *notify << " from " << notify->get_source() << dendl;
12289 mds_rank_t from = mds_rank_t(notify->get_source().num());
12290
12291 if (mds->get_state() < MDSMap::STATE_REJOIN) {
12292 return;
12293 }
12294
12295 CInode *diri = get_inode(notify->get_ino());
12296 if (diri) {
12297 frag_t base = notify->get_basefrag();
12298 int bits = notify->get_bits();
12299
12300 /*
12301 if ((bits < 0 && diri->dirfragtree.is_leaf(base)) ||
12302 (bits > 0 && !diri->dirfragtree.is_leaf(base))) {
12303 dout(10) << " dft " << diri->dirfragtree << " state doesn't match " << base << " by " << bits
12304 << ", must have found out during resolve/rejoin? ignoring. " << *diri << dendl;
12305 return;
12306 }
12307 */
12308
12309 // refragment
12310 MDSContext::vec waiters;
12311 std::vector<CDir*> resultfrags;
12312 adjust_dir_fragments(diri, base, bits, &resultfrags, waiters, false);
12313 if (g_conf()->mds_debug_frag)
12314 diri->verify_dirfrags();
12315
12316 for (const auto& dir : resultfrags) {
12317 diri->take_dir_waiting(dir->get_frag(), waiters);
12318 }
12319
12320 // add new replica dirs values
12321 auto p = notify->basebl.cbegin();
12322 while (!p.end()) {
12323 CDir *tmp_dir = nullptr;
12324 decode_replica_dir(tmp_dir, p, diri, from, waiters);
12325 }
12326
12327 mds->queue_waiters(waiters);
12328 } else {
12329 ceph_abort();
12330 }
12331
12332 if (notify->is_ack_wanted()) {
12333 auto ack = make_message<MMDSFragmentNotifyAck>(notify->get_base_dirfrag(),
12334 notify->get_bits(), notify->get_tid());
12335 mds->send_message_mds(ack, from);
12336 }
12337 }
12338
12339 void MDCache::add_uncommitted_fragment(dirfrag_t basedirfrag, int bits, const frag_vec_t& old_frags,
12340 LogSegment *ls, bufferlist *rollback)
12341 {
12342 dout(10) << "add_uncommitted_fragment: base dirfrag " << basedirfrag << " bits " << bits << dendl;
12343 ceph_assert(!uncommitted_fragments.count(basedirfrag));
12344 ufragment& uf = uncommitted_fragments[basedirfrag];
12345 uf.old_frags = old_frags;
12346 uf.bits = bits;
12347 uf.ls = ls;
12348 ls->uncommitted_fragments.insert(basedirfrag);
12349 if (rollback)
12350 uf.rollback.swap(*rollback);
12351 }
12352
12353 void MDCache::finish_uncommitted_fragment(dirfrag_t basedirfrag, int op)
12354 {
12355 dout(10) << "finish_uncommitted_fragments: base dirfrag " << basedirfrag
12356 << " op " << EFragment::op_name(op) << dendl;
12357 map<dirfrag_t, ufragment>::iterator it = uncommitted_fragments.find(basedirfrag);
12358 if (it != uncommitted_fragments.end()) {
12359 ufragment& uf = it->second;
12360 if (op != EFragment::OP_FINISH && !uf.old_frags.empty()) {
12361 uf.committed = true;
12362 } else {
12363 uf.ls->uncommitted_fragments.erase(basedirfrag);
12364 mds->queue_waiters(uf.waiters);
12365 uncommitted_fragments.erase(it);
12366 }
12367 }
12368 }
12369
12370 void MDCache::rollback_uncommitted_fragment(dirfrag_t basedirfrag, frag_vec_t&& old_frags)
12371 {
12372 dout(10) << "rollback_uncommitted_fragment: base dirfrag " << basedirfrag
12373 << " old_frags (" << old_frags << ")" << dendl;
12374 map<dirfrag_t, ufragment>::iterator it = uncommitted_fragments.find(basedirfrag);
12375 if (it != uncommitted_fragments.end()) {
12376 ufragment& uf = it->second;
12377 if (!uf.old_frags.empty()) {
12378 uf.old_frags = std::move(old_frags);
12379 uf.committed = true;
12380 } else {
12381 uf.ls->uncommitted_fragments.erase(basedirfrag);
12382 uncommitted_fragments.erase(it);
12383 }
12384 }
12385 }
12386
12387 void MDCache::wait_for_uncommitted_fragments(MDSContext* finisher)
12388 {
12389 MDSGatherBuilder gather(g_ceph_context, finisher);
12390 for (auto& p : uncommitted_fragments) {
12391 p.second.waiters.push_back(gather.new_sub());
12392 }
12393 gather.activate();
12394 }
12395
12396 struct C_MDC_FragmentRollback : public MDCacheLogContext {
12397 MutationRef mut;
12398 C_MDC_FragmentRollback(MDCache *c, MutationRef& m) :
12399 MDCacheLogContext(c), mut(m) {}
12400 void finish(int r) override {
12401 mut->apply();
12402 get_mds()->locker->drop_locks(mut.get());
12403 mut->cleanup();
12404 }
12405 };
12406
12407 void MDCache::rollback_uncommitted_fragments()
12408 {
12409 dout(10) << "rollback_uncommitted_fragments: " << uncommitted_fragments.size() << " pending" << dendl;
12410 for (map<dirfrag_t, ufragment>::iterator p = uncommitted_fragments.begin();
12411 p != uncommitted_fragments.end();
12412 ++p) {
12413 ufragment &uf = p->second;
12414 CInode *diri = get_inode(p->first.ino);
12415 ceph_assert(diri);
12416
12417 if (uf.committed) {
12418 _fragment_committed(p->first, MDRequestRef());
12419 continue;
12420 }
12421
12422 dout(10) << " rolling back " << p->first << " refragment by " << uf.bits << " bits" << dendl;
12423
12424 MutationRef mut(new MutationImpl());
12425 mut->ls = mds->mdlog->get_current_segment();
12426 EFragment *le = new EFragment(mds->mdlog, EFragment::OP_ROLLBACK, p->first, uf.bits);
12427 mds->mdlog->start_entry(le);
12428 bool diri_auth = (diri->authority() != CDIR_AUTH_UNDEF);
12429
12430 frag_vec_t old_frags;
12431 diri->dirfragtree.get_leaves_under(p->first.frag, old_frags);
12432
12433 std::vector<CDir*> resultfrags;
12434 if (uf.old_frags.empty()) {
12435 // created by old format EFragment
12436 MDSContext::vec waiters;
12437 adjust_dir_fragments(diri, p->first.frag, -uf.bits, &resultfrags, waiters, true);
12438 } else {
12439 auto bp = uf.rollback.cbegin();
12440 for (const auto& fg : uf.old_frags) {
12441 CDir *dir = force_dir_fragment(diri, fg);
12442 resultfrags.push_back(dir);
12443
12444 dirfrag_rollback rollback;
12445 decode(rollback, bp);
12446
12447 dir->fnode = rollback.fnode;
12448
12449 dir->mark_dirty(mut->ls);
12450
12451 if (!(dir->get_fnode()->rstat == dir->get_fnode()->accounted_rstat)) {
12452 dout(10) << " dirty nestinfo on " << *dir << dendl;
12453 mds->locker->mark_updated_scatterlock(&diri->nestlock);
12454 mut->ls->dirty_dirfrag_nest.push_back(&diri->item_dirty_dirfrag_nest);
12455 mut->add_updated_lock(&diri->nestlock);
12456 }
12457 if (!(dir->get_fnode()->fragstat == dir->get_fnode()->accounted_fragstat)) {
12458 dout(10) << " dirty fragstat on " << *dir << dendl;
12459 mds->locker->mark_updated_scatterlock(&diri->filelock);
12460 mut->ls->dirty_dirfrag_dir.push_back(&diri->item_dirty_dirfrag_dir);
12461 mut->add_updated_lock(&diri->filelock);
12462 }
12463
12464 le->add_orig_frag(dir->get_frag());
12465 le->metablob.add_dir_context(dir);
12466 if (diri_auth) {
12467 le->metablob.add_fragmented_dir(dir, true, false);
12468 } else {
12469 dout(10) << " dirty dirfragtree on " << *dir << dendl;
12470 dir->state_set(CDir::STATE_DIRTYDFT);
12471 le->metablob.add_fragmented_dir(dir, true, true);
12472 }
12473 }
12474 }
12475
12476 if (diri_auth) {
12477 auto pi = diri->project_inode(mut);
12478 pi.inode->version = diri->pre_dirty();
12479 predirty_journal_parents(mut, &le->metablob, diri, 0, PREDIRTY_PRIMARY);
12480 le->metablob.add_primary_dentry(diri->get_projected_parent_dn(), diri, true);
12481 } else {
12482 mds->locker->mark_updated_scatterlock(&diri->dirfragtreelock);
12483 mut->ls->dirty_dirfrag_dirfragtree.push_back(&diri->item_dirty_dirfrag_dirfragtree);
12484 mut->add_updated_lock(&diri->dirfragtreelock);
12485 }
12486
12487 if (g_conf()->mds_debug_frag)
12488 diri->verify_dirfrags();
12489
12490 for (const auto& leaf : old_frags) {
12491 ceph_assert(!diri->dirfragtree.is_leaf(leaf));
12492 }
12493
12494 mds->mdlog->submit_entry(le, new C_MDC_FragmentRollback(this, mut));
12495
12496 uf.old_frags.swap(old_frags);
12497 _fragment_committed(p->first, MDRequestRef());
12498 }
12499 }
12500
12501 void MDCache::force_readonly()
12502 {
12503 if (is_readonly())
12504 return;
12505
12506 dout(1) << "force file system read-only" << dendl;
12507 mds->clog->warn() << "force file system read-only";
12508
12509 set_readonly();
12510
12511 mds->server->force_clients_readonly();
12512
12513 // revoke write caps
12514 int count = 0;
12515 for (auto &p : inode_map) {
12516 CInode *in = p.second;
12517 if (in->is_head())
12518 mds->locker->eval(in, CEPH_CAP_LOCKS);
12519 if (!(++count % mds->heartbeat_reset_grace()))
12520 mds->heartbeat_reset();
12521 }
12522
12523 mds->mdlog->flush();
12524 }
12525
12526
12527 // ==============================================================
12528 // debug crap
12529
12530 void MDCache::show_subtrees(int dbl, bool force_print)
12531 {
12532 if (g_conf()->mds_thrash_exports)
12533 dbl += 15;
12534
12535 //dout(10) << "show_subtrees" << dendl;
12536
12537 if (!g_conf()->subsys.should_gather(ceph_subsys_mds, dbl))
12538 return; // i won't print anything.
12539
12540 if (subtrees.empty()) {
12541 dout(ceph::dout::need_dynamic(dbl)) << "show_subtrees - no subtrees"
12542 << dendl;
12543 return;
12544 }
12545
12546 if (!force_print && subtrees.size() > SUBTREES_COUNT_THRESHOLD &&
12547 !g_conf()->subsys.should_gather<ceph_subsys_mds, 25>()) {
12548 dout(ceph::dout::need_dynamic(dbl)) << "number of subtrees = " << subtrees.size() << "; not "
12549 "printing subtrees" << dendl;
12550 return;
12551 }
12552
12553 // root frags
12554 std::vector<CDir*> basefrags;
12555 for (set<CInode*>::iterator p = base_inodes.begin();
12556 p != base_inodes.end();
12557 ++p)
12558 (*p)->get_dirfrags(basefrags);
12559 //dout(15) << "show_subtrees, base dirfrags " << basefrags << dendl;
12560 dout(15) << "show_subtrees" << dendl;
12561
12562 // queue stuff
12563 list<pair<CDir*,int> > q;
12564 string indent;
12565 set<CDir*> seen;
12566
12567 // calc max depth
12568 for (const auto& dir : basefrags) {
12569 q.emplace_back(dir, 0);
12570 }
12571
12572 set<CDir*> subtrees_seen;
12573
12574 unsigned int depth = 0;
12575 while (!q.empty()) {
12576 CDir *dir = q.front().first;
12577 unsigned int d = q.front().second;
12578 q.pop_front();
12579
12580 if (subtrees.count(dir) == 0) continue;
12581
12582 subtrees_seen.insert(dir);
12583
12584 if (d > depth) depth = d;
12585
12586 // sanity check
12587 //dout(25) << "saw depth " << d << " " << *dir << dendl;
12588 if (seen.count(dir)) dout(0) << "aah, already seen " << *dir << dendl;
12589 ceph_assert(seen.count(dir) == 0);
12590 seen.insert(dir);
12591
12592 // nested items?
12593 if (!subtrees[dir].empty()) {
12594 for (set<CDir*>::iterator p = subtrees[dir].begin();
12595 p != subtrees[dir].end();
12596 ++p) {
12597 //dout(25) << " saw sub " << **p << dendl;
12598 q.push_front(pair<CDir*,int>(*p, d+1));
12599 }
12600 }
12601 }
12602
12603 if (!force_print && depth > SUBTREES_DEPTH_THRESHOLD &&
12604 !g_conf()->subsys.should_gather<ceph_subsys_mds, 25>()) {
12605 dout(ceph::dout::need_dynamic(dbl)) << "max depth among subtrees = " << depth << "; not printing "
12606 "subtrees" << dendl;
12607 return;
12608 }
12609
12610 // print tree
12611 for (const auto& dir : basefrags) {
12612 q.emplace_back(dir, 0);
12613 }
12614
12615 while (!q.empty()) {
12616 CDir *dir = q.front().first;
12617 int d = q.front().second;
12618 q.pop_front();
12619
12620 if (subtrees.count(dir) == 0) continue;
12621
12622 // adjust indenter
12623 while ((unsigned)d < indent.size())
12624 indent.resize(d);
12625
12626 // pad
12627 string pad = "______________________________________";
12628 pad.resize(depth*2+1-indent.size());
12629 if (!subtrees[dir].empty())
12630 pad[0] = '.'; // parent
12631
12632
12633 string auth;
12634 if (dir->is_auth())
12635 auth = "auth ";
12636 else
12637 auth = " rep ";
12638
12639 char s[10];
12640 if (dir->get_dir_auth().second == CDIR_AUTH_UNKNOWN)
12641 snprintf(s, sizeof(s), "%2d ", int(dir->get_dir_auth().first));
12642 else
12643 snprintf(s, sizeof(s), "%2d,%2d", int(dir->get_dir_auth().first), int(dir->get_dir_auth().second));
12644
12645 // print
12646 dout(ceph::dout::need_dynamic(dbl)) << indent << "|_" << pad << s
12647 << " " << auth << *dir << dendl;
12648
12649 if (dir->ino() == CEPH_INO_ROOT)
12650 ceph_assert(dir->inode == root);
12651 if (dir->ino() == MDS_INO_MDSDIR(mds->get_nodeid()))
12652 ceph_assert(dir->inode == myin);
12653 if (dir->inode->is_stray() && (MDS_INO_STRAY_OWNER(dir->ino()) == mds->get_nodeid()))
12654 ceph_assert(strays[MDS_INO_STRAY_INDEX(dir->ino())] == dir->inode);
12655
12656 // nested items?
12657 if (!subtrees[dir].empty()) {
12658 // more at my level?
12659 if (!q.empty() && q.front().second == d)
12660 indent += "| ";
12661 else
12662 indent += " ";
12663
12664 for (set<CDir*>::iterator p = subtrees[dir].begin();
12665 p != subtrees[dir].end();
12666 ++p)
12667 q.push_front(pair<CDir*,int>(*p, d+2));
12668 }
12669 }
12670
12671 // verify there isn't stray crap in subtree map
12672 int lost = 0;
12673 for (map<CDir*, set<CDir*> >::iterator p = subtrees.begin();
12674 p != subtrees.end();
12675 ++p) {
12676 if (subtrees_seen.count(p->first)) continue;
12677 dout(10) << "*** stray/lost entry in subtree map: " << *p->first << dendl;
12678 lost++;
12679 }
12680 ceph_assert(lost == 0);
12681 }
12682
12683 void MDCache::show_cache()
12684 {
12685 if (!g_conf()->subsys.should_gather<ceph_subsys_mds, 7>())
12686 return;
12687 dout(7) << "show_cache" << dendl;
12688
12689 auto show_func = [this](CInode *in) {
12690 // unlinked?
12691 if (!in->parent)
12692 dout(7) << " unlinked " << *in << dendl;
12693
12694 // dirfrags?
12695 auto&& dfs = in->get_dirfrags();
12696 for (const auto& dir : dfs) {
12697 dout(7) << " dirfrag " << *dir << dendl;
12698
12699 for (auto &p : dir->items) {
12700 CDentry *dn = p.second;
12701 dout(7) << " dentry " << *dn << dendl;
12702 CDentry::linkage_t *dnl = dn->get_linkage();
12703 if (dnl->is_primary() && dnl->get_inode())
12704 dout(7) << " inode " << *dnl->get_inode() << dendl;
12705 }
12706 }
12707 };
12708
12709 for (auto &p : inode_map)
12710 show_func(p.second);
12711 for (auto &p : snap_inode_map)
12712 show_func(p.second);
12713 }
12714
12715 void MDCache::cache_status(Formatter *f)
12716 {
12717 f->open_object_section("cache");
12718
12719 f->open_object_section("pool");
12720 mempool::get_pool(mempool::mds_co::id).dump(f);
12721 f->close_section();
12722
12723 f->close_section();
12724 }
12725
12726 void MDCache::dump_tree(CInode *in, const int cur_depth, const int max_depth, Formatter *f)
12727 {
12728 ceph_assert(in);
12729 if ((max_depth >= 0) && (cur_depth > max_depth)) {
12730 return;
12731 }
12732 auto&& ls = in->get_dirfrags();
12733 for (const auto &subdir : ls) {
12734 for (const auto &p : subdir->items) {
12735 CDentry *dn = p.second;
12736 CInode *in = dn->get_linkage()->get_inode();
12737 if (in) {
12738 dump_tree(in, cur_depth + 1, max_depth, f);
12739 }
12740 }
12741 }
12742 f->open_object_section("inode");
12743 in->dump(f, CInode::DUMP_DEFAULT | CInode::DUMP_DIRFRAGS);
12744 f->close_section();
12745 }
12746
12747 int MDCache::dump_cache(std::string_view file_name, double timeout)
12748 {
12749 return dump_cache(file_name, NULL, timeout);
12750 }
12751
12752 int MDCache::dump_cache(Formatter *f, double timeout)
12753 {
12754 return dump_cache(std::string_view(""), f, timeout);
12755 }
12756
12757 /**
12758 * Dump the metadata cache, either to a Formatter, if
12759 * provided, else to a plain text file.
12760 */
12761 int MDCache::dump_cache(std::string_view fn, Formatter *f, double timeout)
12762 {
12763 int r = 0;
12764
12765 // dumping large caches may cause mds to hang or worse get killed.
12766 // so, disallow the dump if the cache size exceeds the configured
12767 // threshold, which is 1G for formatter and unlimited for file (note
12768 // that this can be jacked up by the admin... and is nothing but foot
12769 // shooting, but the option itself is for devs and hence dangerous to
12770 // tune). TODO: remove this when fixed.
12771 uint64_t threshold = f ?
12772 g_conf().get_val<Option::size_t>("mds_dump_cache_threshold_formatter") :
12773 g_conf().get_val<Option::size_t>("mds_dump_cache_threshold_file");
12774
12775 if (threshold && cache_size() > threshold) {
12776 if (f) {
12777 CachedStackStringStream css;
12778 *css << "cache usage exceeds dump threshold";
12779 f->open_object_section("result");
12780 f->dump_string("error", css->strv());
12781 f->close_section();
12782 } else {
12783 derr << "cache usage exceeds dump threshold" << dendl;
12784 r = -CEPHFS_EINVAL;
12785 }
12786 return r;
12787 }
12788
12789 r = 0;
12790 int fd = -1;
12791
12792 if (f) {
12793 f->open_array_section("inodes");
12794 } else {
12795 char path[PATH_MAX] = "";
12796 if (fn.length()) {
12797 snprintf(path, sizeof path, "%s", fn.data());
12798 } else {
12799 snprintf(path, sizeof path, "cachedump.%d.mds%d", (int)mds->mdsmap->get_epoch(), int(mds->get_nodeid()));
12800 }
12801
12802 dout(1) << "dump_cache to " << path << dendl;
12803
12804 fd = ::open(path, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0600);
12805 if (fd < 0) {
12806 derr << "failed to open " << path << ": " << cpp_strerror(errno) << dendl;
12807 return errno;
12808 }
12809 }
12810
12811 auto dump_func = [fd, f](CInode *in) {
12812 int r;
12813 if (f) {
12814 f->open_object_section("inode");
12815 in->dump(f, CInode::DUMP_DEFAULT | CInode::DUMP_DIRFRAGS);
12816 f->close_section();
12817 return 1;
12818 }
12819 CachedStackStringStream css;
12820 *css << *in << std::endl;
12821 auto sv = css->strv();
12822 r = safe_write(fd, sv.data(), sv.size());
12823 if (r < 0)
12824 return r;
12825 auto&& dfs = in->get_dirfrags();
12826 for (auto &dir : dfs) {
12827 CachedStackStringStream css2;
12828 *css2 << " " << *dir << std::endl;
12829 auto sv = css2->strv();
12830 r = safe_write(fd, sv.data(), sv.size());
12831 if (r < 0)
12832 return r;
12833 for (auto &p : dir->items) {
12834 CDentry *dn = p.second;
12835 CachedStackStringStream css3;
12836 *css3 << " " << *dn << std::endl;
12837 auto sv = css3->strv();
12838 r = safe_write(fd, sv.data(), sv.size());
12839 if (r < 0)
12840 return r;
12841 }
12842 dir->check_rstats();
12843 }
12844 return 1;
12845 };
12846
12847 auto start = mono_clock::now();
12848 int64_t count = 0;
12849 for (auto &p : inode_map) {
12850 r = dump_func(p.second);
12851 if (r < 0)
12852 goto out;
12853 if (!(++count % 1000) &&
12854 timeout > 0 &&
12855 std::chrono::duration<double>(mono_clock::now() - start).count() > timeout) {
12856 r = -ETIMEDOUT;
12857 goto out;
12858 }
12859 }
12860 for (auto &p : snap_inode_map) {
12861 r = dump_func(p.second);
12862 if (r < 0)
12863 goto out;
12864 if (!(++count % 1000) &&
12865 timeout > 0 &&
12866 std::chrono::duration<double>(mono_clock::now() - start).count() > timeout) {
12867 r = -ETIMEDOUT;
12868 goto out;
12869 }
12870
12871 }
12872 r = 0;
12873
12874 out:
12875 if (f) {
12876 if (r == -ETIMEDOUT)
12877 {
12878 f->close_section();
12879 f->open_object_section("result");
12880 f->dump_string("error", "the operation timeout");
12881 }
12882 f->close_section(); // inodes
12883 } else {
12884 if (r == -ETIMEDOUT)
12885 {
12886 CachedStackStringStream css;
12887 *css << "error : the operation timeout" << std::endl;
12888 auto sv = css->strv();
12889 r = safe_write(fd, sv.data(), sv.size());
12890 }
12891 ::close(fd);
12892 }
12893 return r;
12894 }
12895
12896 void C_MDS_RetryRequest::finish(int r)
12897 {
12898 mdr->retry++;
12899 cache->dispatch_request(mdr);
12900 }
12901
12902 MDSContext *CF_MDS_RetryRequestFactory::build()
12903 {
12904 if (drop_locks) {
12905 mdcache->mds->locker->drop_locks(mdr.get(), nullptr);
12906 mdr->drop_local_auth_pins();
12907 }
12908 return new C_MDS_RetryRequest(mdcache, mdr);
12909 }
12910
12911 class C_MDS_EnqueueScrub : public Context
12912 {
12913 std::string tag;
12914 Formatter *formatter;
12915 Context *on_finish;
12916 public:
12917 ScrubHeaderRef header;
12918 C_MDS_EnqueueScrub(std::string_view tag, Formatter *f, Context *fin) :
12919 tag(tag), formatter(f), on_finish(fin), header(nullptr) {}
12920
12921 void finish(int r) override {
12922 formatter->open_object_section("results");
12923 formatter->dump_int("return_code", r);
12924 if (r == 0) {
12925 formatter->dump_string("scrub_tag", tag);
12926 formatter->dump_string("mode", "asynchronous");
12927 }
12928 formatter->close_section();
12929
12930 r = 0;
12931 if (on_finish)
12932 on_finish->complete(r);
12933 }
12934 };
12935
12936 void MDCache::enqueue_scrub(
12937 std::string_view path,
12938 std::string_view tag,
12939 bool force, bool recursive, bool repair,
12940 bool scrub_mdsdir, Formatter *f, Context *fin)
12941 {
12942 dout(10) << __func__ << " " << path << dendl;
12943
12944 filepath fp;
12945 if (path.compare(0, 4, "~mds") == 0) {
12946 mds_rank_t rank;
12947 if (path == "~mdsdir") {
12948 rank = mds->get_nodeid();
12949 } else {
12950 std::string err;
12951 rank = strict_strtoll(path.substr(4), 10, &err);
12952 if (!err.empty())
12953 rank = MDS_RANK_NONE;
12954 }
12955 if (rank >= 0 && rank < MAX_MDS)
12956 fp.set_path("", MDS_INO_MDSDIR(rank));
12957 }
12958 if (fp.get_ino() == inodeno_t(0))
12959 fp.set_path(path);
12960
12961 MDRequestRef mdr = request_start_internal(CEPH_MDS_OP_ENQUEUE_SCRUB);
12962 mdr->set_filepath(fp);
12963
12964 bool is_internal = false;
12965 std::string tag_str(tag);
12966 if (tag_str.empty()) {
12967 uuid_d uuid_gen;
12968 uuid_gen.generate_random();
12969 tag_str = uuid_gen.to_string();
12970 is_internal = true;
12971 }
12972
12973 C_MDS_EnqueueScrub *cs = new C_MDS_EnqueueScrub(tag_str, f, fin);
12974 cs->header = std::make_shared<ScrubHeader>(tag_str, is_internal, force,
12975 recursive, repair, scrub_mdsdir);
12976
12977 mdr->internal_op_finish = cs;
12978 enqueue_scrub_work(mdr);
12979 }
12980
12981 void MDCache::enqueue_scrub_work(MDRequestRef& mdr)
12982 {
12983 CInode *in;
12984 CF_MDS_RetryRequestFactory cf(this, mdr, true);
12985 int r = path_traverse(mdr, cf, mdr->get_filepath(),
12986 MDS_TRAVERSE_DISCOVER | MDS_TRAVERSE_RDLOCK_PATH,
12987 nullptr, &in);
12988 if (r > 0)
12989 return;
12990 if (r < 0) {
12991 mds->server->respond_to_request(mdr, r);
12992 return;
12993 }
12994
12995 // Cannot scrub same dentry twice at same time
12996 if (in->scrub_is_in_progress()) {
12997 mds->server->respond_to_request(mdr, -CEPHFS_EBUSY);
12998 return;
12999 } else {
13000 in->scrub_info();
13001 }
13002
13003 C_MDS_EnqueueScrub *cs = static_cast<C_MDS_EnqueueScrub*>(mdr->internal_op_finish);
13004 ScrubHeaderRef& header = cs->header;
13005
13006 r = mds->scrubstack->enqueue(in, header, !header->get_recursive());
13007
13008 mds->server->respond_to_request(mdr, r);
13009 }
13010
13011 struct C_MDC_RespondInternalRequest : public MDCacheLogContext {
13012 MDRequestRef mdr;
13013 C_MDC_RespondInternalRequest(MDCache *c, MDRequestRef& m) :
13014 MDCacheLogContext(c), mdr(m) {}
13015 void finish(int r) override {
13016 mdr->apply();
13017 get_mds()->server->respond_to_request(mdr, r);
13018 }
13019 };
13020
13021 struct C_MDC_ScrubRepaired : public MDCacheContext {
13022 ScrubHeaderRef header;
13023 public:
13024 C_MDC_ScrubRepaired(MDCache *m, const ScrubHeaderRef& h)
13025 : MDCacheContext(m), header(h) {
13026 header->inc_num_pending();
13027 }
13028 void finish(int r) override {
13029 header->dec_num_pending();
13030 }
13031 };
13032
13033 void MDCache::repair_dirfrag_stats(CDir *dir)
13034 {
13035 MDRequestRef mdr = request_start_internal(CEPH_MDS_OP_REPAIR_FRAGSTATS);
13036 mdr->pin(dir);
13037 mdr->internal_op_private = dir;
13038 if (dir->scrub_is_in_progress())
13039 mdr->internal_op_finish = new C_MDC_ScrubRepaired(this, dir->get_scrub_header());
13040 else
13041 mdr->internal_op_finish = new C_MDSInternalNoop;
13042 repair_dirfrag_stats_work(mdr);
13043 }
13044
13045 void MDCache::repair_dirfrag_stats_work(MDRequestRef& mdr)
13046 {
13047 CDir *dir = static_cast<CDir*>(mdr->internal_op_private);
13048 dout(10) << __func__ << " " << *dir << dendl;
13049
13050 if (!dir->is_auth()) {
13051 mds->server->respond_to_request(mdr, -CEPHFS_ESTALE);
13052 return;
13053 }
13054
13055 if (!mdr->is_auth_pinned(dir) && !dir->can_auth_pin()) {
13056 dir->add_waiter(CDir::WAIT_UNFREEZE, new C_MDS_RetryRequest(this, mdr));
13057
13058 mds->locker->drop_locks(mdr.get());
13059 mdr->drop_local_auth_pins();
13060 if (mdr->is_any_remote_auth_pin())
13061 mds->locker->notify_freeze_waiter(dir);
13062 return;
13063 }
13064
13065 mdr->auth_pin(dir);
13066
13067 MutationImpl::LockOpVec lov;
13068 CInode *diri = dir->inode;
13069 lov.add_rdlock(&diri->dirfragtreelock);
13070 lov.add_wrlock(&diri->nestlock);
13071 lov.add_wrlock(&diri->filelock);
13072 if (!mds->locker->acquire_locks(mdr, lov))
13073 return;
13074
13075 if (!dir->is_complete()) {
13076 dir->fetch(new C_MDS_RetryRequest(this, mdr));
13077 return;
13078 }
13079
13080 frag_info_t frag_info;
13081 nest_info_t nest_info;
13082 for (auto it = dir->begin(); it != dir->end(); ++it) {
13083 CDentry *dn = it->second;
13084 if (dn->last != CEPH_NOSNAP)
13085 continue;
13086 CDentry::linkage_t *dnl = dn->get_projected_linkage();
13087 if (dnl->is_primary()) {
13088 CInode *in = dnl->get_inode();
13089 nest_info.add(in->get_projected_inode()->accounted_rstat);
13090 if (in->is_dir())
13091 frag_info.nsubdirs++;
13092 else
13093 frag_info.nfiles++;
13094 } else if (dnl->is_remote())
13095 frag_info.nfiles++;
13096 }
13097
13098 auto pf = dir->get_projected_fnode();
13099 bool good_fragstat = frag_info.same_sums(pf->fragstat);
13100 bool good_rstat = nest_info.same_sums(pf->rstat);
13101 if (good_fragstat && good_rstat) {
13102 dout(10) << __func__ << " no corruption found" << dendl;
13103 mds->server->respond_to_request(mdr, 0);
13104 return;
13105 }
13106
13107 auto _pf = dir->project_fnode(mdr);
13108 _pf->version = dir->pre_dirty();
13109 pf = _pf;
13110
13111 mdr->ls = mds->mdlog->get_current_segment();
13112 EUpdate *le = new EUpdate(mds->mdlog, "repair_dirfrag");
13113 mds->mdlog->start_entry(le);
13114
13115 if (!good_fragstat) {
13116 if (pf->fragstat.mtime > frag_info.mtime)
13117 frag_info.mtime = pf->fragstat.mtime;
13118 if (pf->fragstat.change_attr > frag_info.change_attr)
13119 frag_info.change_attr = pf->fragstat.change_attr;
13120 _pf->fragstat = frag_info;
13121 mds->locker->mark_updated_scatterlock(&diri->filelock);
13122 mdr->ls->dirty_dirfrag_dir.push_back(&diri->item_dirty_dirfrag_dir);
13123 mdr->add_updated_lock(&diri->filelock);
13124 }
13125
13126 if (!good_rstat) {
13127 if (pf->rstat.rctime > nest_info.rctime)
13128 nest_info.rctime = pf->rstat.rctime;
13129 _pf->rstat = nest_info;
13130 mds->locker->mark_updated_scatterlock(&diri->nestlock);
13131 mdr->ls->dirty_dirfrag_nest.push_back(&diri->item_dirty_dirfrag_nest);
13132 mdr->add_updated_lock(&diri->nestlock);
13133 }
13134
13135 le->metablob.add_dir_context(dir);
13136 le->metablob.add_dir(dir, true);
13137
13138 mds->mdlog->submit_entry(le, new C_MDC_RespondInternalRequest(this, mdr));
13139 }
13140
13141 void MDCache::repair_inode_stats(CInode *diri)
13142 {
13143 MDRequestRef mdr = request_start_internal(CEPH_MDS_OP_REPAIR_INODESTATS);
13144 mdr->auth_pin(diri); // already auth pinned by CInode::validate_disk_state()
13145 mdr->internal_op_private = diri;
13146 if (diri->scrub_is_in_progress())
13147 mdr->internal_op_finish = new C_MDC_ScrubRepaired(this, diri->get_scrub_header());
13148 else
13149 mdr->internal_op_finish = new C_MDSInternalNoop;
13150 repair_inode_stats_work(mdr);
13151 }
13152
13153 void MDCache::repair_inode_stats_work(MDRequestRef& mdr)
13154 {
13155 CInode *diri = static_cast<CInode*>(mdr->internal_op_private);
13156 dout(10) << __func__ << " " << *diri << dendl;
13157
13158 if (!diri->is_auth()) {
13159 mds->server->respond_to_request(mdr, -CEPHFS_ESTALE);
13160 return;
13161 }
13162 if (!diri->is_dir()) {
13163 mds->server->respond_to_request(mdr, -CEPHFS_ENOTDIR);
13164 return;
13165 }
13166
13167 MutationImpl::LockOpVec lov;
13168
13169 if (mdr->ls) // already marked filelock/nestlock dirty ?
13170 goto do_rdlocks;
13171
13172 lov.add_rdlock(&diri->dirfragtreelock);
13173 lov.add_wrlock(&diri->nestlock);
13174 lov.add_wrlock(&diri->filelock);
13175 if (!mds->locker->acquire_locks(mdr, lov))
13176 return;
13177
13178 // Fetch all dirfrags and mark filelock/nestlock dirty. This will tirgger
13179 // the scatter-gather process, which will fix any fragstat/rstat errors.
13180 {
13181 frag_vec_t leaves;
13182 diri->dirfragtree.get_leaves(leaves);
13183 for (const auto& leaf : leaves) {
13184 CDir *dir = diri->get_dirfrag(leaf);
13185 if (!dir) {
13186 ceph_assert(mdr->is_auth_pinned(diri));
13187 dir = diri->get_or_open_dirfrag(this, leaf);
13188 }
13189 if (dir->get_version() == 0) {
13190 ceph_assert(dir->is_auth());
13191 dir->fetch_keys({}, new C_MDS_RetryRequest(this, mdr));
13192 return;
13193 }
13194 }
13195 }
13196
13197 diri->state_set(CInode::STATE_REPAIRSTATS);
13198 mdr->ls = mds->mdlog->get_current_segment();
13199 mds->locker->mark_updated_scatterlock(&diri->filelock);
13200 mdr->ls->dirty_dirfrag_dir.push_back(&diri->item_dirty_dirfrag_dir);
13201 mds->locker->mark_updated_scatterlock(&diri->nestlock);
13202 mdr->ls->dirty_dirfrag_nest.push_back(&diri->item_dirty_dirfrag_nest);
13203
13204 mds->locker->drop_locks(mdr.get());
13205
13206 do_rdlocks:
13207 // force the scatter-gather process
13208 lov.clear();
13209 lov.add_rdlock(&diri->dirfragtreelock);
13210 lov.add_rdlock(&diri->nestlock);
13211 lov.add_rdlock(&diri->filelock);
13212 if (!mds->locker->acquire_locks(mdr, lov))
13213 return;
13214
13215 diri->state_clear(CInode::STATE_REPAIRSTATS);
13216
13217 frag_info_t dir_info;
13218 nest_info_t nest_info;
13219 nest_info.rsubdirs = 1; // it gets one to account for self
13220 if (const sr_t *srnode = diri->get_projected_srnode(); srnode)
13221 nest_info.rsnaps = srnode->snaps.size();
13222
13223 {
13224 frag_vec_t leaves;
13225 diri->dirfragtree.get_leaves(leaves);
13226 for (const auto& leaf : leaves) {
13227 CDir *dir = diri->get_dirfrag(leaf);
13228 ceph_assert(dir);
13229 ceph_assert(dir->get_version() > 0);
13230 dir_info.add(dir->get_fnode()->accounted_fragstat);
13231 nest_info.add(dir->get_fnode()->accounted_rstat);
13232 }
13233 }
13234
13235 if (!dir_info.same_sums(diri->get_inode()->dirstat) ||
13236 !nest_info.same_sums(diri->get_inode()->rstat)) {
13237 dout(10) << __func__ << " failed to fix fragstat/rstat on "
13238 << *diri << dendl;
13239 }
13240
13241 mds->server->respond_to_request(mdr, 0);
13242 }
13243
13244 void MDCache::rdlock_dirfrags_stats(CInode *diri, MDSInternalContext* fin)
13245 {
13246 MDRequestRef mdr = request_start_internal(CEPH_MDS_OP_RDLOCK_FRAGSSTATS);
13247 mdr->auth_pin(diri); // already auth pinned by CInode::validate_disk_state()
13248 mdr->internal_op_private = diri;
13249 mdr->internal_op_finish = fin;
13250 return rdlock_dirfrags_stats_work(mdr);
13251 }
13252
13253 void MDCache::rdlock_dirfrags_stats_work(MDRequestRef& mdr)
13254 {
13255 CInode *diri = static_cast<CInode*>(mdr->internal_op_private);
13256 dout(10) << __func__ << " " << *diri << dendl;
13257 if (!diri->is_auth()) {
13258 mds->server->respond_to_request(mdr, -CEPHFS_ESTALE);
13259 return;
13260 }
13261 if (!diri->is_dir()) {
13262 mds->server->respond_to_request(mdr, -CEPHFS_ENOTDIR);
13263 return;
13264 }
13265
13266 MutationImpl::LockOpVec lov;
13267 lov.add_rdlock(&diri->dirfragtreelock);
13268 lov.add_rdlock(&diri->nestlock);
13269 lov.add_rdlock(&diri->filelock);
13270 if (!mds->locker->acquire_locks(mdr, lov))
13271 return;
13272 dout(10) << __func__ << " start dirfrags : " << *diri << dendl;
13273
13274 mds->server->respond_to_request(mdr, 0);
13275 return;
13276 }
13277
13278 void MDCache::flush_dentry(std::string_view path, Context *fin)
13279 {
13280 if (is_readonly()) {
13281 dout(10) << __func__ << ": read-only FS" << dendl;
13282 fin->complete(-CEPHFS_EROFS);
13283 return;
13284 }
13285 dout(10) << "flush_dentry " << path << dendl;
13286 MDRequestRef mdr = request_start_internal(CEPH_MDS_OP_FLUSH);
13287 filepath fp(path);
13288 mdr->set_filepath(fp);
13289 mdr->internal_op_finish = fin;
13290 flush_dentry_work(mdr);
13291 }
13292
13293 class C_FinishIOMDR : public MDSContext {
13294 protected:
13295 MDSRank *mds;
13296 MDRequestRef mdr;
13297 MDSRank *get_mds() override { return mds; }
13298 public:
13299 C_FinishIOMDR(MDSRank *mds_, MDRequestRef& mdr_) : mds(mds_), mdr(mdr_) {}
13300 void finish(int r) override { mds->server->respond_to_request(mdr, r); }
13301 };
13302
13303 void MDCache::flush_dentry_work(MDRequestRef& mdr)
13304 {
13305 MutationImpl::LockOpVec lov;
13306 CInode *in = mds->server->rdlock_path_pin_ref(mdr, true);
13307 if (!in)
13308 return;
13309
13310 ceph_assert(in->is_auth());
13311 in->flush(new C_FinishIOMDR(mds, mdr));
13312 }
13313
13314
13315 /**
13316 * Initialize performance counters with global perfcounter
13317 * collection.
13318 */
13319 void MDCache::register_perfcounters()
13320 {
13321 PerfCountersBuilder pcb(g_ceph_context, "mds_cache", l_mdc_first, l_mdc_last);
13322
13323 pcb.add_u64_counter(l_mdc_dir_update, "dir_update",
13324 "Directory replication directives");
13325 pcb.add_u64_counter(l_mdc_dir_update_receipt, "dir_update_receipt",
13326 "Directory replication directives received");
13327 pcb.add_u64_counter(l_mdc_dir_try_discover, "dir_try_discover",
13328 "Directory replication attempt to discover");
13329 pcb.add_u64_counter(l_mdc_dir_send_discover, "dir_send_discover",
13330 "Directory replication discovery message sent");
13331 pcb.add_u64_counter(l_mdc_dir_handle_discover, "dir_handle_discover",
13332 "Directory replication discovery message handled");
13333
13334 // Stray/purge statistics
13335 pcb.add_u64(l_mdc_num_strays, "num_strays", "Stray dentries", "stry",
13336 PerfCountersBuilder::PRIO_INTERESTING);
13337 pcb.add_u64(l_mdc_num_recovering_enqueued,
13338 "num_recovering_enqueued", "Files waiting for recovery", "recy",
13339 PerfCountersBuilder::PRIO_INTERESTING);
13340 pcb.add_u64_counter(l_mdc_recovery_completed,
13341 "recovery_completed", "File recoveries completed", "recd",
13342 PerfCountersBuilder::PRIO_INTERESTING);
13343
13344 // useful recovery queue statistics
13345 pcb.set_prio_default(PerfCountersBuilder::PRIO_USEFUL);
13346 pcb.add_u64(l_mdc_num_recovering_processing, "num_recovering_processing",
13347 "Files currently being recovered");
13348 pcb.add_u64(l_mdc_num_recovering_prioritized, "num_recovering_prioritized",
13349 "Files waiting for recovery with elevated priority");
13350 pcb.add_u64_counter(l_mdc_recovery_started, "recovery_started",
13351 "File recoveries started");
13352
13353 // along with other stray dentries stats
13354 pcb.add_u64(l_mdc_num_strays_delayed, "num_strays_delayed",
13355 "Stray dentries delayed");
13356 pcb.add_u64(l_mdc_num_strays_enqueuing, "num_strays_enqueuing",
13357 "Stray dentries enqueuing for purge");
13358 pcb.add_u64_counter(l_mdc_strays_created, "strays_created",
13359 "Stray dentries created");
13360 pcb.add_u64_counter(l_mdc_strays_enqueued, "strays_enqueued",
13361 "Stray dentries enqueued for purge");
13362 pcb.add_u64_counter(l_mdc_strays_reintegrated, "strays_reintegrated",
13363 "Stray dentries reintegrated");
13364 pcb.add_u64_counter(l_mdc_strays_migrated, "strays_migrated",
13365 "Stray dentries migrated");
13366
13367 // low prio internal request stats
13368 pcb.add_u64_counter(l_mdss_ireq_enqueue_scrub, "ireq_enqueue_scrub",
13369 "Internal Request type enqueue scrub");
13370 pcb.add_u64_counter(l_mdss_ireq_exportdir, "ireq_exportdir",
13371 "Internal Request type export dir");
13372 pcb.add_u64_counter(l_mdss_ireq_flush, "ireq_flush",
13373 "Internal Request type flush");
13374 pcb.add_u64_counter(l_mdss_ireq_fragmentdir, "ireq_fragmentdir",
13375 "Internal Request type fragmentdir");
13376 pcb.add_u64_counter(l_mdss_ireq_fragstats, "ireq_fragstats",
13377 "Internal Request type frag stats");
13378 pcb.add_u64_counter(l_mdss_ireq_inodestats, "ireq_inodestats",
13379 "Internal Request type inode stats");
13380
13381 logger.reset(pcb.create_perf_counters());
13382 g_ceph_context->get_perfcounters_collection()->add(logger.get());
13383 recovery_queue.set_logger(logger.get());
13384 stray_manager.set_logger(logger.get());
13385 }
13386
13387 /**
13388 * Call this when putting references to an inode/dentry or
13389 * when attempting to trim it.
13390 *
13391 * If this inode is no longer linked by anyone, and this MDS
13392 * rank holds the primary dentry, and that dentry is in a stray
13393 * directory, then give up the dentry to the StrayManager, never
13394 * to be seen again by MDCache.
13395 *
13396 * @param delay if true, then purgeable inodes are stashed til
13397 * the next trim(), rather than being purged right
13398 * away.
13399 */
13400 void MDCache::maybe_eval_stray(CInode *in, bool delay) {
13401 if (in->get_inode()->nlink > 0 || in->is_base() || is_readonly() ||
13402 mds->get_state() <= MDSMap::STATE_REJOIN)
13403 return;
13404
13405 CDentry *dn = in->get_projected_parent_dn();
13406
13407 if (dn->state_test(CDentry::STATE_PURGING)) {
13408 /* We have already entered the purging process, no need
13409 * to re-evaluate me ! */
13410 return;
13411 }
13412
13413 if (dn->get_dir()->get_inode()->is_stray()) {
13414 if (delay)
13415 stray_manager.queue_delayed(dn);
13416 else
13417 stray_manager.eval_stray(dn);
13418 }
13419 }
13420
13421 void MDCache::clear_dirty_bits_for_stray(CInode* diri) {
13422 dout(10) << __func__ << " " << *diri << dendl;
13423 ceph_assert(diri->get_projected_parent_dir()->inode->is_stray());
13424 auto&& ls = diri->get_dirfrags();
13425 for (auto &p : ls) {
13426 if (p->is_auth() && !(p->is_frozen() || p->is_freezing()))
13427 p->try_remove_dentries_for_stray();
13428 }
13429 if (!diri->snaprealm) {
13430 if (diri->is_auth())
13431 diri->clear_dirty_rstat();
13432 diri->clear_scatter_dirty();
13433 }
13434 }
13435
13436 bool MDCache::dump_inode(Formatter *f, uint64_t number) {
13437 CInode *in = get_inode(number);
13438 if (!in) {
13439 return false;
13440 }
13441 f->open_object_section("inode");
13442 in->dump(f, CInode::DUMP_DEFAULT | CInode::DUMP_PATH);
13443 f->close_section();
13444 return true;
13445 }
13446
13447 void MDCache::handle_mdsmap(const MDSMap &mdsmap, const MDSMap &oldmap) {
13448 const mds_rank_t max_mds = mdsmap.get_max_mds();
13449
13450 // process export_pin_delayed_queue whenever a new MDSMap received
13451 auto &q = export_pin_delayed_queue;
13452 for (auto it = q.begin(); it != q.end(); ) {
13453 auto *in = *it;
13454 mds_rank_t export_pin = in->get_export_pin(false);
13455 dout(10) << " delayed export_pin=" << export_pin << " on " << *in
13456 << " max_mds=" << max_mds << dendl;
13457 if (export_pin >= mdsmap.get_max_mds()) {
13458 it++;
13459 continue;
13460 }
13461
13462 in->state_clear(CInode::STATE_DELAYEDEXPORTPIN);
13463 it = q.erase(it);
13464 in->queue_export_pin(export_pin);
13465 }
13466
13467 if (mdsmap.get_max_mds() != oldmap.get_max_mds()) {
13468 dout(10) << "Checking ephemerally pinned directories for redistribute due to max_mds change." << dendl;
13469 /* copy to vector to avoid removals during iteration */
13470 std::vector<CInode*> migrate;
13471 migrate.assign(export_ephemeral_pins.begin(), export_ephemeral_pins.end());
13472 for (auto& in : migrate) {
13473 in->maybe_export_pin();
13474 }
13475 }
13476
13477 if (max_mds <= 1) {
13478 export_ephemeral_dist_frag_bits = 0;
13479 } else {
13480 double want = g_conf().get_val<double>("mds_export_ephemeral_distributed_factor");
13481 want *= max_mds;
13482 unsigned n = 0;
13483 while ((1U << n) < (unsigned)want)
13484 ++n;
13485 export_ephemeral_dist_frag_bits = n;
13486 }
13487 }
13488
13489 void MDCache::upkeep_main(void)
13490 {
13491 std::unique_lock lock(upkeep_mutex);
13492 while (!upkeep_trim_shutdown.load()) {
13493 auto now = clock::now();
13494 auto since = now-upkeep_last_trim;
13495 auto trim_interval = clock::duration(g_conf().get_val<std::chrono::seconds>("mds_cache_trim_interval"));
13496 if (since >= trim_interval*.90) {
13497 lock.unlock(); /* mds_lock -> upkeep_mutex */
13498 std::scoped_lock mds_lock(mds->mds_lock);
13499 lock.lock();
13500 if (upkeep_trim_shutdown.load())
13501 return;
13502 check_memory_usage();
13503 if (mds->is_cache_trimmable()) {
13504 dout(20) << "upkeep thread trimming cache; last trim " << since << " ago" << dendl;
13505 bool active_with_clients = mds->is_active() || mds->is_clientreplay() || mds->is_stopping();
13506 if (active_with_clients) {
13507 trim_client_leases();
13508 }
13509 if (is_open()) {
13510 trim();
13511 }
13512 if (active_with_clients) {
13513 auto recall_flags = Server::RecallFlags::ENFORCE_MAX|Server::RecallFlags::ENFORCE_LIVENESS;
13514 if (cache_toofull()) {
13515 recall_flags = recall_flags|Server::RecallFlags::TRIM;
13516 }
13517 mds->server->recall_client_state(nullptr, recall_flags);
13518 }
13519 upkeep_last_trim = now = clock::now();
13520 } else {
13521 dout(10) << "cache not ready for trimming" << dendl;
13522 }
13523 } else {
13524 trim_interval -= since;
13525 }
13526 since = now-upkeep_last_release;
13527 auto release_interval = clock::duration(g_conf().get_val<std::chrono::seconds>("mds_cache_release_free_interval"));
13528 if (since >= release_interval*.90) {
13529 /* XXX not necessary once MDCache uses PriorityCache */
13530 dout(10) << "releasing free memory" << dendl;
13531 ceph_heap_release_free_memory();
13532 upkeep_last_release = clock::now();
13533 } else {
13534 release_interval -= since;
13535 }
13536 auto interval = std::min(release_interval, trim_interval);
13537 dout(20) << "upkeep thread waiting interval " << interval << dendl;
13538 upkeep_cvar.wait_for(lock, interval);
13539 }
13540 }