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/
31 * lustre/obdclass/lustre_handles.c
33 * Author: Phil Schwan <phil@clusterfs.com>
36 #define DEBUG_SUBSYSTEM S_CLASS
38 #include <linux/random.h>
40 #include <obd_support.h>
41 #include <lustre_handles.h>
42 #include <lustre_lib.h>
45 static __u64 handle_base;
47 static DEFINE_SPINLOCK(handle_base_lock);
49 static struct handle_bucket {
51 struct hlist_head head;
54 #define HANDLE_HASH_SIZE (1 << 16)
55 #define HANDLE_HASH_MASK (HANDLE_HASH_SIZE - 1)
58 * Generate a unique 64bit cookie (hash) for a handle and insert it into
59 * global (per-node) hash-table.
61 void class_handle_hash(struct portals_handle *h, const char *owner)
63 struct handle_bucket *bucket;
68 LASSERT(hlist_unhashed(&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 if (unlikely(handle_base == 0)) {
79 * Cookie of zero is "dangerous", because in many places it's
80 * assumed that 0 means "unassigned" handle, not bound to any
83 CWARN("The universe has been exhausted: cookie wrap-around.\n");
84 handle_base += HANDLE_INCR;
86 h->h_cookie = handle_base;
87 spin_unlock(&handle_base_lock);
91 bucket = &handle_hash[h->h_cookie & HANDLE_HASH_MASK];
92 spin_lock(&bucket->lock);
93 hlist_add_head_rcu(&h->h_link, &bucket->head);
94 spin_unlock(&bucket->lock);
96 CDEBUG(D_INFO, "added object %p with handle %#llx to hash\n",
100 EXPORT_SYMBOL(class_handle_hash);
102 static void class_handle_unhash_nolock(struct portals_handle *h)
104 if (hlist_unhashed(&h->h_link)) {
105 CERROR("removing an already-removed handle (%#llx)\n",
110 CDEBUG(D_INFO, "removing object %p with handle %#llx from hash\n",
113 hlist_del_init_rcu(&h->h_link);
116 void class_handle_unhash(struct portals_handle *h)
118 struct handle_bucket *bucket;
119 bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
121 spin_lock(&bucket->lock);
122 class_handle_unhash_nolock(h);
123 spin_unlock(&bucket->lock);
125 EXPORT_SYMBOL(class_handle_unhash);
127 void *class_handle2object(u64 cookie, const char *owner)
129 struct handle_bucket *bucket;
130 struct portals_handle *h;
135 LASSERT(handle_hash != NULL);
138 * Be careful when you want to change this code. See the
139 * rcu_read_lock() definition on top this file. - jxiong
141 bucket = handle_hash + (cookie & HANDLE_HASH_MASK);
144 hlist_for_each_entry_rcu(h, &bucket->head, h_link) {
145 if (h->h_cookie != cookie || h->h_owner != owner)
148 if (refcount_inc_not_zero(&h->h_ref)) {
149 CDEBUG(D_INFO, "GET %s %p refcount=%d\n",
151 refcount_read(&h->h_ref));
160 EXPORT_SYMBOL(class_handle2object);
162 int class_handle_init(void)
164 struct handle_bucket *bucket;
166 LASSERT(handle_hash == NULL);
168 OBD_ALLOC_PTR_ARRAY_LARGE(handle_hash, HANDLE_HASH_SIZE);
169 if (handle_hash == NULL)
172 for (bucket = handle_hash + HANDLE_HASH_SIZE - 1; bucket >= handle_hash;
174 INIT_HLIST_HEAD(&bucket->head);
175 spin_lock_init(&bucket->lock);
178 get_random_bytes(&handle_base, sizeof(handle_base));
179 LASSERT(handle_base != 0ULL);
184 static int cleanup_all_handles(void)
189 for (rc = i = 0; i < HANDLE_HASH_SIZE; i++) {
190 struct portals_handle *h;
192 spin_lock(&handle_hash[i].lock);
193 hlist_for_each_entry_rcu(h, &handle_hash[i].head, h_link) {
194 CERROR("force clean handle %#llx addr %p owner %p\n",
195 h->h_cookie, h, h->h_owner);
197 class_handle_unhash_nolock(h);
200 spin_unlock(&handle_hash[i].lock);
206 void class_handle_cleanup(void)
210 LASSERT(handle_hash != NULL);
212 count = cleanup_all_handles();
214 OBD_FREE_PTR_ARRAY_LARGE(handle_hash, HANDLE_HASH_SIZE);
218 CERROR("handle_count at cleanup: %d\n", count);