Whamcloud - gitweb
LU-12254 lnet: correct discovery LNetEQFree()
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/obdclass/lustre_handles.c
33  *
34  * Author: Phil Schwan <phil@clusterfs.com>
35  */
36
37 #define DEBUG_SUBSYSTEM S_CLASS
38
39 #include <obd_support.h>
40 #include <lustre_handles.h>
41 #include <lustre_lib.h>
42
43
44 static __u64 handle_base;
45 #define HANDLE_INCR 7
46 static DEFINE_SPINLOCK(handle_base_lock);
47
48 static struct handle_bucket {
49         spinlock_t lock;
50         struct list_head head;
51 } *handle_hash;
52
53 #define HANDLE_HASH_SIZE (1 << 16)
54 #define HANDLE_HASH_MASK (HANDLE_HASH_SIZE - 1)
55
56 /*
57  * Generate a unique 64bit cookie (hash) for a handle and insert it into
58  * global (per-node) hash-table.
59  */
60 void class_handle_hash(struct portals_handle *h,
61                        struct portals_handle_ops *ops)
62 {
63         struct handle_bucket *bucket;
64
65         ENTRY;
66
67         LASSERT(h != NULL);
68         LASSERT(list_empty(&h->h_link));
69
70         /*
71          * This is fast, but simplistic cookie generation algorithm, it will
72          * need a re-do at some point in the future for security.
73          */
74         spin_lock(&handle_base_lock);
75         handle_base += HANDLE_INCR;
76
77         if (unlikely(handle_base == 0)) {
78                 /*
79                  * Cookie of zero is "dangerous", because in many places it's
80                  * assumed that 0 means "unassigned" handle, not bound to any
81                  * object.
82                  */
83                 CWARN("The universe has been exhausted: cookie wrap-around.\n");
84                 handle_base += HANDLE_INCR;
85         }
86         h->h_cookie = handle_base;
87         spin_unlock(&handle_base_lock);
88
89         h->h_ops = ops;
90         spin_lock_init(&h->h_lock);
91
92         bucket = &handle_hash[h->h_cookie & HANDLE_HASH_MASK];
93         spin_lock(&bucket->lock);
94         list_add_rcu(&h->h_link, &bucket->head);
95         h->h_in = 1;
96         spin_unlock(&bucket->lock);
97
98         CDEBUG(D_INFO, "added object %p with handle %#llx to hash\n",
99                h, h->h_cookie);
100         EXIT;
101 }
102 EXPORT_SYMBOL(class_handle_hash);
103
104 static void class_handle_unhash_nolock(struct portals_handle *h)
105 {
106         if (list_empty(&h->h_link)) {
107                 CERROR("removing an already-removed handle (%#llx)\n",
108                        h->h_cookie);
109                 return;
110         }
111
112         CDEBUG(D_INFO, "removing object %p with handle %#llx from hash\n",
113                h, h->h_cookie);
114
115         spin_lock(&h->h_lock);
116         if (h->h_in == 0) {
117                 spin_unlock(&h->h_lock);
118                 return;
119         }
120         h->h_in = 0;
121         spin_unlock(&h->h_lock);
122         list_del_rcu(&h->h_link);
123 }
124
125 void class_handle_unhash(struct portals_handle *h)
126 {
127         struct handle_bucket *bucket;
128         bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
129
130         spin_lock(&bucket->lock);
131         class_handle_unhash_nolock(h);
132         spin_unlock(&bucket->lock);
133 }
134 EXPORT_SYMBOL(class_handle_unhash);
135
136 void class_handle_hash_back(struct portals_handle *h)
137 {
138         struct handle_bucket *bucket;
139         ENTRY;
140
141         bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
142
143         spin_lock(&bucket->lock);
144         list_add_rcu(&h->h_link, &bucket->head);
145         h->h_in = 1;
146         spin_unlock(&bucket->lock);
147
148         EXIT;
149 }
150 EXPORT_SYMBOL(class_handle_hash_back);
151
152 void *class_handle2object(__u64 cookie, const void *owner)
153 {
154         struct handle_bucket *bucket;
155         struct portals_handle *h;
156         void *retval = NULL;
157
158         ENTRY;
159
160         LASSERT(handle_hash != NULL);
161
162         /*
163          * Be careful when you want to change this code. See the
164          * rcu_read_lock() definition on top this file. - jxiong
165          */
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 || h->h_owner != owner)
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(struct rcu_head *rcu)
188 {
189         struct portals_handle *h;
190         void *ptr;
191
192         h = container_of(rcu, struct portals_handle, h_rcu);
193         ptr = (void *)(unsigned long)h->h_cookie;
194
195         if (h->h_ops->hop_free != NULL)
196                 h->h_ops->hop_free(ptr, h->h_size);
197         else
198                 OBD_FREE(ptr, h->h_size);
199 }
200 EXPORT_SYMBOL(class_handle_free_cb);
201
202 int class_handle_init(void)
203 {
204         struct handle_bucket *bucket;
205
206         LASSERT(handle_hash == NULL);
207
208         OBD_ALLOC_LARGE(handle_hash, sizeof(*bucket) * HANDLE_HASH_SIZE);
209         if (handle_hash == NULL)
210                 return -ENOMEM;
211
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         cfs_get_random_bytes(&handle_base, sizeof(handle_base));
219         LASSERT(handle_base != 0ULL);
220
221         return 0;
222 }
223
224 static int cleanup_all_handles(void)
225 {
226         int rc;
227         int i;
228
229         for (rc = i = 0; i < HANDLE_HASH_SIZE; i++) {
230                 struct portals_handle *h;
231
232                 spin_lock(&handle_hash[i].lock);
233                 list_for_each_entry_rcu(h, &(handle_hash[i].head), h_link) {
234                         CERROR("force clean handle %#llx addr %p ops %p\n",
235                                h->h_cookie, h, h->h_ops);
236
237                         class_handle_unhash_nolock(h);
238                         rc++;
239                 }
240                 spin_unlock(&handle_hash[i].lock);
241         }
242
243         return rc;
244 }
245
246 void class_handle_cleanup(void)
247 {
248         int count;
249
250         LASSERT(handle_hash != NULL);
251
252         count = cleanup_all_handles();
253
254         OBD_FREE_LARGE(handle_hash, sizeof(*handle_hash) * HANDLE_HASH_SIZE);
255         handle_hash = NULL;
256
257         if (count != 0)
258                 CERROR("handle_count at cleanup: %d\n", count);
259 }