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"
47 static __u64 handle_base;
49 static spinlock_t handle_base_lock;
51 static struct handle_bucket {
53 struct list_head head;
56 #define HANDLE_HASH_SIZE (1 << 16)
57 #define HANDLE_HASH_MASK (HANDLE_HASH_SIZE - 1)
60 * Generate a unique 64bit cookie (hash) for a handle and insert it into
61 * global (per-node) hash-table.
63 void class_handle_hash(struct portals_handle *h,
64 struct portals_handle_ops *ops)
66 struct handle_bucket *bucket;
69 LASSERT(list_empty(&h->h_link));
72 * This is fast, but simplistic cookie generation algorithm, it will
73 * need a re-do at some point in the future for security.
75 spin_lock(&handle_base_lock);
76 handle_base += HANDLE_INCR;
78 if (unlikely(handle_base == 0)) {
80 * Cookie of zero is "dangerous", because in many places it's
81 * assumed that 0 means "unassigned" handle, not bound to any
84 CWARN("The universe has been exhausted: cookie wrap-around.\n");
85 handle_base += HANDLE_INCR;
87 h->h_cookie = handle_base;
88 spin_unlock(&handle_base_lock);
91 spin_lock_init(&h->h_lock);
93 bucket = &handle_hash[h->h_cookie & HANDLE_HASH_MASK];
94 spin_lock(&bucket->lock);
95 list_add_rcu(&h->h_link, &bucket->head);
97 spin_unlock(&bucket->lock);
99 CDEBUG(D_INFO, "added object %p with handle %#llx to hash\n",
102 EXPORT_SYMBOL(class_handle_hash);
104 static void class_handle_unhash_nolock(struct portals_handle *h)
106 if (list_empty(&h->h_link)) {
107 CERROR("removing an already-removed handle (%#llx)\n",
112 CDEBUG(D_INFO, "removing object %p with handle %#llx from hash\n",
115 spin_lock(&h->h_lock);
117 spin_unlock(&h->h_lock);
121 spin_unlock(&h->h_lock);
122 list_del_rcu(&h->h_link);
125 void class_handle_unhash(struct portals_handle *h)
127 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_handle2object(__u64 cookie)
139 struct handle_bucket *bucket;
140 struct portals_handle *h;
143 LASSERT(handle_hash);
145 /* Be careful when you want to change this code. See the
146 * rcu_read_lock() definition on top this file. - jxiong
148 bucket = handle_hash + (cookie & HANDLE_HASH_MASK);
151 list_for_each_entry_rcu(h, &bucket->head, h_link) {
152 if (h->h_cookie != cookie)
155 spin_lock(&h->h_lock);
156 if (likely(h->h_in != 0)) {
157 h->h_ops->hop_addref(h);
160 spin_unlock(&h->h_lock);
167 EXPORT_SYMBOL(class_handle2object);
169 void class_handle_free_cb(struct rcu_head *rcu)
171 struct portals_handle *h = RCU2HANDLE(rcu);
172 void *ptr = (void *)(unsigned long)h->h_cookie;
174 if (h->h_ops->hop_free)
175 h->h_ops->hop_free(ptr, h->h_size);
179 EXPORT_SYMBOL(class_handle_free_cb);
181 int class_handle_init(void)
183 struct handle_bucket *bucket;
184 struct timespec64 ts;
187 LASSERT(!handle_hash);
189 handle_hash = libcfs_kvzalloc(sizeof(*bucket) * HANDLE_HASH_SIZE,
194 spin_lock_init(&handle_base_lock);
195 for (bucket = handle_hash + HANDLE_HASH_SIZE - 1; bucket >= handle_hash;
197 INIT_LIST_HEAD(&bucket->head);
198 spin_lock_init(&bucket->lock);
201 /** bug 21430: add randomness to the initial base */
202 cfs_get_random_bytes(seed, sizeof(seed));
204 cfs_srand(ts.tv_sec ^ seed[0], ts.tv_nsec ^ seed[1]);
206 cfs_get_random_bytes(&handle_base, sizeof(handle_base));
207 LASSERT(handle_base != 0ULL);
212 static int cleanup_all_handles(void)
217 for (rc = i = 0; i < HANDLE_HASH_SIZE; i++) {
218 struct portals_handle *h;
220 spin_lock(&handle_hash[i].lock);
221 list_for_each_entry_rcu(h, &(handle_hash[i].head), h_link) {
222 CERROR("force clean handle %#llx addr %p ops %p\n",
223 h->h_cookie, h, h->h_ops);
225 class_handle_unhash_nolock(h);
228 spin_unlock(&handle_hash[i].lock);
234 void class_handle_cleanup(void)
238 LASSERT(handle_hash);
240 count = cleanup_all_handles();
246 CERROR("handle_count at cleanup: %d\n", count);