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, Whamcloud, Inc.
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>
50 #if !defined(HAVE_RCU) || !defined(__KERNEL__)
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() cfs_spin_lock(&bucket->lock)
57 # define rcu_read_unlock() cfs_spin_unlock(&bucket->lock)
58 #endif /* ifndef HAVE_RCU */
60 static __u64 handle_base;
62 static cfs_spinlock_t handle_base_lock;
64 static struct handle_bucket {
70 /* For unknown reason, UML uses kmalloc rather than vmalloc to allocate
71 * memory(OBD_VMALLOC). Therefore, we have to redefine the
72 * HANDLE_HASH_SIZE to make the hash heads don't exceed 128K.
74 #define HANDLE_HASH_SIZE 4096
76 #define HANDLE_HASH_SIZE (1 << 16)
77 #endif /* ifdef __arch_um__ */
79 #define HANDLE_HASH_MASK (HANDLE_HASH_SIZE - 1)
82 * Generate a unique 64bit cookie (hash) for a handle and insert it into
83 * global (per-node) hash-table.
85 void class_handle_hash(struct portals_handle *h, portals_handle_addref_cb cb)
87 struct handle_bucket *bucket;
91 LASSERT(cfs_list_empty(&h->h_link));
94 * This is fast, but simplistic cookie generation algorithm, it will
95 * need a re-do at some point in the future for security.
97 cfs_spin_lock(&handle_base_lock);
98 handle_base += HANDLE_INCR;
100 h->h_cookie = handle_base;
101 if (unlikely(handle_base == 0)) {
103 * Cookie of zero is "dangerous", because in many places it's
104 * assumed that 0 means "unassigned" handle, not bound to any
107 CWARN("The universe has been exhausted: cookie wrap-around.\n");
108 handle_base += HANDLE_INCR;
110 cfs_spin_unlock(&handle_base_lock);
113 cfs_spin_lock_init(&h->h_lock);
115 bucket = &handle_hash[h->h_cookie & HANDLE_HASH_MASK];
116 cfs_spin_lock(&bucket->lock);
117 list_add_rcu(&h->h_link, &bucket->head);
119 cfs_spin_unlock(&bucket->lock);
121 CDEBUG(D_INFO, "added object %p with handle "LPX64" to hash\n",
126 static void class_handle_unhash_nolock(struct portals_handle *h)
128 if (cfs_list_empty(&h->h_link)) {
129 CERROR("removing an already-removed handle ("LPX64")\n",
134 CDEBUG(D_INFO, "removing object %p with handle "LPX64" from hash\n",
137 cfs_spin_lock(&h->h_lock);
139 cfs_spin_unlock(&h->h_lock);
143 cfs_spin_unlock(&h->h_lock);
144 list_del_rcu(&h->h_link);
147 void class_handle_unhash(struct portals_handle *h)
149 struct handle_bucket *bucket;
150 bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
152 cfs_spin_lock(&bucket->lock);
153 class_handle_unhash_nolock(h);
154 cfs_spin_unlock(&bucket->lock);
157 void class_handle_hash_back(struct portals_handle *h)
159 struct handle_bucket *bucket;
162 bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
164 cfs_spin_lock(&bucket->lock);
165 list_add_rcu(&h->h_link, &bucket->head);
167 cfs_spin_unlock(&bucket->lock);
172 void *class_handle2object(__u64 cookie)
174 struct handle_bucket *bucket;
175 struct portals_handle *h;
179 LASSERT(handle_hash != NULL);
181 /* Be careful when you want to change this code. See the
182 * rcu_read_lock() definition on top this file. - jxiong */
183 bucket = handle_hash + (cookie & HANDLE_HASH_MASK);
186 list_for_each_entry_rcu(h, &bucket->head, h_link) {
187 if (h->h_cookie != cookie)
190 cfs_spin_lock(&h->h_lock);
191 if (likely(h->h_in != 0)) {
195 cfs_spin_unlock(&h->h_lock);
203 void class_handle_free_cb(cfs_rcu_head_t *rcu)
205 struct portals_handle *h = RCU2HANDLE(rcu);
207 h->h_free_cb(h->h_ptr, h->h_size);
209 void *ptr = h->h_ptr;
210 unsigned int size = h->h_size;
215 int class_handle_init(void)
217 struct handle_bucket *bucket;
221 LASSERT(handle_hash == NULL);
223 OBD_ALLOC_LARGE(handle_hash, sizeof(*bucket) * HANDLE_HASH_SIZE);
224 if (handle_hash == NULL)
227 cfs_spin_lock_init(&handle_base_lock);
228 for (bucket = handle_hash + HANDLE_HASH_SIZE - 1; bucket >= handle_hash;
230 CFS_INIT_LIST_HEAD(&bucket->head);
231 cfs_spin_lock_init(&bucket->lock);
234 /** bug 21430: add randomness to the initial base */
235 cfs_get_random_bytes(seed, sizeof(seed));
236 cfs_gettimeofday(&tv);
237 cfs_srand(tv.tv_sec ^ seed[0], tv.tv_usec ^ seed[1]);
239 cfs_get_random_bytes(&handle_base, sizeof(handle_base));
240 LASSERT(handle_base != 0ULL);
245 static int cleanup_all_handles(void)
250 for (rc = i = 0; i < HANDLE_HASH_SIZE; i++) {
251 struct portals_handle *h;
253 cfs_spin_lock(&handle_hash[i].lock);
254 list_for_each_entry_rcu(h, &(handle_hash[i].head), h_link) {
255 CERROR("force clean handle "LPX64" addr %p addref %p\n",
256 h->h_cookie, h, h->h_addref);
258 class_handle_unhash_nolock(h);
261 cfs_spin_unlock(&handle_hash[i].lock);
267 void class_handle_cleanup(void)
270 LASSERT(handle_hash != NULL);
272 count = cleanup_all_handles();
274 OBD_FREE_LARGE(handle_hash, sizeof(*handle_hash) * HANDLE_HASH_SIZE);
278 CERROR("handle_count at cleanup: %d\n", count);