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, 2012, 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 "../include/obd_support.h"
44 #include "../include/lustre_handles.h"
45 #include "../include/lustre_lib.h"
48 static __u64 handle_base;
50 static spinlock_t handle_base_lock;
52 static struct handle_bucket {
54 struct list_head head;
57 #define HANDLE_HASH_SIZE (1 << 16)
58 #define HANDLE_HASH_MASK (HANDLE_HASH_SIZE - 1)
61 * Generate a unique 64bit cookie (hash) for a handle and insert it into
62 * global (per-node) hash-table.
64 void class_handle_hash(struct portals_handle *h,
65 struct portals_handle_ops *ops)
67 struct handle_bucket *bucket;
70 LASSERT(list_empty(&h->h_link));
73 * This is fast, but simplistic cookie generation algorithm, it will
74 * need a re-do at some point in the future for security.
76 spin_lock(&handle_base_lock);
77 handle_base += HANDLE_INCR;
79 if (unlikely(handle_base == 0)) {
81 * Cookie of zero is "dangerous", because in many places it's
82 * assumed that 0 means "unassigned" handle, not bound to any
85 CWARN("The universe has been exhausted: cookie wrap-around.\n");
86 handle_base += HANDLE_INCR;
88 h->h_cookie = handle_base;
89 spin_unlock(&handle_base_lock);
92 spin_lock_init(&h->h_lock);
94 bucket = &handle_hash[h->h_cookie & HANDLE_HASH_MASK];
95 spin_lock(&bucket->lock);
96 list_add_rcu(&h->h_link, &bucket->head);
98 spin_unlock(&bucket->lock);
100 CDEBUG(D_INFO, "added object %p with handle %#llx to hash\n",
103 EXPORT_SYMBOL(class_handle_hash);
105 static void class_handle_unhash_nolock(struct portals_handle *h)
107 if (list_empty(&h->h_link)) {
108 CERROR("removing an already-removed handle (%#llx)\n",
113 CDEBUG(D_INFO, "removing object %p with handle %#llx from hash\n",
116 spin_lock(&h->h_lock);
118 spin_unlock(&h->h_lock);
122 spin_unlock(&h->h_lock);
123 list_del_rcu(&h->h_link);
126 void class_handle_unhash(struct portals_handle *h)
128 struct handle_bucket *bucket;
129 bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
131 spin_lock(&bucket->lock);
132 class_handle_unhash_nolock(h);
133 spin_unlock(&bucket->lock);
135 EXPORT_SYMBOL(class_handle_unhash);
137 void class_handle_hash_back(struct portals_handle *h)
139 struct handle_bucket *bucket;
141 bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
143 spin_lock(&bucket->lock);
144 list_add_rcu(&h->h_link, &bucket->head);
146 spin_unlock(&bucket->lock);
148 EXPORT_SYMBOL(class_handle_hash_back);
150 void *class_handle2object(__u64 cookie)
152 struct handle_bucket *bucket;
153 struct portals_handle *h;
156 LASSERT(handle_hash != NULL);
158 /* Be careful when you want to change this code. See the
159 * rcu_read_lock() definition on top this file. - jxiong */
160 bucket = handle_hash + (cookie & HANDLE_HASH_MASK);
163 list_for_each_entry_rcu(h, &bucket->head, h_link) {
164 if (h->h_cookie != cookie)
167 spin_lock(&h->h_lock);
168 if (likely(h->h_in != 0)) {
169 h->h_ops->hop_addref(h);
172 spin_unlock(&h->h_lock);
179 EXPORT_SYMBOL(class_handle2object);
181 void class_handle_free_cb(struct rcu_head *rcu)
183 struct portals_handle *h = RCU2HANDLE(rcu);
184 void *ptr = (void *)(unsigned long)h->h_cookie;
186 if (h->h_ops->hop_free != NULL)
187 h->h_ops->hop_free(ptr, h->h_size);
189 OBD_FREE(ptr, h->h_size);
191 EXPORT_SYMBOL(class_handle_free_cb);
193 int class_handle_init(void)
195 struct handle_bucket *bucket;
199 LASSERT(handle_hash == NULL);
201 OBD_ALLOC_LARGE(handle_hash, sizeof(*bucket) * HANDLE_HASH_SIZE);
202 if (handle_hash == NULL)
205 spin_lock_init(&handle_base_lock);
206 for (bucket = handle_hash + HANDLE_HASH_SIZE - 1; bucket >= handle_hash;
208 INIT_LIST_HEAD(&bucket->head);
209 spin_lock_init(&bucket->lock);
212 /** bug 21430: add randomness to the initial base */
213 cfs_get_random_bytes(seed, sizeof(seed));
214 do_gettimeofday(&tv);
215 cfs_srand(tv.tv_sec ^ seed[0], tv.tv_usec ^ seed[1]);
217 cfs_get_random_bytes(&handle_base, sizeof(handle_base));
218 LASSERT(handle_base != 0ULL);
223 static int cleanup_all_handles(void)
228 for (rc = i = 0; i < HANDLE_HASH_SIZE; i++) {
229 struct portals_handle *h;
231 spin_lock(&handle_hash[i].lock);
232 list_for_each_entry_rcu(h, &(handle_hash[i].head), h_link) {
233 CERROR("force clean handle %#llx addr %p ops %p\n",
234 h->h_cookie, h, h->h_ops);
236 class_handle_unhash_nolock(h);
239 spin_unlock(&handle_hash[i].lock);
245 void class_handle_cleanup(void)
248 LASSERT(handle_hash != NULL);
250 count = cleanup_all_handles();
252 OBD_FREE_LARGE(handle_hash, sizeof(*handle_hash) * HANDLE_HASH_SIZE);
256 CERROR("handle_count at cleanup: %d\n", count);