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