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;
143 dummy[MAX_STRING_SIZE] = '\0';
144 if (copy_from_user(dummy, buffer, MAX_STRING_SIZE))
147 if (count == 6 && memcmp(dummy, "clear", 5) == 0) {
149 "dropping all unused locks from namespace %s\n",
151 if (ns_connect_lru_resize(ns)) {
152 int canceled, unused = ns->ns_nr_unused;
154 /* Try to cancel all @ns_nr_unused locks. */
155 canceled = ldlm_cancel_lru(ns, unused, LDLM_SYNC,
157 if (canceled < unused) {
158 CERROR("not all requested locks are canceled, "
159 "requested: %d, canceled: %d\n", unused,
164 tmp = ns->ns_max_unused;
165 ns->ns_max_unused = 0;
166 ldlm_cancel_lru(ns, 0, LDLM_SYNC, LDLM_CANCEL_PASSED);
167 ns->ns_max_unused = tmp;
172 tmp = simple_strtoul(dummy, &end, 0);
174 CERROR("invalid value written\n");
177 lru_resize = (tmp == 0);
179 if (ns_connect_lru_resize(ns)) {
181 ns->ns_max_unused = (unsigned int)tmp;
183 if (tmp > ns->ns_nr_unused)
184 tmp = ns->ns_nr_unused;
185 tmp = ns->ns_nr_unused - tmp;
187 CDEBUG(D_DLMTRACE, "changing namespace %s unused locks from %u to %u\n",
188 ns->ns_name, ns->ns_nr_unused, (unsigned int)tmp);
189 ldlm_cancel_lru(ns, (unsigned int)tmp, LDLM_ASYNC, LDLM_CANCEL_PASSED);
192 CDEBUG(D_DLMTRACE, "disable lru_resize for namespace %s\n",
194 ns->ns_connect_flags &= ~OBD_CONNECT_LRU_RESIZE;
197 CDEBUG(D_DLMTRACE, "changing namespace %s max_unused from %u to %u\n",
198 ns->ns_name, ns->ns_max_unused, (unsigned int)tmp);
199 ns->ns_max_unused = (unsigned int)tmp;
200 ldlm_cancel_lru(ns, 0, LDLM_ASYNC, LDLM_CANCEL_PASSED);
202 /* Make sure that originally lru resize was supported before
203 * turning it on here. */
205 (ns->ns_orig_connect_flags & OBD_CONNECT_LRU_RESIZE)) {
206 CDEBUG(D_DLMTRACE, "enable lru_resize for namespace %s\n",
208 ns->ns_connect_flags |= OBD_CONNECT_LRU_RESIZE;
215 void ldlm_proc_namespace(struct ldlm_namespace *ns)
217 struct lprocfs_vars lock_vars[2];
218 char lock_name[MAX_STRING_SIZE + 1];
221 LASSERT(ns->ns_name != NULL);
223 lock_name[MAX_STRING_SIZE] = '\0';
225 memset(lock_vars, 0, sizeof(lock_vars));
226 lock_vars[0].name = lock_name;
228 snprintf(lock_name, MAX_STRING_SIZE, "%s/resource_count", ns->ns_name);
229 lock_vars[0].data = &ns->ns_refcount;
230 lock_vars[0].read_fptr = lprocfs_rd_atomic;
231 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
233 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_count", ns->ns_name);
234 lock_vars[0].data = &ns->ns_locks;
235 lock_vars[0].read_fptr = lprocfs_rd_atomic;
236 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
238 if (ns_is_client(ns)) {
239 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_unused_count",
241 lock_vars[0].data = &ns->ns_nr_unused;
242 lock_vars[0].read_fptr = lprocfs_rd_uint;
243 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
245 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_size",
247 lock_vars[0].data = ns;
248 lock_vars[0].read_fptr = lprocfs_rd_lru_size;
249 lock_vars[0].write_fptr = lprocfs_wr_lru_size;
250 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
252 snprintf(lock_name, MAX_STRING_SIZE, "%s/shrink_thumb",
254 lock_vars[0].data = ns;
255 lock_vars[0].read_fptr = lprocfs_rd_uint;
256 lock_vars[0].write_fptr = lprocfs_wr_uint;
257 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
259 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_max_age",
261 lock_vars[0].data = &ns->ns_max_age;
262 lock_vars[0].read_fptr = lprocfs_rd_uint;
263 lock_vars[0].write_fptr = lprocfs_wr_uint;
264 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
267 #undef MAX_STRING_SIZE
269 #define ldlm_proc_namespace(ns) do {} while (0)
272 struct ldlm_namespace *ldlm_namespace_new(char *name, ldlm_side_t client,
275 struct ldlm_namespace *ns = NULL;
276 struct list_head *bucket;
277 int rc, idx, namelen;
282 CERROR("ldlm_get_ref failed: %d\n", rc);
290 OBD_VMALLOC(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
294 ns->ns_shrink_thumb = LDLM_LOCK_SHRINK_THUMB;
295 ns->ns_appetite = apt;
296 namelen = strlen(name);
297 OBD_ALLOC(ns->ns_name, namelen + 1);
299 GOTO(out_hash, NULL);
301 strcpy(ns->ns_name, name);
303 CFS_INIT_LIST_HEAD(&ns->ns_root_list);
305 ns->ns_client = client;
306 spin_lock_init(&ns->ns_hash_lock);
307 atomic_set(&ns->ns_locks, 0);
308 ns->ns_resources = 0;
309 cfs_waitq_init(&ns->ns_waitq);
311 for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
313 CFS_INIT_LIST_HEAD(bucket);
315 CFS_INIT_LIST_HEAD(&ns->ns_unused_list);
316 ns->ns_nr_unused = 0;
317 ns->ns_max_unused = LDLM_DEFAULT_LRU_SIZE;
318 ns->ns_max_age = LDLM_DEFAULT_MAX_ALIVE;
319 spin_lock_init(&ns->ns_unused_lock);
320 ns->ns_orig_connect_flags = 0;
321 ns->ns_connect_flags = 0;
322 ldlm_proc_namespace(ns);
324 idx = atomic_read(ldlm_namespace_nr(client));
325 rc = ldlm_pool_init(&ns->ns_pool, ns, idx, client);
327 CERROR("Can't initialize lock pool, rc %d\n", rc);
331 mutex_down(ldlm_namespace_lock(client));
332 list_add(&ns->ns_list_chain, ldlm_namespace_list(client));
333 atomic_inc(ldlm_namespace_nr(client));
334 mutex_up(ldlm_namespace_lock(client));
338 ldlm_namespace_cleanup(ns, 0);
339 OBD_FREE(ns->ns_name, namelen + 1);
341 OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
349 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
351 /* If flags contains FL_LOCAL_ONLY, don't try to tell the server, just cleanup.
352 * This is currently only used for recovery, and we make certain assumptions
353 * as a result--notably, that we shouldn't cancel locks with refs. -phil
355 * Called with the ns_lock held. */
356 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
359 struct list_head *tmp;
360 int rc = 0, client = ns_is_client(res->lr_namespace);
361 int local_only = (flags & LDLM_FL_LOCAL_ONLY);
366 struct ldlm_lock *lock = NULL;
368 /* first, we look for non-cleaned-yet lock
369 * all cleaned locks are marked by CLEANED flag */
371 list_for_each(tmp, q) {
372 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
373 if (lock->l_flags & LDLM_FL_CLEANED) {
378 lock->l_flags |= LDLM_FL_CLEANED;
387 /* Set CBPENDING so nothing in the cancellation path
388 * can match this lock */
389 lock->l_flags |= LDLM_FL_CBPENDING;
390 lock->l_flags |= LDLM_FL_FAILED;
391 lock->l_flags |= flags;
393 /* ... without sending a CANCEL message for local_only. */
395 lock->l_flags |= LDLM_FL_LOCAL_ONLY;
397 if (local_only && (lock->l_readers || lock->l_writers)) {
398 /* This is a little bit gross, but much better than the
399 * alternative: pretend that we got a blocking AST from
400 * the server, so that when the lock is decref'd, it
401 * will go away ... */
403 LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
404 if (lock->l_completion_ast)
405 lock->l_completion_ast(lock, 0, NULL);
411 struct lustre_handle lockh;
414 ldlm_lock2handle(lock, &lockh);
415 rc = ldlm_cli_cancel(&lockh);
417 CERROR("ldlm_cli_cancel: %d\n", rc);
419 ldlm_resource_unlink_lock(lock);
421 LDLM_DEBUG(lock, "Freeing a lock still held by a "
423 ldlm_lock_destroy(lock);
431 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int flags)
433 struct list_head *tmp;
437 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
441 for (i = 0; i < RES_HASH_SIZE; i++) {
442 spin_lock(&ns->ns_hash_lock);
443 tmp = ns->ns_hash[i].next;
444 while (tmp != &(ns->ns_hash[i])) {
445 struct ldlm_resource *res;
446 res = list_entry(tmp, struct ldlm_resource, lr_hash);
447 ldlm_resource_getref(res);
448 spin_unlock(&ns->ns_hash_lock);
450 cleanup_resource(res, &res->lr_granted, flags);
451 cleanup_resource(res, &res->lr_converting, flags);
452 cleanup_resource(res, &res->lr_waiting, flags);
454 spin_lock(&ns->ns_hash_lock);
457 /* XXX: former stuff caused issues in case of race
458 * between ldlm_namespace_cleanup() and lockd() when
459 * client gets blocking ast when lock gets distracted by
460 * server. This is 1_4 branch solution, let's see how
462 if (!ldlm_resource_putref_locked(res))
464 "Namespace %s resource refcount nonzero "
465 "(%d) after lock cleanup; forcing cleanup.\n",
466 ns->ns_name, atomic_read(&res->lr_refcount));
468 spin_unlock(&ns->ns_hash_lock);
474 /* Cleanup, but also free, the namespace */
475 int ldlm_namespace_free_prior(struct ldlm_namespace *ns)
481 mutex_down(ldlm_namespace_lock(ns->ns_client));
482 list_del(&ns->ns_list_chain);
483 atomic_dec(ldlm_namespace_nr(ns->ns_client));
484 ldlm_pool_fini(&ns->ns_pool);
485 mutex_up(ldlm_namespace_lock(ns->ns_client));
487 /* At shutdown time, don't call the cancellation callback */
488 ldlm_namespace_cleanup(ns, 0);
490 if (ns->ns_refcount > 0) {
491 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
494 "dlm namespace %s free waiting on refcount %d\n",
495 ns->ns_name, ns->ns_refcount);
496 rc = l_wait_event(ns->ns_waitq,
497 ns->ns_refcount == 0, &lwi);
499 LCONSOLE_ERROR_MSG(0x139, "Lock manager: wait for %s "
500 "namespace cleanup aborted with %d "
501 "resources in use. (%d)\nI'm going "
502 "to try to clean up anyway, but I "
503 "might need a reboot of this node.\n",
504 ns->ns_name, (int) ns->ns_refcount,
507 "dlm namespace %s free done waiting\n", ns->ns_name);
513 int ldlm_namespace_free_post(struct ldlm_namespace *ns, int force)
521 struct proc_dir_entry *dir;
522 dir = lprocfs_srch(ldlm_ns_proc_dir, ns->ns_name);
524 CERROR("dlm namespace %s has no procfs dir?\n",
527 lprocfs_remove(&dir);
531 OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
532 OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
539 /* Cleanup the resource, and free namespace.
542 * proc1: destroy import
543 * class_disconnect_export(grab cl_sem) ->
544 * -> ldlm_namespace_free ->
545 * -> lprocfs_remove(grab _lprocfs_lock).
546 * proc2: read proc info
547 * lprocfs_fops_read(grab _lprocfs_lock) ->
548 * -> osc_rd_active, etc(grab cl_sem).
550 * So that I have to split the ldlm_namespace_free into two parts - the first
551 * part ldlm_namespace_free_prior is used to cleanup the resource which is
552 * being used; the 2nd part ldlm_namespace_free_post is used to unregister the
553 * lprocfs entries, and then free memory. It will be called w/o cli->cl_sem
556 int ldlm_namespace_free(struct ldlm_namespace *ns, int force)
558 ldlm_namespace_free_prior(ns);
559 ldlm_namespace_free_post(ns, force);
564 void ldlm_namespace_get_nolock(struct ldlm_namespace *ns)
566 LASSERT(ns->ns_refcount >= 0);
570 void ldlm_namespace_get(struct ldlm_namespace *ns)
572 spin_lock(&ns->ns_hash_lock);
573 ldlm_namespace_get_nolock(ns);
574 spin_unlock(&ns->ns_hash_lock);
577 void ldlm_namespace_put_nolock(struct ldlm_namespace *ns, int wakeup)
579 LASSERT(ns->ns_refcount > 0);
581 if (ns->ns_refcount == 0 && wakeup)
582 wake_up(&ns->ns_waitq);
585 void ldlm_namespace_put(struct ldlm_namespace *ns, int wakeup)
587 spin_lock(&ns->ns_hash_lock);
588 ldlm_namespace_put_nolock(ns, wakeup);
589 spin_unlock(&ns->ns_hash_lock);
592 /* Should be called under ldlm_namespace_lock(client) taken */
593 void ldlm_namespace_move(struct ldlm_namespace *ns, ldlm_side_t client)
595 LASSERT(!list_empty(&ns->ns_list_chain));
596 LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
597 list_move_tail(&ns->ns_list_chain, ldlm_namespace_list(client));
600 /* Should be called under ldlm_namespace_lock(client) taken */
601 struct ldlm_namespace *ldlm_namespace_first(ldlm_side_t client)
603 LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
604 LASSERT(!list_empty(ldlm_namespace_list(client)));
605 return container_of(ldlm_namespace_list(client)->next,
606 struct ldlm_namespace, ns_list_chain);
608 static __u32 ldlm_hash_fn(struct ldlm_resource *parent,
609 const struct ldlm_res_id *name)
614 for (i = 0; i < RES_NAME_SIZE; i++)
615 hash += name->name[i];
617 hash += (__u32)((unsigned long)parent >> 4);
619 return (hash & RES_HASH_MASK);
622 static struct ldlm_resource *ldlm_resource_new(void)
624 struct ldlm_resource *res;
627 OBD_SLAB_ALLOC(res, ldlm_resource_slab, CFS_ALLOC_IO, sizeof *res);
631 memset(res, 0, sizeof(*res));
633 CFS_INIT_LIST_HEAD(&res->lr_children);
634 CFS_INIT_LIST_HEAD(&res->lr_childof);
635 CFS_INIT_LIST_HEAD(&res->lr_granted);
636 CFS_INIT_LIST_HEAD(&res->lr_converting);
637 CFS_INIT_LIST_HEAD(&res->lr_waiting);
639 /* initialize interval trees for each lock mode*/
640 for (idx = 0; idx < LCK_MODE_NUM; idx++) {
641 res->lr_itree[idx].lit_size = 0;
642 res->lr_itree[idx].lit_mode = 1 << idx;
643 res->lr_itree[idx].lit_root = NULL;
646 atomic_set(&res->lr_refcount, 1);
647 spin_lock_init(&res->lr_lock);
649 /* one who creates the resource must unlock
650 * the semaphore after lvb initialization */
651 init_MUTEX_LOCKED(&res->lr_lvb_sem);
656 /* must be called with hash lock held */
657 static struct ldlm_resource *
658 ldlm_resource_find(struct ldlm_namespace *ns, const struct ldlm_res_id *name,
661 struct list_head *bucket, *tmp;
662 struct ldlm_resource *res;
664 LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
665 bucket = ns->ns_hash + hash;
667 list_for_each(tmp, bucket) {
668 res = list_entry(tmp, struct ldlm_resource, lr_hash);
669 if (memcmp(&res->lr_name, name, sizeof(res->lr_name)) == 0)
676 /* Args: locked namespace
677 * Returns: newly-allocated, referenced, unlocked resource */
678 static struct ldlm_resource *
679 ldlm_resource_add(struct ldlm_namespace *ns, struct ldlm_resource *parent,
680 const struct ldlm_res_id *name, __u32 hash, ldlm_type_t type)
682 struct list_head *bucket;
683 struct ldlm_resource *res, *old_res;
686 LASSERTF(type >= LDLM_MIN_TYPE && type < LDLM_MAX_TYPE,
689 res = ldlm_resource_new();
693 res->lr_name = *name;
694 res->lr_namespace = ns;
696 res->lr_most_restr = LCK_NL;
698 spin_lock(&ns->ns_hash_lock);
699 old_res = ldlm_resource_find(ns, name, hash);
701 /* someone won the race and added the resource before */
702 ldlm_resource_getref(old_res);
703 spin_unlock(&ns->ns_hash_lock);
704 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
705 /* synchronize WRT resource creation */
706 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
707 down(&old_res->lr_lvb_sem);
708 up(&old_res->lr_lvb_sem);
713 /* we won! let's add the resource */
714 bucket = ns->ns_hash + hash;
715 list_add(&res->lr_hash, bucket);
717 ldlm_namespace_get_nolock(ns);
719 if (parent == NULL) {
720 list_add(&res->lr_childof, &ns->ns_root_list);
722 res->lr_parent = parent;
723 list_add(&res->lr_childof, &parent->lr_children);
725 spin_unlock(&ns->ns_hash_lock);
727 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
730 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CREATE_RESOURCE, 2);
731 rc = ns->ns_lvbo->lvbo_init(res);
733 CERROR("lvbo_init failed for resource "
734 LPU64": rc %d\n", name->name[0], rc);
735 /* we create resource with locked lr_lvb_sem */
736 up(&res->lr_lvb_sem);
742 /* Args: unlocked namespace
743 * Locks: takes and releases ns->ns_lock and res->lr_lock
744 * Returns: referenced, unlocked ldlm_resource or NULL */
745 struct ldlm_resource *
746 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
747 const struct ldlm_res_id *name, ldlm_type_t type, int create)
749 __u32 hash = ldlm_hash_fn(parent, name);
750 struct ldlm_resource *res = NULL;
754 LASSERT(ns->ns_hash != NULL);
755 LASSERT(name->name[0] != 0);
757 spin_lock(&ns->ns_hash_lock);
758 res = ldlm_resource_find(ns, name, hash);
760 ldlm_resource_getref(res);
761 spin_unlock(&ns->ns_hash_lock);
762 /* synchronize WRT resource creation */
763 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
764 down(&res->lr_lvb_sem);
765 up(&res->lr_lvb_sem);
769 spin_unlock(&ns->ns_hash_lock);
774 res = ldlm_resource_add(ns, parent, name, hash, type);
778 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
780 LASSERT(res != NULL);
781 LASSERT(res != LP_POISON);
782 atomic_inc(&res->lr_refcount);
783 CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
784 atomic_read(&res->lr_refcount));
788 void __ldlm_resource_putref_final(struct ldlm_resource *res)
790 struct ldlm_namespace *ns = res->lr_namespace;
792 LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
794 if (!list_empty(&res->lr_granted)) {
795 ldlm_resource_dump(D_ERROR, res);
799 if (!list_empty(&res->lr_converting)) {
800 ldlm_resource_dump(D_ERROR, res);
804 if (!list_empty(&res->lr_waiting)) {
805 ldlm_resource_dump(D_ERROR, res);
809 if (!list_empty(&res->lr_children)) {
810 ldlm_resource_dump(D_ERROR, res);
814 /* Pass 0 here to not wake ->ns_waitq up yet, we will do it few
815 * lines below when all children are freed. */
816 ldlm_namespace_put_nolock(ns, 0);
817 list_del_init(&res->lr_hash);
818 list_del_init(&res->lr_childof);
821 if (ns->ns_resources == 0)
822 wake_up(&ns->ns_waitq);
825 /* Returns 1 if the resource was freed, 0 if it remains. */
826 int ldlm_resource_putref(struct ldlm_resource *res)
828 struct ldlm_namespace *ns = res->lr_namespace;
832 CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
833 atomic_read(&res->lr_refcount) - 1);
834 LASSERTF(atomic_read(&res->lr_refcount) > 0, "%d",
835 atomic_read(&res->lr_refcount));
836 LASSERTF(atomic_read(&res->lr_refcount) < LI_POISON, "%d",
837 atomic_read(&res->lr_refcount));
839 if (atomic_dec_and_lock(&res->lr_refcount, &ns->ns_hash_lock)) {
840 __ldlm_resource_putref_final(res);
841 spin_unlock(&ns->ns_hash_lock);
842 if (res->lr_lvb_data)
843 OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
844 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
851 /* Returns 1 if the resource was freed, 0 if it remains. */
852 int ldlm_resource_putref_locked(struct ldlm_resource *res)
857 CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
858 atomic_read(&res->lr_refcount) - 1);
859 LASSERT(atomic_read(&res->lr_refcount) > 0);
860 LASSERT(atomic_read(&res->lr_refcount) < LI_POISON);
862 LASSERT(atomic_read(&res->lr_refcount) >= 0);
863 if (atomic_dec_and_test(&res->lr_refcount)) {
864 __ldlm_resource_putref_final(res);
865 if (res->lr_lvb_data)
866 OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
867 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
874 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
875 struct ldlm_lock *lock)
877 check_res_locked(res);
879 ldlm_resource_dump(D_OTHER, res);
880 CDEBUG(D_OTHER, "About to add this lock:\n");
881 ldlm_lock_dump(D_OTHER, lock, 0);
883 if (lock->l_destroyed) {
884 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
888 LASSERT(list_empty(&lock->l_res_link));
890 list_add_tail(&lock->l_res_link, head);
893 void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
894 struct ldlm_lock *new)
896 struct ldlm_resource *res = original->l_resource;
898 check_res_locked(res);
900 ldlm_resource_dump(D_OTHER, res);
901 CDEBUG(D_OTHER, "About to insert this lock after %p:\n", original);
902 ldlm_lock_dump(D_OTHER, new, 0);
904 if (new->l_destroyed) {
905 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
909 LASSERT(list_empty(&new->l_res_link));
911 list_add(&new->l_res_link, &original->l_res_link);
915 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
917 int type = lock->l_resource->lr_type;
919 check_res_locked(lock->l_resource);
920 if (type == LDLM_IBITS || type == LDLM_PLAIN)
921 ldlm_unlink_lock_skiplist(lock);
922 else if (type == LDLM_EXTENT)
923 ldlm_extent_unlink_lock(lock);
924 list_del_init(&lock->l_res_link);
927 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
929 desc->lr_type = res->lr_type;
930 desc->lr_name = res->lr_name;
933 void ldlm_dump_all_namespaces(ldlm_side_t client, int level)
935 struct list_head *tmp;
937 if (!((libcfs_debug | D_ERROR) & level))
940 mutex_down(ldlm_namespace_lock(client));
942 list_for_each(tmp, ldlm_namespace_list(client)) {
943 struct ldlm_namespace *ns;
944 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
945 ldlm_namespace_dump(level, ns);
948 mutex_up(ldlm_namespace_lock(client));
951 void ldlm_namespace_dump(int level, struct ldlm_namespace *ns)
953 struct list_head *tmp;
955 if (!((libcfs_debug | D_ERROR) & level))
958 CDEBUG(level, "--- Namespace: %s (rc: %d, side: %s)\n",
959 ns->ns_name, ns->ns_refcount,
960 ns_is_client(ns) ? "client" : "server");
962 if (cfs_time_before(cfs_time_current(), ns->ns_next_dump))
965 spin_lock(&ns->ns_hash_lock);
966 tmp = ns->ns_root_list.next;
967 while (tmp != &ns->ns_root_list) {
968 struct ldlm_resource *res;
969 res = list_entry(tmp, struct ldlm_resource, lr_childof);
971 ldlm_resource_getref(res);
972 spin_unlock(&ns->ns_hash_lock);
975 ldlm_resource_dump(level, res);
978 spin_lock(&ns->ns_hash_lock);
980 ldlm_resource_putref_locked(res);
982 ns->ns_next_dump = cfs_time_shift(10);
983 spin_unlock(&ns->ns_hash_lock);
986 void ldlm_resource_dump(int level, struct ldlm_resource *res)
988 struct list_head *tmp;
991 CLASSERT(RES_NAME_SIZE == 4);
993 if (!((libcfs_debug | D_ERROR) & level))
996 CDEBUG(level, "--- Resource: %p ("LPU64"/"LPU64"/"LPU64"/"LPU64
997 ") (rc: %d)\n", res, res->lr_name.name[0], res->lr_name.name[1],
998 res->lr_name.name[2], res->lr_name.name[3],
999 atomic_read(&res->lr_refcount));
1001 if (!list_empty(&res->lr_granted)) {
1003 CDEBUG(level, "Granted locks:\n");
1004 list_for_each(tmp, &res->lr_granted) {
1005 struct ldlm_lock *lock;
1006 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1007 ldlm_lock_dump(level, lock, ++pos);
1010 if (!list_empty(&res->lr_converting)) {
1012 CDEBUG(level, "Converting locks:\n");
1013 list_for_each(tmp, &res->lr_converting) {
1014 struct ldlm_lock *lock;
1015 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1016 ldlm_lock_dump(level, lock, ++pos);
1019 if (!list_empty(&res->lr_waiting)) {
1021 CDEBUG(level, "Waiting locks:\n");
1022 list_for_each(tmp, &res->lr_waiting) {
1023 struct ldlm_lock *lock;
1024 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1025 ldlm_lock_dump(level, lock, ++pos);