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
34 spinlock_t handle_lock;
35 static __u64 handle_base;
36 #define HANDLE_INCR 7
37 static struct list_head *handle_hash = NULL;
38 static int handle_count = 0;
39
40 #define HANDLE_HASH_SIZE (1 << 14)
41 #define HANDLE_HASH_MASK (HANDLE_HASH_SIZE - 1)
42
43 /*
44  * Generate a unique 64bit cookie (hash) for a handle and insert it into
45  * global (per-node) hash-table.
46  */
47 void class_handle_hash(struct portals_handle *h, portals_handle_addref_cb cb)
48 {
49         struct list_head *bucket;
50         ENTRY;
51
52         LASSERT(h != NULL);
53         LASSERT(list_empty(&h->h_link));
54
55         spin_lock(&handle_lock);
56
57         /*
58          * This is fast, but simplistic cookie generation algorithm, it will
59          * need a re-do at some point in the future for security.
60          */
61         h->h_cookie = handle_base;
62         handle_base += HANDLE_INCR;
63
64         bucket = handle_hash + (h->h_cookie & HANDLE_HASH_MASK);
65         list_add(&h->h_link, bucket);
66         handle_count++;
67
68         if (unlikely(handle_base == 0)) {
69                 /*
70                  * Cookie of zero is "dangerous", because in many places it's
71                  * assumed that 0 means "unassigned" handle, not bound to any
72                  * object.
73                  */
74                 CWARN("The universe has been exhausted: cookie wrap-around.\n");
75                 handle_base += HANDLE_INCR;
76         }
77
78         spin_unlock(&handle_lock);
79
80         h->h_addref = cb;
81         CDEBUG(D_INFO, "added object %p with handle "LPX64" to hash\n",
82                h, h->h_cookie);
83         EXIT;
84 }
85
86 static void class_handle_unhash_nolock(struct portals_handle *h)
87 {
88         if (list_empty(&h->h_link)) {
89                 CERROR("removing an already-removed handle ("LPX64")\n",
90                        h->h_cookie);
91                 return;
92         }
93
94         CDEBUG(D_INFO, "removing object %p with handle "LPX64" from hash\n",
95                h, h->h_cookie);
96
97         handle_count--;
98         list_del_init(&h->h_link);
99 }
100
101 void class_handle_unhash(struct portals_handle *h)
102 {
103         spin_lock(&handle_lock);
104         class_handle_unhash_nolock(h);
105         spin_unlock(&handle_lock);
106 }
107
108 void *class_handle2object(__u64 cookie)
109 {
110         struct list_head *bucket, *tmp;
111         void *retval = NULL;
112         ENTRY;
113
114         LASSERT(handle_hash != NULL);
115
116         bucket = handle_hash + (cookie & HANDLE_HASH_MASK);
117
118         spin_lock(&handle_lock);
119         list_for_each(tmp, bucket) {
120                 struct portals_handle *h;
121                 h = list_entry(tmp, struct portals_handle, h_link);
122
123                 if (h->h_cookie == cookie) {
124                         h->h_addref(h);
125                         retval = h;
126                         break;
127                 }
128         }
129         spin_unlock(&handle_lock);
130
131         RETURN(retval);
132 }
133
134 int class_handle_init(void)
135 {
136         struct list_head *bucket;
137
138         LASSERT(handle_hash == NULL);
139
140         OBD_VMALLOC(handle_hash, sizeof(*handle_hash) * HANDLE_HASH_SIZE);
141         if (handle_hash == NULL)
142                 return -ENOMEM;
143
144         for (bucket = handle_hash + HANDLE_HASH_SIZE - 1; bucket >= handle_hash;
145              bucket--)
146                 CFS_INIT_LIST_HEAD(bucket);
147
148         get_random_bytes(&handle_base, sizeof(handle_base));
149         LASSERT(handle_base != 0ULL);
150
151         return 0;
152 }
153
154 static void cleanup_all_handles(void)
155 {
156         int i;
157
158         spin_lock(&handle_lock);
159         for (i = 0; i < HANDLE_HASH_SIZE; i++) {
160                 struct list_head *tmp, *pos;
161                 list_for_each_safe(tmp, pos, &(handle_hash[i])) {
162                         struct portals_handle *h;
163                         h = list_entry(tmp, struct portals_handle, h_link);
164
165                         CERROR("force clean handle "LPX64" addr %p addref %p\n",
166                                h->h_cookie, h, h->h_addref);
167
168                         class_handle_unhash_nolock(h);
169                 }
170         }
171         spin_unlock(&handle_lock);
172 }
173
174 void class_handle_cleanup(void)
175 {
176         LASSERT(handle_hash != NULL);
177
178         if (handle_count != 0) {
179                 CERROR("handle_count at cleanup: %d\n", handle_count);
180                 cleanup_all_handles();
181         }
182
183         OBD_VFREE(handle_hash, sizeof(*handle_hash) * HANDLE_HASH_SIZE);
184         handle_hash = NULL;
185
186         if (handle_count)
187                 CERROR("leaked %d handles\n", handle_count);
188 }