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