Whamcloud - gitweb
LU-17744 ldiskfs: mballoc stats fixes
[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  *
31  * lustre/obdclass/lustre_handles.c
32  *
33  * Author: Phil Schwan <phil@clusterfs.com>
34  */
35
36 #define DEBUG_SUBSYSTEM S_CLASS
37
38 #include <linux/random.h>
39
40 #include <obd_support.h>
41 #include <lustre_handles.h>
42 #include <lustre_lib.h>
43
44
45 static __u64 handle_base;
46 #define HANDLE_INCR 7
47 static DEFINE_SPINLOCK(handle_base_lock);
48
49 static struct handle_bucket {
50         spinlock_t lock;
51         struct hlist_head       head;
52 } *handle_hash;
53
54 #define HANDLE_HASH_SIZE (1 << 16)
55 #define HANDLE_HASH_MASK (HANDLE_HASH_SIZE - 1)
56
57 /*
58  * Generate a unique 64bit cookie (hash) for a handle and insert it into
59  * global (per-node) hash-table.
60  */
61 void class_handle_hash(struct portals_handle *h, const char *owner)
62 {
63         struct handle_bucket *bucket;
64
65         ENTRY;
66
67         LASSERT(h != NULL);
68         LASSERT(hlist_unhashed(&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_owner = owner;
90
91         bucket = &handle_hash[h->h_cookie & HANDLE_HASH_MASK];
92         spin_lock(&bucket->lock);
93         hlist_add_head_rcu(&h->h_link, &bucket->head);
94         spin_unlock(&bucket->lock);
95
96         CDEBUG(D_INFO, "added object %p with handle %#llx to hash\n",
97                h, h->h_cookie);
98         EXIT;
99 }
100 EXPORT_SYMBOL(class_handle_hash);
101
102 static void class_handle_unhash_nolock(struct portals_handle *h)
103 {
104         if (hlist_unhashed(&h->h_link)) {
105                 CERROR("removing an already-removed handle (%#llx)\n",
106                        h->h_cookie);
107                 return;
108         }
109
110         CDEBUG(D_INFO, "removing object %p with handle %#llx from hash\n",
111                h, h->h_cookie);
112
113         hlist_del_init_rcu(&h->h_link);
114 }
115
116 void class_handle_unhash(struct portals_handle *h)
117 {
118         struct handle_bucket *bucket;
119         bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
120
121         spin_lock(&bucket->lock);
122         class_handle_unhash_nolock(h);
123         spin_unlock(&bucket->lock);
124 }
125 EXPORT_SYMBOL(class_handle_unhash);
126
127 void *class_handle2object(u64 cookie, const char *owner)
128 {
129         struct handle_bucket *bucket;
130         struct portals_handle *h;
131         void *retval = NULL;
132
133         ENTRY;
134
135         LASSERT(handle_hash != NULL);
136
137         /*
138          * Be careful when you want to change this code. See the
139          * rcu_read_lock() definition on top this file. - jxiong
140          */
141         bucket = handle_hash + (cookie & HANDLE_HASH_MASK);
142
143         rcu_read_lock();
144         hlist_for_each_entry_rcu(h, &bucket->head, h_link) {
145                 if (h->h_cookie != cookie || h->h_owner != owner)
146                         continue;
147
148                 if (refcount_inc_not_zero(&h->h_ref)) {
149                         CDEBUG(D_INFO, "GET %s %p refcount=%d\n",
150                                h->h_owner, h,
151                                refcount_read(&h->h_ref));
152                         retval = h;
153                 }
154                 break;
155         }
156         rcu_read_unlock();
157
158         RETURN(retval);
159 }
160 EXPORT_SYMBOL(class_handle2object);
161
162 int class_handle_init(void)
163 {
164         struct handle_bucket *bucket;
165
166         LASSERT(handle_hash == NULL);
167
168         OBD_ALLOC_PTR_ARRAY_LARGE(handle_hash, HANDLE_HASH_SIZE);
169         if (handle_hash == NULL)
170                 return -ENOMEM;
171
172         for (bucket = handle_hash + HANDLE_HASH_SIZE - 1; bucket >= handle_hash;
173              bucket--) {
174                 INIT_HLIST_HEAD(&bucket->head);
175                 spin_lock_init(&bucket->lock);
176         }
177
178         get_random_bytes(&handle_base, sizeof(handle_base));
179         LASSERT(handle_base != 0ULL);
180
181         return 0;
182 }
183
184 static int cleanup_all_handles(void)
185 {
186         int rc;
187         int i;
188
189         for (rc = i = 0; i < HANDLE_HASH_SIZE; i++) {
190                 struct portals_handle *h;
191
192                 spin_lock(&handle_hash[i].lock);
193                 hlist_for_each_entry_rcu(h, &handle_hash[i].head, h_link) {
194                         CERROR("force clean handle %#llx addr %p owner %p\n",
195                                h->h_cookie, h, h->h_owner);
196
197                         class_handle_unhash_nolock(h);
198                         rc++;
199                 }
200                 spin_unlock(&handle_hash[i].lock);
201         }
202
203         return rc;
204 }
205
206 void class_handle_cleanup(void)
207 {
208         int count;
209
210         LASSERT(handle_hash != NULL);
211
212         count = cleanup_all_handles();
213
214         OBD_FREE_PTR_ARRAY_LARGE(handle_hash, HANDLE_HASH_SIZE);
215         handle_hash = NULL;
216
217         if (count != 0)
218                 CERROR("handle_count at cleanup: %d\n", count);
219 }