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, 2017, 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 <linux/random.h>
41 #include <obd_support.h>
42 #include <lustre_handles.h>
43 #include <lustre_lib.h>
46 static __u64 handle_base;
48 static DEFINE_SPINLOCK(handle_base_lock);
50 static struct handle_bucket {
52 struct list_head head;
55 #define HANDLE_HASH_SIZE (1 << 16)
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,
63 const struct portals_handle_ops *ops)
65 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",
104 EXPORT_SYMBOL(class_handle_hash);
106 static void class_handle_unhash_nolock(struct portals_handle *h)
108 if (list_empty(&h->h_link)) {
109 CERROR("removing an already-removed handle (%#llx)\n",
114 CDEBUG(D_INFO, "removing object %p with handle %#llx from hash\n",
117 spin_lock(&h->h_lock);
119 spin_unlock(&h->h_lock);
123 spin_unlock(&h->h_lock);
124 list_del_rcu(&h->h_link);
127 void class_handle_unhash(struct portals_handle *h)
129 struct handle_bucket *bucket;
130 bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
132 spin_lock(&bucket->lock);
133 class_handle_unhash_nolock(h);
134 spin_unlock(&bucket->lock);
136 EXPORT_SYMBOL(class_handle_unhash);
138 void *class_handle2object(u64 cookie, const struct portals_handle_ops *ops)
140 struct handle_bucket *bucket;
141 struct portals_handle *h;
146 LASSERT(handle_hash != NULL);
149 * Be careful when you want to change this code. See the
150 * rcu_read_lock() definition on top this file. - jxiong
152 bucket = handle_hash + (cookie & HANDLE_HASH_MASK);
155 list_for_each_entry_rcu(h, &bucket->head, h_link) {
156 if (h->h_cookie != cookie || h->h_ops != ops)
159 spin_lock(&h->h_lock);
160 if (likely(h->h_in != 0)) {
161 h->h_ops->hop_addref(h);
164 spin_unlock(&h->h_lock);
171 EXPORT_SYMBOL(class_handle2object);
173 void class_handle_free_cb(struct rcu_head *rcu)
175 struct portals_handle *h;
178 h = container_of(rcu, struct portals_handle, h_rcu);
179 ptr = (void *)(unsigned long)h->h_cookie;
181 if (h->h_ops->hop_free != NULL)
182 h->h_ops->hop_free(ptr, h->h_size);
184 OBD_FREE(ptr, h->h_size);
186 EXPORT_SYMBOL(class_handle_free_cb);
188 int class_handle_init(void)
190 struct handle_bucket *bucket;
192 LASSERT(handle_hash == NULL);
194 OBD_ALLOC_LARGE(handle_hash, sizeof(*bucket) * HANDLE_HASH_SIZE);
195 if (handle_hash == NULL)
198 for (bucket = handle_hash + HANDLE_HASH_SIZE - 1; bucket >= handle_hash;
200 INIT_LIST_HEAD(&bucket->head);
201 spin_lock_init(&bucket->lock);
204 get_random_bytes(&handle_base, sizeof(handle_base));
205 LASSERT(handle_base != 0ULL);
210 static int cleanup_all_handles(void)
215 for (rc = i = 0; i < HANDLE_HASH_SIZE; i++) {
216 struct portals_handle *h;
218 spin_lock(&handle_hash[i].lock);
219 list_for_each_entry_rcu(h, &(handle_hash[i].head), h_link) {
220 CERROR("force clean handle %#llx addr %p ops %p\n",
221 h->h_cookie, h, h->h_ops);
223 class_handle_unhash_nolock(h);
226 spin_unlock(&handle_hash[i].lock);
232 void class_handle_cleanup(void)
236 LASSERT(handle_hash != NULL);
238 count = cleanup_all_handles();
240 OBD_FREE_LARGE(handle_hash, sizeof(*handle_hash) * HANDLE_HASH_SIZE);
244 CERROR("handle_count at cleanup: %d\n", count);