1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
16 #include "include/types.h"
28 #include "LogSegment.h"
30 #include "common/bloom_filter.hpp"
31 #include "include/Context.h"
32 #include "common/Clock.h"
34 #include "osdc/Objecter.h"
36 #include "common/config.h"
37 #include "include/assert.h"
38 #include "include/compat.h"
40 #define dout_context g_ceph_context
41 #define dout_subsys ceph_subsys_mds
43 #define dout_prefix *_dout << "mds." << cache->mds->get_nodeid() << ".cache.dir(" << this->dirfrag() << ") "
45 int CDir::num_frozen_trees = 0;
46 int CDir::num_freezing_trees = 0;
48 class CDirContext : public MDSInternalContextBase
52 MDSRank* get_mds() override {return dir->cache->mds;}
55 explicit CDirContext(CDir *d) : dir(d) {
61 class CDirIOContext : public MDSIOContextBase
65 MDSRank* get_mds() override {return dir->cache->mds;}
68 explicit CDirIOContext(CDir *d) : dir(d) {
75 //int cdir_pins[CDIR_NUM_PINS] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
78 ostream& operator<<(ostream& out, const CDir& dir)
80 out << "[dir " << dir.dirfrag() << " " << dir.get_path() << "/"
81 << " [" << dir.first << ",head]";
84 if (dir.is_replicated())
85 out << dir.get_replicas();
87 if (dir.is_projected())
88 out << " pv=" << dir.get_projected_version();
89 out << " v=" << dir.get_version();
90 out << " cv=" << dir.get_committing_version();
91 out << "/" << dir.get_committed_version();
93 mds_authority_t a = dir.authority();
94 out << " rep@" << a.first;
95 if (a.second != CDIR_AUTH_UNKNOWN)
96 out << "," << a.second;
97 out << "." << dir.get_replica_nonce();
100 if (dir.is_rep()) out << " REP";
102 if (dir.get_dir_auth() != CDIR_AUTH_DEFAULT) {
103 if (dir.get_dir_auth().second == CDIR_AUTH_UNKNOWN)
104 out << " dir_auth=" << dir.get_dir_auth().first;
106 out << " dir_auth=" << dir.get_dir_auth();
109 if (dir.get_cum_auth_pins())
110 out << " ap=" << dir.get_auth_pins()
111 << "+" << dir.get_dir_auth_pins()
112 << "+" << dir.get_nested_auth_pins();
114 out << " state=" << dir.get_state();
115 if (dir.state_test(CDir::STATE_COMPLETE)) out << "|complete";
116 if (dir.state_test(CDir::STATE_FREEZINGTREE)) out << "|freezingtree";
117 if (dir.state_test(CDir::STATE_FROZENTREE)) out << "|frozentree";
118 if (dir.state_test(CDir::STATE_AUXSUBTREE)) out << "|auxsubtree";
119 //if (dir.state_test(CDir::STATE_FROZENTREELEAF)) out << "|frozentreeleaf";
120 if (dir.state_test(CDir::STATE_FROZENDIR)) out << "|frozendir";
121 if (dir.state_test(CDir::STATE_FREEZINGDIR)) out << "|freezingdir";
122 if (dir.state_test(CDir::STATE_EXPORTBOUND)) out << "|exportbound";
123 if (dir.state_test(CDir::STATE_IMPORTBOUND)) out << "|importbound";
124 if (dir.state_test(CDir::STATE_BADFRAG)) out << "|badfrag";
125 if (dir.state_test(CDir::STATE_FRAGMENTING)) out << "|fragmenting";
128 out << " " << dir.fnode.fragstat;
129 if (!(dir.fnode.fragstat == dir.fnode.accounted_fragstat))
130 out << "/" << dir.fnode.accounted_fragstat;
131 if (g_conf->mds_debug_scatterstat && dir.is_projected()) {
132 const fnode_t *pf = dir.get_projected_fnode();
133 out << "->" << pf->fragstat;
134 if (!(pf->fragstat == pf->accounted_fragstat))
135 out << "/" << pf->accounted_fragstat;
139 out << " " << dir.fnode.rstat;
140 if (!(dir.fnode.rstat == dir.fnode.accounted_rstat))
141 out << "/" << dir.fnode.accounted_rstat;
142 if (g_conf->mds_debug_scatterstat && dir.is_projected()) {
143 const fnode_t *pf = dir.get_projected_fnode();
144 out << "->" << pf->rstat;
145 if (!(pf->rstat == pf->accounted_rstat))
146 out << "/" << pf->accounted_rstat;
149 out << " hs=" << dir.get_num_head_items() << "+" << dir.get_num_head_null();
150 out << ",ss=" << dir.get_num_snap_items() << "+" << dir.get_num_snap_null();
151 if (dir.get_num_dirty())
152 out << " dirty=" << dir.get_num_dirty();
154 if (dir.get_num_ref()) {
156 dir.print_pin_set(out);
164 void CDir::print(ostream& out)
172 ostream& CDir::print_db_line_prefix(ostream& out)
174 return out << ceph_clock_now() << " mds." << cache->mds->get_nodeid() << ".cache.dir(" << this->dirfrag() << ") ";
179 // -------------------------------------------------------------------
182 CDir::CDir(CInode *in, frag_t fg, MDCache *mdcache, bool auth) :
183 cache(mdcache), inode(in), frag(fg),
185 dirty_rstat_inodes(member_offset(CInode, dirty_rstat_item)),
186 projected_version(0), item_dirty(this), item_new(this),
187 num_head_items(0), num_head_null(0),
188 num_snap_items(0), num_snap_null(0),
189 num_dirty(0), committing_version(0), committed_version(0),
190 dir_auth_pins(0), request_pins(0),
192 pop_me(ceph_clock_now()),
193 pop_nested(ceph_clock_now()),
194 pop_auth_subtree(ceph_clock_now()),
195 pop_auth_subtree_nested(ceph_clock_now()),
196 num_dentries_nested(0), num_dentries_auth_subtree(0),
197 num_dentries_auth_subtree_nested(0),
198 dir_auth(CDIR_AUTH_DEFAULT)
200 state = STATE_INITIAL;
202 memset(&fnode, 0, sizeof(fnode));
205 assert(in->is_dir());
211 * Check the recursive statistics on size for consistency.
212 * If mds_debug_scatterstat is enabled, assert for correctness,
213 * otherwise just print out the mismatch and continue.
215 bool CDir::check_rstats(bool scrub)
217 if (!g_conf->mds_debug_scatterstat && !scrub)
220 dout(25) << "check_rstats on " << this << dendl;
221 if (!is_complete() || !is_auth() || is_frozen()) {
223 dout(10) << "check_rstats bailing out -- incomplete or non-auth or frozen dir!" << dendl;
227 frag_info_t frag_info;
228 nest_info_t nest_info;
229 for (map_t::iterator i = items.begin(); i != items.end(); ++i) {
230 if (i->second->last != CEPH_NOSNAP)
232 CDentry::linkage_t *dnl = i->second->get_linkage();
233 if (dnl->is_primary()) {
234 CInode *in = dnl->get_inode();
235 nest_info.add(in->inode.accounted_rstat);
237 frag_info.nsubdirs++;
240 } else if (dnl->is_remote())
246 if(!frag_info.same_sums(fnode.fragstat)) {
247 dout(1) << "mismatch between head items and fnode.fragstat! printing dentries" << dendl;
248 dout(1) << "get_num_head_items() = " << get_num_head_items()
249 << "; fnode.fragstat.nfiles=" << fnode.fragstat.nfiles
250 << " fnode.fragstat.nsubdirs=" << fnode.fragstat.nsubdirs << dendl;
253 dout(20) << "get_num_head_items() = " << get_num_head_items()
254 << "; fnode.fragstat.nfiles=" << fnode.fragstat.nfiles
255 << " fnode.fragstat.nsubdirs=" << fnode.fragstat.nsubdirs << dendl;
259 if (!nest_info.same_sums(fnode.rstat)) {
260 dout(1) << "mismatch between child accounted_rstats and my rstats!" << dendl;
261 dout(1) << "total of child dentrys: " << nest_info << dendl;
262 dout(1) << "my rstats: " << fnode.rstat << dendl;
265 dout(20) << "total of child dentrys: " << nest_info << dendl;
266 dout(20) << "my rstats: " << fnode.rstat << dendl;
271 for (map_t::iterator i = items.begin(); i != items.end(); ++i) {
272 CDentry *dn = i->second;
273 if (dn->get_linkage()->is_primary()) {
274 CInode *in = dn->get_linkage()->inode;
275 dout(1) << *dn << " rstat " << in->inode.accounted_rstat << dendl;
277 dout(1) << *dn << dendl;
281 assert(frag_info.nfiles == fnode.fragstat.nfiles);
282 assert(frag_info.nsubdirs == fnode.fragstat.nsubdirs);
283 assert(nest_info.rbytes == fnode.rstat.rbytes);
284 assert(nest_info.rfiles == fnode.rstat.rfiles);
285 assert(nest_info.rsubdirs == fnode.rstat.rsubdirs);
288 dout(10) << "check_rstats complete on " << this << dendl;
292 CDentry *CDir::lookup(const string& name, snapid_t snap)
294 dout(20) << "lookup (" << snap << ", '" << name << "')" << dendl;
295 map_t::iterator iter = items.lower_bound(dentry_key_t(snap, name.c_str(),
296 inode->hash_dentry_name(name)));
297 if (iter == items.end())
299 if (iter->second->name == name &&
300 iter->second->first <= snap &&
301 iter->second->last >= snap) {
302 dout(20) << " hit -> " << iter->first << dendl;
305 dout(20) << " miss -> " << iter->first << dendl;
309 CDentry *CDir::lookup_exact_snap(const string& name, snapid_t last) {
310 map_t::iterator p = items.find(dentry_key_t(last, name.c_str(),
311 inode->hash_dentry_name(name)));
312 if (p == items.end())
321 CDentry* CDir::add_null_dentry(const string& dname,
322 snapid_t first, snapid_t last)
325 assert(lookup_exact_snap(dname, last) == 0);
328 CDentry* dn = new CDentry(dname, inode->hash_dentry_name(dname), first, last);
330 dn->state_set(CDentry::STATE_AUTH);
332 cache->bottom_lru.lru_insert_mid(dn);
333 dn->state_set(CDentry::STATE_BOTTOMLRU);
336 dn->version = get_projected_version();
339 assert(items.count(dn->key()) == 0);
340 //assert(null_items.count(dn->name) == 0);
342 items[dn->key()] = dn;
343 if (last == CEPH_NOSNAP)
348 if (state_test(CDir::STATE_DNPINNEDFRAG)) {
349 dn->get(CDentry::PIN_FRAGMENTING);
350 dn->state_set(CDentry::STATE_FRAGMENTING);
353 dout(12) << "add_null_dentry " << *dn << dendl;
356 if (get_num_any() == 1)
359 assert(get_num_any() == items.size());
364 CDentry* CDir::add_primary_dentry(const string& dname, CInode *in,
365 snapid_t first, snapid_t last)
368 assert(lookup_exact_snap(dname, last) == 0);
371 CDentry* dn = new CDentry(dname, inode->hash_dentry_name(dname), first, last);
373 dn->state_set(CDentry::STATE_AUTH);
374 if (is_auth() || !inode->is_stray()) {
375 cache->lru.lru_insert_mid(dn);
377 cache->bottom_lru.lru_insert_mid(dn);
378 dn->state_set(CDentry::STATE_BOTTOMLRU);
382 dn->version = get_projected_version();
385 assert(items.count(dn->key()) == 0);
386 //assert(null_items.count(dn->name) == 0);
388 items[dn->key()] = dn;
390 dn->get_linkage()->inode = in;
391 in->set_primary_parent(dn);
393 link_inode_work(dn, in);
395 if (dn->last == CEPH_NOSNAP)
400 if (state_test(CDir::STATE_DNPINNEDFRAG)) {
401 dn->get(CDentry::PIN_FRAGMENTING);
402 dn->state_set(CDentry::STATE_FRAGMENTING);
405 dout(12) << "add_primary_dentry " << *dn << dendl;
408 if (get_num_any() == 1)
410 assert(get_num_any() == items.size());
414 CDentry* CDir::add_remote_dentry(const string& dname, inodeno_t ino, unsigned char d_type,
415 snapid_t first, snapid_t last)
418 assert(lookup_exact_snap(dname, last) == 0);
421 CDentry* dn = new CDentry(dname, inode->hash_dentry_name(dname), ino, d_type, first, last);
423 dn->state_set(CDentry::STATE_AUTH);
424 cache->lru.lru_insert_mid(dn);
427 dn->version = get_projected_version();
430 assert(items.count(dn->key()) == 0);
431 //assert(null_items.count(dn->name) == 0);
433 items[dn->key()] = dn;
434 if (last == CEPH_NOSNAP)
439 if (state_test(CDir::STATE_DNPINNEDFRAG)) {
440 dn->get(CDentry::PIN_FRAGMENTING);
441 dn->state_set(CDentry::STATE_FRAGMENTING);
444 dout(12) << "add_remote_dentry " << *dn << dendl;
447 if (get_num_any() == 1)
450 assert(get_num_any() == items.size());
456 void CDir::remove_dentry(CDentry *dn)
458 dout(12) << "remove_dentry " << *dn << dendl;
460 // there should be no client leases at this point!
461 assert(dn->client_lease_map.empty());
463 if (state_test(CDir::STATE_DNPINNEDFRAG)) {
464 dn->put(CDentry::PIN_FRAGMENTING);
465 dn->state_clear(CDentry::STATE_FRAGMENTING);
468 if (dn->get_linkage()->is_null()) {
469 if (dn->last == CEPH_NOSNAP)
474 if (dn->last == CEPH_NOSNAP)
480 if (!dn->get_linkage()->is_null())
481 // detach inode and dentry
482 unlink_inode_work(dn);
485 assert(items.count(dn->key()) == 1);
486 items.erase(dn->key());
492 if (dn->state_test(CDentry::STATE_BOTTOMLRU))
493 cache->bottom_lru.lru_remove(dn);
495 cache->lru.lru_remove(dn);
499 if (get_num_any() == 0)
501 assert(get_num_any() == items.size());
504 void CDir::link_remote_inode(CDentry *dn, CInode *in)
506 link_remote_inode(dn, in->ino(), IFTODT(in->get_projected_inode()->mode));
509 void CDir::link_remote_inode(CDentry *dn, inodeno_t ino, unsigned char d_type)
511 dout(12) << "link_remote_inode " << *dn << " remote " << ino << dendl;
512 assert(dn->get_linkage()->is_null());
514 dn->get_linkage()->set_remote(ino, d_type);
516 if (dn->state_test(CDentry::STATE_BOTTOMLRU)) {
517 cache->bottom_lru.lru_remove(dn);
518 cache->lru.lru_insert_mid(dn);
519 dn->state_clear(CDentry::STATE_BOTTOMLRU);
522 if (dn->last == CEPH_NOSNAP) {
529 assert(get_num_any() == items.size());
532 void CDir::link_primary_inode(CDentry *dn, CInode *in)
534 dout(12) << "link_primary_inode " << *dn << " " << *in << dendl;
535 assert(dn->get_linkage()->is_null());
537 dn->get_linkage()->inode = in;
538 in->set_primary_parent(dn);
540 link_inode_work(dn, in);
542 if (dn->state_test(CDentry::STATE_BOTTOMLRU) &&
543 (is_auth() || !inode->is_stray())) {
544 cache->bottom_lru.lru_remove(dn);
545 cache->lru.lru_insert_mid(dn);
546 dn->state_clear(CDentry::STATE_BOTTOMLRU);
549 if (dn->last == CEPH_NOSNAP) {
557 assert(get_num_any() == items.size());
560 void CDir::link_inode_work( CDentry *dn, CInode *in)
562 assert(dn->get_linkage()->get_inode() == in);
563 assert(in->get_parent_dn() == dn);
566 //in->inode.version = dn->get_version();
569 if (in->get_num_ref())
570 dn->get(CDentry::PIN_INODEPIN);
572 // adjust auth pin count
573 if (in->auth_pins + in->nested_auth_pins)
574 dn->adjust_nested_auth_pins(in->auth_pins + in->nested_auth_pins, in->auth_pins, NULL);
576 // verify open snaprealm parent
578 in->snaprealm->adjust_parent();
579 else if (in->is_any_caps())
580 in->move_to_realm(inode->find_snaprealm());
583 void CDir::unlink_inode(CDentry *dn, bool adjust_lru)
585 if (dn->get_linkage()->is_primary()) {
586 dout(12) << "unlink_inode " << *dn << " " << *dn->get_linkage()->get_inode() << dendl;
588 dout(12) << "unlink_inode " << *dn << dendl;
591 unlink_inode_work(dn);
593 if (adjust_lru && !dn->state_test(CDentry::STATE_BOTTOMLRU)) {
594 cache->lru.lru_remove(dn);
595 cache->bottom_lru.lru_insert_mid(dn);
596 dn->state_set(CDentry::STATE_BOTTOMLRU);
599 if (dn->last == CEPH_NOSNAP) {
606 assert(get_num_any() == items.size());
610 void CDir::try_remove_unlinked_dn(CDentry *dn)
612 assert(dn->dir == this);
613 assert(dn->get_linkage()->is_null());
615 // no pins (besides dirty)?
616 if (dn->get_num_ref() != dn->is_dirty())
621 dout(10) << "try_remove_unlinked_dn " << *dn << " in " << *this << dendl;
626 // NOTE: we may not have any more dirty dentries, but the fnode
627 // still changed, so the directory must remain dirty.
632 void CDir::unlink_inode_work( CDentry *dn )
634 CInode *in = dn->get_linkage()->get_inode();
636 if (dn->get_linkage()->is_remote()) {
639 dn->unlink_remote(dn->get_linkage());
641 dn->get_linkage()->set_remote(0, 0);
642 } else if (dn->get_linkage()->is_primary()) {
645 if (in->get_num_ref())
646 dn->put(CDentry::PIN_INODEPIN);
648 // unlink auth_pin count
649 if (in->auth_pins + in->nested_auth_pins)
650 dn->adjust_nested_auth_pins(0 - (in->auth_pins + in->nested_auth_pins), 0 - in->auth_pins, NULL);
653 in->remove_primary_parent(dn);
654 dn->get_linkage()->inode = 0;
656 assert(!dn->get_linkage()->is_null());
660 void CDir::add_to_bloom(CDentry *dn)
662 assert(dn->last == CEPH_NOSNAP);
664 /* not create bloom filter for incomplete dir that was added by log replay */
668 /* don't maintain bloom filters in standby replay (saves cycles, and also
669 * avoids need to implement clearing it in EExport for #16924) */
670 if (cache->mds->is_standby_replay()) {
674 unsigned size = get_num_head_items() + get_num_snap_items();
675 if (size < 100) size = 100;
676 bloom.reset(new bloom_filter(size, 1.0 / size, 0));
678 /* This size and false positive probability is completely random.*/
679 bloom->insert(dn->name.c_str(), dn->name.size());
682 bool CDir::is_in_bloom(const string& name)
686 return bloom->contains(name.c_str(), name.size());
689 void CDir::remove_null_dentries() {
690 dout(12) << "remove_null_dentries " << *this << dendl;
692 CDir::map_t::iterator p = items.begin();
693 while (p != items.end()) {
694 CDentry *dn = p->second;
696 if (dn->get_linkage()->is_null() && !dn->is_projected())
700 assert(num_snap_null == 0);
701 assert(num_head_null == 0);
702 assert(get_num_any() == items.size());
705 /** remove dirty null dentries for deleted directory. the dirfrag will be
706 * deleted soon, so it's safe to not commit dirty dentries.
708 * This is called when a directory is being deleted, a prerequisite
709 * of which is that its children have been unlinked: we expect to only see
710 * null, unprojected dentries here.
712 void CDir::try_remove_dentries_for_stray()
714 dout(10) << __func__ << dendl;
715 assert(get_parent_dir()->inode->is_stray());
717 // clear dirty only when the directory was not snapshotted
718 bool clear_dirty = !inode->snaprealm;
720 CDir::map_t::iterator p = items.begin();
721 while (p != items.end()) {
722 CDentry *dn = p->second;
724 if (dn->last == CEPH_NOSNAP) {
725 assert(!dn->is_projected());
726 assert(dn->get_linkage()->is_null());
727 if (clear_dirty && dn->is_dirty())
729 // It's OK to remove lease prematurely because we will never link
730 // the dentry to inode again.
731 if (dn->is_any_leases())
732 dn->remove_client_leases(cache->mds->locker);
733 if (dn->get_num_ref() == 0)
736 assert(!dn->is_projected());
737 CDentry::linkage_t *dnl= dn->get_linkage();
739 if (dnl->is_primary()) {
740 in = dnl->get_inode();
741 if (clear_dirty && in->is_dirty())
744 if (clear_dirty && dn->is_dirty())
746 if (dn->get_num_ref() == 0) {
749 cache->remove_inode(in);
754 if (clear_dirty && is_dirty())
758 bool CDir::try_trim_snap_dentry(CDentry *dn, const set<snapid_t>& snaps)
760 assert(dn->last != CEPH_NOSNAP);
761 set<snapid_t>::const_iterator p = snaps.lower_bound(dn->first);
762 CDentry::linkage_t *dnl= dn->get_linkage();
764 if (dnl->is_primary())
765 in = dnl->get_inode();
766 if ((p == snaps.end() || *p > dn->last) &&
767 (dn->get_num_ref() == dn->is_dirty()) &&
768 (!in || in->get_num_ref() == in->is_dirty())) {
769 dout(10) << " purging snapped " << *dn << dendl;
770 if (in && in->is_dirty())
774 dout(10) << " purging snapped " << *in << dendl;
775 cache->remove_inode(in);
783 void CDir::purge_stale_snap_data(const set<snapid_t>& snaps)
785 dout(10) << "purge_stale_snap_data " << snaps << dendl;
787 CDir::map_t::iterator p = items.begin();
788 while (p != items.end()) {
789 CDentry *dn = p->second;
792 if (dn->last == CEPH_NOSNAP)
795 try_trim_snap_dentry(dn, snaps);
801 * steal_dentry -- semi-violently move a dentry from one CDir to another
802 * (*) violently, in that nitems, most pins, etc. are not correctly maintained
803 * on the old CDir corpse; must call finish_old_fragment() when finished.
805 void CDir::steal_dentry(CDentry *dn)
807 dout(15) << "steal_dentry " << *dn << dendl;
809 items[dn->key()] = dn;
811 dn->dir->items.erase(dn->key());
812 if (dn->dir->items.empty())
813 dn->dir->put(PIN_CHILD);
815 if (get_num_any() == 0)
817 if (dn->get_linkage()->is_null()) {
818 if (dn->last == CEPH_NOSNAP)
822 } else if (dn->last == CEPH_NOSNAP) {
825 if (dn->get_linkage()->is_primary()) {
826 CInode *in = dn->get_linkage()->get_inode();
827 inode_t *pi = in->get_projected_inode();
828 if (dn->get_linkage()->get_inode()->is_dir())
829 fnode.fragstat.nsubdirs++;
831 fnode.fragstat.nfiles++;
832 fnode.rstat.rbytes += pi->accounted_rstat.rbytes;
833 fnode.rstat.rfiles += pi->accounted_rstat.rfiles;
834 fnode.rstat.rsubdirs += pi->accounted_rstat.rsubdirs;
835 fnode.rstat.rsnaprealms += pi->accounted_rstat.rsnaprealms;
836 if (pi->accounted_rstat.rctime > fnode.rstat.rctime)
837 fnode.rstat.rctime = pi->accounted_rstat.rctime;
839 // move dirty inode rstat to new dirfrag
840 if (in->is_dirty_rstat())
841 dirty_rstat_inodes.push_back(&in->dirty_rstat_item);
842 } else if (dn->get_linkage()->is_remote()) {
843 if (dn->get_linkage()->get_remote_d_type() == DT_DIR)
844 fnode.fragstat.nsubdirs++;
846 fnode.fragstat.nfiles++;
850 if (dn->get_linkage()->is_primary()) {
851 CInode *in = dn->get_linkage()->get_inode();
852 if (in->is_dirty_rstat())
853 dirty_rstat_inodes.push_back(&in->dirty_rstat_item);
857 if (dn->auth_pins || dn->nested_auth_pins) {
858 // use the helpers here to maintain the auth_pin invariants on the dir inode
859 int ap = dn->get_num_auth_pins() + dn->get_num_nested_auth_pins();
860 int dap = dn->get_num_dir_auth_pins();
862 adjust_nested_auth_pins(ap, dap, NULL);
863 dn->dir->adjust_nested_auth_pins(-ap, -dap, NULL);
872 void CDir::prepare_old_fragment(map<string_snap_t, std::list<MDSInternalContextBase*> >& dentry_waiters, bool replay)
874 // auth_pin old fragment for duration so that any auth_pinning
875 // during the dentry migration doesn't trigger side effects
876 if (!replay && is_auth())
879 if (!waiting_on_dentry.empty()) {
880 for (auto p = waiting_on_dentry.begin(); p != waiting_on_dentry.end(); ++p)
881 dentry_waiters[p->first].swap(p->second);
882 waiting_on_dentry.clear();
887 void CDir::prepare_new_fragment(bool replay)
889 if (!replay && is_auth()) {
893 inode->add_dirfrag(this);
896 void CDir::finish_old_fragment(list<MDSInternalContextBase*>& waiters, bool replay)
898 // take waiters _before_ unfreeze...
900 take_waiting(WAIT_ANY_MASK, waiters);
902 auth_unpin(this); // pinned in prepare_old_fragment
903 assert(is_frozen_dir());
908 assert(nested_auth_pins == 0);
909 assert(dir_auth_pins == 0);
910 assert(auth_pins == 0);
912 num_head_items = num_head_null = 0;
913 num_snap_items = num_snap_null = 0;
915 // this mirrors init_fragment_pins()
920 if (state_test(STATE_IMPORTBOUND))
921 put(PIN_IMPORTBOUND);
922 if (state_test(STATE_EXPORTBOUND))
923 put(PIN_EXPORTBOUND);
924 if (is_subtree_root())
930 assert(get_num_ref() == (state_test(STATE_STICKY) ? 1:0));
933 void CDir::init_fragment_pins()
937 if (state_test(STATE_DIRTY))
939 if (state_test(STATE_EXPORTBOUND))
940 get(PIN_EXPORTBOUND);
941 if (state_test(STATE_IMPORTBOUND))
942 get(PIN_IMPORTBOUND);
943 if (is_subtree_root())
947 void CDir::split(int bits, list<CDir*>& subs, list<MDSInternalContextBase*>& waiters, bool replay)
949 dout(10) << "split by " << bits << " bits on " << *this << dendl;
951 assert(replay || is_complete() || !is_auth());
954 frag.split(bits, frags);
956 vector<CDir*> subfrags(1 << bits);
958 double fac = 1.0 / (double)(1 << bits); // for scaling load vecs
960 version_t rstat_version = inode->get_projected_inode()->rstat.version;
961 version_t dirstat_version = inode->get_projected_inode()->dirstat.version;
963 nest_info_t rstatdiff;
964 frag_info_t fragstatdiff;
965 if (fnode.accounted_rstat.version == rstat_version)
966 rstatdiff.add_delta(fnode.accounted_rstat, fnode.rstat);
967 if (fnode.accounted_fragstat.version == dirstat_version)
968 fragstatdiff.add_delta(fnode.accounted_fragstat, fnode.fragstat);
969 dout(10) << " rstatdiff " << rstatdiff << " fragstatdiff " << fragstatdiff << dendl;
971 map<string_snap_t, std::list<MDSInternalContextBase*> > dentry_waiters;
972 prepare_old_fragment(dentry_waiters, replay);
974 // create subfrag dirs
976 for (list<frag_t>::iterator p = frags.begin(); p != frags.end(); ++p) {
977 CDir *f = new CDir(inode, *p, cache, is_auth());
978 f->state_set(state & (MASK_STATE_FRAGMENT_KEPT | STATE_COMPLETE));
979 f->get_replicas() = get_replicas();
980 f->dir_auth = dir_auth;
981 f->init_fragment_pins();
982 f->set_version(get_version());
985 f->pop_me.scale(fac);
987 // FIXME; this is an approximation
988 f->pop_nested = pop_nested;
989 f->pop_nested.scale(fac);
990 f->pop_auth_subtree = pop_auth_subtree;
991 f->pop_auth_subtree.scale(fac);
992 f->pop_auth_subtree_nested = pop_auth_subtree_nested;
993 f->pop_auth_subtree_nested.scale(fac);
995 dout(10) << " subfrag " << *p << " " << *f << dendl;
999 f->set_dir_auth(get_dir_auth());
1000 f->prepare_new_fragment(replay);
1003 // repartition dentries
1004 while (!items.empty()) {
1005 CDir::map_t::iterator p = items.begin();
1007 CDentry *dn = p->second;
1008 frag_t subfrag = inode->pick_dirfrag(dn->name);
1009 int n = (subfrag.value() & (subfrag.mask() ^ frag.mask())) >> subfrag.mask_shift();
1010 dout(15) << " subfrag " << subfrag << " n=" << n << " for " << p->first << dendl;
1011 CDir *f = subfrags[n];
1012 f->steal_dentry(dn);
1015 for (auto& p : dentry_waiters) {
1016 frag_t subfrag = inode->pick_dirfrag(p.first.name);
1017 int n = (subfrag.value() & (subfrag.mask() ^ frag.mask())) >> subfrag.mask_shift();
1018 CDir *f = subfrags[n];
1020 if (f->waiting_on_dentry.empty())
1021 f->get(PIN_DNWAITER);
1022 f->waiting_on_dentry[p.first].swap(p.second);
1025 // FIXME: handle dirty old rstat
1027 // fix up new frag fragstats
1028 for (int i=0; i<n; i++) {
1029 CDir *f = subfrags[i];
1030 f->fnode.rstat.version = rstat_version;
1031 f->fnode.accounted_rstat = f->fnode.rstat;
1032 f->fnode.fragstat.version = dirstat_version;
1033 f->fnode.accounted_fragstat = f->fnode.fragstat;
1034 dout(10) << " rstat " << f->fnode.rstat << " fragstat " << f->fnode.fragstat
1035 << " on " << *f << dendl;
1038 // give any outstanding frag stat differential to first frag
1039 dout(10) << " giving rstatdiff " << rstatdiff << " fragstatdiff" << fragstatdiff
1040 << " to " << *subfrags[0] << dendl;
1041 subfrags[0]->fnode.accounted_rstat.add(rstatdiff);
1042 subfrags[0]->fnode.accounted_fragstat.add(fragstatdiff);
1044 finish_old_fragment(waiters, replay);
1047 void CDir::merge(list<CDir*>& subs, list<MDSInternalContextBase*>& waiters, bool replay)
1049 dout(10) << "merge " << subs << dendl;
1051 mds_authority_t new_auth = CDIR_AUTH_DEFAULT;
1052 for (auto dir : subs) {
1053 if (dir->get_dir_auth() != CDIR_AUTH_DEFAULT &&
1054 dir->get_dir_auth() != new_auth) {
1055 assert(new_auth == CDIR_AUTH_DEFAULT);
1056 new_auth = dir->get_dir_auth();
1060 set_dir_auth(new_auth);
1061 prepare_new_fragment(replay);
1063 nest_info_t rstatdiff;
1064 frag_info_t fragstatdiff;
1065 bool touched_mtime, touched_chattr;
1066 version_t rstat_version = inode->get_projected_inode()->rstat.version;
1067 version_t dirstat_version = inode->get_projected_inode()->dirstat.version;
1069 map<string_snap_t, std::list<MDSInternalContextBase*> > dentry_waiters;
1071 for (auto dir : subs) {
1072 dout(10) << " subfrag " << dir->get_frag() << " " << *dir << dendl;
1073 assert(!dir->is_auth() || dir->is_complete() || replay);
1075 if (dir->fnode.accounted_rstat.version == rstat_version)
1076 rstatdiff.add_delta(dir->fnode.accounted_rstat, dir->fnode.rstat);
1077 if (dir->fnode.accounted_fragstat.version == dirstat_version)
1078 fragstatdiff.add_delta(dir->fnode.accounted_fragstat, dir->fnode.fragstat,
1079 &touched_mtime, &touched_chattr);
1081 dir->prepare_old_fragment(dentry_waiters, replay);
1084 while (!dir->items.empty())
1085 steal_dentry(dir->items.begin()->second);
1087 // merge replica map
1088 for (const auto &p : dir->get_replicas()) {
1089 unsigned cur = get_replicas()[p.first];
1091 get_replicas()[p.first] = p.second;
1095 if (dir->get_version() > get_version())
1096 set_version(dir->get_version());
1099 state_set(dir->get_state() & MASK_STATE_FRAGMENT_KEPT);
1101 dir->finish_old_fragment(waiters, replay);
1102 inode->close_dirfrag(dir->get_frag());
1105 if (!dentry_waiters.empty()) {
1107 for (auto& p : dentry_waiters) {
1108 waiting_on_dentry[p.first].swap(p.second);
1112 if (is_auth() && !replay)
1115 // FIXME: merge dirty old rstat
1116 fnode.rstat.version = rstat_version;
1117 fnode.accounted_rstat = fnode.rstat;
1118 fnode.accounted_rstat.add(rstatdiff);
1120 fnode.fragstat.version = dirstat_version;
1121 fnode.accounted_fragstat = fnode.fragstat;
1122 fnode.accounted_fragstat.add(fragstatdiff);
1124 init_fragment_pins();
1130 void CDir::resync_accounted_fragstat()
1132 fnode_t *pf = get_projected_fnode();
1133 inode_t *pi = inode->get_projected_inode();
1135 if (pf->accounted_fragstat.version != pi->dirstat.version) {
1136 pf->fragstat.version = pi->dirstat.version;
1137 dout(10) << "resync_accounted_fragstat " << pf->accounted_fragstat << " -> " << pf->fragstat << dendl;
1138 pf->accounted_fragstat = pf->fragstat;
1143 * resync rstat and accounted_rstat with inode
1145 void CDir::resync_accounted_rstat()
1147 fnode_t *pf = get_projected_fnode();
1148 inode_t *pi = inode->get_projected_inode();
1150 if (pf->accounted_rstat.version != pi->rstat.version) {
1151 pf->rstat.version = pi->rstat.version;
1152 dout(10) << "resync_accounted_rstat " << pf->accounted_rstat << " -> " << pf->rstat << dendl;
1153 pf->accounted_rstat = pf->rstat;
1154 dirty_old_rstat.clear();
1158 void CDir::assimilate_dirty_rstat_inodes()
1160 dout(10) << "assimilate_dirty_rstat_inodes" << dendl;
1161 for (elist<CInode*>::iterator p = dirty_rstat_inodes.begin_use_current();
1164 assert(in->is_auth());
1165 if (in->is_frozen())
1168 inode_t *pi = in->project_inode();
1169 pi->version = in->pre_dirty();
1171 inode->mdcache->project_rstat_inode_to_frag(in, this, 0, 0, NULL);
1173 state_set(STATE_ASSIMRSTAT);
1174 dout(10) << "assimilate_dirty_rstat_inodes done" << dendl;
1177 void CDir::assimilate_dirty_rstat_inodes_finish(MutationRef& mut, EMetaBlob *blob)
1179 if (!state_test(STATE_ASSIMRSTAT))
1181 state_clear(STATE_ASSIMRSTAT);
1182 dout(10) << "assimilate_dirty_rstat_inodes_finish" << dendl;
1183 elist<CInode*>::iterator p = dirty_rstat_inodes.begin_use_current();
1188 if (in->is_frozen())
1191 CDentry *dn = in->get_projected_parent_dn();
1194 mut->add_projected_inode(in);
1196 in->clear_dirty_rstat();
1197 blob->add_primary_dentry(dn, in, true);
1200 if (!dirty_rstat_inodes.empty())
1201 inode->mdcache->mds->locker->mark_updated_scatterlock(&inode->nestlock);
1207 /****************************************
1211 void CDir::add_dentry_waiter(const string& dname, snapid_t snapid, MDSInternalContextBase *c)
1213 if (waiting_on_dentry.empty())
1215 waiting_on_dentry[string_snap_t(dname, snapid)].push_back(c);
1216 dout(10) << "add_dentry_waiter dentry " << dname
1217 << " snap " << snapid
1218 << " " << c << " on " << *this << dendl;
1221 void CDir::take_dentry_waiting(const string& dname, snapid_t first, snapid_t last,
1222 list<MDSInternalContextBase*>& ls)
1224 if (waiting_on_dentry.empty())
1227 string_snap_t lb(dname, first);
1228 string_snap_t ub(dname, last);
1229 compact_map<string_snap_t, list<MDSInternalContextBase*> >::iterator p = waiting_on_dentry.lower_bound(lb);
1230 while (p != waiting_on_dentry.end() &&
1232 dout(10) << "take_dentry_waiting dentry " << dname
1233 << " [" << first << "," << last << "] found waiter on snap "
1235 << " on " << *this << dendl;
1236 ls.splice(ls.end(), p->second);
1237 waiting_on_dentry.erase(p++);
1240 if (waiting_on_dentry.empty())
1244 void CDir::take_sub_waiting(list<MDSInternalContextBase*>& ls)
1246 dout(10) << "take_sub_waiting" << dendl;
1247 if (!waiting_on_dentry.empty()) {
1248 for (compact_map<string_snap_t, list<MDSInternalContextBase*> >::iterator p = waiting_on_dentry.begin();
1249 p != waiting_on_dentry.end();
1251 ls.splice(ls.end(), p->second);
1252 waiting_on_dentry.clear();
1259 void CDir::add_waiter(uint64_t tag, MDSInternalContextBase *c)
1264 if (tag & WAIT_ATFREEZEROOT) {
1265 if (!(is_freezing_tree_root() || is_frozen_tree_root() ||
1266 is_freezing_dir() || is_frozen_dir())) {
1268 dout(10) << "add_waiter " << std::hex << tag << std::dec << " " << c << " should be ATFREEZEROOT, " << *this << " is not root, trying parent" << dendl;
1269 inode->parent->dir->add_waiter(tag, c);
1275 if (tag & WAIT_ATSUBTREEROOT) {
1276 if (!is_subtree_root()) {
1278 dout(10) << "add_waiter " << std::hex << tag << std::dec << " " << c << " should be ATSUBTREEROOT, " << *this << " is not root, trying parent" << dendl;
1279 inode->parent->dir->add_waiter(tag, c);
1284 assert(!(tag & WAIT_CREATED) || state_test(STATE_CREATING));
1286 MDSCacheObject::add_waiter(tag, c);
1291 /* NOTE: this checks dentry waiters too */
1292 void CDir::take_waiting(uint64_t mask, list<MDSInternalContextBase*>& ls)
1294 if ((mask & WAIT_DENTRY) && !waiting_on_dentry.empty()) {
1295 // take all dentry waiters
1296 while (!waiting_on_dentry.empty()) {
1297 compact_map<string_snap_t, list<MDSInternalContextBase*> >::iterator p = waiting_on_dentry.begin();
1298 dout(10) << "take_waiting dentry " << p->first.name
1299 << " snap " << p->first.snapid << " on " << *this << dendl;
1300 ls.splice(ls.end(), p->second);
1301 waiting_on_dentry.erase(p);
1307 MDSCacheObject::take_waiting(mask, ls);
1311 void CDir::finish_waiting(uint64_t mask, int result)
1313 dout(11) << "finish_waiting mask " << hex << mask << dec << " result " << result << " on " << *this << dendl;
1315 list<MDSInternalContextBase*> finished;
1316 take_waiting(mask, finished);
1318 finish_contexts(g_ceph_context, finished, result);
1320 cache->mds->queue_waiters(finished);
1327 fnode_t *CDir::project_fnode()
1329 assert(get_version() != 0);
1330 fnode_t *p = new fnode_t;
1331 *p = *get_projected_fnode();
1332 projected_fnode.push_back(p);
1334 if (scrub_infop && scrub_infop->last_scrub_dirty) {
1335 p->localized_scrub_stamp = scrub_infop->last_local.time;
1336 p->localized_scrub_version = scrub_infop->last_local.version;
1337 p->recursive_scrub_stamp = scrub_infop->last_recursive.time;
1338 p->recursive_scrub_version = scrub_infop->last_recursive.version;
1339 scrub_infop->last_scrub_dirty = false;
1340 scrub_maybe_delete_info();
1343 dout(10) << "project_fnode " << p << dendl;
1347 void CDir::pop_and_dirty_projected_fnode(LogSegment *ls)
1349 assert(!projected_fnode.empty());
1350 dout(15) << "pop_and_dirty_projected_fnode " << projected_fnode.front()
1351 << " v" << projected_fnode.front()->version << dendl;
1352 fnode = *projected_fnode.front();
1354 delete projected_fnode.front();
1355 projected_fnode.pop_front();
1359 version_t CDir::pre_dirty(version_t min)
1361 if (min > projected_version)
1362 projected_version = min;
1363 ++projected_version;
1364 dout(10) << "pre_dirty " << projected_version << dendl;
1365 return projected_version;
1368 void CDir::mark_dirty(version_t pv, LogSegment *ls)
1370 assert(get_version() < pv);
1371 assert(pv <= projected_version);
1376 void CDir::_mark_dirty(LogSegment *ls)
1378 if (!state_test(STATE_DIRTY)) {
1379 dout(10) << "mark_dirty (was clean) " << *this << " version " << get_version() << dendl;
1383 dout(10) << "mark_dirty (already dirty) " << *this << " version " << get_version() << dendl;
1386 ls->dirty_dirfrags.push_back(&item_dirty);
1388 // if i've never committed, i need to be before _any_ mention of me is trimmed from the journal.
1389 if (committed_version == 0 && !item_new.is_on_list())
1390 ls->new_dirfrags.push_back(&item_new);
1394 void CDir::mark_new(LogSegment *ls)
1396 ls->new_dirfrags.push_back(&item_new);
1397 state_clear(STATE_CREATING);
1399 list<MDSInternalContextBase*> waiters;
1400 take_waiting(CDir::WAIT_CREATED, waiters);
1401 cache->mds->queue_waiters(waiters);
1404 void CDir::mark_clean()
1406 dout(10) << "mark_clean " << *this << " version " << get_version() << dendl;
1407 if (state_test(STATE_DIRTY)) {
1408 item_dirty.remove_myself();
1409 item_new.remove_myself();
1411 state_clear(STATE_DIRTY);
1416 // caller should hold auth pin of this
1417 void CDir::log_mark_dirty()
1419 if (is_dirty() || is_projected())
1420 return; // noop if it is already dirty or will be dirty
1422 version_t pv = pre_dirty();
1423 mark_dirty(pv, cache->mds->mdlog->get_current_segment());
1426 void CDir::mark_complete() {
1427 state_set(STATE_COMPLETE);
1431 void CDir::first_get()
1433 inode->get(CInode::PIN_DIRFRAG);
1436 void CDir::last_put()
1438 inode->put(CInode::PIN_DIRFRAG);
1443 /******************************************************************************
1447 // -----------------------
1449 void CDir::fetch(MDSInternalContextBase *c, bool ignore_authpinnability)
1452 return fetch(c, want, ignore_authpinnability);
1455 void CDir::fetch(MDSInternalContextBase *c, const string& want_dn, bool ignore_authpinnability)
1457 dout(10) << "fetch on " << *this << dendl;
1460 assert(!is_complete());
1462 if (!can_auth_pin() && !ignore_authpinnability) {
1464 dout(7) << "fetch waiting for authpinnable" << dendl;
1465 add_waiter(WAIT_UNFREEZE, c);
1467 dout(7) << "fetch not authpinnable and no context" << dendl;
1471 // unlinked directory inode shouldn't have any entry
1472 if (!inode->is_base() && get_parent_dir()->inode->is_stray() &&
1473 !inode->snaprealm) {
1474 dout(7) << "fetch dirfrag for unlinked directory, mark complete" << dendl;
1475 if (get_version() == 0) {
1476 assert(inode->is_auth());
1479 if (state_test(STATE_REJOINUNDEF)) {
1480 assert(cache->mds->is_rejoin());
1481 state_clear(STATE_REJOINUNDEF);
1482 cache->opened_undef_dirfrag(this);
1488 cache->mds->queue_waiter(c);
1492 if (c) add_waiter(WAIT_COMPLETE, c);
1493 if (!want_dn.empty()) wanted_items.insert(want_dn);
1495 // already fetching?
1496 if (state_test(CDir::STATE_FETCHING)) {
1497 dout(7) << "already fetching; waiting" << dendl;
1502 state_set(CDir::STATE_FETCHING);
1504 if (cache->mds->logger) cache->mds->logger->inc(l_mds_dir_fetch);
1506 std::set<dentry_key_t> empty;
1507 _omap_fetch(NULL, empty);
1510 void CDir::fetch(MDSInternalContextBase *c, const std::set<dentry_key_t>& keys)
1512 dout(10) << "fetch " << keys.size() << " keys on " << *this << dendl;
1515 assert(!is_complete());
1517 if (!can_auth_pin()) {
1518 dout(7) << "fetch keys waiting for authpinnable" << dendl;
1519 add_waiter(WAIT_UNFREEZE, c);
1522 if (state_test(CDir::STATE_FETCHING)) {
1523 dout(7) << "fetch keys waiting for full fetch" << dendl;
1524 add_waiter(WAIT_COMPLETE, c);
1529 if (cache->mds->logger) cache->mds->logger->inc(l_mds_dir_fetch);
1531 _omap_fetch(c, keys);
1534 class C_IO_Dir_OMAP_FetchedMore : public CDirIOContext {
1535 MDSInternalContextBase *fin;
1539 map<string, bufferlist> omap; ///< carry-over from before
1540 map<string, bufferlist> omap_more; ///< new batch
1542 C_IO_Dir_OMAP_FetchedMore(CDir *d, MDSInternalContextBase *f) :
1543 CDirIOContext(d), fin(f), ret(0) { }
1544 void finish(int r) {
1547 omap.swap(omap_more);
1549 omap.insert(omap_more.begin(), omap_more.end());
1552 dir->_omap_fetch_more(hdrbl, omap, fin);
1554 dir->_omap_fetched(hdrbl, omap, !fin, r);
1561 class C_IO_Dir_OMAP_Fetched : public CDirIOContext {
1562 MDSInternalContextBase *fin;
1566 map<string, bufferlist> omap;
1568 int ret1, ret2, ret3;
1570 C_IO_Dir_OMAP_Fetched(CDir *d, MDSInternalContextBase *f) :
1571 CDirIOContext(d), fin(f), ret1(0), ret2(0), ret3(0) { }
1572 void finish(int r) override {
1573 // check the correctness of backtrace
1574 if (r >= 0 && ret3 != -ECANCELED)
1575 dir->inode->verify_diri_backtrace(btbl, ret3);
1576 if (r >= 0) r = ret1;
1577 if (r >= 0) r = ret2;
1579 dir->_omap_fetch_more(hdrbl, omap, fin);
1581 dir->_omap_fetched(hdrbl, omap, !fin, r);
1588 void CDir::_omap_fetch(MDSInternalContextBase *c, const std::set<dentry_key_t>& keys)
1590 C_IO_Dir_OMAP_Fetched *fin = new C_IO_Dir_OMAP_Fetched(this, c);
1591 object_t oid = get_ondisk_object();
1592 object_locator_t oloc(cache->mds->mdsmap->get_metadata_pool());
1594 rd.omap_get_header(&fin->hdrbl, &fin->ret1);
1597 rd.omap_get_vals("", "", g_conf->mds_dir_keys_per_op,
1598 &fin->omap, &fin->more, &fin->ret2);
1601 std::set<std::string> str_keys;
1602 for (auto p = keys.begin(); p != keys.end(); ++p) {
1605 str_keys.insert(str);
1607 rd.omap_get_vals_by_keys(str_keys, &fin->omap, &fin->ret2);
1609 // check the correctness of backtrace
1610 if (g_conf->mds_verify_backtrace > 0 && frag == frag_t()) {
1611 rd.getxattr("parent", &fin->btbl, &fin->ret3);
1612 rd.set_last_op_flags(CEPH_OSD_OP_FLAG_FAILOK);
1614 fin->ret3 = -ECANCELED;
1617 cache->mds->objecter->read(oid, oloc, rd, CEPH_NOSNAP, NULL, 0,
1618 new C_OnFinisher(fin, cache->mds->finisher));
1621 void CDir::_omap_fetch_more(
1623 map<string, bufferlist>& omap,
1624 MDSInternalContextBase *c)
1626 // we have more omap keys to fetch!
1627 object_t oid = get_ondisk_object();
1628 object_locator_t oloc(cache->mds->mdsmap->get_metadata_pool());
1629 C_IO_Dir_OMAP_FetchedMore *fin = new C_IO_Dir_OMAP_FetchedMore(this, c);
1630 fin->hdrbl.claim(hdrbl);
1631 fin->omap.swap(omap);
1633 rd.omap_get_vals(fin->omap.rbegin()->first,
1634 "", /* filter prefix */
1635 g_conf->mds_dir_keys_per_op,
1639 cache->mds->objecter->read(oid, oloc, rd, CEPH_NOSNAP, NULL, 0,
1640 new C_OnFinisher(fin, cache->mds->finisher));
1643 CDentry *CDir::_load_dentry(
1644 const std::string &key,
1645 const std::string &dname,
1646 const snapid_t last,
1649 const std::set<snapid_t> *snaps,
1651 list<CInode*> *undef_inodes)
1653 bufferlist::iterator q = bl.begin();
1662 dout(20) << "_fetched pos " << pos << " marker '" << type << "' dname '" << dname
1663 << " [" << first << "," << last << "]"
1667 if (snaps && last != CEPH_NOSNAP) {
1668 set<snapid_t>::const_iterator p = snaps->lower_bound(first);
1669 if (p == snaps->end() || *p > last) {
1670 dout(10) << " skipping stale dentry on [" << first << "," << last << "]" << dendl;
1676 * look for existing dentry for _last_ snap, because unlink +
1677 * create may leave a "hole" (epochs during which the dentry
1678 * doesn't exist) but for which no explicit negative dentry is in
1683 dn = lookup_exact_snap(dname, last);
1685 dn = lookup(dname, last);
1690 unsigned char d_type;
1692 ::decode(d_type, q);
1696 stale_items.insert(key);
1697 *force_dirty = true;
1703 if (dn->get_linkage()->get_inode() == 0) {
1704 dout(12) << "_fetched had NEG dentry " << *dn << dendl;
1706 dout(12) << "_fetched had dentry " << *dn << dendl;
1710 dn = add_remote_dentry(dname, ino, d_type, first, last);
1713 CInode *in = cache->get_inode(ino); // we may or may not have it.
1715 dn->link_remote(dn->get_linkage(), in);
1716 dout(12) << "_fetched got remote link " << ino << " which we have " << *in << dendl;
1718 dout(12) << "_fetched got remote link " << ino << " (dont' have it)" << dendl;
1722 else if (type == 'I') {
1725 // Load inode data before looking up or constructing CInode
1726 InodeStore inode_data;
1727 inode_data.decode_bare(q);
1731 stale_items.insert(key);
1732 *force_dirty = true;
1737 bool undef_inode = false;
1739 CInode *in = dn->get_linkage()->get_inode();
1741 dout(12) << "_fetched had dentry " << *dn << dendl;
1742 if (in->state_test(CInode::STATE_REJOINUNDEF)) {
1743 undef_inodes->push_back(in);
1747 dout(12) << "_fetched had NEG dentry " << *dn << dendl;
1750 if (!dn || undef_inode) {
1752 CInode *in = cache->get_inode(inode_data.inode.ino, last);
1753 if (!in || undef_inode) {
1754 if (undef_inode && in)
1757 in = new CInode(cache, true, first, last);
1759 in->inode = inode_data.inode;
1761 if (in->is_symlink())
1762 in->symlink = inode_data.symlink;
1764 in->dirfragtree.swap(inode_data.dirfragtree);
1765 in->xattrs.swap(inode_data.xattrs);
1766 in->old_inodes.swap(inode_data.old_inodes);
1767 if (!in->old_inodes.empty()) {
1768 snapid_t min_first = in->old_inodes.rbegin()->first + 1;
1769 if (min_first > in->first)
1770 in->first = min_first;
1773 in->oldest_snap = inode_data.oldest_snap;
1774 in->decode_snap_blob(inode_data.snap_blob);
1775 if (snaps && !in->snaprealm)
1776 in->purge_stale_snap_data(*snaps);
1779 cache->add_inode(in); // add
1780 dn = add_primary_dentry(dname, in, first, last); // link
1782 dout(12) << "_fetched got " << *dn << " " << *in << dendl;
1784 if (in->inode.is_dirty_rstat())
1785 in->mark_dirty_rstat();
1787 //in->hack_accessed = false;
1788 //in->hack_load_stamp = ceph_clock_now();
1789 //num_new_inodes_loaded++;
1791 dout(0) << "_fetched badness: got (but i already had) " << *in
1792 << " mode " << in->inode.mode
1793 << " mtime " << in->inode.mtime << dendl;
1794 string dirpath, inopath;
1795 this->inode->make_path_string(dirpath);
1796 in->make_path_string(inopath);
1797 cache->mds->clog->error() << "loaded dup inode " << inode_data.inode.ino
1798 << " [" << first << "," << last << "] v" << inode_data.inode.version
1799 << " at " << dirpath << "/" << dname
1800 << ", but inode " << in->vino() << " v" << in->inode.version
1801 << " already exists at " << inopath;
1806 std::ostringstream oss;
1807 oss << "Invalid tag char '" << type << "' pos " << pos;
1808 throw buffer::malformed_input(oss.str());
1814 void CDir::_omap_fetched(bufferlist& hdrbl, map<string, bufferlist>& omap,
1815 bool complete, int r)
1817 LogChannelRef clog = cache->mds->clog;
1818 dout(10) << "_fetched header " << hdrbl.length() << " bytes "
1819 << omap.size() << " keys for " << *this << dendl;
1821 assert(r == 0 || r == -ENOENT || r == -ENODATA);
1823 assert(!is_frozen());
1825 if (hdrbl.length() == 0) {
1826 dout(0) << "_fetched missing object for " << *this << dendl;
1828 clog->error() << "dir " << dirfrag() << " object missing on disk; some "
1829 "files may be lost (" << get_path() << ")";
1837 bufferlist::iterator p = hdrbl.begin();
1839 ::decode(got_fnode, p);
1840 } catch (const buffer::error &err) {
1841 derr << "Corrupt fnode in dirfrag " << dirfrag()
1842 << ": " << err << dendl;
1843 clog->warn() << "Corrupt fnode header in " << dirfrag() << ": "
1844 << err << " (" << get_path() << ")";
1849 clog->warn() << "header buffer of dir " << dirfrag() << " has "
1850 << hdrbl.length() - p.get_off() << " extra bytes ("
1851 << get_path() << ")";
1857 dout(10) << "_fetched version " << got_fnode.version << dendl;
1859 // take the loaded fnode?
1860 // only if we are a fresh CDir* with no prior state.
1861 if (get_version() == 0) {
1862 assert(!is_projected());
1863 assert(!state_test(STATE_COMMITTING));
1865 projected_version = committing_version = committed_version = got_fnode.version;
1867 if (state_test(STATE_REJOINUNDEF)) {
1868 assert(cache->mds->is_rejoin());
1869 state_clear(STATE_REJOINUNDEF);
1870 cache->opened_undef_dirfrag(this);
1874 list<CInode*> undef_inodes;
1876 // purge stale snaps?
1877 // only if we have past_parents open!
1878 bool force_dirty = false;
1879 const set<snapid_t> *snaps = NULL;
1880 SnapRealm *realm = inode->find_snaprealm();
1881 if (!realm->have_past_parents_open()) {
1882 dout(10) << " no snap purge, one or more past parents NOT open" << dendl;
1883 } else if (fnode.snap_purged_thru < realm->get_last_destroyed()) {
1884 snaps = &realm->get_snaps();
1885 dout(10) << " snap_purged_thru " << fnode.snap_purged_thru
1886 << " < " << realm->get_last_destroyed()
1887 << ", snap purge based on " << *snaps << dendl;
1888 if (get_num_snap_items() == 0) {
1889 fnode.snap_purged_thru = realm->get_last_destroyed();
1894 unsigned pos = omap.size() - 1;
1895 for (map<string, bufferlist>::reverse_iterator p = omap.rbegin();
1900 dentry_key_t::decode_helper(p->first, dname, last);
1905 p->first, dname, last, p->second, pos, snaps,
1906 &force_dirty, &undef_inodes);
1907 } catch (const buffer::error &err) {
1908 cache->mds->clog->warn() << "Corrupt dentry '" << dname << "' in "
1909 "dir frag " << dirfrag() << ": "
1910 << err << "(" << get_path() << ")";
1912 // Remember that this dentry is damaged. Subsequent operations
1913 // that try to act directly on it will get their EIOs, but this
1914 // dirfrag as a whole will continue to look okay (minus the
1915 // mysteriously-missing dentry)
1916 go_bad_dentry(last, dname);
1918 // Anyone who was WAIT_DENTRY for this guy will get kicked
1919 // to RetryRequest, and hit the DamageTable-interrogating path.
1920 // Stats will now be bogus because we will think we're complete,
1921 // but have 1 or more missing dentries.
1925 if (dn && (wanted_items.count(dname) > 0 || !complete)) {
1926 dout(10) << " touching wanted dn " << *dn << dendl;
1927 inode->mdcache->touch_dentry(dn);
1930 /** clean underwater item?
1931 * Underwater item is something that is dirty in our cache from
1932 * journal replay, but was previously flushed to disk before the
1935 * We only do this is committed_version == 0. that implies either
1936 * - this is a fetch after from a clean/empty CDir is created
1937 * (and has no effect, since the dn won't exist); or
1938 * - this is a fetch after _recovery_, which is what we're worried
1939 * about. Items that are marked dirty from the journal should be
1940 * marked clean if they appear on disk.
1942 if (committed_version == 0 &&
1944 dn->get_version() <= got_fnode.version &&
1946 dout(10) << "_fetched had underwater dentry " << *dn << ", marking clean" << dendl;
1949 if (dn->get_linkage()->is_primary()) {
1950 assert(dn->get_linkage()->get_inode()->get_version() <= got_fnode.version);
1951 dout(10) << "_fetched had underwater inode " << *dn->get_linkage()->get_inode() << ", marking clean" << dendl;
1952 dn->get_linkage()->get_inode()->mark_clean();
1957 //cache->mds->logger->inc("newin", num_new_inodes_loaded);
1959 // mark complete, !fetching
1961 wanted_items.clear();
1963 state_clear(STATE_FETCHING);
1965 if (scrub_infop && scrub_infop->need_scrub_local) {
1966 scrub_infop->need_scrub_local = false;
1971 // open & force frags
1972 while (!undef_inodes.empty()) {
1973 CInode *in = undef_inodes.front();
1974 undef_inodes.pop_front();
1975 in->state_clear(CInode::STATE_REJOINUNDEF);
1976 cache->opened_undef_inode(in);
1979 // dirty myself to remove stale snap dentries
1980 if (force_dirty && !inode->mdcache->is_readonly())
1987 finish_waiting(WAIT_COMPLETE, 0);
1991 void CDir::_go_bad()
1993 if (get_version() == 0)
1995 state_set(STATE_BADFRAG);
1996 // mark complete, !fetching
1998 state_clear(STATE_FETCHING);
2002 finish_waiting(WAIT_COMPLETE, -EIO);
2005 void CDir::go_bad_dentry(snapid_t last, const std::string &dname)
2007 dout(10) << "go_bad_dentry " << dname << dendl;
2008 const bool fatal = cache->mds->damage_table.notify_dentry(
2009 inode->ino(), frag, last, dname, get_path() + "/" + dname);
2011 cache->mds->damaged();
2012 ceph_abort(); // unreachable, damaged() respawns us
2016 void CDir::go_bad(bool complete)
2018 dout(10) << "go_bad " << frag << dendl;
2019 const bool fatal = cache->mds->damage_table.notify_dirfrag(
2020 inode->ino(), frag, get_path());
2022 cache->mds->damaged();
2023 ceph_abort(); // unreachable, damaged() respawns us
2032 // -----------------------
2038 * @param want - min version i want committed
2039 * @param c - callback for completion
2041 void CDir::commit(version_t want, MDSInternalContextBase *c, bool ignore_authpinnability, int op_prio)
2043 dout(10) << "commit want " << want << " on " << *this << dendl;
2044 if (want == 0) want = get_version();
2047 assert(want <= get_version() || get_version() == 0); // can't commit the future
2048 assert(want > committed_version); // the caller is stupid
2050 assert(ignore_authpinnability || can_auth_pin());
2052 // note: queue up a noop if necessary, so that we always
2055 c = new C_MDSInternalNoop;
2057 // auth_pin on first waiter
2058 if (waiting_for_commit.empty())
2060 waiting_for_commit[want].push_back(c);
2063 _commit(want, op_prio);
2066 class C_IO_Dir_Committed : public CDirIOContext {
2069 C_IO_Dir_Committed(CDir *d, version_t v) : CDirIOContext(d), version(v) { }
2070 void finish(int r) override {
2071 dir->_committed(r, version);
2076 * Flush out the modified dentries in this dir. Keep the bufferlist
2077 * below max_write_size;
2079 void CDir::_omap_commit(int op_prio)
2081 dout(10) << "_omap_commit" << dendl;
2083 unsigned max_write_size = cache->max_dir_commit_size;
2084 unsigned write_size = 0;
2087 op_prio = CEPH_MSG_PRIO_DEFAULT;
2090 const set<snapid_t> *snaps = NULL;
2091 SnapRealm *realm = inode->find_snaprealm();
2092 if (!realm->have_past_parents_open()) {
2093 dout(10) << " no snap purge, one or more past parents NOT open" << dendl;
2094 } else if (fnode.snap_purged_thru < realm->get_last_destroyed()) {
2095 snaps = &realm->get_snaps();
2096 dout(10) << " snap_purged_thru " << fnode.snap_purged_thru
2097 << " < " << realm->get_last_destroyed()
2098 << ", snap purge based on " << *snaps << dendl;
2099 // fnode.snap_purged_thru = realm->get_last_destroyed();
2102 set<string> to_remove;
2103 map<string, bufferlist> to_set;
2105 C_GatherBuilder gather(g_ceph_context,
2106 new C_OnFinisher(new C_IO_Dir_Committed(this,
2108 cache->mds->finisher));
2111 object_t oid = get_ondisk_object();
2112 object_locator_t oloc(cache->mds->mdsmap->get_metadata_pool());
2114 if (!stale_items.empty()) {
2115 for (compact_set<string>::iterator p = stale_items.begin();
2116 p != stale_items.end();
2118 to_remove.insert(*p);
2119 write_size += (*p).length();
2121 stale_items.clear();
2124 for (map_t::iterator p = items.begin();
2125 p != items.end(); ) {
2126 CDentry *dn = p->second;
2130 dn->key().encode(key);
2132 if (dn->last != CEPH_NOSNAP &&
2133 snaps && try_trim_snap_dentry(dn, *snaps)) {
2134 dout(10) << " rm " << key << dendl;
2135 write_size += key.length();
2136 to_remove.insert(key);
2140 if (!dn->is_dirty() &&
2141 (!dn->state_test(CDentry::STATE_FRAGMENTING) || dn->get_linkage()->is_null()))
2142 continue; // skip clean dentries
2144 if (dn->get_linkage()->is_null()) {
2145 dout(10) << " rm " << dn->name << " " << *dn << dendl;
2146 write_size += key.length();
2147 to_remove.insert(key);
2149 dout(10) << " set " << dn->name << " " << *dn << dendl;
2151 _encode_dentry(dn, dnbl, snaps);
2152 write_size += key.length() + dnbl.length();
2153 to_set[key].swap(dnbl);
2156 if (write_size >= max_write_size) {
2158 op.priority = op_prio;
2160 // don't create new dirfrag blindly
2161 if (!is_new() && !state_test(CDir::STATE_FRAGMENTING))
2162 op.stat(NULL, (ceph::real_time*) NULL, NULL);
2164 if (!to_set.empty())
2165 op.omap_set(to_set);
2166 if (!to_remove.empty())
2167 op.omap_rm_keys(to_remove);
2169 cache->mds->objecter->mutate(oid, oloc, op, snapc,
2170 ceph::real_clock::now(),
2171 0, gather.new_sub());
2180 op.priority = op_prio;
2182 // don't create new dirfrag blindly
2183 if (!is_new() && !state_test(CDir::STATE_FRAGMENTING))
2184 op.stat(NULL, (ceph::real_time*)NULL, NULL);
2187 * save the header at the last moment.. If we were to send it off before other
2188 * updates, but die before sending them all, we'd think that the on-disk state
2189 * was fully committed even though it wasn't! However, since the messages are
2190 * strictly ordered between the MDS and the OSD, and since messages to a given
2191 * PG are strictly ordered, if we simply send the message containing the header
2192 * off last, we cannot get our header into an incorrect state.
2195 ::encode(fnode, header);
2196 op.omap_set_header(header);
2198 if (!to_set.empty())
2199 op.omap_set(to_set);
2200 if (!to_remove.empty())
2201 op.omap_rm_keys(to_remove);
2203 cache->mds->objecter->mutate(oid, oloc, op, snapc,
2204 ceph::real_clock::now(),
2205 0, gather.new_sub());
2210 void CDir::_encode_dentry(CDentry *dn, bufferlist& bl,
2211 const set<snapid_t> *snaps)
2213 // clear dentry NEW flag, if any. we can no longer silently drop it.
2216 ::encode(dn->first, bl);
2218 // primary or remote?
2219 if (dn->linkage.is_remote()) {
2220 inodeno_t ino = dn->linkage.get_remote_ino();
2221 unsigned char d_type = dn->linkage.get_remote_d_type();
2222 dout(14) << " pos " << bl.length() << " dn '" << dn->name << "' remote ino " << ino << dendl;
2224 // marker, name, ino
2225 bl.append('L'); // remote link
2227 ::encode(d_type, bl);
2228 } else if (dn->linkage.is_primary()) {
2230 CInode *in = dn->linkage.get_inode();
2233 dout(14) << " pos " << bl.length() << " dn '" << dn->name << "' inode " << *in << dendl;
2235 // marker, name, inode, [symlink string]
2236 bl.append('I'); // inode
2238 if (in->is_multiversion()) {
2239 if (!in->snaprealm) {
2241 in->purge_stale_snap_data(*snaps);
2242 } else if (in->snaprealm->have_past_parents_open()) {
2243 in->purge_stale_snap_data(in->snaprealm->get_snaps());
2247 bufferlist snap_blob;
2248 in->encode_snap_blob(snap_blob);
2249 in->encode_bare(bl, cache->mds->mdsmap->get_up_features(), &snap_blob);
2251 assert(!dn->linkage.is_null());
2255 void CDir::_commit(version_t want, int op_prio)
2257 dout(10) << "_commit want " << want << " on " << *this << dendl;
2259 // we can't commit things in the future.
2260 // (even the projected future.)
2261 assert(want <= get_version() || get_version() == 0);
2263 // check pre+postconditions.
2266 // already committed?
2267 if (committed_version >= want) {
2268 dout(10) << "already committed " << committed_version << " >= " << want << dendl;
2271 // already committing >= want?
2272 if (committing_version >= want) {
2273 dout(10) << "already committing " << committing_version << " >= " << want << dendl;
2274 assert(state_test(STATE_COMMITTING));
2278 // alrady committed an older version?
2279 if (committing_version > committed_version) {
2280 dout(10) << "already committing older " << committing_version << ", waiting for that to finish" << dendl;
2285 committing_version = get_version();
2287 // mark committing (if not already)
2288 if (!state_test(STATE_COMMITTING)) {
2289 dout(10) << "marking committing" << dendl;
2290 state_set(STATE_COMMITTING);
2293 if (cache->mds->logger) cache->mds->logger->inc(l_mds_dir_commit);
2295 _omap_commit(op_prio);
2302 * @param v version i just committed
2304 void CDir::_committed(int r, version_t v)
2307 // the directory could be partly purged during MDS failover
2308 if (r == -ENOENT && committed_version == 0 &&
2309 !inode->is_base() && get_parent_dir()->inode->is_stray()) {
2311 if (inode->snaprealm)
2312 inode->state_set(CInode::STATE_MISSINGOBJS);
2315 dout(1) << "commit error " << r << " v " << v << dendl;
2316 cache->mds->clog->error() << "failed to commit dir " << dirfrag() << " object,"
2318 cache->mds->handle_write_error(r);
2323 dout(10) << "_committed v " << v << " on " << *this << dendl;
2326 bool stray = inode->is_stray();
2329 assert(v > committed_version);
2330 assert(v <= committing_version);
2331 committed_version = v;
2333 // _all_ commits done?
2334 if (committing_version == committed_version)
2335 state_clear(CDir::STATE_COMMITTING);
2337 // _any_ commit, even if we've been redirtied, means we're no longer new.
2338 item_new.remove_myself();
2341 if (committed_version == get_version())
2345 for (map_t::iterator it = items.begin();
2346 it != items.end(); ) {
2347 CDentry *dn = it->second;
2351 if (dn->linkage.is_primary()) {
2352 CInode *in = dn->linkage.get_inode();
2354 assert(in->is_auth());
2356 if (committed_version >= in->get_version()) {
2357 if (in->is_dirty()) {
2358 dout(15) << " dir " << committed_version << " >= inode " << in->get_version() << " now clean " << *in << dendl;
2362 dout(15) << " dir " << committed_version << " < inode " << in->get_version() << " still dirty " << *in << dendl;
2363 assert(in->is_dirty() || in->last < CEPH_NOSNAP); // special case for cow snap items (not predirtied)
2368 if (committed_version >= dn->get_version()) {
2369 if (dn->is_dirty()) {
2370 dout(15) << " dir " << committed_version << " >= dn " << dn->get_version() << " now clean " << *dn << dendl;
2373 // drop clean null stray dentries immediately
2375 dn->get_num_ref() == 0 &&
2376 !dn->is_projected() &&
2377 dn->get_linkage()->is_null())
2381 dout(15) << " dir " << committed_version << " < dn " << dn->get_version() << " still dirty " << *dn << dendl;
2386 bool were_waiters = !waiting_for_commit.empty();
2388 compact_map<version_t, list<MDSInternalContextBase*> >::iterator p = waiting_for_commit.begin();
2389 while (p != waiting_for_commit.end()) {
2390 compact_map<version_t, list<MDSInternalContextBase*> >::iterator n = p;
2392 if (p->first > committed_version) {
2393 dout(10) << " there are waiters for " << p->first << ", committing again" << dendl;
2394 _commit(p->first, -1);
2397 cache->mds->queue_waiters(p->second);
2398 waiting_for_commit.erase(p);
2402 // try drop dentries in this dirfrag if it's about to be purged
2403 if (!inode->is_base() && get_parent_dir()->inode->is_stray() &&
2405 cache->maybe_eval_stray(inode, true);
2407 // unpin if we kicked the last waiter.
2409 waiting_for_commit.empty())
2418 void CDir::encode_export(bufferlist& bl)
2420 assert(!is_projected());
2421 ::encode(first, bl);
2422 ::encode(fnode, bl);
2423 ::encode(dirty_old_rstat, bl);
2424 ::encode(committed_version, bl);
2426 ::encode(state, bl);
2427 ::encode(dir_rep, bl);
2429 ::encode(pop_me, bl);
2430 ::encode(pop_auth_subtree, bl);
2432 ::encode(dir_rep_by, bl);
2433 ::encode(get_replicas(), bl);
2435 get(PIN_TEMPEXPORTING);
2438 void CDir::finish_export(utime_t now)
2440 state &= MASK_STATE_EXPORT_KEPT;
2441 pop_auth_subtree_nested.sub(now, cache->decayrate, pop_auth_subtree);
2443 pop_auth_subtree.zero(now);
2444 put(PIN_TEMPEXPORTING);
2445 dirty_old_rstat.clear();
2448 void CDir::decode_import(bufferlist::iterator& blp, utime_t now, LogSegment *ls)
2450 ::decode(first, blp);
2451 ::decode(fnode, blp);
2452 ::decode(dirty_old_rstat, blp);
2453 projected_version = fnode.version;
2454 ::decode(committed_version, blp);
2455 committing_version = committed_version;
2459 state &= MASK_STATE_IMPORT_KEPT;
2460 state_set(STATE_AUTH | (s & MASK_STATE_EXPORTED));
2467 ::decode(dir_rep, blp);
2469 ::decode(pop_me, now, blp);
2470 ::decode(pop_auth_subtree, now, blp);
2471 pop_auth_subtree_nested.add(now, cache->decayrate, pop_auth_subtree);
2473 ::decode(dir_rep_by, blp);
2474 ::decode(get_replicas(), blp);
2475 if (is_replicated()) get(PIN_REPLICATED);
2477 replica_nonce = 0; // no longer defined
2479 // did we import some dirty scatterlock data?
2480 if (dirty_old_rstat.size() ||
2481 !(fnode.rstat == fnode.accounted_rstat)) {
2482 cache->mds->locker->mark_updated_scatterlock(&inode->nestlock);
2483 ls->dirty_dirfrag_nest.push_back(&inode->item_dirty_dirfrag_nest);
2485 if (!(fnode.fragstat == fnode.accounted_fragstat)) {
2486 cache->mds->locker->mark_updated_scatterlock(&inode->filelock);
2487 ls->dirty_dirfrag_dir.push_back(&inode->item_dirty_dirfrag_dir);
2489 if (is_dirty_dft()) {
2490 if (inode->dirfragtreelock.get_state() != LOCK_MIX &&
2491 inode->dirfragtreelock.is_stable()) {
2492 // clear stale dirtydft
2493 state_clear(STATE_DIRTYDFT);
2495 cache->mds->locker->mark_updated_scatterlock(&inode->dirfragtreelock);
2496 ls->dirty_dirfrag_dirfragtree.push_back(&inode->item_dirty_dirfrag_dirfragtree);
2504 /********************************
2509 * if dir_auth.first == parent, auth is same as inode.
2510 * unless .second != unknown, in which case that sticks.
2512 mds_authority_t CDir::authority() const
2514 if (is_subtree_root())
2517 return inode->authority();
2520 /** is_subtree_root()
2521 * true if this is an auth delegation point.
2522 * that is, dir_auth != default (parent,unknown)
2524 * some key observations:
2526 * - any region bound will be an export, or frozen.
2528 * note that this DOES heed dir_auth.pending
2531 bool CDir::is_subtree_root()
2533 if (dir_auth == CDIR_AUTH_DEFAULT) {
2534 //dout(10) << "is_subtree_root false " << dir_auth << " != " << CDIR_AUTH_DEFAULT
2535 //<< " on " << ino() << dendl;
2538 //dout(10) << "is_subtree_root true " << dir_auth << " != " << CDIR_AUTH_DEFAULT
2539 //<< " on " << ino() << dendl;
2546 * true if we are x, or an ancestor of x
2548 bool CDir::contains(CDir *x)
2553 x = x->get_inode()->get_projected_parent_dir();
2563 void CDir::set_dir_auth(mds_authority_t a)
2565 dout(10) << "setting dir_auth=" << a
2566 << " from " << dir_auth
2567 << " on " << *this << dendl;
2569 bool was_subtree = is_subtree_root();
2570 bool was_ambiguous = dir_auth.second >= 0;
2575 // new subtree root?
2576 if (!was_subtree && is_subtree_root()) {
2577 dout(10) << " new subtree root, adjusting auth_pins" << dendl;
2579 // adjust nested auth pins
2580 if (get_cum_auth_pins())
2581 inode->adjust_nested_auth_pins(-1, NULL);
2583 // unpin parent of frozen dir/tree?
2584 if (inode->is_auth()) {
2585 assert(!is_frozen_tree_root());
2586 if (is_frozen_dir())
2587 inode->auth_unpin(this);
2590 if (was_subtree && !is_subtree_root()) {
2591 dout(10) << " old subtree root, adjusting auth_pins" << dendl;
2593 // adjust nested auth pins
2594 if (get_cum_auth_pins())
2595 inode->adjust_nested_auth_pins(1, NULL);
2597 // pin parent of frozen dir/tree?
2598 if (inode->is_auth()) {
2599 assert(!is_frozen_tree_root());
2600 if (is_frozen_dir())
2601 inode->auth_pin(this);
2605 // newly single auth?
2606 if (was_ambiguous && dir_auth.second == CDIR_AUTH_UNKNOWN) {
2607 list<MDSInternalContextBase*> ls;
2608 take_waiting(WAIT_SINGLEAUTH, ls);
2609 cache->mds->queue_waiters(ls);
2614 /*****************************************
2615 * AUTH PINS and FREEZING
2617 * the basic plan is that auth_pins only exist in auth regions, and they
2618 * prevent a freeze (and subsequent auth change).
2620 * however, we also need to prevent a parent from freezing if a child is frozen.
2621 * for that reason, the parent inode of a frozen directory is auth_pinned.
2623 * the oddity is when the frozen directory is a subtree root. if that's the case,
2624 * the parent inode isn't frozen. which means that when subtree authority is adjusted
2625 * at the bounds, inodes for any frozen bound directories need to get auth_pins at that
2630 void CDir::auth_pin(void *by)
2636 #ifdef MDS_AUTHPIN_SET
2637 auth_pin_set.insert(by);
2640 dout(10) << "auth_pin by " << by
2642 << " count now " << auth_pins << " + " << nested_auth_pins << dendl;
2645 if (!is_subtree_root() &&
2646 get_cum_auth_pins() == 1)
2647 inode->adjust_nested_auth_pins(1, by);
2650 void CDir::auth_unpin(void *by)
2654 #ifdef MDS_AUTHPIN_SET
2655 assert(auth_pin_set.count(by));
2656 auth_pin_set.erase(auth_pin_set.find(by));
2661 dout(10) << "auth_unpin by " << by
2663 << " count now " << auth_pins << " + " << nested_auth_pins << dendl;
2664 assert(auth_pins >= 0);
2666 int newcum = get_cum_auth_pins();
2668 maybe_finish_freeze(); // pending freeze?
2671 if (!is_subtree_root() &&
2673 inode->adjust_nested_auth_pins(-1, by);
2676 void CDir::adjust_nested_auth_pins(int inc, int dirinc, void *by)
2679 nested_auth_pins += inc;
2680 dir_auth_pins += dirinc;
2682 dout(15) << "adjust_nested_auth_pins " << inc << "/" << dirinc << " on " << *this
2683 << " by " << by << " count now "
2684 << auth_pins << " + " << nested_auth_pins << dendl;
2685 assert(nested_auth_pins >= 0);
2686 assert(dir_auth_pins >= 0);
2688 int newcum = get_cum_auth_pins();
2690 maybe_finish_freeze(); // pending freeze?
2693 if (!is_subtree_root()) {
2695 inode->adjust_nested_auth_pins(-1, by);
2696 else if (newcum == inc)
2697 inode->adjust_nested_auth_pins(1, by);
2701 #ifdef MDS_VERIFY_FRAGSTAT
2702 void CDir::verify_fragstat()
2704 assert(is_complete());
2705 if (inode->is_stray())
2709 memset(&c, 0, sizeof(c));
2711 for (map_t::iterator it = items.begin();
2714 CDentry *dn = it->second;
2718 dout(10) << " " << *dn << dendl;
2719 if (dn->is_primary())
2720 dout(10) << " " << *dn->inode << dendl;
2722 if (dn->is_primary()) {
2723 if (dn->inode->is_dir())
2728 if (dn->is_remote()) {
2729 if (dn->get_remote_d_type() == DT_DIR)
2736 if (c.nsubdirs != fnode.fragstat.nsubdirs ||
2737 c.nfiles != fnode.fragstat.nfiles) {
2738 dout(0) << "verify_fragstat failed " << fnode.fragstat << " on " << *this << dendl;
2739 dout(0) << " i count " << c << dendl;
2742 dout(0) << "verify_fragstat ok " << fnode.fragstat << " on " << *this << dendl;
2747 /*****************************************************************************
2753 bool CDir::freeze_tree()
2755 assert(!is_frozen());
2756 assert(!is_freezing());
2759 if (is_freezeable(true)) {
2764 state_set(STATE_FREEZINGTREE);
2765 ++num_freezing_trees;
2766 dout(10) << "freeze_tree waiting " << *this << dendl;
2771 void CDir::_freeze_tree()
2773 dout(10) << "_freeze_tree " << *this << dendl;
2774 assert(is_freezeable(true));
2777 if (state_test(STATE_FREEZINGTREE)) {
2778 state_clear(STATE_FREEZINGTREE); // actually, this may get set again by next context?
2779 --num_freezing_trees;
2783 mds_authority_t auth;
2784 bool was_subtree = is_subtree_root();
2786 auth = get_dir_auth();
2788 // temporarily prevent parent subtree from becoming frozen.
2789 inode->auth_pin(this);
2790 // create new subtree
2794 assert(auth.first >= 0);
2795 assert(auth.second == CDIR_AUTH_UNKNOWN);
2796 auth.second = auth.first;
2797 inode->mdcache->adjust_subtree_auth(this, auth);
2799 inode->auth_unpin(this);
2802 state_set(STATE_FROZENTREE);
2807 void CDir::unfreeze_tree()
2809 dout(10) << "unfreeze_tree " << *this << dendl;
2811 if (state_test(STATE_FROZENTREE)) {
2812 // frozen. unfreeze.
2813 state_clear(STATE_FROZENTREE);
2820 assert(is_subtree_root());
2821 // for debug purpose, caller should ensure 'dir_auth.second == dir_auth.first'
2822 mds_authority_t auth = get_dir_auth();
2823 assert(auth.first >= 0);
2824 assert(auth.second == auth.first);
2825 auth.second = CDIR_AUTH_UNKNOWN;
2826 inode->mdcache->adjust_subtree_auth(this, auth);
2830 finish_waiting(WAIT_UNFREEZE);
2832 finish_waiting(WAIT_FROZEN, -1);
2834 // freezing. stop it.
2835 assert(state_test(STATE_FREEZINGTREE));
2836 state_clear(STATE_FREEZINGTREE);
2837 --num_freezing_trees;
2840 finish_waiting(WAIT_UNFREEZE);
2844 bool CDir::is_freezing_tree() const
2846 if (num_freezing_trees == 0)
2848 const CDir *dir = this;
2850 if (dir->is_freezing_tree_root()) return true;
2851 if (dir->is_subtree_root()) return false;
2852 if (dir->inode->parent)
2853 dir = dir->inode->parent->dir;
2855 return false; // root on replica
2859 bool CDir::is_frozen_tree() const
2861 if (num_frozen_trees == 0)
2863 const CDir *dir = this;
2865 if (dir->is_frozen_tree_root()) return true;
2866 if (dir->is_subtree_root()) return false;
2867 if (dir->inode->parent)
2868 dir = dir->inode->parent->dir;
2870 return false; // root on replica
2874 CDir *CDir::get_frozen_tree_root()
2876 assert(is_frozen());
2879 if (dir->is_frozen_tree_root())
2881 if (dir->inode->parent)
2882 dir = dir->inode->parent->dir;
2888 class C_Dir_AuthUnpin : public CDirContext {
2890 explicit C_Dir_AuthUnpin(CDir *d) : CDirContext(d) {}
2891 void finish(int r) override {
2892 dir->auth_unpin(dir->get_inode());
2896 void CDir::maybe_finish_freeze()
2898 if (auth_pins != 1 || dir_auth_pins != 0)
2901 // we can freeze the _dir_ even with nested pins...
2902 if (state_test(STATE_FREEZINGDIR)) {
2905 finish_waiting(WAIT_FROZEN);
2908 if (nested_auth_pins != 0)
2911 if (state_test(STATE_FREEZINGTREE)) {
2912 if (!is_subtree_root() && inode->is_frozen()) {
2913 dout(10) << "maybe_finish_freeze !subtree root and frozen inode, waiting for unfreeze on " << inode << dendl;
2914 // retake an auth_pin...
2916 // and release it when the parent inode unfreezes
2917 inode->add_waiter(WAIT_UNFREEZE, new C_Dir_AuthUnpin(this));
2923 finish_waiting(WAIT_FROZEN);
2931 bool CDir::freeze_dir()
2933 assert(!is_frozen());
2934 assert(!is_freezing());
2937 if (is_freezeable_dir(true)) {
2942 state_set(STATE_FREEZINGDIR);
2943 dout(10) << "freeze_dir + wait " << *this << dendl;
2948 void CDir::_freeze_dir()
2950 dout(10) << "_freeze_dir " << *this << dendl;
2951 //assert(is_freezeable_dir(true));
2952 // not always true during split because the original fragment may have frozen a while
2953 // ago and we're just now getting around to breaking it up.
2955 state_clear(STATE_FREEZINGDIR);
2956 state_set(STATE_FROZENDIR);
2959 if (is_auth() && !is_subtree_root())
2960 inode->auth_pin(this); // auth_pin for duration of freeze
2964 void CDir::unfreeze_dir()
2966 dout(10) << "unfreeze_dir " << *this << dendl;
2968 if (state_test(STATE_FROZENDIR)) {
2969 state_clear(STATE_FROZENDIR);
2972 // unpin (may => FREEZEABLE) FIXME: is this order good?
2973 if (is_auth() && !is_subtree_root())
2974 inode->auth_unpin(this);
2976 finish_waiting(WAIT_UNFREEZE);
2978 finish_waiting(WAIT_FROZEN, -1);
2980 // still freezing. stop.
2981 assert(state_test(STATE_FREEZINGDIR));
2982 state_clear(STATE_FREEZINGDIR);
2985 finish_waiting(WAIT_UNFREEZE);
2990 * Slightly less complete than operator<<, because this is intended
2991 * for identifying a directory and its state rather than for dumping
2994 void CDir::dump(Formatter *f) const
2998 f->dump_stream("path") << get_path();
3000 f->dump_stream("dirfrag") << dirfrag();
3001 f->dump_int("snapid_first", first);
3003 f->dump_stream("projected_version") << get_projected_version();
3004 f->dump_stream("version") << get_version();
3005 f->dump_stream("committing_version") << get_committing_version();
3006 f->dump_stream("committed_version") << get_committed_version();
3008 f->dump_bool("is_rep", is_rep());
3010 if (get_dir_auth() != CDIR_AUTH_DEFAULT) {
3011 if (get_dir_auth().second == CDIR_AUTH_UNKNOWN) {
3012 f->dump_stream("dir_auth") << get_dir_auth().first;
3014 f->dump_stream("dir_auth") << get_dir_auth();
3017 f->dump_string("dir_auth", "");
3020 f->open_array_section("states");
3021 MDSCacheObject::dump_states(f);
3022 if (state_test(CDir::STATE_COMPLETE)) f->dump_string("state", "complete");
3023 if (state_test(CDir::STATE_FREEZINGTREE)) f->dump_string("state", "freezingtree");
3024 if (state_test(CDir::STATE_FROZENTREE)) f->dump_string("state", "frozentree");
3025 if (state_test(CDir::STATE_FROZENDIR)) f->dump_string("state", "frozendir");
3026 if (state_test(CDir::STATE_FREEZINGDIR)) f->dump_string("state", "freezingdir");
3027 if (state_test(CDir::STATE_EXPORTBOUND)) f->dump_string("state", "exportbound");
3028 if (state_test(CDir::STATE_IMPORTBOUND)) f->dump_string("state", "importbound");
3029 if (state_test(CDir::STATE_BADFRAG)) f->dump_string("state", "badfrag");
3032 MDSCacheObject::dump(f);
3035 /****** Scrub Stuff *******/
3037 void CDir::scrub_info_create() const
3039 assert(!scrub_infop);
3041 // break out of const-land to set up implicit initial state
3042 CDir *me = const_cast<CDir*>(this);
3043 fnode_t *fn = me->get_projected_fnode();
3045 std::unique_ptr<scrub_info_t> si(new scrub_info_t());
3047 si->last_recursive.version = si->recursive_start.version =
3048 fn->recursive_scrub_version;
3049 si->last_recursive.time = si->recursive_start.time =
3050 fn->recursive_scrub_stamp;
3052 si->last_local.version = fn->localized_scrub_version;
3053 si->last_local.time = fn->localized_scrub_stamp;
3055 me->scrub_infop.swap(si);
3058 void CDir::scrub_initialize(const ScrubHeaderRefConst& header)
3060 dout(20) << __func__ << dendl;
3061 assert(is_complete());
3062 assert(header != nullptr);
3064 // FIXME: weird implicit construction, is someone else meant
3065 // to be calling scrub_info_create first?
3067 assert(scrub_infop && !scrub_infop->directory_scrubbing);
3069 scrub_infop->recursive_start.version = get_projected_version();
3070 scrub_infop->recursive_start.time = ceph_clock_now();
3072 scrub_infop->directories_to_scrub.clear();
3073 scrub_infop->directories_scrubbing.clear();
3074 scrub_infop->directories_scrubbed.clear();
3075 scrub_infop->others_to_scrub.clear();
3076 scrub_infop->others_scrubbing.clear();
3077 scrub_infop->others_scrubbed.clear();
3079 for (map_t::iterator i = items.begin();
3082 // TODO: handle snapshot scrubbing
3083 if (i->first.snapid != CEPH_NOSNAP)
3086 CDentry::linkage_t *dnl = i->second->get_projected_linkage();
3087 if (dnl->is_primary()) {
3088 if (dnl->get_inode()->is_dir())
3089 scrub_infop->directories_to_scrub.insert(i->first);
3091 scrub_infop->others_to_scrub.insert(i->first);
3092 } else if (dnl->is_remote()) {
3093 // TODO: check remote linkage
3096 scrub_infop->directory_scrubbing = true;
3097 scrub_infop->header = header;
3100 void CDir::scrub_finished()
3102 dout(20) << __func__ << dendl;
3103 assert(scrub_infop && scrub_infop->directory_scrubbing);
3105 assert(scrub_infop->directories_to_scrub.empty());
3106 assert(scrub_infop->directories_scrubbing.empty());
3107 scrub_infop->directories_scrubbed.clear();
3108 assert(scrub_infop->others_to_scrub.empty());
3109 assert(scrub_infop->others_scrubbing.empty());
3110 scrub_infop->others_scrubbed.clear();
3111 scrub_infop->directory_scrubbing = false;
3113 scrub_infop->last_recursive = scrub_infop->recursive_start;
3114 scrub_infop->last_scrub_dirty = true;
3117 int CDir::_next_dentry_on_set(set<dentry_key_t>& dns, bool missing_okay,
3118 MDSInternalContext *cb, CDentry **dnout)
3123 while (!dns.empty()) {
3124 set<dentry_key_t>::iterator front = dns.begin();
3126 dn = lookup(dnkey.name);
3128 if (!is_complete() &&
3129 (!has_bloom() || is_in_bloom(dnkey.name))) {
3130 // need to re-read this dirfrag
3136 dout(15) << " we no longer have directory dentry "
3137 << dnkey.name << ", assuming it got renamed" << dendl;
3141 dout(5) << " we lost dentry " << dnkey.name
3142 << ", bailing out because that's impossible!" << dendl;
3146 // okay, we got a dentry
3149 if (dn->get_projected_version() < scrub_infop->last_recursive.version &&
3150 !(scrub_infop->header->get_force())) {
3151 dout(15) << " skip dentry " << dnkey.name
3152 << ", no change since last scrub" << dendl;
3163 int CDir::scrub_dentry_next(MDSInternalContext *cb, CDentry **dnout)
3165 dout(20) << __func__ << dendl;
3166 assert(scrub_infop && scrub_infop->directory_scrubbing);
3168 dout(20) << "trying to scrub directories underneath us" << dendl;
3169 int rval = _next_dentry_on_set(scrub_infop->directories_to_scrub, true,
3172 dout(20) << __func__ << " inserted to directories scrubbing: "
3174 scrub_infop->directories_scrubbing.insert((*dnout)->key());
3175 } else if (rval == EAGAIN) {
3176 // we don't need to do anything else
3177 } else { // we emptied out the directory scrub set
3178 assert(rval == ENOENT);
3179 dout(20) << "no directories left, moving on to other kinds of dentries"
3182 rval = _next_dentry_on_set(scrub_infop->others_to_scrub, false, cb, dnout);
3184 dout(20) << __func__ << " inserted to others scrubbing: "
3186 scrub_infop->others_scrubbing.insert((*dnout)->key());
3189 dout(20) << " returning " << rval << " with dn=" << *dnout << dendl;
3193 void CDir::scrub_dentries_scrubbing(list<CDentry*> *out_dentries)
3195 dout(20) << __func__ << dendl;
3196 assert(scrub_infop && scrub_infop->directory_scrubbing);
3198 for (set<dentry_key_t>::iterator i =
3199 scrub_infop->directories_scrubbing.begin();
3200 i != scrub_infop->directories_scrubbing.end();
3202 CDentry *d = lookup(i->name, i->snapid);
3204 out_dentries->push_back(d);
3206 for (set<dentry_key_t>::iterator i = scrub_infop->others_scrubbing.begin();
3207 i != scrub_infop->others_scrubbing.end();
3209 CDentry *d = lookup(i->name, i->snapid);
3211 out_dentries->push_back(d);
3215 void CDir::scrub_dentry_finished(CDentry *dn)
3217 dout(20) << __func__ << " on dn " << *dn << dendl;
3218 assert(scrub_infop && scrub_infop->directory_scrubbing);
3219 dentry_key_t dn_key = dn->key();
3220 if (scrub_infop->directories_scrubbing.erase(dn_key)) {
3221 scrub_infop->directories_scrubbed.insert(dn_key);
3223 assert(scrub_infop->others_scrubbing.count(dn_key));
3224 scrub_infop->others_scrubbing.erase(dn_key);
3225 scrub_infop->others_scrubbed.insert(dn_key);
3229 void CDir::scrub_maybe_delete_info()
3232 !scrub_infop->directory_scrubbing &&
3233 !scrub_infop->need_scrub_local &&
3234 !scrub_infop->last_scrub_dirty &&
3235 !scrub_infop->pending_scrub_error &&
3236 scrub_infop->dirty_scrub_stamps.empty()) {
3237 scrub_infop.reset();
3241 bool CDir::scrub_local()
3243 assert(is_complete());
3244 bool rval = check_rstats(true);
3248 scrub_infop->last_local.time = ceph_clock_now();
3249 scrub_infop->last_local.version = get_projected_version();
3250 scrub_infop->pending_scrub_error = false;
3251 scrub_infop->last_scrub_dirty = true;
3253 scrub_infop->pending_scrub_error = true;
3254 if (scrub_infop->header->get_repair())
3255 cache->repair_dirfrag_stats(this);
3260 std::string CDir::get_path() const
3263 get_inode()->make_path_string(path, true);
3267 bool CDir::should_split_fast() const
3269 // Max size a fragment can be before trigger fast splitting
3270 int fast_limit = g_conf->mds_bal_split_size * g_conf->mds_bal_fragment_fast_factor;
3272 // Fast path: the sum of accounted size and null dentries does not
3273 // exceed threshold: we definitely are not over it.
3274 if (get_frag_size() + get_num_head_null() <= fast_limit) {
3278 // Fast path: the accounted size of the frag exceeds threshold: we
3279 // definitely are over it
3280 if (get_frag_size() > fast_limit) {
3284 int64_t effective_size = 0;
3286 for (const auto &p : items) {
3287 const CDentry *dn = p.second;
3288 if (!dn->get_projected_linkage()->is_null()) {
3293 return effective_size > fast_limit;
3296 MEMPOOL_DEFINE_OBJECT_FACTORY(CDir, co_dir, mds_co);