Merge tag 'vfio-v4.10-final' of git://github.com/awilliam/linux-vfio
[linux/fpc-iii.git] / net / ceph / string_table.c
blob22fb96efcf3467713a9e5430057ead684326e3db
1 #include <linux/slab.h>
2 #include <linux/gfp.h>
3 #include <linux/string.h>
4 #include <linux/spinlock.h>
5 #include <linux/ceph/string_table.h>
7 static DEFINE_SPINLOCK(string_tree_lock);
8 static struct rb_root string_tree = RB_ROOT;
10 struct ceph_string *ceph_find_or_create_string(const char* str, size_t len)
12 struct ceph_string *cs, *exist;
13 struct rb_node **p, *parent;
14 int ret;
16 exist = NULL;
17 spin_lock(&string_tree_lock);
18 p = &string_tree.rb_node;
19 while (*p) {
20 exist = rb_entry(*p, struct ceph_string, node);
21 ret = ceph_compare_string(exist, str, len);
22 if (ret > 0)
23 p = &(*p)->rb_left;
24 else if (ret < 0)
25 p = &(*p)->rb_right;
26 else
27 break;
28 exist = NULL;
30 if (exist && !kref_get_unless_zero(&exist->kref)) {
31 rb_erase(&exist->node, &string_tree);
32 RB_CLEAR_NODE(&exist->node);
33 exist = NULL;
35 spin_unlock(&string_tree_lock);
36 if (exist)
37 return exist;
39 cs = kmalloc(sizeof(*cs) + len + 1, GFP_NOFS);
40 if (!cs)
41 return NULL;
43 kref_init(&cs->kref);
44 cs->len = len;
45 memcpy(cs->str, str, len);
46 cs->str[len] = 0;
48 retry:
49 exist = NULL;
50 parent = NULL;
51 p = &string_tree.rb_node;
52 spin_lock(&string_tree_lock);
53 while (*p) {
54 parent = *p;
55 exist = rb_entry(*p, struct ceph_string, node);
56 ret = ceph_compare_string(exist, str, len);
57 if (ret > 0)
58 p = &(*p)->rb_left;
59 else if (ret < 0)
60 p = &(*p)->rb_right;
61 else
62 break;
63 exist = NULL;
65 ret = 0;
66 if (!exist) {
67 rb_link_node(&cs->node, parent, p);
68 rb_insert_color(&cs->node, &string_tree);
69 } else if (!kref_get_unless_zero(&exist->kref)) {
70 rb_erase(&exist->node, &string_tree);
71 RB_CLEAR_NODE(&exist->node);
72 ret = -EAGAIN;
74 spin_unlock(&string_tree_lock);
75 if (ret == -EAGAIN)
76 goto retry;
78 if (exist) {
79 kfree(cs);
80 cs = exist;
83 return cs;
85 EXPORT_SYMBOL(ceph_find_or_create_string);
87 void ceph_release_string(struct kref *ref)
89 struct ceph_string *cs = container_of(ref, struct ceph_string, kref);
91 spin_lock(&string_tree_lock);
92 if (!RB_EMPTY_NODE(&cs->node)) {
93 rb_erase(&cs->node, &string_tree);
94 RB_CLEAR_NODE(&cs->node);
96 spin_unlock(&string_tree_lock);
98 kfree_rcu(cs, rcu);
100 EXPORT_SYMBOL(ceph_release_string);
102 bool ceph_strings_empty(void)
104 return RB_EMPTY_ROOT(&string_tree);