]> git.proxmox.com Git - ceph.git/blob - ceph/src/client/Dentry.h
483a31eccb324b56dd704f1931dbe1a68cea26e6
[ceph.git] / ceph / src / client / Dentry.h
1 #ifndef CEPH_CLIENT_DENTRY_H
2 #define CEPH_CLIENT_DENTRY_H
3
4 #include "include/lru.h"
5 #include "include/xlist.h"
6
7 #include "mds/mdstypes.h"
8 #include "Inode.h"
9 #include "InodeRef.h"
10 #include "Dir.h"
11
12 class Dentry : public LRUObject {
13 public:
14 explicit Dentry(Dir *_dir, const std::string &_name) :
15 dir(_dir), name(_name), inode_xlist_link(this)
16 {
17 auto r = dir->dentries.insert(make_pair(name, this));
18 ceph_assert(r.second);
19 dir->num_null_dentries++;
20 }
21 ~Dentry() {
22 ceph_assert(ref == 0);
23 ceph_assert(dir == nullptr);
24 }
25
26 /*
27 * ref==1 -> cached, unused
28 * ref >1 -> pinned in lru
29 */
30 void get() {
31 ceph_assert(ref > 0);
32 if (++ref == 2)
33 lru_pin();
34 //cout << "dentry.get on " << this << " " << name << " now " << ref << std::endl;
35 }
36 void put() {
37 ceph_assert(ref > 0);
38 if (--ref == 1)
39 lru_unpin();
40 //cout << "dentry.put on " << this << " " << name << " now " << ref << std::endl;
41 if (ref == 0)
42 delete this;
43 }
44 void link(InodeRef in) {
45 inode = in;
46 inode->dentries.push_back(&inode_xlist_link);
47 if (inode->is_dir()) {
48 if (inode->dir)
49 get(); // dir -> dn pin
50 if (inode->ll_ref)
51 get(); // ll_ref -> dn pin
52 }
53 dir->num_null_dentries--;
54 }
55 void unlink(void) {
56 if (inode->is_dir()) {
57 if (inode->dir)
58 put(); // dir -> dn pin
59 if (inode->ll_ref)
60 put(); // ll_ref -> dn pin
61 }
62 ceph_assert(inode_xlist_link.get_list() == &inode->dentries);
63 inode_xlist_link.remove_myself();
64 inode.reset();
65 dir->num_null_dentries++;
66 }
67 void mark_primary() {
68 if (inode && inode->dentries.front() != this)
69 inode->dentries.push_front(&inode_xlist_link);
70 }
71 void detach(void) {
72 ceph_assert(!inode);
73 auto p = dir->dentries.find(name);
74 ceph_assert(p != dir->dentries.end());
75 dir->dentries.erase(p);
76 dir->num_null_dentries--;
77 dir = nullptr;
78 }
79
80 void dump(Formatter *f) const;
81 friend std::ostream &operator<<(std::ostream &oss, const Dentry &Dentry);
82
83 Dir *dir;
84 const string name;
85 InodeRef inode;
86 int ref = 1; // 1 if there's a dir beneath me.
87 int64_t offset = 0;
88 mds_rank_t lease_mds = -1;
89 utime_t lease_ttl;
90 uint64_t lease_gen = 0;
91 ceph_seq_t lease_seq = 0;
92 int cap_shared_gen = 0;
93 std::string alternate_name;
94
95 private:
96 xlist<Dentry *>::item inode_xlist_link;
97 };
98
99 #endif