Whamcloud - gitweb
LU-1347 build: remove the vim/emacs modelines
[fs/lustre-release.git] / 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, Whamcloud, Inc.
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 #ifndef __KERNEL__
43 # include <liblustre.h>
44 #endif
45
46 #include <obd_support.h>
47 #include <lustre_handles.h>
48 #include <lustre_lib.h>
49
50 #if !defined(HAVE_RCU) || !defined(__KERNEL__)
51 # define list_add_rcu            cfs_list_add
52 # define list_del_rcu            cfs_list_del
53 # define list_for_each_rcu       cfs_list_for_each
54 # define list_for_each_safe_rcu  cfs_list_for_each_safe
55 # define list_for_each_entry_rcu cfs_list_for_each_entry
56 # define rcu_read_lock()         cfs_spin_lock(&bucket->lock)
57 # define rcu_read_unlock()       cfs_spin_unlock(&bucket->lock)
58 #endif /* ifndef HAVE_RCU */
59
60 static __u64 handle_base;
61 #define HANDLE_INCR 7
62 static cfs_spinlock_t handle_base_lock;
63
64 static struct handle_bucket {
65         cfs_spinlock_t  lock;
66         cfs_list_t      head;
67 } *handle_hash;
68
69 #ifdef __arch_um__
70 /* For unknown reason, UML uses kmalloc rather than vmalloc to allocate
71  * memory(OBD_VMALLOC). Therefore, we have to redefine the
72  * HANDLE_HASH_SIZE to make the hash heads don't exceed 128K.
73  */
74 #define HANDLE_HASH_SIZE 4096
75 #else
76 #define HANDLE_HASH_SIZE (1 << 16)
77 #endif /* ifdef __arch_um__ */
78
79 #define HANDLE_HASH_MASK (HANDLE_HASH_SIZE - 1)
80
81 /*
82  * Generate a unique 64bit cookie (hash) for a handle and insert it into
83  * global (per-node) hash-table.
84  */
85 void class_handle_hash(struct portals_handle *h, portals_handle_addref_cb cb)
86 {
87         struct handle_bucket *bucket;
88         ENTRY;
89
90         LASSERT(h != NULL);
91         LASSERT(cfs_list_empty(&h->h_link));
92
93         /*
94          * This is fast, but simplistic cookie generation algorithm, it will
95          * need a re-do at some point in the future for security.
96          */
97         cfs_spin_lock(&handle_base_lock);
98         handle_base += HANDLE_INCR;
99
100         h->h_cookie = handle_base;
101         if (unlikely(handle_base == 0)) {
102                 /*
103                  * Cookie of zero is "dangerous", because in many places it's
104                  * assumed that 0 means "unassigned" handle, not bound to any
105                  * object.
106                  */
107                 CWARN("The universe has been exhausted: cookie wrap-around.\n");
108                 handle_base += HANDLE_INCR;
109         }
110         cfs_spin_unlock(&handle_base_lock);
111  
112         h->h_addref = cb;
113         cfs_spin_lock_init(&h->h_lock);
114
115         bucket = &handle_hash[h->h_cookie & HANDLE_HASH_MASK];
116         cfs_spin_lock(&bucket->lock);
117         list_add_rcu(&h->h_link, &bucket->head);
118         h->h_in = 1;
119         cfs_spin_unlock(&bucket->lock);
120
121         CDEBUG(D_INFO, "added object %p with handle "LPX64" to hash\n",
122                h, h->h_cookie);
123         EXIT;
124 }
125
126 static void class_handle_unhash_nolock(struct portals_handle *h)
127 {
128         if (cfs_list_empty(&h->h_link)) {
129                 CERROR("removing an already-removed handle ("LPX64")\n",
130                        h->h_cookie);
131                 return;
132         }
133
134         CDEBUG(D_INFO, "removing object %p with handle "LPX64" from hash\n",
135                h, h->h_cookie);
136
137         cfs_spin_lock(&h->h_lock);
138         if (h->h_in == 0) {
139                 cfs_spin_unlock(&h->h_lock);
140                 return;
141         }
142         h->h_in = 0;
143         cfs_spin_unlock(&h->h_lock);
144         list_del_rcu(&h->h_link);
145 }
146
147 void class_handle_unhash(struct portals_handle *h)
148 {
149         struct handle_bucket *bucket;
150         bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
151
152         cfs_spin_lock(&bucket->lock);
153         class_handle_unhash_nolock(h);
154         cfs_spin_unlock(&bucket->lock);
155 }
156
157 void class_handle_hash_back(struct portals_handle *h)
158 {
159         struct handle_bucket *bucket;
160         ENTRY;
161
162         bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
163
164         cfs_spin_lock(&bucket->lock);
165         list_add_rcu(&h->h_link, &bucket->head);
166         h->h_in = 1;
167         cfs_spin_unlock(&bucket->lock);
168
169         EXIT;
170 }
171
172 void *class_handle2object(__u64 cookie)
173 {
174         struct handle_bucket *bucket;
175         struct portals_handle *h;
176         void *retval = NULL;
177         ENTRY;
178
179         LASSERT(handle_hash != NULL);
180
181         /* Be careful when you want to change this code. See the 
182          * rcu_read_lock() definition on top this file. - jxiong */
183         bucket = handle_hash + (cookie & HANDLE_HASH_MASK);
184
185         rcu_read_lock();
186         list_for_each_entry_rcu(h, &bucket->head, h_link) {
187                 if (h->h_cookie != cookie)
188                         continue;
189
190                 cfs_spin_lock(&h->h_lock);
191                 if (likely(h->h_in != 0)) {
192                         h->h_addref(h);
193                         retval = h;
194                 }
195                 cfs_spin_unlock(&h->h_lock);
196                 break;
197         }
198         rcu_read_unlock();
199
200         RETURN(retval);
201 }
202
203 void class_handle_free_cb(cfs_rcu_head_t *rcu)
204 {
205         struct portals_handle *h = RCU2HANDLE(rcu);
206         if (h->h_free_cb) {
207                 h->h_free_cb(h->h_ptr, h->h_size);
208         } else {
209                 void *ptr = h->h_ptr;
210                 unsigned int size = h->h_size;
211                 OBD_FREE(ptr, size);
212         }
213 }
214
215 int class_handle_init(void)
216 {
217         struct handle_bucket *bucket;
218         struct timeval tv;
219         int seed[2];
220
221         LASSERT(handle_hash == NULL);
222
223         OBD_ALLOC_LARGE(handle_hash, sizeof(*bucket) * HANDLE_HASH_SIZE);
224         if (handle_hash == NULL)
225                 return -ENOMEM;
226
227         cfs_spin_lock_init(&handle_base_lock);
228         for (bucket = handle_hash + HANDLE_HASH_SIZE - 1; bucket >= handle_hash;
229              bucket--) {
230                 CFS_INIT_LIST_HEAD(&bucket->head);
231                 cfs_spin_lock_init(&bucket->lock);
232         }
233
234         /** bug 21430: add randomness to the initial base */
235         cfs_get_random_bytes(seed, sizeof(seed));
236         cfs_gettimeofday(&tv);
237         cfs_srand(tv.tv_sec ^ seed[0], tv.tv_usec ^ seed[1]);
238
239         cfs_get_random_bytes(&handle_base, sizeof(handle_base));
240         LASSERT(handle_base != 0ULL);
241
242         return 0;
243 }
244
245 static int cleanup_all_handles(void)
246 {
247         int rc;
248         int i;
249
250         for (rc = i = 0; i < HANDLE_HASH_SIZE; i++) {
251                 struct portals_handle *h;
252
253                 cfs_spin_lock(&handle_hash[i].lock);
254                 list_for_each_entry_rcu(h, &(handle_hash[i].head), h_link) {
255                         CERROR("force clean handle "LPX64" addr %p addref %p\n",
256                                h->h_cookie, h, h->h_addref);
257
258                         class_handle_unhash_nolock(h);
259                         rc++;
260                 }
261                 cfs_spin_unlock(&handle_hash[i].lock);
262         }
263
264         return rc;
265 }
266
267 void class_handle_cleanup(void)
268 {
269         int count;
270         LASSERT(handle_hash != NULL);
271
272         count = cleanup_all_handles();
273
274         OBD_FREE_LARGE(handle_hash, sizeof(*handle_hash) * HANDLE_HASH_SIZE);
275         handle_hash = NULL;
276
277         if (count != 0)
278                 CERROR("handle_count at cleanup: %d\n", count);
279 }