Whamcloud - gitweb
- make HEAD from b_post_cmd3
[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 the Lustre file system, http://www.lustre.org
9  *   Lustre is a trademark of Cluster File Systems, Inc.
10  *
11  *   You may have signed or agreed to another license before downloading
12  *   this software.  If so, you are bound by the terms and conditions
13  *   of that agreement, and the following does not apply to you.  See the
14  *   LICENSE file included with this distribution for more information.
15  *
16  *   If you did not agree to a different license, then this copy of Lustre
17  *   is open source software; you can redistribute it and/or modify it
18  *   under the terms of version 2 of the GNU General Public License as
19  *   published by the Free Software Foundation.
20  *
21  *   In either case, Lustre is distributed in the hope that it will be
22  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
23  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  *   license text for more details.
25  */
26
27 #define DEBUG_SUBSYSTEM S_LDLM
28 #ifdef __KERNEL__
29 # include <lustre_dlm.h>
30 #else
31 # include <liblustre.h>
32 #endif
33
34 #include <obd_class.h>
35 #include "ldlm_internal.h"
36
37 cfs_mem_cache_t *ldlm_resource_slab, *ldlm_lock_slab;
38
39 struct semaphore ldlm_namespace_lock;
40 struct list_head ldlm_namespace_list = CFS_LIST_HEAD_INIT(ldlm_namespace_list);
41 cfs_proc_dir_entry_t *ldlm_type_proc_dir = NULL;
42 cfs_proc_dir_entry_t *ldlm_ns_proc_dir = NULL;
43 cfs_proc_dir_entry_t *ldlm_svc_proc_dir = NULL;
44
45 #ifdef LPROCFS
46 static int ldlm_proc_dump_ns(struct file *file, const char *buffer,
47                              unsigned long count, void *data)
48 {
49         ldlm_dump_all_namespaces(D_DLMTRACE);
50         RETURN(count);
51 }
52
53 int ldlm_proc_setup(void)
54 {
55         int rc;
56         struct lprocfs_vars list[] = {
57                 { "dump_namespaces", NULL, ldlm_proc_dump_ns, NULL },
58                 { NULL }};
59         ENTRY;
60         LASSERT(ldlm_ns_proc_dir == NULL);
61
62         ldlm_type_proc_dir = lprocfs_register(OBD_LDLM_DEVICENAME,
63                                               proc_lustre_root,
64                                               NULL, NULL);
65         if (IS_ERR(ldlm_type_proc_dir)) {
66                 CERROR("LProcFS failed in ldlm-init\n");
67                 rc = PTR_ERR(ldlm_type_proc_dir);
68                 GOTO(err, rc);
69         }
70
71         ldlm_ns_proc_dir = lprocfs_register("namespaces",
72                                             ldlm_type_proc_dir,
73                                             NULL, NULL);
74         if (IS_ERR(ldlm_ns_proc_dir)) {
75                 CERROR("LProcFS failed in ldlm-init\n");
76                 rc = PTR_ERR(ldlm_ns_proc_dir);
77                 GOTO(err_type, rc);
78         }
79
80         ldlm_svc_proc_dir = lprocfs_register("services",
81                                             ldlm_type_proc_dir,
82                                             NULL, NULL);
83         if (IS_ERR(ldlm_svc_proc_dir)) {
84                 CERROR("LProcFS failed in ldlm-init\n");
85                 rc = PTR_ERR(ldlm_svc_proc_dir);
86                 GOTO(err_ns, rc);
87         }
88
89         rc = lprocfs_add_vars(ldlm_type_proc_dir, list, NULL);
90
91         RETURN(0);
92
93 err_ns:
94         lprocfs_remove(&ldlm_ns_proc_dir);
95 err_type:
96         lprocfs_remove(&ldlm_type_proc_dir);
97 err:
98         ldlm_svc_proc_dir = NULL;
99         RETURN(rc);
100 }
101
102 void ldlm_proc_cleanup(void)
103 {
104         if (ldlm_svc_proc_dir)
105                 lprocfs_remove(&ldlm_svc_proc_dir);
106
107         if (ldlm_ns_proc_dir)
108                 lprocfs_remove(&ldlm_ns_proc_dir);
109
110         if (ldlm_type_proc_dir)
111                 lprocfs_remove(&ldlm_type_proc_dir);
112 }
113
114 static int lprocfs_uint_rd(char *page, char **start, off_t off,
115                            int count, int *eof, void *data)
116 {
117         unsigned int *temp = (unsigned int *)data;
118         return snprintf(page, count, "%u\n", *temp);
119 }
120
121 static int lprocfs_read_lru_size(char *page, char **start, off_t off,
122                                  int count, int *eof, void *data)
123 {
124         struct ldlm_namespace *ns = data;
125         return snprintf(page, count, "%u\n", ns->ns_max_unused);
126 }
127
128 #define MAX_STRING_SIZE 128
129 static int lprocfs_write_lru_size(struct file *file, const char *buffer,
130                                   unsigned long count, void *data)
131 {
132         struct ldlm_namespace *ns = data;
133         char dummy[MAX_STRING_SIZE + 1], *end;
134         unsigned long tmp;
135
136         dummy[MAX_STRING_SIZE] = '\0';
137         if (copy_from_user(dummy, buffer, MAX_STRING_SIZE))
138                 return -EFAULT;
139
140         if (count == 6 && memcmp(dummy, "clear", 5) == 0) {
141                 CDEBUG(D_DLMTRACE,
142                        "dropping all unused locks from namespace %s\n",
143                        ns->ns_name);
144                 tmp = ns->ns_max_unused;
145                 ns->ns_max_unused = 0;
146                 ldlm_cancel_lru(ns, LDLM_SYNC);
147                 ns->ns_max_unused = tmp;
148                 return count;
149         }
150
151         tmp = simple_strtoul(dummy, &end, 0);
152         if (tmp == 0 && *end) {
153                 CERROR("invalid value written\n");
154                 return -EINVAL;
155         }
156
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].name = lock_name;
178
179         snprintf(lock_name, MAX_STRING_SIZE, "%s/resource_count", ns->ns_name);
180         lock_vars[0].data = &ns->ns_refcount;
181         lock_vars[0].read_fptr = lprocfs_rd_atomic;
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         lock_vars[0].read_fptr = lprocfs_rd_atomic;
187         lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
188
189         if (ns->ns_client) {
190                 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_unused_count",
191                          ns->ns_name);
192                 lock_vars[0].data = &ns->ns_nr_unused;
193                 lock_vars[0].read_fptr = lprocfs_uint_rd;
194                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
195
196                 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_size",
197                          ns->ns_name);
198                 lock_vars[0].data = ns;
199                 lock_vars[0].read_fptr = lprocfs_read_lru_size;
200                 lock_vars[0].write_fptr = lprocfs_write_lru_size;
201                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
202         }
203 }
204 #undef MAX_STRING_SIZE
205 #else
206 #define ldlm_proc_namespace(ns) do {} while (0)
207 #endif /* LPROCFS */
208
209 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 client)
210 {
211         struct ldlm_namespace *ns = NULL;
212         struct list_head *bucket;
213         int rc;
214         ENTRY;
215
216         rc = ldlm_get_ref();
217         if (rc) {
218                 CERROR("ldlm_get_ref failed: %d\n", rc);
219                 RETURN(NULL);
220         }
221
222         OBD_ALLOC(ns, sizeof(*ns));
223         if (!ns)
224                 GOTO(out_ref, NULL);
225
226         OBD_VMALLOC(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
227         if (!ns->ns_hash)
228                 GOTO(out_ns, NULL);
229
230         OBD_ALLOC(ns->ns_name, strlen(name) + 1);
231         if (!ns->ns_name)
232                 GOTO(out_hash, NULL);
233
234         strcpy(ns->ns_name, name);
235
236         CFS_INIT_LIST_HEAD(&ns->ns_root_list);
237         ns->ns_refcount = 0;
238         ns->ns_client = client;
239         spin_lock_init(&ns->ns_hash_lock);
240         atomic_set(&ns->ns_locks, 0);
241         ns->ns_resources = 0;
242         cfs_waitq_init(&ns->ns_waitq);
243
244         for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
245              bucket--)
246                 CFS_INIT_LIST_HEAD(bucket);
247
248         CFS_INIT_LIST_HEAD(&ns->ns_unused_list);
249         ns->ns_nr_unused = 0;
250         ns->ns_max_unused = LDLM_DEFAULT_LRU_SIZE;
251         spin_lock_init(&ns->ns_unused_lock);
252
253         mutex_down(&ldlm_namespace_lock);
254         list_add(&ns->ns_list_chain, &ldlm_namespace_list);
255         mutex_up(&ldlm_namespace_lock);
256         ldlm_proc_namespace(ns);
257         RETURN(ns);
258
259 out_hash:
260         POISON(ns->ns_hash, 0x5a, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
261         OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
262 out_ns:
263         OBD_FREE(ns, sizeof(*ns));
264 out_ref:
265         ldlm_put_ref(0);
266         RETURN(NULL);
267 }
268
269 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
270
271 /* If flags contains FL_LOCAL_ONLY, don't try to tell the server, just cleanup.
272  * This is currently only used for recovery, and we make certain assumptions
273  * as a result--notably, that we shouldn't cancel locks with refs. -phil
274  *
275  * Called with the ns_lock held. */
276 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
277                              int flags)
278 {
279         struct list_head *tmp;
280         int rc = 0, client = res->lr_namespace->ns_client;
281         int local_only = (flags & LDLM_FL_LOCAL_ONLY);
282         ENTRY;
283
284
285         do {
286                 struct ldlm_lock *lock = NULL;
287
288                 /* first, we look for non-cleaned-yet lock
289                  * all cleaned locks are marked by CLEANED flag */
290                 lock_res(res);
291                 list_for_each(tmp, q) {
292                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
293                         if (lock->l_flags & LDLM_FL_CLEANED) {
294                                 lock = NULL;
295                                 continue;
296                         }
297                         LDLM_LOCK_GET(lock);
298                         lock->l_flags |= LDLM_FL_CLEANED;
299                         break;
300                 }
301
302                 if (lock == NULL) {
303                         unlock_res(res);
304                         break;
305                 }
306
307                 /* Set CBPENDING so nothing in the cancellation path
308                  * can match this lock */
309                 lock->l_flags |= LDLM_FL_CBPENDING;
310                 lock->l_flags |= LDLM_FL_FAILED;
311                 lock->l_flags |= flags;
312
313                 if (local_only && (lock->l_readers || lock->l_writers)) {
314                         /* This is a little bit gross, but much better than the
315                          * alternative: pretend that we got a blocking AST from
316                          * the server, so that when the lock is decref'd, it
317                          * will go away ... */
318                         /* ... without sending a CANCEL message. */
319                         lock->l_flags |= LDLM_FL_LOCAL_ONLY;
320                         unlock_res(res);
321                         LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
322                         if (lock->l_completion_ast)
323                                 lock->l_completion_ast(lock, 0, NULL);
324                         LDLM_LOCK_PUT(lock);
325                         continue;
326                 }
327
328                 if (client) {
329                         struct lustre_handle lockh;
330
331                         unlock_res(res);
332                         ldlm_lock2handle(lock, &lockh);
333                         if (!local_only) {
334                                 rc = ldlm_cli_cancel(&lockh);
335                                 if (rc)
336                                         CERROR("ldlm_cli_cancel: %d\n", rc);
337                         }
338                         /* Force local cleanup on errors, too. */
339                         if (local_only || rc != ELDLM_OK)
340                                 ldlm_lock_cancel(lock);
341                 } else {
342                         ldlm_resource_unlink_lock(lock);
343                         unlock_res(res);
344                         LDLM_DEBUG(lock, "Freeing a lock still held by a "
345                                    "client node");
346                         ldlm_lock_destroy(lock);
347                 }
348                 LDLM_LOCK_PUT(lock);
349         } while (1);
350
351         EXIT;
352 }
353
354 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int flags)
355 {
356         struct list_head *tmp;
357         int i;
358
359         if (ns == NULL) {
360                 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
361                 return ELDLM_OK;
362         }
363
364         for (i = 0; i < RES_HASH_SIZE; i++) {
365                 spin_lock(&ns->ns_hash_lock);
366                 tmp = ns->ns_hash[i].next;
367                 while (tmp != &(ns->ns_hash[i])) {
368                         struct ldlm_resource *res;
369                         res = list_entry(tmp, struct ldlm_resource, lr_hash);
370                         ldlm_resource_getref(res);
371                         spin_unlock(&ns->ns_hash_lock);
372
373                         cleanup_resource(res, &res->lr_granted, flags);
374                         cleanup_resource(res, &res->lr_converting, flags);
375                         cleanup_resource(res, &res->lr_waiting, flags);
376
377                         spin_lock(&ns->ns_hash_lock);
378                         tmp  = tmp->next;
379
380                         /* XXX: former stuff caused issues in case of race
381                          * between ldlm_namespace_cleanup() and lockd() when
382                          * client gets blocking ast when lock gets distracted by
383                          * server. This is 1_4 branch solution, let's see how
384                          * will it behave. */
385                         if (!ldlm_resource_putref_locked(res))
386                                 CDEBUG(D_INFO,
387                                        "Namespace %s resource refcount nonzero "
388                                        "(%d) after lock cleanup; forcing cleanup.\n",
389                                        ns->ns_name, atomic_read(&res->lr_refcount));
390                 }
391                 spin_unlock(&ns->ns_hash_lock);
392         }
393
394         return ELDLM_OK;
395 }
396
397 /* Cleanup, but also free, the namespace */
398 int ldlm_namespace_free(struct ldlm_namespace *ns, int force)
399 {
400         ENTRY;
401         if (!ns)
402                 RETURN(ELDLM_OK);
403
404         mutex_down(&ldlm_namespace_lock);
405         list_del(&ns->ns_list_chain);
406         mutex_up(&ldlm_namespace_lock);
407
408         /* At shutdown time, don't call the cancellation callback */
409         ldlm_namespace_cleanup(ns, 0);
410
411 #ifdef LPROCFS
412         {
413                 struct proc_dir_entry *dir;
414                 dir = lprocfs_srch(ldlm_ns_proc_dir, ns->ns_name);
415                 if (dir == NULL) {
416                         CERROR("dlm namespace %s has no procfs dir?\n",
417                                ns->ns_name);
418                 } else {
419                         lprocfs_remove(&dir);
420                 }
421         }
422 #endif
423
424         if (ns->ns_refcount > 0) {
425                 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
426                 int rc;
427                 CDEBUG(D_DLMTRACE,
428                        "dlm namespace %s free waiting on refcount %d\n",
429                        ns->ns_name, ns->ns_refcount);
430                 rc = l_wait_event(ns->ns_waitq,
431                                   ns->ns_refcount == 0, &lwi);
432                 if (ns->ns_refcount)
433                         LCONSOLE_ERROR_MSG(0x139, "Lock manager: wait for %s "
434                                            "namespace cleanup aborted with %d "
435                                            "resources in use. (%d)\nI'm going "
436                                            "to try to clean up anyway, but I "
437                                            "might need a reboot of this node.\n",
438                                             ns->ns_name, (int) ns->ns_refcount, 
439                                             rc);
440                 CDEBUG(D_DLMTRACE,
441                        "dlm namespace %s free done waiting\n", ns->ns_name);
442         }
443
444         POISON(ns->ns_hash, 0x5a, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
445         OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
446         OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
447         OBD_FREE(ns, sizeof(*ns));
448
449         ldlm_put_ref(force);
450
451         RETURN(ELDLM_OK);
452 }
453
454 static __u32 ldlm_hash_fn(struct ldlm_resource *parent,
455                           const struct ldlm_res_id *name)
456 {
457         __u32 hash = 0;
458         int i;
459
460         for (i = 0; i < RES_NAME_SIZE; i++)
461                 hash += name->name[i];
462
463         hash += (__u32)((unsigned long)parent >> 4);
464
465         return (hash & RES_HASH_MASK);
466 }
467
468 static struct ldlm_resource *ldlm_resource_new(void)
469 {
470         struct ldlm_resource *res;
471
472         OBD_SLAB_ALLOC(res, ldlm_resource_slab, CFS_ALLOC_IO, sizeof *res);
473         if (res == NULL)
474                 return NULL;
475
476         memset(res, 0, sizeof(*res));
477
478         CFS_INIT_LIST_HEAD(&res->lr_children);
479         CFS_INIT_LIST_HEAD(&res->lr_childof);
480         CFS_INIT_LIST_HEAD(&res->lr_granted);
481         CFS_INIT_LIST_HEAD(&res->lr_converting);
482         CFS_INIT_LIST_HEAD(&res->lr_waiting);
483         atomic_set(&res->lr_refcount, 1);
484         spin_lock_init(&res->lr_lock);
485
486         /* one who creates the resource must unlock
487          * the semaphore after lvb initialization */
488         init_MUTEX_LOCKED(&res->lr_lvb_sem);
489
490         return res;
491 }
492
493 /* must be called with hash lock held */
494 static struct ldlm_resource *
495 ldlm_resource_find(struct ldlm_namespace *ns, const struct ldlm_res_id *name,
496                    __u32 hash)
497 {
498         struct list_head *bucket, *tmp;
499         struct ldlm_resource *res;
500
501         LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
502         bucket = ns->ns_hash + hash;
503
504         list_for_each(tmp, bucket) {
505                 res = list_entry(tmp, struct ldlm_resource, lr_hash);
506                 if (memcmp(&res->lr_name, name, sizeof(res->lr_name)) == 0)
507                         return res;
508         }
509
510         return NULL;
511 }
512
513 /* Args: locked namespace
514  * Returns: newly-allocated, referenced, unlocked resource */
515 static struct ldlm_resource *
516 ldlm_resource_add(struct ldlm_namespace *ns, struct ldlm_resource *parent,
517                   const struct ldlm_res_id *name, __u32 hash, ldlm_type_t type)
518 {
519         struct list_head *bucket;
520         struct ldlm_resource *res, *old_res;
521         ENTRY;
522
523         LASSERTF(type >= LDLM_MIN_TYPE && type < LDLM_MAX_TYPE,
524                  "type: %d\n", type);
525
526         res = ldlm_resource_new();
527         if (!res)
528                 RETURN(NULL);
529
530         res->lr_name = *name;
531         res->lr_namespace = ns;
532         res->lr_type = type;
533         res->lr_most_restr = LCK_NL;
534
535         spin_lock(&ns->ns_hash_lock);
536         old_res = ldlm_resource_find(ns, name, hash);
537         if (old_res) {
538                 /* someone won the race and added the resource before */
539                 ldlm_resource_getref(old_res);
540                 spin_unlock(&ns->ns_hash_lock);
541                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
542                 /* synchronize WRT resource creation */
543                 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
544                         down(&old_res->lr_lvb_sem);
545                         up(&old_res->lr_lvb_sem);
546                 }
547                 RETURN(old_res);
548         }
549
550         /* we won! let's add the resource */
551         bucket = ns->ns_hash + hash;
552         list_add(&res->lr_hash, bucket);
553         ns->ns_resources++;
554         ns->ns_refcount++;
555
556         if (parent == NULL) {
557                 list_add(&res->lr_childof, &ns->ns_root_list);
558         } else {
559                 res->lr_parent = parent;
560                 list_add(&res->lr_childof, &parent->lr_children);
561         }
562         spin_unlock(&ns->ns_hash_lock);
563
564         if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
565                 int rc;
566
567                 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CREATE_RESOURCE, 2);
568                 rc = ns->ns_lvbo->lvbo_init(res);
569                 if (rc)
570                         CERROR("lvbo_init failed for resource "
571                                LPU64": rc %d\n", name->name[0], rc);
572                 /* we create resource with locked lr_lvb_sem */
573                 up(&res->lr_lvb_sem);
574         }
575
576         RETURN(res);
577 }
578
579 /* Args: unlocked namespace
580  * Locks: takes and releases ns->ns_lock and res->lr_lock
581  * Returns: referenced, unlocked ldlm_resource or NULL */
582 struct ldlm_resource *
583 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
584                   const struct ldlm_res_id *name, ldlm_type_t type, int create)
585 {
586         __u32 hash = ldlm_hash_fn(parent, name);
587         struct ldlm_resource *res = NULL;
588         ENTRY;
589
590         LASSERT(ns != NULL);
591         LASSERT(ns->ns_hash != NULL);
592         LASSERT(name->name[0] != 0);
593
594         spin_lock(&ns->ns_hash_lock);
595         res = ldlm_resource_find(ns, name, hash);
596         if (res) {
597                 ldlm_resource_getref(res);
598                 spin_unlock(&ns->ns_hash_lock);
599                 /* synchronize WRT resource creation */
600                 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
601                         down(&res->lr_lvb_sem);
602                         up(&res->lr_lvb_sem);
603                 }
604                 RETURN(res);
605         }
606         spin_unlock(&ns->ns_hash_lock);
607
608         if (create == 0)
609                 RETURN(NULL);
610
611         res = ldlm_resource_add(ns, parent, name, hash, type);
612         RETURN(res);
613 }
614
615 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
616 {
617         LASSERT(res != NULL);
618         LASSERT(res != LP_POISON);
619         atomic_inc(&res->lr_refcount);
620         CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
621                atomic_read(&res->lr_refcount));
622         return res;
623 }
624
625 void __ldlm_resource_putref_final(struct ldlm_resource *res)
626 {
627         struct ldlm_namespace *ns = res->lr_namespace;
628
629         LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
630
631         if (!list_empty(&res->lr_granted)) {
632                 ldlm_resource_dump(D_ERROR, res);
633                 LBUG();
634         }
635
636         if (!list_empty(&res->lr_converting)) {
637                 ldlm_resource_dump(D_ERROR, res);
638                 LBUG();
639         }
640
641         if (!list_empty(&res->lr_waiting)) {
642                 ldlm_resource_dump(D_ERROR, res);
643                 LBUG();
644         }
645
646         if (!list_empty(&res->lr_children)) {
647                 ldlm_resource_dump(D_ERROR, res);
648                 LBUG();
649         }
650
651         ns->ns_refcount--;
652         list_del_init(&res->lr_hash);
653         list_del_init(&res->lr_childof);
654
655         ns->ns_resources--;
656         if (ns->ns_resources == 0)
657                 wake_up(&ns->ns_waitq);
658 }
659
660 /* Returns 1 if the resource was freed, 0 if it remains. */
661 int ldlm_resource_putref(struct ldlm_resource *res)
662 {
663         struct ldlm_namespace *ns = res->lr_namespace;
664         int rc = 0;
665         ENTRY;
666
667         CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
668                atomic_read(&res->lr_refcount) - 1);
669         LASSERTF(atomic_read(&res->lr_refcount) > 0, "%d",
670                  atomic_read(&res->lr_refcount));
671         LASSERTF(atomic_read(&res->lr_refcount) < LI_POISON, "%d",
672                  atomic_read(&res->lr_refcount));
673
674         if (atomic_dec_and_lock(&res->lr_refcount, &ns->ns_hash_lock)) {
675                 __ldlm_resource_putref_final(res);
676                 spin_unlock(&ns->ns_hash_lock);
677                 if (res->lr_lvb_data)
678                         OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
679                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
680                 rc = 1;
681         }
682
683         RETURN(rc);
684 }
685
686 /* Returns 1 if the resource was freed, 0 if it remains. */
687 int ldlm_resource_putref_locked(struct ldlm_resource *res)
688 {
689         int rc = 0;
690         ENTRY;
691
692         CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
693                atomic_read(&res->lr_refcount) - 1);
694         LASSERT(atomic_read(&res->lr_refcount) > 0);
695         LASSERT(atomic_read(&res->lr_refcount) < LI_POISON);
696
697         LASSERT(atomic_read(&res->lr_refcount) >= 0);
698         if (atomic_dec_and_test(&res->lr_refcount)) {
699                 __ldlm_resource_putref_final(res);
700                 if (res->lr_lvb_data)
701                         OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
702                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
703                 rc = 1;
704         }
705
706         RETURN(rc);
707 }
708
709 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
710                             struct ldlm_lock *lock)
711 {
712         check_res_locked(res);
713
714         ldlm_resource_dump(D_OTHER, res);
715         CDEBUG(D_OTHER, "About to add this lock:\n");
716         ldlm_lock_dump(D_OTHER, lock, 0);
717
718         if (lock->l_destroyed) {
719                 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
720                 return;
721         }
722
723         LASSERT(list_empty(&lock->l_res_link));
724
725         list_add_tail(&lock->l_res_link, head);
726 }
727
728 void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
729                                      struct ldlm_lock *new)
730 {
731         struct ldlm_resource *res = original->l_resource;
732
733         check_res_locked(res);
734
735         ldlm_resource_dump(D_OTHER, res);
736         CDEBUG(D_OTHER, "About to insert this lock after %p:\n", original);
737         ldlm_lock_dump(D_OTHER, new, 0);
738
739         if (new->l_destroyed) {
740                 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
741                 goto out;
742         }
743
744         LASSERT(list_empty(&new->l_res_link));
745
746         list_add(&new->l_res_link, &original->l_res_link);
747  out:;
748 }
749
750 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
751 {
752         check_res_locked(lock->l_resource);
753         ldlm_unlink_lock_skiplist(lock);
754         list_del_init(&lock->l_res_link);
755 }
756
757 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
758 {
759         desc->lr_type = res->lr_type;
760         desc->lr_name = res->lr_name;
761 }
762
763 void ldlm_dump_all_namespaces(int level)
764 {
765         struct list_head *tmp;
766
767         if (!((libcfs_debug | D_ERROR) & level))
768                 return;
769
770         mutex_down(&ldlm_namespace_lock);
771
772         list_for_each(tmp, &ldlm_namespace_list) {
773                 struct ldlm_namespace *ns;
774                 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
775                 ldlm_namespace_dump(level, ns);
776         }
777
778         mutex_up(&ldlm_namespace_lock);
779 }
780
781 void ldlm_namespace_dump(int level, struct ldlm_namespace *ns)
782 {
783         struct list_head *tmp;
784
785         if (!((libcfs_debug | D_ERROR) & level))
786                 return;
787
788         CDEBUG(level, "--- Namespace: %s (rc: %d, client: %d)\n",
789                   ns->ns_name, ns->ns_refcount, ns->ns_client);
790
791         if (cfs_time_before(cfs_time_current(), ns->ns_next_dump))
792                 return;
793
794         spin_lock(&ns->ns_hash_lock);
795         tmp = ns->ns_root_list.next;
796         while (tmp != &ns->ns_root_list) {
797                 struct ldlm_resource *res;
798                 res = list_entry(tmp, struct ldlm_resource, lr_childof);
799
800                 ldlm_resource_getref(res);
801                 spin_unlock(&ns->ns_hash_lock);
802
803                 lock_res(res);
804                 ldlm_resource_dump(level, res);
805                 unlock_res(res);
806
807                 spin_lock(&ns->ns_hash_lock);
808                 tmp = tmp->next;
809                 ldlm_resource_putref_locked(res);
810         }
811         ns->ns_next_dump = cfs_time_shift(10);
812         spin_unlock(&ns->ns_hash_lock);
813 }
814
815 void ldlm_resource_dump(int level, struct ldlm_resource *res)
816 {
817         struct list_head *tmp;
818         int pos;
819
820         CLASSERT(RES_NAME_SIZE == 4);
821
822         if (!((libcfs_debug | D_ERROR) & level))
823                 return;
824
825         CDEBUG(level, "--- Resource: %p ("LPU64"/"LPU64"/"LPU64"/"LPU64
826                ") (rc: %d)\n", res, res->lr_name.name[0], res->lr_name.name[1],
827                res->lr_name.name[2], res->lr_name.name[3],
828                atomic_read(&res->lr_refcount));
829
830         if (!list_empty(&res->lr_granted)) {
831                 pos = 0;
832                 CDEBUG(level, "Granted locks:\n");
833                 list_for_each(tmp, &res->lr_granted) {
834                         struct ldlm_lock *lock;
835                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
836                         ldlm_lock_dump(level, lock, ++pos);
837                 }
838         }
839         if (!list_empty(&res->lr_converting)) {
840                 pos = 0;
841                 CDEBUG(level, "Converting locks:\n");
842                 list_for_each(tmp, &res->lr_converting) {
843                         struct ldlm_lock *lock;
844                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
845                         ldlm_lock_dump(level, lock, ++pos);
846                 }
847         }
848         if (!list_empty(&res->lr_waiting)) {
849                 pos = 0;
850                 CDEBUG(level, "Waiting locks:\n");
851                 list_for_each(tmp, &res->lr_waiting) {
852                         struct ldlm_lock *lock;
853                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
854                         ldlm_lock_dump(level, lock, ++pos);
855                 }
856         }
857 }