Whamcloud - gitweb
LU-11607 tests: replace version/fstype calls in sanity/n
[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         spin_lock_init(&h->h_lock);
92
93         bucket = &handle_hash[h->h_cookie & HANDLE_HASH_MASK];
94         spin_lock(&bucket->lock);
95         hlist_add_head_rcu(&h->h_link, &bucket->head);
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 (hlist_unhashed(&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 (hlist_unhashed(&h->h_link)) {
117                 spin_unlock(&h->h_lock);
118                 return;
119         }
120         hlist_del_init_rcu(&h->h_link);
121         spin_unlock(&h->h_lock);
122 }
123
124 void class_handle_unhash(struct portals_handle *h)
125 {
126         struct handle_bucket *bucket;
127         bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
128
129         spin_lock(&bucket->lock);
130         class_handle_unhash_nolock(h);
131         spin_unlock(&bucket->lock);
132 }
133 EXPORT_SYMBOL(class_handle_unhash);
134
135 void *class_handle2object(u64 cookie, const char *owner)
136 {
137         struct handle_bucket *bucket;
138         struct portals_handle *h;
139         void *retval = NULL;
140
141         ENTRY;
142
143         LASSERT(handle_hash != NULL);
144
145         /*
146          * Be careful when you want to change this code. See the
147          * rcu_read_lock() definition on top this file. - jxiong
148          */
149         bucket = handle_hash + (cookie & HANDLE_HASH_MASK);
150
151         rcu_read_lock();
152         hlist_for_each_entry_rcu(h, &bucket->head, h_link) {
153                 if (h->h_cookie != cookie || h->h_owner != owner)
154                         continue;
155
156                 if (refcount_inc_not_zero(&h->h_ref)) {
157                         CDEBUG(D_INFO, "GET %s %p refcount=%d\n",
158                                h->h_owner, h,
159                                refcount_read(&h->h_ref));
160                         retval = h;
161                 }
162                 break;
163         }
164         rcu_read_unlock();
165
166         RETURN(retval);
167 }
168 EXPORT_SYMBOL(class_handle2object);
169
170 int class_handle_init(void)
171 {
172         struct handle_bucket *bucket;
173
174         LASSERT(handle_hash == NULL);
175
176         OBD_ALLOC_LARGE(handle_hash, sizeof(*bucket) * HANDLE_HASH_SIZE);
177         if (handle_hash == NULL)
178                 return -ENOMEM;
179
180         for (bucket = handle_hash + HANDLE_HASH_SIZE - 1; bucket >= handle_hash;
181              bucket--) {
182                 INIT_HLIST_HEAD(&bucket->head);
183                 spin_lock_init(&bucket->lock);
184         }
185
186         get_random_bytes(&handle_base, sizeof(handle_base));
187         LASSERT(handle_base != 0ULL);
188
189         return 0;
190 }
191
192 static int cleanup_all_handles(void)
193 {
194         int rc;
195         int i;
196
197         for (rc = i = 0; i < HANDLE_HASH_SIZE; i++) {
198                 struct portals_handle *h;
199
200                 spin_lock(&handle_hash[i].lock);
201                 hlist_for_each_entry_rcu(h, &handle_hash[i].head, h_link) {
202                         CERROR("force clean handle %#llx addr %p owner %p\n",
203                                h->h_cookie, h, h->h_owner);
204
205                         class_handle_unhash_nolock(h);
206                         rc++;
207                 }
208                 spin_unlock(&handle_hash[i].lock);
209         }
210
211         return rc;
212 }
213
214 void class_handle_cleanup(void)
215 {
216         int count;
217
218         LASSERT(handle_hash != NULL);
219
220         count = cleanup_all_handles();
221
222         OBD_FREE_LARGE(handle_hash, sizeof(*handle_hash) * HANDLE_HASH_SIZE);
223         handle_hash = NULL;
224
225         if (count != 0)
226                 CERROR("handle_count at cleanup: %d\n", count);
227 }