1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #ifndef __CLASS_HASH_H
38 #define __CLASS_HASH_H
40 #include <lustre_lib.h>
42 /* #define LUSTRE_HASH_DEBUG 1 */
44 /* define the hash bucket*/
45 struct lustre_hash_bucket {
46 struct hlist_head lhb_head;
48 #ifdef LUSTRE_HASH_DEBUG
49 /* the number of hash item per bucket,
50 * it will help us to analyse the hash distribute
56 struct lustre_hash_operations;
58 struct lustre_class_hash_body {
60 spinlock_t lchb_lock; /* body lock */
61 struct lustre_hash_bucket *lchb_hash_tables;
62 __u32 lchb_hash_max_size; /* define the hash tables size */
63 /* define the hash operations */
64 struct lustre_hash_operations *lchb_hash_operations;
67 /* hash operations method define */
68 struct lustre_hash_operations {
69 __u32 (*lustre_hashfn) (struct lustre_class_hash_body *hash_body,
71 int (*lustre_hash_key_compare) (void *key,
72 struct hlist_node *compared_hnode);
74 void* (*lustre_hash_object_refcount_get) (struct hlist_node *hash_item);
76 void (*lustre_hash_object_refcount_put) (struct hlist_node *hash_item);
79 static inline struct hlist_node *
80 lustre_hash_getitem_in_bucket_nolock(struct lustre_class_hash_body *hash_body,
81 int hashent, void *key)
83 struct lustre_hash_bucket *bucket;
84 struct hlist_node *hash_item_node;
85 struct lustre_hash_operations *hop = hash_body->lchb_hash_operations;
89 bucket = &hash_body->lchb_hash_tables[hashent];
90 hlist_for_each(hash_item_node, &(bucket->lhb_head)) {
91 find = hop->lustre_hash_key_compare(key, hash_item_node);
95 RETURN(find == 1 ? hash_item_node : NULL);
99 lustre_hash_delitem_nolock(struct lustre_class_hash_body *hash_body,
100 int hashent, struct hlist_node * hash_item)
102 struct lustre_hash_operations *hop = hash_body->lchb_hash_operations;
104 hlist_del_init(hash_item);
106 hop->lustre_hash_object_refcount_put(hash_item);
108 #ifdef LUSTRE_HASH_DEBUG
109 hash_body->lchb_hash_tables[hashent].lhb_item_count--;
110 CDEBUG(D_INFO, "hashname[%s] bucket[%d] has [%d] hashitem\n",
111 hash_body->hashname, hashent,
112 hash_body->lchb_hash_tables[hashent].lhb_item_count);
118 typedef void (*hash_item_iterate_cb) (void *obj, void *data);
120 int lustre_hash_init(struct lustre_class_hash_body **hash_body,
121 char *hashname, __u32 hashsize,
122 struct lustre_hash_operations *hash_operations);
123 void lustre_hash_exit(struct lustre_class_hash_body **hash_body);
124 int lustre_hash_additem_unique(struct lustre_class_hash_body *hash_body,
125 void *key, struct hlist_node *actual_hnode);
126 void *lustre_hash_findadd_unique(struct lustre_class_hash_body *hash_body,
127 void *key, struct hlist_node *actual_hnode);
128 int lustre_hash_additem(struct lustre_class_hash_body *hash_body, void *key,
129 struct hlist_node *actual_hnode);
130 int lustre_hash_delitem_by_key(struct lustre_class_hash_body *hash_body,
132 int lustre_hash_delitem(struct lustre_class_hash_body *hash_body, void *key,
133 struct hlist_node *hash_item);
134 void lustre_hash_bucket_iterate(struct lustre_class_hash_body *hash_body,
135 void *key, hash_item_iterate_cb,
137 void lustre_hash_iterate_all(struct lustre_class_hash_body *hash_body,
138 hash_item_iterate_cb, void *data);
140 void * lustre_hash_get_object_by_key(struct lustre_class_hash_body *hash_body,
143 __u32 djb2_hashfn(struct lustre_class_hash_body *hash_body, void* key,
146 /* ( uuid <-> export ) hash operations define */
147 __u32 uuid_hashfn(struct lustre_class_hash_body *hash_body, void * key);
148 int uuid_hash_key_compare(void *key, struct hlist_node * compared_hnode);
149 void * uuid_export_refcount_get(struct hlist_node * actual_hnode);
150 void uuid_export_refcount_put(struct hlist_node * actual_hnode);
152 /* ( nid <-> export ) hash operations define */
153 __u32 nid_hashfn(struct lustre_class_hash_body *hash_body, void * key);
154 int nid_hash_key_compare(void *key, struct hlist_node * compared_hnode);
155 void * nid_export_refcount_get(struct hlist_node * actual_hnode);
156 void nid_export_refcount_put(struct hlist_node * actual_hnode);
158 /* ( net_peer <-> connection ) hash operations define */
159 __u32 conn_hashfn(struct lustre_class_hash_body *hash_body, void * key);
160 int conn_hash_key_compare(void *key, struct hlist_node * compared_hnode);
161 void * conn_refcount_get(struct hlist_node * actual_hnode);
162 void conn_refcount_put(struct hlist_node * actual_hnode);
164 /* ( nid <-> nidstats ) hash operations define. uses nid_hashfn */
165 int nidstats_hash_key_compare(void *key, struct hlist_node * compared_hnode);
166 void* nidstats_refcount_get(struct hlist_node * actual_hnode);
167 void nidstats_refcount_put(struct hlist_node * actual_hnode);
168 extern struct lustre_hash_operations nid_stat_hash_operations;
170 #endif /* __CLASS_HASH_H */