Whamcloud - gitweb
b=10706
[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(__KERNEL__) || (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
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
41 # ifndef __KERNEL__
42 #  define rcu_read_lock()
43 #  define rcu_read_unlock()
44 # else /* 2.4 kernels? */
45 #  undef rcu_read_lock
46 #  undef rcu_read_unlock
47 #  define rcu_read_lock()         spin_lock(&bucket->lock)
48 #  define rcu_read_unlock()       spin_unlock(&bucket->lock)
49 # endif /* ifndef __KERNEL__ */
50
51 #endif /* if !defined(__KERNEL__) || (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)) */
52
53
54 static __u64 handle_base;
55 #define HANDLE_INCR 7
56 static spinlock_t handle_base_lock;
57
58 static struct handle_bucket {
59         spinlock_t lock;
60         struct list_head head;
61 } *handle_hash;
62
63 static atomic_t handle_count = ATOMIC_INIT(0);
64
65 #define HANDLE_HASH_SIZE (1 << 14)
66 #define HANDLE_HASH_MASK (HANDLE_HASH_SIZE - 1)
67
68 /*
69  * Generate a unique 64bit cookie (hash) for a handle and insert it into
70  * global (per-node) hash-table.
71  */
72 void class_handle_hash(struct portals_handle *h, portals_handle_addref_cb cb)
73 {
74         struct handle_bucket *bucket;
75         ENTRY;
76
77         LASSERT(h != NULL);
78         LASSERT(list_empty(&h->h_link));
79
80         /*
81          * This is fast, but simplistic cookie generation algorithm, it will
82          * need a re-do at some point in the future for security.
83          */
84         spin_lock(&handle_base_lock);
85         handle_base += HANDLE_INCR;
86
87         h->h_cookie = handle_base;
88         if (unlikely(handle_base == 0)) {
89                 /*
90                  * Cookie of zero is "dangerous", because in many places it's
91                  * assumed that 0 means "unassigned" handle, not bound to any
92                  * object.
93                  */
94                 CWARN("The universe has been exhausted: cookie wrap-around.\n");
95                 handle_base += HANDLE_INCR;
96         }
97         spin_unlock(&handle_base_lock);
98
99         atomic_inc(&handle_count);
100         h->h_addref = cb;
101         spin_lock_init(&h->h_lock);
102
103         bucket = &handle_hash[h->h_cookie & HANDLE_HASH_MASK];
104         spin_lock(&bucket->lock);
105         list_add_rcu(&h->h_link, &bucket->head);
106         spin_unlock(&bucket->lock);
107
108         CDEBUG(D_INFO, "added object %p with handle "LPX64" to hash\n",
109                h, h->h_cookie);
110         EXIT;
111 }
112
113 static void class_handle_unhash_nolock(struct portals_handle *h)
114 {
115         if (list_empty(&h->h_link)) {
116                 CERROR("removing an already-removed handle ("LPX64")\n",
117                        h->h_cookie);
118                 return;
119         }
120
121         CDEBUG(D_INFO, "removing object %p with handle "LPX64" from hash\n",
122                h, h->h_cookie);
123
124         spin_lock(&h->h_lock);
125         if (h->h_cookie == 0) {
126                 spin_unlock(&h->h_lock);
127                 return;
128         }
129         h->h_cookie = 0;
130         spin_unlock(&h->h_lock);
131         list_del_rcu(&h->h_link);
132 }
133
134 void class_handle_unhash(struct portals_handle *h)
135 {
136         struct handle_bucket *bucket;
137         bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
138
139         spin_lock(&bucket->lock);
140         class_handle_unhash_nolock(h);
141         spin_unlock(&bucket->lock);
142
143         atomic_dec(&handle_count);
144 }
145
146 void *class_handle2object(__u64 cookie)
147 {
148         struct handle_bucket *bucket;
149         struct list_head *tmp;
150         void *retval = NULL;
151         ENTRY;
152
153         LASSERT(handle_hash != NULL);
154
155         /* Be careful when you want to change this code. See the 
156          * rcu_read_lock() definition on top this file. - jxiong */
157         bucket = handle_hash + (cookie & HANDLE_HASH_MASK);
158
159         rcu_read_lock();
160         list_for_each_rcu(tmp, &bucket->head) {
161                 struct portals_handle *h;
162                 h = list_entry(tmp, struct portals_handle, h_link);
163                 if (h->h_cookie != cookie)
164                         continue;
165
166                 spin_lock(&h->h_lock);
167                 if (likely(h->h_cookie != 0)) {
168                         h->h_addref(h);
169                         retval = h;
170                 }
171                 spin_unlock(&h->h_lock);
172                 break;
173         }
174         rcu_read_unlock();
175
176         RETURN(retval);
177 }
178
179 void class_handle_free_cb(struct rcu_head *rcu)
180 {
181         struct portals_handle *h = RCU2HANDLE(rcu);
182         if (h->h_free_cb) {
183                 h->h_free_cb(h->h_ptr, h->h_size);
184         } else {
185                 void *ptr = h->h_ptr;
186                 unsigned int size = h->h_size;
187                 OBD_FREE(ptr, size);
188         }
189 }
190
191
192 int class_handle_init(void)
193 {
194         struct handle_bucket *bucket;
195
196         LASSERT(handle_hash == NULL);
197
198         OBD_VMALLOC(handle_hash, sizeof(*bucket) * HANDLE_HASH_SIZE);
199         if (handle_hash == NULL)
200                 return -ENOMEM;
201
202         spin_lock_init(&handle_base_lock);
203         for (bucket = handle_hash + HANDLE_HASH_SIZE - 1; bucket >= handle_hash;
204              bucket--) {
205                 CFS_INIT_LIST_HEAD(&bucket->head);
206                 spin_lock_init(&bucket->lock);
207         }
208
209         ll_get_random_bytes(&handle_base, sizeof(handle_base));
210         LASSERT(handle_base != 0ULL);
211
212         return 0;
213 }
214
215 static void cleanup_all_handles(void)
216 {
217         int i;
218
219         for (i = 0; i < HANDLE_HASH_SIZE; i++) {
220                 struct list_head *tmp, *pos;
221                 spin_lock(&handle_hash[i].lock);
222                 list_for_each_safe_rcu(tmp, pos, &(handle_hash[i].head)) {
223                         struct portals_handle *h;
224                         h = list_entry(tmp, struct portals_handle, h_link);
225
226                         CERROR("force clean handle "LPX64" addr %p addref %p\n",
227                                h->h_cookie, h, h->h_addref);
228
229                         class_handle_unhash_nolock(h);
230                 }
231                 spin_unlock(&handle_hash[i].lock);
232         }
233 }
234
235 void class_handle_cleanup(void)
236 {
237         int count;
238         LASSERT(handle_hash != NULL);
239
240         count = atomic_read(&handle_count);
241         if (count != 0) {
242                 CERROR("handle_count at cleanup: %d\n", count);
243                 cleanup_all_handles();
244         }
245
246         OBD_VFREE(handle_hash, sizeof(*handle_hash) * HANDLE_HASH_SIZE);
247         handle_hash = NULL;
248
249         if (atomic_read(&handle_count))
250                 CERROR("leaked %d handles\n", atomic_read(&handle_count));
251 }