]> git.proxmox.com Git - ceph.git/blame - ceph/src/mds/MDCache.h
import ceph 14.2.5
[ceph.git] / ceph / src / mds / MDCache.h
CommitLineData
11fdf7f2 1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
7c673cae
FG
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
16
17#ifndef CEPH_MDCACHE_H
18#define CEPH_MDCACHE_H
19
eafe8130 20#include <atomic>
11fdf7f2 21#include <string_view>
eafe8130 22#include <thread>
94b18763 23
a8e16298 24#include "common/DecayCounter.h"
7c673cae
FG
25#include "include/types.h"
26#include "include/filepath.h"
27#include "include/elist.h"
28
11fdf7f2
TL
29#include "messages/MCacheExpire.h"
30#include "messages/MClientQuota.h"
31#include "messages/MClientRequest.h"
32#include "messages/MClientSnap.h"
33#include "messages/MDentryLink.h"
34#include "messages/MDentryUnlink.h"
35#include "messages/MDirUpdate.h"
36#include "messages/MDiscover.h"
37#include "messages/MDiscoverReply.h"
38#include "messages/MGatherCaps.h"
39#include "messages/MGenericMessage.h"
40#include "messages/MInodeFileCaps.h"
41#include "messages/MLock.h"
42#include "messages/MMDSCacheRejoin.h"
43#include "messages/MMDSFindIno.h"
44#include "messages/MMDSFindInoReply.h"
45#include "messages/MMDSFragmentNotify.h"
46#include "messages/MMDSFragmentNotifyAck.h"
47#include "messages/MMDSOpenIno.h"
48#include "messages/MMDSOpenInoReply.h"
49#include "messages/MMDSResolve.h"
50#include "messages/MMDSResolveAck.h"
51#include "messages/MMDSSlaveRequest.h"
52#include "messages/MMDSSnapUpdate.h"
53
54
7c673cae
FG
55#include "osdc/Filer.h"
56#include "CInode.h"
57#include "CDentry.h"
58#include "CDir.h"
59#include "include/Context.h"
60#include "events/EMetaBlob.h"
61#include "RecoveryQueue.h"
62#include "StrayManager.h"
11fdf7f2 63#include "OpenFileTable.h"
7c673cae
FG
64#include "MDSContext.h"
65#include "MDSMap.h"
66#include "Mutation.h"
67
7c673cae
FG
68
69class PerfCounters;
70
71class MDSRank;
72class Session;
73class Migrator;
74
7c673cae
FG
75class Session;
76
7c673cae
FG
77class ESubtreeMap;
78
79enum {
80 l_mdc_first = 3000,
81 // How many inodes currently in stray dentries
82 l_mdc_num_strays,
83 // How many stray dentries are currently delayed for purge due to refs
84 l_mdc_num_strays_delayed,
85 // How many stray dentries are currently being enqueued for purge
86 l_mdc_num_strays_enqueuing,
87
88 // How many dentries have ever been added to stray dir
89 l_mdc_strays_created,
90 // How many dentries have been passed on to PurgeQueue
91 l_mdc_strays_enqueued,
92 // How many strays have been reintegrated?
93 l_mdc_strays_reintegrated,
94 // How many strays have been migrated?
95 l_mdc_strays_migrated,
96
97 // How many inode sizes currently being recovered
98 l_mdc_num_recovering_processing,
99 // How many inodes currently waiting to have size recovered
100 l_mdc_num_recovering_enqueued,
101 // How many inodes waiting with elevated priority for recovery
102 l_mdc_num_recovering_prioritized,
103 // How many inodes ever started size recovery
104 l_mdc_recovery_started,
105 // How many inodes ever completed size recovery
106 l_mdc_recovery_completed,
107
d2e6a577
FG
108 l_mdss_ireq_enqueue_scrub,
109 l_mdss_ireq_exportdir,
110 l_mdss_ireq_flush,
111 l_mdss_ireq_fragmentdir,
112 l_mdss_ireq_fragstats,
113 l_mdss_ireq_inodestats,
114
7c673cae
FG
115 l_mdc_last,
116};
117
118
119// flags for predirty_journal_parents()
120static const int PREDIRTY_PRIMARY = 1; // primary dn, adjust nested accounting
121static const int PREDIRTY_DIR = 2; // update parent dir mtime/size
122static const int PREDIRTY_SHALLOW = 4; // only go to immediate parent (for easier rollback)
123
124class MDCache {
125 public:
91327a77
AA
126 using clock = ceph::coarse_mono_clock;
127 using time = ceph::coarse_mono_time;
128
11fdf7f2
TL
129 typedef std::map<mds_rank_t, MCacheExpire::ref> expiremap;
130
7c673cae
FG
131 // my master
132 MDSRank *mds;
133
134 // -- my cache --
135 LRU lru; // dentry lru for expiring items from cache
31f18b77 136 LRU bottom_lru; // dentries that should be trimmed ASAP
7c673cae 137 protected:
b32b8144
FG
138 ceph::unordered_map<inodeno_t,CInode*> inode_map; // map of head inodes by ino
139 map<vinodeno_t, CInode*> snap_inode_map; // map of snap inodes by ino
eafe8130
TL
140 CInode *root = nullptr; // root inode
141 CInode *myin = nullptr; // .ceph/mds%d dir
7c673cae 142
eafe8130 143 bool readonly = false;
7c673cae
FG
144 void set_readonly() { readonly = true; }
145
eafe8130
TL
146 std::array<CInode *, NUM_STRAY> strays{}; // my stray dir
147 int stray_index = 0;
7c673cae
FG
148
149 CInode *get_stray() {
150 return strays[stray_index];
151 }
152
153 set<CInode*> base_inodes;
154
155 std::unique_ptr<PerfCounters> logger;
156
157 Filer filer;
158
eafe8130 159 bool exceeded_size_limit = false;
7c673cae 160
91327a77
AA
161private:
162 uint64_t cache_inode_limit;
163 uint64_t cache_memory_limit;
164 double cache_reservation;
165 double cache_health_threshold;
166
7c673cae 167public:
91327a77
AA
168 uint64_t cache_limit_inodes(void) {
169 return cache_inode_limit;
181888fb 170 }
91327a77
AA
171 uint64_t cache_limit_memory(void) {
172 return cache_memory_limit;
181888fb
FG
173 }
174 double cache_toofull_ratio(void) const {
91327a77
AA
175 double inode_reserve = cache_inode_limit*(1.0-cache_reservation);
176 double memory_reserve = cache_memory_limit*(1.0-cache_reservation);
177 return fmax(0.0, fmax((cache_size()-memory_reserve)/memory_reserve, cache_inode_limit == 0 ? 0.0 : (CInode::count()-inode_reserve)/inode_reserve));
181888fb
FG
178 }
179 bool cache_toofull(void) const {
180 return cache_toofull_ratio() > 0.0;
181 }
182 uint64_t cache_size(void) const {
183 return mempool::get_pool(mempool::mds_co::id).allocated_bytes();
184 }
185 bool cache_overfull(void) const {
91327a77 186 return (cache_inode_limit > 0 && CInode::count() > cache_inode_limit*cache_health_threshold) || (cache_size() > cache_memory_limit*cache_health_threshold);
181888fb
FG
187 }
188
7c673cae
FG
189 void advance_stray() {
190 stray_index = (stray_index+1)%NUM_STRAY;
191 }
192
7c673cae
FG
193 /**
194 * Call this when you know that a CDentry is ready to be passed
195 * on to StrayManager (i.e. this is a stray you've just created)
196 */
197 void notify_stray(CDentry *dn) {
11fdf7f2 198 ceph_assert(dn->get_dir()->get_inode()->is_stray());
a8e16298
TL
199 if (dn->state_test(CDentry::STATE_PURGING))
200 return;
201
7c673cae
FG
202 stray_manager.eval_stray(dn);
203 }
204
205 void maybe_eval_stray(CInode *in, bool delay=false);
31f18b77
FG
206 void clear_dirty_bits_for_stray(CInode* diri);
207
7c673cae
FG
208 bool is_readonly() { return readonly; }
209 void force_readonly();
210
211 DecayRate decayrate;
212
eafe8130 213 int num_shadow_inodes = 0;
b32b8144 214
eafe8130 215 int num_inodes_with_caps = 0;
7c673cae
FG
216
217 unsigned max_dir_commit_size;
218
219 static file_layout_t gen_default_file_layout(const MDSMap &mdsmap);
220 static file_layout_t gen_default_log_layout(const MDSMap &mdsmap);
221
222 file_layout_t default_file_layout;
223 file_layout_t default_log_layout;
224
225 void register_perfcounters();
226
227 // -- client leases --
228public:
eafe8130
TL
229 static constexpr std::size_t client_lease_pools = 3;
230 std::array<float, client_lease_pools> client_lease_durations{5.0, 30.0, 300.0};
231
7c673cae 232protected:
eafe8130 233 std::array<xlist<ClientLease*>, client_lease_pools> client_leases{};
7c673cae
FG
234public:
235 void touch_client_lease(ClientLease *r, int pool, utime_t ttl) {
236 client_leases[pool].push_back(&r->item_lease);
237 r->ttl = ttl;
238 }
239
240 void notify_stray_removed()
241 {
242 stray_manager.notify_stray_removed();
243 }
244
245 void notify_stray_created()
246 {
247 stray_manager.notify_stray_created();
248 }
249
31f18b77
FG
250 void eval_remote(CDentry *dn)
251 {
252 stray_manager.eval_remote(dn);
253 }
254
7c673cae 255 // -- client caps --
eafe8130 256 uint64_t last_cap_id = 0;
7c673cae
FG
257
258 // -- discover --
259 struct discover_info_t {
260 ceph_tid_t tid;
261 mds_rank_t mds;
262 inodeno_t ino;
263 frag_t frag;
264 snapid_t snap;
265 filepath want_path;
31f18b77 266 CInode *basei;
7c673cae
FG
267 bool want_base_dir;
268 bool want_xlocked;
269
270 discover_info_t() :
31f18b77 271 tid(0), mds(-1), snap(CEPH_NOSNAP), basei(NULL),
7c673cae
FG
272 want_base_dir(false), want_xlocked(false) {}
273 ~discover_info_t() {
31f18b77
FG
274 if (basei)
275 basei->put(MDSCacheObject::PIN_DISCOVERBASE);
7c673cae 276 }
31f18b77
FG
277 void pin_base(CInode *b) {
278 basei = b;
279 basei->get(MDSCacheObject::PIN_DISCOVERBASE);
7c673cae
FG
280 }
281 };
282
283 map<ceph_tid_t, discover_info_t> discovers;
eafe8130 284 ceph_tid_t discover_last_tid = 0;
7c673cae
FG
285
286 void _send_discover(discover_info_t& dis);
287 discover_info_t& _create_discover(mds_rank_t mds) {
288 ceph_tid_t t = ++discover_last_tid;
289 discover_info_t& d = discovers[t];
290 d.tid = t;
291 d.mds = mds;
292 return d;
293 }
294
295 // waiters
11fdf7f2 296 map<int, map<inodeno_t, MDSContext::vec > > waiting_for_base_ino;
7c673cae 297
11fdf7f2
TL
298 void discover_base_ino(inodeno_t want_ino, MDSContext *onfinish, mds_rank_t from=MDS_RANK_NONE);
299 void discover_dir_frag(CInode *base, frag_t approx_fg, MDSContext *onfinish,
7c673cae 300 mds_rank_t from=MDS_RANK_NONE);
11fdf7f2 301 void discover_path(CInode *base, snapid_t snap, filepath want_path, MDSContext *onfinish,
7c673cae 302 bool want_xlocked=false, mds_rank_t from=MDS_RANK_NONE);
11fdf7f2 303 void discover_path(CDir *base, snapid_t snap, filepath want_path, MDSContext *onfinish,
7c673cae
FG
304 bool want_xlocked=false);
305 void kick_discovers(mds_rank_t who); // after a failure.
306
307
308 // -- subtrees --
81eedcae
TL
309private:
310 static const unsigned int SUBTREES_COUNT_THRESHOLD = 5;
311 static const unsigned int SUBTREES_DEPTH_THRESHOLD = 5;
7c673cae
FG
312protected:
313 /* subtree keys and each tree's non-recursive nested subtrees (the "bounds") */
314 map<CDir*,set<CDir*> > subtrees;
315 map<CInode*,list<pair<CDir*,CDir*> > > projected_subtree_renames; // renamed ino -> target dir
316
317 // adjust subtree auth specification
318 // dir->dir_auth
319 // imports/exports/nested_exports
320 // join/split subtrees as appropriate
321public:
322 bool is_subtrees() { return !subtrees.empty(); }
11fdf7f2
TL
323 template<typename T>
324 void get_subtrees(T& c) {
325 if constexpr (std::is_same_v<T, std::vector<CDir*>>)
326 c.reserve(c.size() + subtrees.size());
327 for (const auto& p : subtrees) {
328 c.push_back(p.first);
329 }
330 }
28e407b8 331 void adjust_subtree_auth(CDir *root, mds_authority_t auth, bool adjust_pop=true);
224ce89b
WB
332 void adjust_subtree_auth(CDir *root, mds_rank_t a, mds_rank_t b=CDIR_AUTH_UNKNOWN) {
333 adjust_subtree_auth(root, mds_authority_t(a,b));
7c673cae 334 }
11fdf7f2
TL
335 void adjust_bounded_subtree_auth(CDir *dir, const set<CDir*>& bounds, mds_authority_t auth);
336 void adjust_bounded_subtree_auth(CDir *dir, const set<CDir*>& bounds, mds_rank_t a) {
7c673cae
FG
337 adjust_bounded_subtree_auth(dir, bounds, mds_authority_t(a, CDIR_AUTH_UNKNOWN));
338 }
11fdf7f2
TL
339 void adjust_bounded_subtree_auth(CDir *dir, const vector<dirfrag_t>& bounds, const mds_authority_t &auth);
340 void adjust_bounded_subtree_auth(CDir *dir, const vector<dirfrag_t>& bounds, mds_rank_t a) {
7c673cae
FG
341 adjust_bounded_subtree_auth(dir, bounds, mds_authority_t(a, CDIR_AUTH_UNKNOWN));
342 }
11fdf7f2 343 void map_dirfrag_set(const list<dirfrag_t>& dfs, set<CDir*>& result);
7c673cae 344 void try_subtree_merge(CDir *root);
28e407b8 345 void try_subtree_merge_at(CDir *root, set<CInode*> *to_eval, bool adjust_pop=true);
7c673cae
FG
346 void subtree_merge_writebehind_finish(CInode *in, MutationRef& mut);
347 void eval_subtree_root(CInode *diri);
348 CDir *get_subtree_root(CDir *dir);
349 CDir *get_projected_subtree_root(CDir *dir);
350 bool is_leaf_subtree(CDir *dir) {
11fdf7f2 351 ceph_assert(subtrees.count(dir));
7c673cae
FG
352 return subtrees[dir].empty();
353 }
354 void remove_subtree(CDir *dir);
355 bool is_subtree(CDir *root) {
356 return subtrees.count(root);
357 }
358 void get_subtree_bounds(CDir *root, set<CDir*>& bounds);
359 void get_wouldbe_subtree_bounds(CDir *root, set<CDir*>& bounds);
360 void verify_subtree_bounds(CDir *root, const set<CDir*>& bounds);
361 void verify_subtree_bounds(CDir *root, const list<dirfrag_t>& bounds);
362
363 void project_subtree_rename(CInode *diri, CDir *olddir, CDir *newdir);
224ce89b 364 void adjust_subtree_after_rename(CInode *diri, CDir *olddir, bool pop);
7c673cae 365
11fdf7f2
TL
366 auto get_auth_subtrees() {
367 std::vector<CDir*> c;
368 for (auto& p : subtrees) {
369 auto& root = p.first;
370 if (root->is_auth()) {
371 c.push_back(root);
372 }
373 }
374 return c;
375 }
7c673cae 376
11fdf7f2
TL
377 auto get_fullauth_subtrees() {
378 std::vector<CDir*> c;
379 for (auto& p : subtrees) {
380 auto& root = p.first;
381 if (root->is_full_dir_auth()) {
382 c.push_back(root);
383 }
384 }
385 return c;
386 }
387 auto num_subtrees_fullauth() const {
388 std::size_t n = 0;
389 for (auto& p : subtrees) {
390 auto& root = p.first;
391 if (root->is_full_dir_auth()) {
392 ++n;
393 }
394 }
395 return n;
396 }
7c673cae 397
11fdf7f2
TL
398 auto num_subtrees_fullnonauth() const {
399 std::size_t n = 0;
400 for (auto& p : subtrees) {
401 auto& root = p.first;
402 if (root->is_full_dir_nonauth()) {
403 ++n;
404 }
405 }
406 return n;
407 }
7c673cae 408
11fdf7f2
TL
409 auto num_subtrees() const {
410 return subtrees.size();
411 }
7c673cae 412
11fdf7f2
TL
413
414protected:
7c673cae
FG
415 // -- requests --
416 ceph::unordered_map<metareqid_t, MDRequestRef> active_requests;
417
418public:
419 int get_num_client_requests();
420
11fdf7f2
TL
421 MDRequestRef request_start(const MClientRequest::const_ref& req);
422 MDRequestRef request_start_slave(metareqid_t rid, __u32 attempt, const Message::const_ref &m);
7c673cae
FG
423 MDRequestRef request_start_internal(int op);
424 bool have_request(metareqid_t rid) {
425 return active_requests.count(rid);
426 }
427 MDRequestRef request_get(metareqid_t rid);
428 void request_pin_ref(MDRequestRef& r, CInode *ref, vector<CDentry*>& trace);
429 void request_finish(MDRequestRef& mdr);
430 void request_forward(MDRequestRef& mdr, mds_rank_t mds, int port=0);
431 void dispatch_request(MDRequestRef& mdr);
432 void request_drop_foreign_locks(MDRequestRef& mdr);
433 void request_drop_non_rdlocks(MDRequestRef& r);
434 void request_drop_locks(MDRequestRef& r);
435 void request_cleanup(MDRequestRef& r);
436
437 void request_kill(MDRequestRef& r); // called when session closes
438
439 // journal/snap helpers
440 CInode *pick_inode_snap(CInode *in, snapid_t follows);
441 CInode *cow_inode(CInode *in, snapid_t last);
442 void journal_cow_dentry(MutationImpl *mut, EMetaBlob *metablob, CDentry *dn,
443 snapid_t follows=CEPH_NOSNAP,
444 CInode **pcow_inode=0, CDentry::linkage_t *dnl=0);
445 void journal_cow_inode(MutationRef& mut, EMetaBlob *metablob, CInode *in, snapid_t follows=CEPH_NOSNAP,
446 CInode **pcow_inode=0);
447 void journal_dirty_inode(MutationImpl *mut, EMetaBlob *metablob, CInode *in, snapid_t follows=CEPH_NOSNAP);
448
449 void project_rstat_inode_to_frag(CInode *cur, CDir *parent, snapid_t first,
450 int linkunlink, SnapRealm *prealm);
94b18763 451 void _project_rstat_inode_to_frag(CInode::mempool_inode & inode, snapid_t ofirst, snapid_t last,
7c673cae
FG
452 CDir *parent, int linkunlink, bool update_inode);
453 void project_rstat_frag_to_inode(nest_info_t& rstat, nest_info_t& accounted_rstat,
454 snapid_t ofirst, snapid_t last,
455 CInode *pin, bool cow_head);
a8e16298 456 void broadcast_quota_to_client(CInode *in, client_t exclude_ct = -1, bool quota_change = false);
7c673cae
FG
457 void predirty_journal_parents(MutationRef mut, EMetaBlob *blob,
458 CInode *in, CDir *parent,
459 int flags, int linkunlink=0,
460 snapid_t follows=CEPH_NOSNAP);
461
462 // slaves
463 void add_uncommitted_master(metareqid_t reqid, LogSegment *ls, set<mds_rank_t> &slaves, bool safe=false) {
464 uncommitted_masters[reqid].ls = ls;
465 uncommitted_masters[reqid].slaves = slaves;
466 uncommitted_masters[reqid].safe = safe;
467 }
11fdf7f2 468 void wait_for_uncommitted_master(metareqid_t reqid, MDSContext *c) {
7c673cae
FG
469 uncommitted_masters[reqid].waiters.push_back(c);
470 }
471 bool have_uncommitted_master(metareqid_t reqid, mds_rank_t from) {
472 auto p = uncommitted_masters.find(reqid);
473 return p != uncommitted_masters.end() && p->second.slaves.count(from) > 0;
474 }
475 void log_master_commit(metareqid_t reqid);
476 void logged_master_update(metareqid_t reqid);
477 void _logged_master_commit(metareqid_t reqid);
478 void committed_master_slave(metareqid_t r, mds_rank_t from);
479 void finish_committed_masters();
480
481 void _logged_slave_commit(mds_rank_t from, metareqid_t reqid);
482
483 // -- recovery --
484protected:
485 set<mds_rank_t> recovery_set;
486
487public:
488 void set_recovery_set(set<mds_rank_t>& s);
489 void handle_mds_failure(mds_rank_t who);
490 void handle_mds_recovery(mds_rank_t who);
491
492protected:
493 // [resolve]
494 // from EImportStart w/o EImportFinish during journal replay
495 map<dirfrag_t, vector<dirfrag_t> > my_ambiguous_imports;
496 // from MMDSResolves
497 map<mds_rank_t, map<dirfrag_t, vector<dirfrag_t> > > other_ambiguous_imports;
498
499 map<mds_rank_t, map<metareqid_t, MDSlaveUpdate*> > uncommitted_slave_updates; // slave: for replay.
500 map<CInode*, int> uncommitted_slave_rename_olddir; // slave: preserve the non-auth dir until seeing commit.
501 map<CInode*, int> uncommitted_slave_unlink; // slave: preserve the unlinked inode until seeing commit.
502
503 // track master requests whose slaves haven't acknowledged commit
504 struct umaster {
505 set<mds_rank_t> slaves;
506 LogSegment *ls;
11fdf7f2 507 MDSContext::vec waiters;
7c673cae
FG
508 bool safe;
509 bool committing;
510 bool recovering;
511 umaster() : ls(NULL), safe(false), committing(false), recovering(false) {}
512 };
513 map<metareqid_t, umaster> uncommitted_masters; // master: req -> slave set
514
515 set<metareqid_t> pending_masters;
516 map<int, set<metareqid_t> > ambiguous_slave_updates;
517
518 friend class ESlaveUpdate;
519 friend class ECommitted;
520
eafe8130 521 bool resolves_pending = false;
7c673cae
FG
522 set<mds_rank_t> resolve_gather; // nodes i need resolves from
523 set<mds_rank_t> resolve_ack_gather; // nodes i need a resolve_ack from
11fdf7f2
TL
524 set<version_t> resolve_snapclient_commits;
525 map<metareqid_t, mds_rank_t> resolve_need_rollback; // rollbacks i'm writing to the journal
526 map<mds_rank_t, MMDSResolve::const_ref> delayed_resolve;
7c673cae 527
11fdf7f2
TL
528 void handle_resolve(const MMDSResolve::const_ref &m);
529 void handle_resolve_ack(const MMDSResolveAck::const_ref &m);
7c673cae
FG
530 void process_delayed_resolve();
531 void discard_delayed_resolve(mds_rank_t who);
532 void maybe_resolve_finish();
533 void disambiguate_my_imports();
534 void disambiguate_other_imports();
535 void trim_unlinked_inodes();
536 void add_uncommitted_slave_update(metareqid_t reqid, mds_rank_t master, MDSlaveUpdate*);
537 void finish_uncommitted_slave_update(metareqid_t reqid, mds_rank_t master);
538 MDSlaveUpdate* get_uncommitted_slave_update(metareqid_t reqid, mds_rank_t master);
11fdf7f2
TL
539
540 void send_slave_resolves();
541 void send_subtree_resolves();
542 void maybe_finish_slave_resolve();
543
7c673cae
FG
544public:
545 void recalc_auth_bits(bool replay);
546 void remove_inode_recursive(CInode *in);
547
548 bool is_ambiguous_slave_update(metareqid_t reqid, mds_rank_t master) {
549 auto p = ambiguous_slave_updates.find(master);
550 return p != ambiguous_slave_updates.end() && p->second.count(reqid);
551 }
552 void add_ambiguous_slave_update(metareqid_t reqid, mds_rank_t master) {
553 ambiguous_slave_updates[master].insert(reqid);
554 }
555 void remove_ambiguous_slave_update(metareqid_t reqid, mds_rank_t master) {
556 auto p = ambiguous_slave_updates.find(master);
557 auto q = p->second.find(reqid);
11fdf7f2 558 ceph_assert(q != p->second.end());
7c673cae
FG
559 p->second.erase(q);
560 if (p->second.empty())
561 ambiguous_slave_updates.erase(p);
562 }
563
564 void add_rollback(metareqid_t reqid, mds_rank_t master) {
11fdf7f2 565 resolve_need_rollback[reqid] = master;
7c673cae
FG
566 }
567 void finish_rollback(metareqid_t reqid);
568
569 // ambiguous imports
570 void add_ambiguous_import(dirfrag_t base, const vector<dirfrag_t>& bounds);
571 void add_ambiguous_import(CDir *base, const set<CDir*>& bounds);
572 bool have_ambiguous_import(dirfrag_t base) {
573 return my_ambiguous_imports.count(base);
574 }
575 void get_ambiguous_import_bounds(dirfrag_t base, vector<dirfrag_t>& bounds) {
11fdf7f2 576 ceph_assert(my_ambiguous_imports.count(base));
7c673cae
FG
577 bounds = my_ambiguous_imports[base];
578 }
579 void cancel_ambiguous_import(CDir *);
580 void finish_ambiguous_import(dirfrag_t dirino);
11fdf7f2 581 void resolve_start(MDSContext *resolve_done_);
7c673cae 582 void send_resolves();
7c673cae
FG
583 void maybe_send_pending_resolves() {
584 if (resolves_pending)
585 send_subtree_resolves();
586 }
587
588 void _move_subtree_map_bound(dirfrag_t df, dirfrag_t oldparent, dirfrag_t newparent,
589 map<dirfrag_t,vector<dirfrag_t> >& subtrees);
590 ESubtreeMap *create_subtree_map();
591
592
593 void clean_open_file_lists();
11fdf7f2
TL
594 void dump_openfiles(Formatter *f);
595 bool dump_inode(Formatter *f, uint64_t number);
7c673cae
FG
596protected:
597 // [rejoin]
eafe8130 598 bool rejoins_pending = false;
7c673cae
FG
599 set<mds_rank_t> rejoin_gather; // nodes from whom i need a rejoin
600 set<mds_rank_t> rejoin_sent; // nodes i sent a rejoin to
31f18b77 601 set<mds_rank_t> rejoin_ack_sent; // nodes i sent a rejoin to
7c673cae
FG
602 set<mds_rank_t> rejoin_ack_gather; // nodes from whom i need a rejoin ack
603 map<mds_rank_t,map<inodeno_t,map<client_t,Capability::Import> > > rejoin_imported_caps;
604 map<inodeno_t,pair<mds_rank_t,map<client_t,Capability::Export> > > rejoin_slave_exports;
11fdf7f2 605
7c673cae 606 map<client_t,entity_inst_t> rejoin_client_map;
11fdf7f2 607 map<client_t,client_metadata_t> rejoin_client_metadata_map;
28e407b8 608 map<client_t,pair<Session*,uint64_t> > rejoin_session_map;
7c673cae 609
28e407b8 610 map<inodeno_t,pair<mds_rank_t,map<client_t,cap_reconnect_t> > > cap_exports; // ino -> target, client -> capex
7c673cae
FG
611
612 map<inodeno_t,map<client_t,map<mds_rank_t,cap_reconnect_t> > > cap_imports; // ino -> client -> frommds -> capex
613 set<inodeno_t> cap_imports_missing;
11fdf7f2 614 map<inodeno_t, MDSContext::vec > cap_reconnect_waiters;
eafe8130 615 int cap_imports_num_opening = 0;
7c673cae
FG
616
617 set<CInode*> rejoin_undef_inodes;
618 set<CInode*> rejoin_potential_updated_scatterlocks;
619 set<CDir*> rejoin_undef_dirfrags;
620 map<mds_rank_t, set<CInode*> > rejoin_unlinked_inodes;
621
622 vector<CInode*> rejoin_recover_q, rejoin_check_q;
623 list<SimpleLock*> rejoin_eval_locks;
11fdf7f2 624 MDSContext::vec rejoin_waiters;
7c673cae 625
11fdf7f2
TL
626 void rejoin_walk(CDir *dir, const MMDSCacheRejoin::ref &rejoin);
627 void handle_cache_rejoin(const MMDSCacheRejoin::const_ref &m);
628 void handle_cache_rejoin_weak(const MMDSCacheRejoin::const_ref &m);
7c673cae
FG
629 CInode* rejoin_invent_inode(inodeno_t ino, snapid_t last);
630 CDir* rejoin_invent_dirfrag(dirfrag_t df);
11fdf7f2
TL
631 void handle_cache_rejoin_strong(const MMDSCacheRejoin::const_ref &m);
632 void rejoin_scour_survivor_replicas(mds_rank_t from, const MMDSCacheRejoin::const_ref &ack,
7c673cae
FG
633 set<vinodeno_t>& acked_inodes,
634 set<SimpleLock *>& gather_locks);
11fdf7f2 635 void handle_cache_rejoin_ack(const MMDSCacheRejoin::const_ref &m);
7c673cae
FG
636 void rejoin_send_acks();
637 void rejoin_trim_undef_inodes();
638 void maybe_send_pending_rejoins() {
639 if (rejoins_pending)
640 rejoin_send_rejoins();
641 }
11fdf7f2
TL
642 std::unique_ptr<MDSContext> rejoin_done;
643 std::unique_ptr<MDSContext> resolve_done;
7c673cae 644public:
11fdf7f2 645 void rejoin_start(MDSContext *rejoin_done_);
7c673cae
FG
646 void rejoin_gather_finish();
647 void rejoin_send_rejoins();
648 void rejoin_export_caps(inodeno_t ino, client_t client, const cap_reconnect_t& icr,
11fdf7f2 649 int target=-1, bool drop_path=false) {
28e407b8
AA
650 auto& ex = cap_exports[ino];
651 ex.first = target;
11fdf7f2
TL
652 auto &_icr = ex.second[client] = icr;
653 if (drop_path)
654 _icr.path.clear();
7c673cae
FG
655 }
656 void rejoin_recovered_caps(inodeno_t ino, client_t client, const cap_reconnect_t& icr,
11fdf7f2
TL
657 mds_rank_t frommds=MDS_RANK_NONE, bool drop_path=false) {
658 auto &_icr = cap_imports[ino][client][frommds] = icr;
659 if (drop_path)
660 _icr.path.clear();
7c673cae 661 }
28e407b8
AA
662 void rejoin_recovered_client(client_t client, const entity_inst_t& inst) {
663 rejoin_client_map.emplace(client, inst);
664 }
11fdf7f2
TL
665 bool rejoin_has_cap_reconnect(inodeno_t ino) const {
666 return cap_imports.count(ino);
667 }
668 void add_replay_ino_alloc(inodeno_t ino) {
669 cap_imports_missing.insert(ino); // avoid opening ino during cache rejoin
670 }
7c673cae
FG
671 const cap_reconnect_t *get_replay_cap_reconnect(inodeno_t ino, client_t client) {
672 if (cap_imports.count(ino) &&
673 cap_imports[ino].count(client) &&
674 cap_imports[ino][client].count(MDS_RANK_NONE)) {
675 return &cap_imports[ino][client][MDS_RANK_NONE];
676 }
677 return NULL;
678 }
679 void remove_replay_cap_reconnect(inodeno_t ino, client_t client) {
11fdf7f2
TL
680 ceph_assert(cap_imports[ino].size() == 1);
681 ceph_assert(cap_imports[ino][client].size() == 1);
7c673cae
FG
682 cap_imports.erase(ino);
683 }
11fdf7f2 684 void wait_replay_cap_reconnect(inodeno_t ino, MDSContext *c) {
7c673cae
FG
685 cap_reconnect_waiters[ino].push_back(c);
686 }
687
688 // [reconnect/rejoin caps]
689 struct reconnected_cap_info_t {
690 inodeno_t realm_ino;
691 snapid_t snap_follows;
692 int dirty_caps;
11fdf7f2 693 bool snapflush;
7c673cae 694 reconnected_cap_info_t() :
11fdf7f2 695 realm_ino(0), snap_follows(0), dirty_caps(0), snapflush(false) {}
7c673cae
FG
696 };
697 map<inodeno_t,map<client_t, reconnected_cap_info_t> > reconnected_caps; // inode -> client -> snap_follows,realmino
698 map<inodeno_t,map<client_t, snapid_t> > reconnected_snaprealms; // realmino -> client -> realmseq
699
700 void add_reconnected_cap(client_t client, inodeno_t ino, const cap_reconnect_t& icr) {
701 reconnected_cap_info_t &info = reconnected_caps[ino][client];
702 info.realm_ino = inodeno_t(icr.capinfo.snaprealm);
703 info.snap_follows = icr.snap_follows;
704 }
11fdf7f2 705 void set_reconnected_dirty_caps(client_t client, inodeno_t ino, int dirty, bool snapflush) {
7c673cae
FG
706 reconnected_cap_info_t &info = reconnected_caps[ino][client];
707 info.dirty_caps |= dirty;
11fdf7f2
TL
708 if (snapflush)
709 info.snapflush = snapflush;
7c673cae
FG
710 }
711 void add_reconnected_snaprealm(client_t client, inodeno_t ino, snapid_t seq) {
712 reconnected_snaprealms[ino][client] = seq;
713 }
714
715 friend class C_MDC_RejoinOpenInoFinish;
716 friend class C_MDC_RejoinSessionsOpened;
717 void rejoin_open_ino_finish(inodeno_t ino, int ret);
11fdf7f2 718 void rejoin_prefetch_ino_finish(inodeno_t ino, int ret);
28e407b8 719 void rejoin_open_sessions_finish(map<client_t,pair<Session*,uint64_t> >& session_map);
7c673cae
FG
720 bool process_imported_caps();
721 void choose_lock_states_and_reconnect_caps();
722 void prepare_realm_split(SnapRealm *realm, client_t client, inodeno_t ino,
11fdf7f2
TL
723 map<client_t,MClientSnap::ref>& splits);
724 void prepare_realm_merge(SnapRealm *realm, SnapRealm *parent_realm, map<client_t,MClientSnap::ref>& splits);
725 void send_snaps(map<client_t,MClientSnap::ref>& splits);
7c673cae 726 Capability* rejoin_import_cap(CInode *in, client_t client, const cap_reconnect_t& icr, mds_rank_t frommds);
11fdf7f2
TL
727 void finish_snaprealm_reconnect(client_t client, SnapRealm *realm, snapid_t seq,
728 map<client_t,MClientSnap::ref>& updates);
a8e16298 729 Capability* try_reconnect_cap(CInode *in, Session *session);
7c673cae
FG
730 void export_remaining_imported_caps();
731
11fdf7f2
TL
732 // realm inodes
733 set<CInode*> rejoin_pending_snaprealms;
7c673cae 734 // cap imports. delayed snap parent opens.
7c673cae
FG
735 map<client_t,set<CInode*> > delayed_imported_caps;
736
737 void do_cap_import(Session *session, CInode *in, Capability *cap,
738 uint64_t p_cap_id, ceph_seq_t p_seq, ceph_seq_t p_mseq,
739 int peer, int p_flags);
740 void do_delayed_cap_imports();
741 void rebuild_need_snapflush(CInode *head_in, SnapRealm *realm, client_t client,
742 snapid_t snap_follows);
11fdf7f2 743 void open_snaprealms();
7c673cae
FG
744
745 bool open_undef_inodes_dirfrags();
746 void opened_undef_inode(CInode *in);
747 void opened_undef_dirfrag(CDir *dir) {
748 rejoin_undef_dirfrags.erase(dir);
749 }
750
751 void reissue_all_caps();
752
753
754 friend class Locker;
755 friend class Migrator;
756 friend class MDBalancer;
757
758 // StrayManager needs to be able to remove_inode() from us
759 // when it is done purging
760 friend class StrayManager;
761
762 // File size recovery
763private:
764 RecoveryQueue recovery_queue;
765 void identify_files_to_recover();
766public:
767 void start_files_to_recover();
768 void do_file_recover();
769 void queue_file_recover(CInode *in);
770 void _queued_file_recover_cow(CInode *in, MutationRef& mut);
771
772 // subsystems
773 std::unique_ptr<Migrator> migrator;
774
775 public:
776 explicit MDCache(MDSRank *m, PurgeQueue &purge_queue_);
777 ~MDCache();
11fdf7f2 778 void handle_conf_change(const ConfigProxy& conf,
91327a77
AA
779 const std::set <std::string> &changed,
780 const MDSMap &mds_map);
7c673cae
FG
781
782 // debug
783 void log_stat();
784
785 // root inode
786 CInode *get_root() { return root; }
787 CInode *get_myin() { return myin; }
788
7c673cae
FG
789 size_t get_cache_size() { return lru.lru_get_size(); }
790
791 // trimming
a8e16298 792 std::pair<bool, uint64_t> trim(uint64_t count=0);
181888fb 793private:
11fdf7f2
TL
794 std::pair<bool, uint64_t> trim_lru(uint64_t count, expiremap& expiremap);
795 bool trim_dentry(CDentry *dn, expiremap& expiremap);
796 void trim_dirfrag(CDir *dir, CDir *con, expiremap& expiremap);
797 bool trim_inode(CDentry *dn, CInode *in, CDir *con, expiremap&);
798 void send_expire_messages(expiremap& expiremap);
7c673cae 799 void trim_non_auth(); // trim out trimmable non-auth items
181888fb 800public:
7c673cae
FG
801 bool trim_non_auth_subtree(CDir *directory);
802 void standby_trim_segment(LogSegment *ls);
803 void try_trim_non_auth_subtree(CDir *dir);
804 bool can_trim_non_auth_dirfrag(CDir *dir) {
805 return my_ambiguous_imports.count((dir)->dirfrag()) == 0 &&
806 uncommitted_slave_rename_olddir.count(dir->inode) == 0;
807 }
808
809 /**
810 * For all unreferenced inodes, dirs, dentries below an inode, compose
811 * expiry messages. This is used when giving up all replicas of entities
812 * for an MDS peer in the 'stopping' state, such that the peer can
813 * empty its cache and finish shutting down.
814 *
815 * We have to make sure we're only expiring un-referenced items to
816 * avoid interfering with ongoing stray-movement (we can't distinguish
817 * between the "moving my strays" and "waiting for my cache to empty"
818 * phases within 'stopping')
819 *
820 * @return false if we completed cleanly, true if caller should stop
821 * expiring because we hit something with refs.
822 */
11fdf7f2 823 bool expire_recursive(CInode *in, expiremap& expiremap);
7c673cae
FG
824
825 void trim_client_leases();
826 void check_memory_usage();
827
7c673cae
FG
828 // shutdown
829private:
f64942e4
AA
830 set<inodeno_t> shutdown_exporting_strays;
831 pair<dirfrag_t, string> shutdown_export_next;
7c673cae
FG
832public:
833 void shutdown_start();
834 void shutdown_check();
835 bool shutdown_pass();
7c673cae 836 bool shutdown(); // clear cache (ie at shutodwn)
f64942e4
AA
837 bool shutdown_export_strays();
838 void shutdown_export_stray_finish(inodeno_t ino) {
839 if (shutdown_exporting_strays.erase(ino))
840 shutdown_export_strays();
841 }
7c673cae 842
eafe8130 843 bool did_shutdown_log_cap = false;
7c673cae
FG
844
845 // inode_map
846 bool have_inode(vinodeno_t vino) {
b32b8144
FG
847 if (vino.snapid == CEPH_NOSNAP)
848 return inode_map.count(vino.ino) ? true : false;
849 else
850 return snap_inode_map.count(vino) ? true : false;
7c673cae
FG
851 }
852 bool have_inode(inodeno_t ino, snapid_t snap=CEPH_NOSNAP) {
853 return have_inode(vinodeno_t(ino, snap));
854 }
855 CInode* get_inode(vinodeno_t vino) {
b32b8144
FG
856 if (vino.snapid == CEPH_NOSNAP) {
857 auto p = inode_map.find(vino.ino);
858 if (p != inode_map.end())
859 return p->second;
860 } else {
861 auto p = snap_inode_map.find(vino);
862 if (p != snap_inode_map.end())
863 return p->second;
864 }
7c673cae
FG
865 return NULL;
866 }
867 CInode* get_inode(inodeno_t ino, snapid_t s=CEPH_NOSNAP) {
868 return get_inode(vinodeno_t(ino, s));
869 }
11fdf7f2
TL
870 CInode* lookup_snap_inode(vinodeno_t vino) {
871 auto p = snap_inode_map.lower_bound(vino);
872 if (p != snap_inode_map.end() &&
873 p->second->ino() == vino.ino && p->second->first <= vino.snapid)
874 return p->second;
875 return NULL;
876 }
7c673cae
FG
877
878 CDir* get_dirfrag(dirfrag_t df) {
879 CInode *in = get_inode(df.ino);
880 if (!in)
881 return NULL;
882 return in->get_dirfrag(df.frag);
883 }
11fdf7f2 884 CDir* get_dirfrag(inodeno_t ino, std::string_view dn) {
7c673cae
FG
885 CInode *in = get_inode(ino);
886 if (!in)
887 return NULL;
888 frag_t fg = in->pick_dirfrag(dn);
889 return in->get_dirfrag(fg);
890 }
891 CDir* get_force_dirfrag(dirfrag_t df, bool replay) {
892 CInode *diri = get_inode(df.ino);
893 if (!diri)
894 return NULL;
895 CDir *dir = force_dir_fragment(diri, df.frag, replay);
896 if (!dir)
897 dir = diri->get_dirfrag(df.frag);
898 return dir;
899 }
900
11fdf7f2 901 MDSCacheObject *get_object(const MDSCacheObjectInfo &info);
7c673cae
FG
902
903
904
905 public:
906 void add_inode(CInode *in);
907
908 void remove_inode(CInode *in);
909 protected:
910 void touch_inode(CInode *in) {
911 if (in->get_parent_dn())
912 touch_dentry(in->get_projected_parent_dn());
913 }
914public:
915 void touch_dentry(CDentry *dn) {
31f18b77
FG
916 if (dn->state_test(CDentry::STATE_BOTTOMLRU)) {
917 bottom_lru.lru_midtouch(dn);
918 } else {
919 if (dn->is_auth())
920 lru.lru_touch(dn);
921 else
922 lru.lru_midtouch(dn);
923 }
7c673cae
FG
924 }
925 void touch_dentry_bottom(CDentry *dn) {
31f18b77
FG
926 if (dn->state_test(CDentry::STATE_BOTTOMLRU))
927 return;
7c673cae 928 lru.lru_bottouch(dn);
7c673cae
FG
929 }
930protected:
931
932 void inode_remove_replica(CInode *in, mds_rank_t rep, bool rejoin,
933 set<SimpleLock *>& gather_locks);
934 void dentry_remove_replica(CDentry *dn, mds_rank_t rep, set<SimpleLock *>& gather_locks);
935
936 void rename_file(CDentry *srcdn, CDentry *destdn);
937
938 public:
939 // truncate
940 void truncate_inode(CInode *in, LogSegment *ls);
941 void _truncate_inode(CInode *in, LogSegment *ls);
942 void truncate_inode_finish(CInode *in, LogSegment *ls);
943 void truncate_inode_logged(CInode *in, MutationRef& mut);
944
945 void add_recovered_truncate(CInode *in, LogSegment *ls);
946 void remove_recovered_truncate(CInode *in, LogSegment *ls);
947 void start_recovered_truncates();
948
949
950 public:
951 CDir *get_auth_container(CDir *in);
952 CDir *get_export_container(CDir *dir);
953 void find_nested_exports(CDir *dir, set<CDir*>& s);
954 void find_nested_exports_under(CDir *import, CDir *dir, set<CDir*>& s);
955
956
957private:
eafe8130 958 bool opening_root = false, open = false;
11fdf7f2 959 MDSContext::vec waiting_for_open;
7c673cae
FG
960
961public:
962 void init_layouts();
963 void create_unlinked_system_inode(CInode *in, inodeno_t ino,
964 int mode) const;
965 CInode *create_system_inode(inodeno_t ino, int mode);
966 CInode *create_root_inode();
967
968 void create_empty_hierarchy(MDSGather *gather);
969 void create_mydir_hierarchy(MDSGather *gather);
970
971 bool is_open() { return open; }
11fdf7f2 972 void wait_for_open(MDSContext *c) {
7c673cae
FG
973 waiting_for_open.push_back(c);
974 }
975
11fdf7f2 976 void open_root_inode(MDSContext *c);
7c673cae 977 void open_root();
11fdf7f2
TL
978 void open_mydir_inode(MDSContext *c);
979 void open_mydir_frag(MDSContext *c);
7c673cae
FG
980 void populate_mydir();
981
11fdf7f2 982 void _create_system_file(CDir *dir, std::string_view name, CInode *in, MDSContext *fin);
7c673cae 983 void _create_system_file_finish(MutationRef& mut, CDentry *dn,
11fdf7f2 984 version_t dpv, MDSContext *fin);
7c673cae 985
11fdf7f2 986 void open_foreign_mdsdir(inodeno_t ino, MDSContext *c);
7c673cae
FG
987 CDir *get_stray_dir(CInode *in);
988 CDentry *get_or_create_stray_dentry(CInode *in);
989
7c673cae
FG
990 /**
991 * Find the given dentry (and whether it exists or not), its ancestors,
992 * and get them all into memory and usable on this MDS. This function
993 * makes a best-effort attempt to load everything; if it needs to
994 * go away and do something then it will put the request on a waitlist.
995 * It prefers the mdr, then the req, then the fin. (At least one of these
996 * must be non-null.)
997 *
998 * At least one of the params mdr, req, and fin must be non-null.
999 *
1000 * @param mdr The MDRequest associated with the path. Can be null.
11fdf7f2 1001 * @param cf A MDSContextFactory for waiter building.
7c673cae
FG
1002 * @param path The path to traverse to.
1003 * @param pdnvec Data return parameter -- on success, contains a
1004 * vector of dentries. On failure, is either empty or contains the
1005 * full trace of traversable dentries.
1006 * @param pin Data return parameter -- if successful, points to the inode
1007 * associated with filepath. If unsuccessful, is null.
1008 * @param onfail Specifies different lookup failure behaviors. If set to
1009 * MDS_TRAVERSE_DISCOVERXLOCK, path_traverse will succeed on null
1010 * dentries (instead of returning -ENOENT). If set to
1011 * MDS_TRAVERSE_FORWARD, it will forward the request to the auth
1012 * MDS if that becomes appropriate (ie, if it doesn't know the contents
1013 * of a directory). If set to MDS_TRAVERSE_DISCOVER, it
1014 * will attempt to look up the path from a different MDS (and bring them
1015 * into its cache as replicas).
1016 *
1017 * @returns 0 on success, 1 on "not done yet", 2 on "forwarding", -errno otherwise.
1018 * If it returns 1, the requester associated with this call has been placed
1019 * on the appropriate waitlist, and it should unwind itself and back out.
1020 * If it returns 2 the request has been forwarded, and again the requester
1021 * should unwind itself and back out.
1022 */
11fdf7f2 1023 int path_traverse(MDRequestRef& mdr, MDSContextFactory& cf, const filepath& path,
7c673cae
FG
1024 vector<CDentry*> *pdnvec, CInode **pin, int onfail);
1025
1026 CInode *cache_traverse(const filepath& path);
1027
11fdf7f2 1028 void open_remote_dirfrag(CInode *diri, frag_t fg, MDSContext *fin);
7c673cae
FG
1029 CInode *get_dentry_inode(CDentry *dn, MDRequestRef& mdr, bool projected=false);
1030
1031 bool parallel_fetch(map<inodeno_t,filepath>& pathmap, set<inodeno_t>& missing);
1032 bool parallel_fetch_traverse_dir(inodeno_t ino, filepath& path,
1033 set<CDir*>& fetch_queue, set<inodeno_t>& missing,
1034 C_GatherBuilder &gather_bld);
1035
11fdf7f2 1036 void open_remote_dentry(CDentry *dn, bool projected, MDSContext *fin,
7c673cae 1037 bool want_xlocked=false);
11fdf7f2 1038 void _open_remote_dentry_finish(CDentry *dn, inodeno_t ino, MDSContext *fin,
7c673cae
FG
1039 bool want_xlocked, int r);
1040
1041 void make_trace(vector<CDentry*>& trace, CInode *in);
1042
1043protected:
1044 struct open_ino_info_t {
1045 vector<inode_backpointer_t> ancestors;
1046 set<mds_rank_t> checked;
1047 mds_rank_t checking;
1048 mds_rank_t auth_hint;
1049 bool check_peers;
1050 bool fetch_backtrace;
1051 bool discover;
1052 bool want_replica;
1053 bool want_xlocked;
1054 version_t tid;
1055 int64_t pool;
1056 int last_err;
11fdf7f2 1057 MDSContext::vec waiters;
7c673cae
FG
1058 open_ino_info_t() : checking(MDS_RANK_NONE), auth_hint(MDS_RANK_NONE),
1059 check_peers(true), fetch_backtrace(true), discover(false),
1060 want_replica(false), want_xlocked(false), tid(0), pool(-1),
1061 last_err(0) {}
1062 };
eafe8130 1063 ceph_tid_t open_ino_last_tid = 0;
7c673cae
FG
1064 map<inodeno_t,open_ino_info_t> opening_inodes;
1065
1066 void _open_ino_backtrace_fetched(inodeno_t ino, bufferlist& bl, int err);
1067 void _open_ino_parent_opened(inodeno_t ino, int ret);
1068 void _open_ino_traverse_dir(inodeno_t ino, open_ino_info_t& info, int err);
11fdf7f2
TL
1069 void _open_ino_fetch_dir(inodeno_t ino, const MMDSOpenIno::const_ref &m, CDir *dir, bool parent);
1070 int open_ino_traverse_dir(inodeno_t ino, const MMDSOpenIno::const_ref &m,
1071 const vector<inode_backpointer_t>& ancestors,
7c673cae
FG
1072 bool discover, bool want_xlocked, mds_rank_t *hint);
1073 void open_ino_finish(inodeno_t ino, open_ino_info_t& info, int err);
1074 void do_open_ino(inodeno_t ino, open_ino_info_t& info, int err);
1075 void do_open_ino_peer(inodeno_t ino, open_ino_info_t& info);
11fdf7f2
TL
1076 void handle_open_ino(const MMDSOpenIno::const_ref &m, int err=0);
1077 void handle_open_ino_reply(const MMDSOpenInoReply::const_ref &m);
7c673cae
FG
1078 friend class C_IO_MDC_OpenInoBacktraceFetched;
1079 friend struct C_MDC_OpenInoTraverseDir;
1080 friend struct C_MDC_OpenInoParentOpened;
1081
1082public:
1083 void kick_open_ino_peers(mds_rank_t who);
11fdf7f2 1084 void open_ino(inodeno_t ino, int64_t pool, MDSContext *fin,
7c673cae
FG
1085 bool want_replica=true, bool want_xlocked=false);
1086
1087 // -- find_ino_peer --
1088 struct find_ino_peer_info_t {
1089 inodeno_t ino;
1090 ceph_tid_t tid;
11fdf7f2 1091 MDSContext *fin;
7c673cae
FG
1092 mds_rank_t hint;
1093 mds_rank_t checking;
1094 set<mds_rank_t> checked;
1095
1096 find_ino_peer_info_t() : tid(0), fin(NULL), hint(MDS_RANK_NONE), checking(MDS_RANK_NONE) {}
1097 };
1098
1099 map<ceph_tid_t, find_ino_peer_info_t> find_ino_peer;
eafe8130 1100 ceph_tid_t find_ino_peer_last_tid = 0;
7c673cae 1101
11fdf7f2 1102 void find_ino_peers(inodeno_t ino, MDSContext *c, mds_rank_t hint=MDS_RANK_NONE);
7c673cae 1103 void _do_find_ino_peer(find_ino_peer_info_t& fip);
11fdf7f2
TL
1104 void handle_find_ino(const MMDSFindIno::const_ref &m);
1105 void handle_find_ino_reply(const MMDSFindInoReply::const_ref &m);
7c673cae
FG
1106 void kick_find_ino_peers(mds_rank_t who);
1107
1108 // -- snaprealms --
11fdf7f2 1109private:
eafe8130 1110 SnapRealm *global_snaprealm = nullptr;
7c673cae 1111public:
11fdf7f2
TL
1112 SnapRealm *get_global_snaprealm() const { return global_snaprealm; }
1113 void create_global_snaprealm();
1114 void do_realm_invalidate_and_update_notify(CInode *in, int snapop, bool notify_clients=true);
1115 void send_snap_update(CInode *in, version_t stid, int snap_op);
1116 void handle_snap_update(const MMDSSnapUpdate::const_ref &m);
1117 void notify_global_snaprealm_update(int snap_op);
7c673cae
FG
1118
1119 // -- stray --
1120public:
7c673cae
FG
1121 void fetch_backtrace(inodeno_t ino, int64_t pool, bufferlist& bl, Context *fin);
1122 uint64_t get_num_strays() const { return stray_manager.get_num_strays(); }
1123
1124protected:
1125 void scan_stray_dir(dirfrag_t next=dirfrag_t());
1126 StrayManager stray_manager;
1127 friend struct C_MDC_RetryScanStray;
7c673cae
FG
1128
1129 // == messages ==
1130 public:
11fdf7f2 1131 void dispatch(const Message::const_ref &m);
7c673cae
FG
1132
1133 protected:
1134 // -- replicas --
11fdf7f2
TL
1135 void handle_discover(const MDiscover::const_ref &dis);
1136 void handle_discover_reply(const MDiscoverReply::const_ref &m);
7c673cae
FG
1137 friend class C_MDC_Join;
1138
1139public:
b32b8144
FG
1140 void replicate_dir(CDir *dir, mds_rank_t to, bufferlist& bl);
1141 void replicate_dentry(CDentry *dn, mds_rank_t to, bufferlist& bl);
7c673cae 1142 void replicate_inode(CInode *in, mds_rank_t to, bufferlist& bl,
b32b8144 1143 uint64_t features);
7c673cae 1144
11fdf7f2
TL
1145 CDir* add_replica_dir(bufferlist::const_iterator& p, CInode *diri, mds_rank_t from, MDSContext::vec& finished);
1146 CDentry *add_replica_dentry(bufferlist::const_iterator& p, CDir *dir, MDSContext::vec& finished);
1147 CInode *add_replica_inode(bufferlist::const_iterator& p, CDentry *dn, MDSContext::vec& finished);
7c673cae
FG
1148
1149 void replicate_stray(CDentry *straydn, mds_rank_t who, bufferlist& bl);
11fdf7f2 1150 CDentry *add_replica_stray(const bufferlist &bl, mds_rank_t from);
7c673cae
FG
1151
1152 // -- namespace --
1153public:
1154 void send_dentry_link(CDentry *dn, MDRequestRef& mdr);
1155 void send_dentry_unlink(CDentry *dn, CDentry *straydn, MDRequestRef& mdr);
1156protected:
11fdf7f2
TL
1157 void handle_dentry_link(const MDentryLink::const_ref &m);
1158 void handle_dentry_unlink(const MDentryUnlink::const_ref &m);
7c673cae
FG
1159
1160
1161 // -- fragmenting --
1162private:
1163 struct ufragment {
1164 int bits;
1165 bool committed;
1166 LogSegment *ls;
11fdf7f2
TL
1167 MDSContext::vec waiters;
1168 frag_vec_t old_frags;
7c673cae
FG
1169 bufferlist rollback;
1170 ufragment() : bits(0), committed(false), ls(NULL) {}
1171 };
1172 map<dirfrag_t, ufragment> uncommitted_fragments;
1173
1174 struct fragment_info_t {
1175 int bits;
1176 list<CDir*> dirs;
1177 list<CDir*> resultfrags;
1178 MDRequestRef mdr;
a8e16298
TL
1179 set<mds_rank_t> notify_ack_waiting;
1180 bool finishing = false;
1181
7c673cae 1182 // for deadlock detection
a8e16298 1183 bool all_frozen = false;
7c673cae 1184 utime_t last_cum_auth_pins_change;
a8e16298
TL
1185 int last_cum_auth_pins = 0;
1186 int num_remote_waiters = 0; // number of remote authpin waiters
1187 fragment_info_t() {}
7c673cae 1188 bool is_fragmenting() { return !resultfrags.empty(); }
a8e16298 1189 uint64_t get_tid() { return mdr ? mdr->reqid.tid : 0; }
7c673cae
FG
1190 };
1191 map<dirfrag_t,fragment_info_t> fragments;
a8e16298 1192 typedef map<dirfrag_t,fragment_info_t>::iterator fragment_info_iterator;
7c673cae
FG
1193
1194 void adjust_dir_fragments(CInode *diri, frag_t basefrag, int bits,
11fdf7f2 1195 list<CDir*>& frags, MDSContext::vec& waiters, bool replay);
7c673cae
FG
1196 void adjust_dir_fragments(CInode *diri,
1197 list<CDir*>& srcfrags,
1198 frag_t basefrag, int bits,
1199 list<CDir*>& resultfrags,
11fdf7f2 1200 MDSContext::vec& waiters,
7c673cae
FG
1201 bool replay);
1202 CDir *force_dir_fragment(CInode *diri, frag_t fg, bool replay=true);
11fdf7f2 1203 void get_force_dirfrag_bound_set(const vector<dirfrag_t>& dfs, set<CDir*>& bounds);
7c673cae
FG
1204
1205 bool can_fragment(CInode *diri, list<CDir*>& dirs);
1206 void fragment_freeze_dirs(list<CDir*>& dirs);
1207 void fragment_mark_and_complete(MDRequestRef& mdr);
1208 void fragment_frozen(MDRequestRef& mdr, int r);
1209 void fragment_unmark_unfreeze_dirs(list<CDir*>& dirs);
a8e16298
TL
1210 void fragment_drop_locks(fragment_info_t &info);
1211 void fragment_maybe_finish(const fragment_info_iterator& it);
7c673cae
FG
1212 void dispatch_fragment_dir(MDRequestRef& mdr);
1213 void _fragment_logged(MDRequestRef& mdr);
1214 void _fragment_stored(MDRequestRef& mdr);
a8e16298
TL
1215 void _fragment_committed(dirfrag_t f, const MDRequestRef& mdr);
1216 void _fragment_old_purged(dirfrag_t f, int bits, const MDRequestRef& mdr);
7c673cae
FG
1217
1218 friend class EFragment;
1219 friend class C_MDC_FragmentFrozen;
1220 friend class C_MDC_FragmentMarking;
1221 friend class C_MDC_FragmentPrep;
1222 friend class C_MDC_FragmentStore;
1223 friend class C_MDC_FragmentCommit;
a8e16298 1224 friend class C_IO_MDC_FragmentPurgeOld;
7c673cae 1225
11fdf7f2
TL
1226 void handle_fragment_notify(const MMDSFragmentNotify::const_ref &m);
1227 void handle_fragment_notify_ack(const MMDSFragmentNotifyAck::const_ref &m);
7c673cae 1228
11fdf7f2 1229 void add_uncommitted_fragment(dirfrag_t basedirfrag, int bits, const frag_vec_t& old_frag,
7c673cae
FG
1230 LogSegment *ls, bufferlist *rollback=NULL);
1231 void finish_uncommitted_fragment(dirfrag_t basedirfrag, int op);
11fdf7f2 1232 void rollback_uncommitted_fragment(dirfrag_t basedirfrag, frag_vec_t&& old_frags);
a8e16298
TL
1233
1234
1235 DecayCounter trim_counter;
1236
7c673cae 1237public:
11fdf7f2
TL
1238 void wait_for_uncommitted_fragment(dirfrag_t dirfrag, MDSContext *c) {
1239 ceph_assert(uncommitted_fragments.count(dirfrag));
7c673cae
FG
1240 uncommitted_fragments[dirfrag].waiters.push_back(c);
1241 }
1242 void split_dir(CDir *dir, int byn);
1243 void merge_dir(CInode *diri, frag_t fg);
1244 void rollback_uncommitted_fragments();
1245
1246 void find_stale_fragment_freeze();
1247 void fragment_freeze_inc_num_waiters(CDir *dir);
1248 bool fragment_are_all_frozen(CDir *dir);
1249 int get_num_fragmenting_dirs() { return fragments.size(); }
1250
1251 // -- updates --
1252 //int send_inode_updates(CInode *in);
1253 //void handle_inode_update(MInodeUpdate *m);
1254
1255 int send_dir_updates(CDir *in, bool bcast=false);
11fdf7f2 1256 void handle_dir_update(const MDirUpdate::const_ref &m);
7c673cae
FG
1257
1258 // -- cache expiration --
11fdf7f2
TL
1259 void handle_cache_expire(const MCacheExpire::const_ref &m);
1260 // delayed cache expire
1261 map<CDir*, expiremap> delayed_expire; // subtree root -> expire msg
7c673cae
FG
1262 void process_delayed_expire(CDir *dir);
1263 void discard_delayed_expire(CDir *dir);
1264
eafe8130
TL
1265 // -- mdsmap --
1266 void handle_mdsmap(const MDSMap &mdsmap);
1267
7c673cae 1268protected:
11fdf7f2 1269 int dump_cache(std::string_view fn, Formatter *f);
7c673cae 1270public:
31f18b77 1271 int dump_cache() { return dump_cache(NULL, NULL); }
11fdf7f2 1272 int dump_cache(std::string_view filename);
31f18b77 1273 int dump_cache(Formatter *f);
11fdf7f2 1274 void dump_tree(CInode *in, const int cur_depth, const int max_depth, Formatter *f);
7c673cae 1275
f64942e4 1276 void cache_status(Formatter *f);
181888fb 1277
7c673cae
FG
1278 void dump_resolve_status(Formatter *f) const;
1279 void dump_rejoin_status(Formatter *f) const;
1280
1281 // == crap fns ==
1282 public:
1283 void show_cache();
81eedcae 1284 void show_subtrees(int dbl=10, bool force_print=false);
7c673cae
FG
1285
1286 CInode *hack_pick_random_inode() {
11fdf7f2 1287 ceph_assert(!inode_map.empty());
7c673cae 1288 int n = rand() % inode_map.size();
b32b8144 1289 auto p = inode_map.begin();
7c673cae
FG
1290 while (n--) ++p;
1291 return p->second;
1292 }
1293
1294protected:
1295 void flush_dentry_work(MDRequestRef& mdr);
1296 /**
1297 * Resolve path to a dentry and pass it onto the ScrubStack.
1298 *
1299 * TODO: return enough information to the original mdr formatter
1300 * and completion that they can subsequeuntly check the progress of
1301 * this scrub (we won't block them on a whole scrub as it can take a very
1302 * long time)
1303 */
1304 void enqueue_scrub_work(MDRequestRef& mdr);
11fdf7f2 1305 void recursive_scrub_finish(const ScrubHeaderRef& header);
7c673cae
FG
1306 void repair_inode_stats_work(MDRequestRef& mdr);
1307 void repair_dirfrag_stats_work(MDRequestRef& mdr);
11fdf7f2
TL
1308 void upgrade_inode_snaprealm_work(MDRequestRef& mdr);
1309 friend class C_MDC_RespondInternalRequest;
7c673cae 1310public:
11fdf7f2 1311 void flush_dentry(std::string_view path, Context *fin);
7c673cae
FG
1312 /**
1313 * Create and start an OP_ENQUEUE_SCRUB
1314 */
11fdf7f2 1315 void enqueue_scrub(std::string_view path, std::string_view tag,
7c673cae
FG
1316 bool force, bool recursive, bool repair,
1317 Formatter *f, Context *fin);
1318 void repair_inode_stats(CInode *diri);
1319 void repair_dirfrag_stats(CDir *dir);
11fdf7f2 1320 void upgrade_inode_snaprealm(CInode *in);
7c673cae
FG
1321
1322public:
1323 /* Because exports may fail, this set lets us keep track of inodes that need exporting. */
1324 std::set<CInode *> export_pin_queue;
eafe8130 1325 std::set<CInode *> export_pin_delayed_queue;
11fdf7f2
TL
1326
1327 OpenFileTable open_file_table;
eafe8130
TL
1328
1329private:
1330 std::thread upkeeper;
1331 ceph::mutex upkeep_mutex = ceph::make_mutex("MDCache::upkeep_mutex");
1332 ceph::condition_variable upkeep_cvar;
1333 time upkeep_last_trim = time::min();
1334 std::atomic<bool> upkeep_trim_shutdown{false};
7c673cae
FG
1335};
1336
1337class C_MDS_RetryRequest : public MDSInternalContext {
1338 MDCache *cache;
1339 MDRequestRef mdr;
1340 public:
1341 C_MDS_RetryRequest(MDCache *c, MDRequestRef& r);
1342 void finish(int r) override;
1343};
1344
1345#endif