1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
8 #include <lustre_lib.h>
10 /* #define LUSTRE_HASH_DEBUG 1 */
12 /* define the hash bucket*/
13 struct lustre_hash_bucket {
14 struct hlist_head lhb_head;
16 #ifdef LUSTRE_HASH_DEBUG
17 /* the number of hash item per bucket,
18 * it will help us to analyse the hash distribute
24 struct lustre_hash_operations;
26 struct lustre_class_hash_body {
28 spinlock_t lchb_lock; /* body lock */
29 struct lustre_hash_bucket *lchb_hash_tables;
30 __u32 lchb_hash_max_size; /* define the hash tables size */
31 /* define the hash operations */
32 struct lustre_hash_operations *lchb_hash_operations;
35 /* hash operations method define */
36 struct lustre_hash_operations {
37 __u32 (*lustre_hashfn) (struct lustre_class_hash_body *hash_body,
39 int (*lustre_hash_key_compare) (void *key,
40 struct hlist_node *compared_hnode);
42 void* (*lustre_hash_object_refcount_get) (struct hlist_node *hash_item);
44 void (*lustre_hash_object_refcount_put) (struct hlist_node *hash_item);
47 static inline struct hlist_node *
48 lustre_hash_getitem_in_bucket_nolock(struct lustre_class_hash_body *hash_body,
49 int hashent, void *key)
51 struct lustre_hash_bucket *bucket;
52 struct hlist_node *hash_item_node;
53 struct lustre_hash_operations *hop = hash_body->lchb_hash_operations;
57 bucket = &hash_body->lchb_hash_tables[hashent];
58 hlist_for_each(hash_item_node, &(bucket->lhb_head)) {
59 find = hop->lustre_hash_key_compare(key, hash_item_node);
63 RETURN(find == 1 ? hash_item_node : NULL);
67 lustre_hash_delitem_nolock(struct lustre_class_hash_body *hash_body,
68 int hashent, struct hlist_node * hash_item)
70 struct lustre_hash_operations *hop = hash_body->lchb_hash_operations;
72 hlist_del_init(hash_item);
74 hop->lustre_hash_object_refcount_put(hash_item);
76 #ifdef LUSTRE_HASH_DEBUG
77 hash_body->lchb_hash_tables[hashent].lhb_item_count--;
78 CDEBUG(D_INFO, "hashname[%s] bucket[%d] has [%d] hashitem\n",
79 hash_body->hashname, hashent,
80 hash_body->lchb_hash_tables[hashent].lhb_item_count);
86 typedef void (*hash_item_iterate_cb) (void *obj, void *data);
88 int lustre_hash_init(struct lustre_class_hash_body **hash_body,
89 char *hashname, __u32 hashsize,
90 struct lustre_hash_operations *hash_operations);
91 void lustre_hash_exit(struct lustre_class_hash_body **hash_body);
92 int lustre_hash_additem_unique(struct lustre_class_hash_body *hash_body,
93 void *key, struct hlist_node *actual_hnode);
94 void *lustre_hash_findadd_unique(struct lustre_class_hash_body *hash_body,
95 void *key, struct hlist_node *actual_hnode);
96 int lustre_hash_additem(struct lustre_class_hash_body *hash_body, void *key,
97 struct hlist_node *actual_hnode);
98 int lustre_hash_delitem_by_key(struct lustre_class_hash_body *hash_body,
100 int lustre_hash_delitem(struct lustre_class_hash_body *hash_body, void *key,
101 struct hlist_node *hash_item);
102 void lustre_hash_bucket_iterate(struct lustre_class_hash_body *hash_body,
103 void *key, hash_item_iterate_cb,
105 void lustre_hash_iterate_all(struct lustre_class_hash_body *hash_body,
106 hash_item_iterate_cb, void *data);
108 void * lustre_hash_get_object_by_key(struct lustre_class_hash_body *hash_body,
111 __u32 djb2_hashfn(struct lustre_class_hash_body *hash_body, void* key,
114 /* ( uuid <-> export ) hash operations define */
115 __u32 uuid_hashfn(struct lustre_class_hash_body *hash_body, void * key);
116 int uuid_hash_key_compare(void *key, struct hlist_node * compared_hnode);
117 void * uuid_export_refcount_get(struct hlist_node * actual_hnode);
118 void uuid_export_refcount_put(struct hlist_node * actual_hnode);
120 /* ( nid <-> export ) hash operations define */
121 __u32 nid_hashfn(struct lustre_class_hash_body *hash_body, void * key);
122 int nid_hash_key_compare(void *key, struct hlist_node * compared_hnode);
123 void * nid_export_refcount_get(struct hlist_node * actual_hnode);
124 void nid_export_refcount_put(struct hlist_node * actual_hnode);
126 /* ( net_peer <-> connection ) hash operations define */
127 __u32 conn_hashfn(struct lustre_class_hash_body *hash_body, void * key);
128 int conn_hash_key_compare(void *key, struct hlist_node * compared_hnode);
129 void * conn_refcount_get(struct hlist_node * actual_hnode);
130 void conn_refcount_put(struct hlist_node * actual_hnode);
132 /* ( nid <-> nidstats ) hash operations define. uses nid_hashfn */
133 int nidstats_hash_key_compare(void *key, struct hlist_node * compared_hnode);
134 void* nidstats_refcount_get(struct hlist_node * actual_hnode);
135 void nidstats_refcount_put(struct hlist_node * actual_hnode);
136 extern struct lustre_hash_operations nid_stat_hash_operations;
138 #endif /* __CLASS_HASH_H */