1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
5 * Author: Phil Schwan <phil@clusterfs.com>
6 * Author: Peter Braam <braam@clusterfs.com>
8 * This file is part of the Lustre file system, http://www.lustre.org
9 * Lustre is a trademark of Cluster File Systems, Inc.
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.
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.
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.
27 #define DEBUG_SUBSYSTEM S_LDLM
29 # include <lustre_dlm.h>
31 # include <liblustre.h>
34 #include <obd_class.h>
35 #include "ldlm_internal.h"
37 cfs_mem_cache_t *ldlm_resource_slab, *ldlm_lock_slab;
39 atomic_t ldlm_srv_namespace_nr = ATOMIC_INIT(0);
40 atomic_t ldlm_cli_namespace_nr = ATOMIC_INIT(0);
42 struct semaphore ldlm_srv_namespace_lock;
43 struct list_head ldlm_srv_namespace_list =
44 CFS_LIST_HEAD_INIT(ldlm_srv_namespace_list);
46 struct semaphore ldlm_cli_namespace_lock;
47 struct list_head ldlm_cli_namespace_list =
48 CFS_LIST_HEAD_INIT(ldlm_cli_namespace_list);
50 cfs_proc_dir_entry_t *ldlm_type_proc_dir = NULL;
51 cfs_proc_dir_entry_t *ldlm_ns_proc_dir = NULL;
52 cfs_proc_dir_entry_t *ldlm_svc_proc_dir = NULL;
55 static int ldlm_proc_dump_ns(struct file *file, const char *buffer,
56 unsigned long count, void *data)
58 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
59 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
63 int ldlm_proc_setup(void)
66 struct lprocfs_vars list[] = {
67 { "dump_namespaces", NULL, ldlm_proc_dump_ns, NULL },
70 LASSERT(ldlm_ns_proc_dir == NULL);
72 ldlm_type_proc_dir = lprocfs_register(OBD_LDLM_DEVICENAME,
75 if (IS_ERR(ldlm_type_proc_dir)) {
76 CERROR("LProcFS failed in ldlm-init\n");
77 rc = PTR_ERR(ldlm_type_proc_dir);
81 ldlm_ns_proc_dir = lprocfs_register("namespaces",
84 if (IS_ERR(ldlm_ns_proc_dir)) {
85 CERROR("LProcFS failed in ldlm-init\n");
86 rc = PTR_ERR(ldlm_ns_proc_dir);
90 ldlm_svc_proc_dir = lprocfs_register("services",
93 if (IS_ERR(ldlm_svc_proc_dir)) {
94 CERROR("LProcFS failed in ldlm-init\n");
95 rc = PTR_ERR(ldlm_svc_proc_dir);
99 rc = lprocfs_add_vars(ldlm_type_proc_dir, list, NULL);
104 lprocfs_remove(&ldlm_ns_proc_dir);
106 lprocfs_remove(&ldlm_type_proc_dir);
108 ldlm_svc_proc_dir = NULL;
112 void ldlm_proc_cleanup(void)
114 if (ldlm_svc_proc_dir)
115 lprocfs_remove(&ldlm_svc_proc_dir);
117 if (ldlm_ns_proc_dir)
118 lprocfs_remove(&ldlm_ns_proc_dir);
120 if (ldlm_type_proc_dir)
121 lprocfs_remove(&ldlm_type_proc_dir);
124 static int lprocfs_rd_lru_size(char *page, char **start, off_t off,
125 int count, int *eof, void *data)
127 struct ldlm_namespace *ns = data;
128 __u32 *nr = &ns->ns_max_unused;
130 if (ns_connect_lru_resize(ns))
131 nr = &ns->ns_nr_unused;
132 return lprocfs_rd_uint(page, start, off, count, eof, nr);
135 static int lprocfs_wr_lru_size(struct file *file, const char *buffer,
136 unsigned long count, void *data)
138 struct ldlm_namespace *ns = data;
139 char dummy[MAX_STRING_SIZE + 1], *end;
142 dummy[MAX_STRING_SIZE] = '\0';
143 if (copy_from_user(dummy, buffer, MAX_STRING_SIZE))
146 if (count == 6 && memcmp(dummy, "clear", 5) == 0) {
148 "dropping all unused locks from namespace %s\n",
150 if (ns_connect_lru_resize(ns)) {
151 int canceled, unused = ns->ns_nr_unused;
153 /* Try to cancel all @ns_nr_unused locks. */
154 canceled = ldlm_cancel_lru(ns, unused, LDLM_SYNC);
155 if (canceled < unused) {
156 CERROR("not all requested locks are canceled, "
157 "requested: %d, canceled: %d\n", unused,
162 tmp = ns->ns_max_unused;
163 ns->ns_max_unused = 0;
164 ldlm_cancel_lru(ns, 0, LDLM_SYNC);
165 ns->ns_max_unused = tmp;
170 tmp = simple_strtoul(dummy, &end, 0);
172 CERROR("invalid value written\n");
176 if (ns_connect_lru_resize(ns)) {
177 if (tmp > ns->ns_nr_unused)
178 tmp = ns->ns_nr_unused;
179 tmp = ns->ns_nr_unused - tmp;
181 CDEBUG(D_DLMTRACE, "changing namespace %s unused locks from %u to %u\n",
182 ns->ns_name, ns->ns_nr_unused, (unsigned int)tmp);
183 ldlm_cancel_lru(ns, (unsigned int)tmp, LDLM_ASYNC);
185 CDEBUG(D_DLMTRACE, "changing namespace %s max_unused from %u to %u\n",
186 ns->ns_name, ns->ns_max_unused, (unsigned int)tmp);
187 ns->ns_max_unused = (unsigned int)tmp;
188 ldlm_cancel_lru(ns, 0, LDLM_ASYNC);
194 void ldlm_proc_namespace(struct ldlm_namespace *ns)
196 struct lprocfs_vars lock_vars[2];
197 char lock_name[MAX_STRING_SIZE + 1];
200 LASSERT(ns->ns_name != NULL);
202 lock_name[MAX_STRING_SIZE] = '\0';
204 memset(lock_vars, 0, sizeof(lock_vars));
205 lock_vars[0].name = lock_name;
207 snprintf(lock_name, MAX_STRING_SIZE, "%s/resource_count", ns->ns_name);
208 lock_vars[0].data = &ns->ns_refcount;
209 lock_vars[0].read_fptr = lprocfs_rd_atomic;
210 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
212 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_count", ns->ns_name);
213 lock_vars[0].data = &ns->ns_locks;
214 lock_vars[0].read_fptr = lprocfs_rd_atomic;
215 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
217 if (ns_is_client(ns)) {
218 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_unused_count",
220 lock_vars[0].data = &ns->ns_nr_unused;
221 lock_vars[0].read_fptr = lprocfs_rd_uint;
222 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
224 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_size",
226 lock_vars[0].data = ns;
227 lock_vars[0].read_fptr = lprocfs_rd_lru_size;
228 lock_vars[0].write_fptr = lprocfs_wr_lru_size;
229 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
231 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_max_age",
233 lock_vars[0].data = &ns->ns_max_age;
234 lock_vars[0].read_fptr = lprocfs_rd_uint;
235 lock_vars[0].write_fptr = lprocfs_wr_uint;
236 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
240 #undef MAX_STRING_SIZE
242 #define ldlm_proc_namespace(ns) do {} while (0)
245 struct ldlm_namespace *ldlm_namespace_new(char *name, ldlm_side_t client,
248 struct ldlm_namespace *ns = NULL;
249 struct list_head *bucket;
250 int rc, idx, namelen;
255 CERROR("ldlm_get_ref failed: %d\n", rc);
263 OBD_VMALLOC(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
267 ns->ns_appetite = apt;
268 namelen = strlen(name);
269 OBD_ALLOC(ns->ns_name, namelen + 1);
271 GOTO(out_hash, NULL);
273 strcpy(ns->ns_name, name);
275 CFS_INIT_LIST_HEAD(&ns->ns_root_list);
277 ns->ns_client = client;
278 spin_lock_init(&ns->ns_hash_lock);
279 atomic_set(&ns->ns_locks, 0);
280 ns->ns_resources = 0;
281 cfs_waitq_init(&ns->ns_waitq);
283 for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
285 CFS_INIT_LIST_HEAD(bucket);
287 CFS_INIT_LIST_HEAD(&ns->ns_unused_list);
288 ns->ns_nr_unused = 0;
289 ns->ns_max_unused = LDLM_DEFAULT_LRU_SIZE;
290 ns->ns_max_age = LDLM_DEFAULT_MAX_ALIVE;
291 spin_lock_init(&ns->ns_unused_lock);
292 ns->ns_connect_flags = 0;
293 ldlm_proc_namespace(ns);
295 idx = atomic_read(ldlm_namespace_nr(client));
296 rc = ldlm_pool_init(&ns->ns_pool, ns, idx, client);
298 CERROR("Can't initialize lock pool, rc %d\n", rc);
302 mutex_down(ldlm_namespace_lock(client));
303 list_add(&ns->ns_list_chain, ldlm_namespace_list(client));
304 atomic_inc(ldlm_namespace_nr(client));
305 mutex_up(ldlm_namespace_lock(client));
309 ldlm_namespace_cleanup(ns, 0);
310 OBD_FREE(ns->ns_name, namelen + 1);
312 OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
320 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
322 /* If flags contains FL_LOCAL_ONLY, don't try to tell the server, just cleanup.
323 * This is currently only used for recovery, and we make certain assumptions
324 * as a result--notably, that we shouldn't cancel locks with refs. -phil
326 * Called with the ns_lock held. */
327 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
330 struct list_head *tmp;
331 int rc = 0, client = ns_is_client(res->lr_namespace);
332 int local_only = (flags & LDLM_FL_LOCAL_ONLY);
337 struct ldlm_lock *lock = NULL;
339 /* first, we look for non-cleaned-yet lock
340 * all cleaned locks are marked by CLEANED flag */
342 list_for_each(tmp, q) {
343 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
344 if (lock->l_flags & LDLM_FL_CLEANED) {
349 lock->l_flags |= LDLM_FL_CLEANED;
358 /* Set CBPENDING so nothing in the cancellation path
359 * can match this lock */
360 lock->l_flags |= LDLM_FL_CBPENDING;
361 lock->l_flags |= LDLM_FL_FAILED;
362 lock->l_flags |= flags;
364 /* ... without sending a CANCEL message for local_only. */
366 lock->l_flags |= LDLM_FL_LOCAL_ONLY;
368 if (local_only && (lock->l_readers || lock->l_writers)) {
369 /* This is a little bit gross, but much better than the
370 * alternative: pretend that we got a blocking AST from
371 * the server, so that when the lock is decref'd, it
372 * will go away ... */
374 LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
375 if (lock->l_completion_ast)
376 lock->l_completion_ast(lock, 0, NULL);
382 struct lustre_handle lockh;
385 ldlm_lock2handle(lock, &lockh);
386 rc = ldlm_cli_cancel(&lockh);
388 CERROR("ldlm_cli_cancel: %d\n", rc);
390 ldlm_resource_unlink_lock(lock);
392 LDLM_DEBUG(lock, "Freeing a lock still held by a "
394 ldlm_lock_destroy(lock);
402 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int flags)
404 struct list_head *tmp;
408 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
412 for (i = 0; i < RES_HASH_SIZE; i++) {
413 spin_lock(&ns->ns_hash_lock);
414 tmp = ns->ns_hash[i].next;
415 while (tmp != &(ns->ns_hash[i])) {
416 struct ldlm_resource *res;
417 res = list_entry(tmp, struct ldlm_resource, lr_hash);
418 ldlm_resource_getref(res);
419 spin_unlock(&ns->ns_hash_lock);
421 cleanup_resource(res, &res->lr_granted, flags);
422 cleanup_resource(res, &res->lr_converting, flags);
423 cleanup_resource(res, &res->lr_waiting, flags);
425 spin_lock(&ns->ns_hash_lock);
428 /* XXX: former stuff caused issues in case of race
429 * between ldlm_namespace_cleanup() and lockd() when
430 * client gets blocking ast when lock gets distracted by
431 * server. This is 1_4 branch solution, let's see how
433 if (!ldlm_resource_putref_locked(res))
435 "Namespace %s resource refcount nonzero "
436 "(%d) after lock cleanup; forcing cleanup.\n",
437 ns->ns_name, atomic_read(&res->lr_refcount));
439 spin_unlock(&ns->ns_hash_lock);
445 /* Cleanup, but also free, the namespace */
446 int ldlm_namespace_free(struct ldlm_namespace *ns, int force)
452 mutex_down(ldlm_namespace_lock(ns->ns_client));
453 list_del(&ns->ns_list_chain);
454 atomic_dec(ldlm_namespace_nr(ns->ns_client));
455 ldlm_pool_fini(&ns->ns_pool);
456 mutex_up(ldlm_namespace_lock(ns->ns_client));
458 /* At shutdown time, don't call the cancellation callback */
459 ldlm_namespace_cleanup(ns, 0);
463 struct proc_dir_entry *dir;
464 dir = lprocfs_srch(ldlm_ns_proc_dir, ns->ns_name);
466 CERROR("dlm namespace %s has no procfs dir?\n",
469 lprocfs_remove(&dir);
474 if (ns->ns_refcount > 0) {
475 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
478 "dlm namespace %s free waiting on refcount %d\n",
479 ns->ns_name, ns->ns_refcount);
480 rc = l_wait_event(ns->ns_waitq,
481 ns->ns_refcount == 0, &lwi);
483 LCONSOLE_ERROR_MSG(0x139, "Lock manager: wait for %s "
484 "namespace cleanup aborted with %d "
485 "resources in use. (%d)\nI'm going "
486 "to try to clean up anyway, but I "
487 "might need a reboot of this node.\n",
488 ns->ns_name, (int) ns->ns_refcount,
491 "dlm namespace %s free done waiting\n", ns->ns_name);
494 OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
495 OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
503 void ldlm_namespace_get_nolock(struct ldlm_namespace *ns)
505 LASSERT(ns->ns_refcount >= 0);
509 void ldlm_namespace_get(struct ldlm_namespace *ns)
511 spin_lock(&ns->ns_hash_lock);
512 ldlm_namespace_get_nolock(ns);
513 spin_unlock(&ns->ns_hash_lock);
516 void ldlm_namespace_put_nolock(struct ldlm_namespace *ns, int wakeup)
518 LASSERT(ns->ns_refcount > 0);
520 if (ns->ns_refcount == 0 && wakeup)
521 wake_up(&ns->ns_waitq);
524 void ldlm_namespace_put(struct ldlm_namespace *ns, int wakeup)
526 spin_lock(&ns->ns_hash_lock);
527 ldlm_namespace_put_nolock(ns, wakeup);
528 spin_unlock(&ns->ns_hash_lock);
531 /* Should be called under ldlm_namespace_lock(client) taken */
532 void ldlm_namespace_move(struct ldlm_namespace *ns, ldlm_side_t client)
534 LASSERT(!list_empty(&ns->ns_list_chain));
535 LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
536 list_move_tail(&ns->ns_list_chain, ldlm_namespace_list(client));
539 /* Should be called under ldlm_namespace_lock(client) taken */
540 struct ldlm_namespace *ldlm_namespace_first(ldlm_side_t client)
542 LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
543 LASSERT(!list_empty(ldlm_namespace_list(client)));
544 return container_of(ldlm_namespace_list(client)->next,
545 struct ldlm_namespace, ns_list_chain);
547 static __u32 ldlm_hash_fn(struct ldlm_resource *parent,
548 const struct ldlm_res_id *name)
553 for (i = 0; i < RES_NAME_SIZE; i++)
554 hash += name->name[i];
556 hash += (__u32)((unsigned long)parent >> 4);
558 return (hash & RES_HASH_MASK);
561 static struct ldlm_resource *ldlm_resource_new(void)
563 struct ldlm_resource *res;
565 OBD_SLAB_ALLOC(res, ldlm_resource_slab, CFS_ALLOC_IO, sizeof *res);
569 memset(res, 0, sizeof(*res));
571 CFS_INIT_LIST_HEAD(&res->lr_children);
572 CFS_INIT_LIST_HEAD(&res->lr_childof);
573 CFS_INIT_LIST_HEAD(&res->lr_granted);
574 CFS_INIT_LIST_HEAD(&res->lr_converting);
575 CFS_INIT_LIST_HEAD(&res->lr_waiting);
576 atomic_set(&res->lr_refcount, 1);
577 spin_lock_init(&res->lr_lock);
579 /* one who creates the resource must unlock
580 * the semaphore after lvb initialization */
581 init_MUTEX_LOCKED(&res->lr_lvb_sem);
586 /* must be called with hash lock held */
587 static struct ldlm_resource *
588 ldlm_resource_find(struct ldlm_namespace *ns, const struct ldlm_res_id *name,
591 struct list_head *bucket, *tmp;
592 struct ldlm_resource *res;
594 LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
595 bucket = ns->ns_hash + hash;
597 list_for_each(tmp, bucket) {
598 res = list_entry(tmp, struct ldlm_resource, lr_hash);
599 if (memcmp(&res->lr_name, name, sizeof(res->lr_name)) == 0)
606 /* Args: locked namespace
607 * Returns: newly-allocated, referenced, unlocked resource */
608 static struct ldlm_resource *
609 ldlm_resource_add(struct ldlm_namespace *ns, struct ldlm_resource *parent,
610 const struct ldlm_res_id *name, __u32 hash, ldlm_type_t type)
612 struct list_head *bucket;
613 struct ldlm_resource *res, *old_res;
616 LASSERTF(type >= LDLM_MIN_TYPE && type < LDLM_MAX_TYPE,
619 res = ldlm_resource_new();
623 res->lr_name = *name;
624 res->lr_namespace = ns;
626 res->lr_most_restr = LCK_NL;
628 spin_lock(&ns->ns_hash_lock);
629 old_res = ldlm_resource_find(ns, name, hash);
631 /* someone won the race and added the resource before */
632 ldlm_resource_getref(old_res);
633 spin_unlock(&ns->ns_hash_lock);
634 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
635 /* synchronize WRT resource creation */
636 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
637 down(&old_res->lr_lvb_sem);
638 up(&old_res->lr_lvb_sem);
643 /* we won! let's add the resource */
644 bucket = ns->ns_hash + hash;
645 list_add(&res->lr_hash, bucket);
647 ldlm_namespace_get_nolock(ns);
649 if (parent == NULL) {
650 list_add(&res->lr_childof, &ns->ns_root_list);
652 res->lr_parent = parent;
653 list_add(&res->lr_childof, &parent->lr_children);
655 spin_unlock(&ns->ns_hash_lock);
657 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
660 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CREATE_RESOURCE, 2);
661 rc = ns->ns_lvbo->lvbo_init(res);
663 CERROR("lvbo_init failed for resource "
664 LPU64": rc %d\n", name->name[0], rc);
665 /* we create resource with locked lr_lvb_sem */
666 up(&res->lr_lvb_sem);
672 /* Args: unlocked namespace
673 * Locks: takes and releases ns->ns_lock and res->lr_lock
674 * Returns: referenced, unlocked ldlm_resource or NULL */
675 struct ldlm_resource *
676 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
677 const struct ldlm_res_id *name, ldlm_type_t type, int create)
679 __u32 hash = ldlm_hash_fn(parent, name);
680 struct ldlm_resource *res = NULL;
684 LASSERT(ns->ns_hash != NULL);
685 LASSERT(name->name[0] != 0);
687 spin_lock(&ns->ns_hash_lock);
688 res = ldlm_resource_find(ns, name, hash);
690 ldlm_resource_getref(res);
691 spin_unlock(&ns->ns_hash_lock);
692 /* synchronize WRT resource creation */
693 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
694 down(&res->lr_lvb_sem);
695 up(&res->lr_lvb_sem);
699 spin_unlock(&ns->ns_hash_lock);
704 res = ldlm_resource_add(ns, parent, name, hash, type);
708 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
710 LASSERT(res != NULL);
711 LASSERT(res != LP_POISON);
712 atomic_inc(&res->lr_refcount);
713 CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
714 atomic_read(&res->lr_refcount));
718 void __ldlm_resource_putref_final(struct ldlm_resource *res)
720 struct ldlm_namespace *ns = res->lr_namespace;
722 LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
724 if (!list_empty(&res->lr_granted)) {
725 ldlm_resource_dump(D_ERROR, res);
729 if (!list_empty(&res->lr_converting)) {
730 ldlm_resource_dump(D_ERROR, res);
734 if (!list_empty(&res->lr_waiting)) {
735 ldlm_resource_dump(D_ERROR, res);
739 if (!list_empty(&res->lr_children)) {
740 ldlm_resource_dump(D_ERROR, res);
744 /* Pass 0 here to not wake ->ns_waitq up yet, we will do it few
745 * lines below when all children are freed. */
746 ldlm_namespace_put_nolock(ns, 0);
747 list_del_init(&res->lr_hash);
748 list_del_init(&res->lr_childof);
751 if (ns->ns_resources == 0)
752 wake_up(&ns->ns_waitq);
755 /* Returns 1 if the resource was freed, 0 if it remains. */
756 int ldlm_resource_putref(struct ldlm_resource *res)
758 struct ldlm_namespace *ns = res->lr_namespace;
762 CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
763 atomic_read(&res->lr_refcount) - 1);
764 LASSERTF(atomic_read(&res->lr_refcount) > 0, "%d",
765 atomic_read(&res->lr_refcount));
766 LASSERTF(atomic_read(&res->lr_refcount) < LI_POISON, "%d",
767 atomic_read(&res->lr_refcount));
769 if (atomic_dec_and_lock(&res->lr_refcount, &ns->ns_hash_lock)) {
770 __ldlm_resource_putref_final(res);
771 spin_unlock(&ns->ns_hash_lock);
772 if (res->lr_lvb_data)
773 OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
774 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
781 /* Returns 1 if the resource was freed, 0 if it remains. */
782 int ldlm_resource_putref_locked(struct ldlm_resource *res)
787 CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
788 atomic_read(&res->lr_refcount) - 1);
789 LASSERT(atomic_read(&res->lr_refcount) > 0);
790 LASSERT(atomic_read(&res->lr_refcount) < LI_POISON);
792 LASSERT(atomic_read(&res->lr_refcount) >= 0);
793 if (atomic_dec_and_test(&res->lr_refcount)) {
794 __ldlm_resource_putref_final(res);
795 if (res->lr_lvb_data)
796 OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
797 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
804 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
805 struct ldlm_lock *lock)
807 check_res_locked(res);
809 ldlm_resource_dump(D_OTHER, res);
810 CDEBUG(D_OTHER, "About to add this lock:\n");
811 ldlm_lock_dump(D_OTHER, lock, 0);
813 if (lock->l_destroyed) {
814 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
818 LASSERT(list_empty(&lock->l_res_link));
820 list_add_tail(&lock->l_res_link, head);
823 void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
824 struct ldlm_lock *new)
826 struct ldlm_resource *res = original->l_resource;
828 check_res_locked(res);
830 ldlm_resource_dump(D_OTHER, res);
831 CDEBUG(D_OTHER, "About to insert this lock after %p:\n", original);
832 ldlm_lock_dump(D_OTHER, new, 0);
834 if (new->l_destroyed) {
835 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
839 LASSERT(list_empty(&new->l_res_link));
841 list_add(&new->l_res_link, &original->l_res_link);
845 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
847 check_res_locked(lock->l_resource);
848 ldlm_unlink_lock_skiplist(lock);
849 list_del_init(&lock->l_res_link);
852 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
854 desc->lr_type = res->lr_type;
855 desc->lr_name = res->lr_name;
858 void ldlm_dump_all_namespaces(ldlm_side_t client, int level)
860 struct list_head *tmp;
862 if (!((libcfs_debug | D_ERROR) & level))
865 mutex_down(ldlm_namespace_lock(client));
867 list_for_each(tmp, ldlm_namespace_list(client)) {
868 struct ldlm_namespace *ns;
869 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
870 ldlm_namespace_dump(level, ns);
873 mutex_up(ldlm_namespace_lock(client));
876 void ldlm_namespace_dump(int level, struct ldlm_namespace *ns)
878 struct list_head *tmp;
880 if (!((libcfs_debug | D_ERROR) & level))
883 CDEBUG(level, "--- Namespace: %s (rc: %d, side: %s)\n",
884 ns->ns_name, ns->ns_refcount,
885 ns_is_client(ns) ? "client" : "server");
887 if (cfs_time_before(cfs_time_current(), ns->ns_next_dump))
890 spin_lock(&ns->ns_hash_lock);
891 tmp = ns->ns_root_list.next;
892 while (tmp != &ns->ns_root_list) {
893 struct ldlm_resource *res;
894 res = list_entry(tmp, struct ldlm_resource, lr_childof);
896 ldlm_resource_getref(res);
897 spin_unlock(&ns->ns_hash_lock);
900 ldlm_resource_dump(level, res);
903 spin_lock(&ns->ns_hash_lock);
905 ldlm_resource_putref_locked(res);
907 ns->ns_next_dump = cfs_time_shift(10);
908 spin_unlock(&ns->ns_hash_lock);
911 void ldlm_resource_dump(int level, struct ldlm_resource *res)
913 struct list_head *tmp;
916 CLASSERT(RES_NAME_SIZE == 4);
918 if (!((libcfs_debug | D_ERROR) & level))
921 CDEBUG(level, "--- Resource: %p ("LPU64"/"LPU64"/"LPU64"/"LPU64
922 ") (rc: %d)\n", res, res->lr_name.name[0], res->lr_name.name[1],
923 res->lr_name.name[2], res->lr_name.name[3],
924 atomic_read(&res->lr_refcount));
926 if (!list_empty(&res->lr_granted)) {
928 CDEBUG(level, "Granted locks:\n");
929 list_for_each(tmp, &res->lr_granted) {
930 struct ldlm_lock *lock;
931 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
932 ldlm_lock_dump(level, lock, ++pos);
935 if (!list_empty(&res->lr_converting)) {
937 CDEBUG(level, "Converting locks:\n");
938 list_for_each(tmp, &res->lr_converting) {
939 struct ldlm_lock *lock;
940 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
941 ldlm_lock_dump(level, lock, ++pos);
944 if (!list_empty(&res->lr_waiting)) {
946 CDEBUG(level, "Waiting locks:\n");
947 list_for_each(tmp, &res->lr_waiting) {
948 struct ldlm_lock *lock;
949 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
950 ldlm_lock_dump(level, lock, ++pos);