~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/ceph/string_table.c

Version: ~ [ linux-5.15-rc5 ] ~ [ linux-5.14.11 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.72 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.152 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.210 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.250 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.286 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.288 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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>
  6 
  7 static DEFINE_SPINLOCK(string_tree_lock);
  8 static struct rb_root string_tree = RB_ROOT;
  9 
 10 struct ceph_string *ceph_find_or_create_string(const char* str, size_t len)
 11 {
 12         struct ceph_string *cs, *exist;
 13         struct rb_node **p, *parent;
 14         int ret;
 15 
 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;
 29         }
 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;
 34         }
 35         spin_unlock(&string_tree_lock);
 36         if (exist)
 37                 return exist;
 38 
 39         cs = kmalloc(sizeof(*cs) + len + 1, GFP_NOFS);
 40         if (!cs)
 41                 return NULL;
 42 
 43         kref_init(&cs->kref);
 44         cs->len = len;
 45         memcpy(cs->str, str, len);
 46         cs->str[len] = 0;
 47 
 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;
 64         }
 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;
 73         }
 74         spin_unlock(&string_tree_lock);
 75         if (ret == -EAGAIN)
 76                 goto retry;
 77 
 78         if (exist) {
 79                 kfree(cs);
 80                 cs = exist;
 81         }
 82 
 83         return cs;
 84 }
 85 EXPORT_SYMBOL(ceph_find_or_create_string);
 86 
 87 void ceph_release_string(struct kref *ref)
 88 {
 89         struct ceph_string *cs = container_of(ref, struct ceph_string, kref);
 90 
 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);
 95         }
 96         spin_unlock(&string_tree_lock);
 97 
 98         kfree_rcu(cs, rcu);
 99 }
100 EXPORT_SYMBOL(ceph_release_string);
101 
102 bool ceph_strings_empty(void)
103 {
104         return RB_EMPTY_ROOT(&string_tree);
105 }
106 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp