]> git.proxmox.com Git - ceph.git/blame - ceph/src/mds/StrayManager.cc
update sources to v12.2.5
[ceph.git] / ceph / src / mds / StrayManager.cc
CommitLineData
7c673cae
FG
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) 2015 Red Hat
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15
16#include "common/perf_counters.h"
17
18#include "mds/MDSRank.h"
19#include "mds/MDCache.h"
20#include "mds/MDLog.h"
21#include "mds/CDir.h"
22#include "mds/CDentry.h"
23#include "events/EUpdate.h"
24#include "messages/MClientRequest.h"
25
26#include "StrayManager.h"
27
28#define dout_context g_ceph_context
29#define dout_subsys ceph_subsys_mds
30#undef dout_prefix
31#define dout_prefix _prefix(_dout, mds)
32static ostream& _prefix(std::ostream *_dout, MDSRank *mds) {
33 return *_dout << "mds." << mds->get_nodeid() << ".cache.strays ";
34}
35
36class StrayManagerIOContext : public virtual MDSIOContextBase {
37protected:
38 StrayManager *sm;
39 MDSRank *get_mds() override
40 {
41 return sm->mds;
42 }
43public:
44 explicit StrayManagerIOContext(StrayManager *sm_) : sm(sm_) {}
45};
46
47class StrayManagerLogContext : public virtual MDSLogContextBase {
48protected:
49 StrayManager *sm;
50 MDSRank *get_mds() override
51 {
52 return sm->mds;
53 }
54public:
55 explicit StrayManagerLogContext(StrayManager *sm_) : sm(sm_) {}
56};
57
58class StrayManagerContext : public virtual MDSInternalContextBase {
59protected:
60 StrayManager *sm;
61 MDSRank *get_mds() override
62 {
63 return sm->mds;
64 }
65public:
66 explicit StrayManagerContext(StrayManager *sm_) : sm(sm_) {}
67};
68
69
70/**
71 * Context wrapper for _purge_stray_purged completion
72 */
73class C_IO_PurgeStrayPurged : public StrayManagerIOContext {
74 CDentry *dn;
75 bool only_head;
7c673cae
FG
76public:
77 C_IO_PurgeStrayPurged(StrayManager *sm_, CDentry *d, bool oh) :
78 StrayManagerIOContext(sm_), dn(d), only_head(oh) { }
79 void finish(int r) override {
80 assert(r == 0 || r == -ENOENT);
d2e6a577 81 sm->_purge_stray_purged(dn, only_head);
7c673cae
FG
82 }
83};
84
85
86void StrayManager::purge(CDentry *dn)
87{
88 CDentry::linkage_t *dnl = dn->get_projected_linkage();
89 CInode *in = dnl->get_inode();
90 dout(10) << __func__ << " " << *dn << " " << *in << dendl;
91 assert(!dn->is_replicated());
92
93 // CHEAT. there's no real need to journal our intent to purge, since
94 // that is implicit in the dentry's presence and non-use in the stray
95 // dir. on recovery, we'll need to re-eval all strays anyway.
96
97 SnapContext nullsnapc;
98
99 PurgeItem item;
100 item.ino = in->inode.ino;
101 if (in->is_dir()) {
102 item.action = PurgeItem::PURGE_DIR;
103 item.fragtree = in->dirfragtree;
104 } else {
105 item.action = PurgeItem::PURGE_FILE;
106
107 const SnapContext *snapc;
108 SnapRealm *realm = in->find_snaprealm();
109 if (realm) {
110 dout(10) << " realm " << *realm << dendl;
111 snapc = &realm->get_snap_context();
112 } else {
113 dout(10) << " NO realm, using null context" << dendl;
114 snapc = &nullsnapc;
115 assert(in->last == CEPH_NOSNAP);
116 }
117
118 uint64_t to = 0;
119 if (in->is_file()) {
120 to = in->inode.get_max_size();
121 to = MAX(in->inode.size, to);
122 // when truncating a file, the filer does not delete stripe objects that are
123 // truncated to zero. so we need to purge stripe objects up to the max size
124 // the file has ever been.
125 to = MAX(in->inode.max_size_ever, to);
126 }
127
94b18763 128 auto pi = in->get_projected_inode();
7c673cae
FG
129
130 item.size = to;
131 item.layout = pi->layout;
94b18763
FG
132 item.old_pools.clear();
133 for (const auto &p : pi->old_pools)
134 item.old_pools.insert(p);
7c673cae
FG
135 item.snapc = *snapc;
136 }
137
138 purge_queue.push(item, new C_IO_PurgeStrayPurged(
139 this, dn, false));
140}
141
142class C_PurgeStrayLogged : public StrayManagerLogContext {
143 CDentry *dn;
144 version_t pdv;
145 LogSegment *ls;
146public:
147 C_PurgeStrayLogged(StrayManager *sm_, CDentry *d, version_t v, LogSegment *s) :
148 StrayManagerLogContext(sm_), dn(d), pdv(v), ls(s) { }
149 void finish(int r) override {
150 sm->_purge_stray_logged(dn, pdv, ls);
151 }
152};
153
154class C_TruncateStrayLogged : public StrayManagerLogContext {
155 CDentry *dn;
156 LogSegment *ls;
157public:
158 C_TruncateStrayLogged(StrayManager *sm, CDentry *d, LogSegment *s) :
159 StrayManagerLogContext(sm), dn(d), ls(s) { }
160 void finish(int r) override {
161 sm->_truncate_stray_logged(dn, ls);
162 }
163};
164
165void StrayManager::_purge_stray_purged(
d2e6a577 166 CDentry *dn, bool only_head)
7c673cae
FG
167{
168 CInode *in = dn->get_projected_linkage()->get_inode();
169 dout(10) << "_purge_stray_purged " << *dn << " " << *in << dendl;
170
171 logger->inc(l_mdc_strays_enqueued);
172 num_strays_enqueuing--;
173 logger->set(l_mdc_num_strays_enqueuing, num_strays_enqueuing);
174
175 if (only_head) {
176 /* This was a ::truncate */
177 EUpdate *le = new EUpdate(mds->mdlog, "purge_stray truncate");
178 mds->mdlog->start_entry(le);
179
94b18763
FG
180 auto &pi = in->project_inode();
181 pi.inode.size = 0;
182 pi.inode.max_size_ever = 0;
183 pi.inode.client_ranges.clear();
184 pi.inode.truncate_size = 0;
185 pi.inode.truncate_from = 0;
186 pi.inode.version = in->pre_dirty();
7c673cae
FG
187
188 le->metablob.add_dir_context(dn->dir);
189 le->metablob.add_primary_dentry(dn, in, true);
190
191 mds->mdlog->submit_entry(le,
192 new C_TruncateStrayLogged(
193 this, dn, mds->mdlog->get_current_segment()));
194 } else {
195 if (in->get_num_ref() != (int)in->is_dirty() ||
196 dn->get_num_ref() != (int)dn->is_dirty() + !!in->get_num_ref() + 1/*PIN_PURGING*/) {
197 // Nobody should be taking new references to an inode when it
198 // is being purged (aside from it were
199
200 derr << "Rogue reference after purge to " << *dn << dendl;
201 assert(0 == "rogue reference to purging inode");
202 }
203
204 // kill dentry.
205 version_t pdv = dn->pre_dirty();
206 dn->push_projected_linkage(); // NULL
207
208 EUpdate *le = new EUpdate(mds->mdlog, "purge_stray");
209 mds->mdlog->start_entry(le);
210
211 // update dirfrag fragstat, rstat
212 CDir *dir = dn->get_dir();
213 fnode_t *pf = dir->project_fnode();
214 pf->version = dir->pre_dirty();
215 if (in->is_dir())
216 pf->fragstat.nsubdirs--;
217 else
218 pf->fragstat.nfiles--;
219 pf->rstat.sub(in->inode.accounted_rstat);
220
221 le->metablob.add_dir_context(dn->dir);
222 EMetaBlob::dirlump& dl = le->metablob.add_dir(dn->dir, true);
223 le->metablob.add_null_dentry(dl, dn, true);
224 le->metablob.add_destroyed_inode(in->ino());
225
226 mds->mdlog->submit_entry(le, new C_PurgeStrayLogged(this, dn, pdv,
227 mds->mdlog->get_current_segment()));
228
229 logger->set(l_mdc_num_strays, num_strays);
230 }
231}
232
233void StrayManager::_purge_stray_logged(CDentry *dn, version_t pdv, LogSegment *ls)
234{
235 CInode *in = dn->get_linkage()->get_inode();
236 dout(10) << "_purge_stray_logged " << *dn << " " << *in << dendl;
237
238 assert(!in->state_test(CInode::STATE_RECOVERING));
239
31f18b77
FG
240 bool new_dn = dn->is_new();
241
7c673cae
FG
242 // unlink
243 assert(dn->get_projected_linkage()->is_null());
31f18b77 244 dn->dir->unlink_inode(dn, !new_dn);
7c673cae
FG
245 dn->pop_projected_linkage();
246 dn->mark_dirty(pdv, ls);
247
248 dn->dir->pop_and_dirty_projected_fnode(ls);
249
250 in->state_clear(CInode::STATE_ORPHAN);
251 dn->state_clear(CDentry::STATE_PURGING | CDentry::STATE_PURGINGPINNED);
252 dn->put(CDentry::PIN_PURGING);
253
254 // drop dentry?
31f18b77 255 if (new_dn) {
7c673cae
FG
256 dout(20) << " dn is new, removing" << dendl;
257 dn->mark_clean();
258 dn->dir->remove_dentry(dn);
7c673cae
FG
259 }
260
261 // drop inode
262 if (in->is_dirty())
263 in->mark_clean();
264 in->mdcache->remove_inode(in);
265}
266
267void StrayManager::enqueue(CDentry *dn, bool trunc)
268{
269 CDentry::linkage_t *dnl = dn->get_projected_linkage();
270 assert(dnl);
271 CInode *in = dnl->get_inode();
272 assert(in);
273
274 /* We consider a stray to be purging as soon as it is enqueued, to avoid
275 * enqueing it twice */
276 dn->state_set(CDentry::STATE_PURGING);
277 in->state_set(CInode::STATE_PURGING);
278
279 /* We must clear this as soon as enqueuing it, to prevent the journal
280 * expiry code from seeing a dirty parent and trying to write a backtrace */
281 if (!trunc) {
282 if (in->is_dirty_parent()) {
283 in->clear_dirty_parent();
284 }
285 }
286
287 dout(20) << __func__ << ": purging dn: " << *dn << dendl;
288
289 if (!dn->state_test(CDentry::STATE_PURGINGPINNED)) {
290 dn->get(CDentry::PIN_PURGING);
291 dn->state_set(CDentry::STATE_PURGINGPINNED);
292 }
293
294 ++num_strays_enqueuing;
295 logger->set(l_mdc_num_strays_enqueuing, num_strays_enqueuing);
296
297 // Resources are available, acquire them and execute the purge
298 _enqueue(dn, trunc);
299
300 dout(10) << __func__ << ": purging this dentry immediately: "
301 << *dn << dendl;
302}
303
304class C_OpenSnapParents : public StrayManagerContext {
305 CDentry *dn;
306 bool trunc;
307 public:
308 C_OpenSnapParents(StrayManager *sm_, CDentry *dn_, bool t) :
309 StrayManagerContext(sm_), dn(dn_), trunc(t) { }
310 void finish(int r) override {
311 sm->_enqueue(dn, trunc);
312 }
313};
314
315void StrayManager::_enqueue(CDentry *dn, bool trunc)
316{
c07f9fc5
FG
317 assert(started);
318
7c673cae
FG
319 CInode *in = dn->get_linkage()->get_inode();
320 if (in->snaprealm &&
321 !in->snaprealm->have_past_parents_open() &&
322 !in->snaprealm->open_parents(new C_OpenSnapParents(this, dn, trunc))) {
323 // this can happen if the dentry had been trimmed from cache.
324 return;
325 }
326
7c673cae
FG
327 if (trunc) {
328 truncate(dn);
329 } else {
330 purge(dn);
331 }
332}
333
334
335void StrayManager::advance_delayed()
336{
c07f9fc5
FG
337 if (!started)
338 return;
339
7c673cae
FG
340 for (elist<CDentry*>::iterator p = delayed_eval_stray.begin(); !p.end(); ) {
341 CDentry *dn = *p;
342 ++p;
343 dn->item_stray.remove_myself();
344 num_strays_delayed--;
345
346 if (dn->get_projected_linkage()->is_null()) {
347 /* A special case: a stray dentry can go null if its inode is being
348 * re-linked into another MDS's stray dir during a shutdown migration. */
349 dout(4) << __func__ << ": delayed dentry is now null: " << *dn << dendl;
350 continue;
351 }
352
353 const bool purging = eval_stray(dn);
354 if (!purging) {
355 derr << "Dentry " << *dn << " was purgeable but no longer is!" << dendl;
356 /*
357 * This can happen if a stray is purgeable, but has gained an extra
358 * reference by virtue of having its backtrace updated.
359 * FIXME perhaps we could simplify this further by
360 * avoiding writing the backtrace of purge-ready strays, so
361 * that this code could be more rigid?
362 */
363 }
364 }
365 logger->set(l_mdc_num_strays_delayed, num_strays_delayed);
366}
367
368void StrayManager::set_num_strays(uint64_t num)
369{
370 assert(!started);
371 num_strays = num;
372 logger->set(l_mdc_num_strays, num_strays);
373}
374
375void StrayManager::notify_stray_created()
376{
377 num_strays++;
378 logger->set(l_mdc_num_strays, num_strays);
379 logger->inc(l_mdc_strays_created);
380}
381
382void StrayManager::notify_stray_removed()
383{
384 num_strays--;
385 logger->set(l_mdc_num_strays, num_strays);
386}
387
388struct C_EvalStray : public StrayManagerContext {
389 CDentry *dn;
390 C_EvalStray(StrayManager *sm_, CDentry *d) : StrayManagerContext(sm_), dn(d) {}
391 void finish(int r) override {
392 sm->eval_stray(dn);
393 }
394};
395
396struct C_MDC_EvalStray : public StrayManagerContext {
397 CDentry *dn;
398 C_MDC_EvalStray(StrayManager *sm_, CDentry *d) : StrayManagerContext(sm_), dn(d) {}
399 void finish(int r) override {
400 sm->eval_stray(dn);
401 }
402};
403
404bool StrayManager::_eval_stray(CDentry *dn, bool delay)
405{
406 dout(10) << "eval_stray " << *dn << dendl;
407 CDentry::linkage_t *dnl = dn->get_projected_linkage();
408 assert(dnl->is_primary());
409 dout(10) << " inode is " << *dnl->get_inode() << dendl;
410 CInode *in = dnl->get_inode();
411 assert(in);
224ce89b 412 assert(!in->state_test(CInode::STATE_REJOINUNDEF));
7c673cae
FG
413
414 // The only dentries elegible for purging are those
415 // in the stray directories
416 assert(dn->get_dir()->get_inode()->is_stray());
417
418 // Inode may not pass through this function if it
419 // was already identified for purging (i.e. cannot
420 // call eval_stray() after purge()
421 assert(!dn->state_test(CDentry::STATE_PURGING));
422
423 if (!dn->is_auth()) {
7c673cae
FG
424 return false;
425 }
426
c07f9fc5
FG
427 if (!started)
428 delay = true;
429
7c673cae
FG
430 if (dn->item_stray.is_on_list()) {
431 if (delay)
432 return false;
433
434 dn->item_stray.remove_myself();
435 num_strays_delayed--;
436 logger->set(l_mdc_num_strays_delayed, num_strays_delayed);
437 }
438
439 // purge?
440 if (in->inode.nlink == 0) {
441 // past snaprealm parents imply snapped dentry remote links.
442 // only important for directories. normal file data snaps are handled
443 // by the object store.
444 if (in->snaprealm) {
445 if (!in->snaprealm->have_past_parents_open() &&
446 !in->snaprealm->open_parents(new C_MDC_EvalStray(this, dn))) {
447 return false;
448 }
449 in->snaprealm->prune_past_parents();
450 in->purge_stale_snap_data(in->snaprealm->get_snaps());
451 }
452 if (in->is_dir()) {
453 if (in->snaprealm && in->snaprealm->has_past_parents()) {
454 dout(20) << " directory has past parents "
455 << in->snaprealm->srnode.past_parents << dendl;
456 if (in->state_test(CInode::STATE_MISSINGOBJS)) {
457 mds->clog->error() << "previous attempt at committing dirfrag of ino "
458 << in->ino() << " has failed, missing object";
459 mds->handle_write_error(-ENOENT);
460 }
461 return false; // not until some snaps are deleted.
462 }
463
31f18b77 464 in->mdcache->clear_dirty_bits_for_stray(in);
7c673cae
FG
465
466 if (!in->remote_parents.empty()) {
467 // unlink any stale remote snap dentry.
94b18763
FG
468 for (auto it = in->remote_parents.begin(); it != in->remote_parents.end(); ) {
469 CDentry *remote_dn = *it;
470 ++it;
7c673cae
FG
471 assert(remote_dn->last != CEPH_NOSNAP);
472 remote_dn->unlink_remote(remote_dn->get_linkage());
473 }
474 }
475 }
476 if (dn->is_replicated()) {
477 dout(20) << " replicated" << dendl;
478 return false;
479 }
480 if (dn->is_any_leases() || in->is_any_caps()) {
481 dout(20) << " caps | leases" << dendl;
482 return false; // wait
483 }
484 if (in->state_test(CInode::STATE_NEEDSRECOVER) ||
485 in->state_test(CInode::STATE_RECOVERING)) {
486 dout(20) << " pending recovery" << dendl;
487 return false; // don't mess with file size probing
488 }
489 if (in->get_num_ref() > (int)in->is_dirty() + (int)in->is_dirty_parent()) {
490 dout(20) << " too many inode refs" << dendl;
491 return false;
492 }
493 if (dn->get_num_ref() > (int)dn->is_dirty() + !!in->get_num_ref()) {
494 dout(20) << " too many dn refs" << dendl;
495 return false;
496 }
497 if (delay) {
498 if (!dn->item_stray.is_on_list()) {
499 delayed_eval_stray.push_back(&dn->item_stray);
500 num_strays_delayed++;
501 logger->set(l_mdc_num_strays_delayed, num_strays_delayed);
502 }
503 // don't purge multiversion inode with snap data
504 } else if (in->snaprealm && in->snaprealm->has_past_parents() &&
505 !in->old_inodes.empty()) {
506 // A file with snapshots: we will truncate the HEAD revision
507 // but leave the metadata intact.
508 assert(!in->is_dir());
509 dout(20) << " file has past parents "
510 << in->snaprealm->srnode.past_parents << dendl;
511 if (in->is_file() && in->get_projected_inode()->size > 0) {
512 enqueue(dn, true); // truncate head objects
513 }
514 } else {
515 // A straightforward file, ready to be purged. Enqueue it.
516 if (in->is_dir()) {
517 in->close_dirfrags();
518 }
519
520 enqueue(dn, false);
521 }
522
523 return true;
524 } else {
525 /*
526 * Where a stray has some links, they should be remotes, check
527 * if we can do anything with them if we happen to have them in
528 * cache.
529 */
31f18b77 530 _eval_stray_remote(dn, NULL);
7c673cae
FG
531 return false;
532 }
533}
534
535void StrayManager::activate()
536{
537 dout(10) << __func__ << dendl;
538 started = true;
c07f9fc5 539 purge_queue.activate();
7c673cae
FG
540}
541
542bool StrayManager::eval_stray(CDentry *dn, bool delay)
543{
544 // avoid nested eval_stray
545 if (dn->state_test(CDentry::STATE_EVALUATINGSTRAY))
546 return false;
547
548 dn->state_set(CDentry::STATE_EVALUATINGSTRAY);
549 bool ret = _eval_stray(dn, delay);
550 dn->state_clear(CDentry::STATE_EVALUATINGSTRAY);
551 return ret;
552}
553
31f18b77 554void StrayManager::eval_remote(CDentry *remote_dn)
7c673cae 555{
31f18b77
FG
556 dout(10) << __func__ << " " << *remote_dn << dendl;
557
558 CDentry::linkage_t *dnl = remote_dn->get_projected_linkage();
559 assert(dnl->is_remote());
560 CInode *in = dnl->get_inode();
561
562 if (!in) {
563 dout(20) << __func__ << ": no inode, cannot evaluate" << dendl;
564 return;
565 }
566
567 if (remote_dn->last != CEPH_NOSNAP) {
568 dout(20) << __func__ << ": snap dentry, cannot evaluate" << dendl;
569 return;
570 }
571
572 // refers to stray?
573 CDentry *primary_dn = in->get_projected_parent_dn();
574 assert(primary_dn != NULL);
575 if (primary_dn->get_dir()->get_inode()->is_stray()) {
576 _eval_stray_remote(primary_dn, remote_dn);
577 } else {
578 dout(20) << __func__ << ": inode's primary dn not stray" << dendl;
579 }
580}
581
582class C_RetryEvalRemote : public StrayManagerContext {
583 CDentry *dn;
584 public:
585 C_RetryEvalRemote(StrayManager *sm_, CDentry *dn_) :
586 StrayManagerContext(sm_), dn(dn_) {
587 dn->get(CDentry::PIN_PTRWAITER);
588 }
589 void finish(int r) override {
590 if (dn->get_projected_linkage()->is_remote())
591 sm->eval_remote(dn);
592 dn->put(CDentry::PIN_PTRWAITER);
593 }
594};
595
596void StrayManager::_eval_stray_remote(CDentry *stray_dn, CDentry *remote_dn)
597{
598 dout(20) << __func__ << " " << *stray_dn << dendl;
7c673cae
FG
599 assert(stray_dn != NULL);
600 assert(stray_dn->get_dir()->get_inode()->is_stray());
601 CDentry::linkage_t *stray_dnl = stray_dn->get_projected_linkage();
602 assert(stray_dnl->is_primary());
603 CInode *stray_in = stray_dnl->get_inode();
604 assert(stray_in->inode.nlink >= 1);
605 assert(stray_in->last == CEPH_NOSNAP);
606
607 /* If no remote_dn hinted, pick one arbitrarily */
608 if (remote_dn == NULL) {
609 if (!stray_in->remote_parents.empty()) {
94b18763
FG
610 for (const auto &dn : stray_in->remote_parents) {
611 if (dn->last == CEPH_NOSNAP && !dn->is_projected()) {
612 if (dn->is_auth()) {
613 remote_dn = dn;
31f18b77
FG
614 if (remote_dn->dir->can_auth_pin())
615 break;
616 } else if (!remote_dn) {
94b18763 617 remote_dn = dn;
31f18b77 618 }
7c673cae 619 }
94b18763 620 }
7c673cae
FG
621 }
622 if (!remote_dn) {
623 dout(20) << __func__ << ": not reintegrating (no remote parents in cache)" << dendl;
624 return;
625 }
626 }
627 assert(remote_dn->last == CEPH_NOSNAP);
628 // NOTE: we repeat this check in _rename(), since our submission path is racey.
629 if (!remote_dn->is_projected()) {
31f18b77
FG
630 if (remote_dn->is_auth()) {
631 if (remote_dn->dir->can_auth_pin()) {
632 reintegrate_stray(stray_dn, remote_dn);
633 } else {
634 remote_dn->dir->add_waiter(CDir::WAIT_UNFREEZE, new C_RetryEvalRemote(this, remote_dn));
635 dout(20) << __func__ << ": not reintegrating (can't authpin remote parent)" << dendl;
636 }
637
7c673cae
FG
638 } else if (!remote_dn->is_auth() && stray_dn->is_auth()) {
639 migrate_stray(stray_dn, remote_dn->authority().first);
640 } else {
641 dout(20) << __func__ << ": not reintegrating" << dendl;
642 }
643 } else {
644 // don't do anything if the remote parent is projected, or we may
645 // break user-visible semantics!
646 dout(20) << __func__ << ": not reintegrating (projected)" << dendl;
647 }
648}
649
650void StrayManager::reintegrate_stray(CDentry *straydn, CDentry *rdn)
651{
652 dout(10) << __func__ << " " << *straydn << " into " << *rdn << dendl;
653
654 logger->inc(l_mdc_strays_reintegrated);
655
656 // rename it to another mds.
657 filepath src;
658 straydn->make_path(src);
659 filepath dst;
660 rdn->make_path(dst);
661
662 MClientRequest *req = new MClientRequest(CEPH_MDS_OP_RENAME);
663 req->set_filepath(dst);
664 req->set_filepath2(src);
665 req->set_tid(mds->issue_tid());
666
667 mds->send_message_mds(req, rdn->authority().first);
668}
669
670void StrayManager::migrate_stray(CDentry *dn, mds_rank_t to)
671{
672 CInode *in = dn->get_projected_linkage()->get_inode();
673 assert(in);
674 CInode *diri = dn->dir->get_inode();
675 assert(diri->is_stray());
676 dout(10) << "migrate_stray from mds." << MDS_INO_STRAY_OWNER(diri->inode.ino)
677 << " to mds." << to
678 << " " << *dn << " " << *in << dendl;
679
680 logger->inc(l_mdc_strays_migrated);
681
682 // rename it to another mds.
683 filepath src;
684 dn->make_path(src);
685 assert(src.depth() == 2);
686
687 filepath dst(MDS_INO_MDSDIR(to));
688 dst.push_dentry(src[0]);
689 dst.push_dentry(src[1]);
690
691 MClientRequest *req = new MClientRequest(CEPH_MDS_OP_RENAME);
692 req->set_filepath(dst);
693 req->set_filepath2(src);
694 req->set_tid(mds->issue_tid());
695
696 mds->send_message_mds(req, to);
697}
698
699StrayManager::StrayManager(MDSRank *mds, PurgeQueue &purge_queue_)
700 : delayed_eval_stray(member_offset(CDentry, item_stray)),
701 mds(mds), logger(NULL), started(false), num_strays(0),
702 num_strays_delayed(0), num_strays_enqueuing(0),
703 purge_queue(purge_queue_)
704{
705 assert(mds != NULL);
706}
707
708void StrayManager::truncate(CDentry *dn)
709{
710 const CDentry::linkage_t *dnl = dn->get_projected_linkage();
711 const CInode *in = dnl->get_inode();
712 assert(in);
713 dout(10) << __func__ << ": " << *dn << " " << *in << dendl;
714 assert(!dn->is_replicated());
715
716 const SnapRealm *realm = in->find_snaprealm();
717 assert(realm);
718 dout(10) << " realm " << *realm << dendl;
719 const SnapContext *snapc = &realm->get_snap_context();
720
721 uint64_t to = in->inode.get_max_size();
722 to = MAX(in->inode.size, to);
723 // when truncating a file, the filer does not delete stripe objects that are
724 // truncated to zero. so we need to purge stripe objects up to the max size
725 // the file has ever been.
726 to = MAX(in->inode.max_size_ever, to);
727
728 assert(to > 0);
729
730 PurgeItem item;
b32b8144 731 item.action = PurgeItem::TRUNCATE_FILE;
7c673cae
FG
732 item.ino = in->inode.ino;
733 item.layout = in->inode.layout;
734 item.snapc = *snapc;
735 item.size = to;
736
737 purge_queue.push(item, new C_IO_PurgeStrayPurged(
738 this, dn, true));
739}
740
741void StrayManager::_truncate_stray_logged(CDentry *dn, LogSegment *ls)
742{
743 CInode *in = dn->get_projected_linkage()->get_inode();
744
745 dout(10) << __func__ << ": " << *dn << " " << *in << dendl;
746
747 dn->state_clear(CDentry::STATE_PURGING | CDentry::STATE_PURGINGPINNED);
748 dn->put(CDentry::PIN_PURGING);
749
750 in->pop_and_dirty_projected_inode(ls);
751
752 eval_stray(dn);
753}
754