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, 2012, 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 "../include/obd_support.h"
40 #include "../include/lustre_handles.h"
41 #include "../include/lustre_lib.h"
43 static __u64 handle_base;
45 static spinlock_t handle_base_lock;
47 static struct handle_bucket {
49 struct list_head head;
52 #define HANDLE_HASH_SIZE (1 << 16)
53 #define HANDLE_HASH_MASK (HANDLE_HASH_SIZE - 1)
56 * Generate a unique 64bit cookie (hash) for a handle and insert it into
57 * global (per-node) hash-table.
59 void class_handle_hash(struct portals_handle *h,
60 struct portals_handle_ops *ops)
62 struct handle_bucket *bucket;
65 LASSERT(list_empty(&h->h_link));
68 * This is fast, but simplistic cookie generation algorithm, it will
69 * need a re-do at some point in the future for security.
71 spin_lock(&handle_base_lock);
72 handle_base += HANDLE_INCR;
74 if (unlikely(handle_base == 0)) {
76 * Cookie of zero is "dangerous", because in many places it's
77 * assumed that 0 means "unassigned" handle, not bound to any
80 CWARN("The universe has been exhausted: cookie wrap-around.\n");
81 handle_base += HANDLE_INCR;
83 h->h_cookie = handle_base;
84 spin_unlock(&handle_base_lock);
87 spin_lock_init(&h->h_lock);
89 bucket = &handle_hash[h->h_cookie & HANDLE_HASH_MASK];
90 spin_lock(&bucket->lock);
91 list_add_rcu(&h->h_link, &bucket->head);
93 spin_unlock(&bucket->lock);
95 CDEBUG(D_INFO, "added object %p with handle %#llx to hash\n",
98 EXPORT_SYMBOL(class_handle_hash);
100 static void class_handle_unhash_nolock(struct portals_handle *h)
102 if (list_empty(&h->h_link)) {
103 CERROR("removing an already-removed handle (%#llx)\n",
108 CDEBUG(D_INFO, "removing object %p with handle %#llx from hash\n",
111 spin_lock(&h->h_lock);
113 spin_unlock(&h->h_lock);
117 spin_unlock(&h->h_lock);
118 list_del_rcu(&h->h_link);
121 void class_handle_unhash(struct portals_handle *h)
123 struct handle_bucket *bucket;
125 bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
127 spin_lock(&bucket->lock);
128 class_handle_unhash_nolock(h);
129 spin_unlock(&bucket->lock);
131 EXPORT_SYMBOL(class_handle_unhash);
133 void *class_handle2object(__u64 cookie, const void *owner)
135 struct handle_bucket *bucket;
136 struct portals_handle *h;
139 LASSERT(handle_hash);
141 /* Be careful when you want to change this code. See the
142 * rcu_read_lock() definition on top this file. - jxiong
144 bucket = handle_hash + (cookie & HANDLE_HASH_MASK);
147 list_for_each_entry_rcu(h, &bucket->head, h_link) {
148 if (h->h_cookie != cookie || h->h_owner != owner)
151 spin_lock(&h->h_lock);
152 if (likely(h->h_in != 0)) {
153 h->h_ops->hop_addref(h);
156 spin_unlock(&h->h_lock);
163 EXPORT_SYMBOL(class_handle2object);
165 void class_handle_free_cb(struct rcu_head *rcu)
167 struct portals_handle *h;
170 h = container_of(rcu, struct portals_handle, h_rcu);
171 ptr = (void *)(unsigned long)h->h_cookie;
173 if (h->h_ops->hop_free)
174 h->h_ops->hop_free(ptr, h->h_size);
178 EXPORT_SYMBOL(class_handle_free_cb);
180 int class_handle_init(void)
182 struct handle_bucket *bucket;
183 struct timespec64 ts;
186 LASSERT(!handle_hash);
188 handle_hash = libcfs_kvzalloc(sizeof(*bucket) * HANDLE_HASH_SIZE,
193 spin_lock_init(&handle_base_lock);
194 for (bucket = handle_hash + HANDLE_HASH_SIZE - 1; bucket >= handle_hash;
196 INIT_LIST_HEAD(&bucket->head);
197 spin_lock_init(&bucket->lock);
200 /** bug 21430: add randomness to the initial base */
201 cfs_get_random_bytes(seed, sizeof(seed));
203 cfs_srand(ts.tv_sec ^ seed[0], ts.tv_nsec ^ seed[1]);
205 cfs_get_random_bytes(&handle_base, sizeof(handle_base));
206 LASSERT(handle_base != 0ULL);
211 static int cleanup_all_handles(void)
216 for (rc = i = 0; i < HANDLE_HASH_SIZE; i++) {
217 struct portals_handle *h;
219 spin_lock(&handle_hash[i].lock);
220 list_for_each_entry_rcu(h, &handle_hash[i].head, h_link) {
221 CERROR("force clean handle %#llx addr %p ops %p\n",
222 h->h_cookie, h, h->h_ops);
224 class_handle_unhash_nolock(h);
227 spin_unlock(&handle_hash[i].lock);
233 void class_handle_cleanup(void)
237 LASSERT(handle_hash);
239 count = cleanup_all_handles();
245 CERROR("handle_count at cleanup: %d\n", count);