Whamcloud - gitweb
LU-12542 handle: rename ops to owner
[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, const char *owner)
63 {
64         struct handle_bucket *bucket;
65
66         ENTRY;
67
68         LASSERT(h != NULL);
69         LASSERT(list_empty(&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         list_add_rcu(&h->h_link, &bucket->head);
96         h->h_in = 1;
97         spin_unlock(&bucket->lock);
98
99         CDEBUG(D_INFO, "added object %p with handle %#llx to hash\n",
100                h, h->h_cookie);
101         EXIT;
102 }
103 EXPORT_SYMBOL(class_handle_hash);
104
105 static void class_handle_unhash_nolock(struct portals_handle *h)
106 {
107         if (list_empty(&h->h_link)) {
108                 CERROR("removing an already-removed handle (%#llx)\n",
109                        h->h_cookie);
110                 return;
111         }
112
113         CDEBUG(D_INFO, "removing object %p with handle %#llx from hash\n",
114                h, h->h_cookie);
115
116         spin_lock(&h->h_lock);
117         if (h->h_in == 0) {
118                 spin_unlock(&h->h_lock);
119                 return;
120         }
121         h->h_in = 0;
122         spin_unlock(&h->h_lock);
123         list_del_rcu(&h->h_link);
124 }
125
126 void class_handle_unhash(struct portals_handle *h)
127 {
128         struct handle_bucket *bucket;
129         bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
130
131         spin_lock(&bucket->lock);
132         class_handle_unhash_nolock(h);
133         spin_unlock(&bucket->lock);
134 }
135 EXPORT_SYMBOL(class_handle_unhash);
136
137 void *class_handle2object(u64 cookie, const char *owner)
138 {
139         struct handle_bucket *bucket;
140         struct portals_handle *h;
141         void *retval = NULL;
142
143         ENTRY;
144
145         LASSERT(handle_hash != NULL);
146
147         /*
148          * Be careful when you want to change this code. See the
149          * rcu_read_lock() definition on top this file. - jxiong
150          */
151         bucket = handle_hash + (cookie & HANDLE_HASH_MASK);
152
153         rcu_read_lock();
154         list_for_each_entry_rcu(h, &bucket->head, h_link) {
155                 if (h->h_cookie != cookie || h->h_owner != owner)
156                         continue;
157
158                 spin_lock(&h->h_lock);
159                 if (likely(h->h_in != 0)) {
160                         refcount_inc(&h->h_ref);
161                         CDEBUG(D_INFO, "GET %s %p refcount=%d\n",
162                                h->h_owner, h,
163                                refcount_read(&h->h_ref));
164                         retval = h;
165                 }
166                 spin_unlock(&h->h_lock);
167                 break;
168         }
169         rcu_read_unlock();
170
171         RETURN(retval);
172 }
173 EXPORT_SYMBOL(class_handle2object);
174
175 int class_handle_init(void)
176 {
177         struct handle_bucket *bucket;
178
179         LASSERT(handle_hash == NULL);
180
181         OBD_ALLOC_LARGE(handle_hash, sizeof(*bucket) * HANDLE_HASH_SIZE);
182         if (handle_hash == NULL)
183                 return -ENOMEM;
184
185         for (bucket = handle_hash + HANDLE_HASH_SIZE - 1; bucket >= handle_hash;
186              bucket--) {
187                 INIT_LIST_HEAD(&bucket->head);
188                 spin_lock_init(&bucket->lock);
189         }
190
191         get_random_bytes(&handle_base, sizeof(handle_base));
192         LASSERT(handle_base != 0ULL);
193
194         return 0;
195 }
196
197 static int cleanup_all_handles(void)
198 {
199         int rc;
200         int i;
201
202         for (rc = i = 0; i < HANDLE_HASH_SIZE; i++) {
203                 struct portals_handle *h;
204
205                 spin_lock(&handle_hash[i].lock);
206                 list_for_each_entry_rcu(h, &(handle_hash[i].head), h_link) {
207                         CERROR("force clean handle %#llx addr %p owner %p\n",
208                                h->h_cookie, h, h->h_owner);
209
210                         class_handle_unhash_nolock(h);
211                         rc++;
212                 }
213                 spin_unlock(&handle_hash[i].lock);
214         }
215
216         return rc;
217 }
218
219 void class_handle_cleanup(void)
220 {
221         int count;
222
223         LASSERT(handle_hash != NULL);
224
225         count = cleanup_all_handles();
226
227         OBD_FREE_LARGE(handle_hash, sizeof(*handle_hash) * HANDLE_HASH_SIZE);
228         handle_hash = NULL;
229
230         if (count != 0)
231                 CERROR("handle_count at cleanup: %d\n", count);
232 }