1 #ifndef _FS_CEPH_STRING_TABLE_H
2 #define _FS_CEPH_STRING_TABLE_H
3 
4 #include <linux/types.h>
5 #include <linux/kref.h>
6 #include <linux/rbtree.h>
7 #include <linux/rcupdate.h>
8 
9 struct ceph_string {
10 	struct kref kref;
11 	union {
12 		struct rb_node node;
13 		struct rcu_head rcu;
14 	};
15 	size_t len;
16 	char str[];
17 };
18 
19 extern void ceph_release_string(struct kref *ref);
20 extern struct ceph_string *ceph_find_or_create_string(const char *str,
21 						      size_t len);
22 extern bool ceph_strings_empty(void);
23 
24 static inline struct ceph_string *ceph_get_string(struct ceph_string *str)
25 {
26 	kref_get(&str->kref);
27 	return str;
28 }
29 
30 static inline void ceph_put_string(struct ceph_string *str)
31 {
32 	if (!str)
33 		return;
34 	kref_put(&str->kref, ceph_release_string);
35 }
36 
37 static inline int ceph_compare_string(struct ceph_string *cs,
38 				      const char* str, size_t len)
39 {
40 	size_t cs_len = cs ? cs->len : 0;
41 	if (cs_len != len)
42 		return cs_len - len;
43 	if (len == 0)
44 		return 0;
45 	return strncmp(cs->str, str, len);
46 }
47 
48 #define ceph_try_get_string(x)					\
49 ({								\
50 	struct ceph_string *___str;				\
51 	rcu_read_lock();					\
52 	for (;;) {						\
53 		___str = rcu_dereference(x);			\
54 		if (!___str ||					\
55 		    kref_get_unless_zero(&___str->kref))	\
56 			break;					\
57 	}							\
58 	rcu_read_unlock();					\
59 	(___str);						\
60 })
61 
62 #endif
63