Whamcloud - gitweb
LU-6158 mdt: always shrink_capsule in getxattr_all
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/obdclass/lustre_handles.c
37  *
38  * Author: Phil Schwan <phil@clusterfs.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_CLASS
42
43 #include <obd_support.h>
44 #include <lustre_handles.h>
45 #include <lustre_lib.h>
46
47
48 static __u64 handle_base;
49 #define HANDLE_INCR 7
50 static spinlock_t handle_base_lock;
51
52 static struct handle_bucket {
53         spinlock_t       lock;
54         struct list_head head;
55 } *handle_hash;
56
57 #define HANDLE_HASH_SIZE (1 << 16)
58 #define HANDLE_HASH_MASK (HANDLE_HASH_SIZE - 1)
59
60 /*
61  * Generate a unique 64bit cookie (hash) for a handle and insert it into
62  * global (per-node) hash-table.
63  */
64 void class_handle_hash(struct portals_handle *h,
65                        struct portals_handle_ops *ops)
66 {
67         struct handle_bucket *bucket;
68         ENTRY;
69
70         LASSERT(h != NULL);
71         LASSERT(list_empty(&h->h_link));
72
73         /*
74          * This is fast, but simplistic cookie generation algorithm, it will
75          * need a re-do at some point in the future for security.
76          */
77         spin_lock(&handle_base_lock);
78         handle_base += HANDLE_INCR;
79
80         if (unlikely(handle_base == 0)) {
81                 /*
82                  * Cookie of zero is "dangerous", because in many places it's
83                  * assumed that 0 means "unassigned" handle, not bound to any
84                  * object.
85                  */
86                 CWARN("The universe has been exhausted: cookie wrap-around.\n");
87                 handle_base += HANDLE_INCR;
88         }
89         h->h_cookie = handle_base;
90         spin_unlock(&handle_base_lock);
91
92         h->h_ops = ops;
93         spin_lock_init(&h->h_lock);
94
95         bucket = &handle_hash[h->h_cookie & HANDLE_HASH_MASK];
96         spin_lock(&bucket->lock);
97         list_add_rcu(&h->h_link, &bucket->head);
98         h->h_in = 1;
99         spin_unlock(&bucket->lock);
100
101         CDEBUG(D_INFO, "added object %p with handle "LPX64" to hash\n",
102                h, h->h_cookie);
103         EXIT;
104 }
105 EXPORT_SYMBOL(class_handle_hash);
106
107 static void class_handle_unhash_nolock(struct portals_handle *h)
108 {
109         if (list_empty(&h->h_link)) {
110                 CERROR("removing an already-removed handle ("LPX64")\n",
111                        h->h_cookie);
112                 return;
113         }
114
115         CDEBUG(D_INFO, "removing object %p with handle "LPX64" from hash\n",
116                h, h->h_cookie);
117
118         spin_lock(&h->h_lock);
119         if (h->h_in == 0) {
120                 spin_unlock(&h->h_lock);
121                 return;
122         }
123         h->h_in = 0;
124         spin_unlock(&h->h_lock);
125         list_del_rcu(&h->h_link);
126 }
127
128 void class_handle_unhash(struct portals_handle *h)
129 {
130         struct handle_bucket *bucket;
131         bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
132
133         spin_lock(&bucket->lock);
134         class_handle_unhash_nolock(h);
135         spin_unlock(&bucket->lock);
136 }
137 EXPORT_SYMBOL(class_handle_unhash);
138
139 void class_handle_hash_back(struct portals_handle *h)
140 {
141         struct handle_bucket *bucket;
142         ENTRY;
143
144         bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
145
146         spin_lock(&bucket->lock);
147         list_add_rcu(&h->h_link, &bucket->head);
148         h->h_in = 1;
149         spin_unlock(&bucket->lock);
150
151         EXIT;
152 }
153 EXPORT_SYMBOL(class_handle_hash_back);
154
155 void *class_handle2object(__u64 cookie, const void *owner)
156 {
157         struct handle_bucket *bucket;
158         struct portals_handle *h;
159         void *retval = NULL;
160         ENTRY;
161
162         LASSERT(handle_hash != NULL);
163
164         /* Be careful when you want to change this code. See the
165          * rcu_read_lock() definition on top this file. - jxiong */
166         bucket = handle_hash + (cookie & HANDLE_HASH_MASK);
167
168         rcu_read_lock();
169         list_for_each_entry_rcu(h, &bucket->head, h_link) {
170                 if (h->h_cookie != cookie || h->h_owner != owner)
171                         continue;
172
173                 spin_lock(&h->h_lock);
174                 if (likely(h->h_in != 0)) {
175                         h->h_ops->hop_addref(h);
176                         retval = h;
177                 }
178                 spin_unlock(&h->h_lock);
179                 break;
180         }
181         rcu_read_unlock();
182
183         RETURN(retval);
184 }
185 EXPORT_SYMBOL(class_handle2object);
186
187 void class_handle_free_cb(struct rcu_head *rcu)
188 {
189         struct portals_handle *h;
190         void *ptr;
191
192         h = container_of(rcu, struct portals_handle, h_rcu);
193         ptr = (void *)(unsigned long)h->h_cookie;
194
195         if (h->h_ops->hop_free != NULL)
196                 h->h_ops->hop_free(ptr, h->h_size);
197         else
198                 OBD_FREE(ptr, h->h_size);
199 }
200 EXPORT_SYMBOL(class_handle_free_cb);
201
202 int class_handle_init(void)
203 {
204         struct handle_bucket *bucket;
205         struct timeval tv;
206         int seed[2];
207
208         LASSERT(handle_hash == NULL);
209
210         OBD_ALLOC_LARGE(handle_hash, sizeof(*bucket) * HANDLE_HASH_SIZE);
211         if (handle_hash == NULL)
212                 return -ENOMEM;
213
214         spin_lock_init(&handle_base_lock);
215         for (bucket = handle_hash + HANDLE_HASH_SIZE - 1; bucket >= handle_hash;
216              bucket--) {
217                 INIT_LIST_HEAD(&bucket->head);
218                 spin_lock_init(&bucket->lock);
219         }
220
221         /** bug 21430: add randomness to the initial base */
222         cfs_get_random_bytes(seed, sizeof(seed));
223         do_gettimeofday(&tv);
224         cfs_srand(tv.tv_sec ^ seed[0], tv.tv_usec ^ seed[1]);
225
226         cfs_get_random_bytes(&handle_base, sizeof(handle_base));
227         LASSERT(handle_base != 0ULL);
228
229         return 0;
230 }
231
232 static int cleanup_all_handles(void)
233 {
234         int rc;
235         int i;
236
237         for (rc = i = 0; i < HANDLE_HASH_SIZE; i++) {
238                 struct portals_handle *h;
239
240                 spin_lock(&handle_hash[i].lock);
241                 list_for_each_entry_rcu(h, &(handle_hash[i].head), h_link) {
242                         CERROR("force clean handle "LPX64" addr %p ops %p\n",
243                                h->h_cookie, h, h->h_ops);
244
245                         class_handle_unhash_nolock(h);
246                         rc++;
247                 }
248                 spin_unlock(&handle_hash[i].lock);
249         }
250
251         return rc;
252 }
253
254 void class_handle_cleanup(void)
255 {
256         int count;
257         LASSERT(handle_hash != NULL);
258
259         count = cleanup_all_handles();
260
261         OBD_FREE_LARGE(handle_hash, sizeof(*handle_hash) * HANDLE_HASH_SIZE);
262         handle_hash = NULL;
263
264         if (count != 0)
265                 CERROR("handle_count at cleanup: %d\n", count);
266 }