1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002 Cluster File Systems, Inc.
5 * Author: Phil Schwan <phil@clusterfs.com>
7 * This file is part of the Lustre file system, http://www.lustre.org
8 * Lustre is a trademark of Cluster File Systems, Inc.
10 * You may have signed or agreed to another license before downloading
11 * this software. If so, you are bound by the terms and conditions
12 * of that agreement, and the following does not apply to you. See the
13 * LICENSE file included with this distribution for more information.
15 * If you did not agree to a different license, then this copy of Lustre
16 * is open source software; you can redistribute it and/or modify it
17 * under the terms of version 2 of the GNU General Public License as
18 * published by the Free Software Foundation.
20 * In either case, Lustre is distributed in the hope that it will be
21 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
22 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * license text for more details.
26 #define DEBUG_SUBSYSTEM S_CLASS
28 # include <liblustre.h>
31 #include <obd_support.h>
32 #include <lustre_handles.h>
33 #include <lustre_lib.h>
35 #if !defined(HAVE_RCU) || !defined(__KERNEL__)
36 # define list_add_rcu list_add
37 # define list_del_rcu list_del
38 # define list_for_each_rcu list_for_each
39 # define list_for_each_safe_rcu list_for_each_safe
40 # define rcu_read_lock() spin_lock(&bucket->lock)
41 # define rcu_read_unlock() spin_unlock(&bucket->lock)
42 #endif /* ifndef HAVE_RCU */
44 static __u64 handle_base;
46 static spinlock_t handle_base_lock;
48 static struct handle_bucket {
50 struct list_head head;
53 static atomic_t handle_count = ATOMIC_INIT(0);
55 #define HANDLE_HASH_SIZE (1 << 14)
56 #define HANDLE_HASH_MASK (HANDLE_HASH_SIZE - 1)
59 * Generate a unique 64bit cookie (hash) for a handle and insert it into
60 * global (per-node) hash-table.
62 void class_handle_hash(struct portals_handle *h, portals_handle_addref_cb cb)
64 struct handle_bucket *bucket;
68 LASSERT(list_empty(&h->h_link));
71 * This is fast, but simplistic cookie generation algorithm, it will
72 * need a re-do at some point in the future for security.
74 spin_lock(&handle_base_lock);
75 handle_base += HANDLE_INCR;
77 h->h_cookie = handle_base;
78 if (unlikely(handle_base == 0)) {
80 * Cookie of zero is "dangerous", because in many places it's
81 * assumed that 0 means "unassigned" handle, not bound to any
84 CWARN("The universe has been exhausted: cookie wrap-around.\n");
85 handle_base += HANDLE_INCR;
87 spin_unlock(&handle_base_lock);
89 atomic_inc(&handle_count);
91 spin_lock_init(&h->h_lock);
93 bucket = &handle_hash[h->h_cookie & HANDLE_HASH_MASK];
94 spin_lock(&bucket->lock);
95 list_add_rcu(&h->h_link, &bucket->head);
96 spin_unlock(&bucket->lock);
98 CDEBUG(D_INFO, "added object %p with handle "LPX64" to hash\n",
103 static void class_handle_unhash_nolock(struct portals_handle *h)
105 if (list_empty(&h->h_link)) {
106 CERROR("removing an already-removed handle ("LPX64")\n",
111 CDEBUG(D_INFO, "removing object %p with handle "LPX64" from hash\n",
114 spin_lock(&h->h_lock);
115 if (h->h_cookie == 0) {
116 spin_unlock(&h->h_lock);
120 spin_unlock(&h->h_lock);
121 list_del_rcu(&h->h_link);
124 void class_handle_unhash(struct portals_handle *h)
126 struct handle_bucket *bucket;
127 bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
129 spin_lock(&bucket->lock);
130 class_handle_unhash_nolock(h);
131 spin_unlock(&bucket->lock);
133 atomic_dec(&handle_count);
136 void class_handle_hash_back(struct portals_handle *h)
138 struct handle_bucket *bucket;
141 bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
143 atomic_inc(&handle_count);
144 spin_lock(&bucket->lock);
145 list_add_rcu(&h->h_link, &bucket->head);
146 spin_unlock(&bucket->lock);
151 void *class_handle2object(__u64 cookie)
153 struct handle_bucket *bucket;
154 struct list_head *tmp;
158 LASSERT(handle_hash != NULL);
160 /* Be careful when you want to change this code. See the
161 * rcu_read_lock() definition on top this file. - jxiong */
162 bucket = handle_hash + (cookie & HANDLE_HASH_MASK);
165 list_for_each_rcu(tmp, &bucket->head) {
166 struct portals_handle *h;
167 h = list_entry(tmp, struct portals_handle, h_link);
168 if (h->h_cookie != cookie)
171 spin_lock(&h->h_lock);
172 if (likely(h->h_cookie != 0)) {
176 spin_unlock(&h->h_lock);
184 void class_handle_free_cb(struct rcu_head *rcu)
186 struct portals_handle *h = RCU2HANDLE(rcu);
188 h->h_free_cb(h->h_ptr, h->h_size);
190 void *ptr = h->h_ptr;
191 unsigned int size = h->h_size;
196 int class_handle_init(void)
198 struct handle_bucket *bucket;
200 LASSERT(handle_hash == NULL);
202 OBD_VMALLOC(handle_hash, sizeof(*bucket) * HANDLE_HASH_SIZE);
203 if (handle_hash == NULL)
206 spin_lock_init(&handle_base_lock);
207 for (bucket = handle_hash + HANDLE_HASH_SIZE - 1; bucket >= handle_hash;
209 CFS_INIT_LIST_HEAD(&bucket->head);
210 spin_lock_init(&bucket->lock);
212 ll_get_random_bytes(&handle_base, sizeof(handle_base));
213 LASSERT(handle_base != 0ULL);
218 static void cleanup_all_handles(void)
222 for (i = 0; i < HANDLE_HASH_SIZE; i++) {
223 struct list_head *tmp, *pos;
224 spin_lock(&handle_hash[i].lock);
225 list_for_each_safe_rcu(tmp, pos, &(handle_hash[i].head)) {
226 struct portals_handle *h;
227 h = list_entry(tmp, struct portals_handle, h_link);
229 CERROR("force clean handle "LPX64" addr %p addref %p\n",
230 h->h_cookie, h, h->h_addref);
232 class_handle_unhash_nolock(h);
234 spin_unlock(&handle_hash[i].lock);
238 void class_handle_cleanup(void)
241 LASSERT(handle_hash != NULL);
243 count = atomic_read(&handle_count);
245 CERROR("handle_count at cleanup: %d\n", count);
246 cleanup_all_handles();
249 OBD_VFREE(handle_hash, sizeof(*handle_hash) * HANDLE_HASH_SIZE);
252 if (atomic_read(&handle_count))
253 CERROR("leaked %d handles\n", atomic_read(&handle_count));