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