]>
git.proxmox.com Git - ceph.git/blob - ceph/src/mds/CDentry.cc
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->cache->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
->cache
->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
.is_auth_pinned())
87 out
<< " ap=" << dn
.get_num_auth_pins() << "+" << dn
.get_num_nested_auth_pins();
89 out
<< " inode=" << dn
.get_linkage()->get_inode();
91 if (dn
.is_new()) out
<< " state=new";
93 if (dn
.get_num_ref()) {
95 dn
.print_pin_set(out
);
104 bool operator<(const CDentry
& l
, const CDentry
& r
)
106 if ((l
.get_dir()->ino() < r
.get_dir()->ino()) ||
107 (l
.get_dir()->ino() == r
.get_dir()->ino() &&
108 (l
.get_name() < r
.get_name() ||
109 (l
.get_name() == r
.get_name() && l
.last
< r
.last
))))
115 void CDentry::print(ostream
& out
)
122 inodeno_t CDentry::get_ino()
125 return get_inode()->ino();
130 mds_authority_t
CDentry::authority() const
132 return dir
->authority();
136 void CDentry::add_waiter(uint64_t tag
, MDSInternalContextBase
*c
)
138 // wait on the directory?
139 if (tag
& (WAIT_UNFREEZE
|WAIT_SINGLEAUTH
)) {
140 dir
->add_waiter(tag
, c
);
143 MDSCacheObject::add_waiter(tag
, c
);
147 version_t
CDentry::pre_dirty(version_t min
)
149 projected_version
= dir
->pre_dirty(min
);
150 dout(10) << " pre_dirty " << *this << dendl
;
151 return projected_version
;
155 void CDentry::_mark_dirty(LogSegment
*ls
)
158 if (!state_test(STATE_DIRTY
)) {
159 state_set(STATE_DIRTY
);
160 dir
->inc_num_dirty();
165 ls
->dirty_dentries
.push_back(&item_dirty
);
168 void CDentry::mark_dirty(version_t pv
, LogSegment
*ls
)
170 dout(10) << " mark_dirty " << *this << dendl
;
172 // i now live in this new dir version
173 assert(pv
<= projected_version
);
178 dir
->mark_dirty(pv
, ls
);
182 void CDentry::mark_clean()
184 dout(10) << " mark_clean " << *this << dendl
;
187 // not always true for recalc_auth_bits during resolve finish
188 //assert(dir->get_version() == 0 || version <= dir->get_version()); // hmm?
191 state_clear(STATE_DIRTY
);
192 dir
->dec_num_dirty();
195 item_dirty
.remove_myself();
200 void CDentry::mark_new()
202 dout(10) << " mark_new " << *this << dendl
;
203 state_set(STATE_NEW
);
206 void CDentry::make_path_string(string
& s
, bool projected
) const
209 dir
->inode
->make_path_string(s
, projected
);
214 s
.append(name
.data(), name
.length());
217 void CDentry::make_path(filepath
& fp
, bool projected
) const
220 dir
->inode
->make_path(fp
, projected
);
221 fp
.push_dentry(name
);
225 * we only add ourselves to remote_parents when the linkage is
226 * active (no longer projected). if the passed dnl is projected,
227 * don't link in, and do that work later in pop_projected_linkage().
229 void CDentry::link_remote(CDentry::linkage_t
*dnl
, CInode
*in
)
231 assert(dnl
->is_remote());
232 assert(in
->ino() == dnl
->get_remote_ino());
236 in
->add_remote_parent(this);
239 void CDentry::unlink_remote(CDentry::linkage_t
*dnl
)
241 assert(dnl
->is_remote());
245 dnl
->inode
->remove_remote_parent(this);
250 void CDentry::push_projected_linkage()
255 CInode
*diri
= dir
->inode
;
256 if (diri
->is_stray())
257 diri
->mdcache
->notify_stray_removed();
262 void CDentry::push_projected_linkage(CInode
*inode
)
264 // dirty rstat tracking is in the projected plane
265 bool dirty_rstat
= inode
->is_dirty_rstat();
267 inode
->clear_dirty_rstat();
269 _project_linkage()->inode
= inode
;
270 inode
->push_projected_parent(this);
273 inode
->mark_dirty_rstat();
276 CInode
*diri
= dir
->inode
;
277 if (diri
->is_stray())
278 diri
->mdcache
->notify_stray_created();
282 CDentry::linkage_t
*CDentry::pop_projected_linkage()
284 assert(projected
.size());
286 linkage_t
& n
= projected
.front();
289 * the idea here is that the link_remote_inode(), link_primary_inode(),
290 * etc. calls should make linkage identical to &n (and we assert as
295 dir
->link_remote_inode(this, n
.remote_ino
, n
.remote_d_type
);
297 linkage
.inode
= n
.inode
;
298 linkage
.inode
->add_remote_parent(this);
300 } else if (n
.inode
) {
301 dir
->link_primary_inode(this, n
.inode
);
302 n
.inode
->pop_projected_parent();
305 assert(n
.inode
== linkage
.inode
);
306 assert(n
.remote_ino
== linkage
.remote_ino
);
307 assert(n
.remote_d_type
== linkage
.remote_d_type
);
309 projected
.pop_front();
316 // ----------------------------
319 int CDentry::get_num_dir_auth_pins() const
321 assert(!is_projected());
322 if (get_linkage()->is_primary())
323 return auth_pins
+ get_linkage()->get_inode()->get_num_auth_pins();
327 bool CDentry::can_auth_pin() const
330 return dir
->can_auth_pin();
333 void CDentry::auth_pin(void *by
)
339 #ifdef MDS_AUTHPIN_SET
340 auth_pin_set
.insert(by
);
343 dout(10) << "auth_pin by " << by
<< " on " << *this
344 << " now " << auth_pins
<< "+" << nested_auth_pins
347 dir
->adjust_nested_auth_pins(1, 1, by
);
350 void CDentry::auth_unpin(void *by
)
354 #ifdef MDS_AUTHPIN_SET
355 assert(auth_pin_set
.count(by
));
356 auth_pin_set
.erase(auth_pin_set
.find(by
));
362 dout(10) << "auth_unpin by " << by
<< " on " << *this
363 << " now " << auth_pins
<< "+" << nested_auth_pins
365 assert(auth_pins
>= 0);
367 dir
->adjust_nested_auth_pins(-1, -1, by
);
370 void CDentry::adjust_nested_auth_pins(int adjustment
, int diradj
, void *by
)
372 nested_auth_pins
+= adjustment
;
374 dout(35) << "adjust_nested_auth_pins by " << by
375 << ", change " << adjustment
<< " yields "
376 << auth_pins
<< "+" << nested_auth_pins
378 assert(nested_auth_pins
>= 0);
380 dir
->adjust_nested_auth_pins(adjustment
, diradj
, by
);
383 bool CDentry::is_frozen() const
385 return dir
->is_frozen();
388 bool CDentry::is_freezing() const
390 return dir
->is_freezing();
393 void CDentry::decode_replica(bufferlist::iterator
& p
, bool is_new
)
397 replica_nonce
= nonce
;
402 unsigned char rdtype
;
410 dir
->link_remote_inode(this, rino
, rdtype
);
415 // ----------------------------
418 void CDentry::set_object_info(MDSCacheObjectInfo
&info
)
420 info
.dirfrag
= dir
->dirfrag();
425 void CDentry::encode_lock_state(int type
, bufferlist
& bl
)
429 // null, ino, or remote_ino?
431 if (linkage
.is_primary()) {
434 ::encode(linkage
.get_inode()->inode
.ino
, bl
);
436 else if (linkage
.is_remote()) {
439 ::encode(linkage
.get_remote_ino(), bl
);
441 else if (linkage
.is_null()) {
447 void CDentry::decode_lock_state(int type
, bufferlist
& bl
)
449 bufferlist::iterator p
= bl
.begin();
452 ::decode(newfirst
, p
);
454 if (!is_auth() && newfirst
!= first
) {
455 dout(10) << "decode_lock_state first " << first
<< " -> " << newfirst
<< dendl
;
456 assert(newfirst
> first
);
462 assert(linkage
.is_null());
475 if (linkage
.is_null() && !is_auth()) {
476 // force trim from cache!
477 dout(10) << "decode_lock_state replica dentry null -> non-null, must trim" << dendl
;
478 //assert(get_num_ref() == 0);
490 ClientLease
*CDentry::add_client_lease(client_t c
, Session
*session
)
493 if (client_lease_map
.count(c
))
494 l
= client_lease_map
[c
];
496 dout(20) << "add_client_lease client." << c
<< " on " << lock
<< dendl
;
497 if (client_lease_map
.empty())
498 get(PIN_CLIENTLEASE
);
499 l
= client_lease_map
[c
] = new ClientLease(c
, this);
500 l
->seq
= ++session
->lease_seq
;
502 lock
.get_client_lease();
508 void CDentry::remove_client_lease(ClientLease
*l
, Locker
*locker
)
510 assert(l
->parent
== this);
514 dout(20) << "remove_client_lease client." << l
->client
<< " on " << lock
<< dendl
;
515 lock
.put_client_lease();
516 if (lock
.get_num_client_lease() == 0 && !lock
.is_stable())
519 client_lease_map
.erase(l
->client
);
520 l
->item_lease
.remove_myself();
521 l
->item_session_lease
.remove_myself();
524 if (client_lease_map
.empty())
525 put(PIN_CLIENTLEASE
);
528 locker
->eval_gather(&lock
);
531 void CDentry::remove_client_leases(Locker
*locker
)
533 while (!client_lease_map
.empty())
534 remove_client_lease(client_lease_map
.begin()->second
, locker
);
539 if (get_num_ref() <= ((int)is_dirty() + 1)) {
540 CDentry::linkage_t
*dnl
= get_projected_linkage();
541 if (dnl
->is_primary()) {
542 CInode
*in
= dnl
->get_inode();
543 if (get_num_ref() == (int)is_dirty() + !!in
->get_num_ref())
544 in
->mdcache
->maybe_eval_stray(in
, true);
549 void CDentry::dump(Formatter
*f
) const
556 f
->dump_string("path", path
.get_path());
557 f
->dump_unsigned("path_ino", path
.get_ino().val
);
558 f
->dump_unsigned("snap_first", first
);
559 f
->dump_unsigned("snap_last", last
);
561 f
->dump_bool("is_primary", get_linkage()->is_primary());
562 f
->dump_bool("is_remote", get_linkage()->is_remote());
563 f
->dump_bool("is_null", get_linkage()->is_null());
564 f
->dump_bool("is_new", is_new());
565 if (get_linkage()->get_inode()) {
566 f
->dump_unsigned("inode", get_linkage()->get_inode()->ino());
568 f
->dump_unsigned("inode", 0);
571 if (linkage
.is_remote()) {
572 f
->dump_string("remote_type", linkage
.get_remote_d_type_string());
574 f
->dump_string("remote_type", "");
577 f
->dump_unsigned("version", get_version());
578 f
->dump_unsigned("projected_version", get_projected_version());
580 f
->dump_int("auth_pins", auth_pins
);
581 f
->dump_int("nested_auth_pins", nested_auth_pins
);
583 MDSCacheObject::dump(f
);
585 f
->open_object_section("lock");
589 f
->open_object_section("versionlock");
593 f
->open_array_section("states");
594 MDSCacheObject::dump_states(f
);
595 if (state_test(STATE_NEW
))
596 f
->dump_string("state", "new");
597 if (state_test(STATE_FRAGMENTING
))
598 f
->dump_string("state", "fragmenting");
599 if (state_test(STATE_PURGING
))
600 f
->dump_string("state", "purging");
601 if (state_test(STATE_BADREMOTEINO
))
602 f
->dump_string("state", "badremoteino");
603 if (state_test(STATE_STRAY
))
604 f
->dump_string("state", "stray");
608 std::string
CDentry::linkage_t::get_remote_d_type_string() const
610 switch (DTTOIF(remote_d_type
)) {
611 case S_IFSOCK
: return "sock";
612 case S_IFLNK
: return "lnk";
613 case S_IFREG
: return "reg";
614 case S_IFBLK
: return "blk";
615 case S_IFDIR
: return "dir";
616 case S_IFCHR
: return "chr";
617 case S_IFIFO
: return "fifo";
618 default: ceph_abort(); return "";