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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2014, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/obdclass/lustre_handles.c
34 * Author: Phil Schwan <phil@clusterfs.com>
37 #define DEBUG_SUBSYSTEM S_CLASS
39 #include <obd_support.h>
40 #include <lustre_handles.h>
41 #include <lustre_lib.h>
44 static __u64 handle_base;
46 static DEFINE_SPINLOCK(handle_base_lock);
48 static struct handle_bucket {
50 struct list_head head;
53 #define HANDLE_HASH_SIZE (1 << 16)
54 #define HANDLE_HASH_MASK (HANDLE_HASH_SIZE - 1)
57 * Generate a unique 64bit cookie (hash) for a handle and insert it into
58 * global (per-node) hash-table.
60 void class_handle_hash(struct portals_handle *h,
61 struct portals_handle_ops *ops)
63 struct handle_bucket *bucket;
67 LASSERT(list_empty(&h->h_link));
70 * This is fast, but simplistic cookie generation algorithm, it will
71 * need a re-do at some point in the future for security.
73 spin_lock(&handle_base_lock);
74 handle_base += HANDLE_INCR;
76 if (unlikely(handle_base == 0)) {
78 * Cookie of zero is "dangerous", because in many places it's
79 * assumed that 0 means "unassigned" handle, not bound to any
82 CWARN("The universe has been exhausted: cookie wrap-around.\n");
83 handle_base += HANDLE_INCR;
85 h->h_cookie = handle_base;
86 spin_unlock(&handle_base_lock);
89 spin_lock_init(&h->h_lock);
91 bucket = &handle_hash[h->h_cookie & HANDLE_HASH_MASK];
92 spin_lock(&bucket->lock);
93 list_add_rcu(&h->h_link, &bucket->head);
95 spin_unlock(&bucket->lock);
97 CDEBUG(D_INFO, "added object %p with handle %#llx to hash\n",
101 EXPORT_SYMBOL(class_handle_hash);
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 (%#llx)\n",
111 CDEBUG(D_INFO, "removing object %p with handle %#llx from hash\n",
114 spin_lock(&h->h_lock);
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 EXPORT_SYMBOL(class_handle_unhash);
135 void class_handle_hash_back(struct portals_handle *h)
137 struct handle_bucket *bucket;
140 bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
142 spin_lock(&bucket->lock);
143 list_add_rcu(&h->h_link, &bucket->head);
145 spin_unlock(&bucket->lock);
149 EXPORT_SYMBOL(class_handle_hash_back);
151 void *class_handle2object(__u64 cookie, const void *owner)
153 struct handle_bucket *bucket;
154 struct portals_handle *h;
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_entry_rcu(h, &bucket->head, h_link) {
166 if (h->h_cookie != cookie || h->h_owner != owner)
169 spin_lock(&h->h_lock);
170 if (likely(h->h_in != 0)) {
171 h->h_ops->hop_addref(h);
174 spin_unlock(&h->h_lock);
181 EXPORT_SYMBOL(class_handle2object);
183 void class_handle_free_cb(struct rcu_head *rcu)
185 struct portals_handle *h;
188 h = container_of(rcu, struct portals_handle, h_rcu);
189 ptr = (void *)(unsigned long)h->h_cookie;
191 if (h->h_ops->hop_free != NULL)
192 h->h_ops->hop_free(ptr, h->h_size);
194 OBD_FREE(ptr, h->h_size);
196 EXPORT_SYMBOL(class_handle_free_cb);
198 int class_handle_init(void)
200 struct handle_bucket *bucket;
201 struct timespec64 ts;
204 LASSERT(handle_hash == NULL);
206 OBD_ALLOC_LARGE(handle_hash, sizeof(*bucket) * HANDLE_HASH_SIZE);
207 if (handle_hash == NULL)
210 for (bucket = handle_hash + HANDLE_HASH_SIZE - 1; bucket >= handle_hash;
212 INIT_LIST_HEAD(&bucket->head);
213 spin_lock_init(&bucket->lock);
216 /** bug 21430: add randomness to the initial base */
217 cfs_get_random_bytes(seed, sizeof(seed));
219 cfs_srand(ts.tv_sec ^ seed[0], ts.tv_nsec ^ seed[1]);
221 cfs_get_random_bytes(&handle_base, sizeof(handle_base));
222 LASSERT(handle_base != 0ULL);
227 static int cleanup_all_handles(void)
232 for (rc = i = 0; i < HANDLE_HASH_SIZE; i++) {
233 struct portals_handle *h;
235 spin_lock(&handle_hash[i].lock);
236 list_for_each_entry_rcu(h, &(handle_hash[i].head), h_link) {
237 CERROR("force clean handle %#llx addr %p ops %p\n",
238 h->h_cookie, h, h->h_ops);
240 class_handle_unhash_nolock(h);
243 spin_unlock(&handle_hash[i].lock);
249 void class_handle_cleanup(void)
252 LASSERT(handle_hash != NULL);
254 count = cleanup_all_handles();
256 OBD_FREE_LARGE(handle_hash, sizeof(*handle_hash) * HANDLE_HASH_SIZE);
260 CERROR("handle_count at cleanup: %d\n", count);