Whamcloud - gitweb
LU-181 obdclass: fix portal_handle memory wastage
[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,
86                        struct portals_handle_ops *ops)
87 {
88         struct handle_bucket *bucket;
89         ENTRY;
90
91         LASSERT(h != NULL);
92         LASSERT(cfs_list_empty(&h->h_link));
93
94         /*
95          * This is fast, but simplistic cookie generation algorithm, it will
96          * need a re-do at some point in the future for security.
97          */
98         cfs_spin_lock(&handle_base_lock);
99         handle_base += HANDLE_INCR;
100
101         h->h_cookie = handle_base;
102         if (unlikely(handle_base == 0)) {
103                 /*
104                  * Cookie of zero is "dangerous", because in many places it's
105                  * assumed that 0 means "unassigned" handle, not bound to any
106                  * object.
107                  */
108                 CWARN("The universe has been exhausted: cookie wrap-around.\n");
109                 handle_base += HANDLE_INCR;
110         }
111         cfs_spin_unlock(&handle_base_lock);
112
113         h->h_ops = ops;
114         cfs_spin_lock_init(&h->h_lock);
115
116         bucket = &handle_hash[h->h_cookie & HANDLE_HASH_MASK];
117         cfs_spin_lock(&bucket->lock);
118         list_add_rcu(&h->h_link, &bucket->head);
119         h->h_in = 1;
120         cfs_spin_unlock(&bucket->lock);
121
122         CDEBUG(D_INFO, "added object %p with handle "LPX64" to hash\n",
123                h, h->h_cookie);
124         EXIT;
125 }
126
127 static void class_handle_unhash_nolock(struct portals_handle *h)
128 {
129         if (cfs_list_empty(&h->h_link)) {
130                 CERROR("removing an already-removed handle ("LPX64")\n",
131                        h->h_cookie);
132                 return;
133         }
134
135         CDEBUG(D_INFO, "removing object %p with handle "LPX64" from hash\n",
136                h, h->h_cookie);
137
138         cfs_spin_lock(&h->h_lock);
139         if (h->h_in == 0) {
140                 cfs_spin_unlock(&h->h_lock);
141                 return;
142         }
143         h->h_in = 0;
144         cfs_spin_unlock(&h->h_lock);
145         list_del_rcu(&h->h_link);
146 }
147
148 void class_handle_unhash(struct portals_handle *h)
149 {
150         struct handle_bucket *bucket;
151         bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
152
153         cfs_spin_lock(&bucket->lock);
154         class_handle_unhash_nolock(h);
155         cfs_spin_unlock(&bucket->lock);
156 }
157
158 void class_handle_hash_back(struct portals_handle *h)
159 {
160         struct handle_bucket *bucket;
161         ENTRY;
162
163         bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
164
165         cfs_spin_lock(&bucket->lock);
166         list_add_rcu(&h->h_link, &bucket->head);
167         h->h_in = 1;
168         cfs_spin_unlock(&bucket->lock);
169
170         EXIT;
171 }
172
173 void *class_handle2object(__u64 cookie)
174 {
175         struct handle_bucket *bucket;
176         struct portals_handle *h;
177         void *retval = NULL;
178         ENTRY;
179
180         LASSERT(handle_hash != NULL);
181
182         /* Be careful when you want to change this code. See the 
183          * rcu_read_lock() definition on top this file. - jxiong */
184         bucket = handle_hash + (cookie & HANDLE_HASH_MASK);
185
186         rcu_read_lock();
187         list_for_each_entry_rcu(h, &bucket->head, h_link) {
188                 if (h->h_cookie != cookie)
189                         continue;
190
191                 cfs_spin_lock(&h->h_lock);
192                 if (likely(h->h_in != 0)) {
193                         h->h_ops->hop_addref(h);
194                         retval = h;
195                 }
196                 cfs_spin_unlock(&h->h_lock);
197                 break;
198         }
199         rcu_read_unlock();
200
201         RETURN(retval);
202 }
203
204 void class_handle_free_cb(cfs_rcu_head_t *rcu)
205 {
206         struct portals_handle *h = RCU2HANDLE(rcu);
207         void *ptr = (void *)(unsigned long)h->h_cookie;
208
209         if (h->h_ops->hop_free != NULL)
210                 h->h_ops->hop_free(ptr, h->h_size);
211         else
212                 OBD_FREE(ptr, h->h_size);
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 ops %p\n",
256                                h->h_cookie, h, h->h_ops);
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 }