]> git.proxmox.com Git - ceph.git/blob - ceph/src/mds/CDir.cc
import quincy beta 17.1.0
[ceph.git] / ceph / src / mds / CDir.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15 #include <string_view>
16 #include <algorithm>
17
18 #include "include/types.h"
19
20 #include "CDir.h"
21 #include "CDentry.h"
22 #include "CInode.h"
23 #include "Mutation.h"
24
25 #include "MDSMap.h"
26 #include "MDSRank.h"
27 #include "MDCache.h"
28 #include "Locker.h"
29 #include "MDLog.h"
30 #include "LogSegment.h"
31 #include "MDBalancer.h"
32
33 #include "common/bloom_filter.hpp"
34 #include "include/Context.h"
35 #include "common/Clock.h"
36
37 #include "osdc/Objecter.h"
38
39 #include "common/config.h"
40 #include "include/ceph_assert.h"
41 #include "include/compat.h"
42
43 #define dout_context g_ceph_context
44 #define dout_subsys ceph_subsys_mds
45 #undef dout_prefix
46 #define dout_prefix *_dout << "mds." << mdcache->mds->get_nodeid() << ".cache.dir(" << this->dirfrag() << ") "
47
48 using namespace std;
49
50 int CDir::num_frozen_trees = 0;
51 int CDir::num_freezing_trees = 0;
52
53 CDir::fnode_const_ptr CDir::empty_fnode = CDir::allocate_fnode();
54
55 class CDirContext : public MDSContext
56 {
57 protected:
58 CDir *dir;
59 MDSRank* get_mds() override {return dir->mdcache->mds;}
60
61 public:
62 explicit CDirContext(CDir *d) : dir(d) {
63 ceph_assert(dir != NULL);
64 }
65 };
66
67
68 class CDirIOContext : public MDSIOContextBase
69 {
70 protected:
71 CDir *dir;
72 MDSRank* get_mds() override {return dir->mdcache->mds;}
73
74 public:
75 explicit CDirIOContext(CDir *d) : dir(d) {
76 ceph_assert(dir != NULL);
77 }
78 };
79
80
81 // PINS
82 //int cdir_pins[CDIR_NUM_PINS] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
83
84
85 ostream& operator<<(ostream& out, const CDir& dir)
86 {
87 out << "[dir " << dir.dirfrag() << " " << dir.get_path() << "/"
88 << " [" << dir.first << ",head]";
89 if (dir.is_auth()) {
90 out << " auth";
91 if (dir.is_replicated())
92 out << dir.get_replicas();
93
94 if (dir.is_projected())
95 out << " pv=" << dir.get_projected_version();
96 out << " v=" << dir.get_version();
97 out << " cv=" << dir.get_committing_version();
98 out << "/" << dir.get_committed_version();
99 } else {
100 mds_authority_t a = dir.authority();
101 out << " rep@" << a.first;
102 if (a.second != CDIR_AUTH_UNKNOWN)
103 out << "," << a.second;
104 out << "." << dir.get_replica_nonce();
105 }
106
107 if (dir.is_rep()) out << " REP";
108
109 if (dir.get_dir_auth() != CDIR_AUTH_DEFAULT) {
110 if (dir.get_dir_auth().second == CDIR_AUTH_UNKNOWN)
111 out << " dir_auth=" << dir.get_dir_auth().first;
112 else
113 out << " dir_auth=" << dir.get_dir_auth();
114 }
115
116 if (dir.get_auth_pins() || dir.get_dir_auth_pins()) {
117 out << " ap=" << dir.get_auth_pins()
118 << "+" << dir.get_dir_auth_pins();
119 #ifdef MDS_AUTHPIN_SET
120 dir.print_authpin_set(out);
121 #endif
122 }
123
124 out << " state=" << dir.get_state();
125 if (dir.state_test(CDir::STATE_COMPLETE)) out << "|complete";
126 if (dir.state_test(CDir::STATE_FREEZINGTREE)) out << "|freezingtree";
127 if (dir.state_test(CDir::STATE_FROZENTREE)) out << "|frozentree";
128 if (dir.state_test(CDir::STATE_AUXSUBTREE)) out << "|auxsubtree";
129 if (dir.state_test(CDir::STATE_FROZENDIR)) out << "|frozendir";
130 if (dir.state_test(CDir::STATE_FREEZINGDIR)) out << "|freezingdir";
131 if (dir.state_test(CDir::STATE_EXPORTBOUND)) out << "|exportbound";
132 if (dir.state_test(CDir::STATE_IMPORTBOUND)) out << "|importbound";
133 if (dir.state_test(CDir::STATE_BADFRAG)) out << "|badfrag";
134 if (dir.state_test(CDir::STATE_FRAGMENTING)) out << "|fragmenting";
135 if (dir.state_test(CDir::STATE_CREATING)) out << "|creating";
136 if (dir.state_test(CDir::STATE_COMMITTING)) out << "|committing";
137 if (dir.state_test(CDir::STATE_FETCHING)) out << "|fetching";
138 if (dir.state_test(CDir::STATE_EXPORTING)) out << "|exporting";
139 if (dir.state_test(CDir::STATE_IMPORTING)) out << "|importing";
140 if (dir.state_test(CDir::STATE_STICKY)) out << "|sticky";
141 if (dir.state_test(CDir::STATE_DNPINNEDFRAG)) out << "|dnpinnedfrag";
142 if (dir.state_test(CDir::STATE_ASSIMRSTAT)) out << "|assimrstat";
143
144 // fragstat
145 out << " " << dir.get_fnode()->fragstat;
146 if (!(dir.get_fnode()->fragstat == dir.get_fnode()->accounted_fragstat))
147 out << "/" << dir.get_fnode()->accounted_fragstat;
148 if (g_conf()->mds_debug_scatterstat && dir.is_projected()) {
149 const auto& pf = dir.get_projected_fnode();
150 out << "->" << pf->fragstat;
151 if (!(pf->fragstat == pf->accounted_fragstat))
152 out << "/" << pf->accounted_fragstat;
153 }
154
155 // rstat
156 out << " " << dir.get_fnode()->rstat;
157 if (!(dir.get_fnode()->rstat == dir.get_fnode()->accounted_rstat))
158 out << "/" << dir.get_fnode()->accounted_rstat;
159 if (g_conf()->mds_debug_scatterstat && dir.is_projected()) {
160 const auto& pf = dir.get_projected_fnode();
161 out << "->" << pf->rstat;
162 if (!(pf->rstat == pf->accounted_rstat))
163 out << "/" << pf->accounted_rstat;
164 }
165
166 out << " hs=" << dir.get_num_head_items() << "+" << dir.get_num_head_null();
167 out << ",ss=" << dir.get_num_snap_items() << "+" << dir.get_num_snap_null();
168 if (dir.get_num_dirty())
169 out << " dirty=" << dir.get_num_dirty();
170
171 if (dir.get_num_ref()) {
172 out << " |";
173 dir.print_pin_set(out);
174 }
175
176 out << " " << &dir;
177 return out << "]";
178 }
179
180
181 void CDir::print(ostream& out)
182 {
183 out << *this;
184 }
185
186
187
188
189 ostream& CDir::print_db_line_prefix(ostream& out)
190 {
191 return out << ceph_clock_now() << " mds." << mdcache->mds->get_nodeid() << ".cache.dir(" << this->dirfrag() << ") ";
192 }
193
194
195
196 // -------------------------------------------------------------------
197 // CDir
198
199 CDir::CDir(CInode *in, frag_t fg, MDCache *mdc, bool auth) :
200 mdcache(mdc), inode(in), frag(fg),
201 dirty_rstat_inodes(member_offset(CInode, dirty_rstat_item)),
202 dirty_dentries(member_offset(CDentry, item_dir_dirty)),
203 item_dirty(this), item_new(this),
204 lock_caches_with_auth_pins(member_offset(MDLockCache::DirItem, item_dir)),
205 freezing_inodes(member_offset(CInode, item_freezing_inode)),
206 dir_rep(REP_NONE),
207 pop_me(mdc->decayrate),
208 pop_nested(mdc->decayrate),
209 pop_auth_subtree(mdc->decayrate),
210 pop_auth_subtree_nested(mdc->decayrate),
211 pop_spread(mdc->decayrate),
212 pop_lru_subdirs(member_offset(CInode, item_pop_lru)),
213 dir_auth(CDIR_AUTH_DEFAULT)
214 {
215 // auth
216 ceph_assert(in->is_dir());
217 if (auth)
218 state_set(STATE_AUTH);
219 }
220
221 /**
222 * Check the recursive statistics on size for consistency.
223 * If mds_debug_scatterstat is enabled, assert for correctness,
224 * otherwise just print out the mismatch and continue.
225 */
226 bool CDir::check_rstats(bool scrub)
227 {
228 if (!g_conf()->mds_debug_scatterstat && !scrub)
229 return true;
230
231 dout(25) << "check_rstats on " << this << dendl;
232 if (!is_complete() || !is_auth() || is_frozen()) {
233 dout(3) << "check_rstats " << (scrub ? "(scrub) " : "")
234 << "bailing out -- incomplete or non-auth or frozen dir on "
235 << *this << dendl;
236 return !scrub;
237 }
238
239 frag_info_t frag_info;
240 nest_info_t nest_info;
241 for (auto i = items.begin(); i != items.end(); ++i) {
242 if (i->second->last != CEPH_NOSNAP)
243 continue;
244 CDentry::linkage_t *dnl = i->second->get_linkage();
245 if (dnl->is_primary()) {
246 CInode *in = dnl->get_inode();
247 nest_info.add(in->get_inode()->accounted_rstat);
248 if (in->is_dir())
249 frag_info.nsubdirs++;
250 else
251 frag_info.nfiles++;
252 } else if (dnl->is_remote())
253 frag_info.nfiles++;
254 }
255
256 bool good = true;
257 // fragstat
258 if(!frag_info.same_sums(fnode->fragstat)) {
259 dout(1) << "mismatch between head items and fnode.fragstat! printing dentries" << dendl;
260 dout(1) << "get_num_head_items() = " << get_num_head_items()
261 << "; fnode.fragstat.nfiles=" << fnode->fragstat.nfiles
262 << " fnode.fragstat.nsubdirs=" << fnode->fragstat.nsubdirs << dendl;
263 good = false;
264 } else {
265 dout(20) << "get_num_head_items() = " << get_num_head_items()
266 << "; fnode.fragstat.nfiles=" << fnode->fragstat.nfiles
267 << " fnode.fragstat.nsubdirs=" << fnode->fragstat.nsubdirs << dendl;
268 }
269
270 // rstat
271 if (!nest_info.same_sums(fnode->rstat)) {
272 dout(1) << "mismatch between child accounted_rstats and my rstats!" << dendl;
273 dout(1) << "total of child dentries: " << nest_info << dendl;
274 dout(1) << "my rstats: " << fnode->rstat << dendl;
275 good = false;
276 } else {
277 dout(20) << "total of child dentries: " << nest_info << dendl;
278 dout(20) << "my rstats: " << fnode->rstat << dendl;
279 }
280
281 if (!good) {
282 if (!scrub) {
283 for (auto i = items.begin(); i != items.end(); ++i) {
284 CDentry *dn = i->second;
285 if (dn->get_linkage()->is_primary()) {
286 CInode *in = dn->get_linkage()->inode;
287 dout(1) << *dn << " rstat " << in->get_inode()->accounted_rstat << dendl;
288 } else {
289 dout(1) << *dn << dendl;
290 }
291 }
292
293 ceph_assert(frag_info.nfiles == fnode->fragstat.nfiles);
294 ceph_assert(frag_info.nsubdirs == fnode->fragstat.nsubdirs);
295 ceph_assert(nest_info.rbytes == fnode->rstat.rbytes);
296 ceph_assert(nest_info.rfiles == fnode->rstat.rfiles);
297 ceph_assert(nest_info.rsubdirs == fnode->rstat.rsubdirs);
298 }
299 }
300 dout(10) << "check_rstats complete on " << this << dendl;
301 return good;
302 }
303
304 void CDir::adjust_num_inodes_with_caps(int d)
305 {
306 // FIXME: smarter way to decide if adding 'this' to open file table
307 if (num_inodes_with_caps == 0 && d > 0)
308 mdcache->open_file_table.add_dirfrag(this);
309 else if (num_inodes_with_caps > 0 && num_inodes_with_caps == -d)
310 mdcache->open_file_table.remove_dirfrag(this);
311
312 num_inodes_with_caps += d;
313 ceph_assert(num_inodes_with_caps >= 0);
314 }
315
316 CDentry *CDir::lookup(std::string_view name, snapid_t snap)
317 {
318 dout(20) << "lookup (" << snap << ", '" << name << "')" << dendl;
319 auto iter = items.lower_bound(dentry_key_t(snap, name, inode->hash_dentry_name(name)));
320 if (iter == items.end())
321 return 0;
322 if (iter->second->get_name() == name &&
323 iter->second->first <= snap &&
324 iter->second->last >= snap) {
325 dout(20) << " hit -> " << iter->first << dendl;
326 return iter->second;
327 }
328 dout(20) << " miss -> " << iter->first << dendl;
329 return 0;
330 }
331
332 CDentry *CDir::lookup_exact_snap(std::string_view name, snapid_t last) {
333 dout(20) << __func__ << " (" << last << ", '" << name << "')" << dendl;
334 auto p = items.find(dentry_key_t(last, name, inode->hash_dentry_name(name)));
335 if (p == items.end())
336 return NULL;
337 return p->second;
338 }
339
340 /***
341 * linking fun
342 */
343
344 CDentry* CDir::add_null_dentry(std::string_view dname,
345 snapid_t first, snapid_t last)
346 {
347 // foreign
348 ceph_assert(lookup_exact_snap(dname, last) == 0);
349
350 // create dentry
351 CDentry* dn = new CDentry(dname, inode->hash_dentry_name(dname), "", first, last);
352 if (is_auth())
353 dn->state_set(CDentry::STATE_AUTH);
354
355 mdcache->bottom_lru.lru_insert_mid(dn);
356 dn->state_set(CDentry::STATE_BOTTOMLRU);
357
358 dn->dir = this;
359 dn->version = get_projected_version();
360
361 // add to dir
362 ceph_assert(items.count(dn->key()) == 0);
363 //assert(null_items.count(dn->get_name()) == 0);
364
365 items[dn->key()] = dn;
366 if (last == CEPH_NOSNAP)
367 num_head_null++;
368 else
369 num_snap_null++;
370
371 if (state_test(CDir::STATE_DNPINNEDFRAG)) {
372 dn->get(CDentry::PIN_FRAGMENTING);
373 dn->state_set(CDentry::STATE_FRAGMENTING);
374 }
375
376 dout(12) << __func__ << " " << *dn << dendl;
377
378 // pin?
379 if (get_num_any() == 1)
380 get(PIN_CHILD);
381
382 ceph_assert(get_num_any() == items.size());
383 return dn;
384 }
385
386
387 CDentry* CDir::add_primary_dentry(std::string_view dname, CInode *in,
388 mempool::mds_co::string alternate_name,
389 snapid_t first, snapid_t last)
390 {
391 // primary
392 ceph_assert(lookup_exact_snap(dname, last) == 0);
393
394 // create dentry
395 CDentry* dn = new CDentry(dname, inode->hash_dentry_name(dname), std::move(alternate_name), first, last);
396 if (is_auth())
397 dn->state_set(CDentry::STATE_AUTH);
398 if (is_auth() || !inode->is_stray()) {
399 mdcache->lru.lru_insert_mid(dn);
400 } else {
401 mdcache->bottom_lru.lru_insert_mid(dn);
402 dn->state_set(CDentry::STATE_BOTTOMLRU);
403 }
404
405 dn->dir = this;
406 dn->version = get_projected_version();
407
408 // add to dir
409 ceph_assert(items.count(dn->key()) == 0);
410 //assert(null_items.count(dn->get_name()) == 0);
411
412 items[dn->key()] = dn;
413
414 dn->get_linkage()->inode = in;
415
416 link_inode_work(dn, in);
417
418 if (dn->last == CEPH_NOSNAP)
419 num_head_items++;
420 else
421 num_snap_items++;
422
423 if (state_test(CDir::STATE_DNPINNEDFRAG)) {
424 dn->get(CDentry::PIN_FRAGMENTING);
425 dn->state_set(CDentry::STATE_FRAGMENTING);
426 }
427
428 dout(12) << __func__ << " " << *dn << dendl;
429
430 // pin?
431 if (get_num_any() == 1)
432 get(PIN_CHILD);
433 ceph_assert(get_num_any() == items.size());
434 return dn;
435 }
436
437 CDentry* CDir::add_remote_dentry(std::string_view dname, inodeno_t ino, unsigned char d_type,
438 mempool::mds_co::string alternate_name,
439 snapid_t first, snapid_t last)
440 {
441 // foreign
442 ceph_assert(lookup_exact_snap(dname, last) == 0);
443
444 // create dentry
445 CDentry* dn = new CDentry(dname, inode->hash_dentry_name(dname), std::move(alternate_name), ino, d_type, first, last);
446 if (is_auth())
447 dn->state_set(CDentry::STATE_AUTH);
448 mdcache->lru.lru_insert_mid(dn);
449
450 dn->dir = this;
451 dn->version = get_projected_version();
452
453 // add to dir
454 ceph_assert(items.count(dn->key()) == 0);
455 //assert(null_items.count(dn->get_name()) == 0);
456
457 items[dn->key()] = dn;
458 if (last == CEPH_NOSNAP)
459 num_head_items++;
460 else
461 num_snap_items++;
462
463 if (state_test(CDir::STATE_DNPINNEDFRAG)) {
464 dn->get(CDentry::PIN_FRAGMENTING);
465 dn->state_set(CDentry::STATE_FRAGMENTING);
466 }
467
468 dout(12) << __func__ << " " << *dn << dendl;
469
470 // pin?
471 if (get_num_any() == 1)
472 get(PIN_CHILD);
473
474 ceph_assert(get_num_any() == items.size());
475 return dn;
476 }
477
478
479
480 void CDir::remove_dentry(CDentry *dn)
481 {
482 dout(12) << __func__ << " " << *dn << dendl;
483
484 // there should be no client leases at this point!
485 ceph_assert(dn->client_lease_map.empty());
486
487 if (state_test(CDir::STATE_DNPINNEDFRAG)) {
488 dn->put(CDentry::PIN_FRAGMENTING);
489 dn->state_clear(CDentry::STATE_FRAGMENTING);
490 }
491
492 if (dn->get_linkage()->is_null()) {
493 if (dn->last == CEPH_NOSNAP)
494 num_head_null--;
495 else
496 num_snap_null--;
497 } else {
498 if (dn->last == CEPH_NOSNAP)
499 num_head_items--;
500 else
501 num_snap_items--;
502 }
503
504 if (!dn->get_linkage()->is_null())
505 // detach inode and dentry
506 unlink_inode_work(dn);
507
508 // remove from list
509 ceph_assert(items.count(dn->key()) == 1);
510 items.erase(dn->key());
511
512 // clean?
513 if (dn->is_dirty())
514 dn->mark_clean();
515
516 if (dn->state_test(CDentry::STATE_BOTTOMLRU))
517 mdcache->bottom_lru.lru_remove(dn);
518 else
519 mdcache->lru.lru_remove(dn);
520 delete dn;
521
522 // unpin?
523 if (get_num_any() == 0)
524 put(PIN_CHILD);
525 ceph_assert(get_num_any() == items.size());
526 }
527
528 void CDir::link_remote_inode(CDentry *dn, CInode *in)
529 {
530 link_remote_inode(dn, in->ino(), IFTODT(in->get_projected_inode()->mode));
531 }
532
533 void CDir::link_remote_inode(CDentry *dn, inodeno_t ino, unsigned char d_type)
534 {
535 dout(12) << __func__ << " " << *dn << " remote " << ino << dendl;
536 ceph_assert(dn->get_linkage()->is_null());
537
538 dn->get_linkage()->set_remote(ino, d_type);
539
540 if (dn->state_test(CDentry::STATE_BOTTOMLRU)) {
541 mdcache->bottom_lru.lru_remove(dn);
542 mdcache->lru.lru_insert_mid(dn);
543 dn->state_clear(CDentry::STATE_BOTTOMLRU);
544 }
545
546 if (dn->last == CEPH_NOSNAP) {
547 num_head_items++;
548 num_head_null--;
549 } else {
550 num_snap_items++;
551 num_snap_null--;
552 }
553 ceph_assert(get_num_any() == items.size());
554 }
555
556 void CDir::link_primary_inode(CDentry *dn, CInode *in)
557 {
558 dout(12) << __func__ << " " << *dn << " " << *in << dendl;
559 ceph_assert(dn->get_linkage()->is_null());
560
561 dn->get_linkage()->inode = in;
562
563 link_inode_work(dn, in);
564
565 if (dn->state_test(CDentry::STATE_BOTTOMLRU) &&
566 (is_auth() || !inode->is_stray())) {
567 mdcache->bottom_lru.lru_remove(dn);
568 mdcache->lru.lru_insert_mid(dn);
569 dn->state_clear(CDentry::STATE_BOTTOMLRU);
570 }
571
572 if (dn->last == CEPH_NOSNAP) {
573 num_head_items++;
574 num_head_null--;
575 } else {
576 num_snap_items++;
577 num_snap_null--;
578 }
579
580 ceph_assert(get_num_any() == items.size());
581 }
582
583 void CDir::link_inode_work( CDentry *dn, CInode *in)
584 {
585 ceph_assert(dn->get_linkage()->get_inode() == in);
586 in->set_primary_parent(dn);
587
588 // set inode version
589 //in->inode.version = dn->get_version();
590
591 // pin dentry?
592 if (in->get_num_ref())
593 dn->get(CDentry::PIN_INODEPIN);
594
595 if (in->state_test(CInode::STATE_TRACKEDBYOFT))
596 mdcache->open_file_table.notify_link(in);
597 if (in->is_any_caps())
598 adjust_num_inodes_with_caps(1);
599
600 // adjust auth pin count
601 if (in->auth_pins)
602 dn->adjust_nested_auth_pins(in->auth_pins, NULL);
603
604 if (in->is_freezing_inode())
605 freezing_inodes.push_back(&in->item_freezing_inode);
606 else if (in->is_frozen_inode() || in->is_frozen_auth_pin())
607 num_frozen_inodes++;
608
609 // verify open snaprealm parent
610 if (in->snaprealm)
611 in->snaprealm->adjust_parent();
612 else if (in->is_any_caps())
613 in->move_to_realm(inode->find_snaprealm());
614 }
615
616 void CDir::unlink_inode(CDentry *dn, bool adjust_lru)
617 {
618 if (dn->get_linkage()->is_primary()) {
619 dout(12) << __func__ << " " << *dn << " " << *dn->get_linkage()->get_inode() << dendl;
620 } else {
621 dout(12) << __func__ << " " << *dn << dendl;
622 }
623
624 unlink_inode_work(dn);
625
626 if (adjust_lru && !dn->state_test(CDentry::STATE_BOTTOMLRU)) {
627 mdcache->lru.lru_remove(dn);
628 mdcache->bottom_lru.lru_insert_mid(dn);
629 dn->state_set(CDentry::STATE_BOTTOMLRU);
630 }
631
632 if (dn->last == CEPH_NOSNAP) {
633 num_head_items--;
634 num_head_null++;
635 } else {
636 num_snap_items--;
637 num_snap_null++;
638 }
639 ceph_assert(get_num_any() == items.size());
640 }
641
642
643 void CDir::try_remove_unlinked_dn(CDentry *dn)
644 {
645 ceph_assert(dn->dir == this);
646 ceph_assert(dn->get_linkage()->is_null());
647
648 // no pins (besides dirty)?
649 if (dn->get_num_ref() != dn->is_dirty())
650 return;
651
652 // was the dn new?
653 if (dn->is_new()) {
654 dout(10) << __func__ << " " << *dn << " in " << *this << dendl;
655 if (dn->is_dirty())
656 dn->mark_clean();
657 remove_dentry(dn);
658
659 // NOTE: we may not have any more dirty dentries, but the fnode
660 // still changed, so the directory must remain dirty.
661 }
662 }
663
664
665 void CDir::unlink_inode_work(CDentry *dn)
666 {
667 CInode *in = dn->get_linkage()->get_inode();
668
669 if (dn->get_linkage()->is_remote()) {
670 // remote
671 if (in)
672 dn->unlink_remote(dn->get_linkage());
673
674 dn->get_linkage()->set_remote(0, 0);
675 } else if (dn->get_linkage()->is_primary()) {
676 // primary
677 // unpin dentry?
678 if (in->get_num_ref())
679 dn->put(CDentry::PIN_INODEPIN);
680
681 if (in->state_test(CInode::STATE_TRACKEDBYOFT))
682 mdcache->open_file_table.notify_unlink(in);
683 if (in->is_any_caps())
684 adjust_num_inodes_with_caps(-1);
685
686 // unlink auth_pin count
687 if (in->auth_pins)
688 dn->adjust_nested_auth_pins(-in->auth_pins, nullptr);
689
690 if (in->is_freezing_inode())
691 in->item_freezing_inode.remove_myself();
692 else if (in->is_frozen_inode() || in->is_frozen_auth_pin())
693 num_frozen_inodes--;
694
695 // detach inode
696 in->remove_primary_parent(dn);
697 if (in->is_dir())
698 in->item_pop_lru.remove_myself();
699 dn->get_linkage()->inode = 0;
700 } else {
701 ceph_assert(!dn->get_linkage()->is_null());
702 }
703 }
704
705 void CDir::add_to_bloom(CDentry *dn)
706 {
707 ceph_assert(dn->last == CEPH_NOSNAP);
708 if (!bloom) {
709 /* not create bloom filter for incomplete dir that was added by log replay */
710 if (!is_complete())
711 return;
712
713 /* don't maintain bloom filters in standby replay (saves cycles, and also
714 * avoids need to implement clearing it in EExport for #16924) */
715 if (mdcache->mds->is_standby_replay()) {
716 return;
717 }
718
719 unsigned size = get_num_head_items() + get_num_snap_items();
720 if (size < 100) size = 100;
721 bloom.reset(new bloom_filter(size, 1.0 / size, 0));
722 }
723 /* This size and false positive probability is completely random.*/
724 bloom->insert(dn->get_name().data(), dn->get_name().size());
725 }
726
727 bool CDir::is_in_bloom(std::string_view name)
728 {
729 if (!bloom)
730 return false;
731 return bloom->contains(name.data(), name.size());
732 }
733
734 void CDir::remove_null_dentries() {
735 dout(12) << __func__ << " " << *this << dendl;
736
737 auto p = items.begin();
738 while (p != items.end()) {
739 CDentry *dn = p->second;
740 ++p;
741 if (dn->get_linkage()->is_null() && !dn->is_projected())
742 remove_dentry(dn);
743 }
744
745 ceph_assert(num_snap_null == 0);
746 ceph_assert(num_head_null == 0);
747 ceph_assert(get_num_any() == items.size());
748 }
749
750 /** remove dirty null dentries for deleted directory. the dirfrag will be
751 * deleted soon, so it's safe to not commit dirty dentries.
752 *
753 * This is called when a directory is being deleted, a prerequisite
754 * of which is that its children have been unlinked: we expect to only see
755 * null, unprojected dentries here.
756 */
757 void CDir::try_remove_dentries_for_stray()
758 {
759 dout(10) << __func__ << dendl;
760 ceph_assert(get_parent_dir()->inode->is_stray());
761
762 // clear dirty only when the directory was not snapshotted
763 bool clear_dirty = !inode->snaprealm;
764
765 auto p = items.begin();
766 while (p != items.end()) {
767 CDentry *dn = p->second;
768 ++p;
769 if (dn->last == CEPH_NOSNAP) {
770 ceph_assert(!dn->is_projected());
771 ceph_assert(dn->get_linkage()->is_null());
772 if (clear_dirty && dn->is_dirty())
773 dn->mark_clean();
774 // It's OK to remove lease prematurely because we will never link
775 // the dentry to inode again.
776 if (dn->is_any_leases())
777 dn->remove_client_leases(mdcache->mds->locker);
778 if (dn->get_num_ref() == 0)
779 remove_dentry(dn);
780 } else {
781 ceph_assert(!dn->is_projected());
782 CDentry::linkage_t *dnl= dn->get_linkage();
783 CInode *in = NULL;
784 if (dnl->is_primary()) {
785 in = dnl->get_inode();
786 if (clear_dirty && in->is_dirty())
787 in->mark_clean();
788 }
789 if (clear_dirty && dn->is_dirty())
790 dn->mark_clean();
791 if (dn->get_num_ref() == 0) {
792 remove_dentry(dn);
793 if (in)
794 mdcache->remove_inode(in);
795 }
796 }
797 }
798
799 if (clear_dirty && is_dirty())
800 mark_clean();
801 }
802
803 bool CDir::try_trim_snap_dentry(CDentry *dn, const set<snapid_t>& snaps)
804 {
805 ceph_assert(dn->last != CEPH_NOSNAP);
806 set<snapid_t>::const_iterator p = snaps.lower_bound(dn->first);
807 CDentry::linkage_t *dnl= dn->get_linkage();
808 CInode *in = 0;
809 if (dnl->is_primary())
810 in = dnl->get_inode();
811 if ((p == snaps.end() || *p > dn->last) &&
812 (dn->get_num_ref() == dn->is_dirty()) &&
813 (!in || in->get_num_ref() == in->is_dirty())) {
814 dout(10) << " purging snapped " << *dn << dendl;
815 if (in && in->is_dirty())
816 in->mark_clean();
817 remove_dentry(dn);
818 if (in) {
819 dout(10) << " purging snapped " << *in << dendl;
820 mdcache->remove_inode(in);
821 }
822 return true;
823 }
824 return false;
825 }
826
827
828 void CDir::purge_stale_snap_data(const set<snapid_t>& snaps)
829 {
830 dout(10) << __func__ << " " << snaps << dendl;
831
832 auto p = items.begin();
833 while (p != items.end()) {
834 CDentry *dn = p->second;
835 ++p;
836
837 if (dn->last == CEPH_NOSNAP)
838 continue;
839
840 try_trim_snap_dentry(dn, snaps);
841 }
842 }
843
844
845 /**
846 * steal_dentry -- semi-violently move a dentry from one CDir to another
847 * (*) violently, in that nitems, most pins, etc. are not correctly maintained
848 * on the old CDir corpse; must call finish_old_fragment() when finished.
849 */
850 void CDir::steal_dentry(CDentry *dn)
851 {
852 dout(15) << __func__ << " " << *dn << dendl;
853
854 items[dn->key()] = dn;
855
856 dn->dir->items.erase(dn->key());
857 if (dn->dir->items.empty())
858 dn->dir->put(PIN_CHILD);
859
860 if (get_num_any() == 0)
861 get(PIN_CHILD);
862 if (dn->get_linkage()->is_null()) {
863 if (dn->last == CEPH_NOSNAP)
864 num_head_null++;
865 else
866 num_snap_null++;
867 } else if (dn->last == CEPH_NOSNAP) {
868 num_head_items++;
869
870 auto _fnode = _get_fnode();
871
872 if (dn->get_linkage()->is_primary()) {
873 CInode *in = dn->get_linkage()->get_inode();
874 const auto& pi = in->get_projected_inode();
875 if (in->is_dir()) {
876 _fnode->fragstat.nsubdirs++;
877 if (in->item_pop_lru.is_on_list())
878 pop_lru_subdirs.push_back(&in->item_pop_lru);
879 } else {
880 _fnode->fragstat.nfiles++;
881 }
882 _fnode->rstat.rbytes += pi->accounted_rstat.rbytes;
883 _fnode->rstat.rfiles += pi->accounted_rstat.rfiles;
884 _fnode->rstat.rsubdirs += pi->accounted_rstat.rsubdirs;
885 _fnode->rstat.rsnaps += pi->accounted_rstat.rsnaps;
886 if (pi->accounted_rstat.rctime > fnode->rstat.rctime)
887 _fnode->rstat.rctime = pi->accounted_rstat.rctime;
888
889 if (in->is_any_caps())
890 adjust_num_inodes_with_caps(1);
891
892 // move dirty inode rstat to new dirfrag
893 if (in->is_dirty_rstat())
894 dirty_rstat_inodes.push_back(&in->dirty_rstat_item);
895 } else if (dn->get_linkage()->is_remote()) {
896 if (dn->get_linkage()->get_remote_d_type() == DT_DIR)
897 _fnode->fragstat.nsubdirs++;
898 else
899 _fnode->fragstat.nfiles++;
900 }
901 } else {
902 num_snap_items++;
903 if (dn->get_linkage()->is_primary()) {
904 CInode *in = dn->get_linkage()->get_inode();
905 if (in->is_dirty_rstat())
906 dirty_rstat_inodes.push_back(&in->dirty_rstat_item);
907 }
908 }
909
910 {
911 int dap = dn->get_num_dir_auth_pins();
912 if (dap) {
913 adjust_nested_auth_pins(dap, NULL);
914 dn->dir->adjust_nested_auth_pins(-dap, NULL);
915 }
916 }
917
918 if (dn->is_dirty()) {
919 dirty_dentries.push_back(&dn->item_dir_dirty);
920 num_dirty++;
921 }
922
923 dn->dir = this;
924 }
925
926 void CDir::prepare_old_fragment(map<string_snap_t, MDSContext::vec >& dentry_waiters, bool replay)
927 {
928 // auth_pin old fragment for duration so that any auth_pinning
929 // during the dentry migration doesn't trigger side effects
930 if (!replay && is_auth())
931 auth_pin(this);
932
933 if (!waiting_on_dentry.empty()) {
934 for (const auto &p : waiting_on_dentry) {
935 auto &e = dentry_waiters[p.first];
936 for (const auto &waiter : p.second) {
937 e.push_back(waiter);
938 }
939 }
940 waiting_on_dentry.clear();
941 put(PIN_DNWAITER);
942 }
943 }
944
945 void CDir::prepare_new_fragment(bool replay)
946 {
947 if (!replay && is_auth()) {
948 _freeze_dir();
949 mark_complete();
950 }
951 inode->add_dirfrag(this);
952 }
953
954 void CDir::finish_old_fragment(MDSContext::vec& waiters, bool replay)
955 {
956 // take waiters _before_ unfreeze...
957 if (!replay) {
958 take_waiting(WAIT_ANY_MASK, waiters);
959 if (is_auth()) {
960 auth_unpin(this); // pinned in prepare_old_fragment
961 ceph_assert(is_frozen_dir());
962 unfreeze_dir();
963 }
964 }
965
966 ceph_assert(dir_auth_pins == 0);
967 ceph_assert(auth_pins == 0);
968
969 num_head_items = num_head_null = 0;
970 num_snap_items = num_snap_null = 0;
971 adjust_num_inodes_with_caps(-num_inodes_with_caps);
972
973 // this mirrors init_fragment_pins()
974 if (is_auth())
975 clear_replica_map();
976 if (is_dirty())
977 mark_clean();
978 if (state_test(STATE_IMPORTBOUND))
979 put(PIN_IMPORTBOUND);
980 if (state_test(STATE_EXPORTBOUND))
981 put(PIN_EXPORTBOUND);
982 if (is_subtree_root())
983 put(PIN_SUBTREE);
984
985 if (auth_pins > 0)
986 put(PIN_AUTHPIN);
987
988 ceph_assert(get_num_ref() == (state_test(STATE_STICKY) ? 1:0));
989 }
990
991 void CDir::init_fragment_pins()
992 {
993 if (is_replicated())
994 get(PIN_REPLICATED);
995 if (state_test(STATE_DIRTY))
996 get(PIN_DIRTY);
997 if (state_test(STATE_EXPORTBOUND))
998 get(PIN_EXPORTBOUND);
999 if (state_test(STATE_IMPORTBOUND))
1000 get(PIN_IMPORTBOUND);
1001 if (is_subtree_root())
1002 get(PIN_SUBTREE);
1003 }
1004
1005 void CDir::split(int bits, std::vector<CDir*>* subs, MDSContext::vec& waiters, bool replay)
1006 {
1007 dout(10) << "split by " << bits << " bits on " << *this << dendl;
1008
1009 ceph_assert(replay || is_complete() || !is_auth());
1010
1011 frag_vec_t frags;
1012 frag.split(bits, frags);
1013
1014 vector<CDir*> subfrags(1 << bits);
1015
1016 double fac = 1.0 / (double)(1 << bits); // for scaling load vecs
1017
1018 version_t rstat_version = inode->get_projected_inode()->rstat.version;
1019 version_t dirstat_version = inode->get_projected_inode()->dirstat.version;
1020
1021 nest_info_t rstatdiff;
1022 frag_info_t fragstatdiff;
1023 if (fnode->accounted_rstat.version == rstat_version)
1024 rstatdiff.add_delta(fnode->accounted_rstat, fnode->rstat);
1025 if (fnode->accounted_fragstat.version == dirstat_version)
1026 fragstatdiff.add_delta(fnode->accounted_fragstat, fnode->fragstat);
1027 dout(10) << " rstatdiff " << rstatdiff << " fragstatdiff " << fragstatdiff << dendl;
1028
1029 map<string_snap_t, MDSContext::vec > dentry_waiters;
1030 prepare_old_fragment(dentry_waiters, replay);
1031
1032 // create subfrag dirs
1033 int n = 0;
1034 for (const auto& fg : frags) {
1035 CDir *f = new CDir(inode, fg, mdcache, is_auth());
1036 f->state_set(state & (MASK_STATE_FRAGMENT_KEPT | STATE_COMPLETE));
1037 f->get_replicas() = get_replicas();
1038 f->pop_me = pop_me;
1039 f->pop_me.scale(fac);
1040
1041 // FIXME; this is an approximation
1042 f->pop_nested = pop_nested;
1043 f->pop_nested.scale(fac);
1044 f->pop_auth_subtree = pop_auth_subtree;
1045 f->pop_auth_subtree.scale(fac);
1046 f->pop_auth_subtree_nested = pop_auth_subtree_nested;
1047 f->pop_auth_subtree_nested.scale(fac);
1048
1049 dout(10) << " subfrag " << fg << " " << *f << dendl;
1050 subfrags[n++] = f;
1051 subs->push_back(f);
1052
1053 f->set_dir_auth(get_dir_auth());
1054 f->freeze_tree_state = freeze_tree_state;
1055 f->prepare_new_fragment(replay);
1056 f->init_fragment_pins();
1057 }
1058
1059 // repartition dentries
1060 while (!items.empty()) {
1061 auto p = items.begin();
1062
1063 CDentry *dn = p->second;
1064 frag_t subfrag = inode->pick_dirfrag(dn->get_name());
1065 int n = (subfrag.value() & (subfrag.mask() ^ frag.mask())) >> subfrag.mask_shift();
1066 dout(15) << " subfrag " << subfrag << " n=" << n << " for " << p->first << dendl;
1067 CDir *f = subfrags[n];
1068 f->steal_dentry(dn);
1069 }
1070
1071 for (const auto &p : dentry_waiters) {
1072 frag_t subfrag = inode->pick_dirfrag(p.first.name);
1073 int n = (subfrag.value() & (subfrag.mask() ^ frag.mask())) >> subfrag.mask_shift();
1074 CDir *f = subfrags[n];
1075
1076 if (f->waiting_on_dentry.empty())
1077 f->get(PIN_DNWAITER);
1078 auto &e = f->waiting_on_dentry[p.first];
1079 for (const auto &waiter : p.second) {
1080 e.push_back(waiter);
1081 }
1082 }
1083
1084 // FIXME: handle dirty old rstat
1085
1086 // fix up new frag fragstats
1087 for (int i = 0; i < n; i++) {
1088 CDir *f = subfrags[i];
1089 auto _fnode = f->_get_fnode();
1090 _fnode->version = f->projected_version = get_version();
1091 _fnode->rstat.version = rstat_version;
1092 _fnode->accounted_rstat = _fnode->rstat;
1093 _fnode->fragstat.version = dirstat_version;
1094 _fnode->accounted_fragstat = _fnode->fragstat;
1095 dout(10) << " rstat " << _fnode->rstat << " fragstat " << _fnode->fragstat
1096 << " on " << *f << dendl;
1097
1098 if (i == 0) {
1099 // give any outstanding frag stat differential to first frag
1100 dout(10) << " giving rstatdiff " << rstatdiff << " fragstatdiff" << fragstatdiff
1101 << " to " << *subfrags[0] << dendl;
1102 _fnode->accounted_rstat.add(rstatdiff);
1103 _fnode->accounted_fragstat.add(fragstatdiff);
1104 }
1105 }
1106
1107 finish_old_fragment(waiters, replay);
1108 }
1109
1110 void CDir::merge(const std::vector<CDir*>& subs, MDSContext::vec& waiters, bool replay)
1111 {
1112 dout(10) << "merge " << subs << dendl;
1113
1114 ceph_assert(subs.size() > 0);
1115
1116 set_dir_auth(subs.front()->get_dir_auth());
1117 freeze_tree_state = subs.front()->freeze_tree_state;
1118
1119 for (const auto& dir : subs) {
1120 ceph_assert(get_dir_auth() == dir->get_dir_auth());
1121 ceph_assert(freeze_tree_state == dir->freeze_tree_state);
1122 }
1123
1124 prepare_new_fragment(replay);
1125
1126 auto _fnode = _get_fnode();
1127
1128 nest_info_t rstatdiff;
1129 frag_info_t fragstatdiff;
1130 bool touched_mtime, touched_chattr;
1131 version_t rstat_version = inode->get_projected_inode()->rstat.version;
1132 version_t dirstat_version = inode->get_projected_inode()->dirstat.version;
1133
1134 map<string_snap_t, MDSContext::vec > dentry_waiters;
1135
1136 for (const auto& dir : subs) {
1137 dout(10) << " subfrag " << dir->get_frag() << " " << *dir << dendl;
1138 ceph_assert(!dir->is_auth() || dir->is_complete() || replay);
1139
1140 if (dir->get_fnode()->accounted_rstat.version == rstat_version)
1141 rstatdiff.add_delta(dir->get_fnode()->accounted_rstat, dir->get_fnode()->rstat);
1142 if (dir->get_fnode()->accounted_fragstat.version == dirstat_version)
1143 fragstatdiff.add_delta(dir->get_fnode()->accounted_fragstat, dir->get_fnode()->fragstat,
1144 &touched_mtime, &touched_chattr);
1145
1146 dir->prepare_old_fragment(dentry_waiters, replay);
1147
1148 // steal dentries
1149 while (!dir->items.empty())
1150 steal_dentry(dir->items.begin()->second);
1151
1152 // merge replica map
1153 for (const auto &p : dir->get_replicas()) {
1154 unsigned cur = get_replicas()[p.first];
1155 if (p.second > cur)
1156 get_replicas()[p.first] = p.second;
1157 }
1158
1159 // merge version
1160 if (dir->get_version() > _fnode->version)
1161 _fnode->version = projected_version = dir->get_version();
1162
1163 // merge state
1164 state_set(dir->get_state() & MASK_STATE_FRAGMENT_KEPT);
1165
1166 dir->finish_old_fragment(waiters, replay);
1167 inode->close_dirfrag(dir->get_frag());
1168 }
1169
1170 if (!dentry_waiters.empty()) {
1171 get(PIN_DNWAITER);
1172 for (const auto &p : dentry_waiters) {
1173 auto &e = waiting_on_dentry[p.first];
1174 for (const auto &waiter : p.second) {
1175 e.push_back(waiter);
1176 }
1177 }
1178 }
1179
1180 if (is_auth() && !replay)
1181 mark_complete();
1182
1183 // FIXME: merge dirty old rstat
1184 _fnode->rstat.version = rstat_version;
1185 _fnode->accounted_rstat = _fnode->rstat;
1186 _fnode->accounted_rstat.add(rstatdiff);
1187
1188 _fnode->fragstat.version = dirstat_version;
1189 _fnode->accounted_fragstat = _fnode->fragstat;
1190 _fnode->accounted_fragstat.add(fragstatdiff);
1191
1192 init_fragment_pins();
1193 }
1194
1195
1196
1197
1198 void CDir::resync_accounted_fragstat()
1199 {
1200 auto pf = _get_projected_fnode();
1201 const auto& pi = inode->get_projected_inode();
1202
1203 if (pf->accounted_fragstat.version != pi->dirstat.version) {
1204 pf->fragstat.version = pi->dirstat.version;
1205 dout(10) << __func__ << " " << pf->accounted_fragstat << " -> " << pf->fragstat << dendl;
1206 pf->accounted_fragstat = pf->fragstat;
1207 }
1208 }
1209
1210 /*
1211 * resync rstat and accounted_rstat with inode
1212 */
1213 void CDir::resync_accounted_rstat()
1214 {
1215 auto pf = _get_projected_fnode();
1216 const auto& pi = inode->get_projected_inode();
1217
1218 if (pf->accounted_rstat.version != pi->rstat.version) {
1219 pf->rstat.version = pi->rstat.version;
1220 dout(10) << __func__ << " " << pf->accounted_rstat << " -> " << pf->rstat << dendl;
1221 pf->accounted_rstat = pf->rstat;
1222 dirty_old_rstat.clear();
1223 }
1224 }
1225
1226 void CDir::assimilate_dirty_rstat_inodes(MutationRef& mut)
1227 {
1228 dout(10) << __func__ << dendl;
1229 for (elist<CInode*>::iterator p = dirty_rstat_inodes.begin_use_current();
1230 !p.end(); ++p) {
1231 CInode *in = *p;
1232 ceph_assert(in->is_auth());
1233 if (in->is_frozen())
1234 continue;
1235
1236 mut->auth_pin(in);
1237
1238 auto pi = in->project_inode(mut);
1239 pi.inode->version = in->pre_dirty();
1240
1241 mdcache->project_rstat_inode_to_frag(mut, in, this, 0, 0, nullptr);
1242 }
1243 state_set(STATE_ASSIMRSTAT);
1244 dout(10) << __func__ << " done" << dendl;
1245 }
1246
1247 void CDir::assimilate_dirty_rstat_inodes_finish(EMetaBlob *blob)
1248 {
1249 if (!state_test(STATE_ASSIMRSTAT))
1250 return;
1251 state_clear(STATE_ASSIMRSTAT);
1252 dout(10) << __func__ << dendl;
1253 elist<CInode*>::iterator p = dirty_rstat_inodes.begin_use_current();
1254 while (!p.end()) {
1255 CInode *in = *p;
1256 ++p;
1257
1258 if (in->is_frozen())
1259 continue;
1260
1261 CDentry *dn = in->get_projected_parent_dn();
1262
1263 in->clear_dirty_rstat();
1264 blob->add_primary_dentry(dn, in, true);
1265 }
1266
1267 if (!dirty_rstat_inodes.empty())
1268 mdcache->mds->locker->mark_updated_scatterlock(&inode->nestlock);
1269 }
1270
1271
1272
1273
1274 /****************************************
1275 * WAITING
1276 */
1277
1278 void CDir::add_dentry_waiter(std::string_view dname, snapid_t snapid, MDSContext *c)
1279 {
1280 if (waiting_on_dentry.empty())
1281 get(PIN_DNWAITER);
1282 waiting_on_dentry[string_snap_t(dname, snapid)].push_back(c);
1283 dout(10) << __func__ << " dentry " << dname
1284 << " snap " << snapid
1285 << " " << c << " on " << *this << dendl;
1286 }
1287
1288 void CDir::take_dentry_waiting(std::string_view dname, snapid_t first, snapid_t last,
1289 MDSContext::vec& ls)
1290 {
1291 if (waiting_on_dentry.empty())
1292 return;
1293
1294 string_snap_t lb(dname, first);
1295 string_snap_t ub(dname, last);
1296 auto it = waiting_on_dentry.lower_bound(lb);
1297 while (it != waiting_on_dentry.end() &&
1298 !(ub < it->first)) {
1299 dout(10) << __func__ << " " << dname
1300 << " [" << first << "," << last << "] found waiter on snap "
1301 << it->first.snapid
1302 << " on " << *this << dendl;
1303 for (const auto &waiter : it->second) {
1304 ls.push_back(waiter);
1305 }
1306 waiting_on_dentry.erase(it++);
1307 }
1308
1309 if (waiting_on_dentry.empty())
1310 put(PIN_DNWAITER);
1311 }
1312
1313 void CDir::take_sub_waiting(MDSContext::vec& ls)
1314 {
1315 dout(10) << __func__ << dendl;
1316 if (!waiting_on_dentry.empty()) {
1317 for (const auto &p : waiting_on_dentry) {
1318 for (const auto &waiter : p.second) {
1319 ls.push_back(waiter);
1320 }
1321 }
1322 waiting_on_dentry.clear();
1323 put(PIN_DNWAITER);
1324 }
1325 }
1326
1327
1328
1329 void CDir::add_waiter(uint64_t tag, MDSContext *c)
1330 {
1331 // hierarchical?
1332
1333 // at subtree root?
1334 if (tag & WAIT_ATSUBTREEROOT) {
1335 if (!is_subtree_root()) {
1336 // try parent
1337 dout(10) << "add_waiter " << std::hex << tag << std::dec << " " << c << " should be ATSUBTREEROOT, " << *this << " is not root, trying parent" << dendl;
1338 inode->parent->dir->add_waiter(tag, c);
1339 return;
1340 }
1341 }
1342
1343 ceph_assert(!(tag & WAIT_CREATED) || state_test(STATE_CREATING));
1344
1345 MDSCacheObject::add_waiter(tag, c);
1346 }
1347
1348
1349
1350 /* NOTE: this checks dentry waiters too */
1351 void CDir::take_waiting(uint64_t mask, MDSContext::vec& ls)
1352 {
1353 if ((mask & WAIT_DENTRY) && !waiting_on_dentry.empty()) {
1354 // take all dentry waiters
1355 for (const auto &p : waiting_on_dentry) {
1356 dout(10) << "take_waiting dentry " << p.first.name
1357 << " snap " << p.first.snapid << " on " << *this << dendl;
1358 for (const auto &waiter : p.second) {
1359 ls.push_back(waiter);
1360 }
1361 }
1362 waiting_on_dentry.clear();
1363 put(PIN_DNWAITER);
1364 }
1365
1366 // waiting
1367 MDSCacheObject::take_waiting(mask, ls);
1368 }
1369
1370
1371 void CDir::finish_waiting(uint64_t mask, int result)
1372 {
1373 dout(11) << __func__ << " mask " << hex << mask << dec << " result " << result << " on " << *this << dendl;
1374
1375 MDSContext::vec finished;
1376 take_waiting(mask, finished);
1377 if (result < 0)
1378 finish_contexts(g_ceph_context, finished, result);
1379 else
1380 mdcache->mds->queue_waiters(finished);
1381 }
1382
1383
1384
1385 // dirty/clean
1386
1387 CDir::fnode_ptr CDir::project_fnode(const MutationRef& mut)
1388 {
1389 ceph_assert(get_version() != 0);
1390
1391 if (mut && mut->is_projected(this))
1392 return std::const_pointer_cast<fnode_t>(projected_fnode.back());
1393
1394 auto pf = allocate_fnode(*get_projected_fnode());
1395
1396 if (scrub_infop && scrub_infop->last_scrub_dirty) {
1397 pf->localized_scrub_stamp = scrub_infop->last_local.time;
1398 pf->localized_scrub_version = scrub_infop->last_local.version;
1399 pf->recursive_scrub_stamp = scrub_infop->last_recursive.time;
1400 pf->recursive_scrub_version = scrub_infop->last_recursive.version;
1401 scrub_infop->last_scrub_dirty = false;
1402 scrub_maybe_delete_info();
1403 }
1404
1405 projected_fnode.emplace_back(pf);
1406 if (mut)
1407 mut->add_projected_node(this);
1408 dout(10) << __func__ << " " << pf.get() << dendl;
1409 return pf;
1410 }
1411
1412 void CDir::pop_and_dirty_projected_fnode(LogSegment *ls, const MutationRef& mut)
1413 {
1414 ceph_assert(!projected_fnode.empty());
1415 auto pf = std::move(projected_fnode.front());
1416 dout(15) << __func__ << " " << pf.get() << " v" << pf->version << dendl;
1417
1418 projected_fnode.pop_front();
1419 if (mut)
1420 mut->remove_projected_node(this);
1421
1422 reset_fnode(std::move(pf));
1423 _mark_dirty(ls);
1424 }
1425
1426 version_t CDir::pre_dirty(version_t min)
1427 {
1428 if (min > projected_version)
1429 projected_version = min;
1430 ++projected_version;
1431 dout(10) << __func__ << " " << projected_version << dendl;
1432 return projected_version;
1433 }
1434
1435 void CDir::mark_dirty(LogSegment *ls, version_t pv)
1436 {
1437 ceph_assert(is_auth());
1438
1439 if (pv) {
1440 ceph_assert(get_version() < pv);
1441 ceph_assert(pv <= projected_version);
1442 ceph_assert(!projected_fnode.empty() &&
1443 pv <= projected_fnode.front()->version);
1444 }
1445
1446 _mark_dirty(ls);
1447 }
1448
1449 void CDir::_mark_dirty(LogSegment *ls)
1450 {
1451 if (!state_test(STATE_DIRTY)) {
1452 dout(10) << __func__ << " (was clean) " << *this << " version " << get_version() << dendl;
1453 _set_dirty_flag();
1454 ceph_assert(ls);
1455 } else {
1456 dout(10) << __func__ << " (already dirty) " << *this << " version " << get_version() << dendl;
1457 }
1458 if (ls) {
1459 ls->dirty_dirfrags.push_back(&item_dirty);
1460
1461 // if i've never committed, i need to be before _any_ mention of me is trimmed from the journal.
1462 if (committed_version == 0 && !item_new.is_on_list())
1463 ls->new_dirfrags.push_back(&item_new);
1464 }
1465 }
1466
1467 void CDir::mark_new(LogSegment *ls)
1468 {
1469 ls->new_dirfrags.push_back(&item_new);
1470 state_clear(STATE_CREATING);
1471
1472 MDSContext::vec waiters;
1473 take_waiting(CDir::WAIT_CREATED, waiters);
1474 mdcache->mds->queue_waiters(waiters);
1475 }
1476
1477 void CDir::mark_clean()
1478 {
1479 dout(10) << __func__ << " " << *this << " version " << get_version() << dendl;
1480 if (state_test(STATE_DIRTY)) {
1481 item_dirty.remove_myself();
1482 item_new.remove_myself();
1483
1484 state_clear(STATE_DIRTY);
1485 put(PIN_DIRTY);
1486 }
1487 }
1488
1489 // caller should hold auth pin of this
1490 void CDir::log_mark_dirty()
1491 {
1492 if (is_dirty() || projected_version > get_version())
1493 return; // noop if it is already dirty or will be dirty
1494
1495 auto _fnode = allocate_fnode(*get_fnode());
1496 _fnode->version = pre_dirty();
1497 reset_fnode(std::move(_fnode));
1498 mark_dirty(mdcache->mds->mdlog->get_current_segment());
1499 }
1500
1501 void CDir::mark_complete() {
1502 state_set(STATE_COMPLETE);
1503 bloom.reset();
1504 }
1505
1506 void CDir::first_get()
1507 {
1508 inode->get(CInode::PIN_DIRFRAG);
1509 }
1510
1511 void CDir::last_put()
1512 {
1513 inode->put(CInode::PIN_DIRFRAG);
1514 }
1515
1516
1517
1518 /******************************************************************************
1519 * FETCH and COMMIT
1520 */
1521
1522 // -----------------------
1523 // FETCH
1524 void CDir::fetch(MDSContext *c, bool ignore_authpinnability)
1525 {
1526 string want;
1527 return fetch(c, want, ignore_authpinnability);
1528 }
1529
1530 void CDir::fetch(MDSContext *c, std::string_view want_dn, bool ignore_authpinnability)
1531 {
1532 dout(10) << "fetch on " << *this << dendl;
1533
1534 ceph_assert(is_auth());
1535 ceph_assert(!is_complete());
1536
1537 if (!can_auth_pin() && !ignore_authpinnability) {
1538 if (c) {
1539 dout(7) << "fetch waiting for authpinnable" << dendl;
1540 add_waiter(WAIT_UNFREEZE, c);
1541 } else
1542 dout(7) << "fetch not authpinnable and no context" << dendl;
1543 return;
1544 }
1545
1546 // unlinked directory inode shouldn't have any entry
1547 if (!inode->is_base() && get_parent_dir()->inode->is_stray() &&
1548 !inode->snaprealm) {
1549 dout(7) << "fetch dirfrag for unlinked directory, mark complete" << dendl;
1550 if (get_version() == 0) {
1551 ceph_assert(inode->is_auth());
1552 auto _fnode = allocate_fnode();
1553 _fnode->version = 1;
1554 reset_fnode(std::move(_fnode));
1555
1556 if (state_test(STATE_REJOINUNDEF)) {
1557 ceph_assert(mdcache->mds->is_rejoin());
1558 state_clear(STATE_REJOINUNDEF);
1559 mdcache->opened_undef_dirfrag(this);
1560 }
1561 }
1562 mark_complete();
1563
1564 if (c)
1565 mdcache->mds->queue_waiter(c);
1566 return;
1567 }
1568
1569 if (c) add_waiter(WAIT_COMPLETE, c);
1570 if (!want_dn.empty()) wanted_items.insert(mempool::mds_co::string(want_dn));
1571
1572 // already fetching?
1573 if (state_test(CDir::STATE_FETCHING)) {
1574 dout(7) << "already fetching; waiting" << dendl;
1575 return;
1576 }
1577
1578 auth_pin(this);
1579 state_set(CDir::STATE_FETCHING);
1580
1581 if (mdcache->mds->logger) mdcache->mds->logger->inc(l_mds_dir_fetch);
1582
1583 mdcache->mds->balancer->hit_dir(this, META_POP_FETCH);
1584
1585 std::set<dentry_key_t> empty;
1586 _omap_fetch(NULL, empty);
1587 }
1588
1589 void CDir::fetch(MDSContext *c, const std::set<dentry_key_t>& keys)
1590 {
1591 dout(10) << "fetch " << keys.size() << " keys on " << *this << dendl;
1592
1593 ceph_assert(is_auth());
1594 ceph_assert(!is_complete());
1595
1596 if (!can_auth_pin()) {
1597 dout(7) << "fetch keys waiting for authpinnable" << dendl;
1598 add_waiter(WAIT_UNFREEZE, c);
1599 return;
1600 }
1601 if (state_test(CDir::STATE_FETCHING)) {
1602 dout(7) << "fetch keys waiting for full fetch" << dendl;
1603 add_waiter(WAIT_COMPLETE, c);
1604 return;
1605 }
1606
1607 auth_pin(this);
1608 if (mdcache->mds->logger) mdcache->mds->logger->inc(l_mds_dir_fetch);
1609
1610 mdcache->mds->balancer->hit_dir(this, META_POP_FETCH);
1611
1612 _omap_fetch(c, keys);
1613 }
1614
1615 class C_IO_Dir_OMAP_FetchedMore : public CDirIOContext {
1616 MDSContext *fin;
1617 public:
1618 const version_t omap_version;
1619 bufferlist hdrbl;
1620 bool more = false;
1621 map<string, bufferlist> omap; ///< carry-over from before
1622 map<string, bufferlist> omap_more; ///< new batch
1623 int ret;
1624 C_IO_Dir_OMAP_FetchedMore(CDir *d, version_t v, MDSContext *f) :
1625 CDirIOContext(d), fin(f), omap_version(v), ret(0) { }
1626 void finish(int r) {
1627 if (omap_version < dir->get_committed_version()) {
1628 omap.clear();
1629 dir->_omap_fetch(fin, {});
1630 return;
1631 }
1632
1633 // merge results
1634 if (omap.empty()) {
1635 omap.swap(omap_more);
1636 } else {
1637 omap.insert(omap_more.begin(), omap_more.end());
1638 }
1639 if (more) {
1640 dir->_omap_fetch_more(omap_version, hdrbl, omap, fin);
1641 } else {
1642 dir->_omap_fetched(hdrbl, omap, !fin, r);
1643 if (fin)
1644 fin->complete(r);
1645 }
1646 }
1647 void print(ostream& out) const override {
1648 out << "dirfrag_fetch_more(" << dir->dirfrag() << ")";
1649 }
1650 };
1651
1652 class C_IO_Dir_OMAP_Fetched : public CDirIOContext {
1653 MDSContext *fin;
1654 public:
1655 const version_t omap_version;
1656 bufferlist hdrbl;
1657 bool more = false;
1658 map<string, bufferlist> omap;
1659 bufferlist btbl;
1660 int ret1, ret2, ret3;
1661
1662 C_IO_Dir_OMAP_Fetched(CDir *d, MDSContext *f) :
1663 CDirIOContext(d), fin(f),
1664 omap_version(d->get_committing_version()),
1665 ret1(0), ret2(0), ret3(0) { }
1666 void finish(int r) override {
1667 // check the correctness of backtrace
1668 if (r >= 0 && ret3 != -CEPHFS_ECANCELED)
1669 dir->inode->verify_diri_backtrace(btbl, ret3);
1670 if (r >= 0) r = ret1;
1671 if (r >= 0) r = ret2;
1672
1673 if (more) {
1674 if (omap_version < dir->get_committed_version()) {
1675 omap.clear();
1676 dir->_omap_fetch(fin, {});
1677 } else {
1678 dir->_omap_fetch_more(omap_version, hdrbl, omap, fin);
1679 }
1680 return;
1681 }
1682
1683 dir->_omap_fetched(hdrbl, omap, !fin, r);
1684 if (fin)
1685 fin->complete(r);
1686
1687 }
1688 void print(ostream& out) const override {
1689 out << "dirfrag_fetch(" << dir->dirfrag() << ")";
1690 }
1691 };
1692
1693 void CDir::_omap_fetch(MDSContext *c, const std::set<dentry_key_t>& keys)
1694 {
1695 C_IO_Dir_OMAP_Fetched *fin = new C_IO_Dir_OMAP_Fetched(this, c);
1696 object_t oid = get_ondisk_object();
1697 object_locator_t oloc(mdcache->mds->mdsmap->get_metadata_pool());
1698 ObjectOperation rd;
1699 rd.omap_get_header(&fin->hdrbl, &fin->ret1);
1700 if (keys.empty()) {
1701 ceph_assert(!c);
1702 rd.omap_get_vals("", "", g_conf()->mds_dir_keys_per_op,
1703 &fin->omap, &fin->more, &fin->ret2);
1704 } else {
1705 ceph_assert(c);
1706 std::set<std::string> str_keys;
1707 for (auto p : keys) {
1708 string str;
1709 p.encode(str);
1710 str_keys.insert(str);
1711 }
1712 rd.omap_get_vals_by_keys(str_keys, &fin->omap, &fin->ret2);
1713 }
1714 // check the correctness of backtrace
1715 if (g_conf()->mds_verify_backtrace > 0 && frag == frag_t()) {
1716 rd.getxattr("parent", &fin->btbl, &fin->ret3);
1717 rd.set_last_op_flags(CEPH_OSD_OP_FLAG_FAILOK);
1718 } else {
1719 fin->ret3 = -CEPHFS_ECANCELED;
1720 }
1721
1722 mdcache->mds->objecter->read(oid, oloc, rd, CEPH_NOSNAP, NULL, 0,
1723 new C_OnFinisher(fin, mdcache->mds->finisher));
1724 }
1725
1726 void CDir::_omap_fetch_more(version_t omap_version, bufferlist& hdrbl,
1727 map<string, bufferlist>& omap, MDSContext *c)
1728 {
1729 // we have more omap keys to fetch!
1730 object_t oid = get_ondisk_object();
1731 object_locator_t oloc(mdcache->mds->mdsmap->get_metadata_pool());
1732 auto fin = new C_IO_Dir_OMAP_FetchedMore(this, omap_version, c);
1733 fin->hdrbl = std::move(hdrbl);
1734 fin->omap.swap(omap);
1735 ObjectOperation rd;
1736 rd.omap_get_vals(fin->omap.rbegin()->first,
1737 "", /* filter prefix */
1738 g_conf()->mds_dir_keys_per_op,
1739 &fin->omap_more,
1740 &fin->more,
1741 &fin->ret);
1742 mdcache->mds->objecter->read(oid, oloc, rd, CEPH_NOSNAP, NULL, 0,
1743 new C_OnFinisher(fin, mdcache->mds->finisher));
1744 }
1745
1746 CDentry *CDir::_load_dentry(
1747 std::string_view key,
1748 std::string_view dname,
1749 const snapid_t last,
1750 bufferlist &bl,
1751 const int pos,
1752 const std::set<snapid_t> *snaps,
1753 double rand_threshold,
1754 bool *force_dirty)
1755 {
1756 auto q = bl.cbegin();
1757
1758 snapid_t first;
1759 decode(first, q);
1760
1761 // marker
1762 char type;
1763 decode(type, q);
1764
1765 dout(20) << "_fetched pos " << pos << " marker '" << type << "' dname '" << dname
1766 << " [" << first << "," << last << "]"
1767 << dendl;
1768
1769 bool stale = false;
1770 if (snaps && last != CEPH_NOSNAP) {
1771 set<snapid_t>::const_iterator p = snaps->lower_bound(first);
1772 if (p == snaps->end() || *p > last) {
1773 dout(10) << " skipping stale dentry on [" << first << "," << last << "]" << dendl;
1774 stale = true;
1775 }
1776 }
1777
1778 /*
1779 * look for existing dentry for _last_ snap, because unlink +
1780 * create may leave a "hole" (epochs during which the dentry
1781 * doesn't exist) but for which no explicit negative dentry is in
1782 * the cache.
1783 */
1784 CDentry *dn;
1785 if (stale)
1786 dn = lookup_exact_snap(dname, last);
1787 else
1788 dn = lookup(dname, last);
1789
1790 if (type == 'L' || type == 'l') {
1791 // hard link
1792 inodeno_t ino;
1793 unsigned char d_type;
1794 mempool::mds_co::string alternate_name;
1795
1796 CDentry::decode_remote(type, ino, d_type, alternate_name, q);
1797
1798 if (stale) {
1799 if (!dn) {
1800 stale_items.insert(mempool::mds_co::string(key));
1801 *force_dirty = true;
1802 }
1803 return dn;
1804 }
1805
1806 if (dn) {
1807 CDentry::linkage_t *dnl = dn->get_linkage();
1808 dout(12) << "_fetched had " << (dnl->is_null() ? "NEG" : "") << " dentry " << *dn << dendl;
1809 if (committed_version == 0 &&
1810 dnl->is_remote() &&
1811 dn->is_dirty() &&
1812 ino == dnl->get_remote_ino() &&
1813 d_type == dnl->get_remote_d_type() &&
1814 alternate_name == dn->get_alternate_name()) {
1815 // see comment below
1816 dout(10) << "_fetched had underwater dentry " << *dn << ", marking clean" << dendl;
1817 dn->mark_clean();
1818 }
1819 } else {
1820 // (remote) link
1821 dn = add_remote_dentry(dname, ino, d_type, std::move(alternate_name), first, last);
1822
1823 // link to inode?
1824 CInode *in = mdcache->get_inode(ino); // we may or may not have it.
1825 if (in) {
1826 dn->link_remote(dn->get_linkage(), in);
1827 dout(12) << "_fetched got remote link " << ino << " which we have " << *in << dendl;
1828 } else {
1829 dout(12) << "_fetched got remote link " << ino << " (don't have it)" << dendl;
1830 }
1831 }
1832 }
1833 else if (type == 'I' || type == 'i') {
1834 InodeStore inode_data;
1835 mempool::mds_co::string alternate_name;
1836 // inode
1837 // Load inode data before looking up or constructing CInode
1838 if (type == 'i') {
1839 DECODE_START(2, q);
1840 if (struct_v >= 2) {
1841 decode(alternate_name, q);
1842 }
1843 inode_data.decode(q);
1844 DECODE_FINISH(q);
1845 } else {
1846 inode_data.decode_bare(q);
1847 }
1848
1849 if (stale) {
1850 if (!dn) {
1851 stale_items.insert(mempool::mds_co::string(key));
1852 *force_dirty = true;
1853 }
1854 return dn;
1855 }
1856
1857 bool undef_inode = false;
1858 if (dn) {
1859 CDentry::linkage_t *dnl = dn->get_linkage();
1860 dout(12) << "_fetched had " << (dnl->is_null() ? "NEG" : "") << " dentry " << *dn << dendl;
1861
1862 if (dnl->is_primary()) {
1863 CInode *in = dnl->get_inode();
1864 if (in->state_test(CInode::STATE_REJOINUNDEF)) {
1865 undef_inode = true;
1866 } else if (committed_version == 0 &&
1867 dn->is_dirty() &&
1868 inode_data.inode->ino == in->ino() &&
1869 inode_data.inode->version == in->get_version()) {
1870 /* clean underwater item?
1871 * Underwater item is something that is dirty in our cache from
1872 * journal replay, but was previously flushed to disk before the
1873 * mds failed.
1874 *
1875 * We only do this is committed_version == 0. that implies either
1876 * - this is a fetch after from a clean/empty CDir is created
1877 * (and has no effect, since the dn won't exist); or
1878 * - this is a fetch after _recovery_, which is what we're worried
1879 * about. Items that are marked dirty from the journal should be
1880 * marked clean if they appear on disk.
1881 */
1882 dout(10) << "_fetched had underwater dentry " << *dn << ", marking clean" << dendl;
1883 dn->mark_clean();
1884 dout(10) << "_fetched had underwater inode " << *dnl->get_inode() << ", marking clean" << dendl;
1885 in->mark_clean();
1886 }
1887 }
1888 }
1889
1890 if (!dn || undef_inode) {
1891 // add inode
1892 CInode *in = mdcache->get_inode(inode_data.inode->ino, last);
1893 if (!in || undef_inode) {
1894 if (undef_inode && in)
1895 in->first = first;
1896 else
1897 in = new CInode(mdcache, true, first, last);
1898
1899 in->reset_inode(std::move(inode_data.inode));
1900 in->reset_xattrs(std::move(inode_data.xattrs));
1901 // symlink?
1902 if (in->is_symlink())
1903 in->symlink = inode_data.symlink;
1904
1905 in->dirfragtree.swap(inode_data.dirfragtree);
1906 in->reset_old_inodes(std::move(inode_data.old_inodes));
1907 if (in->is_any_old_inodes()) {
1908 snapid_t min_first = in->get_old_inodes()->rbegin()->first + 1;
1909 if (min_first > in->first)
1910 in->first = min_first;
1911 }
1912
1913 in->oldest_snap = inode_data.oldest_snap;
1914 in->decode_snap_blob(inode_data.snap_blob);
1915 if (snaps && !in->snaprealm)
1916 in->purge_stale_snap_data(*snaps);
1917
1918 if (!undef_inode) {
1919 mdcache->add_inode(in); // add
1920 dn = add_primary_dentry(dname, in, std::move(alternate_name), first, last); // link
1921 }
1922 dout(12) << "_fetched got " << *dn << " " << *in << dendl;
1923
1924 if (in->get_inode()->is_dirty_rstat())
1925 in->mark_dirty_rstat();
1926
1927 in->maybe_ephemeral_rand(rand_threshold);
1928 //in->hack_accessed = false;
1929 //in->hack_load_stamp = ceph_clock_now();
1930 //num_new_inodes_loaded++;
1931 } else if (g_conf().get_val<bool>("mds_hack_allow_loading_invalid_metadata")) {
1932 dout(20) << "hack: adding duplicate dentry for " << *in << dendl;
1933 dn = add_primary_dentry(dname, in, std::move(alternate_name), first, last);
1934 } else {
1935 dout(0) << "_fetched badness: got (but i already had) " << *in
1936 << " mode " << in->get_inode()->mode
1937 << " mtime " << in->get_inode()->mtime << dendl;
1938 string dirpath, inopath;
1939 this->inode->make_path_string(dirpath);
1940 in->make_path_string(inopath);
1941 mdcache->mds->clog->error() << "loaded dup inode " << inode_data.inode->ino
1942 << " [" << first << "," << last << "] v" << inode_data.inode->version
1943 << " at " << dirpath << "/" << dname
1944 << ", but inode " << in->vino() << " v" << in->get_version()
1945 << " already exists at " << inopath;
1946 return dn;
1947 }
1948 }
1949 } else {
1950 CachedStackStringStream css;
1951 *css << "Invalid tag char '" << type << "' pos " << pos;
1952 throw buffer::malformed_input(css->str());
1953 }
1954
1955 return dn;
1956 }
1957
1958 void CDir::_omap_fetched(bufferlist& hdrbl, map<string, bufferlist>& omap,
1959 bool complete, int r)
1960 {
1961 LogChannelRef clog = mdcache->mds->clog;
1962 dout(10) << "_fetched header " << hdrbl.length() << " bytes "
1963 << omap.size() << " keys for " << *this << dendl;
1964
1965 ceph_assert(r == 0 || r == -CEPHFS_ENOENT || r == -CEPHFS_ENODATA);
1966 ceph_assert(is_auth());
1967 ceph_assert(!is_frozen());
1968
1969 if (hdrbl.length() == 0) {
1970 dout(0) << "_fetched missing object for " << *this << dendl;
1971
1972 clog->error() << "dir " << dirfrag() << " object missing on disk; some "
1973 "files may be lost (" << get_path() << ")";
1974
1975 go_bad(complete);
1976 return;
1977 }
1978
1979 fnode_t got_fnode;
1980 {
1981 auto p = hdrbl.cbegin();
1982 try {
1983 decode(got_fnode, p);
1984 } catch (const buffer::error &err) {
1985 derr << "Corrupt fnode in dirfrag " << dirfrag()
1986 << ": " << err.what() << dendl;
1987 clog->warn() << "Corrupt fnode header in " << dirfrag() << ": "
1988 << err.what() << " (" << get_path() << ")";
1989 go_bad(complete);
1990 return;
1991 }
1992 if (!p.end()) {
1993 clog->warn() << "header buffer of dir " << dirfrag() << " has "
1994 << hdrbl.length() - p.get_off() << " extra bytes ("
1995 << get_path() << ")";
1996 go_bad(complete);
1997 return;
1998 }
1999 }
2000
2001 dout(10) << "_fetched version " << got_fnode.version << dendl;
2002
2003 // take the loaded fnode?
2004 // only if we are a fresh CDir* with no prior state.
2005 if (get_version() == 0) {
2006 ceph_assert(!is_projected());
2007 ceph_assert(!state_test(STATE_COMMITTING));
2008 auto _fnode = allocate_fnode(got_fnode);
2009 reset_fnode(std::move(_fnode));
2010 projected_version = committing_version = committed_version = get_version();
2011
2012 if (state_test(STATE_REJOINUNDEF)) {
2013 ceph_assert(mdcache->mds->is_rejoin());
2014 state_clear(STATE_REJOINUNDEF);
2015 mdcache->opened_undef_dirfrag(this);
2016 }
2017 }
2018
2019 list<CInode*> undef_inodes;
2020
2021 // purge stale snaps?
2022 bool force_dirty = false;
2023 const set<snapid_t> *snaps = NULL;
2024 SnapRealm *realm = inode->find_snaprealm();
2025 if (fnode->snap_purged_thru < realm->get_last_destroyed()) {
2026 snaps = &realm->get_snaps();
2027 dout(10) << " snap_purged_thru " << fnode->snap_purged_thru
2028 << " < " << realm->get_last_destroyed()
2029 << ", snap purge based on " << *snaps << dendl;
2030 if (get_num_snap_items() == 0) {
2031 const_cast<snapid_t&>(fnode->snap_purged_thru) = realm->get_last_destroyed();
2032 force_dirty = true;
2033 }
2034 }
2035
2036 unsigned pos = omap.size() - 1;
2037 double rand_threshold = get_inode()->get_ephemeral_rand();
2038 for (map<string, bufferlist>::reverse_iterator p = omap.rbegin();
2039 p != omap.rend();
2040 ++p, --pos) {
2041 string dname;
2042 snapid_t last;
2043 dentry_key_t::decode_helper(p->first, dname, last);
2044
2045 CDentry *dn = NULL;
2046 try {
2047 dn = _load_dentry(
2048 p->first, dname, last, p->second, pos, snaps,
2049 rand_threshold, &force_dirty);
2050 } catch (const buffer::error &err) {
2051 mdcache->mds->clog->warn() << "Corrupt dentry '" << dname << "' in "
2052 "dir frag " << dirfrag() << ": "
2053 << err.what() << "(" << get_path() << ")";
2054
2055 // Remember that this dentry is damaged. Subsequent operations
2056 // that try to act directly on it will get their CEPHFS_EIOs, but this
2057 // dirfrag as a whole will continue to look okay (minus the
2058 // mysteriously-missing dentry)
2059 go_bad_dentry(last, dname);
2060
2061 // Anyone who was WAIT_DENTRY for this guy will get kicked
2062 // to RetryRequest, and hit the DamageTable-interrogating path.
2063 // Stats will now be bogus because we will think we're complete,
2064 // but have 1 or more missing dentries.
2065 continue;
2066 }
2067
2068 if (!dn)
2069 continue;
2070
2071 CDentry::linkage_t *dnl = dn->get_linkage();
2072 if (dnl->is_primary() && dnl->get_inode()->state_test(CInode::STATE_REJOINUNDEF))
2073 undef_inodes.push_back(dnl->get_inode());
2074
2075 if (wanted_items.count(mempool::mds_co::string(dname)) > 0 || !complete) {
2076 dout(10) << " touching wanted dn " << *dn << dendl;
2077 mdcache->touch_dentry(dn);
2078 }
2079 }
2080
2081 //cache->mds->logger->inc("newin", num_new_inodes_loaded);
2082
2083 // mark complete, !fetching
2084 if (complete) {
2085 wanted_items.clear();
2086 mark_complete();
2087 state_clear(STATE_FETCHING);
2088 }
2089
2090 // open & force frags
2091 while (!undef_inodes.empty()) {
2092 CInode *in = undef_inodes.front();
2093 undef_inodes.pop_front();
2094 in->state_clear(CInode::STATE_REJOINUNDEF);
2095 mdcache->opened_undef_inode(in);
2096 }
2097
2098 // dirty myself to remove stale snap dentries
2099 if (force_dirty && !mdcache->is_readonly())
2100 log_mark_dirty();
2101
2102 auth_unpin(this);
2103
2104 if (complete) {
2105 // kick waiters
2106 finish_waiting(WAIT_COMPLETE, 0);
2107 }
2108 }
2109
2110 void CDir::go_bad_dentry(snapid_t last, std::string_view dname)
2111 {
2112 dout(10) << __func__ << " " << dname << dendl;
2113 std::string path(get_path());
2114 path += "/";
2115 path += dname;
2116 const bool fatal = mdcache->mds->damage_table.notify_dentry(
2117 inode->ino(), frag, last, dname, path);
2118 if (fatal) {
2119 mdcache->mds->damaged();
2120 ceph_abort(); // unreachable, damaged() respawns us
2121 }
2122 }
2123
2124 void CDir::go_bad(bool complete)
2125 {
2126 dout(10) << __func__ << " " << frag << dendl;
2127 const bool fatal = mdcache->mds->damage_table.notify_dirfrag(
2128 inode->ino(), frag, get_path());
2129 if (fatal) {
2130 mdcache->mds->damaged();
2131 ceph_abort(); // unreachable, damaged() respawns us
2132 }
2133
2134 if (complete) {
2135 if (get_version() == 0) {
2136 auto _fnode = allocate_fnode();
2137 _fnode->version = 1;
2138 reset_fnode(std::move(_fnode));
2139 }
2140
2141 state_set(STATE_BADFRAG);
2142 mark_complete();
2143 }
2144
2145 state_clear(STATE_FETCHING);
2146 auth_unpin(this);
2147 finish_waiting(WAIT_COMPLETE, -CEPHFS_EIO);
2148 }
2149
2150 // -----------------------
2151 // COMMIT
2152
2153 /**
2154 * commit
2155 *
2156 * @param want - min version i want committed
2157 * @param c - callback for completion
2158 */
2159 void CDir::commit(version_t want, MDSContext *c, bool ignore_authpinnability, int op_prio)
2160 {
2161 dout(10) << "commit want " << want << " on " << *this << dendl;
2162 if (want == 0) want = get_version();
2163
2164 // preconditions
2165 ceph_assert(want <= get_version() || get_version() == 0); // can't commit the future
2166 ceph_assert(want > committed_version); // the caller is stupid
2167 ceph_assert(is_auth());
2168 ceph_assert(ignore_authpinnability || can_auth_pin());
2169
2170 // note: queue up a noop if necessary, so that we always
2171 // get an auth_pin.
2172 if (!c)
2173 c = new C_MDSInternalNoop;
2174
2175 // auth_pin on first waiter
2176 if (waiting_for_commit.empty())
2177 auth_pin(this);
2178 waiting_for_commit[want].push_back(c);
2179
2180 // ok.
2181 _commit(want, op_prio);
2182 }
2183
2184 class C_IO_Dir_Committed : public CDirIOContext {
2185 version_t version;
2186 public:
2187 C_IO_Dir_Committed(CDir *d, version_t v) : CDirIOContext(d), version(v) { }
2188 void finish(int r) override {
2189 dir->_committed(r, version);
2190 }
2191 void print(ostream& out) const override {
2192 out << "dirfrag_committed(" << dir->dirfrag() << ")";
2193 }
2194 };
2195
2196 class C_IO_Dir_Commit_Ops : public Context {
2197 public:
2198 C_IO_Dir_Commit_Ops(CDir *d, int pr,
2199 vector<CDir::dentry_commit_item> &&s, bufferlist &&bl,
2200 vector<string> &&r,
2201 mempool::mds_co::compact_set<mempool::mds_co::string> &&stales) :
2202 dir(d), op_prio(pr) {
2203 metapool = dir->mdcache->mds->get_metadata_pool();
2204 version = dir->get_version();
2205 is_new = dir->is_new();
2206 to_set.swap(s);
2207 dfts.swap(bl);
2208 to_remove.swap(r);
2209 stale_items.swap(stales);
2210 }
2211
2212 void finish(int r) override {
2213 dir->_omap_commit_ops(r, op_prio, metapool, version, is_new, to_set, dfts,
2214 to_remove, stale_items);
2215 }
2216
2217 private:
2218 CDir *dir;
2219 int op_prio;
2220 int64_t metapool;
2221 version_t version;
2222 bool is_new;
2223 vector<CDir::dentry_commit_item> to_set;
2224 bufferlist dfts;
2225 vector<string> to_remove;
2226 mempool::mds_co::compact_set<mempool::mds_co::string> stale_items;
2227 };
2228
2229 // This is doing the same thing with the InodeStoreBase::encode()
2230 void CDir::_encode_primary_inode_base(dentry_commit_item &item, bufferlist &dfts,
2231 bufferlist &bl)
2232 {
2233 ENCODE_START(6, 4, bl);
2234 encode(*item.inode, bl, item.features);
2235
2236 if (!item.symlink.empty())
2237 encode(item.symlink, bl);
2238
2239 // dirfragtree
2240 dfts.splice(0, item.dft_len, &bl);
2241
2242 if (item.xattrs)
2243 encode(*item.xattrs, bl);
2244 else
2245 encode((__u32)0, bl);
2246
2247 if (item.snaprealm) {
2248 bufferlist snapr_bl;
2249 encode(item.srnode, snapr_bl);
2250 encode(snapr_bl, bl);
2251 } else {
2252 encode(bufferlist(), bl);
2253 }
2254
2255 if (item.old_inodes)
2256 encode(*item.old_inodes, bl, item.features);
2257 else
2258 encode((__u32)0, bl);
2259
2260 encode(item.oldest_snap, bl);
2261 encode(item.damage_flags, bl);
2262 ENCODE_FINISH(bl);
2263 }
2264
2265 // This is not locked by mds_lock
2266 void CDir::_omap_commit_ops(int r, int op_prio, int64_t metapool, version_t version, bool _new,
2267 vector<dentry_commit_item> &to_set, bufferlist &dfts,
2268 vector<string>& to_remove,
2269 mempool::mds_co::compact_set<mempool::mds_co::string> &stales)
2270 {
2271 dout(10) << __func__ << dendl;
2272
2273 if (r < 0) {
2274 mdcache->mds->handle_write_error_with_lock(r);
2275 return;
2276 }
2277
2278 C_GatherBuilder gather(g_ceph_context,
2279 new C_OnFinisher(new C_IO_Dir_Committed(this, version),
2280 mdcache->mds->finisher));
2281
2282 SnapContext snapc;
2283 object_t oid = get_ondisk_object();
2284 object_locator_t oloc(metapool);
2285
2286 map<string, bufferlist> _set;
2287 set<string> _rm;
2288
2289 unsigned max_write_size = mdcache->max_dir_commit_size;
2290 unsigned write_size = 0;
2291
2292 auto commit_one = [&](bool header=false) {
2293 ObjectOperation op;
2294
2295 // don't create new dirfrag blindly
2296 if (!_new)
2297 op.stat(nullptr, nullptr, nullptr);
2298
2299 /*
2300 * save the header at the last moment.. If we were to send it off before
2301 * other updates, but die before sending them all, we'd think that the
2302 * on-disk state was fully committed even though it wasn't! However, since
2303 * the messages are strictly ordered between the MDS and the OSD, and
2304 * since messages to a given PG are strictly ordered, if we simply send
2305 * the message containing the header off last, we cannot get our header
2306 * into an incorrect state.
2307 */
2308 if (header) {
2309 bufferlist header;
2310 encode(*fnode, header);
2311 op.omap_set_header(header);
2312 }
2313
2314 op.priority = op_prio;
2315 if (!_set.empty())
2316 op.omap_set(_set);
2317 if (!_rm.empty())
2318 op.omap_rm_keys(_rm);
2319 mdcache->mds->objecter->mutate(oid, oloc, op, snapc,
2320 ceph::real_clock::now(),
2321 0, gather.new_sub());
2322 write_size = 0;
2323 _set.clear();
2324 _rm.clear();
2325 };
2326
2327 for (auto &key : stales) {
2328 unsigned size = key.length() + sizeof(__u32);
2329 if (write_size + size > max_write_size)
2330 commit_one();
2331
2332 write_size += size;
2333 _rm.emplace(key);
2334 }
2335
2336 for (auto &key : to_remove) {
2337 unsigned size = key.length() + sizeof(__u32);
2338 if (write_size + size > max_write_size)
2339 commit_one();
2340
2341 write_size += size;
2342 _rm.emplace(std::move(key));
2343 }
2344
2345 bufferlist bl;
2346 using ceph::encode;
2347 for (auto &item : to_set) {
2348 encode(item.first, bl);
2349 if (item.is_remote) {
2350 // remote link
2351 CDentry::encode_remote(item.ino, item.d_type, item.alternate_name, bl);
2352 } else {
2353 // marker, name, inode, [symlink string]
2354 bl.append('i'); // inode
2355
2356 ENCODE_START(2, 1, bl);
2357 encode(item.alternate_name, bl);
2358 _encode_primary_inode_base(item, dfts, bl);
2359 ENCODE_FINISH(bl);
2360 }
2361
2362 unsigned size = item.key.length() + bl.length() + 2 * sizeof(__u32);
2363 if (write_size + size > max_write_size)
2364 commit_one();
2365
2366 write_size += size;
2367 _set[std::move(item.key)].swap(bl);
2368 }
2369
2370 commit_one(true);
2371 gather.activate();
2372 }
2373
2374 /**
2375 * Flush out the modified dentries in this dir. Keep the bufferlist
2376 * below max_write_size;
2377 */
2378 void CDir::_omap_commit(int op_prio)
2379 {
2380 dout(10) << __func__ << dendl;
2381
2382 if (op_prio < 0)
2383 op_prio = CEPH_MSG_PRIO_DEFAULT;
2384
2385 // snap purge?
2386 const set<snapid_t> *snaps = NULL;
2387 SnapRealm *realm = inode->find_snaprealm();
2388 if (fnode->snap_purged_thru < realm->get_last_destroyed()) {
2389 snaps = &realm->get_snaps();
2390 dout(10) << " snap_purged_thru " << fnode->snap_purged_thru
2391 << " < " << realm->get_last_destroyed()
2392 << ", snap purge based on " << *snaps << dendl;
2393 // fnode.snap_purged_thru = realm->get_last_destroyed();
2394 }
2395
2396 size_t count = 0;
2397 if (state_test(CDir::STATE_FRAGMENTING) && is_new()) {
2398 count = get_num_head_items() + get_num_snap_items();
2399 } else {
2400 for (elist<CDentry*>::iterator it = dirty_dentries.begin(); !it.end(); ++it)
2401 ++count;
2402 }
2403
2404 vector<string> to_remove;
2405 // reverve enough memories, which maybe larger than the actually needed
2406 to_remove.reserve(count);
2407
2408 vector<dentry_commit_item> to_set;
2409 // reverve enough memories, which maybe larger than the actually needed
2410 to_set.reserve(count);
2411
2412 // for dir fragtrees
2413 bufferlist dfts(CEPH_PAGE_SIZE);
2414
2415 auto write_one = [&](CDentry *dn) {
2416 string key;
2417 dn->key().encode(key);
2418
2419 if (dn->last != CEPH_NOSNAP &&
2420 snaps && try_trim_snap_dentry(dn, *snaps)) {
2421 dout(10) << " rm " << key << dendl;
2422 to_remove.emplace_back(std::move(key));
2423 return;
2424 }
2425
2426 if (dn->get_linkage()->is_null()) {
2427 dout(10) << " rm " << dn->get_name() << " " << *dn << dendl;
2428 to_remove.emplace_back(std::move(key));
2429 } else {
2430 dout(10) << " set " << dn->get_name() << " " << *dn << dendl;
2431
2432 uint64_t off = dfts.length();
2433 // try to reserve new size if there has less
2434 // than 1/8 page space
2435 uint64_t left = CEPH_PAGE_SIZE - off % CEPH_PAGE_SIZE;
2436 if (left < CEPH_PAGE_SIZE / 8)
2437 dfts.reserve(left + CEPH_PAGE_SIZE);
2438
2439 auto& item = to_set.emplace_back();
2440 item.key = std::move(key);
2441 _parse_dentry(dn, item, snaps, dfts);
2442 item.dft_len = dfts.length() - off;
2443 }
2444 };
2445
2446 if (state_test(CDir::STATE_FRAGMENTING) && is_new()) {
2447 ceph_assert(committed_version == 0);
2448 for (auto p = items.begin(); p != items.end(); ) {
2449 CDentry *dn = p->second;
2450 ++p;
2451 if (dn->get_linkage()->is_null())
2452 continue;
2453 write_one(dn);
2454 }
2455 } else {
2456 for (auto p = dirty_dentries.begin(); !p.end(); ) {
2457 CDentry *dn = *p;
2458 ++p;
2459 write_one(dn);
2460 }
2461 }
2462
2463 auto c = new C_IO_Dir_Commit_Ops(this, op_prio, std::move(to_set), std::move(dfts),
2464 std::move(to_remove), std::move(stale_items));
2465 stale_items.clear();
2466 mdcache->mds->finisher->queue(c);
2467 }
2468
2469 void CDir::_parse_dentry(CDentry *dn, dentry_commit_item &item,
2470 const set<snapid_t> *snaps, bufferlist &bl)
2471 {
2472 // clear dentry NEW flag, if any. we can no longer silently drop it.
2473 dn->clear_new();
2474
2475 item.first = dn->first;
2476
2477 // primary or remote?
2478 auto& linkage = dn->linkage;
2479 item.alternate_name = dn->get_alternate_name();
2480 if (linkage.is_remote()) {
2481 item.is_remote = true;
2482 item.ino = linkage.get_remote_ino();
2483 item.d_type = linkage.get_remote_d_type();
2484 dout(14) << " dn '" << dn->get_name() << "' remote ino " << item.ino << dendl;
2485 } else if (linkage.is_primary()) {
2486 // primary link
2487 CInode *in = linkage.get_inode();
2488 ceph_assert(in);
2489
2490 dout(14) << " dn '" << dn->get_name() << "' inode " << *in << dendl;
2491
2492 if (in->is_multiversion()) {
2493 if (!in->snaprealm) {
2494 if (snaps)
2495 in->purge_stale_snap_data(*snaps);
2496 } else {
2497 in->purge_stale_snap_data(in->snaprealm->get_snaps());
2498 }
2499 }
2500
2501 if (in->snaprealm) {
2502 item.snaprealm = true;
2503 item.srnode = in->snaprealm->srnode;
2504 }
2505 item.features = mdcache->mds->mdsmap->get_up_features();
2506 item.inode = in->inode;
2507 if (in->inode->is_symlink())
2508 item.symlink = in->symlink;
2509 using ceph::encode;
2510 encode(in->dirfragtree, bl);
2511 item.xattrs = in->xattrs;
2512 item.old_inodes = in->old_inodes;
2513 item.oldest_snap = in->oldest_snap;
2514 item.damage_flags = in->damage_flags;
2515 } else {
2516 ceph_assert(!linkage.is_null());
2517 }
2518 }
2519
2520 void CDir::_commit(version_t want, int op_prio)
2521 {
2522 dout(10) << "_commit want " << want << " on " << *this << dendl;
2523
2524 // we can't commit things in the future.
2525 // (even the projected future.)
2526 ceph_assert(want <= get_version() || get_version() == 0);
2527
2528 // check pre+postconditions.
2529 ceph_assert(is_auth());
2530
2531 // already committed?
2532 if (committed_version >= want) {
2533 dout(10) << "already committed " << committed_version << " >= " << want << dendl;
2534 return;
2535 }
2536 // already committing >= want?
2537 if (committing_version >= want) {
2538 dout(10) << "already committing " << committing_version << " >= " << want << dendl;
2539 ceph_assert(state_test(STATE_COMMITTING));
2540 return;
2541 }
2542
2543 // alrady committed an older version?
2544 if (committing_version > committed_version) {
2545 dout(10) << "already committing older " << committing_version << ", waiting for that to finish" << dendl;
2546 return;
2547 }
2548
2549 // commit.
2550 committing_version = get_version();
2551
2552 // mark committing (if not already)
2553 if (!state_test(STATE_COMMITTING)) {
2554 dout(10) << "marking committing" << dendl;
2555 state_set(STATE_COMMITTING);
2556 }
2557
2558 if (mdcache->mds->logger) mdcache->mds->logger->inc(l_mds_dir_commit);
2559
2560 mdcache->mds->balancer->hit_dir(this, META_POP_STORE);
2561
2562 _omap_commit(op_prio);
2563 }
2564
2565
2566 /**
2567 * _committed
2568 *
2569 * @param v version i just committed
2570 */
2571 void CDir::_committed(int r, version_t v)
2572 {
2573 if (r < 0) {
2574 // the directory could be partly purged during MDS failover
2575 if (r == -CEPHFS_ENOENT && committed_version == 0 &&
2576 !inode->is_base() && get_parent_dir()->inode->is_stray()) {
2577 r = 0;
2578 if (inode->snaprealm)
2579 inode->state_set(CInode::STATE_MISSINGOBJS);
2580 }
2581 if (r < 0) {
2582 dout(1) << "commit error " << r << " v " << v << dendl;
2583 mdcache->mds->clog->error() << "failed to commit dir " << dirfrag() << " object,"
2584 << " errno " << r;
2585 mdcache->mds->handle_write_error(r);
2586 return;
2587 }
2588 }
2589
2590 dout(10) << "_committed v " << v << " on " << *this << dendl;
2591 ceph_assert(is_auth());
2592
2593 bool stray = inode->is_stray();
2594
2595 // take note.
2596 ceph_assert(v > committed_version);
2597 ceph_assert(v <= committing_version);
2598 committed_version = v;
2599
2600 // _all_ commits done?
2601 if (committing_version == committed_version)
2602 state_clear(CDir::STATE_COMMITTING);
2603
2604 // _any_ commit, even if we've been redirtied, means we're no longer new.
2605 item_new.remove_myself();
2606
2607 // dir clean?
2608 if (committed_version == get_version())
2609 mark_clean();
2610
2611 // dentries clean?
2612 for (auto p = dirty_dentries.begin(); !p.end(); ) {
2613 CDentry *dn = *p;
2614 ++p;
2615
2616 // inode?
2617 if (dn->linkage.is_primary()) {
2618 CInode *in = dn->linkage.get_inode();
2619 ceph_assert(in);
2620 ceph_assert(in->is_auth());
2621
2622 if (committed_version >= in->get_version()) {
2623 if (in->is_dirty()) {
2624 dout(15) << " dir " << committed_version << " >= inode " << in->get_version() << " now clean " << *in << dendl;
2625 in->mark_clean();
2626 }
2627 } else {
2628 dout(15) << " dir " << committed_version << " < inode " << in->get_version() << " still dirty " << *in << dendl;
2629 ceph_assert(in->is_dirty() || in->last < CEPH_NOSNAP); // special case for cow snap items (not predirtied)
2630 }
2631 }
2632
2633 // dentry
2634 if (committed_version >= dn->get_version()) {
2635 dout(15) << " dir " << committed_version << " >= dn " << dn->get_version() << " now clean " << *dn << dendl;
2636 dn->mark_clean();
2637
2638 // drop clean null stray dentries immediately
2639 if (stray &&
2640 dn->get_num_ref() == 0 &&
2641 !dn->is_projected() &&
2642 dn->get_linkage()->is_null())
2643 remove_dentry(dn);
2644 } else {
2645 dout(15) << " dir " << committed_version << " < dn " << dn->get_version() << " still dirty " << *dn << dendl;
2646 ceph_assert(dn->is_dirty());
2647 }
2648 }
2649
2650 // finishers?
2651 bool were_waiters = !waiting_for_commit.empty();
2652
2653 auto it = waiting_for_commit.begin();
2654 while (it != waiting_for_commit.end()) {
2655 auto _it = it;
2656 ++_it;
2657 if (it->first > committed_version) {
2658 dout(10) << " there are waiters for " << it->first << ", committing again" << dendl;
2659 _commit(it->first, -1);
2660 break;
2661 }
2662 MDSContext::vec t;
2663 for (const auto &waiter : it->second)
2664 t.push_back(waiter);
2665 mdcache->mds->queue_waiters(t);
2666 waiting_for_commit.erase(it);
2667 it = _it;
2668 }
2669
2670 // try drop dentries in this dirfrag if it's about to be purged
2671 if (!inode->is_base() && get_parent_dir()->inode->is_stray() &&
2672 inode->snaprealm)
2673 mdcache->maybe_eval_stray(inode, true);
2674
2675 // unpin if we kicked the last waiter.
2676 if (were_waiters &&
2677 waiting_for_commit.empty())
2678 auth_unpin(this);
2679 }
2680
2681
2682
2683
2684 // IMPORT/EXPORT
2685
2686 mds_rank_t CDir::get_export_pin(bool inherit) const
2687 {
2688 mds_rank_t export_pin = inode->get_export_pin(inherit);
2689 if (export_pin == MDS_RANK_EPHEMERAL_DIST)
2690 export_pin = mdcache->hash_into_rank_bucket(ino(), get_frag());
2691 else if (export_pin == MDS_RANK_EPHEMERAL_RAND)
2692 export_pin = mdcache->hash_into_rank_bucket(ino());
2693 return export_pin;
2694 }
2695
2696 bool CDir::is_exportable(mds_rank_t dest) const
2697 {
2698 mds_rank_t export_pin = get_export_pin();
2699 if (export_pin == dest)
2700 return true;
2701 if (export_pin >= 0)
2702 return false;
2703 return true;
2704 }
2705
2706 void CDir::encode_export(bufferlist& bl)
2707 {
2708 ENCODE_START(1, 1, bl);
2709 ceph_assert(!is_projected());
2710 encode(first, bl);
2711 encode(*fnode, bl);
2712 encode(dirty_old_rstat, bl);
2713 encode(committed_version, bl);
2714
2715 encode(state, bl);
2716 encode(dir_rep, bl);
2717
2718 encode(pop_me, bl);
2719 encode(pop_auth_subtree, bl);
2720
2721 encode(dir_rep_by, bl);
2722 encode(get_replicas(), bl);
2723
2724 get(PIN_TEMPEXPORTING);
2725 ENCODE_FINISH(bl);
2726 }
2727
2728 void CDir::finish_export()
2729 {
2730 state &= MASK_STATE_EXPORT_KEPT;
2731 pop_nested.sub(pop_auth_subtree);
2732 pop_auth_subtree_nested.sub(pop_auth_subtree);
2733 pop_me.zero();
2734 pop_auth_subtree.zero();
2735 put(PIN_TEMPEXPORTING);
2736 dirty_old_rstat.clear();
2737 }
2738
2739 void CDir::decode_import(bufferlist::const_iterator& blp, LogSegment *ls)
2740 {
2741 DECODE_START(1, blp);
2742 decode(first, blp);
2743 {
2744 auto _fnode = allocate_fnode();
2745 decode(*_fnode, blp);
2746 reset_fnode(std::move(_fnode));
2747 }
2748 update_projected_version();
2749
2750 decode(dirty_old_rstat, blp);
2751 decode(committed_version, blp);
2752 committing_version = committed_version;
2753
2754 unsigned s;
2755 decode(s, blp);
2756 state &= MASK_STATE_IMPORT_KEPT;
2757 state_set(STATE_AUTH | (s & MASK_STATE_EXPORTED));
2758
2759 if (is_dirty()) {
2760 get(PIN_DIRTY);
2761 _mark_dirty(ls);
2762 }
2763
2764 decode(dir_rep, blp);
2765
2766 decode(pop_me, blp);
2767 decode(pop_auth_subtree, blp);
2768 pop_nested.add(pop_auth_subtree);
2769 pop_auth_subtree_nested.add(pop_auth_subtree);
2770
2771 decode(dir_rep_by, blp);
2772 decode(get_replicas(), blp);
2773 if (is_replicated()) get(PIN_REPLICATED);
2774
2775 replica_nonce = 0; // no longer defined
2776
2777 // did we import some dirty scatterlock data?
2778 if (dirty_old_rstat.size() ||
2779 !(fnode->rstat == fnode->accounted_rstat)) {
2780 mdcache->mds->locker->mark_updated_scatterlock(&inode->nestlock);
2781 ls->dirty_dirfrag_nest.push_back(&inode->item_dirty_dirfrag_nest);
2782 }
2783 if (!(fnode->fragstat == fnode->accounted_fragstat)) {
2784 mdcache->mds->locker->mark_updated_scatterlock(&inode->filelock);
2785 ls->dirty_dirfrag_dir.push_back(&inode->item_dirty_dirfrag_dir);
2786 }
2787 if (is_dirty_dft()) {
2788 if (inode->dirfragtreelock.get_state() != LOCK_MIX &&
2789 inode->dirfragtreelock.is_stable()) {
2790 // clear stale dirtydft
2791 state_clear(STATE_DIRTYDFT);
2792 } else {
2793 mdcache->mds->locker->mark_updated_scatterlock(&inode->dirfragtreelock);
2794 ls->dirty_dirfrag_dirfragtree.push_back(&inode->item_dirty_dirfrag_dirfragtree);
2795 }
2796 }
2797 DECODE_FINISH(blp);
2798 }
2799
2800 void CDir::abort_import()
2801 {
2802 ceph_assert(is_auth());
2803 state_clear(CDir::STATE_AUTH);
2804 remove_bloom();
2805 clear_replica_map();
2806 set_replica_nonce(CDir::EXPORT_NONCE);
2807 if (is_dirty())
2808 mark_clean();
2809
2810 pop_nested.sub(pop_auth_subtree);
2811 pop_auth_subtree_nested.sub(pop_auth_subtree);
2812 pop_me.zero();
2813 pop_auth_subtree.zero();
2814 }
2815
2816 void CDir::encode_dirstat(bufferlist& bl, const session_info_t& info, const DirStat& ds) {
2817 if (info.has_feature(CEPHFS_FEATURE_REPLY_ENCODING)) {
2818 ENCODE_START(1, 1, bl);
2819 encode(ds.frag, bl);
2820 encode(ds.auth, bl);
2821 encode(ds.dist, bl);
2822 ENCODE_FINISH(bl);
2823 }
2824 else {
2825 encode(ds.frag, bl);
2826 encode(ds.auth, bl);
2827 encode(ds.dist, bl);
2828 }
2829 }
2830
2831 /********************************
2832 * AUTHORITY
2833 */
2834
2835 /*
2836 * if dir_auth.first == parent, auth is same as inode.
2837 * unless .second != unknown, in which case that sticks.
2838 */
2839 mds_authority_t CDir::authority() const
2840 {
2841 if (is_subtree_root())
2842 return dir_auth;
2843 else
2844 return inode->authority();
2845 }
2846
2847 /** is_subtree_root()
2848 * true if this is an auth delegation point.
2849 * that is, dir_auth != default (parent,unknown)
2850 *
2851 * some key observations:
2852 * if i am auth:
2853 * - any region bound will be an export, or frozen.
2854 *
2855 * note that this DOES heed dir_auth.pending
2856 */
2857 /*
2858 bool CDir::is_subtree_root()
2859 {
2860 if (dir_auth == CDIR_AUTH_DEFAULT) {
2861 //dout(10) << "is_subtree_root false " << dir_auth << " != " << CDIR_AUTH_DEFAULT
2862 //<< " on " << ino() << dendl;
2863 return false;
2864 } else {
2865 //dout(10) << "is_subtree_root true " << dir_auth << " != " << CDIR_AUTH_DEFAULT
2866 //<< " on " << ino() << dendl;
2867 return true;
2868 }
2869 }
2870 */
2871
2872 /** contains(x)
2873 * true if we are x, or an ancestor of x
2874 */
2875 bool CDir::contains(CDir *x)
2876 {
2877 while (1) {
2878 if (x == this)
2879 return true;
2880 x = x->get_inode()->get_projected_parent_dir();
2881 if (x == 0)
2882 return false;
2883 }
2884 }
2885
2886 bool CDir::can_rep() const
2887 {
2888 if (!is_rep())
2889 return true;
2890
2891 unsigned mds_num = mdcache->mds->get_mds_map()->get_num_mds(MDSMap::STATE_ACTIVE);
2892 if ((mds_num - 1) > get_replicas().size())
2893 return true;
2894
2895 return false;
2896 }
2897
2898
2899 /** set_dir_auth
2900 */
2901 void CDir::set_dir_auth(const mds_authority_t &a)
2902 {
2903 dout(10) << "setting dir_auth=" << a
2904 << " from " << dir_auth
2905 << " on " << *this << dendl;
2906
2907 bool was_subtree = is_subtree_root();
2908 bool was_ambiguous = dir_auth.second >= 0;
2909
2910 // set it.
2911 dir_auth = a;
2912
2913 // new subtree root?
2914 if (!was_subtree && is_subtree_root()) {
2915 dout(10) << " new subtree root, adjusting auth_pins" << dendl;
2916
2917 if (freeze_tree_state) {
2918 // only by CDir::_freeze_tree()
2919 ceph_assert(is_freezing_tree_root());
2920 }
2921
2922 inode->num_subtree_roots++;
2923
2924 // unpin parent of frozen dir/tree?
2925 if (inode->is_auth()) {
2926 ceph_assert(!is_frozen_tree_root());
2927 if (is_frozen_dir())
2928 inode->auth_unpin(this);
2929 }
2930 }
2931 if (was_subtree && !is_subtree_root()) {
2932 dout(10) << " old subtree root, adjusting auth_pins" << dendl;
2933
2934 inode->num_subtree_roots--;
2935
2936 // pin parent of frozen dir/tree?
2937 if (inode->is_auth()) {
2938 ceph_assert(!is_frozen_tree_root());
2939 if (is_frozen_dir())
2940 inode->auth_pin(this);
2941 }
2942 }
2943
2944 // newly single auth?
2945 if (was_ambiguous && dir_auth.second == CDIR_AUTH_UNKNOWN) {
2946 MDSContext::vec ls;
2947 take_waiting(WAIT_SINGLEAUTH, ls);
2948 mdcache->mds->queue_waiters(ls);
2949 }
2950 }
2951
2952 /*****************************************
2953 * AUTH PINS and FREEZING
2954 *
2955 * the basic plan is that auth_pins only exist in auth regions, and they
2956 * prevent a freeze (and subsequent auth change).
2957 *
2958 * however, we also need to prevent a parent from freezing if a child is frozen.
2959 * for that reason, the parent inode of a frozen directory is auth_pinned.
2960 *
2961 * the oddity is when the frozen directory is a subtree root. if that's the case,
2962 * the parent inode isn't frozen. which means that when subtree authority is adjusted
2963 * at the bounds, inodes for any frozen bound directories need to get auth_pins at that
2964 * time.
2965 *
2966 */
2967
2968 void CDir::auth_pin(void *by)
2969 {
2970 if (auth_pins == 0)
2971 get(PIN_AUTHPIN);
2972 auth_pins++;
2973
2974 #ifdef MDS_AUTHPIN_SET
2975 auth_pin_set.insert(by);
2976 #endif
2977
2978 dout(10) << "auth_pin by " << by << " on " << *this << " count now " << auth_pins << dendl;
2979
2980 if (freeze_tree_state)
2981 freeze_tree_state->auth_pins += 1;
2982 }
2983
2984 void CDir::auth_unpin(void *by)
2985 {
2986 auth_pins--;
2987
2988 #ifdef MDS_AUTHPIN_SET
2989 {
2990 auto it = auth_pin_set.find(by);
2991 ceph_assert(it != auth_pin_set.end());
2992 auth_pin_set.erase(it);
2993 }
2994 #endif
2995 if (auth_pins == 0)
2996 put(PIN_AUTHPIN);
2997
2998 dout(10) << "auth_unpin by " << by << " on " << *this << " count now " << auth_pins << dendl;
2999 ceph_assert(auth_pins >= 0);
3000
3001 if (freeze_tree_state)
3002 freeze_tree_state->auth_pins -= 1;
3003
3004 maybe_finish_freeze(); // pending freeze?
3005 }
3006
3007 void CDir::adjust_nested_auth_pins(int dirinc, void *by)
3008 {
3009 ceph_assert(dirinc);
3010 dir_auth_pins += dirinc;
3011
3012 dout(15) << __func__ << " " << dirinc << " on " << *this
3013 << " by " << by << " count now "
3014 << auth_pins << "/" << dir_auth_pins << dendl;
3015 ceph_assert(dir_auth_pins >= 0);
3016
3017 if (freeze_tree_state)
3018 freeze_tree_state->auth_pins += dirinc;
3019
3020 if (dirinc < 0)
3021 maybe_finish_freeze(); // pending freeze?
3022 }
3023
3024 #ifdef MDS_VERIFY_FRAGSTAT
3025 void CDir::verify_fragstat()
3026 {
3027 ceph_assert(is_complete());
3028 if (inode->is_stray())
3029 return;
3030
3031 frag_info_t c;
3032 memset(&c, 0, sizeof(c));
3033
3034 for (auto it = items.begin();
3035 it != items.end();
3036 ++it) {
3037 CDentry *dn = it->second;
3038 if (dn->is_null())
3039 continue;
3040
3041 dout(10) << " " << *dn << dendl;
3042 if (dn->is_primary())
3043 dout(10) << " " << *dn->inode << dendl;
3044
3045 if (dn->is_primary()) {
3046 if (dn->inode->is_dir())
3047 c.nsubdirs++;
3048 else
3049 c.nfiles++;
3050 }
3051 if (dn->is_remote()) {
3052 if (dn->get_remote_d_type() == DT_DIR)
3053 c.nsubdirs++;
3054 else
3055 c.nfiles++;
3056 }
3057 }
3058
3059 if (c.nsubdirs != fnode->fragstat.nsubdirs ||
3060 c.nfiles != fnode->fragstat.nfiles) {
3061 dout(0) << "verify_fragstat failed " << fnode->fragstat << " on " << *this << dendl;
3062 dout(0) << " i count " << c << dendl;
3063 ceph_abort();
3064 } else {
3065 dout(0) << "verify_fragstat ok " << fnode->fragstat << " on " << *this << dendl;
3066 }
3067 }
3068 #endif
3069
3070 /*****************************************************************************
3071 * FREEZING
3072 */
3073
3074 // FREEZE TREE
3075
3076 void CDir::_walk_tree(std::function<bool(CDir*)> callback)
3077 {
3078 deque<CDir*> dfq;
3079 dfq.push_back(this);
3080
3081 while (!dfq.empty()) {
3082 CDir *dir = dfq.front();
3083 dfq.pop_front();
3084
3085 for (auto& p : *dir) {
3086 CDentry *dn = p.second;
3087 if (!dn->get_linkage()->is_primary())
3088 continue;
3089 CInode *in = dn->get_linkage()->get_inode();
3090 if (!in->is_dir())
3091 continue;
3092
3093 auto&& dfv = in->get_nested_dirfrags();
3094 for (auto& dir : dfv) {
3095 auto ret = callback(dir);
3096 if (ret)
3097 dfq.push_back(dir);
3098 }
3099 }
3100 }
3101 }
3102
3103 bool CDir::freeze_tree()
3104 {
3105 ceph_assert(!is_frozen());
3106 ceph_assert(!is_freezing());
3107 ceph_assert(!freeze_tree_state);
3108
3109 auth_pin(this);
3110
3111 // Travese the subtree to mark dirfrags as 'freezing' (set freeze_tree_state)
3112 // and to accumulate auth pins and record total count in freeze_tree_state.
3113 // when auth unpin an 'freezing' object, the counter in freeze_tree_state also
3114 // gets decreased. Subtree become 'frozen' when the counter reaches zero.
3115 freeze_tree_state = std::make_shared<freeze_tree_state_t>(this);
3116 freeze_tree_state->auth_pins += get_auth_pins() + get_dir_auth_pins();
3117 if (!lock_caches_with_auth_pins.empty())
3118 mdcache->mds->locker->invalidate_lock_caches(this);
3119
3120 _walk_tree([this](CDir *dir) {
3121 if (dir->freeze_tree_state)
3122 return false;
3123 dir->freeze_tree_state = freeze_tree_state;
3124 freeze_tree_state->auth_pins += dir->get_auth_pins() + dir->get_dir_auth_pins();
3125 if (!dir->lock_caches_with_auth_pins.empty())
3126 mdcache->mds->locker->invalidate_lock_caches(dir);
3127 return true;
3128 }
3129 );
3130
3131 if (is_freezeable(true)) {
3132 _freeze_tree();
3133 auth_unpin(this);
3134 return true;
3135 } else {
3136 state_set(STATE_FREEZINGTREE);
3137 ++num_freezing_trees;
3138 dout(10) << "freeze_tree waiting " << *this << dendl;
3139 return false;
3140 }
3141 }
3142
3143 void CDir::_freeze_tree()
3144 {
3145 dout(10) << __func__ << " " << *this << dendl;
3146 ceph_assert(is_freezeable(true));
3147
3148 if (freeze_tree_state) {
3149 ceph_assert(is_auth());
3150 } else {
3151 ceph_assert(!is_auth());
3152 freeze_tree_state = std::make_shared<freeze_tree_state_t>(this);
3153 }
3154 freeze_tree_state->frozen = true;
3155
3156 if (is_auth()) {
3157 mds_authority_t auth;
3158 bool was_subtree = is_subtree_root();
3159 if (was_subtree) {
3160 auth = get_dir_auth();
3161 } else {
3162 // temporarily prevent parent subtree from becoming frozen.
3163 inode->auth_pin(this);
3164 // create new subtree
3165 auth = authority();
3166 }
3167
3168 _walk_tree([this, &auth] (CDir *dir) {
3169 if (dir->freeze_tree_state != freeze_tree_state) {
3170 mdcache->adjust_subtree_auth(dir, auth);
3171 return false;
3172 }
3173 return true;
3174 }
3175 );
3176
3177 ceph_assert(auth.first >= 0);
3178 ceph_assert(auth.second == CDIR_AUTH_UNKNOWN);
3179 auth.second = auth.first;
3180 mdcache->adjust_subtree_auth(this, auth);
3181 if (!was_subtree)
3182 inode->auth_unpin(this);
3183 } else {
3184 // importing subtree ?
3185 _walk_tree([this] (CDir *dir) {
3186 ceph_assert(!dir->freeze_tree_state);
3187 dir->freeze_tree_state = freeze_tree_state;
3188 return true;
3189 }
3190 );
3191 }
3192
3193 // twiddle state
3194 if (state_test(STATE_FREEZINGTREE)) {
3195 state_clear(STATE_FREEZINGTREE);
3196 --num_freezing_trees;
3197 }
3198
3199 state_set(STATE_FROZENTREE);
3200 ++num_frozen_trees;
3201 get(PIN_FROZEN);
3202 }
3203
3204 void CDir::unfreeze_tree()
3205 {
3206 dout(10) << __func__ << " " << *this << dendl;
3207
3208 MDSContext::vec unfreeze_waiters;
3209 take_waiting(WAIT_UNFREEZE, unfreeze_waiters);
3210
3211 if (freeze_tree_state) {
3212 _walk_tree([this, &unfreeze_waiters](CDir *dir) {
3213 if (dir->freeze_tree_state != freeze_tree_state)
3214 return false;
3215 dir->freeze_tree_state.reset();
3216 dir->take_waiting(WAIT_UNFREEZE, unfreeze_waiters);
3217 return true;
3218 }
3219 );
3220 }
3221
3222 if (state_test(STATE_FROZENTREE)) {
3223 // frozen. unfreeze.
3224 state_clear(STATE_FROZENTREE);
3225 --num_frozen_trees;
3226
3227 put(PIN_FROZEN);
3228
3229 if (is_auth()) {
3230 // must be subtree
3231 ceph_assert(is_subtree_root());
3232 // for debug purpose, caller should ensure 'dir_auth.second == dir_auth.first'
3233 mds_authority_t auth = get_dir_auth();
3234 ceph_assert(auth.first >= 0);
3235 ceph_assert(auth.second == auth.first);
3236 auth.second = CDIR_AUTH_UNKNOWN;
3237 mdcache->adjust_subtree_auth(this, auth);
3238 }
3239 freeze_tree_state.reset();
3240 } else {
3241 ceph_assert(state_test(STATE_FREEZINGTREE));
3242
3243 // freezing. stop it.
3244 state_clear(STATE_FREEZINGTREE);
3245 --num_freezing_trees;
3246 freeze_tree_state.reset();
3247
3248 finish_waiting(WAIT_FROZEN, -1);
3249 auth_unpin(this);
3250 }
3251
3252 mdcache->mds->queue_waiters(unfreeze_waiters);
3253 }
3254
3255 void CDir::adjust_freeze_after_rename(CDir *dir)
3256 {
3257 if (!freeze_tree_state || dir->freeze_tree_state != freeze_tree_state)
3258 return;
3259 CDir *newdir = dir->get_inode()->get_parent_dir();
3260 if (newdir == this || newdir->freeze_tree_state == freeze_tree_state)
3261 return;
3262
3263 ceph_assert(!freeze_tree_state->frozen);
3264 ceph_assert(get_dir_auth_pins() > 0);
3265
3266 MDSContext::vec unfreeze_waiters;
3267
3268 auto unfreeze = [this, &unfreeze_waiters](CDir *dir) {
3269 if (dir->freeze_tree_state != freeze_tree_state)
3270 return false;
3271 int dec = dir->get_auth_pins() + dir->get_dir_auth_pins();
3272 // shouldn't become zero because srcdn of rename was auth pinned
3273 ceph_assert(freeze_tree_state->auth_pins > dec);
3274 freeze_tree_state->auth_pins -= dec;
3275 dir->freeze_tree_state.reset();
3276 dir->take_waiting(WAIT_UNFREEZE, unfreeze_waiters);
3277 return true;
3278 };
3279
3280 unfreeze(dir);
3281 dir->_walk_tree(unfreeze);
3282
3283 mdcache->mds->queue_waiters(unfreeze_waiters);
3284 }
3285
3286 bool CDir::can_auth_pin(int *err_ret) const
3287 {
3288 int err;
3289 if (!is_auth()) {
3290 err = ERR_NOT_AUTH;
3291 } else if (is_freezing_dir() || is_frozen_dir()) {
3292 err = ERR_FRAGMENTING_DIR;
3293 } else {
3294 auto p = is_freezing_or_frozen_tree();
3295 if (p.first || p.second) {
3296 err = ERR_EXPORTING_TREE;
3297 } else {
3298 err = 0;
3299 }
3300 }
3301 if (err && err_ret)
3302 *err_ret = err;
3303 return !err;
3304 }
3305
3306 class C_Dir_AuthUnpin : public CDirContext {
3307 public:
3308 explicit C_Dir_AuthUnpin(CDir *d) : CDirContext(d) {}
3309 void finish(int r) override {
3310 dir->auth_unpin(dir->get_inode());
3311 }
3312 };
3313
3314 void CDir::maybe_finish_freeze()
3315 {
3316 if (dir_auth_pins != 0)
3317 return;
3318
3319 // we can freeze the _dir_ even with nested pins...
3320 if (state_test(STATE_FREEZINGDIR)) {
3321 if (auth_pins == 1) {
3322 _freeze_dir();
3323 auth_unpin(this);
3324 finish_waiting(WAIT_FROZEN);
3325 }
3326 }
3327
3328 if (freeze_tree_state) {
3329 if (freeze_tree_state->frozen ||
3330 freeze_tree_state->auth_pins != 1)
3331 return;
3332
3333 if (freeze_tree_state->dir != this) {
3334 freeze_tree_state->dir->maybe_finish_freeze();
3335 return;
3336 }
3337
3338 ceph_assert(state_test(STATE_FREEZINGTREE));
3339
3340 if (!is_subtree_root() && inode->is_frozen()) {
3341 dout(10) << __func__ << " !subtree root and frozen inode, waiting for unfreeze on " << inode << dendl;
3342 // retake an auth_pin...
3343 auth_pin(inode);
3344 // and release it when the parent inode unfreezes
3345 inode->add_waiter(WAIT_UNFREEZE, new C_Dir_AuthUnpin(this));
3346 return;
3347 }
3348
3349 _freeze_tree();
3350 auth_unpin(this);
3351 finish_waiting(WAIT_FROZEN);
3352 }
3353 }
3354
3355
3356
3357 // FREEZE DIR
3358
3359 bool CDir::freeze_dir()
3360 {
3361 ceph_assert(!is_frozen());
3362 ceph_assert(!is_freezing());
3363
3364 auth_pin(this);
3365 if (is_freezeable_dir(true)) {
3366 _freeze_dir();
3367 auth_unpin(this);
3368 return true;
3369 } else {
3370 state_set(STATE_FREEZINGDIR);
3371 if (!lock_caches_with_auth_pins.empty())
3372 mdcache->mds->locker->invalidate_lock_caches(this);
3373 dout(10) << "freeze_dir + wait " << *this << dendl;
3374 return false;
3375 }
3376 }
3377
3378 void CDir::_freeze_dir()
3379 {
3380 dout(10) << __func__ << " " << *this << dendl;
3381 //assert(is_freezeable_dir(true));
3382 // not always true during split because the original fragment may have frozen a while
3383 // ago and we're just now getting around to breaking it up.
3384
3385 state_clear(STATE_FREEZINGDIR);
3386 state_set(STATE_FROZENDIR);
3387 get(PIN_FROZEN);
3388
3389 if (is_auth() && !is_subtree_root())
3390 inode->auth_pin(this); // auth_pin for duration of freeze
3391 }
3392
3393
3394 void CDir::unfreeze_dir()
3395 {
3396 dout(10) << __func__ << " " << *this << dendl;
3397
3398 if (state_test(STATE_FROZENDIR)) {
3399 state_clear(STATE_FROZENDIR);
3400 put(PIN_FROZEN);
3401
3402 // unpin (may => FREEZEABLE) FIXME: is this order good?
3403 if (is_auth() && !is_subtree_root())
3404 inode->auth_unpin(this);
3405
3406 finish_waiting(WAIT_UNFREEZE);
3407 } else {
3408 finish_waiting(WAIT_FROZEN, -1);
3409
3410 // still freezing. stop.
3411 ceph_assert(state_test(STATE_FREEZINGDIR));
3412 state_clear(STATE_FREEZINGDIR);
3413 auth_unpin(this);
3414
3415 finish_waiting(WAIT_UNFREEZE);
3416 }
3417 }
3418
3419 void CDir::enable_frozen_inode()
3420 {
3421 ceph_assert(frozen_inode_suppressed > 0);
3422 if (--frozen_inode_suppressed == 0) {
3423 for (auto p = freezing_inodes.begin(); !p.end(); ) {
3424 CInode *in = *p;
3425 ++p;
3426 ceph_assert(in->is_freezing_inode());
3427 in->maybe_finish_freeze_inode();
3428 }
3429 }
3430 }
3431
3432 /**
3433 * Slightly less complete than operator<<, because this is intended
3434 * for identifying a directory and its state rather than for dumping
3435 * debug output.
3436 */
3437 void CDir::dump(Formatter *f, int flags) const
3438 {
3439 ceph_assert(f != NULL);
3440 if (flags & DUMP_PATH) {
3441 f->dump_stream("path") << get_path();
3442 }
3443 if (flags & DUMP_DIRFRAG) {
3444 f->dump_stream("dirfrag") << dirfrag();
3445 }
3446 if (flags & DUMP_SNAPID_FIRST) {
3447 f->dump_int("snapid_first", first);
3448 }
3449 if (flags & DUMP_VERSIONS) {
3450 f->dump_stream("projected_version") << get_projected_version();
3451 f->dump_stream("version") << get_version();
3452 f->dump_stream("committing_version") << get_committing_version();
3453 f->dump_stream("committed_version") << get_committed_version();
3454 }
3455 if (flags & DUMP_REP) {
3456 f->dump_bool("is_rep", is_rep());
3457 }
3458 if (flags & DUMP_DIR_AUTH) {
3459 if (get_dir_auth() != CDIR_AUTH_DEFAULT) {
3460 if (get_dir_auth().second == CDIR_AUTH_UNKNOWN) {
3461 f->dump_stream("dir_auth") << get_dir_auth().first;
3462 } else {
3463 f->dump_stream("dir_auth") << get_dir_auth();
3464 }
3465 } else {
3466 f->dump_string("dir_auth", "");
3467 }
3468 }
3469 if (flags & DUMP_STATES) {
3470 f->open_array_section("states");
3471 MDSCacheObject::dump_states(f);
3472 if (state_test(CDir::STATE_COMPLETE)) f->dump_string("state", "complete");
3473 if (state_test(CDir::STATE_FREEZINGTREE)) f->dump_string("state", "freezingtree");
3474 if (state_test(CDir::STATE_FROZENTREE)) f->dump_string("state", "frozentree");
3475 if (state_test(CDir::STATE_FROZENDIR)) f->dump_string("state", "frozendir");
3476 if (state_test(CDir::STATE_FREEZINGDIR)) f->dump_string("state", "freezingdir");
3477 if (state_test(CDir::STATE_EXPORTBOUND)) f->dump_string("state", "exportbound");
3478 if (state_test(CDir::STATE_IMPORTBOUND)) f->dump_string("state", "importbound");
3479 if (state_test(CDir::STATE_BADFRAG)) f->dump_string("state", "badfrag");
3480 f->close_section();
3481 }
3482 if (flags & DUMP_MDS_CACHE_OBJECT) {
3483 MDSCacheObject::dump(f);
3484 }
3485 if (flags & DUMP_ITEMS) {
3486 f->open_array_section("dentries");
3487 for (auto &p : items) {
3488 CDentry *dn = p.second;
3489 f->open_object_section("dentry");
3490 dn->dump(f);
3491 f->close_section();
3492 }
3493 f->close_section();
3494 }
3495 }
3496
3497 void CDir::dump_load(Formatter *f)
3498 {
3499 f->dump_stream("path") << get_path();
3500 f->dump_stream("dirfrag") << dirfrag();
3501
3502 f->open_object_section("pop_me");
3503 pop_me.dump(f);
3504 f->close_section();
3505
3506 f->open_object_section("pop_nested");
3507 pop_nested.dump(f);
3508 f->close_section();
3509
3510 f->open_object_section("pop_auth_subtree");
3511 pop_auth_subtree.dump(f);
3512 f->close_section();
3513
3514 f->open_object_section("pop_auth_subtree_nested");
3515 pop_auth_subtree_nested.dump(f);
3516 f->close_section();
3517 }
3518
3519 /****** Scrub Stuff *******/
3520
3521 void CDir::scrub_info_create() const
3522 {
3523 ceph_assert(!scrub_infop);
3524
3525 // break out of const-land to set up implicit initial state
3526 CDir *me = const_cast<CDir*>(this);
3527 const auto& pf = me->get_projected_fnode();
3528
3529 std::unique_ptr<scrub_info_t> si(new scrub_info_t());
3530
3531 si->last_recursive.version = pf->recursive_scrub_version;
3532 si->last_recursive.time = pf->recursive_scrub_stamp;
3533
3534 si->last_local.version = pf->localized_scrub_version;
3535 si->last_local.time = pf->localized_scrub_stamp;
3536
3537 me->scrub_infop.swap(si);
3538 }
3539
3540 void CDir::scrub_initialize(const ScrubHeaderRef& header)
3541 {
3542 ceph_assert(header);
3543 // FIXME: weird implicit construction, is someone else meant
3544 // to be calling scrub_info_create first?
3545 scrub_info();
3546 scrub_infop->directory_scrubbing = true;
3547 scrub_infop->header = header;
3548 header->inc_num_pending();
3549 }
3550
3551 void CDir::scrub_aborted() {
3552 dout(20) << __func__ << dendl;
3553 ceph_assert(scrub_is_in_progress());
3554
3555 scrub_infop->last_scrub_dirty = false;
3556 scrub_infop->directory_scrubbing = false;
3557 scrub_infop->header->dec_num_pending();
3558 scrub_infop.reset();
3559 }
3560
3561 void CDir::scrub_finished()
3562 {
3563 dout(20) << __func__ << dendl;
3564 ceph_assert(scrub_is_in_progress());
3565
3566 scrub_infop->last_local.time = ceph_clock_now();
3567 scrub_infop->last_local.version = get_version();
3568 if (scrub_infop->header->get_recursive())
3569 scrub_infop->last_recursive = scrub_infop->last_local;
3570
3571 scrub_infop->last_scrub_dirty = true;
3572
3573 scrub_infop->directory_scrubbing = false;
3574 scrub_infop->header->dec_num_pending();
3575 }
3576
3577 void CDir::scrub_maybe_delete_info()
3578 {
3579 if (scrub_infop &&
3580 !scrub_infop->directory_scrubbing &&
3581 !scrub_infop->last_scrub_dirty)
3582 scrub_infop.reset();
3583 }
3584
3585 bool CDir::scrub_local()
3586 {
3587 ceph_assert(is_complete());
3588 bool good = check_rstats(true);
3589 if (!good && scrub_infop->header->get_repair()) {
3590 mdcache->repair_dirfrag_stats(this);
3591 scrub_infop->header->set_repaired();
3592 }
3593 return good;
3594 }
3595
3596 std::string CDir::get_path() const
3597 {
3598 std::string path;
3599 get_inode()->make_path_string(path, true);
3600 return path;
3601 }
3602
3603 bool CDir::should_split_fast() const
3604 {
3605 // Max size a fragment can be before trigger fast splitting
3606 int fast_limit = g_conf()->mds_bal_split_size * g_conf()->mds_bal_fragment_fast_factor;
3607
3608 // Fast path: the sum of accounted size and null dentries does not
3609 // exceed threshold: we definitely are not over it.
3610 if (get_frag_size() + get_num_head_null() <= fast_limit) {
3611 return false;
3612 }
3613
3614 // Fast path: the accounted size of the frag exceeds threshold: we
3615 // definitely are over it
3616 if (get_frag_size() > fast_limit) {
3617 return true;
3618 }
3619
3620 int64_t effective_size = 0;
3621
3622 for (const auto &p : items) {
3623 const CDentry *dn = p.second;
3624 if (!dn->get_projected_linkage()->is_null()) {
3625 effective_size++;
3626 }
3627 }
3628
3629 return effective_size > fast_limit;
3630 }
3631
3632 bool CDir::should_merge() const
3633 {
3634 if (get_frag() == frag_t())
3635 return false;
3636
3637 if (inode->is_ephemeral_dist()) {
3638 unsigned min_frag_bits = mdcache->get_ephemeral_dist_frag_bits();
3639 if (min_frag_bits > 0 && get_frag().bits() < min_frag_bits + 1)
3640 return false;
3641 }
3642
3643 return (int)get_frag_size() < g_conf()->mds_bal_merge_size;
3644 }
3645
3646 MEMPOOL_DEFINE_OBJECT_FACTORY(CDir, co_dir, mds_co);
3647 MEMPOOL_DEFINE_OBJECT_FACTORY(CDir::scrub_info_t, scrub_info_t, mds_co)