4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/obdclass/lustre_handles.c
38 * Author: Phil Schwan <phil@clusterfs.com>
41 #define DEBUG_SUBSYSTEM S_CLASS
43 # include <liblustre.h>
46 #include <obd_support.h>
47 #include <lustre_handles.h>
48 #include <lustre_lib.h>
51 # define list_add_rcu cfs_list_add
52 # define list_del_rcu cfs_list_del
53 # define list_for_each_rcu cfs_list_for_each
54 # define list_for_each_safe_rcu cfs_list_for_each_safe
55 # define list_for_each_entry_rcu cfs_list_for_each_entry
56 # define rcu_read_lock() spin_lock(&bucket->lock)
57 # define rcu_read_unlock() spin_unlock(&bucket->lock)
58 #endif /* !__KERNEL__ */
60 static __u64 handle_base;
62 static spinlock_t handle_base_lock;
64 static struct handle_bucket {
69 #define HANDLE_HASH_SIZE (1 << 16)
70 #define HANDLE_HASH_MASK (HANDLE_HASH_SIZE - 1)
73 * Generate a unique 64bit cookie (hash) for a handle and insert it into
74 * global (per-node) hash-table.
76 void class_handle_hash(struct portals_handle *h,
77 struct portals_handle_ops *ops)
79 struct handle_bucket *bucket;
83 LASSERT(cfs_list_empty(&h->h_link));
86 * This is fast, but simplistic cookie generation algorithm, it will
87 * need a re-do at some point in the future for security.
89 spin_lock(&handle_base_lock);
90 handle_base += HANDLE_INCR;
92 if (unlikely(handle_base == 0)) {
94 * Cookie of zero is "dangerous", because in many places it's
95 * assumed that 0 means "unassigned" handle, not bound to any
98 CWARN("The universe has been exhausted: cookie wrap-around.\n");
99 handle_base += HANDLE_INCR;
101 h->h_cookie = handle_base;
102 spin_unlock(&handle_base_lock);
105 spin_lock_init(&h->h_lock);
107 bucket = &handle_hash[h->h_cookie & HANDLE_HASH_MASK];
108 spin_lock(&bucket->lock);
109 list_add_rcu(&h->h_link, &bucket->head);
111 spin_unlock(&bucket->lock);
113 CDEBUG(D_INFO, "added object %p with handle "LPX64" to hash\n",
117 EXPORT_SYMBOL(class_handle_hash);
119 static void class_handle_unhash_nolock(struct portals_handle *h)
121 if (cfs_list_empty(&h->h_link)) {
122 CERROR("removing an already-removed handle ("LPX64")\n",
127 CDEBUG(D_INFO, "removing object %p with handle "LPX64" from hash\n",
130 spin_lock(&h->h_lock);
132 spin_unlock(&h->h_lock);
136 spin_unlock(&h->h_lock);
137 list_del_rcu(&h->h_link);
140 void class_handle_unhash(struct portals_handle *h)
142 struct handle_bucket *bucket;
143 bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
145 spin_lock(&bucket->lock);
146 class_handle_unhash_nolock(h);
147 spin_unlock(&bucket->lock);
149 EXPORT_SYMBOL(class_handle_unhash);
151 void class_handle_hash_back(struct portals_handle *h)
153 struct handle_bucket *bucket;
156 bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
158 spin_lock(&bucket->lock);
159 list_add_rcu(&h->h_link, &bucket->head);
161 spin_unlock(&bucket->lock);
165 EXPORT_SYMBOL(class_handle_hash_back);
167 void *class_handle2object(__u64 cookie)
169 struct handle_bucket *bucket;
170 struct portals_handle *h;
174 LASSERT(handle_hash != NULL);
176 /* Be careful when you want to change this code. See the
177 * rcu_read_lock() definition on top this file. - jxiong */
178 bucket = handle_hash + (cookie & HANDLE_HASH_MASK);
181 list_for_each_entry_rcu(h, &bucket->head, h_link) {
182 if (h->h_cookie != cookie)
185 spin_lock(&h->h_lock);
186 if (likely(h->h_in != 0)) {
187 h->h_ops->hop_addref(h);
190 spin_unlock(&h->h_lock);
197 EXPORT_SYMBOL(class_handle2object);
199 void class_handle_free_cb(cfs_rcu_head_t *rcu)
201 struct portals_handle *h = RCU2HANDLE(rcu);
202 void *ptr = (void *)(unsigned long)h->h_cookie;
204 if (h->h_ops->hop_free != NULL)
205 h->h_ops->hop_free(ptr, h->h_size);
207 OBD_FREE(ptr, h->h_size);
209 EXPORT_SYMBOL(class_handle_free_cb);
211 int class_handle_init(void)
213 struct handle_bucket *bucket;
217 LASSERT(handle_hash == NULL);
219 OBD_ALLOC_LARGE(handle_hash, sizeof(*bucket) * HANDLE_HASH_SIZE);
220 if (handle_hash == NULL)
223 spin_lock_init(&handle_base_lock);
224 for (bucket = handle_hash + HANDLE_HASH_SIZE - 1; bucket >= handle_hash;
226 CFS_INIT_LIST_HEAD(&bucket->head);
227 spin_lock_init(&bucket->lock);
230 /** bug 21430: add randomness to the initial base */
231 cfs_get_random_bytes(seed, sizeof(seed));
232 cfs_gettimeofday(&tv);
233 cfs_srand(tv.tv_sec ^ seed[0], tv.tv_usec ^ seed[1]);
235 cfs_get_random_bytes(&handle_base, sizeof(handle_base));
236 LASSERT(handle_base != 0ULL);
241 static int cleanup_all_handles(void)
246 for (rc = i = 0; i < HANDLE_HASH_SIZE; i++) {
247 struct portals_handle *h;
249 spin_lock(&handle_hash[i].lock);
250 list_for_each_entry_rcu(h, &(handle_hash[i].head), h_link) {
251 CERROR("force clean handle "LPX64" addr %p ops %p\n",
252 h->h_cookie, h, h->h_ops);
254 class_handle_unhash_nolock(h);
257 spin_unlock(&handle_hash[i].lock);
263 void class_handle_cleanup(void)
266 LASSERT(handle_hash != NULL);
268 count = cleanup_all_handles();
270 OBD_FREE_LARGE(handle_hash, sizeof(*handle_hash) * HANDLE_HASH_SIZE);
274 CERROR("handle_count at cleanup: %d\n", count);