1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/slab.h>
4 #include <linux/string.h>
5 #include <linux/spinlock.h>
6 #include <linux/ceph/string_table.h>
8 static DEFINE_SPINLOCK(string_tree_lock
);
9 static struct rb_root string_tree
= RB_ROOT
;
11 struct ceph_string
*ceph_find_or_create_string(const char* str
, size_t len
)
13 struct ceph_string
*cs
, *exist
;
14 struct rb_node
**p
, *parent
;
18 spin_lock(&string_tree_lock
);
19 p
= &string_tree
.rb_node
;
21 exist
= rb_entry(*p
, struct ceph_string
, node
);
22 ret
= ceph_compare_string(exist
, str
, len
);
31 if (exist
&& !kref_get_unless_zero(&exist
->kref
)) {
32 rb_erase(&exist
->node
, &string_tree
);
33 RB_CLEAR_NODE(&exist
->node
);
36 spin_unlock(&string_tree_lock
);
40 cs
= kmalloc(sizeof(*cs
) + len
+ 1, GFP_NOFS
);
46 memcpy(cs
->str
, str
, len
);
52 p
= &string_tree
.rb_node
;
53 spin_lock(&string_tree_lock
);
56 exist
= rb_entry(*p
, struct ceph_string
, node
);
57 ret
= ceph_compare_string(exist
, str
, len
);
68 rb_link_node(&cs
->node
, parent
, p
);
69 rb_insert_color(&cs
->node
, &string_tree
);
70 } else if (!kref_get_unless_zero(&exist
->kref
)) {
71 rb_erase(&exist
->node
, &string_tree
);
72 RB_CLEAR_NODE(&exist
->node
);
75 spin_unlock(&string_tree_lock
);
86 EXPORT_SYMBOL(ceph_find_or_create_string
);
88 void ceph_release_string(struct kref
*ref
)
90 struct ceph_string
*cs
= container_of(ref
, struct ceph_string
, kref
);
92 spin_lock(&string_tree_lock
);
93 if (!RB_EMPTY_NODE(&cs
->node
)) {
94 rb_erase(&cs
->node
, &string_tree
);
95 RB_CLEAR_NODE(&cs
->node
);
97 spin_unlock(&string_tree_lock
);
101 EXPORT_SYMBOL(ceph_release_string
);
103 bool ceph_strings_empty(void)
105 return RB_EMPTY_ROOT(&string_tree
);