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 the hash bucket*/
11 struct lustre_hash_bucket {
12 struct hlist_head lhb_head;
14 #ifdef LUSTRE_HASH_DEBUG
15 /* the number of hash item per bucket,
16 * it will help us to analyse the hash distribute
22 struct lustre_hash_operations;
24 struct lustre_class_hash_body {
26 spinlock_t lchb_lock; /* body lock */
27 struct lustre_hash_bucket *lchb_hash_tables;
28 __u32 lchb_hash_max_size; /* define the hash tables size */
29 /* define the hash operations */
30 struct lustre_hash_operations *lchb_hash_operations;
33 /* hash operations method define */
34 struct lustre_hash_operations {
35 __u32 (*lustre_hashfn) (struct lustre_class_hash_body *hash_body,
37 int (*lustre_hash_key_compare) (void *key,
38 struct hlist_node *compared_hnode);
40 void* (*lustre_hash_object_refcount_get) (struct hlist_node *hash_item);
42 void (*lustre_hash_object_refcount_put) (struct hlist_node *hash_item);
45 static inline struct hlist_node *
46 lustre_hash_getitem_in_bucket_nolock(struct lustre_class_hash_body *hash_body,
47 int hashent, void *key)
49 struct lustre_hash_bucket *bucket;
50 struct hlist_node *hash_item_node;
51 struct lustre_hash_operations *hop = hash_body->lchb_hash_operations;
55 bucket = &hash_body->lchb_hash_tables[hashent];
56 hlist_for_each(hash_item_node, &(bucket->lhb_head)) {
57 find = hop->lustre_hash_key_compare(key, hash_item_node);
61 RETURN(find == 1 ? hash_item_node : NULL);
65 lustre_hash_delitem_nolock(struct lustre_class_hash_body *hash_body,
66 int hashent, struct hlist_node * hash_item)
68 struct lustre_hash_operations *hop = hash_body->lchb_hash_operations;
70 hlist_del_init(hash_item);
72 hop->lustre_hash_object_refcount_put(hash_item);
74 #ifdef LUSTRE_HASH_DEBUG
75 hash_body->lchb_hash_tables[hashent].lhb_item_count--;
76 CDEBUG(D_INFO, "hashname[%s] bucket[%d] has [%d] hashitem\n",
77 hash_body->hashname, hashent,
78 hash_body->lchb_hash_tables[hashent].lhb_item_count);
84 int lustre_hash_init(struct lustre_class_hash_body **hash_body,
85 char *hashname, __u32 hashsize,
86 struct lustre_hash_operations *hash_operations);
87 void lustre_hash_exit(struct lustre_class_hash_body **hash_body);
88 int lustre_hash_additem_unique(struct lustre_class_hash_body *hash_body,
89 void *key, struct hlist_node *actual_hnode);
90 int lustre_hash_additem(struct lustre_class_hash_body *hash_body, void *key,
91 struct hlist_node *actual_hnode);
92 int lustre_hash_delitem_by_key(struct lustre_class_hash_body *hash_body,
94 int lustre_hash_delitem(struct lustre_class_hash_body *hash_body, void *key,
95 struct hlist_node *hash_item);
96 void * lustre_hash_get_object_by_key(struct lustre_class_hash_body *hash_body,
99 /* ( uuid <-> export ) hash operations define */
100 __u32 uuid_hashfn(struct lustre_class_hash_body *hash_body, void * key);
101 int uuid_hash_key_compare(void *key, struct hlist_node * compared_hnode);
102 void * uuid_export_refcount_get(struct hlist_node * actual_hnode);
103 void uuid_export_refcount_put(struct hlist_node * actual_hnode);
105 /* ( nid <-> export ) hash operations define */
106 __u32 nid_hashfn(struct lustre_class_hash_body *hash_body, void * key);
107 int nid_hash_key_compare(void *key, struct hlist_node * compared_hnode);
108 void * nid_export_refcount_get(struct hlist_node * actual_hnode);
109 void nid_export_refcount_put(struct hlist_node * actual_hnode);
111 /* ( net_peer <-> connection ) hash operations define */
112 __u32 conn_hashfn(struct lustre_class_hash_body *hash_body, void * key);
113 int conn_hash_key_compare(void *key, struct hlist_node * compared_hnode);
114 void * conn_refcount_get(struct hlist_node * actual_hnode);
115 void conn_refcount_put(struct hlist_node * actual_hnode);
117 #endif /* __CLASS_HASH_H */