Whamcloud - gitweb
Tell us which locks are still outstanding at shutdown time.
[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 DEBUG_SUBSYSTEM S_LDLM
13
14 #include <linux/lustre_dlm.h>
15
16 kmem_cache_t *ldlm_resource_slab, *ldlm_lock_slab;
17
18 spinlock_t ldlm_namespace_lock = SPIN_LOCK_UNLOCKED;
19 struct list_head ldlm_namespace_list = LIST_HEAD_INIT(ldlm_namespace_list);
20 static struct proc_dir_entry *ldlm_ns_proc_dir = NULL;
21
22 int ldlm_proc_setup(struct obd_device *obd)
23 {
24         ENTRY;
25
26         if (obd->obd_proc_entry == NULL)
27                 RETURN(-EINVAL);
28
29         ldlm_ns_proc_dir = proc_mkdir("namespaces", obd->obd_proc_entry);
30         if (ldlm_ns_proc_dir == NULL) {
31                 CERROR("Couldn't create /proc/lustre/ldlm/namespaces\n");
32                 RETURN(-EPERM);
33         }
34         RETURN(0);
35 }
36
37 void ldlm_proc_cleanup(struct obd_device *obd)
38 {
39         proc_lustre_remove_obd_entry("namespaces", obd);
40 }
41
42 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 client)
43 {
44         struct ldlm_namespace *ns = NULL;
45         struct list_head *bucket;
46
47         OBD_ALLOC(ns, sizeof(*ns));
48         if (!ns) {
49                 LBUG();
50                 GOTO(out, NULL);
51         }
52
53         ns->ns_hash = vmalloc(sizeof(*ns->ns_hash) * RES_HASH_SIZE);
54         if (!ns->ns_hash) {
55                 LBUG();
56                 GOTO(out, ns);
57         }
58
59         OBD_ALLOC(ns->ns_name, strlen(name) + 1);
60         if (!ns->ns_name) {
61                 LBUG();
62                 GOTO(out, ns);
63         }
64         strcpy(ns->ns_name, name);
65
66         ptlrpc_init_client(NULL, NULL, LDLM_REQUEST_PORTAL, LDLM_REPLY_PORTAL,
67                            &ns->ns_rpc_client);
68
69         INIT_LIST_HEAD(&ns->ns_root_list);
70         l_lock_init(&ns->ns_lock);
71         ns->ns_refcount = 0;
72         ns->ns_client = client;
73
74         for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
75              bucket--)
76                 INIT_LIST_HEAD(bucket);
77
78         spin_lock(&ldlm_namespace_lock);
79         list_add(&ns->ns_list_chain, &ldlm_namespace_list);
80         ns->ns_proc_dir = proc_mkdir(ns->ns_name, ldlm_ns_proc_dir);
81         if (ns->ns_proc_dir == NULL)
82                 CERROR("Unable to create proc directory for namespace.\n");
83         spin_unlock(&ldlm_namespace_lock);
84
85         RETURN(ns);
86
87  out:
88         if (ns && ns->ns_hash)
89                 vfree(ns->ns_hash);
90         if (ns && ns->ns_name)
91                 OBD_FREE(ns->ns_name, strlen(name) + 1);
92         if (ns)
93                 OBD_FREE(ns, sizeof(*ns));
94         return NULL;
95 }
96
97 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
98
99 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q)
100 {
101         struct list_head *tmp, *pos;
102         int rc = 0, client = res->lr_namespace->ns_client;
103         ENTRY;
104
105         list_for_each_safe(tmp, pos, q) {
106                 struct ldlm_lock *lock;
107                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
108                 LDLM_LOCK_GET(lock);
109
110                 if (client) {
111                         struct lustre_handle lockh;
112                         ldlm_lock2handle(lock, &lockh);
113                         /* can we get away without a connh here? */
114                         rc = ldlm_cli_cancel(&lockh);
115                         if (rc != ELDLM_OK) {
116                                 /* It failed remotely, but we'll force it to
117                                  * cleanup locally. */
118                                 CERROR("ldlm_cli_cancel: %d\n", rc);
119                                 ldlm_lock_cancel(lock);
120                         }
121                 } else {
122                         CERROR("Freeing lock %p still held by client node.\n",
123                                lock);
124                         ldlm_lock_dump(lock);
125
126                         ldlm_resource_unlink_lock(lock);
127                         ldlm_lock_destroy(lock);
128                 }
129                 LDLM_LOCK_PUT(lock);
130         }
131 }
132
133 int ldlm_namespace_free(struct ldlm_namespace *ns)
134 {
135         struct list_head *tmp, *pos;
136         int i;
137
138         if (!ns)
139                 RETURN(ELDLM_OK);
140
141         spin_lock(&ldlm_namespace_lock);
142         list_del(&ns->ns_list_chain);
143         remove_proc_entry(ns->ns_name, ldlm_ns_proc_dir);
144         spin_unlock(&ldlm_namespace_lock);
145
146         l_lock(&ns->ns_lock);
147
148         for (i = 0; i < RES_HASH_SIZE; i++) {
149                 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
150                         struct ldlm_resource *res;
151                         res = list_entry(tmp, struct ldlm_resource, lr_hash);
152                         ldlm_resource_getref(res);
153
154                         cleanup_resource(res, &res->lr_granted);
155                         cleanup_resource(res, &res->lr_converting);
156                         cleanup_resource(res, &res->lr_waiting);
157
158                         if (!ldlm_resource_put(res)) {
159                                 CERROR("Resource refcount nonzero (%d) after "
160                                        "lock cleanup; forcing cleanup.\n",
161                                        atomic_read(&res->lr_refcount));
162                                 ldlm_resource_dump(res);
163                                 atomic_set(&res->lr_refcount, 1);
164                                 ldlm_resource_put(res);
165                         }
166                 }
167         }
168
169         vfree(ns->ns_hash /* , sizeof(struct list_head) * RES_HASH_SIZE */);
170         ptlrpc_cleanup_client(&ns->ns_rpc_client);
171         OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
172         OBD_FREE(ns, sizeof(*ns));
173
174         return ELDLM_OK;
175 }
176
177 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, __u64 *name)
178 {
179         __u32 hash = 0;
180         int i;
181
182         for (i = 0; i < RES_NAME_SIZE; i++)
183                 hash += name[i];
184
185         hash += (__u32)((unsigned long)parent >> 4);
186
187         return (hash & RES_HASH_MASK);
188 }
189
190 static struct ldlm_resource *ldlm_resource_new(void)
191 {
192         struct ldlm_resource *res;
193
194         res = kmem_cache_alloc(ldlm_resource_slab, SLAB_KERNEL);
195         if (res == NULL) {
196                 LBUG();
197                 return NULL;
198         }
199         memset(res, 0, sizeof(*res));
200
201         INIT_LIST_HEAD(&res->lr_children);
202         INIT_LIST_HEAD(&res->lr_childof);
203         INIT_LIST_HEAD(&res->lr_granted);
204         INIT_LIST_HEAD(&res->lr_converting);
205         INIT_LIST_HEAD(&res->lr_waiting);
206
207         atomic_set(&res->lr_refcount, 1);
208
209         return res;
210 }
211
212 /* Args: locked namespace
213  * Returns: newly-allocated, referenced, unlocked resource */
214 static struct ldlm_resource *ldlm_resource_add(struct ldlm_namespace *ns,
215                                                struct ldlm_resource *parent,
216                                                __u64 *name, __u32 type)
217 {
218         struct list_head *bucket;
219         struct ldlm_resource *res;
220         ENTRY;
221
222         if (type < LDLM_MIN_TYPE || type > LDLM_MAX_TYPE) {
223                 LBUG();
224                 RETURN(NULL);
225         }
226
227         res = ldlm_resource_new();
228         if (!res) {
229                 LBUG();
230                 RETURN(NULL);
231         }
232
233         memcpy(res->lr_name, name, sizeof(res->lr_name));
234         res->lr_namespace = ns;
235         ns->ns_refcount++;
236
237         res->lr_type = type;
238         res->lr_most_restr = LCK_NL;
239
240         bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
241         list_add(&res->lr_hash, bucket);
242
243         if (parent == NULL)
244                 list_add(&res->lr_childof, &ns->ns_root_list);
245         else {
246                 res->lr_parent = parent;
247                 list_add(&res->lr_childof, &parent->lr_children);
248         }
249
250         RETURN(res);
251 }
252
253 /* Args: unlocked namespace
254  * Locks: takes and releases ns->ns_lock and res->lr_lock
255  * Returns: referenced, unlocked ldlm_resource or NULL */
256 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
257                                         struct ldlm_resource *parent,
258                                         __u64 *name, __u32 type, int create)
259 {
260         struct list_head *bucket;
261         struct list_head *tmp = bucket;
262         struct ldlm_resource *res = NULL;
263         ENTRY;
264
265         if (ns == NULL || ns->ns_hash == NULL) {
266                 LBUG();
267                 RETURN(NULL);
268         }
269
270         l_lock(&ns->ns_lock);
271         bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
272
273         list_for_each(tmp, bucket) {
274                 struct ldlm_resource *chk;
275                 chk = list_entry(tmp, struct ldlm_resource, lr_hash);
276
277                 if (memcmp(chk->lr_name, name, sizeof(chk->lr_name)) == 0) {
278                         res = chk;
279                         atomic_inc(&res->lr_refcount);
280                         EXIT;
281                         break;
282                 }
283         }
284
285         if (res == NULL && create)
286                 res = ldlm_resource_add(ns, parent, name, type);
287         l_unlock(&ns->ns_lock);
288
289         RETURN(res);
290 }
291
292 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
293 {
294         atomic_inc(&res->lr_refcount);
295         return res;
296 }
297
298 /* Returns 1 if the resource was freed, 0 if it remains. */
299 int ldlm_resource_put(struct ldlm_resource *res)
300 {
301         int rc = 0;
302
303         if (atomic_dec_and_test(&res->lr_refcount)) {
304                 struct ldlm_namespace *ns = res->lr_namespace;
305                 ENTRY;
306
307                 l_lock(&ns->ns_lock);
308
309                 if (atomic_read(&res->lr_refcount) != 0) {
310                         /* We lost the race. */
311                         l_unlock(&ns->ns_lock);
312                         goto out;
313                 }
314
315                 if (!list_empty(&res->lr_granted))
316                         LBUG();
317
318                 if (!list_empty(&res->lr_converting))
319                         LBUG();
320
321                 if (!list_empty(&res->lr_waiting))
322                         LBUG();
323
324                 if (!list_empty(&res->lr_children))
325                         LBUG();
326
327                 ns->ns_refcount--;
328                 list_del(&res->lr_hash);
329                 list_del(&res->lr_childof);
330
331                 kmem_cache_free(ldlm_resource_slab, res);
332                 l_unlock(&ns->ns_lock);
333                 rc = 1;
334         } else {
335                 ENTRY;
336         out:
337                 if (atomic_read(&res->lr_refcount) < 0)
338                         LBUG();
339         }
340
341         RETURN(rc);
342 }
343
344 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
345                             struct ldlm_lock *lock)
346 {
347         l_lock(&res->lr_namespace->ns_lock);
348
349         ldlm_resource_dump(res);
350         ldlm_lock_dump(lock);
351
352         if (!list_empty(&lock->l_res_link))
353                 LBUG();
354
355         list_add(&lock->l_res_link, head);
356         l_unlock(&res->lr_namespace->ns_lock);
357 }
358
359 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
360 {
361         l_lock(&lock->l_resource->lr_namespace->ns_lock);
362         list_del_init(&lock->l_res_link);
363         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
364 }
365
366 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
367 {
368         desc->lr_type = res->lr_type;
369         memcpy(desc->lr_name, res->lr_name, sizeof(desc->lr_name));
370         memcpy(desc->lr_version, res->lr_version, sizeof(desc->lr_version));
371 }
372
373 void ldlm_dump_all_namespaces(void)
374 {
375         struct list_head *tmp;
376
377         spin_lock(&ldlm_namespace_lock);
378
379         list_for_each(tmp, &ldlm_namespace_list) {
380                 struct ldlm_namespace *ns;
381                 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
382                 ldlm_namespace_dump(ns);
383         }
384
385         spin_unlock(&ldlm_namespace_lock);
386 }
387
388 void ldlm_namespace_dump(struct ldlm_namespace *ns)
389 {
390         struct list_head *tmp;
391
392         l_lock(&ns->ns_lock);
393         CDEBUG(D_OTHER, "--- Namespace: %s (rc: %d, client: %d)\n", ns->ns_name,
394                ns->ns_refcount, ns->ns_client);
395
396         list_for_each(tmp, &ns->ns_root_list) {
397                 struct ldlm_resource *res;
398                 res = list_entry(tmp, struct ldlm_resource, lr_childof);
399
400                 /* Once we have resources with children, this should really dump
401                  * them recursively. */
402                 ldlm_resource_dump(res);
403         }
404         l_unlock(&ns->ns_lock);
405 }
406
407 void ldlm_resource_dump(struct ldlm_resource *res)
408 {
409         struct list_head *tmp;
410         char name[256];
411
412         if (RES_NAME_SIZE != 3)
413                 LBUG();
414
415         snprintf(name, sizeof(name), "%Lx %Lx %Lx",
416                  (unsigned long long)res->lr_name[0],
417                  (unsigned long long)res->lr_name[1],
418                  (unsigned long long)res->lr_name[2]);
419
420         CDEBUG(D_OTHER, "--- Resource: %p (%s) (rc: %d)\n", res, name,
421                atomic_read(&res->lr_refcount));
422         CDEBUG(D_OTHER, "Namespace: %p (%s)\n", res->lr_namespace,
423                res->lr_namespace->ns_name);
424         CDEBUG(D_OTHER, "Parent: %p, root: %p\n", res->lr_parent, res->lr_root);
425
426         CDEBUG(D_OTHER, "Granted locks:\n");
427         list_for_each(tmp, &res->lr_granted) {
428                 struct ldlm_lock *lock;
429                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
430                 ldlm_lock_dump(lock);
431         }
432
433         CDEBUG(D_OTHER, "Converting locks:\n");
434         list_for_each(tmp, &res->lr_converting) {
435                 struct ldlm_lock *lock;
436                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
437                 ldlm_lock_dump(lock);
438         }
439
440         CDEBUG(D_OTHER, "Waiting locks:\n");
441         list_for_each(tmp, &res->lr_waiting) {
442                 struct ldlm_lock *lock;
443                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
444                 ldlm_lock_dump(lock);
445         }
446 }