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