Whamcloud - gitweb
Branch b1_4
[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, 2003 Cluster File Systems, Inc.
5  *   Author: Phil Schwan <phil@clusterfs.com>
6  *   Author: Peter Braam <braam@clusterfs.com>
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #define DEBUG_SUBSYSTEM S_LDLM
25 #ifdef __KERNEL__
26 # include <linux/lustre_dlm.h>
27 #else
28 # include <liblustre.h>
29 #endif
30
31 #include <linux/obd_class.h>
32 #include "ldlm_internal.h"
33
34 kmem_cache_t *ldlm_resource_slab, *ldlm_lock_slab;
35
36 DECLARE_MUTEX(ldlm_namespace_lock);
37 struct list_head ldlm_namespace_list = LIST_HEAD_INIT(ldlm_namespace_list);
38 struct proc_dir_entry *ldlm_type_proc_dir = NULL;
39 struct proc_dir_entry *ldlm_ns_proc_dir = NULL;
40 struct proc_dir_entry *ldlm_svc_proc_dir = NULL;
41
42 #ifdef LPROCFS
43 static int ldlm_proc_dump_ns(struct file *file, const char *buffer,
44                              unsigned long count, void *data)
45 {
46         ldlm_dump_all_namespaces(D_DLMTRACE);
47         RETURN(count);
48 }
49
50 int ldlm_proc_setup(void)
51 {
52         int rc;
53         struct lprocfs_vars list[] = {
54                 { "dump_namespaces", NULL, ldlm_proc_dump_ns, NULL },
55                 { NULL }};
56         ENTRY;
57         LASSERT(ldlm_ns_proc_dir == NULL);
58
59         ldlm_type_proc_dir = lprocfs_register(OBD_LDLM_DEVICENAME,
60                                               proc_lustre_root,
61                                               NULL, NULL);
62         if (IS_ERR(ldlm_type_proc_dir)) {
63                 CERROR("LProcFS failed in ldlm-init\n");
64                 rc = PTR_ERR(ldlm_type_proc_dir);
65                 GOTO(err, rc);
66         }
67
68         ldlm_ns_proc_dir = lprocfs_register("namespaces",
69                                             ldlm_type_proc_dir,
70                                             NULL, NULL);
71         if (IS_ERR(ldlm_ns_proc_dir)) {
72                 CERROR("LProcFS failed in ldlm-init\n");
73                 rc = PTR_ERR(ldlm_ns_proc_dir);
74                 GOTO(err_type, rc);
75         }
76
77         ldlm_svc_proc_dir = lprocfs_register("services",
78                                             ldlm_type_proc_dir,
79                                             NULL, NULL);
80         if (IS_ERR(ldlm_svc_proc_dir)) {
81                 CERROR("LProcFS failed in ldlm-init\n");
82                 rc = PTR_ERR(ldlm_svc_proc_dir);
83                 GOTO(err_ns, rc);
84         }
85
86         rc = lprocfs_add_vars(ldlm_type_proc_dir, list, NULL);
87
88         RETURN(0);
89
90 err_ns:
91         lprocfs_remove(ldlm_ns_proc_dir);
92 err_type:
93         lprocfs_remove(ldlm_type_proc_dir);
94 err:
95         ldlm_type_proc_dir = NULL;
96         ldlm_ns_proc_dir = NULL;
97         ldlm_svc_proc_dir = NULL;
98         RETURN(rc);
99 }
100
101 void ldlm_proc_cleanup(void)
102 {
103         if (ldlm_svc_proc_dir) {
104                 lprocfs_remove(ldlm_svc_proc_dir);
105                 ldlm_svc_proc_dir = NULL;
106         }
107
108         if (ldlm_ns_proc_dir) {
109                 lprocfs_remove(ldlm_ns_proc_dir);
110                 ldlm_ns_proc_dir = NULL;
111         }
112
113         if (ldlm_type_proc_dir) {
114                 lprocfs_remove(ldlm_type_proc_dir);
115                 ldlm_type_proc_dir = NULL;
116         }
117 }
118
119 static int lprocfs_uint_rd(char *page, char **start, off_t off,
120                            int count, int *eof, void *data)
121 {
122         unsigned int *temp = (unsigned int *)data;
123         return snprintf(page, count, "%u\n", *temp);
124 }
125
126 static int lprocfs_read_lru_size(char *page, char **start, off_t off,
127                                  int count, int *eof, void *data)
128 {
129         struct ldlm_namespace *ns = data;
130         return lprocfs_uint_rd(page, start, off, count, eof,
131                                &ns->ns_max_unused);
132 }
133
134 #define MAX_STRING_SIZE 128
135 static int lprocfs_write_lru_size(struct file *file, const char *buffer,
136                                   unsigned long count, void *data)
137 {
138         struct ldlm_namespace *ns = data;
139         char dummy[MAX_STRING_SIZE + 1], *end;
140         unsigned long tmp;
141
142         dummy[MAX_STRING_SIZE] = '\0';
143         copy_from_user(dummy, buffer, MAX_STRING_SIZE);
144
145         if (count == 6 && memcmp(dummy, "clear", 5) == 0) {
146                 CDEBUG(D_DLMTRACE,
147                        "dropping all unused locks from namespace %s\n",
148                        ns->ns_name);
149                 tmp = ns->ns_max_unused;
150                 ns->ns_max_unused = 0;
151                 ldlm_cancel_lru(ns, LDLM_SYNC);
152                 ns->ns_max_unused = tmp;
153                 return count;
154         }
155
156         tmp = simple_strtoul(dummy, &end, 0);
157         if (tmp == 0 && *end) {
158                 CERROR("invalid value written\n");
159                 return -EINVAL;
160         }
161
162         CDEBUG(D_DLMTRACE, "changing namespace %s max_unused from %u to %u\n",
163                ns->ns_name, ns->ns_max_unused, (unsigned int)tmp);
164         ns->ns_max_unused = (unsigned int)tmp;
165
166         ldlm_cancel_lru(ns, LDLM_ASYNC);
167
168         return count;
169 }
170
171 void ldlm_proc_namespace(struct ldlm_namespace *ns)
172 {
173         struct lprocfs_vars lock_vars[2];
174         char lock_name[MAX_STRING_SIZE + 1];
175
176         LASSERT(ns != NULL);
177         LASSERT(ns->ns_name != NULL);
178
179         lock_name[MAX_STRING_SIZE] = '\0';
180
181         memset(lock_vars, 0, sizeof(lock_vars));
182         lock_vars[0].name = lock_name;
183
184         snprintf(lock_name, MAX_STRING_SIZE, "%s/resource_count", ns->ns_name);
185         lock_vars[0].data = &ns->ns_refcount;
186         lock_vars[0].read_fptr = lprocfs_rd_atomic;
187         lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
188
189         snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_count", ns->ns_name);
190         lock_vars[0].data = &ns->ns_locks;
191         lock_vars[0].read_fptr = lprocfs_rd_u64;
192         lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
193
194         if (ns->ns_client) {
195                 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_unused_count",
196                          ns->ns_name);
197                 lock_vars[0].data = &ns->ns_nr_unused;
198                 lock_vars[0].read_fptr = lprocfs_uint_rd;
199                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
200
201                 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_size",
202                          ns->ns_name);
203                 lock_vars[0].data = ns;
204                 lock_vars[0].read_fptr = lprocfs_read_lru_size;
205                 lock_vars[0].write_fptr = lprocfs_write_lru_size;
206                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
207         }
208 }
209 #undef MAX_STRING_SIZE
210 #else
211 #define ldlm_proc_namespace(ns) do {} while (0)
212 #endif /* LPROCFS */
213
214 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 client)
215 {
216         struct ldlm_namespace *ns = NULL;
217         struct list_head *bucket;
218         int rc;
219         ENTRY;
220
221         rc = ldlm_get_ref();
222         if (rc) {
223                 CERROR("ldlm_get_ref failed: %d\n", rc);
224                 RETURN(NULL);
225         }
226
227         OBD_ALLOC(ns, sizeof(*ns));
228         if (!ns)
229                 GOTO(out_ref, NULL);
230
231         OBD_VMALLOC(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
232         if (!ns->ns_hash)
233                 GOTO(out_ns, NULL);
234
235         OBD_ALLOC(ns->ns_name, strlen(name) + 1);
236         if (!ns->ns_name)
237                 GOTO(out_hash, NULL);
238
239         strcpy(ns->ns_name, name);
240
241         INIT_LIST_HEAD(&ns->ns_root_list);
242         l_lock_init(&ns->ns_lock);
243         init_waitqueue_head(&ns->ns_refcount_waitq);
244         atomic_set(&ns->ns_refcount, 0);
245         ns->ns_client = client;
246         spin_lock_init(&ns->ns_counter_lock);
247         ns->ns_locks = 0;
248
249         for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
250              bucket--)
251                 INIT_LIST_HEAD(bucket);
252
253         INIT_LIST_HEAD(&ns->ns_unused_list);
254         ns->ns_nr_unused = 0;
255         ns->ns_max_unused = LDLM_DEFAULT_LRU_SIZE;
256
257         down(&ldlm_namespace_lock);
258         list_add(&ns->ns_list_chain, &ldlm_namespace_list);
259         up(&ldlm_namespace_lock);
260         ldlm_proc_namespace(ns);
261         RETURN(ns);
262
263 out_hash:
264         POISON(ns->ns_hash, 0x5a, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
265         OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
266 out_ns:
267         OBD_FREE(ns, sizeof(*ns));
268 out_ref:
269         ldlm_put_ref(0);
270         RETURN(NULL);
271 }
272
273 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
274
275 /* If flags contains FL_LOCAL_ONLY, don't try to tell the server, just cleanup.
276  * This is currently only used for recovery, and we make certain assumptions
277  * as a result--notably, that we shouldn't cancel locks with refs. -phil
278  *
279  * Called with the ns_lock held. */
280 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
281                              int flags)
282 {
283         struct list_head *tmp, *pos;
284         int rc = 0, client = res->lr_namespace->ns_client;
285         int local_only = (flags & LDLM_FL_LOCAL_ONLY);
286         ENTRY;
287
288         list_for_each_safe(tmp, pos, q) {
289                 struct ldlm_lock *lock;
290                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
291                 LDLM_LOCK_GET(lock);
292
293                 /* Set CBPENDING so nothing in the cancellation path
294                  * can match this lock */
295                 lock->l_flags |= LDLM_FL_CBPENDING;
296                 lock->l_flags |= LDLM_FL_FAILED;
297                 lock->l_flags |= flags;
298
299                 if (local_only && (lock->l_readers || lock->l_writers)) {
300                         /* This is a little bit gross, but much better than the
301                          * alternative: pretend that we got a blocking AST from
302                          * the server, so that when the lock is decref'd, it
303                          * will go away ... */
304                         /* ... without sending a CANCEL message. */
305                         lock->l_flags |= LDLM_FL_LOCAL_ONLY;
306                         LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
307                         if (lock->l_completion_ast)
308                                 lock->l_completion_ast(lock, 0, NULL);
309                         LDLM_LOCK_PUT(lock);
310                         continue;
311                 }
312
313                 if (client) {
314                         struct lustre_handle lockh;
315                         ldlm_lock2handle(lock, &lockh);
316                         if (!local_only) {
317                                 rc = ldlm_cli_cancel(&lockh);
318                                 if (rc)
319                                         CERROR("ldlm_cli_cancel: %d\n", rc);
320                         }
321                         /* Force local cleanup on errors, too. */
322                         if (local_only || rc != ELDLM_OK)
323                                 ldlm_lock_cancel(lock);
324                 } else {
325                         LDLM_DEBUG(lock, "Freeing a lock still held by a "
326                                    "client node");
327
328                         ldlm_resource_unlink_lock(lock);
329                         ldlm_lock_destroy(lock);
330                 }
331                 LDLM_LOCK_PUT(lock);
332         }
333         EXIT;
334 }
335
336 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int flags)
337 {
338         int i;
339
340         if (ns == NULL) {
341                 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
342                 return ELDLM_OK;
343         }
344
345         l_lock(&ns->ns_lock);
346         for (i = 0; i < RES_HASH_SIZE; i++) {
347                 struct list_head *tmp, *pos;
348                 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
349                         struct ldlm_resource *res;
350                         res = list_entry(tmp, struct ldlm_resource, lr_hash);
351                         ldlm_resource_getref(res);
352
353                         cleanup_resource(res, &res->lr_granted, flags);
354                         cleanup_resource(res, &res->lr_converting, flags);
355                         cleanup_resource(res, &res->lr_waiting, flags);
356
357                         if (!ldlm_resource_putref(res)) {
358                                 CERROR("Namespace %s resource refcount %d "
359                                        "after lock cleanup\n",
360                                        ns->ns_name,
361                                        atomic_read(&res->lr_refcount));
362                         }
363                 }
364         }
365         l_unlock(&ns->ns_lock);
366
367         return ELDLM_OK;
368 }
369
370 /* Cleanup, but also free, the namespace */
371 int ldlm_namespace_free(struct ldlm_namespace *ns, int force)
372 {
373         if (!ns)
374                 RETURN(ELDLM_OK);
375
376         down(&ldlm_namespace_lock);
377         list_del(&ns->ns_list_chain);
378         up(&ldlm_namespace_lock);
379
380         /* At shutdown time, don't call the cancellation callback */
381         ldlm_namespace_cleanup(ns, 0);
382
383 #ifdef LPROCFS
384         {
385                 struct proc_dir_entry *dir;
386                 dir = lprocfs_srch(ldlm_ns_proc_dir, ns->ns_name);
387                 if (dir == NULL) {
388                         CERROR("dlm namespace %s has no procfs dir?\n",
389                                ns->ns_name);
390                 } else {
391                         lprocfs_remove(dir);
392                 }
393         }
394 #endif
395
396         if (atomic_read(&ns->ns_refcount) > 0) {
397                 struct l_wait_info lwi = LWI_INTR(NULL, NULL);
398                 int rc;
399                 CDEBUG(D_DLMTRACE,
400                        "dlm namespace %s free waiting on refcount %d\n",
401                        ns->ns_name, atomic_read(&ns->ns_refcount));
402                 rc = l_wait_event(ns->ns_refcount_waitq,
403                                   atomic_read(&ns->ns_refcount) == 0, &lwi);
404                 if (atomic_read(&ns->ns_refcount)) {
405                         LCONSOLE_ERROR("Lock manager: wait for %s namespace "
406                                        "cleanup aborted with %d resources in "
407                                        "use. (%d)\nI'm going to try to clean "
408                                        "up anyway, but I might need a reboot "
409                                        "of this node.\n", ns->ns_name,
410                                        atomic_read(&ns->ns_refcount), rc);
411                 }
412                 CDEBUG(D_DLMTRACE,
413                        "dlm namespace %s free done waiting\n", ns->ns_name);
414         }
415
416         POISON(ns->ns_hash, 0x5a, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
417         OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
418         OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
419         OBD_FREE(ns, sizeof(*ns));
420
421         ldlm_put_ref(force);
422
423         return ELDLM_OK;
424 }
425
426 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, struct ldlm_res_id name)
427 {
428         __u32 hash = 0;
429         int i;
430
431         for (i = 0; i < RES_NAME_SIZE; i++)
432                 hash += name.name[i];
433
434         hash += (__u32)((unsigned long)parent >> 4);
435
436         return (hash & RES_HASH_MASK);
437 }
438
439 static struct ldlm_resource *ldlm_resource_new(void)
440 {
441         struct ldlm_resource *res;
442
443         OBD_SLAB_ALLOC(res, ldlm_resource_slab, SLAB_NOFS, sizeof *res);
444         if (res == NULL)
445                 return NULL;
446
447         memset(res, 0, sizeof(*res));
448
449         INIT_LIST_HEAD(&res->lr_children);
450         INIT_LIST_HEAD(&res->lr_childof);
451         INIT_LIST_HEAD(&res->lr_granted);
452         INIT_LIST_HEAD(&res->lr_converting);
453         INIT_LIST_HEAD(&res->lr_waiting);
454         sema_init(&res->lr_lvb_sem, 1);
455         atomic_set(&res->lr_refcount, 1);
456
457         return res;
458 }
459
460 /* Args: locked namespace
461  * Returns: newly-allocated, referenced, unlocked resource */
462 static struct ldlm_resource *
463 ldlm_resource_add(struct ldlm_namespace *ns, struct ldlm_resource *parent,
464                   struct ldlm_res_id name, ldlm_type_t type)
465 {
466         struct list_head *bucket;
467         struct ldlm_resource *res;
468         ENTRY;
469
470         LASSERTF(type >= LDLM_MIN_TYPE && type < LDLM_MAX_TYPE,
471                  "type: %d\n", type);
472
473         res = ldlm_resource_new();
474         if (!res)
475                 RETURN(NULL);
476
477         l_lock(&ns->ns_lock);
478         memcpy(&res->lr_name, &name, sizeof(res->lr_name));
479         res->lr_namespace = ns;
480         atomic_inc(&ns->ns_refcount);
481
482         res->lr_type = type;
483         res->lr_most_restr = LCK_NL;
484
485         bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
486         list_add(&res->lr_hash, bucket);
487
488         if (parent == NULL) {
489                 list_add(&res->lr_childof, &ns->ns_root_list);
490         } else {
491                 res->lr_parent = parent;
492                 list_add(&res->lr_childof, &parent->lr_children);
493         }
494         l_unlock(&ns->ns_lock);
495
496
497         RETURN(res);
498 }
499
500 /* Args: unlocked namespace
501  * Locks: takes and releases ns->ns_lock and res->lr_lock
502  * Returns: referenced, unlocked ldlm_resource or NULL */
503 struct ldlm_resource *
504 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
505                   struct ldlm_res_id name, ldlm_type_t type, int create)
506 {
507         struct list_head *bucket, *tmp;
508         struct ldlm_resource *res = NULL;
509         ENTRY;
510
511         LASSERT(ns != NULL);
512         LASSERT(ns->ns_hash != NULL);
513         LASSERT(name.name[0] != 0);
514
515         l_lock(&ns->ns_lock);
516         bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
517
518         list_for_each(tmp, bucket) {
519                 res = list_entry(tmp, struct ldlm_resource, lr_hash);
520
521                 if (memcmp(&res->lr_name, &name, sizeof(res->lr_name)) == 0) {
522                         ldlm_resource_getref(res);
523                         l_unlock(&ns->ns_lock);
524                         RETURN(res);
525                 }
526         }
527
528         if (create) {
529                 res = ldlm_resource_add(ns, parent, name, type);
530                 if (res == NULL)
531                         GOTO(out, NULL);
532         } else {
533                 res = NULL;
534         }
535
536         if (create && ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
537                 int rc;
538
539                 /* Although this is technically a lock inversion risk (lvb_sem
540                  * should be taken before DLM lock), this resource was just
541                  * created, so nobody else can take the lvb_sem yet. -p */
542                 down(&res->lr_lvb_sem);
543                 /* Drop the dlm lock, because lvbo_init can touch the disk */
544                 l_unlock(&ns->ns_lock);
545                 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CREATE_RESOURCE, 2);
546                 rc = ns->ns_lvbo->lvbo_init(res);
547                 up(&res->lr_lvb_sem);
548                 if (rc)
549                         CERROR("lvbo_init failed for resource "LPU64"/"LPU64
550                                ": rc %d\n", name.name[0], name.name[1], rc);
551         } else {
552 out:
553                 l_unlock(&ns->ns_lock);
554         }
555
556         RETURN(res);
557 }
558
559 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
560 {
561         LASSERT(res != NULL);
562         LASSERT(res != LP_POISON);
563         atomic_inc(&res->lr_refcount);
564         CDEBUG(D_DLMTRACE, "getref res: %p count: %d\n", res,
565                atomic_read(&res->lr_refcount));
566         return res;
567 }
568
569 /* Returns 1 if the resource was freed, 0 if it remains. */
570 int ldlm_resource_putref(struct ldlm_resource *res)
571 {
572         int rc = 0;
573         ENTRY;
574
575         CDEBUG(D_DLMTRACE, "putref res: %p count: %d\n", res,
576                atomic_read(&res->lr_refcount) - 1);
577         LASSERT(atomic_read(&res->lr_refcount) > 0);
578         LASSERT(atomic_read(&res->lr_refcount) < LI_POISON);
579
580         if (atomic_dec_and_test(&res->lr_refcount)) {
581                 struct ldlm_namespace *ns = res->lr_namespace;
582                 ENTRY;
583
584                 l_lock(&ns->ns_lock);
585
586                 if (atomic_read(&res->lr_refcount) != 0) {
587                         /* We lost the race. */
588                         l_unlock(&ns->ns_lock);
589                         RETURN(rc);
590                 }
591
592                 if (!list_empty(&res->lr_granted)) {
593                         ldlm_resource_dump(D_ERROR, res);
594                         LBUG();
595                 }
596
597                 if (!list_empty(&res->lr_converting)) {
598                         ldlm_resource_dump(D_ERROR, res);
599                         LBUG();
600                 }
601
602                 if (!list_empty(&res->lr_waiting)) {
603                         ldlm_resource_dump(D_ERROR, res);
604                         LBUG();
605                 }
606
607                 if (!list_empty(&res->lr_children)) {
608                         ldlm_resource_dump(D_ERROR, res);
609                         LBUG();
610                 }
611
612                 list_del_init(&res->lr_hash);
613                 list_del_init(&res->lr_childof);
614                 if (res->lr_lvb_data)
615                         OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
616                 l_unlock(&ns->ns_lock);
617
618                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
619
620                 if (atomic_dec_and_test(&ns->ns_refcount)) {
621                         CDEBUG(D_DLMTRACE, "last ref on ns %s\n", ns->ns_name);
622                         wake_up(&ns->ns_refcount_waitq);
623                 }
624
625                 rc = 1;
626                 EXIT;
627         }
628
629         RETURN(rc);
630 }
631
632 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
633                             struct ldlm_lock *lock)
634 {
635         l_lock(&res->lr_namespace->ns_lock);
636
637         ldlm_resource_dump(D_OTHER, res);
638         CDEBUG(D_OTHER, "About to add this lock:\n");
639         ldlm_lock_dump(D_OTHER, lock, 0);
640
641         if (lock->l_destroyed) {
642                 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
643                 goto out;
644         }
645
646         LASSERT(list_empty(&lock->l_res_link));
647
648         list_add_tail(&lock->l_res_link, head);
649  out:
650         l_unlock(&res->lr_namespace->ns_lock);
651 }
652
653 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
654 {
655         l_lock(&lock->l_resource->lr_namespace->ns_lock);
656         list_del_init(&lock->l_res_link);
657         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
658 }
659
660 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
661 {
662         desc->lr_type = res->lr_type;
663         memcpy(&desc->lr_name, &res->lr_name, sizeof(desc->lr_name));
664 }
665
666 void ldlm_dump_all_namespaces(int level)
667 {
668         struct list_head *tmp;
669
670         down(&ldlm_namespace_lock);
671
672         list_for_each(tmp, &ldlm_namespace_list) {
673                 struct ldlm_namespace *ns;
674                 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
675                 ldlm_namespace_dump(level, ns);
676         }
677
678         up(&ldlm_namespace_lock);
679 }
680
681 void ldlm_namespace_dump(int level, struct ldlm_namespace *ns)
682 {
683         struct list_head *tmp;
684
685         CDEBUG(level, "--- Namespace: %s (rc: %d, client: %d)\n", ns->ns_name,
686                atomic_read(&ns->ns_refcount), ns->ns_client);
687
688         l_lock(&ns->ns_lock);
689         if (time_after(jiffies, ns->ns_next_dump)) {
690                 list_for_each(tmp, &ns->ns_root_list) {
691                         struct ldlm_resource *res;
692                         res = list_entry(tmp, struct ldlm_resource, lr_childof);
693
694                         /* Once we have resources with children, this should
695                          * really dump them recursively. */
696                         ldlm_resource_dump(level, res);
697                 }
698                 ns->ns_next_dump = jiffies + 10 * HZ;
699         }
700         l_unlock(&ns->ns_lock);
701 }
702
703 void ldlm_resource_dump(int level, struct ldlm_resource *res)
704 {
705         struct list_head *tmp;
706         int pos;
707
708         if (RES_NAME_SIZE != 4)
709                 LBUG();
710
711         CDEBUG(level, "--- Resource: %p ("LPU64"/"LPU64"/"LPU64"/"LPU64
712                ") (rc: %d)\n", res, res->lr_name.name[0], res->lr_name.name[1],
713                res->lr_name.name[2], res->lr_name.name[3],
714                atomic_read(&res->lr_refcount));
715
716         if (!list_empty(&res->lr_granted)) {
717                 pos = 0;
718                 CDEBUG(level, "Granted locks:\n");
719                 list_for_each(tmp, &res->lr_granted) {
720                         struct ldlm_lock *lock;
721                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
722                         ldlm_lock_dump(level, lock, ++pos);
723                 }
724         }
725         if (!list_empty(&res->lr_converting)) {
726                 pos = 0;
727                 CDEBUG(level, "Converting locks:\n");
728                 list_for_each(tmp, &res->lr_converting) {
729                         struct ldlm_lock *lock;
730                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
731                         ldlm_lock_dump(level, lock, ++pos);
732                 }
733         }
734         if (!list_empty(&res->lr_waiting)) {
735                 pos = 0;
736                 CDEBUG(level, "Waiting locks:\n");
737                 list_for_each(tmp, &res->lr_waiting) {
738                         struct ldlm_lock *lock;
739                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
740                         ldlm_lock_dump(level, lock, ++pos);
741                 }
742         }
743 }