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 CFS_LIST_HEAD(ldlm_srv_namespace_list);
45 struct semaphore ldlm_cli_namespace_lock;
46 CFS_LIST_HEAD(ldlm_cli_namespace_list);
48 cfs_proc_dir_entry_t *ldlm_type_proc_dir = NULL;
49 cfs_proc_dir_entry_t *ldlm_ns_proc_dir = NULL;
50 cfs_proc_dir_entry_t *ldlm_svc_proc_dir = NULL;
53 static int ldlm_proc_dump_ns(struct file *file, const char *buffer,
54 unsigned long count, void *data)
56 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
57 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
61 int ldlm_proc_setup(void)
64 struct lprocfs_vars list[] = {
65 { "dump_namespaces", NULL, ldlm_proc_dump_ns, NULL },
68 LASSERT(ldlm_ns_proc_dir == NULL);
70 ldlm_type_proc_dir = lprocfs_register(OBD_LDLM_DEVICENAME,
73 if (IS_ERR(ldlm_type_proc_dir)) {
74 CERROR("LProcFS failed in ldlm-init\n");
75 rc = PTR_ERR(ldlm_type_proc_dir);
79 ldlm_ns_proc_dir = lprocfs_register("namespaces",
82 if (IS_ERR(ldlm_ns_proc_dir)) {
83 CERROR("LProcFS failed in ldlm-init\n");
84 rc = PTR_ERR(ldlm_ns_proc_dir);
88 ldlm_svc_proc_dir = lprocfs_register("services",
91 if (IS_ERR(ldlm_svc_proc_dir)) {
92 CERROR("LProcFS failed in ldlm-init\n");
93 rc = PTR_ERR(ldlm_svc_proc_dir);
97 rc = lprocfs_add_vars(ldlm_type_proc_dir, list, NULL);
102 lprocfs_remove(&ldlm_ns_proc_dir);
104 lprocfs_remove(&ldlm_type_proc_dir);
106 ldlm_svc_proc_dir = NULL;
110 void ldlm_proc_cleanup(void)
112 if (ldlm_svc_proc_dir)
113 lprocfs_remove(&ldlm_svc_proc_dir);
115 if (ldlm_ns_proc_dir)
116 lprocfs_remove(&ldlm_ns_proc_dir);
118 if (ldlm_type_proc_dir)
119 lprocfs_remove(&ldlm_type_proc_dir);
122 static int lprocfs_rd_lru_size(char *page, char **start, off_t off,
123 int count, int *eof, void *data)
125 struct ldlm_namespace *ns = data;
126 __u32 *nr = &ns->ns_max_unused;
128 if (ns_connect_lru_resize(ns))
129 nr = &ns->ns_nr_unused;
130 return lprocfs_rd_uint(page, start, off, count, eof, nr);
133 static int lprocfs_wr_lru_size(struct file *file, const char *buffer,
134 unsigned long count, void *data)
136 struct ldlm_namespace *ns = data;
137 char dummy[MAX_STRING_SIZE + 1], *end;
141 dummy[MAX_STRING_SIZE] = '\0';
142 if (copy_from_user(dummy, buffer, MAX_STRING_SIZE))
145 if (strncmp(dummy, "clear", 5) == 0) {
147 "dropping all unused locks from namespace %s\n",
149 if (ns_connect_lru_resize(ns)) {
150 int canceled, unused = ns->ns_nr_unused;
152 /* Try to cancel all @ns_nr_unused locks. */
153 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, LDLM_CANCEL_PASSED);
165 ns->ns_max_unused = tmp;
170 tmp = simple_strtoul(dummy, &end, 0);
172 CERROR("invalid value written\n");
175 lru_resize = (tmp == 0);
177 if (ns_connect_lru_resize(ns)) {
179 ns->ns_max_unused = (unsigned int)tmp;
181 if (tmp > ns->ns_nr_unused)
182 tmp = ns->ns_nr_unused;
183 tmp = ns->ns_nr_unused - tmp;
186 "changing namespace %s unused locks from %u to %u\n",
187 ns->ns_name, ns->ns_nr_unused, (unsigned int)tmp);
188 ldlm_cancel_lru(ns, tmp, LDLM_ASYNC, LDLM_CANCEL_PASSED);
192 "disable lru_resize for namespace %s\n",
194 ns->ns_connect_flags &= ~OBD_CONNECT_LRU_RESIZE;
198 "changing namespace %s max_unused from %u to %u\n",
199 ns->ns_name, ns->ns_max_unused, (unsigned int)tmp);
200 ns->ns_max_unused = (unsigned int)tmp;
201 ldlm_cancel_lru(ns, 0, LDLM_ASYNC, LDLM_CANCEL_PASSED);
203 /* Make sure that originally lru resize was supported before
204 * turning it on here. */
206 (ns->ns_orig_connect_flags & OBD_CONNECT_LRU_RESIZE)) {
208 "enable lru_resize for namespace %s\n",
210 ns->ns_connect_flags |= OBD_CONNECT_LRU_RESIZE;
217 void ldlm_proc_namespace(struct ldlm_namespace *ns)
219 struct lprocfs_vars lock_vars[2];
220 char lock_name[MAX_STRING_SIZE + 1];
223 LASSERT(ns->ns_name != NULL);
225 lock_name[MAX_STRING_SIZE] = '\0';
227 memset(lock_vars, 0, sizeof(lock_vars));
228 lock_vars[0].name = lock_name;
230 snprintf(lock_name, MAX_STRING_SIZE, "%s/resource_count", ns->ns_name);
231 lock_vars[0].data = &ns->ns_refcount;
232 lock_vars[0].read_fptr = lprocfs_rd_atomic;
233 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
235 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_count", ns->ns_name);
236 lock_vars[0].data = &ns->ns_locks;
237 lock_vars[0].read_fptr = lprocfs_rd_atomic;
238 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
240 if (ns_is_client(ns)) {
241 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_unused_count",
243 lock_vars[0].data = &ns->ns_nr_unused;
244 lock_vars[0].read_fptr = lprocfs_rd_uint;
245 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
247 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_size",
249 lock_vars[0].data = ns;
250 lock_vars[0].read_fptr = lprocfs_rd_lru_size;
251 lock_vars[0].write_fptr = lprocfs_wr_lru_size;
252 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
254 snprintf(lock_name, MAX_STRING_SIZE, "%s/shrink_thumb",
256 lock_vars[0].data = ns;
257 lock_vars[0].read_fptr = lprocfs_rd_uint;
258 lock_vars[0].write_fptr = lprocfs_wr_uint;
259 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
261 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_max_age",
263 lock_vars[0].data = &ns->ns_max_age;
264 lock_vars[0].read_fptr = lprocfs_rd_uint;
265 lock_vars[0].write_fptr = lprocfs_wr_uint;
266 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
268 snprintf(lock_name, MAX_STRING_SIZE, "%s/ctime_age_limit",
270 lock_vars[0].data = &ns->ns_ctime_age_limit;
271 lock_vars[0].read_fptr = lprocfs_rd_uint;
272 lock_vars[0].write_fptr = lprocfs_wr_uint;
273 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
275 snprintf(lock_name, MAX_STRING_SIZE, "%s/max_nolock_bytes",
277 lock_vars[0].data = &ns->ns_max_nolock_size;
278 lock_vars[0].read_fptr = lprocfs_rd_uint;
279 lock_vars[0].write_fptr = lprocfs_wr_uint;
280 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
282 snprintf(lock_name, MAX_STRING_SIZE, "%s/contention_seconds",
284 lock_vars[0].data = &ns->ns_contention_time;
285 lock_vars[0].read_fptr = lprocfs_rd_uint;
286 lock_vars[0].write_fptr = lprocfs_wr_uint;
287 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
289 snprintf(lock_name, MAX_STRING_SIZE, "%s/contended_locks",
291 lock_vars[0].data = &ns->ns_contended_locks;
292 lock_vars[0].read_fptr = lprocfs_rd_uint;
293 lock_vars[0].write_fptr = lprocfs_wr_uint;
294 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
297 #undef MAX_STRING_SIZE
299 #define ldlm_proc_namespace(ns) do {} while (0)
302 struct ldlm_namespace *ldlm_namespace_new(struct obd_device *obd, char *name,
303 ldlm_side_t client, ldlm_appetite_t apt)
305 struct ldlm_namespace *ns = NULL;
306 struct list_head *bucket;
307 int rc, idx, namelen;
312 CERROR("ldlm_get_ref failed: %d\n", rc);
320 OBD_VMALLOC(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
324 ns->ns_shrink_thumb = LDLM_LOCK_SHRINK_THUMB;
325 ns->ns_appetite = apt;
327 LASSERT(obd != NULL);
330 namelen = strlen(name);
331 OBD_ALLOC(ns->ns_name, namelen + 1);
333 GOTO(out_hash, NULL);
335 strcpy(ns->ns_name, name);
337 CFS_INIT_LIST_HEAD(&ns->ns_root_list);
338 CFS_INIT_LIST_HEAD(&ns->ns_list_chain);
340 ns->ns_client = client;
341 spin_lock_init(&ns->ns_hash_lock);
342 atomic_set(&ns->ns_locks, 0);
343 ns->ns_resources = 0;
344 cfs_waitq_init(&ns->ns_waitq);
345 ns->ns_max_nolock_size = NS_DEFAULT_MAX_NOLOCK_BYTES;
346 ns->ns_contention_time = NS_DEFAULT_CONTENTION_SECONDS;
347 ns->ns_contended_locks = NS_DEFAULT_CONTENDED_LOCKS;
349 for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
351 CFS_INIT_LIST_HEAD(bucket);
353 CFS_INIT_LIST_HEAD(&ns->ns_unused_list);
354 ns->ns_nr_unused = 0;
355 ns->ns_max_unused = LDLM_DEFAULT_LRU_SIZE;
356 ns->ns_max_age = LDLM_DEFAULT_MAX_ALIVE;
357 ns->ns_ctime_age_limit = LDLM_CTIME_AGE_LIMIT;
358 spin_lock_init(&ns->ns_unused_lock);
359 ns->ns_orig_connect_flags = 0;
360 ns->ns_connect_flags = 0;
361 ldlm_proc_namespace(ns);
363 idx = atomic_read(ldlm_namespace_nr(client));
364 rc = ldlm_pool_init(&ns->ns_pool, ns, idx, client);
366 CERROR("Can't initialize lock pool, rc %d\n", rc);
370 ldlm_namespace_register(ns, client);
373 ldlm_namespace_cleanup(ns, 0);
374 OBD_FREE(ns->ns_name, namelen + 1);
376 OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
384 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
386 /* If flags contains FL_LOCAL_ONLY, don't try to tell the server, just cleanup.
387 * This is currently only used for recovery, and we make certain assumptions
388 * as a result--notably, that we shouldn't cancel locks with refs. -phil
390 * Called with the ns_lock held. */
391 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
394 struct list_head *tmp;
395 int rc = 0, client = ns_is_client(res->lr_namespace);
396 int local_only = (flags & LDLM_FL_LOCAL_ONLY);
401 struct ldlm_lock *lock = NULL;
403 /* first, we look for non-cleaned-yet lock
404 * all cleaned locks are marked by CLEANED flag */
406 list_for_each(tmp, q) {
407 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
408 if (lock->l_flags & LDLM_FL_CLEANED) {
413 lock->l_flags |= LDLM_FL_CLEANED;
422 /* Set CBPENDING so nothing in the cancellation path
423 * can match this lock */
424 lock->l_flags |= LDLM_FL_CBPENDING;
425 lock->l_flags |= LDLM_FL_FAILED;
426 lock->l_flags |= flags;
428 /* ... without sending a CANCEL message for local_only. */
430 lock->l_flags |= LDLM_FL_LOCAL_ONLY;
432 if (local_only && (lock->l_readers || lock->l_writers)) {
433 /* This is a little bit gross, but much better than the
434 * alternative: pretend that we got a blocking AST from
435 * the server, so that when the lock is decref'd, it
436 * will go away ... */
438 LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
439 if (lock->l_completion_ast)
440 lock->l_completion_ast(lock, 0, NULL);
446 struct lustre_handle lockh;
449 ldlm_lock2handle(lock, &lockh);
450 rc = ldlm_cli_cancel(&lockh);
452 CERROR("ldlm_cli_cancel: %d\n", rc);
454 ldlm_resource_unlink_lock(lock);
456 LDLM_DEBUG(lock, "Freeing a lock still held by a "
458 ldlm_lock_destroy(lock);
466 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int flags)
468 struct list_head *tmp;
472 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
476 for (i = 0; i < RES_HASH_SIZE; i++) {
477 spin_lock(&ns->ns_hash_lock);
478 tmp = ns->ns_hash[i].next;
479 while (tmp != &(ns->ns_hash[i])) {
480 struct ldlm_resource *res;
481 res = list_entry(tmp, struct ldlm_resource, lr_hash);
482 ldlm_resource_getref(res);
483 spin_unlock(&ns->ns_hash_lock);
485 cleanup_resource(res, &res->lr_granted, flags);
486 cleanup_resource(res, &res->lr_converting, flags);
487 cleanup_resource(res, &res->lr_waiting, flags);
489 spin_lock(&ns->ns_hash_lock);
492 /* XXX: former stuff caused issues in case of race
493 * between ldlm_namespace_cleanup() and lockd() when
494 * client gets blocking ast when lock gets distracted by
495 * server. This is 1_4 branch solution, let's see how
497 if (!ldlm_resource_putref_locked(res))
499 "Namespace %s resource refcount nonzero "
500 "(%d) after lock cleanup; forcing cleanup.\n",
501 ns->ns_name, atomic_read(&res->lr_refcount));
503 spin_unlock(&ns->ns_hash_lock);
509 static int __ldlm_namespace_free(struct ldlm_namespace *ns, int force)
513 /* At shutdown time, don't call the cancellation callback */
514 ldlm_namespace_cleanup(ns, force ? LDLM_FL_LOCAL_ONLY : 0);
516 if (ns->ns_refcount > 0) {
517 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
520 "dlm namespace %s free waiting on refcount %d\n",
521 ns->ns_name, ns->ns_refcount);
524 lwi = LWI_TIMEOUT(obd_timeout * HZ / 4, NULL, NULL);
526 rc = l_wait_event(ns->ns_waitq,
527 ns->ns_refcount == 0, &lwi);
529 /* Forced cleanups should be able to reclaim all references,
530 * so it's safe to wait forever... we can't leak locks... */
531 if (force && rc == -ETIMEDOUT) {
532 LCONSOLE_ERROR("Forced cleanup waiting for %s "
533 "namespace with %d resources in use, "
534 "(rc=%d)\n", ns->ns_name,
535 ns->ns_refcount, rc);
536 GOTO(force_wait, rc);
539 if (ns->ns_refcount) {
540 LCONSOLE_ERROR("Cleanup waiting for %s namespace "
541 "with %d resources in use, (rc=%d)\n",
543 ns->ns_refcount, rc);
544 RETURN(ELDLM_NAMESPACE_EXISTS);
547 "dlm namespace %s free done waiting\n", ns->ns_name);
554 * Performs various cleanups for passed \a ns to make it drop refc and be ready
555 * for freeing. Waits for refc == 0.
557 * The following is done:
558 * (0) Unregister \a ns from its list to make inaccessible for potential users
559 * like pools thread and others;
560 * (1) Clear all locks in \a ns.
562 void ldlm_namespace_free_prior(struct ldlm_namespace *ns,
563 struct obd_import *imp,
574 * Make sure that nobody can find this ns in its list.
576 ldlm_namespace_unregister(ns, ns->ns_client);
579 * Can fail with -EINTR when force == 0 in which case try harder.
581 rc = __ldlm_namespace_free(ns, force);
582 if (rc != ELDLM_OK) {
584 ptlrpc_disconnect_import(imp, 0);
585 ptlrpc_invalidate_import(imp);
589 * With all requests dropped and the import inactive
590 * we are gaurenteed all reference will be dropped.
592 rc = __ldlm_namespace_free(ns, 1);
599 * Performs freeing memory structures related to \a ns. This is only done when
600 * ldlm_namespce_free_prior() successfully removed all resources referencing
601 * \a ns and its refc == 0.
603 void ldlm_namespace_free_post(struct ldlm_namespace *ns)
612 * Fini pool _before_ parent proc dir is removed. This is important as
613 * ldlm_pool_fini() removes own proc dir which is child to @dir. Removing
614 * it after @dir may cause oops.
616 ldlm_pool_fini(&ns->ns_pool);
620 struct proc_dir_entry *dir;
621 dir = lprocfs_srch(ldlm_ns_proc_dir, ns->ns_name);
623 CERROR("dlm namespace %s has no procfs dir?\n",
626 lprocfs_remove(&dir);
631 OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
632 OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
635 * Namespace \a ns should be not on list in this time, otherwise this
636 * will cause issues realted to using freed \a ns in pools thread.
638 LASSERT(list_empty(&ns->ns_list_chain));
645 /* Cleanup the resource, and free namespace.
648 * proc1: destroy import
649 * class_disconnect_export(grab cl_sem) ->
650 * -> ldlm_namespace_free ->
651 * -> lprocfs_remove(grab _lprocfs_lock).
652 * proc2: read proc info
653 * lprocfs_fops_read(grab _lprocfs_lock) ->
654 * -> osc_rd_active, etc(grab cl_sem).
656 * So that I have to split the ldlm_namespace_free into two parts - the first
657 * part ldlm_namespace_free_prior is used to cleanup the resource which is
658 * being used; the 2nd part ldlm_namespace_free_post is used to unregister the
659 * lprocfs entries, and then free memory. It will be called w/o cli->cl_sem
662 void ldlm_namespace_free(struct ldlm_namespace *ns,
663 struct obd_import *imp,
666 ldlm_namespace_free_prior(ns, imp, force);
667 ldlm_namespace_free_post(ns);
671 void ldlm_namespace_get_locked(struct ldlm_namespace *ns)
673 LASSERT(ns->ns_refcount >= 0);
677 void ldlm_namespace_get(struct ldlm_namespace *ns)
679 spin_lock(&ns->ns_hash_lock);
680 ldlm_namespace_get_locked(ns);
681 spin_unlock(&ns->ns_hash_lock);
684 void ldlm_namespace_put_locked(struct ldlm_namespace *ns, int wakeup)
686 LASSERT(ns->ns_refcount > 0);
688 if (ns->ns_refcount == 0 && wakeup)
689 wake_up(&ns->ns_waitq);
692 void ldlm_namespace_put(struct ldlm_namespace *ns, int wakeup)
694 spin_lock(&ns->ns_hash_lock);
695 ldlm_namespace_put_locked(ns, wakeup);
696 spin_unlock(&ns->ns_hash_lock);
699 /* Register @ns in the list of namespaces */
700 void ldlm_namespace_register(struct ldlm_namespace *ns, ldlm_side_t client)
702 mutex_down(ldlm_namespace_lock(client));
703 LASSERT(list_empty(&ns->ns_list_chain));
704 list_add(&ns->ns_list_chain, ldlm_namespace_list(client));
705 atomic_inc(ldlm_namespace_nr(client));
706 mutex_up(ldlm_namespace_lock(client));
709 /* Unregister @ns from the list of namespaces */
710 void ldlm_namespace_unregister(struct ldlm_namespace *ns, ldlm_side_t client)
712 mutex_down(ldlm_namespace_lock(client));
713 LASSERT(!list_empty(&ns->ns_list_chain));
715 * Some asserts and possibly other parts of code still using
716 * list_empty(&ns->ns_list_chain). This is why it is important
717 * to use list_del_init() here.
719 list_del_init(&ns->ns_list_chain);
720 atomic_dec(ldlm_namespace_nr(client));
721 mutex_up(ldlm_namespace_lock(client));
724 /* Should be called under ldlm_namespace_lock(client) taken */
725 void ldlm_namespace_move_locked(struct ldlm_namespace *ns, ldlm_side_t client)
727 LASSERT(!list_empty(&ns->ns_list_chain));
728 LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
729 list_move_tail(&ns->ns_list_chain, ldlm_namespace_list(client));
732 /* Should be called under ldlm_namespace_lock(client) taken */
733 struct ldlm_namespace *ldlm_namespace_first_locked(ldlm_side_t client)
735 LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
736 LASSERT(!list_empty(ldlm_namespace_list(client)));
737 return container_of(ldlm_namespace_list(client)->next,
738 struct ldlm_namespace, ns_list_chain);
740 static __u32 ldlm_hash_fn(struct ldlm_resource *parent,
741 const struct ldlm_res_id *name)
746 for (i = 0; i < RES_NAME_SIZE; i++)
747 hash += name->name[i];
749 hash += (__u32)((unsigned long)parent >> 4);
751 return (hash & RES_HASH_MASK);
754 static struct ldlm_resource *ldlm_resource_new(void)
756 struct ldlm_resource *res;
759 OBD_SLAB_ALLOC(res, ldlm_resource_slab, CFS_ALLOC_IO, sizeof *res);
763 memset(res, 0, sizeof(*res));
765 CFS_INIT_LIST_HEAD(&res->lr_children);
766 CFS_INIT_LIST_HEAD(&res->lr_childof);
767 CFS_INIT_LIST_HEAD(&res->lr_granted);
768 CFS_INIT_LIST_HEAD(&res->lr_converting);
769 CFS_INIT_LIST_HEAD(&res->lr_waiting);
771 /* initialize interval trees for each lock mode*/
772 for (idx = 0; idx < LCK_MODE_NUM; idx++) {
773 res->lr_itree[idx].lit_size = 0;
774 res->lr_itree[idx].lit_mode = 1 << idx;
775 res->lr_itree[idx].lit_root = NULL;
778 atomic_set(&res->lr_refcount, 1);
779 spin_lock_init(&res->lr_lock);
781 /* one who creates the resource must unlock
782 * the semaphore after lvb initialization */
783 init_MUTEX_LOCKED(&res->lr_lvb_sem);
788 /* must be called with hash lock held */
789 static struct ldlm_resource *
790 ldlm_resource_find(struct ldlm_namespace *ns, const struct ldlm_res_id *name,
793 struct list_head *bucket, *tmp;
794 struct ldlm_resource *res;
796 LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
797 bucket = ns->ns_hash + hash;
799 list_for_each(tmp, bucket) {
800 res = list_entry(tmp, struct ldlm_resource, lr_hash);
801 if (memcmp(&res->lr_name, name, sizeof(res->lr_name)) == 0)
808 /* Args: locked namespace
809 * Returns: newly-allocated, referenced, unlocked resource */
810 static struct ldlm_resource *
811 ldlm_resource_add(struct ldlm_namespace *ns, struct ldlm_resource *parent,
812 const struct ldlm_res_id *name, __u32 hash, ldlm_type_t type)
814 struct list_head *bucket;
815 struct ldlm_resource *res, *old_res;
818 LASSERTF(type >= LDLM_MIN_TYPE && type < LDLM_MAX_TYPE,
821 res = ldlm_resource_new();
825 res->lr_name = *name;
826 res->lr_namespace = ns;
828 res->lr_most_restr = LCK_NL;
830 spin_lock(&ns->ns_hash_lock);
831 old_res = ldlm_resource_find(ns, name, hash);
833 /* someone won the race and added the resource before */
834 ldlm_resource_getref(old_res);
835 spin_unlock(&ns->ns_hash_lock);
836 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
837 /* synchronize WRT resource creation */
838 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
839 down(&old_res->lr_lvb_sem);
840 up(&old_res->lr_lvb_sem);
845 /* we won! let's add the resource */
846 bucket = ns->ns_hash + hash;
847 list_add(&res->lr_hash, bucket);
849 ldlm_namespace_get_locked(ns);
851 if (parent == NULL) {
852 list_add(&res->lr_childof, &ns->ns_root_list);
854 res->lr_parent = parent;
855 list_add(&res->lr_childof, &parent->lr_children);
857 spin_unlock(&ns->ns_hash_lock);
859 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
862 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CREATE_RESOURCE, 2);
863 rc = ns->ns_lvbo->lvbo_init(res);
865 CERROR("lvbo_init failed for resource "
866 LPU64": rc %d\n", name->name[0], rc);
867 /* we create resource with locked lr_lvb_sem */
868 up(&res->lr_lvb_sem);
874 /* Args: unlocked namespace
875 * Locks: takes and releases ns->ns_lock and res->lr_lock
876 * Returns: referenced, unlocked ldlm_resource or NULL */
877 struct ldlm_resource *
878 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
879 const struct ldlm_res_id *name, ldlm_type_t type, int create)
881 __u32 hash = ldlm_hash_fn(parent, name);
882 struct ldlm_resource *res = NULL;
886 LASSERT(ns->ns_hash != NULL);
887 LASSERT(name->name[0] != 0);
889 spin_lock(&ns->ns_hash_lock);
890 res = ldlm_resource_find(ns, name, hash);
892 ldlm_resource_getref(res);
893 spin_unlock(&ns->ns_hash_lock);
894 /* synchronize WRT resource creation */
895 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
896 down(&res->lr_lvb_sem);
897 up(&res->lr_lvb_sem);
901 spin_unlock(&ns->ns_hash_lock);
906 res = ldlm_resource_add(ns, parent, name, hash, type);
910 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
912 LASSERT(res != NULL);
913 LASSERT(res != LP_POISON);
914 atomic_inc(&res->lr_refcount);
915 CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
916 atomic_read(&res->lr_refcount));
920 void __ldlm_resource_putref_final(struct ldlm_resource *res)
922 struct ldlm_namespace *ns = res->lr_namespace;
924 LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
926 if (!list_empty(&res->lr_granted)) {
927 ldlm_resource_dump(D_ERROR, res);
931 if (!list_empty(&res->lr_converting)) {
932 ldlm_resource_dump(D_ERROR, res);
936 if (!list_empty(&res->lr_waiting)) {
937 ldlm_resource_dump(D_ERROR, res);
941 if (!list_empty(&res->lr_children)) {
942 ldlm_resource_dump(D_ERROR, res);
946 /* Pass 0 here to not wake ->ns_waitq up yet, we will do it few
947 * lines below when all children are freed. */
948 ldlm_namespace_put_locked(ns, 0);
949 list_del_init(&res->lr_hash);
950 list_del_init(&res->lr_childof);
953 if (ns->ns_resources == 0)
954 wake_up(&ns->ns_waitq);
957 /* Returns 1 if the resource was freed, 0 if it remains. */
958 int ldlm_resource_putref(struct ldlm_resource *res)
960 struct ldlm_namespace *ns = res->lr_namespace;
964 CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
965 atomic_read(&res->lr_refcount) - 1);
966 LASSERTF(atomic_read(&res->lr_refcount) > 0, "%d",
967 atomic_read(&res->lr_refcount));
968 LASSERTF(atomic_read(&res->lr_refcount) < LI_POISON, "%d",
969 atomic_read(&res->lr_refcount));
971 if (atomic_dec_and_lock(&res->lr_refcount, &ns->ns_hash_lock)) {
972 __ldlm_resource_putref_final(res);
973 spin_unlock(&ns->ns_hash_lock);
974 if (res->lr_lvb_data)
975 OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
976 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
983 /* Returns 1 if the resource was freed, 0 if it remains. */
984 int ldlm_resource_putref_locked(struct ldlm_resource *res)
989 CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
990 atomic_read(&res->lr_refcount) - 1);
991 LASSERT(atomic_read(&res->lr_refcount) > 0);
992 LASSERT(atomic_read(&res->lr_refcount) < LI_POISON);
994 LASSERT(atomic_read(&res->lr_refcount) >= 0);
995 if (atomic_dec_and_test(&res->lr_refcount)) {
996 __ldlm_resource_putref_final(res);
997 if (res->lr_lvb_data)
998 OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
999 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
1006 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
1007 struct ldlm_lock *lock)
1009 check_res_locked(res);
1011 ldlm_resource_dump(D_INFO, res);
1012 CDEBUG(D_INFO, "About to add this lock:\n");
1013 ldlm_lock_dump(D_INFO, lock, 0);
1015 if (lock->l_destroyed) {
1016 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
1020 LASSERT(list_empty(&lock->l_res_link));
1022 list_add_tail(&lock->l_res_link, head);
1025 void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
1026 struct ldlm_lock *new)
1028 struct ldlm_resource *res = original->l_resource;
1030 check_res_locked(res);
1032 ldlm_resource_dump(D_OTHER, res);
1033 CDEBUG(D_OTHER, "About to insert this lock after %p:\n", original);
1034 ldlm_lock_dump(D_OTHER, new, 0);
1036 if (new->l_destroyed) {
1037 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
1041 LASSERT(list_empty(&new->l_res_link));
1043 list_add(&new->l_res_link, &original->l_res_link);
1047 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
1049 int type = lock->l_resource->lr_type;
1051 check_res_locked(lock->l_resource);
1052 if (type == LDLM_IBITS || type == LDLM_PLAIN)
1053 ldlm_unlink_lock_skiplist(lock);
1054 else if (type == LDLM_EXTENT)
1055 ldlm_extent_unlink_lock(lock);
1056 list_del_init(&lock->l_res_link);
1059 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
1061 desc->lr_type = res->lr_type;
1062 desc->lr_name = res->lr_name;
1065 void ldlm_dump_all_namespaces(ldlm_side_t client, int level)
1067 struct list_head *tmp;
1069 if (!((libcfs_debug | D_ERROR) & level))
1072 mutex_down(ldlm_namespace_lock(client));
1074 list_for_each(tmp, ldlm_namespace_list(client)) {
1075 struct ldlm_namespace *ns;
1076 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
1077 ldlm_namespace_dump(level, ns);
1080 mutex_up(ldlm_namespace_lock(client));
1083 void ldlm_namespace_dump(int level, struct ldlm_namespace *ns)
1085 struct list_head *tmp;
1087 if (!((libcfs_debug | D_ERROR) & level))
1090 CDEBUG(level, "--- Namespace: %s (rc: %d, side: %s)\n",
1091 ns->ns_name, ns->ns_refcount,
1092 ns_is_client(ns) ? "client" : "server");
1094 if (cfs_time_before(cfs_time_current(), ns->ns_next_dump))
1097 spin_lock(&ns->ns_hash_lock);
1098 tmp = ns->ns_root_list.next;
1099 while (tmp != &ns->ns_root_list) {
1100 struct ldlm_resource *res;
1101 res = list_entry(tmp, struct ldlm_resource, lr_childof);
1103 ldlm_resource_getref(res);
1104 spin_unlock(&ns->ns_hash_lock);
1107 ldlm_resource_dump(level, res);
1110 spin_lock(&ns->ns_hash_lock);
1112 ldlm_resource_putref_locked(res);
1114 ns->ns_next_dump = cfs_time_shift(10);
1115 spin_unlock(&ns->ns_hash_lock);
1118 void ldlm_resource_dump(int level, struct ldlm_resource *res)
1120 struct list_head *tmp;
1123 CLASSERT(RES_NAME_SIZE == 4);
1125 if (!((libcfs_debug | D_ERROR) & level))
1128 CDEBUG(level, "--- Resource: %p ("LPU64"/"LPU64"/"LPU64"/"LPU64
1129 ") (rc: %d)\n", res, res->lr_name.name[0], res->lr_name.name[1],
1130 res->lr_name.name[2], res->lr_name.name[3],
1131 atomic_read(&res->lr_refcount));
1133 if (!list_empty(&res->lr_granted)) {
1135 CDEBUG(level, "Granted locks:\n");
1136 list_for_each(tmp, &res->lr_granted) {
1137 struct ldlm_lock *lock;
1138 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1139 ldlm_lock_dump(level, lock, ++pos);
1142 if (!list_empty(&res->lr_converting)) {
1144 CDEBUG(level, "Converting locks:\n");
1145 list_for_each(tmp, &res->lr_converting) {
1146 struct ldlm_lock *lock;
1147 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1148 ldlm_lock_dump(level, lock, ++pos);
1151 if (!list_empty(&res->lr_waiting)) {
1153 CDEBUG(level, "Waiting locks:\n");
1154 list_for_each(tmp, &res->lr_waiting) {
1155 struct ldlm_lock *lock;
1156 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1157 ldlm_lock_dump(level, lock, ++pos);