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.
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 list_add
52 # define list_del_rcu list_del
53 # define list_for_each_rcu list_for_each
54 # define list_for_each_safe_rcu list_for_each_safe
55 # define rcu_read_lock() spin_lock(&bucket->lock)
56 # define rcu_read_unlock() spin_unlock(&bucket->lock)
57 #endif /* ifndef HAVE_RCU */
59 static __u64 handle_base;
61 static spinlock_t handle_base_lock;
63 static struct handle_bucket {
65 struct list_head head;
68 static atomic_t handle_count = ATOMIC_INIT(0);
71 /* For unknown reason, UML uses kmalloc rather than vmalloc to allocate
72 * memory(OBD_VMALLOC). Therefore, we have to redefine the
73 * HANDLE_HASH_SIZE to make the hash heads don't exceed 128K.
75 #define HANDLE_HASH_SIZE 4096
77 #define HANDLE_HASH_SIZE (1 << 14)
78 #endif /* ifdef __arch_um__ */
80 #define HANDLE_HASH_MASK (HANDLE_HASH_SIZE - 1)
83 * Generate a unique 64bit cookie (hash) for a handle and insert it into
84 * global (per-node) hash-table.
86 void class_handle_hash(struct portals_handle *h, portals_handle_addref_cb cb)
88 struct handle_bucket *bucket;
92 LASSERT(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 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 spin_unlock(&handle_base_lock);
113 atomic_inc(&handle_count);
115 spin_lock_init(&h->h_lock);
117 bucket = &handle_hash[h->h_cookie & HANDLE_HASH_MASK];
118 spin_lock(&bucket->lock);
119 list_add_rcu(&h->h_link, &bucket->head);
121 spin_unlock(&bucket->lock);
123 CDEBUG(D_INFO, "added object %p with handle "LPX64" to hash\n",
128 static void class_handle_unhash_nolock(struct portals_handle *h)
130 if (list_empty(&h->h_link)) {
131 CERROR("removing an already-removed handle ("LPX64")\n",
136 CDEBUG(D_INFO, "removing object %p with handle "LPX64" from hash\n",
139 spin_lock(&h->h_lock);
141 spin_unlock(&h->h_lock);
145 spin_unlock(&h->h_lock);
146 list_del_rcu(&h->h_link);
149 void class_handle_unhash(struct portals_handle *h)
151 struct handle_bucket *bucket;
152 bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
154 spin_lock(&bucket->lock);
155 class_handle_unhash_nolock(h);
156 spin_unlock(&bucket->lock);
158 atomic_dec(&handle_count);
161 void *class_handle2object(__u64 cookie)
163 struct handle_bucket *bucket;
164 struct list_head *tmp;
168 LASSERT(handle_hash != NULL);
170 /* Be careful when you want to change this code. See the
171 * rcu_read_lock() definition on top this file. - jxiong */
172 bucket = handle_hash + (cookie & HANDLE_HASH_MASK);
175 list_for_each_rcu(tmp, &bucket->head) {
176 struct portals_handle *h;
177 h = list_entry(tmp, struct portals_handle, h_link);
178 if (h->h_cookie != cookie)
181 spin_lock(&h->h_lock);
182 if (likely(h->h_in != 0)) {
186 spin_unlock(&h->h_lock);
194 void class_handle_free_cb(struct rcu_head *rcu)
196 struct portals_handle *h = RCU2HANDLE(rcu);
198 h->h_free_cb(h->h_ptr, h->h_size);
200 void *ptr = h->h_ptr;
201 unsigned int size = h->h_size;
207 int class_handle_init(void)
209 struct handle_bucket *bucket;
211 LASSERT(handle_hash == NULL);
213 OBD_VMALLOC(handle_hash, sizeof(*bucket) * HANDLE_HASH_SIZE);
214 if (handle_hash == NULL)
217 spin_lock_init(&handle_base_lock);
218 for (bucket = handle_hash + HANDLE_HASH_SIZE - 1; bucket >= handle_hash;
220 CFS_INIT_LIST_HEAD(&bucket->head);
221 spin_lock_init(&bucket->lock);
224 ll_get_random_bytes(&handle_base, sizeof(handle_base));
225 LASSERT(handle_base != 0ULL);
230 static void cleanup_all_handles(void)
234 for (i = 0; i < HANDLE_HASH_SIZE; i++) {
235 struct list_head *tmp, *pos;
236 spin_lock(&handle_hash[i].lock);
237 list_for_each_safe_rcu(tmp, pos, &(handle_hash[i].head)) {
238 struct portals_handle *h;
239 h = list_entry(tmp, struct portals_handle, h_link);
241 CERROR("force clean handle "LPX64" addr %p addref %p\n",
242 h->h_cookie, h, h->h_addref);
244 class_handle_unhash_nolock(h);
246 spin_unlock(&handle_hash[i].lock);
250 void class_handle_cleanup(void)
253 LASSERT(handle_hash != NULL);
255 count = atomic_read(&handle_count);
257 CERROR("handle_count at cleanup: %d\n", count);
258 cleanup_all_handles();
261 OBD_VFREE(handle_hash, sizeof(*handle_hash) * HANDLE_HASH_SIZE);
264 if (atomic_read(&handle_count))
265 CERROR("leaked %d handles\n", atomic_read(&handle_count));