staging: ks7010: replace cast type in assignment in hostif_sme_set_pmksa
[linux/fpc-iii.git] / net / ceph / string_table.c
blob3191d9d160a2c6fa9a81e885788f2182e6a20bfa
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/slab.h>
3 #include <linux/gfp.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;
15 int ret;
17 exist = NULL;
18 spin_lock(&string_tree_lock);
19 p = &string_tree.rb_node;
20 while (*p) {
21 exist = rb_entry(*p, struct ceph_string, node);
22 ret = ceph_compare_string(exist, str, len);
23 if (ret > 0)
24 p = &(*p)->rb_left;
25 else if (ret < 0)
26 p = &(*p)->rb_right;
27 else
28 break;
29 exist = NULL;
31 if (exist && !kref_get_unless_zero(&exist->kref)) {
32 rb_erase(&exist->node, &string_tree);
33 RB_CLEAR_NODE(&exist->node);
34 exist = NULL;
36 spin_unlock(&string_tree_lock);
37 if (exist)
38 return exist;
40 cs = kmalloc(sizeof(*cs) + len + 1, GFP_NOFS);
41 if (!cs)
42 return NULL;
44 kref_init(&cs->kref);
45 cs->len = len;
46 memcpy(cs->str, str, len);
47 cs->str[len] = 0;
49 retry:
50 exist = NULL;
51 parent = NULL;
52 p = &string_tree.rb_node;
53 spin_lock(&string_tree_lock);
54 while (*p) {
55 parent = *p;
56 exist = rb_entry(*p, struct ceph_string, node);
57 ret = ceph_compare_string(exist, str, len);
58 if (ret > 0)
59 p = &(*p)->rb_left;
60 else if (ret < 0)
61 p = &(*p)->rb_right;
62 else
63 break;
64 exist = NULL;
66 ret = 0;
67 if (!exist) {
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);
73 ret = -EAGAIN;
75 spin_unlock(&string_tree_lock);
76 if (ret == -EAGAIN)
77 goto retry;
79 if (exist) {
80 kfree(cs);
81 cs = exist;
84 return cs;
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);
99 kfree_rcu(cs, rcu);
101 EXPORT_SYMBOL(ceph_release_string);
103 bool ceph_strings_empty(void)
105 return RB_EMPTY_ROOT(&string_tree);