Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/hid
[deliverable/linux.git] / drivers / staging / lustre / lustre / obdclass / lustre_handles.c
1 /*
2 * GPL HEADER START
3 *
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
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.
9 *
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).
15 *
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
19 *
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
22 * have any questions.
23 *
24 * GPL HEADER END
25 */
26 /*
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
29 *
30 * Copyright (c) 2011, 2012, Intel Corporation.
31 */
32 /*
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
35 *
36 * lustre/obdclass/lustre_handles.c
37 *
38 * Author: Phil Schwan <phil@clusterfs.com>
39 */
40
41 #define DEBUG_SUBSYSTEM S_CLASS
42
43 #include <obd_support.h>
44 #include <lustre_handles.h>
45 #include <lustre_lib.h>
46
47
48 static __u64 handle_base;
49 #define HANDLE_INCR 7
50 static spinlock_t handle_base_lock;
51
52 static struct handle_bucket {
53 spinlock_t lock;
54 struct list_head head;
55 } *handle_hash;
56
57 #define HANDLE_HASH_SIZE (1 << 16)
58 #define HANDLE_HASH_MASK (HANDLE_HASH_SIZE - 1)
59
60 /*
61 * Generate a unique 64bit cookie (hash) for a handle and insert it into
62 * global (per-node) hash-table.
63 */
64 void class_handle_hash(struct portals_handle *h,
65 struct portals_handle_ops *ops)
66 {
67 struct handle_bucket *bucket;
68 ENTRY;
69
70 LASSERT(h != NULL);
71 LASSERT(list_empty(&h->h_link));
72
73 /*
74 * This is fast, but simplistic cookie generation algorithm, it will
75 * need a re-do at some point in the future for security.
76 */
77 spin_lock(&handle_base_lock);
78 handle_base += HANDLE_INCR;
79
80 if (unlikely(handle_base == 0)) {
81 /*
82 * Cookie of zero is "dangerous", because in many places it's
83 * assumed that 0 means "unassigned" handle, not bound to any
84 * object.
85 */
86 CWARN("The universe has been exhausted: cookie wrap-around.\n");
87 handle_base += HANDLE_INCR;
88 }
89 h->h_cookie = handle_base;
90 spin_unlock(&handle_base_lock);
91
92 h->h_ops = ops;
93 spin_lock_init(&h->h_lock);
94
95 bucket = &handle_hash[h->h_cookie & HANDLE_HASH_MASK];
96 spin_lock(&bucket->lock);
97 list_add_rcu(&h->h_link, &bucket->head);
98 h->h_in = 1;
99 spin_unlock(&bucket->lock);
100
101 CDEBUG(D_INFO, "added object %p with handle "LPX64" to hash\n",
102 h, h->h_cookie);
103 EXIT;
104 }
105 EXPORT_SYMBOL(class_handle_hash);
106
107 static void class_handle_unhash_nolock(struct portals_handle *h)
108 {
109 if (list_empty(&h->h_link)) {
110 CERROR("removing an already-removed handle ("LPX64")\n",
111 h->h_cookie);
112 return;
113 }
114
115 CDEBUG(D_INFO, "removing object %p with handle "LPX64" from hash\n",
116 h, h->h_cookie);
117
118 spin_lock(&h->h_lock);
119 if (h->h_in == 0) {
120 spin_unlock(&h->h_lock);
121 return;
122 }
123 h->h_in = 0;
124 spin_unlock(&h->h_lock);
125 list_del_rcu(&h->h_link);
126 }
127
128 void class_handle_unhash(struct portals_handle *h)
129 {
130 struct handle_bucket *bucket;
131 bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
132
133 spin_lock(&bucket->lock);
134 class_handle_unhash_nolock(h);
135 spin_unlock(&bucket->lock);
136 }
137 EXPORT_SYMBOL(class_handle_unhash);
138
139 void class_handle_hash_back(struct portals_handle *h)
140 {
141 struct handle_bucket *bucket;
142 ENTRY;
143
144 bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
145
146 spin_lock(&bucket->lock);
147 list_add_rcu(&h->h_link, &bucket->head);
148 h->h_in = 1;
149 spin_unlock(&bucket->lock);
150
151 EXIT;
152 }
153 EXPORT_SYMBOL(class_handle_hash_back);
154
155 void *class_handle2object(__u64 cookie)
156 {
157 struct handle_bucket *bucket;
158 struct portals_handle *h;
159 void *retval = NULL;
160 ENTRY;
161
162 LASSERT(handle_hash != NULL);
163
164 /* Be careful when you want to change this code. See the
165 * rcu_read_lock() definition on top this file. - jxiong */
166 bucket = handle_hash + (cookie & HANDLE_HASH_MASK);
167
168 rcu_read_lock();
169 list_for_each_entry_rcu(h, &bucket->head, h_link) {
170 if (h->h_cookie != cookie)
171 continue;
172
173 spin_lock(&h->h_lock);
174 if (likely(h->h_in != 0)) {
175 h->h_ops->hop_addref(h);
176 retval = h;
177 }
178 spin_unlock(&h->h_lock);
179 break;
180 }
181 rcu_read_unlock();
182
183 RETURN(retval);
184 }
185 EXPORT_SYMBOL(class_handle2object);
186
187 void class_handle_free_cb(cfs_rcu_head_t *rcu)
188 {
189 struct portals_handle *h = RCU2HANDLE(rcu);
190 void *ptr = (void *)(unsigned long)h->h_cookie;
191
192 if (h->h_ops->hop_free != NULL)
193 h->h_ops->hop_free(ptr, h->h_size);
194 else
195 OBD_FREE(ptr, h->h_size);
196 }
197 EXPORT_SYMBOL(class_handle_free_cb);
198
199 int class_handle_init(void)
200 {
201 struct handle_bucket *bucket;
202 struct timeval tv;
203 int seed[2];
204
205 LASSERT(handle_hash == NULL);
206
207 OBD_ALLOC_LARGE(handle_hash, sizeof(*bucket) * HANDLE_HASH_SIZE);
208 if (handle_hash == NULL)
209 return -ENOMEM;
210
211 spin_lock_init(&handle_base_lock);
212 for (bucket = handle_hash + HANDLE_HASH_SIZE - 1; bucket >= handle_hash;
213 bucket--) {
214 INIT_LIST_HEAD(&bucket->head);
215 spin_lock_init(&bucket->lock);
216 }
217
218 /** bug 21430: add randomness to the initial base */
219 cfs_get_random_bytes(seed, sizeof(seed));
220 do_gettimeofday(&tv);
221 cfs_srand(tv.tv_sec ^ seed[0], tv.tv_usec ^ seed[1]);
222
223 cfs_get_random_bytes(&handle_base, sizeof(handle_base));
224 LASSERT(handle_base != 0ULL);
225
226 return 0;
227 }
228
229 static int cleanup_all_handles(void)
230 {
231 int rc;
232 int i;
233
234 for (rc = i = 0; i < HANDLE_HASH_SIZE; i++) {
235 struct portals_handle *h;
236
237 spin_lock(&handle_hash[i].lock);
238 list_for_each_entry_rcu(h, &(handle_hash[i].head), h_link) {
239 CERROR("force clean handle "LPX64" addr %p ops %p\n",
240 h->h_cookie, h, h->h_ops);
241
242 class_handle_unhash_nolock(h);
243 rc++;
244 }
245 spin_unlock(&handle_hash[i].lock);
246 }
247
248 return rc;
249 }
250
251 void class_handle_cleanup(void)
252 {
253 int count;
254 LASSERT(handle_hash != NULL);
255
256 count = cleanup_all_handles();
257
258 OBD_FREE_LARGE(handle_hash, sizeof(*handle_hash) * HANDLE_HASH_SIZE);
259 handle_hash = NULL;
260
261 if (count != 0)
262 CERROR("handle_count at cleanup: %d\n", count);
263 }
This page took 0.043089 seconds and 6 git commands to generate.