Whamcloud - gitweb
27f69715e582d122eae908a2e38b06dae03c4178
[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 hlist_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, const char *owner)
63 {
64         struct handle_bucket *bucket;
65
66         ENTRY;
67
68         LASSERT(h != NULL);
69         LASSERT(hlist_unhashed(&h->h_link));
70
71         /*
72          * This is fast, but simplistic cookie generation algorithm, it will
73          * need a re-do at some point in the future for security.
74          */
75         spin_lock(&handle_base_lock);
76         handle_base += HANDLE_INCR;
77
78         if (unlikely(handle_base == 0)) {
79                 /*
80                  * Cookie of zero is "dangerous", because in many places it's
81                  * assumed that 0 means "unassigned" handle, not bound to any
82                  * object.
83                  */
84                 CWARN("The universe has been exhausted: cookie wrap-around.\n");
85                 handle_base += HANDLE_INCR;
86         }
87         h->h_cookie = handle_base;
88         spin_unlock(&handle_base_lock);
89
90         h->h_owner = owner;
91
92         bucket = &handle_hash[h->h_cookie & HANDLE_HASH_MASK];
93         spin_lock(&bucket->lock);
94         hlist_add_head_rcu(&h->h_link, &bucket->head);
95         spin_unlock(&bucket->lock);
96
97         CDEBUG(D_INFO, "added object %p with handle %#llx to hash\n",
98                h, h->h_cookie);
99         EXIT;
100 }
101 EXPORT_SYMBOL(class_handle_hash);
102
103 static void class_handle_unhash_nolock(struct portals_handle *h)
104 {
105         if (hlist_unhashed(&h->h_link)) {
106                 CERROR("removing an already-removed handle (%#llx)\n",
107                        h->h_cookie);
108                 return;
109         }
110
111         CDEBUG(D_INFO, "removing object %p with handle %#llx from hash\n",
112                h, h->h_cookie);
113
114         hlist_del_init_rcu(&h->h_link);
115 }
116
117 void class_handle_unhash(struct portals_handle *h)
118 {
119         struct handle_bucket *bucket;
120         bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
121
122         spin_lock(&bucket->lock);
123         class_handle_unhash_nolock(h);
124         spin_unlock(&bucket->lock);
125 }
126 EXPORT_SYMBOL(class_handle_unhash);
127
128 void *class_handle2object(u64 cookie, const char *owner)
129 {
130         struct handle_bucket *bucket;
131         struct portals_handle *h;
132         void *retval = NULL;
133
134         ENTRY;
135
136         LASSERT(handle_hash != NULL);
137
138         /*
139          * Be careful when you want to change this code. See the
140          * rcu_read_lock() definition on top this file. - jxiong
141          */
142         bucket = handle_hash + (cookie & HANDLE_HASH_MASK);
143
144         rcu_read_lock();
145         hlist_for_each_entry_rcu(h, &bucket->head, h_link) {
146                 if (h->h_cookie != cookie || h->h_owner != owner)
147                         continue;
148
149                 if (refcount_inc_not_zero(&h->h_ref)) {
150                         CDEBUG(D_INFO, "GET %s %p refcount=%d\n",
151                                h->h_owner, h,
152                                refcount_read(&h->h_ref));
153                         retval = h;
154                 }
155                 break;
156         }
157         rcu_read_unlock();
158
159         RETURN(retval);
160 }
161 EXPORT_SYMBOL(class_handle2object);
162
163 int class_handle_init(void)
164 {
165         struct handle_bucket *bucket;
166
167         LASSERT(handle_hash == NULL);
168
169         OBD_ALLOC_LARGE(handle_hash, sizeof(*bucket) * HANDLE_HASH_SIZE);
170         if (handle_hash == NULL)
171                 return -ENOMEM;
172
173         for (bucket = handle_hash + HANDLE_HASH_SIZE - 1; bucket >= handle_hash;
174              bucket--) {
175                 INIT_HLIST_HEAD(&bucket->head);
176                 spin_lock_init(&bucket->lock);
177         }
178
179         get_random_bytes(&handle_base, sizeof(handle_base));
180         LASSERT(handle_base != 0ULL);
181
182         return 0;
183 }
184
185 static int cleanup_all_handles(void)
186 {
187         int rc;
188         int i;
189
190         for (rc = i = 0; i < HANDLE_HASH_SIZE; i++) {
191                 struct portals_handle *h;
192
193                 spin_lock(&handle_hash[i].lock);
194                 hlist_for_each_entry_rcu(h, &handle_hash[i].head, h_link) {
195                         CERROR("force clean handle %#llx addr %p owner %p\n",
196                                h->h_cookie, h, h->h_owner);
197
198                         class_handle_unhash_nolock(h);
199                         rc++;
200                 }
201                 spin_unlock(&handle_hash[i].lock);
202         }
203
204         return rc;
205 }
206
207 void class_handle_cleanup(void)
208 {
209         int count;
210
211         LASSERT(handle_hash != NULL);
212
213         count = cleanup_all_handles();
214
215         OBD_FREE_LARGE(handle_hash, sizeof(*handle_hash) * HANDLE_HASH_SIZE);
216         handle_hash = NULL;
217
218         if (count != 0)
219                 CERROR("handle_count at cleanup: %d\n", count);
220 }