Whamcloud - gitweb
Branch: b_new_cmd
[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 __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(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         if (copy_from_user(dummy, buffer, MAX_STRING_SIZE))
144                 return -EFAULT;
145
146         if (count == 6 && memcmp(dummy, "clear", 5) == 0) {
147                 CDEBUG(D_DLMTRACE,
148                        "dropping all unused locks from namespace %s\n",
149                        ns->ns_name);
150                 tmp = ns->ns_max_unused;
151                 ns->ns_max_unused = 0;
152                 ldlm_cancel_lru(ns, LDLM_SYNC);
153                 ns->ns_max_unused = tmp;
154                 return count;
155         }
156
157         tmp = simple_strtoul(dummy, &end, 0);
158         if (tmp == 0 && *end) {
159                 CERROR("invalid value written\n");
160                 return -EINVAL;
161         }
162
163         CDEBUG(D_DLMTRACE, "changing namespace %s max_unused from %u to %u\n",
164                ns->ns_name, ns->ns_max_unused, (unsigned int)tmp);
165
166         ns->ns_max_unused = (unsigned int)tmp;
167         ldlm_cancel_lru(ns, LDLM_ASYNC);
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].read_fptr = lprocfs_rd_u64;
183         lock_vars[0].name = lock_name;
184
185         snprintf(lock_name, MAX_STRING_SIZE, "%s/resource_count", ns->ns_name);
186         lock_vars[0].data = &ns->ns_resources;
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         lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
192
193         if (ns->ns_client) {
194                 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_unused_count",
195                          ns->ns_name);
196                 lock_vars[0].data = &ns->ns_nr_unused;
197                 lock_vars[0].read_fptr = lprocfs_uint_rd;
198                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
199
200                 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_size",
201                          ns->ns_name);
202                 lock_vars[0].data = ns;
203                 lock_vars[0].read_fptr = lprocfs_read_lru_size;
204                 lock_vars[0].write_fptr = lprocfs_write_lru_size;
205                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
206         }
207 }
208 #endif
209 #undef MAX_STRING_SIZE
210
211 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 client)
212 {
213         struct ldlm_namespace *ns = NULL;
214         struct list_head *bucket;
215         int rc;
216         ENTRY;
217
218         rc = ldlm_get_ref();
219         if (rc) {
220                 CERROR("ldlm_get_ref failed: %d\n", rc);
221                 RETURN(NULL);
222         }
223
224         OBD_ALLOC(ns, sizeof(*ns));
225         if (!ns)
226                 GOTO(out_ref, NULL);
227
228         OBD_VMALLOC(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
229         if (!ns->ns_hash)
230                 GOTO(out_ns, NULL);
231
232         OBD_ALLOC(ns->ns_name, strlen(name) + 1);
233         if (!ns->ns_name)
234                 GOTO(out_hash, NULL);
235
236         strcpy(ns->ns_name, name);
237
238         INIT_LIST_HEAD(&ns->ns_root_list);
239         ns->ns_refcount = 0;
240         ns->ns_client = client;
241         spin_lock_init(&ns->ns_hash_lock);
242         atomic_set(&ns->ns_locks, 0);
243         ns->ns_resources = 0;
244         init_waitqueue_head(&ns->ns_waitq);
245
246         for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
247              bucket--)
248                 INIT_LIST_HEAD(bucket);
249
250         INIT_LIST_HEAD(&ns->ns_unused_list);
251         ns->ns_nr_unused = 0;
252         ns->ns_max_unused = LDLM_DEFAULT_LRU_SIZE;
253         spin_lock_init(&ns->ns_unused_lock);
254
255         down(&ldlm_namespace_lock);
256         list_add(&ns->ns_list_chain, &ldlm_namespace_list);
257         up(&ldlm_namespace_lock);
258 #ifdef __KERNEL__
259         ldlm_proc_namespace(ns);
260 #endif
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;
284         int rc = 0, client = res->lr_namespace->ns_client;
285         int local_only = (flags & LDLM_FL_LOCAL_ONLY);
286         ENTRY;
287
288         
289         do {
290                 struct ldlm_lock *lock = NULL;
291  
292                 /* first, we look for non-cleaned-yet lock
293                  * all cleaned locks are marked by CLEANED flag */
294                 lock_res(res);
295                 list_for_each(tmp, q) {
296                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
297                         if (lock->l_flags & LDLM_FL_CLEANED) {
298                                 lock = NULL;
299                                 continue;
300                         }
301                         LDLM_LOCK_GET(lock);
302                         lock->l_flags |= LDLM_FL_CLEANED;
303                         break;
304                 }
305                 
306                 if (lock == NULL) {
307                         unlock_res(res);
308                         break;
309                 }
310
311                 /* Set CBPENDING so nothing in the cancellation path
312                  * can match this lock */
313                 lock->l_flags |= LDLM_FL_CBPENDING;
314                 lock->l_flags |= LDLM_FL_FAILED;
315                 lock->l_flags |= flags;
316
317                 if (local_only && (lock->l_readers || lock->l_writers)) {
318                         /* This is a little bit gross, but much better than the
319                          * alternative: pretend that we got a blocking AST from
320                          * the server, so that when the lock is decref'd, it
321                          * will go away ... */
322                         /* ... without sending a CANCEL message. */
323                         lock->l_flags |= LDLM_FL_LOCAL_ONLY;
324                         unlock_res(res);
325                         LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
326                         if (lock->l_completion_ast)
327                                 lock->l_completion_ast(lock, 0, NULL);
328                         LDLM_LOCK_PUT(lock);
329                         continue;
330                 }
331
332                 if (client) {
333                         struct lustre_handle lockh;
334
335                         unlock_res(res);
336                         ldlm_lock2handle(lock, &lockh);
337                         if (!local_only) {
338                                 rc = ldlm_cli_cancel(&lockh);
339                                 if (rc)
340                                         CERROR("ldlm_cli_cancel: %d\n", rc);
341                         }
342                         /* Force local cleanup on errors, too. */
343                         if (local_only || rc != ELDLM_OK)
344                                 ldlm_lock_cancel(lock);
345                 } else {
346                         ldlm_resource_unlink_lock(lock);
347                         unlock_res(res);
348                         LDLM_DEBUG(lock, "Freeing a lock still held by a "
349                                    "client node");
350                         ldlm_lock_destroy(lock);
351                 }
352                 LDLM_LOCK_PUT(lock);
353         } while (1);
354
355         EXIT;
356 }
357
358 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int flags)
359 {
360         struct list_head *tmp;
361         int i;
362
363         if (ns == NULL) {
364                 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
365                 return ELDLM_OK;
366         }
367
368         for (i = 0; i < RES_HASH_SIZE; i++) {
369                 spin_lock(&ns->ns_hash_lock);
370                 tmp = ns->ns_hash[i].next;
371                 while (tmp != &(ns->ns_hash[i])) {
372                         struct ldlm_resource *res;
373                         res = list_entry(tmp, struct ldlm_resource, lr_hash);
374                         ldlm_resource_getref(res);
375                         spin_unlock(&ns->ns_hash_lock);
376
377                         cleanup_resource(res, &res->lr_granted, flags);
378                         cleanup_resource(res, &res->lr_converting, flags);
379                         cleanup_resource(res, &res->lr_waiting, flags);
380
381                         spin_lock(&ns->ns_hash_lock);
382                         tmp  = tmp->next;
383
384                         /* XXX: former stuff caused issues in case of race
385                          * between ldlm_namespace_cleanup() and lockd() when
386                          * client gets blocking ast when lock gets distracted by
387                          * server. This is 1_4 branch solution, let's see how
388                          * will it behave. */
389                         if (!ldlm_resource_putref_locked(res))
390                                 CDEBUG(D_INFO,
391                                        "Namespace %s resource refcount nonzero "
392                                        "(%d) after lock cleanup; forcing cleanup.\n",
393                                        ns->ns_name, atomic_read(&res->lr_refcount));
394                 }
395                 spin_unlock(&ns->ns_hash_lock);
396         }
397
398         return ELDLM_OK;
399 }
400
401 static inline int ldlm_ns_refcount_atomic(struct ldlm_namespace *ns)
402 {
403         int refcount;
404         spin_lock(&ns->ns_hash_lock);
405         refcount = ns->ns_refcount;
406         spin_unlock(&ns->ns_hash_lock);
407         return refcount;
408 }
409
410 /* Cleanup, but also free, the namespace */
411 int ldlm_namespace_free(struct ldlm_namespace *ns, int force)
412 {
413         if (!ns)
414                 RETURN(ELDLM_OK);
415
416         down(&ldlm_namespace_lock);
417         list_del(&ns->ns_list_chain);
418         up(&ldlm_namespace_lock);
419
420         /* At shutdown time, don't call the cancellation callback */
421         ldlm_namespace_cleanup(ns, 0);
422
423 #ifdef __KERNEL__
424         {
425                 struct proc_dir_entry *dir;
426                 dir = lprocfs_srch(ldlm_ns_proc_dir, ns->ns_name);
427                 if (dir == NULL) {
428                         CERROR("dlm namespace %s has no procfs dir?\n",
429                                ns->ns_name);
430                 } else {
431                         lprocfs_remove(dir);
432                 }
433         }
434 #endif
435
436         if (ldlm_ns_refcount_atomic(ns) > 0) {
437                 struct l_wait_info lwi = LWI_INTR(NULL, NULL);
438                 int rc;
439                 CDEBUG(D_DLMTRACE,
440                        "dlm namespace %s free waiting on refcount %d\n",
441                        ns->ns_name, ns->ns_refcount);
442                 rc = l_wait_event(ns->ns_waitq,
443                                   ldlm_ns_refcount_atomic(ns) == 0, &lwi);
444                 if (ldlm_ns_refcount_atomic(ns)) {
445                         CDEBUG(D_ERROR, "Lock manager: wait for %s namespace "
446                                "cleanup aborted with %d resources in "
447                                "use. (%d)\nI'm going to try to clean "
448                                "up anyway, but I might need a reboot "
449                                "of this node.\n", ns->ns_name,
450                                ldlm_ns_refcount_atomic(ns), rc);
451                 }
452                 CDEBUG(D_DLMTRACE,
453                        "dlm namespace %s free done waiting\n", ns->ns_name);
454         }
455
456         POISON(ns->ns_hash, 0x5a, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
457         OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
458         OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
459         OBD_FREE(ns, sizeof(*ns));
460
461         ldlm_put_ref(force);
462
463         return ELDLM_OK;
464 }
465
466 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, struct ldlm_res_id name)
467 {
468         __u32 hash = 0;
469         int i;
470
471         for (i = 0; i < RES_NAME_SIZE; i++)
472                 hash += name.name[i];
473
474         hash += (__u32)((unsigned long)parent >> 4);
475
476         return (hash & RES_HASH_MASK);
477 }
478
479 static struct ldlm_resource *ldlm_resource_new(void)
480 {
481         struct ldlm_resource *res;
482
483         OBD_SLAB_ALLOC(res, ldlm_resource_slab, SLAB_NOFS, sizeof *res);
484         if (res == NULL)
485                 return NULL;
486
487         memset(res, 0, sizeof(*res));
488
489         INIT_LIST_HEAD(&res->lr_children);
490         INIT_LIST_HEAD(&res->lr_childof);
491         INIT_LIST_HEAD(&res->lr_granted);
492         INIT_LIST_HEAD(&res->lr_converting);
493         INIT_LIST_HEAD(&res->lr_waiting);
494         atomic_set(&res->lr_refcount, 1);
495         spin_lock_init(&res->lr_lock);
496
497         /* one who creates the resource must unlock
498          * the semaphore after lvb initialization */
499         init_MUTEX_LOCKED(&res->lr_lvb_sem);
500
501         return res;
502 }
503
504 /* must be called with hash lock held */
505 static struct ldlm_resource *
506 ldlm_resource_find(struct ldlm_namespace *ns, struct ldlm_res_id name, __u32 hash)
507 {
508         struct list_head *bucket, *tmp;
509         struct ldlm_resource *res;
510
511         LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
512         bucket = ns->ns_hash + hash;
513
514         list_for_each(tmp, bucket) {
515                 res = list_entry(tmp, struct ldlm_resource, lr_hash);
516                 if (memcmp(&res->lr_name, &name, sizeof(res->lr_name)) == 0)
517                         return res;
518         }
519
520         return NULL;
521 }
522
523 /* Args: locked namespace
524  * Returns: newly-allocated, referenced, unlocked resource */
525 static struct ldlm_resource *
526 ldlm_resource_add(struct ldlm_namespace *ns, struct ldlm_resource *parent,
527                   struct ldlm_res_id name, __u32 hash, __u32 type)
528 {
529         struct list_head *bucket;
530         struct ldlm_resource *res, *old_res;
531         ENTRY;
532
533         LASSERTF(type >= LDLM_MIN_TYPE && type <= LDLM_MAX_TYPE,
534                  "type: %d", type);
535
536         res = ldlm_resource_new();
537         if (!res)
538                 RETURN(NULL);
539
540         memcpy(&res->lr_name, &name, sizeof(res->lr_name));
541         res->lr_namespace = ns;
542         res->lr_type = type;
543         res->lr_most_restr = LCK_NL;
544
545         spin_lock(&ns->ns_hash_lock);
546         old_res = ldlm_resource_find(ns, name, hash);
547         if (old_res) {
548                 /* someone won the race and added the resource before */
549                 ldlm_resource_getref(old_res);
550                 spin_unlock(&ns->ns_hash_lock);
551                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
552                 /* synchronize WRT resource creation */
553                 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
554                         down(&old_res->lr_lvb_sem);
555                         up(&old_res->lr_lvb_sem);
556                 }
557                 RETURN(old_res);
558         }
559
560         /* we won! let's add the resource */
561         bucket = ns->ns_hash + hash;
562         list_add(&res->lr_hash, bucket);
563         ns->ns_resources++;
564         ns->ns_refcount++;
565
566         if (parent == NULL) {
567                 list_add(&res->lr_childof, &ns->ns_root_list);
568         } else {
569                 res->lr_parent = parent;
570                 list_add(&res->lr_childof, &parent->lr_children);
571         }
572         spin_unlock(&ns->ns_hash_lock);
573
574         if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
575                 int rc;
576
577                 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CREATE_RESOURCE, 2);
578                 rc = ns->ns_lvbo->lvbo_init(res);
579                 if (rc)
580                         CERROR("lvbo_init failed for resource "
581                                LPU64": rc %d\n", name.name[0], rc);
582                 /* we create resource with locked lr_lvb_sem */
583                 up(&res->lr_lvb_sem);
584         }
585
586         RETURN(res);
587 }
588
589 /* Args: unlocked namespace
590  * Locks: takes and releases ns->ns_lock and res->lr_lock
591  * Returns: referenced, unlocked ldlm_resource or NULL */
592 struct ldlm_resource *
593 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
594                   struct ldlm_res_id name, __u32 type, int create)
595 {
596         __u32 hash = ldlm_hash_fn(parent, name);
597         struct ldlm_resource *res = NULL;
598         ENTRY;
599
600         LASSERT(ns != NULL);
601         LASSERT(ns->ns_hash != NULL);
602         LASSERT(name.name[0] != 0);
603
604         spin_lock(&ns->ns_hash_lock);
605         res = ldlm_resource_find(ns, name, hash);
606         if (res) {
607                 ldlm_resource_getref(res);
608                 spin_unlock(&ns->ns_hash_lock);
609                 /* synchronize WRT resource creation */
610                 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
611                         down(&res->lr_lvb_sem);
612                         up(&res->lr_lvb_sem);
613                 }
614                 RETURN(res);
615         }
616         spin_unlock(&ns->ns_hash_lock);
617
618         if (create == 0)
619                 RETURN(NULL);
620
621         res = ldlm_resource_add(ns, parent, name, hash, type);
622         RETURN(res);
623 }
624
625 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
626 {
627         LASSERT(res != NULL);
628         LASSERT(res != LP_POISON);
629         atomic_inc(&res->lr_refcount);
630         CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
631                atomic_read(&res->lr_refcount));
632         return res;
633 }
634
635 void __ldlm_resource_putref_final(struct ldlm_resource *res)
636 {
637         struct ldlm_namespace *ns = res->lr_namespace;
638
639         LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
640
641         if (!list_empty(&res->lr_granted)) {
642                 ldlm_resource_dump(D_ERROR, res);
643                 LBUG();
644         }
645
646         if (!list_empty(&res->lr_converting)) {
647                 ldlm_resource_dump(D_ERROR, res);
648                 LBUG();
649         }
650
651         if (!list_empty(&res->lr_waiting)) {
652                 ldlm_resource_dump(D_ERROR, res);
653                 LBUG();
654         }
655
656         if (!list_empty(&res->lr_children)) {
657                 ldlm_resource_dump(D_ERROR, res);
658                 LBUG();
659         }
660
661         ns->ns_refcount--;
662         list_del_init(&res->lr_hash);
663         list_del_init(&res->lr_childof);
664
665         ns->ns_resources--;
666         if (ns->ns_resources == 0)
667                 wake_up(&ns->ns_waitq);
668 }
669
670 /* Returns 1 if the resource was freed, 0 if it remains. */
671 int ldlm_resource_putref(struct ldlm_resource *res)
672 {
673         struct ldlm_namespace *ns = res->lr_namespace;
674         int rc = 0;
675         ENTRY;
676
677         CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
678                atomic_read(&res->lr_refcount) - 1);
679         LASSERT(atomic_read(&res->lr_refcount) > 0);
680         LASSERT(atomic_read(&res->lr_refcount) < LI_POISON);
681
682         LASSERT(atomic_read(&res->lr_refcount) >= 0);
683         if (atomic_dec_and_lock(&res->lr_refcount, &ns->ns_hash_lock)) {
684                 __ldlm_resource_putref_final(res);
685                 spin_unlock(&ns->ns_hash_lock);
686                 if (res->lr_lvb_data)
687                         OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
688                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
689                 rc = 1;
690         }
691
692         RETURN(rc);
693 }
694
695 /* Returns 1 if the resource was freed, 0 if it remains. */
696 int ldlm_resource_putref_locked(struct ldlm_resource *res)
697 {
698         int rc = 0;
699         ENTRY;
700
701         CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
702                atomic_read(&res->lr_refcount) - 1);
703         LASSERT(atomic_read(&res->lr_refcount) > 0);
704         LASSERT(atomic_read(&res->lr_refcount) < LI_POISON);
705
706         LASSERT(atomic_read(&res->lr_refcount) >= 0);
707         if (atomic_dec_and_test(&res->lr_refcount)) {
708                 __ldlm_resource_putref_final(res);
709                 if (res->lr_lvb_data)
710                         OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
711                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
712                 rc = 1;
713         }
714
715         RETURN(rc);
716 }
717
718 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
719                             struct ldlm_lock *lock)
720 {
721         check_res_locked(res);
722
723         ldlm_resource_dump(D_OTHER, res);
724         CDEBUG(D_OTHER, "About to add this lock:\n");
725         ldlm_lock_dump(D_OTHER, lock, 0);
726
727         if (lock->l_destroyed) {
728                 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
729                 return;
730         }
731
732         LASSERT(list_empty(&lock->l_res_link));
733
734         list_add_tail(&lock->l_res_link, head);
735 }
736
737 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
738 {
739         check_res_locked(lock->l_resource);
740         list_del_init(&lock->l_res_link);
741 }
742
743 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
744 {
745         desc->lr_type = res->lr_type;
746         memcpy(&desc->lr_name, &res->lr_name, sizeof(desc->lr_name));
747 }
748
749 void ldlm_dump_all_namespaces(int level)
750 {
751         struct list_head *tmp;
752
753         down(&ldlm_namespace_lock);
754
755         list_for_each(tmp, &ldlm_namespace_list) {
756                 struct ldlm_namespace *ns;
757                 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
758                 ldlm_namespace_dump(level, ns);
759         }
760
761         up(&ldlm_namespace_lock);
762 }
763
764 void ldlm_namespace_dump(int level, struct ldlm_namespace *ns)
765 {
766         struct list_head *tmp;
767
768         CDEBUG(level, "--- Namespace: %s (rc: %d, client: %d)\n",
769                ns->ns_name, ns->ns_refcount, ns->ns_client);
770
771         if (time_before(jiffies, ns->ns_next_dump))
772                 return;
773
774         spin_lock(&ns->ns_hash_lock);
775         tmp = ns->ns_root_list.next;
776         while (tmp != &ns->ns_root_list) {
777                 struct ldlm_resource *res;
778                 res = list_entry(tmp, struct ldlm_resource, lr_childof);
779
780                 ldlm_resource_getref(res);
781                 spin_unlock(&ns->ns_hash_lock);
782
783                 lock_res(res);
784                 ldlm_resource_dump(level, res);
785                 unlock_res(res);
786                 
787                 spin_lock(&ns->ns_hash_lock);
788                 tmp = tmp->next;
789                 ldlm_resource_putref_locked(res);
790         }
791         ns->ns_next_dump = jiffies + 10 * HZ;
792         spin_unlock(&ns->ns_hash_lock);
793 }
794
795 void ldlm_resource_dump(int level, struct ldlm_resource *res)
796 {
797         struct list_head *tmp;
798         int pos;
799
800         if (RES_NAME_SIZE != 4)
801                 LBUG();
802
803         CDEBUG(level, "--- Resource: %p ("LPU64"/"LPU64"/"LPU64"/"LPU64
804                ") (rc: %d)\n", res, res->lr_name.name[0], res->lr_name.name[1],
805                res->lr_name.name[2], res->lr_name.name[3],
806                atomic_read(&res->lr_refcount));
807
808         if (!list_empty(&res->lr_granted)) {
809                 pos = 0;
810                 CDEBUG(level, "Granted locks:\n");
811                 list_for_each(tmp, &res->lr_granted) {
812                         struct ldlm_lock *lock;
813                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
814                         ldlm_lock_dump(level, lock, ++pos);
815                 }
816         }
817         if (!list_empty(&res->lr_converting)) {
818                 pos = 0;
819                 CDEBUG(level, "Converting locks:\n");
820                 list_for_each(tmp, &res->lr_converting) {
821                         struct ldlm_lock *lock;
822                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
823                         ldlm_lock_dump(level, lock, ++pos);
824                 }
825         }
826         if (!list_empty(&res->lr_waiting)) {
827                 pos = 0;
828                 CDEBUG(level, "Waiting locks:\n");
829                 list_for_each(tmp, &res->lr_waiting) {
830                         struct ldlm_lock *lock;
831                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
832                         ldlm_lock_dump(level, lock, ++pos);
833                 }
834         }
835 }