Whamcloud - gitweb
LU-1163 llite: never try to invalidate a dirty page
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  *
38  * lustre/obdclass/lustre_handles.c
39  *
40  * Author: Phil Schwan <phil@clusterfs.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_CLASS
44 #ifndef __KERNEL__
45 # include <liblustre.h>
46 #endif
47
48 #include <obd_support.h>
49 #include <lustre_handles.h>
50 #include <lustre_lib.h>
51
52 #if !defined(HAVE_RCU) || !defined(__KERNEL__)
53 # define list_add_rcu            cfs_list_add
54 # define list_del_rcu            cfs_list_del
55 # define list_for_each_rcu       cfs_list_for_each
56 # define list_for_each_safe_rcu  cfs_list_for_each_safe
57 # define list_for_each_entry_rcu cfs_list_for_each_entry
58 # define rcu_read_lock()         cfs_spin_lock(&bucket->lock)
59 # define rcu_read_unlock()       cfs_spin_unlock(&bucket->lock)
60 #endif /* ifndef HAVE_RCU */
61
62 static __u64 handle_base;
63 #define HANDLE_INCR 7
64 static cfs_spinlock_t handle_base_lock;
65
66 static struct handle_bucket {
67         cfs_spinlock_t  lock;
68         cfs_list_t      head;
69 } *handle_hash;
70
71 #ifdef __arch_um__
72 /* For unknown reason, UML uses kmalloc rather than vmalloc to allocate
73  * memory(OBD_VMALLOC). Therefore, we have to redefine the
74  * HANDLE_HASH_SIZE to make the hash heads don't exceed 128K.
75  */
76 #define HANDLE_HASH_SIZE 4096
77 #else
78 #define HANDLE_HASH_SIZE (1 << 16)
79 #endif /* ifdef __arch_um__ */
80
81 #define HANDLE_HASH_MASK (HANDLE_HASH_SIZE - 1)
82
83 /*
84  * Generate a unique 64bit cookie (hash) for a handle and insert it into
85  * global (per-node) hash-table.
86  */
87 void class_handle_hash(struct portals_handle *h, portals_handle_addref_cb cb)
88 {
89         struct handle_bucket *bucket;
90         ENTRY;
91
92         LASSERT(h != NULL);
93         LASSERT(cfs_list_empty(&h->h_link));
94
95         /*
96          * This is fast, but simplistic cookie generation algorithm, it will
97          * need a re-do at some point in the future for security.
98          */
99         cfs_spin_lock(&handle_base_lock);
100         handle_base += HANDLE_INCR;
101
102         h->h_cookie = handle_base;
103         if (unlikely(handle_base == 0)) {
104                 /*
105                  * Cookie of zero is "dangerous", because in many places it's
106                  * assumed that 0 means "unassigned" handle, not bound to any
107                  * object.
108                  */
109                 CWARN("The universe has been exhausted: cookie wrap-around.\n");
110                 handle_base += HANDLE_INCR;
111         }
112         cfs_spin_unlock(&handle_base_lock);
113  
114         h->h_addref = cb;
115         cfs_spin_lock_init(&h->h_lock);
116
117         bucket = &handle_hash[h->h_cookie & HANDLE_HASH_MASK];
118         cfs_spin_lock(&bucket->lock);
119         list_add_rcu(&h->h_link, &bucket->head);
120         h->h_in = 1;
121         cfs_spin_unlock(&bucket->lock);
122
123         CDEBUG(D_INFO, "added object %p with handle "LPX64" to hash\n",
124                h, h->h_cookie);
125         EXIT;
126 }
127
128 static void class_handle_unhash_nolock(struct portals_handle *h)
129 {
130         if (cfs_list_empty(&h->h_link)) {
131                 CERROR("removing an already-removed handle ("LPX64")\n",
132                        h->h_cookie);
133                 return;
134         }
135
136         CDEBUG(D_INFO, "removing object %p with handle "LPX64" from hash\n",
137                h, h->h_cookie);
138
139         cfs_spin_lock(&h->h_lock);
140         if (h->h_in == 0) {
141                 cfs_spin_unlock(&h->h_lock);
142                 return;
143         }
144         h->h_in = 0;
145         cfs_spin_unlock(&h->h_lock);
146         list_del_rcu(&h->h_link);
147 }
148
149 void class_handle_unhash(struct portals_handle *h)
150 {
151         struct handle_bucket *bucket;
152         bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
153
154         cfs_spin_lock(&bucket->lock);
155         class_handle_unhash_nolock(h);
156         cfs_spin_unlock(&bucket->lock);
157 }
158
159 void class_handle_hash_back(struct portals_handle *h)
160 {
161         struct handle_bucket *bucket;
162         ENTRY;
163
164         bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
165
166         cfs_spin_lock(&bucket->lock);
167         list_add_rcu(&h->h_link, &bucket->head);
168         h->h_in = 1;
169         cfs_spin_unlock(&bucket->lock);
170
171         EXIT;
172 }
173
174 void *class_handle2object(__u64 cookie)
175 {
176         struct handle_bucket *bucket;
177         struct portals_handle *h;
178         void *retval = NULL;
179         ENTRY;
180
181         LASSERT(handle_hash != NULL);
182
183         /* Be careful when you want to change this code. See the 
184          * rcu_read_lock() definition on top this file. - jxiong */
185         bucket = handle_hash + (cookie & HANDLE_HASH_MASK);
186
187         rcu_read_lock();
188         list_for_each_entry_rcu(h, &bucket->head, h_link) {
189                 if (h->h_cookie != cookie)
190                         continue;
191
192                 cfs_spin_lock(&h->h_lock);
193                 if (likely(h->h_in != 0)) {
194                         h->h_addref(h);
195                         retval = h;
196                 }
197                 cfs_spin_unlock(&h->h_lock);
198                 break;
199         }
200         rcu_read_unlock();
201
202         RETURN(retval);
203 }
204
205 void class_handle_free_cb(cfs_rcu_head_t *rcu)
206 {
207         struct portals_handle *h = RCU2HANDLE(rcu);
208         if (h->h_free_cb) {
209                 h->h_free_cb(h->h_ptr, h->h_size);
210         } else {
211                 void *ptr = h->h_ptr;
212                 unsigned int size = h->h_size;
213                 OBD_FREE(ptr, size);
214         }
215 }
216
217 int class_handle_init(void)
218 {
219         struct handle_bucket *bucket;
220         struct timeval tv;
221         int seed[2];
222
223         LASSERT(handle_hash == NULL);
224
225         OBD_ALLOC_LARGE(handle_hash, sizeof(*bucket) * HANDLE_HASH_SIZE);
226         if (handle_hash == NULL)
227                 return -ENOMEM;
228
229         cfs_spin_lock_init(&handle_base_lock);
230         for (bucket = handle_hash + HANDLE_HASH_SIZE - 1; bucket >= handle_hash;
231              bucket--) {
232                 CFS_INIT_LIST_HEAD(&bucket->head);
233                 cfs_spin_lock_init(&bucket->lock);
234         }
235
236         /** bug 21430: add randomness to the initial base */
237         cfs_get_random_bytes(seed, sizeof(seed));
238         cfs_gettimeofday(&tv);
239         cfs_srand(tv.tv_sec ^ seed[0], tv.tv_usec ^ seed[1]);
240
241         cfs_get_random_bytes(&handle_base, sizeof(handle_base));
242         LASSERT(handle_base != 0ULL);
243
244         return 0;
245 }
246
247 static int cleanup_all_handles(void)
248 {
249         int rc;
250         int i;
251
252         for (rc = i = 0; i < HANDLE_HASH_SIZE; i++) {
253                 struct portals_handle *h;
254
255                 cfs_spin_lock(&handle_hash[i].lock);
256                 list_for_each_entry_rcu(h, &(handle_hash[i].head), h_link) {
257                         CERROR("force clean handle "LPX64" addr %p addref %p\n",
258                                h->h_cookie, h, h->h_addref);
259
260                         class_handle_unhash_nolock(h);
261                         rc++;
262                 }
263                 cfs_spin_unlock(&handle_hash[i].lock);
264         }
265
266         return rc;
267 }
268
269 void class_handle_cleanup(void)
270 {
271         int count;
272         LASSERT(handle_hash != NULL);
273
274         count = cleanup_all_handles();
275
276         OBD_FREE_LARGE(handle_hash, sizeof(*handle_hash) * HANDLE_HASH_SIZE);
277         handle_hash = NULL;
278
279         if (count != 0)
280                 CERROR("handle_count at cleanup: %d\n", count);
281 }