Whamcloud - gitweb
Land b1_6_bug11013 onto HEAD (20070313_0924)
[fs/lustre-release.git] / lustre / include / class_hash.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  */
4
5 #ifndef __CLASS_HASH_H
6 #define __CLASS_HASH_H
7
8 #include <lustre_lib.h>
9
10 /* define the hash bucket*/
11 struct lustre_hash_bucket { 
12         struct hlist_head lhb_head;
13         spinlock_t lhb_lock;
14 #ifdef LUSTRE_HASH_DEBUG
15         /* the number of hash item per bucket, 
16          * it will help us to analyse the hash distribute 
17          */
18         int lhb_item_count; 
19 #endif      
20 };
21
22 struct lustre_hash_operations;
23
24 struct lustre_class_hash_body {
25         char hashname[128];
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;
31 };
32
33 /* hash operations method define */
34 struct lustre_hash_operations {
35         __u32 (*lustre_hashfn) (struct lustre_class_hash_body *hash_body, 
36                                 void *key);
37         int   (*lustre_hash_key_compare) (void *key, 
38                                           struct hlist_node *compared_hnode);
39         /* add refcount */ 
40         void* (*lustre_hash_object_refcount_get) (struct hlist_node *hash_item);
41         /* dec refcount */
42         void  (*lustre_hash_object_refcount_put) (struct hlist_node *hash_item);
43 };
44
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)
48 {
49         struct lustre_hash_bucket *bucket;
50         struct hlist_node  *hash_item_node;
51         struct lustre_hash_operations *hop = hash_body->lchb_hash_operations;
52         int find = 0;
53         ENTRY;
54
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);
58                 if (find == 1)
59                         break;
60         }
61         RETURN(find == 1 ? hash_item_node : NULL);
62 }
63
64 static inline int 
65 lustre_hash_delitem_nolock(struct lustre_class_hash_body *hash_body, 
66                            int hashent, struct hlist_node * hash_item)
67 {
68         struct lustre_hash_operations *hop = hash_body->lchb_hash_operations;
69
70         hlist_del_init(hash_item);
71
72         hop->lustre_hash_object_refcount_put(hash_item);
73
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);
79 #endif
80
81         RETURN(0);
82 }
83
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, 
93                                void *key);
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,
97                                       void *key);
98
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);
104
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);
110
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);
116
117 #endif /* __CLASS_HASH_H */