Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / obdclass / lustre_handles.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2002 Cluster File Systems, Inc.
5  *   Author: Phil Schwan <phil@clusterfs.com>
6  *
7  *   This file is part of the Lustre file system, http://www.lustre.org
8  *   Lustre is a trademark of Cluster File Systems, Inc.
9  *
10  *   You may have signed or agreed to another license before downloading
11  *   this software.  If so, you are bound by the terms and conditions
12  *   of that agreement, and the following does not apply to you.  See the
13  *   LICENSE file included with this distribution for more information.
14  *
15  *   If you did not agree to a different license, then this copy of Lustre
16  *   is open source software; you can redistribute it and/or modify it
17  *   under the terms of version 2 of the GNU General Public License as
18  *   published by the Free Software Foundation.
19  *
20  *   In either case, Lustre is distributed in the hope that it will be
21  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
22  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  *   license text for more details.
24  */
25
26 #define DEBUG_SUBSYSTEM S_CLASS
27 #ifndef __KERNEL__
28 # include <liblustre.h>
29 #endif
30
31 #include <obd_support.h>
32 #include <lustre_handles.h>
33 #include <lustre_lib.h>
34
35 #if !defined(HAVE_RCU) || !defined(__KERNEL__)
36 # define list_add_rcu            list_add
37 # define list_del_rcu            list_del
38 # define list_for_each_rcu       list_for_each
39 # define list_for_each_safe_rcu  list_for_each_safe
40 # define rcu_read_lock()         spin_lock(&bucket->lock)
41 # define rcu_read_unlock()       spin_unlock(&bucket->lock)
42 #endif /* ifndef HAVE_RCU */
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 static atomic_t handle_count = ATOMIC_INIT(0);
54
55 #define HANDLE_HASH_SIZE (1 << 14)
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, portals_handle_addref_cb cb)
63 {
64         struct handle_bucket *bucket;
65         ENTRY;
66
67         LASSERT(h != NULL);
68         LASSERT(list_empty(&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         h->h_cookie = handle_base;
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         spin_unlock(&handle_base_lock);
88  
89         atomic_inc(&handle_count);
90         h->h_addref = cb;
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 "LPX64" to hash\n",
100                h, h->h_cookie);
101         EXIT;
102 }
103
104 static void class_handle_unhash_nolock(struct portals_handle *h)
105 {
106         if (list_empty(&h->h_link)) {
107                 CERROR("removing an already-removed handle ("LPX64")\n",
108                        h->h_cookie);
109                 return;
110         }
111
112         CDEBUG(D_INFO, "removing object %p with handle "LPX64" from hash\n",
113                h, h->h_cookie);
114
115         spin_lock(&h->h_lock);
116         if (h->h_in == 0) {
117                 spin_unlock(&h->h_lock);
118                 return;
119         }
120         h->h_in = 0;
121         spin_unlock(&h->h_lock);
122         list_del_rcu(&h->h_link);
123 }
124
125 void class_handle_unhash(struct portals_handle *h)
126 {
127         struct handle_bucket *bucket;
128         bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
129
130         spin_lock(&bucket->lock);
131         class_handle_unhash_nolock(h);
132         spin_unlock(&bucket->lock);
133
134         atomic_dec(&handle_count);
135 }
136
137 void class_handle_hash_back(struct portals_handle *h)
138 {
139         struct handle_bucket *bucket;
140         ENTRY;
141
142         bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
143
144         atomic_inc(&handle_count);
145         spin_lock(&bucket->lock);
146         list_add_rcu(&h->h_link, &bucket->head);
147         h->h_in = 1;
148         spin_unlock(&bucket->lock);
149
150         EXIT;
151 }
152
153 void *class_handle2object(__u64 cookie)
154 {
155         struct handle_bucket *bucket;
156         struct list_head *tmp;
157         void *retval = NULL;
158         ENTRY;
159
160         LASSERT(handle_hash != NULL);
161
162         /* Be careful when you want to change this code. See the 
163          * rcu_read_lock() definition on top this file. - jxiong */
164         bucket = handle_hash + (cookie & HANDLE_HASH_MASK);
165
166         rcu_read_lock();
167         list_for_each_rcu(tmp, &bucket->head) {
168                 struct portals_handle *h;
169                 h = list_entry(tmp, struct portals_handle, h_link);
170                 if (h->h_cookie != cookie)
171                         continue;
172
173                 spin_lock(&h->h_lock);
174                 if (likely(h->h_cookie != 0)) {
175                         h->h_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
186 void class_handle_free_cb(struct rcu_head *rcu)
187 {
188         struct portals_handle *h = RCU2HANDLE(rcu);
189         if (h->h_free_cb) {
190                 h->h_free_cb(h->h_ptr, h->h_size);
191         } else {
192                 void *ptr = h->h_ptr;
193                 unsigned int size = h->h_size;
194                 OBD_FREE(ptr, size);
195         }
196 }
197
198 int class_handle_init(void)
199 {
200         struct handle_bucket *bucket;
201
202         LASSERT(handle_hash == NULL);
203
204         OBD_VMALLOC(handle_hash, sizeof(*bucket) * HANDLE_HASH_SIZE);
205         if (handle_hash == NULL)
206                 return -ENOMEM;
207
208         spin_lock_init(&handle_base_lock);
209         for (bucket = handle_hash + HANDLE_HASH_SIZE - 1; bucket >= handle_hash;
210              bucket--) {
211                 CFS_INIT_LIST_HEAD(&bucket->head);
212                 spin_lock_init(&bucket->lock);
213         }
214         ll_get_random_bytes(&handle_base, sizeof(handle_base));
215         LASSERT(handle_base != 0ULL);
216
217         return 0;
218 }
219
220 static void cleanup_all_handles(void)
221 {
222         int i;
223
224         for (i = 0; i < HANDLE_HASH_SIZE; i++) {
225                 struct list_head *tmp, *pos;
226                 spin_lock(&handle_hash[i].lock);
227                 list_for_each_safe_rcu(tmp, pos, &(handle_hash[i].head)) {
228                         struct portals_handle *h;
229                         h = list_entry(tmp, struct portals_handle, h_link);
230
231                         CERROR("force clean handle "LPX64" addr %p addref %p\n",
232                                h->h_cookie, h, h->h_addref);
233
234                         class_handle_unhash_nolock(h);
235                 }
236                 spin_unlock(&handle_hash[i].lock);
237         }
238 }
239
240 void class_handle_cleanup(void)
241 {
242         int count;
243         LASSERT(handle_hash != NULL);
244
245         count = atomic_read(&handle_count);
246         if (count != 0) {
247                 CERROR("handle_count at cleanup: %d\n", count);
248                 cleanup_all_handles();
249         }
250
251         OBD_VFREE(handle_hash, sizeof(*handle_hash) * HANDLE_HASH_SIZE);
252         handle_hash = NULL;
253
254         if (atomic_read(&handle_count))
255                 CERROR("leaked %d handles\n", atomic_read(&handle_count));
256 }