Whamcloud - gitweb
LU-12355 llite: include file linux/selinux.h removed
[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                        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_handle_hash_back(struct portals_handle *h)
139 {
140         struct handle_bucket *bucket;
141         ENTRY;
142
143         bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
144
145         spin_lock(&bucket->lock);
146         list_add_rcu(&h->h_link, &bucket->head);
147         h->h_in = 1;
148         spin_unlock(&bucket->lock);
149
150         EXIT;
151 }
152 EXPORT_SYMBOL(class_handle_hash_back);
153
154 void *class_handle2object(__u64 cookie, const void *owner)
155 {
156         struct handle_bucket *bucket;
157         struct portals_handle *h;
158         void *retval = NULL;
159
160         ENTRY;
161
162         LASSERT(handle_hash != NULL);
163
164         /*
165          * Be careful when you want to change this code. See the
166          * rcu_read_lock() definition on top this file. - jxiong
167          */
168         bucket = handle_hash + (cookie & HANDLE_HASH_MASK);
169
170         rcu_read_lock();
171         list_for_each_entry_rcu(h, &bucket->head, h_link) {
172                 if (h->h_cookie != cookie || h->h_owner != owner)
173                         continue;
174
175                 spin_lock(&h->h_lock);
176                 if (likely(h->h_in != 0)) {
177                         h->h_ops->hop_addref(h);
178                         retval = h;
179                 }
180                 spin_unlock(&h->h_lock);
181                 break;
182         }
183         rcu_read_unlock();
184
185         RETURN(retval);
186 }
187 EXPORT_SYMBOL(class_handle2object);
188
189 void class_handle_free_cb(struct rcu_head *rcu)
190 {
191         struct portals_handle *h;
192         void *ptr;
193
194         h = container_of(rcu, struct portals_handle, h_rcu);
195         ptr = (void *)(unsigned long)h->h_cookie;
196
197         if (h->h_ops->hop_free != NULL)
198                 h->h_ops->hop_free(ptr, h->h_size);
199         else
200                 OBD_FREE(ptr, h->h_size);
201 }
202 EXPORT_SYMBOL(class_handle_free_cb);
203
204 int class_handle_init(void)
205 {
206         struct handle_bucket *bucket;
207
208         LASSERT(handle_hash == NULL);
209
210         OBD_ALLOC_LARGE(handle_hash, sizeof(*bucket) * HANDLE_HASH_SIZE);
211         if (handle_hash == NULL)
212                 return -ENOMEM;
213
214         for (bucket = handle_hash + HANDLE_HASH_SIZE - 1; bucket >= handle_hash;
215              bucket--) {
216                 INIT_LIST_HEAD(&bucket->head);
217                 spin_lock_init(&bucket->lock);
218         }
219
220         get_random_bytes(&handle_base, sizeof(handle_base));
221         LASSERT(handle_base != 0ULL);
222
223         return 0;
224 }
225
226 static int cleanup_all_handles(void)
227 {
228         int rc;
229         int i;
230
231         for (rc = i = 0; i < HANDLE_HASH_SIZE; i++) {
232                 struct portals_handle *h;
233
234                 spin_lock(&handle_hash[i].lock);
235                 list_for_each_entry_rcu(h, &(handle_hash[i].head), h_link) {
236                         CERROR("force clean handle %#llx addr %p ops %p\n",
237                                h->h_cookie, h, h->h_ops);
238
239                         class_handle_unhash_nolock(h);
240                         rc++;
241                 }
242                 spin_unlock(&handle_hash[i].lock);
243         }
244
245         return rc;
246 }
247
248 void class_handle_cleanup(void)
249 {
250         int count;
251
252         LASSERT(handle_hash != NULL);
253
254         count = cleanup_all_handles();
255
256         OBD_FREE_LARGE(handle_hash, sizeof(*handle_hash) * HANDLE_HASH_SIZE);
257         handle_hash = NULL;
258
259         if (count != 0)
260                 CERROR("handle_count at cleanup: %d\n", count);
261 }