Whamcloud - gitweb
- merge 2 weeks of b1_4 fixes onto HEAD
[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 spinlock_t ldlm_namespace_lock = SPIN_LOCK_UNLOCKED;
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 __KERNEL__
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();
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];
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, NULL, 0);
157         CDEBUG(D_DLMTRACE, "changing namespace %s max_unused from %u to %u\n",
158                ns->ns_name, ns->ns_max_unused, (unsigned int)tmp);
159         ns->ns_max_unused = (unsigned int)tmp;
160
161         ldlm_cancel_lru(ns, LDLM_ASYNC);
162
163         return count;
164 }
165
166 void ldlm_proc_namespace(struct ldlm_namespace *ns)
167 {
168         struct lprocfs_vars lock_vars[2];
169         char lock_name[MAX_STRING_SIZE + 1];
170
171         LASSERT(ns != NULL);
172         LASSERT(ns->ns_name != NULL);
173
174         lock_name[MAX_STRING_SIZE] = '\0';
175
176         memset(lock_vars, 0, sizeof(lock_vars));
177         lock_vars[0].read_fptr = lprocfs_rd_u64;
178         lock_vars[0].name = lock_name;
179
180         snprintf(lock_name, MAX_STRING_SIZE, "%s/resource_count", ns->ns_name);
181         lock_vars[0].data = &ns->ns_resources;
182         lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
183
184         snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_count", ns->ns_name);
185         lock_vars[0].data = &ns->ns_locks;
186         lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
187
188         if (ns->ns_client) {
189                 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_unused_count",
190                          ns->ns_name);
191                 lock_vars[0].data = &ns->ns_nr_unused;
192                 lock_vars[0].read_fptr = lprocfs_uint_rd;
193                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
194
195                 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_size",
196                          ns->ns_name);
197                 lock_vars[0].data = ns;
198                 lock_vars[0].read_fptr = lprocfs_read_lru_size;
199                 lock_vars[0].write_fptr = lprocfs_write_lru_size;
200                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
201         }
202 }
203 #endif
204 #undef MAX_STRING_SIZE
205
206 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 client)
207 {
208         struct ldlm_namespace *ns = NULL;
209         struct list_head *bucket;
210         int rc;
211         ENTRY;
212
213         rc = ldlm_get_ref();
214         if (rc) {
215                 CERROR("ldlm_get_ref failed: %d\n", rc);
216                 RETURN(NULL);
217         }
218
219         OBD_ALLOC(ns, sizeof(*ns));
220         if (!ns)
221                 GOTO(out_ref, NULL);
222
223         OBD_VMALLOC(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
224         if (!ns->ns_hash)
225                 GOTO(out_ns, NULL);
226
227         OBD_ALLOC(ns->ns_name, strlen(name) + 1);
228         if (!ns->ns_name)
229                 GOTO(out_hash, NULL);
230
231         strcpy(ns->ns_name, name);
232
233         INIT_LIST_HEAD(&ns->ns_root_list);
234         l_lock_init(&ns->ns_lock);
235         ns->ns_refcount = 0;
236         ns->ns_client = client;
237         spin_lock_init(&ns->ns_counter_lock);
238         ns->ns_locks = 0;
239         ns->ns_resources = 0;
240
241         for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
242              bucket--)
243                 INIT_LIST_HEAD(bucket);
244
245         INIT_LIST_HEAD(&ns->ns_unused_list);
246         ns->ns_nr_unused = 0;
247         ns->ns_max_unused = LDLM_DEFAULT_LRU_SIZE;
248
249         spin_lock(&ldlm_namespace_lock);
250         list_add(&ns->ns_list_chain, &ldlm_namespace_list);
251         spin_unlock(&ldlm_namespace_lock);
252 #ifdef __KERNEL__
253         ldlm_proc_namespace(ns);
254 #endif
255         RETURN(ns);
256
257 out_hash:
258         POISON(ns->ns_hash, 0x5a, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
259         OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
260 out_ns:
261         OBD_FREE(ns, sizeof(*ns));
262 out_ref:
263         ldlm_put_ref(0);
264         RETURN(NULL);
265 }
266
267 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
268
269 /* If flags contains FL_LOCAL_ONLY, don't try to tell the server, just cleanup.
270  * This is currently only used for recovery, and we make certain assumptions
271  * as a result--notably, that we shouldn't cancel locks with refs. -phil
272  *
273  * Called with the ns_lock held. */
274 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
275                              int flags)
276 {
277         struct list_head *tmp, *pos;
278         int rc = 0, client = res->lr_namespace->ns_client;
279         int local_only = (flags & LDLM_FL_LOCAL_ONLY);
280         ENTRY;
281
282         list_for_each_safe(tmp, pos, q) {
283                 struct ldlm_lock *lock;
284                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
285                 LDLM_LOCK_GET(lock);
286
287                 /* Set CBPENDING so nothing in the cancellation path
288                  * can match this lock */
289                 lock->l_flags |= LDLM_FL_CBPENDING;
290                 lock->l_flags |= LDLM_FL_FAILED;
291                 lock->l_flags |= flags;
292
293                 if (local_only && (lock->l_readers || lock->l_writers)) {
294                         /* This is a little bit gross, but much better than the
295                          * alternative: pretend that we got a blocking AST from
296                          * the server, so that when the lock is decref'd, it
297                          * will go away ... */
298                         /* ... without sending a CANCEL message. */
299                         lock->l_flags |= LDLM_FL_LOCAL_ONLY;
300                         LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
301                         if (lock->l_completion_ast)
302                                 lock->l_completion_ast(lock, 0, NULL);
303                         LDLM_LOCK_PUT(lock);
304                         continue;
305                 }
306
307                 if (client) {
308                         struct lustre_handle lockh;
309                         ldlm_lock2handle(lock, &lockh);
310                         if (!local_only) {
311                                 rc = ldlm_cli_cancel(&lockh);
312                                 if (rc)
313                                         CERROR("ldlm_cli_cancel: %d\n", rc);
314                         }
315                         /* Force local cleanup on errors, too. */
316                         if (local_only || rc != ELDLM_OK)
317                                 ldlm_lock_cancel(lock);
318                 } else {
319                         LDLM_DEBUG(lock, "Freeing a lock still held by a "
320                                    "client node");
321
322                         ldlm_resource_unlink_lock(lock);
323                         ldlm_lock_destroy(lock);
324                 }
325                 LDLM_LOCK_PUT(lock);
326         }
327         EXIT;
328 }
329
330 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int flags)
331 {
332         int i;
333
334         if (ns == NULL) {
335                 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
336                 return ELDLM_OK;
337         }
338
339         l_lock(&ns->ns_lock);
340         for (i = 0; i < RES_HASH_SIZE; i++) {
341                 struct list_head *tmp, *pos;
342                 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
343                         struct ldlm_resource *res;
344                         res = list_entry(tmp, struct ldlm_resource, lr_hash);
345                         ldlm_resource_getref(res);
346
347                         cleanup_resource(res, &res->lr_granted, flags);
348                         cleanup_resource(res, &res->lr_converting, flags);
349                         cleanup_resource(res, &res->lr_waiting, flags);
350
351                         /* XXX what a mess: don't force cleanup if we're
352                          * local_only (which is only used by recovery).  In that
353                          * case, we probably still have outstanding lock refs
354                          * which reference these resources. -phil */
355                         if (!ldlm_resource_putref(res) &&
356                             !(flags & LDLM_FL_LOCAL_ONLY)) {
357                                 CERROR("Resource refcount nonzero (%d) after "
358                                        "lock cleanup; forcing cleanup.\n",
359                                        atomic_read(&res->lr_refcount));
360                                 ldlm_resource_dump(res);
361                                 atomic_set(&res->lr_refcount, 1);
362                                 ldlm_resource_putref(res);
363                         }
364                 }
365         }
366         l_unlock(&ns->ns_lock);
367
368         return ELDLM_OK;
369 }
370
371 /* Cleanup, but also free, the namespace */
372 int ldlm_namespace_free(struct ldlm_namespace *ns, int force)
373 {
374         if (!ns)
375                 RETURN(ELDLM_OK);
376
377         spin_lock(&ldlm_namespace_lock);
378         list_del(&ns->ns_list_chain);
379
380         spin_unlock(&ldlm_namespace_lock);
381
382         /* At shutdown time, don't call the cancellation callback */
383         ldlm_namespace_cleanup(ns, 0);
384
385 #ifdef __KERNEL__
386         {
387                 struct proc_dir_entry *dir;
388                 dir = lprocfs_srch(ldlm_ns_proc_dir, ns->ns_name);
389                 if (dir == NULL) {
390                         CERROR("dlm namespace %s has no procfs dir?\n",
391                                ns->ns_name);
392                 } else {
393                         lprocfs_remove(dir);
394                 }
395         }
396 #endif
397
398         POISON(ns->ns_hash, 0x5a, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
399         OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
400         OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
401         OBD_FREE(ns, sizeof(*ns));
402
403         ldlm_put_ref(force);
404
405         return ELDLM_OK;
406 }
407
408 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, struct ldlm_res_id name)
409 {
410         __u32 hash = 0;
411         int i;
412
413         for (i = 0; i < RES_NAME_SIZE; i++)
414                 hash += name.name[i];
415
416         hash += (__u32)((unsigned long)parent >> 4);
417
418         return (hash & RES_HASH_MASK);
419 }
420
421 static struct ldlm_resource *ldlm_resource_new(void)
422 {
423         struct ldlm_resource *res;
424
425         OBD_SLAB_ALLOC(res, ldlm_resource_slab, SLAB_NOFS, sizeof *res);
426         if (res == NULL) {
427                 LBUG();
428                 return NULL;
429         }
430         memset(res, 0, sizeof(*res));
431
432         INIT_LIST_HEAD(&res->lr_children);
433         INIT_LIST_HEAD(&res->lr_childof);
434         INIT_LIST_HEAD(&res->lr_granted);
435         INIT_LIST_HEAD(&res->lr_converting);
436         INIT_LIST_HEAD(&res->lr_waiting);
437         sema_init(&res->lr_lvb_sem, 1);
438         atomic_set(&res->lr_refcount, 1);
439
440         return res;
441 }
442
443 /* Args: locked namespace
444  * Returns: newly-allocated, referenced, unlocked resource */
445 static struct ldlm_resource *
446 ldlm_resource_add(struct ldlm_namespace *ns, struct ldlm_resource *parent,
447                   struct ldlm_res_id name, __u32 type)
448 {
449         struct list_head *bucket;
450         struct ldlm_resource *res;
451         ENTRY;
452
453         if (type < LDLM_MIN_TYPE || type > LDLM_MAX_TYPE) {
454                 LBUG();
455                 RETURN(NULL);
456         }
457
458         res = ldlm_resource_new();
459         if (!res) {
460                 LBUG();
461                 RETURN(NULL);
462         }
463
464         spin_lock(&ns->ns_counter_lock);
465         ns->ns_resources++;
466         spin_unlock(&ns->ns_counter_lock);
467
468         l_lock(&ns->ns_lock);
469         memcpy(&res->lr_name, &name, sizeof(res->lr_name));
470         res->lr_namespace = ns;
471         ns->ns_refcount++;
472
473         res->lr_type = type;
474         res->lr_most_restr = LCK_NL;
475
476         bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
477         list_add(&res->lr_hash, bucket);
478
479         if (parent == NULL) {
480                 list_add(&res->lr_childof, &ns->ns_root_list);
481         } else {
482                 res->lr_parent = parent;
483                 list_add(&res->lr_childof, &parent->lr_children);
484         }
485         l_unlock(&ns->ns_lock);
486
487
488         RETURN(res);
489 }
490
491 /* Args: unlocked namespace
492  * Locks: takes and releases ns->ns_lock and res->lr_lock
493  * Returns: referenced, unlocked ldlm_resource or NULL */
494 struct ldlm_resource *
495 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
496                   struct ldlm_res_id name, __u32 type, int create)
497 {
498         struct list_head *bucket, *tmp;
499         struct ldlm_resource *res = NULL;
500         ENTRY;
501
502         LASSERT(ns != NULL);
503         LASSERT(ns->ns_hash != NULL);
504         LASSERT(name.name[0] != 0);
505
506         l_lock(&ns->ns_lock);
507         bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
508
509         list_for_each(tmp, bucket) {
510                 res = list_entry(tmp, struct ldlm_resource, lr_hash);
511
512                 if (memcmp(&res->lr_name, &name, sizeof(res->lr_name)) == 0) {
513                         ldlm_resource_getref(res);
514                         l_unlock(&ns->ns_lock);
515                         RETURN(res);
516                 }
517         }
518
519         if (create)
520                 res = ldlm_resource_add(ns, parent, name, type);
521         else
522                 res = NULL;
523
524         l_unlock(&ns->ns_lock);
525
526         if (create && ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
527                 int rc = ns->ns_lvbo->lvbo_init(res);
528                 if (rc)
529                         CERROR("lvbo_init failed for resource "LPU64": rc %d\n",
530                                name.name[0], rc);
531         }
532
533         RETURN(res);
534 }
535
536 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
537 {
538         LASSERT(res != NULL);
539         LASSERT(res != LP_POISON);
540         atomic_inc(&res->lr_refcount);
541         CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
542                atomic_read(&res->lr_refcount));
543         return res;
544 }
545
546 /* Returns 1 if the resource was freed, 0 if it remains. */
547 int ldlm_resource_putref(struct ldlm_resource *res)
548 {
549         int rc = 0;
550         ENTRY;
551
552         CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
553                atomic_read(&res->lr_refcount) - 1);
554         LASSERT(atomic_read(&res->lr_refcount) > 0);
555         LASSERT(atomic_read(&res->lr_refcount) < LI_POISON);
556
557         if (atomic_dec_and_test(&res->lr_refcount)) {
558                 struct ldlm_namespace *ns = res->lr_namespace;
559                 ENTRY;
560
561                 l_lock(&ns->ns_lock);
562
563                 if (atomic_read(&res->lr_refcount) != 0) {
564                         /* We lost the race. */
565                         l_unlock(&ns->ns_lock);
566                         RETURN(rc);
567                 }
568
569                 if (!list_empty(&res->lr_granted)) {
570                         ldlm_resource_dump(res);
571                         LBUG();
572                 }
573
574                 if (!list_empty(&res->lr_converting)) {
575                         ldlm_resource_dump(res);
576                         LBUG();
577                 }
578
579                 if (!list_empty(&res->lr_waiting)) {
580                         ldlm_resource_dump(res);
581                         LBUG();
582                 }
583
584                 if (!list_empty(&res->lr_children)) {
585                         ldlm_resource_dump(res);
586                         LBUG();
587                 }
588
589                 ns->ns_refcount--;
590                 list_del_init(&res->lr_hash);
591                 list_del_init(&res->lr_childof);
592                 if (res->lr_lvb_data)
593                         OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
594                 l_unlock(&ns->ns_lock);
595
596                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
597
598                 spin_lock(&ns->ns_counter_lock);
599                 ns->ns_resources--;
600                 spin_unlock(&ns->ns_counter_lock);
601
602                 rc = 1;
603                 EXIT;
604         }
605
606         RETURN(rc);
607 }
608
609 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
610                             struct ldlm_lock *lock)
611 {
612         l_lock(&res->lr_namespace->ns_lock);
613
614         ldlm_resource_dump(res);
615         CDEBUG(D_OTHER, "About to add this lock:\n");
616         ldlm_lock_dump(D_OTHER, lock, 0);
617
618         if (lock->l_destroyed) {
619                 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
620                 goto out;
621         }
622
623         LASSERT(list_empty(&lock->l_res_link));
624
625         list_add_tail(&lock->l_res_link, head);
626  out:
627         l_unlock(&res->lr_namespace->ns_lock);
628 }
629
630 void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
631                                      struct ldlm_lock *new)
632 {
633         struct ldlm_resource *res = original->l_resource;
634
635         l_lock(&res->lr_namespace->ns_lock);
636
637         ldlm_resource_dump(res);
638         CDEBUG(D_OTHER, "About to insert this lock after %p:\n", original);
639         ldlm_lock_dump(D_OTHER, new, 0);
640
641         if (new->l_destroyed) {
642                 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
643                 goto out;
644         }
645
646         LASSERT(list_empty(&new->l_res_link));
647
648         list_add(&new->l_res_link, &original->l_res_link);
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(void)
667 {
668         struct list_head *tmp;
669
670         spin_lock(&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(ns);
676         }
677
678         spin_unlock(&ldlm_namespace_lock);
679 }
680
681 void ldlm_namespace_dump(struct ldlm_namespace *ns)
682 {
683         struct list_head *tmp;
684         unsigned int debug_save = portal_debug;
685
686         portal_debug |= D_OTHER;
687         l_lock(&ns->ns_lock);
688         CDEBUG(D_OTHER, "--- Namespace: %s (rc: %d, client: %d)\n", ns->ns_name,
689                ns->ns_refcount, ns->ns_client);
690
691         list_for_each(tmp, &ns->ns_root_list) {
692                 struct ldlm_resource *res;
693                 res = list_entry(tmp, struct ldlm_resource, lr_childof);
694
695                 /* Once we have resources with children, this should really dump
696                  * them recursively. */
697                 ldlm_resource_dump(res);
698         }
699         l_unlock(&ns->ns_lock);
700         portal_debug = debug_save;
701 }
702
703 void ldlm_resource_dump(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(D_OTHER, "--- 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(D_OTHER, "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(D_OTHER, lock, ++pos);
723                 }
724         }
725         if (!list_empty(&res->lr_converting)) {
726                 pos = 0;
727                 CDEBUG(D_OTHER, "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(D_OTHER, lock, ++pos);
732                 }
733         }
734         if (!list_empty(&res->lr_waiting)) {
735                 pos = 0;
736                 CDEBUG(D_OTHER, "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(D_OTHER, lock, ++pos);
741                 }
742         }
743 }