1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
24 #include "LogSegment.h"
26 #include "messages/MLock.h"
28 #define dout_context g_ceph_context
29 #define dout_subsys ceph_subsys_mds
31 #define dout_prefix *_dout << "mds." << dir->mdcache->mds->get_nodeid() << ".cache.den(" << dir->dirfrag() << " " << name << ") "
34 ostream
& CDentry::print_db_line_prefix(ostream
& out
)
36 return out
<< ceph_clock_now() << " mds." << dir
->mdcache
->mds
->get_nodeid() << ".cache.den(" << dir
->ino() << " " << name
<< ") ";
39 LockType
CDentry::lock_type(CEPH_LOCK_DN
);
40 LockType
CDentry::versionlock_type(CEPH_LOCK_DVERSION
);
45 ostream
& operator<<(ostream
& out
, const CDentry
& dn
)
50 out
<< "[dentry " << path
;
52 if (true || dn
.first
!= 0 || dn
.last
!= CEPH_NOSNAP
) {
53 out
<< " [" << dn
.first
<< ",";
54 if (dn
.last
== CEPH_NOSNAP
)
63 if (dn
.is_replicated())
64 out
<< dn
.get_replicas();
66 out
<< " rep@" << dn
.authority();
67 out
<< "." << dn
.get_replica_nonce();
70 if (dn
.get_linkage()->is_null()) out
<< " NULL";
71 if (dn
.get_linkage()->is_remote()) {
73 out
<< dn
.get_linkage()->get_remote_d_type_string();
77 if (!dn
.lock
.is_sync_and_unlocked())
78 out
<< " " << dn
.lock
;
79 if (!dn
.versionlock
.is_sync_and_unlocked())
80 out
<< " " << dn
.versionlock
;
82 if (dn
.get_projected_version() != dn
.get_version())
83 out
<< " pv=" << dn
.get_projected_version();
84 out
<< " v=" << dn
.get_version();
86 if (dn
.get_num_auth_pins()) {
87 out
<< " ap=" << dn
.get_num_auth_pins();
88 #ifdef MDS_AUTHPIN_SET
89 dn
.print_authpin_set(out
);
94 const CInode
*inode
= dn
.get_linkage()->get_inode();
103 out
<< " state=" << dn
.get_state();
104 if (dn
.is_new()) out
<< "|new";
105 if (dn
.state_test(CDentry::STATE_BOTTOMLRU
)) out
<< "|bottomlru";
107 if (dn
.get_num_ref()) {
109 dn
.print_pin_set(out
);
112 if (dn
.get_alternate_name().size()) {
113 out
<< " altname=" << binstrprint(dn
.get_alternate_name(), 16);
122 bool operator<(const CDentry
& l
, const CDentry
& r
)
124 if ((l
.get_dir()->ino() < r
.get_dir()->ino()) ||
125 (l
.get_dir()->ino() == r
.get_dir()->ino() &&
126 (l
.get_name() < r
.get_name() ||
127 (l
.get_name() == r
.get_name() && l
.last
< r
.last
))))
133 void CDentry::print(ostream
& out
)
140 inodeno_t CDentry::get_ino()
143 return get_inode()->ino();
148 mds_authority_t
CDentry::authority() const
150 return dir
->authority();
154 void CDentry::add_waiter(uint64_t tag
, MDSContext
*c
)
156 // wait on the directory?
157 if (tag
& (WAIT_UNFREEZE
|WAIT_SINGLEAUTH
)) {
158 dir
->add_waiter(tag
, c
);
161 MDSCacheObject::add_waiter(tag
, c
);
165 version_t
CDentry::pre_dirty(version_t min
)
167 projected_version
= dir
->pre_dirty(min
);
168 dout(10) << __func__
<< " " << *this << dendl
;
169 return projected_version
;
173 void CDentry::_mark_dirty(LogSegment
*ls
)
176 if (!state_test(STATE_DIRTY
)) {
177 state_set(STATE_DIRTY
);
179 dir
->inc_num_dirty();
180 dir
->dirty_dentries
.push_back(&item_dir_dirty
);
184 ls
->dirty_dentries
.push_back(&item_dirty
);
187 void CDentry::mark_dirty(version_t pv
, LogSegment
*ls
)
189 dout(10) << __func__
<< " " << *this << dendl
;
191 // i now live in this new dir version
192 ceph_assert(pv
<= projected_version
);
197 dir
->mark_dirty(ls
, pv
);
201 void CDentry::mark_clean()
203 dout(10) << __func__
<< " " << *this << dendl
;
204 ceph_assert(is_dirty());
206 // not always true for recalc_auth_bits during resolve finish
207 //assert(dir->get_version() == 0 || version <= dir->get_version()); // hmm?
209 state_clear(STATE_DIRTY
|STATE_NEW
);
210 dir
->dec_num_dirty();
212 item_dir_dirty
.remove_myself();
213 item_dirty
.remove_myself();
218 void CDentry::mark_new()
220 dout(10) << __func__
<< " " << *this << dendl
;
221 state_set(STATE_NEW
);
224 void CDentry::make_path_string(string
& s
, bool projected
) const
227 dir
->inode
->make_path_string(s
, projected
);
232 s
.append(name
.data(), name
.length());
235 void CDentry::make_path(filepath
& fp
, bool projected
) const
238 dir
->inode
->make_path(fp
, projected
);
239 fp
.push_dentry(get_name());
243 * we only add ourselves to remote_parents when the linkage is
244 * active (no longer projected). if the passed dnl is projected,
245 * don't link in, and do that work later in pop_projected_linkage().
247 void CDentry::link_remote(CDentry::linkage_t
*dnl
, CInode
*in
)
249 ceph_assert(dnl
->is_remote());
250 ceph_assert(in
->ino() == dnl
->get_remote_ino());
254 in
->add_remote_parent(this);
257 void CDentry::unlink_remote(CDentry::linkage_t
*dnl
)
259 ceph_assert(dnl
->is_remote());
260 ceph_assert(dnl
->inode
);
263 dnl
->inode
->remove_remote_parent(this);
268 void CDentry::push_projected_linkage()
273 CInode
*diri
= dir
->inode
;
274 if (diri
->is_stray())
275 diri
->mdcache
->notify_stray_removed();
280 void CDentry::push_projected_linkage(CInode
*inode
)
282 // dirty rstat tracking is in the projected plane
283 bool dirty_rstat
= inode
->is_dirty_rstat();
285 inode
->clear_dirty_rstat();
287 _project_linkage()->inode
= inode
;
288 inode
->push_projected_parent(this);
291 inode
->mark_dirty_rstat();
294 CInode
*diri
= dir
->inode
;
295 if (diri
->is_stray())
296 diri
->mdcache
->notify_stray_created();
300 CDentry::linkage_t
*CDentry::pop_projected_linkage()
302 ceph_assert(projected
.size());
304 linkage_t
& n
= projected
.front();
307 * the idea here is that the link_remote_inode(), link_primary_inode(),
308 * etc. calls should make linkage identical to &n (and we assert as
313 dir
->link_remote_inode(this, n
.remote_ino
, n
.remote_d_type
);
315 linkage
.inode
= n
.inode
;
316 linkage
.inode
->add_remote_parent(this);
320 dir
->link_primary_inode(this, n
.inode
);
321 n
.inode
->pop_projected_parent();
325 ceph_assert(n
.inode
== linkage
.inode
);
326 ceph_assert(n
.remote_ino
== linkage
.remote_ino
);
327 ceph_assert(n
.remote_d_type
== linkage
.remote_d_type
);
329 projected
.pop_front();
336 // ----------------------------
339 int CDentry::get_num_dir_auth_pins() const
341 ceph_assert(!is_projected());
342 if (get_linkage()->is_primary())
343 return auth_pins
+ get_linkage()->get_inode()->get_num_auth_pins();
347 bool CDentry::can_auth_pin(int *err_ret
) const
350 return dir
->can_auth_pin(err_ret
);
353 void CDentry::auth_pin(void *by
)
359 #ifdef MDS_AUTHPIN_SET
360 auth_pin_set
.insert(by
);
363 dout(10) << "auth_pin by " << by
<< " on " << *this << " now " << auth_pins
<< dendl
;
365 dir
->adjust_nested_auth_pins(1, by
);
368 void CDentry::auth_unpin(void *by
)
372 #ifdef MDS_AUTHPIN_SET
374 auto it
= auth_pin_set
.find(by
);
375 ceph_assert(it
!= auth_pin_set
.end());
376 auth_pin_set
.erase(it
);
383 dout(10) << "auth_unpin by " << by
<< " on " << *this << " now " << auth_pins
<< dendl
;
384 ceph_assert(auth_pins
>= 0);
386 dir
->adjust_nested_auth_pins(-1, by
);
389 void CDentry::adjust_nested_auth_pins(int diradj
, void *by
)
391 dir
->adjust_nested_auth_pins(diradj
, by
);
394 bool CDentry::is_frozen() const
396 return dir
->is_frozen();
399 bool CDentry::is_freezing() const
401 return dir
->is_freezing();
404 // ----------------------------
407 void CDentry::set_object_info(MDSCacheObjectInfo
&info
)
409 info
.dirfrag
= dir
->dirfrag();
414 void CDentry::encode_lock_state(int type
, bufferlist
& bl
)
418 // null, ino, or remote_ino?
420 if (linkage
.is_primary()) {
423 encode(linkage
.get_inode()->ino(), bl
);
425 else if (linkage
.is_remote()) {
428 encode(linkage
.get_remote_ino(), bl
);
430 else if (linkage
.is_null()) {
436 void CDentry::decode_lock_state(int type
, const bufferlist
& bl
)
438 auto p
= bl
.cbegin();
443 if (!is_auth() && newfirst
!= first
) {
444 dout(10) << __func__
<< " first " << first
<< " -> " << newfirst
<< dendl
;
445 ceph_assert(newfirst
> first
);
451 ceph_assert(linkage
.is_null());
464 if (linkage
.is_null() && !is_auth()) {
465 // force trim from cache!
466 dout(10) << __func__
<< " replica dentry null -> non-null, must trim" << dendl
;
467 //assert(get_num_ref() == 0);
479 ClientLease
*CDentry::add_client_lease(client_t c
, Session
*session
)
482 if (client_lease_map
.count(c
))
483 l
= client_lease_map
[c
];
485 dout(20) << __func__
<< " client." << c
<< " on " << lock
<< dendl
;
486 if (client_lease_map
.empty()) {
487 get(PIN_CLIENTLEASE
);
488 lock
.get_client_lease();
490 l
= client_lease_map
[c
] = new ClientLease(c
, this);
491 l
->seq
= ++session
->lease_seq
;
498 void CDentry::remove_client_lease(ClientLease
*l
, Locker
*locker
)
500 ceph_assert(l
->parent
== this);
504 dout(20) << __func__
<< " client." << l
->client
<< " on " << lock
<< dendl
;
506 client_lease_map
.erase(l
->client
);
507 l
->item_lease
.remove_myself();
508 l
->item_session_lease
.remove_myself();
511 if (client_lease_map
.empty()) {
512 gather
= !lock
.is_stable();
513 lock
.put_client_lease();
514 put(PIN_CLIENTLEASE
);
518 locker
->eval_gather(&lock
);
521 void CDentry::remove_client_leases(Locker
*locker
)
523 while (!client_lease_map
.empty())
524 remove_client_lease(client_lease_map
.begin()->second
, locker
);
529 if (get_num_ref() <= ((int)is_dirty() + 1)) {
530 CDentry::linkage_t
*dnl
= get_projected_linkage();
531 if (dnl
->is_primary()) {
532 CInode
*in
= dnl
->get_inode();
533 if (get_num_ref() == (int)is_dirty() + !!in
->get_num_ref())
534 in
->mdcache
->maybe_eval_stray(in
, true);
539 void CDentry::encode_remote(inodeno_t
& ino
, unsigned char d_type
,
540 std::string_view alternate_name
,
543 bl
.append('l'); // remote link
546 ENCODE_START(2, 1, bl
);
549 encode(alternate_name
, bl
);
553 void CDentry::decode_remote(char icode
, inodeno_t
& ino
, unsigned char& d_type
,
554 mempool::mds_co::string
& alternate_name
,
555 ceph::buffer::list::const_iterator
& bl
)
562 decode(alternate_name
, bl
);
564 } else if (icode
== 'L') {
567 } else ceph_assert(0);
570 void CDentry::dump(Formatter
*f
) const
572 ceph_assert(f
!= NULL
);
577 f
->dump_string("path", path
.get_path());
578 f
->dump_unsigned("path_ino", path
.get_ino().val
);
579 f
->dump_unsigned("snap_first", first
);
580 f
->dump_unsigned("snap_last", last
);
582 f
->dump_bool("is_primary", get_linkage()->is_primary());
583 f
->dump_bool("is_remote", get_linkage()->is_remote());
584 f
->dump_bool("is_null", get_linkage()->is_null());
585 f
->dump_bool("is_new", is_new());
586 if (get_linkage()->get_inode()) {
587 f
->dump_unsigned("inode", get_linkage()->get_inode()->ino());
589 f
->dump_unsigned("inode", 0);
592 if (linkage
.is_remote()) {
593 f
->dump_string("remote_type", linkage
.get_remote_d_type_string());
595 f
->dump_string("remote_type", "");
598 f
->dump_unsigned("version", get_version());
599 f
->dump_unsigned("projected_version", get_projected_version());
601 f
->dump_int("auth_pins", auth_pins
);
603 MDSCacheObject::dump(f
);
605 f
->open_object_section("lock");
609 f
->open_object_section("versionlock");
613 f
->open_array_section("states");
614 MDSCacheObject::dump_states(f
);
615 if (state_test(STATE_NEW
))
616 f
->dump_string("state", "new");
617 if (state_test(STATE_FRAGMENTING
))
618 f
->dump_string("state", "fragmenting");
619 if (state_test(STATE_PURGING
))
620 f
->dump_string("state", "purging");
621 if (state_test(STATE_BADREMOTEINO
))
622 f
->dump_string("state", "badremoteino");
623 if (state_test(STATE_STRAY
))
624 f
->dump_string("state", "stray");
628 std::string
CDentry::linkage_t::get_remote_d_type_string() const
630 switch (DTTOIF(remote_d_type
)) {
631 case S_IFSOCK
: return "sock";
632 case S_IFLNK
: return "lnk";
633 case S_IFREG
: return "reg";
634 case S_IFBLK
: return "blk";
635 case S_IFDIR
: return "dir";
636 case S_IFCHR
: return "chr";
637 case S_IFIFO
: return "fifo";
638 default: ceph_abort(); return "";
642 MEMPOOL_DEFINE_OBJECT_FACTORY(CDentry
, co_dentry
, mds_co
);