Whamcloud - gitweb
Vmalloc ns_hash instead of kmalloc (it is 128kB). This appears to have
[fs/lustre-release.git] / lustre / ldlm / ldlm_resource.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  *
6  * This code is issued under the GNU General Public License.
7  * See the file COPYING in this distribution
8  *
9  * by Cluster File Systems, Inc.
10  */
11
12 #define EXPORT_SYMTAB
13 #define DEBUG_SUBSYSTEM S_LDLM
14
15 #include <linux/lustre_dlm.h>
16
17 kmem_cache_t *ldlm_resource_slab, *ldlm_lock_slab;
18
19 struct ldlm_namespace *ldlm_namespace_new(struct obd_device *obddev,
20                                           __u32 local)
21 {
22         struct ldlm_namespace *ns;
23         struct list_head *bucket;
24
25         OBD_ALLOC(ns, sizeof(*ns));
26         if (!ns) {
27                 LBUG();
28                 RETURN(NULL);
29         }
30         ns->ns_hash = vmalloc(sizeof(*ns->ns_hash) * RES_HASH_SIZE);
31         if (!ns->ns_hash) {
32                 OBD_FREE(ns, sizeof(*ns));
33                 LBUG();
34                 RETURN(NULL);
35         }
36
37         ns->ns_obddev = obddev;
38         INIT_LIST_HEAD(&ns->ns_root_list);
39         ns->ns_lock = SPIN_LOCK_UNLOCKED;
40         ns->ns_refcount = 0;
41         ns->ns_local = local;
42
43         for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
44              bucket--)
45                 INIT_LIST_HEAD(bucket);
46
47         return ns;
48 }
49
50 static int cleanup_resource(struct ldlm_resource *res, struct list_head *q)
51 {
52         struct list_head *tmp, *pos;
53         int rc = 0;
54
55         list_for_each_safe(tmp, pos, q) {
56                 struct ldlm_lock *lock;
57
58                 if (rc) {
59                         /* Res was already cleaned up. */
60                         LBUG();
61                 }
62
63                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
64                 spin_lock(&lock->l_lock);
65                 ldlm_resource_del_lock(lock);
66                 ldlm_lock_free(lock);
67
68                 rc = ldlm_resource_put(res);
69         }
70
71         return rc;
72 }
73
74 int ldlm_namespace_free(struct ldlm_namespace *ns)
75 {
76         struct list_head *tmp, *pos;
77         int i, rc;
78
79         /* We should probably take the ns_lock, but then ldlm_resource_put
80          * couldn't take it.  Hmm. */
81         for (i = 0; i < RES_HASH_SIZE; i++) {
82                 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
83                         struct ldlm_resource *res;
84                         res = list_entry(tmp, struct ldlm_resource, lr_hash);
85
86                         spin_lock(&res->lr_lock);
87                         rc = cleanup_resource(res, &res->lr_granted);
88                         if (!rc)
89                                 rc = cleanup_resource(res, &res->lr_converting);
90                         if (!rc)
91                                 rc = cleanup_resource(res, &res->lr_waiting);
92
93                         if (rc == 0) {
94                                 CERROR("Resource refcount nonzero after lock "
95                                        "cleanup; forcing cleanup.\n");
96                                 res->lr_refcount = 1;
97                                 rc = ldlm_resource_put(res);
98                         }
99                 }
100         }
101
102         vfree(ns->ns_hash /* , sizeof(struct list_head) * RES_HASH_SIZE */);
103         OBD_FREE(ns, sizeof(*ns));
104
105         return ELDLM_OK;
106 }
107
108 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, __u64 *name)
109 {
110         __u32 hash = 0;
111         int i;
112
113         for (i = 0; i < RES_NAME_SIZE; i++)
114                 hash += name[i];
115
116         hash += (__u32)((unsigned long)parent >> 4);
117
118         return (hash & RES_HASH_MASK);
119 }
120
121 static struct ldlm_resource *ldlm_resource_new(void)
122 {
123         struct ldlm_resource *res;
124
125         res = kmem_cache_alloc(ldlm_resource_slab, SLAB_KERNEL);
126         if (res == NULL) {
127                 LBUG();
128                 return NULL;
129         }
130         memset(res, 0, sizeof(*res));
131
132         INIT_LIST_HEAD(&res->lr_children);
133         INIT_LIST_HEAD(&res->lr_childof);
134         INIT_LIST_HEAD(&res->lr_granted);
135         INIT_LIST_HEAD(&res->lr_converting);
136         INIT_LIST_HEAD(&res->lr_waiting);
137
138         res->lr_lock = SPIN_LOCK_UNLOCKED;
139         res->lr_refcount = 1;
140
141         return res;
142 }
143
144 /* Args: locked namespace
145  * Returns: newly-allocated, referenced, unlocked resource */
146 static struct ldlm_resource *ldlm_resource_add(struct ldlm_namespace *ns,
147                                                struct ldlm_resource *parent,
148                                                __u64 *name, __u32 type)
149 {
150         struct list_head *bucket;
151         struct ldlm_resource *res;
152         ENTRY;
153
154         if (type < 0 || type > LDLM_MAX_TYPE) {
155                 LBUG();
156                 RETURN(NULL);
157         }
158
159         res = ldlm_resource_new();
160         if (!res) {
161                 LBUG();
162                 RETURN(NULL);
163         }
164
165         memcpy(res->lr_name, name, sizeof(res->lr_name));
166         res->lr_namespace = ns;
167         ns->ns_refcount++;
168
169         res->lr_type = type; 
170         res->lr_most_restr = LCK_NL;
171
172         bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
173         list_add(&res->lr_hash, bucket);
174
175         if (parent == NULL) {
176                 res->lr_parent = res;
177                 list_add(&res->lr_rootlink, &ns->ns_root_list);
178         } else {
179                 res->lr_parent = parent;
180                 list_add(&res->lr_childof, &parent->lr_children);
181         }
182
183         RETURN(res);
184 }
185
186 /* Args: unlocked namespace
187  * Locks: takes and releases ns->ns_lock and res->lr_lock
188  * Returns: referenced, unlocked ldlm_resource or NULL */
189 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
190                                         struct ldlm_resource *parent,
191                                         __u64 *name, __u32 type, int create)
192 {
193         struct list_head *bucket;
194         struct list_head *tmp = bucket;
195         struct ldlm_resource *res = NULL;
196
197         ENTRY;
198
199         if (ns->ns_hash == NULL)
200                 RETURN(NULL);
201
202         spin_lock(&ns->ns_lock);
203         bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
204
205         list_for_each(tmp, bucket) {
206                 struct ldlm_resource *chk;
207                 chk = list_entry(tmp, struct ldlm_resource, lr_hash);
208
209                 if (memcmp(chk->lr_name, name, sizeof(chk->lr_name)) == 0) {
210                         res = chk;
211                         spin_lock(&res->lr_lock);
212                         res->lr_refcount++;
213                         spin_unlock(&res->lr_lock);
214                         EXIT;
215                         break;
216                 }
217         }
218
219         if (res == NULL && create)
220                 res = ldlm_resource_add(ns, parent, name, type);
221         spin_unlock(&ns->ns_lock);
222
223         RETURN(res);
224 }
225
226 /* Args: locked resource
227  * Locks: takes and releases res->lr_lock
228  *        takes and releases ns->ns_lock iff res->lr_refcount falls to 0
229  */
230 int ldlm_resource_put(struct ldlm_resource *res)
231 {
232         int rc = 0;
233
234         if (res->lr_refcount == 1) {
235                 struct ldlm_namespace *ns = res->lr_namespace;
236                 ENTRY;
237
238                 spin_unlock(&res->lr_lock);
239                 spin_lock(&ns->ns_lock);
240                 spin_lock(&res->lr_lock);
241
242                 if (res->lr_refcount != 1) {
243                         spin_unlock(&ns->ns_lock);
244                         goto out;
245                 }
246
247                 if (!list_empty(&res->lr_granted))
248                         LBUG();
249
250                 if (!list_empty(&res->lr_converting))
251                         LBUG();
252
253                 if (!list_empty(&res->lr_waiting))
254                         LBUG();
255
256                 if (!list_empty(&res->lr_children))
257                         LBUG();
258
259                 ns->ns_refcount--;
260                 list_del(&res->lr_hash);
261                 list_del(&res->lr_rootlink);
262                 list_del(&res->lr_childof);
263
264                 kmem_cache_free(ldlm_resource_slab, res);
265                 spin_unlock(&ns->ns_lock);
266                 rc = 1;
267         } else {
268                 ENTRY;
269         out:
270                 res->lr_refcount--;
271                 if (res->lr_refcount < 0)
272                         LBUG();
273         }
274
275         RETURN(rc); 
276 }
277
278 /* Must be called with resource->lr_lock taken */
279 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
280                             struct ldlm_lock *lock)
281 {
282         list_add(&lock->l_res_link, head);
283         res->lr_refcount++;
284 }
285
286 /* Must be called with resource->lr_lock taken */
287 void ldlm_resource_del_lock(struct ldlm_lock *lock)
288 {
289         list_del_init(&lock->l_res_link);
290         lock->l_resource->lr_refcount--;
291 }
292
293 int ldlm_get_resource_handle(struct ldlm_resource *res, struct ldlm_handle *h)
294 {
295         LBUG();
296         return 0;
297 }
298
299 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
300 {
301         desc->lr_type = res->lr_type;
302         memcpy(desc->lr_name, res->lr_name, sizeof(desc->lr_name));
303         memcpy(desc->lr_version, res->lr_version, sizeof(desc->lr_version));
304 }
305
306 void ldlm_resource_dump(struct ldlm_resource *res)
307 {
308         struct list_head *tmp;
309         char name[256];
310
311         if (RES_NAME_SIZE != 3)
312                 LBUG();
313
314         snprintf(name, sizeof(name), "%Lx %Lx %Lx",
315                  (unsigned long long)res->lr_name[0],
316                  (unsigned long long)res->lr_name[1],
317                  (unsigned long long)res->lr_name[2]);
318
319         CDEBUG(D_OTHER, "--- Resource: %p (%s)\n", res, name);
320         CDEBUG(D_OTHER, "Namespace: %p\n", res->lr_namespace);
321         CDEBUG(D_OTHER, "Parent: %p, root: %p\n", res->lr_parent, res->lr_root);
322
323         CDEBUG(D_OTHER, "Granted locks:\n");
324         list_for_each(tmp, &res->lr_granted) {
325                 struct ldlm_lock *lock;
326                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
327                 ldlm_lock_dump(lock);
328         }
329
330         CDEBUG(D_OTHER, "Converting locks:\n");
331         list_for_each(tmp, &res->lr_converting) {
332                 struct ldlm_lock *lock;
333                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
334                 ldlm_lock_dump(lock);
335         }
336
337         CDEBUG(D_OTHER, "Waiting locks:\n");
338         list_for_each(tmp, &res->lr_waiting) {
339                 struct ldlm_lock *lock;
340                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
341                 ldlm_lock_dump(lock);
342         }
343 }