Whamcloud - gitweb
LU-12542 handles: discard h_owner in favour of h_ops
[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 <linux/random.h>
40
41 #include <obd_support.h>
42 #include <lustre_handles.h>
43 #include <lustre_lib.h>
44
45
46 static __u64 handle_base;
47 #define HANDLE_INCR 7
48 static DEFINE_SPINLOCK(handle_base_lock);
49
50 static struct handle_bucket {
51         spinlock_t lock;
52         struct list_head head;
53 } *handle_hash;
54
55 #define HANDLE_HASH_SIZE (1 << 16)
56 #define HANDLE_HASH_MASK (HANDLE_HASH_SIZE - 1)
57
58 /*
59  * Generate a unique 64bit cookie (hash) for a handle and insert it into
60  * global (per-node) hash-table.
61  */
62 void class_handle_hash(struct portals_handle *h,
63                        const struct portals_handle_ops *ops)
64 {
65         struct handle_bucket *bucket;
66
67         ENTRY;
68
69         LASSERT(h != NULL);
70         LASSERT(list_empty(&h->h_link));
71
72         /*
73          * This is fast, but simplistic cookie generation algorithm, it will
74          * need a re-do at some point in the future for security.
75          */
76         spin_lock(&handle_base_lock);
77         handle_base += HANDLE_INCR;
78
79         if (unlikely(handle_base == 0)) {
80                 /*
81                  * Cookie of zero is "dangerous", because in many places it's
82                  * assumed that 0 means "unassigned" handle, not bound to any
83                  * object.
84                  */
85                 CWARN("The universe has been exhausted: cookie wrap-around.\n");
86                 handle_base += HANDLE_INCR;
87         }
88         h->h_cookie = handle_base;
89         spin_unlock(&handle_base_lock);
90
91         h->h_ops = ops;
92         spin_lock_init(&h->h_lock);
93
94         bucket = &handle_hash[h->h_cookie & HANDLE_HASH_MASK];
95         spin_lock(&bucket->lock);
96         list_add_rcu(&h->h_link, &bucket->head);
97         h->h_in = 1;
98         spin_unlock(&bucket->lock);
99
100         CDEBUG(D_INFO, "added object %p with handle %#llx to hash\n",
101                h, h->h_cookie);
102         EXIT;
103 }
104 EXPORT_SYMBOL(class_handle_hash);
105
106 static void class_handle_unhash_nolock(struct portals_handle *h)
107 {
108         if (list_empty(&h->h_link)) {
109                 CERROR("removing an already-removed handle (%#llx)\n",
110                        h->h_cookie);
111                 return;
112         }
113
114         CDEBUG(D_INFO, "removing object %p with handle %#llx from hash\n",
115                h, h->h_cookie);
116
117         spin_lock(&h->h_lock);
118         if (h->h_in == 0) {
119                 spin_unlock(&h->h_lock);
120                 return;
121         }
122         h->h_in = 0;
123         spin_unlock(&h->h_lock);
124         list_del_rcu(&h->h_link);
125 }
126
127 void class_handle_unhash(struct portals_handle *h)
128 {
129         struct handle_bucket *bucket;
130         bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
131
132         spin_lock(&bucket->lock);
133         class_handle_unhash_nolock(h);
134         spin_unlock(&bucket->lock);
135 }
136 EXPORT_SYMBOL(class_handle_unhash);
137
138 void *class_handle2object(u64 cookie, const struct portals_handle_ops *ops)
139 {
140         struct handle_bucket *bucket;
141         struct portals_handle *h;
142         void *retval = NULL;
143
144         ENTRY;
145
146         LASSERT(handle_hash != NULL);
147
148         /*
149          * Be careful when you want to change this code. See the
150          * rcu_read_lock() definition on top this file. - jxiong
151          */
152         bucket = handle_hash + (cookie & HANDLE_HASH_MASK);
153
154         rcu_read_lock();
155         list_for_each_entry_rcu(h, &bucket->head, h_link) {
156                 if (h->h_cookie != cookie || h->h_ops != ops)
157                         continue;
158
159                 spin_lock(&h->h_lock);
160                 if (likely(h->h_in != 0)) {
161                         h->h_ops->hop_addref(h);
162                         retval = h;
163                 }
164                 spin_unlock(&h->h_lock);
165                 break;
166         }
167         rcu_read_unlock();
168
169         RETURN(retval);
170 }
171 EXPORT_SYMBOL(class_handle2object);
172
173 void class_handle_free_cb(struct rcu_head *rcu)
174 {
175         struct portals_handle *h;
176         void *ptr;
177
178         h = container_of(rcu, struct portals_handle, h_rcu);
179         ptr = (void *)(unsigned long)h->h_cookie;
180
181         if (h->h_ops->hop_free != NULL)
182                 h->h_ops->hop_free(ptr, h->h_size);
183         else
184                 OBD_FREE(ptr, h->h_size);
185 }
186 EXPORT_SYMBOL(class_handle_free_cb);
187
188 int class_handle_init(void)
189 {
190         struct handle_bucket *bucket;
191
192         LASSERT(handle_hash == NULL);
193
194         OBD_ALLOC_LARGE(handle_hash, sizeof(*bucket) * HANDLE_HASH_SIZE);
195         if (handle_hash == NULL)
196                 return -ENOMEM;
197
198         for (bucket = handle_hash + HANDLE_HASH_SIZE - 1; bucket >= handle_hash;
199              bucket--) {
200                 INIT_LIST_HEAD(&bucket->head);
201                 spin_lock_init(&bucket->lock);
202         }
203
204         get_random_bytes(&handle_base, sizeof(handle_base));
205         LASSERT(handle_base != 0ULL);
206
207         return 0;
208 }
209
210 static int cleanup_all_handles(void)
211 {
212         int rc;
213         int i;
214
215         for (rc = i = 0; i < HANDLE_HASH_SIZE; i++) {
216                 struct portals_handle *h;
217
218                 spin_lock(&handle_hash[i].lock);
219                 list_for_each_entry_rcu(h, &(handle_hash[i].head), h_link) {
220                         CERROR("force clean handle %#llx addr %p ops %p\n",
221                                h->h_cookie, h, h->h_ops);
222
223                         class_handle_unhash_nolock(h);
224                         rc++;
225                 }
226                 spin_unlock(&handle_hash[i].lock);
227         }
228
229         return rc;
230 }
231
232 void class_handle_cleanup(void)
233 {
234         int count;
235
236         LASSERT(handle_hash != NULL);
237
238         count = cleanup_all_handles();
239
240         OBD_FREE_LARGE(handle_hash, sizeof(*handle_hash) * HANDLE_HASH_SIZE);
241         handle_hash = NULL;
242
243         if (count != 0)
244                 CERROR("handle_count at cleanup: %d\n", count);
245 }