Whamcloud - gitweb
use special macro for print time_t, cleanup in includes.
[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 LUSTRE_HASH_DEBUG 1 */
11
12 /* define the hash bucket*/
13 struct lustre_hash_bucket { 
14         struct hlist_head lhb_head;
15         spinlock_t lhb_lock;
16 #ifdef LUSTRE_HASH_DEBUG
17         /* the number of hash item per bucket, 
18          * it will help us to analyse the hash distribute 
19          */
20         int lhb_item_count; 
21 #endif
22 };
23
24 struct lustre_hash_operations;
25
26 struct lustre_class_hash_body {
27         char hashname[128];
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;
33 };
34
35 /* hash operations method define */
36 struct lustre_hash_operations {
37         __u32 (*lustre_hashfn) (struct lustre_class_hash_body *hash_body, 
38                                 void *key);
39         int   (*lustre_hash_key_compare) (void *key, 
40                                           struct hlist_node *compared_hnode);
41         /* add refcount */ 
42         void* (*lustre_hash_object_refcount_get) (struct hlist_node *hash_item);
43         /* dec refcount */
44         void  (*lustre_hash_object_refcount_put) (struct hlist_node *hash_item);
45 };
46
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)
50 {
51         struct lustre_hash_bucket *bucket;
52         struct hlist_node  *hash_item_node;
53         struct lustre_hash_operations *hop = hash_body->lchb_hash_operations;
54         int find = 0;
55         ENTRY;
56
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);
60                 if (find == 1)
61                         break;
62         }
63         RETURN(find == 1 ? hash_item_node : NULL);
64 }
65
66 static inline int 
67 lustre_hash_delitem_nolock(struct lustre_class_hash_body *hash_body, 
68                            int hashent, struct hlist_node * hash_item)
69 {
70         struct lustre_hash_operations *hop = hash_body->lchb_hash_operations;
71
72         hlist_del_init(hash_item);
73
74         hop->lustre_hash_object_refcount_put(hash_item);
75
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);
81 #endif
82
83         RETURN(0);
84 }
85
86 typedef void (*hash_item_iterate_cb) (void *obj, void *data);
87
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, 
99                                void *key);
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,
104                                 void *data);
105 void lustre_hash_iterate_all(struct lustre_class_hash_body *hash_body,
106                              hash_item_iterate_cb, void *data);
107
108 void * lustre_hash_get_object_by_key(struct lustre_class_hash_body *hash_body,
109                                       void *key);
110
111 __u32 djb2_hashfn(struct lustre_class_hash_body *hash_body, void* key,
112                   size_t size);
113
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);
119
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);
125
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);
131
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;
137
138 #endif /* __CLASS_HASH_H */