Whamcloud - gitweb
LU-8726 osd-ldiskfs: bypass read for benchmarking
[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, 2014, 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 <obd_support.h>
40 #include <lustre_handles.h>
41 #include <lustre_lib.h>
42
43
44 static __u64 handle_base;
45 #define HANDLE_INCR 7
46 static spinlock_t handle_base_lock;
47
48 static struct handle_bucket {
49         spinlock_t       lock;
50         struct list_head head;
51 } *handle_hash;
52
53 #define HANDLE_HASH_SIZE (1 << 16)
54 #define HANDLE_HASH_MASK (HANDLE_HASH_SIZE - 1)
55
56 /*
57  * Generate a unique 64bit cookie (hash) for a handle and insert it into
58  * global (per-node) hash-table.
59  */
60 void class_handle_hash(struct portals_handle *h,
61                        struct portals_handle_ops *ops)
62 {
63         struct handle_bucket *bucket;
64         ENTRY;
65
66         LASSERT(h != NULL);
67         LASSERT(list_empty(&h->h_link));
68
69         /*
70          * This is fast, but simplistic cookie generation algorithm, it will
71          * need a re-do at some point in the future for security.
72          */
73         spin_lock(&handle_base_lock);
74         handle_base += HANDLE_INCR;
75
76         if (unlikely(handle_base == 0)) {
77                 /*
78                  * Cookie of zero is "dangerous", because in many places it's
79                  * assumed that 0 means "unassigned" handle, not bound to any
80                  * object.
81                  */
82                 CWARN("The universe has been exhausted: cookie wrap-around.\n");
83                 handle_base += HANDLE_INCR;
84         }
85         h->h_cookie = handle_base;
86         spin_unlock(&handle_base_lock);
87
88         h->h_ops = ops;
89         spin_lock_init(&h->h_lock);
90
91         bucket = &handle_hash[h->h_cookie & HANDLE_HASH_MASK];
92         spin_lock(&bucket->lock);
93         list_add_rcu(&h->h_link, &bucket->head);
94         h->h_in = 1;
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 (list_empty(&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         spin_lock(&h->h_lock);
115         if (h->h_in == 0) {
116                 spin_unlock(&h->h_lock);
117                 return;
118         }
119         h->h_in = 0;
120         spin_unlock(&h->h_lock);
121         list_del_rcu(&h->h_link);
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_handle_hash_back(struct portals_handle *h)
136 {
137         struct handle_bucket *bucket;
138         ENTRY;
139
140         bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
141
142         spin_lock(&bucket->lock);
143         list_add_rcu(&h->h_link, &bucket->head);
144         h->h_in = 1;
145         spin_unlock(&bucket->lock);
146
147         EXIT;
148 }
149 EXPORT_SYMBOL(class_handle_hash_back);
150
151 void *class_handle2object(__u64 cookie, const void *owner)
152 {
153         struct handle_bucket *bucket;
154         struct portals_handle *h;
155         void *retval = NULL;
156         ENTRY;
157
158         LASSERT(handle_hash != NULL);
159
160         /* Be careful when you want to change this code. See the
161          * rcu_read_lock() definition on top this file. - jxiong */
162         bucket = handle_hash + (cookie & HANDLE_HASH_MASK);
163
164         rcu_read_lock();
165         list_for_each_entry_rcu(h, &bucket->head, h_link) {
166                 if (h->h_cookie != cookie || h->h_owner != owner)
167                         continue;
168
169                 spin_lock(&h->h_lock);
170                 if (likely(h->h_in != 0)) {
171                         h->h_ops->hop_addref(h);
172                         retval = h;
173                 }
174                 spin_unlock(&h->h_lock);
175                 break;
176         }
177         rcu_read_unlock();
178
179         RETURN(retval);
180 }
181 EXPORT_SYMBOL(class_handle2object);
182
183 void class_handle_free_cb(struct rcu_head *rcu)
184 {
185         struct portals_handle *h;
186         void *ptr;
187
188         h = container_of(rcu, struct portals_handle, h_rcu);
189         ptr = (void *)(unsigned long)h->h_cookie;
190
191         if (h->h_ops->hop_free != NULL)
192                 h->h_ops->hop_free(ptr, h->h_size);
193         else
194                 OBD_FREE(ptr, h->h_size);
195 }
196 EXPORT_SYMBOL(class_handle_free_cb);
197
198 int class_handle_init(void)
199 {
200         struct handle_bucket *bucket;
201         struct timespec64 ts;
202         int seed[2];
203
204         LASSERT(handle_hash == NULL);
205
206         OBD_ALLOC_LARGE(handle_hash, sizeof(*bucket) * HANDLE_HASH_SIZE);
207         if (handle_hash == NULL)
208                 return -ENOMEM;
209
210         spin_lock_init(&handle_base_lock);
211         for (bucket = handle_hash + HANDLE_HASH_SIZE - 1; bucket >= handle_hash;
212              bucket--) {
213                 INIT_LIST_HEAD(&bucket->head);
214                 spin_lock_init(&bucket->lock);
215         }
216
217         /** bug 21430: add randomness to the initial base */
218         cfs_get_random_bytes(seed, sizeof(seed));
219         ktime_get_ts64(&ts);
220         cfs_srand(ts.tv_sec ^ seed[0], ts.tv_nsec ^ seed[1]);
221
222         cfs_get_random_bytes(&handle_base, sizeof(handle_base));
223         LASSERT(handle_base != 0ULL);
224
225         return 0;
226 }
227
228 static int cleanup_all_handles(void)
229 {
230         int rc;
231         int i;
232
233         for (rc = i = 0; i < HANDLE_HASH_SIZE; i++) {
234                 struct portals_handle *h;
235
236                 spin_lock(&handle_hash[i].lock);
237                 list_for_each_entry_rcu(h, &(handle_hash[i].head), h_link) {
238                         CERROR("force clean handle %#llx addr %p ops %p\n",
239                                h->h_cookie, h, h->h_ops);
240
241                         class_handle_unhash_nolock(h);
242                         rc++;
243                 }
244                 spin_unlock(&handle_hash[i].lock);
245         }
246
247         return rc;
248 }
249
250 void class_handle_cleanup(void)
251 {
252         int count;
253         LASSERT(handle_hash != NULL);
254
255         count = cleanup_all_handles();
256
257         OBD_FREE_LARGE(handle_hash, sizeof(*handle_hash) * HANDLE_HASH_SIZE);
258         handle_hash = NULL;
259
260         if (count != 0)
261                 CERROR("handle_count at cleanup: %d\n", count);
262 }