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>
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 /* !__KERNEL__ */
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,
86 struct portals_handle_ops *ops)
88 struct handle_bucket *bucket;
92 LASSERT(cfs_list_empty(&h->h_link));
95 * This is fast, but simplistic cookie generation algorithm, it will
96 * need a re-do at some point in the future for security.
98 cfs_spin_lock(&handle_base_lock);
99 handle_base += HANDLE_INCR;
101 h->h_cookie = handle_base;
102 if (unlikely(handle_base == 0)) {
104 * Cookie of zero is "dangerous", because in many places it's
105 * assumed that 0 means "unassigned" handle, not bound to any
108 CWARN("The universe has been exhausted: cookie wrap-around.\n");
109 handle_base += HANDLE_INCR;
111 cfs_spin_unlock(&handle_base_lock);
114 cfs_spin_lock_init(&h->h_lock);
116 bucket = &handle_hash[h->h_cookie & HANDLE_HASH_MASK];
117 cfs_spin_lock(&bucket->lock);
118 list_add_rcu(&h->h_link, &bucket->head);
120 cfs_spin_unlock(&bucket->lock);
122 CDEBUG(D_INFO, "added object %p with handle "LPX64" to hash\n",
127 static void class_handle_unhash_nolock(struct portals_handle *h)
129 if (cfs_list_empty(&h->h_link)) {
130 CERROR("removing an already-removed handle ("LPX64")\n",
135 CDEBUG(D_INFO, "removing object %p with handle "LPX64" from hash\n",
138 cfs_spin_lock(&h->h_lock);
140 cfs_spin_unlock(&h->h_lock);
144 cfs_spin_unlock(&h->h_lock);
145 list_del_rcu(&h->h_link);
148 void class_handle_unhash(struct portals_handle *h)
150 struct handle_bucket *bucket;
151 bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
153 cfs_spin_lock(&bucket->lock);
154 class_handle_unhash_nolock(h);
155 cfs_spin_unlock(&bucket->lock);
158 void class_handle_hash_back(struct portals_handle *h)
160 struct handle_bucket *bucket;
163 bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
165 cfs_spin_lock(&bucket->lock);
166 list_add_rcu(&h->h_link, &bucket->head);
168 cfs_spin_unlock(&bucket->lock);
173 void *class_handle2object(__u64 cookie)
175 struct handle_bucket *bucket;
176 struct portals_handle *h;
180 LASSERT(handle_hash != NULL);
182 /* Be careful when you want to change this code. See the
183 * rcu_read_lock() definition on top this file. - jxiong */
184 bucket = handle_hash + (cookie & HANDLE_HASH_MASK);
187 list_for_each_entry_rcu(h, &bucket->head, h_link) {
188 if (h->h_cookie != cookie)
191 cfs_spin_lock(&h->h_lock);
192 if (likely(h->h_in != 0)) {
193 h->h_ops->hop_addref(h);
196 cfs_spin_unlock(&h->h_lock);
204 void class_handle_free_cb(cfs_rcu_head_t *rcu)
206 struct portals_handle *h = RCU2HANDLE(rcu);
207 void *ptr = (void *)(unsigned long)h->h_cookie;
209 if (h->h_ops->hop_free != NULL)
210 h->h_ops->hop_free(ptr, h->h_size);
212 OBD_FREE(ptr, 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 ops %p\n",
256 h->h_cookie, h, h->h_ops);
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);